Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 91 additions & 5 deletions crates/lib-dialects/src/clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,70 @@ pub fn dialect(config: Option<&Value>) -> Dialect {
),
]);

// ClickHouse allows tuple literals as expression arguments in function calls,
// e.g. addInterval((INTERVAL 1 DAY, INTERVAL 1 YEAR), INTERVAL 1 MONTH).
clickhouse_dialect.replace_grammar(
"BaseExpressionElementGrammar",
ansi_dialect.grammar("BaseExpressionElementGrammar").copy(
Some(vec![Ref::new("TupleSegment").to_matchable()]),
None,
Some(Ref::new("ExpressionSegment").to_matchable()),
None,
Vec::new(),
false,
),
);

// Function arguments in ClickHouse may include tuple literals, e.g.
// addInterval((INTERVAL 1 DAY, INTERVAL 1 YEAR), INTERVAL 1 MONTH).
clickhouse_dialect.replace_grammar(
"FunctionContentsExpressionGrammar",
one_of(vec![
Ref::new("TupleSegment").to_matchable(),
Ref::new("ExpressionSegment").to_matchable(),
])
.to_matchable(),
);

// Prioritize delimited argument parsing so single tuple arguments are parsed as
// function arguments instead of generic bracketed expressions.
clickhouse_dialect.replace_grammar(
"FunctionContentsGrammar",
ansi_dialect.grammar("FunctionContentsGrammar").copy(
Some(vec![
Sequence::new(vec![
Ref::keyword("DISTINCT").optional().to_matchable(),
one_of(vec![
Ref::new("StarSegment").to_matchable(),
Delimited::new(vec![
Ref::new("FunctionContentsExpressionGrammar").to_matchable(),
])
.to_matchable(),
])
.to_matchable(),
])
.to_matchable(),
]),
Some(0),
None,
None,
Vec::new(),
false,
),
);

// ClickHouse supports aggregate modifiers after the function call, e.g.
// any(x) RESPECT NULLS.
clickhouse_dialect.replace_grammar(
"PostFunctionGrammar",
one_of(vec![
Ref::new("OverClauseSegment").to_matchable(),
Ref::new("FilterClauseGrammar").to_matchable(),
Ref::new("IgnoreRespectNullsGrammar").to_matchable(),
])
.to_matchable(),
);

clickhouse_dialect.replace_grammar(
"WildcardExpressionSegment",
ansi::wildcard_expression_segment().copy(
Expand Down Expand Up @@ -768,6 +832,22 @@ pub fn dialect(config: Option<&Value>) -> Dialect {
.into(),
)]);

clickhouse_dialect.add([(
"QualifyClauseSegment".into(),
NodeMatcher::new(SyntaxKind::QualifyClause, |_| {
Sequence::new(vec![
Ref::keyword("QUALIFY").to_matchable(),
MetaSegment::implicit_indent().to_matchable(),
optionally_bracketed(vec![Ref::new("ExpressionSegment").to_matchable()])
.to_matchable(),
MetaSegment::dedent().to_matchable(),
])
.to_matchable()
})
.to_matchable()
.into(),
)]);

// We need to replace the UnorderedSelectStatementSegment to include PREWHERE
clickhouse_dialect.replace_grammar(
"UnorderedSelectStatementSegment",
Expand Down Expand Up @@ -800,6 +880,16 @@ pub fn dialect(config: Option<&Value>) -> Dialect {
Vec::new(),
false,
)
.copy(
Some(vec![
Ref::new("QualifyClauseSegment").optional().to_matchable(),
]),
None,
Some(Ref::new("OrderByClauseSegment").optional().to_matchable()),
None,
Vec::new(),
false,
)
.copy(
Some(vec![
Ref::new("FormatClauseSegment").optional().to_matchable(),
Expand Down Expand Up @@ -835,11 +925,7 @@ pub fn dialect(config: Option<&Value>) -> Dialect {
.to_matchable(),
Sequence::new(vec![
Ref::keyword("STEP").to_matchable(),
one_of(vec![
Ref::new("NumericLiteralSegment").to_matchable(),
Ref::new("IntervalExpressionSegment").to_matchable(),
])
.to_matchable(),
Ref::new("ExpressionSegment").to_matchable(),
])
.config(|this| this.optional())
.to_matchable(),
Expand Down
1 change: 1 addition & 0 deletions crates/lib-dialects/src/clickhouse_keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ pub(crate) const UNRESERVED_KEYWORDS: &[&str] = &[
"PROFILE",
"PROJECTION",
"QUARTER",
"QUALIFY",
"QUERY",
"QUOTA",
"QUEUES",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,13 @@ file:
- expression:
- quoted_literal: '''key'''
- comma: ','
- expression:
- tuple:
- bracketed:
- start_bracket: (
- column_reference:
- naked_identifier: category
- comma: ','
- column_reference:
- quoted_identifier: '''CATEGORY'''
- quoted_literal: '''CATEGORY'''
- end_bracket: )
- comma: ','
- expression:
Expand Down Expand Up @@ -661,14 +660,13 @@ file:
- expression:
- quoted_literal: '''price'''
- comma: ','
- expression:
- tuple:
- bracketed:
- start_bracket: (
- column_reference:
- naked_identifier: category
- comma: ','
- column_reference:
- quoted_identifier: '''RESOURCE_TYPE'''
- quoted_literal: '''RESOURCE_TYPE'''
- end_bracket: )
- comma: ','
- expression:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ file:
- column_reference:
- naked_identifier: date
- comma: ','
- expression:
- tuple:
- bracketed:
- start_bracket: (
- column_reference:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ file:
- expression:
- numeric_literal: '5.51'
- keyword: STEP
- numeric_literal: '0.5'
- expression:
- numeric_literal: '0.5'
- statement_terminator: ;
- statement:
- select_statement:
Expand Down Expand Up @@ -173,7 +174,8 @@ file:
- keyword: WITH
- keyword: FILL
- keyword: STEP
- numeric_literal: '5'
- expression:
- numeric_literal: '5'
- statement_terminator: ;
- statement:
- select_statement:
Expand Down Expand Up @@ -260,7 +262,8 @@ file:
- keyword: WITH
- keyword: FILL
- keyword: STEP
- numeric_literal: '5'
- expression:
- numeric_literal: '5'
- comma: ','
- column_reference:
- naked_identifier: d2
Expand Down Expand Up @@ -352,10 +355,11 @@ file:
- keyword: WITH
- keyword: FILL
- keyword: STEP
- interval_expression:
- keyword: INTERVAL
- numeric_literal: '1'
- date_part: DAY
- expression:
- interval_expression:
- keyword: INTERVAL
- numeric_literal: '1'
- date_part: DAY
- comma: ','
- column_reference:
- naked_identifier: d2
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
SELECT any_respect_nulls(x) FROM t;
SELECT any(x) RESPECT NULLS FROM t;
SELECT anyRespectNulls(x) FROM t;
SELECT any_value_respect_nulls(x) FROM t;
SELECT anyValueRespectNulls(x) FROM t;
SELECT any_value(x) RESPECT NULLS FROM t;
SELECT first_value_respect_nulls(x) FROM t;
SELECT firstValueRespectNulls(x) FROM t;
SELECT first_value(x) RESPECT NULLS FROM t;
SELECT anyLast_respect_nulls(x) FROM t;
SELECT anyLastRespectNulls(x) FROM t;
SELECT anyLast(x) RESPECT NULLS FROM t;
SELECT last_value_respect_nulls(x) FROM t;
SELECT lastValueRespectNulls(x) FROM t;
SELECT last_value(x) RESPECT NULLS FROM t;
Loading
Loading