Skip to main content

sqruff_lib_dialects/
clickhouse.rs

1use sqruff_lib_core::dialects::Dialect;
2use sqruff_lib_core::dialects::init::DialectKind;
3use sqruff_lib_core::dialects::syntax::SyntaxKind;
4use sqruff_lib_core::helpers::{Config, ToMatchable};
5use sqruff_lib_core::parser::grammar::anyof::{
6    AnyNumberOf, any_set_of, one_of, optionally_bracketed,
7};
8use sqruff_lib_core::parser::grammar::conditional::Conditional;
9use sqruff_lib_core::parser::grammar::delimited::Delimited;
10use sqruff_lib_core::parser::grammar::sequence::{Bracketed, Sequence};
11use sqruff_lib_core::parser::grammar::{Anything, Ref};
12use sqruff_lib_core::parser::lexer::Matcher;
13use sqruff_lib_core::parser::matchable::MatchableTrait;
14use sqruff_lib_core::parser::node_matcher::NodeMatcher;
15use sqruff_lib_core::parser::parsers::{RegexParser, StringParser, TypedParser};
16use sqruff_lib_core::parser::segments::generator::SegmentGenerator;
17use sqruff_lib_core::parser::segments::meta::MetaSegment;
18use sqruff_lib_core::parser::types::ParseMode;
19
20use super::ansi::{self, raw_dialect};
21use crate::clickhouse_keywords::UNRESERVED_KEYWORDS;
22use sqruff_lib_core::dialects::init::DialectConfig;
23use sqruff_lib_core::value::Value;
24
25sqruff_lib_core::dialect_config!(ClickHouseDialectConfig {});
26
27pub fn dialect(config: Option<&Value>) -> Dialect {
28    // Parse and validate dialect configuration, falling back to defaults on failure
29    let _dialect_config: ClickHouseDialectConfig = config
30        .map(ClickHouseDialectConfig::from_value)
31        .unwrap_or_default();
32    let ansi_dialect = raw_dialect();
33
34    let mut clickhouse_dialect = raw_dialect();
35    clickhouse_dialect.name = DialectKind::Clickhouse;
36    clickhouse_dialect
37        .sets_mut("unreserved_keywords")
38        .extend(UNRESERVED_KEYWORDS);
39
40    clickhouse_dialect.sets_mut("datetime_units").clear();
41    clickhouse_dialect.sets_mut("datetime_units").extend([
42        // https://github.com/ClickHouse/ClickHouse/blob/1cdccd527f0cbf5629b21d29970e28d5156003dc/src/Parsers/parseIntervalKind.cpp#L8
43        "NANOSECOND",
44        "NANOSECONDS",
45        "SQL_TSI_NANOSECOND",
46        "NS",
47        "MICROSECOND",
48        "MICROSECONDS",
49        "SQL_TSI_MICROSECOND",
50        "MCS",
51        "MILLISECOND",
52        "MILLISECONDS",
53        "SQL_TSI_MILLISECOND",
54        "MS",
55        "SECOND",
56        "SECONDS",
57        "SQL_TSI_SECOND",
58        "SS",
59        "S",
60        "MINUTE",
61        "MINUTES",
62        "SQL_TSI_MINUTE",
63        "MI",
64        "N",
65        "HOUR",
66        "HOURS",
67        "SQL_TSI_HOUR",
68        "HH",
69        "H",
70        "DAY",
71        "DAYS",
72        "SQL_TSI_DAY",
73        "DD",
74        "D",
75        "WEEK",
76        "WEEKS",
77        "SQL_TSI_WEEK",
78        "WK",
79        "WW",
80        "MONTH",
81        "MONTHS",
82        "SQL_TSI_MONTH",
83        "MM",
84        "M",
85        "QUARTER",
86        "QUARTERS",
87        "SQL_TSI_QUARTER",
88        "QQ",
89        "Q",
90        "YEAR",
91        "YEARS",
92        "SQL_TSI_YEAR",
93        "YYYY",
94        "YY",
95    ]);
96
97    // ClickHouse supports CTEs with DML statements (INSERT, UPDATE, DELETE)
98    // We add these to NonWithSelectableGrammar so WithCompoundStatementSegment can use them
99    clickhouse_dialect.add([(
100        "NonWithSelectableGrammar".into(),
101        one_of(vec![
102            Ref::new("SetExpressionSegment").to_matchable(),
103            optionally_bracketed(vec![Ref::new("SelectStatementSegment").to_matchable()])
104                .to_matchable(),
105            Ref::new("NonSetSelectableGrammar").to_matchable(),
106            Ref::new("UpdateStatementSegment").to_matchable(),
107            Ref::new("InsertStatementSegment").to_matchable(),
108            Ref::new("DeleteStatementSegment").to_matchable(),
109        ])
110        .to_matchable()
111        .into(),
112    )]);
113
114    clickhouse_dialect.add([
115        (
116            "SelectClauseTerminatorGrammar".into(),
117            clickhouse_dialect
118                .grammar("SelectClauseTerminatorGrammar")
119                .copy(
120                    Some(vec![
121                        Ref::keyword("PREWHERE").to_matchable(),
122                        Ref::keyword("INTO").to_matchable(),
123                        Ref::keyword("FORMAT").to_matchable(),
124                    ]),
125                    None,
126                    Some(Ref::keyword("WHERE").to_matchable()),
127                    None,
128                    Vec::new(),
129                    false,
130                )
131                .into(),
132        ),
133        (
134            "FromClauseTerminatorGrammar".into(),
135            clickhouse_dialect
136                .grammar("FromClauseTerminatorGrammar")
137                .copy(
138                    Some(vec![
139                        Ref::keyword("PREWHERE").to_matchable(),
140                        Ref::keyword("INTO").to_matchable(),
141                        Ref::keyword("FORMAT").to_matchable(),
142                    ]),
143                    None,
144                    Some(Ref::keyword("WHERE").to_matchable()),
145                    None,
146                    Vec::new(),
147                    false,
148                )
149                .copy(
150                    Some(vec![Ref::new("SettingsClauseSegment").to_matchable()]),
151                    None,
152                    None,
153                    None,
154                    Vec::new(),
155                    false,
156                )
157                .into(),
158        ),
159        (
160            "DateTimeLiteralGrammar".into(),
161            Sequence::new(vec![
162                one_of(vec![
163                    Ref::keyword("DATE").to_matchable(),
164                    Ref::keyword("TIME").to_matchable(),
165                    Ref::keyword("TIMESTAMP").to_matchable(),
166                ])
167                .to_matchable(),
168                TypedParser::new(SyntaxKind::SingleQuote, SyntaxKind::DateConstructorLiteral)
169                    .to_matchable(),
170            ])
171            .to_matchable()
172            .into(),
173        ),
174    ]);
175
176    // Disambiguate wildcard EXCEPT from set operator EXCEPT.
177    // Exclude patterns like `EXCEPT ( ... )` and `EXCEPT identifier` from
178    // being parsed as a set operator to allow wildcard `* EXCEPT ...` to bind.
179    clickhouse_dialect.replace_grammar(
180        "SetOperatorSegment",
181        one_of(vec![
182            Ref::new("UnionGrammar").to_matchable(),
183            Sequence::new(vec![
184                one_of(vec![
185                    Ref::keyword("INTERSECT").to_matchable(),
186                    Ref::keyword("EXCEPT").to_matchable(),
187                ])
188                .to_matchable(),
189                Ref::keyword("ALL").optional().to_matchable(),
190            ])
191            .to_matchable(),
192            Ref::keyword("MINUS").to_matchable(),
193        ])
194        .config(|config| {
195            config.exclude = Some(
196                one_of(vec![
197                    Sequence::new(vec![
198                        Ref::keyword("EXCEPT").to_matchable(),
199                        Bracketed::new(vec![Anything::new().to_matchable()]).to_matchable(),
200                    ])
201                    .to_matchable(),
202                    Sequence::new(vec![
203                        Ref::keyword("EXCEPT").to_matchable(),
204                        Ref::new("SingleIdentifierGrammar").to_matchable(),
205                    ])
206                    .to_matchable(),
207                ])
208                .to_matchable(),
209            );
210        })
211        .to_matchable(),
212    );
213
214    clickhouse_dialect.replace_grammar(
215        "FromExpressionElementSegment",
216        Sequence::new(vec![
217            Ref::new("PreTableFunctionKeywordsGrammar")
218                .optional()
219                .to_matchable(),
220            optionally_bracketed(vec![Ref::new("TableExpressionSegment").to_matchable()])
221                .to_matchable(),
222            Ref::new("AliasExpressionSegment")
223                .exclude(one_of(vec![
224                    Ref::new("FromClauseTerminatorGrammar").to_matchable(),
225                    Ref::new("SamplingExpressionSegment").to_matchable(),
226                    Ref::new("JoinLikeClauseGrammar").to_matchable(),
227                    Ref::keyword("FINAL").to_matchable(),
228                    Ref::new("JoinClauseSegment").to_matchable(),
229                ]))
230                .optional()
231                .to_matchable(),
232            Ref::keyword("FINAL").optional().to_matchable(),
233            Sequence::new(vec![
234                Ref::keyword("WITH").to_matchable(),
235                Ref::keyword("OFFSET").to_matchable(),
236                Ref::new("AliasExpressionSegment").to_matchable(),
237            ])
238            .config(|this| this.optional())
239            .to_matchable(),
240            Ref::new("SamplingExpressionSegment")
241                .optional()
242                .to_matchable(),
243            Ref::new("PostTableExpressionGrammar")
244                .optional()
245                .to_matchable(),
246        ])
247        .to_matchable(),
248    );
249
250    clickhouse_dialect.replace_grammar(
251        "JoinClauseSegment",
252        one_of(vec![
253            Sequence::new(vec![
254                Ref::new("JoinTypeKeywords").optional().to_matchable(),
255                Ref::new("JoinKeywordsGrammar").to_matchable(),
256                MetaSegment::indent().to_matchable(),
257                Ref::new("FromExpressionElementSegment").to_matchable(),
258                MetaSegment::dedent().to_matchable(),
259                Conditional::new(MetaSegment::indent())
260                    .indented_using_on()
261                    .to_matchable(),
262                one_of(vec![
263                    Ref::new("JoinOnConditionSegment").to_matchable(),
264                    Sequence::new(vec![
265                        Ref::keyword("USING").to_matchable(),
266                        Conditional::new(MetaSegment::indent())
267                            .indented_using_on()
268                            .to_matchable(),
269                        Delimited::new(vec![
270                            one_of(vec![
271                                Bracketed::new(vec![
272                                    Delimited::new(vec![
273                                        Ref::new("SingleIdentifierGrammar").to_matchable(),
274                                    ])
275                                    .to_matchable(),
276                                ])
277                                .config(|this| this.parse_mode(ParseMode::Greedy))
278                                .to_matchable(),
279                                Delimited::new(vec![
280                                    Ref::new("SingleIdentifierGrammar").to_matchable(),
281                                ])
282                                .to_matchable(),
283                            ])
284                            .to_matchable(),
285                        ])
286                        .to_matchable(),
287                        Conditional::new(MetaSegment::dedent())
288                            .indented_using_on()
289                            .to_matchable(),
290                    ])
291                    .to_matchable(),
292                ])
293                .config(|this| this.optional())
294                .to_matchable(),
295                Conditional::new(MetaSegment::dedent())
296                    .indented_using_on()
297                    .to_matchable(),
298            ])
299            .to_matchable(),
300        ])
301        .to_matchable(),
302    );
303
304    clickhouse_dialect.replace_grammar(
305        "SelectClauseModifierSegment",
306        one_of(vec![
307            Sequence::new(vec![
308                Ref::keyword("DISTINCT").to_matchable(),
309                Sequence::new(vec![
310                    Ref::keyword("ON").to_matchable(),
311                    Bracketed::new(vec![
312                        Delimited::new(vec![Ref::new("ExpressionSegment").to_matchable()])
313                            .to_matchable(),
314                    ])
315                    .to_matchable(),
316                ])
317                .config(|this| this.optional())
318                .to_matchable(),
319            ])
320            .to_matchable(),
321            Ref::keyword("ALL").to_matchable(),
322        ])
323        .to_matchable(),
324    );
325
326    clickhouse_dialect.replace_grammar(
327        "OrderByClauseSegment",
328        Sequence::new(vec![
329            Ref::keyword("ORDER").to_matchable(),
330            Ref::keyword("BY").to_matchable(),
331            MetaSegment::indent().to_matchable(),
332            Delimited::new(vec![
333                Sequence::new(vec![
334                    one_of(vec![
335                        Ref::new("ColumnReferenceSegment").to_matchable(),
336                        Ref::new("NumericLiteralSegment").to_matchable(),
337                        Ref::new("ExpressionSegment").to_matchable(),
338                    ])
339                    .to_matchable(),
340                    one_of(vec![
341                        Ref::keyword("ASC").to_matchable(),
342                        Ref::keyword("DESC").to_matchable(),
343                    ])
344                    .config(|this| this.optional())
345                    .to_matchable(),
346                    Sequence::new(vec![
347                        Ref::keyword("NULLS").to_matchable(),
348                        one_of(vec![
349                            Ref::keyword("FIRST").to_matchable(),
350                            Ref::keyword("LAST").to_matchable(),
351                        ])
352                        .to_matchable(),
353                    ])
354                    .config(|this| this.optional())
355                    .to_matchable(),
356                    Ref::new("WithFillSegment").optional().to_matchable(),
357                ])
358                .to_matchable(),
359            ])
360            .config(|this| {
361                this.terminators = vec![
362                    Ref::keyword("LIMIT").to_matchable(),
363                    Ref::new("FrameClauseUnitGrammar").to_matchable(),
364                ]
365            })
366            .to_matchable(),
367            MetaSegment::dedent().to_matchable(),
368        ])
369        .to_matchable(),
370    );
371
372    clickhouse_dialect.add([
373        (
374            "NakedIdentifierSegment".into(),
375            SegmentGenerator::new(|dialect| {
376                // Generate the anti template from the set of reserved keywords
377                let reserved_keywords = dialect.sets("reserved_keywords");
378                let pattern = reserved_keywords.into_iter().collect::<Vec<_>>().join("|");
379                let anti_template = format!("^({pattern})$");
380                RegexParser::new("[a-zA-Z_][0-9a-zA-Z_]*", SyntaxKind::NakedIdentifier)
381                    .anti_template(&anti_template)
382                    .to_matchable()
383            })
384            .into(),
385        ),
386        (
387            "BackQuotedIdentifierSegment".into(),
388            TypedParser::new(SyntaxKind::BackQuote, SyntaxKind::QuotedIdentifier)
389                .to_matchable()
390                .into(),
391        ),
392        (
393            "SingleIdentifierGrammar".into(),
394            one_of(vec![
395                Ref::new("NakedIdentifierSegment").to_matchable(),
396                Ref::new("QuotedIdentifierSegment").to_matchable(),
397                Ref::new("SingleQuotedIdentifierSegment").to_matchable(),
398                Ref::new("BackQuotedIdentifierSegment").to_matchable(),
399            ])
400            .to_matchable()
401            .into(),
402        ),
403        // A Clickhouse SELECT EXCEPT clause.
404        // https://clickhouse.com/docs/en/sql-reference/statements/select#except
405        (
406            "ExceptClauseSegment".into(),
407            NodeMatcher::new(SyntaxKind::SelectExceptClause, |_| {
408                Sequence::new(vec![
409                    Ref::keyword("EXCEPT").to_matchable(),
410                    one_of(vec![
411                        Bracketed::new(vec![
412                            Delimited::new(vec![
413                                Ref::new("SingleIdentifierGrammar").to_matchable(),
414                            ])
415                            .to_matchable(),
416                        ])
417                        .to_matchable(),
418                        Ref::new("SingleIdentifierGrammar").to_matchable(),
419                    ])
420                    .to_matchable(),
421                ])
422                .to_matchable()
423            })
424            .to_matchable()
425            .into(),
426        ),
427        (
428            "QuotedLiteralSegment".into(),
429            one_of(vec![
430                TypedParser::new(SyntaxKind::SingleQuote, SyntaxKind::QuotedLiteral).to_matchable(),
431                TypedParser::new(SyntaxKind::DollarQuote, SyntaxKind::QuotedLiteral).to_matchable(),
432            ])
433            .to_matchable()
434            .into(),
435        ),
436        (
437            "TupleSegment".into(),
438            NodeMatcher::new(SyntaxKind::Tuple, |_| {
439                Bracketed::new(vec![
440                    Delimited::new(vec![
441                        Ref::new("BaseExpressionElementGrammar").to_matchable(),
442                    ])
443                    .to_matchable(),
444                ])
445                .to_matchable()
446            })
447            .to_matchable()
448            .into(),
449        ),
450    ]);
451
452    clickhouse_dialect.replace_grammar(
453        "WildcardExpressionSegment",
454        ansi::wildcard_expression_segment().copy(
455            Some(vec![
456                Ref::new("ExceptClauseSegment").optional().to_matchable(),
457            ]),
458            None,
459            None,
460            None,
461            Vec::new(),
462            false,
463        ),
464    );
465
466    clickhouse_dialect.add([(
467        "SettingsClauseSegment".into(),
468        Sequence::new(vec![
469            Ref::keyword("SETTINGS").to_matchable(),
470            Delimited::new(vec![
471                Sequence::new(vec![
472                    Ref::new("NakedIdentifierSegment").to_matchable(),
473                    Ref::new("EqualsSegment").to_matchable(),
474                    one_of(vec![
475                        Ref::new("NakedIdentifierSegment").to_matchable(),
476                        Ref::new("NumericLiteralSegment").to_matchable(),
477                        Ref::new("QuotedLiteralSegment").to_matchable(),
478                        Ref::new("BooleanLiteralGrammar").to_matchable(),
479                    ])
480                    .to_matchable(),
481                ])
482                .config(|this| this.optional())
483                .to_matchable(),
484            ])
485            .to_matchable(),
486        ])
487        .config(|this| this.optional())
488        .to_matchable()
489        .into(),
490    )]);
491
492    clickhouse_dialect.add([
493        (
494            // https://clickhouse.com/docs/interfaces/formats
495            "FormatValueSegment".into(),
496            RegexParser::new("[a-zA-Z]*", SyntaxKind::Word)
497                .to_matchable()
498                .into(),
499        ),
500        (
501            "IntoOutfileClauseSegment".into(),
502            Sequence::new(vec![
503                Ref::keyword("INTO").to_matchable(),
504                Ref::keyword("OUTFILE").to_matchable(),
505                Ref::new("QuotedLiteralSegment").to_matchable(),
506                Ref::new("FormatClauseSegment").optional().to_matchable(),
507            ])
508            .to_matchable()
509            .into(),
510        ),
511        (
512            "FormatClauseSegment".into(),
513            Sequence::new(vec![
514                Ref::keyword("FORMAT").to_matchable(),
515                Ref::new("FormatValueSegment").to_matchable(),
516                Ref::new("SettingsClauseSegment").optional().to_matchable(),
517            ])
518            .to_matchable()
519            .into(),
520        ),
521        (
522            "MergeTreesOrderByClauseSegment".into(),
523            NodeMatcher::new(SyntaxKind::MergeTreeOrderByClause, |_| {
524                Sequence::new(vec![
525                    Ref::keyword("ORDER").to_matchable(),
526                    Ref::keyword("BY").to_matchable(),
527                    one_of(vec![
528                        Sequence::new(vec![
529                            Ref::keyword("TUPLE").to_matchable(),
530                            Bracketed::new(vec![]).to_matchable(),
531                        ])
532                        .to_matchable(),
533                        Bracketed::new(vec![
534                            Delimited::new(vec![
535                                Ref::new("ColumnReferenceSegment").to_matchable(),
536                                Ref::new("ExpressionSegment").to_matchable(),
537                            ])
538                            .to_matchable(),
539                        ])
540                        .to_matchable(),
541                        Ref::new("ColumnReferenceSegment").to_matchable(),
542                    ])
543                    .to_matchable(),
544                ])
545                .to_matchable()
546            })
547            .to_matchable()
548            .into(),
549        ),
550    ]);
551
552    clickhouse_dialect.insert_lexer_matchers(
553        vec![Matcher::string("lambda", "->", SyntaxKind::Lambda)],
554        "newline",
555    );
556
557    clickhouse_dialect.add(vec![
558        (
559            "JoinTypeKeywords".into(),
560            Sequence::new(vec![
561                Ref::keyword("GLOBAL").optional().to_matchable(),
562                one_of(vec![
563                    // This case INNER [ANY,ALL] JOIN
564                    Sequence::new(vec![
565                        Ref::keyword("INNER").to_matchable(),
566                        one_of(vec![
567                            Ref::keyword("ALL").to_matchable(),
568                            Ref::keyword("ANY").to_matchable(),
569                        ])
570                        .config(|this| this.optional())
571                        .to_matchable(),
572                    ])
573                    .to_matchable(),
574                    // This case [ANY,ALL] INNER JOIN
575                    Sequence::new(vec![
576                        one_of(vec![
577                            Ref::keyword("ALL").to_matchable(),
578                            Ref::keyword("ANY").to_matchable(),
579                        ])
580                        .config(|this| this.optional())
581                        .to_matchable(),
582                        Ref::keyword("INNER").to_matchable(),
583                    ])
584                    .to_matchable(),
585                    // This case FULL ALL OUTER JOIN
586                    Sequence::new(vec![
587                        Ref::keyword("FULL").to_matchable(),
588                        Ref::keyword("ALL").optional().to_matchable(),
589                        Ref::keyword("OUTER").optional().to_matchable(),
590                    ])
591                    .to_matchable(),
592                    // This case ALL FULL OUTER JOIN
593                    Sequence::new(vec![
594                        Ref::keyword("ALL").optional().to_matchable(),
595                        Ref::keyword("FULL").to_matchable(),
596                        Ref::keyword("OUTER").optional().to_matchable(),
597                    ])
598                    .to_matchable(),
599                    // This case LEFT [OUTER,ANTI,SEMI,ANY,ASOF] JOIN
600                    Sequence::new(vec![
601                        Ref::keyword("LEFT").to_matchable(),
602                        one_of(vec![
603                            Ref::keyword("ANTI").to_matchable(),
604                            Ref::keyword("SEMI").to_matchable(),
605                            one_of(vec![
606                                Ref::keyword("ANY").to_matchable(),
607                                Ref::keyword("ALL").to_matchable(),
608                            ])
609                            .config(|this| this.optional())
610                            .to_matchable(),
611                            Ref::keyword("ASOF").to_matchable(),
612                        ])
613                        .config(|this| this.optional())
614                        .to_matchable(),
615                        Ref::keyword("OUTER").optional().to_matchable(),
616                    ])
617                    .to_matchable(),
618                    // This case [ANTI,SEMI,ANY,ASOF] LEFT JOIN
619                    Sequence::new(vec![
620                        one_of(vec![
621                            Ref::keyword("ANTI").to_matchable(),
622                            Ref::keyword("SEMI").to_matchable(),
623                            one_of(vec![
624                                Ref::keyword("ANY").to_matchable(),
625                                Ref::keyword("ALL").to_matchable(),
626                            ])
627                            .config(|this| this.optional())
628                            .to_matchable(),
629                            Ref::keyword("ASOF").to_matchable(),
630                        ])
631                        .to_matchable(),
632                        Ref::keyword("LEFT").to_matchable(),
633                    ])
634                    .to_matchable(),
635                    // This case RIGHT [OUTER,ANTI,SEMI,ANY,ASOF] JOIN
636                    Sequence::new(vec![
637                        Ref::keyword("RIGHT").to_matchable(),
638                        one_of(vec![
639                            Ref::keyword("OUTER").to_matchable(),
640                            Ref::keyword("ANTI").to_matchable(),
641                            Ref::keyword("SEMI").to_matchable(),
642                            one_of(vec![
643                                Ref::keyword("ANY").to_matchable(),
644                                Ref::keyword("ALL").to_matchable(),
645                            ])
646                            .config(|this| this.optional())
647                            .to_matchable(),
648                        ])
649                        .config(|this| this.optional())
650                        .to_matchable(),
651                        Ref::keyword("OUTER").optional().to_matchable(),
652                    ])
653                    .to_matchable(),
654                    // This case [OUTER,ANTI,SEMI,ANY] RIGHT JOIN
655                    Sequence::new(vec![
656                        one_of(vec![
657                            Ref::keyword("ANTI").to_matchable(),
658                            Ref::keyword("SEMI").to_matchable(),
659                            one_of(vec![
660                                Ref::keyword("ANY").to_matchable(),
661                                Ref::keyword("ALL").to_matchable(),
662                            ])
663                            .config(|this| this.optional())
664                            .to_matchable(),
665                        ])
666                        .to_matchable(),
667                        Ref::keyword("RIGHT").to_matchable(),
668                    ])
669                    .to_matchable(),
670                    // This case CROSS JOIN
671                    Ref::keyword("CROSS").to_matchable(),
672                    // This case PASTE JOIN
673                    Ref::keyword("PASTE").to_matchable(),
674                    // This case ASOF JOIN
675                    Ref::keyword("ASOF").to_matchable(),
676                    // This case ANY JOIN
677                    Ref::keyword("ANY").to_matchable(),
678                    // This case ALL JOIN
679                    Ref::keyword("ALL").to_matchable(),
680                ])
681                .to_matchable(),
682            ])
683            .to_matchable()
684            .into(),
685        ),
686        (
687            "LambdaFunctionSegment".into(),
688            TypedParser::new(SyntaxKind::Lambda, SyntaxKind::Lambda)
689                .to_matchable()
690                .into(),
691        ),
692    ]);
693
694    clickhouse_dialect.add(vec![(
695        "BinaryOperatorGrammar".into(),
696        one_of(vec![
697            Ref::new("ArithmeticBinaryOperatorGrammar").to_matchable(),
698            Ref::new("StringBinaryOperatorGrammar").to_matchable(),
699            Ref::new("BooleanBinaryOperatorGrammar").to_matchable(),
700            Ref::new("ComparisonOperatorGrammar").to_matchable(),
701            // Add Lambda Function
702            Ref::new("LambdaFunctionSegment").to_matchable(),
703        ])
704        .to_matchable()
705        .into(),
706    )]);
707
708    clickhouse_dialect.add([
709        (
710            "JoinLikeClauseGrammar".into(),
711            Sequence::new(vec![
712                AnyNumberOf::new(vec![Ref::new("ArrayJoinClauseSegment").to_matchable()])
713                    .config(|this| this.min_times(1))
714                    .to_matchable(),
715                Ref::new("AliasExpressionSegment").optional().to_matchable(),
716            ])
717            .to_matchable()
718            .into(),
719        ),
720        (
721            "InOperatorGrammar".into(),
722            Sequence::new(vec![
723                Ref::keyword("GLOBAL").optional().to_matchable(),
724                Ref::keyword("NOT").optional().to_matchable(),
725                Ref::keyword("IN").to_matchable(),
726                one_of(vec![
727                    Ref::new("FunctionSegment").to_matchable(), // IN tuple(1, 2)
728                    Ref::new("ArrayLiteralSegment").to_matchable(), // IN [1, 2]
729                    Ref::new("TupleSegment").to_matchable(),    // IN (1, 2)
730                    Ref::new("SingleIdentifierGrammar").to_matchable(), // IN TABLE, IN CTE
731                    Bracketed::new(vec![
732                        Delimited::new(vec![Ref::new("Expression_A_Grammar").to_matchable()])
733                            .to_matchable(),
734                        Ref::new("SelectableGrammar").to_matchable(),
735                    ])
736                    .config(|this| this.parse_mode(ParseMode::Greedy))
737                    .to_matchable(),
738                ])
739                .to_matchable(),
740            ])
741            .to_matchable()
742            .into(),
743        ),
744    ]);
745
746    clickhouse_dialect.add([(
747        "PreWhereClauseSegment".into(),
748        NodeMatcher::new(SyntaxKind::PreWhereClause, |_| {
749            Sequence::new(vec![
750                Ref::keyword("PREWHERE").to_matchable(),
751                // NOTE: The indent here is implicit to allow
752                // constructions like:
753                //
754                //    PREWHERE a
755                //        AND b
756                //
757                // to be valid without forcing an indent between
758                // "PREWHERE" and "a".
759                MetaSegment::implicit_indent().to_matchable(),
760                optionally_bracketed(vec![Ref::new("ExpressionSegment").to_matchable()])
761                    .to_matchable(),
762                MetaSegment::dedent().to_matchable(),
763            ])
764            .to_matchable()
765        })
766        .to_matchable()
767        .into(),
768    )]);
769
770    // We need to replace the UnorderedSelectStatementSegment to include PREWHERE
771    clickhouse_dialect.replace_grammar(
772        "UnorderedSelectStatementSegment",
773        ansi_dialect
774            .grammar("UnorderedSelectStatementSegment")
775            .match_grammar(&ansi_dialect)
776            .unwrap()
777            .copy(
778                Some(vec![
779                    Ref::new("PreWhereClauseSegment").optional().to_matchable(),
780                ]),
781                None,
782                Some(Ref::new("WhereClauseSegment").optional().to_matchable()),
783                None,
784                Vec::new(),
785                false,
786            ),
787    );
788
789    clickhouse_dialect.replace_grammar(
790        "SelectStatementSegment",
791        ansi::select_statement()
792            .copy(
793                Some(vec![
794                    Ref::new("PreWhereClauseSegment").optional().to_matchable(),
795                ]),
796                None,
797                Some(Ref::new("WhereClauseSegment").optional().to_matchable()),
798                None,
799                Vec::new(),
800                false,
801            )
802            .copy(
803                Some(vec![
804                    Ref::new("FormatClauseSegment").optional().to_matchable(),
805                    Ref::new("IntoOutfileClauseSegment")
806                        .optional()
807                        .to_matchable(),
808                    Ref::new("SettingsClauseSegment").optional().to_matchable(),
809                ]),
810                None,
811                None,
812                None,
813                Vec::new(),
814                false,
815            ),
816    );
817
818    clickhouse_dialect.add([(
819        "WithFillSegment".into(),
820        Sequence::new(vec![
821            Ref::keyword("WITH").to_matchable(),
822            Ref::keyword("FILL").to_matchable(),
823            Sequence::new(vec![
824                Ref::keyword("FROM").to_matchable(),
825                Ref::new("ExpressionSegment").to_matchable(),
826            ])
827            .config(|this| this.optional())
828            .to_matchable(),
829            Sequence::new(vec![
830                Ref::keyword("TO").to_matchable(),
831                Ref::new("ExpressionSegment").to_matchable(),
832            ])
833            .config(|this| this.optional())
834            .to_matchable(),
835            Sequence::new(vec![
836                Ref::keyword("STEP").to_matchable(),
837                one_of(vec![
838                    Ref::new("NumericLiteralSegment").to_matchable(),
839                    Ref::new("IntervalExpressionSegment").to_matchable(),
840                ])
841                .to_matchable(),
842            ])
843            .config(|this| this.optional())
844            .to_matchable(),
845        ])
846        .to_matchable()
847        .into(),
848    )]);
849
850    clickhouse_dialect.replace_grammar(
851        "DatatypeSegment",
852        one_of(vec![
853            // Nullable(Type)
854            Sequence::new(vec![
855                StringParser::new("NULLABLE", SyntaxKind::DataTypeIdentifier).to_matchable(),
856                Bracketed::new(vec![Ref::new("DatatypeSegment").to_matchable()]).to_matchable(),
857            ])
858            .to_matchable(),
859            // LowCardinality(Type)
860            Sequence::new(vec![
861                StringParser::new("LOWCARDINALITY", SyntaxKind::DataTypeIdentifier).to_matchable(),
862                Bracketed::new(vec![Ref::new("DatatypeSegment").to_matchable()]).to_matchable(),
863            ])
864            .to_matchable(),
865            // DateTime64(precision, 'timezone')
866            Sequence::new(vec![
867                StringParser::new("DATETIME64", SyntaxKind::DataTypeIdentifier).to_matchable(),
868                Bracketed::new(vec![
869                    Delimited::new(vec![
870                        one_of(vec![
871                            Ref::new("NumericLiteralSegment").to_matchable(), // precision
872                            Ref::new("QuotedLiteralSegment").to_matchable(),
873                        ])
874                        .to_matchable(),
875                    ])
876                    .config(|this| {
877                        this.optional();
878                    })
879                    .to_matchable(),
880                ])
881                .to_matchable(),
882            ])
883            .to_matchable(),
884            // DateTime('timezone')
885            Sequence::new(vec![
886                StringParser::new("DATETIME", SyntaxKind::DataTypeIdentifier).to_matchable(),
887                Bracketed::new(vec![Ref::new("QuotedLiteralSegment").to_matchable()])
888                    .config(|this| this.optional())
889                    .to_matchable(),
890            ])
891            .to_matchable(),
892            // FixedString(length)
893            Sequence::new(vec![
894                StringParser::new("FIXEDSTRING", SyntaxKind::DataTypeIdentifier).to_matchable(),
895                Bracketed::new(vec![Ref::new("NumericLiteralSegment").to_matchable()])
896                    .to_matchable(),
897            ])
898            .to_matchable(),
899            // Array(Type)
900            Sequence::new(vec![
901                StringParser::new("ARRAY", SyntaxKind::DataTypeIdentifier).to_matchable(),
902                Bracketed::new(vec![Ref::new("DatatypeSegment").to_matchable()]).to_matchable(),
903            ])
904            .to_matchable(),
905            // Map(KeyType, ValueType)
906            Sequence::new(vec![
907                StringParser::new("MAP", SyntaxKind::DataTypeIdentifier).to_matchable(),
908                Bracketed::new(vec![
909                    Delimited::new(vec![Ref::new("DatatypeSegment").to_matchable()]).to_matchable(),
910                ])
911                .to_matchable(),
912            ])
913            .to_matchable(),
914            // Tuple(Type1, Type2) or Tuple(name1 Type1, ...)
915            Sequence::new(vec![
916                StringParser::new("TUPLE", SyntaxKind::DataTypeIdentifier).to_matchable(),
917                Bracketed::new(vec![
918                    Delimited::new(vec![
919                        one_of(vec![
920                            // Named tuple element: name Type
921                            Sequence::new(vec![
922                                one_of(vec![
923                                    Ref::new("SingleIdentifierGrammar").to_matchable(),
924                                    Ref::new("QuotedIdentifierSegment").to_matchable(),
925                                ])
926                                .to_matchable(),
927                                Ref::new("DatatypeSegment").to_matchable(),
928                            ])
929                            .to_matchable(),
930                            // Regular tuple element: just Type
931                            Ref::new("DatatypeSegment").to_matchable(),
932                        ])
933                        .to_matchable(),
934                    ])
935                    .to_matchable(),
936                ])
937                .to_matchable(),
938            ])
939            .to_matchable(),
940            // Nested(name1 Type1, name2 Type2)
941            Sequence::new(vec![
942                StringParser::new("NESTED", SyntaxKind::DataTypeIdentifier).to_matchable(),
943                Bracketed::new(vec![
944                    Delimited::new(vec![
945                        Sequence::new(vec![
946                            Ref::new("SingleIdentifierGrammar").to_matchable(),
947                            Ref::new("DatatypeSegment").to_matchable(),
948                        ])
949                        .to_matchable(),
950                    ])
951                    .to_matchable(),
952                ])
953                .to_matchable(),
954            ])
955            .to_matchable(),
956            // JSON data type
957            StringParser::new("JSON", SyntaxKind::DataTypeIdentifier).to_matchable(),
958            // Enum8('val1' = 1, 'val2' = 2)
959            Sequence::new(vec![
960                one_of(vec![
961                    StringParser::new("ENUM8", SyntaxKind::DataTypeIdentifier).to_matchable(),
962                    StringParser::new("ENUM16", SyntaxKind::DataTypeIdentifier).to_matchable(),
963                ])
964                .to_matchable(),
965                Bracketed::new(vec![
966                    Delimited::new(vec![
967                        Sequence::new(vec![
968                            Ref::new("QuotedLiteralSegment").to_matchable(),
969                            Ref::new("EqualsSegment").to_matchable(),
970                            Ref::new("NumericLiteralSegment").to_matchable(),
971                        ])
972                        .to_matchable(),
973                    ])
974                    .to_matchable(),
975                ])
976                .to_matchable(),
977            ])
978            .to_matchable(),
979            // double args
980            Sequence::new(vec![
981                one_of(vec![
982                    StringParser::new("DECIMAL", SyntaxKind::DataTypeIdentifier).to_matchable(),
983                    StringParser::new("NUMERIC", SyntaxKind::DataTypeIdentifier).to_matchable(),
984                ])
985                .to_matchable(),
986                Ref::new("BracketedArguments").optional().to_matchable(),
987            ])
988            .to_matchable(),
989            // single args
990            Sequence::new(vec![
991                one_of(vec![
992                    StringParser::new("DECIMAL32", SyntaxKind::DataTypeIdentifier).to_matchable(),
993                    StringParser::new("DECIMAL64", SyntaxKind::DataTypeIdentifier).to_matchable(),
994                    StringParser::new("DECIMAL128", SyntaxKind::DataTypeIdentifier).to_matchable(),
995                    StringParser::new("DECIMAL256", SyntaxKind::DataTypeIdentifier).to_matchable(),
996                ])
997                .to_matchable(),
998                Bracketed::new(vec![Ref::new("NumericLiteralSegment").to_matchable()])
999                    .to_matchable(),
1000            ])
1001            .to_matchable(),
1002            Ref::new("TupleTypeSegment").to_matchable(),
1003            Ref::new("DatatypeIdentifierSegment").to_matchable(),
1004            Ref::new("NumericLiteralSegment").to_matchable(),
1005            Sequence::new(vec![
1006                StringParser::new("DATETIME64", SyntaxKind::DataTypeIdentifier).to_matchable(),
1007                Bracketed::new(vec![
1008                    Delimited::new(vec![
1009                        Ref::new("NumericLiteralSegment").to_matchable(), // precision
1010                        Ref::new("QuotedLiteralSegment").optional().to_matchable(),
1011                    ])
1012                    // The brackets might be empty as well
1013                    .config(|this| {
1014                        this.optional();
1015                    })
1016                    .to_matchable(),
1017                ])
1018                .config(|this| this.optional())
1019                .to_matchable(),
1020            ])
1021            .to_matchable(),
1022            Sequence::new(vec![
1023                StringParser::new("ARRAY", SyntaxKind::DataTypeIdentifier).to_matchable(),
1024                Bracketed::new(vec![Ref::new("DatatypeSegment").to_matchable()]).to_matchable(),
1025            ])
1026            .to_matchable(),
1027        ])
1028        .to_matchable(),
1029    );
1030
1031    clickhouse_dialect.add([(
1032        "TupleTypeSegment".into(),
1033        Sequence::new(vec![
1034            Ref::keyword("TUPLE").to_matchable(),
1035            Ref::new("TupleTypeSchemaSegment").to_matchable(),
1036        ])
1037        .to_matchable()
1038        .into(),
1039    )]);
1040
1041    clickhouse_dialect.add([(
1042        "TupleTypeSchemaSegment".into(),
1043        Bracketed::new(vec![
1044            Delimited::new(vec![
1045                Sequence::new(vec![
1046                    Ref::new("SingleIdentifierGrammar").to_matchable(),
1047                    Ref::new("DatatypeSegment").to_matchable(),
1048                ])
1049                .to_matchable(),
1050            ])
1051            .to_matchable(),
1052        ])
1053        .to_matchable()
1054        .into(),
1055    )]);
1056
1057    clickhouse_dialect.replace_grammar(
1058        "BracketedArguments",
1059        Bracketed::new(vec![
1060            Delimited::new(vec![
1061                one_of(vec![
1062                    Ref::new("DatatypeIdentifierSegment").to_matchable(),
1063                    Ref::new("NumericLiteralSegment").to_matchable(),
1064                ])
1065                .to_matchable(),
1066            ])
1067            .config(|this| this.optional())
1068            .to_matchable(),
1069        ])
1070        .to_matchable(),
1071    );
1072
1073    clickhouse_dialect.add([(
1074        "ArrayJoinClauseSegment".into(),
1075        NodeMatcher::new(SyntaxKind::ArrayJoinClause, |_| {
1076            Sequence::new(vec![
1077                Ref::keyword("LEFT").optional().to_matchable(),
1078                Ref::keyword("ARRAY").to_matchable(),
1079                Ref::new("JoinKeywordsGrammar").to_matchable(),
1080                MetaSegment::indent().to_matchable(),
1081                Delimited::new(vec![Ref::new("SelectClauseElementSegment").to_matchable()])
1082                    .to_matchable(),
1083                MetaSegment::dedent().to_matchable(),
1084            ])
1085            .to_matchable()
1086        })
1087        .to_matchable()
1088        .into(),
1089    )]);
1090
1091    clickhouse_dialect.replace_grammar(
1092        "CTEDefinitionSegment",
1093        one_of(vec![
1094            Sequence::new(vec![
1095                Ref::new("SingleIdentifierGrammar").to_matchable(),
1096                Ref::new("CTEColumnList").optional().to_matchable(),
1097                Ref::keyword("AS").to_matchable(),
1098                Bracketed::new(vec![Ref::new("SelectableGrammar").to_matchable()])
1099                    .config(|this| this.parse_mode(ParseMode::Greedy))
1100                    .to_matchable(),
1101            ])
1102            .to_matchable(),
1103            Sequence::new(vec![
1104                Ref::new("ExpressionSegment").to_matchable(),
1105                Ref::keyword("AS").to_matchable(),
1106                Ref::new("SingleIdentifierGrammar").to_matchable(),
1107            ])
1108            .to_matchable(),
1109        ])
1110        .to_matchable(),
1111    );
1112
1113    clickhouse_dialect.replace_grammar(
1114        "AliasExpressionSegment",
1115        Sequence::new(vec![
1116            MetaSegment::indent().to_matchable(),
1117            Ref::keyword("AS").optional().to_matchable(),
1118            one_of(vec![
1119                Sequence::new(vec![
1120                    Ref::new("SingleIdentifierGrammar").to_matchable(),
1121                    Bracketed::new(vec![Ref::new("SingleIdentifierListSegment").to_matchable()])
1122                        .config(|this| this.optional())
1123                        .to_matchable(),
1124                ])
1125                .to_matchable(),
1126                Ref::new("SingleQuotedIdentifierSegment").to_matchable(),
1127            ])
1128            .config(|this| {
1129                this.exclude = one_of(vec![
1130                    Ref::keyword("LATERAL").to_matchable(),
1131                    Ref::keyword("WINDOW").to_matchable(),
1132                    Ref::keyword("KEYS").to_matchable(),
1133                ])
1134                .to_matchable()
1135                .into()
1136            })
1137            .to_matchable(),
1138            MetaSegment::dedent().to_matchable(),
1139        ])
1140        .to_matchable(),
1141    );
1142
1143    clickhouse_dialect.add([
1144        (
1145            "TableEngineFunctionSegment".into(),
1146            NodeMatcher::new(SyntaxKind::TableEngineFunction, |_| {
1147                Sequence::new(vec![
1148                    Ref::new("FunctionNameSegment")
1149                        .exclude(one_of(vec![
1150                            Ref::new("DatePartFunctionNameSegment").to_matchable(),
1151                            Ref::new("ValuesClauseSegment").to_matchable(),
1152                        ]))
1153                        .to_matchable(),
1154                    Bracketed::new(vec![
1155                        Ref::new("FunctionContentsGrammar")
1156                            .optional()
1157                            .to_matchable(),
1158                    ])
1159                    .config(|this| {
1160                        this.optional();
1161                        this.parse_mode(ParseMode::Greedy)
1162                    })
1163                    .to_matchable(),
1164                ])
1165                .to_matchable()
1166            })
1167            .to_matchable()
1168            .into(),
1169        ),
1170        (
1171            "OnClusterClauseSegment".into(),
1172            NodeMatcher::new(SyntaxKind::OnClusterClause, |_| {
1173                Sequence::new(vec![
1174                    Ref::keyword("ON").to_matchable(),
1175                    Ref::keyword("CLUSTER").to_matchable(),
1176                    one_of(vec![
1177                        Ref::new("SingleIdentifierGrammar").to_matchable(),
1178                        // Support for placeholders like '{cluster}'
1179                        Ref::new("QuotedLiteralSegment").to_matchable(),
1180                    ])
1181                    .to_matchable(),
1182                ])
1183                .to_matchable()
1184            })
1185            .to_matchable()
1186            .into(),
1187        ),
1188        (
1189            "TableEngineSegment".into(),
1190            NodeMatcher::new(SyntaxKind::Engine, |_| {
1191                Sequence::new(vec![
1192                    Ref::keyword("ENGINE").to_matchable(),
1193                    Ref::new("EqualsSegment").optional().to_matchable(),
1194                    Sequence::new(vec![
1195                        Ref::new("TableEngineFunctionSegment").to_matchable(),
1196                        any_set_of(vec![
1197                            Ref::new("MergeTreesOrderByClauseSegment").to_matchable(),
1198                            Sequence::new(vec![
1199                                Ref::keyword("PARTITION").to_matchable(),
1200                                Ref::keyword("BY").to_matchable(),
1201                                Ref::new("ExpressionSegment").to_matchable(),
1202                            ])
1203                            .to_matchable(),
1204                            Sequence::new(vec![
1205                                Ref::keyword("PRIMARY").to_matchable(),
1206                                Ref::keyword("KEY").to_matchable(),
1207                                Ref::new("ExpressionSegment").to_matchable(),
1208                            ])
1209                            .to_matchable(),
1210                            Sequence::new(vec![
1211                                Ref::keyword("SAMPLE").to_matchable(),
1212                                Ref::keyword("BY").to_matchable(),
1213                                Ref::new("ExpressionSegment").to_matchable(),
1214                            ])
1215                            .to_matchable(),
1216                            Ref::new("SettingsClauseSegment").optional().to_matchable(),
1217                        ])
1218                        .to_matchable(),
1219                    ])
1220                    .to_matchable(),
1221                ])
1222                .to_matchable()
1223            })
1224            .to_matchable()
1225            .into(),
1226        ),
1227        (
1228            "DatabaseEngineFunctionSegment".into(),
1229            NodeMatcher::new(SyntaxKind::EngineFunction, |_| {
1230                Sequence::new(vec![
1231                    one_of(vec![
1232                        Ref::keyword("ATOMIC").to_matchable(),
1233                        Ref::keyword("MYSQL").to_matchable(),
1234                        Ref::keyword("MATERIALIZEDMYSQL").to_matchable(),
1235                        Ref::keyword("LAZY").to_matchable(),
1236                        Ref::keyword("POSTGRESQL").to_matchable(),
1237                        Ref::keyword("MATERIALIZEDPOSTGRESQL").to_matchable(),
1238                        Ref::keyword("REPLICATED").to_matchable(),
1239                        Ref::keyword("SQLITE").to_matchable(),
1240                    ])
1241                    .to_matchable(),
1242                    Bracketed::new(vec![
1243                        Ref::new("FunctionContentsGrammar")
1244                            .optional()
1245                            .to_matchable(),
1246                    ])
1247                    .config(|this| {
1248                        this.parse_mode(ParseMode::Greedy);
1249                        this.optional();
1250                    })
1251                    .to_matchable(),
1252                ])
1253                .to_matchable()
1254            })
1255            .to_matchable()
1256            .into(),
1257        ),
1258        (
1259            "DatabaseEngineSegment".into(),
1260            NodeMatcher::new(SyntaxKind::DatabaseEngine, |_| {
1261                Sequence::new(vec![
1262                    Ref::keyword("ENGINE").to_matchable(),
1263                    Ref::new("EqualsSegment").to_matchable(),
1264                    Sequence::new(vec![
1265                        Ref::new("DatabaseEngineFunctionSegment").to_matchable(),
1266                        any_set_of(vec![
1267                            Ref::new("MergeTreesOrderByClauseSegment").to_matchable(),
1268                            Sequence::new(vec![
1269                                Ref::keyword("PARTITION").to_matchable(),
1270                                Ref::keyword("BY").to_matchable(),
1271                                Ref::new("ExpressionSegment").to_matchable(),
1272                            ])
1273                            .config(|this| this.optional())
1274                            .to_matchable(),
1275                            Sequence::new(vec![
1276                                Ref::keyword("PRIMARY").to_matchable(),
1277                                Ref::keyword("KEY").to_matchable(),
1278                                Ref::new("ExpressionSegment").to_matchable(),
1279                            ])
1280                            .config(|this| this.optional())
1281                            .to_matchable(),
1282                            Sequence::new(vec![
1283                                Ref::keyword("SAMPLE").to_matchable(),
1284                                Ref::keyword("BY").to_matchable(),
1285                                Ref::new("ExpressionSegment").to_matchable(),
1286                            ])
1287                            .config(|this| this.optional())
1288                            .to_matchable(),
1289                            Ref::new("SettingsClauseSegment").optional().to_matchable(),
1290                        ])
1291                        .to_matchable(),
1292                    ])
1293                    .to_matchable(),
1294                ])
1295                .to_matchable()
1296            })
1297            .to_matchable()
1298            .into(),
1299        ),
1300        (
1301            "ColumnTTLSegment".into(),
1302            NodeMatcher::new(SyntaxKind::ColumnTtlSegment, |_| {
1303                Sequence::new(vec![
1304                    Ref::keyword("TTL").to_matchable(),
1305                    Ref::new("ExpressionSegment").to_matchable(),
1306                ])
1307                .to_matchable()
1308            })
1309            .to_matchable()
1310            .into(),
1311        ),
1312        (
1313            "TableTTLSegment".into(),
1314            NodeMatcher::new(SyntaxKind::TableTtlSegment, |_| {
1315                Sequence::new(vec![
1316                    Ref::keyword("TTL").to_matchable(),
1317                    Delimited::new(vec![
1318                        Sequence::new(vec![
1319                            Ref::new("ExpressionSegment").to_matchable(),
1320                            one_of(vec![
1321                                Ref::keyword("DELETE").to_matchable(),
1322                                Sequence::new(vec![
1323                                    Ref::keyword("TO").to_matchable(),
1324                                    Ref::keyword("VOLUME").to_matchable(),
1325                                    Ref::new("QuotedLiteralSegment").to_matchable(),
1326                                ])
1327                                .to_matchable(),
1328                                Sequence::new(vec![
1329                                    Ref::keyword("TO").to_matchable(),
1330                                    Ref::keyword("DISK").to_matchable(),
1331                                    Ref::new("QuotedLiteralSegment").to_matchable(),
1332                                ])
1333                                .to_matchable(),
1334                            ])
1335                            .config(|this| this.optional())
1336                            .to_matchable(),
1337                            Ref::new("WhereClauseSegment").optional().to_matchable(),
1338                            Ref::new("GroupByClauseSegment").optional().to_matchable(),
1339                        ])
1340                        .to_matchable(),
1341                    ])
1342                    .to_matchable(),
1343                ])
1344                .to_matchable()
1345            })
1346            .to_matchable()
1347            .into(),
1348        ),
1349        (
1350            "ColumnConstraintSegment".into(),
1351            NodeMatcher::new(SyntaxKind::ColumnConstraintSegment, |_| {
1352                any_set_of(vec![
1353                    Sequence::new(vec![
1354                        Sequence::new(vec![
1355                            Ref::keyword("CONSTRAINT").to_matchable(),
1356                            Ref::new("ObjectReferenceSegment").to_matchable(),
1357                        ])
1358                        .config(|this| this.optional())
1359                        .to_matchable(),
1360                        one_of(vec![
1361                            Sequence::new(vec![
1362                                Ref::keyword("NOT").optional().to_matchable(),
1363                                Ref::keyword("NULL").to_matchable(),
1364                            ])
1365                            .to_matchable(),
1366                            Sequence::new(vec![
1367                                Ref::keyword("CHECK").to_matchable(),
1368                                Bracketed::new(vec![Ref::new("ExpressionSegment").to_matchable()])
1369                                    .to_matchable(),
1370                            ])
1371                            .to_matchable(),
1372                            Sequence::new(vec![
1373                                one_of(vec![
1374                                    Ref::keyword("DEFAULT").to_matchable(),
1375                                    Ref::keyword("MATERIALIZED").to_matchable(),
1376                                    Ref::keyword("ALIAS").to_matchable(),
1377                                ])
1378                                .to_matchable(),
1379                                one_of(vec![
1380                                    Ref::new("LiteralGrammar").to_matchable(),
1381                                    Ref::new("FunctionSegment").to_matchable(),
1382                                    Ref::new("BareFunctionSegment").to_matchable(),
1383                                ])
1384                                .to_matchable(),
1385                            ])
1386                            .to_matchable(),
1387                            Sequence::new(vec![
1388                                Ref::keyword("EPHEMERAL").to_matchable(),
1389                                one_of(vec![
1390                                    Ref::new("LiteralGrammar").to_matchable(),
1391                                    Ref::new("FunctionSegment").to_matchable(),
1392                                    Ref::new("BareFunctionSegment").to_matchable(),
1393                                ])
1394                                .config(|this| this.optional())
1395                                .to_matchable(),
1396                            ])
1397                            .to_matchable(),
1398                            Ref::new("PrimaryKeyGrammar").to_matchable(),
1399                            Sequence::new(vec![
1400                                Ref::keyword("CODEC").to_matchable(),
1401                                Ref::new("FunctionContentsGrammar").to_matchable(),
1402                            ])
1403                            .config(|this| this.optional())
1404                            .to_matchable(),
1405                            Ref::new("ColumnTTLSegment").to_matchable(),
1406                        ])
1407                        .to_matchable(),
1408                    ])
1409                    .to_matchable(),
1410                ])
1411                .to_matchable()
1412            })
1413            .to_matchable()
1414            .into(),
1415        ),
1416    ]);
1417
1418    clickhouse_dialect.replace_grammar(
1419        "CreateDatabaseStatementSegment",
1420        Sequence::new(vec![
1421            Ref::keyword("CREATE").to_matchable(),
1422            Ref::keyword("DATABASE").to_matchable(),
1423            Ref::new("IfNotExistsGrammar").optional().to_matchable(),
1424            Ref::new("DatabaseReferenceSegment").to_matchable(),
1425            any_set_of(vec![
1426                Ref::new("OnClusterClauseSegment").optional().to_matchable(),
1427                Ref::new("DatabaseEngineSegment").optional().to_matchable(),
1428                Sequence::new(vec![
1429                    Ref::keyword("COMMENT").to_matchable(),
1430                    Ref::new("SingleIdentifierGrammar").to_matchable(),
1431                ])
1432                .config(|this| this.optional())
1433                .to_matchable(),
1434            ])
1435            .to_matchable(),
1436            AnyNumberOf::new(vec![
1437                Ref::keyword("TABLE").to_matchable(),
1438                Ref::keyword("OVERRIDE").to_matchable(),
1439                Ref::new("TableReferenceSegment").to_matchable(),
1440                Bracketed::new(vec![
1441                    Delimited::new(vec![
1442                        Ref::new("TableConstraintSegment").to_matchable(),
1443                        Ref::new("ColumnDefinitionSegment").to_matchable(),
1444                        Ref::new("ColumnConstraintSegment").to_matchable(),
1445                    ])
1446                    .to_matchable(),
1447                ])
1448                .config(|this| this.optional())
1449                .to_matchable(),
1450            ])
1451            .config(|this| this.optional())
1452            .to_matchable(),
1453        ])
1454        .to_matchable(),
1455    );
1456
1457    // https://clickhouse.com/docs/sql-reference/statements/rename
1458    clickhouse_dialect.add([(
1459        "RenameStatementSegment".into(),
1460        NodeMatcher::new(SyntaxKind::RenameTableStatement, |_| {
1461            Sequence::new(vec![
1462                Ref::keyword("RENAME").to_matchable(),
1463                one_of(vec![
1464                    Sequence::new(vec![
1465                        Ref::keyword("TABLE").to_matchable(),
1466                        Delimited::new(vec![
1467                            Sequence::new(vec![
1468                                Ref::new("TableReferenceSegment").to_matchable(),
1469                                Ref::keyword("TO").to_matchable(),
1470                                Ref::new("TableReferenceSegment").to_matchable(),
1471                            ])
1472                            .to_matchable(),
1473                        ])
1474                        .to_matchable(),
1475                    ])
1476                    .to_matchable(),
1477                    Sequence::new(vec![
1478                        Ref::keyword("DATABASE").to_matchable(),
1479                        Delimited::new(vec![
1480                            Sequence::new(vec![
1481                                Ref::new("DatabaseReferenceSegment").to_matchable(),
1482                                Ref::keyword("TO").to_matchable(),
1483                                Ref::new("DatabaseReferenceSegment").to_matchable(),
1484                            ])
1485                            .to_matchable(),
1486                        ])
1487                        .to_matchable(),
1488                    ])
1489                    .to_matchable(),
1490                    Sequence::new(vec![
1491                        Ref::keyword("DICTIONARY").to_matchable(),
1492                        Delimited::new(vec![
1493                            Sequence::new(vec![
1494                                Ref::new("ObjectReferenceSegment").to_matchable(),
1495                                Ref::keyword("TO").to_matchable(),
1496                                Ref::new("ObjectReferenceSegment").to_matchable(),
1497                            ])
1498                            .to_matchable(),
1499                        ])
1500                        .to_matchable(),
1501                    ])
1502                    .to_matchable(),
1503                ])
1504                .to_matchable(),
1505                Ref::new("OnClusterClauseSegment").optional().to_matchable(),
1506            ])
1507            .to_matchable()
1508        })
1509        .to_matchable()
1510        .into(),
1511    )]);
1512
1513    clickhouse_dialect.replace_grammar(
1514        "CreateTableStatementSegment",
1515        one_of(vec![
1516            // Regular CREATE TABLE statement
1517            Sequence::new(vec![
1518                Ref::keyword("CREATE").to_matchable(),
1519                Ref::new("OrReplaceGrammar").optional().to_matchable(),
1520                Ref::keyword("TABLE").to_matchable(),
1521                Ref::new("IfNotExistsGrammar").optional().to_matchable(),
1522                Ref::new("TableReferenceSegment").to_matchable(),
1523                Ref::new("OnClusterClauseSegment").optional().to_matchable(),
1524                one_of(vec![
1525                    // CREATE TABLE (...):
1526                    Sequence::new(vec![
1527                        Bracketed::new(vec![
1528                            Delimited::new(vec![
1529                                one_of(vec![
1530                                    Ref::new("TableConstraintSegment").to_matchable(),
1531                                    Ref::new("ColumnDefinitionSegment").to_matchable(),
1532                                    Ref::new("ColumnConstraintSegment").to_matchable(),
1533                                ])
1534                                .to_matchable(),
1535                            ])
1536                            .to_matchable(),
1537                        ])
1538                        .config(|this| this.optional())
1539                        .to_matchable(),
1540                        Ref::new("TableEngineSegment").to_matchable(),
1541                        // CREATE TABLE (...) AS SELECT:
1542                        Sequence::new(vec![
1543                            Ref::keyword("AS").to_matchable(),
1544                            Ref::new("SelectableGrammar").to_matchable(),
1545                        ])
1546                        .config(|this| this.optional())
1547                        .to_matchable(),
1548                    ])
1549                    .to_matchable(),
1550                    // CREATE TABLE AS other_table:
1551                    Sequence::new(vec![
1552                        Ref::keyword("AS").to_matchable(),
1553                        Ref::new("TableReferenceSegment").to_matchable(),
1554                        Ref::new("TableEngineSegment").optional().to_matchable(),
1555                    ])
1556                    .to_matchable(),
1557                    // CREATE TABLE AS table_function():
1558                    Sequence::new(vec![
1559                        Ref::keyword("AS").to_matchable(),
1560                        Ref::new("FunctionSegment").to_matchable(),
1561                    ])
1562                    .to_matchable(),
1563                ])
1564                .to_matchable(),
1565                any_set_of(vec![
1566                    Sequence::new(vec![
1567                        Ref::keyword("COMMENT").to_matchable(),
1568                        one_of(vec![
1569                            Ref::new("SingleIdentifierGrammar").to_matchable(),
1570                            Ref::new("QuotedIdentifierSegment").to_matchable(),
1571                        ])
1572                        .to_matchable(),
1573                    ])
1574                    .to_matchable(),
1575                    Ref::new("TableTTLSegment").to_matchable(),
1576                ])
1577                .config(|this| this.optional())
1578                .to_matchable(),
1579                Ref::new("TableEndClauseSegment").optional().to_matchable(),
1580            ])
1581            .to_matchable(),
1582            // CREATE TEMPORARY TABLE statement
1583            Sequence::new(vec![
1584                Ref::keyword("CREATE").to_matchable(),
1585                Ref::keyword("TEMPORARY").to_matchable(),
1586                Ref::keyword("TABLE").to_matchable(),
1587                Ref::new("IfNotExistsGrammar").optional().to_matchable(),
1588                Ref::new("TableReferenceSegment").to_matchable(),
1589                one_of(vec![
1590                    // CREATE TEMPORARY TABLE (...):
1591                    Sequence::new(vec![
1592                        Bracketed::new(vec![
1593                            Delimited::new(vec![
1594                                one_of(vec![
1595                                    Ref::new("TableConstraintSegment").to_matchable(),
1596                                    Ref::new("ColumnDefinitionSegment").to_matchable(),
1597                                    Ref::new("ColumnConstraintSegment").to_matchable(),
1598                                ])
1599                                .to_matchable(),
1600                            ])
1601                            .to_matchable(),
1602                        ])
1603                        .config(|this| this.optional())
1604                        .to_matchable(),
1605                        Ref::new("TableEngineSegment").to_matchable(),
1606                        // CREATE TEMPORARY TABLE (...) AS SELECT:
1607                        Sequence::new(vec![
1608                            Ref::keyword("AS").to_matchable(),
1609                            Ref::new("SelectableGrammar").to_matchable(),
1610                        ])
1611                        .config(|this| this.optional())
1612                        .to_matchable(),
1613                    ])
1614                    .to_matchable(),
1615                    // CREATE TEMPORARY TABLE AS other_table:
1616                    Sequence::new(vec![
1617                        Ref::keyword("AS").to_matchable(),
1618                        Ref::new("TableReferenceSegment").to_matchable(),
1619                        Ref::new("TableEngineSegment").optional().to_matchable(),
1620                    ])
1621                    .to_matchable(),
1622                    // CREATE TEMPORARY TABLE AS table_function():
1623                    Sequence::new(vec![
1624                        Ref::keyword("AS").to_matchable(),
1625                        Ref::new("FunctionSegment").to_matchable(),
1626                    ])
1627                    .to_matchable(),
1628                    // CREATE TEMPORARY TABLE AS SELECT (without column definitions)
1629                    Sequence::new(vec![
1630                        Ref::keyword("AS").to_matchable(),
1631                        Ref::new("SelectableGrammar").to_matchable(),
1632                    ])
1633                    .to_matchable(),
1634                ])
1635                .to_matchable(),
1636                any_set_of(vec![
1637                    Sequence::new(vec![
1638                        Ref::keyword("COMMENT").to_matchable(),
1639                        one_of(vec![
1640                            Ref::new("SingleIdentifierGrammar").to_matchable(),
1641                            Ref::new("QuotedIdentifierSegment").to_matchable(),
1642                        ])
1643                        .to_matchable(),
1644                    ])
1645                    .to_matchable(),
1646                    Ref::new("TableTTLSegment").to_matchable(),
1647                ])
1648                .config(|this| this.optional())
1649                .to_matchable(),
1650                Ref::new("TableEndClauseSegment").optional().to_matchable(),
1651            ])
1652            .to_matchable(),
1653        ])
1654        .to_matchable(),
1655    );
1656
1657    clickhouse_dialect.replace_grammar(
1658        "CreateViewStatementSegment",
1659        NodeMatcher::new(SyntaxKind::CreateViewStatement, |_| {
1660            Sequence::new(vec![
1661                Ref::keyword("CREATE").to_matchable(),
1662                Ref::new("OrReplaceGrammar").optional().to_matchable(),
1663                Ref::keyword("VIEW").to_matchable(),
1664                Ref::new("IfNotExistsGrammar").optional().to_matchable(),
1665                Ref::new("TableReferenceSegment").to_matchable(),
1666                Ref::new("OnClusterClauseSegment").optional().to_matchable(),
1667                Ref::keyword("AS").to_matchable(),
1668                Ref::new("SelectableGrammar").to_matchable(),
1669                Ref::new("TableEndClauseSegment").optional().to_matchable(),
1670            ])
1671            .to_matchable()
1672        })
1673        .to_matchable(),
1674    );
1675
1676    clickhouse_dialect.add([(
1677        "CreateMaterializedViewStatementSegment".into(),
1678        NodeMatcher::new(SyntaxKind::CreateMaterializedViewStatement, |_| {
1679            Sequence::new(vec![
1680                Ref::keyword("CREATE").to_matchable(),
1681                Ref::keyword("MATERIALIZED").to_matchable(),
1682                Ref::keyword("VIEW").to_matchable(),
1683                Ref::new("IfNotExistsGrammar").optional().to_matchable(),
1684                Ref::new("TableReferenceSegment").to_matchable(),
1685                Ref::new("OnClusterClauseSegment").optional().to_matchable(),
1686                one_of(vec![
1687                    Sequence::new(vec![
1688                        Ref::keyword("TO").to_matchable(),
1689                        Ref::new("TableReferenceSegment").to_matchable(),
1690                        // Add support for column list in TO clause
1691                        Bracketed::new(vec![
1692                            Delimited::new(vec![
1693                                Ref::new("SingleIdentifierGrammar").to_matchable(),
1694                            ])
1695                            .to_matchable(),
1696                        ])
1697                        .config(|this| this.optional())
1698                        .to_matchable(),
1699                        Ref::new("TableEngineSegment").optional().to_matchable(),
1700                    ])
1701                    .to_matchable(),
1702                    Sequence::new(vec![
1703                        Ref::new("TableEngineSegment").optional().to_matchable(),
1704                        // Add support for PARTITION BY clause
1705                        Sequence::new(vec![
1706                            Ref::keyword("PARTITION").to_matchable(),
1707                            Ref::keyword("BY").to_matchable(),
1708                            Ref::new("ExpressionSegment").to_matchable(),
1709                        ])
1710                        .config(|this| this.optional())
1711                        .to_matchable(),
1712                        // Add support for ORDER BY clause
1713                        Ref::new("MergeTreesOrderByClauseSegment")
1714                            .optional()
1715                            .to_matchable(),
1716                        // Add support for TTL clause
1717                        Ref::new("TableTTLSegment").optional().to_matchable(),
1718                        // Add support for SETTINGS clause
1719                        Ref::new("SettingsClauseSegment").optional().to_matchable(),
1720                        Ref::keyword("POPULATE").optional().to_matchable(),
1721                    ])
1722                    .to_matchable(),
1723                ])
1724                .to_matchable(),
1725                Ref::keyword("AS").to_matchable(),
1726                Ref::new("SelectableGrammar").to_matchable(),
1727                Ref::new("TableEndClauseSegment").optional().to_matchable(),
1728            ])
1729            .to_matchable()
1730        })
1731        .to_matchable()
1732        .into(),
1733    )]);
1734
1735    clickhouse_dialect.replace_grammar(
1736        "DropTableStatementSegment",
1737        Sequence::new(vec![
1738            Ref::keyword("DROP").to_matchable(),
1739            Ref::keyword("TEMPORARY").optional().to_matchable(),
1740            Ref::keyword("TABLE").to_matchable(),
1741            Ref::new("IfExistsGrammar").optional().to_matchable(),
1742            Ref::new("TableReferenceSegment").to_matchable(),
1743            Ref::new("OnClusterClauseSegment").optional().to_matchable(),
1744            Ref::keyword("SYNC").optional().to_matchable(),
1745        ])
1746        .to_matchable(),
1747    );
1748
1749    clickhouse_dialect.replace_grammar(
1750        "DropDatabaseStatementSegment",
1751        Sequence::new(vec![
1752            Ref::keyword("DROP").to_matchable(),
1753            Ref::keyword("DATABASE").to_matchable(),
1754            Ref::new("IfExistsGrammar").optional().to_matchable(),
1755            Ref::new("DatabaseReferenceSegment").to_matchable(),
1756            Ref::new("OnClusterClauseSegment").optional().to_matchable(),
1757            Ref::keyword("SYNC").optional().to_matchable(),
1758        ])
1759        .to_matchable(),
1760    );
1761
1762    clickhouse_dialect.add([(
1763        "DropDictionaryStatementSegment".into(),
1764        NodeMatcher::new(SyntaxKind::DropDictionaryStatement, |_| {
1765            Sequence::new(vec![
1766                Ref::keyword("DROP").to_matchable(),
1767                Ref::keyword("DICTIONARY").to_matchable(),
1768                Ref::new("IfExistsGrammar").optional().to_matchable(),
1769                Ref::new("SingleIdentifierGrammar").to_matchable(),
1770                Ref::keyword("SYNC").optional().to_matchable(),
1771            ])
1772            .to_matchable()
1773        })
1774        .to_matchable()
1775        .into(),
1776    )]);
1777
1778    clickhouse_dialect.replace_grammar(
1779        "DropUserStatementSegment",
1780        Sequence::new(vec![
1781            Ref::keyword("DROP").to_matchable(),
1782            Ref::keyword("USER").to_matchable(),
1783            Ref::new("IfExistsGrammar").optional().to_matchable(),
1784            Ref::new("SingleIdentifierGrammar").to_matchable(),
1785            Ref::new("OnClusterClauseSegment").optional().to_matchable(),
1786        ])
1787        .to_matchable(),
1788    );
1789
1790    clickhouse_dialect.replace_grammar(
1791        "DropRoleStatementSegment",
1792        Sequence::new(vec![
1793            Ref::keyword("DROP").to_matchable(),
1794            Ref::keyword("ROLE").to_matchable(),
1795            Ref::new("IfExistsGrammar").optional().to_matchable(),
1796            Ref::new("SingleIdentifierGrammar").to_matchable(),
1797            Ref::new("OnClusterClauseSegment").optional().to_matchable(),
1798        ])
1799        .to_matchable(),
1800    );
1801
1802    clickhouse_dialect.add([
1803        (
1804            "DropQuotaStatementSegment".into(),
1805            NodeMatcher::new(SyntaxKind::DropQuotaStatement, |_| {
1806                Sequence::new(vec![
1807                    Ref::keyword("DROP").to_matchable(),
1808                    Ref::keyword("QUOTA").to_matchable(),
1809                    Ref::new("IfExistsGrammar").optional().to_matchable(),
1810                    Ref::new("SingleIdentifierGrammar").to_matchable(),
1811                    Ref::new("OnClusterClauseSegment").optional().to_matchable(),
1812                ])
1813                .to_matchable()
1814            })
1815            .to_matchable()
1816            .into(),
1817        ),
1818        (
1819            "DropSettingProfileStatementSegment".into(),
1820            NodeMatcher::new(SyntaxKind::DropSettingProfileStatement, |_| {
1821                Sequence::new(vec![
1822                    Ref::keyword("DROP").to_matchable(),
1823                    Delimited::new(vec![Ref::new("NakedIdentifierSegment").to_matchable()])
1824                        .config(|this| this.min_delimiters = 0)
1825                        .to_matchable(),
1826                    Ref::keyword("PROFILE").to_matchable(),
1827                    Ref::new("IfExistsGrammar").optional().to_matchable(),
1828                    Ref::new("SingleIdentifierGrammar").to_matchable(),
1829                    Ref::new("OnClusterClauseSegment").optional().to_matchable(),
1830                ])
1831                .to_matchable()
1832            })
1833            .to_matchable()
1834            .into(),
1835        ),
1836    ]);
1837
1838    clickhouse_dialect.replace_grammar(
1839        "DropViewStatementSegment",
1840        Sequence::new(vec![
1841            Ref::keyword("DROP").to_matchable(),
1842            Ref::keyword("VIEW").to_matchable(),
1843            Ref::new("IfExistsGrammar").optional().to_matchable(),
1844            Ref::new("TableReferenceSegment").to_matchable(),
1845            Ref::new("OnClusterClauseSegment").optional().to_matchable(),
1846            Ref::keyword("SYNC").optional().to_matchable(),
1847        ])
1848        .to_matchable(),
1849    );
1850
1851    clickhouse_dialect.replace_grammar(
1852        "DropFunctionStatementSegment",
1853        Sequence::new(vec![
1854            Ref::keyword("DROP").to_matchable(),
1855            Ref::keyword("FUNCTION").to_matchable(),
1856            Ref::new("IfExistsGrammar").optional().to_matchable(),
1857            Ref::new("SingleIdentifierGrammar").to_matchable(),
1858            Ref::new("OnClusterClauseSegment").optional().to_matchable(),
1859        ])
1860        .to_matchable(),
1861    );
1862
1863    clickhouse_dialect.add([
1864        (
1865            "SystemMergesSegment".into(),
1866            NodeMatcher::new(SyntaxKind::SystemMergesSegment, |_| {
1867                Sequence::new(vec![
1868                    one_of(vec![
1869                        Ref::keyword("START").to_matchable(),
1870                        Ref::keyword("STOP").to_matchable(),
1871                    ])
1872                    .to_matchable(),
1873                    Ref::keyword("MERGES").to_matchable(),
1874                    one_of(vec![
1875                        Sequence::new(vec![
1876                            Ref::keyword("ON").to_matchable(),
1877                            Ref::keyword("VOLUME").to_matchable(),
1878                            Ref::new("ObjectReferenceSegment").to_matchable(),
1879                        ])
1880                        .to_matchable(),
1881                        Ref::new("TableReferenceSegment").to_matchable(),
1882                    ])
1883                    .to_matchable(),
1884                ])
1885                .to_matchable()
1886            })
1887            .to_matchable()
1888            .into(),
1889        ),
1890        (
1891            "SystemTTLMergesSegment".into(),
1892            NodeMatcher::new(SyntaxKind::SystemTtlMergesSegment, |_| {
1893                Sequence::new(vec![
1894                    one_of(vec![
1895                        Ref::keyword("START").to_matchable(),
1896                        Ref::keyword("STOP").to_matchable(),
1897                    ])
1898                    .to_matchable(),
1899                    Ref::keyword("TTL").to_matchable(),
1900                    Ref::keyword("MERGES").to_matchable(),
1901                    Ref::new("TableReferenceSegment").optional().to_matchable(),
1902                ])
1903                .to_matchable()
1904            })
1905            .to_matchable()
1906            .into(),
1907        ),
1908        (
1909            "SystemMovesSegment".into(),
1910            NodeMatcher::new(SyntaxKind::SystemMovesSegment, |_| {
1911                Sequence::new(vec![
1912                    one_of(vec![
1913                        Ref::keyword("START").to_matchable(),
1914                        Ref::keyword("STOP").to_matchable(),
1915                    ])
1916                    .to_matchable(),
1917                    Ref::keyword("MOVES").to_matchable(),
1918                    Ref::new("TableReferenceSegment").optional().to_matchable(),
1919                ])
1920                .to_matchable()
1921            })
1922            .to_matchable()
1923            .into(),
1924        ),
1925        (
1926            "SystemReplicaSegment".into(),
1927            NodeMatcher::new(SyntaxKind::SystemReplicaSegment, |_| {
1928                one_of(vec![
1929                    Sequence::new(vec![
1930                        Ref::keyword("SYNC").to_matchable(),
1931                        Ref::keyword("REPLICA").to_matchable(),
1932                        Ref::new("OnClusterClauseSegment").optional().to_matchable(),
1933                        Ref::new("TableReferenceSegment").to_matchable(),
1934                        Ref::keyword("STRICT").optional().to_matchable(),
1935                    ])
1936                    .to_matchable(),
1937                    Sequence::new(vec![
1938                        Ref::keyword("DROP").to_matchable(),
1939                        Ref::keyword("REPLICA").to_matchable(),
1940                        Ref::new("SingleIdentifierGrammar").to_matchable(),
1941                        Sequence::new(vec![
1942                            Ref::keyword("FROM").to_matchable(),
1943                            one_of(vec![
1944                                Sequence::new(vec![
1945                                    Ref::keyword("DATABASE").to_matchable(),
1946                                    Ref::new("ObjectReferenceSegment").to_matchable(),
1947                                ])
1948                                .to_matchable(),
1949                                Sequence::new(vec![
1950                                    Ref::keyword("TABLE").to_matchable(),
1951                                    Ref::new("TableReferenceSegment").to_matchable(),
1952                                ])
1953                                .to_matchable(),
1954                                Sequence::new(vec![
1955                                    Ref::keyword("ZKPATH").to_matchable(),
1956                                    Ref::new("PathSegment").to_matchable(),
1957                                ])
1958                                .to_matchable(),
1959                            ])
1960                            .to_matchable(),
1961                        ])
1962                        .config(|this| this.optional())
1963                        .to_matchable(),
1964                    ])
1965                    .to_matchable(),
1966                    Sequence::new(vec![
1967                        Ref::keyword("RESTART").to_matchable(),
1968                        Ref::keyword("REPLICA").to_matchable(),
1969                        Ref::new("TableReferenceSegment").to_matchable(),
1970                    ])
1971                    .to_matchable(),
1972                    Sequence::new(vec![
1973                        Ref::keyword("RESTORE").to_matchable(),
1974                        Ref::keyword("REPLICA").to_matchable(),
1975                        Ref::new("TableReferenceSegment").to_matchable(),
1976                        Ref::new("OnClusterClauseSegment").optional().to_matchable(),
1977                    ])
1978                    .to_matchable(),
1979                ])
1980                .to_matchable()
1981            })
1982            .to_matchable()
1983            .into(),
1984        ),
1985        (
1986            "SystemFilesystemSegment".into(),
1987            NodeMatcher::new(SyntaxKind::SystemFilesystemSegment, |_| {
1988                Sequence::new(vec![
1989                    Ref::keyword("DROP").to_matchable(),
1990                    Ref::keyword("FILESYSTEM").to_matchable(),
1991                    Ref::keyword("CACHE").to_matchable(),
1992                ])
1993                .to_matchable()
1994            })
1995            .to_matchable()
1996            .into(),
1997        ),
1998        (
1999            "SystemReplicatedSegment".into(),
2000            NodeMatcher::new(SyntaxKind::SystemReplicatedSegment, |_| {
2001                Sequence::new(vec![
2002                    one_of(vec![
2003                        Ref::keyword("START").to_matchable(),
2004                        Ref::keyword("STOP").to_matchable(),
2005                    ])
2006                    .to_matchable(),
2007                    Ref::keyword("REPLICATED").to_matchable(),
2008                    Ref::keyword("SENDS").to_matchable(),
2009                    Ref::new("TableReferenceSegment").optional().to_matchable(),
2010                ])
2011                .to_matchable()
2012            })
2013            .to_matchable()
2014            .into(),
2015        ),
2016        (
2017            "SystemReplicationSegment".into(),
2018            NodeMatcher::new(SyntaxKind::SystemReplicationSegment, |_| {
2019                Sequence::new(vec![
2020                    one_of(vec![
2021                        Ref::keyword("START").to_matchable(),
2022                        Ref::keyword("STOP").to_matchable(),
2023                    ])
2024                    .to_matchable(),
2025                    Ref::keyword("REPLICATION").to_matchable(),
2026                    Ref::keyword("QUEUES").to_matchable(),
2027                    Ref::new("TableReferenceSegment").optional().to_matchable(),
2028                ])
2029                .to_matchable()
2030            })
2031            .to_matchable()
2032            .into(),
2033        ),
2034        (
2035            "SystemFetchesSegment".into(),
2036            NodeMatcher::new(SyntaxKind::SystemFetchesSegment, |_| {
2037                Sequence::new(vec![
2038                    one_of(vec![
2039                        Ref::keyword("START").to_matchable(),
2040                        Ref::keyword("STOP").to_matchable(),
2041                    ])
2042                    .to_matchable(),
2043                    Ref::keyword("FETCHES").to_matchable(),
2044                    Ref::new("TableReferenceSegment").optional().to_matchable(),
2045                ])
2046                .to_matchable()
2047            })
2048            .to_matchable()
2049            .into(),
2050        ),
2051        (
2052            "SystemDistributedSegment".into(),
2053            NodeMatcher::new(SyntaxKind::SystemDistributedSegment, |_| {
2054                Sequence::new(vec![
2055                    one_of(vec![
2056                        Sequence::new(vec![
2057                            one_of(vec![
2058                                Ref::keyword("START").to_matchable(),
2059                                Ref::keyword("STOP").to_matchable(),
2060                            ])
2061                            .to_matchable(),
2062                            Ref::keyword("DISTRIBUTED").to_matchable(),
2063                            Ref::keyword("SENDS").to_matchable(),
2064                            Ref::new("TableReferenceSegment").to_matchable(),
2065                        ])
2066                        .to_matchable(),
2067                        Sequence::new(vec![
2068                            Ref::keyword("FLUSH").to_matchable(),
2069                            Ref::keyword("DISTRIBUTED").to_matchable(),
2070                            Ref::new("TableReferenceSegment").to_matchable(),
2071                        ])
2072                        .to_matchable(),
2073                    ])
2074                    .to_matchable(),
2075                ])
2076                .to_matchable()
2077            })
2078            .to_matchable()
2079            .into(),
2080        ),
2081        (
2082            "SystemModelSegment".into(),
2083            NodeMatcher::new(SyntaxKind::SystemModelSegment, |_| {
2084                Sequence::new(vec![
2085                    Ref::keyword("RELOAD").to_matchable(),
2086                    one_of(vec![
2087                        Sequence::new(vec![
2088                            Ref::keyword("MODELS").to_matchable(),
2089                            Ref::new("OnClusterClauseSegment").optional().to_matchable(),
2090                        ])
2091                        .to_matchable(),
2092                        Sequence::new(vec![
2093                            Ref::keyword("MODEL").to_matchable(),
2094                            any_set_of(vec![
2095                                Ref::new("OnClusterClauseSegment").optional().to_matchable(),
2096                                Ref::new("PathSegment").to_matchable(),
2097                            ])
2098                            .to_matchable(),
2099                        ])
2100                        .to_matchable(),
2101                    ])
2102                    .to_matchable(),
2103                ])
2104                .to_matchable()
2105            })
2106            .to_matchable()
2107            .into(),
2108        ),
2109        (
2110            "SystemFileSegment".into(),
2111            NodeMatcher::new(SyntaxKind::SystemFileSegment, |_| {
2112                Sequence::new(vec![
2113                    Ref::keyword("SYNC").to_matchable(),
2114                    Ref::keyword("FILE").to_matchable(),
2115                    Ref::keyword("CACHE").to_matchable(),
2116                ])
2117                .to_matchable()
2118            })
2119            .to_matchable()
2120            .into(),
2121        ),
2122        (
2123            "SystemUnfreezeSegment".into(),
2124            NodeMatcher::new(SyntaxKind::SystemUnfreezeSegment, |_| {
2125                Sequence::new(vec![
2126                    Ref::keyword("UNFREEZE").to_matchable(),
2127                    Ref::keyword("WITH").to_matchable(),
2128                    Ref::keyword("NAME").to_matchable(),
2129                    Ref::new("ObjectReferenceSegment").to_matchable(),
2130                ])
2131                .to_matchable()
2132            })
2133            .to_matchable()
2134            .into(),
2135        ),
2136        (
2137            "SystemStatementSegment".into(),
2138            NodeMatcher::new(SyntaxKind::SystemStatement, |_| {
2139                Sequence::new(vec![
2140                    Ref::keyword("SYSTEM").to_matchable(),
2141                    one_of(vec![
2142                        Ref::new("SystemMergesSegment").to_matchable(),
2143                        Ref::new("SystemTTLMergesSegment").to_matchable(),
2144                        Ref::new("SystemMovesSegment").to_matchable(),
2145                        Ref::new("SystemReplicaSegment").to_matchable(),
2146                        Ref::new("SystemReplicatedSegment").to_matchable(),
2147                        Ref::new("SystemReplicationSegment").to_matchable(),
2148                        Ref::new("SystemFetchesSegment").to_matchable(),
2149                        Ref::new("SystemDistributedSegment").to_matchable(),
2150                        Ref::new("SystemFileSegment").to_matchable(),
2151                        Ref::new("SystemFilesystemSegment").to_matchable(),
2152                        Ref::new("SystemUnfreezeSegment").to_matchable(),
2153                        Ref::new("SystemModelSegment").to_matchable(),
2154                    ])
2155                    .to_matchable(),
2156                ])
2157                .to_matchable()
2158            })
2159            .to_matchable()
2160            .into(),
2161        ),
2162    ]);
2163
2164    // https://clickhouse.com/docs/sql-reference/statements/alter
2165    clickhouse_dialect.replace_grammar(
2166        "AlterTableStatementSegment",
2167        Sequence::new(vec![
2168            Ref::keyword("ALTER").to_matchable(),
2169            Ref::keyword("TABLE").to_matchable(),
2170            Ref::new("IfExistsGrammar").optional().to_matchable(),
2171            Ref::new("TableReferenceSegment").to_matchable(),
2172            Ref::new("OnClusterClauseSegment").optional().to_matchable(),
2173            one_of(vec![
2174                // ALTER TABLE ... DROP COLUMN [IF EXISTS] name
2175                Sequence::new(vec![
2176                    Ref::keyword("DROP").to_matchable(),
2177                    Ref::keyword("COLUMN").to_matchable(),
2178                    Ref::new("IfExistsGrammar").optional().to_matchable(),
2179                    Ref::new("SingleIdentifierGrammar").to_matchable(),
2180                ])
2181                .to_matchable(),
2182                // ALTER TABLE ... ADD COLUMN [IF NOT EXISTS] name [type]
2183                Sequence::new(vec![
2184                    Ref::keyword("ADD").to_matchable(),
2185                    Ref::keyword("COLUMN").to_matchable(),
2186                    Ref::new("IfNotExistsGrammar").optional().to_matchable(),
2187                    Ref::new("SingleIdentifierGrammar").to_matchable(),
2188                    one_of(vec![
2189                        // Regular column with type
2190                        Sequence::new(vec![
2191                            Ref::new("DatatypeSegment").to_matchable(),
2192                            Sequence::new(vec![
2193                                Ref::keyword("DEFAULT").to_matchable(),
2194                                Ref::new("ExpressionSegment").to_matchable(),
2195                            ])
2196                            .config(|this| this.optional())
2197                            .to_matchable(),
2198                            Sequence::new(vec![
2199                                Ref::keyword("MATERIALIZED").to_matchable(),
2200                                Ref::new("ExpressionSegment").to_matchable(),
2201                            ])
2202                            .config(|this| this.optional())
2203                            .to_matchable(),
2204                            Sequence::new(vec![
2205                                Ref::keyword("CODEC").to_matchable(),
2206                                Bracketed::new(vec![
2207                                    Delimited::new(vec![
2208                                        one_of(vec![
2209                                            Ref::new("FunctionSegment").to_matchable(),
2210                                            Ref::new("SingleIdentifierGrammar").to_matchable(),
2211                                        ])
2212                                        .to_matchable(),
2213                                    ])
2214                                    .to_matchable(),
2215                                ])
2216                                .to_matchable(),
2217                            ])
2218                            .config(|this| this.optional())
2219                            .to_matchable(),
2220                        ])
2221                        .to_matchable(),
2222                        // Alias column with type
2223                        Sequence::new(vec![
2224                            Ref::new("DatatypeSegment").to_matchable(),
2225                            Ref::keyword("ALIAS").to_matchable(),
2226                            Ref::new("ExpressionSegment").to_matchable(),
2227                        ])
2228                        .to_matchable(),
2229                        // Alias column without type
2230                        Sequence::new(vec![
2231                            Ref::keyword("ALIAS").to_matchable(),
2232                            Ref::new("ExpressionSegment").to_matchable(),
2233                        ])
2234                        .to_matchable(),
2235                        // Default could also be used without type
2236                        Sequence::new(vec![
2237                            Ref::keyword("DEFAULT").to_matchable(),
2238                            Ref::new("ExpressionSegment").to_matchable(),
2239                        ])
2240                        .to_matchable(),
2241                        // Materialized could also be used without type
2242                        Sequence::new(vec![
2243                            Ref::keyword("MATERIALIZED").to_matchable(),
2244                            Ref::new("ExpressionSegment").to_matchable(),
2245                        ])
2246                        .to_matchable(),
2247                    ])
2248                    .config(|this| this.optional())
2249                    .to_matchable(),
2250                    one_of(vec![
2251                        Sequence::new(vec![
2252                            Ref::keyword("AFTER").to_matchable(),
2253                            Ref::new("SingleIdentifierGrammar").to_matchable(),
2254                        ])
2255                        .to_matchable(),
2256                        Ref::keyword("FIRST").to_matchable(),
2257                    ])
2258                    .config(|this| this.optional())
2259                    .to_matchable(),
2260                ])
2261                .to_matchable(),
2262                // ALTER TABLE ... ADD ALIAS name FOR column_name
2263                Sequence::new(vec![
2264                    Ref::keyword("ADD").to_matchable(),
2265                    Ref::keyword("ALIAS").to_matchable(),
2266                    Ref::new("IfNotExistsGrammar").optional().to_matchable(),
2267                    Ref::new("SingleIdentifierGrammar").to_matchable(),
2268                    Ref::keyword("FOR").to_matchable(),
2269                    Ref::new("SingleIdentifierGrammar").to_matchable(),
2270                ])
2271                .to_matchable(),
2272                // ALTER TABLE ... RENAME COLUMN [IF EXISTS] name to new_name
2273                Sequence::new(vec![
2274                    Ref::keyword("RENAME").to_matchable(),
2275                    Ref::keyword("COLUMN").to_matchable(),
2276                    Ref::new("IfExistsGrammar").optional().to_matchable(),
2277                    Ref::new("SingleIdentifierGrammar").to_matchable(),
2278                    Ref::keyword("TO").to_matchable(),
2279                    Ref::new("SingleIdentifierGrammar").to_matchable(),
2280                ])
2281                .to_matchable(),
2282                // ALTER TABLE ... COMMENT COLUMN [IF EXISTS] name 'Text comment'
2283                Sequence::new(vec![
2284                    Ref::keyword("COMMENT").to_matchable(),
2285                    Ref::keyword("COLUMN").to_matchable(),
2286                    Ref::new("IfExistsGrammar").optional().to_matchable(),
2287                    Ref::new("SingleIdentifierGrammar").to_matchable(),
2288                    Ref::new("QuotedLiteralSegment").to_matchable(),
2289                ])
2290                .to_matchable(),
2291                // ALTER TABLE ... COMMENT 'Text comment'
2292                Sequence::new(vec![
2293                    Ref::keyword("COMMENT").to_matchable(),
2294                    Ref::new("QuotedLiteralSegment").to_matchable(),
2295                ])
2296                .to_matchable(),
2297                // ALTER TABLE ... MODIFY COMMENT 'Text comment'
2298                Sequence::new(vec![
2299                    Ref::keyword("MODIFY").to_matchable(),
2300                    Ref::keyword("COMMENT").to_matchable(),
2301                    Ref::new("QuotedLiteralSegment").to_matchable(),
2302                ])
2303                .to_matchable(),
2304                // ALTER TABLE ... MODIFY COLUMN [IF EXISTS] name [TYPE] [type]
2305                Sequence::new(vec![
2306                    Ref::keyword("MODIFY").to_matchable(),
2307                    Ref::keyword("COLUMN").to_matchable(),
2308                    Ref::new("IfExistsGrammar").optional().to_matchable(),
2309                    Ref::new("SingleIdentifierGrammar").to_matchable(),
2310                    one_of(vec![
2311                        // Type modification with explicit TYPE keyword
2312                        Sequence::new(vec![
2313                            Ref::keyword("TYPE").to_matchable(),
2314                            Ref::new("DatatypeSegment").to_matchable(),
2315                            Sequence::new(vec![
2316                                Ref::keyword("DEFAULT").to_matchable(),
2317                                Ref::new("ExpressionSegment").to_matchable(),
2318                            ])
2319                            .config(|this| this.optional())
2320                            .to_matchable(),
2321                            Sequence::new(vec![
2322                                Ref::keyword("MATERIALIZED").to_matchable(),
2323                                Ref::new("ExpressionSegment").to_matchable(),
2324                            ])
2325                            .config(|this| this.optional())
2326                            .to_matchable(),
2327                            Sequence::new(vec![
2328                                Ref::keyword("CODEC").to_matchable(),
2329                                Bracketed::new(vec![
2330                                    Delimited::new(vec![
2331                                        one_of(vec![
2332                                            Ref::new("FunctionSegment").to_matchable(),
2333                                            Ref::new("SingleIdentifierGrammar").to_matchable(),
2334                                        ])
2335                                        .to_matchable(),
2336                                    ])
2337                                    .to_matchable(),
2338                                ])
2339                                .to_matchable(),
2340                            ])
2341                            .config(|this| this.optional())
2342                            .to_matchable(),
2343                        ])
2344                        .to_matchable(),
2345                        // Type modification without TYPE keyword
2346                        Sequence::new(vec![
2347                            Ref::new("DatatypeSegment").optional().to_matchable(),
2348                            Sequence::new(vec![
2349                                Ref::keyword("DEFAULT").to_matchable(),
2350                                Ref::new("ExpressionSegment").to_matchable(),
2351                            ])
2352                            .config(|this| this.optional())
2353                            .to_matchable(),
2354                            Sequence::new(vec![
2355                                Ref::keyword("MATERIALIZED").to_matchable(),
2356                                Ref::new("ExpressionSegment").to_matchable(),
2357                            ])
2358                            .config(|this| this.optional())
2359                            .to_matchable(),
2360                            Sequence::new(vec![
2361                                Ref::keyword("ALIAS").to_matchable(),
2362                                Ref::new("ExpressionSegment").to_matchable(),
2363                            ])
2364                            .config(|this| this.optional())
2365                            .to_matchable(),
2366                            Sequence::new(vec![
2367                                Ref::keyword("CODEC").to_matchable(),
2368                                Bracketed::new(vec![
2369                                    Delimited::new(vec![
2370                                        one_of(vec![
2371                                            Ref::new("FunctionSegment").to_matchable(),
2372                                            Ref::new("SingleIdentifierGrammar").to_matchable(),
2373                                        ])
2374                                        .to_matchable(),
2375                                    ])
2376                                    .to_matchable(),
2377                                ])
2378                                .to_matchable(),
2379                            ])
2380                            .config(|this| this.optional())
2381                            .to_matchable(),
2382                        ])
2383                        .to_matchable(),
2384                        // Alias modification
2385                        Sequence::new(vec![
2386                            Ref::keyword("ALIAS").to_matchable(),
2387                            Ref::new("ExpressionSegment").to_matchable(),
2388                        ])
2389                        .to_matchable(),
2390                        // Remove alias
2391                        Sequence::new(vec![
2392                            Ref::keyword("REMOVE").to_matchable(),
2393                            Ref::new("ALIAS").to_matchable(),
2394                        ])
2395                        .to_matchable(),
2396                        // Remove property
2397                        Sequence::new(vec![
2398                            Ref::keyword("REMOVE").to_matchable(),
2399                            one_of(vec![
2400                                Ref::keyword("ALIAS").to_matchable(),
2401                                Ref::keyword("DEFAULT").to_matchable(),
2402                                Ref::keyword("MATERIALIZED").to_matchable(),
2403                                Ref::keyword("CODEC").to_matchable(),
2404                                Ref::keyword("COMMENT").to_matchable(),
2405                                Ref::keyword("TTL").to_matchable(),
2406                            ])
2407                            .to_matchable(),
2408                        ])
2409                        .to_matchable(),
2410                        // Modify setting
2411                        Sequence::new(vec![
2412                            Ref::keyword("MODIFY").to_matchable(),
2413                            Ref::keyword("SETTING").to_matchable(),
2414                            Ref::new("SingleIdentifierGrammar").to_matchable(),
2415                            Ref::new("EqualsSegment").to_matchable(),
2416                            Ref::new("LiteralGrammar").to_matchable(),
2417                        ])
2418                        .to_matchable(),
2419                        // Reset setting
2420                        Sequence::new(vec![
2421                            Ref::keyword("RESET").to_matchable(),
2422                            Ref::keyword("SETTING").to_matchable(),
2423                            Ref::new("SingleIdentifierGrammar").to_matchable(),
2424                        ])
2425                        .to_matchable(),
2426                    ])
2427                    .config(|this| this.optional())
2428                    .to_matchable(),
2429                    one_of(vec![
2430                        Sequence::new(vec![
2431                            Ref::keyword("AFTER").to_matchable(),
2432                            Ref::new("SingleIdentifierGrammar").to_matchable(),
2433                        ])
2434                        .to_matchable(),
2435                        Ref::keyword("FIRST").to_matchable(),
2436                    ])
2437                    .config(|this| this.optional())
2438                    .to_matchable(),
2439                ])
2440                .to_matchable(),
2441                // ALTER TABLE ... ALTER COLUMN name [TYPE] [type]
2442                Sequence::new(vec![
2443                    Ref::keyword("ALTER").to_matchable(),
2444                    Ref::keyword("COLUMN").to_matchable(),
2445                    Ref::new("IfExistsGrammar").optional().to_matchable(),
2446                    Ref::new("SingleIdentifierGrammar").to_matchable(),
2447                    one_of(vec![
2448                        // With TYPE keyword
2449                        Sequence::new(vec![
2450                            Ref::keyword("TYPE").to_matchable(),
2451                            Ref::new("DatatypeSegment").to_matchable(),
2452                        ])
2453                        .to_matchable(),
2454                        Ref::new("DatatypeSegment").to_matchable(),
2455                    ])
2456                    .to_matchable(),
2457                    // Without TYPE keyword
2458                    one_of(vec![
2459                        Sequence::new(vec![
2460                            Ref::keyword("AFTER").to_matchable(),
2461                            Ref::new("SingleIdentifierGrammar").to_matchable(),
2462                        ])
2463                        .to_matchable(),
2464                        Ref::keyword("FIRST").to_matchable(),
2465                    ])
2466                    .config(|this| this.optional())
2467                    .to_matchable(),
2468                ])
2469                .to_matchable(),
2470                // ALTER TABLE ... REMOVE TTL
2471                Sequence::new(vec![
2472                    Ref::keyword("REMOVE").to_matchable(),
2473                    Ref::keyword("TTL").to_matchable(),
2474                ])
2475                .to_matchable(),
2476                // ALTER TABLE ... MODIFY TTL expression
2477                Sequence::new(vec![
2478                    Ref::keyword("MODIFY").to_matchable(),
2479                    Ref::keyword("TTL").to_matchable(),
2480                    Ref::new("ExpressionSegment").to_matchable(),
2481                ])
2482                .to_matchable(),
2483                // ALTER TABLE ... MODIFY QUERY select_statement
2484                Sequence::new(vec![
2485                    Ref::keyword("MODIFY").to_matchable(),
2486                    Ref::keyword("QUERY").to_matchable(),
2487                    Ref::new("SelectStatementSegment").to_matchable(),
2488                ])
2489                .to_matchable(),
2490                // ALTER TABLE ... MATERIALIZE COLUMN col
2491                Sequence::new(vec![
2492                    Ref::keyword("MATERIALIZE").to_matchable(),
2493                    Ref::keyword("COLUMN").to_matchable(),
2494                    Ref::new("SingleIdentifierGrammar").to_matchable(),
2495                    one_of(vec![
2496                        Sequence::new(vec![
2497                            Ref::keyword("IN").to_matchable(),
2498                            Ref::keyword("PARTITION").to_matchable(),
2499                            Ref::new("SingleIdentifierGrammar").to_matchable(),
2500                        ])
2501                        .to_matchable(),
2502                        Sequence::new(vec![
2503                            Ref::keyword("IN").to_matchable(),
2504                            Ref::keyword("PARTITION").to_matchable(),
2505                            Ref::keyword("ID").to_matchable(),
2506                            Ref::new("QuotedLiteralSegment").to_matchable(),
2507                        ])
2508                        .to_matchable(),
2509                    ])
2510                    .config(|this| this.optional())
2511                    .to_matchable(),
2512                ])
2513                .to_matchable(),
2514                // ALTER TABLE ... DROP PARTITION|PART partition_expr
2515                Sequence::new(vec![
2516                    Ref::keyword("DROP").to_matchable(),
2517                    one_of(vec![
2518                        Ref::keyword("PARTITION").to_matchable(),
2519                        Ref::keyword("PART").to_matchable(),
2520                    ])
2521                    .to_matchable(),
2522                    Ref::new("SingleIdentifierGrammar").to_matchable(),
2523                ])
2524                .to_matchable(),
2525                // ALTER TABLE ... REPLACE PARTITION partition_expr FROM table1
2526                Sequence::new(vec![
2527                    Ref::keyword("REPLACE").to_matchable(),
2528                    Ref::keyword("PARTITION").to_matchable(),
2529                    Ref::new("SingleIdentifierGrammar").to_matchable(),
2530                    Ref::keyword("FROM").to_matchable(),
2531                    Ref::new("TableReferenceSegment").to_matchable(),
2532                ])
2533                .to_matchable(),
2534            ])
2535            .to_matchable(),
2536        ])
2537        .to_matchable(),
2538    );
2539
2540    clickhouse_dialect.replace_grammar(
2541        "StatementSegment",
2542        ansi::statement_segment().copy(
2543            Some(vec![
2544                Ref::new("CreateMaterializedViewStatementSegment").to_matchable(),
2545                Ref::new("DropDictionaryStatementSegment").to_matchable(),
2546                Ref::new("DropQuotaStatementSegment").to_matchable(),
2547                Ref::new("DropSettingProfileStatementSegment").to_matchable(),
2548                Ref::new("SystemStatementSegment").to_matchable(),
2549                Ref::new("RenameStatementSegment").to_matchable(),
2550                Ref::new("AlterTableStatementSegment").to_matchable(),
2551            ]),
2552            None,
2553            None,
2554            None,
2555            Vec::new(),
2556            false,
2557        ),
2558    );
2559
2560    clickhouse_dialect.add([(
2561        "LimitClauseComponentSegment".into(),
2562        optionally_bracketed(vec![
2563            one_of(vec![
2564                Ref::new("NumericLiteralSegment").to_matchable(),
2565                Ref::new("ExpressionSegment").to_matchable(),
2566            ])
2567            .to_matchable(),
2568        ])
2569        .to_matchable()
2570        .into(),
2571    )]);
2572
2573    clickhouse_dialect.replace_grammar(
2574        "LimitClauseSegment",
2575        Sequence::new(vec![
2576            Ref::keyword("LIMIT").to_matchable(),
2577            MetaSegment::indent().to_matchable(),
2578            Sequence::new(vec![
2579                Ref::new("LimitClauseComponentSegment").to_matchable(),
2580                one_of(vec![
2581                    Sequence::new(vec![
2582                        Ref::keyword("OFFSET").to_matchable(),
2583                        Ref::new("LimitClauseComponentSegment").to_matchable(),
2584                    ])
2585                    .to_matchable(),
2586                    Sequence::new(vec![
2587                        // LIMIT 1,2 only accepts constants
2588                        // and can't be bracketed like that LIMIT (1, 2)
2589                        // but can be bracketed like that LIMIT (1), (2)
2590                        Ref::new("CommaSegment").to_matchable(),
2591                        Ref::new("LimitClauseComponentSegment").to_matchable(),
2592                    ])
2593                    .to_matchable(),
2594                ])
2595                .config(|this| this.optional())
2596                .to_matchable(),
2597                Sequence::new(vec![
2598                    Ref::keyword("BY").to_matchable(),
2599                    one_of(vec![
2600                        Ref::new("BracketedColumnReferenceListGrammar").to_matchable(),
2601                        Ref::new("ColumnReferenceSegment").to_matchable(),
2602                    ])
2603                    .to_matchable(),
2604                ])
2605                .config(|this| this.optional())
2606                .to_matchable(),
2607            ])
2608            .to_matchable(),
2609            MetaSegment::dedent().to_matchable(),
2610        ])
2611        .to_matchable(),
2612    );
2613
2614    // https://clickhouse.com/docs/sql-reference/data-types/special-data-types/interval
2615    // https://clickhouse.com/docs/sql-reference/operators#interval
2616    clickhouse_dialect.replace_grammar(
2617        "IntervalExpressionSegment",
2618        Sequence::new(vec![
2619            Ref::keyword("INTERVAL").to_matchable(),
2620            one_of(vec![
2621                // The Numeric Version
2622                Sequence::new(vec![
2623                    Ref::new("NumericLiteralSegment").to_matchable(),
2624                    Ref::new("DatetimeUnitSegment").to_matchable(),
2625                ])
2626                .to_matchable(),
2627                // The String version
2628                Ref::new("QuotedLiteralSegment").to_matchable(),
2629                // Combine version
2630                Sequence::new(vec![
2631                    Ref::new("QuotedLiteralSegment").to_matchable(),
2632                    Ref::new("DatetimeUnitSegment").to_matchable(),
2633                ])
2634                .to_matchable(),
2635                // With expression as value
2636                Sequence::new(vec![
2637                    Ref::new("ExpressionSegment").to_matchable(),
2638                    Ref::new("DatetimeUnitSegment").to_matchable(),
2639                ])
2640                .to_matchable(),
2641            ])
2642            .to_matchable(),
2643        ])
2644        .to_matchable(),
2645    );
2646
2647    clickhouse_dialect.replace_grammar(
2648        "ColumnDefinitionSegment",
2649        Sequence::new(vec![
2650            one_of(vec![
2651                Ref::new("SingleIdentifierGrammar").to_matchable(),
2652                Ref::new("QuotedIdentifierSegment").to_matchable(),
2653            ])
2654            .to_matchable(),
2655            Ref::new("DatatypeSegment").to_matchable(),
2656            AnyNumberOf::new(vec![
2657                one_of(vec![
2658                    // DEFAULT expression
2659                    Sequence::new(vec![
2660                        Ref::keyword("DEFAULT").to_matchable(),
2661                        one_of(vec![
2662                            Ref::new("LiteralGrammar").to_matchable(),
2663                            Ref::new("FunctionSegment").to_matchable(),
2664                            Ref::new("ExpressionSegment").to_matchable(),
2665                        ])
2666                        .to_matchable(),
2667                    ])
2668                    .to_matchable(),
2669                    // ALIAS expression
2670                    Sequence::new(vec![
2671                        Ref::keyword("ALIAS").to_matchable(),
2672                        Ref::new("ExpressionSegment").to_matchable(),
2673                    ])
2674                    .to_matchable(),
2675                    // MATERIALIZED expression
2676                    Sequence::new(vec![
2677                        Ref::keyword("MATERIALIZED").to_matchable(),
2678                        Ref::new("ExpressionSegment").to_matchable(),
2679                    ])
2680                    .to_matchable(),
2681                    // CODEC(...)
2682                    Sequence::new(vec![
2683                        Ref::keyword("CODEC").to_matchable(),
2684                        Bracketed::new(vec![
2685                            Delimited::new(vec![
2686                                Ref::new("FunctionSegment").to_matchable(),
2687                                Ref::new("SingleIdentifierGrammar").to_matchable(),
2688                            ])
2689                            .to_matchable(),
2690                        ])
2691                        .to_matchable(),
2692                    ])
2693                    .to_matchable(),
2694                    // COMMENT 'text'
2695                    Sequence::new(vec![
2696                        Ref::keyword("COMMENT").to_matchable(),
2697                        Ref::new("QuotedLiteralSegment").to_matchable(),
2698                    ])
2699                    .to_matchable(),
2700                    // Column constraint
2701                    Ref::new("ColumnConstraintSegment").to_matchable(),
2702                ])
2703                .to_matchable(),
2704            ])
2705            .config(|this| this.optional())
2706            .to_matchable(),
2707        ])
2708        .to_matchable(),
2709    );
2710
2711    clickhouse_dialect.expand();
2712    clickhouse_dialect
2713}