You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by da...@apache.org on 2021/03/12 03:52:07 UTC

[calcite] 01/01: [CALCITE-4526] SqlSnapshot unparse lost the AS keyword when the table has alias (jibiyr)

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

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

commit 39d477d96b90eca05e5af1cec0f8ae0617226b5d
Author: laughing.sheng <la...@qq.com>
AuthorDate: Wed Mar 10 16:46:14 2021 +0800

    [CALCITE-4526] SqlSnapshot unparse lost the AS keyword when the table has alias (jibiyr)
    
    For SQL: table_name FOR SYSTEM_TIME AS OF time_point AS table_alias,
    when SqlSnapshot's table has alias(the AS operator), the unparse result is wrong:
    the alias is missing but the name reference is still hold by other
    expression.
    
    close apache/calcite#2366
---
 .../main/java/org/apache/calcite/sql/SqlSnapshot.java   | 17 ++++++++++++++++-
 .../org/apache/calcite/sql/parser/SqlParserTest.java    |  9 +++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/core/src/main/java/org/apache/calcite/sql/SqlSnapshot.java b/core/src/main/java/org/apache/calcite/sql/SqlSnapshot.java
index 07ecce7..4ccf3d1 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlSnapshot.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlSnapshot.java
@@ -127,8 +127,23 @@ public class SqlSnapshot extends SqlCall {
         int leftPrec,
         int rightPrec) {
       final SqlSnapshot snapshot = (SqlSnapshot) call;
+      SqlNode tableRef = snapshot.tableRef;
+
+      if (tableRef instanceof SqlBasicCall
+          && ((SqlBasicCall) tableRef).getOperator() instanceof SqlAsOperator) {
+        SqlBasicCall basicCall = (SqlBasicCall) tableRef;
+        basicCall.operand(0).unparse(writer, 0, 0);
+        writer.setNeedWhitespace(true);
+        writeForSystemTimeAsOf(writer, snapshot);
+        writer.keyword("AS");
+        basicCall.operand(1).unparse(writer, 0, 0);
+      } else {
+        tableRef.unparse(writer, 0, 0);
+        writeForSystemTimeAsOf(writer, snapshot);
+      }
+    }
 
-      snapshot.tableRef.unparse(writer, 0, 0);
+    private static void writeForSystemTimeAsOf(SqlWriter writer, SqlSnapshot snapshot) {
       writer.keyword("FOR SYSTEM_TIME AS OF");
       snapshot.period.unparse(writer, 0, 0);
     }
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 1737908..8034698 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
@@ -7766,6 +7766,15 @@ public class SqlParserTest {
         SqlParserUtil.addCarets("abcdef", 1, 7, 1, 7));
   }
 
+  @Test void testSnapshotForSystemTimeWithAlias() {
+    sql("SELECT * FROM orders LEFT JOIN products FOR SYSTEM_TIME AS OF "
+        + "orders.proctime as products ON orders.product_id = products.pro_id")
+        .ok("SELECT *\n"
+            + "FROM `ORDERS`\n"
+            + "LEFT JOIN `PRODUCTS` FOR SYSTEM_TIME AS OF `ORDERS`.`PROCTIME` AS `PRODUCTS` ON (`ORDERS`"
+            + ".`PRODUCT_ID` = `PRODUCTS`.`PRO_ID`)");
+  }
+
   @Test protected void testMetadata() {
     SqlAbstractParserImpl.Metadata metadata = getSqlParser("").getMetadata();
     assertThat(metadata.isReservedFunctionName("ABS"), is(true));