calcite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jh...@apache.org
Subject [15/50] incubator-calcite git commit: [CALCITE-800] Window function defined within another window function should be invalid (Hsuan-Yi Chu)
Date Wed, 02 Sep 2015 22:16:03 GMT
[CALCITE-800] Window function defined within another window function should be invalid (Hsuan-Yi
Chu)

Close apache/incubator-calcite#106


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

Branch: refs/heads/branch-release
Commit: ef5833f3f4b8b6a2296a6f43170981dac03012ad
Parents: d38e6b1
Author: Hsuan-Yi Chu <hsuanyi@usc.edu>
Authored: Mon Jul 13 15:59:39 2015 -0700
Committer: Julian Hyde <jhyde@apache.org>
Committed: Thu Jul 16 17:20:10 2015 -0700

----------------------------------------------------------------------
 .../apache/calcite/runtime/CalciteResource.java |  6 ++++
 .../org/apache/calcite/sql/SqlRankFunction.java |  6 ++--
 .../java/org/apache/calcite/sql/SqlWindow.java  | 15 ++++++++
 .../calcite/sql/fun/SqlStdOperatorTable.java    | 10 +++---
 .../main/java/org/apache/calcite/util/Util.java | 17 +++++++++
 .../calcite/runtime/CalciteResource.properties  |  2 ++
 .../apache/calcite/test/SqlValidatorTest.java   | 36 +++++++++++++++++++-
 7 files changed, 84 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/ef5833f3/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java b/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java
index ee3cb5a..007a410 100644
--- a/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java
+++ b/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java
@@ -332,6 +332,12 @@ public interface CalciteResource {
   @BaseMessage("Window specification must contain an ORDER BY clause")
   ExInst<SqlValidatorException> overMissingOrderBy();
 
+  @BaseMessage("PARTITION BY expression should not contain OVER clause")
+  ExInst<SqlValidatorException> partitionbyShouldNotContainOver();
+
+  @BaseMessage("ORDER BY expression should not contain OVER clause")
+  ExInst<SqlValidatorException> orderbyShouldNotContainOver();
+
   @BaseMessage("UNBOUNDED FOLLOWING cannot be specified for the lower frame boundary")
   ExInst<SqlValidatorException> badLowerBoundary();
 

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/ef5833f3/core/src/main/java/org/apache/calcite/sql/SqlRankFunction.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/sql/SqlRankFunction.java b/core/src/main/java/org/apache/calcite/sql/SqlRankFunction.java
index 85712d3..11b5ca1 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlRankFunction.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlRankFunction.java
@@ -31,11 +31,12 @@ import java.util.List;
 public class SqlRankFunction extends SqlAggFunction {
   //~ Instance fields --------------------------------------------------------
 
+  private final boolean requiresOrder;
   private final RelDataType type = null;
 
   //~ Constructors -----------------------------------------------------------
 
-  public SqlRankFunction(String name) {
+  public SqlRankFunction(String name, boolean requiresOrder) {
     super(
         name,
         SqlKind.OTHER_FUNCTION,
@@ -43,12 +44,13 @@ public class SqlRankFunction extends SqlAggFunction {
         null,
         OperandTypes.NILADIC,
         SqlFunctionCategory.NUMERIC);
+    this.requiresOrder = requiresOrder;
   }
 
   //~ Methods ----------------------------------------------------------------
 
   @Override public boolean requiresOrder() {
-    return true;
+    return requiresOrder;
   }
 
   @Override public boolean allowsFraming() {

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/ef5833f3/core/src/main/java/org/apache/calcite/sql/SqlWindow.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/sql/SqlWindow.java b/core/src/main/java/org/apache/calcite/sql/SqlWindow.java
index a8ae456..40ba4ee 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlWindow.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlWindow.java
@@ -27,6 +27,7 @@ import org.apache.calcite.sql.util.SqlVisitor;
 import org.apache.calcite.sql.validate.SqlValidator;
 import org.apache.calcite.sql.validate.SqlValidatorScope;
 import org.apache.calcite.sql.validate.SqlValidatorUtil;
+import org.apache.calcite.util.ControlFlowException;
 import org.apache.calcite.util.ImmutableNullableList;
 import org.apache.calcite.util.Util;
 
@@ -523,6 +524,13 @@ public class SqlWindow extends SqlCall {
     }
 
     for (SqlNode partitionItem : partitionList) {
+      try {
+        partitionItem.accept(Util.OverFinder.INSTANCE);
+      } catch (ControlFlowException e) {
+        throw validator.newValidationError(this,
+            RESOURCE.partitionbyShouldNotContainOver());
+      }
+
       partitionItem.validateExpr(validator, operandScope);
     }
 
@@ -531,6 +539,13 @@ public class SqlWindow extends SqlCall {
           validator.getColumnReferenceExpansion();
       validator.setColumnReferenceExpansion(false);
       try {
+        orderItem.accept(Util.OverFinder.INSTANCE);
+      } catch (ControlFlowException e) {
+        throw validator.newValidationError(this,
+            RESOURCE.orderbyShouldNotContainOver());
+      }
+
+      try {
         orderItem.validateExpr(validator, scope);
       } finally {
         validator.setColumnReferenceExpansion(

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/ef5833f3/core/src/main/java/org/apache/calcite/sql/fun/SqlStdOperatorTable.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 7a724b9..fa5fda5 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
@@ -850,30 +850,30 @@ public class SqlStdOperatorTable extends ReflectiveSqlOperatorTable
{
    * <code>CUME_DIST</code> Window function.
    */
   public static final SqlRankFunction CUME_DIST =
-      new SqlRankFunction("CUME_DIST");
+      new SqlRankFunction("CUME_DIST", true);
 
   /**
    * <code>DENSE_RANK</code> Window function.
    */
   public static final SqlRankFunction DENSE_RANK =
-      new SqlRankFunction("DENSE_RANK");
+      new SqlRankFunction("DENSE_RANK", true);
 
   /**
    * <code>PERCENT_RANK</code> Window function.
    */
   public static final SqlRankFunction PERCENT_RANK =
-      new SqlRankFunction("PERCENT_RANK");
+      new SqlRankFunction("PERCENT_RANK", true);
 
   /**
    * <code>RANK</code> Window function.
    */
-  public static final SqlRankFunction RANK = new SqlRankFunction("RANK");
+  public static final SqlRankFunction RANK = new SqlRankFunction("RANK", true);
 
   /**
    * <code>ROW_NUMBER</code> Window function.
    */
   public static final SqlRankFunction ROW_NUMBER =
-      new SqlRankFunction("ROW_NUMBER");
+      new SqlRankFunction("ROW_NUMBER", false);
 
   //-------------------------------------------------------------
   //                   SPECIAL OPERATORS

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/ef5833f3/core/src/main/java/org/apache/calcite/util/Util.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/util/Util.java b/core/src/main/java/org/apache/calcite/util/Util.java
index f739fbd..040775f 100644
--- a/core/src/main/java/org/apache/calcite/util/Util.java
+++ b/core/src/main/java/org/apache/calcite/util/Util.java
@@ -21,10 +21,12 @@ import org.apache.calcite.linq4j.Ord;
 import org.apache.calcite.runtime.CalciteException;
 import org.apache.calcite.sql.SqlAggFunction;
 import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlKind;
 import org.apache.calcite.sql.SqlLiteral;
 import org.apache.calcite.sql.SqlNode;
 import org.apache.calcite.sql.SqlValuesOperator;
 import org.apache.calcite.sql.fun.SqlRowOperator;
+import org.apache.calcite.sql.util.SqlBasicVisitor;
 
 import com.google.common.base.Function;
 import com.google.common.base.Objects;
@@ -2175,6 +2177,21 @@ public class Util {
       return node;
     }
   }
+
+  /**
+   * Visitor which looks for an OVER clause inside a tree of
+   * {@link SqlNode} objects.
+   */
+  public static class OverFinder extends SqlBasicVisitor<Void> {
+    public static final OverFinder INSTANCE = new Util.OverFinder();
+
+    @Override public Void visit(SqlCall call) {
+      if (call.getKind() == SqlKind.OVER) {
+        throw FoundOne.NULL;
+      }
+      return super.visit(call);
+    }
+  }
 }
 
 // End Util.java

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/ef5833f3/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties
----------------------------------------------------------------------
diff --git a/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties
b/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties
index e170b67..3aec262 100644
--- a/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties
+++ b/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties
@@ -113,6 +113,8 @@ OrderByRangeMismatch=Data Type mismatch between ORDER BY and RANGE clause
 DateRequiresInterval=Window ORDER BY expression of type DATE requires range of type INTERVAL
 RowMustBeNonNegativeIntegral=ROWS value must be a non-negative integral constant
 OverMissingOrderBy=Window specification must contain an ORDER BY clause
+PartitionbyShouldNotContainOver=PARTITION BY expression should not contain OVER clause
+OrderbyShouldNotContainOver=ORDER BY expression should not contain OVER clause
 BadLowerBoundary=UNBOUNDED FOLLOWING cannot be specified for the lower frame boundary
 BadUpperBoundary=UNBOUNDED PRECEDING cannot be specified for the upper frame boundary
 CurrentRowPrecedingError=Upper frame boundary cannot be PRECEDING when lower boundary is
CURRENT ROW

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/ef5833f3/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
index cc8153e..76413ec 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
@@ -3801,6 +3801,32 @@ public class SqlValidatorTest extends SqlValidatorTestCase {
     // Test specified collation, window clause syntax rule 4,5.
   }
 
+  @Test public void testOverInPartitionBy() {
+    winSql(
+        "select sum(deptno) over ^(partition by sum(deptno) \n"
+        + "over(order by deptno))^ from emp")
+        .fails("PARTITION BY expression should not contain OVER clause");
+
+    winSql(
+        "select sum(deptno) over w \n"
+        + "from emp \n"
+        + "window w as ^(partition by sum(deptno) over(order by deptno))^")
+        .fails("PARTITION BY expression should not contain OVER clause");
+  }
+
+  @Test public void testOverInOrderBy() {
+    winSql(
+        "select sum(deptno) over ^(order by sum(deptno) \n"
+        + "over(order by deptno))^ from emp")
+        .fails("ORDER BY expression should not contain OVER clause");
+
+    winSql(
+        "select sum(deptno) over w \n"
+        + "from emp \n"
+        + "window w as ^(order by sum(deptno) over(order by deptno))^")
+        .fails("ORDER BY expression should not contain OVER clause");
+  }
+
   @Test public void testWindowFunctions() {
     // SQL 03 Section 6.10
 
@@ -3859,8 +3885,15 @@ public class SqlValidatorTest extends SqlValidatorTestCase {
             "RANK or DENSE_RANK functions require ORDER BY clause in window specification");
     winSql("select rank() over w2 from emp\n"
         + "window w as (partition by sal), w2 as (w order by deptno)").ok();
+
     // row_number function
     winExp("row_number() over (order by deptno)").ok();
+    winExp("row_number() over (partition by deptno)").ok();
+    winExp("row_number() over ()").ok();
+    winExp("row_number() over (order by deptno ^rows^ 2 preceding)")
+        .fails("ROW/RANGE not allowed with RANK or DENSE_RANK functions");
+    winExp("row_number() over (order by deptno ^range^ 2 preceding)")
+        .fails("ROW/RANGE not allowed with RANK or DENSE_RANK functions");
 
     // rank function type
     if (defined.contains("DENSE_RANK")) {
@@ -4187,7 +4220,8 @@ public class SqlValidatorTest extends SqlValidatorTestCase {
         "Expression 'COMM' is not being grouped");
 
     // syntax rule 7
-    win("window w as (order by rank() over (order by sal))").ok();
+    win("window w as ^(order by rank() over (order by sal))^")
+        .fails("ORDER BY expression should not contain OVER clause");
 
     // ------------------------------------
     // ---- window frame between tests ----


Mime
View raw message