Skip to content

Commit

Permalink
address comments and remove typed struct flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ayman-sigma committed Nov 26, 2024
1 parent e5bf6a1 commit 5ca4524
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 45 deletions.
6 changes: 4 additions & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,12 +857,14 @@ pub enum Expr {
/// Syntax:
/// ```sql
/// STRUCT<[field_name] field_type, ...>( expr1 [, ... ])
///
/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#struct_type)
/// [Databricks](https://docs.databricks.com/en/sql/language-manual/functions/struct.html)
/// ```
Struct {
/// Struct values.
values: Vec<Expr>,
/// BigQuery specific: Struct field definitions.
/// see https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#struct_type
/// Struct field definitions.
fields: Vec<StructField>,
},
/// `BigQuery` specific: An named expression in a typeless struct [1]
Expand Down
5 changes: 0 additions & 5 deletions src/dialect/bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,4 @@ impl Dialect for BigQueryDialect {
fn supports_struct_literal(&self) -> bool {
true
}

// See https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#typed_struct_syntax
fn supports_typed_struct_syntax(&self) -> bool {
true
}
}
4 changes: 0 additions & 4 deletions src/dialect/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,4 @@ impl Dialect for GenericDialect {
fn supports_struct_literal(&self) -> bool {
true
}

fn supports_typed_struct_syntax(&self) -> bool {
true
}
}
10 changes: 0 additions & 10 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,16 +375,6 @@ pub trait Dialect: Debug + Any {
false
}

/// Return true if the dialect supports typed struct syntax
///
/// Example for bigquery
/// ```sql
/// SELECT STRUCT<x int64, y string>(1, 'foo')
/// ```
fn supports_typed_struct_syntax(&self) -> bool {
false
}

/// Dialect-specific infix parser override
///
/// This method is called to parse the next infix expression.
Expand Down
20 changes: 8 additions & 12 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2370,25 +2370,21 @@ impl<'a> Parser<'a> {

/// Syntax
/// ```sql
/// -- typed, specific to bigquery
/// -- typed
/// STRUCT<[field_name] field_type, ...>( expr1 [, ... ])
/// -- typeless
/// STRUCT( expr1 [AS field_name] [, ... ])
/// ```
fn parse_struct_literal(&mut self) -> Result<Expr, ParserError> {
let mut fields = vec![];
// Typed struct syntax is only supported by BigQuery
// https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#typed_struct_syntax
if self.dialect.supports_typed_struct_syntax() {
self.prev_token();
let trailing_bracket;
(fields, trailing_bracket) =
self.parse_struct_type_def(Self::parse_struct_field_def)?;
if trailing_bracket.0 {
return parser_err!("unmatched > in STRUCT literal", self.peek_token().location);
}
// Parse the fields definition if exist `<[field_name] field_type, ...>`
self.prev_token();
let (fields, trailing_bracket) =
self.parse_struct_type_def(Self::parse_struct_field_def)?;
if trailing_bracket.0 {
return parser_err!("unmatched > in STRUCT literal", self.peek_token().location);
}

// Parse the struct values `(expr1 [, ... ])`
self.expect_token(&Token::LParen)?;
let values = self
.parse_comma_separated(|parser| parser.parse_struct_field_expr(!fields.is_empty()))?;
Expand Down
14 changes: 2 additions & 12 deletions tests/sqlparser_databricks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ fn parse_use() {
#[test]
fn parse_databricks_struct_function() {
assert_eq!(
databricks()
databricks_and_generic()
.verified_only_select("SELECT STRUCT(1, 'foo')")
.projection[0],
SelectItem::UnnamedExpr(Expr::Struct {
Expand All @@ -294,7 +294,7 @@ fn parse_databricks_struct_function() {
})
);
assert_eq!(
databricks()
databricks_and_generic()
.verified_only_select("SELECT STRUCT(1 AS one, 'foo' AS foo, false)")
.projection[0],
SelectItem::UnnamedExpr(Expr::Struct {
Expand All @@ -313,13 +313,3 @@ fn parse_databricks_struct_function() {
})
);
}

#[test]
fn parse_invalid_struct_function() {
assert_eq!(
databricks()
.parse_sql_statements("SELECT STRUCT<INT64>(1)") // This works only in BigQuery
.unwrap_err(),
ParserError::ParserError("Expected: (, found: <".to_string())
);
}

0 comments on commit 5ca4524

Please sign in to comment.