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 27a1557138..9eb7792b03 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 @@ -1383,8 +1383,10 @@ public void testTransformerDatabase() throws Exception { resetHMSClient(); String dbName = "testdb"; + String dbWithLocation = "dbWithLocation"; try { silentDropDatabase(dbName); + silentDropDatabase(dbWithLocation); } catch (Exception e) { LOG.info("Drop database failed for " + dbName); } @@ -1417,6 +1419,31 @@ public void testTransformerDatabase() throws Exception { assertTrue("Database location expected to be external warehouse:actual=" + db.getLocationUri(), db.getLocationUri().contains(conf.get(MetastoreConf.ConfVars.WAREHOUSE_EXTERNAL.getVarname()))); resetHMSClient(); + + Warehouse wh = new Warehouse(conf); + String mgdPath = wh.getDefaultDatabasePath(dbWithLocation, false).toString(); + new DatabaseBuilder() + .setName(dbWithLocation) + .setLocation(mgdPath) + .create(client, conf); + + capabilities = new ArrayList<>(); + capabilities.add("EXTWRITE"); + setHMSClient("TestGetDatabaseWithLocation", (String[])(capabilities.toArray(new String[0]))); + + db = client.getDatabase(dbWithLocation); + assertTrue("Database location expected to be external warehouse:actual=" + db.getLocationUri(), + db.getLocationUri().contains(conf.get(MetastoreConf.ConfVars.WAREHOUSE_EXTERNAL.getVarname()))); + assertNull("Database managed location expected to be null", db.getManagedLocationUri()); + resetHMSClient(); + + capabilities.add("HIVEMANAGEDINSERTWRITE"); + setHMSClient("TestGetDatabaseWithLocation#2", (String[])(capabilities.toArray(new String[0]))); + + db = client.getDatabase(dbWithLocation); + assertTrue("Database location expected to be external warehouse", db.getLocationUri().contains(conf.get(MetastoreConf.ConfVars.WAREHOUSE_EXTERNAL.getVarname()))); + assertEquals("Database managedLocationUri expected to be set to locationUri", mgdPath, db.getManagedLocationUri()); + resetHMSClient(); } catch (Exception e) { System.err.println(org.apache.hadoop.util.StringUtils.stringifyException(e)); System.err.println("testTransformerDatabase() failed."); 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 eff19b2153..a2727174e3 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 @@ -684,16 +684,28 @@ public Database transformDatabase(Database db, List processorCapabilitie } LOG.info("Starting translation for transformDatabase for processor " + processorId + " with " + processorCapabilities - + " on database " + db.getName()); - - if (!isTenantBasedStorage && (processorCapabilities == null || (!processorCapabilities.contains(HIVEMANAGEDINSERTWRITE) && - !processorCapabilities.contains(HIVEFULLACIDWRITE)))) { - LOG.info("Processor does not have any of ACID write capabilities, changing current location from " + - db.getLocationUri() + " to external warehouse location"); - Path extWhLocation = hmsHandler.getWh().getDefaultExternalDatabasePath(db.getName()); - LOG.debug("Setting DBLocation to " + extWhLocation.toString()); - // TODO should not alter database now. - db.setLocationUri(extWhLocation.toString()); + + " on database {} locationUri={} managedLocationUri={}", db.getName(), db.getLocationUri(), db.getManagedLocationUri()); + + if (!isTenantBasedStorage) { + Path locationPath = Path.getPathWithoutSchemeAndAuthority(new Path(db.getLocationUri())); + Path whRootPath = Path.getPathWithoutSchemeAndAuthority(hmsHandler.getWh().getWhRoot()); + if (FileUtils.isSubdirectory(whRootPath.toString(), locationPath.toString())) { // legacy path + if (processorCapabilities != null && (processorCapabilities.contains(HIVEMANAGEDINSERTWRITE) || + processorCapabilities.contains(HIVEFULLACIDWRITE))) { + LOG.debug("Processor has atleast one of ACID write capabilities, setting current locationUri " + db.getLocationUri() + " as managedLocationUri"); + db.setManagedLocationUri(new Path(db.getLocationUri()).toString()); + } + Path extWhLocation = hmsHandler.getWh().getDefaultExternalDatabasePath(db.getName()); + LOG.info("Database's location is a managed location, setting to a new default path based on external warehouse path:" + extWhLocation.toString()); + db.setLocationUri(extWhLocation.toString()); + } else { + if (processorCapabilities != null && (processorCapabilities.contains(HIVEMANAGEDINSERTWRITE) || + processorCapabilities.contains(HIVEFULLACIDWRITE))) { + Path mgdWhLocation = hmsHandler.getWh().getDefaultDatabasePath(db.getName(), false); + LOG.debug("Processor has atleast one of ACID write capabilities, setting default managed path to " + mgdWhLocation.toString()); + db.setManagedLocationUri(mgdWhLocation.toString()); + } + } } LOG.info("Transformer returning database:" + db.toString()); return db;