Skip to content

Commit da00abe

Browse files
authored
fix: agg modifiers (#498)
fixes parsing arg lists with modifiers such as `DISTINCT`. before, additional args after `DISTINCT` were failing: ```sql select string_agg(distinct test, ',') from x; ```
1 parent 733e718 commit da00abe

File tree

3 files changed

+75
-11
lines changed

3 files changed

+75
-11
lines changed

crates/squawk_parser/src/grammar.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,24 +1696,19 @@ fn arg_list(p: &mut Parser<'_>) {
16961696
m.complete(p, ARG_LIST);
16971697
return;
16981698
}
1699-
if p.nth_at(1, DISTINCT_KW) || p.nth_at(1, ALL_KW) {
1700-
p.bump(L_PAREN);
1701-
p.bump_any();
1702-
if arg_expr(p).is_none() {
1703-
p.error("expected expression");
1704-
}
1705-
p.expect(R_PAREN);
1706-
m.complete(p, ARG_LIST);
1707-
return;
1708-
}
17091699
delimited(
17101700
p,
17111701
L_PAREN,
17121702
R_PAREN,
17131703
COMMA,
17141704
|| "expected expression".into(),
17151705
EXPR_FIRST.union(ATTRIBUTE_FIRST),
1716-
|p| arg_expr(p).is_some(),
1706+
|p| {
1707+
if p.at(DISTINCT_KW) || p.at(ALL_KW) {
1708+
p.bump_any(); // consume DISTINCT or ALL
1709+
}
1710+
arg_expr(p).is_some()
1711+
},
17171712
);
17181713
m.complete(p, ARG_LIST);
17191714
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,3 +500,9 @@ ORDER BY sensor_id, day;
500500

501501
-- select_from_user_table
502502
select * from user;
503+
504+
-- select with aggregates
505+
explain (costs off)
506+
select string_agg(distinct f1, ',') filter (where length(f1) > 1)
507+
from varchar_tbl;
508+

crates/squawk_parser/tests/snapshots/tests__select_ok.snap

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
source: crates/squawk_parser/tests/tests.rs
33
input_file: crates/squawk_parser/tests/data/ok/select.sql
4+
snapshot_kind: text
45
---
56
SOURCE_FILE
67
COMMENT "-- parens_and_unions"
@@ -5864,4 +5865,66 @@ SOURCE_FILE
58645865
NAME_REF
58655866
USER_KW "user"
58665867
SEMICOLON ";"
5868+
WHITESPACE "\n\n"
5869+
COMMENT "-- select with aggregates"
58675870
WHITESPACE "\n"
5871+
EXPLAIN
5872+
EXPLAIN_KW "explain"
5873+
WHITESPACE " "
5874+
L_PAREN "("
5875+
IDENT "costs"
5876+
WHITESPACE " "
5877+
LITERAL
5878+
OFF_KW "off"
5879+
R_PAREN ")"
5880+
WHITESPACE "\n"
5881+
SELECT
5882+
SELECT_CLAUSE
5883+
SELECT_KW "select"
5884+
WHITESPACE " "
5885+
TARGET_LIST
5886+
TARGET
5887+
CALL_EXPR
5888+
NAME_REF
5889+
IDENT "string_agg"
5890+
ARG_LIST
5891+
L_PAREN "("
5892+
DISTINCT_KW "distinct"
5893+
WHITESPACE " "
5894+
NAME_REF
5895+
IDENT "f1"
5896+
COMMA ","
5897+
WHITESPACE " "
5898+
LITERAL
5899+
STRING "','"
5900+
R_PAREN ")"
5901+
WHITESPACE " "
5902+
FILTER_CLAUSE
5903+
FILTER_KW "filter"
5904+
WHITESPACE " "
5905+
L_PAREN "("
5906+
WHERE_KW "where"
5907+
WHITESPACE " "
5908+
BIN_EXPR
5909+
CALL_EXPR
5910+
NAME_REF
5911+
IDENT "length"
5912+
ARG_LIST
5913+
L_PAREN "("
5914+
NAME_REF
5915+
IDENT "f1"
5916+
R_PAREN ")"
5917+
WHITESPACE " "
5918+
R_ANGLE ">"
5919+
WHITESPACE " "
5920+
LITERAL
5921+
INT_NUMBER "1"
5922+
R_PAREN ")"
5923+
WHITESPACE "\n"
5924+
FROM_CLAUSE
5925+
FROM_KW "from"
5926+
WHITESPACE " "
5927+
NAME_REF
5928+
IDENT "varchar_tbl"
5929+
SEMICOLON ";"
5930+
WHITESPACE "\n\n"

0 commit comments

Comments
 (0)