Skip to main content

sqruff_lib_dialects/
sparksql.rs

1use sqruff_lib_core::dialects::Dialect;
2use sqruff_lib_core::dialects::init::DialectKind;
3use sqruff_lib_core::dialects::syntax::SyntaxKind;
4use sqruff_lib_core::helpers::{Config, ToMatchable};
5use sqruff_lib_core::parser::grammar::anyof::{
6    AnyNumberOf, any_set_of, one_of, optionally_bracketed,
7};
8use sqruff_lib_core::parser::grammar::conditional::Conditional;
9use sqruff_lib_core::parser::grammar::delimited::Delimited;
10use sqruff_lib_core::parser::grammar::sequence::{Bracketed, Sequence};
11use sqruff_lib_core::parser::grammar::{Anything, Ref};
12use sqruff_lib_core::parser::lexer::Matcher;
13use sqruff_lib_core::parser::matchable::MatchableTrait;
14use sqruff_lib_core::parser::node_matcher::NodeMatcher;
15use sqruff_lib_core::parser::parsers::{MultiStringParser, RegexParser, StringParser, TypedParser};
16use sqruff_lib_core::parser::segments::bracketed::BracketedSegmentMatcher;
17use sqruff_lib_core::parser::segments::meta::MetaSegment;
18use sqruff_lib_core::parser::types::ParseMode;
19
20use super::sparksql_keywords::{RESERVED_KEYWORDS, UNRESERVED_KEYWORDS};
21use crate::ansi;
22
23pub fn raw_dialect() -> Dialect {
24    let ansi_dialect = ansi::raw_dialect();
25    let hive_dialect = super::hive::raw_dialect();
26    let mut sparksql_dialect = ansi_dialect;
27    sparksql_dialect.name = DialectKind::Sparksql;
28
29    sparksql_dialect.patch_lexer_matchers(vec![
30        Matcher::regex("inline_comment", r"(--)[^\n]*", SyntaxKind::InlineComment),
31        Matcher::regex("equals", r"==|<=>|=", SyntaxKind::RawComparisonOperator),
32        Matcher::regex("back_quote", r"`([^`]|``)*`", SyntaxKind::BackQuote),
33        Matcher::legacy("numeric_literal", |s| s.starts_with(|ch: char| ch == '.' || ch == '-' || ch.is_ascii_alphanumeric()), r#"(?>(?>\d+\.\d+|\d+\.|\.\d+)([eE][+-]?\d+)?([dDfF]|BD|bd)?|\d+[eE][+-]?\d+([dDfF]|BD|bd)?|\d+([dDfFlLsSyY]|BD|bd)?)((?<=\.)|(?=\b))"#, SyntaxKind::NumericLiteral),
34    ]);
35
36    sparksql_dialect.insert_lexer_matchers(
37        vec![
38            Matcher::regex(
39                "bytes_single_quote",
40                r"X'([^'\\]|\\.)*'",
41                SyntaxKind::BytesSingleQuote,
42            ),
43            Matcher::regex(
44                "bytes_double_quote",
45                r#"X"([^"\\]|\\.)*""#,
46                SyntaxKind::BytesDoubleQuote,
47            ),
48        ],
49        "single_quote",
50    );
51
52    sparksql_dialect.insert_lexer_matchers(
53        vec![
54            Matcher::regex(
55                "bytes_single_quote",
56                r"X'([^'\\]|\\.)*'",
57                SyntaxKind::BytesSingleQuote,
58            ),
59            Matcher::regex(
60                "bytes_double_quote",
61                r#"X"([^"\\]|\\.)*""#,
62                SyntaxKind::BytesDoubleQuote,
63            ),
64        ],
65        "single_quote",
66    );
67
68    sparksql_dialect.insert_lexer_matchers(
69        vec![Matcher::regex(
70            "at_sign_literal",
71            r"@\w*",
72            SyntaxKind::AtSignLiteral,
73        )],
74        "word",
75    );
76
77    sparksql_dialect.insert_lexer_matchers(
78        vec![
79            Matcher::regex("file_literal", r#"[a-zA-Z0-9]*:?([a-zA-Z0-9\-_\.]*(/|\\)){2,}((([a-zA-Z0-9\-_\.]*(:|\?|=|&)[a-zA-Z0-9\-_\.]*)+)|([a-zA-Z0-9\-_\.]*\.[a-z]+))"#, SyntaxKind::FileLiteral),
80        ],
81        "newline",
82    );
83
84    sparksql_dialect.sets_mut("bare_functions").clear();
85    sparksql_dialect.sets_mut("bare_functions").extend([
86        "CURRENT_DATE",
87        "CURRENT_TIMESTAMP",
88        "CURRENT_USER",
89    ]);
90
91    sparksql_dialect.sets_mut("date_part_function_name").clear();
92    sparksql_dialect
93        .sets_mut("date_part_function_name")
94        .extend([
95            "DATE_ADD",
96            "DATE_DIFF",
97            "DATEADD",
98            "DATEDIFF",
99            "TIMESTAMPADD",
100            "TIMESTAMPDIFF",
101        ]);
102
103    sparksql_dialect.sets_mut("datetime_units").clear();
104    sparksql_dialect.sets_mut("datetime_units").extend([
105        "YEAR",
106        "YEARS",
107        "YYYY",
108        "YY",
109        "QUARTER",
110        "QUARTERS",
111        "MONTH",
112        "MONTHS",
113        "MON",
114        "MM",
115        "WEEK",
116        "WEEKS",
117        "DAY",
118        "DAYS",
119        "DD",
120        "HOUR",
121        "HOURS",
122        "MINUTE",
123        "MINUTES",
124        "SECOND",
125        "SECONDS",
126        "MILLISECOND",
127        "MILLISECONDS",
128        "MICROSECOND",
129        "MICROSECONDS",
130    ]);
131
132    sparksql_dialect
133        .sets_mut("unreserved_keywords")
134        .extend(UNRESERVED_KEYWORDS);
135    sparksql_dialect
136        .sets_mut("reserved_keywords")
137        .extend(RESERVED_KEYWORDS);
138
139    sparksql_dialect.update_bracket_sets(
140        "angle_bracket_pairs",
141        vec![(
142            "angle",
143            "StartAngleBracketSegment",
144            "EndAngleBracketSegment",
145            false,
146        )],
147    );
148
149    sparksql_dialect.add([
150        (
151            "SelectClauseTerminatorGrammar".into(),
152            ansi::raw_dialect()
153                .grammar("SelectClauseTerminatorGrammar")
154                .copy(
155                    Some(vec![
156                        Sequence::new(vec![
157                            Ref::keyword("CLUSTER").to_matchable(),
158                            Ref::keyword("BY").to_matchable(),
159                        ])
160                        .to_matchable(),
161                        Sequence::new(vec![
162                            Ref::keyword("DISTRIBUTE").to_matchable(),
163                            Ref::keyword("BY").to_matchable(),
164                        ])
165                        .to_matchable(),
166                        Sequence::new(vec![
167                            Ref::keyword("SORT").to_matchable(),
168                            Ref::keyword("BY").to_matchable(),
169                        ])
170                        .to_matchable(),
171                        Ref::keyword("QUALIFY").to_matchable(),
172                    ]),
173                    None,
174                    None,
175                    None,
176                    Vec::new(),
177                    false,
178                )
179                .into(),
180        ),
181        (
182            "ComparisonOperatorGrammar".into(),
183            one_of(vec![
184                Ref::new("EqualsSegment").to_matchable(),
185                Ref::new("EqualsSegment_a").to_matchable(),
186                Ref::new("EqualsSegment_b").to_matchable(),
187                Ref::new("GreaterThanSegment").to_matchable(),
188                Ref::new("LessThanSegment").to_matchable(),
189                Ref::new("GreaterThanOrEqualToSegment").to_matchable(),
190                Ref::new("LessThanOrEqualToSegment").to_matchable(),
191                Ref::new("NotEqualToSegment").to_matchable(),
192                Ref::new("LikeOperatorSegment").to_matchable(),
193                Sequence::new(vec![
194                    Ref::keyword("IS").to_matchable(),
195                    Ref::keyword("DISTINCT").to_matchable(),
196                    Ref::keyword("FROM").to_matchable(),
197                ])
198                .to_matchable(),
199                Sequence::new(vec![
200                    Ref::keyword("IS").to_matchable(),
201                    Ref::keyword("NOT").to_matchable(),
202                    Ref::keyword("DISTINCT").to_matchable(),
203                    Ref::keyword("FROM").to_matchable(),
204                ])
205                .to_matchable(),
206            ])
207            .to_matchable()
208            .into(),
209        ),
210        (
211            "FromClauseTerminatorGrammar".into(),
212            one_of(vec![
213                Ref::keyword("WHERE").to_matchable(),
214                Ref::keyword("LIMIT").to_matchable(),
215                Sequence::new(vec![
216                    Ref::keyword("GROUP").to_matchable(),
217                    Ref::keyword("BY").to_matchable(),
218                ])
219                .to_matchable(),
220                Sequence::new(vec![
221                    Ref::keyword("ORDER").to_matchable(),
222                    Ref::keyword("BY").to_matchable(),
223                ])
224                .to_matchable(),
225                Sequence::new(vec![
226                    Ref::keyword("CLUSTER").to_matchable(),
227                    Ref::keyword("BY").to_matchable(),
228                ])
229                .to_matchable(),
230                Sequence::new(vec![
231                    Ref::keyword("DISTRIBUTE").to_matchable(),
232                    Ref::keyword("BY").to_matchable(),
233                ])
234                .to_matchable(),
235                Sequence::new(vec![
236                    Ref::keyword("SORT").to_matchable(),
237                    Ref::keyword("BY").to_matchable(),
238                ])
239                .to_matchable(),
240                Ref::keyword("HAVING").to_matchable(),
241                Ref::keyword("QUALIFY").to_matchable(),
242                Ref::new("SetOperatorSegment").to_matchable(),
243                Ref::new("WithNoSchemaBindingClauseSegment").to_matchable(),
244                Ref::new("WithDataClauseSegment").to_matchable(),
245                Ref::keyword("KEYS").to_matchable(),
246            ])
247            .to_matchable()
248            .into(),
249        ),
250        (
251            "TemporaryGrammar".into(),
252            Sequence::new(vec![
253                Sequence::new(vec![Ref::keyword("GLOBAL").to_matchable()])
254                    .config(|config| {
255                        config.optional();
256                    })
257                    .to_matchable(),
258                one_of(vec![
259                    Ref::keyword("TEMP").to_matchable(),
260                    Ref::keyword("TEMPORARY").to_matchable(),
261                ])
262                .to_matchable(),
263            ])
264            .to_matchable()
265            .into(),
266        ),
267        (
268            "QuotedLiteralSegment".into(),
269            one_of(vec![
270                TypedParser::new(SyntaxKind::SingleQuote, SyntaxKind::QuotedLiteral).to_matchable(),
271                TypedParser::new(SyntaxKind::DoubleQuote, SyntaxKind::QuotedLiteral).to_matchable(),
272            ])
273            .to_matchable()
274            .into(),
275        ),
276        (
277            "LiteralGrammar".into(),
278            sparksql_dialect
279                .grammar("LiteralGrammar")
280                .copy(
281                    Some(vec![Ref::new("BytesQuotedLiteralSegment").to_matchable()]),
282                    None,
283                    None,
284                    None,
285                    Vec::new(),
286                    false,
287                )
288                .into(),
289        ),
290        (
291            "NaturalJoinKeywordsGrammar".into(),
292            Sequence::new(vec![
293                Ref::keyword("NATURAL").to_matchable(),
294                Ref::new("JoinTypeKeywords").optional().to_matchable(),
295            ])
296            .to_matchable()
297            .into(),
298        ),
299        (
300            "LikeGrammar".into(),
301            one_of(vec![
302                Sequence::new(vec![
303                    one_of(vec![
304                        Ref::keyword("LIKE").to_matchable(),
305                        Ref::keyword("ILIKE").to_matchable(),
306                    ])
307                    .to_matchable(),
308                    one_of(vec![
309                        Ref::keyword("ALL").to_matchable(),
310                        Ref::keyword("ANY").to_matchable(),
311                        Ref::keyword("SOME").to_matchable(),
312                    ])
313                    .config(|config| {
314                        config.optional();
315                    })
316                    .to_matchable(),
317                ])
318                .to_matchable(),
319                Ref::keyword("RLIKE").to_matchable(),
320                Ref::keyword("REGEXP").to_matchable(),
321            ])
322            .to_matchable()
323            .into(),
324        ),
325        (
326            "SingleIdentifierGrammar".into(),
327            one_of(vec![
328                Ref::new("NakedIdentifierSegment").to_matchable(),
329                Ref::new("QuotedIdentifierSegment").to_matchable(),
330                Ref::new("SingleQuotedIdentifierSegment").to_matchable(),
331                Ref::new("BackQuotedIdentifierSegment").to_matchable(),
332            ])
333            .to_matchable()
334            .into(),
335        ),
336        (
337            "WhereClauseTerminatorGrammar".into(),
338            one_of(vec![
339                Ref::keyword("LIMIT").to_matchable(),
340                Sequence::new(vec![
341                    one_of(vec![
342                        Ref::keyword("CLUSTER").to_matchable(),
343                        Ref::keyword("DISTRIBUTE").to_matchable(),
344                        Ref::keyword("GROUP").to_matchable(),
345                        Ref::keyword("ORDER").to_matchable(),
346                        Ref::keyword("SORT").to_matchable(),
347                    ])
348                    .to_matchable(),
349                    Ref::keyword("BY").to_matchable(),
350                ])
351                .to_matchable(),
352                Sequence::new(vec![
353                    Ref::keyword("ORDER").to_matchable(),
354                    Ref::keyword("BY").to_matchable(),
355                ])
356                .to_matchable(),
357                Sequence::new(vec![
358                    Ref::keyword("DISTRIBUTE").to_matchable(),
359                    Ref::keyword("BY").to_matchable(),
360                ])
361                .to_matchable(),
362                Ref::keyword("HAVING").to_matchable(),
363                Ref::keyword("QUALIFY").to_matchable(),
364                Ref::keyword("WINDOW").to_matchable(),
365                Ref::keyword("OVERLAPS").to_matchable(),
366                Ref::keyword("APPLY").to_matchable(),
367            ])
368            .to_matchable()
369            .into(),
370        ),
371        (
372            "GroupByClauseTerminatorGrammar".into(),
373            one_of(vec![
374                Sequence::new(vec![
375                    one_of(vec![
376                        Ref::keyword("ORDER").to_matchable(),
377                        Ref::keyword("DISTRIBUTE").to_matchable(),
378                        Ref::keyword("CLUSTER").to_matchable(),
379                        Ref::keyword("SORT").to_matchable(),
380                    ])
381                    .to_matchable(),
382                    Ref::keyword("BY").to_matchable(),
383                ])
384                .to_matchable(),
385                Ref::keyword("LIMIT").to_matchable(),
386                Ref::keyword("HAVING").to_matchable(),
387                Ref::keyword("WINDOW").to_matchable(),
388            ])
389            .to_matchable()
390            .into(),
391        ),
392        (
393            "HavingClauseTerminatorGrammar".into(),
394            one_of(vec![
395                Sequence::new(vec![
396                    one_of(vec![
397                        Ref::keyword("ORDER").to_matchable(),
398                        Ref::keyword("CLUSTER").to_matchable(),
399                        Ref::keyword("DISTRIBUTE").to_matchable(),
400                        Ref::keyword("SORT").to_matchable(),
401                    ])
402                    .to_matchable(),
403                    Ref::keyword("BY").to_matchable(),
404                ])
405                .to_matchable(),
406                Ref::keyword("LIMIT").to_matchable(),
407                Ref::keyword("QUALIFY").to_matchable(),
408                Ref::keyword("WINDOW").to_matchable(),
409            ])
410            .to_matchable()
411            .into(),
412        ),
413        (
414            "ArithmeticBinaryOperatorGrammar".into(),
415            one_of(vec![
416                Ref::new("PlusSegment").to_matchable(),
417                Ref::new("MinusSegment").to_matchable(),
418                Ref::new("DivideSegment").to_matchable(),
419                Ref::new("MultiplySegment").to_matchable(),
420                Ref::new("ModuloSegment").to_matchable(),
421                Ref::new("BitwiseAndSegment").to_matchable(),
422                Ref::new("BitwiseOrSegment").to_matchable(),
423                Ref::new("BitwiseXorSegment").to_matchable(),
424                Ref::new("BitwiseLShiftSegment").to_matchable(),
425                Ref::new("BitwiseRShiftSegment").to_matchable(),
426                Ref::new("DivBinaryOperatorSegment").to_matchable(),
427            ])
428            .to_matchable()
429            .into(),
430        ),
431        (
432            "BinaryOperatorGrammar".into(),
433            one_of(vec![
434                Ref::new("ArithmeticBinaryOperatorGrammar").to_matchable(),
435                Ref::new("StringBinaryOperatorGrammar").to_matchable(),
436                Ref::new("BooleanBinaryOperatorGrammar").to_matchable(),
437                Ref::new("ComparisonOperatorGrammar").to_matchable(),
438                Ref::new("RightArrowOperator").to_matchable(),
439            ])
440            .to_matchable()
441            .into(),
442        ),
443        (
444            "AccessorGrammar".into(),
445            AnyNumberOf::new(vec![
446                Ref::new("ArrayAccessorSegment").to_matchable(),
447                Ref::new("SemiStructuredAccessorSegment").to_matchable(),
448            ])
449            .to_matchable()
450            .into(),
451        ),
452        (
453            "ObjectReferenceTerminatorGrammar".into(),
454            one_of(vec![
455                Ref::keyword("ON").to_matchable(),
456                Ref::keyword("AS").to_matchable(),
457                Ref::keyword("USING").to_matchable(),
458                Ref::new("CommaSegment").to_matchable(),
459                Ref::new("CastOperatorSegment").to_matchable(),
460                Ref::new("StartSquareBracketSegment").to_matchable(),
461                Ref::new("StartBracketSegment").to_matchable(),
462                Ref::new("BinaryOperatorGrammar").to_matchable(),
463                Ref::new("DelimiterGrammar").to_matchable(),
464                Ref::new("JoinLikeClauseGrammar").to_matchable(),
465                BracketedSegmentMatcher::new().to_matchable(),
466            ])
467            .to_matchable()
468            .into(),
469        ),
470        (
471            "FunctionContentsExpressionGrammar".into(),
472            one_of(vec![
473                Ref::new("ExpressionSegment").to_matchable(),
474                Ref::new("StarSegment").to_matchable(),
475            ])
476            .to_matchable()
477            .into(),
478        ),
479        (
480            // An `IDENTIFIER` clause segment.
481            // https://docs.databricks.com/en/sql/language-manual/sql-ref-names-identifier-clause.html
482            "IdentifierClauseSegment".into(),
483            Sequence::new(vec![
484                Ref::keyword("IDENTIFIER").to_matchable(),
485                Bracketed::new(vec![Ref::new("ExpressionSegment").to_matchable()]).to_matchable(),
486            ])
487            .to_matchable()
488            .into(),
489        ),
490    ]);
491
492    sparksql_dialect.add([
493        (
494            "FileLiteralSegment".into(),
495            TypedParser::new(SyntaxKind::FileLiteral, SyntaxKind::FileLiteral)
496                .to_matchable()
497                .into(),
498        ),
499        (
500            "BackQuotedIdentifierSegment".into(),
501            TypedParser::new(SyntaxKind::BackQuote, SyntaxKind::QuotedIdentifier)
502                .to_matchable()
503                .into(),
504        ),
505        (
506            "NakedSemiStructuredElementSegment".into(),
507            RegexParser::new("[A-Z0-9_]*", SyntaxKind::SemiStructuredElement)
508                .to_matchable()
509                .into(),
510        ),
511        (
512            "QuotedSemiStructuredElementSegment".into(),
513            TypedParser::new(SyntaxKind::SingleQuote, SyntaxKind::SemiStructuredElement)
514                .to_matchable()
515                .into(),
516        ),
517        (
518            "RightArrowOperator".into(),
519            StringParser::new("->", SyntaxKind::BinaryOperator)
520                .to_matchable()
521                .into(),
522        ),
523        (
524            "BINARYFILE".into(),
525            StringParser::new("BINARYFILE", SyntaxKind::FileFormat)
526                .to_matchable()
527                .into(),
528        ),
529        (
530            "JSONFILE".into(),
531            StringParser::new("JSONFILE", SyntaxKind::FileFormat)
532                .to_matchable()
533                .into(),
534        ),
535        (
536            "RCFILE".into(),
537            StringParser::new("RCFILE", SyntaxKind::FileFormat)
538                .to_matchable()
539                .into(),
540        ),
541        (
542            "SEQUENCEFILE".into(),
543            StringParser::new("SEQUENCEFILE", SyntaxKind::FileFormat)
544                .to_matchable()
545                .into(),
546        ),
547        (
548            "TEXTFILE".into(),
549            StringParser::new("TEXTFILE", SyntaxKind::FileFormat)
550                .to_matchable()
551                .into(),
552        ),
553        (
554            "StartAngleBracketSegment".into(),
555            StringParser::new("<", SyntaxKind::StartAngleBracket)
556                .to_matchable()
557                .into(),
558        ),
559        (
560            "EndAngleBracketSegment".into(),
561            StringParser::new(">", SyntaxKind::EndAngleBracket)
562                .to_matchable()
563                .into(),
564        ),
565        (
566            "EqualsSegment_a".into(),
567            StringParser::new("==", SyntaxKind::ComparisonOperator)
568                .to_matchable()
569                .into(),
570        ),
571        (
572            "EqualsSegment_b".into(),
573            StringParser::new("<=>", SyntaxKind::ComparisonOperator)
574                .to_matchable()
575                .into(),
576        ),
577        (
578            "FILE".into(),
579            MultiStringParser::new(vec!["FILE".into(), "FILES".into()], SyntaxKind::FileKeyword)
580                .to_matchable()
581                .into(),
582        ),
583        (
584            "JAR".into(),
585            MultiStringParser::new(vec!["JAR".into(), "JARS".into()], SyntaxKind::FileKeyword)
586                .to_matchable()
587                .into(),
588        ),
589        (
590            "NOSCAN".into(),
591            StringParser::new("NOSCAN", SyntaxKind::Keyword)
592                .to_matchable()
593                .into(),
594        ),
595        (
596            "WHL".into(),
597            StringParser::new("WHL", SyntaxKind::FileKeyword)
598                .to_matchable()
599                .into(),
600        ),
601        (
602            "CommentGrammar".into(),
603            hive_dialect.grammar("CommentGrammar").into(),
604        ),
605        (
606            "LocationGrammar".into(),
607            hive_dialect.grammar("LocationGrammar").into(),
608        ),
609        (
610            "SerdePropertiesGrammar".into(),
611            hive_dialect.grammar("SerdePropertiesGrammar").into(),
612        ),
613        (
614            "StoredAsGrammar".into(),
615            hive_dialect.grammar("StoredAsGrammar").into(),
616        ),
617        (
618            "StoredByGrammar".into(),
619            hive_dialect.grammar("StoredByGrammar").into(),
620        ),
621        (
622            "StorageFormatGrammar".into(),
623            hive_dialect.grammar("StorageFormatGrammar").into(),
624        ),
625        (
626            "TerminatedByGrammar".into(),
627            hive_dialect.grammar("TerminatedByGrammar").into(),
628        ),
629        (
630            "PropertyGrammar".into(),
631            Sequence::new(vec![
632                Ref::new("PropertyNameSegment").to_matchable(),
633                Ref::new("EqualsSegment").optional().to_matchable(),
634                one_of(vec![
635                    Ref::new("LiteralGrammar").to_matchable(),
636                    // when property value is Java Class Name
637                    Delimited::new(vec![
638                        Ref::new("PropertiesNakedIdentifierSegment").to_matchable(),
639                    ])
640                    .config(|config| {
641                        config.delimiter(Ref::new("DotSegment"));
642                    })
643                    .to_matchable(),
644                ])
645                .to_matchable(),
646            ])
647            .to_matchable()
648            .into(),
649        ),
650        (
651            "PropertyNameListGrammar".into(),
652            Delimited::new(vec![Ref::new("PropertyNameSegment").to_matchable()])
653                .to_matchable()
654                .into(),
655        ),
656        (
657            "BracketedPropertyNameListGrammar".into(),
658            Bracketed::new(vec![Ref::new("PropertyNameListGrammar").to_matchable()])
659                .to_matchable()
660                .into(),
661        ),
662        (
663            "PropertyListGrammar".into(),
664            Delimited::new(vec![Ref::new("PropertyGrammar").to_matchable()])
665                .to_matchable()
666                .into(),
667        ),
668        (
669            "BracketedPropertyListGrammar".into(),
670            Bracketed::new(vec![Ref::new("PropertyListGrammar").to_matchable()])
671                .to_matchable()
672                .into(),
673        ),
674        (
675            "OptionsGrammar".into(),
676            Sequence::new(vec![
677                Ref::keyword("OPTIONS").to_matchable(),
678                Ref::new("BracketedPropertyListGrammar").to_matchable(),
679            ])
680            .to_matchable()
681            .into(),
682        ),
683        (
684            "BucketSpecGrammar".into(),
685            Sequence::new(vec![
686                Ref::new("ClusteredBySpecGrammar").to_matchable(),
687                Ref::new("SortedBySpecGrammar").optional().to_matchable(),
688                Ref::keyword("INTO").to_matchable(),
689                Ref::new("NumericLiteralSegment").to_matchable(),
690                Ref::keyword("BUCKETS").to_matchable(),
691            ])
692            .to_matchable()
693            .into(),
694        ),
695        (
696            "ClusteredBySpecGrammar".into(),
697            Sequence::new(vec![
698                Ref::keyword("CLUSTERED").to_matchable(),
699                Ref::keyword("BY").to_matchable(),
700                Ref::new("BracketedColumnReferenceListGrammar").to_matchable(),
701            ])
702            .to_matchable()
703            .into(),
704        ),
705        (
706            "DatabasePropertiesGrammar".into(),
707            Sequence::new(vec![
708                Ref::keyword("DBPROPERTIES").to_matchable(),
709                Ref::new("BracketedPropertyListGrammar").to_matchable(),
710            ])
711            .to_matchable()
712            .into(),
713        ),
714        (
715            "DataSourcesV2FileTypeGrammar".into(),
716            one_of(vec![
717                Ref::keyword("AVRO").to_matchable(),
718                Ref::keyword("CSV").to_matchable(),
719                Ref::keyword("JSON").to_matchable(),
720                Ref::keyword("PARQUET").to_matchable(),
721                Ref::keyword("ORC").to_matchable(),
722                Ref::keyword("DELTA").to_matchable(),
723                Ref::keyword("CSV").to_matchable(),
724                Ref::keyword("ICEBERG").to_matchable(),
725                Ref::keyword("TEXT").to_matchable(),
726                Ref::keyword("BINARYFILE").to_matchable(),
727            ])
728            .to_matchable()
729            .into(),
730        ),
731        (
732            "FileFormatGrammar".into(),
733            one_of(vec![
734                Ref::new("DataSourcesV2FileTypeGrammar").to_matchable(),
735                Ref::keyword("SEQUENCEFILE").to_matchable(),
736                Ref::keyword("TEXTFILE").to_matchable(),
737                Ref::keyword("RCFILE").to_matchable(),
738                Ref::keyword("JSONFILE").to_matchable(),
739                Sequence::new(vec![
740                    Ref::keyword("INPUTFORMAT").to_matchable(),
741                    Ref::new("QuotedLiteralSegment").to_matchable(),
742                    Ref::keyword("OUTPUTFORMAT").to_matchable(),
743                    Ref::new("QuotedLiteralSegment").to_matchable(),
744                ])
745                .to_matchable(),
746            ])
747            .to_matchable()
748            .into(),
749        ),
750        (
751            "TimestampAsOfGrammar".into(),
752            Sequence::new(vec![
753                Ref::keyword("TIMESTAMP").to_matchable(),
754                Ref::keyword("AS").to_matchable(),
755                Ref::keyword("OF").to_matchable(),
756                one_of(vec![
757                    Ref::new("QuotedLiteralSegment").to_matchable(),
758                    Ref::new("BareFunctionSegment").to_matchable(),
759                    Ref::new("FunctionSegment").to_matchable(),
760                ])
761                .to_matchable(),
762            ])
763            .to_matchable()
764            .into(),
765        ),
766        (
767            "VersionAsOfGrammar".into(),
768            Sequence::new(vec![
769                Ref::keyword("VERSION").to_matchable(),
770                Ref::keyword("AS").to_matchable(),
771                Ref::keyword("OF").to_matchable(),
772                Ref::new("NumericLiteralSegment").to_matchable(),
773            ])
774            .to_matchable()
775            .into(),
776        ),
777        (
778            "StartHintSegment".into(),
779            StringParser::new("/*+", SyntaxKind::StartHint)
780                .to_matchable()
781                .into(),
782        ),
783        (
784            "EndHintSegment".into(),
785            StringParser::new("*/", SyntaxKind::EndHint)
786                .to_matchable()
787                .into(),
788        ),
789        (
790            "PartitionSpecGrammar".into(),
791            Sequence::new(vec![
792                one_of(vec![
793                    Ref::keyword("PARTITION").to_matchable(),
794                    Sequence::new(vec![
795                        Ref::keyword("PARTITIONED").to_matchable(),
796                        Ref::keyword("BY").to_matchable(),
797                    ])
798                    .to_matchable(),
799                ])
800                .to_matchable(),
801                Bracketed::new(vec![
802                    Delimited::new(vec![
803                        one_of(vec![
804                            Ref::new("ColumnDefinitionSegment").to_matchable(),
805                            Sequence::new(vec![
806                                Ref::new("ColumnReferenceSegment").to_matchable(),
807                                Ref::new("EqualsSegment").optional().to_matchable(),
808                                Ref::new("LiteralGrammar").optional().to_matchable(),
809                                Ref::new("CommentGrammar").optional().to_matchable(),
810                            ])
811                            .to_matchable(),
812                            Ref::new("IcebergTransformationSegment")
813                                .optional()
814                                .to_matchable(),
815                        ])
816                        .to_matchable(),
817                    ])
818                    .to_matchable(),
819                ])
820                .to_matchable(),
821            ])
822            .to_matchable()
823            .into(),
824        ),
825        (
826            "PartitionFieldGrammar".into(),
827            Sequence::new(vec![
828                Ref::keyword("PARTITION").to_matchable(),
829                Ref::keyword("FIELD").to_matchable(),
830                Delimited::new(vec![
831                    one_of(vec![
832                        Ref::new("ColumnDefinitionSegment").to_matchable(),
833                        Sequence::new(vec![
834                            Ref::new("ColumnReferenceSegment").to_matchable(),
835                            Ref::new("EqualsSegment").optional().to_matchable(),
836                            Ref::new("LiteralGrammar").optional().to_matchable(),
837                            Ref::new("CommentGrammar").optional().to_matchable(),
838                        ])
839                        .to_matchable(),
840                        Ref::new("IcebergTransformationSegment")
841                            .optional()
842                            .to_matchable(),
843                    ])
844                    .to_matchable(),
845                ])
846                .to_matchable(),
847                Sequence::new(vec![
848                    Ref::keyword("WITH").optional().to_matchable(),
849                    Delimited::new(vec![
850                        one_of(vec![
851                            Ref::new("ColumnDefinitionSegment").to_matchable(),
852                            Sequence::new(vec![
853                                Ref::new("ColumnReferenceSegment").to_matchable(),
854                                Ref::new("EqualsSegment").optional().to_matchable(),
855                                Ref::new("LiteralGrammar").optional().to_matchable(),
856                                Ref::new("CommentGrammar").optional().to_matchable(),
857                            ])
858                            .to_matchable(),
859                            Ref::new("IcebergTransformationSegment")
860                                .optional()
861                                .to_matchable(),
862                        ])
863                        .to_matchable(),
864                    ])
865                    .to_matchable(),
866                ])
867                .config(|config| {
868                    config.optional();
869                })
870                .to_matchable(),
871                Sequence::new(vec![
872                    Ref::keyword("AS").to_matchable(),
873                    Ref::new("NakedIdentifierSegment").to_matchable(),
874                ])
875                .config(|config| {
876                    config.optional();
877                })
878                .to_matchable(),
879            ])
880            .to_matchable()
881            .into(),
882        ),
883        (
884            "PropertiesNakedIdentifierSegment".into(),
885            RegexParser::new(
886                "[A-Z0-9]*[A-Z][A-Z0-9]*",
887                SyntaxKind::PropertiesNakedIdentifier,
888            )
889            .to_matchable()
890            .into(),
891        ),
892        (
893            "ResourceFileGrammar".into(),
894            one_of(vec![
895                Ref::new("JAR").to_matchable(),
896                Ref::new("WHL").to_matchable(),
897                Ref::new("FILE").to_matchable(),
898            ])
899            .to_matchable()
900            .into(),
901        ),
902        (
903            "ResourceLocationGrammar".into(),
904            Sequence::new(vec![
905                Ref::keyword("USING").to_matchable(),
906                Ref::new("ResourceFileGrammar").to_matchable(),
907                Ref::new("QuotedLiteralSegment").to_matchable(),
908            ])
909            .to_matchable()
910            .into(),
911        ),
912        (
913            "SortedBySpecGrammar".into(),
914            Sequence::new(vec![
915                Ref::keyword("SORTED").to_matchable(),
916                Ref::keyword("BY").to_matchable(),
917                Bracketed::new(vec![
918                    Delimited::new(vec![
919                        Sequence::new(vec![
920                            Ref::new("ColumnReferenceSegment").to_matchable(),
921                            one_of(vec![
922                                Ref::keyword("ASC").to_matchable(),
923                                Ref::keyword("DESC").to_matchable(),
924                            ])
925                            .config(|config| {
926                                config.optional();
927                            })
928                            .to_matchable(),
929                        ])
930                        .to_matchable(),
931                    ])
932                    .to_matchable(),
933                ])
934                .to_matchable(),
935            ])
936            .config(|config| {
937                config.optional();
938            })
939            .to_matchable()
940            .into(),
941        ),
942        (
943            "UnsetTablePropertiesGrammar".into(),
944            Sequence::new(vec![
945                Ref::keyword("UNSET").to_matchable(),
946                Ref::keyword("TBLPROPERTIES").to_matchable(),
947                Ref::new("IfExistsGrammar").optional().to_matchable(),
948                Ref::new("BracketedPropertyNameListGrammar").to_matchable(),
949            ])
950            .to_matchable()
951            .into(),
952        ),
953        (
954            "TablePropertiesGrammar".into(),
955            Sequence::new(vec![
956                Ref::keyword("TBLPROPERTIES").to_matchable(),
957                Ref::new("BracketedPropertyListGrammar").to_matchable(),
958            ])
959            .to_matchable()
960            .into(),
961        ),
962        (
963            "BytesQuotedLiteralSegment".into(),
964            one_of(vec![
965                TypedParser::new(SyntaxKind::BytesSingleQuote, SyntaxKind::BytesQuotedLiteral)
966                    .to_matchable(),
967                TypedParser::new(SyntaxKind::BytesDoubleQuote, SyntaxKind::BytesQuotedLiteral)
968                    .to_matchable(),
969            ])
970            .to_matchable()
971            .into(),
972        ),
973        (
974            "JoinTypeKeywords".into(),
975            one_of(vec![
976                Ref::keyword("CROSS").to_matchable(),
977                Ref::keyword("INNER").to_matchable(),
978                Sequence::new(vec![
979                    one_of(vec![
980                        Ref::keyword("FULL").to_matchable(),
981                        Ref::keyword("LEFT").to_matchable(),
982                        Ref::keyword("RIGHT").to_matchable(),
983                    ])
984                    .to_matchable(),
985                    Ref::keyword("OUTER").optional().to_matchable(),
986                ])
987                .to_matchable(),
988                Sequence::new(vec![
989                    Ref::keyword("LEFT").optional().to_matchable(),
990                    Ref::keyword("SEMI").to_matchable(),
991                ])
992                .to_matchable(),
993                Sequence::new(vec![
994                    Ref::keyword("LEFT").optional().to_matchable(),
995                    Ref::keyword("ANTI").to_matchable(),
996                ])
997                .to_matchable(),
998            ])
999            .to_matchable()
1000            .into(),
1001        ),
1002        (
1003            "AtSignLiteralSegment".into(),
1004            TypedParser::new(SyntaxKind::AtSignLiteral, SyntaxKind::AtSignLiteral)
1005                .to_matchable()
1006                .into(),
1007        ),
1008        (
1009            "SignedQuotedLiteralSegment".into(),
1010            one_of(vec![
1011                TypedParser::new(SyntaxKind::SingleQuote, SyntaxKind::SignedQuotedLiteral)
1012                    .to_matchable(),
1013                TypedParser::new(SyntaxKind::DoubleQuote, SyntaxKind::SignedQuotedLiteral)
1014                    .to_matchable(),
1015            ])
1016            .to_matchable()
1017            .into(),
1018        ),
1019        (
1020            "OrRefreshGrammar".into(),
1021            Sequence::new(vec![
1022                Ref::keyword("OR").to_matchable(),
1023                Ref::keyword("REFRESH").to_matchable(),
1024            ])
1025            .to_matchable()
1026            .into(),
1027        ),
1028        (
1029            "WidgetNameIdentifierSegment".into(),
1030            RegexParser::new("[A-Z][A-Z0-9_]*", SyntaxKind::WidgetNameIdentifier)
1031                .to_matchable()
1032                .into(),
1033        ),
1034        (
1035            "WidgetDefaultGrammar".into(),
1036            Sequence::new(vec![
1037                Ref::keyword("DEFAULT").to_matchable(),
1038                Ref::new("QuotedLiteralSegment").to_matchable(),
1039            ])
1040            .to_matchable()
1041            .into(),
1042        ),
1043        (
1044            "TableDefinitionSegment".into(),
1045            Sequence::new(vec![
1046                one_of(vec![
1047                    Ref::new("OrReplaceGrammar").to_matchable(),
1048                    Ref::new("OrRefreshGrammar").to_matchable(),
1049                ])
1050                .config(|config| {
1051                    config.optional();
1052                })
1053                .to_matchable(),
1054                Ref::new("TemporaryGrammar").optional().to_matchable(),
1055                Ref::keyword("EXTERNAL").optional().to_matchable(),
1056                Ref::keyword("STREAMING").optional().to_matchable(),
1057                Ref::keyword("LIVE").optional().to_matchable(),
1058                Ref::keyword("TABLE").to_matchable(),
1059                Ref::new("IfNotExistsGrammar").optional().to_matchable(),
1060                one_of(vec![
1061                    Ref::new("FileReferenceSegment").to_matchable(),
1062                    Ref::new("TableReferenceSegment").to_matchable(),
1063                ])
1064                .to_matchable(),
1065                one_of(vec![
1066                    Bracketed::new(vec![
1067                        Delimited::new(vec![
1068                            Sequence::new(vec![
1069                                one_of(vec![
1070                                    Ref::new("ColumnDefinitionSegment").to_matchable(),
1071                                    Ref::new("GeneratedColumnDefinitionSegment").to_matchable(),
1072                                ])
1073                                .to_matchable(),
1074                                Ref::new("CommentGrammar").optional().to_matchable(),
1075                            ])
1076                            .to_matchable(),
1077                            Ref::new("ConstraintStatementSegment")
1078                                .optional()
1079                                .to_matchable(),
1080                        ])
1081                        .to_matchable(),
1082                    ])
1083                    .to_matchable(),
1084                    Sequence::new(vec![
1085                        Ref::keyword("LIKE").to_matchable(),
1086                        one_of(vec![
1087                            Ref::new("FileReferenceSegment").to_matchable(),
1088                            Ref::new("TableReferenceSegment").to_matchable(),
1089                        ])
1090                        .to_matchable(),
1091                    ])
1092                    .to_matchable(),
1093                ])
1094                .config(|config| {
1095                    config.optional();
1096                })
1097                .to_matchable(),
1098                Ref::new("UsingClauseSegment").optional().to_matchable(),
1099                any_set_of(vec![
1100                    Ref::new("RowFormatClauseSegment").to_matchable(),
1101                    Ref::new("StoredAsGrammar").to_matchable(),
1102                    Ref::new("CommentGrammar").to_matchable(),
1103                    Ref::new("OptionsGrammar").to_matchable(),
1104                    Ref::new("PartitionSpecGrammar").to_matchable(),
1105                    Ref::new("BucketSpecGrammar").to_matchable(),
1106                    Ref::new("LocationGrammar").to_matchable(),
1107                    Ref::new("CommentGrammar").to_matchable(),
1108                    Ref::new("TablePropertiesGrammar").to_matchable(),
1109                    Sequence::new(vec![
1110                        Ref::keyword("CLUSTER").to_matchable(),
1111                        Ref::keyword("BY").to_matchable(),
1112                        Ref::new("BracketedColumnReferenceListGrammar").to_matchable(),
1113                    ])
1114                    .to_matchable(),
1115                ])
1116                .config(|config| {
1117                    config.optional();
1118                })
1119                .to_matchable(),
1120                Sequence::new(vec![
1121                    Ref::keyword("AS").optional().to_matchable(),
1122                    optionally_bracketed(vec![Ref::new("SelectableGrammar").to_matchable()])
1123                        .to_matchable(),
1124                ])
1125                .config(|config| {
1126                    config.optional();
1127                })
1128                .to_matchable(),
1129            ])
1130            .to_matchable()
1131            .into(),
1132        ),
1133    ]);
1134
1135    sparksql_dialect.insert_lexer_matchers(
1136        vec![Matcher::legacy(
1137            "start_hint",
1138            |s| s.starts_with("/*+"),
1139            r"\/\*\+",
1140            SyntaxKind::StartHint,
1141        )],
1142        "block_comment",
1143    );
1144
1145    sparksql_dialect.insert_lexer_matchers(
1146        vec![Matcher::regex("end_hint", r"\*\/", SyntaxKind::EndHint)],
1147        "single_quote",
1148    );
1149
1150    sparksql_dialect.insert_lexer_matchers(
1151        vec![Matcher::string("end_hint", r"->", SyntaxKind::RightArrow)],
1152        "like_operator",
1153    );
1154
1155    sparksql_dialect.add([
1156        (
1157            "SQLConfPropertiesSegment".into(),
1158            NodeMatcher::new(SyntaxKind::SqlConfOption, |_| {
1159                Sequence::new(vec![
1160                    StringParser::new("-", SyntaxKind::Dash).to_matchable(),
1161                    StringParser::new("v", SyntaxKind::SqlConfOption).to_matchable(),
1162                ])
1163                .config(|config| {
1164                    config.disallow_gaps();
1165                })
1166                .to_matchable()
1167            })
1168            .to_matchable()
1169            .into(),
1170        ),
1171        (
1172            "DivBinaryOperatorSegment".into(),
1173            NodeMatcher::new(SyntaxKind::BinaryOperator, |_| {
1174                Ref::keyword("DIV").to_matchable()
1175            })
1176            .to_matchable()
1177            .into(),
1178        ),
1179        (
1180            "QualifyClauseSegment".into(),
1181            NodeMatcher::new(SyntaxKind::QualifyClause, |_| {
1182                Sequence::new(vec![
1183                    Ref::keyword("QUALIFY").to_matchable(),
1184                    MetaSegment::indent().to_matchable(),
1185                    optionally_bracketed(vec![Ref::new("ExpressionSegment").to_matchable()])
1186                        .to_matchable(),
1187                    MetaSegment::dedent().to_matchable(),
1188                ])
1189                .to_matchable()
1190            })
1191            .to_matchable()
1192            .into(),
1193        ),
1194    ]);
1195
1196    sparksql_dialect.add([
1197        (
1198            "PrimitiveTypeSegment".into(),
1199            NodeMatcher::new(SyntaxKind::PrimitiveType, |_| {
1200                one_of(vec![
1201                    Ref::keyword("BOOLEAN").to_matchable(),
1202                    Ref::keyword("TINYINT").to_matchable(),
1203                    Ref::keyword("LONG").to_matchable(),
1204                    Ref::keyword("SMALLINT").to_matchable(),
1205                    Ref::keyword("INT").to_matchable(),
1206                    Ref::keyword("INTEGER").to_matchable(),
1207                    Ref::keyword("BIGINT").to_matchable(),
1208                    Ref::keyword("FLOAT").to_matchable(),
1209                    Ref::keyword("REAL").to_matchable(),
1210                    Ref::keyword("DOUBLE").to_matchable(),
1211                    Ref::keyword("DATE").to_matchable(),
1212                    Ref::keyword("TIMESTAMP").to_matchable(),
1213                    Ref::keyword("TIMESTAMP_LTZ").to_matchable(),
1214                    Ref::keyword("TIMESTAMP_NTZ").to_matchable(),
1215                    Ref::keyword("STRING").to_matchable(),
1216                    Sequence::new(vec![
1217                        one_of(vec![
1218                            Ref::keyword("CHAR").to_matchable(),
1219                            Ref::keyword("CHARACTER").to_matchable(),
1220                            Ref::keyword("VARCHAR").to_matchable(),
1221                            Ref::keyword("DECIMAL").to_matchable(),
1222                            Ref::keyword("DEC").to_matchable(),
1223                            Ref::keyword("NUMERIC").to_matchable(),
1224                        ])
1225                        .to_matchable(),
1226                        Ref::new("BracketedArguments").optional().to_matchable(),
1227                    ])
1228                    .to_matchable(),
1229                    Ref::keyword("BINARY").to_matchable(),
1230                    Ref::keyword("INTERVAL").to_matchable(),
1231                    Ref::keyword("VARIANT").to_matchable(),
1232                ])
1233                .to_matchable()
1234            })
1235            .to_matchable()
1236            .into(),
1237        ),
1238        (
1239            "ArrayTypeSegment".into(),
1240            hive_dialect.grammar("ArrayTypeSegment").into(),
1241        ),
1242        (
1243            "StructTypeSegment".into(),
1244            hive_dialect.grammar("StructTypeSegment").into(),
1245        ),
1246        (
1247            "StructTypeSchemaSegment".into(),
1248            hive_dialect.grammar("StructTypeSchemaSegment").into(),
1249        ),
1250    ]);
1251
1252    sparksql_dialect.add([
1253        (
1254            "SemiStructuredAccessorSegment".into(),
1255            NodeMatcher::new(SyntaxKind::SemiStructuredExpression, |_| {
1256                Sequence::new(vec![
1257                    Ref::new("ColonSegment").to_matchable(),
1258                    one_of(vec![
1259                        Ref::new("NakedSemiStructuredElementSegment").to_matchable(),
1260                        Bracketed::new(vec![
1261                            Ref::new("QuotedSemiStructuredElementSegment").to_matchable(),
1262                        ])
1263                        .config(|config| {
1264                            config.bracket_type = "square";
1265                        })
1266                        .to_matchable(),
1267                    ])
1268                    .to_matchable(),
1269                    Ref::new("ArrayAccessorSegment").optional().to_matchable(),
1270                    AnyNumberOf::new(vec![
1271                        Sequence::new(vec![
1272                            one_of(vec![
1273                                Ref::new("DotSegment").to_matchable(),
1274                                Ref::new("ColonSegment").to_matchable(),
1275                            ])
1276                            .to_matchable(),
1277                            one_of(vec![
1278                                Ref::new("NakedSemiStructuredElementSegment").to_matchable(),
1279                                Bracketed::new(vec![
1280                                    Ref::new("QuotedSemiStructuredElementSegment").to_matchable(),
1281                                ])
1282                                .config(|config| {
1283                                    config.bracket_type = "square";
1284                                })
1285                                .to_matchable(),
1286                            ])
1287                            .to_matchable(),
1288                        ])
1289                        .to_matchable(),
1290                        Ref::new("ArrayAccessorSegment").optional().to_matchable(),
1291                    ])
1292                    .to_matchable(),
1293                ])
1294                .to_matchable()
1295            })
1296            .to_matchable()
1297            .into(),
1298        ),
1299        (
1300            "DatatypeSegment".into(),
1301            NodeMatcher::new(SyntaxKind::DataType, |_| {
1302                one_of(vec![
1303                    Ref::new("PrimitiveTypeSegment").to_matchable(),
1304                    Ref::new("ArrayTypeSegment").to_matchable(),
1305                    Sequence::new(vec![
1306                        Ref::keyword("MAP").to_matchable(),
1307                        Bracketed::new(vec![
1308                            Sequence::new(vec![
1309                                Ref::new("DatatypeSegment").to_matchable(),
1310                                Ref::new("CommaSegment").to_matchable(),
1311                                Ref::new("DatatypeSegment").to_matchable(),
1312                            ])
1313                            .to_matchable(),
1314                        ])
1315                        .config(|config| {
1316                            config.bracket_pairs_set = "angle_bracket_pairs";
1317                            config.bracket_type = "angle";
1318                        })
1319                        .to_matchable(),
1320                    ])
1321                    .to_matchable(),
1322                    Ref::new("StructTypeSegment").to_matchable(),
1323                ])
1324                .to_matchable()
1325            })
1326            .to_matchable()
1327            .into(),
1328        ),
1329        (
1330            // An `ALTER DATABASE/SCHEMA` statement.
1331            // http://spark.apache.org/docs/latest/sql-ref-syntax-ddl-alter-database.html
1332            "AlterDatabaseStatementSegment".into(),
1333            NodeMatcher::new(SyntaxKind::AlterDatabaseStatement, |_| {
1334                Sequence::new(vec![
1335                    Ref::keyword("ALTER").to_matchable(),
1336                    one_of(vec![
1337                        Ref::keyword("DATABASE").to_matchable(),
1338                        Ref::keyword("SCHEMA").to_matchable(),
1339                    ])
1340                    .to_matchable(),
1341                    Ref::new("DatabaseReferenceSegment").to_matchable(),
1342                    Ref::keyword("SET").to_matchable(),
1343                    one_of(vec![
1344                        Ref::new("DatabasePropertiesGrammar").to_matchable(),
1345                        Ref::new("LocationGrammar").to_matchable(),
1346                    ])
1347                    .to_matchable(),
1348                ])
1349                .to_matchable()
1350            })
1351            .to_matchable()
1352            .into(),
1353        ),
1354        (
1355            // A `SET VARIABLE` statement used to set session variables.
1356            // https://spark.apache.org/docs/4.0.0-preview2/sql-ref-syntax-aux-set-var.html
1357            "SetVariableStatementSegment".into(),
1358            Sequence::new(vec![
1359                Ref::keyword("SET").to_matchable(),
1360                one_of(vec![
1361                    Ref::keyword("VAR").to_matchable(),
1362                    Ref::keyword("VARIABLE").to_matchable(),
1363                ])
1364                .to_matchable(),
1365                one_of(vec![
1366                    Bracketed::new(vec![
1367                        Delimited::new(vec![Ref::new("SingleIdentifierGrammar").to_matchable()])
1368                            .to_matchable(),
1369                    ])
1370                    .to_matchable(),
1371                    Delimited::new(vec![Ref::new("SingleIdentifierGrammar").to_matchable()])
1372                        .to_matchable(),
1373                ])
1374                .to_matchable(),
1375                Ref::new("EqualsSegment").to_matchable(),
1376                one_of(vec![
1377                    Ref::keyword("DEFAULT").to_matchable(),
1378                    Ref::new("ExpressionSegment").to_matchable(),
1379                    Bracketed::new(vec![Ref::new("ExpressionSegment").to_matchable()])
1380                        .to_matchable(),
1381                ])
1382                .to_matchable(),
1383            ])
1384            .allow_gaps(true)
1385            .to_matchable()
1386            .into(),
1387        ),
1388    ]);
1389
1390    sparksql_dialect.replace_grammar(
1391        "AlterTableStatementSegment",
1392        Sequence::new(vec![
1393            Ref::keyword("ALTER").to_matchable(),
1394            Ref::keyword("TABLE").to_matchable(),
1395            Ref::new("TableReferenceSegment").to_matchable(),
1396            MetaSegment::indent().to_matchable(),
1397            one_of(vec![
1398                Sequence::new(vec![
1399                    Ref::keyword("RENAME").to_matchable(),
1400                    Ref::keyword("TO").to_matchable(),
1401                    Ref::new("TableReferenceSegment").to_matchable(),
1402                ])
1403                .to_matchable(),
1404                Sequence::new(vec![
1405                    Ref::new("PartitionSpecGrammar").to_matchable(),
1406                    Ref::keyword("RENAME").to_matchable(),
1407                    Ref::keyword("TO").to_matchable(),
1408                    Ref::new("PartitionSpecGrammar").to_matchable(),
1409                ])
1410                .to_matchable(),
1411                Sequence::new(vec![
1412                    Ref::keyword("RENAME").to_matchable(),
1413                    Ref::keyword("COLUMN").to_matchable(),
1414                    Ref::new("ColumnReferenceSegment").to_matchable(),
1415                    Ref::keyword("TO").to_matchable(),
1416                    Ref::new("ColumnReferenceSegment").to_matchable(),
1417                ])
1418                .to_matchable(),
1419                Sequence::new(vec![
1420                    Ref::keyword("ADD").to_matchable(),
1421                    one_of(vec![
1422                        Ref::keyword("COLUMNS").to_matchable(),
1423                        Ref::keyword("COLUMN").to_matchable(),
1424                    ])
1425                    .to_matchable(),
1426                    MetaSegment::indent().to_matchable(),
1427                    optionally_bracketed(vec![
1428                        Delimited::new(vec![
1429                            Sequence::new(vec![
1430                                Ref::new("ColumnFieldDefinitionSegment").to_matchable(),
1431                                one_of(vec![
1432                                    Ref::keyword("FIRST").to_matchable(),
1433                                    Sequence::new(vec![
1434                                        Ref::keyword("AFTER").to_matchable(),
1435                                        Ref::new("ColumnReferenceSegment").to_matchable(),
1436                                    ])
1437                                    .to_matchable(),
1438                                ])
1439                                .config(|config| {
1440                                    config.optional();
1441                                })
1442                                .to_matchable(),
1443                            ])
1444                            .to_matchable(),
1445                        ])
1446                        .to_matchable(),
1447                    ])
1448                    .to_matchable(),
1449                    MetaSegment::dedent().to_matchable(),
1450                ])
1451                .to_matchable(),
1452                Sequence::new(vec![
1453                    one_of(vec![
1454                        Ref::keyword("ALTER").to_matchable(),
1455                        Ref::keyword("CHANGE").to_matchable(),
1456                    ])
1457                    .to_matchable(),
1458                    Ref::keyword("COLUMN").optional().to_matchable(),
1459                    MetaSegment::indent().to_matchable(),
1460                    AnyNumberOf::new(vec![
1461                        Ref::new("ColumnReferenceSegment")
1462                            .exclude(one_of(vec![
1463                                Ref::keyword("COMMENT").to_matchable(),
1464                                Ref::keyword("TYPE").to_matchable(),
1465                                Ref::new("DatatypeSegment").to_matchable(),
1466                                Ref::keyword("FIRST").to_matchable(),
1467                                Ref::keyword("AFTER").to_matchable(),
1468                                Ref::keyword("SET").to_matchable(),
1469                                Ref::keyword("DROP").to_matchable(),
1470                            ]))
1471                            .config(|config| {
1472                                config.exclude = one_of(vec![
1473                                    Ref::keyword("COMMENT").to_matchable(),
1474                                    Ref::keyword("TYPE").to_matchable(),
1475                                    Ref::new("DatatypeSegment").to_matchable(),
1476                                    Ref::keyword("FIRST").to_matchable(),
1477                                    Ref::keyword("AFTER").to_matchable(),
1478                                    Ref::keyword("SET").to_matchable(),
1479                                    Ref::keyword("DROP").to_matchable(),
1480                                ])
1481                                .to_matchable()
1482                                .into();
1483                            })
1484                            .to_matchable(),
1485                    ])
1486                    .config(|config| {
1487                        config.max_times = Some(2);
1488                    })
1489                    .to_matchable(),
1490                    Ref::keyword("TYPE").optional().to_matchable(),
1491                    Ref::new("DatatypeSegment").optional().to_matchable(),
1492                    Ref::new("CommentGrammar").optional().to_matchable(),
1493                    one_of(vec![
1494                        Ref::keyword("FIRST").to_matchable(),
1495                        Sequence::new(vec![
1496                            Ref::keyword("AFTER").to_matchable(),
1497                            Ref::new("ColumnReferenceSegment").to_matchable(),
1498                        ])
1499                        .to_matchable(),
1500                    ])
1501                    .config(|config| {
1502                        config.optional();
1503                    })
1504                    .to_matchable(),
1505                    Sequence::new(vec![
1506                        one_of(vec![
1507                            Ref::keyword("SET").to_matchable(),
1508                            Ref::keyword("DROP").to_matchable(),
1509                        ])
1510                        .to_matchable(),
1511                        Ref::keyword("NOT").to_matchable(),
1512                        Ref::keyword("NULL").to_matchable(),
1513                    ])
1514                    .config(|config| {
1515                        config.optional();
1516                    })
1517                    .to_matchable(),
1518                    MetaSegment::dedent().to_matchable(),
1519                ])
1520                .to_matchable(),
1521                Sequence::new(vec![
1522                    Ref::keyword("REPLACE").to_matchable(),
1523                    Ref::keyword("COLUMNS").to_matchable(),
1524                    Bracketed::new(vec![
1525                        Delimited::new(vec![
1526                            Sequence::new(vec![
1527                                Ref::new("ColumnDefinitionSegment").to_matchable(),
1528                                Ref::new("CommentGrammar").optional().to_matchable(),
1529                            ])
1530                            .to_matchable(),
1531                        ])
1532                        .to_matchable(),
1533                    ])
1534                    .to_matchable(),
1535                ])
1536                .to_matchable(),
1537                Sequence::new(vec![
1538                    Ref::keyword("DROP").to_matchable(),
1539                    one_of(vec![
1540                        Sequence::new(vec![
1541                            Ref::keyword("COLUMN").to_matchable(),
1542                            Ref::new("ColumnReferenceSegment").to_matchable(),
1543                        ])
1544                        .to_matchable(),
1545                        Sequence::new(vec![
1546                            Ref::keyword("COLUMNS").to_matchable(),
1547                            Bracketed::new(vec![
1548                                Delimited::new(vec![
1549                                    AnyNumberOf::new(vec![
1550                                        Ref::new("ColumnReferenceSegment").to_matchable(),
1551                                    ])
1552                                    .to_matchable(),
1553                                ])
1554                                .to_matchable(),
1555                            ])
1556                            .to_matchable(),
1557                        ])
1558                        .to_matchable(),
1559                    ])
1560                    .to_matchable(),
1561                ])
1562                .to_matchable(),
1563                Sequence::new(vec![
1564                    Ref::keyword("ADD").to_matchable(),
1565                    Ref::new("IfNotExistsGrammar").optional().to_matchable(),
1566                    AnyNumberOf::new(vec![
1567                        Ref::new("PartitionSpecGrammar").to_matchable(),
1568                        Ref::new("PartitionFieldGrammar").to_matchable(),
1569                    ])
1570                    .config(|config| {
1571                        config.min_times = 1;
1572                    })
1573                    .to_matchable(),
1574                ])
1575                .to_matchable(),
1576                Sequence::new(vec![
1577                    Ref::keyword("DROP").to_matchable(),
1578                    Ref::new("IfExistsGrammar").optional().to_matchable(),
1579                    one_of(vec![
1580                        Ref::new("PartitionSpecGrammar").to_matchable(),
1581                        Ref::new("PartitionFieldGrammar").to_matchable(),
1582                    ])
1583                    .to_matchable(),
1584                    Sequence::new(vec![Ref::keyword("PURGE").to_matchable()])
1585                        .config(|config| {
1586                            config.optional();
1587                        })
1588                        .to_matchable(),
1589                ])
1590                .to_matchable(),
1591                Sequence::new(vec![
1592                    Ref::keyword("REPLACE").to_matchable(),
1593                    Ref::new("PartitionFieldGrammar").to_matchable(),
1594                ])
1595                .to_matchable(),
1596                Sequence::new(vec![
1597                    Ref::keyword("RECOVER").to_matchable(),
1598                    Ref::keyword("PARTITIONS").to_matchable(),
1599                ])
1600                .to_matchable(),
1601                Sequence::new(vec![
1602                    Ref::keyword("SET").to_matchable(),
1603                    Ref::new("TablePropertiesGrammar").to_matchable(),
1604                ])
1605                .to_matchable(),
1606                Ref::new("UnsetTablePropertiesGrammar").to_matchable(),
1607                Sequence::new(vec![
1608                    Ref::new("PartitionSpecGrammar").optional().to_matchable(),
1609                    Ref::keyword("SET").to_matchable(),
1610                    one_of(vec![
1611                        Sequence::new(vec![
1612                            Ref::keyword("SERDEPROPERTIES").to_matchable(),
1613                            Ref::new("BracketedPropertyListGrammar").to_matchable(),
1614                        ])
1615                        .to_matchable(),
1616                        Sequence::new(vec![
1617                            Ref::keyword("SERDE").to_matchable(),
1618                            Ref::new("QuotedLiteralSegment").to_matchable(),
1619                            Ref::new("SerdePropertiesGrammar").optional().to_matchable(),
1620                        ])
1621                        .to_matchable(),
1622                    ])
1623                    .to_matchable(),
1624                ])
1625                .to_matchable(),
1626                Sequence::new(vec![
1627                    Ref::new("PartitionSpecGrammar").optional().to_matchable(),
1628                    Ref::keyword("SET").to_matchable(),
1629                    Ref::keyword("FILEFORMAT").to_matchable(),
1630                    Ref::new("DataSourceFormatSegment").to_matchable(),
1631                ])
1632                .to_matchable(),
1633                Sequence::new(vec![
1634                    Ref::new("PartitionSpecGrammar").optional().to_matchable(),
1635                    Ref::keyword("SET").to_matchable(),
1636                    Ref::new("LocationGrammar").to_matchable(),
1637                ])
1638                .to_matchable(),
1639                Sequence::new(vec![
1640                    MetaSegment::indent().to_matchable(),
1641                    one_of(vec![
1642                        Ref::keyword("ADD").to_matchable(),
1643                        Ref::keyword("DROP").to_matchable(),
1644                    ])
1645                    .to_matchable(),
1646                    Ref::keyword("CONSTRAINT").to_matchable(),
1647                    Ref::new("ColumnReferenceSegment")
1648                        .exclude(Ref::keyword("CHECK"))
1649                        .config(|config| {
1650                            config.exclude = Ref::keyword("CHECK").to_matchable().into();
1651                        })
1652                        .to_matchable(),
1653                    Ref::keyword("CHECK").optional().to_matchable(),
1654                    Bracketed::new(vec![Ref::new("ExpressionSegment").to_matchable()])
1655                        .config(|config| {
1656                            config.optional();
1657                        })
1658                        .to_matchable(),
1659                    MetaSegment::dedent().to_matchable(),
1660                ])
1661                .to_matchable(),
1662                Sequence::new(vec![
1663                    Ref::keyword("WRITE").to_matchable(),
1664                    AnyNumberOf::new(vec![
1665                        Sequence::new(vec![
1666                            Ref::keyword("DISTRIBUTED").to_matchable(),
1667                            Ref::keyword("BY").to_matchable(),
1668                            Ref::keyword("PARTITION").to_matchable(),
1669                        ])
1670                        .config(|config| {
1671                            config.optional();
1672                        })
1673                        .to_matchable(),
1674                        Sequence::new(vec![
1675                            Ref::keyword("LOCALLY").optional().to_matchable(),
1676                            Ref::keyword("ORDERED").to_matchable(),
1677                            Ref::keyword("BY").to_matchable(),
1678                            MetaSegment::indent().to_matchable(),
1679                            Delimited::new(vec![
1680                                Sequence::new(vec![
1681                                    Ref::new("ColumnReferenceSegment").to_matchable(),
1682                                    one_of(vec![
1683                                        Ref::keyword("ASC").to_matchable(),
1684                                        Ref::keyword("DESC").to_matchable(),
1685                                    ])
1686                                    .config(|config| {
1687                                        config.optional();
1688                                    })
1689                                    .to_matchable(),
1690                                    Sequence::new(vec![
1691                                        Ref::keyword("NULLS").to_matchable(),
1692                                        one_of(vec![
1693                                            Ref::keyword("FIRST").to_matchable(),
1694                                            Ref::keyword("LAST").to_matchable(),
1695                                        ])
1696                                        .to_matchable(),
1697                                    ])
1698                                    .config(|config| {
1699                                        config.optional();
1700                                    })
1701                                    .to_matchable(),
1702                                ])
1703                                .to_matchable(),
1704                            ])
1705                            .config(|config| {
1706                                config.optional();
1707                            })
1708                            .to_matchable(),
1709                            MetaSegment::dedent().to_matchable(),
1710                        ])
1711                        .config(|config| {
1712                            config.optional();
1713                        })
1714                        .to_matchable(),
1715                    ])
1716                    .config(|config| {
1717                        config.min_times = 1;
1718                        config.max_times_per_element = Some(1);
1719                    })
1720                    .to_matchable(),
1721                ])
1722                .to_matchable(),
1723                Sequence::new(vec![
1724                    Ref::keyword("SET").to_matchable(),
1725                    Ref::keyword("IDENTIFIER").to_matchable(),
1726                    Ref::keyword("FIELDS").to_matchable(),
1727                    MetaSegment::indent().to_matchable(),
1728                    Delimited::new(vec![
1729                        Sequence::new(vec![Ref::new("ColumnReferenceSegment").to_matchable()])
1730                            .to_matchable(),
1731                    ])
1732                    .to_matchable(),
1733                    MetaSegment::dedent().to_matchable(),
1734                ])
1735                .to_matchable(),
1736                Sequence::new(vec![
1737                    Ref::keyword("DROP").to_matchable(),
1738                    Ref::keyword("IDENTIFIER").to_matchable(),
1739                    Ref::keyword("FIELDS").to_matchable(),
1740                    MetaSegment::indent().to_matchable(),
1741                    Delimited::new(vec![
1742                        Sequence::new(vec![Ref::new("ColumnReferenceSegment").to_matchable()])
1743                            .to_matchable(),
1744                    ])
1745                    .to_matchable(),
1746                    MetaSegment::dedent().to_matchable(),
1747                ])
1748                .to_matchable(),
1749            ])
1750            .to_matchable(),
1751            MetaSegment::dedent().to_matchable(),
1752        ])
1753        .to_matchable(),
1754    );
1755
1756    sparksql_dialect.add([(
1757        "ColumnFieldDefinitionSegment".into(),
1758        NodeMatcher::new(SyntaxKind::ColumnDefinition, |_| {
1759            Sequence::new(vec![
1760                Ref::new("ColumnReferenceSegment").to_matchable(),
1761                Ref::new("DatatypeSegment").to_matchable(),
1762                Bracketed::new(vec![Anything::new().to_matchable()])
1763                    .config(|config| {
1764                        config.optional();
1765                    })
1766                    .to_matchable(),
1767                AnyNumberOf::new(vec![
1768                    Ref::new("ColumnConstraintSegment")
1769                        .optional()
1770                        .to_matchable(),
1771                ])
1772                .to_matchable(),
1773            ])
1774            .to_matchable()
1775        })
1776        .to_matchable()
1777        .into(),
1778    )]);
1779
1780    sparksql_dialect.add([(
1781        "AlterViewStatementSegment".into(),
1782        NodeMatcher::new(SyntaxKind::AlterViewStatement, |_| {
1783            Sequence::new(vec![
1784                Ref::keyword("ALTER").to_matchable(),
1785                Ref::keyword("VIEW").to_matchable(),
1786                Ref::new("TableReferenceSegment").to_matchable(),
1787                one_of(vec![
1788                    Sequence::new(vec![
1789                        Ref::keyword("RENAME").to_matchable(),
1790                        Ref::keyword("TO").to_matchable(),
1791                        Ref::new("TableReferenceSegment").to_matchable(),
1792                    ])
1793                    .to_matchable(),
1794                    Sequence::new(vec![
1795                        Ref::keyword("SET").to_matchable(),
1796                        Ref::new("TablePropertiesGrammar").to_matchable(),
1797                    ])
1798                    .to_matchable(),
1799                    Ref::new("UnsetTablePropertiesGrammar").to_matchable(),
1800                    Sequence::new(vec![
1801                        Ref::keyword("AS").to_matchable(),
1802                        optionally_bracketed(vec![
1803                            Ref::new("SelectStatementSegment").to_matchable(),
1804                        ])
1805                        .to_matchable(),
1806                    ])
1807                    .to_matchable(),
1808                ])
1809                .to_matchable(),
1810            ])
1811            .to_matchable()
1812        })
1813        .to_matchable()
1814        .into(),
1815    )]);
1816
1817    sparksql_dialect.add([(
1818        "JoinLikeClauseGrammar".into(),
1819        Sequence::new(vec![
1820            one_of(vec![
1821                Ref::new("PivotClauseSegment").to_matchable(),
1822                Ref::new("UnpivotClauseSegment").to_matchable(),
1823                Ref::new("LateralViewClauseSegment").to_matchable(),
1824            ])
1825            .to_matchable(),
1826            Ref::new("AliasExpressionSegment").optional().to_matchable(),
1827        ])
1828        .to_matchable()
1829        .into(),
1830    )]);
1831
1832    sparksql_dialect.add([
1833        // An Unpivot segment.
1834        // https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select-unpivot.html
1835        (
1836            "UnpivotClauseSegment".into(),
1837            NodeMatcher::new(SyntaxKind::UnpivotClause, |_| {
1838                Sequence::new(vec![
1839                    Ref::keyword("UNPIVOT").to_matchable(),
1840                    Sequence::new(vec![
1841                        one_of(vec![
1842                            Ref::keyword("INCLUDE").to_matchable(),
1843                            Ref::keyword("EXCLUDE").to_matchable(),
1844                        ])
1845                        .to_matchable(),
1846                        Ref::keyword("NULLS").to_matchable(),
1847                    ])
1848                    .config(|config| {
1849                        config.optional();
1850                    })
1851                    .to_matchable(),
1852                    MetaSegment::indent().to_matchable(),
1853                    Bracketed::new(vec![
1854                        one_of(vec![
1855                            Ref::new("SingleValueColumnUnpivotSegment").to_matchable(),
1856                            Ref::new("MultiValueColumnUnpivotSegment").to_matchable(),
1857                        ])
1858                        .to_matchable(),
1859                    ])
1860                    .to_matchable(),
1861                    MetaSegment::dedent().to_matchable(),
1862                ])
1863                .to_matchable()
1864            })
1865            .to_matchable()
1866            .into(),
1867        ),
1868        (
1869            "SingleValueColumnUnpivotSegment".into(),
1870            Sequence::new(vec![
1871                Ref::new("SingleIdentifierGrammar").to_matchable(),
1872                Ref::keyword("FOR").to_matchable(),
1873                Ref::new("SingleIdentifierGrammar").to_matchable(),
1874                Ref::keyword("IN").to_matchable(),
1875                Bracketed::new(vec![
1876                    MetaSegment::indent().to_matchable(),
1877                    Delimited::new(vec![
1878                        Sequence::new(vec![
1879                            Ref::new("ColumnReferenceSegment").to_matchable(),
1880                            Ref::new("AliasExpressionSegment").optional().to_matchable(),
1881                        ])
1882                        .to_matchable(),
1883                    ])
1884                    .to_matchable(),
1885                    MetaSegment::dedent().to_matchable(),
1886                ])
1887                .config(|config| {
1888                    config.parse_mode = ParseMode::Greedy;
1889                })
1890                .to_matchable(),
1891            ])
1892            .to_matchable()
1893            .into(),
1894        ),
1895        (
1896            "MultiValueColumnUnpivotSegment".into(),
1897            Sequence::new(vec![
1898                Bracketed::new(vec![
1899                    Delimited::new(vec![Ref::new("SingleIdentifierGrammar").to_matchable()])
1900                        .to_matchable(),
1901                ])
1902                .to_matchable(),
1903                MetaSegment::indent().to_matchable(),
1904                Ref::keyword("FOR").to_matchable(),
1905                Ref::new("SingleIdentifierGrammar").to_matchable(),
1906                Ref::keyword("IN").to_matchable(),
1907                Bracketed::new(vec![
1908                    MetaSegment::indent().to_matchable(),
1909                    Delimited::new(vec![
1910                        Sequence::new(vec![
1911                            Bracketed::new(vec![
1912                                MetaSegment::indent().to_matchable(),
1913                                Delimited::new(vec![
1914                                    Ref::new("ColumnReferenceSegment").to_matchable(),
1915                                ])
1916                                .to_matchable(),
1917                            ])
1918                            .to_matchable(),
1919                            Ref::new("AliasExpressionSegment").optional().to_matchable(),
1920                        ])
1921                        .to_matchable(),
1922                    ])
1923                    .to_matchable(),
1924                ])
1925                .config(|config| {
1926                    config.parse_mode = ParseMode::Greedy;
1927                })
1928                .to_matchable(),
1929                MetaSegment::dedent().to_matchable(),
1930            ])
1931            .to_matchable()
1932            .into(),
1933        ),
1934    ]);
1935
1936    sparksql_dialect.replace_grammar(
1937        "CreateDatabaseStatementSegment",
1938        Sequence::new(vec![
1939            Ref::keyword("CREATE").to_matchable(),
1940            one_of(vec![
1941                Ref::keyword("DATABASE").to_matchable(),
1942                Ref::keyword("SCHEMA").to_matchable(),
1943            ])
1944            .to_matchable(),
1945            Ref::new("IfNotExistsGrammar").optional().to_matchable(),
1946            Ref::new("DatabaseReferenceSegment").to_matchable(),
1947            Ref::new("CommentGrammar").optional().to_matchable(),
1948            Ref::new("LocationGrammar").optional().to_matchable(),
1949            Sequence::new(vec![
1950                Ref::keyword("WITH").to_matchable(),
1951                Ref::keyword("DBPROPERTIES").to_matchable(),
1952                Ref::new("BracketedPropertyListGrammar").to_matchable(),
1953            ])
1954            .config(|config| {
1955                config.optional();
1956            })
1957            .to_matchable(),
1958        ])
1959        .to_matchable(),
1960    );
1961
1962    sparksql_dialect.replace_grammar(
1963        "CreateFunctionStatementSegment",
1964        Sequence::new(vec![
1965            Ref::keyword("CREATE").to_matchable(),
1966            Sequence::new(vec![
1967                Ref::keyword("OR").to_matchable(),
1968                Ref::keyword("REPLACE").to_matchable(),
1969            ])
1970            .config(|config| {
1971                config.optional();
1972            })
1973            .to_matchable(),
1974            Ref::new("TemporaryGrammar").optional().to_matchable(),
1975            Ref::keyword("FUNCTION").to_matchable(),
1976            Ref::new("IfNotExistsGrammar").optional().to_matchable(),
1977            Ref::new("FunctionNameIdentifierSegment").to_matchable(),
1978            Ref::keyword("AS").to_matchable(),
1979            Ref::new("QuotedLiteralSegment").to_matchable(),
1980            Ref::new("ResourceLocationGrammar")
1981                .optional()
1982                .to_matchable(),
1983        ])
1984        .to_matchable(),
1985    );
1986
1987    sparksql_dialect.replace_grammar(
1988        "CreateTableStatementSegment",
1989        Sequence::new(vec![
1990            Ref::keyword("CREATE").to_matchable(),
1991            Ref::new("TableDefinitionSegment").to_matchable(),
1992        ])
1993        .to_matchable(),
1994    );
1995
1996    sparksql_dialect.add([(
1997        "CreateHiveFormatTableStatementSegment".into(),
1998        hive_dialect.grammar("CreateTableStatementSegment").into(),
1999    )]);
2000
2001    // SparkSQL supports CTEs with DML statements (INSERT, UPDATE, DELETE, etc.)
2002    // We add these to NonWithSelectableGrammar so WithCompoundStatementSegment can use them
2003    sparksql_dialect.add([(
2004        "NonWithSelectableGrammar".into(),
2005        one_of(vec![
2006            Ref::new("SetExpressionSegment").to_matchable(),
2007            optionally_bracketed(vec![Ref::new("SelectStatementSegment").to_matchable()])
2008                .to_matchable(),
2009            Ref::new("NonSetSelectableGrammar").to_matchable(),
2010            Ref::new("UpdateStatementSegment").to_matchable(),
2011            Ref::new("InsertStatementSegment").to_matchable(),
2012            Ref::new("DeleteStatementSegment").to_matchable(),
2013            Ref::new("MergeStatementSegment").to_matchable(),
2014            Ref::new("InsertOverwriteDirectorySegment").to_matchable(),
2015        ])
2016        .to_matchable()
2017        .into(),
2018    )]);
2019
2020    sparksql_dialect.replace_grammar(
2021        "CreateViewStatementSegment",
2022        Sequence::new(vec![
2023            Ref::keyword("CREATE").to_matchable(),
2024            one_of(vec![
2025                Ref::new("OrReplaceGrammar").to_matchable(),
2026                Ref::new("OrRefreshGrammar").to_matchable(),
2027            ])
2028            .config(|config| {
2029                config.optional();
2030            })
2031            .to_matchable(),
2032            Ref::new("TemporaryGrammar").optional().to_matchable(),
2033            Ref::keyword("STREAMING").optional().to_matchable(),
2034            Ref::keyword("LIVE").optional().to_matchable(),
2035            Ref::keyword("MATERIALIZED").optional().to_matchable(),
2036            Ref::keyword("VIEW").to_matchable(),
2037            Ref::new("IfNotExistsGrammar").optional().to_matchable(),
2038            Ref::new("TableReferenceSegment").to_matchable(),
2039            Sequence::new(vec![
2040                Bracketed::new(vec![
2041                    Delimited::new(vec![
2042                        Sequence::new(vec![
2043                            Ref::new("ColumnReferenceSegment").to_matchable(),
2044                            Ref::new("CommentGrammar").optional().to_matchable(),
2045                        ])
2046                        .to_matchable(),
2047                        Ref::new("ConstraintStatementSegment")
2048                            .optional()
2049                            .to_matchable(),
2050                    ])
2051                    .to_matchable(),
2052                ])
2053                .to_matchable(),
2054            ])
2055            .config(|config| {
2056                config.optional();
2057            })
2058            .to_matchable(),
2059            Sequence::new(vec![
2060                Ref::keyword("USING").to_matchable(),
2061                Ref::new("DataSourceFormatSegment").to_matchable(),
2062            ])
2063            .config(|config| {
2064                config.optional();
2065            })
2066            .to_matchable(),
2067            Ref::new("OptionsGrammar").optional().to_matchable(),
2068            Ref::new("CommentGrammar").optional().to_matchable(),
2069            Ref::new("TablePropertiesGrammar").optional().to_matchable(),
2070            Sequence::new(vec![
2071                Ref::keyword("AS").to_matchable(),
2072                optionally_bracketed(vec![Ref::new("SelectableGrammar").to_matchable()])
2073                    .to_matchable(),
2074            ])
2075            .config(|config| {
2076                config.optional();
2077            })
2078            .to_matchable(),
2079            Ref::new("WithNoSchemaBindingClauseSegment")
2080                .optional()
2081                .to_matchable(),
2082        ])
2083        .to_matchable(),
2084    );
2085    sparksql_dialect.add([
2086        (
2087            "CreateWidgetStatementSegment".into(),
2088            NodeMatcher::new(SyntaxKind::CreateWidgetStatement, |_| {
2089                Sequence::new(vec![
2090                    Ref::keyword("CREATE").to_matchable(),
2091                    Ref::keyword("WIDGET").to_matchable(),
2092                    one_of(vec![
2093                        Sequence::new(vec![
2094                            Ref::keyword("DROPDOWN").to_matchable(),
2095                            Ref::new("WidgetNameIdentifierSegment").to_matchable(),
2096                            Ref::new("WidgetDefaultGrammar").to_matchable(),
2097                            Sequence::new(vec![
2098                                Ref::keyword("CHOICES").to_matchable(),
2099                                Ref::new("SelectStatementSegment").to_matchable(),
2100                            ])
2101                            .to_matchable(),
2102                        ])
2103                        .to_matchable(),
2104                        Sequence::new(vec![
2105                            Ref::keyword("TEXT").to_matchable(),
2106                            Ref::new("WidgetNameIdentifierSegment").to_matchable(),
2107                            Ref::new("WidgetDefaultGrammar").to_matchable(),
2108                        ])
2109                        .to_matchable(),
2110                    ])
2111                    .to_matchable(),
2112                ])
2113                .to_matchable()
2114            })
2115            .to_matchable()
2116            .into(),
2117        ),
2118        (
2119            "ReplaceTableStatementSegment".into(),
2120            NodeMatcher::new(SyntaxKind::ReplaceTableStatement, |_| {
2121                Sequence::new(vec![
2122                    Ref::keyword("REPLACE").to_matchable(),
2123                    Ref::new("TableDefinitionSegment").to_matchable(),
2124                ])
2125                .to_matchable()
2126            })
2127            .to_matchable()
2128            .into(),
2129        ),
2130        (
2131            "RemoveWidgetStatementSegment".into(),
2132            NodeMatcher::new(SyntaxKind::RemoveWidgetStatement, |_| {
2133                Sequence::new(vec![
2134                    Ref::keyword("REMOVE").to_matchable(),
2135                    Ref::keyword("WIDGET").to_matchable(),
2136                    Ref::new("WidgetNameIdentifierSegment").to_matchable(),
2137                ])
2138                .to_matchable()
2139            })
2140            .to_matchable()
2141            .into(),
2142        ),
2143    ]);
2144
2145    sparksql_dialect.replace_grammar(
2146        "DropDatabaseStatementSegment",
2147        Sequence::new(vec![
2148            Ref::keyword("DROP").to_matchable(),
2149            one_of(vec![
2150                Ref::keyword("DATABASE").to_matchable(),
2151                Ref::keyword("SCHEMA").to_matchable(),
2152            ])
2153            .to_matchable(),
2154            Ref::new("IfExistsGrammar").optional().to_matchable(),
2155            Ref::new("DatabaseReferenceSegment").to_matchable(),
2156            Ref::new("DropBehaviorGrammar").optional().to_matchable(),
2157        ])
2158        .to_matchable(),
2159    );
2160    sparksql_dialect.add([(
2161        "DropFunctionStatementSegment".into(),
2162        NodeMatcher::new(SyntaxKind::DropFunctionStatement, |_| {
2163            Sequence::new(vec![
2164                Ref::keyword("DROP").to_matchable(),
2165                Ref::new("TemporaryGrammar").optional().to_matchable(),
2166                Ref::keyword("FUNCTION").to_matchable(),
2167                Ref::new("IfExistsGrammar").optional().to_matchable(),
2168                Ref::new("FunctionNameSegment").to_matchable(),
2169            ])
2170            .to_matchable()
2171        })
2172        .to_matchable()
2173        .into(),
2174    )]);
2175
2176    sparksql_dialect.add([(
2177        "MsckRepairTableStatementSegment".into(),
2178        hive_dialect
2179            .grammar("MsckRepairTableStatementSegment")
2180            .into(),
2181    )]);
2182
2183    sparksql_dialect.replace_grammar(
2184        "TruncateStatementSegment",
2185        Sequence::new(vec![
2186            Ref::keyword("TRUNCATE").to_matchable(),
2187            Ref::keyword("TABLE").to_matchable(),
2188            Ref::new("TableReferenceSegment").to_matchable(),
2189            Ref::new("PartitionSpecGrammar").optional().to_matchable(),
2190        ])
2191        .to_matchable(),
2192    );
2193    sparksql_dialect.add([
2194        (
2195            "UseDatabaseStatementSegment".into(),
2196            NodeMatcher::new(SyntaxKind::UseDatabaseStatement, |_| {
2197                Sequence::new(vec![
2198                    Ref::keyword("USE").to_matchable(),
2199                    Ref::new("DatabaseReferenceSegment").to_matchable(),
2200                ])
2201                .to_matchable()
2202            })
2203            .to_matchable()
2204            .into(),
2205        ),
2206        (
2207            "InsertBracketedColumnReferenceListGrammar".into(),
2208            Ref::new("BracketedColumnReferenceListGrammar")
2209                .to_matchable()
2210                .into(),
2211        ),
2212        (
2213            "InsertStatementSegment".into(),
2214            NodeMatcher::new(SyntaxKind::InsertStatement, |_| {
2215                Sequence::new(vec![
2216                    Ref::keyword("INSERT").to_matchable(),
2217                    one_of(vec![
2218                        Ref::keyword("INTO").to_matchable(),
2219                        Ref::keyword("OVERWRITE").to_matchable(),
2220                    ])
2221                    .to_matchable(),
2222                    Ref::keyword("TABLE").optional().to_matchable(),
2223                    Ref::new("TableReferenceSegment").to_matchable(),
2224                    Ref::new("PartitionSpecGrammar").optional().to_matchable(),
2225                    Ref::new("InsertBracketedColumnReferenceListGrammar")
2226                        .optional()
2227                        .to_matchable(),
2228                    one_of(vec![
2229                        AnyNumberOf::new(vec![Ref::new("ValuesClauseSegment").to_matchable()])
2230                            .config(|config| {
2231                                config.min_times = 1;
2232                            })
2233                            .to_matchable(),
2234                        Ref::new("SelectableGrammar").to_matchable(),
2235                        Sequence::new(vec![
2236                            Ref::keyword("TABLE").optional().to_matchable(),
2237                            Ref::new("TableReferenceSegment").to_matchable(),
2238                        ])
2239                        .to_matchable(),
2240                        Sequence::new(vec![
2241                            Ref::keyword("FROM").to_matchable(),
2242                            Ref::new("TableReferenceSegment").to_matchable(),
2243                            Ref::keyword("SELECT").to_matchable(),
2244                            Delimited::new(vec![Ref::new("ColumnReferenceSegment").to_matchable()])
2245                                .to_matchable(),
2246                            Ref::new("WhereClauseSegment").optional().to_matchable(),
2247                            Ref::new("GroupByClauseSegment").optional().to_matchable(),
2248                            Ref::new("OrderByClauseSegment").optional().to_matchable(),
2249                            Ref::new("LimitClauseSegment").optional().to_matchable(),
2250                        ])
2251                        .to_matchable(),
2252                    ])
2253                    .to_matchable(),
2254                ])
2255                .to_matchable()
2256            })
2257            .to_matchable()
2258            .into(),
2259        ),
2260        (
2261            "InsertOverwriteDirectorySegment".into(),
2262            NodeMatcher::new(SyntaxKind::InsertOverwriteDirectoryStatement, |_| {
2263                Sequence::new(vec![
2264                    Ref::keyword("INSERT").to_matchable(),
2265                    Ref::keyword("OVERWRITE").to_matchable(),
2266                    Ref::keyword("LOCAL").optional().to_matchable(),
2267                    Ref::keyword("DIRECTORY").to_matchable(),
2268                    Ref::new("QuotedLiteralSegment").optional().to_matchable(),
2269                    Ref::keyword("USING").to_matchable(),
2270                    Ref::new("DataSourceFormatSegment").to_matchable(),
2271                    Ref::new("OptionsGrammar").optional().to_matchable(),
2272                    one_of(vec![
2273                        AnyNumberOf::new(vec![Ref::new("ValuesClauseSegment").to_matchable()])
2274                            .config(|config| {
2275                                config.min_times = 1;
2276                            })
2277                            .to_matchable(),
2278                        Ref::new("SelectableGrammar").to_matchable(),
2279                    ])
2280                    .to_matchable(),
2281                ])
2282                .to_matchable()
2283            })
2284            .to_matchable()
2285            .into(),
2286        ),
2287        (
2288            "InsertOverwriteDirectoryHiveFmtSegment".into(),
2289            NodeMatcher::new(SyntaxKind::InsertOverwriteDirectoryHiveFmtStatement, |_| {
2290                Sequence::new(vec![
2291                    Ref::keyword("INSERT").to_matchable(),
2292                    Ref::keyword("OVERWRITE").to_matchable(),
2293                    Ref::keyword("LOCAL").optional().to_matchable(),
2294                    Ref::keyword("DIRECTORY").to_matchable(),
2295                    Ref::new("QuotedLiteralSegment").to_matchable(),
2296                    Ref::new("RowFormatClauseSegment").optional().to_matchable(),
2297                    Ref::new("StoredAsGrammar").optional().to_matchable(),
2298                    one_of(vec![
2299                        AnyNumberOf::new(vec![Ref::new("ValuesClauseSegment").to_matchable()])
2300                            .config(|config| {
2301                                config.min_times = 1;
2302                            })
2303                            .to_matchable(),
2304                        Ref::new("SelectableGrammar").to_matchable(),
2305                    ])
2306                    .to_matchable(),
2307                ])
2308                .to_matchable()
2309            })
2310            .to_matchable()
2311            .into(),
2312        ),
2313        (
2314            "LoadDataSegment".into(),
2315            NodeMatcher::new(SyntaxKind::LoadDataStatement, |_| {
2316                Sequence::new(vec![
2317                    Ref::keyword("LOAD").to_matchable(),
2318                    Ref::keyword("DATA").to_matchable(),
2319                    Ref::keyword("LOCAL").optional().to_matchable(),
2320                    Ref::keyword("INPATH").to_matchable(),
2321                    Ref::new("QuotedLiteralSegment").to_matchable(),
2322                    Ref::keyword("OVERWRITE").optional().to_matchable(),
2323                    Ref::keyword("INTO").to_matchable(),
2324                    Ref::keyword("TABLE").to_matchable(),
2325                    Ref::new("TableReferenceSegment").to_matchable(),
2326                    Ref::new("PartitionSpecGrammar").optional().to_matchable(),
2327                ])
2328                .to_matchable()
2329            })
2330            .to_matchable()
2331            .into(),
2332        ),
2333        (
2334            "ClusterByClauseSegment".into(),
2335            NodeMatcher::new(SyntaxKind::ClusterByClause, |_| {
2336                Sequence::new(vec![
2337                    Ref::keyword("CLUSTER").to_matchable(),
2338                    Ref::keyword("BY").to_matchable(),
2339                    MetaSegment::indent().to_matchable(),
2340                    Delimited::new(vec![
2341                        Sequence::new(vec![
2342                            one_of(vec![
2343                                Ref::new("ColumnReferenceSegment").to_matchable(),
2344                                Ref::new("NumericLiteralSegment").to_matchable(),
2345                                Ref::new("ExpressionSegment").to_matchable(),
2346                            ])
2347                            .to_matchable(),
2348                        ])
2349                        .to_matchable(),
2350                    ])
2351                    .config(|config| {
2352                        config.terminators = vec![
2353                            Ref::keyword("LIMIT").to_matchable(),
2354                            Ref::keyword("HAVING").to_matchable(),
2355                            Ref::keyword("WINDOW").to_matchable(),
2356                            Ref::new("FrameClauseUnitGrammar").to_matchable(),
2357                            Ref::keyword("SEPARATOR").to_matchable(),
2358                        ];
2359                    })
2360                    .to_matchable(),
2361                    MetaSegment::dedent().to_matchable(),
2362                ])
2363                .to_matchable()
2364            })
2365            .to_matchable()
2366            .into(),
2367        ),
2368        (
2369            "DistributeByClauseSegment".into(),
2370            NodeMatcher::new(SyntaxKind::DistributeByClause, |_| {
2371                Sequence::new(vec![
2372                    Ref::keyword("DISTRIBUTE").to_matchable(),
2373                    Ref::keyword("BY").to_matchable(),
2374                    MetaSegment::indent().to_matchable(),
2375                    Delimited::new(vec![
2376                        Sequence::new(vec![
2377                            one_of(vec![
2378                                Ref::new("ColumnReferenceSegment").to_matchable(),
2379                                Ref::new("NumericLiteralSegment").to_matchable(),
2380                                Ref::new("ExpressionSegment").to_matchable(),
2381                            ])
2382                            .to_matchable(),
2383                        ])
2384                        .to_matchable(),
2385                    ])
2386                    .config(|config| {
2387                        config.terminators = vec![
2388                            Ref::keyword("SORT").to_matchable(),
2389                            Ref::keyword("LIMIT").to_matchable(),
2390                            Ref::keyword("HAVING").to_matchable(),
2391                            Ref::keyword("WINDOW").to_matchable(),
2392                            Ref::new("FrameClauseUnitGrammar").to_matchable(),
2393                            Ref::keyword("SEPARATOR").to_matchable(),
2394                        ];
2395                    })
2396                    .to_matchable(),
2397                    MetaSegment::dedent().to_matchable(),
2398                ])
2399                .to_matchable()
2400            })
2401            .to_matchable()
2402            .into(),
2403        ),
2404        (
2405            "HintFunctionSegment".into(),
2406            NodeMatcher::new(SyntaxKind::HintFunction, |_| {
2407                Sequence::new(vec![
2408                    Ref::new("FunctionNameSegment").to_matchable(),
2409                    Bracketed::new(vec![
2410                        Delimited::new(vec![
2411                            AnyNumberOf::new(vec![
2412                                Ref::new("SingleIdentifierGrammar").to_matchable(),
2413                                Ref::new("NumericLiteralSegment").to_matchable(),
2414                                Ref::new("TableReferenceSegment").to_matchable(),
2415                                Ref::new("ColumnReferenceSegment").to_matchable(),
2416                            ])
2417                            .config(|config| {
2418                                config.min_times = 1;
2419                            })
2420                            .to_matchable(),
2421                        ])
2422                        .to_matchable(),
2423                    ])
2424                    .config(|config| {
2425                        config.optional();
2426                    })
2427                    .to_matchable(),
2428                ])
2429                .to_matchable()
2430            })
2431            .to_matchable()
2432            .into(),
2433        ),
2434        (
2435            "SelectHintSegment".into(),
2436            NodeMatcher::new(SyntaxKind::SelectHint, |_| {
2437                Sequence::new(vec![
2438                    Sequence::new(vec![
2439                        Ref::new("StartHintSegment").to_matchable(),
2440                        Delimited::new(vec![
2441                            AnyNumberOf::new(vec![Ref::new("HintFunctionSegment").to_matchable()])
2442                                .config(|config| {
2443                                    config.min_times = 1;
2444                                })
2445                                .to_matchable(),
2446                        ])
2447                        .config(|config| {
2448                            config.terminators = vec![Ref::new("EndHintSegment").to_matchable()];
2449                        })
2450                        .to_matchable(),
2451                        Ref::new("EndHintSegment").to_matchable(),
2452                    ])
2453                    .to_matchable(),
2454                ])
2455                .to_matchable()
2456            })
2457            .to_matchable()
2458            .into(),
2459        ),
2460    ]);
2461
2462    sparksql_dialect.replace_grammar(
2463        "LimitClauseSegment",
2464        Sequence::new(vec![
2465            Ref::keyword("LIMIT").to_matchable(),
2466            MetaSegment::indent().to_matchable(),
2467            one_of(vec![
2468                Ref::new("NumericLiteralSegment").to_matchable(),
2469                Ref::keyword("ALL").to_matchable(),
2470                Ref::new("FunctionSegment").to_matchable(),
2471            ])
2472            .to_matchable(),
2473            MetaSegment::dedent().to_matchable(),
2474        ])
2475        .to_matchable(),
2476    );
2477
2478    sparksql_dialect.replace_grammar(
2479        "SetOperatorSegment",
2480        one_of(vec![
2481            Sequence::new(vec![
2482                one_of(vec![
2483                    Ref::keyword("EXCEPT").to_matchable(),
2484                    Ref::keyword("MINUS").to_matchable(),
2485                ])
2486                .to_matchable(),
2487                Ref::keyword("ALL").optional().to_matchable(),
2488            ])
2489            .to_matchable(),
2490            Sequence::new(vec![
2491                one_of(vec![
2492                    Ref::keyword("UNION").to_matchable(),
2493                    Ref::keyword("INTERSECT").to_matchable(),
2494                ])
2495                .to_matchable(),
2496                one_of(vec![
2497                    Ref::keyword("DISTINCT").to_matchable(),
2498                    Ref::keyword("ALL").to_matchable(),
2499                ])
2500                .config(|config| {
2501                    config.optional();
2502                })
2503                .to_matchable(),
2504            ])
2505            .to_matchable(),
2506        ])
2507        .config(|config| {
2508            config.exclude = Some(
2509                Sequence::new(vec![
2510                    Ref::keyword("EXCEPT").to_matchable(),
2511                    Bracketed::new(vec![Anything::new().to_matchable()]).to_matchable(),
2512                ])
2513                .to_matchable(),
2514            )
2515        })
2516        .to_matchable(),
2517    );
2518
2519    sparksql_dialect.replace_grammar(
2520        "SelectClauseModifierSegment",
2521        Sequence::new(vec![
2522            Ref::new("SelectHintSegment").optional().to_matchable(),
2523            one_of(vec![
2524                Ref::keyword("DISTINCT").to_matchable(),
2525                Ref::keyword("ALL").to_matchable(),
2526            ])
2527            .config(|config| {
2528                config.optional();
2529            })
2530            .to_matchable(),
2531        ])
2532        .to_matchable(),
2533    );
2534
2535    sparksql_dialect.replace_grammar(
2536        "UnorderedSelectStatementSegment",
2537        ansi::get_unordered_select_statement_segment_grammar().copy(
2538            Some(vec![
2539                Ref::new("QualifyClauseSegment").optional().to_matchable(),
2540                Ref::new("ClusterByClauseSegment").optional().to_matchable(),
2541                Ref::new("DistributeByClauseSegment")
2542                    .optional()
2543                    .to_matchable(),
2544                Ref::new("SortByClauseSegment").optional().to_matchable(),
2545            ]),
2546            None,
2547            None,
2548            Some(vec![
2549                Ref::new("OverlapsClauseSegment").optional().to_matchable(),
2550            ]),
2551            Vec::new(),
2552            false,
2553        ),
2554    );
2555
2556    sparksql_dialect.replace_grammar(
2557        "SelectStatementSegment",
2558        ansi::select_statement()
2559            .copy(
2560                Some(vec![
2561                    Ref::new("ClusterByClauseSegment").optional().to_matchable(),
2562                    Ref::new("DistributeByClauseSegment")
2563                        .optional()
2564                        .to_matchable(),
2565                    Ref::new("SortByClauseSegment").optional().to_matchable(),
2566                ]),
2567                None,
2568                Some(Ref::new("LimitClauseSegment").optional().to_matchable()),
2569                None,
2570                Vec::new(),
2571                false,
2572            )
2573            .copy(
2574                Some(vec![
2575                    Ref::new("QualifyClauseSegment").optional().to_matchable(),
2576                ]),
2577                None,
2578                Some(Ref::new("OrderByClauseSegment").optional().to_matchable()),
2579                None,
2580                Vec::new(),
2581                false,
2582            ),
2583    );
2584
2585    sparksql_dialect.replace_grammar(
2586        // Enhance `GROUP BY` clause like in `SELECT` for 'CUBE' and 'ROLLUP`.
2587        // https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select-groupby.html
2588        "GroupByClauseSegment",
2589        Sequence::new(vec![
2590            Ref::keyword("GROUP").to_matchable(),
2591            Ref::keyword("BY").to_matchable(),
2592            MetaSegment::indent().to_matchable(),
2593            one_of(vec![
2594                Delimited::new(vec![
2595                    Ref::new("CubeRollupClauseSegment").to_matchable(),
2596                    Ref::new("GroupingSetsClauseSegment").to_matchable(),
2597                    Ref::new("ColumnReferenceSegment").to_matchable(),
2598                    Ref::new("NumericLiteralSegment").to_matchable(),
2599                    Ref::new("ExpressionSegment").to_matchable(),
2600                ])
2601                .to_matchable(),
2602                Sequence::new(vec![
2603                    Delimited::new(vec![
2604                        Ref::new("ColumnReferenceSegment").to_matchable(),
2605                        Ref::new("NumericLiteralSegment").to_matchable(),
2606                        Ref::new("ExpressionSegment").to_matchable(),
2607                    ])
2608                    .to_matchable(),
2609                    one_of(vec![
2610                        Ref::new("WithCubeRollupClauseSegment").to_matchable(),
2611                        Ref::new("GroupingSetsClauseSegment").to_matchable(),
2612                    ])
2613                    .to_matchable(),
2614                ])
2615                .to_matchable(),
2616            ])
2617            .to_matchable(),
2618            MetaSegment::dedent().to_matchable(),
2619        ])
2620        .to_matchable(),
2621    );
2622    sparksql_dialect.add([
2623        (
2624            "WithCubeRollupClauseSegment".into(),
2625            NodeMatcher::new(SyntaxKind::WithCubeRollupClause, |_| {
2626                Sequence::new(vec![
2627                    Ref::keyword("WITH").to_matchable(),
2628                    one_of(vec![
2629                        Ref::keyword("CUBE").to_matchable(),
2630                        Ref::keyword("ROLLUP").to_matchable(),
2631                    ])
2632                    .to_matchable(),
2633                ])
2634                .to_matchable()
2635            })
2636            .to_matchable()
2637            .into(),
2638        ),
2639        (
2640            "SortByClauseSegment".into(),
2641            NodeMatcher::new(SyntaxKind::SortByClause, |_| {
2642                Sequence::new(vec![
2643                    Ref::keyword("SORT").to_matchable(),
2644                    Ref::keyword("BY").to_matchable(),
2645                    MetaSegment::indent().to_matchable(),
2646                    Delimited::new(vec![
2647                        Sequence::new(vec![
2648                            one_of(vec![
2649                                Ref::new("ColumnReferenceSegment").to_matchable(),
2650                                Ref::new("NumericLiteralSegment").to_matchable(),
2651                                Ref::new("ExpressionSegment").to_matchable(),
2652                            ])
2653                            .to_matchable(),
2654                            one_of(vec![
2655                                Ref::keyword("ASC").to_matchable(),
2656                                Ref::keyword("DESC").to_matchable(),
2657                            ])
2658                            .config(|config| {
2659                                config.optional();
2660                            })
2661                            .to_matchable(),
2662                            Sequence::new(vec![
2663                                Ref::keyword("NULLS").to_matchable(),
2664                                one_of(vec![
2665                                    Ref::keyword("FIRST").to_matchable(),
2666                                    Ref::keyword("LAST").to_matchable(),
2667                                ])
2668                                .to_matchable(),
2669                            ])
2670                            .config(|config| {
2671                                config.optional();
2672                            })
2673                            .to_matchable(),
2674                        ])
2675                        .to_matchable(),
2676                    ])
2677                    .config(|config| {
2678                        config.terminators = vec![
2679                            Ref::keyword("LIMIT").to_matchable(),
2680                            Ref::keyword("HAVING").to_matchable(),
2681                            Ref::keyword("QUALIFY").to_matchable(),
2682                            Ref::keyword("WINDOW").to_matchable(),
2683                            Ref::new("FrameClauseUnitGrammar").to_matchable(),
2684                            Ref::keyword("SEPARATOR").to_matchable(),
2685                        ];
2686                    })
2687                    .to_matchable(),
2688                    MetaSegment::dedent().to_matchable(),
2689                ])
2690                .to_matchable()
2691            })
2692            .to_matchable()
2693            .into(),
2694        ),
2695    ]);
2696
2697    // A `TABLESAMPLE` clause following a table identifier.
2698    // https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select-sampling.html
2699    sparksql_dialect.replace_grammar(
2700        "SamplingExpressionSegment",
2701        Sequence::new(vec![
2702            Ref::keyword("TABLESAMPLE").to_matchable(),
2703            one_of(vec![
2704                Bracketed::new(vec![
2705                    Ref::new("NumericLiteralSegment").to_matchable(),
2706                    one_of(vec![
2707                        Ref::keyword("PERCENT").to_matchable(),
2708                        Ref::keyword("ROWS").to_matchable(),
2709                    ])
2710                    .to_matchable(),
2711                ])
2712                .to_matchable(),
2713                Bracketed::new(vec![
2714                    Ref::keyword("BUCKET").to_matchable(),
2715                    Ref::new("NumericLiteralSegment").to_matchable(),
2716                    Ref::keyword("OUT").to_matchable(),
2717                    Ref::keyword("OF").to_matchable(),
2718                    Ref::new("NumericLiteralSegment").to_matchable(),
2719                ])
2720                .to_matchable(),
2721            ])
2722            .to_matchable(),
2723        ])
2724        .to_matchable(),
2725    );
2726
2727    sparksql_dialect.add([
2728        (
2729            "LateralViewClauseSegment".into(),
2730            NodeMatcher::new(SyntaxKind::LateralViewClause, |_| {
2731                Sequence::new(vec![
2732                    MetaSegment::indent().to_matchable(),
2733                    Ref::keyword("LATERAL").to_matchable(),
2734                    Ref::keyword("VIEW").to_matchable(),
2735                    Ref::keyword("OUTER").optional().to_matchable(),
2736                    Ref::new("FunctionSegment").to_matchable(),
2737                    one_of(vec![
2738                        Sequence::new(vec![
2739                            Ref::new("SingleIdentifierGrammar").to_matchable(),
2740                            Sequence::new(vec![
2741                                Ref::keyword("AS").optional().to_matchable(),
2742                                Delimited::new(vec![
2743                                    Ref::new("SingleIdentifierGrammar").to_matchable(),
2744                                ])
2745                                .to_matchable(),
2746                            ])
2747                            .config(|config| {
2748                                config.optional();
2749                            })
2750                            .to_matchable(),
2751                        ])
2752                        .to_matchable(),
2753                        Sequence::new(vec![
2754                            Ref::keyword("AS").optional().to_matchable(),
2755                            Delimited::new(vec![
2756                                Ref::new("SingleIdentifierGrammar").to_matchable(),
2757                            ])
2758                            .to_matchable(),
2759                        ])
2760                        .to_matchable(),
2761                    ])
2762                    .to_matchable(),
2763                    MetaSegment::dedent().to_matchable(),
2764                ])
2765                .to_matchable()
2766            })
2767            .to_matchable()
2768            .into(),
2769        ),
2770        (
2771            "PivotClauseSegment".into(),
2772            NodeMatcher::new(SyntaxKind::PivotClause, |_| {
2773                Sequence::new(vec![
2774                    MetaSegment::indent().to_matchable(),
2775                    Ref::keyword("PIVOT").to_matchable(),
2776                    Bracketed::new(vec![
2777                        MetaSegment::indent().to_matchable(),
2778                        Delimited::new(vec![
2779                            Sequence::new(vec![
2780                                Ref::new("BaseExpressionElementGrammar").to_matchable(),
2781                                Ref::new("AliasExpressionSegment").optional().to_matchable(),
2782                            ])
2783                            .to_matchable(),
2784                        ])
2785                        .to_matchable(),
2786                        Ref::keyword("FOR").to_matchable(),
2787                        optionally_bracketed(vec![
2788                            one_of(vec![
2789                                Ref::new("SingleIdentifierGrammar").to_matchable(),
2790                                Delimited::new(vec![
2791                                    Ref::new("SingleIdentifierGrammar").to_matchable(),
2792                                ])
2793                                .to_matchable(),
2794                            ])
2795                            .to_matchable(),
2796                        ])
2797                        .to_matchable(),
2798                        Ref::keyword("IN").to_matchable(),
2799                        Bracketed::new(vec![
2800                            Delimited::new(vec![
2801                                Sequence::new(vec![
2802                                    one_of(vec![
2803                                        Bracketed::new(vec![
2804                                            Delimited::new(vec![
2805                                                Ref::new("ExpressionSegment").to_matchable(),
2806                                            ])
2807                                            .to_matchable(),
2808                                        ])
2809                                        .config(|config| {
2810                                            config.parse_mode(ParseMode::Greedy);
2811                                        })
2812                                        .to_matchable(),
2813                                        Delimited::new(vec![
2814                                            Ref::new("ExpressionSegment").to_matchable(),
2815                                        ])
2816                                        .to_matchable(),
2817                                    ])
2818                                    .to_matchable(),
2819                                    Ref::new("AliasExpressionSegment").optional().to_matchable(),
2820                                ])
2821                                .to_matchable(),
2822                            ])
2823                            .to_matchable(),
2824                        ])
2825                        .to_matchable(),
2826                        MetaSegment::dedent().to_matchable(),
2827                    ])
2828                    .to_matchable(),
2829                    MetaSegment::dedent().to_matchable(),
2830                ])
2831                .to_matchable()
2832            })
2833            .to_matchable()
2834            .into(),
2835        ),
2836        (
2837            "TransformClauseSegment".into(),
2838            NodeMatcher::new(SyntaxKind::TransformClause, |_| {
2839                Sequence::new(vec![
2840                    Ref::keyword("TRANSFORM").to_matchable(),
2841                    Bracketed::new(vec![
2842                        Delimited::new(vec![Ref::new("SingleIdentifierGrammar").to_matchable()])
2843                            .to_matchable(),
2844                    ])
2845                    .config(|config| {
2846                        config.parse_mode(ParseMode::Greedy);
2847                    })
2848                    .to_matchable(),
2849                    MetaSegment::indent().to_matchable(),
2850                    Ref::new("RowFormatClauseSegment").optional().to_matchable(),
2851                    Ref::keyword("USING").to_matchable(),
2852                    Ref::new("QuotedLiteralSegment").to_matchable(),
2853                    Sequence::new(vec![
2854                        Ref::keyword("AS").to_matchable(),
2855                        Bracketed::new(vec![
2856                            Delimited::new(vec![
2857                                AnyNumberOf::new(vec![
2858                                    Ref::new("SingleIdentifierGrammar").to_matchable(),
2859                                    Ref::new("DatatypeSegment").to_matchable(),
2860                                ])
2861                                .to_matchable(),
2862                            ])
2863                            .to_matchable(),
2864                        ])
2865                        .to_matchable(),
2866                    ])
2867                    .config(|config| {
2868                        config.optional();
2869                    })
2870                    .to_matchable(),
2871                    Ref::new("RowFormatClauseSegment").optional().to_matchable(),
2872                ])
2873                .to_matchable()
2874            })
2875            .to_matchable()
2876            .into(),
2877        ),
2878        (
2879            "RowFormatClauseSegment".into(),
2880            hive_dialect.grammar("RowFormatClauseSegment").into(),
2881        ),
2882        (
2883            "SkewedByClauseSegment".into(),
2884            hive_dialect.grammar("SkewedByClauseSegment").into(),
2885        ),
2886    ]);
2887
2888    sparksql_dialect.replace_grammar(
2889        "ExplainStatementSegment",
2890        Sequence::new(vec![
2891            Ref::keyword("EXPLAIN").to_matchable(),
2892            one_of(vec![
2893                Ref::keyword("EXTENDED").to_matchable(),
2894                Ref::keyword("CODEGEN").to_matchable(),
2895                Ref::keyword("COST").to_matchable(),
2896                Ref::keyword("FORMATTED").to_matchable(),
2897            ])
2898            .config(|config| {
2899                config.optional();
2900            })
2901            .to_matchable(),
2902            Ref::new("StatementSegment").to_matchable(),
2903        ])
2904        .to_matchable(),
2905    );
2906
2907    sparksql_dialect.add([
2908        (
2909            "AddFileSegment".into(),
2910            NodeMatcher::new(SyntaxKind::AddFileStatement, |_| {
2911                Sequence::new(vec![
2912                    Ref::keyword("ADD").to_matchable(),
2913                    Ref::keyword("FILE").to_matchable(),
2914                    AnyNumberOf::new(vec![Ref::new("QuotedLiteralSegment").to_matchable()])
2915                        .to_matchable(),
2916                ])
2917                .to_matchable()
2918            })
2919            .to_matchable()
2920            .into(),
2921        ),
2922        (
2923            "AddJarSegment".into(),
2924            NodeMatcher::new(SyntaxKind::AddJarStatement, |_| {
2925                Sequence::new(vec![
2926                    Ref::keyword("ADD").to_matchable(),
2927                    Ref::keyword("JAR").to_matchable(),
2928                    AnyNumberOf::new(vec![
2929                        Ref::new("QuotedLiteralSegment").to_matchable(),
2930                        Ref::new("FileLiteralSegment").to_matchable(),
2931                    ])
2932                    .to_matchable(),
2933                ])
2934                .to_matchable()
2935            })
2936            .to_matchable()
2937            .into(),
2938        ),
2939        (
2940            "AnalyzeTableSegment".into(),
2941            NodeMatcher::new(SyntaxKind::AnalyzeTableStatement, |_| {
2942                Sequence::new(vec![
2943                    Ref::keyword("ANALYZE").to_matchable(),
2944                    one_of(vec![
2945                        Sequence::new(vec![
2946                            Ref::keyword("TABLE").to_matchable(),
2947                            Ref::new("TableReferenceSegment").to_matchable(),
2948                            Ref::new("PartitionSpecGrammar").optional().to_matchable(),
2949                            Ref::keyword("COMPUTE").to_matchable(),
2950                            Ref::keyword("STATISTICS").to_matchable(),
2951                            one_of(vec![
2952                                Ref::keyword("NOSCAN").to_matchable(),
2953                                Sequence::new(vec![
2954                                    Ref::keyword("FOR").to_matchable(),
2955                                    Ref::keyword("COLUMNS").to_matchable(),
2956                                    optionally_bracketed(vec![
2957                                        Delimited::new(vec![
2958                                            Ref::new("ColumnReferenceSegment").to_matchable(),
2959                                        ])
2960                                        .to_matchable(),
2961                                    ])
2962                                    .to_matchable(),
2963                                ])
2964                                .to_matchable(),
2965                            ])
2966                            .config(|config| {
2967                                config.optional();
2968                            })
2969                            .to_matchable(),
2970                        ])
2971                        .to_matchable(),
2972                        Sequence::new(vec![
2973                            Ref::keyword("TABLES").to_matchable(),
2974                            Sequence::new(vec![
2975                                one_of(vec![
2976                                    Ref::keyword("FROM").to_matchable(),
2977                                    Ref::keyword("IN").to_matchable(),
2978                                ])
2979                                .to_matchable(),
2980                                Ref::new("DatabaseReferenceSegment").to_matchable(),
2981                            ])
2982                            .config(|config| {
2983                                config.optional();
2984                            })
2985                            .to_matchable(),
2986                            Ref::keyword("COMPUTE").to_matchable(),
2987                            Ref::keyword("STATISTICS").to_matchable(),
2988                            Ref::keyword("NOSCAN").optional().to_matchable(),
2989                        ])
2990                        .to_matchable(),
2991                    ])
2992                    .to_matchable(),
2993                ])
2994                .to_matchable()
2995            })
2996            .to_matchable()
2997            .into(),
2998        ),
2999        (
3000            "CacheTableSegment".into(),
3001            NodeMatcher::new(SyntaxKind::CacheTable, |_| {
3002                Sequence::new(vec![
3003                    Ref::keyword("CACHE").to_matchable(),
3004                    Ref::keyword("LAZY").optional().to_matchable(),
3005                    Ref::keyword("TABLE").to_matchable(),
3006                    Ref::new("TableReferenceSegment").to_matchable(),
3007                    Ref::new("OptionsGrammar").optional().to_matchable(),
3008                    Sequence::new(vec![
3009                        Ref::keyword("AS").optional().to_matchable(),
3010                        Ref::new("SelectableGrammar").to_matchable(),
3011                    ])
3012                    .config(|config| {
3013                        config.optional();
3014                    })
3015                    .to_matchable(),
3016                ])
3017                .to_matchable()
3018            })
3019            .to_matchable()
3020            .into(),
3021        ),
3022        (
3023            "ClearCacheSegment".into(),
3024            NodeMatcher::new(SyntaxKind::ClearCache, |_| {
3025                Sequence::new(vec![
3026                    Ref::keyword("CLEAR").to_matchable(),
3027                    Ref::keyword("CACHE").to_matchable(),
3028                ])
3029                .to_matchable()
3030            })
3031            .to_matchable()
3032            .into(),
3033        ),
3034        (
3035            "DescribeObjectGrammar".into(),
3036            one_of(vec![
3037                Sequence::new(vec![
3038                    one_of(vec![
3039                        Ref::keyword("DATABASE").to_matchable(),
3040                        Ref::keyword("SCHEMA").to_matchable(),
3041                    ])
3042                    .to_matchable(),
3043                    Ref::keyword("EXTENDED").optional().to_matchable(),
3044                    Ref::new("DatabaseReferenceSegment").to_matchable(),
3045                ])
3046                .to_matchable(),
3047                Sequence::new(vec![
3048                    Ref::keyword("FUNCTION").to_matchable(),
3049                    Ref::keyword("EXTENDED").optional().to_matchable(),
3050                    Ref::new("FunctionNameSegment").to_matchable(),
3051                ])
3052                .to_matchable(),
3053                Sequence::new(vec![
3054                    Ref::keyword("TABLE").optional().to_matchable(),
3055                    Ref::keyword("EXTENDED").optional().to_matchable(),
3056                    Ref::new("TableReferenceSegment").to_matchable(),
3057                    Ref::new("PartitionSpecGrammar").optional().to_matchable(),
3058                    Sequence::new(vec![
3059                        Ref::new("SingleIdentifierGrammar").to_matchable(),
3060                        AnyNumberOf::new(vec![
3061                            Sequence::new(vec![
3062                                Ref::new("DotSegment").to_matchable(),
3063                                Ref::new("SingleIdentifierGrammar").to_matchable(),
3064                            ])
3065                            .config(|config| {
3066                                config.disallow_gaps();
3067                            })
3068                            .to_matchable(),
3069                        ])
3070                        .config(|config| {
3071                            config.max_times = Some(2);
3072                            config.disallow_gaps();
3073                        })
3074                        .to_matchable(),
3075                    ])
3076                    .config(|config| {
3077                        config.optional();
3078                        config.disallow_gaps();
3079                    })
3080                    .to_matchable(),
3081                ])
3082                .to_matchable(),
3083                Sequence::new(vec![
3084                    Ref::keyword("QUERY").optional().to_matchable(),
3085                    one_of(vec![
3086                        Sequence::new(vec![
3087                            Ref::keyword("TABLE").to_matchable(),
3088                            Ref::new("TableReferenceSegment").to_matchable(),
3089                        ])
3090                        .to_matchable(),
3091                        Sequence::new(vec![
3092                            Ref::keyword("FROM").to_matchable(),
3093                            Ref::new("TableReferenceSegment").to_matchable(),
3094                            Ref::keyword("SELECT").to_matchable(),
3095                            Delimited::new(vec![Ref::new("ColumnReferenceSegment").to_matchable()])
3096                                .to_matchable(),
3097                            Ref::new("WhereClauseSegment").optional().to_matchable(),
3098                            Ref::new("GroupByClauseSegment").optional().to_matchable(),
3099                            Ref::new("OrderByClauseSegment").optional().to_matchable(),
3100                            Ref::new("LimitClauseSegment").optional().to_matchable(),
3101                        ])
3102                        .to_matchable(),
3103                        Ref::new("StatementSegment").to_matchable(),
3104                    ])
3105                    .to_matchable(),
3106                ])
3107                .to_matchable(),
3108            ])
3109            .config(|config| {
3110                config.exclude = one_of(vec![
3111                    Ref::keyword("HISTORY").to_matchable(),
3112                    Ref::keyword("DETAIL").to_matchable(),
3113                ])
3114                .to_matchable()
3115                .into();
3116            })
3117            .to_matchable()
3118            .into(),
3119        ),
3120        // A `DESCRIBE` statement.
3121        // This class provides coverage for databases, tables, functions, and queries.
3122
3123        // NB: These are similar enough that it makes sense to include them in a
3124        // common class, especially since there wouldn't be any specific rules that
3125        // would apply to one describe vs another, but they could be broken out to
3126        // one class per describe statement type.
3127
3128        // https://spark.apache.org/docs/latest/sql-ref-syntax-aux-describe-database.html
3129        // https://spark.apache.org/docs/latest/sql-ref-syntax-aux-describe-function.html
3130        // https://spark.apache.org/docs/latest/sql-ref-syntax-aux-describe-query.html
3131        // https://spark.apache.org/docs/latest/sql-ref-syntax-aux-describe-table.html
3132        (
3133            "DescribeStatementSegment".into(),
3134            NodeMatcher::new(SyntaxKind::DescribeStatement, |_| {
3135                Sequence::new(vec![
3136                    one_of(vec![
3137                        Ref::keyword("DESCRIBE").to_matchable(),
3138                        Ref::keyword("DESC").to_matchable(),
3139                    ])
3140                    .to_matchable(),
3141                    Ref::new("DescribeObjectGrammar").to_matchable(),
3142                ])
3143                .to_matchable()
3144            })
3145            .to_matchable()
3146            .into(),
3147        ),
3148        (
3149            "ListFileSegment".into(),
3150            NodeMatcher::new(SyntaxKind::ListFileStatement, |_| {
3151                Sequence::new(vec![
3152                    Ref::keyword("LIST").to_matchable(),
3153                    Ref::keyword("FILE").to_matchable(),
3154                    AnyNumberOf::new(vec![Ref::new("QuotedLiteralSegment").to_matchable()])
3155                        .to_matchable(),
3156                ])
3157                .to_matchable()
3158            })
3159            .to_matchable()
3160            .into(),
3161        ),
3162        (
3163            "ListJarSegment".into(),
3164            NodeMatcher::new(SyntaxKind::ListJarStatement, |_| {
3165                Sequence::new(vec![
3166                    Ref::keyword("LIST").to_matchable(),
3167                    Ref::keyword("JAR").to_matchable(),
3168                    AnyNumberOf::new(vec![Ref::new("QuotedLiteralSegment").to_matchable()])
3169                        .to_matchable(),
3170                ])
3171                .to_matchable()
3172            })
3173            .to_matchable()
3174            .into(),
3175        ),
3176        (
3177            "RefreshStatementSegment".into(),
3178            NodeMatcher::new(SyntaxKind::RefreshStatement, |_| {
3179                Sequence::new(vec![
3180                    Ref::keyword("REFRESH").to_matchable(),
3181                    one_of(vec![
3182                        Ref::new("QuotedLiteralSegment").to_matchable(),
3183                        Sequence::new(vec![
3184                            Ref::keyword("TABLE").optional().to_matchable(),
3185                            Ref::new("TableReferenceSegment").to_matchable(),
3186                        ])
3187                        .to_matchable(),
3188                        Sequence::new(vec![
3189                            Ref::keyword("FUNCTION").to_matchable(),
3190                            Ref::new("FunctionNameSegment").to_matchable(),
3191                        ])
3192                        .to_matchable(),
3193                    ])
3194                    .to_matchable(),
3195                ])
3196                .to_matchable()
3197            })
3198            .to_matchable()
3199            .into(),
3200        ),
3201        (
3202            "ResetStatementSegment".into(),
3203            NodeMatcher::new(SyntaxKind::ResetStatement, |_| {
3204                Sequence::new(vec![
3205                    Ref::keyword("RESET").to_matchable(),
3206                    Delimited::new(vec![Ref::new("SingleIdentifierGrammar").to_matchable()])
3207                        .config(|config| {
3208                            config.delimiter(Ref::new("DotSegment"));
3209                            config.optional();
3210                        })
3211                        .to_matchable(),
3212                ])
3213                .to_matchable()
3214            })
3215            .to_matchable()
3216            .into(),
3217        ),
3218        (
3219            "ShowViewsGrammar".into(),
3220            Sequence::new(vec![
3221                Ref::keyword("VIEWS").to_matchable(),
3222                Sequence::new(vec![
3223                    one_of(vec![
3224                        Ref::keyword("FROM").to_matchable(),
3225                        Ref::keyword("IN").to_matchable(),
3226                    ])
3227                    .to_matchable(),
3228                    Ref::new("DatabaseReferenceSegment").to_matchable(),
3229                ])
3230                .config(|config| {
3231                    config.optional();
3232                })
3233                .to_matchable(),
3234                Sequence::new(vec![
3235                    Ref::keyword("LIKE").to_matchable(),
3236                    Ref::new("QuotedLiteralSegment").to_matchable(),
3237                ])
3238                .config(|config| {
3239                    config.optional();
3240                })
3241                .to_matchable(),
3242            ])
3243            .to_matchable()
3244            .into(),
3245        ),
3246        (
3247            "SetStatementSegment".into(),
3248            NodeMatcher::new(SyntaxKind::SetStatement, |_| {
3249                Sequence::new(vec![
3250                    Ref::keyword("SET").to_matchable(),
3251                    Ref::new("SQLConfPropertiesSegment")
3252                        .optional()
3253                        .to_matchable(),
3254                    one_of(vec![
3255                        Ref::new("PropertyListGrammar").to_matchable(),
3256                        Ref::new("PropertyNameSegment").to_matchable(),
3257                    ])
3258                    .config(|config| {
3259                        config.optional();
3260                    })
3261                    .to_matchable(),
3262                ])
3263                .to_matchable()
3264            })
3265            .to_matchable()
3266            .into(),
3267        ),
3268        (
3269            // Common class for `SHOW` statements.
3270            //
3271            // NB: These are similar enough that it makes sense to include them in a
3272            // common class, especially since there wouldn't be any specific rules that
3273            // would apply to one show vs another, but they could be broken out to
3274            // one class per show statement type.
3275            //
3276            // https://spark.apache.org/docs/latest/sql-ref-syntax-aux-show-columns.html
3277            // https://spark.apache.org/docs/latest/sql-ref-syntax-aux-show-create-table.html
3278            // https://spark.apache.org/docs/latest/sql-ref-syntax-aux-show-databases.html
3279            // https://spark.apache.org/docs/latest/sql-ref-syntax-aux-show-functions.html
3280            // https://spark.apache.org/docs/latest/sql-ref-syntax-aux-show-partitions.html
3281            // https://spark.apache.org/docs/latest/sql-ref-syntax-aux-show-table.html
3282            // https://spark.apache.org/docs/latest/sql-ref-syntax-aux-show-tables.html
3283            // https://spark.apache.org/docs/latest/sql-ref-syntax-aux-show-tblproperties.html
3284            // https://spark.apache.org/docs/latest/sql-ref-syntax-aux-show-views.html
3285            "ShowStatement".into(),
3286            NodeMatcher::new(SyntaxKind::ShowStatement, |_| {
3287                Sequence::new(vec![
3288                    Ref::keyword("SHOW").to_matchable(),
3289                    Ref::new("ShowObjectGrammar").to_matchable(),
3290                ])
3291                .to_matchable()
3292            })
3293            .to_matchable()
3294            .into(),
3295        ),
3296        (
3297            "UncacheTableSegment".into(),
3298            NodeMatcher::new(SyntaxKind::UncacheTable, |_| {
3299                Sequence::new(vec![
3300                    Ref::keyword("UNCACHE").to_matchable(),
3301                    Ref::keyword("TABLE").to_matchable(),
3302                    Ref::new("IfExistsGrammar").optional().to_matchable(),
3303                    Ref::new("TableReferenceSegment").to_matchable(),
3304                ])
3305                .to_matchable()
3306            })
3307            .to_matchable()
3308            .into(),
3309        ),
3310    ]);
3311
3312    sparksql_dialect.replace_grammar(
3313        "StatementSegment",
3314        ansi::statement_segment().copy(
3315            Some(vec![
3316                Ref::new("AlterDatabaseStatementSegment").to_matchable(),
3317                Ref::new("AlterTableStatementSegment").to_matchable(),
3318                Ref::new("AlterViewStatementSegment").to_matchable(),
3319                Ref::new("CreateHiveFormatTableStatementSegment").to_matchable(),
3320                Ref::new("MsckRepairTableStatementSegment").to_matchable(),
3321                Ref::new("UseDatabaseStatementSegment").to_matchable(),
3322                Ref::new("AddFileSegment").to_matchable(),
3323                Ref::new("AddJarSegment").to_matchable(),
3324                Ref::new("AnalyzeTableSegment").to_matchable(),
3325                Ref::new("CacheTableSegment").to_matchable(),
3326                Ref::new("ClearCacheSegment").to_matchable(),
3327                Ref::new("ListFileSegment").to_matchable(),
3328                Ref::new("ListJarSegment").to_matchable(),
3329                Ref::new("RefreshStatementSegment").to_matchable(),
3330                Ref::new("ResetStatementSegment").to_matchable(),
3331                Ref::new("SetStatementSegment").to_matchable(),
3332                Ref::new("ShowStatement").to_matchable(),
3333                Ref::new("UncacheTableSegment").to_matchable(),
3334                Ref::new("InsertOverwriteDirectorySegment").to_matchable(),
3335                Ref::new("InsertOverwriteDirectoryHiveFmtSegment").to_matchable(),
3336                Ref::new("LoadDataSegment").to_matchable(),
3337                Ref::new("ClusterByClauseSegment").to_matchable(),
3338                Ref::new("DistributeByClauseSegment").to_matchable(),
3339                Ref::new("VacuumStatementSegment").to_matchable(),
3340                Ref::new("DescribeHistoryStatementSegment").to_matchable(),
3341                Ref::new("DescribeDetailStatementSegment").to_matchable(),
3342                Ref::new("GenerateManifestFileStatementSegment").to_matchable(),
3343                Ref::new("ConvertToDeltaStatementSegment").to_matchable(),
3344                Ref::new("RestoreTableStatementSegment").to_matchable(),
3345                Ref::new("ConstraintStatementSegment").to_matchable(),
3346                Ref::new("ApplyChangesIntoStatementSegment").to_matchable(),
3347                Ref::new("CreateWidgetStatementSegment").to_matchable(),
3348                Ref::new("RemoveWidgetStatementSegment").to_matchable(),
3349                Ref::new("ReplaceTableStatementSegment").to_matchable(),
3350                Ref::new("SetVariableStatementSegment").to_matchable(),
3351            ]),
3352            None,
3353            None,
3354            Some(vec![
3355                Ref::new("TransactionStatementSegment").to_matchable(),
3356                Ref::new("CreateSchemaStatementSegment").to_matchable(),
3357                Ref::new("SetSchemaStatementSegment").to_matchable(),
3358                Ref::new("CreateModelStatementSegment").to_matchable(),
3359                Ref::new("DropModelStatementSegment").to_matchable(),
3360            ]),
3361            Vec::new(),
3362            false,
3363        ),
3364    );
3365
3366    sparksql_dialect.replace_grammar(
3367        "JoinClauseSegment",
3368        one_of(vec![
3369            Sequence::new(vec![
3370                Ref::new("JoinTypeKeywords").optional().to_matchable(),
3371                Ref::new("JoinKeywordsGrammar").to_matchable(),
3372                MetaSegment::indent().to_matchable(),
3373                Ref::new("FromExpressionElementSegment").to_matchable(),
3374                MetaSegment::dedent().to_matchable(),
3375                Conditional::new(MetaSegment::indent())
3376                    .indented_using_on()
3377                    .to_matchable(),
3378                one_of(vec![
3379                    Ref::new("JoinOnConditionSegment").to_matchable(),
3380                    Sequence::new(vec![
3381                        Ref::keyword("USING").to_matchable(),
3382                        Conditional::new(MetaSegment::indent()).to_matchable(),
3383                        Bracketed::new(vec![
3384                            Delimited::new(vec![
3385                                Ref::new("SingleIdentifierGrammar").to_matchable(),
3386                            ])
3387                            .to_matchable(),
3388                        ])
3389                        .config(|config| {
3390                            config.parse_mode(ParseMode::Greedy);
3391                        })
3392                        .to_matchable(),
3393                        Conditional::new(MetaSegment::dedent()).to_matchable(),
3394                    ])
3395                    .to_matchable(),
3396                ])
3397                .config(|config| {
3398                    config.optional();
3399                })
3400                .to_matchable(),
3401                Conditional::new(MetaSegment::dedent())
3402                    .indented_using_on()
3403                    .to_matchable(),
3404            ])
3405            .to_matchable(),
3406            Sequence::new(vec![
3407                Ref::new("NaturalJoinKeywordsGrammar").to_matchable(),
3408                Ref::new("JoinKeywordsGrammar").to_matchable(),
3409                MetaSegment::indent().to_matchable(),
3410                Ref::new("FromExpressionElementSegment").to_matchable(),
3411                MetaSegment::dedent().to_matchable(),
3412            ])
3413            .to_matchable(),
3414        ])
3415        .to_matchable(),
3416    );
3417
3418    sparksql_dialect.replace_grammar(
3419        "AliasExpressionSegment",
3420        Sequence::new(vec![
3421            Ref::keyword("AS").optional().to_matchable(),
3422            one_of(vec![
3423                Sequence::new(vec![
3424                    Ref::new("SingleIdentifierGrammar")
3425                        .optional()
3426                        .to_matchable(),
3427                    Bracketed::new(vec![Ref::new("SingleIdentifierListSegment").to_matchable()])
3428                        .to_matchable(),
3429                ])
3430                .to_matchable(),
3431                Ref::new("SingleIdentifierGrammar").to_matchable(),
3432            ])
3433            .config(|config| {
3434                config.exclude = one_of(vec![
3435                    Ref::keyword("LATERAL").to_matchable(),
3436                    Ref::new("JoinTypeKeywords").to_matchable(),
3437                    Ref::keyword("WINDOW").to_matchable(),
3438                    Ref::keyword("PIVOT").to_matchable(),
3439                    Ref::keyword("KEYS").to_matchable(),
3440                    Ref::keyword("FROM").to_matchable(),
3441                ])
3442                .to_matchable()
3443                .into();
3444            })
3445            .to_matchable(),
3446        ])
3447        .to_matchable(),
3448    );
3449
3450    sparksql_dialect.replace_grammar(
3451        "ValuesClauseSegment",
3452        Sequence::new(vec![
3453            Ref::keyword("VALUES").to_matchable(),
3454            Delimited::new(vec![
3455                one_of(vec![
3456                    Bracketed::new(vec![
3457                        Delimited::new(vec![
3458                            Ref::keyword("NULL").to_matchable(),
3459                            Ref::new("ExpressionSegment").to_matchable(),
3460                        ])
3461                        .to_matchable(),
3462                    ])
3463                    .config(|config| {
3464                        config.parse_mode(ParseMode::Greedy);
3465                    })
3466                    .to_matchable(),
3467                    Ref::keyword("NULL").to_matchable(),
3468                    Ref::new("ExpressionSegment").to_matchable(),
3469                ])
3470                .config(|config| {
3471                    config.exclude = one_of(vec![Ref::keyword("VALUES").to_matchable()])
3472                        .to_matchable()
3473                        .into();
3474                })
3475                .to_matchable(),
3476            ])
3477            .to_matchable(),
3478            Ref::new("AliasExpressionSegment")
3479                .exclude(one_of(vec![
3480                    Ref::keyword("LIMIT").to_matchable(),
3481                    Ref::keyword("ORDER").to_matchable(),
3482                ]))
3483                .optional()
3484                .config(|config| {
3485                    config.exclude = one_of(vec![
3486                        Ref::keyword("LIMIT").to_matchable(),
3487                        Ref::keyword("ORDER").to_matchable(),
3488                    ])
3489                    .to_matchable()
3490                    .into();
3491                })
3492                .to_matchable(),
3493            Ref::new("OrderByClauseSegment").optional().to_matchable(),
3494            Ref::new("LimitClauseSegment").optional().to_matchable(),
3495        ])
3496        .to_matchable(),
3497    );
3498
3499    sparksql_dialect.replace_grammar(
3500        "TableExpressionSegment",
3501        one_of(vec![
3502            Ref::new("ValuesClauseSegment").to_matchable(),
3503            Ref::new("BareFunctionSegment").to_matchable(),
3504            Ref::new("FunctionSegment").to_matchable(),
3505            Sequence::new(vec![
3506                one_of(vec![
3507                    Ref::new("FileReferenceSegment").to_matchable(),
3508                    Ref::new("TableReferenceSegment").to_matchable(),
3509                ])
3510                .to_matchable(),
3511                one_of(vec![
3512                    Ref::new("AtSignLiteralSegment").to_matchable(),
3513                    Sequence::new(vec![
3514                        MetaSegment::indent().to_matchable(),
3515                        one_of(vec![
3516                            Ref::new("TimestampAsOfGrammar").to_matchable(),
3517                            Ref::new("VersionAsOfGrammar").to_matchable(),
3518                        ])
3519                        .to_matchable(),
3520                        MetaSegment::dedent().to_matchable(),
3521                    ])
3522                    .to_matchable(),
3523                ])
3524                .config(|config| {
3525                    config.optional();
3526                })
3527                .to_matchable(),
3528            ])
3529            .to_matchable(),
3530            Bracketed::new(vec![Ref::new("SelectableGrammar").to_matchable()]).to_matchable(),
3531        ])
3532        .to_matchable(),
3533    );
3534    sparksql_dialect.add([(
3535        "FileReferenceSegment".into(),
3536        NodeMatcher::new(SyntaxKind::FileReference, |_| {
3537            Sequence::new(vec![
3538                Ref::new("DataSourcesV2FileTypeGrammar").to_matchable(),
3539                Ref::new("DotSegment").to_matchable(),
3540                Ref::new("BackQuotedIdentifierSegment").to_matchable(),
3541            ])
3542            .to_matchable()
3543        })
3544        .to_matchable()
3545        .into(),
3546    )]);
3547
3548    sparksql_dialect.replace_grammar(
3549        "FromExpressionElementSegment",
3550        Sequence::new(vec![
3551            Ref::new("PreTableFunctionKeywordsGrammar")
3552                .optional()
3553                .to_matchable(),
3554            optionally_bracketed(vec![Ref::new("TableExpressionSegment").to_matchable()])
3555                .to_matchable(),
3556            Ref::new("SamplingExpressionSegment")
3557                .optional()
3558                .to_matchable(),
3559            Ref::new("AliasExpressionSegment")
3560                .exclude(one_of(vec![
3561                    Ref::new("FromClauseTerminatorGrammar").to_matchable(),
3562                    Ref::new("JoinLikeClauseGrammar").to_matchable(),
3563                ]))
3564                .optional()
3565                .to_matchable(),
3566            Ref::new("SamplingExpressionSegment")
3567                .optional()
3568                .to_matchable(),
3569            AnyNumberOf::new(vec![Ref::new("LateralViewClauseSegment").to_matchable()])
3570                .to_matchable(),
3571            Ref::new("NamedWindowSegment").optional().to_matchable(),
3572            Ref::new("PostTableExpressionGrammar")
3573                .optional()
3574                .to_matchable(),
3575        ])
3576        .to_matchable(),
3577    );
3578    sparksql_dialect.add([
3579        (
3580            "PropertyNameSegment".into(),
3581            NodeMatcher::new(SyntaxKind::PropertyNameIdentifier, |_| {
3582                Sequence::new(vec![
3583                    one_of(vec![
3584                        Delimited::new(vec![
3585                            Ref::new("PropertiesNakedIdentifierSegment").to_matchable(),
3586                        ])
3587                        .config(|config| {
3588                            config.delimiter(Ref::new("DotSegment"));
3589                            config.disallow_gaps();
3590                        })
3591                        .to_matchable(),
3592                        Ref::new("SingleIdentifierGrammar").to_matchable(),
3593                    ])
3594                    .to_matchable(),
3595                ])
3596                .to_matchable()
3597            })
3598            .to_matchable()
3599            .into(),
3600        ),
3601        (
3602            "GeneratedColumnDefinitionSegment".into(),
3603            NodeMatcher::new(SyntaxKind::GeneratedColumnDefinition, |_| {
3604                Sequence::new(vec![
3605                    Ref::new("SingleIdentifierGrammar").to_matchable(),
3606                    Ref::new("DatatypeSegment").to_matchable(),
3607                    Bracketed::new(vec![Anything::new().to_matchable()])
3608                        .config(|config| {
3609                            config.optional();
3610                        })
3611                        .to_matchable(),
3612                    Sequence::new(vec![
3613                        Ref::keyword("GENERATED").to_matchable(),
3614                        Ref::keyword("ALWAYS").to_matchable(),
3615                        Ref::keyword("AS").to_matchable(),
3616                        Bracketed::new(vec![
3617                            one_of(vec![
3618                                Ref::new("FunctionSegment").to_matchable(),
3619                                Ref::new("BareFunctionSegment").to_matchable(),
3620                            ])
3621                            .to_matchable(),
3622                        ])
3623                        .to_matchable(),
3624                    ])
3625                    .to_matchable(),
3626                    AnyNumberOf::new(vec![
3627                        Ref::new("ColumnConstraintSegment")
3628                            .optional()
3629                            .to_matchable(),
3630                    ])
3631                    .to_matchable(),
3632                ])
3633                .to_matchable()
3634            })
3635            .to_matchable()
3636            .into(),
3637        ),
3638    ]);
3639
3640    sparksql_dialect.replace_grammar(
3641        "MergeUpdateClauseSegment",
3642        Sequence::new(vec![
3643            Ref::keyword("UPDATE").to_matchable(),
3644            one_of(vec![
3645                Sequence::new(vec![
3646                    Ref::keyword("SET").to_matchable(),
3647                    Ref::new("WildcardIdentifierSegment").to_matchable(),
3648                ])
3649                .to_matchable(),
3650                Sequence::new(vec![
3651                    MetaSegment::indent().to_matchable(),
3652                    Ref::new("SetClauseListSegment").to_matchable(),
3653                    MetaSegment::dedent().to_matchable(),
3654                ])
3655                .to_matchable(),
3656            ])
3657            .to_matchable(),
3658        ])
3659        .to_matchable(),
3660    );
3661
3662    sparksql_dialect.replace_grammar(
3663        "MergeInsertClauseSegment",
3664        Sequence::new(vec![
3665            Ref::keyword("INSERT").to_matchable(),
3666            one_of(vec![
3667                Ref::new("WildcardIdentifierSegment").to_matchable(),
3668                Sequence::new(vec![
3669                    MetaSegment::indent().to_matchable(),
3670                    Ref::new("BracketedColumnReferenceListGrammar").to_matchable(),
3671                    MetaSegment::dedent().to_matchable(),
3672                    Ref::new("ValuesClauseSegment").to_matchable(),
3673                ])
3674                .to_matchable(),
3675            ])
3676            .to_matchable(),
3677        ])
3678        .to_matchable(),
3679    );
3680
3681    sparksql_dialect.replace_grammar(
3682        "UpdateStatementSegment",
3683        Sequence::new(vec![
3684            Ref::keyword("UPDATE").to_matchable(),
3685            one_of(vec![
3686                Ref::new("FileReferenceSegment").to_matchable(),
3687                Ref::new("TableReferenceSegment").to_matchable(),
3688            ])
3689            .to_matchable(),
3690            Ref::new("AliasExpressionSegment")
3691                .exclude(Ref::keyword("SET"))
3692                .optional()
3693                .config(|config| {
3694                    config.exclude = Ref::keyword("SET").to_matchable().into();
3695                })
3696                .to_matchable(),
3697            Ref::new("SetClauseListSegment").to_matchable(),
3698            Ref::new("WhereClauseSegment").to_matchable(),
3699        ])
3700        .to_matchable(),
3701    );
3702    sparksql_dialect.add([(
3703        "IntervalLiteralSegment".into(),
3704        NodeMatcher::new(SyntaxKind::IntervalLiteral, |_| {
3705            Sequence::new(vec![
3706                Ref::new("SignedSegmentGrammar").optional().to_matchable(),
3707                one_of(vec![
3708                    Ref::new("NumericLiteralSegment").to_matchable(),
3709                    Ref::new("SignedQuotedLiteralSegment").to_matchable(),
3710                ])
3711                .to_matchable(),
3712                Ref::new("DatetimeUnitSegment").to_matchable(),
3713                Ref::keyword("TO").optional().to_matchable(),
3714                Ref::new("DatetimeUnitSegment").optional().to_matchable(),
3715            ])
3716            .to_matchable()
3717        })
3718        .to_matchable()
3719        .into(),
3720    )]);
3721
3722    sparksql_dialect.replace_grammar(
3723        "IntervalExpressionSegment",
3724        Sequence::new(vec![
3725            Ref::keyword("INTERVAL").to_matchable(),
3726            one_of(vec![
3727                AnyNumberOf::new(vec![Ref::new("IntervalLiteralSegment").to_matchable()])
3728                    .to_matchable(),
3729                Ref::new("QuotedLiteralSegment").to_matchable(),
3730            ])
3731            .to_matchable(),
3732        ])
3733        .to_matchable(),
3734    );
3735    sparksql_dialect.add([
3736        (
3737            "VacuumStatementSegment".into(),
3738            NodeMatcher::new(SyntaxKind::VacuumStatement, |_| {
3739                Sequence::new(vec![
3740                    Ref::keyword("VACUUM").to_matchable(),
3741                    one_of(vec![
3742                        Ref::new("QuotedLiteralSegment").to_matchable(),
3743                        Ref::new("FileReferenceSegment").to_matchable(),
3744                        Ref::new("TableReferenceSegment").to_matchable(),
3745                    ])
3746                    .to_matchable(),
3747                    one_of(vec![
3748                        Sequence::new(vec![
3749                            Ref::keyword("RETAIN").to_matchable(),
3750                            Ref::new("NumericLiteralSegment").to_matchable(),
3751                            Ref::new("DatetimeUnitSegment").to_matchable(),
3752                        ])
3753                        .to_matchable(),
3754                        Sequence::new(vec![
3755                            Ref::keyword("DRY").to_matchable(),
3756                            Ref::keyword("RUN").to_matchable(),
3757                        ])
3758                        .to_matchable(),
3759                    ])
3760                    .config(|config| {
3761                        config.optional();
3762                    })
3763                    .to_matchable(),
3764                ])
3765                .to_matchable()
3766            })
3767            .to_matchable()
3768            .into(),
3769        ),
3770        (
3771            "DescribeHistoryStatementSegment".into(),
3772            NodeMatcher::new(SyntaxKind::DescribeHistoryStatement, |_| {
3773                Sequence::new(vec![
3774                    Ref::keyword("DESCRIBE").to_matchable(),
3775                    Ref::keyword("HISTORY").to_matchable(),
3776                    one_of(vec![
3777                        Ref::new("QuotedLiteralSegment").to_matchable(),
3778                        Ref::new("FileReferenceSegment").to_matchable(),
3779                        Ref::new("TableReferenceSegment").to_matchable(),
3780                    ])
3781                    .to_matchable(),
3782                    Ref::new("LimitClauseSegment").optional().to_matchable(),
3783                ])
3784                .to_matchable()
3785            })
3786            .to_matchable()
3787            .into(),
3788        ),
3789        (
3790            "DescribeDetailStatementSegment".into(),
3791            NodeMatcher::new(SyntaxKind::DescribeDetailStatement, |_| {
3792                Sequence::new(vec![
3793                    Ref::keyword("DESCRIBE").to_matchable(),
3794                    Ref::keyword("DETAIL").to_matchable(),
3795                    one_of(vec![
3796                        Ref::new("QuotedLiteralSegment").to_matchable(),
3797                        Ref::new("FileReferenceSegment").to_matchable(),
3798                        Ref::new("TableReferenceSegment").to_matchable(),
3799                    ])
3800                    .to_matchable(),
3801                ])
3802                .to_matchable()
3803            })
3804            .to_matchable()
3805            .into(),
3806        ),
3807        (
3808            "GenerateManifestFileStatementSegment".into(),
3809            NodeMatcher::new(SyntaxKind::GenerateManifestFileStatement, |_| {
3810                Sequence::new(vec![
3811                    Ref::keyword("GENERATE").to_matchable(),
3812                    StringParser::new("symlink_format_manifest", SyntaxKind::SymlinkFormatManifest)
3813                        .to_matchable(),
3814                    Ref::keyword("FOR").to_matchable(),
3815                    Ref::keyword("TABLE").to_matchable(),
3816                    one_of(vec![
3817                        Ref::new("QuotedLiteralSegment").to_matchable(),
3818                        Ref::new("FileReferenceSegment").to_matchable(),
3819                        Ref::new("TableReferenceSegment").to_matchable(),
3820                    ])
3821                    .to_matchable(),
3822                ])
3823                .to_matchable()
3824            })
3825            .to_matchable()
3826            .into(),
3827        ),
3828        (
3829            "ConvertToDeltaStatementSegment".into(),
3830            NodeMatcher::new(SyntaxKind::ConvertToDeltaStatement, |_| {
3831                Sequence::new(vec![
3832                    Ref::keyword("CONVERT").to_matchable(),
3833                    Ref::keyword("TO").to_matchable(),
3834                    Ref::keyword("DELTA").to_matchable(),
3835                    Ref::new("FileReferenceSegment").to_matchable(),
3836                    Sequence::new(vec![
3837                        Ref::keyword("NO").to_matchable(),
3838                        Ref::keyword("STATISTICS").to_matchable(),
3839                    ])
3840                    .config(|config| {
3841                        config.optional();
3842                    })
3843                    .to_matchable(),
3844                    Ref::new("PartitionSpecGrammar").optional().to_matchable(),
3845                ])
3846                .to_matchable()
3847            })
3848            .to_matchable()
3849            .into(),
3850        ),
3851        (
3852            "RestoreTableStatementSegment".into(),
3853            NodeMatcher::new(SyntaxKind::RestoreTableStatement, |_| {
3854                Sequence::new(vec![
3855                    Ref::keyword("RESTORE").to_matchable(),
3856                    Ref::keyword("TABLE").to_matchable(),
3857                    one_of(vec![
3858                        Ref::new("QuotedLiteralSegment").to_matchable(),
3859                        Ref::new("FileReferenceSegment").to_matchable(),
3860                        Ref::new("TableReferenceSegment").to_matchable(),
3861                    ])
3862                    .to_matchable(),
3863                    Ref::keyword("TO").to_matchable(),
3864                    one_of(vec![
3865                        Ref::new("TimestampAsOfGrammar").to_matchable(),
3866                        Ref::new("VersionAsOfGrammar").to_matchable(),
3867                    ])
3868                    .to_matchable(),
3869                ])
3870                .to_matchable()
3871            })
3872            .to_matchable()
3873            .into(),
3874        ),
3875        (
3876            "ConstraintStatementSegment".into(),
3877            NodeMatcher::new(SyntaxKind::ConstraintStatement, |_| {
3878                Sequence::new(vec![
3879                    Ref::keyword("CONSTRAINT").to_matchable(),
3880                    Ref::new("ObjectReferenceSegment").to_matchable(),
3881                    Ref::keyword("EXPECT").to_matchable(),
3882                    Bracketed::new(vec![Ref::new("ExpressionSegment").to_matchable()])
3883                        .to_matchable(),
3884                    Sequence::new(vec![
3885                        Ref::keyword("ON").to_matchable(),
3886                        Ref::keyword("VIOLATION").to_matchable(),
3887                    ])
3888                    .config(|config| {
3889                        config.optional();
3890                    })
3891                    .to_matchable(),
3892                    one_of(vec![
3893                        Sequence::new(vec![
3894                            Ref::keyword("FAIL").to_matchable(),
3895                            Ref::keyword("UPDATE").to_matchable(),
3896                        ])
3897                        .to_matchable(),
3898                        Sequence::new(vec![
3899                            Ref::keyword("DROP").to_matchable(),
3900                            Ref::keyword("ROW").to_matchable(),
3901                        ])
3902                        .to_matchable(),
3903                    ])
3904                    .config(|config| {
3905                        config.optional();
3906                    })
3907                    .to_matchable(),
3908                ])
3909                .to_matchable()
3910            })
3911            .to_matchable()
3912            .into(),
3913        ),
3914        (
3915            // A statement ingest CDC data a target table.
3916            // https://docs.databricks.com/workflows/delta-live-tables/delta-live-tables-cdc.html#sql
3917            "ApplyChangesIntoStatementSegment".into(),
3918            NodeMatcher::new(SyntaxKind::ApplyChangesIntoStatement, |_| {
3919                Sequence::new(vec![
3920                    Sequence::new(vec![
3921                        Ref::keyword("APPLY").to_matchable(),
3922                        Ref::keyword("CHANGES").to_matchable(),
3923                        Ref::keyword("INTO").to_matchable(),
3924                    ])
3925                    .to_matchable(),
3926                    MetaSegment::indent().to_matchable(),
3927                    Ref::new("TableExpressionSegment").to_matchable(),
3928                    MetaSegment::dedent().to_matchable(),
3929                    Ref::new("FromClauseSegment").to_matchable(),
3930                    Sequence::new(vec![
3931                        Ref::keyword("KEYS").to_matchable(),
3932                        MetaSegment::indent().to_matchable(),
3933                        Ref::new("BracketedColumnReferenceListGrammar").to_matchable(),
3934                        MetaSegment::dedent().to_matchable(),
3935                    ])
3936                    .to_matchable(),
3937                    Sequence::new(vec![
3938                        Ref::keyword("IGNORE").to_matchable(),
3939                        Ref::keyword("NULL").to_matchable(),
3940                        Ref::keyword("UPDATES").to_matchable(),
3941                    ])
3942                    .config(|config| {
3943                        config.optional();
3944                    })
3945                    .to_matchable(),
3946                    Ref::new("WhereClauseSegment").optional().to_matchable(),
3947                    AnyNumberOf::new(vec![
3948                        Sequence::new(vec![
3949                            Ref::keyword("APPLY").to_matchable(),
3950                            Ref::keyword("AS").to_matchable(),
3951                            one_of(vec![
3952                                Ref::keyword("DELETE").to_matchable(),
3953                                Ref::keyword("TRUNCATE").to_matchable(),
3954                            ])
3955                            .to_matchable(),
3956                            Ref::keyword("WHEN").to_matchable(),
3957                            Ref::new("ColumnReferenceSegment").to_matchable(),
3958                            Ref::new("EqualsSegment").to_matchable(),
3959                            Ref::new("QuotedLiteralSegment").to_matchable(),
3960                        ])
3961                        .to_matchable(),
3962                    ])
3963                    .config(|config| {
3964                        config.max_times = Some(2);
3965                    })
3966                    .to_matchable(),
3967                    Sequence::new(vec![
3968                        Ref::keyword("SEQUENCE").to_matchable(),
3969                        Ref::keyword("BY").to_matchable(),
3970                        Ref::new("ColumnReferenceSegment").to_matchable(),
3971                    ])
3972                    .to_matchable(),
3973                    Sequence::new(vec![
3974                        Ref::keyword("COLUMNS").to_matchable(),
3975                        one_of(vec![
3976                            Delimited::new(vec![Ref::new("ColumnReferenceSegment").to_matchable()])
3977                                .to_matchable(),
3978                            Sequence::new(vec![
3979                                Ref::new("StarSegment").to_matchable(),
3980                                Ref::keyword("EXCEPT").to_matchable(),
3981                                Ref::new("BracketedColumnReferenceListGrammar").to_matchable(),
3982                            ])
3983                            .to_matchable(),
3984                        ])
3985                        .to_matchable(),
3986                    ])
3987                    .config(|config| {
3988                        config.optional();
3989                    })
3990                    .to_matchable(),
3991                    Sequence::new(vec![
3992                        Ref::keyword("STORED").to_matchable(),
3993                        Ref::keyword("AS").to_matchable(),
3994                        Ref::keyword("SCD").to_matchable(),
3995                        Ref::keyword("TYPE").to_matchable(),
3996                        Ref::new("NumericLiteralSegment").to_matchable(),
3997                    ])
3998                    .config(|config| {
3999                        config.optional();
4000                    })
4001                    .to_matchable(),
4002                    Sequence::new(vec![
4003                        Ref::keyword("TRACK").to_matchable(),
4004                        Ref::keyword("HISTORY").to_matchable(),
4005                        Ref::keyword("ON").to_matchable(),
4006                        one_of(vec![
4007                            Delimited::new(vec![Ref::new("ColumnReferenceSegment").to_matchable()])
4008                                .to_matchable(),
4009                            Sequence::new(vec![
4010                                Ref::new("StarSegment").to_matchable(),
4011                                Ref::keyword("EXCEPT").to_matchable(),
4012                                Ref::new("BracketedColumnReferenceListGrammar").to_matchable(),
4013                            ])
4014                            .to_matchable(),
4015                        ])
4016                        .to_matchable(),
4017                    ])
4018                    .config(|config| {
4019                        config.optional();
4020                    })
4021                    .to_matchable(),
4022                ])
4023                .to_matchable()
4024            })
4025            .to_matchable()
4026            .into(),
4027        ),
4028    ]);
4029
4030    sparksql_dialect.replace_grammar(
4031        "WildcardExpressionSegment",
4032        ansi::wildcard_expression_segment().copy(
4033            Some(vec![
4034                Ref::new("ExceptClauseSegment").optional().to_matchable(),
4035            ]),
4036            None,
4037            None,
4038            None,
4039            Vec::new(),
4040            false,
4041        ),
4042    );
4043
4044    // A reference to an object.
4045    // allow whitespace
4046    sparksql_dialect.replace_grammar(
4047        "ObjectReferenceSegment",
4048        Delimited::new(vec![
4049            one_of(vec![
4050                Ref::new("SingleIdentifierGrammar").to_matchable(),
4051                Ref::new("IdentifierClauseSegment").to_matchable(),
4052            ])
4053            .to_matchable(),
4054        ])
4055        .config(|config| {
4056            config.delimiter(Ref::new("ObjectReferenceDelimiterGrammar"));
4057            config.terminators = vec![Ref::new("ObjectReferenceTerminatorGrammar").to_matchable()];
4058            config.disallow_gaps();
4059        })
4060        .to_matchable(),
4061    );
4062
4063    sparksql_dialect.add([
4064        (
4065            "ExceptClauseSegment".into(),
4066            NodeMatcher::new(SyntaxKind::SelectExceptClause, |_| {
4067                Sequence::new(vec![
4068                    Ref::keyword("EXCEPT").to_matchable(),
4069                    Bracketed::new(vec![
4070                        Delimited::new(vec![Ref::new("ColumnReferenceSegment").to_matchable()])
4071                            .to_matchable(),
4072                    ])
4073                    .to_matchable(),
4074                ])
4075                .to_matchable()
4076            })
4077            .to_matchable()
4078            .into(),
4079        ),
4080        (
4081            "SelectClauseSegment".into(),
4082            NodeMatcher::new(SyntaxKind::SelectClause, |_| {
4083                Sequence::new(vec![
4084                    Ref::keyword("SELECT").to_matchable(),
4085                    one_of(vec![
4086                        Ref::new("TransformClauseSegment").to_matchable(),
4087                        Sequence::new(vec![
4088                            Ref::new("SelectClauseModifierSegment")
4089                                .optional()
4090                                .to_matchable(),
4091                            MetaSegment::indent().to_matchable(),
4092                            Delimited::new(vec![
4093                                Ref::new("SelectClauseElementSegment").to_matchable(),
4094                            ])
4095                            .config(|config| {
4096                                config.allow_trailing = true;
4097                            })
4098                            .to_matchable(),
4099                        ])
4100                        .to_matchable(),
4101                    ])
4102                    .to_matchable(),
4103                ])
4104                .config(|config| {
4105                    config.terminators =
4106                        vec![Ref::new("SelectClauseTerminatorGrammar").to_matchable()];
4107                    config.parse_mode(ParseMode::GreedyOnceStarted);
4108                })
4109                .to_matchable()
4110            })
4111            .to_matchable()
4112            .into(),
4113        ),
4114        (
4115            "UsingClauseSegment".into(),
4116            NodeMatcher::new(SyntaxKind::UsingClause, |_| {
4117                Sequence::new(vec![
4118                    Ref::keyword("USING").to_matchable(),
4119                    Ref::new("DataSourceFormatSegment").to_matchable(),
4120                ])
4121                .to_matchable()
4122            })
4123            .to_matchable()
4124            .into(),
4125        ),
4126        (
4127            "DataSourceFormatSegment".into(),
4128            NodeMatcher::new(SyntaxKind::DataSourceFormat, |_| {
4129                one_of(vec![
4130                    Ref::new("FileFormatGrammar").to_matchable(),
4131                    Ref::keyword("JDBC").to_matchable(),
4132                    Ref::new("ObjectReferenceSegment").to_matchable(),
4133                ])
4134                .to_matchable()
4135            })
4136            .to_matchable()
4137            .into(),
4138        ),
4139        (
4140            "IcebergTransformationSegment".into(),
4141            NodeMatcher::new(SyntaxKind::IcebergTransformation, |_| {
4142                one_of(vec![
4143                    Sequence::new(vec![
4144                        one_of(vec![
4145                            Ref::keyword("YEARS").to_matchable(),
4146                            Ref::keyword("MONTHS").to_matchable(),
4147                            Ref::keyword("DAYS").to_matchable(),
4148                            Ref::keyword("DATE").to_matchable(),
4149                            Ref::keyword("HOURS").to_matchable(),
4150                            Ref::keyword("DATE_HOUR").to_matchable(),
4151                        ])
4152                        .to_matchable(),
4153                        Bracketed::new(vec![Ref::new("ColumnReferenceSegment").to_matchable()])
4154                            .to_matchable(),
4155                    ])
4156                    .to_matchable(),
4157                    Sequence::new(vec![
4158                        one_of(vec![
4159                            Ref::keyword("BUCKET").to_matchable(),
4160                            Ref::keyword("TRUNCATE").to_matchable(),
4161                        ])
4162                        .to_matchable(),
4163                        Bracketed::new(vec![
4164                            Sequence::new(vec![
4165                                Ref::new("NumericLiteralSegment").to_matchable(),
4166                                Ref::new("CommaSegment").to_matchable(),
4167                                Ref::new("ColumnReferenceSegment").to_matchable(),
4168                            ])
4169                            .to_matchable(),
4170                        ])
4171                        .to_matchable(),
4172                    ])
4173                    .to_matchable(),
4174                ])
4175                .to_matchable()
4176            })
4177            .to_matchable()
4178            .into(),
4179        ),
4180    ]);
4181
4182    sparksql_dialect.add([
4183        (
4184            // Show Functions
4185            "ShowFunctionsGrammar".into(),
4186            Sequence::new(vec![
4187                one_of(vec![
4188                    Ref::keyword("USER").to_matchable(),
4189                    Ref::keyword("SYSTEM").to_matchable(),
4190                    Ref::keyword("ALL").to_matchable(),
4191                ])
4192                .config(|config| {
4193                    config.optional();
4194                })
4195                .to_matchable(),
4196                Ref::keyword("FUNCTIONS").to_matchable(),
4197                one_of(vec![
4198                    // qualified function from a database
4199                    Sequence::new(vec![
4200                        Ref::new("DatabaseReferenceSegment").to_matchable(),
4201                        Ref::new("DotSegment").to_matchable(),
4202                        Ref::new("FunctionNameSegment").to_matchable(),
4203                    ])
4204                    .config(|config| {
4205                        config.disallow_gaps();
4206                        config.optional();
4207                    })
4208                    .to_matchable(),
4209                    // non-qualified function
4210                    Ref::new("FunctionNameSegment").optional().to_matchable(),
4211                    Sequence::new(vec![
4212                        Ref::keyword("LIKE").to_matchable(),
4213                        Ref::new("QuotedLiteralSegment").to_matchable(),
4214                    ])
4215                    .config(|config| {
4216                        config.optional();
4217                    })
4218                    .to_matchable(),
4219                ])
4220                .config(|config| {
4221                    config.optional();
4222                })
4223                .to_matchable(),
4224            ])
4225            .to_matchable()
4226            .into(),
4227        ),
4228        (
4229            "ShowTablesGrammar".into(),
4230            Sequence::new(vec![
4231                Ref::keyword("TABLES").to_matchable(),
4232                Sequence::new(vec![
4233                    one_of(vec![
4234                        Ref::keyword("FROM").to_matchable(),
4235                        Ref::keyword("IN").to_matchable(),
4236                    ])
4237                    .to_matchable(),
4238                    Ref::new("DatabaseReferenceSegment").to_matchable(),
4239                ])
4240                .config(|config| {
4241                    config.optional();
4242                })
4243                .to_matchable(),
4244                Sequence::new(vec![
4245                    Ref::keyword("LIKE").to_matchable(),
4246                    Ref::new("QuotedLiteralSegment").to_matchable(),
4247                ])
4248                .config(|config| {
4249                    config.optional();
4250                })
4251                .to_matchable(),
4252            ])
4253            .to_matchable()
4254            .into(),
4255        ),
4256        (
4257            "ShowDatabasesSchemasGrammar".into(),
4258            Sequence::new(vec![
4259                one_of(vec![
4260                    Ref::keyword("DATABASES").to_matchable(),
4261                    Ref::keyword("SCHEMAS").to_matchable(),
4262                ])
4263                .to_matchable(),
4264                Sequence::new(vec![
4265                    Ref::keyword("LIKE").to_matchable(),
4266                    Ref::new("QuotedLiteralSegment").to_matchable(),
4267                ])
4268                .config(|config| {
4269                    config.optional();
4270                })
4271                .to_matchable(),
4272            ])
4273            .to_matchable()
4274            .into(),
4275        ),
4276        (
4277            "ShowObjectGrammar".into(),
4278            one_of(vec![
4279                Sequence::new(vec![
4280                    one_of(vec![
4281                        Ref::new("ShowFunctionsGrammar").to_matchable(),
4282                        Ref::new("ShowViewsGrammar").to_matchable(),
4283                        Ref::new("ShowTablesGrammar").to_matchable(),
4284                        Ref::new("ShowDatabasesSchemasGrammar").to_matchable(),
4285                        Sequence::new(vec![
4286                            Ref::keyword("CREATE").to_matchable(),
4287                            Ref::keyword("TABLE").to_matchable(),
4288                            Ref::new("TableExpressionSegment").to_matchable(),
4289                            Sequence::new(vec![
4290                                Ref::keyword("AS").to_matchable(),
4291                                Ref::keyword("SERDE").to_matchable(),
4292                            ])
4293                            .config(|config| {
4294                                config.optional();
4295                            })
4296                            .to_matchable(),
4297                        ])
4298                        .to_matchable(),
4299                        Sequence::new(vec![
4300                            Ref::keyword("COLUMNS").to_matchable(),
4301                            Ref::keyword("IN").to_matchable(),
4302                            Ref::new("TableExpressionSegment").to_matchable(),
4303                            Sequence::new(vec![
4304                                Ref::keyword("IN").to_matchable(),
4305                                Ref::new("DatabaseReferenceSegment").to_matchable(),
4306                            ])
4307                            .config(|config| {
4308                                config.optional();
4309                            })
4310                            .to_matchable(),
4311                        ])
4312                        .to_matchable(),
4313                        Sequence::new(vec![
4314                            one_of(vec![
4315                                Ref::keyword("USER").to_matchable(),
4316                                Ref::keyword("SYSTEM").to_matchable(),
4317                                Ref::keyword("ALL").to_matchable(),
4318                            ])
4319                            .config(|config| {
4320                                config.optional();
4321                            })
4322                            .to_matchable(),
4323                            Ref::keyword("FUNCTIONS").to_matchable(),
4324                            one_of(vec![
4325                                Sequence::new(vec![
4326                                    Ref::new("DatabaseReferenceSegment").to_matchable(),
4327                                    Ref::new("DotSegment").to_matchable(),
4328                                    Ref::new("FunctionNameSegment").to_matchable(),
4329                                ])
4330                                .config(|config| {
4331                                    config.disallow_gaps();
4332                                    config.optional();
4333                                })
4334                                .to_matchable(),
4335                                Ref::new("FunctionNameSegment").optional().to_matchable(),
4336                                Sequence::new(vec![
4337                                    Ref::keyword("LIKE").to_matchable(),
4338                                    Ref::new("QuotedLiteralSegment").to_matchable(),
4339                                ])
4340                                .config(|config| {
4341                                    config.optional();
4342                                })
4343                                .to_matchable(),
4344                            ])
4345                            .to_matchable(),
4346                        ])
4347                        .to_matchable(),
4348                        Sequence::new(vec![
4349                            Ref::keyword("PARTITIONS").to_matchable(),
4350                            Ref::new("TableReferenceSegment").to_matchable(),
4351                            Ref::new("PartitionSpecGrammar").optional().to_matchable(),
4352                        ])
4353                        .to_matchable(),
4354                        Sequence::new(vec![
4355                            Ref::keyword("TABLE").to_matchable(),
4356                            Ref::keyword("EXTENDED").to_matchable(),
4357                            Sequence::new(vec![
4358                                one_of(vec![
4359                                    Ref::keyword("IN").to_matchable(),
4360                                    Ref::keyword("FROM").to_matchable(),
4361                                ])
4362                                .to_matchable(),
4363                                Ref::new("DatabaseReferenceSegment").to_matchable(),
4364                            ])
4365                            .config(|config| {
4366                                config.optional();
4367                            })
4368                            .to_matchable(),
4369                            Ref::keyword("LIKE").to_matchable(),
4370                            Ref::new("QuotedLiteralSegment").to_matchable(),
4371                            Ref::new("PartitionSpecGrammar").optional().to_matchable(),
4372                        ])
4373                        .to_matchable(),
4374                        Sequence::new(vec![
4375                            Ref::keyword("TBLPROPERTIES").to_matchable(),
4376                            Ref::new("TableReferenceSegment").to_matchable(),
4377                            Ref::new("BracketedPropertyNameListGrammar")
4378                                .optional()
4379                                .to_matchable(),
4380                        ])
4381                        .to_matchable(),
4382                        Sequence::new(vec![
4383                            Ref::keyword("VIEWS").to_matchable(),
4384                            Sequence::new(vec![
4385                                one_of(vec![
4386                                    Ref::keyword("FROM").to_matchable(),
4387                                    Ref::keyword("IN").to_matchable(),
4388                                ])
4389                                .to_matchable(),
4390                                Ref::new("DatabaseReferenceSegment").to_matchable(),
4391                            ])
4392                            .config(|config| {
4393                                config.optional();
4394                            })
4395                            .to_matchable(),
4396                            Sequence::new(vec![
4397                                Ref::keyword("LIKE").to_matchable(),
4398                                Ref::new("QuotedLiteralSegment").to_matchable(),
4399                            ])
4400                            .config(|config| {
4401                                config.optional();
4402                            })
4403                            .to_matchable(),
4404                        ])
4405                        .to_matchable(),
4406                    ])
4407                    .to_matchable(),
4408                ])
4409                .to_matchable(),
4410            ])
4411            .to_matchable()
4412            .into(),
4413        ),
4414    ]);
4415
4416    sparksql_dialect.replace_grammar(
4417        "FrameClauseSegment",
4418        {
4419            let frame_extent = one_of(vec![
4420                Sequence::new(vec![
4421                    Ref::keyword("CURRENT").to_matchable(),
4422                    Ref::keyword("ROW").to_matchable(),
4423                ])
4424                .to_matchable(),
4425                Sequence::new(vec![
4426                    one_of(vec![
4427                        Ref::new("NumericLiteralSegment").to_matchable(),
4428                        Ref::keyword("UNBOUNDED").to_matchable(),
4429                        Ref::new("IntervalExpressionSegment").to_matchable(),
4430                    ])
4431                    .to_matchable(),
4432                    one_of(vec![
4433                        Ref::keyword("PRECEDING").to_matchable(),
4434                        Ref::keyword("FOLLOWING").to_matchable(),
4435                    ])
4436                    .to_matchable(),
4437                ])
4438                .to_matchable(),
4439            ]);
4440
4441            Sequence::new(vec![
4442                Ref::new("FrameClauseUnitGrammar").to_matchable(),
4443                one_of(vec![
4444                    frame_extent.clone().to_matchable(),
4445                    Sequence::new(vec![
4446                        Ref::keyword("BETWEEN").to_matchable(),
4447                        frame_extent.clone().to_matchable(),
4448                        Ref::keyword("AND").to_matchable(),
4449                        frame_extent.to_matchable(),
4450                    ])
4451                    .to_matchable(),
4452                ])
4453                .to_matchable(),
4454            ])
4455        }
4456        .to_matchable(),
4457    );
4458    sparksql_dialect
4459}
4460
4461use sqruff_lib_core::dialects::init::{DialectConfig, NullDialectConfig};
4462use sqruff_lib_core::value::Value;
4463
4464/// Configuration for the SparkSQL dialect.
4465pub type SparkSQLDialectConfig = NullDialectConfig;
4466
4467pub fn dialect(config: Option<&Value>) -> Dialect {
4468    // Parse and validate dialect configuration, falling back to defaults on failure
4469    let _dialect_config: SparkSQLDialectConfig = config
4470        .map(SparkSQLDialectConfig::from_value)
4471        .unwrap_or_default();
4472
4473    raw_dialect().config(|c| c.expand())
4474}