Skip to content

Conversation

pravic
Copy link

@pravic pravic commented Sep 8, 2025

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.

Common scalar expressions:
`WITH <expr> AS <ident>`.
Due to incompatible changes in `With`.
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

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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