1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//! Cfg-gated aggregate-buffering probe (issue #1578, Epic D / D2).
//!
//! # Why this exists
//!
//! D2 makes a GROUP-BY-free aggregate (`COUNT`/`MIN`/`MAX`/`SUM`/`AVG` with no
//! `GROUP BY`) fold the scan stream into an O(1) accumulator instead of buffering
//! the whole table into a `Vec<QueryRow>` before aggregating. A correct answer
//! never proves *how much memory* the aggregate held — `COUNT(*)` returns the
//! same number whether it streamed one row at a time or materialized the table.
//! This probe makes the buffering observable so the O(1) claim is pinned the
//! no-heuristics way: observe the *work* (rows resident in the aggregate input
//! buffer), not just the result.
//!
//! # The counter
//!
//! - [`record_buffered_rows`] adds the number of rows a single **buffered**
//! aggregate input carried (the length of the `Vec<QueryRow>` fed into the
//! materializing `execute_aggregation`). The streaming fold path never calls
//! it, so a GROUP-BY-free aggregate driven by the fold records `0` regardless
//! of table size. On `main`/pre-D2 a `COUNT(*)` over N rows buffered all N and
//! recorded N — so the D2 memory guard (`buffered_rows() == 0` for a global
//! aggregate; flat across fixture sizes) flips red without the fold.
//!
//! # Zero overhead in release (same pattern as [`crate::storage::sstable::read_work_counters`])
//!
//! [`record_buffered_rows`] is an **unconditional** `#[inline(always)]` public
//! function; its body is `#[cfg(any(test, feature = "work-counters"))]`. In a
//! default/release build (no `work-counters`, no `cfg(test)`) the body is empty
//! and optimizes away — no atomic is even linked. The getter + [`reset`] are
//! `#[cfg(any(test, feature = "work-counters"))]`: in-crate unit tests reach them
//! via the `test` cfg; integration tests in `tests/` enable the `work-counters`
//! feature (they compile the lib WITHOUT its `test` cfg).
use ;
/// Process-global count of rows fed into the *buffered* aggregate input since the
/// last [`reset`] (test/feature builds only).
static BUFFERED_ROWS: AtomicU64 = new;
/// Record that a buffered aggregate input carried `n` rows (`AGG_BUFFERED_ROWS`;
/// consumer D2). Called unconditionally by the materializing `execute_aggregation`;
/// the body compiles to a no-op in a release build.
/// Rows fed into the buffered aggregate input since the last [`reset`]
/// (`AGG_BUFFERED_ROWS`). A GROUP-BY-free aggregate served by the D2 streaming
/// fold records `0`.
/// Clear the process-global counter. Integration tests call this before a measured
/// query so a stale value cannot satisfy a later assertion; because the global is
/// shared, callers serialize on the shared test mutex (the counter-test convention).