ingraph 0.1.0

A declaration-driven indexing framework: records and nodes are written as Rust types, and the storage rows, the dataflow between them and the GraphQL query surface are emitted from the declaration.
Documentation
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! The SQL engine: what a stored row answers a query with, what a dialect has to be able to do, and
//! the statement the three lanes render.
//!
//! # The binding is on the ROW, because a row is what one database IS here
//!
//! A declaration storing a record in two databases emits two row types, and everything a query
//! needs that varies — the column names, the stored types, the table — varies with exactly that.
//! So [`SqlQuery`] is implemented on the row and names the record it stores, which is the same key
//! `ToDatabaseComparison<Row, Db>` already turns: one record, one comparison per database, told
//! apart by naming the row.
//!
//! The DIALECT stays a type parameter on top of that, and it is not decoration. An expansion may
//! not name `sqlx::Sqlite`, because a declaration storing one dialect would then need that
//! dialect's feature to COMPILE; the row's own name is what picks which column names are rendered,
//! and the dialect that brackets them is chosen by the pool a caller passes.
//!
//! # Two traits, because ordering is a capability and not a part of being queryable
//!
//! [`SqlQuery`] is the whole of what `get`, an unordered `fetch` and a `limit` need. [`SqlOrdering`]
//! adds the ordering terms a client may write, the cursor values a row projects into, and the
//! address tail that completes a written order — and a record that has no order-by type to name
//! simply does not implement it, so `.order(…)` is a missing impl rather than a runtime refusal.
//!
//! # What is measured here, and what is measured against an engine
//!
//! The tests beside this module assert on the STATEMENT, because rendering is what this module
//! does. Whether the statement pages correctly is not visible in its text — a keyset that skips a
//! row is valid SQL that answers without an error — so that half is measured against real engines
//! and recorded where it was measured; see [`crate::comparison::sql::SqlKeyset`] for the walk and
//! [`find`](self::find) for this layer's own.

mod ddl;
mod error;
mod find;
mod hydrate;
mod interface;
mod snapshot;
mod write;

use core::fmt::Display;

pub use ddl::{AsyncSchemaDialect, Create, SqlSchemaError, Validate};
// CRATE-INTERNAL, and reached by the one other literal in this crate that claims the reserved
// namespace — the retirement ledger's table prefix. See `ddl::leads_with` for why it is shared
// rather than copied.
pub(crate) use ddl::leads_with;
pub use error::{SqlQueryError, SqlWriteError};
pub use find::{
  Backward, Find, Forward, Limited, Ordered, Paging, Positioned, gathered, get, rows,
};
pub use hydrate::{ELEMENT, EdgeCollection, aggregated, distribute, element_alias};
pub use interface::{
  InterfaceFind, InterfaceOrdered, InterfacePaging, SqlInterface, total_order, union,
};
pub use snapshot::{BeginRead, BeginWrite, ReadSnapshot, WriteTransaction};
pub use write::{
  Affected, EdgeWrite, ShadowWrite, SideWrite, SqlWrite, Upsert, delete, delete_where, insert,
  upsert,
};

use crate::{
  Carrier, ComplexComparison, CursorValue, Identifiable, OrderTerm, Record, RecordAddress,
  Transformer,
  comparison::{
    Backend,
    sql::{BindCeiling, NullOrder, OrderSubjects, QuoteIdent, SqlConjunction, SqlLoweringError},
  },
  query::OrderCapability,
};

/// What a record ⇄ row crossing refuses with — the QUERY layer's
/// [`CrossingError`](crate::query::CrossingError), re-exported here.
///
/// It was declared in this module while SQL was the only engine, and it moved up the moment a
/// second one needed it: a declared conversion refuses the same way whichever database the row came
/// out of. The re-export is what keeps `query::sql::CrossingError` — the path an emitted binding
/// writes, through the shelf — resolving to the same type it always did.
pub use super::CrossingError;

/// The shape an edge table keeps a collection in, re-exported from [`schema`](crate::schema).
///
/// It was declared in this module while a MATERIALIZATION was the only party asking, and it moved to
/// the descriptor layer the moment the DDL needed it: a build with no SQL family still describes a
/// table, and this module is not compiled there. The re-export is what keeps `query::sql::EdgeShape`
/// — the path a collection plan is written through — resolving to the same type it always did.
pub use crate::EdgeShape;

/// Everything a page needs of a SQL dialect, in ONE name.
///
/// # A bundle rather than six bounds at every signature
///
/// Each member is already a per-dialect capability with its own home and its own reason —
/// [`QuoteIdent`] states the bracket, [`NullOrder`] states how the framework's pinned `NULL`
/// position is reached, [`BindCeiling`] states the wire's parameter limit, and [`Backend`] states
/// the accumulator, the refusal and the buffer. What this adds is nothing of its own: the blanket
/// impl below is the whole of it, so a dialect this crate does not ship joins by implementing the
/// PARTS and never this.
///
/// # Three predicates are deliberately NOT in it, and the reason was measured
///
/// A statement is executable only where `i64` binds — `LIMIT` and `OFFSET` take a signed 64-bit
/// integer — where the dialect's arguments cross into `Execute`, and where `&mut` its connection is
/// an [`Executor`](sqlx::Executor). All three are predicates whose SUBJECT is not the dialect, so
/// none of them can be a supertrait, and putting them in this trait's `where` clause is worse than
/// leaving them out: **a `where` predicate on a trait is an obligation at every USE of the bound
/// and is not implied by having it.** Written there, `Db: SqlDialect` obliges a caller to prove all
/// three and then hands none of them back — so every signature pays for all of them whether it
/// spends one or none. Measured, on this crate, before they were moved out.
///
/// So they travel with the signatures that spend them, each restating the ones it uses and no
/// more. It costs the ENGINE and not its callers: a concrete dialect satisfies all three, so an
/// emitted face constructing a builder never names them and a caller's `fetch` discharges them by
/// having a real pool.
pub trait SqlDialect: sqlx::Database
  + QuoteIdent
  + NullOrder
  + BindCeiling
  + ReadSnapshot
  + OrderCapability
  + for<'q> Backend<Builder<'q> = SqlConjunction<'q, Self>, Error = SqlLoweringError, Buffer: Display>
{
}

impl<Db> SqlDialect for Db where
  Db: sqlx::Database
    + QuoteIdent
    + NullOrder
    + BindCeiling
    + ReadSnapshot
    + OrderCapability
    + for<'q> Backend<Builder<'q> = SqlConjunction<'q, Db>, Error = SqlLoweringError, Buffer: Display>
{
}

/// The nesting ceiling, which is the QUERY layer's rather than this family's — a filter that nests
/// too deep for one backend must be refused by both. Re-exported so that
/// `query::sql::DEPTH_CEILING`, which is the path an expansion writes and this family's own note
/// spells, keeps resolving.
pub use super::DEPTH_CEILING;

/// One database's stored row, as the engine queries it.
///
/// # What a query needs, and nothing more
///
/// The table and key columns it selects from, the columns it selects, the predicate type a filter
/// crosses into, the predicate that names ONE row, and the way back to the record. Ordering is not
/// here — see [`SqlOrdering`], which is what a page needs on top.
///
/// # The address is a `const`, so nothing builds one per query
///
/// [`RecordAddress::new`] is `const` and its key list is `&'static`, which is the shape that makes a
/// row's address an ITEM. An emitted binding states it once beside the row it describes.
///
/// # The dialect is an `sqlx::Database` here, and the COLLECTION PLAN is what made it one
///
/// A query needs a backend and nothing more, which is what this bound used to say. What it cannot
/// say is [`COLLECTIONS`](Self::COLLECTIONS): a materialization crosses the dialect's own ROW type
/// at both ends, so the plan's type names `Db::Row` and the trait has to be able to. Every party
/// affected already had it — the engine's lanes are `Db: SqlDialect` and an emitted binding states
/// `Db: sqlx::Database` for its cursor value's carrier — so the tightening costs the callers
/// nothing and is what keeps hydration a defaulted item here rather than a fourth trait a row would
/// have to remember to implement.
///
/// The plan is also a `&'static` list, so a row is `'static` too — which it already was, being the
/// OWNED shape a `FromRow` decode produces rather than a borrow of one. It is stated in the
/// supertrait list rather than as a `where` predicate, so a caller holding `Row: SqlQuery<Db>` is
/// GIVEN it instead of having to prove it.
pub trait SqlQuery<Db: Backend + sqlx::Database>: Sized + 'static {
  /// The record this row stores.
  ///
  /// TWO vocabularies, and the engine reads one projection out of each. [`Identifiable`] — which
  /// [`Transformer`] takes as a supertrait, so this one bound gives both — gives the address, and
  /// `get` takes a `Self::Record::Id`; [`Record<Db>`](Record) gives the predicate
  /// this database's columns take, which is what a client's filter crosses into. Both RIDE the
  /// record's own bindings rather than being restated here, which is the whole of what the
  /// vocabulary is for: a comparison is a fact about a record in a database, and this trait now
  /// reads it instead of declaring a second copy.
  ///
  /// The item bound rides along in the same place: `Comparison: ComplexComparison<Db>` is what a
  /// party rendering one needs, and stating it HERE keeps it implied by `Row: SqlQuery<Db>` — where
  /// a `where` predicate on the trait would be an obligation at every use and imply nothing. That
  /// is the measurement [`SqlDialect`] records, applied one trait over.
  type Record: Transformer + Record<Db, Comparison: ComplexComparison<Db>>;

  /// The predicate naming exactly one row, built from that row's id.
  ///
  /// Its own type rather than the record's own comparison, because a filter and an address are
  /// different questions: a filter says which rows are wanted and an address says which row IS. The
  /// emitted binding answers it with the family's compound-key membership over a set of ONE, which
  /// is the existing machinery rather than a second lowering of equality.
  type Address: ComplexComparison<Db>;

  /// The table this row is stored in, and the key columns identifying one of them.
  const ADDRESS: RecordAddress;

  /// Every name a NESTED scope may call this row's table by, indexed by the depth of the scope
  /// wearing it.
  ///
  /// ```text
  /// ALIASES == ["__ig_scene_0__", "__ig_scene_1__", …, "__ig_scene_7__"]
  /// ```
  ///
  /// # Minted at expansion, because there is nothing here a macro does not know
  ///
  /// An alias is a pure function of the table's name and a depth. Building one while rendering
  /// therefore bought nothing and cost a string per occurrence per statement, on the path this
  /// family is arranged to keep allocation off; the mint is `ingraph-codegen`'s `alias` module and
  /// what arrives here is `&'static str`. A party that needs one INDEXES.
  ///
  /// # `ALIASES[0]` is minted like the rest and is never WORN
  ///
  /// The outermost `FROM` names the real table — a base scope has nothing to be distinguished from
  /// — so depth zero takes no alias. The entry exists so that the array is indexed by depth
  /// directly: a table starting at depth one would need every use site to subtract, which is an
  /// arithmetic step each of them could get wrong and none could be caught at.
  ///
  /// # A SLICE, so the depth is this row's rather than the framework's
  ///
  /// [`DEPTH_CEILING`] is what an expansion mints when the declaration names no number; a row whose
  /// filters nest deeper names one — `<db>(alias(depth = 16))` — and lands here longer.
  /// That is the whole of what the type buys: a fixed-length array would put the count in this
  /// item's TYPE, where it could not vary per row, and every party below already reads
  /// [`len`](slice::len) rather than the constant.
  ///
  /// The names also need not begin `__ig`: the same block's `alias(prefix = "…")` moves the whole
  /// family, for a database whose schema already holds legacy tables in that namespace.
  ///
  /// # Past the last entry a lowering REFUSES
  ///
  /// A relation nesting past the last name answers
  /// [`SqlLoweringError::RelationDepth`](crate::comparison::sql::SqlLoweringError::RelationDepth) —
  /// carrying THIS slice's length, so the message describes the row it is about — rather than
  /// reusing a name. Reuse is the silent case: SQL resolves a reference to the innermost scope that
  /// has a match, so a repeated alias correlates to the wrong row source and the statement still
  /// answers.
  const ALIASES: &'static [&'static str];

  /// Every FIELD this row carries, in the order the decode reads them, and what each one's type
  /// spans.
  ///
  /// The `SELECT` list. It is the ROW's rather than the record's: a partial row selects the columns
  /// it holds, and the decode reads each by name, so the two cannot disagree about which columns a
  /// statement asked for.
  ///
  /// # An entry is a SHAPE, and the list's `len` is FIELDS rather than columns
  ///
  /// A field whose type spans two columns is ONE entry here and two names in the statement, so a
  /// party sizing a statement spends [`width`](crate::width) and a party indexing a row's binds
  /// spends `len`. That is not a trap to remember: they are different questions, and a list of
  /// names could ask only the second one — which is what made the flattened composite unreachable
  /// before this const changed shape. See [`ColumnShape`](crate::ColumnShape).
  ///
  /// The names are joined at RENDER time and appear nowhere as data. Nothing here holds `tb_num`.
  ///
  /// A collection column named in [`COLLECTIONS`](Self::COLLECTIONS) is NOT here — see there.
  const COLUMNS: &'static [crate::ColumnShape];

  /// Every collection column this database keeps in an EDGE TABLE, and how each is materialized.
  ///
  /// # It DEFAULTS to none, and that is what keeps hydration off the critical path of a row
  ///
  /// A record with no collection column, and a record whose collections this database stores in a
  /// column of the row, both state nothing: the empty plan renders no extra `SELECT` field and runs
  /// no extra statement, so the whole of hydration for such a row is one `is_empty` test. That is
  /// also what lets this item arrive on a trait that already has implementors — the alternative,
  /// a second trait every row must implement, would make the collection-free case the one that pays.
  ///
  /// # The columns here are the ones [`COLUMNS`](Self::COLUMNS) does NOT hold
  ///
  /// The two lists partition the row's stored columns rather than overlapping: a gathered column
  /// has no value on the page row to select, and an aggregated one is an expression the engine
  /// renders rather than a name. Naming one in both would ask the page's own table for a column it
  /// does not have.
  const COLLECTIONS: &'static [EdgeCollection<Self, Db>] = &[];

  /// The predicate that names exactly the rows of `ids`.
  ///
  /// # A SET rather than one id, and `get` is the degenerate width
  ///
  /// The predicate is a membership either way — `SqlKeyIn` at width one — so one address and a
  /// batch of them are the same statement with a different `k`. Stating the plural is what gives an
  /// interface's hydration its lane: a page of a union answers with the members' own ids, and each
  /// member's rows are then read in ONE statement rather than one per row. `get` passes a slice of
  /// one and the rendering is unchanged.
  ///
  /// An EMPTY slice is a membership of nothing, which asks for no rows — an operand rather than an
  /// absence, the distinction [`SqlKeyIn::keys`](crate::comparison::sql::SqlKeyIn::keys) already
  /// records.
  ///
  /// # The whole parameter is the ID, and the `*Key` is not narrowed by that
  ///
  /// An address is what names a row, and one column does: every table this engine addresses
  /// declares its id `UNIQUE`. The lineage a record's `*Key` also carries is stored beside the id as
  /// ordinary columns — a filter, an order and a scoped delete all still reach them — so what the
  /// key stopped being is the ADDRESS, not a value.
  ///
  /// [`Identifiable`] is what the projection is spelled through, and it has to be: qualification
  /// selects a trait's OWN items, `Id` is the supertrait's, and `<Self::Record as Transformer>::Id`
  /// is `E0576`.
  ///
  /// # Errors
  ///
  /// Whatever the declaration's own column conversion refuses with: an address holds the record's
  /// domain value and the predicate binds this database's stored one, so a declared `try_from`
  /// converter is on this path and may say no.
  fn addresses(ids: &[<Self::Record as Identifiable>::Id]) -> Result<Self::Address, CrossingError>;

  /// The id of the row THIS is — the address column, read back into the record's domain type.
  ///
  /// # It is what lets a batch be lined up again
  ///
  /// A gather over `k` ids answers in whatever sequence the engine chose, so a caller that asked in
  /// one order gets rows in another. Every other party in this crate solves that by holding the key
  /// it correlated on; a row read by its address has to be able to say which address it was, and
  /// this is that. It is the DUAL of [`addresses`](Self::addresses) — the same column, the same
  /// declared conversion, read the other way — so a declaration that renames or converts its id
  /// moves both at once.
  ///
  /// # Errors
  ///
  /// Whatever the declaration's own column conversion refuses with, the read direction this time.
  fn id(&self) -> Result<<Self::Record as Identifiable>::Id, CrossingError>;

  /// The record this row holds.
  ///
  /// # Errors
  ///
  /// Whatever the declaration's own conversions refuse with — see [`CrossingError`].
  fn record(self) -> Result<Self::Record, CrossingError>;
}

/// A row that can be ORDERED, and therefore paged.
///
/// # It is a second trait, and the split is what makes the gap structural
///
/// A record with no order-by type has nothing to write a term with, so it implements this and
/// nothing here can be called on it — `.order(…)` fails to resolve rather than refusing at runtime.
/// Every other lane stays open to it: `get`, an unordered `fetch`, and a `limit`.
///
/// # Four functions, and one list drives every half of a page
///
/// [`complete`](Self::complete) turns a client's written terms into the total order the page runs
/// in; [`term`](Self::term) renders one of those into the [`OrderTerm`] this database spells;
/// [`subjects`](Self::subjects) says what each of them COMPARES; and [`value`](Self::value) reads
/// the row at that same term. So the `ORDER BY`, the keyset `WHERE`, the SELECT-list projection and
/// the cursor are four readings of ONE list, which is what keeps them from disagreeing about a
/// column, an expression or a direction — the failure that skips or repeats exactly the rows at a
/// page boundary.
pub trait SqlOrdering<Db: Backend + sqlx::Database>: SqlQuery<Db> {
  /// One ordering term a client may write: which column, and which way it runs.
  ///
  /// # `Clone` rather than `Copy`, and a term over a NEIGHBOUR is what took the `Copy` away
  ///
  /// A term used to be a column and a direction, which is two words — so the engine copied one
  /// wherever it needed a value. A term that orders by a to-one target's column is not two words:
  /// it carries the TARGET's whole order-by, which carries its own, and a type that contains itself
  /// is `E0072` unless the nesting goes behind a pointer. So the bound is what an owning pointer
  /// can satisfy, and the engine clones where it used to copy.
  ///
  /// The clone sites are three, and they are the whole ripple: the terms a page renders, the terms
  /// an ordered scan renders, and the value every ROW projects at every term. The first two are
  /// once per statement and the third is once per ROW per term, so a page at
  /// [`MAX_PAGE_SIZE`](crate::query::MAX_PAGE_SIZE) over a two-term order clones **2002 times** —
  /// which is what makes which pointer a nested term nests behind a measured question rather than a
  /// taste.
  ///
  /// | pointer | that page, ×200 | allocations per page | depth 3 |
  /// |---|---|---|---|
  /// | [`Box`](std::boxed::Box) | 7.5 ms | 2002 | 20.8 ms, 6006 allocations |
  /// | [`Arc`](std::sync::Arc) | 2.0 ms | **0** | 2.3 ms, **0** |
  ///
  /// **`Arc`**, and the last column is the reason rather than the first: a `Box` clone is a DEEP
  /// COPY, so its cost is the nesting's depth and it allocates once per level per row, while an
  /// `Arc` clone is one increment whatever the term nests. The framework has no depth ceiling — see
  /// the ruling — so the pointer whose cost grows with depth is the one that would put one there.
  type Order: Clone;

  /// One slot of a cursor over this database's stored values.
  ///
  /// The record's own sum over its orderable columns, which is what makes a heterogeneous order
  /// expressible: a cursor's tuple is one type across its slots, and the set of columns is the
  /// record's, so the record's expansion is what writes the sum.
  type Value: Carrier<Db> + CursorValue;

  /// What each term of a completed order COMPARES — see [`OrderSubjects`].
  ///
  /// [`TermColumns`] is the answer for every order whose terms are all columns, and it is what a row
  /// with no nested ordering term states. It is not a DEFAULT: an associated type has none on
  /// stable, and the full-set law this crate follows would refuse one anyway — a row that says
  /// nothing about its subjects would be a row nobody could ask.
  ///
  /// It exists here rather than being built by the engine because a row is the only party that knows
  /// which of its own terms are not columns: the engine holds [`OrderTerm`]s, which carry a column
  /// name whatever produced them.
  ///
  /// [`OrderSubjects`]: crate::comparison::sql::OrderSubjects
  /// [`TermColumns`]: crate::comparison::sql::TermColumns
  type Subjects: OrderSubjects<Db>;

  /// The written order completed into a TOTAL one: the client's terms, then every column of
  /// [`ADDRESS`](SqlQuery::ADDRESS) none of them named, ascending. The address rather than the whole
  /// key — one column on a framework-emitted row, and whatever a declared layout is addressed by
  /// otherwise — because the columns no two rows share are the only ones the totality argument
  /// spends.
  ///
  /// The tail is not an option a caller may leave out. A keyset cursor is `(sort values…, address)`
  /// because sort values REPEAT, and a cursor that cannot break the tie skips or repeats rows at
  /// the page boundary — so completing the client's own order is what makes the client's page
  /// correct. A term that already names an address column MOVES it rather than repeating it.
  fn complete(written: &[Self::Order]) -> Vec<Self::Order>;

  /// One term as this database renders it: its rename-proof identity, the column this database
  /// spells it, the direction it runs in, and whether that column can hold `NULL`.
  ///
  /// It answers with the WIDE term, whatever the column's type spans. What a statement walks is
  /// [`OrderTerm::levels`] of it — one ordinary term per level for a citizen that states a semantic
  /// sequence, and the term itself for everything else — so this seat states one column's own facts
  /// and the expansion is the framework's, in one place, rather than a thing every emission
  /// remembers.
  fn term(order: Self::Order) -> OrderTerm;

  /// What `order`'s terms compare, in the sequence they run.
  ///
  /// ONE value, built once per query and spent in three positions — the `ORDER BY`, the keyset
  /// `WHERE`, and the SELECT-list projection a term that is an expression rides under. Three
  /// renderings of one subject rather than three parties that agree, which is the same standard
  /// [`SqlOrder`](crate::comparison::sql::SqlOrder) holds one layer down.
  ///
  /// It is handed the COMPLETED order rather than the client's, because that is the list every one
  /// of those three positions walks.
  fn subjects(order: &[Self::Order]) -> Self::Subjects;

  /// The value this row holds at `order`'s `leaf`-th LEVEL, or [`None`] where it holds SQL `NULL`.
  ///
  /// # `leaf` is the LEVEL's own leaf ordinal, and for every ordinary term it is `0`
  ///
  /// A cursor holds one slot per TERM the statement ran, and after
  /// [`OrderTerm::levels`](crate::OrderTerm::levels) a term IS a level — so a citizen that states a
  /// semantic sequence is asked once per level, each time for ONE leaf's value, through its own
  /// [`OrderLeaves`](crate::OrderLeaves) vocabulary. Every other column has one level and one leaf,
  /// so it is asked exactly once with `0` and answers with the field, which is what it always did.
  ///
  /// A DERIVED leaf is RECOMPUTED at this call rather than carried: the row holds the value columns,
  /// the index is a function of them, and asking the value for its leaf is the same computation the
  /// write binds. That is what keeps the boundary the value the statement sorted by — see
  /// [`OrderLeaves::leaf`](crate::OrderLeaves::leaf), whose [`None`] is the same overflow the write
  /// writes `NULL` for.
  ///
  /// # The RAW driver row arrives with it, and most terms never read one
  ///
  /// A term over a COLUMN OF THIS ROW reads `self`: the value is on the decoded row, where the
  /// `SELECT` list put it. Two subjects have no column on this row to read, and both take the value
  /// the page statement projected under `alias` — which is the name the ENGINE wrote and therefore
  /// the name the engine hands over, exactly as it hands an aggregated collection's alias to that
  /// crossing:
  ///
  /// | subject | what the row has | where the value comes from |
  /// |---|---|---|
  /// | a column | the decoded field | `self` |
  /// | an EXPRESSION — a to-one target's column | nothing anywhere | `raw` at `alias` |
  /// | a COMPANION — a `sortable` collection's `__ig_<field>_sort__` | the COLLECTION, hydrated | `raw` at `alias` |
  ///
  /// So the raw row is here for every term whether or not the term reads one, which is the
  /// arrangement [`At`](crate::At) already makes one layer down for a correlated comparison's table.
  ///
  /// # It freezes the BOUNDARY, and that is a ruling rather than an implementation detail
  ///
  /// What the cursor carries is the value this page STOPPED AT, never the id of whom it stopped
  /// beside. A cursor that re-derived a nested sort value through the foreign key would compare
  /// against whatever the target row holds NOW: a rename between two pages silently empties or
  /// duplicates the next one, with no error anywhere. Reading the value off the row the page
  /// returned is what makes a cursor mean *where I stopped*.
  ///
  /// **A COMPANION is the same ruling with the second reading nearer to hand, and it is declined the
  /// same way.** The row ends up holding the collection itself, materialized by the gather, so
  /// [`collection_sort_key`](crate::collection_sort_key) over that list would produce a boundary
  /// with no projection at all. It is a SECOND STATEMENT's answer: the ordering ran over the
  /// companion column of the page statement, and the elements arrive from the edge table afterwards.
  /// Two readings, and a page skips or repeats exactly the rows at the boundary wherever they
  /// disagree. The bytes therefore come off the ordered row under `alias`, so the value the cursor
  /// freezes is the value the `ORDER BY` sorted by.
  ///
  /// # Errors
  ///
  /// Whatever the driver refuses a decode with. A term over a column refuses nothing — it is
  /// reading a field of a row that already decoded — so the `Result` is the expression term's, and
  /// it is on the trait for the reason every fallible seam here is: a middle layer can refuse.
  fn value(
    &self,
    order: Self::Order,
    leaf: usize,
    raw: &Db::Row,
    alias: &str,
  ) -> Result<Option<Self::Value>, sqlx::Error>;
}