-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.rs
More file actions
3574 lines (3382 loc) · 166 KB
/
Copy pathmatrix.rs
File metadata and controls
3574 lines (3382 loc) · 166 KB
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Type-generic test matrix for encrypted scalar domains.
//!
//! Two entry points:
//!
//! - **`scalar_matrix!`** — the recommended wrapper. One invocation per type
//! (~5 lines), with a `caps` capability marker selecting the shape:
//! `caps = [eq, ord]` for an ordered scalar (i32, i64, date, timestamptz,
//! ...) where all four variants are present and the full
//! `=`/`<>`/`<`/`>`/`min`/`max` surface applies; `caps = [eq]` for a
//! hypothetical equality-only scalar (e.g. a future hash-only type) where
//! only storage + `_eq` materialise and the ord operators are blockers — no
//! current type uses this shape. The only other inputs that change per type
//! are the scalar
//! itself, the suite token (used to derive domain + test names), and the EQL
//! type name (the fixture `scripts(...)` ref); pivots are derived from the
//! `ScalarType` impl.
//!
//! - **`scalar_domain_matrix!`** — the lower-level macro the wrapper
//! expands to. Use directly only for types with a non-standard surface
//! that neither `caps` shape covers.
//!
//! Each invocation emits one `#[sqlx::test]` per (category, domain,
//! operator, pivot) tuple. Categories: sanity, correctness, cross-shape,
//! supported-NULL, blocker raises, index engagement, ORDER BY, ORDER BY
//! USING.
//!
//! Per-domain capability and payload metadata live in `Variant` (see
//! `scalar_domains.rs`); the macro derives the runtime `ScalarDomainSpec`
//! from `<$scalar as ScalarType>::PG_TYPE` + `Variant::<X>` so no
//! per-type constants are needed.
// ============================================================================
// EXPLAIN plan inspection — node-type-aware index-engagement assertion.
//
// The index-engagement arms (`*_index_engages_*`, `*_ord_routes_through_ob`)
// previously asserted `plan_text.contains(index_name)` on a *text* EXPLAIN.
// That substring match is too weak in two independent ways:
//
// 1. It cannot distinguish an actual index-scan node from an incidental
// textual mention of the index name (e.g. inside an `Index Cond`, a
// filter expression, or a "Recheck Cond" line) — any line carrying the
// string passes, even if the relation is still read in full.
// 2. It says nothing about *which kind* of node read the relation. A
// Bitmap-recheck that still touches every heap row, or a node that
// merely references the index, looks identical to a clean Index Scan.
//
// `assert_index_scan_uses` parses `EXPLAIN (FORMAT JSON)` and requires a
// genuine index-scan node (`Index Scan` / `Index Only Scan` /
// `Bitmap Index Scan`) whose `Index Name` is the expected index. This is a
// structurally meaningful assertion even with `enable_seqscan = off`.
//
// LOUD CAVEAT — VALIDITY, NOT PREFERENCE. Even after this upgrade, the
// index-engagement arms run against a ~17-row fixture with
// `SET LOCAL enable_seqscan = off`. With the only cheaper alternative
// (seqscan) forcibly disabled, the planner will pick essentially any usable
// index. So these arms prove the index is USABLE / VALID (the operator
// resolves through the functional index and produces a real index-scan node)
// — they do NOT prove the planner would PREFER the index under realistic
// costs. Cost-preference is proven exclusively by `__scalar_matrix_scale_case`
// (the `*_scale_preference_*` tests), which build ~5000 rows and leave
// `enable_seqscan` ON. Those are `#[cfg(feature = "scale")]` and are OFF in
// default PR CI. Do not read a green index-engagement arm as "the planner
// chooses this index" — it only means "the planner *can* use this index".
// ============================================================================
/// Assert that a JSON EXPLAIN plan contains a real index-scan node whose
/// `Index Name` matches `index_name`.
///
/// Recursively walks the plan tree. A node qualifies only if its `Node Type`
/// is one of `Index Scan`, `Index Only Scan`, or `Bitmap Index Scan` AND its
/// `Index Name` equals `index_name`. This is strictly stronger than a
/// substring match on the text plan, which would also accept an index name
/// appearing in an `Index Cond` / `Recheck Cond` / filter expression without
/// any index-scan node actually reading the relation.
///
/// `query` is the bare SQL (no `EXPLAIN` prefix); it is interpolated directly,
/// so it must be a trusted/hardcoded string. `tx` is any sqlx executor.
///
/// Returns `Err` (with the full pretty-printed plan) if no qualifying node is
/// found, so it composes with the `?` operator inside the generated arms.
pub async fn assert_index_scan_uses<'e, E>(
executor: E,
query: &str,
index_name: &str,
context: &str,
) -> anyhow::Result<()>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let sql = format!("EXPLAIN (FORMAT JSON) {query}");
let plan: serde_json::Value = sqlx::query_scalar(&sql)
.fetch_one(executor)
.await
.map_err(|e| anyhow::anyhow!("running `{sql}`: {e}"))?;
let mut index_scan_nodes: Vec<(String, String)> = Vec::new();
collect_index_scan_nodes(&plan, &mut index_scan_nodes);
let matched = index_scan_nodes
.iter()
.any(|(_node_type, name)| name == index_name);
anyhow::ensure!(
matched,
"{context}: expected an index-scan node (Index Scan / Index Only Scan / \
Bitmap Index Scan) referencing index `{index_name}`, but found none. \
Index-scan nodes present: {index_scan_nodes:?}. Full plan:\n{}",
serde_json::to_string_pretty(&plan).unwrap_or_else(|_| plan.to_string()),
);
Ok(())
}
/// Recursively collect `(Node Type, Index Name)` pairs for every index-scan
/// node in a JSON EXPLAIN plan tree. Only the three index-scan node types are
/// collected; other nodes (Seq Scan, Aggregate, Sort, ...) are skipped but
/// their children are still walked.
fn collect_index_scan_nodes(value: &serde_json::Value, found: &mut Vec<(String, String)>) {
match value {
serde_json::Value::Object(map) => {
if let Some(node_type) = map.get("Node Type").and_then(|v| v.as_str()) {
if matches!(
node_type,
"Index Scan" | "Index Only Scan" | "Bitmap Index Scan"
) {
let index_name = map
.get("Index Name")
.and_then(|v| v.as_str())
.unwrap_or("<unnamed>");
found.push((node_type.to_string(), index_name.to_string()));
}
}
for v in map.values() {
collect_index_scan_nodes(v, found);
}
}
serde_json::Value::Array(arr) => {
for item in arr {
collect_index_scan_nodes(item, found);
}
}
_ => {}
}
}
/// Unified convention wrapper for scalar encrypted-domain suites. Replaces the
/// two parallel wrappers (`ordered_numeric_matrix!` + `eq_only_scalar_matrix!`)
/// with one entry point selected by a `caps` capability marker:
///
/// - `caps = [eq, ord]` — the ordered-numeric shape (all four variants;
/// `=`/`<>`/`<`/`<=`/`>`/`>=`; ORDER BY / ORDER BY USING; ORE injectivity;
/// the ordered functional index). Consumers:
/// `int2`/`int4`/`int8`/`date`/`timestamptz`/`numeric`.
/// - `caps = [eq]` — equality-only (storage + `_eq` only; `=`/`<>` meaningful,
/// the four ord operators are deliberate blockers). The empty `ord_domains`
/// make the order-by / ORE arms emit zero tests. No current consumer —
/// `timestamptz` was promoted to the ordered shape once the N-block ORE
/// comparator could order its native 12-block width.
///
/// Both arms take the identical `(suite, scalar, eql_type)` signature, so the
/// invocation shape is the same regardless of capability — only the `caps`
/// marker differs. The emitted test names for an ordered type are byte-identical
/// to the old `ordered_numeric_matrix!`; the eq-only name set is exactly that
/// set minus the `_ord` / `order_by` / `routes_through_ob` lines.
///
/// Pivots — the comparison anchors swept by the correctness / cross-shape
/// arms — are the `OrderedScalar` anchors: `min_pivot()`, `max_pivot()`, and the
/// interior `mid_pivot()`. Integer scalars resolve `min`/`max` to
/// `Self::MIN`/`Self::MAX` and `mid` to the origin `0`; temporal scalars use
/// explicit sentinel dates (`mid` = the epoch); `text` uses a real median
/// fixture for `mid` (its `Default` `""` is degenerate for ORE, #262). The
/// fixture must contain those three plaintext rows, since each pivot's
/// ciphertext is fetched at test time via `fetch_fixture_payload`.
#[macro_export]
macro_rules! scalar_matrix {
(
suite = $suite:ident,
scalar = $scalar:ty,
eql_type = $eql_type:literal,
caps = [eq, ord] $(,)?
) => {
$crate::scalar_domain_matrix! {
suite = $suite,
scalar = $scalar,
eql_type = $eql_type,
// Relative to the suite source file at
// tests/sqlx/tests/encrypted_domain/scalars/<T>.rs; sqlx's
// include_str! resolves it against that file. Every scalar
// suite lives at this depth, so the path is fixed here rather
// than repeated per invocation.
fixture_path = "../../../fixtures",
all_domains = [(storage, Storage), (eq, Eq), (ord, Ord), (ord_ore, OrdOre)],
eq_domains = [(eq, Eq), (ord, Ord), (ord_ore, OrdOre)],
ord_domains = [(ord, Ord), (ord_ore, OrdOre)],
ord_ore_domains = [(ord_ore, OrdOre)],
pivots = [
(min, <$scalar as $crate::scalar_domains::OrderedScalar>::min_pivot()),
(max, <$scalar as $crate::scalar_domains::OrderedScalar>::max_pivot()),
(mid, <$scalar as $crate::scalar_domains::OrderedScalar>::mid_pivot()),
],
eq_ops = [(eq, "="), (neq, "<>")],
ord_ops = [(lt, "<"), (lte, "<="), (gt, ">"), (gte, ">=")],
// The extractor per combo is NOT restated here — it is derived at
// runtime from the combo's ops via `Variant::extractor_for_op`
// (the same `Term::extractor_for_operator` codegen uses). Every op
// in one combo must share a single extractor (one functional index
// serves them all); `combo_extractor` asserts that.
index_combos = [
(eq, Eq, "btree", [(eq, "=")]),
(eq, Eq, "hash", [(eq, "=")]),
(ord, Ord, "btree",
[(eq, "="), (lt, "<"), (lte, "<="), (gt, ">"), (gte, ">=")]),
(ord_ore, OrdOre, "btree",
[(eq, "="), (lt, "<"), (lte, "<="), (gt, ">"), (gte, ">=")]),
],
blocker_combos = [
(storage, Storage, [
(eq, "="), (neq, "<>"),
(lt, "<"), (lte, "<="), (gt, ">"), (gte, ">="),
(contains, "@>"), (contained_by, "<@"),
]),
(eq, Eq, [
(lt, "<"), (lte, "<="), (gt, ">"), (gte, ">="),
(contains, "@>"), (contained_by, "<@"),
]),
(ord, Ord, [(contains, "@>"), (contained_by, "<@")]),
(ord_ore, OrdOre, [(contains, "@>"), (contained_by, "<@")]),
],
// Always-on cost-preference proof (#239 thread 17): the recommended
// converged ordered domain, btree. One curated combo keeps PR CI
// cost bounded. The extractor (`=`-serving) is derived at runtime.
scale_default_combos = [
(ord, Ord, "btree"),
],
// No bloom-match domain on a pure ordered scalar (int/date).
match_domains = [],
}
};
(
suite = $suite:ident,
scalar = $scalar:ty,
eql_type = $eql_type:literal,
caps = [eq, ord, search] $(,)?
) => {
$crate::scalar_domain_matrix! {
suite = $suite,
scalar = $scalar,
eql_type = $eql_type,
// See the `caps = [eq, ord]` arm for the fixed-path rationale.
fixture_path = "../../../fixtures",
// `_search` (combined `[Hm, Ore, Bloom]`) rides the eq + ord arms
// like any ordered domain, plus the bloom-match arms below. `_match`
// is deliberately absent: it supports only `@>`/`<@`, so it cannot
// ride the eq/ord arms, and its behaviour is covered by the sibling
// `encrypted_domain/text/text_match` suite.
all_domains = [(storage, Storage), (eq, Eq), (ord, Ord), (ord_ore, OrdOre), (search, Search)],
eq_domains = [(eq, Eq), (ord, Ord), (ord_ore, OrdOre), (search, Search)],
ord_domains = [(ord, Ord), (ord_ore, OrdOre), (search, Search)],
ord_ore_domains = [(ord_ore, OrdOre)],
pivots = [
(min, <$scalar as $crate::scalar_domains::OrderedScalar>::min_pivot()),
(max, <$scalar as $crate::scalar_domains::OrderedScalar>::max_pivot()),
(mid, <$scalar as $crate::scalar_domains::OrderedScalar>::mid_pivot()),
],
eq_ops = [(eq, "="), (neq, "<>")],
ord_ops = [(lt, "<"), (lte, "<="), (gt, ">"), (gte, ">=")],
// Equality on every text domain routes through `eq_term` (exact hm),
// never ORE, while the ordering ops route through `ord_term`. Because
// a single functional index serves one extractor, the `=` proof is
// SPLIT into its own combo (distinct `_eqidx` dom_name) from the
// ordering ops — they cannot share an index. The extractor itself is
// NOT restated; `combo_extractor` derives it from each combo's ops at
// runtime (and asserts the combo is single-extractor). The `_search`
// domain gets both an ordering combo and an `_eqidx` combo.
index_combos = [
(eq, Eq, "btree", [(eq, "=")]),
(eq, Eq, "hash", [(eq, "=")]),
(ord, Ord, "btree",
[(lt, "<"), (lte, "<="), (gt, ">"), (gte, ">=")]),
(ord_eqidx, Ord, "btree", [(eq, "=")]),
(ord_ore, OrdOre, "btree",
[(lt, "<"), (lte, "<="), (gt, ">"), (gte, ">=")]),
(ord_ore_eqidx, OrdOre, "btree", [(eq, "=")]),
(search, Search, "btree",
[(lt, "<"), (lte, "<="), (gt, ">"), (gte, ">=")]),
(search_eqidx, Search, "btree", [(eq, "=")]),
],
blocker_combos = [
(storage, Storage, [
(eq, "="), (neq, "<>"),
(lt, "<"), (lte, "<="), (gt, ">"), (gte, ">="),
(contains, "@>"), (contained_by, "<@"),
]),
(eq, Eq, [
(lt, "<"), (lte, "<="), (gt, ">"), (gte, ">="),
(contains, "@>"), (contained_by, "<@"),
]),
// Ordered text domains block bloom containment (no Bloom term);
// `_search` is omitted — it SUPPORTS `@>`/`<@`, proven by the
// match arms below (they would raise if `@>` were blocked).
(ord, Ord, [(contains, "@>"), (contained_by, "<@")]),
(ord_ore, OrdOre, [(contains, "@>"), (contained_by, "<@")]),
],
// Selective `=` on a text ordered domain prefers the `eq_term`
// functional index (equality is hm-exact), not ord_term — derived at
// runtime from the `=`-serving extractor.
scale_default_combos = [
(ord, Ord, "btree"),
],
// `_search` carries the Bloom term: prove `@>`/`<@` containment
// behaviour + GIN index engagement through the matrix.
match_domains = [(search, Search)],
}
};
(
suite = $suite:ident,
scalar = $scalar:ty,
eql_type = $eql_type:literal,
caps = [eq] $(,)?
) => {
$crate::scalar_domain_matrix! {
suite = $suite,
scalar = $scalar,
eql_type = $eql_type,
// Fixed path; see the `caps = [eq, ord]` arm for the rationale.
fixture_path = "../../../fixtures",
all_domains = [(storage, Storage), (eq, Eq)],
eq_domains = [(eq, Eq)],
ord_domains = [],
ord_ore_domains = [],
// Pivots derived from the scalar type exactly like the ordered arm
// (`OrderedScalar::min_pivot()`/`max_pivot()`/`mid_pivot()`), so the
// equality correctness / cross-shape arms sweep the same three
// anchors and the eq-only name set stays a clean subset of the
// ordered one.
pivots = [
(min, <$scalar as $crate::scalar_domains::OrderedScalar>::min_pivot()),
(max, <$scalar as $crate::scalar_domains::OrderedScalar>::max_pivot()),
(mid, <$scalar as $crate::scalar_domains::OrderedScalar>::mid_pivot()),
],
eq_ops = [(eq, "="), (neq, "<>")],
ord_ops = [(lt, "<"), (lte, "<="), (gt, ">"), (gte, ">=")],
// Extractor derived at runtime from each combo's ops; see the
// `caps = [eq, ord]` arm.
index_combos = [
(eq, Eq, "btree", [(eq, "=")]),
(eq, Eq, "hash", [(eq, "=")]),
],
blocker_combos = [
(storage, Storage, [
(eq, "="), (neq, "<>"),
(lt, "<"), (lte, "<="), (gt, ">"), (gte, ">="),
(contains, "@>"), (contained_by, "<@"),
]),
(eq, Eq, [
(lt, "<"), (lte, "<="), (gt, ">"), (gte, ">="),
(contains, "@>"), (contained_by, "<@"),
]),
],
// Equality-only scalars have no ordered functional index to prefer.
scale_default_combos = [],
// No bloom-match domain on an equality-only scalar.
match_domains = [],
}
};
(
suite = $suite:ident,
scalar = $scalar:ty,
eql_type = $eql_type:literal,
caps = [storage] $(,)?
) => {
// Storage-only / encryption-only (`bool`): a single term-less
// `eql_v3.<T>` domain, no `_eq`/`_ord`. The value is encrypted at rest
// and decrypted by the proxy, but NOTHING is searchable server-side.
//
// Rather than thread empty `eq_domains`/`ord_domains`/`pivots`/
// `index_combos` through `scalar_domain_matrix!` (whose `+`-arity
// transcribers reject empty lists, and which would require relaxing the
// shared macro the other seven scalar types depend on), this arm invokes
// ONLY the leaf drivers that are meaningful without any comparison
// capability. The comparison/index/order/aggregate categories are
// deliberately NOT emitted — they have no storage-only analogue — so a
// storage-only type needs neither `OrderedScalar` nor comparison pivots.
//
// Emitted categories (all over the single storage domain): sanity,
// blocker-raises (every comparison + containment op raises),
// payload-check (envelope CHECK), path-op blockers, native-absent
// (`~~`/`~~*`), typed-column blockers, count, and fixture-shape.
$crate::__scalar_matrix_sanity! {
suite = $suite, scalar = $scalar,
domains = [(storage, Storage)],
}
$crate::__scalar_matrix_blocker_outer! {
suite = $suite, scalar = $scalar,
// The storage domain carries no term, so every comparison and
// containment operator routes to a blocker. This is the substantive
// proof for a storage-only type.
combos = [
(storage, Storage, [
(eq, "="), (neq, "<>"),
(lt, "<"), (lte, "<="), (gt, ">"), (gte, ">="),
(contains, "@>"), (contained_by, "<@"),
]),
],
}
$crate::__scalar_matrix_payload_check_outer! {
suite = $suite, scalar = $scalar,
domains = [(storage, Storage)],
}
$crate::__scalar_matrix_path_op_outer! {
suite = $suite, scalar = $scalar,
domains = [(storage, Storage)],
}
$crate::__scalar_matrix_native_absent_outer! {
suite = $suite, scalar = $scalar,
domains = [(storage, Storage)],
}
$crate::__scalar_matrix_typed_column_outer! {
suite = $suite, scalar = $scalar,
combos = [
(storage, Storage, [
(eq, "="), (neq, "<>"),
(lt, "<"), (lte, "<="), (gt, ">"), (gte, ">="),
(contains, "@>"), (contained_by, "<@"),
]),
],
}
$crate::__scalar_matrix_count_outer! {
suite = $suite, scalar = $scalar,
script = $eql_type, script_path = "../../../fixtures",
domains = [(storage, Storage)],
}
// Asserts `eql_v3.min/max(storage_domain)` is REJECTED (no aggregate on a
// term-less domain). The case branches at runtime on `supports_ord()`,
// which is false for storage — same coverage the other shapes emit for
// their own storage variant.
$crate::__scalar_matrix_aggregate_typecheck_outer! {
suite = $suite, scalar = $scalar,
domains = [(storage, Storage)],
}
$crate::__scalar_matrix_fixture_shape! {
suite = $suite, scalar = $scalar,
script = $eql_type, script_path = "../../../fixtures",
}
};
}
/// Reduced behaviour matrix for a SteVec **entry** view type (e.g.
/// `JsonbEntryInt4`). Runs only the leaf drivers that are surface-agnostic
/// once routed through the access-path seam: correctness (d,d only),
/// supported_null, order_by(+nulls/+using), count, index_engages, and — once
/// `src/v3/jsonb/aggregates.sql` exists — aggregate(+group_by/+parallel).
/// Containment / blockers / payload_check / path-op / native-absent /
/// planner-metadata stay in the hand-written `v3_jsonb_tests` suite — they have
/// no scalar analogue or assert document-specific surface.
/// `ord_routes_through_ob` and scalar `ore_injectivity` are also excluded: they
/// are scalar-term invariants and are not semantically correct for
/// `ste_vec_entry` (entry equality routes through `eq_term`, not ORE).
///
/// The single `(entry, Ord)` "domain" is variant-independent — `ste_vec_entry`
/// has one domain. Equality reduces through `eql_v3.eq_term`; ordering, index,
/// count-distinct, and aggregates reduce through `eql_v3.ore_cllw` via the
/// `JsonbEntryInt4` extractor overrides.
#[macro_export]
macro_rules! jsonb_entry_matrix {
(
suite = $suite:ident,
scalar = $scalar:ty,
eql_type = $eql_type:literal $(,)?
) => {
$crate::__scalar_matrix_dxop_outer! {
case = __scalar_matrix_correctness_case,
suite = $suite, scalar = $scalar, script = $eql_type, script_path = "../../fixtures",
domains = [(entry, Ord)],
ops_list = [(eq, "="), (neq, "<>"), (lt, "<"), (lte, "<="), (gt, ">"), (gte, ">=")],
pivots_list = [
(min, <$scalar as $crate::scalar_domains::OrderedScalar>::min_pivot()),
(max, <$scalar as $crate::scalar_domains::OrderedScalar>::max_pivot()),
(mid, <$scalar as $crate::scalar_domains::OrderedScalar>::mid_pivot()),
],
}
$crate::__scalar_matrix_dxo_outer! {
case = __scalar_matrix_supported_null_case,
suite = $suite, scalar = $scalar, script = $eql_type, script_path = "../../fixtures",
domains = [(entry, Ord)],
ops_list = [(eq, "="), (neq, "<>"), (lt, "<"), (lte, "<="), (gt, ">"), (gte, ">=")],
}
$crate::__scalar_matrix_order_by_outer! {
suite = $suite, scalar = $scalar, script = $eql_type, script_path = "../../fixtures",
domains = [(entry, Ord)],
}
$crate::__scalar_matrix_order_by_nulls_outer! {
suite = $suite, scalar = $scalar, script = $eql_type, script_path = "../../fixtures",
domains = [(entry, Ord)],
}
$crate::__scalar_matrix_order_by_using_outer! {
suite = $suite, scalar = $scalar, script = $eql_type, script_path = "../../fixtures",
domains = [(entry, Ord)], ops_list = [(lt, "<"), (lte, "<="), (gt, ">"), (gte, ">=")],
}
$crate::__scalar_matrix_count_outer! {
suite = $suite, scalar = $scalar, script = $eql_type, script_path = "../../fixtures",
domains = [(entry, Ord)],
}
// Index engagement is NOT driven by `__scalar_matrix_index_outer!` for
// entries: that shared driver also sweeps a bare-jsonb RHS
// (`value < '<lit>'::jsonb`), which is load-bearing for scalars (they have
// `(domain, jsonb)` cross-type operators) but UNSAFE for entries —
// `ste_vec_entry` has no `(entry, jsonb)` operator, so a bare-jsonb RHS
// flattens to native `jsonb < jsonb` (no ore_cllw, no index) rather than
// the entry operator. The hand-written `jsonb_entry_int4_index_engages`
// test in the suite probes index engagement with the domain-cast RHS only.
// Aggregates: eql_v3.min/max over ste_vec_entry (src/v3/jsonb/aggregates.sql).
// The aggregate leaf cases compare extrema via the ord-extractor seam
// (eql_v3.ore_cllw for entries), so the entry min/max route through the
// `oc` (CLLW ORE) term exactly like the comparison operators.
$crate::__scalar_matrix_aggregate_outer! {
suite = $suite, scalar = $scalar, script = $eql_type, script_path = "../../fixtures",
domains = [(entry, Ord)],
}
$crate::__scalar_matrix_aggregate_group_by_outer! {
suite = $suite, scalar = $scalar, script = $eql_type, script_path = "../../fixtures",
domains = [(entry, Ord)],
}
$crate::__scalar_matrix_aggregate_parallel_outer! {
suite = $suite, scalar = $scalar,
domains = [(entry, Ord)],
}
};
}
/// Low-level entry point. Use `scalar_matrix!` instead unless
/// your type's surface deviates from the standard scalar shapes.
#[macro_export]
macro_rules! scalar_domain_matrix {
(
suite = $suite:ident,
scalar = $scalar:ty,
eql_type = $eql_type:literal,
fixture_path = $fixture_path:literal,
all_domains = [$(($all_name:ident, $all_variant:ident)),+ $(,)?],
eq_domains = [$($eq_dom:tt),+ $(,)?],
ord_domains = [$($ord_dom:tt),* $(,)?],
ord_ore_domains = [$($ord_ore_dom:tt),* $(,)?],
pivots = [$($pivot:tt),+ $(,)?],
eq_ops = [$($eq_op:tt),+ $(,)?],
ord_ops = [$($ord_op:tt),+ $(,)?],
index_combos = [$($index_combo:tt),+ $(,)?],
blocker_combos = [$($blocker_combo:tt),+ $(,)?],
// Curated combo(s) that get an ALWAYS-ON cost-preference test (#239
// thread 17). May be empty (e.g. equality-only scalars have no ordered
// index to prefer).
scale_default_combos = [$($scale_default_combo:tt),* $(,)?],
// Domains carrying the Bloom term (`@>`/`<@` containment). May be empty
// (only `text`'s `_search` declares one). Each gets bloom-match
// correctness + GIN index-engagement arms.
match_domains = [$($match_dom:tt),* $(,)?] $(,)?
) => {
$crate::__scalar_matrix_sanity! {
suite = $suite, scalar = $scalar,
domains = [$(($all_name, $all_variant)),+],
}
$crate::__scalar_matrix_dxop_outer! {
case = __scalar_matrix_correctness_case,
suite = $suite, scalar = $scalar, script = $eql_type, script_path = $fixture_path,
domains = [$($eq_dom),+], ops_list = [$($eq_op),+],
pivots_list = [$($pivot),+],
}
$crate::__scalar_matrix_dxop_outer! {
case = __scalar_matrix_correctness_case,
suite = $suite, scalar = $scalar, script = $eql_type, script_path = $fixture_path,
domains = [$($ord_dom),*], ops_list = [$($ord_op),+],
pivots_list = [$($pivot),+],
}
$crate::__scalar_matrix_dxop_outer! {
case = __scalar_matrix_cross_shape_case,
suite = $suite, scalar = $scalar, script = $eql_type, script_path = $fixture_path,
domains = [$($eq_dom),+], ops_list = [$($eq_op),+],
pivots_list = [$($pivot),+],
}
$crate::__scalar_matrix_dxop_outer! {
case = __scalar_matrix_cross_shape_case,
suite = $suite, scalar = $scalar, script = $eql_type, script_path = $fixture_path,
domains = [$($ord_dom),*], ops_list = [$($ord_op),+],
pivots_list = [$($pivot),+],
}
$crate::__scalar_matrix_dxo_outer! {
case = __scalar_matrix_supported_null_case,
suite = $suite, scalar = $scalar, script = $eql_type, script_path = $fixture_path,
domains = [$($eq_dom),+], ops_list = [$($eq_op),+],
}
$crate::__scalar_matrix_dxo_outer! {
case = __scalar_matrix_supported_null_case,
suite = $suite, scalar = $scalar, script = $eql_type, script_path = $fixture_path,
domains = [$($ord_dom),*], ops_list = [$($ord_op),+],
}
$crate::__scalar_matrix_blocker_outer! {
suite = $suite, scalar = $scalar,
combos = [$($blocker_combo),+],
}
$crate::__scalar_matrix_payload_check_outer! {
suite = $suite, scalar = $scalar,
domains = [$(($all_name, $all_variant)),+],
}
$crate::__scalar_matrix_path_op_outer! {
suite = $suite, scalar = $scalar,
domains = [$(($all_name, $all_variant)),+],
}
$crate::__scalar_matrix_native_absent_outer! {
suite = $suite, scalar = $scalar,
domains = [$(($all_name, $all_variant)),+],
}
$crate::__scalar_matrix_native_jsonb_blocker_outer! {
suite = $suite, scalar = $scalar,
domains = [$(($all_name, $all_variant)),+],
}
$crate::__scalar_matrix_typed_column_outer! {
suite = $suite, scalar = $scalar,
combos = [$($blocker_combo),+],
}
$crate::__scalar_matrix_planner_metadata_outer! {
suite = $suite, scalar = $scalar, group = eq,
domains = [$($eq_dom),+],
ops_list = [$($eq_op),+],
}
$crate::__scalar_matrix_planner_metadata_outer! {
suite = $suite, scalar = $scalar, group = ord,
domains = [$($ord_dom),*],
ops_list = [$($ord_op),+],
}
$crate::__scalar_matrix_index_outer! {
suite = $suite, scalar = $scalar, script = $eql_type, script_path = $fixture_path,
combos = [$($index_combo),+],
}
$crate::__scalar_matrix_scale_outer! {
suite = $suite, scalar = $scalar, script = $eql_type, script_path = $fixture_path,
combos = [$($index_combo),+],
}
$crate::__scalar_matrix_scale_default_outer! {
suite = $suite, scalar = $scalar, script = $eql_type, script_path = $fixture_path,
combos = [$($scale_default_combo),*],
}
$crate::__scalar_matrix_fixture_shape! {
suite = $suite, scalar = $scalar, script = $eql_type, script_path = $fixture_path,
}
$crate::__scalar_matrix_ord_routes_outer! {
suite = $suite, scalar = $scalar, script = $eql_type, script_path = $fixture_path,
domains = [$($ord_dom),*],
}
$crate::__scalar_matrix_match_outer! {
suite = $suite, scalar = $scalar, script = $eql_type, script_path = $fixture_path,
domains = [$($match_dom),*],
}
$crate::__scalar_matrix_ore_injectivity_outer! {
suite = $suite, scalar = $scalar, script = $eql_type, script_path = $fixture_path,
domains = [$($ord_ore_dom),*],
}
$crate::__scalar_matrix_aggregate_outer! {
suite = $suite, scalar = $scalar, script = $eql_type, script_path = $fixture_path,
domains = [$($ord_dom),*],
}
$crate::__scalar_matrix_aggregate_group_by_outer! {
suite = $suite, scalar = $scalar, script = $eql_type, script_path = $fixture_path,
domains = [$($ord_dom),*],
}
$crate::__scalar_matrix_aggregate_parallel_outer! {
suite = $suite, scalar = $scalar,
domains = [$($ord_dom),*],
}
$crate::__scalar_matrix_aggregate_typecheck_outer! {
suite = $suite, scalar = $scalar,
domains = [$(($all_name, $all_variant)),+],
}
$crate::__scalar_matrix_count_outer! {
suite = $suite, scalar = $scalar, script = $eql_type, script_path = $fixture_path,
domains = [$(($all_name, $all_variant)),+],
}
$crate::__scalar_matrix_order_by_outer! {
suite = $suite, scalar = $scalar, script = $eql_type, script_path = $fixture_path,
domains = [$($ord_dom),*],
}
$crate::__scalar_matrix_order_by_nulls_outer! {
suite = $suite, scalar = $scalar, script = $eql_type, script_path = $fixture_path,
domains = [$($ord_dom),*],
}
$crate::__scalar_matrix_order_by_using_outer! {
suite = $suite, scalar = $scalar, script = $eql_type, script_path = $fixture_path,
domains = [$($ord_dom),*], ops_list = [$($ord_op),+],
}
};
}
// ============================================================================
// Helpers: spec construction inside generated test bodies.
// ============================================================================
/// Inside a generated test body, build the runtime `ScalarDomainSpec`
/// from `<$scalar>::PG_TYPE` + `Variant::$variant`. All categories use
/// this — keeps the per-case body short.
#[macro_export]
#[doc(hidden)]
macro_rules! __scalar_matrix_spec {
($scalar:ty, $variant:ident) => {
$crate::scalar_domains::ScalarDomainSpec::new::<$scalar>(
$crate::scalar_domains::Variant::$variant,
)
};
}
// ============================================================================
// Sanity category — one test per domain. Cheap thread-through check that
// the macro expanded and the trait wires up.
// ============================================================================
#[macro_export]
#[doc(hidden)]
macro_rules! __scalar_matrix_sanity {
(
suite = $suite:ident,
scalar = $scalar:ty,
domains = [$(($name:ident, $variant:ident)),+ $(,)?] $(,)?
) => {
$(
$crate::paste::paste! {
#[sqlx::test]
async fn [<matrix_ $suite _ $name _sanity>](_pool: sqlx::PgPool)
-> anyhow::Result<()>
{
let spec = $crate::__scalar_matrix_spec!($scalar, $variant);
assert!(!spec.sql_domain.is_empty());
assert!(<$scalar as $crate::scalar_domains::ScalarType>::fixture_table_name()
.starts_with("fixtures."));
Ok(())
}
}
)+
};
}
// ============================================================================
// Shared cartesian-product drivers. `macro_rules!` cannot cross-product
// independent lists in one repetition (`$($($(…)*)*)*` over flat depth-1
// lists does not compile — every metavariable is bound at depth 1), so one
// recursion level fixes one dimension. These generic drivers do that fan-out
// once and dispatch to a per-category leaf macro named by `case`. The
// dimension lists are independent: this is a product, not a zip.
// ============================================================================
// domain × op × pivot.
#[macro_export]
#[doc(hidden)]
macro_rules! __scalar_matrix_dxop_outer {
(
case = $case:ident,
suite = $suite:ident, scalar = $scalar:ty, script = $script:literal, script_path = $script_path:literal,
domains = [$($domain:tt),* $(,)?],
ops_list = $ops_list:tt, pivots_list = $pivots_list:tt $(,)?
) => {
$(
$crate::__scalar_matrix_dxop_mid! {
case = $case,
suite = $suite, scalar = $scalar, script = $script, script_path = $script_path,
domain = $domain, ops_list = $ops_list, pivots_list = $pivots_list,
}
)*
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! __scalar_matrix_dxop_mid {
(
case = $case:ident,
suite = $suite:ident, scalar = $scalar:ty, script = $script:literal, script_path = $script_path:literal,
domain = ($dom_name:ident, $variant:ident),
ops_list = [$($op:tt),+ $(,)?], pivots_list = $pivots_list:tt $(,)?
) => {
$(
$crate::__scalar_matrix_dxop_inner! {
case = $case,
suite = $suite, scalar = $scalar, script = $script, script_path = $script_path,
dom_name = $dom_name, variant = $variant,
op = $op, pivots_list = $pivots_list,
}
)+
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! __scalar_matrix_dxop_inner {
(
case = $case:ident,
suite = $suite:ident, scalar = $scalar:ty, script = $script:literal, script_path = $script_path:literal,
dom_name = $dom_name:ident, variant = $variant:ident,
op = ($op_name:ident, $op:literal),
pivots_list = [$($pivot:tt),+ $(,)?] $(,)?
) => {
$(
$crate::$case! {
suite = $suite, scalar = $scalar, script = $script, script_path = $script_path,
dom_name = $dom_name, variant = $variant,
op_name = $op_name, op = $op, pivot = $pivot,
}
)+
};
}
// domain × op.
#[macro_export]
#[doc(hidden)]
macro_rules! __scalar_matrix_dxo_outer {
(
case = $case:ident,
suite = $suite:ident, scalar = $scalar:ty, script = $script:literal, script_path = $script_path:literal,
domains = [$($domain:tt),* $(,)?], ops_list = $ops_list:tt $(,)?
) => {
$(
$crate::__scalar_matrix_dxo_inner! {
case = $case,
suite = $suite, scalar = $scalar, script = $script, script_path = $script_path,
domain = $domain, ops_list = $ops_list,
}
)*
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! __scalar_matrix_dxo_inner {
(
case = $case:ident,
suite = $suite:ident, scalar = $scalar:ty, script = $script:literal, script_path = $script_path:literal,
domain = ($dom_name:ident, $variant:ident),
ops_list = [$($op:tt),+ $(,)?] $(,)?
) => {
$(
$crate::$case! {
suite = $suite, scalar = $scalar, script = $script, script_path = $script_path,
dom_name = $dom_name, variant = $variant, op = $op,
}
)+
};
}
// ============================================================================
// Correctness category — leaf for the domain × op × pivot driver: assert the
// row set from `WHERE col op pivot` matches `T::expected_forward(op, pivot)`.
// ============================================================================
#[macro_export]
#[doc(hidden)]
macro_rules! __scalar_matrix_correctness_case {
(
suite = $suite:ident, scalar = $scalar:ty, script = $script:literal, script_path = $script_path:literal,
dom_name = $dom_name:ident, variant = $variant:ident,
op_name = $op_name:ident, op = $op:literal,
pivot = ($pivot_name:ident, $pivot_val:expr) $(,)?
) => {
$crate::paste::paste! {
#[sqlx::test(fixtures(path = $script_path, scripts($script)))]
async fn [<matrix_ $suite _ $dom_name _ $op_name _pivot_ $pivot_name _correctness>](
pool: sqlx::PgPool,
) -> anyhow::Result<()> {
let spec = $crate::__scalar_matrix_spec!($scalar, $variant);
let pivot: $scalar = $pivot_val;
let payload =
$crate::scalar_domains::fetch_fixture_payload::<$scalar>(&pool, pivot.clone()).await?;
let lit = $crate::scalar_domains::sql_string_literal(&payload);
let predicate = format!(
"({col})::{d} {op} {lit}::jsonb::{d}",
col = &spec.column_expr, d = &spec.sql_domain, op = $op,
);
let expected =
<$scalar as $crate::scalar_domains::ScalarType>::expected_forward($op, pivot);
$crate::scalar_domains::assert_scalar_plaintexts::<$scalar>(
&pool, &spec.sql_domain, $op, &predicate, &expected,
)
.await
}
}
};
}
// ============================================================================
// Cross-shape category — leaf for the domain × op × pivot driver: per
// (domain, op, pivot) sweep the three operator argument shapes (d,d), (d,j),
// (j,d) and assert each returns the right row count. The `j_d` shape uses the
// commuted operator's expected set.
// ============================================================================
#[macro_export]
#[doc(hidden)]
macro_rules! __scalar_matrix_cross_shape_case {
(
suite = $suite:ident, scalar = $scalar:ty, script = $script:literal, script_path = $script_path:literal,
dom_name = $dom_name:ident, variant = $variant:ident,
op_name = $op_name:ident, op = $op:literal,
pivot = ($pivot_name:ident, $pivot_val:expr) $(,)?
) => {
$crate::paste::paste! {
#[sqlx::test(fixtures(path = $script_path, scripts($script)))]
async fn [<matrix_ $suite _ $dom_name _ $op_name _pivot_ $pivot_name _cross_shape>](
pool: sqlx::PgPool,
) -> anyhow::Result<()> {
let spec = $crate::__scalar_matrix_spec!($scalar, $variant);
let pivot: $scalar = $pivot_val;
let payload =
$crate::scalar_domains::fetch_fixture_payload::<$scalar>(&pool, pivot.clone()).await?;
let lit = $crate::scalar_domains::sql_string_literal(&payload);
let forward_count =
<$scalar as $crate::scalar_domains::ScalarType>::expected_forward($op, pivot.clone())
.len() as i64;
let commuted_count = <$scalar as $crate::scalar_domains::ScalarType>::expected_forward(
$crate::scalar_domains::commute_op($op), pivot.clone(),
).len() as i64;
let d = &spec.sql_domain;
let col = &spec.column_expr;
let shapes = [
("d_d", format!("({col})::{d} {op} {lit}::jsonb::{d}", op = $op), forward_count),
("d_j", format!("({col})::{d} {op} {lit}::jsonb", op = $op), forward_count),
("j_d", format!("{lit}::jsonb {op} ({col})::{d}", op = $op), commuted_count),
];
let table = <$scalar as $crate::scalar_domains::ScalarType>::fixture_table_name();
for (shape_label, predicate, expected_count) in shapes {
let count_sql = format!("SELECT count(*) FROM {table} WHERE {predicate}");
let count: i64 = sqlx::query_scalar(&count_sql).fetch_one(&pool).await?;
assert_eq!(
count, expected_count,
"domain={} op={} pivot={:?} shape={shape_label} SQL={count_sql} \
expected {expected_count} rows, got {count}",
d, $op, pivot
);
}
Ok(())
}
}
};
}
// ============================================================================
// Supported-NULL category — leaf for the domain × op driver: STRICT wrappers
// must propagate NULL on all three NULL positions (left, right, both). This is
// three-valued logic — a supported op (e.g. `<>`) with a NULL operand must
// yield NULL, not true and not false; easy to get wrong in domain wrappers,
// which is why every (domain, op) pair is swept here. (Subsumes the deleted
// `neq_propagates_null_under_three_valued_logic` int4 hand-test.)
// ============================================================================
#[macro_export]
#[doc(hidden)]
macro_rules! __scalar_matrix_supported_null_case {
(
suite = $suite:ident, scalar = $scalar:ty, script = $script:literal, script_path = $script_path:literal,
dom_name = $dom_name:ident, variant = $variant:ident,
op = ($op_name:ident, $op:literal) $(,)?
) => {
$crate::paste::paste! {
#[sqlx::test]
async fn [<matrix_ $suite _ $dom_name _ $op_name _supported_null>](
pool: sqlx::PgPool,
) -> anyhow::Result<()> {
let spec = $crate::__scalar_matrix_spec!($scalar, $variant);
let payload = spec.placeholder_payload;
let sql = format!(
"SELECT $1::jsonb::{d} {op} $2::jsonb::{d}",
d = &spec.sql_domain, op = $op,
);
$crate::scalar_domains::assert_null(&pool, &sql, &[Some(payload), None]).await?;
$crate::scalar_domains::assert_null(&pool, &sql, &[None, Some(payload)]).await?;
$crate::scalar_domains::assert_null(&pool, &sql, &[None, None]).await?;
Ok(())
}
}
};
}
// ============================================================================
// Blocker category — per blocked (domain, op), sweep 3 arg shapes (all
// must raise) and 3 NULL positions on the (d, d) shape (non-STRICT proof).
// ============================================================================
#[macro_export]
#[doc(hidden)]
macro_rules! __scalar_matrix_blocker_outer {
(
suite = $suite:ident, scalar = $scalar:ty,
combos = [$($combo:tt),+ $(,)?] $(,)?
) => {
$(
$crate::__scalar_matrix_blocker_combo! {
suite = $suite, scalar = $scalar, combo = $combo,
}
)+
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! __scalar_matrix_blocker_combo {
(