kdn36 opened a new issue, #2642:
URL: https://github.com/apache/datafusion-sqlparser-rs/issues/2642
With array suffixes (`supports_array_typedef_with_brackets`, e.g.
`GenericDialect`), a `[]` that follows two struct
types closed by a single `>>` token is applied to the inner struct instead
of the outer one:
```sql
SELECT CAST(NULL AS STRUCT<a STRUCT<b INT>>[])
-- parses as STRUCT<a STRUCT<b INT>[]> (a struct whose field `a` is an
array)
-- expected STRUCT<a STRUCT<b INT>>[] (an array of structs)
```
Rust repro
```rust
use sqlparser::dialect::GenericDialect;
use sqlparser::parser::Parser;
fn main() {
for sql in [
"SELECT CAST(NULL AS STRUCT<a STRUCT<b INT>>[])",
"SELECT CAST(NULL AS ARRAY<ARRAY<INT>>[])",
"SELECT CAST(NULL AS STRUCT<a STRUCT<b INT> >[])",
] {
let stmt = &Parser::parse_sql(&GenericDialect {}, sql).unwrap()[0];
println!("{sql}\n -> {stmt}");
}
}
```
Output, with 0.62.0 and with `main` (f1854e47):
```
SELECT CAST(NULL AS STRUCT<a STRUCT<b INT>>[])
-> SELECT CAST(NULL AS STRUCT<a STRUCT<b INT>[]>)
SELECT CAST(NULL AS ARRAY<ARRAY<INT>>[])
-> SELECT CAST(NULL AS ARRAY<ARRAY<INT>[]>)
SELECT CAST(NULL AS STRUCT<a STRUCT<b INT> >[])
-> SELECT CAST(NULL AS STRUCT<a STRUCT<b INT>>[])
```
With a space between the brackets (`STRUCT<a STRUCT<b INT> >[]`) it parses
correctly. The AST is identical to the one
for `STRUCT<a STRUCT<b INT>[]>`, so callers can't tell the two apart.
Cause: in `parse_data_type_helper`, the inner `STRUCT` consumes `>>` and
returns `MatchedTrailingBracket(true)`,
but the array-suffix loop (`while self.consume_token(&Token::LBracket)`)
still runs at the inner level and takes the
`[]` that belongs to the enclosing type.
Observed on sqlparser 0.62; 0.63 and `main` behave the same. AI was used for
analysis and draft.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]