Skip to content

Commit

Permalink
BiqQuery correct struct definition parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
zdenal committed Feb 29, 2024
1 parent 7c4eac3 commit 2ec74d2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
16 changes: 6 additions & 10 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1824,18 +1824,14 @@ impl<'a> Parser<'a> {
fn parse_big_query_struct_field_def(
&mut self,
) -> Result<(StructField, MatchedTrailingBracket), ParserError> {
let is_anonymous_field = if let Token::Word(w) = self.peek_token().token {
ALL_KEYWORDS
.binary_search(&w.value.to_uppercase().as_str())
.is_ok()
let field_name: Option<WithSpan<Ident>> = if let Token::Word(_) = self.peek_token().token {
match self.peek_nth_token(1).token {
// if there is another word it is named field
Token::Word(_) => Some(self.parse_identifier()?),
_ => None,
}
} else {
false
};

let field_name = if is_anonymous_field {
None
} else {
Some(self.parse_identifier()?)
};

let (field_type, trailing_bracket) = self.parse_data_type_helper()?;
Expand Down
7 changes: 6 additions & 1 deletion tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ fn parse_typed_struct_syntax() {
// typed struct syntax https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#typed_struct_syntax
// syntax: STRUCT<[field_name] field_type, ...>( expr1 [, ... ])

let sql = r#"SELECT STRUCT<INT64>(5), STRUCT<x INT64, y STRING>(1, t.str_col), STRUCT<arr ARRAY<FLOAT64>, str STRUCT<BOOL>>(nested_col)"#;
let sql = r#"SELECT STRUCT<INT64>(5), STRUCT<x INT64, y STRING, timezone STRING>(1, t.str_col, timezone), STRUCT<arr ARRAY<FLOAT64>, str STRUCT<BOOL>>(nested_col)"#;
let select = bigquery().verified_only_select(sql);
assert_eq!(3, select.projection.len());
assert_eq!(
Expand Down Expand Up @@ -285,6 +285,7 @@ fn parse_typed_struct_syntax() {
]
.empty_span()
),
Expr::Identifier(Ident::new("timezone").empty_span()),
],
fields: vec![
StructField {
Expand All @@ -295,6 +296,10 @@ fn parse_typed_struct_syntax() {
field_name: Some(Ident::new("y").empty_span()),
field_type: DataType::String(None),
},
StructField {
field_name: Some(Ident::new("timezone").empty_span()),
field_type: DataType::String(None),
},
],
},
expr_from_projection(&select.projection[1])
Expand Down

0 comments on commit 2ec74d2

Please sign in to comment.