[SPARK-53440][PYTHON] Allow Column.transform() to accept SQL lambda expression strings#54965
Draft
xiaoxuandev wants to merge 1 commit intoapache:masterfrom
Draft
[SPARK-53440][PYTHON] Allow Column.transform() to accept SQL lambda expression strings#54965xiaoxuandev wants to merge 1 commit intoapache:masterfrom
xiaoxuandev wants to merge 1 commit intoapache:masterfrom
Conversation
…ression strings
### What changes were proposed in this pull request?
Extend `Column.transform` to accept a SQL lambda expression string (e.g. `'x -> x * 2'`) in addition to the existing Python callable support.
For Classic mode, the SQL lambda is parsed by `CatalystSqlParser`, the lambda body's parameter variable is replaced with the actual column expression, and the result is returned directly — no intermediate array wrapping.
For Connect mode, the SQL lambda string is sent to the server via `SQLExpression` and evaluated using `transform(array(col), lambda)[0]`, since the client has no local Catalyst parser.
### Why are the changes needed?
Currently `Column.transform` only accepts Python callables. In some situations it is preferable to express transformations using SQL syntax (e.g. `'x -> x + 1'`) since no Python introspection happens, just simple parsing. This was requested in SPARK-53440.
### Does this PR introduce _any_ user-facing change?
Yes. `Column.transform` now accepts a `str` argument in addition to `Callable`:
```python
df.value.transform(lambda c: c * 2) # existing — Python callable
df.value.transform('x -> x * 2') # new — SQL lambda string
```
### How was this patch tested?
Added 8 new tests in `test_column.py` covering SQL lambda arithmetic, function calls, conditional logic, null handling, chaining (SQL-only and mixed SQL+Python), and error paths (missing arrow, invalid param name).
### Was this patch authored or co-authored using generative AI tooling?
Yes, co-authored with Kiro.
HyukjinKwon
reviewed
Mar 23, 2026
|
|
||
| @dispatch_col_method | ||
| def transform(self, f: Callable[["Column"], "Column"]) -> "Column": | ||
| def transform(self, f: Union[Callable[["Column"], "Column"], str]) -> "Column": |
Member
There was a problem hiding this comment.
Hm, can't people just use it likeexpr("transform(c, 'x -> x * 2')")? These API are supposed to be python friendly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
Extend
Column.transformto accept a SQL lambda expression string (e.g.'x -> x * 2') in addition to the existing Python callable support.For Classic mode, the SQL lambda is parsed by
CatalystSqlParser, the lambda body's parameter variable is replaced with the actual column expression, and the result is returned directly — no intermediate array wrapping.For Connect mode, the SQL lambda string is sent to the server via
SQLExpressionand evaluated usingtransform(array(col), lambda)[0], since the client has no local Catalyst parser.Why are the changes needed?
Currently
Column.transformonly accepts Python callables. In some situations it is preferable to express transformations using SQL syntax (e.g.'x -> x + 1') since no Python introspection happens, just simple parsing.Does this PR introduce any user-facing change?
Yes.
Column.transformnow accepts astrargument in addition toCallable:How was this patch tested?
Added 8 new tests in
test_column.pycovering SQL lambda arithmetic, function calls, conditional logic, null handling, chaining (SQL-only and mixed SQL+Python), and error paths (missing arrow, invalid param name).Was this patch authored or co-authored using generative AI tooling?
Yes, co-authored with Kiro.