relate-planner 1.5.0

Bind OpenCypher AST to a logical plan
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
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
//! `order` tests: the endpoint ladder's measured levels (equality, the
//! range window, CONTAINS provenance, label rows), the degree scaling's
//! mean/tail policy, and the orientation and part-order decisions they
//! drive.

use relate_ir::{BinaryOp, Clause};
use relate_names::{NamedToken, SmallName};

use super::*;
use crate::plan_with_stats;
use crate::stats::{ContainsCount, NoPatternStats};

mod range;
mod seek;
mod selectivity;

/// One parse and leading-MATCH destructure — the front half every
/// endpoint consumer rides (the closure keeps the query alive for the
/// consumers' borrows).
fn with_leading_match<R>(cypher: &str, f: impl FnOnce(&PathPattern, Option<&Expr>) -> R) -> R {
    let query = relate_front::parse_and_lower(cypher).unwrap();
    let Clause::Match {
        patterns,
        where_clause,
        ..
    } = &query.parts[0].clauses[0]
    else {
        panic!("expected a leading MATCH");
    };
    f(&patterns[0], where_clause.as_ref())
}

/// The endpoint tests' one "nothing is bound" fact — the `Bound`
/// every endpoint estimate is called with.
fn unbound(_: &str) -> bool {
    false
}

/// One endpoint estimate over the parsed pieces — the single
/// `endpoint_estimate` spelling every consumer rides (with the
/// `unbound` fact every endpoint test uses).
fn endpoint_of(
    pattern: &PathPattern,
    where_clause: Option<&Expr>,
    stats: &dyn PatternStats,
) -> Estimate {
    endpoint_estimate(
        &pattern.nodes[0],
        &unbound,
        where_conjuncts_of(where_clause).iter().copied(),
        stats,
    )
}

/// One parse → leading MATCH → endpoint estimate.
fn estimate_of(cypher: &str, stats: &dyn PatternStats) -> Estimate {
    with_leading_match(cypher, |pattern, where_clause| {
        endpoint_of(pattern, where_clause, stats)
    })
}

fn label_stats(labels: &'static [(&'static str, u64)]) -> Stub {
    Stub {
        labels,
        ..Stub::EMPTY
    }
}

fn first_label(path: &PathPattern) -> String {
    path.nodes[0].labels[0].0.to_string()
}

struct Stub {
    labels: &'static [(&'static str, u64)],
    equals: &'static [(&'static str, &'static str, &'static str, u64)],
    ranges: &'static [(&'static str, &'static str, BinaryOp, i64, u64)],
    contains: &'static [(&'static str, &'static str, &'static str, ContainsCount)],
    rels: &'static [(&'static str, u64)],
}

/// One name-keyed table lookup — the idiom every stub's `label_rows`
/// and `rel_type_rows` delegates to (one spelling, never a per-struct
/// copy).
fn rows_of(table: &[(&str, u64)], key: &str) -> Option<u64> {
    table
        .iter()
        .find(|(name, _)| *name == key)
        .map(|(_, rows)| *rows)
}

impl Stub {
    /// Every table empty — the base each literal spreads, so a site
    /// spells only the tables its test reads.
    const EMPTY: Stub = Stub {
        labels: &[],
        equals: &[],
        ranges: &[],
        contains: &[],
        rels: &[],
    };
}

impl PatternStats for Stub {
    fn label_rows(&self, (label, _): &NamedToken) -> Option<u64> {
        label.with_str(|text| rows_of(self.labels, text))
    }

    fn equality_rows(
        &self,
        (label, _): &NamedToken,
        property: &NamedToken,
        value: &Expr,
    ) -> Option<u64> {
        let text = match value {
            Expr::String(text) => text.as_str(),
            _ => return None,
        };
        self.equals
            .iter()
            .find(|(l, p, v, _)| label == *l && property.0 == *p && *v == text)
            .map(|(_, _, _, rows)| *rows)
    }

    fn range_rows(
        &self,
        (label, _): &NamedToken,
        property: &NamedToken,
        op: BinaryOp,
        bound: &Expr,
    ) -> Option<u64> {
        let Expr::Int(bound) = bound else {
            return None;
        };
        self.ranges
            .iter()
            .find(|(l, p, o, b, _)| label == *l && property.0 == *p && *o == op && *b == *bound)
            .map(|(_, _, _, _, rows)| *rows)
    }

    fn contains_rows(
        &self,
        (label, _): &NamedToken,
        property: &NamedToken,
        needle: &Expr,
    ) -> Option<ContainsCount> {
        let text = match needle {
            Expr::String(text) => text.as_str(),
            _ => return None,
        };
        self.contains
            .iter()
            .find(|(l, p, n, _)| label == *l && property.0 == *p && *n == text)
            .map(|(_, _, _, count)| *count)
    }

    fn rel_type_rows(&self, (rel_type, _): &NamedToken) -> Option<u64> {
        rel_type.with_str(|text| rows_of(self.rels, text))
    }
}

fn patterns_of(cypher: &str, stats: &dyn PatternStats) -> Vec<PathPattern> {
    let query = relate_front::parse_and_lower(cypher).unwrap();
    let plan = plan_with_stats(query, &mut |_| false, stats).unwrap();
    let Clause::Match { patterns, .. } = &plan.parts[0][0] else {
        panic!("expected a leading MATCH");
    };
    patterns.clone()
}

/// LABS-426 (the mean-to-tail flip): the tail-carrying stats — one
/// (label, rel-type) tail beside the mean's inputs, so the flip's
/// own arithmetic is pinned without the executor's graphs.
struct TailStub {
    labels: &'static [(&'static str, u64)],
    rels: &'static [(&'static str, u64)],
    tails: &'static [(&'static str, &'static str, u64)],
}

impl PatternStats for TailStub {
    fn label_rows(&self, (label, _): &NamedToken) -> Option<u64> {
        label.with_str(|text| rows_of(self.labels, text))
    }

    fn equality_rows(&self, _: &NamedToken, _: &NamedToken, _: &Expr) -> Option<u64> {
        None
    }

    fn contains_rows(&self, _: &NamedToken, _: &NamedToken, _: &Expr) -> Option<ContainsCount> {
        None
    }

    fn rel_type_rows(&self, (rel_type, _): &NamedToken) -> Option<u64> {
        rel_type.with_str(|text| rows_of(self.rels, text))
    }

    fn degree_tail(&self, (label, _): &NamedToken, (rel_type, _): &NamedToken) -> Option<u64> {
        self.tails
            .iter()
            .find(|(l, t, _)| *l == label && *t == rel_type)
            .map(|(_, _, tail)| *tail)
    }
}

/// The pattern's written first hop — parse only, no planning: the
/// written spelling is what both walk orientations evaluate against
/// (the reversed evaluation walks it from the end side), so the
/// parity pins need the un-reversed, un-flipped form.
fn first_hop(cypher: &str) -> (NodePattern, RelPattern) {
    with_leading_match(cypher, |path, _| {
        (path.nodes[0].clone(), path.rels[0].clone())
    })
}

/// LABS-426: an outgoing hop's fanout rides the (start-label,
/// rel-type) out-degree tail, not the mean — the label's mean (53)
/// hides the one 50k-degree supernode; the tail (32768, the log2
/// bucket top) does not. An incoming or undirected hop keeps the mean
/// (the histogram's key side is the far side), and a multi-label
/// start takes the min tail across its labels (every label's tail
/// bounds the intersection's max degree).
#[test]
fn apply_degree_rides_the_tail_on_an_outgoing_hop() {
    let stats = TailStub {
        labels: &[("P", 1000), ("A", 1000), ("B", 1000)],
        rels: &[("KNOWS", 53000)],
        tails: &[("P", "KNOWS", 32768), ("A", "KNOWS", 16384)],
    };
    // Outgoing, a small-seed seek: rows x tail, clamped by the type
    // total — 1 x 32768 (the mean would price the supernode at 53).
    let (start, rel) = first_hop("MATCH (p:P)-[:KNOWS]->(q:Q) RETURN q");
    assert!(seed_on_rel_start(&rel, false));
    assert_eq!(apply_degree(1, &start, &rel, true, &stats), 32_768);
    // Outgoing, a whole-label scan: the clamp holds the sum at the
    // type total — 53000, the mean's own scale, not 1000 x 32768.
    assert_eq!(apply_degree(1000, &start, &rel, true, &stats), 53_000);
    // Incoming: the mean — the histogram's key side is the far side.
    let (start, rel) = first_hop("MATCH (p:P)<-[:KNOWS]-(q:Q) RETURN q");
    assert!(!seed_on_rel_start(&rel, false));
    assert_eq!(apply_degree(1000, &start, &rel, false, &stats), 53_000);
    // Undirected: no start side in EITHER orientation (round 1,
    // F-1-1: the XOR coincidence answered true for reversed) — the
    // mean in both walks.
    let (start, rel) = first_hop("MATCH (p:P)-[:KNOWS]-(q:Q) RETURN q");
    assert!(!seed_on_rel_start(&rel, false));
    assert!(!seed_on_rel_start(&rel, true));
    assert_eq!(apply_degree(1000, &start, &rel, false, &stats), 53_000);
    assert_eq!(apply_degree(1000, &start, &rel, true, &stats), 53_000);
    // A multi-label start takes the min tail across its labels
    // (a small seed: 1 x 16384, clamped by the 53000 total).
    let (start, rel) = first_hop("MATCH (p:A:B)-[:KNOWS]->(q:Q) RETURN q");
    assert_eq!(apply_degree(1, &start, &rel, true, &stats), 16_384);
    // A label with no histogram entry keeps the mean (B alone: no
    // (B,KNOWS) tail).
    let (start, rel) = first_hop("MATCH (p:B)-[:KNOWS]->(q:Q) RETURN q");
    assert_eq!(apply_degree(1000, &start, &rel, true, &stats), 53_000);
    // The seed on the rel's END side (the reversed walk of a
    // written-outgoing hop): the histogram's key side is the far
    // side, so the mean stays even when the seed's label carries a
    // tail.
    let (start, rel) = first_hop("MATCH (p:P)-[:KNOWS]->(q:Q) RETURN q");
    assert!(!seed_on_rel_start(&rel, true));
    assert_eq!(apply_degree(1000, &start, &rel, false, &stats), 53_000);
    // The reversed walk of a written-INCOMING hop: the seed IS the
    // rel's start side — the tail rides again (a small seed).
    let (start, rel) = first_hop("MATCH (p:P)<-[:KNOWS]-(q:Q) RETURN q");
    assert!(seed_on_rel_start(&rel, true));
    assert_eq!(apply_degree(1, &start, &rel, true, &stats), 32_768);
}

/// The path-multiset hop (`apply_path_degree`): the same fanout policy,
/// without the degree-sum clamp — rows that are paths revisit nodes, so
/// a chained multiplicity (4 paths x tail 4 = 16 on a 12-rel type)
/// outgrows the type total the distinct-seed hop clamps at.
#[test]
fn apply_path_degree_drops_the_degree_sum_clamp() {
    let stats = TailStub {
        labels: &[("P", 3)],
        rels: &[("KNOWS", 12)],
        tails: &[("P", "KNOWS", 4)],
    };
    let (start, rel) = first_hop("MATCH (p:P)-[:KNOWS]->(q:Q) RETURN q");
    assert_eq!(apply_degree(4, &start, &rel, true, &stats), 12);
    assert_eq!(apply_path_degree(4, &start, &rel, true, &stats), 16);
    // The mean arm carries no clamp in either form: 4 x 12 / 3.
    let (start, rel) = first_hop("MATCH (p:P)<-[:KNOWS]-(q:Q) RETURN q");
    assert_eq!(apply_degree(4, &start, &rel, false, &stats), 16);
    assert_eq!(apply_path_degree(4, &start, &rel, false, &stats), 16);
}

/// LABS-991: stats with an any-label tail per rel type (`""` keys the
/// any-type tail) beside the label and rel-type counts.
struct AnyTailStub {
    labels: &'static [(&'static str, u64)],
    rels: &'static [(&'static str, u64)],
    any_tails: &'static [(&'static str, u64)],
}

impl PatternStats for AnyTailStub {
    fn label_rows(&self, (label, _): &NamedToken) -> Option<u64> {
        label.with_str(|text| rows_of(self.labels, text))
    }

    fn equality_rows(&self, _: &NamedToken, _: &NamedToken, _: &Expr) -> Option<u64> {
        None
    }

    fn contains_rows(&self, _: &NamedToken, _: &NamedToken, _: &Expr) -> Option<ContainsCount> {
        None
    }

    fn rel_type_rows(&self, (rel_type, _): &NamedToken) -> Option<u64> {
        rel_type.with_str(|text| rows_of(self.rels, text))
    }

    fn any_degree_tail(&self, rel_type: Option<&SmallName>) -> Option<u64> {
        let key = rel_type.map_or(String::new(), |rel_type| rel_type.to_string());
        rows_of(self.any_tails, &key)
    }
}

/// LABS-991: a hop the walk model cannot price — an untyped rel, or a hop
/// leaving an unlabeled node — rides the any-label tail in the kernel
/// gate's [`apply_hop_bound`], unclamped so it compounds across a chain
/// (an alternation rides the any-type tail); every hop the model CAN price
/// keeps `apply_degree`'s price, and the shapes with no histogram to ride
/// (incoming, undirected, no stats) keep the row count.
#[test]
fn apply_hop_bound_prices_what_the_walk_model_leaves_unpriced() {
    let stats = AnyTailStub {
        labels: &[("P", 1000)],
        rels: &[("KNOWS", 53000)],
        any_tails: &[("KNOWS", 1024), ("", 2048)],
    };
    // Unlabeled seed side, typed: the walk model leaves the rows; the
    // bound is the type's any-label tail, and it compounds hop on hop.
    let (start, rel) = first_hop("MATCH ()-[:KNOWS]->(q) RETURN q");
    assert_eq!(apply_degree(1, &start, &rel, true, &stats), 1);
    assert_eq!(apply_hop_bound(1, &start, &rel, true, &stats), 1024);
    assert_eq!(apply_hop_bound(1024, &start, &rel, true, &stats), 1_048_576);
    // Untyped rel (labeled or not): the any-type tail.
    let (start, rel) = first_hop("MATCH (p:P)-->(q) RETURN q");
    assert_eq!(apply_degree(1, &start, &rel, true, &stats), 1);
    assert_eq!(apply_hop_bound(1, &start, &rel, true, &stats), 2048);
    // Incoming or undirected: no histogram side to ride — unchanged.
    let (start, rel) = first_hop("MATCH ()<-[:KNOWS]-(q) RETURN q");
    assert_eq!(apply_hop_bound(7, &start, &rel, false, &stats), 7);
    let (start, rel) = first_hop("MATCH ()-[:KNOWS]-(q) RETURN q");
    assert_eq!(apply_hop_bound(7, &start, &rel, false, &stats), 7);
    // An alternation rides the any-type tail, which bounds every type.
    let (start, rel) = first_hop("MATCH ()-[:KNOWS|LIKES]->(q) RETURN q");
    assert_eq!(apply_hop_bound(7, &start, &rel, true, &stats), 7 * 2048);
    // A priceable hop (labeled seed, typed, mean arm) keeps apply_degree's
    // price — the bound never replaces a price the model has.
    let (start, rel) = first_hop("MATCH (p:P)<-[:KNOWS]-(q) RETURN q");
    assert_eq!(
        apply_hop_bound(1000, &start, &rel, false, &stats),
        apply_degree(1000, &start, &rel, false, &stats)
    );
    // No histogram (the null stats): the rows pass through, as before.
    let (start, rel) = first_hop("MATCH ()-[:KNOWS]->(q) RETURN q");
    assert_eq!(apply_hop_bound(5, &start, &rel, true, &NoPatternStats), 5);
}

fn starts_with_label(path: &PathPattern, label: &str) -> bool {
    path.nodes
        .first()
        .is_some_and(|node| node.label_names().any(|got| got == label))
}

#[test]
fn no_stats_reorders_q22_and_q27_and_keeps_equal_scores() {
    let stats = NoPatternStats;
    let q22 = patterns_of(
        "MATCH (p:Person)-[:workAt]->(o:Organisation), \
         (c:Comment)-[:replyOfPost]->(post:Post), \
         (c)-[:commentHasCreator]->(p) \
         WHERE o.name = 'Linxair' RETURN p",
        &stats,
    );
    assert!(starts_with_label(&q22[0], "Organisation"));
    assert_eq!(
        q22[1].nodes[0].variable.as_ref().map(|v| v.to_string()),
        Some("p".to_string())
    );
    assert_eq!(
        q22[2].nodes[0].variable.as_ref().map(|v| v.to_string()),
        Some("c".to_string())
    );

    let q27 = patterns_of(
        "MATCH (c:Comment)-[:commentHasCreator]->(p:Person), \
         (p)-[:personIsLocatedIn]->(l:Place), \
         (c)-[:commentHasTag]->(t:Tag) \
         WHERE l.name = 'Toronto' AND t.name = 'Winston_Churchill' \
         RETURN c",
        &stats,
    );
    assert!(starts_with_label(&q27[0], "Place"));
    assert_eq!(
        q27[1].nodes[0].variable.as_ref().map(|v| v.to_string()),
        Some("p".to_string())
    );
    assert_eq!(
        q27[2].nodes[0].variable.as_ref().map(|v| v.to_string()),
        Some("c".to_string())
    );

    let tied = patterns_of(
        "MATCH (a:A)-[:R]->(b:B), (c:C)-[:S]->(d:D) RETURN a",
        &stats,
    );
    assert_eq!(
        tied[0].nodes[0].variable.as_ref().map(|v| v.to_string()),
        Some("a".to_string())
    );
    assert_eq!(
        tied[1].nodes[0].variable.as_ref().map(|v| v.to_string()),
        Some("c".to_string())
    );
    assert_eq!(tied[0].rels[0].direction, relate_ir::Direction::Outgoing);
}

/// The reversal's evaluation-order gate: a pattern whose element
/// predicate references a peer pattern variable keeps its written
/// orientation — the reference is legal in that order, and the strict
/// evaluator rejects it once the walk order is reversed (ADR-0335).
/// Without such a reference the bound-endpoint reversal still applies.
#[test]
fn peer_referenced_predicate_keeps_the_written_orientation() {
    let stats = NoPatternStats;
    let referencing = patterns_of(
        "MATCH (q:Question)<-[:COMMENTED_ON]-(first:Comment), \
         (u2:User)-[:COMMENTED]->(c2:Comment {seen: u2.name})-[:COMMENTED_ON]->(q) \
         RETURN u2.name AS name",
        &stats,
    );
    assert_eq!(
        referencing[1].nodes[0]
            .variable
            .as_ref()
            .map(|v| v.to_string()),
        Some("u2".to_string())
    );
    assert_eq!(
        referencing[1].nodes[1]
            .variable
            .as_ref()
            .map(|v| v.to_string()),
        Some("c2".to_string()),
        "the written order binds u2 before the referencing predicate"
    );

    let plain = patterns_of(
        "MATCH (q:Question)<-[:COMMENTED_ON]-(first:Comment), \
         (u2:User)-[:COMMENTED]->(c2:Comment {seen: 'x'})-[:COMMENTED_ON]->(q) \
         RETURN u2.name AS name",
        &stats,
    );
    assert_eq!(
        plain[1].nodes[0].variable.as_ref().map(|v| v.to_string()),
        Some("q".to_string()),
        "the bound-endpoint reversal still applies without a peer reference"
    );
}

#[test]
fn label_and_index_counts_seed_q19_and_q28() {
    let stats = Stub {
        labels: &[
            ("Comment", 2_000_000),
            ("Tag", 16_000),
            ("Place", 1_460),
            ("Tagclass", 71),
            ("Person", 9_892),
        ],
        equals: &[
            ("Place", "name", "Manila", 1),
            ("Tagclass", "name", "BritishRoyalty", 1),
        ],
        contains: &[(
            "Tag",
            "name",
            "Copernicus",
            ContainsCount::SampleScan {
                rows: 4,
                scan: 16_000,
            },
        )],
        rels: &[
            ("commentHasTag", 2_000_000),
            ("commentIsLocatedIn", 2_000_000),
            ("personIsLocatedIn", 9_892),
            ("hasType", 16_080),
            ("hasInterest", 229_166),
        ],
        ..Stub::EMPTY
    };
    let q19 = patterns_of(
        "MATCH (c:Comment)-[:commentHasTag]->(t:Tag), \
         (c)-[:commentIsLocatedIn]->(l:Place) \
         WHERE t.name CONTAINS 'Copernicus' RETURN l",
        &stats,
    );
    assert!(starts_with_label(&q19[0], "Tag"));
    assert_eq!(
        q19[1].nodes[0].variable.as_ref().map(|v| v.to_string()),
        Some("c".to_string())
    );

    let q28 = patterns_of(
        "MATCH (t:Tag)-[:hasType]->(tc:Tagclass), \
         (p:Person)-[:hasInterest]->(t), \
         (p)-[:personIsLocatedIn]->(l:Place) \
         WHERE tc.name = 'BritishRoyalty' AND l.name = 'Manila' RETURN p",
        &stats,
    );
    assert!(starts_with_label(&q28[0], "Place"));
    assert!(q28[1].rels[0].type_names().any(|ty| ty == "hasInterest"));
    assert!(q28[2].rels[0].type_names().any(|ty| ty == "hasType"));
}

#[test]
fn named_variable_length_stays_written() {
    let stats = label_stats(&[("B", 1)]);
    let paths = patterns_of("MATCH (a)-[r*]->(b:B) RETURN a", &stats);
    assert_eq!(
        paths[0].nodes[0].variable.as_ref().map(|v| v.to_string()),
        Some("a".to_string())
    );
    assert_eq!(paths[0].rels[0].direction, relate_ir::Direction::Outgoing);
}

#[test]
fn unbound_part_reverses_toward_the_rare_label() {
    let stats = label_stats(&[("Hub", 200_000), ("Target", 10)]);
    let patterns = patterns_of("MATCH (h:Hub)-[:R]->(t:Target) RETURN h", &stats);
    assert_eq!(first_label(&patterns[0]), "Target");
    assert_eq!(
        patterns[0].nodes.last().unwrap().labels[0].0.to_string(),
        "Hub"
    );
    assert_eq!(
        patterns[0].rels[0].direction,
        relate_ir::Direction::Incoming
    );
}

// A typed hop scales both directions to the same edge count. The seed
// is still the rarer label: that is the scan ADR-0218 exists to avoid.
#[test]
fn degree_tie_still_seeds_the_rare_label() {
    let stats = Stub {
        labels: &[("Hub", 200_000), ("Target", 10)],
        rels: &[("R", 100)],
        ..Stub::EMPTY
    };
    let patterns = patterns_of("MATCH (h:Hub)-[:R]->(t:Target) RETURN h", &stats);
    assert_eq!(first_label(&patterns[0]), "Target");
}

/// The compound's flip side (ADR-0474, the GBL q8 shape): a sampled
/// CONTAINS seed whose 1,000-row label scan dwarfs its 1-row output
/// ties the rival's degree-scaled walk (both land on the 1,000 rels),
/// and the tie-break's seed comparison prices the sampled seed at its
/// scan — so the 10-row label scan keeps the start the raw selectivity
/// (1 survivor) would have stolen.
#[test]
fn sampled_contains_scan_floor_flips_the_seed_to_the_cheaper_scan() {
    let stats = Stub {
        labels: &[("A", 10), ("B", 1_000)],
        contains: &[(
            "B",
            "x",
            "needle",
            ContainsCount::SampleScan {
                rows: 1,
                scan: 1_000,
            },
        )],
        rels: &[("R", 1_000)],
        ..Stub::EMPTY
    };
    let patterns = patterns_of(
        "MATCH (a:A)<-[:R]-(b:B) WHERE b.x CONTAINS 'needle' RETURN a",
        &stats,
    );
    assert_eq!(first_label(&patterns[0]), "A");
}

/// The compound's keep side (ADR-0474, the GBL q19 shape): the scan
/// floor raises the selective sampled seed from its 10-row scaled
/// output to its 40-row label scan, and that still beats the rival
/// pattern's 200-row degree-scaled walk — the genuinely selective
/// seed keeps the anchor the floor gave it.
#[test]
fn sampled_contains_scan_floor_keeps_a_selective_seed_over_a_pricier_walk() {
    let stats = Stub {
        labels: &[("C", 200), ("B", 40), ("P", 30)],
        contains: &[(
            "B",
            "x",
            "needle",
            ContainsCount::SampleScan { rows: 2, scan: 40 },
        )],
        rels: &[("T", 200), ("L", 200)],
        ..Stub::EMPTY
    };
    let patterns = patterns_of(
        "MATCH (c:C)-[:T]->(b:B), (c)-[:L]->(p:P) \
         WHERE b.x CONTAINS 'needle' RETURN p",
        &stats,
    );
    assert_eq!(first_label(&patterns[0]), "B");
    assert_eq!(
        patterns[1].nodes[0]
            .variable
            .as_ref()
            .map(|v| v.to_string()),
        Some("c".to_string())
    );
}

/// The TEXT-indexed measurement carries no scan floor (ADR-0474): the
/// index seeks, so the exact hit count prices the seed alone and the
/// 1-hit index-served side keeps the start over the 10-row label scan
/// — the compound never prices a scan the index did the work to skip.
#[test]
fn text_indexed_contains_keeps_the_exact_count_without_a_scan_floor() {
    let stats = Stub {
        labels: &[("A", 10), ("B", 1_000)],
        contains: &[("B", "x", "needle", ContainsCount::TextIndex(1))],
        rels: &[("R", 1_000)],
        ..Stub::EMPTY
    };
    let patterns = patterns_of(
        "MATCH (a:A)<-[:R]-(b:B) WHERE b.x CONTAINS 'needle' RETURN a",
        &stats,
    );
    assert_eq!(first_label(&patterns[0]), "B");
}

#[test]
fn selective_written_first_end_stays_first() {
    let stats = label_stats(&[("Hub", 200_000), ("Target", 10)]);
    let patterns = patterns_of("MATCH (t:Target)<-[:R]-(h:Hub) RETURN t", &stats);
    assert_eq!(first_label(&patterns[0]), "Target");
}

#[test]
fn equal_cardinalities_keep_written_order() {
    let stats = label_stats(&[("A", 5), ("B", 5)]);
    let patterns = patterns_of("MATCH (a:A)-[:R]->(b:B) RETURN a", &stats);
    assert_eq!(first_label(&patterns[0]), "A");
}

#[test]
fn no_stats_keeps_written_order() {
    let patterns = patterns_of("MATCH (h:Hub)-[:R]->(t:Target) RETURN h", &NoPatternStats);
    assert_eq!(first_label(&patterns[0]), "Hub");
}

#[test]
fn no_stats_keeps_written_part_order() {
    let patterns = patterns_of(
        "MATCH (h:Hub)-[:R]->(t:Target), (x:Tiny)-[:S]->(y:Tiny) RETURN x",
        &NoPatternStats,
    );
    assert_eq!(first_label(&patterns[0]), "Hub");
    assert_eq!(first_label(&patterns[1]), "Tiny");
}

#[test]
fn bound_node_still_wins_over_cardinality() {
    let stats = label_stats(&[("Hub", 200_000), ("Target", 10)]);
    let query =
        relate_front::parse_and_lower("MATCH (h:Hub) WITH h MATCH (h)-[:R]->(t:Target) RETURN t")
            .unwrap();
    let plan = plan_with_stats(query, &mut |_| false, &stats).unwrap();
    let Clause::Match { patterns, .. } = &plan.parts[0][2] else {
        panic!("expected the second MATCH");
    };
    assert_eq!(
        patterns[0].nodes[0]
            .variable
            .as_ref()
            .map(|v| v.to_string()),
        Some("h".to_string())
    );
}

#[test]
fn cheaper_independent_part_runs_first() {
    let stats = label_stats(&[("Hub", 200_000), ("Target", 10), ("Tiny", 3)]);
    let patterns = patterns_of(
        "MATCH (h:Hub)-[:R]->(t:Target), (x:Tiny)-[:S]->(y:Tiny) RETURN x",
        &stats,
    );
    assert_eq!(first_label(&patterns[0]), "Tiny");
}

#[test]
fn inline_where_on_start_still_folds_and_reverses_to_selective_end() {
    let stats = Stub {
        labels: &[("User", 1_000_000)],
        equals: &[("User", "id", "5867", 1)],
        rels: &[("KNOWS", 10_000_000)],
        ..Stub::EMPTY
    };
    let patterns = patterns_of(
        "MATCH (a:User WHERE a.age > 20)-[:KNOWS]->(b:User) WHERE b.id = '5867' RETURN count(*)",
        &stats,
    );
    let p = &patterns[0];
    // Reversed so `b` (with folded `{id: '5867'}`) is first in both `nodes` and `elements`.
    assert_eq!(
        p.nodes[0].variable.as_ref().map(|v| v.to_string()),
        Some("b".to_string())
    );
    assert!(
        p.nodes[0].properties.is_some(),
        "b.id = '5867' must fold into b.properties"
    );
    match p.elements.first() {
        Some(relate_ir::PatternElement::Node(first_elem)) => {
            assert_eq!(
                first_elem.variable.as_ref().map(|v| v.to_string()),
                Some("b".to_string())
            );
            assert!(
                first_elem.properties.is_some(),
                "elements[0] must mirror folded properties for the GPM walker"
            );
        }
        other => panic!("expected Node as elements[0], got {other:?}"),
    }
}

#[test]
fn inline_where_referencing_peer_keeps_written_order() {
    let stats = Stub {
        labels: &[("User", 1_000_000)],
        equals: &[("User", "id", "5867", 1)],
        rels: &[("KNOWS", 10_000_000)],
        ..Stub::EMPTY
    };
    let patterns = patterns_of(
        "MATCH (a:User)-[:KNOWS]->(b:User WHERE b.age > a.age) WHERE b.id = '5867' RETURN count(*)",
        &stats,
    );
    assert_eq!(
        patterns[0].nodes[0]
            .variable
            .as_ref()
            .map(|v| v.to_string()),
        Some("a".to_string())
    );
}