Skip to content

Commit 71393c7

Browse files
authored
feat(query): Support json_agg function as an alias of json_array_agg (#18478)
1 parent 7e169a7 commit 71393c7

File tree

7 files changed

+131
-1
lines changed

7 files changed

+131
-1
lines changed

โ€Žsrc/query/functions/src/aggregates/aggregate_function_factory.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ use super::AggregateFunctionSortAdaptor;
2828
use super::Aggregators;
2929

3030
// The NULL value in the those function needs to be handled separately.
31-
const NEED_NULL_AGGREGATE_FUNCTIONS: [&str; 7] = [
31+
const NEED_NULL_AGGREGATE_FUNCTIONS: [&str; 8] = [
3232
"array_agg",
3333
"list",
34+
"json_agg",
3435
"json_array_agg",
3536
"json_object_agg",
3637
"group_array_moving_avg",

โ€Žsrc/query/functions/src/aggregates/aggregator.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ impl Aggregators {
127127
"group_array_moving_sum",
128128
aggregate_array_moving_sum_function_desc(),
129129
);
130+
factory.register("json_agg", aggregate_json_array_agg_function_desc());
130131
factory.register("json_array_agg", aggregate_json_array_agg_function_desc());
131132
factory.register("json_object_agg", aggregate_json_object_agg_function_desc());
132133
factory.register("kurtosis", aggregate_kurtosis_function_desc());

โ€Žsrc/query/functions/tests/it/aggregates/agg.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ fn test_aggr_funtions() {
9090
test_agg_group_array_moving_avg(file, eval_aggr);
9191
test_agg_group_array_moving_sum(file, eval_aggr);
9292
test_agg_histogram(file, eval_aggr);
93+
test_agg_json_agg(file, eval_aggr);
9394
test_agg_json_array_agg(file, eval_aggr);
9495
test_agg_json_object_agg(file, eval_aggr);
9596
test_agg_mode(file, eval_aggr);
@@ -132,6 +133,7 @@ fn test_aggr_functions_group_by() {
132133
test_agg_bitmap(file, simulate_two_groups_group_by);
133134
test_agg_group_array_moving_avg(file, eval_aggr);
134135
test_agg_group_array_moving_sum(file, eval_aggr);
136+
test_agg_json_agg(file, eval_aggr);
135137
test_agg_json_array_agg(file, eval_aggr);
136138
test_agg_json_object_agg(file, eval_aggr);
137139
test_agg_mode(file, simulate_two_groups_group_by);
@@ -1308,6 +1310,30 @@ fn test_agg_histogram(file: &mut impl Write, simulator: impl AggregationSimulato
13081310
);
13091311
}
13101312

1313+
fn test_agg_json_agg(file: &mut impl Write, simulator: impl AggregationSimulator) {
1314+
run_agg_ast(
1315+
file,
1316+
"json_agg(a)",
1317+
get_example().as_slice(),
1318+
simulator,
1319+
vec![],
1320+
);
1321+
run_agg_ast(
1322+
file,
1323+
"json_agg(x_null)",
1324+
get_example().as_slice(),
1325+
simulator,
1326+
vec![],
1327+
);
1328+
run_agg_ast(
1329+
file,
1330+
"json_agg(dec)",
1331+
get_example().as_slice(),
1332+
simulator,
1333+
vec![],
1334+
);
1335+
}
1336+
13111337
fn test_agg_json_array_agg(file: &mut impl Write, simulator: impl AggregationSimulator) {
13121338
run_agg_ast(
13131339
file,

โ€Žsrc/query/functions/tests/it/aggregates/testdata/agg.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,36 @@ evaluation (internal):
13951395
+--------+----------------------------------------------------------------------------------------------------------------------------+
13961396

13971397

1398+
ast: json_agg(a)
1399+
evaluation (internal):
1400+
+--------+-----------------------------------------------------------------------+
1401+
| Column | Data |
1402+
+--------+-----------------------------------------------------------------------+
1403+
| a | Int64([4, 3, 2, 1]) |
1404+
| Output | Variant([0x80000004200000022000000220000002200000024004400340024001]) |
1405+
+--------+-----------------------------------------------------------------------+
1406+
1407+
1408+
ast: json_agg(x_null)
1409+
evaluation (internal):
1410+
+--------+-------------------------------------------------------------------------+
1411+
| Column | Data |
1412+
+--------+-------------------------------------------------------------------------+
1413+
| x_null | NullableColumn { column: UInt64([1, 2, 3, 4]), validity: [0b____0011] } |
1414+
| Output | Variant([0x80000002200000022000000250015002]) |
1415+
+--------+-------------------------------------------------------------------------+
1416+
1417+
1418+
ast: json_agg(dec)
1419+
evaluation (internal):
1420+
+--------+-----------------------------------------------------------------------------------------------------------+
1421+
| Column | Data |
1422+
+--------+-----------------------------------------------------------------------------------------------------------+
1423+
| dec | NullableColumn { column: Decimal64([1.10, 2.20, 0.00, 3.30]), validity: [0b____1011] } |
1424+
| Output | Variant([0x800000032000000a2000000a2000000a70000000000000006e027000000000000000dc0270000000000000014a02]) |
1425+
+--------+-----------------------------------------------------------------------------------------------------------+
1426+
1427+
13981428
ast: json_array_agg(1)
13991429
evaluation (internal):
14001430
+--------+-----------------------------------------------------------------------+

โ€Žsrc/query/functions/tests/it/aggregates/testdata/agg_group_by.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,36 @@ evaluation (internal):
13331333
+--------+----------------------------------------------------------------------------------------+
13341334

13351335

1336+
ast: json_agg(a)
1337+
evaluation (internal):
1338+
+--------+-----------------------------------------------------------------------+
1339+
| Column | Data |
1340+
+--------+-----------------------------------------------------------------------+
1341+
| a | Int64([4, 3, 2, 1]) |
1342+
| Output | Variant([0x80000004200000022000000220000002200000024004400340024001]) |
1343+
+--------+-----------------------------------------------------------------------+
1344+
1345+
1346+
ast: json_agg(x_null)
1347+
evaluation (internal):
1348+
+--------+-------------------------------------------------------------------------+
1349+
| Column | Data |
1350+
+--------+-------------------------------------------------------------------------+
1351+
| x_null | NullableColumn { column: UInt64([1, 2, 3, 4]), validity: [0b____0011] } |
1352+
| Output | Variant([0x80000002200000022000000250015002]) |
1353+
+--------+-------------------------------------------------------------------------+
1354+
1355+
1356+
ast: json_agg(dec)
1357+
evaluation (internal):
1358+
+--------+-----------------------------------------------------------------------------------------------------------+
1359+
| Column | Data |
1360+
+--------+-----------------------------------------------------------------------------------------------------------+
1361+
| dec | NullableColumn { column: Decimal64([1.10, 2.20, 0.00, 3.30]), validity: [0b____1011] } |
1362+
| Output | Variant([0x800000032000000a2000000a2000000a70000000000000006e027000000000000000dc0270000000000000014a02]) |
1363+
+--------+-----------------------------------------------------------------------------------------------------------+
1364+
1365+
13361366
ast: json_array_agg(1)
13371367
evaluation (internal):
13381368
+--------+-----------------------------------------------------------------------+

โ€Žtests/sqllogictests/suites/query/cte/basic_r_cte.test

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,43 @@ from
283283
----
284284
236700 473200 210000
285285

286+
statement ok
287+
CREATE OR REPLACE table t_tree (id int, parent int, name string);
288+
289+
statement ok
290+
INSERT INTO t_tree VALUES(1, null, 'root'),(2, 1, 'a'),(3, 2, 'b'),(4, 1, 'c'),(5, 4, 'd');
291+
292+
query T
293+
WITH RECURSIVE tree AS (
294+
SELECT
295+
id,
296+
parent,
297+
name,
298+
CAST(name AS TEXT) AS path,
299+
1 AS level
300+
FROM
301+
t_tree
302+
WHERE
303+
parent IS NULL
304+
UNION ALL
305+
SELECT
306+
t.id,
307+
t.parent,
308+
t.name,
309+
tree.path || '->' || t.name,
310+
tree.level + 1
311+
FROM
312+
t_tree t
313+
INNER JOIN tree ON t.parent = tree.id
314+
)
315+
SELECT json_object_agg(name, (
316+
SELECT json_object_agg(name, (
317+
SELECT json_agg(name) FROM tree WHERE parent = t2.id and level = 3
318+
)) FROM tree t2 WHERE parent = t1.id and level=2
319+
)) FROM tree t1 WHERE level = 1;
320+
----
321+
{"root":{"a":["b"],"c":["d"]}}
322+
286323
statement ok
287324
use default;
288325

โ€Žtests/sqllogictests/suites/query/functions/02_0000_function_aggregate_mix.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,11 @@ select stddev_samp(a) from d where b = 'abc';
512512
----
513513
NULL
514514

515+
query TTTTT
516+
select json_agg(a), json_agg(b), json_agg(c), json_agg(d), json_agg(e), json_agg('a') from d
517+
----
518+
[20.00,10.00,4.23,5.99] ["abc","de","xyz"] [100,200,300] [{"k":"v"},null,"uvw",[1,2,3]] [["a","b"],[],["x","y"],["z"]] ["a","a","a","a"]
519+
515520
query TTTTT
516521
select json_array_agg(a), json_array_agg(b), json_array_agg(c), json_array_agg(d), json_array_agg(e), json_array_agg('a') from d
517522
----

0 commit comments

Comments
ย (0)