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 2015/03/11 07:59:21 UTC

[1/3] incubator-calcite git commit: Further fix for [CALCITE-588]

Repository: incubator-calcite
Updated Branches:
  refs/heads/master 3eb6515d6 -> 469e5fc12


Further fix for [CALCITE-588]


Project: http://git-wip-us.apache.org/repos/asf/incubator-calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-calcite/commit/abe76e3d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-calcite/tree/abe76e3d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-calcite/diff/abe76e3d

Branch: refs/heads/master
Commit: abe76e3d1e9b823518b7264a359c448641b7b14c
Parents: 3eb6515
Author: Julian Hyde <jh...@apache.org>
Authored: Tue Mar 10 15:37:42 2015 -0700
Committer: Julian Hyde <jh...@apache.org>
Committed: Tue Mar 10 20:38:33 2015 -0700

----------------------------------------------------------------------
 .../java/org/apache/calcite/rex/RexLiteral.java | 35 ++++++++++++++++
 .../sql/validate/SqlUserDefinedTableMacro.java  |  4 +-
 .../java/org/apache/calcite/util/Litmus.java    | 42 ++++++++++++++++++++
 .../java/org/apache/calcite/test/JdbcTest.java  |  4 ++
 4 files changed, 84 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/abe76e3d/core/src/main/java/org/apache/calcite/rex/RexLiteral.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/rex/RexLiteral.java b/core/src/main/java/org/apache/calcite/rex/RexLiteral.java
index 5e879a1..767734d 100644
--- a/core/src/main/java/org/apache/calcite/rex/RexLiteral.java
+++ b/core/src/main/java/org/apache/calcite/rex/RexLiteral.java
@@ -26,6 +26,7 @@ import org.apache.calcite.sql.fun.SqlStdOperatorTable;
 import org.apache.calcite.sql.parser.SqlParserUtil;
 import org.apache.calcite.sql.type.SqlTypeName;
 import org.apache.calcite.util.ConversionUtil;
+import org.apache.calcite.util.Litmus;
 import org.apache.calcite.util.NlsString;
 import org.apache.calcite.util.SaffronProperties;
 import org.apache.calcite.util.Util;
@@ -40,6 +41,8 @@ import java.math.BigDecimal;
 import java.nio.ByteBuffer;
 import java.nio.charset.Charset;
 import java.util.Calendar;
+import java.util.List;
+import java.util.Map;
 import java.util.TimeZone;
 
 /**
@@ -255,6 +258,38 @@ public class RexLiteral extends RexNode {
     return sw.toString();
   }
 
+  /** Returns whether a value is valid as a constant value, using the same
+   * criteria as {@link #valueMatchesType}. */
+  public static boolean validConstant(Object o, Litmus litmus) {
+    if (o == null
+        || o instanceof BigDecimal
+        || o instanceof NlsString
+        || o instanceof ByteString) {
+      return litmus.succeed();
+    } else if (o instanceof List) {
+      List list = (List) o;
+      for (Object o1 : list) {
+        if (!validConstant(o1, litmus)) {
+          return litmus.fail("not a constant: " + o1);
+        }
+      }
+      return litmus.succeed();
+    } else if (o instanceof Map) {
+      final Map<Object, Object> map = (Map) o;
+      for (Map.Entry entry : map.entrySet()) {
+        if (!validConstant(entry.getKey(), litmus)) {
+          return litmus.fail("not a constant: " + entry.getKey());
+        }
+        if (!validConstant(entry.getValue(), litmus)) {
+          return litmus.fail("not a constant: " + entry.getValue());
+        }
+      }
+      return litmus.succeed();
+    } else {
+      return litmus.fail("not a constant: " + o);
+    }
+  }
+
   /**
    * Prints the value this literal as a Java string constant.
    */

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/abe76e3d/core/src/main/java/org/apache/calcite/sql/validate/SqlUserDefinedTableMacro.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/sql/validate/SqlUserDefinedTableMacro.java b/core/src/main/java/org/apache/calcite/sql/validate/SqlUserDefinedTableMacro.java
index 1c6c3f3..3e670aa 100644
--- a/core/src/main/java/org/apache/calcite/sql/validate/SqlUserDefinedTableMacro.java
+++ b/core/src/main/java/org/apache/calcite/sql/validate/SqlUserDefinedTableMacro.java
@@ -129,7 +129,9 @@ public class SqlUserDefinedTableMacro extends SqlFunction {
           ImmutableMap.builder();
       final List<SqlNode> operands = ((SqlCall) right).getOperandList();
       for (int i = 0; i < operands.size(); i += 2) {
-        builder2.put(operands.get(i), operands.get(i + 1));
+        final SqlNode key = operands.get(i);
+        final SqlNode value = operands.get(i + 1);
+        builder2.put(getValue(key), getValue(value));
       }
       return builder2.build();
     default:

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/abe76e3d/core/src/main/java/org/apache/calcite/util/Litmus.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/util/Litmus.java b/core/src/main/java/org/apache/calcite/util/Litmus.java
new file mode 100644
index 0000000..e7e2bbd
--- /dev/null
+++ b/core/src/main/java/org/apache/calcite/util/Litmus.java
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.util;
+
+/**
+ * Callback to be called when a test for validity succeeds or fails.
+ */
+public interface Litmus {
+  /** Implementation of {@link org.apache.calcite.util.Litmus} that throws
+   * an {@link java.lang.AssertionError} on failure. */
+  Litmus THROW = new Litmus() {
+    @Override public boolean fail(String message) {
+      throw new AssertionError(message);
+    }
+
+    @Override public boolean succeed() {
+      return true;
+    }
+  };
+
+  /** Called when test fails. Returns false or throws. */
+  boolean fail(String message);
+
+  /** Called when test succeeds. Returns true. */
+  boolean succeed();
+}
+
+// End Litmus.java

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/abe76e3d/core/src/test/java/org/apache/calcite/test/JdbcTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/JdbcTest.java b/core/src/test/java/org/apache/calcite/test/JdbcTest.java
index 890fd08..0942334 100644
--- a/core/src/test/java/org/apache/calcite/test/JdbcTest.java
+++ b/core/src/test/java/org/apache/calcite/test/JdbcTest.java
@@ -54,6 +54,7 @@ import org.apache.calcite.rel.logical.LogicalTableModify;
 import org.apache.calcite.rel.type.RelDataType;
 import org.apache.calcite.rel.type.RelDataTypeFactory;
 import org.apache.calcite.rel.type.RelProtoDataType;
+import org.apache.calcite.rex.RexLiteral;
 import org.apache.calcite.runtime.Hook;
 import org.apache.calcite.runtime.SqlFunctions;
 import org.apache.calcite.schema.ModifiableTable;
@@ -78,6 +79,7 @@ import org.apache.calcite.sql.advise.SqlAdvisorGetHintsFunction;
 import org.apache.calcite.sql.parser.SqlParserUtil;
 import org.apache.calcite.sql.type.SqlTypeName;
 import org.apache.calcite.util.Bug;
+import org.apache.calcite.util.Litmus;
 import org.apache.calcite.util.Pair;
 import org.apache.calcite.util.Util;
 
@@ -6185,6 +6187,8 @@ public class JdbcTest {
   }
 
   public static TranslatableTable str(Object o, Object p) {
+    assertThat(RexLiteral.validConstant(o, Litmus.THROW), is(true));
+    assertThat(RexLiteral.validConstant(p, Litmus.THROW), is(true));
     return new ViewTable(Object.class,
         new RelProtoDataType() {
           public RelDataType apply(RelDataTypeFactory typeFactory) {


[3/3] incubator-calcite git commit: [CALCITE-612] Update AvaticaStatement to handle cancelled queries (Parth Chandra)

Posted by jh...@apache.org.
[CALCITE-612] Update AvaticaStatement to handle cancelled queries (Parth Chandra)


Project: http://git-wip-us.apache.org/repos/asf/incubator-calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-calcite/commit/469e5fc1
Tree: http://git-wip-us.apache.org/repos/asf/incubator-calcite/tree/469e5fc1
Diff: http://git-wip-us.apache.org/repos/asf/incubator-calcite/diff/469e5fc1

Branch: refs/heads/master
Commit: 469e5fc1231ee93db333c47222ecf4b6533f2c6b
Parents: 1b8b6b4
Author: Julian Hyde <jh...@apache.org>
Authored: Tue Mar 10 23:31:23 2015 -0700
Committer: Julian Hyde <jh...@apache.org>
Committed: Tue Mar 10 23:31:23 2015 -0700

----------------------------------------------------------------------
 .../main/java/org/apache/calcite/avatica/AvaticaStatement.java   | 4 ++++
 1 file changed, 4 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/469e5fc1/avatica/src/main/java/org/apache/calcite/avatica/AvaticaStatement.java
----------------------------------------------------------------------
diff --git a/avatica/src/main/java/org/apache/calcite/avatica/AvaticaStatement.java b/avatica/src/main/java/org/apache/calcite/avatica/AvaticaStatement.java
index 58bf287..37242c2 100644
--- a/avatica/src/main/java/org/apache/calcite/avatica/AvaticaStatement.java
+++ b/avatica/src/main/java/org/apache/calcite/avatica/AvaticaStatement.java
@@ -376,6 +376,10 @@ public abstract class AvaticaStatement
   protected boolean executeInternal(Meta.Signature signature)
       throws SQLException {
     ResultSet resultSet = executeQueryInternal(signature);
+    // user may have cancelled the query
+    if (resultSet.isClosed()) {
+      return false;
+    }
     return true;
   }
 


[2/3] incubator-calcite git commit: Add sliding and cascading windows to stream specification

Posted by jh...@apache.org.
Add sliding and cascading windows to stream specification


Project: http://git-wip-us.apache.org/repos/asf/incubator-calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-calcite/commit/1b8b6b4d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-calcite/tree/1b8b6b4d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-calcite/diff/1b8b6b4d

Branch: refs/heads/master
Commit: 1b8b6b4dd11998a7cd49e1a77e5f1fa571bd71f6
Parents: abe76e3
Author: Julian Hyde <jh...@apache.org>
Authored: Mon Mar 9 13:04:19 2015 -0700
Committer: Julian Hyde <jh...@apache.org>
Committed: Tue Mar 10 21:36:58 2015 -0700

----------------------------------------------------------------------
 doc/STREAM.md   | 100 +++++++++++++++++++++++++++++++++++++++++++++++++--
 doc/TUTORIAL.md |   2 +-
 2 files changed, 98 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/1b8b6b4d/doc/STREAM.md
----------------------------------------------------------------------
diff --git a/doc/STREAM.md b/doc/STREAM.md
index b0981fd..f324b37 100644
--- a/doc/STREAM.md
+++ b/doc/STREAM.md
@@ -83,10 +83,10 @@ an error:
 
 ```sql
 > SELECT * FROM Shipments;
-ERROR: Cannot convert table 'SHIPMENTS' to a stream
+ERROR: Cannot convert stream 'SHIPMENTS' to a table
 
 > SELECT STREAM * FROM Products;
-ERROR: Cannot convert stream 'PRODUCTS' to a table
+ERROR: Cannot convert table 'PRODUCTS' to a stream
 ```
 
 # Filtering rows
@@ -486,6 +486,89 @@ would never return any rows.
 ERROR: Cannot stream VALUES
 ```
 
+## Sliding windows
+
+Standard SQL features so-called "analytic functions" that can be used in the
+`SELECT` clause. Unlike `GROUP BY`, these do not collapse records. For each
+record that goes in, one record comes out. But the aggregate function is based
+on a window of many rows.
+
+Let's look at an example.
+
+```sql
+SELECT STREAM rowtime,
+  productId,
+  units,
+  SUM(units) OVER (ORDER BY rowtime RANGE INTERVAL '1' HOUR PRECEDING) unitsLastHour
+FROM Orders;
+```
+
+The feature packs a lot of power with little effort. You can have multiple
+functions in the `SELECT` clause, based on multiple window specifications.
+
+The following example returns orders whose average order size over the last
+10 minutes is greater than the average order size for the last week.
+
+```sql
+SELECT STREAM *
+FROM (
+  SELECT STREAM rowtime,
+    productId,
+    units,
+    AVG(units) OVER product (RANGE INTERVAL '10' MINUTE PRECEDING) AS m10,
+    AVG(units) OVER product (RANGE INTERVAL '7' DAY PRECEDING) AS d7
+  FROM Orders
+  WINDOW product AS (
+    ORDER BY rowtime
+    PARTITION BY productId))
+WHERE m10 > d7;
+```
+
+For conciseness, here we use a syntax where you partially define a window
+using a `WINDOW` clause and then refine the window in each `OVER` clause.
+You could also define all windows in the `WINDOW` clause, or all windows inline,
+if you wish.
+
+But the real power goes beyond syntax. Behind the scenes, this query is
+maintaining two tables, and adding and removing values from sub-totals using
+with FIFO queues. But you can access those tables without introducing a join
+into the query.
+
+Some other features of the windowed aggregation syntax:
+* You can define windows based on row count.
+* The window can reference rows that have not yet arrived.
+  (The stream will wait until they have arrived).
+* You can compute order-dependent functions such as `RANK` and median.
+
+## Cascading windows
+
+What if we want a query that returns a result for every record, like a
+sliding window, but resets totals on a fixed time period, like a
+tumbling window? Such a pattern is called a *cascading window*. Here
+is an example:
+
+```sql
+SELECT STREAM rowtime,
+  productId,
+  units,
+  SUM(units) OVER (PARTITION BY FLOOR(rowtime TO HOUR)) AS unitsSinceTopOfHour
+FROM Orders;
+```
+
+It looks similar to a sliding window query, but the monotonic
+expression occurs within the `PARTITION BY` clause of the window. As
+the rowtime moves from from 10:59:59 to 11:00:00, `FLOOR(rowtime TO
+HOUR)` changes from 10:00:00 to 11:00:00, and therefore a new
+partition starts. The first row to arrive in the new hour will start a
+new total; the second row will have a total that consists of two rows,
+and so on.
+
+Calcite knows that the old partition will never be used again, so
+removes all sub-totals for that partition from its internal storage.
+
+Analytic functions that using cascading and sliding windows can be
+combined in the same query.
+
 ## State of the stream
 
 Not all concepts in this article have been implemented in Calcite.
@@ -504,12 +587,23 @@ such as Samza SQL [<a href="#ref3">3</a>].
 * Stream on view
 * Streaming UNION ALL with ORDER BY (merge)
 * Relational query on stream
-* Streaming windowed aggregation
+* Streaming windowed aggregation (sliding and cascading windows)
 * Check that STREAM in sub-queries and views is ignored
 * Check that streaming ORDER BY cannot have OFFSET or LIMIT
 * Limited history; at run time, check that there is sufficient history
   to run the query.
 
+### To do in this document
+* Re-visit whether you can stream VALUES
+* OVER clause to define window on stream
+* Windowed aggregation
+* Punctuation
+* Stream-to-table join
+** Stream-to-table join where table is changing
+* Stream-to-stream join
+* Relational queries on streams (e.g. "pie chart" query)
+* Diagrams for various window types
+
 ## References
 
 * [<a name="ref1">1</a>]

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/1b8b6b4d/doc/TUTORIAL.md
----------------------------------------------------------------------
diff --git a/doc/TUTORIAL.md b/doc/TUTORIAL.md
index 40a5982..d12fbeb 100644
--- a/doc/TUTORIAL.md
+++ b/doc/TUTORIAL.md
@@ -260,7 +260,7 @@ the tables <code>EMPS</code> and <code>DEPTS</code>.
 ## Tables and views in schemas
 
 Note how we did not need to define any tables in the model; the schema
-generated the tables automatically. 
+generated the tables automatically.
 
 You can define extra tables,
 beyond those that are created automatically,