Skip to content

Commit

Permalink
Merge 86f97a8 into 425aa1e
Browse files Browse the repository at this point in the history
  • Loading branch information
benfdking authored Feb 7, 2025
2 parents 425aa1e + 86f97a8 commit 349cbab
Show file tree
Hide file tree
Showing 11 changed files with 966 additions and 3 deletions.
155 changes: 152 additions & 3 deletions crates/lib-dialects/src/trino.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,13 @@ pub fn dialect() -> Dialect {
"FunctionContentsGrammar".into(),
AnyNumberOf::new(vec_of_erased![
Ref::new("ExpressionSegment"),
// A Cast-like function
Sequence::new(vec_of_erased![
Ref::new("ExpressionSegment"),
Ref::keyword("AS"),
Ref::new("DatatypeSegment")
]),
// A Trim function
Sequence::new(vec_of_erased![
Ref::new("TrimParametersGrammar"),
Ref::new("ExpressionSegment")
Expand All @@ -171,6 +173,7 @@ pub fn dialect() -> Dialect {
Ref::keyword("FROM"),
Ref::new("ExpressionSegment")
]),
// An extract-like or substring-like function
Sequence::new(vec_of_erased![
one_of(vec_of_erased![
Ref::new("DatetimeUnitSegment"),
Expand Down Expand Up @@ -204,7 +207,8 @@ pub fn dialect() -> Dialect {
]),
Ref::new("IgnoreRespectNullsGrammar"),
Ref::new("IndexColumnDefinitionSegment"),
Ref::new("EmptyStructLiteralSegment")
Ref::new("EmptyStructLiteralSegment"),
Ref::new("ListaggOverflowClauseSegment")
])
.to_matchable()
.into(),
Expand Down Expand Up @@ -275,7 +279,7 @@ pub fn dialect() -> Dialect {
]),
Ref::keyword("ARRAY"),
Ref::keyword("MAP"),
Ref::keyword("ROW"),
Ref::new("RowTypeSegment"),
Ref::keyword("IPADDRESS"),
Ref::keyword("UUID")
])
Expand All @@ -284,6 +288,35 @@ pub fn dialect() -> Dialect {
.to_matchable()
.into(),
),
(
// Expression to construct a ROW datatype.
"RowTypeSegment".into(),
Sequence::new(vec_of_erased![
Ref::keyword("ROW"),
Ref::new("RowTypeSchemaSegment").optional()
])
.to_matchable()
.into(),
),
(
// Expression to construct the schema of a ROW datatype.
"RowTypeSchemaSegment".into(),
Bracketed::new(vec_of_erased![Delimited::new(vec_of_erased![
// Comma-separated list of field names/types
Sequence::new(vec_of_erased![one_of(vec_of_erased![
// ParameterNames can look like Datatypes so can't use
// Optional=True here and instead do a OneOf in order
// with DataType only first, followed by both.
Ref::new("DatatypeSegment"),
Sequence::new(vec_of_erased![
Ref::new("ParameterNameSegment"),
Ref::new("DatatypeSegment"),
])
]),])
])])
.to_matchable()
.into(),
),
(
"OverlapsClauseSegment".into(),
NodeMatcher::new(SyntaxKind::OverlapsClause, Nothing::new().to_matchable())
Expand All @@ -304,7 +337,10 @@ pub fn dialect() -> Dialect {
trino_dialect.replace_grammar(
"StatementSegment",
super::ansi::statement_segment().copy(
None,
Some(vec_of_erased![
Ref::new("AnalyzeStatementSegment"),
Ref::new("CommentOnStatementSegment")
]),
None,
None,
Some(vec_of_erased![Ref::new("TransactionStatementSegment")]),
Expand All @@ -314,6 +350,119 @@ pub fn dialect() -> Dialect {
);

trino_dialect.add([
// An 'ANALYZE' statement.
// As per docs https://trino.io/docs/current/sql/analyze.html
(
"AnalyzeStatementSegment".into(),
Sequence::new(vec_of_erased![
Ref::keyword("ANALYZE"),
Ref::new("TableReferenceSegment"),
Sequence::new(vec_of_erased![
Ref::keyword("WITH"),
Bracketed::new(vec_of_erased![Delimited::new(vec_of_erased![
Ref::new("ParameterNameSegment"),
Ref::new("EqualsSegment"),
Ref::new("ExpressionSegment"),
]),]),
])
.config(|config| {
config.optional();
})
])
.to_matchable()
.into(),
),
(
"PostFunctionGrammar".into(),
super::ansi::raw_dialect()
.grammar("PostFunctionGrammar")
.copy(
Some(vec_of_erased![Ref::new("WithinGroupClauseSegment")]),
None,
None,
Some(vec_of_erased![Ref::new("TransactionStatementSegment")]),
Vec::new(),
false,
)
.into(),
),
(
// ON OVERFLOW clause of listagg function.
// https://trino.io/docs/current/functions/aggregate.html#array_agg
"ListaggOverflowClauseSegment".into(),
Sequence::new(vec_of_erased![
Ref::keyword("ON"),
Ref::keyword("OVERFLOW"),
one_of(vec_of_erased![
Ref::keyword("ERROR"),
Sequence::new(vec_of_erased![
Ref::keyword("TRUNCATE"),
Ref::new("SingleQuotedIdentifierSegment").optional(),
one_of(vec_of_erased![
Ref::keyword("WITH"),
Ref::keyword("WITHOUT")
])
.config(|config| {
config.optional();
}),
Ref::keyword("COUNT").optional()
]),
]),
])
.to_matchable()
.into(),
),
(
// An WITHIN GROUP clause for window functions.
// https://trino.io/docs/current/functions/aggregate.html#array_agg
// Trino supports an optional FILTER during aggregation that comes
// immediately after the WITHIN GROUP clause.
// https://trino.io/docs/current/functions/aggregate.html#filtering-during-aggregation
"WithinGroupClauseSegment".into(),
Sequence::new(vec_of_erased![
Ref::keyword("WITHIN"),
Ref::keyword("GROUP"),
Bracketed::new(vec_of_erased![Ref::new("OrderByClauseSegment")]),
Ref::new("FilterClauseGrammar").optional(),
])
.to_matchable()
.into(),
),
(
// `COMMENT ON` statement.
// https://trino.io/docs/current/sql/comment.html
"CommentOnStatementSegment".into(),
Sequence::new(vec_of_erased![
Ref::keyword("COMMENT"),
Ref::keyword("ON"),
Sequence::new(vec_of_erased![
one_of(vec_of_erased![
Sequence::new(vec_of_erased![
one_of(vec_of_erased![
Ref::keyword("TABLE"),
// TODO: Create a ViewReferenceSegment
Ref::keyword("VIEW"),
]),
Ref::new("TableReferenceSegment"),
]),
Sequence::new(vec_of_erased![
Ref::keyword("COLUMN"),
// TODO: Does this correctly emit a Table Reference?
Ref::new("ColumnReferenceSegment"),
]),
]),
Sequence::new(vec_of_erased![
Ref::keyword("IS"),
one_of(vec_of_erased![
Ref::new("QuotedLiteralSegment"),
Ref::keyword("NULL")
]),
]),
]),
])
.to_matchable()
.into(),
),
(
"IntervalExpressionSegment".into(),
NodeMatcher::new(
Expand Down
5 changes: 5 additions & 0 deletions crates/lib-dialects/test/fixtures/dialects/trino/analyze.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ANALYZE web;

ANALYZE hive.default.stores;

ANALYZE hive.default.sales WITH (partitions = 1);
34 changes: 34 additions & 0 deletions crates/lib-dialects/test/fixtures/dialects/trino/analyze.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
file:
- statement:
- keyword: ANALYZE
- table_reference:
- naked_identifier: web
- statement_terminator: ;
- statement:
- keyword: ANALYZE
- table_reference:
- naked_identifier: hive
- dot: .
- naked_identifier: default
- dot: .
- naked_identifier: stores
- statement_terminator: ;
- statement:
- keyword: ANALYZE
- table_reference:
- naked_identifier: hive
- dot: .
- naked_identifier: default
- dot: .
- naked_identifier: sales
- keyword: WITH
- bracketed:
- start_bracket: (
- expression:
- column_reference:
- naked_identifier: partitions
- comparison_operator:
- raw_comparison_operator: =
- numeric_literal: '1'
- end_bracket: )
- statement_terminator: ;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
COMMENT ON TABLE abc IS 'xyz';
COMMENT ON VIEW abc IS 'xyz';
COMMENT ON COLUMN table1.column1 IS 'abc';
COMMENT ON COLUMN table1.column2 IS 'abc';
COMMENT ON COLUMN table1.column3 IS 'abc.';
52 changes: 52 additions & 0 deletions crates/lib-dialects/test/fixtures/dialects/trino/comment_on.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
file:
- statement:
- keyword: COMMENT
- keyword: ON
- keyword: TABLE
- table_reference:
- naked_identifier: abc
- keyword: IS
- quoted_literal: '''xyz'''
- statement_terminator: ;
- statement:
- keyword: COMMENT
- keyword: ON
- keyword: VIEW
- table_reference:
- naked_identifier: abc
- keyword: IS
- quoted_literal: '''xyz'''
- statement_terminator: ;
- statement:
- keyword: COMMENT
- keyword: ON
- keyword: COLUMN
- column_reference:
- naked_identifier: table1
- dot: .
- naked_identifier: column1
- keyword: IS
- quoted_literal: '''abc'''
- statement_terminator: ;
- statement:
- keyword: COMMENT
- keyword: ON
- keyword: COLUMN
- column_reference:
- naked_identifier: table1
- dot: .
- naked_identifier: column2
- keyword: IS
- quoted_literal: '''abc'''
- statement_terminator: ;
- statement:
- keyword: COMMENT
- keyword: ON
- keyword: COLUMN
- column_reference:
- naked_identifier: table1
- dot: .
- naked_identifier: column3
- keyword: IS
- quoted_literal: '''abc.'''
- statement_terminator: ;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT
name,
CAST(ROW(price, store) AS ROW(price REAL, store VARCHAR)) AS data_row
FROM customers
53 changes: 53 additions & 0 deletions crates/lib-dialects/test/fixtures/dialects/trino/row_datatype.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
file:
- statement:
- select_statement:
- select_clause:
- keyword: SELECT
- select_clause_element:
- column_reference:
- naked_identifier: name
- comma: ','
- select_clause_element:
- function:
- function_name:
- function_name_identifier: CAST
- bracketed:
- start_bracket: (
- expression:
- function:
- function_name:
- function_name_identifier: ROW
- bracketed:
- start_bracket: (
- expression:
- column_reference:
- naked_identifier: price
- comma: ','
- expression:
- column_reference:
- naked_identifier: store
- end_bracket: )
- keyword: AS
- data_type:
- keyword: ROW
- bracketed:
- start_bracket: (
- parameter: price
- data_type:
- keyword: REAL
- comma: ','
- parameter: store
- data_type:
- keyword: VARCHAR
- end_bracket: )
- end_bracket: )
- alias_expression:
- keyword: AS
- naked_identifier: data_row
- from_clause:
- keyword: FROM
- from_expression:
- from_expression_element:
- table_expression:
- table_reference:
- naked_identifier: customers
Loading

0 comments on commit 349cbab

Please sign in to comment.