Skip to content

Commit ff22a5c

Browse files
authored
chore(query): allow row as column ident (#18568)
* chore(query): allow row as column ident * chore(query): allow row as column ident
1 parent ede42b4 commit ff22a5c

File tree

5 files changed

+317
-4
lines changed

5 files changed

+317
-4
lines changed

src/query/ast/src/parser/common.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,13 @@ pub fn column_id(i: Input) -> IResult<ColumnID> {
322322
span: Some(token.span),
323323
}))
324324
}),
325+
// ROW could be a column name for compatibility
326+
map_res(rule! {ROW}, |token| {
327+
Ok(ColumnID::Name(Identifier::from_name(
328+
transform_span(&[token.clone()]),
329+
"row",
330+
)))
331+
}),
325332
map_res(rule! { #ident }, |ident| Ok(ColumnID::Name(ident))),
326333
))(i)
327334
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ fn test_statement() {
243243
r#"select * FROM t where ((a));"#,
244244
r#"select * FROM t where ((select 1) > 1);"#,
245245
r#"select ((t1.a)>=(((((t2.b)<=(t3.c))) IS NOT NULL)::INTEGER));"#,
246+
r#"select 33 as row, abc(33, row), def(row)"#,
247+
r#"SELECT func(ROW) FROM (SELECT 1 as ROW) t"#,
246248
r#"select * from t sample row (99);"#,
247249
r#"select * from t sample block (99);"#,
248250
r#"select * from t sample row (10 rows);"#,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ error:
5252
--> SQL:1:10
5353
|
5454
1 | CAST(col1)
55-
| ---- ^ unexpected `)`, expecting `AS`, `,`, `(`, `IS`, `NOT`, `IN`, `LIKE`, `EXISTS`, `BETWEEN`, `+`, `-`, `*`, `/`, `//`, `DIV`, `%`, `||`, `<=>`, `<+>`, `<->`, `>`, `<`, `>=`, `<=`, `=`, `<>`, `!=`, `^`, `AND`, `OR`, `XOR`, `REGEXP`, `RLIKE`, `SOUNDS`, <BitWiseOr>, <BitWiseAnd>, <BitWiseXor>, <ShiftLeft>, <ShiftRight>, `->`, `->>`, `#>`, `#>>`, `?`, `?|`, `?&`, `@>`, `<@`, `@?`, `@@`, `#-`, <Factorial>, <SquareRoot>, <BitWiseNot>, <CubeRoot>, <Abs>, `CAST`, `TRY_CAST`, `::`, `POSITION`, or 46 more ...
55+
| ---- ^ unexpected `)`, expecting `AS`, `,`, `(`, `IS`, `NOT`, `IN`, `LIKE`, `EXISTS`, `BETWEEN`, `+`, `-`, `*`, `/`, `//`, `DIV`, `%`, `||`, `<=>`, `<+>`, `<->`, `>`, `<`, `>=`, `<=`, `=`, `<>`, `!=`, `^`, `AND`, `OR`, `XOR`, `REGEXP`, `RLIKE`, `SOUNDS`, <BitWiseOr>, <BitWiseAnd>, <BitWiseXor>, <ShiftLeft>, <ShiftRight>, `->`, `->>`, `#>`, `#>>`, `?`, `?|`, `?&`, `@>`, `<@`, `@?`, `@@`, `#-`, <Factorial>, <SquareRoot>, <BitWiseNot>, <CubeRoot>, <Abs>, `CAST`, `TRY_CAST`, `::`, `POSITION`, or 47 more ...
5656
| |
5757
| while parsing `CAST(... AS ...)`
5858
| while parsing expression
@@ -81,7 +81,7 @@ error:
8181
1 | $ abc + 3
8282
| ^
8383
| |
84-
| unexpected `$`, expecting `IS`, `IN`, `LIKE`, `EXISTS`, `BETWEEN`, `+`, `-`, `*`, `/`, `//`, `DIV`, `%`, `||`, `<=>`, `<+>`, `<->`, `>`, `<`, `>=`, `<=`, `=`, `<>`, `!=`, `^`, `AND`, `OR`, `XOR`, `NOT`, `REGEXP`, `RLIKE`, `SOUNDS`, <BitWiseOr>, <BitWiseAnd>, <BitWiseXor>, <ShiftLeft>, <ShiftRight>, `->`, `->>`, `#>`, `#>>`, `?`, `?|`, `?&`, `@>`, `<@`, `@?`, `@@`, `#-`, <Factorial>, <SquareRoot>, <BitWiseNot>, <CubeRoot>, <Abs>, `CAST`, `TRY_CAST`, `::`, `POSITION`, `IdentVariable`, `DATEADD`, `DATE_ADD`, or 44 more ...
84+
| unexpected `$`, expecting `IS`, `IN`, `LIKE`, `EXISTS`, `BETWEEN`, `+`, `-`, `*`, `/`, `//`, `DIV`, `%`, `||`, `<=>`, `<+>`, `<->`, `>`, `<`, `>=`, `<=`, `=`, `<>`, `!=`, `^`, `AND`, `OR`, `XOR`, `NOT`, `REGEXP`, `RLIKE`, `SOUNDS`, <BitWiseOr>, <BitWiseAnd>, <BitWiseXor>, <ShiftLeft>, <ShiftRight>, `->`, `->>`, `#>`, `#>>`, `?`, `?|`, `?&`, `@>`, `<@`, `@?`, `@@`, `#-`, <Factorial>, <SquareRoot>, <BitWiseNot>, <CubeRoot>, <Abs>, `CAST`, `TRY_CAST`, `::`, `POSITION`, `IdentVariable`, `DATEADD`, `DATE_ADD`, or 45 more ...
8585
| while parsing expression
8686

8787

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ error:
556556
--> SQL:1:41
557557
|
558558
1 | SELECT * FROM t GROUP BY GROUPING SETS ()
559-
| ------ ^ unexpected `)`, expecting `(`, `IS`, `IN`, `LIKE`, `EXISTS`, `BETWEEN`, `+`, `-`, `*`, `/`, `//`, `DIV`, `%`, `||`, `<=>`, `<+>`, `<->`, `>`, `<`, `>=`, `<=`, `=`, `<>`, `!=`, `^`, `AND`, `OR`, `XOR`, `NOT`, `REGEXP`, `RLIKE`, `SOUNDS`, <BitWiseOr>, <BitWiseAnd>, <BitWiseXor>, <ShiftLeft>, <ShiftRight>, `->`, `->>`, `#>`, `#>>`, `?`, `?|`, `?&`, `@>`, `<@`, `@?`, `@@`, `#-`, <Factorial>, <SquareRoot>, <BitWiseNot>, <CubeRoot>, <Abs>, `CAST`, `TRY_CAST`, `::`, `POSITION`, `IdentVariable`, `DATEADD`, or 44 more ...
559+
| ------ ^ unexpected `)`, expecting `(`, `IS`, `IN`, `LIKE`, `EXISTS`, `BETWEEN`, `+`, `-`, `*`, `/`, `//`, `DIV`, `%`, `||`, `<=>`, `<+>`, `<->`, `>`, `<`, `>=`, `<=`, `=`, `<>`, `!=`, `^`, `AND`, `OR`, `XOR`, `NOT`, `REGEXP`, `RLIKE`, `SOUNDS`, <BitWiseOr>, <BitWiseAnd>, <BitWiseXor>, <ShiftLeft>, <ShiftRight>, `->`, `->>`, `#>`, `#>>`, `?`, `?|`, `?&`, `@>`, `<@`, `@?`, `@@`, `#-`, <Factorial>, <SquareRoot>, <BitWiseNot>, <CubeRoot>, <Abs>, `CAST`, `TRY_CAST`, `::`, `POSITION`, `IdentVariable`, `DATEADD`, or 45 more ...
560560
| |
561561
| while parsing `SELECT ...`
562562

@@ -978,7 +978,7 @@ error:
978978
--> SQL:1:65
979979
|
980980
1 | CREATE FUNCTION IF NOT EXISTS isnotempty AS(p) -> not(is_null(p)
981-
| ------ -- ---- ^ unexpected end of input, expecting `)`, `(`, `WITHIN`, `IGNORE`, `RESPECT`, `OVER`, `IS`, `NOT`, `IN`, `LIKE`, `EXISTS`, `BETWEEN`, `+`, `-`, `*`, `/`, `//`, `DIV`, `%`, `||`, `<=>`, `<+>`, `<->`, `>`, `<`, `>=`, `<=`, `=`, `<>`, `!=`, `^`, `AND`, `OR`, `XOR`, `REGEXP`, `RLIKE`, `SOUNDS`, <BitWiseOr>, <BitWiseAnd>, <BitWiseXor>, <ShiftLeft>, <ShiftRight>, `->`, `->>`, `#>`, `#>>`, `?`, `?|`, `?&`, `@>`, `<@`, `@?`, `@@`, `#-`, <Factorial>, <SquareRoot>, <BitWiseNot>, <CubeRoot>, <Abs>, `CAST`, or 50 more ...
981+
| ------ -- ---- ^ unexpected end of input, expecting `)`, `(`, `WITHIN`, `IGNORE`, `RESPECT`, `OVER`, `IS`, `NOT`, `IN`, `LIKE`, `EXISTS`, `BETWEEN`, `+`, `-`, `*`, `/`, `//`, `DIV`, `%`, `||`, `<=>`, `<+>`, `<->`, `>`, `<`, `>=`, `<=`, `=`, `<>`, `!=`, `^`, `AND`, `OR`, `XOR`, `REGEXP`, `RLIKE`, `SOUNDS`, <BitWiseOr>, <BitWiseAnd>, <BitWiseXor>, <ShiftLeft>, <ShiftRight>, `->`, `->>`, `#>`, `#>>`, `?`, `?|`, `?&`, `@>`, `<@`, `@?`, `@@`, `#-`, <Factorial>, <SquareRoot>, <BitWiseNot>, <CubeRoot>, <Abs>, `CAST`, or 51 more ...
982982
| | | | |
983983
| | | | while parsing `(<expr> [, ...])`
984984
| | | while parsing expression

src/query/ast/tests/it/testdata/stmt.txt

Lines changed: 304 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11053,6 +11053,310 @@ Query(
1105311053
)
1105411054

1105511055

11056+
---------- Input ----------
11057+
select 33 as row, abc(33, row), def(row)
11058+
---------- Output ---------
11059+
SELECT 33 AS row, abc(33, row), def(row)
11060+
---------- AST ------------
11061+
Query(
11062+
Query {
11063+
span: Some(
11064+
0..40,
11065+
),
11066+
with: None,
11067+
body: Select(
11068+
SelectStmt {
11069+
span: Some(
11070+
0..40,
11071+
),
11072+
hints: None,
11073+
distinct: false,
11074+
top_n: None,
11075+
select_list: [
11076+
AliasedExpr {
11077+
expr: Literal {
11078+
span: Some(
11079+
7..9,
11080+
),
11081+
value: UInt64(
11082+
33,
11083+
),
11084+
},
11085+
alias: Some(
11086+
Identifier {
11087+
span: Some(
11088+
13..16,
11089+
),
11090+
name: "row",
11091+
quote: None,
11092+
ident_type: None,
11093+
},
11094+
),
11095+
},
11096+
AliasedExpr {
11097+
expr: FunctionCall {
11098+
span: Some(
11099+
18..30,
11100+
),
11101+
func: FunctionCall {
11102+
distinct: false,
11103+
name: Identifier {
11104+
span: Some(
11105+
18..21,
11106+
),
11107+
name: "abc",
11108+
quote: None,
11109+
ident_type: None,
11110+
},
11111+
args: [
11112+
Literal {
11113+
span: Some(
11114+
22..24,
11115+
),
11116+
value: UInt64(
11117+
33,
11118+
),
11119+
},
11120+
ColumnRef {
11121+
span: Some(
11122+
26..29,
11123+
),
11124+
column: ColumnRef {
11125+
database: None,
11126+
table: None,
11127+
column: Name(
11128+
Identifier {
11129+
span: Some(
11130+
26..29,
11131+
),
11132+
name: "row",
11133+
quote: None,
11134+
ident_type: None,
11135+
},
11136+
),
11137+
},
11138+
},
11139+
],
11140+
params: [],
11141+
order_by: [],
11142+
window: None,
11143+
lambda: None,
11144+
},
11145+
},
11146+
alias: None,
11147+
},
11148+
AliasedExpr {
11149+
expr: FunctionCall {
11150+
span: Some(
11151+
32..40,
11152+
),
11153+
func: FunctionCall {
11154+
distinct: false,
11155+
name: Identifier {
11156+
span: Some(
11157+
32..35,
11158+
),
11159+
name: "def",
11160+
quote: None,
11161+
ident_type: None,
11162+
},
11163+
args: [
11164+
ColumnRef {
11165+
span: Some(
11166+
36..39,
11167+
),
11168+
column: ColumnRef {
11169+
database: None,
11170+
table: None,
11171+
column: Name(
11172+
Identifier {
11173+
span: Some(
11174+
36..39,
11175+
),
11176+
name: "row",
11177+
quote: None,
11178+
ident_type: None,
11179+
},
11180+
),
11181+
},
11182+
},
11183+
],
11184+
params: [],
11185+
order_by: [],
11186+
window: None,
11187+
lambda: None,
11188+
},
11189+
},
11190+
alias: None,
11191+
},
11192+
],
11193+
from: [],
11194+
selection: None,
11195+
group_by: None,
11196+
having: None,
11197+
window_list: None,
11198+
qualify: None,
11199+
},
11200+
),
11201+
order_by: [],
11202+
limit: [],
11203+
offset: None,
11204+
ignore_result: false,
11205+
},
11206+
)
11207+
11208+
11209+
---------- Input ----------
11210+
SELECT func(ROW) FROM (SELECT 1 as ROW) t
11211+
---------- Output ---------
11212+
SELECT func(row) FROM (SELECT 1 AS ROW) AS t
11213+
---------- AST ------------
11214+
Query(
11215+
Query {
11216+
span: Some(
11217+
0..41,
11218+
),
11219+
with: None,
11220+
body: Select(
11221+
SelectStmt {
11222+
span: Some(
11223+
0..41,
11224+
),
11225+
hints: None,
11226+
distinct: false,
11227+
top_n: None,
11228+
select_list: [
11229+
AliasedExpr {
11230+
expr: FunctionCall {
11231+
span: Some(
11232+
7..16,
11233+
),
11234+
func: FunctionCall {
11235+
distinct: false,
11236+
name: Identifier {
11237+
span: Some(
11238+
7..11,
11239+
),
11240+
name: "func",
11241+
quote: None,
11242+
ident_type: None,
11243+
},
11244+
args: [
11245+
ColumnRef {
11246+
span: Some(
11247+
12..15,
11248+
),
11249+
column: ColumnRef {
11250+
database: None,
11251+
table: None,
11252+
column: Name(
11253+
Identifier {
11254+
span: Some(
11255+
12..15,
11256+
),
11257+
name: "row",
11258+
quote: None,
11259+
ident_type: None,
11260+
},
11261+
),
11262+
},
11263+
},
11264+
],
11265+
params: [],
11266+
order_by: [],
11267+
window: None,
11268+
lambda: None,
11269+
},
11270+
},
11271+
alias: None,
11272+
},
11273+
],
11274+
from: [
11275+
Subquery {
11276+
span: Some(
11277+
22..41,
11278+
),
11279+
lateral: false,
11280+
subquery: Query {
11281+
span: Some(
11282+
23..38,
11283+
),
11284+
with: None,
11285+
body: Select(
11286+
SelectStmt {
11287+
span: Some(
11288+
23..38,
11289+
),
11290+
hints: None,
11291+
distinct: false,
11292+
top_n: None,
11293+
select_list: [
11294+
AliasedExpr {
11295+
expr: Literal {
11296+
span: Some(
11297+
30..31,
11298+
),
11299+
value: UInt64(
11300+
1,
11301+
),
11302+
},
11303+
alias: Some(
11304+
Identifier {
11305+
span: Some(
11306+
35..38,
11307+
),
11308+
name: "ROW",
11309+
quote: None,
11310+
ident_type: None,
11311+
},
11312+
),
11313+
},
11314+
],
11315+
from: [],
11316+
selection: None,
11317+
group_by: None,
11318+
having: None,
11319+
window_list: None,
11320+
qualify: None,
11321+
},
11322+
),
11323+
order_by: [],
11324+
limit: [],
11325+
offset: None,
11326+
ignore_result: false,
11327+
},
11328+
alias: Some(
11329+
TableAlias {
11330+
name: Identifier {
11331+
span: Some(
11332+
40..41,
11333+
),
11334+
name: "t",
11335+
quote: None,
11336+
ident_type: None,
11337+
},
11338+
columns: [],
11339+
},
11340+
),
11341+
pivot: None,
11342+
unpivot: None,
11343+
},
11344+
],
11345+
selection: None,
11346+
group_by: None,
11347+
having: None,
11348+
window_list: None,
11349+
qualify: None,
11350+
},
11351+
),
11352+
order_by: [],
11353+
limit: [],
11354+
offset: None,
11355+
ignore_result: false,
11356+
},
11357+
)
11358+
11359+
1105611360
---------- Input ----------
1105711361
select * from t sample row (99);
1105811362
---------- Output ---------

0 commit comments

Comments
 (0)