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


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/FillUpQualifyMissingSlot.java:
##########
@@ -208,10 +412,40 @@ public Expression visitWindow(WindowExpression window, 
List<NamedExpression> con
                 .map(Expression::getInputSlots)
                 .flatMap(Set::stream)
                 .filter(s -> !projectOutputSet.contains(s))
+                // ATTN: exclude outer query's correlated slots, they belong 
to outer query
+                // and should not be filled up into the inner project's output.
+                .filter(s -> !(outerScope.isPresent()
+                        && outerScope.get().getCorrelatedSlots().contains(s)))
                 .collect(Collectors.toSet());
 
+        // getInputSlots() deliberately does not traverse a subquery's inner 
plan, so a nested
+        // subquery in QUALIFY correlated to a column of the inner query that 
is not in the project
+        // output (e.g. `EXISTS (SELECT ... WHERE j.v = i.not_grouped)` where 
the project outputs
+        // neither i.not_grouped nor anything that produces it) would be 
invisible here and leave a
+        // dangling slot in the apply's right side that no later rewrite can 
fix. Reject this shape
+        // with a clear error instead of failing with a cryptic 
slot-validation error.
+        for (Expression conjunct : conjuncts) {
+            if (conjunct.containsType(SubqueryExpr.class)) {
+                Set<SubqueryExpr> subqueryExprs =
+                        conjunct.collect(e -> e instanceof SubqueryExpr);
+                for (SubqueryExpr subqueryExpr : subqueryExprs) {
+                    for (Slot correlatedSlot : 
subqueryExpr.getCorrelateSlots()) {
+                        if (!projectOutputSet.contains(correlatedSlot)

Review Comment:
   [P1] Preserve nested correlations already carried by a plain project
   
   For a non-DISTINCT plan such as:
   
   ```text
   Qualify[rn=1, i.not_grouped>0, EXISTS(j.v=i.not_grouped)]
     Project[i.k, rn]
       Scan(i.k, i.not_grouped)
   ```
   
   the direct predicate already puts the original `i.not_grouped` ExprId in 
`notExistedInProject`, so this rule has classified it as a child-owned support 
slot. This check still compares only against the original project output and 
throws before line 447 appends that slot. Without the throw, the existing 
upper/lower-project split gives the nested Apply a left child that owns 
`i.not_grouped`, and the unchanged upper project strips the helper without 
changing rows or schema.
   
   The earlier thread covers the DISTINCT barrier, where extending the key can 
change semantics; this plain project has no such barrier. Please surface 
child-owned nested correlation slots here (or at least admit slots already 
present in `notExistedInProject`), retain targeted rejection only for proven 
DISTINCT/aggregate barriers, and add a full-pipeline positive 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