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