JeelRajodiya opened a new pull request, #27655:
URL: https://github.com/apache/datafusion/pull/27655

   ## Which issue does this PR close?
   
   - Part of #21172 (the "array_transform index parameter" item).
   
   ## Rationale for this change
   
   Today an `array_transform` lambda only sees the element, so there is no way 
to compute a value that depends on the element's position. Spark 
(`transform(arr, (x, i) -> ...)`) and DuckDB (`list_transform(l, (x, i) -> 
...)`) both pass the position as an optional second lambda parameter.
   
   ```sql
   SELECT array_transform([10, 20, 30], (v, i) -> v * i);
   -- before: Error during planning: lambda defined 2 params but UDF support 
only 1
   -- after:  [10, 40, 90]
   ```
   
   ## What changes are included in this PR?
   
   `array_transform` now offers two lambda parameters: the element and its 
**1-based `Int64` index** within its own list. A lambda can still declare only 
the element (`v -> ...`), so existing queries are unchanged.
   
   The index array is passed to `LambdaArgument::evaluate` as a lazy closure 
like the values, so it is only built when the lambda body actually references 
the index. Queries that don't use it pay nothing.
   
   **Open questions for reviewers**
   
   - **1-based vs 0-based.** I chose 1-based to match DataFusion array indexing 
(`array_element(arr, 1)` is the first element) and DuckDB, so `arr[i]` works 
inside the lambda. Spark uses 0-based, so a Spark-compatible `transform` would 
need to subtract one.
   - **`Int64` vs `Int32`.** I chose `Int64` to match the `Int64` index that 
`array_element` takes. Spark uses `Int32`.
   
   ## What is the testing strategy for this PR?
   
   - `array_transform.slt`: index on literals, on a multi-row column with a 
`NULL` list, an empty list and a `NULL` element, `LargeList`, together with a 
captured column, from a nested lambda, the index type, and the updated "too 
many params" error.
   - Unit test `index_on_sliced_list_restarts_at_one_per_row` for a sliced 
input, which SQL cannot produce reliably.
   
   ## Are there any user-facing changes?
   
   Yes. `array_transform` lambdas accept an optional second `index` parameter, 
and the function docs show an example. No API changes.
   


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