use of org.apache.hadoop.hive.metastore.client.builder.SQLForeignKeyBuilder in project hive by apache.
the class TestForeignKey method addNoSuchCatalog.
@Test
public void addNoSuchCatalog() throws TException {
Table parentTable = testTables[0];
List<SQLPrimaryKey> pk = new SQLPrimaryKeyBuilder().onTable(parentTable).addColumn("col1").build(metaStore.getConf());
client.addPrimaryKey(pk);
try {
List<SQLForeignKey> fk = new SQLForeignKeyBuilder().setTableName(testTables[0].getTableName()).setDbName(testTables[0].getDbName()).setCatName("nosuch").fromPrimaryKey(pk).addColumn("col2").build(metaStore.getConf());
client.addForeignKey(fk);
Assert.fail();
} catch (InvalidObjectException | TApplicationException e) {
// NOP
}
}
use of org.apache.hadoop.hive.metastore.client.builder.SQLForeignKeyBuilder in project hive by apache.
the class TestForeignKey method createTableWithConstraints.
@Test
public void createTableWithConstraints() throws TException {
String constraintName = "ctwckk";
Table parentTable = testTables[0];
Table table = new TableBuilder().setTableName("table_with_constraints").setDbName(parentTable.getDbName()).addCol("col1", "int").addCol("col2", "varchar(32)").build(metaStore.getConf());
List<SQLPrimaryKey> pk = new SQLPrimaryKeyBuilder().onTable(parentTable).addColumn("col1").build(metaStore.getConf());
client.addPrimaryKey(pk);
List<SQLForeignKey> fk = new SQLForeignKeyBuilder().fromPrimaryKey(pk).onTable(table).addColumn("col1").setConstraintName(constraintName).build(metaStore.getConf());
client.createTableWithConstraints(table, null, fk, null, null, null, null);
ForeignKeysRequest rqst = new ForeignKeysRequest(parentTable.getDbName(), parentTable.getTableName(), table.getDbName(), table.getTableName());
rqst.setCatName(table.getCatName());
List<SQLForeignKey> fetched = client.getForeignKeys(rqst);
Assert.assertEquals(fk, fetched);
}
use of org.apache.hadoop.hive.metastore.client.builder.SQLForeignKeyBuilder in project hive by apache.
the class NonCatCallsWithCatalog method primaryKeyAndForeignKey.
@Test
public void primaryKeyAndForeignKey() throws TException {
Table parentTable = testTables[2];
Table table = testTables[3];
String constraintName = "othercatfk";
// Single column unnamed primary key in default catalog and database
List<SQLPrimaryKey> pk = new SQLPrimaryKeyBuilder().onTable(parentTable).addColumn("test_col1").build(conf);
client.addPrimaryKey(pk);
List<SQLForeignKey> fk = new SQLForeignKeyBuilder().fromPrimaryKey(pk).onTable(table).addColumn("test_col1").setConstraintName(constraintName).build(conf);
client.addForeignKey(fk);
PrimaryKeysRequest pkRqst = new PrimaryKeysRequest(parentTable.getDbName(), parentTable.getTableName());
pkRqst.setCatName(parentTable.getCatName());
List<SQLPrimaryKey> pkFetched = client.getPrimaryKeys(pkRqst);
Assert.assertEquals(1, pkFetched.size());
Assert.assertEquals(expectedCatalog(), pkFetched.get(0).getCatName());
Assert.assertEquals(parentTable.getDbName(), pkFetched.get(0).getTable_db());
Assert.assertEquals(parentTable.getTableName(), pkFetched.get(0).getTable_name());
Assert.assertEquals("test_col1", pkFetched.get(0).getColumn_name());
Assert.assertEquals(1, pkFetched.get(0).getKey_seq());
Assert.assertTrue(pkFetched.get(0).isEnable_cstr());
Assert.assertFalse(pkFetched.get(0).isValidate_cstr());
Assert.assertFalse(pkFetched.get(0).isRely_cstr());
Assert.assertEquals(parentTable.getCatName(), pkFetched.get(0).getCatName());
ForeignKeysRequest rqst = new ForeignKeysRequest(parentTable.getDbName(), parentTable.getTableName(), table.getDbName(), table.getTableName());
rqst.setCatName(table.getCatName());
List<SQLForeignKey> fetched = client.getForeignKeys(rqst);
Assert.assertEquals(1, fetched.size());
Assert.assertEquals(table.getDbName(), fetched.get(0).getFktable_db());
Assert.assertEquals(table.getTableName(), fetched.get(0).getFktable_name());
Assert.assertEquals(expectedCatalog(), fetched.get(0).getCatName());
Assert.assertEquals("test_col1", fetched.get(0).getFkcolumn_name());
Assert.assertEquals(parentTable.getDbName(), fetched.get(0).getPktable_db());
Assert.assertEquals(parentTable.getTableName(), fetched.get(0).getPktable_name());
Assert.assertEquals("test_col1", fetched.get(0).getFkcolumn_name());
Assert.assertEquals(1, fetched.get(0).getKey_seq());
Assert.assertEquals(parentTable.getTableName() + "_primary_key", fetched.get(0).getPk_name());
Assert.assertEquals(constraintName, fetched.get(0).getFk_name());
String table0FkName = fetched.get(0).getFk_name();
Assert.assertTrue(fetched.get(0).isEnable_cstr());
Assert.assertFalse(fetched.get(0).isValidate_cstr());
Assert.assertFalse(fetched.get(0).isRely_cstr());
Assert.assertEquals(table.getCatName(), fetched.get(0).getCatName());
// Drop a foreign key
client.dropConstraint(table.getDbName(), table.getTableName(), table0FkName);
rqst = new ForeignKeysRequest(parentTable.getDbName(), parentTable.getTableName(), table.getDbName(), table.getTableName());
rqst.setCatName(table.getCatName());
fetched = client.getForeignKeys(rqst);
Assert.assertTrue(fetched.isEmpty());
}
use of org.apache.hadoop.hive.metastore.client.builder.SQLForeignKeyBuilder in project hive by apache.
the class TestForeignKey method createGetDrop2Column.
@Test
public void createGetDrop2Column() throws TException {
Table parentTable = testTables[1];
Table table = testTables[0];
String constraintName = "2colfk";
// Single column unnamed primary key in default catalog and database
List<SQLPrimaryKey> pk = new SQLPrimaryKeyBuilder().onTable(parentTable).addColumn("col1").addColumn("col2").build(metaStore.getConf());
client.addPrimaryKey(pk);
List<SQLForeignKey> fk = new SQLForeignKeyBuilder().fromPrimaryKey(pk).onTable(table).addColumn("col1").addColumn("col2").setConstraintName(constraintName).build(metaStore.getConf());
client.addForeignKey(fk);
ForeignKeysRequest rqst = new ForeignKeysRequest(parentTable.getDbName(), parentTable.getTableName(), table.getDbName(), table.getTableName());
rqst.setCatName(table.getCatName());
List<SQLForeignKey> fetched = client.getForeignKeys(rqst);
Assert.assertEquals(fk, fetched);
// Drop a foreign key
client.dropConstraint(table.getCatName(), table.getDbName(), table.getTableName(), constraintName);
rqst = new ForeignKeysRequest(parentTable.getDbName(), parentTable.getTableName(), table.getDbName(), table.getTableName());
rqst.setCatName(table.getCatName());
fetched = client.getForeignKeys(rqst);
Assert.assertTrue(fetched.isEmpty());
// Make sure I can add it back
client.addForeignKey(fk);
}
use of org.apache.hadoop.hive.metastore.client.builder.SQLForeignKeyBuilder in project hive by apache.
the class TestForeignKey method createTableWithConstraintsInOtherCatalog.
@Test
public void createTableWithConstraintsInOtherCatalog() throws TException {
String constraintName = "ctwcocfk";
Table parentTable = testTables[2];
Table table = new TableBuilder().setTableName("table_with_constraints").inDb(inOtherCatalog).addCol("col1", "int").addCol("col2", "varchar(32)").build(metaStore.getConf());
List<SQLPrimaryKey> pk = new SQLPrimaryKeyBuilder().onTable(parentTable).addColumn("col1").build(metaStore.getConf());
client.addPrimaryKey(pk);
List<SQLForeignKey> fk = new SQLForeignKeyBuilder().fromPrimaryKey(pk).onTable(table).addColumn("col1").setConstraintName(constraintName).build(metaStore.getConf());
client.createTableWithConstraints(table, null, fk, null, null, null, null);
ForeignKeysRequest rqst = new ForeignKeysRequest(parentTable.getDbName(), parentTable.getTableName(), table.getDbName(), table.getTableName());
rqst.setCatName(table.getCatName());
List<SQLForeignKey> fetched = client.getForeignKeys(rqst);
Assert.assertEquals(fk, fetched);
}
Aggregations