julianhyde commented on code in PR #2953:
URL: https://github.com/apache/calcite/pull/2953#discussion_r1103296651


##########
core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties:
##########
@@ -333,4 +333,5 @@ InvalidPartitionKeys=Only tables with set semantics may be 
partitioned. Invalid
 InvalidOrderBy=Only tables with set semantics may be ordered. Invalid ORDER BY 
clause in the {0,number,#}-th operand of table function ''{1}''
 MultipleRowSemanticsTables=A table function at most has one input table with 
row semantics. Table function ''{0}'' has multiple input tables with row 
semantics
 NoOperator=No operator for ''{0}'' with kind: ''{1}'', syntax: ''{2}'' during 
JSON deserialization
+QualifyExpressionMustContainWindowFunction=QUALIFY expression ''{0}'' must 
contain a window function.

Review Comment:
   Don't add to end of file. It causes conflicts. Add in the most logical place.
   
   Error messages don't end in '.'



##########
core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java:
##########
@@ -7355,4 +7465,11 @@ private enum Clause {
     ORDER,
     CURSOR
   }
+
+  /** Which clause is being expanded. */

Review Comment:
   could you just reuse `enum Clause`? Certainly there's no need to make it 
`public`.



##########
core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java:
##########
@@ -6788,65 +6873,90 @@ static class SelectExpander extends Expander {
   static class ExtendedExpander extends Expander {
     final SqlSelect select;
     final SqlNode root;
-    final boolean havingExpr;
+    final ExpansionClause clause;
 
     ExtendedExpander(SqlValidatorImpl validator, SqlValidatorScope scope,
-        SqlSelect select, SqlNode root, boolean havingExpr) {
+        SqlSelect select, SqlNode root, ExpansionClause clause) {
       super(validator, scope);
       this.select = select;
       this.root = root;
-      this.havingExpr = havingExpr;
+      this.clause = clause;
     }
 
     @Override public @Nullable SqlNode visit(SqlIdentifier id) {
-      if (id.isSimple()
-          && (havingExpr
-              ? validator.config().conformance().isHavingAlias()
-              : validator.config().conformance().isGroupByAlias())) {
-        String name = id.getSimple();
-        SqlNode expr = null;
-        final SqlNameMatcher nameMatcher =
-            validator.catalogReader.nameMatcher();
-        int n = 0;
-        for (SqlNode s : SqlNonNullableAccessors.getSelectList(select)) {
-          final @Nullable String alias = SqlValidatorUtil.alias(s);
-          if (alias != null && nameMatcher.matches(alias, name)) {
-            expr = s;
-            n++;
-          }
-        }
-        if (n == 0) {
-          return super.visit(id);
-        } else if (n > 1) {
-          // More than one column has this alias.
-          throw validator.newValidationError(id,
-              RESOURCE.columnAmbiguous(name));
-        }
-        if (havingExpr && validator.isAggregate(root)) {
-          return super.visit(id);
-        }
-        expr = stripAs(expr);
-        if (expr instanceof SqlIdentifier) {
-          SqlIdentifier sid = (SqlIdentifier) expr;
-          final SqlIdentifier fqId = getScope().fullyQualify(sid).identifier;
-          expr = expandDynamicStar(sid, fqId);
-        }
-        return expr;
+      if (!id.isSimple()) {
+        return super.visit(id);
       }
-      if (id.isSimple()) {
+
+      boolean replaceAliases;
+      switch (clause) {
+      case GROUP_BY:
+        replaceAliases = validator.config().conformance().isGroupByAlias();
+        break;
+
+      case HAVING:
+        replaceAliases = validator.config().conformance().isHavingAlias();
+        break;
+
+      case QUALIFY:
+        replaceAliases = true;
+        break;
+
+      default:
+        throw Util.unexpected(clause);
+      }
+
+      if (!replaceAliases) {
         final SelectScope scope = validator.getRawSelectScope(select);
         SqlNode node = expandCommonColumn(select, id, scope, validator);
         if (node != id) {
           return node;
         }
+
+        return super.visit(id);
+      }
+
+      String name = id.getSimple();
+      SqlNode expr = null;
+      final SqlNameMatcher nameMatcher =
+          validator.catalogReader.nameMatcher();
+      int n = 0;
+      for (SqlNode s : SqlNonNullableAccessors.getSelectList(select)) {
+        final @Nullable String alias = SqlValidatorUtil.alias(s);
+        if (alias != null && nameMatcher.matches(alias, name)) {
+          expr = s;
+          n++;
+        }
       }
-      return super.visit(id);
+
+      if (n == 0) {
+        return super.visit(id);
+      } else if (n > 1) {
+        // More than one column has this alias.
+        throw validator.newValidationError(id,
+            RESOURCE.columnAmbiguous(name));
+      }
+
+      if ((clause == ExpansionClause.HAVING) && validator.isAggregate(root)) {
+        return super.visit(id);
+      }
+
+      expr = stripAs(expr);
+      if (expr instanceof SqlIdentifier) {
+        SqlIdentifier sid = (SqlIdentifier) expr;
+        final SqlIdentifier fqId = getScope().fullyQualify(sid).identifier;
+        expr = expandDynamicStar(sid, fqId);
+      }
+
+      return expr;
     }
 
     @Override public @Nullable SqlNode visit(SqlLiteral literal) {
-      if (havingExpr || !validator.config().conformance().isGroupByOrdinal()) {
+      boolean expandGroupByOrdinal = (clause == ExpansionClause.GROUP_BY) && 
validator.config().conformance().isGroupByOrdinal();

Review Comment:
   let's keep the lines reasonably short



##########
core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java:
##########
@@ -4187,6 +4194,82 @@ protected void validateWindowClause(SqlSelect select) {
     windowList.validate(this, windowScope);
   }
 
+  protected void validateQualifyClause(SqlSelect select) {
+    SqlNode qualifyNode = select.getQualify();
+    if (qualifyNode == null) {
+      return;
+    }
+
+    SqlValidatorScope qualifyScope = getSelectScope(select);
+
+    qualifyNode = extendedExpand(qualifyNode, qualifyScope, select, 
ExpansionClause.QUALIFY);
+    select.setQualify(qualifyNode);
+
+    inferUnknownTypes(
+        booleanType,
+        qualifyScope,
+        qualifyNode);
+
+    qualifyNode.validate(this, qualifyScope);
+
+    final RelDataType type = deriveType(qualifyScope, qualifyNode);
+    if (!SqlTypeUtil.inBooleanFamily(type)) {
+      throw newValidationError(qualifyNode, 
RESOURCE.condMustBeBoolean("QUALIFY"));
+    }
+
+    boolean qualifyContainsWindowFunction = 
qualifyNode.accept(WindowFunctionDetector.INSTANCE);
+    if (!qualifyContainsWindowFunction) {
+      throw newValidationError(qualifyNode,
+          
RESOURCE.qualifyExpressionMustContainWindowFunction(qualifyNode.toString()));
+    }
+  }
+
+  /** Detects OVER. */

Review Comment:
   I'm surprised this class is needed. Doesn't `AggVisitor` (with `over=true`) 
not do the job?



##########
core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java:
##########
@@ -6788,65 +6873,90 @@ static class SelectExpander extends Expander {
   static class ExtendedExpander extends Expander {
     final SqlSelect select;
     final SqlNode root;
-    final boolean havingExpr;
+    final ExpansionClause clause;
 
     ExtendedExpander(SqlValidatorImpl validator, SqlValidatorScope scope,
-        SqlSelect select, SqlNode root, boolean havingExpr) {
+        SqlSelect select, SqlNode root, ExpansionClause clause) {
       super(validator, scope);
       this.select = select;
       this.root = root;
-      this.havingExpr = havingExpr;
+      this.clause = clause;
     }
 
     @Override public @Nullable SqlNode visit(SqlIdentifier id) {
-      if (id.isSimple()
-          && (havingExpr
-              ? validator.config().conformance().isHavingAlias()
-              : validator.config().conformance().isGroupByAlias())) {
-        String name = id.getSimple();
-        SqlNode expr = null;
-        final SqlNameMatcher nameMatcher =
-            validator.catalogReader.nameMatcher();
-        int n = 0;
-        for (SqlNode s : SqlNonNullableAccessors.getSelectList(select)) {
-          final @Nullable String alias = SqlValidatorUtil.alias(s);
-          if (alias != null && nameMatcher.matches(alias, name)) {
-            expr = s;
-            n++;
-          }
-        }
-        if (n == 0) {
-          return super.visit(id);
-        } else if (n > 1) {
-          // More than one column has this alias.
-          throw validator.newValidationError(id,
-              RESOURCE.columnAmbiguous(name));
-        }
-        if (havingExpr && validator.isAggregate(root)) {
-          return super.visit(id);
-        }
-        expr = stripAs(expr);
-        if (expr instanceof SqlIdentifier) {
-          SqlIdentifier sid = (SqlIdentifier) expr;
-          final SqlIdentifier fqId = getScope().fullyQualify(sid).identifier;
-          expr = expandDynamicStar(sid, fqId);
-        }
-        return expr;
+      if (!id.isSimple()) {
+        return super.visit(id);
       }
-      if (id.isSimple()) {
+
+      boolean replaceAliases;

Review Comment:
   move the `switch` into a function, document it, make `replaceAliases` 
`final`.



##########
core/src/main/java/org/apache/calcite/runtime/CalciteResource.java:
##########
@@ -1019,4 +1019,6 @@ ExInstWithCause<CalciteException> failedToAccessField(
   @BaseMessage("A table function at most has one input table with row 
semantics. Table function ''{0}'' has multiple input tables with row semantics")
   ExInst<SqlValidatorException> multipleRowSemanticsTables(String funcName);
 
+  @BaseMessage("QUALIFY expression ''{0}'' must contain a window function.")
+  ExInst<SqlValidatorException> 
qualifyExpressionMustContainWindowFunction(String a0);

Review Comment:
   don't put this at end of file. That creates conflicts. Put it into the most 
logical place.



##########
core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java:
##########
@@ -2749,6 +2749,12 @@ private void registerQuery(
           select,
           SqlSelect.WHERE_OPERAND);
 
+      // Register subqueries in the qualify clause

Review Comment:
   s/qualify/QUALIFY/



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

Reply via email to