github-actions[bot] commented on code in PR #64778:
URL: https://github.com/apache/doris/pull/64778#discussion_r3471398220
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/SubExprAnalyzer.java:
##########
@@ -230,6 +250,26 @@ private void checkRootIsLimit(AnalyzedResult
analyzedResult) {
}
}
+ /**
+ * Check whether the analyzed subquery plan has a top-level scalar
aggregate
+ * (aggregate without GROUP BY). Such an aggregate is guaranteed to return
+ * exactly one row regardless of its input, so EXISTS over it is always
TRUE
+ * and NOT EXISTS is always FALSE. Sorting the single row cannot change
+ * EXISTS semantics, so we also strip leading LogicalSort wrappers.
+ */
+ private boolean hasTopLevelScalarAgg(AnalyzedResult analyzedResult) {
Review Comment:
This helper still misses derived-table alias wrappers. During analysis, a
query like:
```text
WHERE EXISTS (
SELECT * FROM (
SELECT COUNT(*)
FROM (
SELECT c1 FROM t2 WHERE t2.c1 = outer.c1
UNION ALL
SELECT c1 FROM t3 WHERE t3.c1 = outer.c1
) u
) a
)
```
can be reduced to:
```text
LogicalProject(a.c)
LogicalSubQueryAlias(a)
LogicalAggregate(groupBy=[])
LogicalUnion
LogicalFilter(t2.c1 = outer.c1)
LogicalFilter(t3.c1 = outer.c1)
```
`SubExprAnalyzer.analyzeSubquery()` runs during analysis, while
`LogicalSubQueryAliasToLogicalProject` is only applied later in plan
normalization, so this alias can still be present here. Since the loop strips
only `LogicalProject` and `LogicalSort`, `hasTopLevelScalarAgg()` returns false
and line 110 rejects the correlated `LogicalUnion`, even though the scalar
aggregate still guarantees one row and `EXISTS`/`NOT EXISTS` should fold to
TRUE/FALSE. Please also treat `LogicalSubQueryAlias` as a transparent wrapper
here, or share the existing top-level scalar aggregate helper that already
allows it, and add a regression for this derived-table shape.
--
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]