Skip to content

Commit c64b31e

Browse files
committed
generalize struct support and add databricks
1 parent 4ab3ab9 commit c64b31e

File tree

7 files changed

+108
-15
lines changed

7 files changed

+108
-15
lines changed

src/ast/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -931,16 +931,16 @@ pub enum Expr {
931931
Rollup(Vec<Vec<Expr>>),
932932
/// ROW / TUPLE a single value, such as `SELECT (1, 2)`
933933
Tuple(Vec<Expr>),
934-
/// `BigQuery` specific `Struct` literal expression [1]
934+
/// `Struct` literal expression
935935
/// Syntax:
936936
/// ```sql
937937
/// STRUCT<[field_name] field_type, ...>( expr1 [, ... ])
938938
/// ```
939-
/// [1]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#struct_type
940939
Struct {
941940
/// Struct values.
942941
values: Vec<Expr>,
943-
/// Struct field definitions.
942+
/// BigQuery specific: Struct field definitions.
943+
/// see https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#struct_type
944944
fields: Vec<StructField>,
945945
},
946946
/// `BigQuery` specific: An named expression in a typeless struct [1]

src/dialect/bigquery.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,14 @@ impl Dialect for BigQueryDialect {
7272
fn require_interval_qualifier(&self) -> bool {
7373
true
7474
}
75+
76+
// See https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#constructing_a_struct
77+
fn supports_struct_literal(&self) -> bool {
78+
true
79+
}
80+
81+
// See https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#typed_struct_syntax
82+
fn supports_typed_struct_syntax(&self) -> bool {
83+
true
84+
}
7585
}

src/dialect/databricks.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,9 @@ impl Dialect for DatabricksDialect {
5959
fn require_interval_qualifier(&self) -> bool {
6060
true
6161
}
62+
63+
// See https://docs.databricks.com/en/sql/language-manual/functions/struct.html
64+
fn supports_struct_literal(&self) -> bool {
65+
true
66+
}
6267
}

src/dialect/generic.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,12 @@ impl Dialect for GenericDialect {
123123
fn supports_named_fn_args_with_assignment_operator(&self) -> bool {
124124
true
125125
}
126+
127+
fn supports_struct_literal(&self) -> bool {
128+
true
129+
}
130+
131+
fn supports_typed_struct_syntax(&self) -> bool {
132+
true
133+
}
126134
}

src/dialect/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,26 @@ pub trait Dialect: Debug + Any {
375375
false
376376
}
377377

378+
/// Return true if the dialect supports the STRUCT literal
379+
///
380+
/// Example
381+
/// ```sql
382+
/// SELECT STRUCT(1 as one, 'foo' as foo, false)
383+
/// ```
384+
fn supports_struct_literal(&self) -> bool {
385+
false
386+
}
387+
388+
/// Return true if the dialect supports typed struct syntax
389+
///
390+
/// Example for bigquery
391+
/// ```sql
392+
/// SELECT STRUCT<x int64, y string>(1, 'foo')
393+
/// ```
394+
fn supports_typed_struct_syntax(&self) -> bool {
395+
false
396+
}
397+
378398
/// Dialect-specific infix parser override
379399
///
380400
/// This method is called to parse the next infix expression.

src/parser/mod.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,9 +1123,8 @@ impl<'a> Parser<'a> {
11231123
Keyword::MATCH if dialect_of!(self is MySqlDialect | GenericDialect) => {
11241124
Ok(Some(self.parse_match_against()?))
11251125
}
1126-
Keyword::STRUCT if dialect_of!(self is BigQueryDialect | GenericDialect) => {
1127-
self.prev_token();
1128-
Ok(Some(self.parse_bigquery_struct_literal()?))
1126+
Keyword::STRUCT if self.dialect.supports_struct_literal() => {
1127+
Ok(Some(self.parse_struct_literal()?))
11291128
}
11301129
Keyword::PRIOR if matches!(self.state, ParserState::ConnectBy) => {
11311130
let expr = self.parse_subexpr(self.dialect.prec_value(Precedence::PlusMinus))?;
@@ -2383,22 +2382,28 @@ impl<'a> Parser<'a> {
23832382
}
23842383
}
23852384

2386-
/// Bigquery specific: Parse a struct literal
23872385
/// Syntax
23882386
/// ```sql
2389-
/// -- typed
2387+
/// -- typed, specific to bigquery
23902388
/// STRUCT<[field_name] field_type, ...>( expr1 [, ... ])
23912389
/// -- typeless
23922390
/// STRUCT( expr1 [AS field_name] [, ... ])
23932391
/// ```
2394-
fn parse_bigquery_struct_literal(&mut self) -> Result<Expr, ParserError> {
2395-
let (fields, trailing_bracket) =
2396-
self.parse_struct_type_def(Self::parse_struct_field_def)?;
2397-
if trailing_bracket.0 {
2398-
return parser_err!(
2392+
fn parse_struct_literal(&mut self) -> Result<Expr, ParserError> {
2393+
let mut fields = vec![];
2394+
// Typed struct syntax is only supported by BigQuery
2395+
// https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#typed_struct_syntax
2396+
if self.dialect.supports_typed_struct_syntax() {
2397+
self.prev_token();
2398+
let trailing_bracket;
2399+
(fields, trailing_bracket) =
2400+
self.parse_struct_type_def(Self::parse_struct_field_def)?;
2401+
if trailing_bracket.0 {
2402+
return parser_err!(
23992403
"unmatched > in STRUCT literal",
24002404
self.peek_token().span.start
24012405
);
2406+
}
24022407
}
24032408

24042409
self.expect_token(&Token::LParen)?;
@@ -2409,13 +2414,13 @@ impl<'a> Parser<'a> {
24092414
Ok(Expr::Struct { values, fields })
24102415
}
24112416

2412-
/// Parse an expression value for a bigquery struct [1]
2417+
/// Parse an expression value for a struct literal
24132418
/// Syntax
24142419
/// ```sql
24152420
/// expr [AS name]
24162421
/// ```
24172422
///
2418-
/// Parameter typed_syntax is set to true if the expression
2423+
/// For biquery [1], Parameter typed_syntax is set to true if the expression
24192424
/// is to be parsed as a field expression declared using typed
24202425
/// struct syntax [2], and false if using typeless struct syntax [3].
24212426
///

tests/sqlparser_databricks.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,48 @@ fn parse_use() {
278278
);
279279
}
280280
}
281+
282+
#[test]
283+
fn parse_databricks_struct_function() {
284+
assert_eq!(
285+
databricks()
286+
.verified_only_select("SELECT STRUCT(1, 'foo')")
287+
.projection[0],
288+
SelectItem::UnnamedExpr(Expr::Struct {
289+
values: vec![
290+
Expr::Value(number("1")),
291+
Expr::Value(Value::SingleQuotedString("foo".to_string()))
292+
],
293+
fields: vec![]
294+
})
295+
);
296+
assert_eq!(
297+
databricks()
298+
.verified_only_select("SELECT STRUCT(1 AS one, 'foo' AS foo, false)")
299+
.projection[0],
300+
SelectItem::UnnamedExpr(Expr::Struct {
301+
values: vec![
302+
Expr::Named {
303+
expr: Expr::Value(number("1")).into(),
304+
name: Ident::new("one")
305+
},
306+
Expr::Named {
307+
expr: Expr::Value(Value::SingleQuotedString("foo".to_string())).into(),
308+
name: Ident::new("foo")
309+
},
310+
Expr::Value(Value::Boolean(false))
311+
],
312+
fields: vec![]
313+
})
314+
);
315+
}
316+
317+
#[test]
318+
fn parse_invalid_struct_function() {
319+
assert_eq!(
320+
databricks()
321+
.parse_sql_statements("SELECT STRUCT<INT64>(1)") // This works only in BigQuery
322+
.unwrap_err(),
323+
ParserError::ParserError("Expected: (, found: <".to_string())
324+
);
325+
}

0 commit comments

Comments
 (0)