github-actions[bot] commented on code in PR #64778:
URL: https://github.com/apache/doris/pull/64778#discussion_r3467045435


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/SubExprAnalyzer.java:
##########
@@ -230,6 +250,24 @@ 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.
+     */
+    private boolean hasTopLevelScalarAgg(AnalyzedResult analyzedResult) {
+        LogicalPlan plan = analyzedResult.getLogicalPlan();
+        // Strip leading projects — analysis may wrap the aggregate in a 
project.
+        while (plan instanceof LogicalProject) {

Review Comment:
   This shortcut still misses the `ORDER BY` wrapper that the old apply-time 
shortcut handled. A reduced analyzed shape for `WHERE EXISTS (SELECT COUNT(*) 
FROM (<correlated UNION ALL>) u ORDER BY 1)` is:
   
   ```text
   LogicalSort(order by count(*))
     LogicalAggregate(groupBy=[])
       LogicalUnion
         LogicalFilter(t2.c1 = outer.c1)
         LogicalFilter(t3.c1 = outer.c1)
   ```
   
   `hasTopLevelScalarAgg()` strips only `LogicalProject`, so it returns false 
for this plan and line 110 then calls `checkNoCorrelatedSlotsUnderSetOp()`, 
which finds the correlated `LogicalUnion` and rejects the query. Before this 
PR, `SubqueryToApply.hasTopLevelScalarAgg()` recursed through both 
`LogicalProject` and `LogicalSort`, and `ReplaceSubquery.visitExistsSubquery()` 
folded the EXISTS/NOT EXISTS to TRUE/FALSE. Sorting the single scalar-aggregate 
row cannot change EXISTS semantics, and `EliminateSortUnderSubqueryOrView` is a 
later rewrite-stage rule, so analysis should preserve the same wrapper handling 
here, ideally by sharing or mirroring the existing helper and adding an `ORDER 
BY` regression.



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