Repository navigation
Commit 60cf0b3
aggregate: add window_prepare hook for per-partition derived state
Windowed aggregates with non-trivial window_init() state today have no
clean way to memoise the deserialised form across the many window() calls
the framework makes per partition: _WindowStatePlaceholder uses __slots__
without __weakref__, so neither attribute attachment nor WeakKeyDictionary
works, and (execution_id, partition_id) — the framework's own cache key —
isn't exposed to user code. Users end up with module-level dicts keyed by
id(), with content-addressable identity verification to defend against id
reuse after gc. That's a footgun: in our portfolio_ops worker we hit a
silent correctness bug where 89,949 out of 99,844 cells diverged when an
old partition's state was reused for a fresh one.
window_prepare is a new optional classmethod on AggregateFunction that
runs once per partition (in aggregate_window_init for fresh state, or
lazily on the first dispatcher call for cold-reloaded state), with its
return value passed to every subsequent window() call as window_state.
The result lives on _CachedWindowPartition.prepared_state alongside the
placeholder; it's dropped automatically when the partition is evicted
from the LRU or its destructor fires — same lifecycle as the framework's
existing partition cache, no user-side bookkeeping required.
Default implementation returns window_state unchanged, so aggregates that
don't define this hook see the placeholder exactly as before — fully
backward compatible.
For our N=100K cumulative-window benchmark, switching from the
id()-keyed cache workaround to window_prepare drops portfolio_agg_numpy
from 1.1s to 0.83s (parity with the equivalent native UNNEST + sum() OVER
+ map_from_entries SQL). The bigger win is in the user code: ~80 lines
of cache + identity-check logic delete.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent d2fe246 commit 60cf0b3
2 files changed
Lines changed: 84 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
413 | 413 | | |
414 | 414 | | |
415 | 415 | | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
416 | 450 | | |
417 | 451 | | |
418 | 452 | | |
| |||
429 | 463 | | |
430 | 464 | | |
431 | 465 | | |
432 | | - | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
433 | 470 | | |
434 | 471 | | |
435 | 472 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
284 | 284 | | |
285 | 285 | | |
286 | 286 | | |
287 | | - | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
288 | 297 | | |
289 | 298 | | |
290 | 299 | | |
291 | 300 | | |
| 301 | + | |
292 | 302 | | |
293 | 303 | | |
294 | 304 | | |
| |||
1832 | 1842 | | |
1833 | 1843 | | |
1834 | 1844 | | |
| 1845 | + | |
| 1846 | + | |
| 1847 | + | |
| 1848 | + | |
| 1849 | + | |
| 1850 | + | |
1835 | 1851 | | |
1836 | 1852 | | |
1837 | 1853 | | |
1838 | 1854 | | |
1839 | 1855 | | |
1840 | 1856 | | |
1841 | 1857 | | |
| 1858 | + | |
1842 | 1859 | | |
1843 | 1860 | | |
1844 | 1861 | | |
| |||
1864 | 1881 | | |
1865 | 1882 | | |
1866 | 1883 | | |
1867 | | - | |
1868 | 1884 | | |
1869 | 1885 | | |
1870 | 1886 | | |
| |||
1878 | 1894 | | |
1879 | 1895 | | |
1880 | 1896 | | |
| 1897 | + | |
| 1898 | + | |
| 1899 | + | |
| 1900 | + | |
| 1901 | + | |
| 1902 | + | |
| 1903 | + | |
| 1904 | + | |
1881 | 1905 | | |
1882 | | - | |
| 1906 | + | |
| 1907 | + | |
| 1908 | + | |
1883 | 1909 | | |
1884 | 1910 | | |
1885 | 1911 | | |
| |||
1901 | 1927 | | |
1902 | 1928 | | |
1903 | 1929 | | |
| 1930 | + | |
| 1931 | + | |
| 1932 | + | |
| 1933 | + | |
| 1934 | + | |
1904 | 1935 | | |
1905 | 1936 | | |
1906 | 1937 | | |
| |||
1938 | 1969 | | |
1939 | 1970 | | |
1940 | 1971 | | |
| 1972 | + | |
1941 | 1973 | | |
1942 | 1974 | | |
1943 | 1975 | | |
| |||
1962 | 1994 | | |
1963 | 1995 | | |
1964 | 1996 | | |
1965 | | - | |
1966 | 1997 | | |
1967 | 1998 | | |
1968 | 1999 | | |
| |||
1976 | 2007 | | |
1977 | 2008 | | |
1978 | 2009 | | |
| 2010 | + | |
| 2011 | + | |
| 2012 | + | |
| 2013 | + | |
| 2014 | + | |
| 2015 | + | |
| 2016 | + | |
| 2017 | + | |
1979 | 2018 | | |
1980 | 2019 | | |
1981 | 2020 | | |
| |||
1993 | 2032 | | |
1994 | 2033 | | |
1995 | 2034 | | |
1996 | | - | |
| 2035 | + | |
| 2036 | + | |
| 2037 | + | |
1997 | 2038 | | |
1998 | 2039 | | |
1999 | 2040 | | |
| |||
0 commit comments