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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//! The geometry family's SQL side — the three filters' storage-side twins, and the one lowering
//! shape all of them are.
//!
//! ```text
//!   { x: { gte: 0.5 }, width: { lt: 0.2 } }
//!        │
//!        ▼   one frame per component, the parent's address pushed onto each
//!   "box_x" >= $1 AND "box_width" < $2
//! ```
//!
//! # EVERY conjunct is a COMPONENT's, and that is the whole of this file
//!
//! There is no whole-shape operator anywhere in the family — see
//! [`BoundingBoxFilter`](super::BoundingBoxFilter), where the absence is argued — so a comparison
//! here holds one child per component and renders nothing of its own. It pushes a FRAME per
//! component and lets the child render beneath it, which is exactly what
//! [`SqlTimebaseComparison`](crate::SqlTimebaseComparison) does over `num`/`den` and what
//! [`SqlMediaTimeRangeComparison`](crate::SqlMediaTimeRangeComparison) does over its nested
//! timebase.
//!
//! **The leaf-tuple EQUALITY falls out of that rather than being built.** Four component `eq`s under
//! four frames render `x = ? AND y = ? AND width = ? AND height = ?`, which is what whole-value
//! equality over a composite means — the one operator that decomposes conjunctively. Nothing here
//! decomposes a whole-shape operand into the component slots the way the timebase's crossing does,
//! because there is no whole-shape operand to decompose, so this house has NO crossing error at all:
//! every refusal it can produce is one the component family already names.
//!
//! # WHAT IS NOT HERE, and it is the same list at every arity
//!
//! No row-value `IN`, no negation, no ordering over a whole shape. The first two are absent because
//! their operands are, and the third because four numbers have no order. A caller reaching for a
//! spatial predicate finds none yet and finds the door named at the filter, where the columns that
//! would answer one are already stored.

use core::{fmt, marker::PhantomData};

use crate::{
  At, Backend, ColumnKind, ComplexComparison, CompositeComparison, ToDatabaseComparison,
  TryFromFilter, comparison::sql::SqlConjunction,
};

use super::{
  BoundingBox, BoundingBoxFilter, Point2, Point2Filter, Quad, QuadFilter,
  bbox::{AREA, HEIGHT, WIDTH, X as BOX_X, Y as BOX_Y},
  point::{X as POINT_X, Y as POINT_Y},
  quad::{BOTTOM_LEFT, BOTTOM_RIGHT, TOP_LEFT, TOP_RIGHT},
};

/// [`Point2Filter`](super::Point2Filter)'s storage-side twin — the two components' comparisons, and
/// nothing of its own.
///
/// # TWO parameters, and the first is the COMPONENT TYPE rather than its comparison
///
/// `C` is what each component half IS. `T` is the component's own Rust type, carried for
/// [`SqlTimeRangedComparison`](crate::SqlTimeRangedComparison)'s reason: a composite comparison has
/// to state the COLUMNS it spans, a width is a property of the value rather than of the comparison,
/// and a generic one therefore needs the value's type present for its `SEGMENTS` to be readable at
/// all. It costs a caller one type argument and buys the `flatten` face.
///
/// There is no `Db` parameter, and its absence is a measurement rather than a saving: `Db` occurs in
/// no field here — every component comparison carries its own — so a third parameter would be a
/// `PhantomData` a caller has to spell and nothing else. That is where this kind departs from
/// [`SqlTimeRangedComparison`](crate::SqlTimeRangedComparison), which holds a `Db`-parameterised
/// range half and therefore has one to name.
pub struct SqlPoint2Comparison<T, C> {
  x: C,
  y: C,
  // `fn() -> T` so the component contributes no auto-trait or variance obligation — the shape every
  // reading parameter in this crate's filter family takes.
  component: PhantomData<fn() -> T>,
}

impl<T, C> SqlPoint2Comparison<T, C> {
  /// The empty comparison — no operator set on either component.
  #[inline]
  #[must_use]
  pub fn new() -> Self
  where
    C: Default,
  {
    Self {
      x: C::default(),
      y: C::default(),
      component: PhantomData,
    }
  }

  /// The horizontal component's comparison.
  #[inline]
  pub const fn x(&self) -> &C {
    &self.x
  }

  /// The vertical component's comparison.
  #[inline]
  pub const fn y(&self) -> &C {
    &self.y
  }
}

impl<T, C, Db> ComplexComparison<Db> for SqlPoint2Comparison<T, C>
where
  Db: sqlx::Database + for<'q> Backend<Builder<'q> = SqlConjunction<'q, Db>>,
  C: ComplexComparison<Db>,
{
  /// EXACT, and summed rather than counted: the number of conjuncts is the components', because
  /// this level renders none.
  fn conjuncts(&self) -> usize {
    self.x.conjuncts() + self.y.conjuncts()
  }

  fn apply<'q>(
    &'q self,
    at: At<'_>,
    builder: Db::Builder<'q>,
  ) -> Result<Db::Builder<'q>, Db::Error> {
    let x = at.frame(POINT_X);
    let builder = self.x.apply(x.at(), builder)?;
    let y = at.frame(POINT_Y);
    self.y.apply(y.at(), builder)
  }
}

/// The one place the columns a position spans are stated — [`Point2<T>`](super::Point2)'s own
/// [`ColumnKind`] row, read rather than restated, which is what the `T` parameter is FOR.
impl<T, C, Db> CompositeComparison<Db> for SqlPoint2Comparison<T, C>
where
  Db: sqlx::Database + for<'q> Backend<Builder<'q> = SqlConjunction<'q, Db>>,
  C: ComplexComparison<Db>,
  T: ColumnKind,
{
  const SEGMENTS: &'static [crate::ColumnShape] = <Point2<T> as ColumnKind>::SEGMENTS;
}

// The four are written out rather than derived, for `SqlTimebaseComparison`'s reason read one
// parameter over: a derive would bound `T` — a marker no component implements any of these for —
// where each of these needs the bound on `C` alone.
impl<T, C: Default> Default for SqlPoint2Comparison<T, C> {
  #[inline]
  fn default() -> Self {
    Self::new()
  }
}

impl<T, C: fmt::Debug> fmt::Debug for SqlPoint2Comparison<T, C> {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.debug_struct("SqlPoint2Comparison")
      .field("x", &self.x)
      .field("y", &self.y)
      .finish()
  }
}

impl<T, C: Clone> Clone for SqlPoint2Comparison<T, C> {
  fn clone(&self) -> Self {
    Self {
      x: self.x.clone(),
      y: self.y.clone(),
      component: PhantomData,
    }
  }
}

impl<T, C: PartialEq> PartialEq for SqlPoint2Comparison<T, C> {
  fn eq(&self, other: &Self) -> bool {
    self.x == other.x && self.y == other.y
  }
}

/// **THE ROUTING ROW** — a `Point2<T>` column is reached by a `Point2Filter` over whatever filter
/// the component's own family routes with.
impl<T, CF, Db> ToDatabaseComparison<Point2<T>, Db> for Point2Filter<CF>
where
  Db: sqlx::Database + for<'q> Backend<Builder<'q> = SqlConjunction<'q, Db>>,
  CF: ToDatabaseComparison<T, Db>,
{
  type Comparison = SqlPoint2Comparison<T, CF::Comparison>;
}

/// The crossing, and the only route into a [`SqlPoint2Comparison`].
///
/// The error is the COMPONENT's, unwrapped rather than boxed in a variant of this house's own: this
/// level has no operand of its own to refuse, so every refusal the crossing can produce is one the
/// component family already names, and widening it here would publish a failure mode that does not
/// exist.
impl<T, CF, C> TryFromFilter<Point2Filter<CF>> for SqlPoint2Comparison<T, C>
where
  CF: Default,
  C: TryFromFilter<CF>,
{
  type Error = C::Error;

  fn try_from_filter(filter: Point2Filter<CF>) -> Result<Self, Self::Error> {
    // Destructured rather than field-accessed: a facet added tomorrow is `E0027` here rather than a
    // slot silently left out of the lowering.
    let Point2Filter { x, y } = filter;

    Ok(Self {
      x: C::try_from_filter(x.unwrap_or_default())?,
      y: C::try_from_filter(y.unwrap_or_default())?,
      component: PhantomData,
    })
  }
}

/// [`BoundingBoxFilter`](super::BoundingBoxFilter)'s storage-side twin — four comparisons over the
/// columns a rectangle HOLDS, one over the column it is INDEXED by, and nothing of its own.
///
/// The parameters are [`SqlPoint2Comparison`]'s and carry its argument.
///
/// # THE FIFTH CHILD IS THE DERIVED LEAF'S, and it is the only crossing in this house that is
///
/// `area` is a column like the other four at this level — one frame, one child rendering beneath it
/// — and that sameness is the whole design: `{ area: { gte: 0.05 } }` becomes `"box_area" >= $1`
/// through the SAME component comparison a `width` bound goes through, so the derived leaf needs no
/// operator kind, no widening and no arithmetic anywhere in the lowering. The write already put the
/// product in the column ([`BoundingBox::area`](super::BoundingBox::area) is the single source), and
/// what a filter does is name it.
///
/// It is the LAWFUL side of the derived-leaf discipline. *A derived leaf serves the semantic order
/// only; every structural judgment walks value leaves only* — a `WHERE` clause a client wrote is a
/// semantic predicate, and it is the one place this column is allowed to appear. The structural
/// judgments that must NOT reach it are elsewhere and are kept elsewhere: the row-value equality and
/// membership forms walk [`value_width`](crate::value_width) and bind through
/// [`Carrier::push_value_bind`](crate::Carrier::push_value_bind), and the keyset walks the citizen's
/// four order LEVELS.
pub struct SqlBoundingBoxComparison<T, C> {
  x: C,
  y: C,
  width: C,
  height: C,
  area: C,
  component: PhantomData<fn() -> T>,
}

impl<T, C> SqlBoundingBoxComparison<T, C> {
  /// The empty comparison — no operator set on any component.
  #[inline]
  #[must_use]
  pub fn new() -> Self
  where
    C: Default,
  {
    Self {
      x: C::default(),
      y: C::default(),
      width: C::default(),
      height: C::default(),
      area: C::default(),
      component: PhantomData,
    }
  }

  /// The left edge's comparison.
  #[inline]
  pub const fn x(&self) -> &C {
    &self.x
  }

  /// The top edge's comparison.
  #[inline]
  pub const fn y(&self) -> &C {
    &self.y
  }

  /// The horizontal extent's comparison.
  #[inline]
  pub const fn width(&self) -> &C {
    &self.width
  }

  /// The vertical extent's comparison.
  #[inline]
  pub const fn height(&self) -> &C {
    &self.height
  }

  /// The DERIVED area's comparison — the one child of this composite that names a column no decode
  /// reads.
  #[inline]
  pub const fn area(&self) -> &C {
    &self.area
  }
}

impl<T, C, Db> ComplexComparison<Db> for SqlBoundingBoxComparison<T, C>
where
  Db: sqlx::Database + for<'q> Backend<Builder<'q> = SqlConjunction<'q, Db>>,
  C: ComplexComparison<Db>,
{
  fn conjuncts(&self) -> usize {
    self.x.conjuncts()
      + self.y.conjuncts()
      + self.width.conjuncts()
      + self.height.conjuncts()
      + self.area.conjuncts()
  }

  /// SEGMENT ORDER, which puts the derived column LAST because that is where it is written.
  fn apply<'q>(
    &'q self,
    at: At<'_>,
    builder: Db::Builder<'q>,
  ) -> Result<Db::Builder<'q>, Db::Error> {
    let x = at.frame(BOX_X);
    let builder = self.x.apply(x.at(), builder)?;
    let y = at.frame(BOX_Y);
    let builder = self.y.apply(y.at(), builder)?;
    let width = at.frame(WIDTH);
    let builder = self.width.apply(width.at(), builder)?;
    let height = at.frame(HEIGHT);
    let builder = self.height.apply(height.at(), builder)?;
    let area = at.frame(AREA);
    self.area.apply(area.at(), builder)
  }
}

/// The columns a rectangle spans — [`BoundingBox<T>`](super::BoundingBox)'s own row, read.
impl<T, C, Db> CompositeComparison<Db> for SqlBoundingBoxComparison<T, C>
where
  Db: sqlx::Database + for<'q> Backend<Builder<'q> = SqlConjunction<'q, Db>>,
  C: ComplexComparison<Db>,
  T: ColumnKind,
{
  const SEGMENTS: &'static [crate::ColumnShape] = <BoundingBox<T> as ColumnKind>::SEGMENTS;
}

impl<T, C: Default> Default for SqlBoundingBoxComparison<T, C> {
  #[inline]
  fn default() -> Self {
    Self::new()
  }
}

impl<T, C: fmt::Debug> fmt::Debug for SqlBoundingBoxComparison<T, C> {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.debug_struct("SqlBoundingBoxComparison")
      .field("x", &self.x)
      .field("y", &self.y)
      .field("width", &self.width)
      .field("height", &self.height)
      .field("area", &self.area)
      .finish()
  }
}

impl<T, C: Clone> Clone for SqlBoundingBoxComparison<T, C> {
  fn clone(&self) -> Self {
    Self {
      x: self.x.clone(),
      y: self.y.clone(),
      width: self.width.clone(),
      height: self.height.clone(),
      area: self.area.clone(),
      component: PhantomData,
    }
  }
}

impl<T, C: PartialEq> PartialEq for SqlBoundingBoxComparison<T, C> {
  fn eq(&self, other: &Self) -> bool {
    self.x == other.x
      && self.y == other.y
      && self.width == other.width
      && self.height == other.height
      && self.area == other.area
  }
}

/// **THE ROUTING ROW** for a rectangle column.
impl<T, CF, Db> ToDatabaseComparison<BoundingBox<T>, Db> for BoundingBoxFilter<CF>
where
  Db: sqlx::Database + for<'q> Backend<Builder<'q> = SqlConjunction<'q, Db>>,
  CF: ToDatabaseComparison<T, Db>,
{
  type Comparison = SqlBoundingBoxComparison<T, CF::Comparison>;
}

/// The crossing, on [`SqlPoint2Comparison`]'s terms and for its reason.
impl<T, CF, C> TryFromFilter<BoundingBoxFilter<CF>> for SqlBoundingBoxComparison<T, C>
where
  CF: Default,
  C: TryFromFilter<CF>,
{
  type Error = C::Error;

  fn try_from_filter(filter: BoundingBoxFilter<CF>) -> Result<Self, Self::Error> {
    let BoundingBoxFilter {
      x,
      y,
      width,
      height,
      area,
    } = filter;

    Ok(Self {
      x: C::try_from_filter(x.unwrap_or_default())?,
      y: C::try_from_filter(y.unwrap_or_default())?,
      width: C::try_from_filter(width.unwrap_or_default())?,
      height: C::try_from_filter(height.unwrap_or_default())?,
      area: C::try_from_filter(area.unwrap_or_default())?,
      component: PhantomData,
    })
  }
}

/// [`QuadFilter`](super::QuadFilter)'s storage-side twin — four CORNER comparisons, each a whole
/// [`SqlPoint2Comparison`].
///
/// **The nesting is the recursion**, and it goes both ways: everything a position's comparison can
/// answer is answerable one level up without a line of it being restated, and the frames compose —
/// a corner at `top_left` reaches `top_left_x` and `top_left_y`, and the same comparison applied at
/// the TOP LEVEL reaches `x` and `y`. That is
/// [`SqlMediaTimeRangeComparison`](crate::SqlMediaTimeRangeComparison)'s own arrangement over its
/// nested timebase.
pub struct SqlQuadComparison<T, C> {
  top_left: SqlPoint2Comparison<T, C>,
  top_right: SqlPoint2Comparison<T, C>,
  bottom_right: SqlPoint2Comparison<T, C>,
  bottom_left: SqlPoint2Comparison<T, C>,
}

impl<T, C> SqlQuadComparison<T, C> {
  /// The empty comparison — no operator set on any corner.
  #[inline]
  #[must_use]
  pub fn new() -> Self
  where
    C: Default,
  {
    Self {
      top_left: SqlPoint2Comparison::new(),
      top_right: SqlPoint2Comparison::new(),
      bottom_right: SqlPoint2Comparison::new(),
      bottom_left: SqlPoint2Comparison::new(),
    }
  }

  /// The first corner's comparison, in winding order.
  #[inline]
  pub const fn top_left(&self) -> &SqlPoint2Comparison<T, C> {
    &self.top_left
  }

  /// The second corner's comparison, in winding order.
  #[inline]
  pub const fn top_right(&self) -> &SqlPoint2Comparison<T, C> {
    &self.top_right
  }

  /// The third corner's comparison, in winding order.
  #[inline]
  pub const fn bottom_right(&self) -> &SqlPoint2Comparison<T, C> {
    &self.bottom_right
  }

  /// The fourth corner's comparison, in winding order.
  #[inline]
  pub const fn bottom_left(&self) -> &SqlPoint2Comparison<T, C> {
    &self.bottom_left
  }
}

impl<T, C, Db> ComplexComparison<Db> for SqlQuadComparison<T, C>
where
  Db: sqlx::Database + for<'q> Backend<Builder<'q> = SqlConjunction<'q, Db>>,
  C: ComplexComparison<Db>,
{
  fn conjuncts(&self) -> usize {
    ComplexComparison::<Db>::conjuncts(&self.top_left)
      + ComplexComparison::<Db>::conjuncts(&self.top_right)
      + ComplexComparison::<Db>::conjuncts(&self.bottom_right)
      + ComplexComparison::<Db>::conjuncts(&self.bottom_left)
  }

  /// WINDING ORDER, which is the segment order, which is the order the columns were written in.
  fn apply<'q>(
    &'q self,
    at: At<'_>,
    builder: Db::Builder<'q>,
  ) -> Result<Db::Builder<'q>, Db::Error> {
    let top_left = at.frame(TOP_LEFT);
    let builder = self.top_left.apply(top_left.at(), builder)?;
    let top_right = at.frame(TOP_RIGHT);
    let builder = self.top_right.apply(top_right.at(), builder)?;
    let bottom_right = at.frame(BOTTOM_RIGHT);
    let builder = self.bottom_right.apply(bottom_right.at(), builder)?;
    let bottom_left = at.frame(BOTTOM_LEFT);
    self.bottom_left.apply(bottom_left.at(), builder)
  }
}

/// The columns a quadrilateral spans — [`Quad<T>`](super::Quad)'s own row, read.
impl<T, C, Db> CompositeComparison<Db> for SqlQuadComparison<T, C>
where
  Db: sqlx::Database + for<'q> Backend<Builder<'q> = SqlConjunction<'q, Db>>,
  C: ComplexComparison<Db>,
  T: ColumnKind,
{
  const SEGMENTS: &'static [crate::ColumnShape] = <Quad<T> as ColumnKind>::SEGMENTS;
}

impl<T, C: Default> Default for SqlQuadComparison<T, C> {
  #[inline]
  fn default() -> Self {
    Self::new()
  }
}

impl<T, C: fmt::Debug> fmt::Debug for SqlQuadComparison<T, C> {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.debug_struct("SqlQuadComparison")
      .field("top_left", &self.top_left)
      .field("top_right", &self.top_right)
      .field("bottom_right", &self.bottom_right)
      .field("bottom_left", &self.bottom_left)
      .finish()
  }
}

impl<T, C: Clone> Clone for SqlQuadComparison<T, C> {
  fn clone(&self) -> Self {
    Self {
      top_left: self.top_left.clone(),
      top_right: self.top_right.clone(),
      bottom_right: self.bottom_right.clone(),
      bottom_left: self.bottom_left.clone(),
    }
  }
}

impl<T, C: PartialEq> PartialEq for SqlQuadComparison<T, C> {
  fn eq(&self, other: &Self) -> bool {
    self.top_left == other.top_left
      && self.top_right == other.top_right
      && self.bottom_right == other.bottom_right
      && self.bottom_left == other.bottom_left
  }
}

/// **THE ROUTING ROW** for a quadrilateral column.
impl<T, CF, Db> ToDatabaseComparison<Quad<T>, Db> for QuadFilter<CF>
where
  Db: sqlx::Database + for<'q> Backend<Builder<'q> = SqlConjunction<'q, Db>>,
  CF: ToDatabaseComparison<T, Db>,
{
  type Comparison = SqlQuadComparison<T, CF::Comparison>;
}

/// The crossing, delegating to the corner's own — which delegates to the component's.
impl<T, CF, C> TryFromFilter<QuadFilter<CF>> for SqlQuadComparison<T, C>
where
  CF: Default,
  C: TryFromFilter<CF>,
{
  type Error = C::Error;

  fn try_from_filter(filter: QuadFilter<CF>) -> Result<Self, Self::Error> {
    let QuadFilter {
      top_left,
      top_right,
      bottom_right,
      bottom_left,
    } = filter;

    let corner = |facet: Option<Point2Filter<CF>>| {
      SqlPoint2Comparison::try_from_filter(facet.unwrap_or_default())
    };

    Ok(Self {
      top_left: corner(top_left)?,
      top_right: corner(top_right)?,
      bottom_right: corner(bottom_right)?,
      bottom_left: corner(bottom_left)?,
    })
  }
}