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