diff --git a/src/ast/mod.rs b/src/ast/mod.rs index b66a83da0..624344375 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -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, - /// BigQuery specific: Struct field definitions. - /// see https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#struct_type + /// Struct field definitions. fields: Vec, }, /// `BigQuery` specific: An named expression in a typeless struct [1] diff --git a/src/dialect/bigquery.rs b/src/dialect/bigquery.rs index 9c519435f..66d7d2061 100644 --- a/src/dialect/bigquery.rs +++ b/src/dialect/bigquery.rs @@ -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 - } } diff --git a/src/dialect/generic.rs b/src/dialect/generic.rs index 73402b8cb..61e5070fb 100644 --- a/src/dialect/generic.rs +++ b/src/dialect/generic.rs @@ -127,8 +127,4 @@ impl Dialect for GenericDialect { fn supports_struct_literal(&self) -> bool { true } - - fn supports_typed_struct_syntax(&self) -> bool { - true - } } diff --git a/src/dialect/mod.rs b/src/dialect/mod.rs index f5fa66701..e1aeed39b 100644 --- a/src/dialect/mod.rs +++ b/src/dialect/mod.rs @@ -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(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. diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 7f72645f8..4b2f121f1 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -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 { - 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()))?; diff --git a/tests/sqlparser_databricks.rs b/tests/sqlparser_databricks.rs index 7d01c14f1..d73c088a7 100644 --- a/tests/sqlparser_databricks.rs +++ b/tests/sqlparser_databricks.rs @@ -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 { @@ -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 { @@ -313,13 +313,3 @@ fn parse_databricks_struct_function() { }) ); } - -#[test] -fn parse_invalid_struct_function() { - assert_eq!( - databricks() - .parse_sql_statements("SELECT STRUCT(1)") // This works only in BigQuery - .unwrap_err(), - ParserError::ParserError("Expected: (, found: <".to_string()) - ); -}