Skip to content

fix: try to lower plain reserved functions to columns as well #16669

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

Merged
merged 1 commit into from
Jul 8, 2025

Conversation

crepererum
Copy link
Contributor

Which issue does this PR close?

Fixes #14141.

Rationale for this change

When a reserved function like user is called without parenthesis, it may as well be a column. This works in PostgreSQL for example:

psql (17.5 (Debian 17.5-1.pgdg120+1))
Type "help" for help.

postgres=# create table t(a int, "user" text);
CREATE TABLE
postgres=# insert into t values (1, 'foo');
INSERT 0 1
postgres=# select t.user from t;
 user
------
 foo
(1 row)
postgres=# select user from t;
   user
----------
 postgres
(1 row)

However sqlparser tries to detect these functions, see apache/datafusion-sqlparser-rs#1909

What changes are included in this PR?

We now first try to use the respective function and then also consider columns.

Are these changes tested?

More sqllogictests

Are there any user-facing changes?

More queries work as expected (IMHO).

@github-actions github-actions bot added sql SQL Planner sqllogictest SQL Logic Tests (.slt) labels Jul 3, 2025
When a reserved function like `user` is called without parenthesis, it
may as well be a column. This works in PostgreSQL for example:

```text
psql (17.5 (Debian 17.5-1.pgdg120+1))
Type "help" for help.

postgres=# create table t(a int, "user" text);
CREATE TABLE
postgres=# insert into t values (1, 'foo');
INSERT 0 1
postgres=# select t.user from t;
 user
------
 foo
(1 row)
postgres=# select user from t;
   user
----------
 postgres
(1 row)
```

However sqlparser tries to detect these functions, see
apache/datafusion-sqlparser-rs#1909

We now first try to use the respective function and then also consider
columns.

Fixes apache#14141.
@crepererum crepererum force-pushed the crepererum/issue14141 branch from 7adc63a to 95805f0 Compare July 3, 2025 10:19
@crepererum crepererum marked this pull request as ready for review July 3, 2025 12:05
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thank you @crepererum -- looks good to me

cc @jonahgao in case you also have time to review

@@ -93,6 +93,8 @@ struct FunctionArgs {
distinct: bool,
/// WITHIN GROUP clause, if any
within_group: Vec<OrderByExpr>,
/// Was the function called without parenthesis, i.e. could this also be a column reference?
function_without_paranthesis: bool,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think there is a typo here:

Suggested change
function_without_paranthesis: bool,
function_without_parenthesis: bool,

Copy link
Member

@jonahgao jonahgao left a comment

Choose a reason for hiding this comment

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

Looks good to me!

@alamb
Copy link
Contributor

alamb commented Jul 8, 2025

Thank you for the review @jonahgao ❤️

@alamb alamb merged commit 2884612 into apache:main Jul 8, 2025
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sql SQL Planner sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error when use user field in where clause
3 participants