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