diff --git itests/hive-unit/src/test/java/org/apache/hadoop/hive/metastore/TestHiveMetastoreTransformer.java itests/hive-unit/src/test/java/org/apache/hadoop/hive/metastore/TestHiveMetastoreTransformer.java index 6a5915d94e..4ff732d081 100644 --- itests/hive-unit/src/test/java/org/apache/hadoop/hive/metastore/TestHiveMetastoreTransformer.java +++ itests/hive-unit/src/test/java/org/apache/hadoop/hive/metastore/TestHiveMetastoreTransformer.java @@ -74,6 +74,7 @@ protected static boolean isThriftClient = true; private static final String CAPABILITIES_KEY = "OBJCAPABILITIES"; + private static final String DATABASE_WAREHOUSE_SUFFIX = ".db"; @Before public void setUp() throws Exception { @@ -89,6 +90,7 @@ public void setUp() throws Exception { MetastoreConf.setVar(conf, ConfVars.METASTORE_METADATA_TRANSFORMER_CLASS, "org.apache.hadoop.hive.metastore.MetastoreDefaultTransformer"); MetastoreConf.setBoolVar(conf, ConfVars.HIVE_IN_TEST, false); + MetastoreConf.setVar(conf, ConfVars.WAREHOUSE, wh.getCanonicalPath()); MetastoreConf.setVar(conf, ConfVars.WAREHOUSE_EXTERNAL, ext_wh.getCanonicalPath()); client = new HiveMetaStoreClient(conf); } @@ -1174,12 +1176,15 @@ public void testTransformerAlterTable() throws Exception { String tableLocation = tbl2.getSd().getLocation(); int idx = (tableLocation.indexOf(":") > 0) ? tableLocation.indexOf(":") : 0; tableLocation = tableLocation.substring(idx+1); + String expectedPath = ext_wh.getAbsolutePath().concat(File.separator).concat(dbName).concat(DATABASE_WAREHOUSE_SUFFIX) + .concat(File.separator).concat(tblName); + assertEquals("Table location", expectedPath, tableLocation); String newLocation = wh.getAbsolutePath().concat(File.separator).concat(dbName).concat(File.separator) .concat(tblName); - table.getSd().setLocation(newLocation); + tbl2.getSd().setLocation(newLocation); try { - client.alter_table(dbName, tblName, table); + client.alter_table(dbName, tblName, tbl2); fail("alter_table expected to fail due to location:" + newLocation); } catch (Exception e) { e.printStackTrace(); diff --git standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/MetastoreDefaultTransformer.java standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/MetastoreDefaultTransformer.java index f6043cef0a..386c5a4720 100644 --- standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/MetastoreDefaultTransformer.java +++ standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/MetastoreDefaultTransformer.java @@ -557,7 +557,7 @@ public Table transformCreateTable(Table table, List processorCapabilitie params.remove(TABLE_IS_TRANSACTIONAL); params.remove(TABLE_TRANSACTIONAL_PROPERTIES); params.put("EXTERNAL", "TRUE"); - params.put(EXTERNAL_TABLE_PURGE, "TRUE"); + params.put(EXTERNAL_TABLE_PURGE, "true"); params.put("TRANSLATED_TO_EXTERNAL", "TRUE"); newTable.setParameters(params); LOG.info("Modified table params are:" + params.toString()); @@ -595,14 +595,16 @@ public Table transformCreateTable(Table table, List processorCapabilitie } else if (TableType.EXTERNAL_TABLE.name().equals(tableType)) { LOG.info("Table to be created is of type " + tableType + " but not " + TableType.MANAGED_TABLE.toString()); String tableLocation = table.getSd().getLocation(); - String externalWHRoot = hmsHandler.getWh().getWhRootExternal().toString(); + Path whRootPath = Path.getPathWithoutSchemeAndAuthority(hmsHandler.getWh().getWhRoot()); - if (tableLocation != null && !tableLocation.startsWith(externalWHRoot)) { - throw new MetaException( - "An external table's location needs to be under the external warehouse root directory," + "table:" - + table.getTableName() + ",location:" + tableLocation + ",Hive warehouse:" + externalWHRoot); + if (tableLocation != null) { + Path tablePath = Path.getPathWithoutSchemeAndAuthority(new Path(tableLocation)); + if (tablePath.toString().startsWith(whRootPath.toString())) { + throw new MetaException( + "An external table's location should not be located within managed warehouse root directory," + "table:" + + table.getTableName() + ",location:" + tablePath + ",Hive managed warehouse:" + whRootPath); + } } - } LOG.info("Transformer returning table:" + newTable.toString()); return newTable; @@ -613,25 +615,22 @@ public Table transformAlterTable(Table table, List processorCapabilities LOG.info("Starting translation for Alter table for processor " + processorId + " with " + processorCapabilities + " on table " + table.getTableName()); String tableType = table.getTableType(); + Path tableLocation = Path.getPathWithoutSchemeAndAuthority(new Path(table.getSd().getLocation())); + Path whRootPath = Path.getPathWithoutSchemeAndAuthority(hmsHandler.getWh().getWhRoot()); if (TableType.MANAGED_TABLE.name().equals(tableType)) { LOG.debug("Table is a MANAGED_TABLE"); - Path tableLocation = Path.getPathWithoutSchemeAndAuthority(new Path(table.getSd().getLocation())); - Path whRootPath = Path.getPathWithoutSchemeAndAuthority(hmsHandler.getWh().getWhRoot()); - if (!tableLocation.toString().startsWith(whRootPath.toString())) { + if (tableLocation != null && !tableLocation.toString().startsWith(whRootPath.toString())) { throw new MetaException( "A managed table's location needs to be under the hive warehouse root directory," + "table:" + table.getTableName() + ",location:" + tableLocation + ",Hive warehouse:" + whRootPath); } } else if (TableType.EXTERNAL_TABLE.name().equals(tableType)) { - LOG.debug("Table is a EXTERNAL TABLE"); - Path tableLocation = Path.getPathWithoutSchemeAndAuthority(new Path(table.getSd().getLocation())); - Path externalWHRootPath = Path.getPathWithoutSchemeAndAuthority(hmsHandler.getWh().getWhRootExternal()); - - if (tableLocation != null && !tableLocation.toString().startsWith(externalWHRootPath.toString())) { + LOG.debug("Table is a EXTERNAL TABLE:tableLocation=" + tableLocation.toString() + ",whroot=" + whRootPath.toString()); + if (tableLocation != null && tableLocation.toString().startsWith(whRootPath.toString())) { throw new MetaException( - "An external table's location needs to be under the external warehouse root directory," + "table:" - + table.getTableName() + ",location:" + tableLocation + ",Hive external warehouse:" + externalWHRootPath); + "An external table's location should not be located within managed warehouse root directory," + "table:" + + table.getTableName() + ",location:" + tableLocation + ",Hive managed warehouse:" + whRootPath); } } LOG.debug("Transformer returning table:" + table.toString());