-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Scoping of lambda functions in list_apply
and list_filter
seems artificially restrictive
#9981
Comments
I noticed the same problem in the now stale closed issue #5739 |
Hi @cpcloud. I am unsure about this. We throw this error because when binding the RHS of the lambda function ( create or replace table t as select [1, 2, 3] as x;
select list_apply(['hello'], x -> x) from t;
Error: Binder Error: Invalid parameter name 't.x': must be unqualified If we allow column names as lambda parameters, the following query would be ambiguous. create or replace table t as select [[1], [2], [3]] as x;
select list_transform([[1], [2], [3]], x -> x[1]) from t;
Error: Binder Error: Invalid parameter name 't.x': must be unqualified # possible result: x is a lambda parameter
[1, 2, 3]
# possible result: x is a column name in t
[[1], [1], [1]] Using the column as the input list is possible, i.e., the following query works. D create or replace table t as select [1, 2, 3] as x;
D select list_apply(t.x, y -> y + 1) from t;
┌─────────────────────────────────┐
│ list_apply(t.x, (y -> (y + 1))) │
│ int32[] │
├─────────────────────────────────┤
│ [2, 3, 4] │
└─────────────────────────────────┘ I can look into making the error message more clear in this scenario, but I don't see a way to address the possible ambiguity between lambda parameter names and column names in the RHS of the lambda expression. |
@taniabogatsch Thanks for looking into it. I'm not sure I understand the ambiguity here, as long as there are well-defined scoping rules for lambdas.
Why would
Other systems that implement equivalent functionality such as ClickHouse and Trino do not have this problem. ClickHouse:
Trino:
|
We bind lambda functions with our SQL scalar function binder, which does not have a concept of local variables that are not columns. I.e., the binder treats lambda parameters the same as it treats columns. That's also why we recently pushed this PR...
SQL scalar functions can reference column names without table identifiers, which should not change for lambda functions. But after looking into this more now, I agree that adding scoping here for lambda functions, which are already a particular case, can make sense and make the lambda functions more intuitive to use. I should be able to change the lambda binding to prioritize lambda parameter names over column names and inner lambda parameter names over outer lambda parameter names, like so: |
PR got merged. |
Much appreciated! |
What happens?
Using a parameter name in a lambda functions that matches the column name of the input column results in a strange error message about needing an unqualified parameter name
To Reproduce
Since the
x
s are unrelated to each other here I would expect there to be no issue executing this query.OS:
Linux (NixOS)
DuckDB Version:
v0.9.2
DuckDB Client:
CLI
Full Name:
Phillip Cloud
Affiliation:
Voltron Data
Have you tried this on the latest
main
branch?I have tested with a main build
Have you tried the steps to reproduce? Do they include all relevant data and configuration? Does the issue you report still appear there?
The text was updated successfully, but these errors were encountered: