You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorrect NULL row in streaming MV after GROUP BY #27451
A materialized view with GROUP BY over an empty input incorrectly returns one NULL row, while its identical defining query returns zero rows.
AggGroupBySimplifyRule appears to remove the final grouping key, changing grouped aggregation into scalar aggregation and violating empty-input semantics.
Error message/log
No SQL error is reported. `SELECT * FROM m16` returns one `NULL` row, while the identical defining query returns zero rows.
To Reproduce
Run the following SQL:
CREATE MATERIALIZED VIEW m6 ASSELECTmin(x) AS col_0
FROM (VALUES (1)) AS v(x);
FLUSH;
CREATE MATERIALIZED VIEW m16 ASSELECT col_0 FROM m6
WHERE false
GROUP BY col_0;
FLUSH;
SELECT*FROM m16;
SELECT col_0 FROM m6
WHERE false
GROUP BY col_0;
Expected behavior
Both queries should return zero rows, because WHERE false produces an empty input and grouped aggregation should produce no groups.
Instead, the materialized view incorrectly contains one NULL row.
EXPLAIN (TRACE) shows that AggGroupBySimplifyRule removes the final grouping key, converting the query into a StreamSimpleAgg with internal_last_seen_value.
This appears to change the empty-input semantics from zero rows for grouped aggregation to one row for scalar aggregation, causing the unexpected NULL row.
The issue disappears when using GROUP BY GROUPING SETS ((col_0)), which avoids this simplification.
Planned fix: in AggGroupBySimplifyRule, keep at least one group key column when
the minimized key would be empty (and skip the rule for an empty group key). Other
redundant columns are still turned into internal_last_seen_value as before.
I'll add planner tests and an e2e streaming test based on the reproducer. I'll open
a PR shortly; feedback on the approach is welcome.
Activity
yashaggarwal85d commentedon Oct 21, 2026
Hi! I'd like to work on this.
Planned fix: in
AggGroupBySimplifyRule, keep at least one group key column whenthe minimized key would be empty (and skip the rule for an empty group key). Other
redundant columns are still turned into
internal_last_seen_valueas before.I'll add planner tests and an e2e streaming test based on the reproducer. I'll open
a PR shortly; feedback on the approach is welcome.