Skip to content

Commit e5bf6a1

Browse files
committed
generalize struct support and add databricks
1 parent 525d178 commit e5bf6a1

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
@@ -853,16 +853,16 @@ pub enum Expr {
853853
Rollup(Vec<Vec<Expr>>),
854854
/// ROW / TUPLE a single value, such as `SELECT (1, 2)`
855855
Tuple(Vec<Expr>),
856-
/// `BigQuery` specific `Struct` literal expression [1]
856+
/// `Struct` literal expression
857857
/// Syntax:
858858
/// ```sql
859859
/// STRUCT<[field_name] field_type, ...>( expr1 [, ... ])
860860
/// ```
861-
/// [1]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#struct_type
862861
Struct {
863862
/// Struct values.
864863
values: Vec<Expr>,
865-
/// Struct field definitions.
864+
/// BigQuery specific: Struct field definitions.
865+
/// see https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#struct_type
866866
fields: Vec<StructField>,
867867
},
868868
/// `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
@@ -365,6 +365,26 @@ pub trait Dialect: Debug + Any {
365365
self.supports_trailing_commas()
366366
}
367367

368+
/// Return true if the dialect supports the STRUCT literal
369+
///
370+
/// Example
371+
/// ```sql
372+
/// SELECT STRUCT(1 as one, 'foo' as foo, false)
373+
/// ```
374+
fn supports_struct_literal(&self) -> bool {
375+
false
376+
}
377+
378+
/// Return true if the dialect supports typed struct syntax
379+
///
380+
/// Example for bigquery
381+
/// ```sql
382+
/// SELECT STRUCT<x int64, y string>(1, 'foo')
383+
/// ```
384+
fn supports_typed_struct_syntax(&self) -> bool {
385+
false
386+
}
387+
368388
/// Dialect-specific infix parser override
369389
///
370390
/// 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
@@ -1118,9 +1118,8 @@ impl<'a> Parser<'a> {
11181118
Keyword::MATCH if dialect_of!(self is MySqlDialect | GenericDialect) => {
11191119
Ok(Some(self.parse_match_against()?))
11201120
}
1121-
Keyword::STRUCT if dialect_of!(self is BigQueryDialect | GenericDialect) => {
1122-
self.prev_token();
1123-
Ok(Some(self.parse_bigquery_struct_literal()?))
1121+
Keyword::STRUCT if self.dialect.supports_struct_literal() => {
1122+
Ok(Some(self.parse_struct_literal()?))
11241123
}
11251124
Keyword::PRIOR if matches!(self.state, ParserState::ConnectBy) => {
11261125
let expr = self.parse_subexpr(self.dialect.prec_value(Precedence::PlusMinus))?;
@@ -2369,19 +2368,25 @@ impl<'a> Parser<'a> {
23692368
}
23702369
}
23712370

2372-
/// Bigquery specific: Parse a struct literal
23732371
/// Syntax
23742372
/// ```sql
2375-
/// -- typed
2373+
/// -- typed, specific to bigquery
23762374
/// STRUCT<[field_name] field_type, ...>( expr1 [, ... ])
23772375
/// -- typeless
23782376
/// STRUCT( expr1 [AS field_name] [, ... ])
23792377
/// ```
2380-
fn parse_bigquery_struct_literal(&mut self) -> Result<Expr, ParserError> {
2381-
let (fields, trailing_bracket) =
2382-
self.parse_struct_type_def(Self::parse_struct_field_def)?;
2383-
if trailing_bracket.0 {
2384-
return parser_err!("unmatched > in STRUCT literal", self.peek_token().location);
2378+
fn parse_struct_literal(&mut self) -> Result<Expr, ParserError> {
2379+
let mut fields = vec![];
2380+
// Typed struct syntax is only supported by BigQuery
2381+
// https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#typed_struct_syntax
2382+
if self.dialect.supports_typed_struct_syntax() {
2383+
self.prev_token();
2384+
let trailing_bracket;
2385+
(fields, trailing_bracket) =
2386+
self.parse_struct_type_def(Self::parse_struct_field_def)?;
2387+
if trailing_bracket.0 {
2388+
return parser_err!("unmatched > in STRUCT literal", self.peek_token().location);
2389+
}
23852390
}
23862391

23872392
self.expect_token(&Token::LParen)?;
@@ -2392,13 +2397,13 @@ impl<'a> Parser<'a> {
23922397
Ok(Expr::Struct { values, fields })
23932398
}
23942399

2395-
/// Parse an expression value for a bigquery struct [1]
2400+
/// Parse an expression value for a struct literal
23962401
/// Syntax
23972402
/// ```sql
23982403
/// expr [AS name]
23992404
/// ```
24002405
///
2401-
/// Parameter typed_syntax is set to true if the expression
2406+
/// For biquery [1], Parameter typed_syntax is set to true if the expression
24022407
/// is to be parsed as a field expression declared using typed
24032408
/// struct syntax [2], and false if using typeless struct syntax [3].
24042409
///

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)