Sid4030 opened a new pull request, #59766:
URL: https://github.com/apache/spark/pull/59766

   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: 
https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: 
https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., 
'[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a 
faster review.
     7. If you want to add a new configuration, please read the guideline first 
for naming configurations in
        
'common/utils/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
     8. If you want to add or modify an error type or message, please read the 
guideline first in
        'common/utils/src/main/resources/error/README.md'.
   -->
   
   ### What changes were proposed in this pull request?
   This PR fixes `HigherOrderFunction.canonicalized` to only renumber lambda 
variables that are **bound** within the higher-order function (the arguments of 
its own lambda functions and nested lambda functions), rather than all lambda 
variables found in the expression tree.
   
   Specifically:
   - Bound variables (`boundExprIds`) are renumbered sequentially starting from 
0 and normalized with `value = null`.
   - Free/captured variables (variables bound by an outer enclosing lambda) 
keep their own `exprId`.
   - Free variables have their runtime `AtomicReference` value normalized to a 
single shared non-null reference (`HigherOrderFunction.FreeVariableValue`). 
This ensures:
     1. Copies of the same plan across optimizer rules still match in 
`semanticEquals`.
     2. A free variable never accidentally equals a canonicalized bound 
variable (which has `value = null`).
   
   ### Why are the changes needed?
   Previously, `HigherOrderFunction.canonicalized` renumbered all 
`NamedLambdaVariable`s in its subtree, including variables captured from 
enclosing outer lambdas. 
   
   As a result, two different nested higher-order functions capturing different 
outer variables—such as `filter(ys, y -> y > x1)` and `filter(ys, y -> y > 
x2)`—both renumbered `x1` and `x2` to `0`. Consequently, `canonicalized` (and 
`semanticEquals`) erroneously reported them as identical if `x1` and `x2` 
shared the same data type and nullability.
   
   This bug caused incorrect query results in several optimizer rules:
   - **`SimplifyBinaryComparison`**: Simplified `filter(ys, y -> y > x1) <=> 
filter(ys, y -> y > x2)` to `true`, producing wrong answers.
   - **`SubexpressionElimination`**: Incorrectly merged non-identical nested 
higher-order functions.
   
   ### Does this PR introduce _any_ user-facing change?
   Yes. It fixes a bug where queries containing nested higher-order functions 
referencing distinct outer lambda variables produced wrong query results due to 
false semantic equality.
   
   ### How was this patch tested?
   - Added unit tests in `HigherOrderFunctionsSuite.scala` testing:
     - `semanticEquals` of nested higher-order functions with captured 
variables.
     - Subexpression elimination with nested higher-order functions.
   - Added end-to-end SQL tests in `DataFrameFunctionsSuite.scala` validating 
query execution with `filter` and outer captured lambda variables.
   - Ran test suites:
     - `build/sbt 'catalyst/testOnly *HigherOrderFunctionsSuite'`
     - `build/sbt 'catalyst/testOnly *BinaryComparisonSimplificationSuite'`
     - `build/sbt 'sql/testOnly *DataFrameFunctionsSuite'`
   
   ### Was this patch authored or co-authored using generative AI tooling?
   Generated-by: Antigravity IDE (Gemini 2.0)
   


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