From 9c8ad5083b5378ff03b326f882ce466199bb164d Mon Sep 17 00:00:00 2001 From: Naresh P R Date: Mon, 22 Jul 2019 14:54:11 +0530 Subject: [PATCH] HIVE-22009 - CTLV with user specified location is not honoured --- .../apache/hive/jdbc/TestJdbcWithMiniHS2.java | 21 +++++++++++++++++++ .../creation/CreateTableLikeOperation.java | 16 +++++++++----- 2 files changed, 32 insertions(+), 5 deletions(-) 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 2e151ec91a..03a1926440 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 @@ -1676,4 +1676,25 @@ static String getDetailedTableDescription(Statement stmt, String table) throws S } 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/ddl/table/creation/CreateTableLikeOperation.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/creation/CreateTableLikeOperation.java index 4837d4405f..8a112619d3 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/creation/CreateTableLikeOperation.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/creation/CreateTableLikeOperation.java @@ -94,6 +94,8 @@ private Table createViewLikeTable(Table oldTable) throws HiveException { setExternalProperties(table); } + setUserSpecifiedLocation(table); + table.setFields(oldTable.getCols()); table.setPartCols(oldTable.getPartCols()); @@ -114,11 +116,7 @@ private Table createTableLikeTable(Table table) throws SemanticException, HiveEx table.setTableName(names[1]); table.setOwner(SessionState.getUserFromAuthenticator()); - if (desc.getLocation() != null) { - table.setDataLocation(new Path(desc.getLocation())); - } else { - table.unsetDataLocation(); - } + setUserSpecifiedLocation(table); setTableParameters(table); @@ -138,6 +136,14 @@ private Table createTableLikeTable(Table table) throws SemanticException, HiveEx return table; } + private void setUserSpecifiedLocation(Table table) { + if (desc.getLocation() != null) { + table.setDataLocation(new Path(desc.getLocation())); + } else { + table.unsetDataLocation(); + } + } + private void setTableParameters(Table tbl) throws HiveException { Set retainer = new HashSet(); -- 2.18.0