Skip to content

Commit b04a423

Browse files
authored
parser: fix parsing string literal type cast (#446)
previously we'd parse: ```sql select t 1; ``` as a type cast, but this sort of type cast is only valid when we're using a string literal! Now we're more restrictive
1 parent 25e4315 commit b04a423

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

crates/squawk_parser/src/grammar.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,10 +1798,10 @@ fn name_ref_(p: &mut Parser<'_>) -> Option<CompletedMarker> {
17981798
p.bump_any();
17991799
}
18001800
let cm = m.complete(p, NAME_REF);
1801-
// A path followed by a literal is a type cast so we insert a CAST_EXPR
1801+
// A path followed by a string is a type cast so we insert a CAST_EXPR
18021802
// preceding it to wrap the previously parsed data.
18031803
// e.g., `select numeric '12312'`
1804-
if !p.at(NULL_KW) && !p.at(DEFAULT_KW) && literal(p).is_some() {
1804+
if opt_string_literal(p).is_some() {
18051805
if is_interval_cast {
18061806
opt_interval_trailing(p);
18071807
}

crates/squawk_parser/src/snapshots/squawk_parser__test__select_err.snap

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,22 @@ SOURCE_FILE
192192
IDENT "c"
193193
SEMICOLON ";"
194194
WHITESPACE "\n\n"
195+
COMMENT "-- type cast must use a string literal"
196+
WHITESPACE "\n"
197+
SELECT
198+
SELECT_CLAUSE
199+
SELECT_KW "select"
200+
WHITESPACE " "
201+
TARGET_LIST
202+
TARGET
203+
NAME_REF
204+
NUMERIC_KW "numeric"
205+
WHITESPACE " "
206+
TARGET
207+
LITERAL
208+
INT_NUMBER "1234"
209+
SEMICOLON ";"
210+
WHITESPACE "\n\n"
195211
COMMENT "-- trailing comma at EOF"
196212
WHITESPACE "\n"
197213
SELECT
@@ -215,4 +231,5 @@ ERROR@394: expected expression
215231
ERROR@395: expected expression
216232
ERROR@396: expected expression
217233
ERROR@397: expected expression
218-
ERROR@500: unexpected trailing comma
234+
ERROR@520: missing comma
235+
ERROR@561: unexpected trailing comma

crates/squawk_parser/test_data/err/select.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@ select f(a,,,,,);
2222
-- in can only be used with tuples / sub queries
2323
select 1 in c;
2424

25+
-- type cast must use a string literal
26+
select numeric 1234;
27+
2528
-- trailing comma at EOF
2629
select 1,

0 commit comments

Comments
 (0)