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
//! The per-pattern walk-plan memo (REL8-841): the two setup products of
//! `match_resolved_path` that depend on the entry row's *names*, never on
//! its values — the compiled scan-predicate specialization
//! ([`WalkSpec`]) and the binding-schema ladder ([`WalkLadder`]) — built
//! once per (pattern, entry schema) and reused by every later driving row
//! of the same clause call.
//!
//! This is a plan cache at the pattern grain, not a result memo: it stores
//! no data and no per-row state, so its memory is a handful of small
//! entries whatever the row count, and the walk's store reads are
//! unchanged.
//!
//! **The invariant that makes the key the schema alone:** the memo lives
//! in the pattern's [`PathFilters`](super::one_path), whose life is one
//! `match_clause` call over one input batch — never across a write and
//! never across statements. Within it the graph, the parameters and the
//! residual's conjunct list are fixed (the residual's `scope` varies per
//! row but is a function of the row's names and is not an input of
//! [`WalkSpec::compile`]), so a spec depends on the entry schema only. A
//! caller that handed one memo two different residuals would break this;
//! every caller builds its [`PathFilters`](super::one_path) beside the
//! one residual it walks under.
//!
//! The entry schema's identity is the interned `Arc<RowSchema>` the rows
//! of one stream share ([`SchemaKey`] holds it, so the address cannot be
//! reused while an entry lives). The start selection's *value* inputs (the
//! bound node id, the estimates) stay per row: only the ladder's arm
//! ([`LadderArm`], the one normalization [`WalkLadder::new`] builds from)
//! keys the ladder. Both tables are bounded recency tables
//! ([`RecencySlots`], the repo's one bounded-memo policy).
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::num::NonZeroUsize;
use std::rc::Rc;
use relate_core::Value;
use relate_ir::{Expr, PathPattern};
use crate::overlay::ExecGraph;
use crate::pattern_walk::{LadderArm, Selection, WalkLadder};
use crate::recency::RecencySlots;
use crate::row::{row_bound, Row, SchemaKey};
use super::predicate_spec::WalkSpec;
/// Entries kept per table. A pattern sees one entry schema per input
/// stream and at most a few start arms; a full table evicts its
/// least-recently-used entry.
const CAPACITY: NonZeroUsize = NonZeroUsize::new(8).expect("8 is non-zero");
struct SpecEntry {
schema: SchemaKey,
spec: Rc<WalkSpec>,
}
struct LadderEntry {
schema: SchemaKey,
arm: LadderArm,
ladder: Rc<WalkLadder>,
}
/// One pattern's memo of its walk setup (see the module doc).
pub(crate) struct WalkPlans {
specs: RefCell<RecencySlots<SpecEntry>>,
ladders: RefCell<RecencySlots<LadderEntry>>,
}
impl Default for WalkPlans {
fn default() -> Self {
WalkPlans {
specs: RefCell::new(RecencySlots::new(CAPACITY)),
ladders: RefCell::new(RecencySlots::new(CAPACITY)),
}
}
}
impl WalkPlans {
/// The compiled scan-predicate specialization for `path` under `row`'s
/// entry schema: compiled on the first row of a schema, shared after.
pub(crate) fn spec(
&self,
graph: &ExecGraph<'_>,
path: &PathPattern,
params: &BTreeMap<String, Value>,
conjuncts: &[Expr],
row: &Row,
) -> Rc<WalkSpec> {
let mut specs = self.specs.borrow_mut();
if let Some(hit) = specs.hit(|e| e.schema.is(row)) {
return Rc::clone(&hit.spec);
}
// The entry row's bindings are the compile's one row-dependent
// input, and only through which *names* it owns — the schema.
let spec = Rc::new(WalkSpec::compile(
graph,
path,
params,
conjuncts,
&row_bound(row),
));
specs.insert(SpecEntry {
schema: row.schema_key(),
spec: Rc::clone(&spec),
});
spec
}
/// The binding-schema ladder for the walk `selection` chose, under
/// `row`'s entry schema: built on the first row of a (schema, arm),
/// shared after. The ladder reads the entry row's names only.
pub(crate) fn ladder(
&self,
graph: &ExecGraph<'_>,
path: &PathPattern,
row: &Row,
selection: Option<&Selection>,
) -> Rc<WalkLadder> {
let arm = LadderArm::of(selection);
let mut ladders = self.ladders.borrow_mut();
if let Some(hit) = ladders.hit(|e| e.schema.is(row) && e.arm == arm) {
return Rc::clone(&hit.ladder);
}
let ladder = Rc::new(WalkLadder::new(
&graph.seat_prefixes(),
row,
path,
arm,
path.has_path_name(),
));
ladders.insert(LadderEntry {
schema: row.schema_key(),
arm,
ladder: Rc::clone(&ladder),
});
ladder
}
}