use of org.apache.hadoop.hive.metastore.client.builder.SQLNotNullConstraintBuilder 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);
}
use of org.apache.hadoop.hive.metastore.client.builder.SQLNotNullConstraintBuilder in project hive by apache.
the class TestNotNullConstraint method doubleAddNotNullConstraint.
@Test
public void doubleAddNotNullConstraint() throws TException {
Table table = testTables[0];
// Make sure get on a table with no key returns empty list
NotNullConstraintsRequest rqst = new NotNullConstraintsRequest(table.getCatName(), table.getDbName(), table.getTableName());
List<SQLNotNullConstraint> fetched = client.getNotNullConstraints(rqst);
Assert.assertTrue(fetched.isEmpty());
// Single column unnamed primary key in default catalog and database
List<SQLNotNullConstraint> nn = new SQLNotNullConstraintBuilder().onTable(table).addColumn("col1").build(metaStore.getConf());
client.addNotNullConstraint(nn);
try {
nn = new SQLNotNullConstraintBuilder().onTable(table).addColumn("col2").build(metaStore.getConf());
client.addNotNullConstraint(nn);
Assert.fail();
} catch (InvalidObjectException | TApplicationException e) {
// NOP
}
}
use of org.apache.hadoop.hive.metastore.client.builder.SQLNotNullConstraintBuilder in project hive by apache.
the class TestNotNullConstraint 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<SQLNotNullConstraint> nn = new SQLNotNullConstraintBuilder().onTable(table).addColumn("col1").build(metaStore.getConf());
client.createTableWithConstraints(table, null, null, null, nn, null, null);
NotNullConstraintsRequest rqst = new NotNullConstraintsRequest(table.getCatName(), table.getDbName(), table.getTableName());
List<SQLNotNullConstraint> fetched = client.getNotNullConstraints(rqst);
nn.get(0).setNn_name(fetched.get(0).getNn_name());
Assert.assertEquals(nn, fetched);
client.dropConstraint(table.getCatName(), table.getDbName(), table.getTableName(), nn.get(0).getNn_name());
rqst = new NotNullConstraintsRequest(table.getCatName(), table.getDbName(), table.getTableName());
fetched = client.getNotNullConstraints(rqst);
Assert.assertTrue(fetched.isEmpty());
}
use of org.apache.hadoop.hive.metastore.client.builder.SQLNotNullConstraintBuilder in project hive by apache.
the class TestNotNullConstraint method createGetDrop.
@Test
public void createGetDrop() throws TException {
Table table = testTables[0];
// Make sure get on a table with no key returns empty list
NotNullConstraintsRequest rqst = new NotNullConstraintsRequest(table.getCatName(), table.getDbName(), table.getTableName());
List<SQLNotNullConstraint> fetched = client.getNotNullConstraints(rqst);
Assert.assertTrue(fetched.isEmpty());
// Single column unnamed primary key in default catalog and database
List<SQLNotNullConstraint> nn = new SQLNotNullConstraintBuilder().onTable(table).addColumn("col1").build(metaStore.getConf());
client.addNotNullConstraint(nn);
rqst = new NotNullConstraintsRequest(table.getCatName(), table.getDbName(), table.getTableName());
fetched = client.getNotNullConstraints(rqst);
nn.get(0).setNn_name(fetched.get(0).getNn_name());
Assert.assertEquals(nn, fetched);
// Drop a primary key
client.dropConstraint(table.getCatName(), table.getDbName(), table.getTableName(), nn.get(0).getNn_name());
rqst = new NotNullConstraintsRequest(table.getCatName(), table.getDbName(), table.getTableName());
fetched = client.getNotNullConstraints(rqst);
Assert.assertTrue(fetched.isEmpty());
// Make sure I can add it back
client.addNotNullConstraint(nn);
}
Aggregations