-
Notifications
You must be signed in to change notification settings - Fork 730
ClickHouse: Support unparenthesized IN right-hand side #2385
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -435,6 +435,15 @@ pub trait Dialect: Debug + Any { | |||||||||||
| false | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /// Returns true if the dialect supports a bare expression as the right-hand | ||||||||||||
| /// side of `IN`, without a parenthesized list — as in `x IN 'a'` or the | ||||||||||||
| /// ClickHouse `{name:Type}` query-parameter placeholder `x IN {ids:Array(UInt64)}`. | ||||||||||||
| /// The expression is wrapped into a single-element list, matching ClickHouse, | ||||||||||||
| /// which reformats `x IN 'a'` to `x IN ('a')`. | ||||||||||||
|
Comment on lines
+439
to
+442
Contributor
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.
Suggested change
|
||||||||||||
| fn supports_in_unparenthesized_expr(&self) -> bool { | ||||||||||||
| false | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /// Returns true if the dialect supports `BEGIN {DEFERRED | IMMEDIATE | EXCLUSIVE | TRY | CATCH} [TRANSACTION]` statements | ||||||||||||
| fn supports_start_transaction_modifier(&self) -> bool { | ||||||||||||
| false | ||||||||||||
|
|
@@ -2051,6 +2060,10 @@ mod tests { | |||||||||||
| self.0.supports_in_empty_list() | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| fn supports_in_unparenthesized_expr(&self) -> bool { | ||||||||||||
| self.0.supports_in_unparenthesized_expr() | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| fn convert_type_before_value(&self) -> bool { | ||||||||||||
| self.0.convert_type_before_value() | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -4392,6 +4392,17 @@ impl<'a> Parser<'a> { | |||||
| negated, | ||||||
| }); | ||||||
| } | ||||||
| // ClickHouse accepts a bare expression as the IN RHS (e.g. `x IN 'a'` or | ||||||
| // a `{name:Type}` placeholder), wrapping it into a single-element list. | ||||||
|
Comment on lines
+4395
to
+4396
Contributor
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.
Suggested change
|
||||||
| if self.dialect.supports_in_unparenthesized_expr() | ||||||
| && self.peek_token_ref().token != Token::LParen | ||||||
| { | ||||||
| return Ok(Expr::InList { | ||||||
| expr: Box::new(expr), | ||||||
| list: vec![self.parse_expr()?], | ||||||
| negated, | ||||||
| }); | ||||||
| } | ||||||
| self.expect_token(&Token::LParen)?; | ||||||
| let in_op = match self.maybe_parse(|p| p.parse_query())? { | ||||||
| Some(subquery) => Expr::InSubquery { | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1846,6 +1846,44 @@ fn parse_inner_array_join() { | |
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_in_unparenthesized_placeholder() { | ||
| // ClickHouse `{name:Type}` query-parameter placeholder as the IN RHS, without parens. | ||
| match clickhouse().expr_parses_to("x IN {ids:Array(UInt64)}", "x IN ({ids: Array(UInt64)})") { | ||
|
Contributor
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. lets rewrite these tests to use all_dialects_where and verified_stmt, we can remove the test for dialects that donts support the feature |
||
| Expr::InList { list, negated, .. } => { | ||
| assert!(!negated); | ||
| assert_eq!(list.len(), 1); | ||
| assert!(matches!(list[0], Expr::Dictionary(_))); | ||
| } | ||
| other => panic!("expected InList, got {other:?}"), | ||
|
Comment on lines
+1853
to
+1858
Contributor
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. we can probably drop the ast assertions, I think the funcationality is simple enough to rely only on roundtrip tests |
||
| } | ||
|
|
||
| // NOT IN sets negated. | ||
| match clickhouse().expr_parses_to( | ||
| "x NOT IN {ids:Array(UInt64)}", | ||
| "x NOT IN ({ids: Array(UInt64)})", | ||
| ) { | ||
| Expr::InList { negated, .. } => assert!(negated), | ||
| other => panic!("expected InList, got {other:?}"), | ||
| } | ||
|
|
||
| // A bare scalar is also wrapped, matching ClickHouse (`x IN 'a'` -> `x IN ('a')`). | ||
| clickhouse().expr_parses_to("x IN 'a'", "x IN ('a')"); | ||
|
|
||
| // The new branch must not fire when the next token is `(` (regressions). | ||
| clickhouse().verified_expr("x IN ({ids: Array(UInt64)})"); | ||
| clickhouse().verified_expr("x IN (1, 2, 3)"); | ||
| clickhouse().verified_stmt("SELECT * FROM t WHERE x IN (SELECT y FROM u)"); | ||
|
|
||
| // Precedence: the trailing `AND` is not swallowed into the placeholder. | ||
| clickhouse().verified_expr("x IN ({p: Array(UInt64)}) AND y = 1"); | ||
|
|
||
| // Dialect-scoped: GenericDialect (capability defaults false) still errors. | ||
| assert!(TestedDialects::new(vec![Box::new(GenericDialect {})]) | ||
| .parse_sql_statements("SELECT * FROM t WHERE x IN {ids:Array(UInt64)}") | ||
| .is_err()); | ||
| } | ||
|
|
||
| fn clickhouse() -> TestedDialects { | ||
| TestedDialects::new(vec![Box::new(ClickHouseDialect {})]) | ||
| } | ||
|
|
||
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.
lets add a link to the docs where the syntax comes from