-
Notifications
You must be signed in to change notification settings - Fork 652
Add support for ClickHouse CSE. #2024
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
Open
pravic
wants to merge
11
commits into
apache:main
Choose a base branch
from
pravic:feat/parse-cse
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+202
−12
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8559018
feat: Add support for ClickHouse CSE.
pravic 6998a6d
feat: Add CSE tests.
pravic 5a62df3
fix: Update existing tests with CTE.
pravic 258d1fd
fixup! feat: Add support for ClickHouse CSE.
pravic 43e4f10
refactor: Rename to `WithExpression`.
pravic 0d767a8
doc: Better docs for `CTE`/`CSE`.
pravic 4a307a9
feat: Add `supports_common_scalar_expressions`.
pravic f2ada38
fix: Expect `AS` in CSE.
pravic a31ad5a
fix: We need closure for `Parser::maybe_parse`.
pravic ec2abf6
chore: Add test for mixed CTE/CSE.
pravic a8cca3b
chore: Add more tests for CSE.
pravic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1729,6 +1729,65 @@ fn test_parse_not_null_in_column_options() { | |
); | ||
} | ||
|
||
#[test] | ||
fn parse_cse() { | ||
clickhouse().verified_stmt("WITH x AS (SELECT 1) UPDATE t SET bar = (SELECT * FROM x)"); | ||
|
||
let with = concat!( | ||
"WITH", | ||
" toIntervalSecond(300) AS bucket_size,", | ||
pravic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
" toDateTime64(1735751460, 9) AS start_time,", | ||
" toDateTime64(1735755060, 9) AS end_time ", | ||
"SELECT", | ||
" toStartOfInterval(EventTime, bucket_size) AS bucket,", | ||
" count() AS count ", | ||
"FROM logs", | ||
); | ||
clickhouse().verified_query(with); | ||
|
||
let mixed = concat!( | ||
"WITH", | ||
" toDate(now()) AS today,", | ||
" tbl (c) AS (SELECT toDate('2000-01-01')) ", | ||
"SELECT", | ||
" * ", | ||
"FROM tbl ", | ||
"WHERE c < today" | ||
); | ||
clickhouse().verified_query(mixed); | ||
|
||
// valid | ||
clickhouse() | ||
.parse_sql_statements("WITH foo() AS bar SELECT 1") | ||
.unwrap(); | ||
|
||
// ClickHouse allows these, but not sqlparser | ||
clickhouse() | ||
.parse_sql_statements("WITH foo, bar SELECT 1") | ||
.expect_err("Expected: AS, found: ,"); | ||
|
||
clickhouse() | ||
.parse_sql_statements("WITH foo(), bar SELECT 1") | ||
.expect_err("Expected: identifier, found: )"); | ||
Comment on lines
+1764
to
+1771
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting: https://fiddle.clickhouse.com/1292850a-31e4-49c7-a309-79bc47a273a2 I am not sure whether it's a feature or a bug in the ClickHouse syntax parser. But since these examples don't make much sense, let's leave them as rejected. |
||
|
||
// invalid | ||
clickhouse() | ||
.parse_sql_statements("WITH foo bar SELECT 1") | ||
.expect_err("Expected: "); | ||
|
||
clickhouse() | ||
.parse_sql_statements("WITH foo() bar SELECT 1") | ||
.expect_err("Expected: "); | ||
|
||
clickhouse() | ||
.parse_sql_statements("WITH foo() bar() SELECT 1") | ||
.expect_err("Expected: "); | ||
|
||
clickhouse() | ||
.parse_sql_statements("WITH foo() AS bar() SELECT 1") | ||
.expect_err("Expected: "); | ||
} | ||
|
||
fn clickhouse() -> TestedDialects { | ||
TestedDialects::new(vec![Box::new(ClickHouseDialect {})]) | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also add this to the generic dialect? I could not grasp the idea of what expressions should be supported in the generic dialect and what should be not.
For example,
supports_select_expr_star
isn't listed as supported. Neither issupports_select_exclude
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general we try to add support for new syntax to the generic dialect as long as the syntax doesn't conflict with existing syntax support on the dialect
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, CSE exist only in ClickHouse. Does it make sense to include this to the generic dialect? If yes, then:
It's difficult for me to answer to this question - I don't know whether there can be a conflict. But since it's only a ClickHouse feature - I'd not include this to the generic dialect. But I won't oppose either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah if there's a conflict that would show up in the tests, so that it should be a matter of flagging it to true in the dialect and then changing the tests to use
all_dialects_where(|d| di.supports_common_scalar_expressions())
to test instead ofclickhouse()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pravic just double checking the status of this PR, if you would have time to address this comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@iffyio No, haven't touched this bit so far. I you can, I would appreciate changing this.