Skip to content

Commit

Permalink
feat: adding array parsing to trino
Browse files Browse the repository at this point in the history
  • Loading branch information
benfdking committed Feb 8, 2025
1 parent f59fe72 commit 75ac84a
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 6 deletions.
37 changes: 31 additions & 6 deletions crates/lib-dialects/src/trino.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ pub fn dialect() -> Dialect {
),
(
"StartAngleBracketSegment".into(),
StringParser::new("<", SyntaxKind::Symbol)
StringParser::new("<", SyntaxKind::StartAngleBracket)
.to_matchable()
.into(),
),
(
"EndAngleBracketSegment".into(),
StringParser::new(">", SyntaxKind::Symbol)
StringParser::new(">", SyntaxKind::EndAngleBracket)
.to_matchable()
.into(),
),
Expand All @@ -91,14 +91,15 @@ pub fn dialect() -> Dialect {
),
]);

trino_dialect
.bracket_sets_mut("angle_bracket_pairs")
.extend([(
trino_dialect.update_bracket_sets(
"angle_bracket_pairs",
vec![(
"angle",
"StartAngleBracketSegment",
"EndAngleBracketSegment",
false,
)]);
)],
);

trino_dialect.add([
(
Expand Down Expand Up @@ -347,6 +348,30 @@ pub fn dialect() -> Dialect {
.to_matchable(),
);

trino_dialect.replace_grammar(
"ArrayTypeSegment",
Sequence::new(vec_of_erased![
Ref::keyword("ARRAY"),
Ref::new("ArrayTypeSchemaSegment").optional()
])
.to_matchable(),
);

trino_dialect.add([(
"ArrayTypeSchemaSegment".into(),
one_of(vec_of_erased![
Bracketed::new(vec_of_erased![Ref::new("DatatypeSegment")]).config(|config| {
config.bracket_pairs_set = "angle_bracket_pairs";
config.bracket_type = "angle";
}),
Bracketed::new(vec_of_erased![Ref::new("DatatypeSegment")]).config(|config| {
config.bracket_type = "round";
})
])
.to_matchable()
.into(),
)]);

trino_dialect.replace_grammar(
"GroupByClauseSegment",
Sequence::new(vec_of_erased![
Expand Down
11 changes: 11 additions & 0 deletions crates/lib-dialects/test/fixtures/dialects/trino/array.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SELECT ARRAY[1,2] || ARRAY[3,4];

SELECT ARRAY[ARRAY['meeting', 'lunch'], ARRAY['training', 'presentation']];

SELECT column FROM UNNEST(ARRAY[1, 2]);

SELECT FILTER(ARRAY[5, -6, NULL, 7], x -> x > 0);

SELECT ANY_MATCH(ARRAY[5, -6, NULL, 7], x -> x > 0);

SELECT ELEMENT_AT(ARRAY['apple', 'banana', 'orange'], 2);
190 changes: 190 additions & 0 deletions crates/lib-dialects/test/fixtures/dialects/trino/array.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
file:
- statement:
- select_statement:
- select_clause:
- keyword: SELECT
- select_clause_element:
- expression:
- typed_array_literal:
- array_type:
- keyword: ARRAY
- array_literal:
- start_square_bracket: '['
- numeric_literal: '1'
- comma: ','
- numeric_literal: '2'
- end_square_bracket: ']'
- binary_operator:
- pipe: '|'
- pipe: '|'
- typed_array_literal:
- array_type:
- keyword: ARRAY
- array_literal:
- start_square_bracket: '['
- numeric_literal: '3'
- comma: ','
- numeric_literal: '4'
- end_square_bracket: ']'
- statement_terminator: ;
- statement:
- select_statement:
- select_clause:
- keyword: SELECT
- select_clause_element:
- typed_array_literal:
- array_type:
- keyword: ARRAY
- array_literal:
- start_square_bracket: '['
- typed_array_literal:
- array_type:
- keyword: ARRAY
- array_literal:
- start_square_bracket: '['
- quoted_literal: '''meeting'''
- comma: ','
- quoted_literal: '''lunch'''
- end_square_bracket: ']'
- comma: ','
- typed_array_literal:
- array_type:
- keyword: ARRAY
- array_literal:
- start_square_bracket: '['
- quoted_literal: '''training'''
- comma: ','
- quoted_literal: '''presentation'''
- end_square_bracket: ']'
- end_square_bracket: ']'
- statement_terminator: ;
- statement:
- select_statement:
- select_clause:
- keyword: SELECT
- select_clause_element:
- column_reference:
- naked_identifier: column
- from_clause:
- keyword: FROM
- from_expression:
- from_expression_element:
- table_expression:
- function:
- function_name:
- function_name_identifier: UNNEST
- bracketed:
- start_bracket: (
- expression:
- typed_array_literal:
- array_type:
- keyword: ARRAY
- array_literal:
- start_square_bracket: '['
- numeric_literal: '1'
- comma: ','
- numeric_literal: '2'
- end_square_bracket: ']'
- end_bracket: )
- statement_terminator: ;
- statement:
- select_statement:
- select_clause:
- keyword: SELECT
- select_clause_element:
- function:
- function_name:
- function_name_identifier: FILTER
- bracketed:
- start_bracket: (
- expression:
- typed_array_literal:
- array_type:
- keyword: ARRAY
- array_literal:
- start_square_bracket: '['
- numeric_literal: '5'
- comma: ','
- numeric_literal:
- sign_indicator: '-'
- numeric_literal: '6'
- comma: ','
- null_literal: 'NULL'
- comma: ','
- numeric_literal: '7'
- end_square_bracket: ']'
- comma: ','
- parameter: x
- symbol: ->
- expression:
- column_reference:
- naked_identifier: x
- comparison_operator:
- raw_comparison_operator: '>'
- numeric_literal: '0'
- end_bracket: )
- statement_terminator: ;
- statement:
- select_statement:
- select_clause:
- keyword: SELECT
- select_clause_element:
- function:
- function_name:
- function_name_identifier: ANY_MATCH
- bracketed:
- start_bracket: (
- expression:
- typed_array_literal:
- array_type:
- keyword: ARRAY
- array_literal:
- start_square_bracket: '['
- numeric_literal: '5'
- comma: ','
- numeric_literal:
- sign_indicator: '-'
- numeric_literal: '6'
- comma: ','
- null_literal: 'NULL'
- comma: ','
- numeric_literal: '7'
- end_square_bracket: ']'
- comma: ','
- parameter: x
- symbol: ->
- expression:
- column_reference:
- naked_identifier: x
- comparison_operator:
- raw_comparison_operator: '>'
- numeric_literal: '0'
- end_bracket: )
- statement_terminator: ;
- statement:
- select_statement:
- select_clause:
- keyword: SELECT
- select_clause_element:
- function:
- function_name:
- function_name_identifier: ELEMENT_AT
- bracketed:
- start_bracket: (
- expression:
- typed_array_literal:
- array_type:
- keyword: ARRAY
- array_literal:
- start_square_bracket: '['
- quoted_literal: '''apple'''
- comma: ','
- quoted_literal: '''banana'''
- comma: ','
- quoted_literal: '''orange'''
- end_square_bracket: ']'
- comma: ','
- expression:
- numeric_literal: '2'
- end_bracket: )
- statement_terminator: ;

0 comments on commit 75ac84a

Please sign in to comment.