You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by ya...@apache.org on 2020/07/24 01:22:40 UTC

[calcite] branch master updated: [CALCITE-4022] Support unparse special syntax when operator is INSERT (Xu zhaohui)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 2f7c6e2  [CALCITE-4022] Support unparse special syntax when operator is INSERT (Xu zhaohui)
2f7c6e2 is described below

commit 2f7c6e277758969da4f2e4132aaf251f9e6b0d4a
Author: xzh <95...@qq.com>
AuthorDate: Tue Jun 9 16:14:49 2020 +0800

    [CALCITE-4022] Support unparse special syntax when operator is INSERT (Xu zhaohui)
---
 .../java/org/apache/calcite/sql/SqlSyntax.java     |  5 +---
 .../main/java/org/apache/calcite/sql/SqlUtil.java  | 26 ++++++++++++++++++++
 .../apache/calcite/sql/parser/SqlParserTest.java   | 28 ++++++++++++++++++++++
 3 files changed, 55 insertions(+), 4 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/sql/SqlSyntax.java b/core/src/main/java/org/apache/calcite/sql/SqlSyntax.java
index 0f53ee1..c36158f 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlSyntax.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlSyntax.java
@@ -17,7 +17,6 @@
 package org.apache.calcite.sql;
 
 import org.apache.calcite.sql.validate.SqlConformance;
-import org.apache.calcite.util.Util;
 
 /**
  * Enumeration of possible syntactic types of {@link SqlOperator operators}.
@@ -111,9 +110,7 @@ public enum SqlSyntax {
         SqlCall call,
         int leftPrec,
         int rightPrec) {
-      // You probably need to override the operator's unparse
-      // method.
-      throw Util.needToImplement(this);
+      SqlUtil.unparseSpecialSyntax(operator, writer, call);
     }
   },
 
diff --git a/core/src/main/java/org/apache/calcite/sql/SqlUtil.java b/core/src/main/java/org/apache/calcite/sql/SqlUtil.java
index 16d463b..30a52e9 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlUtil.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlUtil.java
@@ -262,6 +262,32 @@ public abstract class SqlUtil {
   }
 
   /**
+   * Unparses a call to an operator which has special syntax.
+   *
+   * @param operator The operator
+   * @param writer   Writer
+   * @param call     List of 0 or more operands
+   */
+  public static void unparseSpecialSyntax(SqlOperator operator,
+      SqlWriter writer,
+      SqlCall call) {
+    final List<SqlNode> operands = call.getOperandList();
+    switch (operator.getKind()) {
+    case INSERT:
+      assert operands.size() == 4;
+      final SqlInsert insert = new SqlInsert(call.getParserPosition(),
+          (SqlNodeList) operands.get(0), operands.get(1), operands.get(2),
+          (SqlNodeList) operands.get(3));
+      insert.unparse(writer, operator.getLeftPrec(), operator.getRightPrec());
+      return;
+    default:
+      // You probably need to override the operator's unparse
+      // method.
+      throw Util.needToImplement(operator);
+    }
+  }
+
+  /**
    * Unparses a call to an operator which has function syntax.
    *
    * @param operator    The operator
diff --git a/core/src/test/java/org/apache/calcite/sql/parser/SqlParserTest.java b/core/src/test/java/org/apache/calcite/sql/parser/SqlParserTest.java
index c7d9ffd..badb4ce 100644
--- a/core/src/test/java/org/apache/calcite/sql/parser/SqlParserTest.java
+++ b/core/src/test/java/org/apache/calcite/sql/parser/SqlParserTest.java
@@ -5818,6 +5818,34 @@ public class SqlParserTest {
     assertTrue(sqlNodeVisited.getKind() == SqlKind.INSERT);
   }
 
+  @Test void testSqlInsertSqlBasicCallToString() throws Exception {
+    final String sql0 = "insert into emps select * from emps";
+    final SqlNode sqlNode0 = getSqlParser(sql0).parseStmt();
+    final SqlNode sqlNodeVisited0 = sqlNode0.accept(new SqlShuttle() {
+      @Override public SqlNode visit(SqlIdentifier identifier) {
+        return new SqlIdentifier(identifier.names,
+            identifier.getParserPosition());
+      }
+    });
+    final String str0 = "INSERT INTO `EMPS`\n"
+        + "(SELECT *\n"
+        + "FROM `EMPS`)";
+    assertEquals(linux(sqlNodeVisited0.toString()), str0);
+
+    final String sql1 = "insert into emps select empno from emps";
+    final SqlNode sqlNode1 = getSqlParser(sql1).parseStmt();
+    final SqlNode sqlNodeVisited1 = sqlNode1.accept(new SqlShuttle() {
+      @Override public SqlNode visit(SqlIdentifier identifier) {
+        return new SqlIdentifier(identifier.names,
+            identifier.getParserPosition());
+      }
+    });
+    final String str1 = "INSERT INTO `EMPS`\n"
+        + "(SELECT `EMPNO`\n"
+        + "FROM `EMPS`)";
+    assertEquals(linux(sqlNodeVisited1.toString()), str1);
+  }
+
   /**
    * Runs tests for INTERVAL... DAY TO HOUR that should pass parser but fail
    * validator. A substantially identical set of tests exists in