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

   ### What problem does this PR solve?
   
   Issue Number: None
   
   Problem Summary:
   
   A lambda body may reference the columns of the enclosing query, but this
   only worked for a single level of high-order function. As soon as the
   lambda was nested inside another lambda, the reference failed to bind:
   
   ```sql
   WITH t2 AS (SELECT c IS NULL AS dict_flag, ARRAY('A') AS arr1, ARRAY('a') AS 
arr2 FROM t)
   SELECT ARRAY_MAP(arg1 -> ARRAY_SUM(ARRAY_MAP(arg2 -> IF(dict_flag, 1, 0), 
arr2)), arr1) FROM t2;
   -- ERROR 1105 (HY000): Unknown lambda slot 'dict_flag in lambda 
arguments[arg2]
   ```
   
   The same happened with `array_sortby`, `array_filter` and any other
   high-order function used as the inner lambda.
   
   Root cause: `ExpressionAnalyzer` analyzes every lambda body with a
   nested analyzer whose scope only holds the lambda arguments and whose
   outer scope is the enclosing scope. Slot binding intentionally looks at
   most one level up (the outer scope is meant for correlated subqueries).
   With two nested lambdas the scope chain became
   `inner arguments -> outer arguments -> query columns`, so the query
   columns were out of reach for the inner lambda body.
   
   Fix: when the enclosing analyzer is itself analyzing a lambda body, the
   scope of the nested lambda merges the enclosing lambda arguments (a
   same-named inner argument still shadows the outer one) and keeps the
   query scope as its outer scope. Every nesting depth therefore sees the
   same names as a single-level lambda: all enclosing lambda arguments plus
   the columns of the query. Single-level lambdas and correlated subquery
   binding are unchanged.
   
   ### Release note
   
   None
   
   ### Check List (For Author)
   
   - Test:
       - Unit Test: `BindFunctionTest` covers query-column capture in nested
         `array_map`/`array_sortby`/three-level lambdas, visibility of all
         enclosing lambda arguments, same-name shadowing and the unknown
         slot error.
       - Regression test: `test_nested_array_map` adds nested lambdas that
         capture query columns (plain column, CTE alias, together with the
         enclosing lambda argument, three levels deep).
   - Behavior changed: No
   - Does this need documentation: No
   
   https://claude.ai/code/session_016A7UJu7EA7j4NkGz3yjkt6
   


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