Allow keyword as field names in BigQuery struct syntax (#1254)

This commit is contained in:
Ifeanyi Ubah 2024-05-06 21:34:33 +02:00 committed by GitHub
parent c36e617d61
commit 138722a7c9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 7 deletions

View file

@ -2192,13 +2192,12 @@ impl<'a> Parser<'a> {
fn parse_big_query_struct_field_def( fn parse_big_query_struct_field_def(
&mut self, &mut self,
) -> Result<(StructField, MatchedTrailingBracket), ParserError> { ) -> Result<(StructField, MatchedTrailingBracket), ParserError> {
let is_anonymous_field = if let Token::Word(w) = self.peek_token().token { // Look beyond the next item to infer whether both field name
ALL_KEYWORDS // and type are specified.
.binary_search(&w.value.to_uppercase().as_str()) let is_anonymous_field = !matches!(
.is_ok() (self.peek_nth_token(0).token, self.peek_nth_token(1).token),
} else { (Token::Word(_), Token::Word(_))
false );
};
let field_name = if is_anonymous_field { let field_name = if is_anonymous_field {
None None

View file

@ -792,6 +792,27 @@ fn parse_typed_struct_syntax_bigquery() {
}, },
expr_from_projection(&select.projection[1]) expr_from_projection(&select.projection[1])
); );
// Keywords in the parser may be used as field names.
let sql = r#"SELECT STRUCT<key INT64, value INT64>(1, 2)"#;
let select = bigquery().verified_only_select(sql);
assert_eq!(1, select.projection.len());
assert_eq!(
&Expr::Struct {
values: vec![Expr::Value(number("1")), Expr::Value(number("2")),],
fields: vec![
StructField {
field_name: Some("key".into()),
field_type: DataType::Int64,
},
StructField {
field_name: Some("value".into()),
field_type: DataType::Int64,
},
]
},
expr_from_projection(&select.projection[0])
);
} }
#[test] #[test]