Skip to main content

sqruff_lib_dialects/
bigquery.rs

1use itertools::Itertools;
2use sqruff_lib_core::dialects::Dialect;
3use sqruff_lib_core::dialects::init::DialectKind;
4use sqruff_lib_core::dialects::syntax::SyntaxKind;
5use sqruff_lib_core::helpers::{Config, ToMatchable};
6use sqruff_lib_core::parser::grammar::anyof::{AnyNumberOf, one_of, optionally_bracketed};
7use sqruff_lib_core::parser::grammar::delimited::Delimited;
8use sqruff_lib_core::parser::grammar::sequence::{Bracketed, Sequence};
9use sqruff_lib_core::parser::grammar::{Anything, Nothing, Ref};
10use sqruff_lib_core::parser::lexer::Matcher;
11use sqruff_lib_core::parser::matchable::MatchableTrait;
12use sqruff_lib_core::parser::node_matcher::NodeMatcher;
13use sqruff_lib_core::parser::parsers::{MultiStringParser, RegexParser, StringParser, TypedParser};
14use sqruff_lib_core::parser::segments::generator::SegmentGenerator;
15use sqruff_lib_core::parser::segments::meta::MetaSegment;
16use sqruff_lib_core::parser::types::ParseMode;
17
18use super::ansi::{self, raw_dialect};
19use super::bigquery_keywords::{BIGQUERY_RESERVED_KEYWORDS, BIGQUERY_UNRESERVED_KEYWORDS};
20use sqruff_lib_core::dialects::init::DialectConfig;
21use sqruff_lib_core::value::Value;
22
23sqruff_lib_core::dialect_config!(BigQueryDialectConfig {});
24
25pub fn dialect(config: Option<&Value>) -> Dialect {
26    // Parse and validate dialect configuration, falling back to defaults on failure
27    let _dialect_config: BigQueryDialectConfig = config
28        .map(BigQueryDialectConfig::from_value)
29        .unwrap_or_default();
30    let mut dialect = raw_dialect();
31    dialect.name = DialectKind::Bigquery;
32
33    dialect.insert_lexer_matchers(
34        vec![
35            Matcher::string("right_arrow", "=>", SyntaxKind::RightArrow),
36            Matcher::string("question_mark", "?", SyntaxKind::QuestionMark),
37            Matcher::regex(
38                "at_sign_literal",
39                r"@[a-zA-Z_][\w]*",
40                SyntaxKind::AtSignLiteral,
41            ),
42        ],
43        "equals",
44    );
45
46    dialect.patch_lexer_matchers(vec![
47        Matcher::legacy(
48            "single_quote",
49            |s| s.starts_with(['\'', 'R', 'r', 'B', 'b'].as_ref()),
50            r"([rR]?[bB]?|[bB]?[rR]?)?('''((?<!\\)(\\{2})*\\'|'{,2}(?!')|[^'])*(?<!\\)(\\{2})*'''|'((?<!\\)(\\{2})*\\'|[^'])*(?<!\\)(\\{2})*')",
51            SyntaxKind::SingleQuote
52        ),
53        Matcher::legacy(
54            "double_quote",
55            |s| s.starts_with(['"', 'R', 'r', 'B', 'b']),
56            r#"([rR]?[bB]?|[bB]?[rR]?)?(\"\"\"((?<!\\)(\\{2})*\\\"|\"{,2}(?!\")|[^\"])*(?<!\\)(\\{2})*\"\"\"|"((?<!\\)(\\{2})*\\"|[^"])*(?<!\\)(\\{2})*")"#,
57            SyntaxKind::DoubleQuote
58        ),
59    ]);
60
61    // BigQuery supports CTEs with DML statements (INSERT, UPDATE, DELETE, MERGE)
62    // We add these to NonWithSelectableGrammar so WithCompoundStatementSegment can use them
63    dialect.add([(
64        "NonWithSelectableGrammar".into(),
65        one_of(vec![
66            Ref::new("SetExpressionSegment").to_matchable(),
67            optionally_bracketed(vec![Ref::new("SelectStatementSegment").to_matchable()])
68                .to_matchable(),
69            Ref::new("NonSetSelectableGrammar").to_matchable(),
70            Ref::new("UpdateStatementSegment").to_matchable(),
71            Ref::new("InsertStatementSegment").to_matchable(),
72            Ref::new("DeleteStatementSegment").to_matchable(),
73            Ref::new("MergeStatementSegment").to_matchable(),
74        ])
75        .to_matchable()
76        .into(),
77    )]);
78
79    dialect.add([
80        (
81            "DoubleQuotedLiteralSegment".into(),
82            TypedParser::new(SyntaxKind::DoubleQuote, SyntaxKind::QuotedLiteral)
83                .to_matchable()
84                .into(),
85        ),
86        (
87            "SingleQuotedLiteralSegment".into(),
88            TypedParser::new(SyntaxKind::SingleQuote, SyntaxKind::QuotedLiteral)
89                .to_matchable()
90                .into(),
91        ),
92        (
93            "DoubleQuotedUDFBody".into(),
94            TypedParser::new(SyntaxKind::DoubleQuote, SyntaxKind::UdfBody)
95                .to_matchable()
96                .into(),
97        ),
98        (
99            "SingleQuotedUDFBody".into(),
100            TypedParser::new(SyntaxKind::SingleQuote, SyntaxKind::UdfBody)
101                .to_matchable()
102                .into(),
103        ),
104        (
105            "StartAngleBracketSegment".into(),
106            StringParser::new("<", SyntaxKind::StartAngleBracket)
107                .to_matchable()
108                .into(),
109        ),
110        (
111            "EndAngleBracketSegment".into(),
112            StringParser::new(">", SyntaxKind::EndAngleBracket)
113                .to_matchable()
114                .into(),
115        ),
116        (
117            "RightArrowSegment".into(),
118            StringParser::new("=>", SyntaxKind::RightArrow)
119                .to_matchable()
120                .into(),
121        ),
122        (
123            "DashSegment".into(),
124            StringParser::new("-", SyntaxKind::Dash)
125                .to_matchable()
126                .into(),
127        ),
128        (
129            "SingleIdentifierFullGrammar".into(),
130            one_of(vec![
131                Ref::new("NakedIdentifierSegment").to_matchable(),
132                Ref::new("QuotedIdentifierSegment").to_matchable(),
133                Ref::new("NakedIdentifierFullSegment").to_matchable(),
134            ])
135            .to_matchable()
136            .into(),
137        ),
138        (
139            "QuestionMarkSegment".into(),
140            StringParser::new("?", SyntaxKind::QuestionMark)
141                .to_matchable()
142                .into(),
143        ),
144        (
145            "AtSignLiteralSegment".into(),
146            TypedParser::new(SyntaxKind::AtSignLiteral, SyntaxKind::AtSignLiteral)
147                .to_matchable()
148                .into(),
149        ),
150        (
151            "DefaultDeclareOptionsGrammar".into(),
152            Sequence::new(vec![
153                Ref::keyword("DEFAULT").to_matchable(),
154                one_of(vec![
155                    Ref::new("LiteralGrammar").to_matchable(),
156                    Bracketed::new(vec![Ref::new("SelectStatementSegment").to_matchable()])
157                        .to_matchable(),
158                    Ref::new("BareFunctionSegment").to_matchable(),
159                    Ref::new("FunctionSegment").to_matchable(),
160                    Ref::new("ArrayLiteralSegment").to_matchable(),
161                    Ref::new("TupleSegment").to_matchable(),
162                    Ref::new("BaseExpressionElementGrammar").to_matchable(),
163                ])
164                .config(|this| {
165                    this.terminators = vec![Ref::new("SemicolonSegment").to_matchable()];
166                })
167                .to_matchable(),
168            ])
169            .to_matchable()
170            .into(),
171        ),
172        (
173            "ExtendedDatetimeUnitSegment".into(),
174            SegmentGenerator::new(|dialect| {
175                MultiStringParser::new(
176                    dialect
177                        .sets("extended_datetime_units")
178                        .into_iter()
179                        .map_into()
180                        .collect_vec(),
181                    SyntaxKind::DatePart,
182                )
183                .to_matchable()
184            })
185            .into(),
186        ),
187        (
188            "NakedIdentifierFullSegment".into(),
189            RegexParser::new("[A-Z_][A-Z0-9_]*", SyntaxKind::NakedIdentifierAll)
190                .to_matchable()
191                .into(),
192        ),
193        (
194            "NakedIdentifierPart".into(),
195            RegexParser::new("[A-Z0-9_]+", SyntaxKind::NakedIdentifier)
196                .to_matchable()
197                .into(),
198        ),
199        (
200            "ProcedureNameIdentifierSegment".into(),
201            one_of(vec![
202                RegexParser::new("[A-Z_][A-Z0-9_]*", SyntaxKind::ProcedureNameIdentifier)
203                    .anti_template("STRUCT")
204                    .to_matchable(),
205                RegexParser::new("`[^`]*`", SyntaxKind::ProcedureNameIdentifier).to_matchable(),
206            ])
207            .to_matchable()
208            .into(),
209        ),
210        (
211            "ProcedureParameterGrammar".into(),
212            one_of(vec![
213                Sequence::new(vec![
214                    one_of(vec![
215                        Ref::keyword("IN").to_matchable(),
216                        Ref::keyword("OUT").to_matchable(),
217                        Ref::keyword("INOUT").to_matchable(),
218                    ])
219                    .config(|this| this.optional())
220                    .to_matchable(),
221                    Ref::new("ParameterNameSegment").optional().to_matchable(),
222                    one_of(vec![
223                        Sequence::new(vec![
224                            Ref::keyword("ANY").to_matchable(),
225                            Ref::keyword("TYPE").to_matchable(),
226                        ])
227                        .to_matchable(),
228                        Ref::new("DatatypeSegment").to_matchable(),
229                    ])
230                    .to_matchable(),
231                ])
232                .to_matchable(),
233                one_of(vec![
234                    Sequence::new(vec![
235                        Ref::keyword("ANY").to_matchable(),
236                        Ref::keyword("TYPE").to_matchable(),
237                    ])
238                    .to_matchable(),
239                    Ref::new("DatatypeSegment").to_matchable(),
240                ])
241                .to_matchable(),
242            ])
243            .to_matchable()
244            .into(),
245        ),
246    ]);
247
248    dialect.add([
249        (
250            "NakedIdentifierSegment".into(),
251            SegmentGenerator::new(|dialect| {
252                // Generate the anti template from the set of reserved keywords
253                let reserved_keywords = dialect.sets("reserved_keywords");
254                let pattern = reserved_keywords.iter().join("|");
255                let anti_template = format!("^({pattern})$");
256
257                RegexParser::new("[A-Z_][A-Z0-9_]*", SyntaxKind::NakedIdentifier)
258                    .anti_template(&anti_template)
259                    .to_matchable()
260            })
261            .into(),
262        ),
263        (
264            "FunctionContentsExpressionGrammar".into(),
265            one_of(vec![
266                Ref::new("DatetimeUnitSegment").to_matchable(),
267                Ref::new("DatePartWeekSegment").to_matchable(),
268                Sequence::new(vec![
269                    Ref::new("ExpressionSegment").to_matchable(),
270                    Sequence::new(vec![
271                        one_of(vec![
272                            Ref::keyword("IGNORE").to_matchable(),
273                            Ref::keyword("RESPECT").to_matchable(),
274                        ])
275                        .to_matchable(),
276                        Ref::keyword("NULLS").to_matchable(),
277                    ])
278                    .config(|this| this.optional())
279                    .to_matchable(),
280                ])
281                .to_matchable(),
282                Sequence::new(vec![
283                    Ref::new("ExpressionSegment").to_matchable(),
284                    Ref::keyword("HAVING").to_matchable(),
285                    one_of(vec![
286                        Ref::keyword("MIN").to_matchable(),
287                        Ref::keyword("MAX").to_matchable(),
288                    ])
289                    .to_matchable(),
290                    Ref::new("ExpressionSegment").to_matchable(),
291                ])
292                .to_matchable(),
293                Ref::new("NamedArgumentSegment").to_matchable(),
294            ])
295            .to_matchable()
296            .into(),
297        ),
298        (
299            "TrimParametersGrammar".into(),
300            Nothing::new().to_matchable().into(),
301        ),
302        (
303            "ParameterNameSegment".into(),
304            one_of(vec![
305                RegexParser::new("[A-Z_][A-Z0-9_]*", SyntaxKind::Parameter).to_matchable(),
306                RegexParser::new("`[^`]*`", SyntaxKind::Parameter).to_matchable(),
307            ])
308            .to_matchable()
309            .into(),
310        ),
311        (
312            "DateTimeLiteralGrammar".into(),
313            Sequence::new(vec![
314                one_of(vec![
315                    Ref::keyword("DATE").to_matchable(),
316                    Ref::keyword("DATETIME").to_matchable(),
317                    Ref::keyword("TIME").to_matchable(),
318                    Ref::keyword("TIMESTAMP").to_matchable(),
319                ])
320                .to_matchable(),
321                TypedParser::new(SyntaxKind::SingleQuote, SyntaxKind::DateConstructorLiteral)
322                    .to_matchable(),
323            ])
324            .to_matchable()
325            .into(),
326        ),
327        (
328            "JoinLikeClauseGrammar".into(),
329            Sequence::new(vec![
330                AnyNumberOf::new(vec![
331                    Ref::new("FromPivotExpressionSegment").to_matchable(),
332                    Ref::new("FromUnpivotExpressionSegment").to_matchable(),
333                ])
334                .config(|this| this.min_times = 1)
335                .to_matchable(),
336                Ref::new("AliasExpressionSegment").optional().to_matchable(),
337            ])
338            .to_matchable()
339            .into(),
340        ),
341        (
342            "NaturalJoinKeywordsGrammar".into(),
343            Nothing::new().to_matchable().into(),
344        ),
345        (
346            "AccessorGrammar".into(),
347            AnyNumberOf::new(vec![
348                Ref::new("ArrayAccessorSegment").to_matchable(),
349                Ref::new("SemiStructuredAccessorSegment").to_matchable(),
350            ])
351            .to_matchable()
352            .into(),
353        ),
354        (
355            "MergeIntoLiteralGrammar".into(),
356            Sequence::new(vec![
357                Ref::keyword("MERGE").to_matchable(),
358                Ref::keyword("INTO").optional().to_matchable(),
359            ])
360            .to_matchable()
361            .into(),
362        ),
363        (
364            "PrimaryKeyGrammar".into(),
365            Nothing::new().to_matchable().into(),
366        ),
367        (
368            "ForeignKeyGrammar".into(),
369            Nothing::new().to_matchable().into(),
370        ),
371    ]);
372
373    // Set Keywords
374    dialect.sets_mut("unreserved_keywords").clear();
375    dialect.update_keywords_set_from_multiline_string(
376        "unreserved_keywords",
377        BIGQUERY_UNRESERVED_KEYWORDS,
378    );
379
380    dialect.sets_mut("reserved_keywords").clear();
381    dialect
382        .update_keywords_set_from_multiline_string("reserved_keywords", BIGQUERY_RESERVED_KEYWORDS);
383
384    // Add additional datetime units
385    // https://cloud.google.com/bigquery/docs/reference/standard-sql/timestamp_functions#extract
386    dialect.sets_mut("datetime_units").extend([
387        "MICROSECOND",
388        "MILLISECOND",
389        "SECOND",
390        "MINUTE",
391        "HOUR",
392        "DAY",
393        "DAYOFWEEK",
394        "DAYOFYEAR",
395        "WEEK",
396        "ISOWEEK",
397        "MONTH",
398        "QUARTER",
399        "YEAR",
400        "ISOYEAR",
401    ]);
402
403    // Add additional datetime units only recognised in some functions (e.g.
404    // extract)
405    dialect
406        .sets_mut("extended_datetime_units")
407        .extend(["DATE", "DATETIME", "TIME"]);
408
409    dialect.sets_mut("date_part_function_name").clear();
410    dialect.sets_mut("date_part_function_name").extend([
411        "DATE_DIFF",
412        "DATE_TRUNC",
413        "DATETIME_DIFF",
414        "DATETIME_TRUNC",
415        "EXTRACT",
416        "LAST_DAY",
417        "TIME_DIFF",
418        "TIME_TRUNC",
419        "TIMESTAMP_DIFF",
420        "TIMESTAMP_TRUNC",
421    ]);
422
423    // Set value table functions
424    dialect.sets_mut("value_table_functions").extend(["UNNEST"]);
425
426    // Set angle bracket pairs
427    dialect.bracket_sets_mut("angle_bracket_pairs").extend([(
428        "angle",
429        "StartAngleBracketSegment",
430        "EndAngleBracketSegment",
431        false,
432    )]);
433
434    dialect.add([
435        (
436            "ProcedureParameterListSegment".into(),
437            NodeMatcher::new(SyntaxKind::ProcedureParameterList, |_| {
438                Bracketed::new(vec![
439                    Delimited::new(vec![Ref::new("ProcedureParameterGrammar").to_matchable()])
440                        .config(|this| this.optional())
441                        .to_matchable(),
442                ])
443                .to_matchable()
444            })
445            .to_matchable()
446            .into(),
447        ),
448        (
449            "ProcedureStatements".into(),
450            NodeMatcher::new(SyntaxKind::ProcedureStatements, |_| {
451                AnyNumberOf::new(vec![
452                    Sequence::new(vec![
453                        Ref::new("StatementSegment").to_matchable(),
454                        Ref::new("DelimiterGrammar").to_matchable(),
455                    ])
456                    .to_matchable(),
457                ])
458                .config(|this| {
459                    this.terminators = vec![Ref::keyword("END").to_matchable()];
460                    this.parse_mode = ParseMode::Greedy;
461                })
462                .to_matchable()
463            })
464            .to_matchable()
465            .into(),
466        ),
467        (
468            "CreateProcedureStatementSegment".into(),
469            NodeMatcher::new(SyntaxKind::CreateProcedureStatement, |_| {
470                Sequence::new(vec![
471                    Ref::keyword("CREATE").to_matchable(),
472                    Ref::new("OrReplaceGrammar").optional().to_matchable(),
473                    Ref::keyword("PROCEDURE").to_matchable(),
474                    Ref::new("IfNotExistsGrammar").optional().to_matchable(),
475                    Ref::new("ProcedureNameSegment").to_matchable(),
476                    Ref::new("ProcedureParameterListSegment").to_matchable(),
477                    Ref::new("OptionsSegment").optional().to_matchable(),
478                    Ref::keyword("BEGIN").to_matchable(),
479                    MetaSegment::indent().to_matchable(),
480                    Ref::new("ProcedureStatements").to_matchable(),
481                    MetaSegment::dedent().to_matchable(),
482                    Ref::keyword("END").to_matchable(),
483                ])
484                .to_matchable()
485            })
486            .to_matchable()
487            .into(),
488        ),
489        (
490            "CallStatementSegment".into(),
491            NodeMatcher::new(SyntaxKind::CallStatement, |_| {
492                Sequence::new(vec![
493                    Ref::keyword("CALL").to_matchable(),
494                    Ref::new("ProcedureNameSegment").to_matchable(),
495                    Bracketed::new(vec![
496                        Delimited::new(vec![Ref::new("ExpressionSegment").to_matchable()])
497                            .config(|this| this.optional())
498                            .to_matchable(),
499                    ])
500                    .to_matchable(),
501                ])
502                .to_matchable()
503            })
504            .to_matchable()
505            .into(),
506        ),
507        (
508            "ReturnStatementSegment".into(),
509            NodeMatcher::new(SyntaxKind::ReturnStatement, |_| {
510                Sequence::new(vec![Ref::keyword("RETURN").to_matchable()]).to_matchable()
511            })
512            .to_matchable()
513            .into(),
514        ),
515        (
516            "BreakStatementSegment".into(),
517            NodeMatcher::new(SyntaxKind::BreakStatement, |_| {
518                Sequence::new(vec![Ref::keyword("BREAK").to_matchable()]).to_matchable()
519            })
520            .to_matchable()
521            .into(),
522        ),
523        (
524            "LeaveStatementSegment".into(),
525            NodeMatcher::new(SyntaxKind::LeaveStatement, |_| {
526                Sequence::new(vec![Ref::keyword("LEAVE").to_matchable()]).to_matchable()
527            })
528            .to_matchable()
529            .into(),
530        ),
531        (
532            "ContinueStatementSegment".into(),
533            NodeMatcher::new(SyntaxKind::ContinueStatement, |_| {
534                one_of(vec![
535                    Ref::keyword("CONTINUE").to_matchable(),
536                    Ref::keyword("ITERATE").to_matchable(),
537                ])
538                .to_matchable()
539            })
540            .to_matchable()
541            .into(),
542        ),
543        (
544            "RaiseStatementSegment".into(),
545            NodeMatcher::new(SyntaxKind::RaiseStatement, |_| {
546                Sequence::new(vec![
547                    Ref::keyword("RAISE").to_matchable(),
548                    Sequence::new(vec![
549                        Ref::keyword("USING").to_matchable(),
550                        Ref::keyword("MESSAGE").to_matchable(),
551                        Ref::new("EqualsSegment").to_matchable(),
552                        Ref::new("ExpressionSegment").optional().to_matchable(),
553                    ])
554                    .config(|this| this.optional())
555                    .to_matchable(),
556                ])
557                .to_matchable()
558            })
559            .to_matchable()
560            .into(),
561        ),
562    ]);
563
564    dialect.replace_grammar(
565        "ArrayTypeSegment",
566        Sequence::new(vec![
567            Ref::keyword("ARRAY").to_matchable(),
568            Bracketed::new(vec![Ref::new("DatatypeSegment").to_matchable()])
569                .config(|this| {
570                    this.bracket_type = "angle";
571                    this.bracket_pairs_set = "angle_bracket_pairs";
572                    this.optional();
573                })
574                .to_matchable(),
575        ])
576        .to_matchable(),
577    );
578
579    dialect.add([
580        (
581            "QualifyClauseSegment".into(),
582            NodeMatcher::new(SyntaxKind::QualifyClause, |_| {
583                Sequence::new(vec![
584                    Ref::keyword("QUALIFY").to_matchable(),
585                    MetaSegment::indent().to_matchable(),
586                    optionally_bracketed(vec![Ref::new("ExpressionSegment").to_matchable()])
587                        .to_matchable(),
588                    MetaSegment::dedent().to_matchable(),
589                ])
590                .to_matchable()
591            })
592            .to_matchable()
593            .into(),
594        ),
595        (
596            "SetOperatorSegment".into(),
597            NodeMatcher::new(SyntaxKind::SetOperator, |_| {
598                one_of(vec![
599                    Sequence::new(vec![
600                        Ref::keyword("UNION").to_matchable(),
601                        one_of(vec![
602                            Ref::keyword("DISTINCT").to_matchable(),
603                            Ref::keyword("ALL").to_matchable(),
604                        ])
605                        .to_matchable(),
606                    ])
607                    .to_matchable(),
608                    Sequence::new(vec![
609                        Ref::keyword("INTERSECT").to_matchable(),
610                        Ref::keyword("DISTINCT").to_matchable(),
611                    ])
612                    .to_matchable(),
613                    Sequence::new(vec![
614                        Ref::keyword("EXCEPT").to_matchable(),
615                        Ref::keyword("DISTINCT").to_matchable(),
616                    ])
617                    .to_matchable(),
618                ])
619                .to_matchable()
620            })
621            .to_matchable()
622            .into(),
623        ),
624    ]);
625
626    dialect.replace_grammar("SetExpressionSegment", {
627        Sequence::new(vec![
628            one_of(vec![
629                Ref::new("NonSetSelectableGrammar").to_matchable(),
630                Bracketed::new(vec![Ref::new("SetExpressionSegment").to_matchable()])
631                    .to_matchable(),
632            ])
633            .to_matchable(),
634            AnyNumberOf::new(vec![
635                Sequence::new(vec![
636                    Ref::new("SetOperatorSegment").to_matchable(),
637                    one_of(vec![
638                        Ref::new("NonSetSelectableGrammar").to_matchable(),
639                        Bracketed::new(vec![Ref::new("SetExpressionSegment").to_matchable()])
640                            .to_matchable(),
641                    ])
642                    .to_matchable(),
643                ])
644                .to_matchable(),
645            ])
646            .config(|this| this.min_times = 1)
647            .to_matchable(),
648            Ref::new("OrderByClauseSegment").optional().to_matchable(),
649            Ref::new("LimitClauseSegment").optional().to_matchable(),
650            Ref::new("NamedWindowSegment").optional().to_matchable(),
651        ])
652        .to_matchable()
653    });
654
655    dialect.replace_grammar("SelectStatementSegment", {
656        ansi::select_statement().copy(
657            Some(vec![
658                Ref::new("QualifyClauseSegment").optional().to_matchable(),
659            ]),
660            None,
661            Some(Ref::new("OrderByClauseSegment").optional().to_matchable()),
662            None,
663            Vec::new(),
664            false,
665        )
666    });
667
668    dialect.replace_grammar(
669        "UnorderedSelectStatementSegment",
670        ansi::get_unordered_select_statement_segment_grammar().copy(
671            Some(vec![
672                Ref::new("QualifyClauseSegment").optional().to_matchable(),
673            ]),
674            None,
675            Some(Ref::new("OverlapsClauseSegment").optional().to_matchable()),
676            None,
677            Vec::new(),
678            false,
679        ),
680    );
681
682    dialect.add([(
683        "MultiStatementSegment".into(),
684        NodeMatcher::new(SyntaxKind::MultiStatementSegment, |_| {
685            one_of(vec![
686                Ref::new("ForInStatementSegment").to_matchable(),
687                Ref::new("RepeatStatementSegment").to_matchable(),
688                Ref::new("WhileStatementSegment").to_matchable(),
689                Ref::new("LoopStatementSegment").to_matchable(),
690                Ref::new("IfStatementSegment").to_matchable(),
691                Ref::new("CreateProcedureStatementSegment").to_matchable(),
692            ])
693            .to_matchable()
694        })
695        .to_matchable()
696        .into(),
697    )]);
698
699    dialect.replace_grammar(
700        "FileSegment",
701        Sequence::new(vec![
702            Sequence::new(vec![
703                one_of(vec![
704                    Ref::new("MultiStatementSegment").to_matchable(),
705                    Ref::new("StatementSegment").to_matchable(),
706                ])
707                .to_matchable(),
708            ])
709            .to_matchable(),
710            AnyNumberOf::new(vec![
711                Ref::new("DelimiterGrammar").to_matchable(),
712                one_of(vec![
713                    Ref::new("MultiStatementSegment").to_matchable(),
714                    Ref::new("StatementSegment").to_matchable(),
715                ])
716                .to_matchable(),
717            ])
718            .to_matchable(),
719            Ref::new("DelimiterGrammar").optional().to_matchable(),
720        ])
721        .to_matchable(),
722    );
723
724    dialect.replace_grammar(
725        "StatementSegment",
726        ansi::statement_segment().copy(
727            Some(vec![
728                Ref::new("DeclareStatementSegment").to_matchable(),
729                Ref::new("SetStatementSegment").to_matchable(),
730                Ref::new("ExportStatementSegment").to_matchable(),
731                Ref::new("CreateExternalTableStatementSegment").to_matchable(),
732                Ref::new("AssertStatementSegment").to_matchable(),
733                Ref::new("CallStatementSegment").to_matchable(),
734                Ref::new("ReturnStatementSegment").to_matchable(),
735                Ref::new("BreakStatementSegment").to_matchable(),
736                Ref::new("LeaveStatementSegment").to_matchable(),
737                Ref::new("ContinueStatementSegment").to_matchable(),
738                Ref::new("RaiseStatementSegment").to_matchable(),
739                Ref::new("AlterViewStatementSegment").to_matchable(),
740                Ref::new("CreateMaterializedViewStatementSegment").to_matchable(),
741                Ref::new("AlterMaterializedViewStatementSegment").to_matchable(),
742                Ref::new("DropMaterializedViewStatementSegment").to_matchable(),
743            ]),
744            None,
745            None,
746            None,
747            Vec::new(),
748            false,
749        ),
750    );
751
752    dialect.add([(
753        "AssertStatementSegment".into(),
754        NodeMatcher::new(SyntaxKind::AssertStatement, |_| {
755            Sequence::new(vec![
756                Ref::keyword("ASSERT").to_matchable(),
757                Ref::new("ExpressionSegment").to_matchable(),
758                Sequence::new(vec![
759                    Ref::keyword("AS").to_matchable(),
760                    Ref::new("QuotedLiteralSegment").to_matchable(),
761                ])
762                .config(|this| this.optional())
763                .to_matchable(),
764            ])
765            .to_matchable()
766        })
767        .to_matchable()
768        .into(),
769    )]);
770
771    dialect.add([(
772        "ForInStatementsSegment".into(),
773        NodeMatcher::new(SyntaxKind::ForInStatements, |_| {
774            AnyNumberOf::new(vec![
775                Sequence::new(vec![
776                    one_of(vec![
777                        Ref::new("StatementSegment").to_matchable(),
778                        Ref::new("MultiStatementSegment").to_matchable(),
779                    ])
780                    .to_matchable(),
781                    Ref::new("DelimiterGrammar").to_matchable(),
782                ])
783                .to_matchable(),
784            ])
785            .config(|this| {
786                this.terminators = vec![
787                    Sequence::new(vec![
788                        Ref::keyword("END").to_matchable(),
789                        Ref::keyword("FOR").to_matchable(),
790                    ])
791                    .to_matchable(),
792                ];
793                this.parse_mode = ParseMode::Greedy;
794            })
795            .to_matchable()
796        })
797        .to_matchable()
798        .into(),
799    )]);
800
801    dialect.add([(
802        "ForInStatementSegment".into(),
803        NodeMatcher::new(SyntaxKind::ForInStatement, |_| {
804            Sequence::new(vec![
805                Ref::keyword("FOR").to_matchable(),
806                Ref::new("SingleIdentifierGrammar").to_matchable(),
807                Ref::keyword("IN").to_matchable(),
808                MetaSegment::indent().to_matchable(),
809                Ref::new("SelectableGrammar").to_matchable(),
810                MetaSegment::dedent().to_matchable(),
811                Ref::keyword("DO").to_matchable(),
812                MetaSegment::indent().to_matchable(),
813                Ref::new("ForInStatementsSegment").to_matchable(),
814                MetaSegment::dedent().to_matchable(),
815                Ref::keyword("END").to_matchable(),
816                Ref::keyword("FOR").to_matchable(),
817            ])
818            .to_matchable()
819        })
820        .to_matchable()
821        .into(),
822    )]);
823
824    dialect.add([
825        (
826            "RepeatStatementsSegment".into(),
827            NodeMatcher::new(SyntaxKind::RepeatStatements, |_| {
828                AnyNumberOf::new(vec![
829                    Sequence::new(vec![
830                        one_of(vec![
831                            Ref::new("StatementSegment").to_matchable(),
832                            Ref::new("MultiStatementSegment").to_matchable(),
833                        ])
834                        .to_matchable(),
835                        Ref::new("DelimiterGrammar").to_matchable(),
836                    ])
837                    .to_matchable(),
838                ])
839                .config(|this| {
840                    this.terminators = vec![Ref::keyword("UNTIL").to_matchable()];
841                    this.parse_mode = ParseMode::Greedy;
842                })
843                .to_matchable()
844            })
845            .to_matchable()
846            .into(),
847        ),
848        (
849            "RepeatStatementSegment".into(),
850            NodeMatcher::new(SyntaxKind::RepeatStatement, |_| {
851                Sequence::new(vec![
852                    Ref::keyword("REPEAT").to_matchable(),
853                    MetaSegment::indent().to_matchable(),
854                    Ref::new("RepeatStatementsSegment").to_matchable(),
855                    Ref::keyword("UNTIL").to_matchable(),
856                    Ref::new("ExpressionSegment").to_matchable(),
857                    MetaSegment::dedent().to_matchable(),
858                    Ref::keyword("END").to_matchable(),
859                    Ref::keyword("REPEAT").to_matchable(),
860                ])
861                .to_matchable()
862            })
863            .to_matchable()
864            .into(),
865        ),
866        (
867            "IfStatementsSegment".into(),
868            NodeMatcher::new(SyntaxKind::IfStatements, |_| {
869                AnyNumberOf::new(vec![
870                    Sequence::new(vec![
871                        one_of(vec![
872                            Ref::new("StatementSegment").to_matchable(),
873                            Ref::new("MultiStatementSegment").to_matchable(),
874                        ])
875                        .to_matchable(),
876                        Ref::new("DelimiterGrammar").to_matchable(),
877                    ])
878                    .to_matchable(),
879                ])
880                .config(|this| {
881                    this.terminators = vec![
882                        Ref::keyword("ELSE").to_matchable(),
883                        Ref::keyword("ELSEIF").to_matchable(),
884                        Sequence::new(vec![
885                            Ref::keyword("END").to_matchable(),
886                            Ref::keyword("IF").to_matchable(),
887                        ])
888                        .to_matchable(),
889                    ];
890                    this.parse_mode = ParseMode::Greedy;
891                })
892                .to_matchable()
893            })
894            .to_matchable()
895            .into(),
896        ),
897        (
898            "IfStatementSegment".into(),
899            NodeMatcher::new(SyntaxKind::IfStatement, |_| {
900                Sequence::new(vec![
901                    Ref::keyword("IF").to_matchable(),
902                    Ref::new("ExpressionSegment").to_matchable(),
903                    Ref::keyword("THEN").to_matchable(),
904                    MetaSegment::indent().to_matchable(),
905                    Ref::new("IfStatementsSegment").to_matchable(),
906                    MetaSegment::dedent().to_matchable(),
907                    AnyNumberOf::new(vec![
908                        Sequence::new(vec![
909                            Ref::keyword("ELSEIF").to_matchable(),
910                            Ref::new("ExpressionSegment").to_matchable(),
911                            Ref::keyword("THEN").to_matchable(),
912                            MetaSegment::indent().to_matchable(),
913                            Ref::new("IfStatementsSegment").to_matchable(),
914                            MetaSegment::dedent().to_matchable(),
915                        ])
916                        .to_matchable(),
917                    ])
918                    .to_matchable(),
919                    Sequence::new(vec![
920                        Ref::keyword("ELSE").to_matchable(),
921                        MetaSegment::indent().to_matchable(),
922                        Ref::new("IfStatementsSegment").to_matchable(),
923                        MetaSegment::dedent().to_matchable(),
924                    ])
925                    .config(|this| this.optional())
926                    .to_matchable(),
927                    Ref::keyword("END").to_matchable(),
928                    Ref::keyword("IF").to_matchable(),
929                ])
930                .to_matchable()
931            })
932            .to_matchable()
933            .into(),
934        ),
935        (
936            "LoopStatementsSegment".into(),
937            NodeMatcher::new(SyntaxKind::LoopStatements, |_| {
938                AnyNumberOf::new(vec![
939                    Sequence::new(vec![
940                        one_of(vec![
941                            Ref::new("StatementSegment").to_matchable(),
942                            Ref::new("MultiStatementSegment").to_matchable(),
943                        ])
944                        .to_matchable(),
945                        Ref::new("DelimiterGrammar").to_matchable(),
946                    ])
947                    .to_matchable(),
948                ])
949                .config(|this| {
950                    this.terminators = vec![
951                        Sequence::new(vec![
952                            Ref::keyword("END").to_matchable(),
953                            Ref::keyword("LOOP").to_matchable(),
954                        ])
955                        .to_matchable(),
956                    ];
957                    this.parse_mode = ParseMode::Greedy;
958                })
959                .to_matchable()
960            })
961            .to_matchable()
962            .into(),
963        ),
964        (
965            "LoopStatementSegment".into(),
966            NodeMatcher::new(SyntaxKind::LoopStatement, |_| {
967                Sequence::new(vec![
968                    Ref::keyword("LOOP").to_matchable(),
969                    MetaSegment::indent().to_matchable(),
970                    Ref::new("LoopStatementsSegment").to_matchable(),
971                    MetaSegment::dedent().to_matchable(),
972                    Ref::keyword("END").to_matchable(),
973                    Ref::keyword("LOOP").to_matchable(),
974                ])
975                .to_matchable()
976            })
977            .to_matchable()
978            .into(),
979        ),
980        (
981            "WhileStatementsSegment".into(),
982            NodeMatcher::new(SyntaxKind::WhileStatements, |_| {
983                AnyNumberOf::new(vec![
984                    Sequence::new(vec![
985                        Ref::new("StatementSegment").to_matchable(),
986                        Ref::new("DelimiterGrammar").to_matchable(),
987                    ])
988                    .to_matchable(),
989                ])
990                .config(|this| {
991                    this.terminators = vec![
992                        Sequence::new(vec![
993                            Ref::keyword("END").to_matchable(),
994                            Ref::keyword("WHILE").to_matchable(),
995                        ])
996                        .to_matchable(),
997                    ];
998                    this.parse_mode = ParseMode::Greedy;
999                })
1000                .to_matchable()
1001            })
1002            .to_matchable()
1003            .into(),
1004        ),
1005        (
1006            "WhileStatementSegment".into(),
1007            NodeMatcher::new(SyntaxKind::WhileStatement, |_| {
1008                Sequence::new(vec![
1009                    Ref::keyword("WHILE").to_matchable(),
1010                    Ref::new("ExpressionSegment").to_matchable(),
1011                    Ref::keyword("DO").to_matchable(),
1012                    MetaSegment::indent().to_matchable(),
1013                    Ref::new("WhileStatementsSegment").to_matchable(),
1014                    MetaSegment::dedent().to_matchable(),
1015                    Ref::keyword("END").to_matchable(),
1016                    Ref::keyword("WHILE").to_matchable(),
1017                ])
1018                .to_matchable()
1019            })
1020            .to_matchable()
1021            .into(),
1022        ),
1023    ]);
1024
1025    dialect.replace_grammar(
1026        "SelectClauseModifierSegment",
1027        Sequence::new(vec![
1028            one_of(vec![
1029                Ref::keyword("DISTINCT").to_matchable(),
1030                Ref::keyword("ALL").to_matchable(),
1031            ])
1032            .config(|this| this.optional())
1033            .to_matchable(),
1034            Sequence::new(vec![
1035                Ref::keyword("AS").to_matchable(),
1036                one_of(vec![
1037                    Ref::keyword("STRUCT").to_matchable(),
1038                    Ref::keyword("VALUE").to_matchable(),
1039                ])
1040                .to_matchable(),
1041            ])
1042            .config(|this| this.optional())
1043            .to_matchable(),
1044        ])
1045        .to_matchable(),
1046    );
1047
1048    dialect.replace_grammar(
1049        "IntervalExpressionSegment",
1050        Sequence::new(vec![
1051            Ref::keyword("INTERVAL").to_matchable(),
1052            Ref::new("ExpressionSegment").to_matchable(),
1053            one_of(vec![
1054                Ref::new("QuotedLiteralSegment").to_matchable(),
1055                Ref::new("DatetimeUnitSegment").to_matchable(),
1056                Sequence::new(vec![
1057                    Ref::new("DatetimeUnitSegment").to_matchable(),
1058                    Ref::keyword("TO").to_matchable(),
1059                    Ref::new("DatetimeUnitSegment").to_matchable(),
1060                ])
1061                .to_matchable(),
1062            ])
1063            .to_matchable(),
1064        ])
1065        .to_matchable(),
1066    );
1067
1068    dialect.add([
1069        (
1070            "DateTimeFunctionContentsSegment".into(),
1071            NodeMatcher::new(SyntaxKind::FunctionContents, |_| {
1072                Bracketed::new(vec![
1073                    Delimited::new(vec![
1074                        Ref::new("DatetimeUnitSegment").to_matchable(),
1075                        Ref::new("DatePartWeekSegment").to_matchable(),
1076                        Ref::new("FunctionContentsGrammar").to_matchable(),
1077                    ])
1078                    .to_matchable(),
1079                ])
1080                .to_matchable()
1081            })
1082            .to_matchable()
1083            .into(),
1084        ),
1085        (
1086            "ExtractFunctionContentsSegment".into(),
1087            NodeMatcher::new(SyntaxKind::FunctionContents, |_| {
1088                Bracketed::new(vec![
1089                    one_of(vec![
1090                        Ref::new("DatetimeUnitSegment").to_matchable(),
1091                        Ref::new("DatePartWeekSegment").to_matchable(),
1092                        Ref::new("ExtendedDatetimeUnitSegment").to_matchable(),
1093                    ])
1094                    .to_matchable(),
1095                    Ref::keyword("FROM").to_matchable(),
1096                    Ref::new("ExpressionSegment").to_matchable(),
1097                ])
1098                .to_matchable()
1099            })
1100            .to_matchable()
1101            .into(),
1102        ),
1103        (
1104            "NormalizeFunctionContentsSegment".into(),
1105            NodeMatcher::new(SyntaxKind::FunctionContents, |_| {
1106                Bracketed::new(vec![
1107                    Ref::new("ExpressionSegment").to_matchable(),
1108                    Sequence::new(vec![
1109                        Ref::new("CommaSegment").to_matchable(),
1110                        one_of(vec![
1111                            Ref::keyword("NFC").to_matchable(),
1112                            Ref::keyword("NFKC").to_matchable(),
1113                            Ref::keyword("NFD").to_matchable(),
1114                            Ref::keyword("NFKD").to_matchable(),
1115                        ])
1116                        .to_matchable(),
1117                    ])
1118                    .config(|this| this.optional())
1119                    .to_matchable(),
1120                ])
1121                .to_matchable()
1122            })
1123            .to_matchable()
1124            .into(),
1125        ),
1126        (
1127            "ExtractFunctionNameSegment".into(),
1128            NodeMatcher::new(SyntaxKind::FunctionName, |_| {
1129                StringParser::new("EXTRACT", SyntaxKind::FunctionNameIdentifier).to_matchable()
1130            })
1131            .to_matchable()
1132            .into(),
1133        ),
1134        (
1135            "ArrayFunctionNameSegment".into(),
1136            NodeMatcher::new(SyntaxKind::FunctionName, |_| {
1137                StringParser::new("ARRAY", SyntaxKind::FunctionNameIdentifier).to_matchable()
1138            })
1139            .to_matchable()
1140            .into(),
1141        ),
1142        (
1143            "DatePartWeekSegment".into(),
1144            NodeMatcher::new(SyntaxKind::DatePartWeek, |_| {
1145                Sequence::new(vec![
1146                    Ref::keyword("WEEK").to_matchable(),
1147                    Bracketed::new(vec![
1148                        one_of(vec![
1149                            Ref::keyword("SUNDAY").to_matchable(),
1150                            Ref::keyword("MONDAY").to_matchable(),
1151                            Ref::keyword("TUESDAY").to_matchable(),
1152                            Ref::keyword("WEDNESDAY").to_matchable(),
1153                            Ref::keyword("THURSDAY").to_matchable(),
1154                            Ref::keyword("FRIDAY").to_matchable(),
1155                            Ref::keyword("SATURDAY").to_matchable(),
1156                        ])
1157                        .to_matchable(),
1158                    ])
1159                    .to_matchable(),
1160                ])
1161                .to_matchable()
1162            })
1163            .to_matchable()
1164            .into(),
1165        ),
1166        (
1167            "NormalizeFunctionNameSegment".into(),
1168            NodeMatcher::new(SyntaxKind::FunctionName, |_| {
1169                one_of(vec![
1170                    StringParser::new("NORMALIZE", SyntaxKind::FunctionNameIdentifier)
1171                        .to_matchable(),
1172                    StringParser::new("NORMALIZE_AND_CASEFOLD", SyntaxKind::FunctionNameIdentifier)
1173                        .to_matchable(),
1174                ])
1175                .to_matchable()
1176            })
1177            .to_matchable()
1178            .into(),
1179        ),
1180    ]);
1181
1182    dialect.replace_grammar(
1183        "FunctionNameSegment",
1184        Sequence::new(vec![
1185            // AnyNumberOf to handle project names, schemas, or the SAFE keyword
1186            AnyNumberOf::new(vec![
1187                Sequence::new(vec![
1188                    one_of(vec![
1189                        Ref::keyword("SAFE").to_matchable(),
1190                        Ref::new("SingleIdentifierGrammar").to_matchable(),
1191                    ])
1192                    .to_matchable(),
1193                    Ref::new("DotSegment").to_matchable(),
1194                ])
1195                .terminators(vec![Ref::new("BracketedSegment").to_matchable()])
1196                .to_matchable(),
1197            ])
1198            .to_matchable(),
1199            // Base function name
1200            one_of(vec![
1201                Ref::new("FunctionNameIdentifierSegment").to_matchable(),
1202                Ref::new("QuotedIdentifierSegment").to_matchable(),
1203            ])
1204            .config(|this| this.terminators = vec![Ref::new("BracketedSegment").to_matchable()])
1205            .to_matchable(),
1206        ])
1207        .allow_gaps(true)
1208        .to_matchable(),
1209    );
1210
1211    dialect.replace_grammar(
1212        "FunctionSegment",
1213        Sequence::new(vec![
1214            one_of(vec![
1215                Sequence::new(vec![
1216                    // BigQuery EXTRACT allows optional TimeZone
1217                    Ref::new("ExtractFunctionNameSegment").to_matchable(),
1218                    Ref::new("ExtractFunctionContentsSegment").to_matchable(),
1219                ])
1220                .to_matchable(),
1221                Sequence::new(vec![
1222                    // BigQuery NORMALIZE allows optional normalization_mode
1223                    Ref::new("NormalizeFunctionNameSegment").to_matchable(),
1224                    Ref::new("NormalizeFunctionContentsSegment").to_matchable(),
1225                ])
1226                .to_matchable(),
1227                Sequence::new(vec![
1228                    // Treat functions which take date parts separately
1229                    Ref::new("DatePartFunctionNameSegment")
1230                        .exclude(Ref::new("ExtractFunctionNameSegment"))
1231                        .to_matchable(),
1232                    Ref::new("DateTimeFunctionContentsSegment").to_matchable(),
1233                ])
1234                .to_matchable(),
1235                Sequence::new(vec![
1236                    Sequence::new(vec![
1237                        Ref::new("FunctionNameSegment")
1238                            .exclude(one_of(vec![
1239                                Ref::new("DatePartFunctionNameSegment").to_matchable(),
1240                                Ref::new("NormalizeFunctionNameSegment").to_matchable(),
1241                                Ref::new("ValuesClauseSegment").to_matchable(),
1242                            ]))
1243                            .to_matchable(),
1244                        Ref::new("FunctionContentsSegment").to_matchable(),
1245                    ])
1246                    .to_matchable(),
1247                    Ref::new("ArrayAccessorSegment").optional().to_matchable(),
1248                    Ref::new("SemiStructuredAccessorSegment")
1249                        .optional()
1250                        .to_matchable(),
1251                    Ref::new("PostFunctionGrammar").optional().to_matchable(),
1252                ])
1253                .to_matchable(),
1254            ])
1255            .to_matchable(),
1256        ])
1257        .config(|this| this.allow_gaps = false)
1258        .to_matchable(),
1259    );
1260
1261    dialect.replace_grammar(
1262        "FunctionDefinitionGrammar",
1263        Sequence::new(vec![
1264            AnyNumberOf::new(vec![
1265                Sequence::new(vec![
1266                    one_of(vec![
1267                        Ref::keyword("DETERMINISTIC").to_matchable(),
1268                        Sequence::new(vec![
1269                            Ref::keyword("NOT").to_matchable(),
1270                            Ref::keyword("DETERMINISTIC").to_matchable(),
1271                        ])
1272                        .to_matchable(),
1273                    ])
1274                    .to_matchable(),
1275                ])
1276                .config(|this| this.optional())
1277                .to_matchable(),
1278                Sequence::new(vec![
1279                    Ref::keyword("LANGUAGE").to_matchable(),
1280                    Ref::new("NakedIdentifierSegment").to_matchable(),
1281                    Sequence::new(vec![
1282                        Ref::keyword("OPTIONS").to_matchable(),
1283                        Bracketed::new(vec![
1284                            Delimited::new(vec![
1285                                Sequence::new(vec![
1286                                    Ref::new("ParameterNameSegment").to_matchable(),
1287                                    Ref::new("EqualsSegment").to_matchable(),
1288                                    Anything::new().to_matchable(),
1289                                ])
1290                                .to_matchable(),
1291                                Ref::new("CommaSegment").to_matchable(),
1292                            ])
1293                            .to_matchable(),
1294                        ])
1295                        .to_matchable(),
1296                    ])
1297                    .config(|this| this.optional())
1298                    .to_matchable(),
1299                ])
1300                .to_matchable(),
1301                Sequence::new(vec![
1302                    Ref::keyword("AS").to_matchable(),
1303                    one_of(vec![
1304                        Ref::new("DoubleQuotedUDFBody").to_matchable(),
1305                        Ref::new("SingleQuotedUDFBody").to_matchable(),
1306                        Bracketed::new(vec![
1307                            one_of(vec![
1308                                Ref::new("ExpressionSegment").to_matchable(),
1309                                Ref::new("SelectStatementSegment").to_matchable(),
1310                            ])
1311                            .to_matchable(),
1312                        ])
1313                        .to_matchable(),
1314                    ])
1315                    .to_matchable(),
1316                ])
1317                .to_matchable(),
1318            ])
1319            .to_matchable(),
1320        ])
1321        .to_matchable(),
1322    );
1323
1324    dialect.replace_grammar(
1325        "WildcardExpressionSegment",
1326        ansi::wildcard_expression_segment().copy(
1327            Some(vec![
1328                Ref::new("ExceptClauseSegment").optional().to_matchable(),
1329                Ref::new("ReplaceClauseSegment").optional().to_matchable(),
1330            ]),
1331            None,
1332            None,
1333            None,
1334            Vec::new(),
1335            false,
1336        ),
1337    );
1338
1339    dialect.add([
1340        (
1341            "ExceptClauseSegment".into(),
1342            NodeMatcher::new(SyntaxKind::SelectExceptClause, |_| {
1343                Sequence::new(vec![
1344                    Ref::keyword("EXCEPT").to_matchable(),
1345                    Bracketed::new(vec![
1346                        Delimited::new(vec![Ref::new("SingleIdentifierGrammar").to_matchable()])
1347                            .to_matchable(),
1348                    ])
1349                    .to_matchable(),
1350                ])
1351                .to_matchable()
1352            })
1353            .to_matchable()
1354            .into(),
1355        ),
1356        (
1357            "ReplaceClauseSegment".into(),
1358            NodeMatcher::new(SyntaxKind::SelectReplaceClause, |_| {
1359                Sequence::new(vec![
1360                    Ref::keyword("REPLACE").to_matchable(),
1361                    Bracketed::new(vec![
1362                        Delimited::new(vec![Ref::new("SelectClauseElementSegment").to_matchable()])
1363                            .to_matchable(),
1364                    ])
1365                    .to_matchable(),
1366                ])
1367                .to_matchable()
1368            })
1369            .to_matchable()
1370            .into(),
1371        ),
1372    ]);
1373
1374    dialect.replace_grammar("DatatypeSegment", {
1375        one_of(vec![
1376            Sequence::new(vec![
1377                Ref::new("DatatypeIdentifierSegment").to_matchable(),
1378                Ref::new("BracketedArguments").optional().to_matchable(),
1379            ])
1380            .to_matchable(),
1381            Sequence::new(vec![
1382                Ref::keyword("ANY").to_matchable(),
1383                Ref::keyword("TYPE").to_matchable(),
1384            ])
1385            .to_matchable(),
1386            Ref::new("ArrayTypeSegment").to_matchable(),
1387            Ref::new("StructTypeSegment").to_matchable(),
1388        ])
1389        .to_matchable()
1390    });
1391
1392    dialect.replace_grammar(
1393        "StructTypeSegment",
1394        Sequence::new(vec![
1395            Ref::keyword("STRUCT").to_matchable(),
1396            Ref::new("StructTypeSchemaSegment")
1397                .optional()
1398                .to_matchable(),
1399        ])
1400        .to_matchable(),
1401    );
1402
1403    dialect.add([(
1404        "StructTypeSchemaSegment".into(),
1405        NodeMatcher::new(SyntaxKind::StructTypeSchema, |_| {
1406            Bracketed::new(vec![
1407                Delimited::new(vec![
1408                    Sequence::new(vec![
1409                        one_of(vec![
1410                            Ref::new("DatatypeSegment").to_matchable(),
1411                            Sequence::new(vec![
1412                                Ref::new("ParameterNameSegment").to_matchable(),
1413                                Ref::new("DatatypeSegment").to_matchable(),
1414                            ])
1415                            .to_matchable(),
1416                        ])
1417                        .to_matchable(),
1418                        AnyNumberOf::new(vec![Ref::new("ColumnConstraintSegment").to_matchable()])
1419                            .to_matchable(),
1420                        Ref::new("OptionsSegment").optional().to_matchable(),
1421                    ])
1422                    .to_matchable(),
1423                ])
1424                .to_matchable(),
1425            ])
1426            .config(|this| {
1427                this.bracket_type = "angle";
1428                this.bracket_pairs_set = "angle_bracket_pairs";
1429            })
1430            .to_matchable()
1431        })
1432        .to_matchable()
1433        .into(),
1434    )]);
1435
1436    dialect.add([(
1437        "ArrayFunctionContentsSegment".into(),
1438        NodeMatcher::new(SyntaxKind::FunctionContents, |_| {
1439            Bracketed::new(vec![Ref::new("SelectableGrammar").to_matchable()]).to_matchable()
1440        })
1441        .to_matchable()
1442        .into(),
1443    )]);
1444
1445    dialect.replace_grammar(
1446        "ArrayExpressionSegment",
1447        Sequence::new(vec![
1448            Ref::new("ArrayFunctionNameSegment").to_matchable(),
1449            Ref::new("ArrayFunctionContentsSegment").to_matchable(),
1450        ])
1451        .to_matchable(),
1452    );
1453
1454    dialect.add([
1455        (
1456            "TupleSegment".into(),
1457            NodeMatcher::new(SyntaxKind::Tuple, |_| {
1458                Bracketed::new(vec![
1459                    Delimited::new(vec![
1460                        Ref::new("BaseExpressionElementGrammar").to_matchable(),
1461                    ])
1462                    .to_matchable(),
1463                ])
1464                .to_matchable()
1465            })
1466            .to_matchable()
1467            .into(),
1468        ),
1469        (
1470            "NamedArgumentSegment".into(),
1471            NodeMatcher::new(SyntaxKind::NamedArgument, |_| {
1472                Sequence::new(vec![
1473                    Ref::new("NakedIdentifierSegment").to_matchable(),
1474                    Ref::new("RightArrowSegment").to_matchable(),
1475                    Ref::new("ExpressionSegment").to_matchable(),
1476                ])
1477                .to_matchable()
1478            })
1479            .to_matchable()
1480            .into(),
1481        ),
1482    ]);
1483
1484    dialect.add([(
1485        "SemiStructuredAccessorSegment".into(),
1486        NodeMatcher::new(SyntaxKind::SemiStructuredExpression, |_| {
1487            Sequence::new(vec![
1488                AnyNumberOf::new(vec![
1489                    Sequence::new(vec![
1490                        Ref::new("DotSegment").to_matchable(),
1491                        one_of(vec![
1492                            Ref::new("SingleIdentifierGrammar").to_matchable(),
1493                            Ref::new("StarSegment").to_matchable(),
1494                        ])
1495                        .to_matchable(),
1496                    ])
1497                    .config(|this| this.allow_gaps = true)
1498                    .to_matchable(),
1499                    Ref::new("ArrayAccessorSegment").optional().to_matchable(),
1500                ])
1501                .config(|this| {
1502                    this.allow_gaps = true;
1503                    this.min_times = 1;
1504                })
1505                .to_matchable(),
1506            ])
1507            .config(|this| this.allow_gaps = true)
1508            .to_matchable()
1509        })
1510        .to_matchable()
1511        .into(),
1512    )]);
1513
1514    dialect.replace_grammar(
1515        "ColumnReferenceSegment",
1516        Sequence::new(vec![
1517            Ref::new("SingleIdentifierGrammar").to_matchable(),
1518            Sequence::new(vec![
1519                Ref::new("ObjectReferenceDelimiterGrammar").to_matchable(),
1520                Delimited::new(vec![Ref::new("SingleIdentifierFullGrammar").to_matchable()])
1521                    .config(|this| {
1522                        this.delimiter(Ref::new("ObjectReferenceDelimiterGrammar"));
1523                        this.terminators = vec![
1524                            Ref::keyword("ON").to_matchable(),
1525                            Ref::keyword("AS").to_matchable(),
1526                            Ref::keyword("USING").to_matchable(),
1527                            Ref::new("CommaSegment").to_matchable(),
1528                            Ref::new("CastOperatorSegment").to_matchable(),
1529                            Ref::new("StartSquareBracketSegment").to_matchable(),
1530                            Ref::new("StartBracketSegment").to_matchable(),
1531                            Ref::new("BinaryOperatorGrammar").to_matchable(),
1532                            Ref::new("ColonSegment").to_matchable(),
1533                            Ref::new("DelimiterGrammar").to_matchable(),
1534                            Ref::new("BracketedSegment").to_matchable(),
1535                        ];
1536                        this.allow_gaps = false;
1537                    })
1538                    .to_matchable(),
1539            ])
1540            .allow_gaps(false)
1541            .config(|this| this.optional())
1542            .to_matchable(),
1543        ])
1544        .allow_gaps(false)
1545        .to_matchable(),
1546    );
1547
1548    dialect.replace_grammar("TableReferenceSegment", {
1549        Delimited::new(vec![
1550            Sequence::new(vec![
1551                Ref::new("SingleIdentifierGrammar").to_matchable(),
1552                AnyNumberOf::new(vec![
1553                    Sequence::new(vec![
1554                        Ref::new("DashSegment").to_matchable(),
1555                        Ref::new("NakedIdentifierPart").to_matchable(),
1556                    ])
1557                    .config(|this| this.allow_gaps = false)
1558                    .to_matchable(),
1559                ])
1560                .config(|this| this.optional())
1561                .to_matchable(),
1562            ])
1563            .config(|this| this.allow_gaps = false)
1564            .to_matchable(),
1565        ])
1566        .config(|this| {
1567            this.delimiter(Ref::new("ObjectReferenceDelimiterGrammar"));
1568            this.terminators = vec![
1569                Ref::keyword("ON").to_matchable(),
1570                Ref::keyword("AS").to_matchable(),
1571                Ref::keyword("USING").to_matchable(),
1572                Ref::new("CommaSegment").to_matchable(),
1573                Ref::new("CastOperatorSegment").to_matchable(),
1574                Ref::new("StartSquareBracketSegment").to_matchable(),
1575                Ref::new("StartBracketSegment").to_matchable(),
1576                Ref::new("ColonSegment").to_matchable(),
1577                Ref::new("DelimiterGrammar").to_matchable(),
1578                Ref::new("JoinLikeClauseGrammar").to_matchable(),
1579                Ref::new("BracketedSegment").to_matchable(),
1580            ];
1581            this.allow_gaps = false;
1582        })
1583        .to_matchable()
1584    });
1585
1586    dialect.add([
1587        (
1588            "DeclareStatementSegment".into(),
1589            NodeMatcher::new(SyntaxKind::DeclareSegment, |_| {
1590                Sequence::new(vec![
1591                    Ref::keyword("DECLARE").to_matchable(),
1592                    Delimited::new(vec![Ref::new("SingleIdentifierFullGrammar").to_matchable()])
1593                        .to_matchable(),
1594                    one_of(vec![
1595                        Ref::new("DefaultDeclareOptionsGrammar").to_matchable(),
1596                        Sequence::new(vec![
1597                            Ref::new("DatatypeSegment").to_matchable(),
1598                            Ref::new("DefaultDeclareOptionsGrammar")
1599                                .optional()
1600                                .to_matchable(),
1601                        ])
1602                        .to_matchable(),
1603                    ])
1604                    .to_matchable(),
1605                ])
1606                .to_matchable()
1607            })
1608            .to_matchable()
1609            .into(),
1610        ),
1611        (
1612            "SetStatementSegment".into(),
1613            NodeMatcher::new(SyntaxKind::SetSegment, |_| {
1614                Sequence::new(vec![
1615                    Ref::keyword("SET").to_matchable(),
1616                    one_of(vec![
1617                        Ref::new("NakedIdentifierSegment").to_matchable(),
1618                        Bracketed::new(vec![
1619                            Delimited::new(vec![Ref::new("NakedIdentifierSegment").to_matchable()])
1620                                .to_matchable(),
1621                        ])
1622                        .to_matchable(),
1623                    ])
1624                    .to_matchable(),
1625                    Ref::new("EqualsSegment").to_matchable(),
1626                    Delimited::new(vec![
1627                        one_of(vec![
1628                            Ref::new("LiteralGrammar").to_matchable(),
1629                            Bracketed::new(vec![Ref::new("SelectStatementSegment").to_matchable()])
1630                                .to_matchable(),
1631                            Ref::new("BareFunctionSegment").to_matchable(),
1632                            Ref::new("FunctionSegment").to_matchable(),
1633                            Bracketed::new(vec![
1634                                Delimited::new(vec![
1635                                    one_of(vec![
1636                                        Ref::new("LiteralGrammar").to_matchable(),
1637                                        Bracketed::new(vec![
1638                                            Ref::new("SelectStatementSegment").to_matchable(),
1639                                        ])
1640                                        .to_matchable(),
1641                                        Ref::new("BareFunctionSegment").to_matchable(),
1642                                        Ref::new("FunctionSegment").to_matchable(),
1643                                    ])
1644                                    .to_matchable(),
1645                                ])
1646                                .to_matchable(),
1647                            ])
1648                            .to_matchable(),
1649                            Ref::new("ArrayLiteralSegment").to_matchable(),
1650                            Ref::new("ExpressionSegment").to_matchable(),
1651                        ])
1652                        .to_matchable(),
1653                    ])
1654                    .to_matchable(),
1655                ])
1656                .to_matchable()
1657            })
1658            .to_matchable()
1659            .into(),
1660        ),
1661        (
1662            "PartitionBySegment".into(),
1663            NodeMatcher::new(SyntaxKind::PartitionBySegment, |_| {
1664                Sequence::new(vec![
1665                    Ref::keyword("PARTITION").to_matchable(),
1666                    Ref::keyword("BY").to_matchable(),
1667                    Ref::new("ExpressionSegment").to_matchable(),
1668                ])
1669                .to_matchable()
1670            })
1671            .to_matchable()
1672            .into(),
1673        ),
1674        (
1675            "ClusterBySegment".into(),
1676            NodeMatcher::new(SyntaxKind::ClusterBySegment, |_| {
1677                Sequence::new(vec![
1678                    Ref::keyword("CLUSTER").to_matchable(),
1679                    Ref::keyword("BY").to_matchable(),
1680                    Delimited::new(vec![Ref::new("ExpressionSegment").to_matchable()])
1681                        .to_matchable(),
1682                ])
1683                .to_matchable()
1684            })
1685            .to_matchable()
1686            .into(),
1687        ),
1688        (
1689            "OptionsSegment".into(),
1690            NodeMatcher::new(SyntaxKind::OptionsSegment, |_| {
1691                Sequence::new(vec![
1692                    Ref::keyword("OPTIONS").to_matchable(),
1693                    Bracketed::new(vec![
1694                        Delimited::new(vec![
1695                            Sequence::new(vec![
1696                                Ref::new("ParameterNameSegment").to_matchable(),
1697                                Ref::new("EqualsSegment").to_matchable(),
1698                                Ref::new("BaseExpressionElementGrammar").to_matchable(),
1699                            ])
1700                            .to_matchable(),
1701                        ])
1702                        .to_matchable(),
1703                    ])
1704                    .to_matchable(),
1705                ])
1706                .to_matchable()
1707            })
1708            .to_matchable()
1709            .into(),
1710        ),
1711    ]);
1712
1713    dialect.replace_grammar(
1714        "ColumnDefinitionSegment",
1715        Sequence::new(vec![
1716            Ref::new("SingleIdentifierGrammar").to_matchable(), // Column name
1717            Ref::new("DatatypeSegment").to_matchable(),         // Column type
1718            AnyNumberOf::new(vec![Ref::new("ColumnConstraintSegment").to_matchable()])
1719                .to_matchable(),
1720            Ref::new("OptionsSegment").optional().to_matchable(),
1721        ])
1722        .to_matchable(),
1723    );
1724
1725    dialect.replace_grammar(
1726        "CreateTableStatementSegment",
1727        Sequence::new(vec![
1728            Ref::keyword("CREATE").to_matchable(),
1729            Ref::new("OrReplaceGrammar").optional().to_matchable(),
1730            Ref::new("TemporaryTransientGrammar")
1731                .optional()
1732                .to_matchable(),
1733            Ref::keyword("TABLE").to_matchable(),
1734            Ref::new("IfNotExistsGrammar").optional().to_matchable(),
1735            Ref::new("TableReferenceSegment").to_matchable(),
1736            Sequence::new(vec![
1737                one_of(vec![
1738                    Ref::keyword("COPY").to_matchable(),
1739                    Ref::keyword("LIKE").to_matchable(),
1740                    Ref::keyword("CLONE").to_matchable(),
1741                ])
1742                .to_matchable(),
1743                Ref::new("TableReferenceSegment").to_matchable(),
1744            ])
1745            .config(|this| this.optional())
1746            .to_matchable(),
1747            Sequence::new(vec![
1748                Bracketed::new(vec![
1749                    Delimited::new(vec![Ref::new("ColumnDefinitionSegment").to_matchable()])
1750                        .config(|this| this.allow_trailing())
1751                        .to_matchable(),
1752                ])
1753                .to_matchable(),
1754            ])
1755            .config(|this| this.optional())
1756            .to_matchable(),
1757            Ref::new("PartitionBySegment").optional().to_matchable(),
1758            Ref::new("ClusterBySegment").optional().to_matchable(),
1759            Ref::new("OptionsSegment").optional().to_matchable(),
1760            Sequence::new(vec![
1761                Ref::keyword("AS").to_matchable(),
1762                optionally_bracketed(vec![Ref::new("SelectableGrammar").to_matchable()])
1763                    .to_matchable(),
1764            ])
1765            .config(|this| this.optional())
1766            .to_matchable(),
1767        ])
1768        .to_matchable(),
1769    );
1770
1771    dialect.replace_grammar(
1772        "AlterTableStatementSegment",
1773        Sequence::new(vec![
1774            Ref::keyword("ALTER").to_matchable(),
1775            Ref::keyword("TABLE").to_matchable(),
1776            Ref::new("IfExistsGrammar").optional().to_matchable(),
1777            Ref::new("TableReferenceSegment").to_matchable(),
1778            one_of(vec![
1779                // SET OPTIONS
1780                Sequence::new(vec![
1781                    Ref::keyword("SET").to_matchable(),
1782                    Ref::new("OptionsSegment").to_matchable(),
1783                ])
1784                .to_matchable(),
1785                // ADD COLUMN
1786                Delimited::new(vec![
1787                    Sequence::new(vec![
1788                        Ref::keyword("ADD").to_matchable(),
1789                        Ref::keyword("COLUMN").to_matchable(),
1790                        Ref::new("IfNotExistsGrammar").optional().to_matchable(),
1791                        Ref::new("ColumnDefinitionSegment").to_matchable(),
1792                    ])
1793                    .to_matchable(),
1794                ])
1795                .config(|this| this.allow_trailing = true)
1796                .to_matchable(),
1797                // RENAME TO
1798                Sequence::new(vec![
1799                    Ref::keyword("RENAME").to_matchable(),
1800                    Ref::keyword("TO").to_matchable(),
1801                    Ref::new("TableReferenceSegment").to_matchable(),
1802                ])
1803                .to_matchable(),
1804                // RENAME COLUMN
1805                Delimited::new(vec![
1806                    Sequence::new(vec![
1807                        Ref::keyword("RENAME").to_matchable(),
1808                        Ref::keyword("COLUMN").to_matchable(),
1809                        Ref::new("IfExistsGrammar").optional().to_matchable(),
1810                        Ref::new("SingleIdentifierGrammar").to_matchable(),
1811                        Ref::keyword("TO").to_matchable(),
1812                        Ref::new("SingleIdentifierGrammar").to_matchable(),
1813                    ])
1814                    .to_matchable(),
1815                ])
1816                .config(|this| this.allow_trailing = true)
1817                .to_matchable(),
1818                // DROP COLUMN
1819                Delimited::new(vec![
1820                    Sequence::new(vec![
1821                        Ref::keyword("DROP").to_matchable(),
1822                        Ref::keyword("COLUMN").to_matchable(),
1823                        Ref::new("IfExistsGrammar").optional().to_matchable(),
1824                        Ref::new("SingleIdentifierGrammar").to_matchable(),
1825                    ])
1826                    .to_matchable(),
1827                ])
1828                .to_matchable(),
1829                // ALTER COLUMN SET OPTIONS
1830                Delimited::new(vec![
1831                    Sequence::new(vec![
1832                        Ref::keyword("ALTER").to_matchable(),
1833                        Ref::keyword("COLUMN").to_matchable(),
1834                        Ref::new("IfExistsGrammar").optional().to_matchable(),
1835                        Ref::new("SingleIdentifierGrammar").to_matchable(),
1836                        one_of(vec![
1837                            Sequence::new(vec![
1838                                Ref::keyword("SET").to_matchable(),
1839                                one_of(vec![
1840                                    Ref::new("OptionsSegment").to_matchable(),
1841                                    Sequence::new(vec![
1842                                        Ref::keyword("DATA").to_matchable(),
1843                                        Ref::keyword("TYPE").to_matchable(),
1844                                        Ref::new("DatatypeSegment").to_matchable(),
1845                                    ])
1846                                    .to_matchable(),
1847                                    Sequence::new(vec![
1848                                        Ref::keyword("DEFAULT").to_matchable(),
1849                                        one_of(vec![
1850                                            Ref::new("LiteralGrammar").to_matchable(),
1851                                            Ref::new("FunctionSegment").to_matchable(),
1852                                        ])
1853                                        .to_matchable(),
1854                                    ])
1855                                    .to_matchable(),
1856                                ])
1857                                .to_matchable(),
1858                            ])
1859                            .to_matchable(),
1860                            Sequence::new(vec![
1861                                Ref::keyword("DROP").to_matchable(),
1862                                one_of(vec![
1863                                    Ref::keyword("DEFAULT").to_matchable(),
1864                                    Sequence::new(vec![
1865                                        Ref::keyword("NOT").to_matchable(),
1866                                        Ref::keyword("NULL").to_matchable(),
1867                                    ])
1868                                    .to_matchable(),
1869                                ])
1870                                .to_matchable(),
1871                            ])
1872                            .to_matchable(),
1873                        ])
1874                        .to_matchable(),
1875                    ])
1876                    .to_matchable(),
1877                ])
1878                .to_matchable(),
1879            ])
1880            .to_matchable(),
1881        ])
1882        .to_matchable(),
1883    );
1884
1885    dialect.add([(
1886        "CreateExternalTableStatementSegment".into(),
1887        NodeMatcher::new(SyntaxKind::CreateExternalTableStatement, |_| {
1888            Sequence::new(vec![
1889                Ref::keyword("CREATE").to_matchable(),
1890                Sequence::new(vec![
1891                    Ref::keyword("OR").optional().to_matchable(),
1892                    Ref::keyword("REPLACE").optional().to_matchable(),
1893                ])
1894                .config(|this| this.optional())
1895                .to_matchable(),
1896                Ref::keyword("EXTERNAL").to_matchable(),
1897                Ref::keyword("TABLE").to_matchable(),
1898                Sequence::new(vec![
1899                    Ref::keyword("IF").optional().to_matchable(),
1900                    Ref::keyword("NOT").optional().to_matchable(),
1901                    Ref::keyword("EXISTS").optional().to_matchable(),
1902                ])
1903                .config(|this| this.optional())
1904                .to_matchable(),
1905                Ref::new("TableReferenceSegment").to_matchable(),
1906                Bracketed::new(vec![
1907                    Delimited::new(vec![Ref::new("ColumnDefinitionSegment").to_matchable()])
1908                        .config(|this| this.allow_trailing = true)
1909                        .to_matchable(),
1910                ])
1911                .config(|this| this.optional())
1912                .to_matchable(),
1913                AnyNumberOf::new(vec![
1914                    Sequence::new(vec![
1915                        Ref::keyword("WITH").to_matchable(),
1916                        Ref::keyword("CONNECTION").to_matchable(),
1917                        Ref::new("TableReferenceSegment").to_matchable(),
1918                    ])
1919                    .config(|this| this.optional())
1920                    .to_matchable(),
1921                    Sequence::new(vec![
1922                        Ref::keyword("WITH").to_matchable(),
1923                        Ref::keyword("PARTITION").to_matchable(),
1924                        Ref::keyword("COLUMNS").to_matchable(),
1925                        Bracketed::new(vec![
1926                            Delimited::new(vec![
1927                                Ref::new("ColumnDefinitionSegment").to_matchable(),
1928                            ])
1929                            .config(|this| this.allow_trailing = true)
1930                            .to_matchable(),
1931                        ])
1932                        .config(|this| this.optional())
1933                        .to_matchable(),
1934                    ])
1935                    .config(|this| this.optional())
1936                    .to_matchable(),
1937                    Ref::new("OptionsSegment").optional().to_matchable(),
1938                ])
1939                .to_matchable(),
1940            ])
1941            .to_matchable()
1942        })
1943        .to_matchable()
1944        .into(),
1945    )]);
1946
1947    dialect.add([(
1948        "CreateExternalTableStatementSegment".into(),
1949        NodeMatcher::new(SyntaxKind::CreateExternalTableStatement, |_| {
1950            Sequence::new(vec![
1951                Ref::keyword("CREATE").to_matchable(),
1952                Sequence::new(vec![
1953                    Ref::keyword("OR").optional().to_matchable(),
1954                    Ref::keyword("REPLACE").optional().to_matchable(),
1955                ])
1956                .config(|this| this.optional())
1957                .to_matchable(),
1958                Ref::keyword("EXTERNAL").to_matchable(),
1959                Ref::keyword("TABLE").to_matchable(),
1960                Sequence::new(vec![
1961                    Ref::keyword("IF").optional().to_matchable(),
1962                    Ref::keyword("NOT").optional().to_matchable(),
1963                    Ref::keyword("EXISTS").optional().to_matchable(),
1964                ])
1965                .config(|this| this.optional())
1966                .to_matchable(),
1967                Ref::new("TableReferenceSegment").to_matchable(),
1968                Bracketed::new(vec![
1969                    Delimited::new(vec![Ref::new("ColumnDefinitionSegment").to_matchable()])
1970                        .config(|this| this.allow_trailing = true)
1971                        .to_matchable(),
1972                ])
1973                .config(|this| this.optional())
1974                .to_matchable(),
1975                AnyNumberOf::new(vec![
1976                    Sequence::new(vec![
1977                        Ref::keyword("WITH").to_matchable(),
1978                        Ref::keyword("CONNECTION").to_matchable(),
1979                        Ref::new("TableReferenceSegment").to_matchable(),
1980                    ])
1981                    .config(|this| this.optional())
1982                    .to_matchable(),
1983                    Sequence::new(vec![
1984                        Ref::keyword("WITH").to_matchable(),
1985                        Ref::keyword("PARTITION").to_matchable(),
1986                        Ref::keyword("COLUMNS").to_matchable(),
1987                        Bracketed::new(vec![
1988                            Delimited::new(vec![
1989                                Ref::new("ColumnDefinitionSegment").to_matchable(),
1990                            ])
1991                            .config(|this| this.allow_trailing = true)
1992                            .to_matchable(),
1993                        ])
1994                        .config(|this| this.optional())
1995                        .to_matchable(),
1996                    ])
1997                    .config(|this| this.optional())
1998                    .to_matchable(),
1999                    Ref::new("OptionsSegment").optional().to_matchable(),
2000                ])
2001                .to_matchable(),
2002            ])
2003            .to_matchable()
2004        })
2005        .to_matchable()
2006        .into(),
2007    )]);
2008
2009    dialect.replace_grammar(
2010        "CreateViewStatementSegment",
2011        Sequence::new(vec![
2012            Ref::keyword("CREATE").to_matchable(),
2013            Ref::new("OrReplaceGrammar").optional().to_matchable(),
2014            Ref::keyword("VIEW").to_matchable(),
2015            Ref::new("IfNotExistsGrammar").optional().to_matchable(),
2016            Ref::new("TableReferenceSegment").to_matchable(),
2017            Ref::new("BracketedColumnReferenceListGrammar")
2018                .optional()
2019                .to_matchable(),
2020            Ref::new("OptionsSegment").optional().to_matchable(),
2021            Ref::keyword("AS").to_matchable(),
2022            optionally_bracketed(vec![Ref::new("SelectableGrammar").to_matchable()]).to_matchable(),
2023        ])
2024        .to_matchable(),
2025    );
2026
2027    dialect.add([
2028        (
2029            "AlterViewStatementSegment".into(),
2030            NodeMatcher::new(SyntaxKind::AlterViewStatement, |_| {
2031                Sequence::new(vec![
2032                    Ref::keyword("ALTER").to_matchable(),
2033                    Ref::keyword("VIEW").to_matchable(),
2034                    Ref::new("IfExistsGrammar").optional().to_matchable(),
2035                    Ref::new("TableReferenceSegment").to_matchable(),
2036                    Ref::keyword("SET").to_matchable(),
2037                    Ref::new("OptionsSegment").to_matchable(),
2038                ])
2039                .to_matchable()
2040            })
2041            .to_matchable()
2042            .into(),
2043        ),
2044        (
2045            "CreateMaterializedViewStatementSegment".into(),
2046            NodeMatcher::new(SyntaxKind::CreateMaterializedViewStatement, |_| {
2047                Sequence::new(vec![
2048                    Ref::keyword("CREATE").to_matchable(),
2049                    Ref::new("OrReplaceGrammar").optional().to_matchable(),
2050                    Ref::keyword("MATERIALIZED").to_matchable(),
2051                    Ref::keyword("VIEW").to_matchable(),
2052                    Ref::new("IfNotExistsGrammar").optional().to_matchable(),
2053                    Ref::new("TableReferenceSegment").to_matchable(),
2054                    Ref::new("PartitionBySegment").optional().to_matchable(),
2055                    Ref::new("ClusterBySegment").optional().to_matchable(),
2056                    Ref::new("OptionsSegment").optional().to_matchable(),
2057                    Ref::keyword("AS").to_matchable(),
2058                    optionally_bracketed(vec![Ref::new("SelectableGrammar").to_matchable()])
2059                        .to_matchable(),
2060                ])
2061                .to_matchable()
2062            })
2063            .to_matchable()
2064            .into(),
2065        ),
2066        (
2067            "AlterMaterializedViewStatementSegment".into(),
2068            NodeMatcher::new(SyntaxKind::AlterMaterializedViewSetOptionsStatement, |_| {
2069                Sequence::new(vec![
2070                    Ref::keyword("ALTER").to_matchable(),
2071                    Ref::keyword("MATERIALIZED").to_matchable(),
2072                    Ref::keyword("VIEW").to_matchable(),
2073                    Ref::new("IfExistsGrammar").optional().to_matchable(),
2074                    Ref::new("TableReferenceSegment").to_matchable(),
2075                    Ref::keyword("SET").to_matchable(),
2076                    Ref::new("OptionsSegment").to_matchable(),
2077                ])
2078                .to_matchable()
2079            })
2080            .to_matchable()
2081            .into(),
2082        ),
2083        (
2084            "DropMaterializedViewStatementSegment".into(),
2085            NodeMatcher::new(SyntaxKind::DropMaterializedViewStatement, |_| {
2086                Sequence::new(vec![
2087                    Ref::keyword("DROP").to_matchable(),
2088                    Ref::keyword("MATERIALIZED").to_matchable(),
2089                    Ref::keyword("VIEW").to_matchable(),
2090                    Ref::new("IfExistsGrammar").optional().to_matchable(),
2091                    Ref::new("TableReferenceSegment").to_matchable(),
2092                ])
2093                .to_matchable()
2094            })
2095            .to_matchable()
2096            .into(),
2097        ),
2098        (
2099            "ParameterizedSegment".into(),
2100            NodeMatcher::new(SyntaxKind::ParameterizedExpression, |_| {
2101                one_of(vec![
2102                    Ref::new("AtSignLiteralSegment").to_matchable(),
2103                    Ref::new("QuestionMarkSegment").to_matchable(),
2104                ])
2105                .to_matchable()
2106            })
2107            .to_matchable()
2108            .into(),
2109        ),
2110        (
2111            "PivotForClauseSegment".into(),
2112            NodeMatcher::new(SyntaxKind::PivotForClause, |_| {
2113                Sequence::new(vec![
2114                    Ref::new("BaseExpressionElementGrammar").to_matchable(),
2115                ])
2116                .config(|this| {
2117                    this.terminators = vec![Ref::keyword("IN").to_matchable()];
2118                    this.parse_mode(ParseMode::Greedy);
2119                })
2120                .to_matchable()
2121            })
2122            .to_matchable()
2123            .into(),
2124        ),
2125        (
2126            "FromPivotExpressionSegment".into(),
2127            NodeMatcher::new(SyntaxKind::FromPivotExpression, |_| {
2128                Sequence::new(vec![
2129                    Ref::keyword("PIVOT").to_matchable(),
2130                    Bracketed::new(vec![
2131                        Delimited::new(vec![
2132                            Sequence::new(vec![
2133                                Ref::new("FunctionSegment").to_matchable(),
2134                                Ref::new("AliasExpressionSegment").optional().to_matchable(),
2135                            ])
2136                            .to_matchable(),
2137                        ])
2138                        .to_matchable(),
2139                        Ref::keyword("FOR").to_matchable(),
2140                        Ref::new("PivotForClauseSegment").to_matchable(),
2141                        Ref::keyword("IN").to_matchable(),
2142                        Bracketed::new(vec![
2143                            Delimited::new(vec![
2144                                Sequence::new(vec![
2145                                    Ref::new("LiteralGrammar").to_matchable(),
2146                                    Ref::new("AliasExpressionSegment").optional().to_matchable(),
2147                                ])
2148                                .to_matchable(),
2149                            ])
2150                            .to_matchable(),
2151                        ])
2152                        .to_matchable(),
2153                    ])
2154                    .to_matchable(),
2155                ])
2156                .to_matchable()
2157            })
2158            .to_matchable()
2159            .into(),
2160        ),
2161        (
2162            "UnpivotAliasExpressionSegment".into(),
2163            NodeMatcher::new(SyntaxKind::AliasExpression, |_| {
2164                Sequence::new(vec![
2165                    MetaSegment::indent().to_matchable(),
2166                    Ref::keyword("AS").optional().to_matchable(),
2167                    one_of(vec![
2168                        Ref::new("QuotedLiteralSegment").to_matchable(),
2169                        Ref::new("NumericLiteralSegment").to_matchable(),
2170                    ])
2171                    .to_matchable(),
2172                    MetaSegment::dedent().to_matchable(),
2173                ])
2174                .to_matchable()
2175            })
2176            .to_matchable()
2177            .into(),
2178        ),
2179    ]);
2180
2181    dialect.add([(
2182        "FromUnpivotExpressionSegment".into(),
2183        NodeMatcher::new(SyntaxKind::FromUnpivotExpression, |_| {
2184            Sequence::new(vec![
2185                Ref::keyword("UNPIVOT").to_matchable(),
2186                Sequence::new(vec![
2187                    one_of(vec![
2188                        Ref::keyword("INCLUDE").to_matchable(),
2189                        Ref::keyword("EXCLUDE").to_matchable(),
2190                    ])
2191                    .to_matchable(),
2192                    Ref::keyword("NULLS").to_matchable(),
2193                ])
2194                .config(|this| this.optional())
2195                .to_matchable(),
2196                one_of(vec![
2197                    // single column unpivot
2198                    Bracketed::new(vec![
2199                        Ref::new("SingleIdentifierGrammar").to_matchable(),
2200                        Ref::keyword("FOR").to_matchable(),
2201                        Ref::new("SingleIdentifierGrammar").to_matchable(),
2202                        Ref::keyword("IN").to_matchable(),
2203                        Bracketed::new(vec![
2204                            Delimited::new(vec![
2205                                Sequence::new(vec![
2206                                    Delimited::new(vec![
2207                                        Ref::new("SingleIdentifierGrammar").to_matchable(),
2208                                    ])
2209                                    .to_matchable(),
2210                                    Ref::new("UnpivotAliasExpressionSegment")
2211                                        .optional()
2212                                        .to_matchable(),
2213                                ])
2214                                .to_matchable(),
2215                            ])
2216                            .to_matchable(),
2217                        ])
2218                        .to_matchable(),
2219                    ])
2220                    .to_matchable(),
2221                    // multi column unpivot
2222                    Bracketed::new(vec![
2223                        Bracketed::new(vec![
2224                            Delimited::new(vec![
2225                                Ref::new("SingleIdentifierGrammar").to_matchable(),
2226                            ])
2227                            .config(|this| this.min_delimiters = 1)
2228                            .to_matchable(),
2229                        ])
2230                        .to_matchable(),
2231                        Ref::keyword("FOR").to_matchable(),
2232                        Ref::new("SingleIdentifierGrammar").to_matchable(),
2233                        Ref::keyword("IN").to_matchable(),
2234                        Bracketed::new(vec![
2235                            Delimited::new(vec![
2236                                Sequence::new(vec![
2237                                    Bracketed::new(vec![
2238                                        Delimited::new(vec![
2239                                            Ref::new("SingleIdentifierGrammar").to_matchable(),
2240                                        ])
2241                                        .config(|this| this.min_delimiters = 1)
2242                                        .to_matchable(),
2243                                    ])
2244                                    .to_matchable(),
2245                                    Ref::new("UnpivotAliasExpressionSegment")
2246                                        .optional()
2247                                        .to_matchable(),
2248                                ])
2249                                .to_matchable(),
2250                            ])
2251                            .to_matchable(),
2252                        ])
2253                        .to_matchable(),
2254                    ])
2255                    .to_matchable(),
2256                ])
2257                .to_matchable(),
2258            ])
2259            .to_matchable()
2260        })
2261        .to_matchable()
2262        .into(),
2263    )]);
2264
2265    dialect.add([
2266        (
2267            "InsertStatementSegment".into(),
2268            NodeMatcher::new(SyntaxKind::InsertStatement, |_| {
2269                Sequence::new(vec![
2270                    Ref::keyword("INSERT").to_matchable(),
2271                    Ref::keyword("INTO").optional().to_matchable(),
2272                    Ref::new("TableReferenceSegment").to_matchable(),
2273                    Ref::new("BracketedColumnReferenceListGrammar")
2274                        .optional()
2275                        .to_matchable(),
2276                    Ref::new("SelectableGrammar").to_matchable(),
2277                ])
2278                .to_matchable()
2279            })
2280            .to_matchable()
2281            .into(),
2282        ),
2283        (
2284            "SamplingExpressionSegment".into(),
2285            NodeMatcher::new(SyntaxKind::SampleExpression, |_| {
2286                Sequence::new(vec![
2287                    Ref::keyword("TABLESAMPLE").to_matchable(),
2288                    Ref::keyword("SYSTEM").to_matchable(),
2289                    Bracketed::new(vec![
2290                        Ref::new("NumericLiteralSegment").to_matchable(),
2291                        Ref::keyword("PERCENT").to_matchable(),
2292                    ])
2293                    .to_matchable(),
2294                ])
2295                .to_matchable()
2296            })
2297            .to_matchable()
2298            .into(),
2299        ),
2300        (
2301            "MergeMatchSegment".into(),
2302            NodeMatcher::new(SyntaxKind::MergeMatch, |_| {
2303                AnyNumberOf::new(vec![
2304                    Ref::new("MergeMatchedClauseSegment").to_matchable(),
2305                    Ref::new("MergeNotMatchedByTargetClauseSegment").to_matchable(),
2306                    Ref::new("MergeNotMatchedBySourceClauseSegment").to_matchable(),
2307                ])
2308                .config(|this| this.min_times = 1)
2309                .to_matchable()
2310            })
2311            .to_matchable()
2312            .into(),
2313        ),
2314        (
2315            "MergeNotMatchedByTargetClauseSegment".into(),
2316            NodeMatcher::new(SyntaxKind::NotMatchedByTargetClause, |_| {
2317                Sequence::new(vec![
2318                    Ref::keyword("WHEN").to_matchable(),
2319                    Ref::keyword("NOT").to_matchable(),
2320                    Ref::keyword("MATCHED").to_matchable(),
2321                    Sequence::new(vec![
2322                        Ref::keyword("BY").to_matchable(),
2323                        Ref::keyword("TARGET").to_matchable(),
2324                    ])
2325                    .config(|this| this.optional())
2326                    .to_matchable(),
2327                    Sequence::new(vec![
2328                        Ref::keyword("AND").to_matchable(),
2329                        Ref::new("ExpressionSegment").to_matchable(),
2330                    ])
2331                    .config(|this| this.optional())
2332                    .to_matchable(),
2333                    Ref::keyword("THEN").to_matchable(),
2334                    MetaSegment::indent().to_matchable(),
2335                    Ref::new("MergeInsertClauseSegment").to_matchable(),
2336                    MetaSegment::dedent().to_matchable(),
2337                ])
2338                .to_matchable()
2339            })
2340            .to_matchable()
2341            .into(),
2342        ),
2343        (
2344            "MergeNotMatchedBySourceClauseSegment".into(),
2345            NodeMatcher::new(SyntaxKind::MergeWhenMatchedClause, |_| {
2346                Sequence::new(vec![
2347                    Ref::keyword("WHEN").to_matchable(),
2348                    Ref::keyword("NOT").to_matchable(),
2349                    Ref::keyword("MATCHED").to_matchable(),
2350                    Ref::keyword("BY").to_matchable(),
2351                    Ref::keyword("SOURCE").to_matchable(),
2352                    Sequence::new(vec![
2353                        Ref::keyword("AND").to_matchable(),
2354                        Ref::new("ExpressionSegment").to_matchable(),
2355                    ])
2356                    .config(|this| this.optional())
2357                    .to_matchable(),
2358                    Ref::keyword("THEN").to_matchable(),
2359                    MetaSegment::indent().to_matchable(),
2360                    one_of(vec![
2361                        Ref::new("MergeUpdateClauseSegment").to_matchable(),
2362                        Ref::new("MergeDeleteClauseSegment").to_matchable(),
2363                    ])
2364                    .to_matchable(),
2365                    MetaSegment::dedent().to_matchable(),
2366                ])
2367                .to_matchable()
2368            })
2369            .to_matchable()
2370            .into(),
2371        ),
2372        (
2373            "MergeInsertClauseSegment".into(),
2374            NodeMatcher::new(SyntaxKind::MergeInsertClause, |_| {
2375                one_of(vec![
2376                    Sequence::new(vec![
2377                        Ref::keyword("INSERT").to_matchable(),
2378                        MetaSegment::indent().to_matchable(),
2379                        Ref::new("BracketedColumnReferenceListGrammar")
2380                            .optional()
2381                            .to_matchable(),
2382                        MetaSegment::dedent().to_matchable(),
2383                        Ref::new("ValuesClauseSegment").optional().to_matchable(),
2384                    ])
2385                    .to_matchable(),
2386                    Sequence::new(vec![
2387                        Ref::keyword("INSERT").to_matchable(),
2388                        Ref::keyword("ROW").to_matchable(),
2389                    ])
2390                    .to_matchable(),
2391                ])
2392                .to_matchable()
2393            })
2394            .to_matchable()
2395            .into(),
2396        ),
2397    ]);
2398
2399    dialect.add([
2400        (
2401            "DeleteStatementSegment".into(),
2402            NodeMatcher::new(SyntaxKind::DeleteStatement, |_| {
2403                Sequence::new(vec![
2404                    Ref::keyword("DELETE").to_matchable(),
2405                    Ref::keyword("FROM").optional().to_matchable(),
2406                    Ref::new("TableReferenceSegment").to_matchable(),
2407                    Ref::new("AliasExpressionSegment").optional().to_matchable(),
2408                    Ref::new("WhereClauseSegment").optional().to_matchable(),
2409                ])
2410                .to_matchable()
2411            })
2412            .to_matchable()
2413            .into(),
2414        ),
2415        (
2416            "ExportStatementSegment".into(),
2417            NodeMatcher::new(SyntaxKind::ExportStatement, |_| {
2418                Sequence::new(vec![
2419                    Ref::keyword("EXPORT").to_matchable(),
2420                    Ref::keyword("DATA").to_matchable(),
2421                    Sequence::new(vec![
2422                        Ref::keyword("WITH").to_matchable(),
2423                        Ref::keyword("CONNECTION").to_matchable(),
2424                        Ref::new("ObjectReferenceSegment").to_matchable(),
2425                    ])
2426                    .config(|this| this.optional())
2427                    .to_matchable(),
2428                    Ref::keyword("OPTIONS").to_matchable(),
2429                    Bracketed::new(vec![
2430                        Delimited::new(vec![
2431                            Sequence::new(vec![
2432                                one_of(vec![
2433                                    StringParser::new("compression", SyntaxKind::ExportOption)
2434                                        .to_matchable(),
2435                                    StringParser::new("field_delimiter", SyntaxKind::ExportOption)
2436                                        .to_matchable(),
2437                                    StringParser::new("format", SyntaxKind::ExportOption)
2438                                        .to_matchable(),
2439                                    StringParser::new("uri", SyntaxKind::ExportOption)
2440                                        .to_matchable(),
2441                                ])
2442                                .to_matchable(),
2443                                Ref::new("EqualsSegment").to_matchable(),
2444                                Ref::new("QuotedLiteralSegment").to_matchable(),
2445                            ])
2446                            .to_matchable(),
2447                            Sequence::new(vec![
2448                                one_of(vec![
2449                                    StringParser::new("header", SyntaxKind::ExportOption)
2450                                        .to_matchable(),
2451                                    StringParser::new("overwrite", SyntaxKind::ExportOption)
2452                                        .to_matchable(),
2453                                    StringParser::new(
2454                                        "use_avro_logical_types",
2455                                        SyntaxKind::ExportOption,
2456                                    )
2457                                    .to_matchable(),
2458                                ])
2459                                .to_matchable(),
2460                                Ref::new("EqualsSegment").to_matchable(),
2461                                one_of(vec![
2462                                    Ref::keyword("TRUE").to_matchable(),
2463                                    Ref::keyword("FALSE").to_matchable(),
2464                                ])
2465                                .to_matchable(),
2466                            ])
2467                            .to_matchable(),
2468                        ])
2469                        .to_matchable(),
2470                    ])
2471                    .to_matchable(),
2472                    Ref::keyword("AS").to_matchable(),
2473                    Ref::new("SelectableGrammar").to_matchable(),
2474                ])
2475                .to_matchable()
2476            })
2477            .to_matchable()
2478            .into(),
2479        ),
2480        (
2481            "ProcedureNameSegment".into(),
2482            NodeMatcher::new(SyntaxKind::ProcedureName, |_| {
2483                Sequence::new(vec![
2484                    AnyNumberOf::new(vec![
2485                        Sequence::new(vec![
2486                            Ref::new("SingleIdentifierGrammar").to_matchable(),
2487                            Ref::new("DotSegment").to_matchable(),
2488                        ])
2489                        .to_matchable(),
2490                    ])
2491                    .to_matchable(),
2492                    one_of(vec![
2493                        Ref::new("ProcedureNameIdentifierSegment").to_matchable(),
2494                        Ref::new("QuotedIdentifierSegment").to_matchable(),
2495                    ])
2496                    .to_matchable(),
2497                ])
2498                .config(|this| this.allow_gaps = false)
2499                .to_matchable()
2500            })
2501            .to_matchable()
2502            .into(),
2503        ),
2504    ]);
2505
2506    dialect.add([
2507        (
2508            "QuotedIdentifierSegment".into(),
2509            TypedParser::new(SyntaxKind::BackQuote, SyntaxKind::QuotedIdentifier)
2510                .to_matchable()
2511                .into(),
2512        ),
2513        (
2514            "NumericLiteralSegment".into(),
2515            one_of(vec![
2516                TypedParser::new(SyntaxKind::NumericLiteral, SyntaxKind::NumericLiteral)
2517                    .to_matchable(),
2518                Ref::new("ParameterizedSegment").to_matchable(),
2519            ])
2520            .to_matchable()
2521            .into(),
2522        ),
2523        (
2524            "QuotedLiteralSegment".into(),
2525            one_of(vec![
2526                Ref::new("SingleQuotedLiteralSegment").to_matchable(),
2527                Ref::new("DoubleQuotedLiteralSegment").to_matchable(),
2528            ])
2529            .to_matchable()
2530            .into(),
2531        ),
2532        (
2533            "LiteralGrammar".into(),
2534            dialect
2535                .grammar("LiteralGrammar")
2536                .copy(
2537                    Some(vec![Ref::new("ParameterizedSegment").to_matchable()]),
2538                    None,
2539                    None,
2540                    None,
2541                    Vec::new(),
2542                    false,
2543                )
2544                .into(),
2545        ),
2546        (
2547            "PostTableExpressionGrammar".into(),
2548            Sequence::new(vec![
2549                Sequence::new(vec![
2550                    Ref::keyword("FOR").to_matchable(),
2551                    one_of(vec![
2552                        Ref::keyword("SYSTEM_TIME").to_matchable(),
2553                        Sequence::new(vec![
2554                            Ref::keyword("SYSTEM").to_matchable(),
2555                            Ref::keyword("TIME").to_matchable(),
2556                        ])
2557                        .to_matchable(),
2558                    ])
2559                    .to_matchable(),
2560                    Ref::keyword("AS").to_matchable(),
2561                    Ref::keyword("OF").to_matchable(),
2562                    Ref::new("ExpressionSegment").to_matchable(),
2563                ])
2564                .config(|this| this.optional())
2565                .to_matchable(),
2566                Sequence::new(vec![
2567                    Ref::keyword("WITH").to_matchable(),
2568                    Ref::keyword("OFFSET").to_matchable(),
2569                    Sequence::new(vec![
2570                        Ref::keyword("AS").to_matchable(),
2571                        Ref::new("SingleIdentifierGrammar").to_matchable(),
2572                    ])
2573                    .config(|this| this.optional())
2574                    .to_matchable(),
2575                ])
2576                .config(|this| this.optional())
2577                .to_matchable(),
2578            ])
2579            .to_matchable()
2580            .into(),
2581        ),
2582        (
2583            "FunctionNameIdentifierSegment".into(),
2584            one_of(vec![
2585                RegexParser::new("[A-Z_][A-Z0-9_]*", SyntaxKind::FunctionNameIdentifier)
2586                    .anti_template("^(STRUCT|ARRAY)$")
2587                    .to_matchable(),
2588                RegexParser::new("`[^`]*`", SyntaxKind::FunctionNameIdentifier).to_matchable(),
2589            ])
2590            .to_matchable()
2591            .into(),
2592        ),
2593    ]);
2594
2595    dialect.expand();
2596    dialect
2597}