diff --git a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java index d7d7097336..428ec8f8ce 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java @@ -1671,4 +1671,25 @@ private String getDetailedTableDescription(Statement stmt, String table) throws } return extendedDescription; } + + @Test + public void testCustomPathsForCTLV() throws Exception { + try (Statement stmt = conTestDb.createStatement()) { + // Initialize + stmt.execute("CREATE TABLE emp_table (id int, name string, salary int)"); + stmt.execute("insert into emp_table values(1,'aaaaa',20000)"); + stmt.execute("CREATE VIEW emp_view AS SELECT * FROM emp_table WHERE salary>10000"); + String customPath = System.getProperty("test.tmp.dir") + "/custom"; + + //Test External CTLV + String extPath = customPath + "/emp_ext_table"; + stmt.execute("CREATE EXTERNAL TABLE emp_ext_table like emp_view STORED AS PARQUET LOCATION '" + extPath + "'"); + assertTrue(getDetailedTableDescription(stmt, "emp_ext_table").contains(extPath)); + + //Test Managed CTLV + String mndPath = customPath + "/emp_mm_table"; + stmt.execute("CREATE TABLE emp_mm_table like emp_view STORED AS ORC LOCATION '" + mndPath + "'"); + assertTrue(getDetailedTableDescription(stmt, "emp_mm_table").contains(mndPath)); + } + } } diff --git a/ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java b/ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java index acca49012b..236799016d 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java @@ -5000,6 +5000,7 @@ private int createTableLike(Hive db, CreateTableLikeDesc crtTbl) throws Exceptio tbl.setTableType(TableType.EXTERNAL_TABLE); } + setUserSpecifiedLocation(crtTbl, tbl); tbl.setFields(oldtbl.getCols()); tbl.setPartCols(oldtbl.getPartCols()); @@ -5041,11 +5042,7 @@ private int createTableLike(Hive db, CreateTableLikeDesc crtTbl) throws Exceptio // using old table object, hence reset the owner to current user for new table. tbl.setOwner(SessionState.getUserFromAuthenticator()); - if (crtTbl.getLocation() != null) { - tbl.setDataLocation(new Path(crtTbl.getLocation())); - } else { - tbl.unsetDataLocation(); - } + setUserSpecifiedLocation(crtTbl, tbl); Class serdeClass = oldtbl.getDeserializerClass(); @@ -5117,6 +5114,14 @@ private int createTableLike(Hive db, CreateTableLikeDesc crtTbl) throws Exceptio return 0; } + private void setUserSpecifiedLocation(CreateTableLikeDesc crtTbl, Table tbl) { + if (crtTbl.getLocation() != null) { + tbl.setDataLocation(new Path(crtTbl.getLocation())); + } else { + tbl.unsetDataLocation(); + } + } + /** * Create a new view. *