Skip to content

Commit bc53de6

Browse files
authored
Merge pull request #10560 from andylokandy/fixx5
fix(parser): properly parse tuple type with field name
2 parents f4a49b4 + bfe89c7 commit bc53de6

File tree

4 files changed

+37
-41
lines changed

4 files changed

+37
-41
lines changed

src/query/ast/src/parser/expr.rs

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,26 +1412,20 @@ pub fn type_name(i: Input) -> IResult<TypeName> {
14121412
|(_, item_type)| TypeName::Nullable(Box::new(item_type.1)),
14131413
);
14141414
let ty_tuple = map(
1415-
rule! { TUPLE ~ "(" ~ #comma_separated_list1(tuple_types) ~ ")" },
1416-
|(_, _, tuple_types, _)| {
1417-
let mut fields_name = Vec::with_capacity(tuple_types.len());
1418-
let mut fields_type = Vec::with_capacity(tuple_types.len());
1419-
for tuple_type in tuple_types {
1420-
if let Some(field_name) = tuple_type.0 {
1421-
fields_name.push(field_name.name);
1422-
}
1423-
fields_type.push(tuple_type.1);
1424-
}
1425-
if fields_name.is_empty() {
1426-
TypeName::Tuple {
1427-
fields_name: None,
1428-
fields_type,
1429-
}
1430-
} else {
1431-
TypeName::Tuple {
1432-
fields_name: Some(fields_name),
1433-
fields_type,
1434-
}
1415+
rule! { TUPLE ~ "(" ~ #comma_separated_list1(type_name) ~ ")" },
1416+
|(_, _, fields_type, _)| TypeName::Tuple {
1417+
fields_name: None,
1418+
fields_type,
1419+
},
1420+
);
1421+
let ty_named_tuple = map(
1422+
rule! { TUPLE ~ "(" ~ #comma_separated_list1(rule! { #ident ~ #type_name }) ~ ")" },
1423+
|(_, _, fields, _)| {
1424+
let (fields_name, fields_type) =
1425+
fields.into_iter().map(|(name, ty)| (name.name, ty)).unzip();
1426+
TypeName::Tuple {
1427+
fields_name: Some(fields_name),
1428+
fields_type,
14351429
}
14361430
},
14371431
);
@@ -1461,7 +1455,8 @@ pub fn type_name(i: Input) -> IResult<TypeName> {
14611455
| #ty_decimal
14621456
| #ty_array
14631457
| #ty_map
1464-
| #ty_tuple
1458+
| #ty_tuple : "TUPLE(<type>, ...)"
1459+
| #ty_named_tuple : "TUPLE(<name> <type>, ...)"
14651460
| #ty_date
14661461
| #ty_datetime
14671462
| #ty_string
@@ -1479,26 +1474,6 @@ pub fn type_name(i: Input) -> IResult<TypeName> {
14791474
)(i)
14801475
}
14811476

1482-
pub fn tuple_types(i: Input) -> IResult<(Option<Identifier>, TypeName)> {
1483-
let tuple_types = map(
1484-
rule! {
1485-
#type_name
1486-
},
1487-
|type_name| (None, type_name),
1488-
);
1489-
let named_tuple_types = map(
1490-
rule! {
1491-
#ident ~ #type_name
1492-
},
1493-
|(name, type_name)| (Some(name), type_name),
1494-
);
1495-
1496-
rule!(
1497-
#tuple_types
1498-
| #named_tuple_types
1499-
)(i)
1500-
}
1501-
15021477
pub fn interval_kind(i: Input) -> IResult<IntervalKind> {
15031478
alt((
15041479
value(IntervalKind::Year, rule! { YEAR }),

src/query/ast/tests/it/parser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ fn test_statement_error() {
402402
r#"create table a (c varch)"#,
403403
r#"create table a (c tuple())"#,
404404
r#"create table a (c decimal)"#,
405+
r#"create table a (b tuple(c int, uint64));"#,
405406
r#"drop table if a.b"#,
406407
r#"truncate table a.b.c.d"#,
407408
r#"truncate a"#,

src/query/ast/tests/it/testdata/statement-error.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,23 @@ error:
6565
| while parsing `CREATE TABLE [IF NOT EXISTS] [<database>.]<table> [<source>] [<table_options>]`
6666

6767

68+
---------- Input ----------
69+
create table a (b tuple(c int, uint64));
70+
---------- Output ---------
71+
error:
72+
--> SQL:1:38
73+
|
74+
1 | create table a (b tuple(c int, uint64));
75+
| ------ - ----- ^
76+
| | | | |
77+
| | | | expected `BOOLEAN`, `BOOL`, `UINT8`, `TINYINT`, `UINT16`, `SMALLINT`, or 30 more ...
78+
| | | | while parsing type name
79+
| | | while parsing TUPLE(<name> <type>, ...)
80+
| | | while parsing type name
81+
| | while parsing `<column name> <type> [DEFAULT <default value>] [COMMENT '<comment>']`
82+
| while parsing `CREATE TABLE [IF NOT EXISTS] [<database>.]<table> [<source>] [<table_options>]`
83+
84+
6885
---------- Input ----------
6986
drop table if a.b
7087
---------- Output ---------

tests/sqllogictests/suites/base/03_common/03_0026_insert_into_tuple

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ select t.1, t.2, t.3, t.4, t.5, t.6 from t1
2525
1 100 12.34 abc 2020-01-01 2020-01-01 00:00:00.000000
2626
0 200 -25.73 xyz 2022-06-01 2022-06-01 12:00:00.000000
2727

28+
statement error 1005
29+
CREATE TABLE IF NOT EXISTS t2(t Tuple(a Bool, Int64)) Engine = Fuse
30+
2831
statement ok
2932
CREATE TABLE IF NOT EXISTS t2(id Int, t Tuple(a Bool, b Int64, c Float64, d String, e Date, f Timestamp)) Engine = Fuse
3033

0 commit comments

Comments
 (0)