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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//! The ordered window — the bounded top-k container an `ORDER BY ...
//! LIMIT` clause folds its producer into (ADR-0555 §3). Split out of
//! `order.rs` when the family grew past a third of it: the sort's own
//! vocabulary (keys, parking, comparison, paging) stays there, the
//! container and its charge protocol live here.
use std::cmp::Ordering as CmpOrdering;
use std::collections::BinaryHeap;
use relate_core::{Result, Value};
use crate::overlay::ExecGraph;
use crate::row::Row;
use super::order::{cmp_sort_keys, fill_parked_keys, OrderKeys};
/// One entry of a [`TopKWindow`]: the parked keys beside the row they sort,
/// with the arrival sequence that keeps the selection stable.
///
/// `Ord` is the sort's own comparator ([`cmp_sort_keys`] over the parked
/// keys, the sequence as the tiebreak): better sorts *smaller*, so a
/// `BinaryHeap`'s root is the entry the next better row evicts.
struct TopKEntry<'k, 'a> {
keys: &'k OrderKeys<'a>,
parked: Vec<Value>,
seq: usize,
row: Row,
/// The entry's landing figure, taken once at construction (the row
/// solely owned its payloads then) and refunded verbatim on eviction —
/// re-measuring at eviction would read the shared key payloads as zero.
bytes: u64,
}
impl PartialEq for TopKEntry<'_, '_> {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == CmpOrdering::Equal
}
}
impl Eq for TopKEntry<'_, '_> {}
impl PartialOrd for TopKEntry<'_, '_> {
fn partial_cmp(&self, other: &Self) -> Option<CmpOrdering> {
Some(self.cmp(other))
}
}
impl Ord for TopKEntry<'_, '_> {
fn cmp(&self, other: &Self) -> CmpOrdering {
cmp_sort_keys(self.keys, &self.parked, self.seq, &other.parked, other.seq)
}
}
/// The bounded form of an ordered window (ADR-0555 §3: "a top-k heap for
/// ordered limits") — the container an `ORDER BY ... LIMIT` (plus its
/// `SKIP`) folds its producer into, row by row, instead of collecting the
/// projection's every output row first. At most `keep` entries are ever
/// held: past capacity the newcomer replaces the worst entry only when it
/// sorts before it, and is dropped otherwise.
///
/// The window is the retaining container and charges what it holds
/// (ADR-0463): each entry's row and key columns are added on insert and
/// subtracted on eviction, so the running charge is the live window, never
/// the input. The projection's per-row charge (`plain_rows`'s
/// `retain_bytes`) does not exist on this path — the window is that
/// charge's bounded replacement.
pub(crate) struct TopKWindow<'k, 'a> {
keys: &'k OrderKeys<'a>,
keep: std::num::NonZeroUsize,
seq: usize,
/// Worst entry at the root — the one the next better row evicts.
heap: BinaryHeap<TopKEntry<'k, 'a>>,
/// The comparison scratch: one key vector refilled per offered row, so
/// a row the window *rejects* — the overwhelming majority on a large
/// input — costs no allocation. While the scratch holds the vector it
/// is uncharged: its buffer is one key row's worth of slots, a fixed
/// cost of the container like the heap's own storage. On admission the
/// filled vector is *moved* into the entry as that entry's parked keys
/// and is charged with it through [`entry_bytes`]; the next comparison
/// reallocates a fresh, uncharged scratch.
scratch: Vec<Value>,
/// The live window's bytes — the one source of its size. The charge
/// tracks them whether or not a workspace ledger is installed, so the
/// default-on per-allocation cap reads the same figure the ledger
/// admission does ([`relate_core::tokens::WorkspaceCharge::bytes`]).
charge: relate_core::tokens::WorkspaceCharge,
}
impl<'k, 'a> TopKWindow<'k, 'a> {
/// `keep` entries at most. The capacity is a [`std::num::NonZeroUsize`], so the
/// "at least one entry" invariant is the type's rather than a promise
/// the caller keeps: a zero-capacity page is the clause's own decision
/// ([`super::classification::ProjectionClass::window_keep`]) and never
/// reaches a window. The
/// charge starts empty and tracks the live window — no reservation to
/// make yet, so the construction cannot fail.
pub(crate) fn new(keys: &'k OrderKeys<'a>, keep: std::num::NonZeroUsize) -> Self {
Self {
keys,
keep,
seq: 0,
heap: BinaryHeap::new(),
scratch: Vec::with_capacity(keys.len()),
charge: relate_core::tokens::WorkspaceCharge::empty(),
}
}
/// Offer one projected row (its keys already parked). Both charges are
/// settled **before** the window mutates: the prospective total is
/// computed by peeking the entry the push would evict, so a refusal —
/// either the per-allocation cap or the workspace ledger — leaves the
/// heap and the running total exactly as they were.
pub(crate) fn push(&mut self, graph: &ExecGraph<'_>, row: Row) -> Result<()> {
// The landing figure is taken while the row still solely owns its
// payloads: the key refill below shares each parked payload with the
// row's own `__ord*` column, and the ownership-aware walk counts a
// payload only where one holder owns it — measured afterwards, the
// key payloads would count zero times and the workspace cap would
// not cover them.
let bytes = entry_bytes(&row, self.keys.len());
fill_parked_keys(self.keys, &row, &mut self.scratch);
// The prospective state: a window with room keeps the entry; a full
// one keeps it only when it sorts before the worst held, and then
// evicts that worst. A newcomer that loses to the worst is dropped
// uncharged, with the window untouched — and, since it is compared
// from the scratch, without allocating.
// One reading of the occupancy: the window is full or it is not, and
// both the eviction figure and the pop below read that one answer.
let full = self.heap.len() == self.keep.get();
let evicted_bytes = if full {
let worst = self.heap.peek().expect("a full window");
if !cmp_sort_keys(self.keys, &self.scratch, self.seq, &worst.parked, worst.seq).is_lt()
{
// The offered row departs uncharged, so its key clones must
// depart with it: `clear` keeps the buffer, drops the values.
self.scratch.clear();
return Ok(());
}
worst.bytes
} else {
0
};
// Every charge reads the same figure: the window's steady state once
// this entry is in and the evicted one is out. Checking the
// pre-eviction total here would refuse a row whose steady state fits.
let net = self.charge.bytes() + bytes - evicted_bytes;
// Both admissions settle before the window mutates. A refusal is one
// exit, and it takes the offered row's clones with it like every
// other — the row departs uncharged, so nothing of it may outlive it.
if let Err(error) = graph
.check_expression_allocation(net)
.and_then(|()| self.charge.resize(net))
{
self.scratch.clear();
return Err(error);
}
// Admitted — only now does the window mutate, and the eviction
// happens first so the heap never holds `keep + 1` entries. The
// filled scratch moves into the entry; the evicted entry's own key
// vector drops with it, its bytes already refunded.
if full {
self.heap.pop().expect("the window is over capacity");
}
let parked = std::mem::take(&mut self.scratch);
let entry = TopKEntry {
keys: self.keys,
parked,
seq: self.seq,
row,
bytes,
};
self.seq += 1;
self.heap.push(entry);
Ok(())
}
/// The live window's charged bytes — the entry figure the window's own
/// charge tracks (PROFILE's terminal-retention fact for a window sink).
pub(crate) fn retained_bytes(&self) -> u64 {
self.charge.bytes()
}
/// The held rows, best first — at most `keep`, already in the sort's
/// order, so the finish tail sorts nothing on this shape. The window's
/// charge is released with it; the rows are the caller's.
pub(crate) fn finish(self) -> Vec<Row> {
self.heap
.into_sorted_vec()
.into_iter()
.map(|entry| entry.row)
.collect()
}
}
/// One entry's landing figure: the row's retained payloads and its buffer
/// slots — [`crate::overlay::RetainedTotal::add_row`]'s spelling for the
/// payloads, so both
/// containers price a row's heap identically — plus everything the window
/// holds beside it: the parked key columns the row does not carry, the
/// value buffer they and the row's slots share, and the heap element the
/// entry itself occupies. A window admitted under a large `LIMIT` must not
/// hold materially more than the total it passed.
fn entry_bytes(row: &Row, key_columns: usize) -> u64 {
let values = row.values();
// Each held allocation priced once: the row's landing figure (its
// payload heap and its value slots — the same figure a retained `Vec`
// charges per row), the parked key vector's slots, and the heap element
// the entry occupies — which already carries the row's and the vectors'
// inline headers, so no `size_of::<Row>()` is added beside it.
crate::workspace::row_landing_bytes(values)
+ crate::workspace::value_buffer_bytes(key_columns)
+ std::mem::size_of::<TopKEntry<'_, '_>>() as u64
}