mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-07-07 17:04:59 +00:00
Fix parsing error when having fields after nested struct in BigQuery (#1897)
Some checks are pending
license / Release Audit Tool (RAT) (push) Waiting to run
Rust / codestyle (push) Waiting to run
Rust / lint (push) Waiting to run
Rust / benchmark-lint (push) Waiting to run
Rust / compile (push) Waiting to run
Rust / docs (push) Waiting to run
Rust / compile-no-std (push) Waiting to run
Rust / test (beta) (push) Waiting to run
Rust / test (nightly) (push) Waiting to run
Rust / test (stable) (push) Waiting to run
Some checks are pending
license / Release Audit Tool (RAT) (push) Waiting to run
Rust / codestyle (push) Waiting to run
Rust / lint (push) Waiting to run
Rust / benchmark-lint (push) Waiting to run
Rust / compile (push) Waiting to run
Rust / docs (push) Waiting to run
Rust / compile-no-std (push) Waiting to run
Rust / test (beta) (push) Waiting to run
Rust / test (nightly) (push) Waiting to run
Rust / test (stable) (push) Waiting to run
This commit is contained in:
parent
b1b379e570
commit
185a490218
2 changed files with 77 additions and 38 deletions
|
@ -3034,7 +3034,6 @@ impl<'a> Parser<'a> {
|
|||
where
|
||||
F: FnMut(&mut Parser<'a>) -> Result<(StructField, MatchedTrailingBracket), ParserError>,
|
||||
{
|
||||
let start_token = self.peek_token();
|
||||
self.expect_keyword_is(Keyword::STRUCT)?;
|
||||
|
||||
// Nothing to do if we have no type information.
|
||||
|
@ -3047,16 +3046,10 @@ impl<'a> Parser<'a> {
|
|||
let trailing_bracket = loop {
|
||||
let (def, trailing_bracket) = elem_parser(self)?;
|
||||
field_defs.push(def);
|
||||
if !self.consume_token(&Token::Comma) {
|
||||
// The struct field definition is finished if it occurs `>>` or comma.
|
||||
if trailing_bracket.0 || !self.consume_token(&Token::Comma) {
|
||||
break trailing_bracket;
|
||||
}
|
||||
|
||||
// Angle brackets are balanced so we only expect the trailing `>>` after
|
||||
// we've matched all field types for the current struct.
|
||||
// e.g. this is invalid syntax `STRUCT<STRUCT<INT>>>, INT>(NULL)`
|
||||
if trailing_bracket.0 {
|
||||
return parser_err!("unmatched > in STRUCT definition", start_token.span.start);
|
||||
}
|
||||
};
|
||||
|
||||
Ok((
|
||||
|
|
|
@ -635,35 +635,6 @@ fn parse_nested_data_types() {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_invalid_brackets() {
|
||||
let sql = "SELECT STRUCT<INT64>>(NULL)";
|
||||
assert_eq!(
|
||||
bigquery_and_generic()
|
||||
.parse_sql_statements(sql)
|
||||
.unwrap_err(),
|
||||
ParserError::ParserError("unmatched > in STRUCT literal".to_string())
|
||||
);
|
||||
|
||||
let sql = "SELECT STRUCT<STRUCT<INT64>>>(NULL)";
|
||||
assert_eq!(
|
||||
bigquery_and_generic()
|
||||
.parse_sql_statements(sql)
|
||||
.unwrap_err(),
|
||||
ParserError::ParserError("Expected: (, found: >".to_string())
|
||||
);
|
||||
|
||||
let sql = "CREATE TABLE table (x STRUCT<STRUCT<INT64>>>)";
|
||||
assert_eq!(
|
||||
bigquery_and_generic()
|
||||
.parse_sql_statements(sql)
|
||||
.unwrap_err(),
|
||||
ParserError::ParserError(
|
||||
"Expected: ',' or ')' after column definition, found: >".to_string()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_tuple_struct_literal() {
|
||||
// tuple syntax: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#tuple_syntax
|
||||
|
@ -2472,3 +2443,78 @@ fn test_struct_field_options() {
|
|||
")",
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_struct_trailing_and_nested_bracket() {
|
||||
bigquery().verified_stmt(concat!(
|
||||
"CREATE TABLE my_table (",
|
||||
"f0 STRING, ",
|
||||
"f1 STRUCT<a STRING, b STRUCT<c INT64, d STRING>>, ",
|
||||
"f2 STRING",
|
||||
")",
|
||||
));
|
||||
|
||||
// More complex nested structs
|
||||
bigquery().verified_stmt(concat!(
|
||||
"CREATE TABLE my_table (",
|
||||
"f0 STRING, ",
|
||||
"f1 STRUCT<a STRING, b STRUCT<c INT64, d STRUCT<e STRING>>>, ",
|
||||
"f2 STRUCT<h STRING, i STRUCT<j INT64, k STRUCT<l STRUCT<m STRING>>>>, ",
|
||||
"f3 STRUCT<e STRING, f STRUCT<c INT64>>",
|
||||
")",
|
||||
));
|
||||
|
||||
// Bad case with missing closing bracket
|
||||
assert_eq!(
|
||||
ParserError::ParserError("Expected: >, found: )".to_owned()),
|
||||
bigquery()
|
||||
.parse_sql_statements("CREATE TABLE my_table(f1 STRUCT<a STRING, b INT64)")
|
||||
.unwrap_err()
|
||||
);
|
||||
|
||||
// Bad case with redundant closing bracket
|
||||
assert_eq!(
|
||||
ParserError::ParserError(
|
||||
"unmatched > after parsing data type STRUCT<a STRING, b INT64>)".to_owned()
|
||||
),
|
||||
bigquery()
|
||||
.parse_sql_statements("CREATE TABLE my_table(f1 STRUCT<a STRING, b INT64>>)")
|
||||
.unwrap_err()
|
||||
);
|
||||
|
||||
// Base case with redundant closing bracket in nested struct
|
||||
assert_eq!(
|
||||
ParserError::ParserError(
|
||||
"Expected: ',' or ')' after column definition, found: >".to_owned()
|
||||
),
|
||||
bigquery()
|
||||
.parse_sql_statements("CREATE TABLE my_table(f1 STRUCT<a STRUCT<b INT>>>, c INT64)")
|
||||
.unwrap_err()
|
||||
);
|
||||
|
||||
let sql = "SELECT STRUCT<INT64>>(NULL)";
|
||||
assert_eq!(
|
||||
bigquery_and_generic()
|
||||
.parse_sql_statements(sql)
|
||||
.unwrap_err(),
|
||||
ParserError::ParserError("unmatched > in STRUCT literal".to_string())
|
||||
);
|
||||
|
||||
let sql = "SELECT STRUCT<STRUCT<INT64>>>(NULL)";
|
||||
assert_eq!(
|
||||
bigquery_and_generic()
|
||||
.parse_sql_statements(sql)
|
||||
.unwrap_err(),
|
||||
ParserError::ParserError("Expected: (, found: >".to_string())
|
||||
);
|
||||
|
||||
let sql = "CREATE TABLE table (x STRUCT<STRUCT<INT64>>>)";
|
||||
assert_eq!(
|
||||
bigquery_and_generic()
|
||||
.parse_sql_statements(sql)
|
||||
.unwrap_err(),
|
||||
ParserError::ParserError(
|
||||
"Expected: ',' or ')' after column definition, found: >".to_string()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue