Fix handling of multiple arguments in aggregate functions

This bug occurred when arguments were read for the GROUP BY sorter — all
arguments were incorrectly resolved to the first column. Added tests
confirm that aggregates now work correctly both with and without the
sorter.
This commit is contained in:
Piotr Rzysko
2025-08-31 10:44:36 +02:00
parent 3ad4016080
commit 7d179bd9fe
2 changed files with 37 additions and 2 deletions

View File

@@ -479,7 +479,11 @@ impl<'a> GroupByAggArgumentSource<'a> {
dest_reg_start,
..
} => {
program.emit_column_or_rowid(*cursor_id, *col_start, dest_reg_start + arg_idx);
program.emit_column_or_rowid(
*cursor_id,
*col_start + arg_idx,
dest_reg_start + arg_idx,
);
Ok(dest_reg_start + arg_idx)
}
GroupByAggArgumentSource::Register {

View File

@@ -143,4 +143,35 @@ do_execsql_test select-agg-json-array-object {
do_execsql_test select-distinct-agg-functions {
SELECT sum(distinct age), count(distinct age), avg(distinct age) FROM users;
} {5050|100|50.5}
} {5050|100|50.5}
do_execsql_test select-json-group-object {
select price,
json_group_object(cast (id as text), name)
from products
group by price
order by price;
} {1.0|{"9":"boots"}
18.0|{"3":"shirt"}
25.0|{"4":"sweater"}
33.0|{"10":"coat"}
70.0|{"6":"shorts"}
74.0|{"5":"sweatshirt"}
78.0|{"7":"jeans"}
79.0|{"1":"hat"}
81.0|{"11":"accessories"}
82.0|{"2":"cap","8":"sneakers"}}
do_execsql_test select-json-group-object-no-sorting-required {
select age,
json_group_object(cast (id as text), first_name)
from users
where first_name like 'Am%'
group by age
order by age
limit 5;
} {1|{"6737":"Amy"}
2|{"2297":"Amy","3580":"Amanda"}
3|{"3437":"Amanda"}
5|{"2378":"Amy","3227":"Amy","5605":"Amanda"}
7|{"2454":"Amber"}}