Skip to content

Commit f97c2ac

Browse files
authored
parser: fix more cast variants (#533)
1 parent 0e1950b commit f97c2ac

19 files changed

+1250
-466
lines changed

crates/squawk_lexer/src/snapshots/squawk_lexer__tests__floats.snap

Lines changed: 0 additions & 15 deletions
This file was deleted.

crates/squawk_parser/src/grammar.rs

Lines changed: 84 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,29 +1819,51 @@ fn name_ref_(p: &mut Parser<'_>) -> Option<CompletedMarker> {
18191819
let m = p.start();
18201820
// TODO: this needs to be cleaned up
18211821
let mut is_interval_cast = false;
1822-
let kind = if p.eat(COLLATION_KW) {
1823-
p.expect(FOR_KW);
1824-
NAME_REF
1825-
// timestamp with time zone / time with time zone
1826-
} else if p.eat(TIMESTAMP_KW) || p.eat(TIME_KW) {
1827-
if p.eat(L_PAREN) {
1828-
if opt_numeric_literal(p).is_none() {
1829-
p.error("expected numeric literal");
1822+
let kind = match p.current() {
1823+
COLLATION_KW => {
1824+
p.bump(COLLATION_KW);
1825+
p.expect(FOR_KW);
1826+
NAME_REF
1827+
}
1828+
TIMESTAMP_KW | TIME_KW => {
1829+
p.bump_any();
1830+
if p.eat(L_PAREN) {
1831+
if opt_numeric_literal(p).is_none() {
1832+
p.error("expected numeric literal");
1833+
}
1834+
p.expect(R_PAREN);
18301835
}
1831-
p.expect(R_PAREN);
1836+
if p.eat(WITH_KW) || p.eat(WITHOUT_KW) {
1837+
p.expect(TIME_KW);
1838+
p.expect(ZONE_KW);
1839+
}
1840+
TIME_TYPE
18321841
}
1833-
if p.eat(WITH_KW) {
1834-
p.expect(TIME_KW);
1835-
p.expect(ZONE_KW);
1842+
BIT_KW => {
1843+
p.bump(BIT_KW);
1844+
p.eat(VARYING_KW);
1845+
BIT_TYPE
1846+
}
1847+
NATIONAL_KW if matches!(p.nth(1), CHAR_KW | CHARACTER_KW) => {
1848+
p.bump(NATIONAL_KW);
1849+
char_type(p)
1850+
}
1851+
DOUBLE_KW if p.nth_at(1, PRECISION_KW) => {
1852+
p.bump(DOUBLE_KW);
1853+
p.bump(PRECISION_KW);
1854+
DOUBLE_TYPE
1855+
}
1856+
CHARACTER_KW | CHAR_KW | NCHAR_KW | VARCHAR_KW => char_type(p),
1857+
INTERVAL_KW => {
1858+
p.bump(INTERVAL_KW);
1859+
opt_interval_trailing(p);
1860+
is_interval_cast = true;
1861+
INTERVAL_TYPE
1862+
}
1863+
_ => {
1864+
p.bump_any();
1865+
NAME_REF
18361866
}
1837-
TIME_TYPE
1838-
} else if p.eat(INTERVAL_KW) {
1839-
opt_interval_trailing(p);
1840-
is_interval_cast = true;
1841-
INTERVAL_TYPE
1842-
} else {
1843-
p.bump_any();
1844-
NAME_REF
18451867
};
18461868
let cm = m.complete(p, if p.at(STRING) { kind } else { NAME_REF });
18471869

@@ -10920,7 +10942,7 @@ fn create_view(p: &mut Parser<'_>) -> CompletedMarker {
1092010942
},
1092110943
) {
1092210944
Some(statement) => match statement.kind() {
10923-
SELECT | COMPOUND_SELECT | SELECT_INTO => (),
10945+
SELECT | COMPOUND_SELECT | SELECT_INTO | VALUES | TABLE => (),
1092410946
kind => p.error(format!("expected SELECT, got {:?}", kind)),
1092510947
},
1092610948
None => p.error("expected SELECT"),
@@ -11033,8 +11055,26 @@ fn reset(p: &mut Parser<'_>) -> CompletedMarker {
1103311055
assert!(p.at(RESET_KW));
1103411056
let m = p.start();
1103511057
p.bump(RESET_KW);
11036-
if !p.eat(ALL_KW) {
11037-
path_name_ref(p);
11058+
match p.current() {
11059+
ALL_KW => {
11060+
p.bump(ALL_KW);
11061+
}
11062+
SESSION_KW => {
11063+
p.bump(SESSION_KW);
11064+
p.expect(AUTHORIZATION_KW);
11065+
}
11066+
TRANSACTION_KW => {
11067+
p.bump(TRANSACTION_KW);
11068+
p.expect(ISOLATION_KW);
11069+
p.expect(LEVEL_KW);
11070+
}
11071+
TIME_KW => {
11072+
p.bump(TIME_KW);
11073+
p.expect(ZONE_KW);
11074+
}
11075+
_ => {
11076+
path_name_ref(p);
11077+
}
1103811078
}
1103911079
m.complete(p, RESET)
1104011080
}
@@ -12945,8 +12985,26 @@ fn show(p: &mut Parser<'_>) -> CompletedMarker {
1294512985
assert!(p.at(SHOW_KW));
1294612986
let m = p.start();
1294712987
p.bump(SHOW_KW);
12948-
if !p.eat(ALL_KW) {
12949-
path_name_ref(p);
12988+
match p.current() {
12989+
ALL_KW => {
12990+
p.bump(ALL_KW);
12991+
}
12992+
SESSION_KW => {
12993+
p.bump(SESSION_KW);
12994+
p.expect(AUTHORIZATION_KW);
12995+
}
12996+
TRANSACTION_KW => {
12997+
p.bump(TRANSACTION_KW);
12998+
p.expect(ISOLATION_KW);
12999+
p.expect(LEVEL_KW);
13000+
}
13001+
TIME_KW => {
13002+
p.bump(TIME_KW);
13003+
p.expect(ZONE_KW);
13004+
}
13005+
_ => {
13006+
path_name_ref(p);
13007+
}
1295013008
}
1295113009
m.complete(p, SHOW)
1295213010
}
@@ -13268,7 +13326,7 @@ fn alter_table_action(p: &mut Parser<'_>) -> Option<SyntaxKind> {
1326813326
p.bump(DETACH_KW);
1326913327
p.expect(PARTITION_KW);
1327013328
// partition_name
13271-
name_ref(p);
13329+
path_name_ref(p);
1327213330
// [ CONCURRENTLY | FINALIZE ]
1327313331
if !p.eat(CONCURRENTLY_KW) {
1327413332
p.eat(FINALIZE_KW);

crates/squawk_parser/tests/data/ok/reset.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22
reset some_config_param;
33
reset all;
44

5+
reset foo.bar.buzz;
6+
reset time zone;
7+
reset transaction isolation level;
8+
reset session authorization;

crates/squawk_parser/tests/data/ok/select_casts.sql

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,60 @@ select cast(a as foo.bar);
211211
select treat(a as foo.b);
212212
select treat('1231' as numeric);
213213

214+
-- prefix
215+
select json '{}';
216+
217+
select timestamp(4) '';
218+
select timestamp '';
219+
220+
select time(4) '';
221+
select time '';
222+
223+
select character varying '';
224+
select character '';
225+
select char varying '';
226+
select char '';
227+
select varchar '';
228+
select national character varying '';
229+
select national character '';
230+
select national char varying '';
231+
select national char '';
232+
select nchar varying '';
233+
select nchar '';
234+
235+
select character varying(10) '';
236+
select character(10) '';
237+
select char varying(10) '';
238+
select char(10) '';
239+
select varchar(10) '';
240+
select national character varying(10) '';
241+
select national character(10) '';
242+
select national char varying(10) '';
243+
select national char(10) '';
244+
select nchar varying(10) '';
245+
select nchar(10) '';
246+
247+
select bit varying(10) '';
248+
select bit(10) '';
249+
250+
select bit varying '';
251+
select bit '';
252+
253+
select int '';
254+
select integer '';
255+
select smallint '';
256+
select bigint '';
257+
select real '';
258+
select float '';
259+
select float(8) '';
260+
select double precision '';
261+
select decimal(10, 2) '10';
262+
select decimal '10';
263+
select dec(10, 2) '10';
264+
select dec '10';
265+
select numeric(10, 2) '10';
266+
select numeric '10';
267+
select boolean 'false';
268+
269+
select foo.bar '100';
270+
select foo.bar(10, 2) '100';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
select foo[10].bar(10, 2) '100';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
show time zone;
2+
show transaction isolation level;
3+
show session authorization;
4+
show all;
5+
6+
show v;
7+
show a.b;
8+
show a.b.c.d;

crates/squawk_parser/tests/data/regression_suite/groupingsets.sql

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ create temp view gstest1(a,b,v)
1313
create temp table gstest2 (a integer, b integer, c integer, d integer,
1414
e integer, f integer, g integer, h integer);
1515
copy gstest2 from stdin;
16-
1 1 1 1 1 1 1 1
17-
1 1 1 1 1 1 1 2
18-
1 1 1 1 1 1 2 2
19-
1 1 1 1 1 2 2 2
20-
1 1 1 1 2 2 2 2
21-
1 1 1 2 2 2 2 2
22-
1 1 2 2 2 2 2 2
23-
1 2 2 2 2 2 2 2
24-
2 2 2 2 2 2 2 2
16+
-- 1 1 1 1 1 1 1 1
17+
-- 1 1 1 1 1 1 1 2
18+
-- 1 1 1 1 1 1 2 2
19+
-- 1 1 1 1 1 2 2 2
20+
-- 1 1 1 1 2 2 2 2
21+
-- 1 1 1 2 2 2 2 2
22+
-- 1 1 2 2 2 2 2 2
23+
-- 1 2 2 2 2 2 2 2
24+
-- 2 2 2 2 2 2 2 2
2525

2626
create temp table gstest3 (a integer, b integer, c integer, d integer);
2727
copy gstest3 from stdin;
28-
1 1 1 1
29-
2 2 2 2
28+
-- 1 1 1 1
29+
-- 2 2 2 2
3030
alter table gstest3 add primary key (a);
3131

3232
create temp table gstest4(id integer, v integer,

crates/squawk_parser/tests/data/regression_suite/rules.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ select * from rtest_v1;
182182
insert into rtest_v1 values (2, 12);
183183
insert into rtest_v1 values (2, 13);
184184
select * from rtest_v1;
185-
** Remember the delete rule on rtest_v1: It says
186-
** DO INSTEAD DELETE FROM rtest_t1 WHERE a = old.a
187-
** So this time both rows with a = 2 must get deleted
185+
-- ** Remember the delete rule on rtest_v1: It says
186+
-- ** DO INSTEAD DELETE FROM rtest_t1 WHERE a = old.a
187+
-- ** So this time both rows with a = 2 must get deleted
188188
delete from rtest_v1 where b = 12;
189189
select * from rtest_v1;
190190
delete from rtest_v1;

crates/squawk_parser/tests/snapshots/tests__alter_table_ok.snap

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3692,8 +3692,10 @@ SOURCE_FILE
36923692
WHITESPACE " "
36933693
PARTITION_KW "partition"
36943694
WHITESPACE " "
3695-
NAME_REF
3696-
IDENT "f"
3695+
PATH
3696+
PATH_SEGMENT
3697+
NAME_REF
3698+
IDENT "f"
36973699
SEMICOLON ";"
36983700
WHITESPACE "\n\n"
36993701
COMMENT "-- concurrently"
@@ -3714,8 +3716,10 @@ SOURCE_FILE
37143716
WHITESPACE " "
37153717
PARTITION_KW "partition"
37163718
WHITESPACE " "
3717-
NAME_REF
3718-
IDENT "f"
3719+
PATH
3720+
PATH_SEGMENT
3721+
NAME_REF
3722+
IDENT "f"
37193723
WHITESPACE " "
37203724
CONCURRENTLY_KW "concurrently"
37213725
SEMICOLON ";"
@@ -3738,8 +3742,10 @@ SOURCE_FILE
37383742
WHITESPACE " "
37393743
PARTITION_KW "partition"
37403744
WHITESPACE " "
3741-
NAME_REF
3742-
IDENT "f"
3745+
PATH
3746+
PATH_SEGMENT
3747+
NAME_REF
3748+
IDENT "f"
37433749
WHITESPACE " "
37443750
FINALIZE_KW "finalize"
37453751
SEMICOLON ";"

crates/squawk_parser/tests/snapshots/tests__regression_foreign_key.snap

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,4 @@
22
source: crates/squawk_parser/tests/tests.rs
33
input_file: crates/squawk_parser/tests/data/regression_suite/foreign_key.sql
44
---
5-
ERROR@65605: expected SEMICOLON
6-
ERROR@65605: expected command, found DOT
7-
ERROR@65606: expected command, found IDENT
8-
ERROR@66561: expected SEMICOLON
9-
ERROR@66561: expected command, found DOT
10-
ERROR@66562: expected command, found IDENT
5+

0 commit comments

Comments
 (0)