diff --git metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java index b16218d..4676e15 100644 --- metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java +++ metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java @@ -8539,7 +8539,17 @@ private String getPrimaryKeyConstraintName(String db_name, String tbl_name) thro final String parent_tbl_name = parent_tbl_name_input; final String foreign_db_name = foreign_db_name_input; final String foreign_tbl_name = foreign_tbl_name_input; - return new GetListHelper(foreign_db_name, foreign_tbl_name, allowSql, allowJdo) { + final String db_name; + final String tbl_name; + if (foreign_tbl_name == null) { + // The FK table name might be null if we are retrieving the constraint from the PK side + db_name = parent_db_name_input; + tbl_name = parent_tbl_name_input; + } else { + db_name = foreign_db_name_input; + tbl_name = foreign_tbl_name_input; + } + return new GetListHelper(db_name, tbl_name, allowSql, allowJdo) { @Override protected List getSqlResult(GetHelper> ctx) throws MetaException { diff --git metastore/src/test/org/apache/hadoop/hive/metastore/TestObjectStore.java metastore/src/test/org/apache/hadoop/hive/metastore/TestObjectStore.java index 69e8826..b28ea73 100644 --- metastore/src/test/org/apache/hadoop/hive/metastore/TestObjectStore.java +++ metastore/src/test/org/apache/hadoop/hive/metastore/TestObjectStore.java @@ -63,6 +63,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.collect.ImmutableList; + import javax.jdo.Query; public class TestObjectStore { @@ -204,22 +206,63 @@ public void testDatabaseOps() throws MetaException, InvalidObjectException, NoSu public void testTableOps() throws MetaException, InvalidObjectException, NoSuchObjectException, InvalidInputException { Database db1 = new Database(DB1, "description", "locationurl", null); objectStore.createDatabase(db1); - StorageDescriptor sd = new StorageDescriptor(null, "location", null, null, false, 0, new SerDeInfo("SerDeName", "serializationLib", null), null, null, null); + StorageDescriptor sd1 = new StorageDescriptor(ImmutableList.of(new FieldSchema("pk_col", "double", null)), + "location", null, null, false, 0, new SerDeInfo("SerDeName", "serializationLib", null), + null, null, null); HashMap params = new HashMap(); params.put("EXTERNAL", "false"); - Table tbl1 = new Table(TABLE1, DB1, "owner", 1, 2, 3, sd, null, params, null, null, "MANAGED_TABLE"); + Table tbl1 = new Table(TABLE1, DB1, "owner", 1, 2, 3, sd1, null, params, null, null, "MANAGED_TABLE"); objectStore.createTable(tbl1); List tables = objectStore.getAllTables(DB1); Assert.assertEquals(1, tables.size()); Assert.assertEquals(TABLE1, tables.get(0)); - Table newTbl1 = new Table("new" + TABLE1, DB1, "owner", 1, 2, 3, sd, null, params, null, null, "MANAGED_TABLE"); + StorageDescriptor sd2 = new StorageDescriptor(ImmutableList.of(new FieldSchema("fk_col", "double", null)), + "location", null, null, false, 0, new SerDeInfo("SerDeName", "serializationLib", null), + null, null, null); + Table newTbl1 = new Table("new" + TABLE1, DB1, "owner", 1, 2, 3, sd2, null, params, null, null, "MANAGED_TABLE"); objectStore.alterTable(DB1, TABLE1, newTbl1); tables = objectStore.getTables(DB1, "new*"); Assert.assertEquals(1, tables.size()); Assert.assertEquals("new" + TABLE1, tables.get(0)); + objectStore.createTable(tbl1); + tables = objectStore.getAllTables(DB1); + Assert.assertEquals(2, tables.size()); + + List foreignKeys = objectStore.getForeignKeys(DB1, TABLE1, null, null); + Assert.assertEquals(0, foreignKeys.size()); + + SQLPrimaryKey pk = new SQLPrimaryKey(DB1, TABLE1, "pk_col", 1, + "pk_const_1", false, false, false); + objectStore.addPrimaryKeys(ImmutableList.of(pk)); + SQLForeignKey fk = new SQLForeignKey(DB1, TABLE1, "pk_col", + DB1, "new" + TABLE1, "fk_col", 1, + 0, 0, "fk_const_1", "pk_const_1", false, false, false); + objectStore.addForeignKeys(ImmutableList.of(fk)); + + // Retrieve from PK side + foreignKeys = objectStore.getForeignKeys(null, null, DB1, "new" + TABLE1); + Assert.assertEquals(1, foreignKeys.size()); + + List fks = objectStore.getForeignKeys(null, null, DB1, "new" + TABLE1); + if (fks != null) { + for (SQLForeignKey fkcol : fks) { + objectStore.dropConstraint(fkcol.getFktable_db(), fkcol.getFktable_name(), fkcol.getFk_name()); + } + } + // Retrieve from FK side + foreignKeys = objectStore.getForeignKeys(DB1, TABLE1, null, null); + Assert.assertEquals(0, foreignKeys.size()); + // Retrieve from PK side + foreignKeys = objectStore.getForeignKeys(null, null, DB1, "new" + TABLE1); + Assert.assertEquals(0, foreignKeys.size()); + + objectStore.dropTable(DB1, TABLE1); + tables = objectStore.getAllTables(DB1); + Assert.assertEquals(1, tables.size()); + objectStore.dropTable(DB1, "new" + TABLE1); tables = objectStore.getAllTables(DB1); Assert.assertEquals(0, tables.size());