yujun777 commented on code in PR #69890:
URL: https://github.com/apache/doris/pull/69890#discussion_r4511163000


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/CommonSubExpressionCollector.java:
##########
@@ -80,12 +80,14 @@ private int collectChildrenDepth(List<Expression> children, 
Boolean inLambda) {
 
     private int collectCommonExpressionByDepth(int depth, Expression expr, 
boolean inLambda) {
         Set<Expression> expressions = getExpressionsFromDepthMap(depth, 
expressionsByDepth);
-        // ArrayItemSlot and ArrayItemReference could not be common expressions
-        // TODO: could not extract common expression when expression contains 
same lambda expression
-        //   because ArrayItemSlot in Lambda are not same.
+        // An expression that contains a lambda argument cannot be evaluated 
outside that lambda:
+        // hoisting it into an earlier projection leaves the argument without 
its binding. The
+        // check must not depend on the inLambda flag -- a lambda nested in 
another expression can
+        // be visited again from a context that does not carry the flag (for 
example when the same
+        // subtree appears in two projections), and the extraction then breaks 
the lambda scope.
         if (expressions.contains(expr)
                 && !(expr instanceof WhenClause)
-                && !(inLambda && expr.containsType(ArrayItemSlot.class, 
ArrayItemReference.class))) {
+                && !expr.containsType(ArrayItemSlot.class, 
ArrayItemReference.class)) {

Review Comment:
   Follow-up in 149bad541eb: the formal review found an additional 
evaluation-boundary issue beyond lexical binding. A closed inner array_map must 
not be hoisted out of an enclosing lambda, because an empty outer array skips 
that call. Restored the inLambda boundary restriction alongside 
hasFreeArrayItem. The new collector test and stored-column SQL both reproduced 
the failure before the fix. All 12 CSE unit tests now pass; topk_8 verifies 
empty-outer results, valid nested inputs, and the active-outer length-mismatch 
error. Complete top-level ArrayMap extraction remains covered by the existing 
test.



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