Search in sources :

Example 26 with SQLUniqueConstraint

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

the class SQLUniqueConstraintBuilder method build.

public List<SQLUniqueConstraint> build(Configuration conf) throws MetaException {
    checkBuildable("unique_constraint", conf);
    List<SQLUniqueConstraint> uc = new ArrayList<>(columns.size());
    for (String column : columns) {
        SQLUniqueConstraint c = new SQLUniqueConstraint(catName, dbName, tableName, column, getNextSeq(), constraintName, enable, validate, rely);
        uc.add(c);
    }
    return uc;
}
Also used : SQLUniqueConstraint(org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint) ArrayList(java.util.ArrayList)

Example 27 with SQLUniqueConstraint

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

the class NonCatCallsWithCatalog method uniqueConstraint.

@Test
public void uniqueConstraint() throws TException {
    String constraintName = "ocuc";
    // Table in non 'hive' catalog
    List<SQLUniqueConstraint> uc = new SQLUniqueConstraintBuilder().onTable(testTables[2]).addColumn("test_col1").setConstraintName(constraintName).build(conf);
    client.addUniqueConstraint(uc);
    UniqueConstraintsRequest rqst = new UniqueConstraintsRequest(testTables[2].getCatName(), testTables[2].getDbName(), testTables[2].getTableName());
    List<SQLUniqueConstraint> fetched = client.getUniqueConstraints(rqst);
    Assert.assertEquals(1, fetched.size());
    Assert.assertEquals(testTables[2].getDbName(), fetched.get(0).getTable_db());
    Assert.assertEquals(testTables[2].getTableName(), fetched.get(0).getTable_name());
    Assert.assertEquals("test_col1", fetched.get(0).getColumn_name());
    Assert.assertEquals(1, fetched.get(0).getKey_seq());
    Assert.assertEquals(constraintName, fetched.get(0).getUk_name());
    Assert.assertTrue(fetched.get(0).isEnable_cstr());
    Assert.assertFalse(fetched.get(0).isValidate_cstr());
    Assert.assertFalse(fetched.get(0).isRely_cstr());
    Assert.assertEquals(testTables[2].getCatName(), fetched.get(0).getCatName());
    Assert.assertEquals(expectedCatalog(), fetched.get(0).getCatName());
    client.dropConstraint(testTables[2].getDbName(), testTables[2].getTableName(), constraintName);
    rqst = new UniqueConstraintsRequest(testTables[2].getCatName(), testTables[2].getDbName(), testTables[2].getTableName());
    fetched = client.getUniqueConstraints(rqst);
    Assert.assertTrue(fetched.isEmpty());
}
Also used : SQLUniqueConstraint(org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint) UniqueConstraintsRequest(org.apache.hadoop.hive.metastore.api.UniqueConstraintsRequest) SQLUniqueConstraintBuilder(org.apache.hadoop.hive.metastore.client.builder.SQLUniqueConstraintBuilder) Test(org.junit.Test)

Example 28 with SQLUniqueConstraint

use of org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint 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 29 with SQLUniqueConstraint

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

the class TestUniqueConstraint method doubleAddUniqueConstraint.

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

Example 30 with SQLUniqueConstraint

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

the class TestUniqueConstraint method getNoSuchCatalog.

@Test
public void getNoSuchCatalog() throws TException {
    UniqueConstraintsRequest rqst = new UniqueConstraintsRequest("nosuch", testTables[0].getDbName(), testTables[0].getTableName());
    List<SQLUniqueConstraint> uc = client.getUniqueConstraints(rqst);
    Assert.assertTrue(uc.isEmpty());
}
Also used : UniqueConstraintsRequest(org.apache.hadoop.hive.metastore.api.UniqueConstraintsRequest) SQLUniqueConstraint(org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Aggregations

SQLUniqueConstraint (org.apache.hadoop.hive.metastore.api.SQLUniqueConstraint)32 SQLNotNullConstraint (org.apache.hadoop.hive.metastore.api.SQLNotNullConstraint)16 SQLCheckConstraint (org.apache.hadoop.hive.metastore.api.SQLCheckConstraint)15 SQLDefaultConstraint (org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint)15 Test (org.junit.Test)14 SQLPrimaryKey (org.apache.hadoop.hive.metastore.api.SQLPrimaryKey)13 ArrayList (java.util.ArrayList)11 MetastoreCheckinTest (org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)11 SQLForeignKey (org.apache.hadoop.hive.metastore.api.SQLForeignKey)11 UniqueConstraintsRequest (org.apache.hadoop.hive.metastore.api.UniqueConstraintsRequest)11 SQLUniqueConstraintBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLUniqueConstraintBuilder)10 Table (org.apache.hadoop.hive.metastore.api.Table)7 MConstraint (org.apache.hadoop.hive.metastore.model.MConstraint)5 InvalidObjectException (org.apache.hadoop.hive.metastore.api.InvalidObjectException)4 MetaException (org.apache.hadoop.hive.metastore.api.MetaException)3 SQLDefaultConstraintBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLDefaultConstraintBuilder)3 SQLPrimaryKeyBuilder (org.apache.hadoop.hive.metastore.client.builder.SQLPrimaryKeyBuilder)3 TableBuilder (org.apache.hadoop.hive.metastore.client.builder.TableBuilder)3 AddUniqueConstraintMessage (org.apache.hadoop.hive.metastore.messaging.AddUniqueConstraintMessage)3 Table (org.apache.hadoop.hive.ql.metadata.Table)3