Describe the bug
A grouped aggregate with a FILTER clause can fail when a group exists but none of its rows satisfy the aggregate filter.
The grouping key is created independently of the aggregate filter. However, the accumulator is currently expanded only when aggregate rows are updated. If all rows for an aggregate are filtered out, its accumulator may remain shorter than the number of groups.
The query then fails while producing the result:
index out of bounds: the len is 0 but the index is 0
To Reproduce
Run a filtered aggregation where one merchant has no refunded orders:
WITH orders AS (
SELECT * FROM VALUES
('A', 100, 'PAID'),
('A', 20, 'REFUNDED'),
('B', 200, 'PAID'),
('B', 300, 'PAID')
AS orders(merchant_id, amount, status)
)
SELECT
merchant_id,
SUM(amount) FILTER (WHERE status = 'PAID') AS paid_amount,
COUNT(*) FILTER (WHERE status = 'REFUNDED') AS refund_count
FROM orders
GROUP BY merchant_id
ORDER BY merchant_id;
Expected behavior
All discovered groups should be preserved, using each aggregate's empty-input value:
Screenshots
Additional context
Describe the bug
A grouped aggregate with a
FILTERclause can fail when a group exists but none of its rows satisfy the aggregate filter.The grouping key is created independently of the aggregate filter. However, the accumulator is currently expanded only when aggregate rows are updated. If all rows for an aggregate are filtered out, its accumulator may remain shorter than the number of groups.
The query then fails while producing the result:
To Reproduce
Run a filtered aggregation where one merchant has no refunded orders:
Expected behavior
All discovered groups should be preserved, using each aggregate's empty-input value:
Screenshots
Additional context