gortiz commented on code in PR #20298:
URL: https://github.com/apache/pinot/pull/20298#discussion_r4630688954


##########
pinot-query-planner/src/test/resources/queries/GroupByPlans.json:
##########
@@ -506,6 +506,19 @@
           "\n"
         ]
       },
+      {
+        "description": "Group by an alias that is not a column and uses 
another alias is unchanged with preferSelectAliasInGroupBy",

Review Comment:
   This covers the first example in the description. Could we also add the 
`USING` one (`SELECT UPPER(col1) AS u, COUNT(*) FROM a JOIN b USING (col1) 
GROUP BY u` with the option set)? It is the other case this fix targets, and it 
goes through a different path (`findQualifyingTableNames` returns two tables).



##########
pinot-query-planner/src/main/java/org/apache/pinot/query/validate/Validator.java:
##########
@@ -180,38 +164,33 @@ private SqlNode rewriteGroupByName(SqlIdentifier 
identifier, List<SqlNode> selec
     if (expr instanceof SqlIdentifier && 
nameMatcher.matches(Util.last(((SqlIdentifier) expr).names), name)) {
       return identifier;
     }
-    aliasNames.add(name);
+    SelectScope selectScope = getRawSelectScope(select);
+    if (selectScope == null || selectScope.findQualifyingTableNames(name, 
select, nameMatcher).size() != 1) {

Review Comment:
   `ListScope.findQualifyingTableNames` falls back to the parent scope when no 
child of this `FROM` has the name. So in a correlated subquery, a name that is 
a column of the outer query counts as "exactly one column" here, and the name 
becomes an ordinal. Before this PR that check only drove the meter, now it also 
decides the rewrite. Is that the intended behavior? If yes, I think the javadoc 
should say "a column visible from the `FROM` clause" instead of "a column of 
the `FROM` clause". If not, we could count only the children of `selectScope`.



##########
pinot-query-planner/src/main/java/org/apache/pinot/query/QueryEnvironment.java:
##########
@@ -328,6 +328,11 @@ public CompiledQuery compile(String sqlQuery, 
SqlNodeAndOptions sqlNodeAndOption
       RelRoot relRoot = compileQuery(queryNode, plannerContext);
       return new CompiledQuery(_envConfig.getDatabase(), sqlQuery, relRoot, 
plannerContext, sqlNodeAndOptions);
     } catch (QueryException e) {
+      if (plannerContext != null && 
plannerContext.getValidator().isGroupByNameResolvedToShadowedColumn()) {
+        // Lets callers retry with preferSelectAliasInGroupBy. With the 
column, the query can fail in the validation
+        // or later, for example when HAVING compares the name with a literal 
of the type of the alias
+        throw new GroupByAliasShadowedByColumnException(e);

Review Comment:
   This is not new, but this branch does not close `plannerContext`, while the 
`Throwable` branch below does. Since this block is changing anyway, could we 
close it here too, before we rethrow (both the wrapped and the plain exception)?



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