Repository navigation
Commit b77ce27
aggregate: persist state by tracking explicit user writes
Replace the byte-equality optimisation in `aggregate_update` that
skipped persisting "new groups whose state didn't change" with an
explicit tracking dict. The old heuristic conflated two cases:
1. update() saw rows but chose not to mutate state (e.g. SumFunction
skipping all-NULL value_sum) -> finalize should return NULL.
2. update() saw rows and assigned a state byte-equal to the initial
state (e.g. SumState(total=0) after summing zeros) -> finalize
should return that state.
The conflation meant `vgi_sum(0)` returned NULL where built-in
`sum(0)` returns 0 — observable both as `SELECT vgi_sum(0) FROM
range(5)` and `SELECT vgi_sum(i) OVER (ORDER BY i ROWS BETWEEN 1
PRECEDING AND CURRENT ROW) FROM range(5)`. Same trap applied to any
aggregate whose post-update state happened to match its initial
state for some input.
The fix introduces `_UpdateTrackingDict`, a dict subclass that
records every gid the user explicitly reassigns. After update()
runs, the framework persists (a) every gid that already had storage
from a prior batch and (b) every gid the user wrote during this
batch. Pre-existing entries from prior batches are also persisted
so multi-batch state survives. The all-NULL guarantee is preserved
because SumFunction's `if val is not None` guard never executes
`states[gid] = ...`, leaving the gid out of `states.written`.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent ac1caf4 commit b77ce27
1 file changed
Lines changed: 51 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
289 | 289 | | |
290 | 290 | | |
291 | 291 | | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
292 | 316 | | |
293 | 317 | | |
294 | 318 | | |
| |||
1686 | 1710 | | |
1687 | 1711 | | |
1688 | 1712 | | |
1689 | | - | |
1690 | | - | |
1691 | | - | |
| 1713 | + | |
| 1714 | + | |
| 1715 | + | |
| 1716 | + | |
| 1717 | + | |
| 1718 | + | |
| 1719 | + | |
| 1720 | + | |
| 1721 | + | |
| 1722 | + | |
| 1723 | + | |
| 1724 | + | |
| 1725 | + | |
| 1726 | + | |
| 1727 | + | |
| 1728 | + | |
| 1729 | + | |
1692 | 1730 | | |
1693 | 1731 | | |
1694 | 1732 | | |
1695 | 1733 | | |
1696 | 1734 | | |
| 1735 | + | |
1697 | 1736 | | |
1698 | | - | |
1699 | | - | |
1700 | | - | |
1701 | | - | |
| 1737 | + | |
| 1738 | + | |
| 1739 | + | |
| 1740 | + | |
1702 | 1741 | | |
1703 | 1742 | | |
1704 | 1743 | | |
| |||
1731 | 1770 | | |
1732 | 1771 | | |
1733 | 1772 | | |
1734 | | - | |
1735 | | - | |
1736 | | - | |
1737 | | - | |
1738 | | - | |
1739 | | - | |
1740 | | - | |
1741 | | - | |
1742 | | - | |
| 1773 | + | |
| 1774 | + | |
| 1775 | + | |
| 1776 | + | |
| 1777 | + | |
1743 | 1778 | | |
1744 | 1779 | | |
1745 | 1780 | | |
| |||
0 commit comments