Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
base: main
Choose a base branch
Loading
from pravic:feat/parse-cse
Open

Conversation

Copy link

@pravic pravic commented Sep 8, 2025
edited
Loading

https://clickhouse.com/docs/sql-reference/statements/select/with#common-scalar-expressions:

WITH <expression> AS <identifier>

fixes #1514.

  • syntax
  • docs
  • tests
  • Unfortunately, this changes the public API a bit, so requires a version bump.

src/ast/query.rs Outdated
Comment on lines 652 to 666
impl CteOrCse {
pub fn cte(&self) -> Option<&Cte> {
match self {
CteOrCse::Cte(cte) => Some(cte),
CteOrCse::Cse(_) => None,
}
}

pub fn cse(&self) -> Option<&Cse> {
match self {
CteOrCse::Cte(_) => None,
CteOrCse::Cse(cse) => Some(cse),
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
impl CteOrCse {
pub fn cte(&self) -> Option<&Cte> {
match self {
CteOrCse::Cte(cte) => Some(cte),
CteOrCse::Cse(_) => None,
}
}
pub fn cse(&self) -> Option<&Cse> {
match self {
CteOrCse::Cte(_) => None,
CteOrCse::Cse(cse) => Some(cse),
}
}
}

I think we can skip this impl, in order to reduce the API surface of the library

Copy link
Author

Choose a reason for hiding this comment

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

But then we'd need way more changes in the existing code that relies on https://docs.rs/sqlparser/latest/sqlparser/ast/struct.With.html#structfield.cte_tables being Cte. I am on the edge here.

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 this is okay to skip, users can match directly on the enum when needed similar to other nodes in the AST

pravic reacted with thumbs up emoji
Comment on lines +609 to +611
fn supports_common_scalar_expressions(&self) -> bool {
false
}
Copy link
Author

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 is supports_select_exclude.

Copy link
Contributor

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

Copy link
Author

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:

as long as the syntax doesn't conflict with existing syntax support on the dialect

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.

Copy link
Contributor

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 of clickhouse()

Copy link
Contributor

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?

Copy link
Author

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.

@pravic pravic requested a review from iffyio September 10, 2025 12:22
Comment on lines +1764 to +1771
// 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: )");
Copy link
Author

Choose a reason for hiding this comment

The 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@iffyio iffyio Awaiting requested review from iffyio

At least 1 approving review is required to merge this pull request.

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Clickhouse WITH <expression> AS <identifier> syntax
2 participants

AltStyle によって変換されたページ (->オリジナル) /