This is an automated email from the ASF dual-hosted git repository.

dmollitor pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hive.git


The following commit(s) were added to refs/heads/master by this push:
     new d2163cb  HIVE-23128: SHOW CREATE TABLE Creates Incorrect Syntax When 
Database Specified (David Mollitor, reviewed by Miklos Gergely)
d2163cb is described below

commit d2163cbfb8bacf859fa8572e24c8533bb2dcb0f3
Author: David Mollitor <dmolli...@apache.org>
AuthorDate: Wed Apr 8 15:30:07 2020 -0400

    HIVE-23128: SHOW CREATE TABLE Creates Incorrect Syntax When Database 
Specified (David Mollitor, reviewed by Miklos Gergely)
---
 .../table/create/show/ShowCreateTableAnalyzer.java | 18 +++++++---
 .../ddl/table/create/show/ShowCreateTableDesc.java | 18 ++++++++--
 .../create/show/ShowCreateTableOperation.java      | 38 +++++++++++++++-------
 .../clientpositive/llap/whroot_external1.q.out     |  6 ++--
 .../show_create_table_db_table.q.out               |  8 ++---
 .../show_create_table_temp_table.q.out             |  2 +-
 6 files changed, 63 insertions(+), 27 deletions(-)

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 c7479da..b362837 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,15 +46,20 @@ public class ShowCreateTableAnalyzer extends 
BaseSemanticAnalyzer {
   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));
+    Entry<String, String> tableIdentifier = getDbTableNamePair((ASTNode) 
root.getChild(0));
+    Table table = getTable(tableIdentifier.getKey(), 
tableIdentifier.getValue(), true);
+
+    inputs.add(new ReadEntity(table));
+
+    // If no DB was specified in statement, do not include it in the final 
output
+    ShowCreateTableDesc desc = new ShowCreateTableDesc(table.getDbName(), 
table.getTableName(),
+        ctx.getResFile().toString(), 
StringUtils.isBlank(tableIdentifier.getKey()));
 
-    ShowCreateTableDesc desc = new ShowCreateTableDesc(tableName, 
ctx.getResFile().toString());
     Task<DDLWork> task = TaskFactory.get(new DDLWork(getInputs(), 
getOutputs(), desc));
+    task.setFetchSource(true);
+
     rootTasks.add(task);
 
-    task.setFetchSource(true);
     setFetchTask(createFetchTask(ShowCreateTableDesc.SCHEMA));
   }
 }
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 4687cbc..cd580b7 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 class ShowCreateTableDesc implements DDLDesc, 
Serializable {
 
   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 class ShowCreateTableDesc implements DDLDesc, 
Serializable {
   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 e07559f..51d9f10 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 @@ import com.google.common.collect.Sets;
 public class ShowCreateTableOperation extends 
DDLOperation<ShowCreateTableDesc> {
   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 class ShowCreateTableOperation extends 
DDLOperation<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);
+      Table table = context.getDb().getTable(desc.getDatabaseName(), 
desc.getTableName());
+      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 <if(" + DATABASE_NAME + ")>`<" + DATABASE_NAME + 
">`.<endif>" + "`<" + TABLE_NAME + ">`" + " AS <SQL>";
+
+  private String getCreateViewCommand(Table table, boolean isRelative) {
+    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 <if(" + DATABASE_NAME 
+ ")>`<" + DATABASE_NAME + ">`.<endif>"
+          + "`<" + TABLE_NAME + ">`(\n" +
       "<" + LIST_COLUMNS + ">)\n" +
       "<" + COMMENT + ">\n" +
       "<" + PARTITIONS + ">\n" +
@@ -120,10 +131,13 @@ public class ShowCreateTableOperation extends 
DDLOperation<ShowCreateTableDesc>
       "TBLPROPERTIES (\n" +
       "<" + PROPERTIES + ">)\n";
 
-  private String getCreateTableCommand(Table table) {
+  private String getCreateTableCommand(Table table, boolean isRelative) {
     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/llap/whroot_external1.q.out 
b/ql/src/test/results/clientpositive/llap/whroot_external1.q.out
index a0115a4..268e5ff 100644
--- a/ql/src/test/results/clientpositive/llap/whroot_external1.q.out
+++ b/ql/src/test/results/clientpositive/llap/whroot_external1.q.out
@@ -233,7 +233,7 @@ PREHOOK: Input: wre1_db@wre1_ext3
 POSTHOOK: query: show create table wre1_db.wre1_ext3
 POSTHOOK: type: SHOW_CREATETABLE
 POSTHOOK: Input: wre1_db@wre1_ext3
-CREATE EXTERNAL TABLE `wre1_db.wre1_ext3`(
+CREATE EXTERNAL TABLE `wre1_db`.`wre1_ext3`(
   `c1` string, 
   `c2` string)
 ROW FORMAT SERDE 
@@ -318,7 +318,7 @@ PREHOOK: Input: wre1_db@wre1_ext4
 POSTHOOK: query: show create table wre1_db.wre1_ext4
 POSTHOOK: type: SHOW_CREATETABLE
 POSTHOOK: Input: wre1_db@wre1_ext4
-CREATE EXTERNAL TABLE `wre1_db.wre1_ext4`(
+CREATE EXTERNAL TABLE `wre1_db`.`wre1_ext4`(
   `c1` string, 
   `c2` string)
 ROW FORMAT SERDE 
@@ -484,7 +484,7 @@ PREHOOK: Input: wre1_db@wre1_ext6
 POSTHOOK: query: show create table wre1_db.wre1_ext6
 POSTHOOK: type: SHOW_CREATETABLE
 POSTHOOK: Input: wre1_db@wre1_ext6
-CREATE EXTERNAL TABLE `wre1_db.wre1_ext6`(
+CREATE EXTERNAL TABLE `wre1_db`.`wre1_ext6`(
   `c1` string, 
   `c2` string)
 ROW FORMAT SERDE 
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 27e68b8..1406743 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<map<int,uniontype<struct<`a`:int, `b`:string>, 
array<struct<`c`:int, `d`:string>>>>>>)
 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 bf25602..917dca5 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 

Reply via email to