From df0474076f6d1338e265efea425b26d062b50711 Mon Sep 17 00:00:00 2001 From: Xiangjin Date: Wed, 20 Nov 2024 16:22:31 +0800 Subject: [PATCH 1/2] reproduce error of `>>[]` being `>[]>` --- src/sqlparser/tests/testdata/array.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/sqlparser/tests/testdata/array.yaml b/src/sqlparser/tests/testdata/array.yaml index 20ed73ab9d910..2aa27dffabf73 100644 --- a/src/sqlparser/tests/testdata/array.yaml +++ b/src/sqlparser/tests/testdata/array.yaml @@ -74,3 +74,6 @@ sql parser error: expected an expression, found: [ LINE 1: SELECT [1,2] ^ +- input: CREATE TABLE t (params STRUCT>[]) + formatted_sql: CREATE TABLE t (params STRUCT[]>) + formatted_ast: 'CreateTable { or_replace: false, temporary: false, if_not_exists: false, name: ObjectName([Ident { value: "t", quote_style: None }]), columns: [ColumnDef { name: Ident { value: "params", quote_style: None }, data_type: Some(Struct([StructField { name: Ident { value: "a", quote_style: None }, data_type: Array(Struct([StructField { name: Ident { value: "b", quote_style: None }, data_type: Int }])) }])), collation: None, options: [] }], wildcard_idx: None, constraints: [], with_options: [], format_encode: None, source_watermarks: [], append_only: false, on_conflict: None, with_version_column: None, query: None, cdc_table_info: None, include_column_options: [] }' From bdf8fd3be1620f842a9e4d488a42a9b07fd84a03 Mon Sep 17 00:00:00 2001 From: Xiangjin Date: Wed, 20 Nov 2024 18:08:22 +0800 Subject: [PATCH 2/2] check `remaining_close` before peeking for `LBracket` --- src/sqlparser/src/parser_v2/data_type.rs | 7 ++++++- src/sqlparser/tests/testdata/array.yaml | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/sqlparser/src/parser_v2/data_type.rs b/src/sqlparser/src/parser_v2/data_type.rs index dff2d857c4953..0e1505e18cb1a 100644 --- a/src/sqlparser/src/parser_v2/data_type.rs +++ b/src/sqlparser/src/parser_v2/data_type.rs @@ -152,8 +152,13 @@ fn data_type_stateful(input: &mut StatefulStream) -> PResult where S: TokenStream, { + let base = data_type_stateful_inner.parse_next(input)?; + // Shall not peek for `Token::LBracket` when `>>` is partially consumed. + if *input.state.remaining_close.borrow() { + return Ok(base); + } ( - data_type_stateful_inner, + empty.value(base), repeat(0.., (Token::LBracket, cut_err(Token::RBracket))), ) .map(|(mut dt, depth)| { diff --git a/src/sqlparser/tests/testdata/array.yaml b/src/sqlparser/tests/testdata/array.yaml index 2aa27dffabf73..b2cf8e2070b11 100644 --- a/src/sqlparser/tests/testdata/array.yaml +++ b/src/sqlparser/tests/testdata/array.yaml @@ -75,5 +75,5 @@ LINE 1: SELECT [1,2] ^ - input: CREATE TABLE t (params STRUCT>[]) - formatted_sql: CREATE TABLE t (params STRUCT[]>) - formatted_ast: 'CreateTable { or_replace: false, temporary: false, if_not_exists: false, name: ObjectName([Ident { value: "t", quote_style: None }]), columns: [ColumnDef { name: Ident { value: "params", quote_style: None }, data_type: Some(Struct([StructField { name: Ident { value: "a", quote_style: None }, data_type: Array(Struct([StructField { name: Ident { value: "b", quote_style: None }, data_type: Int }])) }])), collation: None, options: [] }], wildcard_idx: None, constraints: [], with_options: [], format_encode: None, source_watermarks: [], append_only: false, on_conflict: None, with_version_column: None, query: None, cdc_table_info: None, include_column_options: [] }' + formatted_sql: CREATE TABLE t (params STRUCT>[]) + formatted_ast: 'CreateTable { or_replace: false, temporary: false, if_not_exists: false, name: ObjectName([Ident { value: "t", quote_style: None }]), columns: [ColumnDef { name: Ident { value: "params", quote_style: None }, data_type: Some(Array(Struct([StructField { name: Ident { value: "a", quote_style: None }, data_type: Struct([StructField { name: Ident { value: "b", quote_style: None }, data_type: Int }]) }]))), collation: None, options: [] }], wildcard_idx: None, constraints: [], with_options: [], format_encode: None, source_watermarks: [], append_only: false, on_conflict: None, with_version_column: None, query: None, cdc_table_info: None, include_column_options: [] }'