Skip to content

Commit 64338b9

Browse files
committed
feat: update array_agg to use COALESCE for handling empty results in DuckDB and SQLite
1 parent 5c2295d commit 64338b9

3 files changed

Lines changed: 21 additions & 4 deletions

File tree

prqlc/prqlc/src/sql/std.sql.prql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ module clickhouse {
275275
}
276276

277277
module duckdb {
278+
@{window_frame=true, coalesce="[]"}
279+
let array_agg = column -> s"ARRAY_AGG({column:0})"
280+
278281
@{window_frame=true}
279282
let max_by = column by -> s"arg_max({column:0}, {by:0})"
280283

@@ -439,7 +442,7 @@ module sqlite {
439442
@{window_frame=true, coalesce="''"}
440443
let concat_array = column -> s"GROUP_CONCAT({column:0}, '')"
441444

442-
@{window_frame=true}
445+
@{window_frame=true, coalesce="'[]'"}
443446
let array_agg = column -> s"json_group_array({column:0})"
444447

445448
@{binding_strength=11}

prqlc/prqlc/tests/integration/snapshots/integration__queries__compileall__aggregation.snap

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ expression: "# mysql:skip\n# clickhouse:skip\n# glaredb:skip (the string_agg fun
44
input_file: prqlc/prqlc/tests/integration/queries/aggregation.prql
55
---
66

7+
--- generic
8+
+++ duckdb
9+
@@ -1,10 +1,10 @@
10+
SELECT
11+
COALESCE(SUM(track_id), 0),
12+
COALESCE(STRING_AGG(name, ''), ''),
13+
- ARRAY_AGG(name),
14+
+ COALESCE(ARRAY_AGG(name), []),
15+
COALESCE(BOOL_AND(name = ''), TRUE),
16+
COALESCE(BOOL_OR(name = ''), FALSE)
17+
FROM
18+
tracks
19+
WHERE
20+
genre_id = 100
721

822

923

@@ -18,7 +32,7 @@ input_file: prqlc/prqlc/tests/integration/queries/aggregation.prql
1832
- COALESCE(BOOL_AND(name = ''), TRUE),
1933
- COALESCE(BOOL_OR(name = ''), FALSE)
2034
+ COALESCE(GROUP_CONCAT(name, ''), ''),
21-
+ json_group_array(name),
35+
+ COALESCE(json_group_array(name), '[]'),
2236
+ COALESCE(MIN(name = '') > 0, TRUE),
2337
+ COALESCE(MAX(name = '') > 0, FALSE)
2438
FROM

prqlc/prqlc/tests/integration/sql.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn test_array_agg_per_dialect() {
5252

5353
assert_snapshot!(compile_with_sql_dialect(query, sql::Dialect::DuckDb).unwrap(), @r#"
5454
SELECT
55-
ARRAY_AGG(name) AS names
55+
COALESCE(ARRAY_AGG(name), []) AS names
5656
FROM
5757
tracks
5858
"#);
@@ -73,7 +73,7 @@ fn test_array_agg_per_dialect() {
7373

7474
assert_snapshot!(compile_with_sql_dialect(query, sql::Dialect::SQLite).unwrap(), @r#"
7575
SELECT
76-
json_group_array(name) AS names
76+
COALESCE(json_group_array(name), '[]') AS names
7777
FROM
7878
tracks
7979
"#);

0 commit comments

Comments
 (0)