924060929 commented on code in PR #64458:
URL: https://github.com/apache/doris/pull/64458#discussion_r3425444008


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/NormalizeAggregate.java:
##########
@@ -300,37 +300,51 @@ public LogicalPlan normalizeAgg(LogicalAggregate<Plan> 
aggregate, Optional<Logic
             }
             if (!missingSlotsInAggregate.isEmpty()) {
                 if (SqlModeHelper.hasOnlyFullGroupBy()) {
-                    throw new AnalysisException(String.format("PROJECT 
expression %s must appear in the GROUP BY"

Review Comment:
   试过——上一版(a8e7db00)就是这么写的:非 uniformAndNotNull 抛异常,其余走下面的 any_value 
补全。问题出在**裸列(非别名)输出的 exprId**。
   
   `any_value(b#2) AS b#3` 把输出列的 exprId 从 `b#2` 换成了 `b#3`。但 NormalizeAggregate 
只重写自己这棵子树,够不到上层的 ResultSink/parent——它们还引用 `b#2`,于是 rewrite 阶段悬空。实测 `SELECT a, b 
FROM (SELECT 1 a, 2 b) t1 GROUP BY a`:
   
   ```
   analyze 后:
   LogicalResultSink   ( outputExprs=[a#1, b#2] )            <- 仍引用 b#2
   +--LogicalProject   ( projects=[a#1, b#3] )               <- any_value(b#2) 
AS b#3
      +--LogicalAggregate ( groupBy=[a#1], output=[a#1, any_value(b#2) AS b#3] )
   
   rewrite:Input slot(s) not in child's output: b#2
   ```
   
   **别名列**(`a as b`)能跑,是因为别名的 exprId 不变、只换里面的 `a#1`;**裸列**(`GROUP BY a` 直接 
select a, b)就会悬空。所以那一版别名 case 过了、裸列 case 挂了(对应之前 CI 上挂掉的 
`qt_const_not_in_groupby`)。
   
   改成把常量列加进 group by key 之后,exprId 完全不动(还是 `b#2`),ResultSink 引用不破;真正"换 exprId + 
向上重写引用"的活交给 `EliminateGroupByKeyByUniform`(它本来就有 ExprIdRewriter 
那套机制)。所以这版是踩了上面这个坑之后才改成 add-to-group-by 的。



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