From 003784422b953b66e463cb4ccccaf5a41c521faa Mon Sep 17 00:00:00 2001 From: Ben King <9087625+benfdking@users.noreply.github.com> Date: Wed, 18 Dec 2024 10:00:45 +0100 Subject: [PATCH] refactor: move show views into own statement for databricks override --- crates/lib-dialects/src/sparksql.rs | 248 ++++++++++++++++------------ 1 file changed, 142 insertions(+), 106 deletions(-) diff --git a/crates/lib-dialects/src/sparksql.rs b/crates/lib-dialects/src/sparksql.rs index 58c5b738..a6f3cd8b 100644 --- a/crates/lib-dialects/src/sparksql.rs +++ b/crates/lib-dialects/src/sparksql.rs @@ -396,6 +396,7 @@ pub fn dialect() -> Dialect { .into(), ), ]); + sparksql_dialect.add([ ( "FileLiteralSegment".into(), @@ -2569,6 +2570,29 @@ pub fn dialect() -> Dialect { .to_matchable() .into(), ), + ( + "ShowViewsStatement".into(), + Sequence::new(vec_of_erased![ + Ref::keyword("SHOW"), + Ref::keyword("VIEWS"), + Sequence::new(vec_of_erased![ + one_of(vec_of_erased![Ref::keyword("FROM"), Ref::keyword("IN")]), + Ref::new("DatabaseReferenceSegment") + ]) + .config(|config| { + config.optional(); + }), + Sequence::new(vec_of_erased![ + Ref::keyword("LIKE"), + Ref::new("QuotedLiteralSegment") + ]) + .config(|config| { + config.optional(); + }) + ]) + .to_matchable() + .into(), + ), ( "SetStatementSegment".into(), NodeMatcher::new( @@ -2593,67 +2617,40 @@ pub fn dialect() -> Dialect { "ShowStatement".into(), NodeMatcher::new( SyntaxKind::ShowStatement, - Sequence::new(vec_of_erased![ - Ref::keyword("SHOW"), - one_of(vec_of_erased![ - Sequence::new(vec_of_erased![ - Ref::keyword("CREATE"), - Ref::keyword("TABLE"), - Ref::new("TableExpressionSegment"), - Sequence::new(vec_of_erased![ - Ref::keyword("AS"), - Ref::keyword("SERDE") - ]) - .config(|config| { - config.optional(); - }) - ]), - Sequence::new(vec_of_erased![ - Ref::keyword("COLUMNS"), - Ref::keyword("IN"), - Ref::new("TableExpressionSegment"), + one_of(vec_of_erased![ + Ref::new("ShowViewsStatement"), + Sequence::new(vec_of_erased![ + Ref::keyword("SHOW"), + one_of(vec_of_erased![ Sequence::new(vec_of_erased![ - Ref::keyword("IN"), - Ref::new("DatabaseReferenceSegment") - ]) - .config(|config| { - config.optional(); - }) - ]), - Sequence::new(vec_of_erased![ - one_of(vec_of_erased![ - Ref::keyword("DATABASES"), - Ref::keyword("SCHEMAS") + Ref::keyword("CREATE"), + Ref::keyword("TABLE"), + Ref::new("TableExpressionSegment"), + Sequence::new(vec_of_erased![ + Ref::keyword("AS"), + Ref::keyword("SERDE") + ]) + .config(|config| { + config.optional(); + }) ]), Sequence::new(vec_of_erased![ - Ref::keyword("LIKE"), - Ref::new("QuotedLiteralSegment") - ]) - .config(|config| { - config.optional(); - }) - ]), - Sequence::new(vec_of_erased![ - one_of(vec_of_erased![ - Ref::keyword("USER"), - Ref::keyword("SYSTEM"), - Ref::keyword("ALL") - ]) - .config(|config| { - config.optional(); - }), - Ref::keyword("FUNCTIONS"), - one_of(vec_of_erased![ + Ref::keyword("COLUMNS"), + Ref::keyword("IN"), + Ref::new("TableExpressionSegment"), Sequence::new(vec_of_erased![ - Ref::new("DatabaseReferenceSegment"), - Ref::new("DotSegment"), - Ref::new("FunctionNameSegment") + Ref::keyword("IN"), + Ref::new("DatabaseReferenceSegment") ]) .config(|config| { - config.disallow_gaps(); config.optional(); - }), - Ref::new("FunctionNameSegment").optional(), + }) + ]), + Sequence::new(vec_of_erased![ + one_of(vec_of_erased![ + Ref::keyword("DATABASES"), + Ref::keyword("SCHEMAS") + ]), Sequence::new(vec_of_erased![ Ref::keyword("LIKE"), Ref::new("QuotedLiteralSegment") @@ -2661,65 +2658,104 @@ pub fn dialect() -> Dialect { .config(|config| { config.optional(); }) - ]) - ]), - Sequence::new(vec_of_erased![ - Ref::keyword("PARTITIONS"), - Ref::new("TableReferenceSegment"), - Ref::new("PartitionSpecGrammar").optional() - ]), - Sequence::new(vec_of_erased![ - Ref::keyword("TABLE"), - Ref::keyword("EXTENDED"), + ]), Sequence::new(vec_of_erased![ - one_of(vec_of_erased![Ref::keyword("IN"), Ref::keyword("FROM")]), - Ref::new("DatabaseReferenceSegment") - ]) - .config(|config| { - config.optional(); - }), - Ref::keyword("LIKE"), - Ref::new("QuotedLiteralSegment"), - Ref::new("PartitionSpecGrammar").optional() - ]), - Sequence::new(vec_of_erased![ - Ref::keyword("TABLES"), + one_of(vec_of_erased![ + Ref::keyword("USER"), + Ref::keyword("SYSTEM"), + Ref::keyword("ALL") + ]) + .config(|config| { + config.optional(); + }), + Ref::keyword("FUNCTIONS"), + one_of(vec_of_erased![ + Sequence::new(vec_of_erased![ + Ref::new("DatabaseReferenceSegment"), + Ref::new("DotSegment"), + Ref::new("FunctionNameSegment") + ]) + .config(|config| { + config.disallow_gaps(); + config.optional(); + }), + Ref::new("FunctionNameSegment").optional(), + Sequence::new(vec_of_erased![ + Ref::keyword("LIKE"), + Ref::new("QuotedLiteralSegment") + ]) + .config(|config| { + config.optional(); + }) + ]) + ]), Sequence::new(vec_of_erased![ - one_of(vec_of_erased![Ref::keyword("FROM"), Ref::keyword("IN")]), - Ref::new("DatabaseReferenceSegment") - ]) - .config(|config| { - config.optional(); - }), + Ref::keyword("PARTITIONS"), + Ref::new("TableReferenceSegment"), + Ref::new("PartitionSpecGrammar").optional() + ]), Sequence::new(vec_of_erased![ + Ref::keyword("TABLE"), + Ref::keyword("EXTENDED"), + Sequence::new(vec_of_erased![ + one_of(vec_of_erased![ + Ref::keyword("IN"), + Ref::keyword("FROM") + ]), + Ref::new("DatabaseReferenceSegment") + ]) + .config(|config| { + config.optional(); + }), Ref::keyword("LIKE"), - Ref::new("QuotedLiteralSegment") - ]) - .config(|config| { - config.optional(); - }) - ]), - Sequence::new(vec_of_erased![ - Ref::keyword("TBLPROPERTIES"), - Ref::new("TableReferenceSegment"), - Ref::new("BracketedPropertyNameListGrammar").optional() - ]), - Sequence::new(vec_of_erased![ - Ref::keyword("VIEWS"), + Ref::new("QuotedLiteralSegment"), + Ref::new("PartitionSpecGrammar").optional() + ]), Sequence::new(vec_of_erased![ - one_of(vec_of_erased![Ref::keyword("FROM"), Ref::keyword("IN")]), - Ref::new("DatabaseReferenceSegment") - ]) - .config(|config| { - config.optional(); - }), + Ref::keyword("TABLES"), + Sequence::new(vec_of_erased![ + one_of(vec_of_erased![ + Ref::keyword("FROM"), + Ref::keyword("IN") + ]), + Ref::new("DatabaseReferenceSegment") + ]) + .config(|config| { + config.optional(); + }), + Sequence::new(vec_of_erased![ + Ref::keyword("LIKE"), + Ref::new("QuotedLiteralSegment") + ]) + .config(|config| { + config.optional(); + }) + ]), Sequence::new(vec_of_erased![ - Ref::keyword("LIKE"), - Ref::new("QuotedLiteralSegment") + Ref::keyword("TBLPROPERTIES"), + Ref::new("TableReferenceSegment"), + Ref::new("BracketedPropertyNameListGrammar").optional() + ]), + Sequence::new(vec_of_erased![ + Ref::keyword("VIEWS"), + Sequence::new(vec_of_erased![ + one_of(vec_of_erased![ + Ref::keyword("FROM"), + Ref::keyword("IN") + ]), + Ref::new("DatabaseReferenceSegment") + ]) + .config(|config| { + config.optional(); + }), + Sequence::new(vec_of_erased![ + Ref::keyword("LIKE"), + Ref::new("QuotedLiteralSegment") + ]) + .config(|config| { + config.optional(); + }) ]) - .config(|config| { - config.optional(); - }) ]) ]) ])