Skip to content

Commit

Permalink
Trino Dialect: update ARRAY type handling
Browse files Browse the repository at this point in the history
Trino supports type casting arrays, and the syntax accepts ARRAY<DTYPE> and ARRAY(DTYPE)
  • Loading branch information
Kirk Hansen committed Aug 30, 2024
1 parent 4d0185a commit 492ab45
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 6 deletions.
41 changes: 36 additions & 5 deletions src/sqlfluff/dialects/dialect_trino.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@

trino_dialect.add(
RightArrowOperator=StringParser("->", SymbolSegment, type="binary_operator"),
StartAngleBracketSegment=StringParser(
"<", SymbolSegment, type="start_angle_bracket"
),
EndAngleBracketSegment=StringParser(">", SymbolSegment, type="end_angle_bracket"),
)

trino_dialect.bracket_sets("angle_bracket_pairs").update(
[
("angle", "StartAngleBracketSegment", "EndAngleBracketSegment", False),
]
)

trino_dialect.patch_lexer_matchers(
Expand Down Expand Up @@ -258,7 +268,7 @@ class DatatypeSegment(BaseSegment):
Sequence(OneOf("WITH", "WITHOUT"), "TIME", "ZONE", optional=True),
),
# Structural
"ARRAY",
Ref("ArrayTypeSegment"),
"MAP",
Ref("RowTypeSegment"),
# Others
Expand Down Expand Up @@ -466,13 +476,34 @@ class ListaggOverflowClauseSegment(BaseSegment):


class ArrayTypeSegment(ansi.ArrayTypeSegment):
"""Prefix for array literals.
"""Prefix for array literals optionally specifying the type."""

type = "array_type"
match_grammar = Sequence(
"ARRAY",
Ref("ArrayTypeSchemaSegment", optional=True),
)


Trino supports "ARRAY"
class ArrayTypeSchemaSegment(ansi.ArrayTypeSegment):
"""Data type segment of the array.
Trino supports ARRAY(DATA_TYPE) and ARRAY<DATA_TYPE>
"""

type = "array_type"
match_grammar = Ref.keyword("ARRAY")
type = "array_type_schema"
match_grammar = OneOf(
Bracketed(
Ref("DatatypeSegment"),
bracket_pairs_set="angle_bracket_pairs",
bracket_type="angle",
),
Bracketed(
Ref("DatatypeSegment"),
bracket_pairs_set="bracket_pairs",
bracket_type="round",
),
)


class GroupByClauseSegment(BaseSegment):
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/dialects/trino/array.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ 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);

SELECT CAST(ARRAY[1,2] AS ARRAY<DOUBLE>);

SELECT CAST(ARRAY[1,2] AS ARRAY(DOUBLE));

SELECT CAST(JSON_PARSE('[1, 2, 3, 4, 5]') AS ARRAY<DOUBLE>);
99 changes: 98 additions & 1 deletion test/fixtures/dialects/trino/array.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# computed by SQLFluff when running the tests. Please run
# `python test/generate_parse_fixture_yml.py` to generate them after adding or
# altering SQL files.
_hash: 08a22fad6dd8609d2246337125972253f4c412c21f1df040fd2349a0e9e4a85c
_hash: d0bf6b8b24c65e7cdb438e4729eb7ce5c42cb21c0eacd859a76bfac0b021f319
file:
- statement:
select_statement:
Expand Down Expand Up @@ -200,3 +200,100 @@ file:
numeric_literal: '2'
- end_bracket: )
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
function:
function_name:
function_name_identifier: CAST
function_contents:
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: ']'
keyword: AS
data_type:
array_type:
keyword: ARRAY
array_type_schema:
start_angle_bracket: <
data_type:
keyword: DOUBLE
end_angle_bracket: '>'
end_bracket: )
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
function:
function_name:
function_name_identifier: CAST
function_contents:
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: ']'
keyword: AS
data_type:
array_type:
keyword: ARRAY
array_type_schema:
bracketed:
start_bracket: (
data_type:
keyword: DOUBLE
end_bracket: )
end_bracket: )
- statement_terminator: ;
- statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
function:
function_name:
function_name_identifier: CAST
function_contents:
bracketed:
start_bracket: (
expression:
function:
function_name:
function_name_identifier: JSON_PARSE
function_contents:
bracketed:
start_bracket: (
expression:
quoted_literal: "'[1, 2, 3, 4, 5]'"
end_bracket: )
keyword: AS
data_type:
array_type:
keyword: ARRAY
array_type_schema:
start_angle_bracket: <
data_type:
keyword: DOUBLE
end_angle_bracket: '>'
end_bracket: )
- statement_terminator: ;

0 comments on commit 492ab45

Please sign in to comment.