bchong95 commented on code in PR #2953:
URL: https://github.com/apache/calcite/pull/2953#discussion_r1105141461
##########
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:
Done
--
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]