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.

Comment on lines 12263 to 12264
/// Parse a CTE or CSE.
pub fn parse_cte_or_cse(&mut self) -> Result<CteOrCse, ParserError> {
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
/// Parse a CTE or CSE.
pubfn parse_cte_or_cse(&mut self) -> Result<CteOrCse, ParserError> {
/// Parse an expression in a `WITH` clause.
fn parse_with_expression(&mut self) -> Result<WithExpression, ParserError> {

pravic reacted with thumbs up emoji
Copy link
Author

Choose a reason for hiding this comment

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

Not pub?

Here there is a suggestion to use a parser method directly (as a solution/workaround for some problem): #713

So, I assumed all/most of Parser methods are/should be available to users.

Copy link
Author

Choose a reason for hiding this comment

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

For example, currently it's 195 private methods and 322 public methods.

}

/// Parse a CSE (`<expr> AS <ident>`).
pub fn parse_cse(&mut self) -> Result<Cse, ParserError> {
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
pubfn parse_cse(&mut self) -> Result<Cse, ParserError> {
fn parse_cse(&mut self) -> Result<Cse, ParserError> {

Copy link
Author

Choose a reason for hiding this comment

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

Same as above: #2024 (comment)

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.

@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

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 によって変換されたページ (->オリジナル) /