Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding json format to trino #1307

Merged
merged 1 commit into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions crates/lib-dialects/src/trino.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,21 @@ pub fn dialect() -> Dialect {
Ref::new("ExpressionSegment")
]),
Sequence::new(vec_of_erased![
// Allow an optional DISTINCT keyword here.
Ref::keyword("DISTINCT").optional(),
one_of(vec_of_erased![
// Most functions will be using the delimiited route
// but for COUNT(*) or similar we allow the star segement
// here.
Ref::new("StarSegment"),
Delimited::new(vec_of_erased![Ref::new(
"FunctionContentsExpressionGrammar"
)])
])
]),
Ref::new("OrderByClauseSegment"),
// # used by string_agg (postgres), group_concat (exasol),listagg (snowflake)
// # like a function call: POSITION ( 'QL' IN 'SQL')
Sequence::new(vec_of_erased![
one_of(vec_of_erased![
Ref::new("QuotedLiteralSegment"),
Expand All @@ -205,6 +211,36 @@ pub fn dialect() -> Dialect {
Ref::new("ColumnReferenceSegment")
])
]),
// For JSON_QUERY function
// https://trino.io/docs/current/functions/json.html#json_query
Sequence::new(vec_of_erased![
Ref::new("ExpressionSegment"),
Ref::new("FormatJsonEncodingGrammar").optional(),
Ref::new("CommaSegment"),
Ref::new("ExpressionSegment"),
one_of(vec_of_erased![
Sequence::new(vec_of_erased![
Ref::keyword("WITHOUT"),
Ref::keyword("ARRAY").optional(),
Ref::keyword("WRAPPER"),
]),
Sequence::new(vec_of_erased![
Ref::keyword("WITH"),
one_of(vec_of_erased![
Ref::keyword("CONDITIONAL"),
Ref::keyword("UNCONDITIONAL")
])
.config(|config| {
config.optional();
}),
Ref::keyword("ARRAY").optional(),
Ref::keyword("WRAPPER")
])
])
.config(|config| {
config.optional();
})
]),
Ref::new("IgnoreRespectNullsGrammar"),
Ref::new("IndexColumnDefinitionSegment"),
Ref::new("EmptyStructLiteralSegment"),
Expand All @@ -213,6 +249,26 @@ pub fn dialect() -> Dialect {
.to_matchable()
.into(),
),
(
"FormatJsonEncodingGrammar".into(),
Sequence::new(vec_of_erased![
Ref::keyword("FORMAT"),
Ref::keyword("JSON"),
Sequence::new(vec_of_erased![
Ref::keyword("ENCODING"),
one_of(vec_of_erased![
Ref::keyword("UTF8"),
Ref::keyword("UTF16"),
Ref::keyword("UTF32")
])
.config(|config| {
config.optional();
})
]),
])
.to_matchable()
.into(),
),
]);
trino_dialect.replace_grammar(
"UnorderedSelectStatementSegment",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
select
json_query(payload format json, 'lax $.unstructured.abcd[*].field?(@ > 0.5)' with array wrapper),
json_query(payload format json encoding utf8, 'lax $.unstructured.abcd[*].field?(@ > 0.5)' without array wrapper),
json_query(payload format json encoding utf16, 'lax $.unstructured.abcd[*].field?(@ > 0.5)' with conditional wrapper),
json_query(payload format json encoding utf32, 'lax $.unstructured.abcd[*].field?(@ > 0.5)' with unconditional array wrapper),
json_query(payload format json, 'lax $.unstructured.abcd[*].field?(@ > 0.5)')
;
112 changes: 112 additions & 0 deletions crates/lib-dialects/test/fixtures/dialects/trino/json_functions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
file:
- statement:
- select_statement:
- select_clause:
- keyword: select
- select_clause_element:
- function:
- function_name:
- function_name_identifier: json_query
- bracketed:
- start_bracket: (
- expression:
- column_reference:
- naked_identifier: payload
- expression:
- column_reference:
- naked_identifier: format
- expression:
- column_reference:
- naked_identifier: json
- comma: ','
- expression:
- quoted_literal: '''lax $.unstructured.abcd[*].field?(@ > 0.5)'''
- keyword: with
- keyword: array
- keyword: wrapper
- end_bracket: )
- comma: ','
- select_clause_element:
- function:
- function_name:
- function_name_identifier: json_query
- bracketed:
- start_bracket: (
- expression:
- column_reference:
- naked_identifier: payload
- keyword: format
- keyword: json
- keyword: encoding
- keyword: utf8
- comma: ','
- expression:
- quoted_literal: '''lax $.unstructured.abcd[*].field?(@ > 0.5)'''
- keyword: without
- keyword: array
- keyword: wrapper
- end_bracket: )
- comma: ','
- select_clause_element:
- function:
- function_name:
- function_name_identifier: json_query
- bracketed:
- start_bracket: (
- expression:
- column_reference:
- naked_identifier: payload
- keyword: format
- keyword: json
- keyword: encoding
- keyword: utf16
- comma: ','
- expression:
- quoted_literal: '''lax $.unstructured.abcd[*].field?(@ > 0.5)'''
- keyword: with
- keyword: conditional
- keyword: wrapper
- end_bracket: )
- comma: ','
- select_clause_element:
- function:
- function_name:
- function_name_identifier: json_query
- bracketed:
- start_bracket: (
- expression:
- column_reference:
- naked_identifier: payload
- keyword: format
- keyword: json
- keyword: encoding
- keyword: utf32
- comma: ','
- expression:
- quoted_literal: '''lax $.unstructured.abcd[*].field?(@ > 0.5)'''
- keyword: with
- keyword: unconditional
- keyword: array
- keyword: wrapper
- end_bracket: )
- comma: ','
- select_clause_element:
- function:
- function_name:
- function_name_identifier: json_query
- bracketed:
- start_bracket: (
- expression:
- column_reference:
- naked_identifier: payload
- expression:
- column_reference:
- naked_identifier: format
- expression:
- column_reference:
- naked_identifier: json
- comma: ','
- expression:
- quoted_literal: '''lax $.unstructured.abcd[*].field?(@ > 0.5)'''
- end_bracket: )
- statement_terminator: ;
Loading