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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//! The bounded sinks a `MATCH`'s rows fold into as they are emitted
//! (ADR-0555 §3, ADR-0660): the ordered top-k window of a following
//! `ORDER BY … LIMIT`, the pass-through page of a following named
//! `DISTINCT` (first-seen gate, page, then the trailing plain `WHERE` on the
//! answered page), or the count-only sink of a provably empty page. One type
//! over the projection's one landing container policy — the container owns
//! the selection, the seen-set keying, the page and the charge; the sink
//! supplies the sink projection and the charge for a rung's finished
//! container.
//!
//! The admission ([`open_sink`]) reads the clause's fields, shape and
//! bounds once, claims the part's classification slot only on the accepting
//! path, and declines every shape whose bounded form the projection's own
//! path owns.
use std::cell::{Cell, RefCell};
use std::collections::BTreeMap;
use relate_core::{Result, Value};
use relate_ir::Clause;
use relate_names::SmallName;
use crate::ctx::ExecCtx;
use crate::eval::bound_is_row_invariant;
use crate::overlay::ExecGraph;
use crate::row::Row;
use super::classification::{ProjectionClass, ProjectionShape};
use super::match_chain::{Landing, LandingKind};
use super::match_clause::charge_and_fold;
use super::pattern_policies::RowFold;
use super::post_merge::{LandedTail, OutputContainer};
use super::project::RowSource;
use super::scope::ProjectionFields;
use super::sink::{finish_match_row, SinkProjection};
/// A `MATCH`'s emitted rows folded straight into the projection's bounded
/// landing container. `projection` evaluates the following clause's items
/// per row when the walk hands over raw bindings; it is `None` when the
/// caller already projected them (the match chain's last stage).
pub(crate) struct BoundedSink<'a> {
container: RefCell<OutputContainer<'a, 'a>>,
projection: Option<SinkProjection<'a>>,
params: &'a BTreeMap<String, Value>,
offered: Cell<u64>,
/// The tail stages the container ran, carried across the seam.
tail: LandedTail,
}
impl<'a> BoundedSink<'a> {
/// The sink over the admission's own container — the constructor the
/// admission calls, never a kind it re-derives. The tail is the
/// container's own answer ([`OutputContainer::tail`]).
fn open(
container: OutputContainer<'a, 'a>,
projection: Option<SinkProjection<'a>>,
params: &'a BTreeMap<String, Value>,
) -> Self {
let tail = container.tail();
Self {
container: RefCell::new(container),
projection,
params,
offered: Cell::new(0),
tail,
}
}
/// The sink's landing — the container's rows and the tail facts it ran,
/// schemas shared — beside the number of rows the producer offered it.
pub(crate) fn finish(self) -> Landing<'a> {
let mut rows = self.container.into_inner().finish();
Row::share_schemas(&mut rows);
Landing {
offered: self.offered.get(),
consumed: 0,
kind: LandingKind::Rows {
rows,
source: RowSource::SinkLanded(self.tail),
},
}
}
/// The live container's charged bytes (the PROFILE terminal-retention
/// fact, read before the landing leaves the run).
pub(crate) fn retained_bytes(&self) -> u64 {
self.container.borrow().retained_bytes()
}
}
impl RowFold for BoundedSink<'_> {
/// A rung's finished container headed to the sink is retained while it
/// is folded: charge it first, as a stage does, so a kernel-built
/// container cannot ride the pool uncharged and make the sink's bounded
/// state the only figure the cap sees.
fn offer_container(&self, graph: &ExecGraph<'_>, rows: Vec<Row>) -> Result<()> {
charge_and_fold(self, graph, rows)
}
fn is_id_ordered_window(&self, var: &SmallName) -> bool {
self.projection
.as_ref()
.is_some_and(|projection| projection.orders_ascending_by_id_of(var))
&& self.container.borrow().is_window()
}
fn id_order_saturated(&self, var: &SmallName) -> bool {
self.is_id_ordered_window(var) && self.container.borrow().window_is_full()
}
fn offer(&self, graph: &ExecGraph<'_>, row: Row) -> Result<()> {
self.offered.set(self.offered.get() + 1);
// The page's own state says it is satisfied; later rows are
// counted and never projected.
if self.container.borrow().is_full() {
return Ok(());
}
let Some(projection) = &self.projection else {
return self.container.borrow_mut().land(graph, self.params, row);
};
let mut container = self.container.borrow_mut();
// A window keys the candidate first and projects it only when it
// enters: the other 99,9xx candidates of a large scan cost their keys.
if container.is_window() && projection.keys_before_projection(graph, &row) {
return container.land_keyed(
graph,
self.params,
|keys| projection.fill_key_values(graph, &row, self.params, keys),
|keys| projection.row_with_keys(graph, &row, keys, self.params),
);
}
let row = finish_match_row(graph, &row, projection, self.params)?;
container.land(graph, self.params, row)
}
}
/// The count-only sink for a provably empty page (`LIMIT 0`): the answer
/// needs no rows, so it counts what it is offered and stores nothing — no
/// container, no projection, no keying.
pub(crate) struct EmptySink {
offered: Cell<u64>,
}
impl EmptySink {
fn new() -> Self {
Self {
offered: Cell::new(0),
}
}
fn finish(self) -> Landing<'static> {
Landing {
offered: self.offered.get(),
consumed: 0,
kind: LandingKind::Rows {
rows: Vec::new(),
// Nothing ran on zero rows: the type's own untouched tail.
source: RowSource::SinkLanded(LandedTail::untouched()),
},
}
}
}
impl RowFold for EmptySink {
fn offer(&self, _: &ExecGraph<'_>, _: Row) -> Result<()> {
self.offered.set(self.offered.get() + 1);
Ok(())
}
/// The count-only sink still holds whatever a rung hands it while it
/// counts: the canonical charge-then-fold covers it, and `offer`'s own
/// increment counts the rows.
fn offer_container(&self, graph: &ExecGraph<'_>, rows: Vec<Row>) -> Result<()> {
charge_and_fold(self, graph, rows)
}
}
/// The bounded sink the admission chose: a container fold, or the count-only
/// sink of a provably empty page.
#[allow(clippy::large_enum_variant)]
pub(crate) enum Sink<'a> {
Bounded(BoundedSink<'a>),
Empty(EmptySink),
}
impl<'a> Sink<'a> {
pub(crate) fn as_fold(&self) -> &dyn RowFold {
match self {
Sink::Bounded(sink) => sink,
Sink::Empty(sink) => sink,
}
}
pub(crate) fn finish(self) -> Landing<'a> {
match self {
Sink::Bounded(sink) => sink.finish(),
Sink::Empty(sink) => sink.finish(),
}
}
pub(crate) fn retained_bytes(&self) -> u64 {
match self {
Sink::Bounded(sink) => sink.retained_bytes(),
Sink::Empty(_) => 0,
}
}
}
/// The bounded sink for the clause after a `MATCH` (or after a run's last
/// stage), when its shape has one: an `ORDER BY … LIMIT` over the top-k
/// window, a named `DISTINCT` over the pass-through page, or a provably
/// empty page over the count-only sink. `None` keeps the held path — no
/// bounded form, a classification slot the part already claimed, a non-plain
/// shape, or page bounds that may resolve differently at the projection.
pub(crate) fn open_sink<'a>(
ctx: ExecCtx<'a>,
next: &'a Clause,
class_slot: &'a std::cell::OnceCell<ProjectionClass<'a>>,
projection: Option<SinkProjection<'a>>,
) -> Result<Option<Sink<'a>>> {
let ExecCtx { graph, params, .. } = ctx;
if class_slot.get().is_some() {
return Ok(None);
}
let Some(fields) = ProjectionFields::from_clause(next) else {
return Ok(None);
};
let Ok(shape) = ProjectionShape::decide(&fields) else {
return Ok(None);
};
// The sink's landing facts describe a named projection; a `*` shape's
// rows key every visible binding, which the merged pass owns.
if !shape.plain() {
return Ok(None);
}
// The class is built locally and stored only once the admission accepts:
// a declined candidate must leave the part's one classification slot free
// for a later `MATCH`'s fold.
let class = ProjectionClass::new(graph, &fields, shape);
// A non-provable bound (a `rand()` limit) is the held path's: the sink
// and the projection must resolve one page.
if !bounds_are_provable(&fields) {
return Ok(None);
}
let bounds = class.page_bounds(graph, params)?;
// A provably empty page needs no rows: the count-only sink, so no input
// row is projected or keyed for an answer known to be empty.
if bounds.answers_nothing() {
return Ok(Some(Sink::Empty(EmptySink::new())));
}
// The admission decides the container before it claims the part's one
// classification slot — a declined candidate must leave that slot free
// for a later `MATCH`'s fold. It then hands the sink the container it
// chose, so no second reading of the window decision re-checks the
// variant. The page here is the named-`DISTINCT` form alone: the plain
// page's rows are the projection path's.
let landing = match class.window_keep(bounds) {
Some(keep) => SinkLanding::Window(keep),
None if class.streams_distinct() => SinkLanding::Page,
None => return Ok(None),
};
let class = class_slot.get_or_init(|| class);
let container = match landing {
SinkLanding::Window(keep) => OutputContainer::window(class, keep),
SinkLanding::Page => OutputContainer::page(graph, class, bounds)?,
};
Ok(Some(Sink::Bounded(BoundedSink::open(
container, projection, params,
))))
}
/// The container the sink admission chose — the closed vocabulary the
/// decision settles into before the part's classification slot is claimed.
enum SinkLanding {
Window(std::num::NonZeroUsize),
Page,
}
/// Whether the clause's page bounds resolve to one value: a literal or
/// parameter does, a nondeterministic call (`rand()`) does not — the sink
/// resolves them here and the projection resolves them again when it runs.
fn bounds_are_provable(fields: &ProjectionFields<'_>) -> bool {
fields.limit.is_none_or(bound_is_row_invariant)
&& fields.skip.is_none_or(bound_is_row_invariant)
}