Search in sources :

Example 21 with SQLDefaultConstraint

use of org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint in project hive by apache.

the class TestGetAllTableConstraints method allConstraintsPresent.

/**
 * Test to verify all constraint are present in table
 * @throws TException
 */
@Test
public void allConstraintsPresent() throws TException {
    Table table = testTables[0];
    Table parentTable = testTables[1];
    SQLAllTableConstraints expected = new SQLAllTableConstraints();
    // Set col1 as primary key Constraint in default catalog and database
    List<SQLPrimaryKey> pk = new SQLPrimaryKeyBuilder().onTable(table).addColumn("col1").setConstraintName("col1_pk").build(metaStore.getConf());
    client.addPrimaryKey(pk);
    expected.setPrimaryKeys(pk);
    // Set col2 with Unique Constraint in default catalog and database
    String uniqueConstraintName = "col2_unique";
    List<SQLUniqueConstraint> uc = new SQLUniqueConstraintBuilder().onTable(table).addColumn("col2").setConstraintName("col2_unique").build(metaStore.getConf());
    client.addUniqueConstraint(uc);
    expected.setUniqueConstraints(uc);
    // Set col3 with default Constraint in default catalog and database
    List<SQLDefaultConstraint> dv = new SQLDefaultConstraintBuilder().onTable(table).addColumn("col3").setConstraintName("col3_default").setDefaultVal(false).build(metaStore.getConf());
    client.addDefaultConstraint(dv);
    expected.setDefaultConstraints(dv);
    // Set col3 with not null constraint in default catalog and database;
    List<SQLNotNullConstraint> nn = new SQLNotNullConstraintBuilder().onTable(table).addColumn("col3").setConstraintName("col3_not_null").build(metaStore.getConf());
    client.addNotNullConstraint(nn);
    expected.setNotNullConstraints(nn);
    // Set col2 with not check constraint in default catalog and database;
    List<SQLCheckConstraint> cc = new SQLCheckConstraintBuilder().onTable(table).addColumn("col2").setConstraintName("col2_check").setCheckExpression("= 5").build(metaStore.getConf());
    client.addCheckConstraint(cc);
    expected.setCheckConstraints(cc);
    // Set col1 of parent table to PK and Set Col4 of table to FK
    List<SQLPrimaryKey> parentPk = new SQLPrimaryKeyBuilder().onTable(parentTable).addColumn("col1").setConstraintName("parentpk").build(metaStore.getConf());
    client.addPrimaryKey(parentPk);
    String fkConstraintName = "fk";
    List<SQLForeignKey> fk = new SQLForeignKeyBuilder().fromPrimaryKey(parentPk).onTable(table).setConstraintName(fkConstraintName).addColumn("col4").build(metaStore.getConf());
    client.addForeignKey(fk);
    expected.setForeignKeys(fk);
    // Fetch all constraints for the table in default catalog and database
    AllTableConstraintsRequest request = new AllTableConstraintsRequest(table.getDbName(), table.getTableName(), table.getCatName());
    SQLAllTableConstraints actual = client.getAllTableConstraints(request);
    Assert.assertEquals(expected, actual);
}
Also used : SQLPrimaryKeyBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLPrimaryKeyBuilder) SQLUniqueConstraint(org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint) SQLForeignKeyBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLForeignKeyBuilder) SQLPrimaryKey(org.apache.hadoop.hive.metastore.api.SQLPrimaryKey) SQLCheckConstraintBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLCheckConstraintBuilder) Table(org.apache.hadoop.hive.metastore.api.Table) SQLCheckConstraint(org.apache.hadoop.hive.metastore.api.SQLCheckConstraint) SQLForeignKey(org.apache.hadoop.hive.metastore.api.SQLForeignKey) SQLDefaultConstraint(org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint) SQLNotNullConstraint(org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint) SQLDefaultConstraintBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLDefaultConstraintBuilder) SQLNotNullConstraintBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLNotNullConstraintBuilder) SQLAllTableConstraints(org.apache.hadoop.hive.metastore.api.SQLAllTableConstraints) SQLUniqueConstraintBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLUniqueConstraintBuilder) AllTableConstraintsRequest(org.apache.hadoop.hive.metastore.api.AllTableConstraintsRequest) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 22 with SQLDefaultConstraint

use of org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint in project hive by apache.

the class TestDefaultConstraint method createTableWithConstraintsPkInOtherCatalog.

@Test
public void createTableWithConstraintsPkInOtherCatalog() throws TException {
    Table table = new TableBuilder().setTableName("table_in_other_catalog_with_constraints").inDb(inOtherCatalog).addCol("col1", "int").addCol("col2", "varchar(32)").build(metaStore.getConf());
    List<SQLDefaultConstraint> dv = new SQLDefaultConstraintBuilder().onTable(table).addColumn("col1").setDefaultVal(0).build(metaStore.getConf());
    client.createTableWithConstraints(table, null, null, null, null, dv, null);
    DefaultConstraintsRequest rqst = new DefaultConstraintsRequest(table.getCatName(), table.getDbName(), table.getTableName());
    List<SQLDefaultConstraint> fetched = client.getDefaultConstraints(rqst);
    dv.get(0).setDc_name(fetched.get(0).getDc_name());
    Assert.assertEquals(dv, fetched);
    client.dropConstraint(table.getCatName(), table.getDbName(), table.getTableName(), dv.get(0).getDc_name());
    rqst = new DefaultConstraintsRequest(table.getCatName(), table.getDbName(), table.getTableName());
    fetched = client.getDefaultConstraints(rqst);
    Assert.assertTrue(fetched.isEmpty());
}
Also used : DefaultConstraintsRequest(org.apache.hadoop.hive.metastore.api.DefaultConstraintsRequest) SQLDefaultConstraintBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLDefaultConstraintBuilder) Table(org.apache.hadoop.hive.metastore.api.Table) TableBuilder(org.apache.hadoop.hive.metastore.client.builder.TableBuilder) SQLDefaultConstraint(org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 23 with SQLDefaultConstraint

use of org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint in project hive by apache.

the class TestDefaultConstraint method getNoSuchTable.

@Test
public void getNoSuchTable() throws TException {
    DefaultConstraintsRequest rqst = new DefaultConstraintsRequest(DEFAULT_CATALOG_NAME, DEFAULT_DATABASE_NAME, "nosuch");
    List<SQLDefaultConstraint> dv = client.getDefaultConstraints(rqst);
    Assert.assertTrue(dv.isEmpty());
}
Also used : DefaultConstraintsRequest(org.apache.hadoop.hive.metastore.api.DefaultConstraintsRequest) SQLDefaultConstraint(org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 24 with SQLDefaultConstraint

use of org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint in project hive by apache.

the class TestDefaultConstraint method createTableWithConstraintsPk.

@Test
public void createTableWithConstraintsPk() throws TException {
    String constraintName = "ctwcdv";
    Table table = new TableBuilder().setTableName("table_with_constraints").addCol("col1", "int").addCol("col2", "varchar(32)").build(metaStore.getConf());
    List<SQLDefaultConstraint> dv = new SQLDefaultConstraintBuilder().onTable(table).addColumn("col1").setConstraintName(constraintName).setDefaultVal(0).build(metaStore.getConf());
    client.createTableWithConstraints(table, null, null, null, null, dv, null);
    DefaultConstraintsRequest rqst = new DefaultConstraintsRequest(table.getCatName(), table.getDbName(), table.getTableName());
    List<SQLDefaultConstraint> fetched = client.getDefaultConstraints(rqst);
    Assert.assertEquals(dv, fetched);
    client.dropConstraint(table.getCatName(), table.getDbName(), table.getTableName(), constraintName);
    rqst = new DefaultConstraintsRequest(table.getCatName(), table.getDbName(), table.getTableName());
    fetched = client.getDefaultConstraints(rqst);
    Assert.assertTrue(fetched.isEmpty());
}
Also used : DefaultConstraintsRequest(org.apache.hadoop.hive.metastore.api.DefaultConstraintsRequest) SQLDefaultConstraintBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLDefaultConstraintBuilder) Table(org.apache.hadoop.hive.metastore.api.Table) TableBuilder(org.apache.hadoop.hive.metastore.client.builder.TableBuilder) SQLDefaultConstraint(org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 25 with SQLDefaultConstraint

use of org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint in project hive by apache.

the class TestDefaultConstraint method doubleAddUniqueConstraint.

@Test
public void doubleAddUniqueConstraint() throws TException {
    Table table = testTables[0];
    // Make sure get on a table with no key returns empty list
    DefaultConstraintsRequest rqst = new DefaultConstraintsRequest(table.getCatName(), table.getDbName(), table.getTableName());
    List<SQLDefaultConstraint> fetched = client.getDefaultConstraints(rqst);
    Assert.assertTrue(fetched.isEmpty());
    // Single column unnamed primary key in default catalog and database
    List<SQLDefaultConstraint> dv = new SQLDefaultConstraintBuilder().onTable(table).addColumn("col1").setDefaultVal(0).build(metaStore.getConf());
    client.addDefaultConstraint(dv);
    try {
        dv = new SQLDefaultConstraintBuilder().onTable(table).addColumn("col2").setDefaultVal("this string intentionally left empty").build(metaStore.getConf());
        client.addDefaultConstraint(dv);
        Assert.fail();
    } catch (InvalidObjectException | TApplicationException e) {
    // NOP
    }
}
Also used : DefaultConstraintsRequest(org.apache.hadoop.hive.metastore.api.DefaultConstraintsRequest) SQLDefaultConstraintBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLDefaultConstraintBuilder) Table(org.apache.hadoop.hive.metastore.api.Table) InvalidObjectException(org.apache.hadoop.hive.metastore.api.InvalidObjectException) SQLDefaultConstraint(org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint) TApplicationException(org.apache.thrift.TApplicationException) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Aggregations

SQLDefaultConstraint (org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint)26 Test (org.junit.Test)13 SQLNotNullConstraint (org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint)12 SQLUniqueConstraint (org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint)12 DefaultConstraintsRequest (org.apache.hadoop.hive.metastore.api.DefaultConstraintsRequest)11 SQLCheckConstraint (org.apache.hadoop.hive.metastore.api.SQLCheckConstraint)11 MetastoreCheckinTest (org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)10 SQLDefaultConstraintBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLDefaultConstraintBuilder)9 ArrayList (java.util.ArrayList)8 SQLPrimaryKey (org.apache.hadoop.hive.metastore.api.SQLPrimaryKey)8 SQLForeignKey (org.apache.hadoop.hive.metastore.api.SQLForeignKey)7 Table (org.apache.hadoop.hive.metastore.api.Table)7 TableName (org.apache.hadoop.hive.common.TableName)3 AllTableConstraintsRequest (org.apache.hadoop.hive.metastore.api.AllTableConstraintsRequest)3 MetaException (org.apache.hadoop.hive.metastore.api.MetaException)3 SQLPrimaryKeyBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLPrimaryKeyBuilder)3 SQLUniqueConstraintBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLUniqueConstraintBuilder)3 AddDefaultConstraintMessage (org.apache.hadoop.hive.metastore.messaging.AddDefaultConstraintMessage)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2