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


##########
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:
   Yes, intended. Calcite 1.42 also resolves the name to an outer-query column, 
so these queries changed too. Example, where `mcol1` is a column of `e` only:
   
   `SELECT col1 FROM e WHERE col2 IN (SELECT UPPER(a.col2) AS mcol1 FROM a 
GROUP BY mcol1)`
   
   It plans on 1.40, fails on 1.42 (`a.col2 is not being grouped`), and plans 
again with the option. I updated the Javadoc and added a test.



##########
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:
   Done.



##########
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:
   Added. For `u` the lookup returns no table, so it takes the same path as the 
first test.
   
   The two-table case is `GROUP BY col1` with `USING (col1)`. Calcite 1.42 
resolves that name to the column, and this PR no longer restored the alias 
there (master does). Fixed: a `USING` or `NATURAL` join column now counts as a 
shadowing column. Added a test for that too.



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