asdf2014 commented on code in PR #16742:
URL: https://github.com/apache/druid/pull/16742#discussion_r1680501194


##########
sql/src/main/java/org/apache/druid/sql/calcite/planner/DruidSqlValidator.java:
##########
@@ -857,4 +861,44 @@ private SqlNode getSqlNodeFor(SqlInsert insert, int idx)
     }
     return src;
   }
+
+  @Override
+  protected void validateHavingClause(SqlSelect select)
+  {
+    super.validateHavingClause(select);
+    SqlNode having = select.getHaving();
+    if (containsOver(having)) {
+      throw buildCalciteContextException("Window functions are not allowed in 
HAVING", having);
+    }
+  }
+
+  private boolean containsOver(SqlNode having)
+  {
+    final Predicate<SqlCall> callPredicate = call -> call.getOperator() 
instanceof SqlOverOperator;
+    return containsCall(having, callPredicate);
+  }
+
+  // copy of SqlUtil#containsCall
+  /** Returns whether an AST tree contains a call that matches a given
+   * predicate. */
+  private static boolean containsCall(SqlNode node,
+      Predicate<SqlCall> callPredicate) {
+    try {
+      SqlVisitor<Void> visitor =
+          new SqlBasicVisitor<Void>() {
+            @Override public Void visit(SqlCall call) {
+              if (callPredicate.test(call)) {
+                throw new Util.FoundOne(call);
+              }
+              return super.visit(call);
+            }
+          };
+      node.accept(visitor);
+      return false;
+    } catch (Util.FoundOne e) {

Review Comment:
   ```suggestion
       }
       catch (Util.FoundOne e) {
   ```



##########
sql/src/main/java/org/apache/druid/sql/calcite/planner/DruidSqlValidator.java:
##########
@@ -857,4 +861,44 @@ private SqlNode getSqlNodeFor(SqlInsert insert, int idx)
     }
     return src;
   }
+
+  @Override
+  protected void validateHavingClause(SqlSelect select)
+  {
+    super.validateHavingClause(select);
+    SqlNode having = select.getHaving();
+    if (containsOver(having)) {
+      throw buildCalciteContextException("Window functions are not allowed in 
HAVING", having);
+    }
+  }
+
+  private boolean containsOver(SqlNode having)
+  {
+    final Predicate<SqlCall> callPredicate = call -> call.getOperator() 
instanceof SqlOverOperator;
+    return containsCall(having, callPredicate);
+  }
+
+  // copy of SqlUtil#containsCall
+  /** Returns whether an AST tree contains a call that matches a given
+   * predicate. */
+  private static boolean containsCall(SqlNode node,
+      Predicate<SqlCall> callPredicate) {
+    try {
+      SqlVisitor<Void> visitor =
+          new SqlBasicVisitor<Void>() {
+            @Override public Void visit(SqlCall call) {

Review Comment:
   ```suggestion
               @Override public Void visit(SqlCall call)
               {
   ```



##########
sql/src/main/java/org/apache/druid/sql/calcite/planner/DruidSqlValidator.java:
##########
@@ -857,4 +861,44 @@ private SqlNode getSqlNodeFor(SqlInsert insert, int idx)
     }
     return src;
   }
+
+  @Override
+  protected void validateHavingClause(SqlSelect select)
+  {
+    super.validateHavingClause(select);
+    SqlNode having = select.getHaving();
+    if (containsOver(having)) {
+      throw buildCalciteContextException("Window functions are not allowed in 
HAVING", having);
+    }
+  }
+
+  private boolean containsOver(SqlNode having)
+  {
+    final Predicate<SqlCall> callPredicate = call -> call.getOperator() 
instanceof SqlOverOperator;
+    return containsCall(having, callPredicate);
+  }
+
+  // copy of SqlUtil#containsCall
+  /** Returns whether an AST tree contains a call that matches a given
+   * predicate. */
+  private static boolean containsCall(SqlNode node,
+      Predicate<SqlCall> callPredicate) {

Review Comment:
   ```suggestion
         Predicate<SqlCall> callPredicate)
     {
   ```



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to