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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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.

@@ -2960,6 +2961,9 @@ def _parse_select(
)
this.comments = comments

while self._match_pair(TokenType.APPLY, TokenType.L_PAREN, advance=False):
Copy link
Owner

Choose a reason for hiding this comment

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

is this section tested? how is it different from the parse alias section?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In _parse_alias(), we don't want to parse an actual alias if we match "APPLY", "(".

In _parse_select(), we want to match the APPLY(...) clause between the projection list and the FROM clause.

I can add more tests that check that e.g. SELECT 1 apply produces an exp.Alias clause

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
2 participants