brucearctor opened a new pull request, #69938:
URL: https://github.com/apache/doris/pull/69938

   ### What problem does this PR solve?
   
   Issue Number: close #69930
   
   Related PR: N/A
   
   Problem Summary:
   
   When an enclosing higher-order function (such as `ARRAY_FIRST(p -> 
STRUCT_ELEMENT(p, 1) = k, arr)`) references slots from both sides of a join, it 
cannot be pushed down as a whole. `PushDownProject` then descends into its 
children via `rewriteDownShortCircuit`.
   
   Because `Expression.getInputSlots()` (`Expression.java:73-74`) deliberately 
excludes `ArrayItemSlot`, a `PreferPushDownProject` sub-expression inside the 
lambda body that only references the lambda parameter `p` (such as 
`element_at(p, 1)`) has an empty `getInputSlots()` set. Both 
`PushDownProject.pushDownProjectInExpressions` and 
`PushdownProjectHelper.pushDownExpression` treat 
`child.getOutputSet().containsAll(emptySet)` as satisfied by the first child 
(`join.left()`) and extract `element_at(p, 1)` out of its enclosing `Lambda` 
into a child `LogicalProject`, leaving `p` unbound:
   
   **Before:**
   ```text
   LogicalProject(k, element_at(element_at(array_filter(arr, array_map(p -> #x 
= k, arr)), 1), 2) AS v)
     LogicalJoin(CROSS_JOIN)
       left:  LogicalProject(k, element_at(p, 1) AS #x)   <-- 'p' 
(ArrayItemSlot) is unbound here!
                LogicalOneRowRelation(k)
       right: LogicalOneRowRelation(arr)
   ```
   
   During physical plan translation 
(`PhysicalPlanTranslator.visitPhysicalProject` -> 
`ExpressionTranslator.visitArrayItemSlot`), `p` is not present in 
`lambdaSlotToColumnRef` (which is only populated while translating 
`visitArrayMap`), returning `null` and failing with a `NullPointerException` in 
`Expr.extractSlots`.
   
   **After:**
   ```text
   LogicalProject(k, element_at(element_at(array_filter(arr, array_map(p -> 
element_at(p, 1) = k, arr)), 1), 2) AS v)
     LogicalJoin(CROSS_JOIN)
       left:  LogicalOneRowRelation(k)
       right: LogicalOneRowRelation(arr)
   ```
   
   **Fix:**
   - Guard both `PreferPushDownProject` rewrite sites in `PushDownProject` 
(`pushDownProjectInExpressions` and `PushdownProjectHelper.pushDownExpression`) 
with `!hasFreeLambdaArgument(e)`.
   - `hasFreeLambdaArgument` uses an O(1) `containsType(ArrayItemSlot.class)` 
fast path and tracks bound lambda parameter `ExprId`s lexically across nested 
`Lambda`s (checking `ArrayItemReference.getArrayExpression()` in the outer 
scope before binding the lambda's own parameters).
   - Complete, self-contained higher-order expressions whose relational inputs 
come from a single child continue to be pushed down as before.
   
   cc @matanper @morrySnow @924060929 @englefly
   
   ### Release note
   
   None
   
   ### Check List (For Author)
   
   - Test <!-- At least one of them must be included. -->
       - [ ] Regression test
       - [x] Unit Test (`PushDownProjectTest`)
       - [ ] Manual test (add detailed scripts or steps below)
       - [ ] No need to test or manual test. Explain why:
           - [ ] This is a refactor/code format and no logic has been changed.
           - [ ] Previous test can cover this change.
           - [ ] No code files have been changed.
           - [ ] Other reason <!-- Add your reason?  -->
   
   - Behavior changed:
       - [x] No.
       - [ ] Yes. <!-- Explain the behavior change -->
   
   - Does this need documentation?
       - [x] No.
       - [ ] Yes. <!-- Add document PR link here. eg: 
https://github.com/apache/doris-website/pull/1214 -->
   
   ### Check List (For Reviewer who merge this PR)
   
   - [ ] Confirm the release note
   - [ ] Confirm test cases
   - [ ] Confirm document
   - [ ] Add branch pick label <!-- Add branch pick label that this PR should 
merge into -->


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