You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jh...@apache.org on 2019/10/12 08:37:41 UTC

[calcite] branch master updated: [CALCITE-3382] Hard-wire the TUMBLE grouping function into SQL parser (Rui Wang)

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

jhyde 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 79f6aeb  [CALCITE-3382] Hard-wire the TUMBLE grouping function into SQL parser (Rui Wang)
79f6aeb is described below

commit 79f6aeb980fe900a33d94e9bb42993f2b2e4e7b1
Author: amaliujia <am...@163.com>
AuthorDate: Fri Sep 27 14:12:37 2019 -0700

    [CALCITE-3382] Hard-wire the TUMBLE grouping function into SQL parser (Rui Wang)
    
    Rename the grouping function to "$TUMBLE", since it is no longer
    resolved by name. This will allow us to later add a TUMBLE table UDF.
    
    Close apache/calcite#1481
---
 core/src/main/codegen/config.fmpp                  |  1 +
 core/src/main/codegen/templates/Parser.jj          | 36 ++++++++++++++++++++++
 .../calcite/sql/SqlGroupedWindowFunction.java      |  7 +++++
 .../calcite/sql/fun/SqlStdOperatorTable.java       | 16 ++++++++--
 .../org/apache/calcite/sql/ddl/SqlCreateTable.java |  2 +-
 site/_docs/reference.md                            |  1 +
 6 files changed, 60 insertions(+), 3 deletions(-)

diff --git a/core/src/main/codegen/config.fmpp b/core/src/main/codegen/config.fmpp
index 88ed04cd..eb9adff 100644
--- a/core/src/main/codegen/config.fmpp
+++ b/core/src/main/codegen/config.fmpp
@@ -336,6 +336,7 @@ data: {
       "TRIGGER_CATALOG"
       "TRIGGER_NAME"
       "TRIGGER_SCHEMA"
+      "TUMBLE"
       "TYPE"
       "UNBOUNDED"
       "UNCOMMITTED"
diff --git a/core/src/main/codegen/templates/Parser.jj b/core/src/main/codegen/templates/Parser.jj
index afebba8..3f80693 100644
--- a/core/src/main/codegen/templates/Parser.jj
+++ b/core/src/main/codegen/templates/Parser.jj
@@ -821,6 +821,23 @@ SqlNodeList ParenthesizedQueryOrCommaListWithDefault(
 }
 
 /**
+ * Parses function parameter lists.
+ * If the list starts with DISTINCT or ALL, it is discarded.
+ */
+List UnquantifiedFunctionParameterList(
+    ExprContext exprContext) :
+{
+    final List args;
+}
+{
+    args = FunctionParameterList(exprContext) {
+        final SqlLiteral quantifier = (SqlLiteral) args.get(0);
+        args.remove(0); // remove DISTINCT or ALL, if present
+        return args;
+    }
+}
+
+/**
  * Parses function parameter lists including DISTINCT keyword recognition,
  * DEFAULT, and named argument assignment.
  */
@@ -5267,6 +5284,8 @@ SqlNode BuiltinFunctionCall() :
         node = JsonArrayFunctionCall() { return node; }
     |
         node = JsonArrayAggFunctionCall() { return node; }
+    |
+        node = GroupByWindowingCall() { return node; }
     )
 }
 
@@ -5839,6 +5858,22 @@ SqlCall TimestampDiffFunctionCall() :
     }
 }
 
+/**
+ * Parses a call to a grouping function inside the GROUP BY clause,
+ * for example {@code TUMBLE(rowtime, INTERVAL '1' MINUTE)}.
+ */
+SqlCall GroupByWindowingCall():
+{
+    final Span s;
+    final List<SqlNode> args;
+}
+{
+    <TUMBLE> { s = span(); }
+    args = UnquantifiedFunctionParameterList(ExprContext.ACCEPT_SUB_QUERY) {
+        return SqlStdOperatorTable.TUMBLE.createCall(s.end(this), args);
+    }
+}
+
 SqlCall MatchRecognizeFunctionCall() :
 {
     final SqlCall func;
@@ -7121,6 +7156,7 @@ SqlPostfixOperator PostfixRowOperator() :
 |   < TRIM_ARRAY: "TRIM_ARRAY" >
 |   < TRUE: "TRUE" >
 |   < TRUNCATE: "TRUNCATE" >
+|   < TUMBLE: "TUMBLE" >
 |   < TYPE: "TYPE" >
 |   < UESCAPE: "UESCAPE" >
 |   < UNBOUNDED: "UNBOUNDED" >
diff --git a/core/src/main/java/org/apache/calcite/sql/SqlGroupedWindowFunction.java b/core/src/main/java/org/apache/calcite/sql/SqlGroupedWindowFunction.java
index 59d4242..81b19d3 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlGroupedWindowFunction.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlGroupedWindowFunction.java
@@ -128,6 +128,13 @@ public class SqlGroupedWindowFunction extends SqlFunction {
     // make the method abstract.
     return call.getOperandMonotonicity(0).unstrict();
   }
+
+  @Override public String getName() {
+    // Always rename the name of the SqlKind. The tumble operator is called
+    // "$TUMBLE", so that it does not clash with a user-defined table function
+    // "TUMBLE", but we want the name to be "TUMBLE".
+    return getKind().name();
+  }
 }
 
 // End SqlGroupedWindowFunction.java
diff --git a/core/src/main/java/org/apache/calcite/sql/fun/SqlStdOperatorTable.java b/core/src/main/java/org/apache/calcite/sql/fun/SqlStdOperatorTable.java
index 377f4bb..383a370 100644
--- a/core/src/main/java/org/apache/calcite/sql/fun/SqlStdOperatorTable.java
+++ b/core/src/main/java/org/apache/calcite/sql/fun/SqlStdOperatorTable.java
@@ -2252,9 +2252,21 @@ public class SqlStdOperatorTable extends ReflectiveSqlOperatorTable {
         }
       };
 
-  /** The {@code TUMBLE} group function. */
+  /** The {@code TUMBLE} group function.
+   *
+   * <p>This operator is named "$TUMBLE" (not "TUMBLE") because it is created
+   * directly by the parser, not by looking up an operator by name.
+   *
+   * <p>Why did we add TUMBLE to the parser? Because we plan to support TUMBLE
+   * as a table function (see [CALCITE-3272]); "TUMBLE" as a name will only be
+   * used by the TUMBLE table function.
+   *
+   * <p>After the TUMBLE table function is introduced, we plan to deprecate
+   * this TUMBLE group function, and in fact all group functions. See
+   * [CALCITE-3340] for details.
+   */
   public static final SqlGroupedWindowFunction TUMBLE =
-      new SqlGroupedWindowFunction(SqlKind.TUMBLE.name(), SqlKind.TUMBLE,
+      new SqlGroupedWindowFunction("$TUMBLE", SqlKind.TUMBLE,
           null, ReturnTypes.ARG0, null,
           OperandTypes.or(OperandTypes.DATETIME_INTERVAL,
               OperandTypes.DATETIME_INTERVAL_TIME),
diff --git a/server/src/main/java/org/apache/calcite/sql/ddl/SqlCreateTable.java b/server/src/main/java/org/apache/calcite/sql/ddl/SqlCreateTable.java
index 0e15918..a81e22a 100644
--- a/server/src/main/java/org/apache/calcite/sql/ddl/SqlCreateTable.java
+++ b/server/src/main/java/org/apache/calcite/sql/ddl/SqlCreateTable.java
@@ -207,7 +207,7 @@ public class SqlCreateTable extends SqlCreate
               int iColumn, InitializerContext context) {
             final ColumnDef c = columns.get(iColumn);
             if (c.expr != null) {
-              // REVIEW Danny 2019-10-09: Should we support validation for DDL nodes ?
+              // REVIEW Danny 2019-10-09: Should we support validation for DDL nodes?
               final SqlNode validated = context.validateExpression(storedRowType, c.expr);
               // The explicit specified type should have the same nullability
               // with the column expression inferred type,
diff --git a/site/_docs/reference.md b/site/_docs/reference.md
index 3390ba9..b26674c 100644
--- a/site/_docs/reference.md
+++ b/site/_docs/reference.md
@@ -950,6 +950,7 @@ TRIGGER_SCHEMA,
 **TRIM_ARRAY**,
 **TRUE**,
 **TRUNCATE**,
+TUMBLE,
 TYPE,
 **UESCAPE**,
 UNBOUNDED,