Skip to content

feat: expose lambda and higher-order array functions#1561

Merged
timsaucer merged 6 commits into
apache:mainfrom
timsaucer:feat/lambda-support
May 29, 2026
Merged

feat: expose lambda and higher-order array functions#1561
timsaucer merged 6 commits into
apache:mainfrom
timsaucer:feat/lambda-support

Conversation

@timsaucer
Copy link
Copy Markdown
Member

@timsaucer timsaucer commented May 26, 2026

Which issue does this PR close?

Closes #811

Rationale for this change

DataFusion gained lambda expressions and higher-order array functions (apache/datafusion#21172). The Python binding already had the read-only Lambda, HigherOrderFunction, and LambdaVariable wrapper classes, but offered no way to construct a lambda or call the higher-order functions. This PR adds a Pythonic API for both.

What changes are included in this PR?

Adds the following:

  • Functions array_transform and array_any_match that accept either a Python callable or an explicit lambda built with lambda_ / lambda_var
  • Unit tests
  • Functions to create lambda expressions or variables
  • Documentation

Are there any user-facing changes?

There are new APIs but they are all new additions.

timsaucer and others added 2 commits May 26, 2026 15:04
Add a Pythonic API for DataFusion's higher-order array functions and the
lambda expressions they consume.

- Rust: lambda_, lambda_var, array_transform, and array_any_match pyfunctions,
  plus a ResolveLambdaVariables analyzer rule so expression-builder plans
  (which emit unresolved lambda variables) resolve before optimization.
- Python: array_transform / array_any_match (with list_transform, any_match,
  list_any_match aliases) accept either a Python callable or an explicit
  lambda built with lambda_ / lambda_var. Callables are introspected so their
  parameter names become the lambda parameters.
- Tests and docs (expressions guide + agent skill), noting v1 limits: lambda
  expressions are not serializable, and SQL arrow syntax needs the DuckDB
  dialect.
Combine the eight higher-order function result tests into a single
parametrized test_higher_order_function_results, and the two to_lambda
rejection tests into test_to_lambda_rejects_invalid_arg. Each case keeps
a readable id via pytest.param.

Co-Authored-By: Claude <noreply@anthropic.com>
@timsaucer timsaucer marked this pull request as draft May 27, 2026 15:58
timsaucer and others added 3 commits May 27, 2026 13:23
Add array_filter, the remaining lambda-based higher-order array function
in DataFusion (alongside the already-exposed array_transform and
array_any_match). Includes the list_filter alias matching upstream, tests,
and documentation in the expressions guide and skill.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Lead user-facing array-lambda docs with "lambda function" instead of
"higher-order function," which is less recognizable to users. Drop the
alias list, serialization caveat, and DuckDB-dialect note from the skill
to keep it lean; those details already live in the docstrings.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@timsaucer timsaucer marked this pull request as ready for review May 27, 2026 19:17
@timsaucer timsaucer mentioned this pull request May 27, 2026
11 tasks
Comment on lines +189 to +191
contains a lambda raises ``Lambda not implemented``. SQL lambda syntax
(``x -> x * 2``) is only parsed by dialects that support lambdas; set
``datafusion.sql_parser.dialect`` to ``DuckDB`` to use it. The Python
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DuckDB will remove this syntax in v2.1. Perhaps add other dialects that support this syntax (spark, databricks, clickhouse, snowflake), and/or add the new duckdb syntax (``lambda x: x *2`) ?

duckdb#17235
duckdb#22682

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it makes sense to just remove this whole discussion until the syntax stabilizes?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I see now that duckdb is the only dialect in PyDialect that supports lambdas. Just clarifying my comment, the new syntax is already stabilized and supported in duckdb and sqlparser-rs, alongside the old arrow syntax.
duckdb v2.1 will only remove lambda support for the old syntax, and likely use it for json operators. My main point was to also document syntax+dialect pairs that will not break in the future, since sqlparser-rs may follow the duckdb new syntax when it get's released, so it can correcly parse json operators.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the documentation. I appreciate the feedback!

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Other dialects (ClickHouse, Snowflake, Databricks) also enable lambda
parsing via sqlparser-rs. Document the full set and recommend the
``lambda x: x`` keyword form, since DuckDB will drop the ``x -> x``
arrow form in v2.1. Parametrize the SQL test over the four dialects
using the keyword syntax.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@timsaucer timsaucer requested a review from gstvg May 29, 2026 20:53
Copy link
Copy Markdown

@gstvg gstvg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great @timsaucer. The support for callable is very nice

@timsaucer timsaucer merged commit af38866 into apache:main May 29, 2026
21 checks passed
@timsaucer timsaucer deleted the feat/lambda-support branch May 29, 2026 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add functions.transform

2 participants