danny0405 commented on a change in pull request #1896: 3877, 3892, 3814 and 3876
URL: https://github.com/apache/calcite/pull/1896#discussion_r403420946
 
 

 ##########
 File path: core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
 ##########
 @@ -1943,24 +1944,43 @@ private RexNode convertOver(Blackboard bb, SqlNode 
node) {
 
     SqlNode windowOrRef = call.operand(1);
     final SqlWindow window =
-        validator.resolveWindow(windowOrRef, bb.scope, true);
+        validator.resolveWindow(windowOrRef, bb.scope);
 
-    // ROW_NUMBER() expects specific kind of framing.
-    if (aggCall.getKind() == SqlKind.ROW_NUMBER) {
-      
window.setLowerBound(SqlWindow.createUnboundedPreceding(SqlParserPos.ZERO));
-      window.setUpperBound(SqlWindow.createCurrentRow(SqlParserPos.ZERO));
-      window.setRows(SqlLiteral.createBoolean(true, SqlParserPos.ZERO));
+    SqlNode sqlLowerBound = window.getLowerBound();
+    SqlNode sqlUpperBound = window.getUpperBound();
+    boolean rows = window.isRows();
+    SqlNodeList orderList = window.getOrderList();
+
+    if (!aggCall.getOperator().allowsFraming()) {
+      // If the operator does not allow framing, bracketing is implicitly
+      // everything up to the current row.
+      sqlLowerBound = SqlWindow.createUnboundedPreceding(SqlParserPos.ZERO);
+      sqlUpperBound = SqlWindow.createCurrentRow(SqlParserPos.ZERO);
+      if (aggCall.getKind() == SqlKind.ROW_NUMBER) {
+        // ROW_NUMBER() expects specific kind of framing.
+        rows = true;
+      }
+    } else if (orderList.size() == 0) {
+      // Without ORDER BY, there must be no bracketing.
+      sqlLowerBound = SqlWindow.createUnboundedPreceding(SqlParserPos.ZERO);
 
 Review comment:
   I checked these for MS-SQL 2017
   ```sql
   create table t(
     a int,
     b int
   );
   
   select max(a) over( rows 2 PRECEDING ) from t; -- syntax error throws
   select max(a) over( order by b rows 2 PRECEDING ) from t; -- this works
   
   select max(a) over(ROWS between UNBOUNDED PRECEDING and CURRENT ROW) from t; 
-- syntax error throws
   select max(a) over(order by b ROWS between UNBOUNDED PRECEDING and CURRENT 
ROW) from t; -- this works
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to