diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/create/show/ShowCreateTableAnalyzer.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/create/show/ShowCreateTableAnalyzer.java index c7479dad87..f56488341b 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/create/show/ShowCreateTableAnalyzer.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/create/show/ShowCreateTableAnalyzer.java @@ -18,6 +18,9 @@ package org.apache.hadoop.hive.ql.ddl.table.create.show; +import java.util.Map.Entry; + +import org.apache.commons.lang3.StringUtils; import org.apache.hadoop.hive.ql.QueryState; import org.apache.hadoop.hive.ql.ddl.DDLWork; import org.apache.hadoop.hive.ql.ddl.DDLSemanticAnalyzerFactory.DDLType; @@ -43,12 +46,14 @@ public ShowCreateTableAnalyzer(QueryState queryState) throws SemanticException { public void analyzeInternal(ASTNode root) throws SemanticException { ctx.setResFile(ctx.getLocalTmpPath()); - String tableName = getUnescapedName((ASTNode)root.getChild(0)); - Table tab = getTable(tableName); - inputs.add(new ReadEntity(tab)); + final Entry tableName = getDbTableNamePair((ASTNode) root.getChild(0)); + Table table = getTable(tableName.getKey(), tableName.getValue(), true); + inputs.add(new ReadEntity(table)); + + final ShowCreateTableDesc desc = new ShowCreateTableDesc(table.getDbName(), table.getTableName(), + ctx.getResFile().toString(), StringUtils.isBlank(tableName.getKey())); - ShowCreateTableDesc desc = new ShowCreateTableDesc(tableName, ctx.getResFile().toString()); - Task task = TaskFactory.get(new DDLWork(getInputs(), getOutputs(), desc)); + final Task task = TaskFactory.get(new DDLWork(getInputs(), getOutputs(), desc)); rootTasks.add(task); task.setFetchSource(true); diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/create/show/ShowCreateTableDesc.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/create/show/ShowCreateTableDesc.java index 4687cbc085..cd580b7f70 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/create/show/ShowCreateTableDesc.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/create/show/ShowCreateTableDesc.java @@ -33,12 +33,16 @@ public static final String SCHEMA = "createtab_stmt#string"; - private final String resFile; + private final String databaseName; private final String tableName; + private final String resFile; + private final boolean isRelative; - public ShowCreateTableDesc(String tableName, String resFile) { + public ShowCreateTableDesc(String databaseName, String tableName, String resFile, boolean isRelative) { + this.databaseName = databaseName; this.tableName = tableName; this.resFile = resFile; + this.isRelative = isRelative; } @Explain(displayName = "result file", explainLevels = { Level.EXTENDED }) @@ -50,4 +54,14 @@ public String getResFile() { public String getTableName() { return tableName; } + + @Explain(displayName = "database name", explainLevels = { Level.USER, Level.DEFAULT, Level.EXTENDED }) + public String getDatabaseName() { + return databaseName; + } + + @Explain(displayName = "relative table location", explainLevels = { Level.EXTENDED }) + public boolean isRelative() { + return isRelative; + } } diff --git a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/create/show/ShowCreateTableOperation.java b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/create/show/ShowCreateTableOperation.java index e07559f692..2321fe49ed 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/create/show/ShowCreateTableOperation.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/ddl/table/create/show/ShowCreateTableOperation.java @@ -69,7 +69,8 @@ public class ShowCreateTableOperation extends DDLOperation { private static final String EXTERNAL = "external"; private static final String TEMPORARY = "temporary"; - private static final String NAME = "name"; + private static final String DATABASE_NAME = "databaseName"; + private static final String TABLE_NAME = "tableName"; private static final String LIST_COLUMNS = "columns"; private static final String COMMENT = "comment"; private static final String PARTITIONS = "partitions"; @@ -88,28 +89,38 @@ public ShowCreateTableOperation(DDLOperationContext context, ShowCreateTableDesc public int execute() throws HiveException { // get the create table statement for the table and populate the output try (DataOutputStream outStream = DDLUtils.getOutputStream(new Path(desc.getResFile()), context)) { - Table table = context.getDb().getTable(desc.getTableName(), false); - String command = table.isView() ? - getCreateViewCommand(table) : - getCreateTableCommand(table); + final Table table = context.getDb().getTable(desc.getDatabaseName(), desc.getTableName()); + final String command = table.isView() ? getCreateViewCommand(table, desc.isRelative()) + : getCreateTableCommand(table, desc.isRelative()); + outStream.write(command.getBytes(StandardCharsets.UTF_8)); return 0; } catch (IOException e) { - LOG.info("show create table: ", e); + LOG.info("Show create table failed", e); return 1; } catch (Exception e) { throw new HiveException(e); } } - private static final String CREATE_VIEW_COMMAND = "CREATE VIEW `%s` AS %s"; + private static final String CREATE_VIEW_TEMPLATE = + "CREATE VIEW `<" + DATABASE_NAME + ">`." + "`<" + TABLE_NAME + ">`" + " AS "; + + private String getCreateViewCommand(Table table, boolean isRelative) { + final ST command = new ST(CREATE_VIEW_TEMPLATE); + + if (!isRelative) { + command.add(DATABASE_NAME, table.getDbName()); + } + command.add(TABLE_NAME, table.getTableName()); + command.add("SQL", table.getViewExpandedText()); - private String getCreateViewCommand(Table table) { - return String.format(CREATE_VIEW_COMMAND, desc.getTableName(), table.getViewExpandedText()); + return command.render(); } private static final String CREATE_TABLE_TEMPLATE = - "CREATE <" + TEMPORARY + "><" + EXTERNAL + ">TABLE `<" + NAME + ">`(\n" + + "CREATE <" + TEMPORARY + "><" + EXTERNAL + ">TABLE `<" + DATABASE_NAME + ">`." + + "`<" + TABLE_NAME + ">`(\n" + "<" + LIST_COLUMNS + ">)\n" + "<" + COMMENT + ">\n" + "<" + PARTITIONS + ">\n" + @@ -120,10 +131,13 @@ private String getCreateViewCommand(Table table) { "TBLPROPERTIES (\n" + "<" + PROPERTIES + ">)\n"; - private String getCreateTableCommand(Table table) { - ST command = new ST(CREATE_TABLE_TEMPLATE); + private String getCreateTableCommand(Table table, boolean isRelative) { + final ST command = new ST(CREATE_TABLE_TEMPLATE); - command.add(NAME, desc.getTableName()); + if (!isRelative) { + command.add(DATABASE_NAME, table.getDbName()); + } + command.add(TABLE_NAME, table.getTableName()); command.add(TEMPORARY, getTemporary(table)); command.add(EXTERNAL, getExternal(table)); command.add(LIST_COLUMNS, getColumns(table)); diff --git a/ql/src/test/results/clientpositive/show_create_table_db_table.q.out b/ql/src/test/results/clientpositive/show_create_table_db_table.q.out index 27e68b8ecc..14067431f3 100644 --- a/ql/src/test/results/clientpositive/show_create_table_db_table.q.out +++ b/ql/src/test/results/clientpositive/show_create_table_db_table.q.out @@ -54,7 +54,7 @@ PREHOOK: Input: tmp_feng@tmp_showcrt1 POSTHOOK: query: SHOW CREATE TABLE tmp_feng.tmp_showcrt1 POSTHOOK: type: SHOW_CREATETABLE POSTHOOK: Input: tmp_feng@tmp_showcrt1 -CREATE TABLE `tmp_feng.tmp_showcrt1`( +CREATE TABLE `tmp_feng`.`tmp_showcrt1`( `key` string, `value` int) ROW FORMAT SERDE @@ -74,7 +74,7 @@ PREHOOK: Input: tmp_feng@tmp_showcrt2 POSTHOOK: query: SHOW CREATE TABLE tmp_feng.tmp_showcrt2 POSTHOOK: type: SHOW_CREATETABLE POSTHOOK: Input: tmp_feng@tmp_showcrt2 -CREATE TABLE `tmp_feng.tmp_showcrt2`( +CREATE TABLE `tmp_feng`.`tmp_showcrt2`( `key` string, `value` int) SKEWED BY (key) @@ -96,7 +96,7 @@ PREHOOK: Input: tmp_feng@tmp_showcrt3 POSTHOOK: query: SHOW CREATE TABLE tmp_feng.tmp_showcrt3 POSTHOOK: type: SHOW_CREATETABLE POSTHOOK: Input: tmp_feng@tmp_showcrt3 -CREATE TABLE `tmp_feng.tmp_showcrt3`( +CREATE TABLE `tmp_feng`.`tmp_showcrt3`( `key` string, `value` int) SKEWED BY (key) @@ -119,7 +119,7 @@ PREHOOK: Input: tmp_feng@tmp_showcrt4 POSTHOOK: query: SHOW CREATE TABLE tmp_feng.tmp_showcrt4 POSTHOOK: type: SHOW_CREATETABLE POSTHOOK: Input: tmp_feng@tmp_showcrt4 -CREATE TABLE `tmp_feng.tmp_showcrt4`( +CREATE TABLE `tmp_feng`.`tmp_showcrt4`( `s1` struct<`p1`:string>, `s2` struct<`p2`:array, array>>>>>) ROW FORMAT SERDE diff --git a/ql/src/test/results/clientpositive/show_create_table_temp_table.q.out b/ql/src/test/results/clientpositive/show_create_table_temp_table.q.out index bf25602ae3..917dca5c85 100644 --- a/ql/src/test/results/clientpositive/show_create_table_temp_table.q.out +++ b/ql/src/test/results/clientpositive/show_create_table_temp_table.q.out @@ -18,7 +18,7 @@ PREHOOK: Input: tmpdb@tmp1 POSTHOOK: query: show create table tmpdb.tmp1 POSTHOOK: type: SHOW_CREATETABLE POSTHOOK: Input: tmpdb@tmp1 -CREATE TEMPORARY TABLE `tmpdb.tmp1`( +CREATE TEMPORARY TABLE `tmpdb`.`tmp1`( `c1` string, `c2` string) ROW FORMAT SERDE