Skip to content
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

feat(clickhouse): Add support for APPLY query modifier #4141

Merged
merged 4 commits into from
Sep 23, 2024

Conversation

VaggelisD
Copy link
Collaborator

Fixes #4139

Clickhouse supports the APPLY query modifier:

SELECT <expr> APPLY( <func> ) FROM [db.]table_name

Although apply is still a valid alias, we shouldn't parse it as such if it's accompanied by an L_PAREN as is the case here, thus the overriden _parse_alias() in Clickhouse:

clickhouse> select 1 apply;

   ┌─apply─┐
1. │     1 │
   └───────┘

clickhouse> with t as (Select 1 as col) select COLUMNS('col') apply(sum) from t;

   ┌─sum(col)─┐
1. │        1 │
   └──────────┘

Note that APPLY can be chained, as is shown in the docs & relevant test case

Docs

Clickhouse APPLY modifier

@tobymao
Copy link
Owner

tobymao commented Sep 20, 2024

is this the right approach? should we treat this similarly to window functions?

@VaggelisD
Copy link
Collaborator Author

VaggelisD commented Sep 20, 2024

@tobymao Clickhouse is a bit lax with the rules, the grammar etc so I struggled a bit to not break any functionality as with the apply alias vs modifier. Regarding window functions, which part would that be, reusing _parse_query_modifiers()?

Note that APPLY comes before the query's FROM so we must either parse it early on it's own (the approach here) or add it in QUERY_MODIFIER_PARSERS and call _parse_query_modifiers() in the place of my current while loop afaict.

sqlglot/parser.py Outdated Show resolved Hide resolved
@VaggelisD VaggelisD merged commit 3ec96ab into main Sep 23, 2024
6 checks passed
@VaggelisD VaggelisD deleted the vaggelisd/clickhouse_apply branch September 23, 2024 16:27
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.

Clickhouse COLUMNS APPLY parsing error
3 participants