use of org.apache.hadoop.hive.metastore.api.SQLForeignKey in project hive by apache.
the class TestForeignKey method createGetDrop.
@Test
public void createGetDrop() throws TException {
Table parentTable = testTables[1];
Table table = testTables[0];
// Make sure get on a table with no key returns empty list
ForeignKeysRequest rqst = new ForeignKeysRequest(parentTable.getDbName(), parentTable.getTableName(), table.getDbName(), table.getTableName());
rqst.setCatName(table.getCatName());
List<SQLForeignKey> fetched = client.getForeignKeys(rqst);
Assert.assertTrue(fetched.isEmpty());
// Single column unnamed primary key in default catalog and database
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").build(metaStore.getConf());
client.addForeignKey(fk);
rqst = new ForeignKeysRequest(parentTable.getDbName(), parentTable.getTableName(), table.getDbName(), table.getTableName());
rqst.setCatName(table.getCatName());
fetched = client.getForeignKeys(rqst);
fk.get(0).setFk_name(fetched.get(0).getFk_name());
Assert.assertEquals(fk, fetched);
// Drop a foreign key
client.dropConstraint(table.getCatName(), table.getDbName(), table.getTableName(), fk.get(0).getFk_name());
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.api.SQLForeignKey in project hive by apache.
the class TestForeignKey method noSuchPk.
@Test(expected = MetaException.class)
public void noSuchPk() throws TException {
List<SQLPrimaryKey> pk = new SQLPrimaryKeyBuilder().onTable(testTables[1]).addColumn("col1").build(metaStore.getConf());
// Don't actually create the key
List<SQLForeignKey> fk = new SQLForeignKeyBuilder().onTable(testTables[0]).fromPrimaryKey(pk).addColumn("col2").build(metaStore.getConf());
client.addForeignKey(fk);
Assert.fail();
}
use of org.apache.hadoop.hive.metastore.api.SQLForeignKey in project hive by apache.
the class TestForeignKey method foreignKeyAcrossCatalogs.
@Test
public void foreignKeyAcrossCatalogs() throws TException {
Table parentTable = testTables[2];
Table table = testTables[0];
// Single column unnamed primary key in default catalog and database
List<SQLPrimaryKey> pk = new SQLPrimaryKeyBuilder().onTable(parentTable).addColumn("col1").build(metaStore.getConf());
client.addPrimaryKey(pk);
try {
List<SQLForeignKey> fk = new SQLForeignKeyBuilder().fromPrimaryKey(pk).onTable(table).addColumn("col1").build(metaStore.getConf());
client.addForeignKey(fk);
Assert.fail();
} catch (InvalidObjectException | TApplicationException e) {
// NOP
}
}
use of org.apache.hadoop.hive.metastore.api.SQLForeignKey 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.api.SQLForeignKey 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);
}
Aggregations