Search in sources :

Example 6 with SQLCheckConstraintBuilder

use of org.apache.hadoop.hive.metastore.client.builder.SQLCheckConstraintBuilder in project hive by apache.

the class TestCheckConstraint method inOtherCatalog.

@Test
public void inOtherCatalog() throws TException {
    String constraintName = "occc";
    // Table in non 'hive' catalog
    List<SQLCheckConstraint> cc = new SQLCheckConstraintBuilder().onTable(testTables[2]).addColumn("col1").setConstraintName(constraintName).setCheckExpression("like s%").build(metaStore.getConf());
    client.addCheckConstraint(cc);
    CheckConstraintsRequest rqst = new CheckConstraintsRequest(testTables[2].getCatName(), testTables[2].getDbName(), testTables[2].getTableName());
    List<SQLCheckConstraint> fetched = client.getCheckConstraints(rqst);
    Assert.assertEquals(cc, fetched);
    client.dropConstraint(testTables[2].getCatName(), testTables[2].getDbName(), testTables[2].getTableName(), constraintName);
    rqst = new CheckConstraintsRequest(testTables[2].getCatName(), testTables[2].getDbName(), testTables[2].getTableName());
    fetched = client.getCheckConstraints(rqst);
    Assert.assertTrue(fetched.isEmpty());
}
Also used : SQLCheckConstraintBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLCheckConstraintBuilder) SQLCheckConstraint(org.apache.hadoop.hive.metastore.api.SQLCheckConstraint) CheckConstraintsRequest(org.apache.hadoop.hive.metastore.api.CheckConstraintsRequest) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 7 with SQLCheckConstraintBuilder

use of org.apache.hadoop.hive.metastore.client.builder.SQLCheckConstraintBuilder 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)

Aggregations

SQLCheckConstraint (org.apache.hadoop.hive.metastore.api.SQLCheckConstraint)7 SQLCheckConstraintBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLCheckConstraintBuilder)7 Test (org.junit.Test)7 MetastoreCheckinTest (org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)6 CheckConstraintsRequest (org.apache.hadoop.hive.metastore.api.CheckConstraintsRequest)6 Table (org.apache.hadoop.hive.metastore.api.Table)6 TableBuilder (org.apache.hadoop.hive.metastore.client.builder.TableBuilder)3 SQLDefaultConstraint (org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint)2 SQLForeignKey (org.apache.hadoop.hive.metastore.api.SQLForeignKey)2 SQLNotNullConstraint (org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint)2 SQLPrimaryKey (org.apache.hadoop.hive.metastore.api.SQLPrimaryKey)2 SQLUniqueConstraint (org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint)2 SQLDefaultConstraintBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLDefaultConstraintBuilder)2 SQLForeignKeyBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLForeignKeyBuilder)2 SQLNotNullConstraintBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLNotNullConstraintBuilder)2 SQLPrimaryKeyBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLPrimaryKeyBuilder)2 SQLUniqueConstraintBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLUniqueConstraintBuilder)2 AllTableConstraintsRequest (org.apache.hadoop.hive.metastore.api.AllTableConstraintsRequest)1 DefaultConstraintsRequest (org.apache.hadoop.hive.metastore.api.DefaultConstraintsRequest)1 ForeignKeysRequest (org.apache.hadoop.hive.metastore.api.ForeignKeysRequest)1