relate-executor 1.0.1

In-memory graph and Cypher physical executor
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
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
use super::super::function;
use super::string::{parse_decimal_integer, DecimalInteger};
use super::*;
use crate::graph::Graph;
use crate::row::Row;
use std::collections::BTreeMap;

use relate_ir::Expr;
use relate_names::SmallName;

#[test]
fn to_boolean_or_null_and_to_string_or_null_match_neo4j() {
    let g = Graph::default();
    let graph = ExecGraph::read(&g);
    let row = Row::new();
    let params = BTreeMap::new();
    let call =
        |name: &str, arg: Expr| function(&graph, &row, &params, name, false, &[arg], None).unwrap();

    // success passthrough
    assert_eq!(
        call("toBooleanOrNull", Expr::String("true".into())),
        Value::Bool(true)
    );
    assert_eq!(
        call("toBooleanOrNull", Expr::Bool(false)),
        Value::Bool(false)
    );
    // INTEGER: zero is false, every other integer true (`toBoolean(0)`
    // never raises).
    assert_eq!(call("toBoolean", Expr::Int(0)), Value::Bool(false));
    assert_eq!(call("toBoolean", Expr::Int(1)), Value::Bool(true));
    assert_eq!(call("toBoolean", Expr::Int(-7)), Value::Bool(true));
    // parse failure and null stay null
    assert_eq!(
        call("toBooleanOrNull", Expr::String("nope".into())),
        Value::Null
    );
    assert_eq!(call("toBooleanOrNull", Expr::Null), Value::Null);
    assert_eq!(
        call("toStringOrNull", Expr::Int(7)),
        Value::String("7".into())
    );
    assert_eq!(call("toStringOrNull", Expr::Null), Value::Null);
    // The dispatcher carries no static type gate: CIP-271's homogeneous
    // `LIST<T>` rejection lives in the front's validation layer, so at the
    // value level every list renders in display form — for `toString` and
    // `toStringOrNull` alike.
    let homogeneous = Expr::List(vec![Expr::Int(1), Expr::Int(2)]);
    assert_eq!(
        call("toString", homogeneous.clone()),
        Value::String("[1, 2]".into())
    );
    assert_eq!(
        call("toStringOrNull", homogeneous),
        Value::String("[1, 2]".into())
    );
    // A heterogeneous literal, or one with a null, is `LIST<ANY>`: it
    // renders.
    let mixed = Expr::List(vec![Expr::Int(1), Expr::String("a".into())]);
    assert_eq!(
        call("toString", mixed.clone()),
        Value::String("[1, a]".into())
    );
    assert_eq!(
        call(
            "toString",
            Expr::List(vec![Expr::Int(1), Expr::Null, Expr::Int(2)])
        ),
        Value::String("[1, null, 2]".into())
    );
}

/// The dispatcher converts by value: a list read from a variable renders,
/// whatever its element types. CIP-271's static `LIST<T>` rejection is the
/// front's job (it sees the argument expression); the executor does not.
#[test]
fn tostring_of_a_list_read_from_a_variable_renders() {
    let g = Graph::default();
    let graph = ExecGraph::read(&g);
    let params = BTreeMap::new();
    let mut row = Row::new();
    row.insert(
        "xs".into(),
        Value::List(vec![Value::Int(1), Value::Int(2)].into()),
    );
    assert_eq!(
        function(
            &graph,
            &row,
            &params,
            "toString",
            false,
            &[Expr::Variable("xs".into())],
            None,
        )
        .unwrap(),
        Value::String("[1, 2]".into())
    );
}

// CIP-271 is Cypher-25-only: under an explicit `CYPHER 5` preamble the
// pre-CIP-271 contract holds — `toString()` raises on containers and graph
// values, and `toStringOrNull()` returns null for them.
#[test]
fn tostring_container_semantics_follow_the_cypher5_dialect() {
    let g = Graph::default();
    let graph = ExecGraph::read(&g);
    let row = Row::new();
    let params = BTreeMap::new();
    let call = |name: &str, arg: Expr, version: Option<u32>| {
        function(&graph, &row, &params, name, false, &[arg], version)
    };
    let mixed = Expr::List(vec![Expr::Int(1), Expr::String("a".into())]);
    let homogeneous = Expr::List(vec![Expr::Int(1), Expr::Int(2)]);
    let map = Expr::Map(vec![("a".into(), Expr::Int(1))]);

    // Cypher 25 (the default): every container renders in display form —
    // CIP-271's homogeneous-list rejection is a front-end static check, not
    // a value-level one.
    assert_eq!(
        call("toString", mixed.clone(), None).unwrap(),
        Value::String("[1, a]".into())
    );
    assert_eq!(
        call("toStringOrNull", map.clone(), None).unwrap(),
        Value::String("{a: 1}".into())
    );
    assert_eq!(
        call("toString", homogeneous.clone(), None).unwrap(),
        Value::String("[1, 2]".into())
    );
    assert_eq!(
        call("toStringOrNull", homogeneous, None).unwrap(),
        Value::String("[1, 2]".into())
    );

    // CYPHER 5: every container is non-stringifiable for both spellings.
    assert!(call("toString", mixed.clone(), Some(5)).is_err());
    assert!(call("toString", map.clone(), Some(5)).is_err());
    assert_eq!(
        call("toStringOrNull", mixed.clone(), Some(5)).unwrap(),
        Value::Null
    );
    assert_eq!(call("toStringOrNull", map, Some(5)).unwrap(), Value::Null);

    // Scalars are unaffected in either dialect.
    assert_eq!(
        call("toString", Expr::Int(7), Some(5)).unwrap(),
        Value::String("7".into())
    );
}

#[test]
fn to_integer_or_null_and_to_float_or_null_match_neo4j() {
    let g = Graph::default();
    let graph = ExecGraph::read(&g);
    let row = Row::new();
    let params = BTreeMap::new();
    let call =
        |name: &str, arg: Expr| function(&graph, &row, &params, name, false, &[arg], None).unwrap();

    // Success cases behave exactly like toInteger()/toFloat().
    assert_eq!(
        call("toIntegerOrNull", Expr::String("42".into())),
        Value::Int(42)
    );
    assert_eq!(
        call("toIntegerOrNull", Expr::String("3.5".into())),
        Value::Int(3)
    );
    assert_eq!(call("toIntegerOrNull", Expr::Int(7)), Value::Int(7));
    assert_eq!(call("toIntegerOrNull", Expr::Float(2.9)), Value::Int(2));
    assert_eq!(call("toIntegerOrNull", Expr::Null), Value::Null);

    assert_eq!(
        call("toFloatOrNull", Expr::String("3.5".into())),
        Value::Float(3.5)
    );
    assert_eq!(
        call("toFloatOrNull", Expr::String("42".into())),
        Value::Float(42.0)
    );
    assert_eq!(call("toFloatOrNull", Expr::Int(3)), Value::Float(3.0));
    assert_eq!(call("toFloatOrNull", Expr::Float(1.25)), Value::Float(1.25));
    assert_eq!(call("toFloatOrNull", Expr::Null), Value::Null);

    // Parse failures and non-convertible inputs yield null, never an error.
    assert_eq!(
        call("toIntegerOrNull", Expr::String("abc".into())),
        Value::Null
    );
    assert_eq!(
        call("toFloatOrNull", Expr::String("nope".into())),
        Value::Null
    );
    // Neo4j converts booleans to 1/0 for toInteger (live 2026.10.1);
    // toFloat has no boolean arm.
    assert_eq!(call("toIntegerOrNull", Expr::Bool(true)), Value::Int(1));
    assert_eq!(call("toFloatOrNull", Expr::Bool(true)), Value::Null);
    assert_eq!(
        call("toIntegerOrNull", Expr::List(vec![Expr::Int(1)])),
        Value::Null
    );
    assert_eq!(
        call("toFloatOrNull", Expr::List(vec![Expr::Int(1)])),
        Value::Null
    );

    // The base functions keep the same arms: toInteger converts a boolean
    // (live 2026.10.1: toInteger(true) is 1), toFloat has no boolean arm.
    assert_eq!(
        function(
            &graph,
            &row,
            &params,
            "toInteger",
            false,
            &[Expr::Bool(true)],
            None
        )
        .unwrap(),
        Value::Int(1)
    );
    assert!(function(
        &graph,
        &row,
        &params,
        "toFloat",
        false,
        &[Expr::Bool(true)],
        None
    )
    .is_err());
}

/// The expected outcome of one conversion probe.
enum Want {
    Is(Value),
    OutOfRange,
}

const NULL: Want = Want::Is(Value::Null);
const OUT: Want = Want::OutOfRange;
const OUT_OF_RANGE: &str = "type error: InvalidArgumentValue: toInteger()";

fn int(i: i64) -> Want {
    Want::Is(Value::Int(i))
}

fn float(f: f64) -> Want {
    Want::Is(Value::Float(f))
}

fn ints(xs: &[i64]) -> Want {
    Want::Is(Value::List(
        xs.iter()
            .copied()
            .map(Value::Int)
            .collect::<Vec<_>>()
            .into(),
    ))
}

fn list(xs: Vec<Value>) -> Want {
    Want::Is(Value::List(xs.into()))
}

/// Exact equality, except that NaN matches NaN.
fn same(a: &Value, b: &Value) -> bool {
    match (a, b) {
        (Value::Float(x), Value::Float(y)) => x == y || (x.is_nan() && y.is_nan()),
        (Value::List(xs), Value::List(ys)) => {
            xs.len() == ys.len() && xs.iter().zip(ys.iter()).all(|(x, y)| same(x, y))
        }
        _ => a == b,
    }
}

/// Check every `(cypher, want)` row: `cypher` is a whole expression, as
/// the ADR's tables write it, parsed by the product parser, lowered by
/// the product front-end and evaluated against an empty graph and row.
fn check_probes(rows: Vec<(&str, Want)>) {
    let g = Graph::default();
    let graph = ExecGraph::read(&g);
    for (cypher, want) in rows {
        let parsed = relate_parser::parser::parse_expression_str(cypher).expect(cypher);
        let expr = relate_front::lower_expr(&parsed).expect(cypher);
        let got = crate::eval::eval(&graph, &Row::new(), &expr, &BTreeMap::new(), None);
        match (want, got) {
            (Want::Is(want), Ok(got)) => {
                assert!(same(&got, &want), "{cypher}: got {got:?}, want {want:?}")
            }
            (Want::OutOfRange, Err(e)) => assert!(
                e.to_string().starts_with(OUT_OF_RANGE),
                "{cypher}: got error {e}, want {OUT_OF_RANGE}"
            ),
            (Want::Is(want), Err(e)) => panic!("{cypher}: got error {e}, want {want:?}"),
            (Want::OutOfRange, Ok(got)) => {
                panic!("{cypher}: got {got:?}, want error {OUT_OF_RANGE}")
            }
        }
    }
}

// ADR-0168 "Probe results", verified against neo4j:2026.09-community:
// every row's "Relate after" cell. Floats keep Java's `(long)` cast
// (NaN → 0, saturate at the INTEGER limits, truncate toward zero),
// out-of-range numeric strings raise NumberOutOfRange, toIntegerList()
// keeps INTEGER elements exact, and toFloat() accepts only Java's
// non-finite spellings.
#[test]
fn conversions_match_adr_0168_probe_table() {
    let (max, min) = (i64::MAX, i64::MIN);
    let (nan, inf, ninf) = (f64::NAN, f64::INFINITY, f64::NEG_INFINITY);
    check_probes(vec![
        ("toInteger(0.0/0.0)", int(0)),
        ("toInteger(1.0/0.0)", int(max)),
        ("toInteger(-1.0/0.0)", int(min)),
        ("toInteger(1e300)", int(max)),
        ("toInteger(-1e300)", int(min)),
        ("toInteger(9.3e18)", int(max)),
        ("toInteger(-9.3e18)", int(min)),
        ("toInteger(9.2e18)", int(9_200_000_000_000_000_000)),
        ("toInteger('NaN')", NULL),
        ("toInteger('nan')", NULL),
        ("toInteger('Infinity')", NULL),
        ("toInteger('-Infinity')", NULL),
        ("toInteger('inf')", NULL),
        ("toInteger('1e300')", OUT),
        ("toInteger('9.3e18')", OUT),
        ("toIntegerOrNull(0.0/0.0)", int(0)),
        ("toIntegerOrNull(1.0/0.0)", int(max)),
        ("toIntegerOrNull(-1.0/0.0)", int(min)),
        ("toIntegerOrNull(1e300)", int(max)),
        ("toIntegerOrNull('NaN')", NULL),
        ("toIntegerOrNull('1e300')", NULL),
        ("toIntegerList([0.0/0.0])", ints(&[0])),
        ("toIntegerList([1.0/0.0])", ints(&[max])),
        ("toIntegerList([-1.0/0.0])", ints(&[min])),
        ("toIntegerList([1e300])", ints(&[max])),
        (
            "toIntegerList([2.5, 9007199254740993])",
            ints(&[2, 9_007_199_254_740_993]),
        ),
        ("toFloat('NaN')", float(nan)),
        ("toFloat('-NaN')", float(nan)),
        ("toFloat('nan')", NULL),
        ("toFloat('Infinity')", float(inf)),
        ("toFloat('-Infinity')", float(ninf)),
        ("toFloat('+Infinity')", float(inf)),
        ("toFloat('infinity')", NULL),
        ("toFloat('inf')", NULL),
        ("toFloat('Inf')", NULL),
        ("toFloat('-inf')", NULL),
        ("toFloat('1e400')", float(inf)),
        ("toFloat(0.0/0.0)", float(nan)),
        ("toFloat(1.0/0.0)", float(inf)),
        ("toFloatOrNull('NaN')", float(nan)),
        ("toFloatOrNull('nan')", NULL),
        ("toFloatOrNull('Infinity')", float(inf)),
        ("toFloatOrNull('inf')", NULL),
        (
            "toFloatList([0.0/0.0, 1.0/0.0])",
            Want::Is(Value::List(
                vec![Value::Float(nan), Value::Float(inf)].into(),
            )),
        ),
    ]);
}

// ADR-0168 "Supplementary probes", verified against
// neo4j:2026.09-community: the boundaries of the exact-decimal string
// rule, INTEGER list elements versus VECTOR coordinates, and the
// toFloat() spellings. `toInteger('1e3000000000')` was ADR-0168's
// deliberate deviation (Neo4j saturates, Relate errored); ADR-0175
// supersedes it — the giant exponent diverts to the Cypher25-literal
// fallback, so the value saturates like Neo4j.
#[test]
fn conversions_match_adr_0168_supplementary_probes() {
    let (max, min) = (i64::MAX, i64::MIN);
    check_probes(vec![
        ("toInteger('-1e300')", OUT),
        ("toInteger('-9.3e18')", OUT),
        ("toInteger('1e400')", OUT),
        ("toInteger('-1e400')", OUT),
        ("toInteger(' 1e300 ')", OUT),
        ("toIntegerOrNull('1e400')", NULL),
        ("toIntegerOrNull('9.3e18')", NULL),
        ("toInteger('9223372036854775807')", int(max)),
        ("toInteger('9223372036854775808')", OUT),
        ("toIntegerOrNull('9223372036854775808')", NULL),
        ("toInteger('-9223372036854775808')", int(min)),
        ("toInteger('-9223372036854775809')", OUT),
        ("toInteger('9223372036854775807.0')", int(max)),
        ("toInteger('9223372036854775807.5')", OUT),
        (
            "toInteger('9223372036854775806.5')",
            int(9_223_372_036_854_775_806),
        ),
        ("toInteger('-9223372036854775808.0')", int(min)),
        ("toInteger('-9223372036854775808.5')", OUT),
        (
            "toInteger('9007199254740993.5')",
            int(9_007_199_254_740_993),
        ),
        ("toInteger('9.2e18')", int(9_200_000_000_000_000_000)),
        ("toInteger('-0.5')", int(0)),
        ("toInteger('1e-400')", int(0)),
        // ADR-0175 supersedes ADR-0168's deliberate deviation here: the
        // giant exponent overflows the BigDecimal path's int32 gate and
        // the literal fallback's float parse saturates, as in Neo4j.
        ("toInteger('1e3000000000')", int(max)),
        ("toInteger('+Infinity')", NULL),
        ("toInteger('-NaN')", NULL),
        (
            "toIntegerList([9223372036854775806])",
            ints(&[9_223_372_036_854_775_806]),
        ),
        (
            "toIntegerList([-9223372036854775807])",
            ints(&[-9_223_372_036_854_775_807]),
        ),
        ("toIntegerList([9.3e18, -1e300])", ints(&[max, min])),
        (
            "toIntegerList([9223372036854775807, -9223372036854775808])",
            ints(&[max, min]),
        ),
        ("toFloat('+NaN')", float(f64::NAN)),
        ("toFloat('NAN')", NULL),
        ("toFloat('INFINITY')", NULL),
        ("toFloat('+inf')", NULL),
        ("toFloat('-1e400')", float(f64::NEG_INFINITY)),
        ("toFloatOrNull('-Infinity')", float(f64::NEG_INFINITY)),
        ("toFloatOrNull('Inf')", NULL),
        // VECTOR coordinates convert through FLOAT, so an INTEGER64
        // coordinate above 2^53 rounds, as in Neo4j.
        (
            "toIntegerList(vector([9007199254740993, 2], 2, INTEGER64))",
            ints(&[9_007_199_254_740_992, 2]),
        ),
        (
            "toIntegerList(vector([2.5, 1e300], 2, FLOAT64))",
            ints(&[2, max]),
        ),
    ]);
}

// ADR-0168: the exact-decimal grammar `toInteger()` applies to a trimmed
// string. TCK TypeConversion2's decimal rows ('2.9' → 2, '1.7' → 1) and
// every form Rust's f64 parser accepted keep converting; the
// non-decimal forms stay non-numeric. ADR-0175: an exponent beyond the
// i32 range makes the BigDecimal-equivalent path report `None` (the
// string diverts to the Cypher25-literal fallback, whose float parse
// still yields 0 for `0e…`/`5e-…` and saturates to i64::MAX for
// `5e…` — pinned end-to-end by the '1e3000000000' probe rows).
#[test]
fn decimal_integer_parse_is_exact_and_rejects_non_decimals() {
    use DecimalInteger::{InRange, OutOfRange};
    for (input, want) in [
        ("2.9", InRange(2)),
        ("1.7", InRange(1)),
        ("-2.9", InRange(-2)),
        ("+42", InRange(42)),
        ("007", InRange(7)),
        ("7.", InRange(7)),
        (".5", InRange(0)),
        ("-.5", InRange(0)),
        ("1E3", InRange(1000)),
        ("1e+3", InRange(1000)),
        ("1234e-2", InRange(12)),
        ("0.000123e7", InRange(1230)),
        ("10000000000000000000", OutOfRange),
        ("-9223372036854775808e0", InRange(i64::MIN)),
        ("922337203685477580.75e1", OutOfRange),
        ("922337203685477580.70e1", InRange(i64::MAX)),
    ] {
        assert_eq!(parse_decimal_integer(input), Some(want), "{input}");
    }
    for input in [
        "", "+", "-", ".", "e5", "1e", "1e+", "1.2.3", "1e5e3", "--1", "+-1", "0x10", "1_000",
        "1.5d", " 1", "NaN", "Infinity", "inf", "nan",
    ] {
        assert_eq!(parse_decimal_integer(input), None, "{input:?}");
    }
    // An exponent's digits are checked even where its value overflows.
    for input in ["1e99999999999999999999x", "1e-99999999999999999999x"] {
        assert_eq!(parse_decimal_integer(input), None, "{input:?}");
    }
    // ADR-0175's int32-exponent gate: beyond it, BigDecimal throws and
    // the string is the literal fallback's, not this function's.
    for input in [
        "1e3000000000",
        "-1e3000000000",
        "1e-3000000000",
        "1e2147483648",
        "0e99999999999999999999999",
        "5e-99999999999999999999999",
        "5e99999999999999999999999",
    ] {
        assert_eq!(parse_decimal_integer(input), None, "{input:?}");
    }
    // The gate's boundary: i32::MAX stays on the exact-decimal path.
    assert_eq!(
        parse_decimal_integer("1e2147483647"),
        Some(OutOfRange),
        "1e2147483647"
    );
}

// ADR-0175 "Probe results", list-elements and string-forms groups,
// verified against neo4j:2026.09-community: every row's "Relate
// after" cell. String elements of the list conversions parse with the
// scalar OrNull string rules and never raise (an out-of-range numeric
// string becomes a NULL element); boolean elements are 1/0 in
// toIntegerList() and null in toFloatList(). The string forms follow
// Neo4j's pipeline: <= U+0020 trim, parseLong / parseDouble (hex
// floats, f/F/d/D suffixes, exact-case NaN/Infinity), the exact-decimal
// parse with the int32-exponent gate, then the Cypher25-literal
// fallback (Unicode trim, lowercase 0x/0o, underscores, suffixed
// decimal doubles).
#[test]
fn conversions_match_adr_0175_list_elements_and_string_forms() {
    let (max, min) = (i64::MAX, i64::MIN);
    let (nan, inf, ninf) = (f64::NAN, f64::INFINITY, f64::NEG_INFINITY);
    check_probes(vec![
        // --- list-elements ---
        (
            "toIntegerList(['1', 'NaN'])",
            list(vec![Value::Int(1), Value::Null]),
        ),
        (
            "toFloatList(['1.5', 'NaN'])",
            list(vec![Value::Float(1.5), Value::Float(nan)]),
        ),
        ("toIntegerList(['1e300'])", list(vec![Value::Null])),
        (
            "toIntegerList(['9223372036854775808', 'x', 1.5])",
            list(vec![Value::Null, Value::Null, Value::Int(1)]),
        ),
        (
            "toIntegerList([' 7 ', '2.9', '-0.5'])",
            list(vec![Value::Int(7), Value::Int(2), Value::Int(0)]),
        ),
        (
            "toFloatList(['Infinity', 'inf', ' 1.5 ', '1e400'])",
            list(vec![
                Value::Float(inf),
                Value::Null,
                Value::Float(1.5),
                Value::Float(inf),
            ]),
        ),
        (
            "toIntegerList(['0x1F', '1_000', '1.5f'])",
            list(vec![Value::Int(31), Value::Int(1000), Value::Int(1)]),
        ),
        (
            "toFloatList(['0x1p3', '1.5d'])",
            list(vec![Value::Float(8.0), Value::Float(1.5)]),
        ),
        (
            "toIntegerList([true, 'true'])",
            list(vec![Value::Int(1), Value::Null]),
        ),
        (
            "toFloatList([true, 'true'])",
            list(vec![Value::Null, Value::Null]),
        ),
        (
            "toIntegerList(['1e3000000000'])",
            list(vec![Value::Int(max)]),
        ),
        ("toIntegerList(['1e400'])", list(vec![Value::Null])),
        (
            "toFloatList(['1e3000000000'])",
            list(vec![Value::Float(inf)]),
        ),
        (
            "toIntegerList(['9223372036854775807.5'])",
            list(vec![Value::Null]),
        ),
        (
            "toIntegerList(['9223372036854775807.0'])",
            list(vec![Value::Int(max)]),
        ),
        (
            "toFloatList(['0x1F', '1_000', '0o17'])",
            list(vec![
                Value::Float(31.0),
                Value::Float(1000.0),
                Value::Float(15.0),
            ]),
        ),
        (
            "toIntegerList(['nan', 'inf', '-Infinity'])",
            list(vec![Value::Null, Value::Null, Value::Null]),
        ),
        (
            "toFloatList(['nan', 'inf', '-Infinity'])",
            list(vec![Value::Null, Value::Null, Value::Float(ninf)]),
        ),
        (
            "toIntegerList([null, '42'])",
            list(vec![Value::Null, Value::Int(42)]),
        ),
        (
            "toFloatList([null, '42'])",
            list(vec![Value::Null, Value::Float(42.0)]),
        ),
        (
            "toIntegerList(['+1', '-0', '0001'])",
            list(vec![Value::Int(1), Value::Int(0), Value::Int(1)]),
        ),
        (
            "toFloatList(['.5', '5.', '+.5', '1e3'])",
            list(vec![
                Value::Float(0.5),
                Value::Float(5.0),
                Value::Float(0.5),
                Value::Float(1000.0),
            ]),
        ),
        (
            "toIntegerList(['.5', '5.', '1e3'])",
            list(vec![Value::Int(0), Value::Int(5), Value::Int(1000)]),
        ),
        (
            "toIntegerList([true, false])",
            list(vec![Value::Int(1), Value::Int(0)]),
        ),
        (
            "toFloatList([true, false])",
            list(vec![Value::Null, Value::Null]),
        ),
        ("toIntegerList(['\\t7\\n'])", list(vec![Value::Int(7)])),
        ("toIntegerList([' 0x1F '])", list(vec![Value::Int(31)])),
        ("toFloatList([' 0x1p3 '])", list(vec![Value::Float(8.0)])),
        (
            "toIntegerList(['2L', '2f'])",
            list(vec![Value::Null, Value::Null]),
        ),
        (
            "toFloatList(['2L', '2f'])",
            list(vec![Value::Null, Value::Float(2.0)]),
        ),
        // --- string-forms ---
        ("toInteger(' 1.5 ')", int(1)),
        ("toFloat(' 1.5 ')", float(1.5)),
        ("toIntegerOrNull(' 1.5 ')", int(1)),
        ("toFloatOrNull(' 1.5 ')", float(1.5)),
        ("toInteger('1.5f')", int(1)),
        ("toFloat('1.5f')", float(1.5)),
        ("toIntegerOrNull('1.5f')", int(1)),
        ("toFloatOrNull('1.5f')", float(1.5)),
        ("toInteger('1.5F')", int(1)),
        ("toFloat('1.5F')", float(1.5)),
        ("toIntegerOrNull('1.5F')", int(1)),
        ("toFloatOrNull('1.5F')", float(1.5)),
        ("toInteger('1.5d')", int(1)),
        ("toFloat('1.5d')", float(1.5)),
        ("toIntegerOrNull('1.5d')", int(1)),
        ("toFloatOrNull('1.5d')", float(1.5)),
        ("toInteger('1.5D')", int(1)),
        ("toFloat('1.5D')", float(1.5)),
        ("toIntegerOrNull('1.5D')", int(1)),
        ("toFloatOrNull('1.5D')", float(1.5)),
        ("toInteger('2f')", NULL),
        ("toFloat('2f')", float(2.0)),
        ("toIntegerOrNull('2f')", NULL),
        ("toFloatOrNull('2f')", float(2.0)),
        ("toInteger('2L')", NULL),
        ("toFloat('2L')", NULL),
        ("toIntegerOrNull('2L')", NULL),
        ("toFloatOrNull('2L')", NULL),
        ("toInteger('0x1F')", int(31)),
        ("toFloat('0x1F')", float(31.0)),
        ("toIntegerOrNull('0x1F')", int(31)),
        ("toFloatOrNull('0x1F')", float(31.0)),
        ("toInteger('-0x1F')", int(-31)),
        ("toFloat('-0x1F')", float(-31.0)),
        ("toIntegerOrNull('-0x1F')", int(-31)),
        ("toFloatOrNull('-0x1F')", float(-31.0)),
        ("toInteger('0o17')", int(15)),
        ("toFloat('0o17')", float(15.0)),
        ("toIntegerOrNull('0o17')", int(15)),
        ("toFloatOrNull('0o17')", float(15.0)),
        ("toInteger('017')", int(17)),
        ("toFloat('017')", float(17.0)),
        ("toIntegerOrNull('017')", int(17)),
        ("toFloatOrNull('017')", float(17.0)),
        ("toInteger('1_000')", int(1000)),
        ("toFloat('1_000')", float(1000.0)),
        ("toIntegerOrNull('1_000')", int(1000)),
        ("toFloatOrNull('1_000')", float(1000.0)),
        ("toInteger('1_000.5')", int(1000)),
        ("toFloat('1_000.5')", float(1000.5)),
        ("toIntegerOrNull('1_000.5')", int(1000)),
        ("toFloatOrNull('1_000.5')", float(1000.5)),
        ("toInteger('0x1p3')", NULL),
        ("toFloat('0x1p3')", float(8.0)),
        ("toIntegerOrNull('0x1p3')", NULL),
        ("toFloatOrNull('0x1p3')", float(8.0)),
        ("toInteger('0x1.8p1')", NULL),
        ("toFloat('0x1.8p1')", float(3.0)),
        ("toIntegerOrNull('0x1.8p1')", NULL),
        ("toFloatOrNull('0x1.8p1')", float(3.0)),
        ("toInteger('1e3000000000')", int(max)),
        ("toFloat('1e3000000000')", float(inf)),
        ("toIntegerOrNull('1e3000000000')", int(max)),
        ("toFloatOrNull('1e3000000000')", float(inf)),
        ("toInteger('-1e3000000000')", int(min)),
        ("toFloat('-1e3000000000')", float(ninf)),
        ("toIntegerOrNull('-1e3000000000')", int(min)),
        ("toFloatOrNull('-1e3000000000')", float(ninf)),
        ("toInteger('1e-3000000000')", int(0)),
        ("toFloat('1e-3000000000')", float(0.0)),
        ("toIntegerOrNull('1e-3000000000')", int(0)),
        ("toFloatOrNull('1e-3000000000')", float(0.0)),
        ("toInteger('\\t1.5\\t')", int(1)),
        ("toFloat('\\t1.5\\t')", float(1.5)),
        ("toIntegerOrNull('\\t1.5\\t')", int(1)),
        ("toFloatOrNull('\\t1.5\\t')", float(1.5)),
        ("toInteger('\\n1.5\\n')", int(1)),
        ("toFloat('\\n1.5\\n')", float(1.5)),
        ("toIntegerOrNull('\\n1.5\\n')", int(1)),
        ("toFloatOrNull('\\n1.5\\n')", float(1.5)),
        ("toInteger('1e-400')", int(0)),
        ("toFloat('1e-400')", float(0.0)),
        ("toIntegerOrNull('1e-400')", int(0)),
        ("toFloatOrNull('1e-400')", float(0.0)),
        ("toInteger('.5f')", int(0)),
        ("toFloat('.5f')", float(0.5)),
        ("toIntegerOrNull('.5f')", int(0)),
        ("toFloatOrNull('.5f')", float(0.5)),
        ("toInteger('0x')", NULL),
        ("toFloat('0x')", NULL),
        ("toInteger('0X1F')", NULL),
        ("toFloat('0X1F')", NULL),
        ("toInteger('+0x1F')", NULL),
        ("toFloat('+0x1F')", NULL),
        ("toInteger('0X1P3')", NULL),
        ("toFloat('0X1P3')", float(8.0)),
        ("toInteger('-1.5f')", int(-1)),
        ("toFloat('-1.5f')", float(-1.5)),
        ("toInteger('1.5e2f')", int(150)),
        ("toFloat('1.5e2f')", float(150.0)),
        ("toFloat(' Infinity ')", float(inf)),
        ("toFloat('NaN ')", float(nan)),
        ("toFloat('\\u00A01.5\\u00A0')", float(1.5)),
        ("toInteger('\\u00A07\\u00A0')", int(7)),
        ("toInteger('')", NULL),
        ("toFloat('')", NULL),
        // --- Cypher comments in the literal fallback ---
        // Neo4j parses the string with the Cypher parser, whose lexer skips
        // `//` line and `/* */` block comments, so these are number
        // literals. Whitespace and comments may sit around the sign.
        ("toInteger('1 // :(')", int(1)),
        ("toInteger('-1 // :(')", int(-1)),
        ("toInteger('/* :( */ 1 /* :( */')", int(1)),
        ("toInteger('-/* :( */ 1 /* :( */')", int(-1)),
        ("toFloat('1 // 2')", float(1.0)),
        // Two tokens either side of a comment stay two tokens.
        ("toInteger('1 1')", NULL),
        ("toInteger('1/*c*/2')", NULL),
        // An unterminated block comment is a lexer error, not a number.
        ("toInteger('1 /* unterminated')", NULL),
    ]);
}

// ADR-0448: the rendering policy is a property of the value — one
// full-precision rendering, through the value layer's display policy,
// whether the temporal is top-level, nested in a map, or built by any
// spelling. Top-level and nested are both pinned so the two paths
// cannot drift apart again.
#[test]
fn to_string_renders_temporals_at_full_precision() {
    let t = relate_core::temporal::datetime_from_epoch(1_709_164_800, 0).unwrap();
    let full = t.full_seconds_string();
    assert_eq!(
        to_string_value(Some(&Value::Temporal(t.clone())), None),
        Ok(Value::String(full.clone().into()))
    );
    let nested = Value::Map(BTreeMap::from([("at".to_string(), Value::Temporal(t))]).into());
    assert_eq!(
        to_string_value(Some(&nested), None),
        Ok(Value::String(format!("{{at: {full}}}").into()))
    );
}

// `toStringList()` routes through the shared list-conversion scaffold like
// `toIntegerList()`/`toFloatList()`, so a VECTOR argument is accepted; the
// scaffold normalises vector coordinates to FLOAT (ADR-0168), so an
// INTEGER64 vector renders as float strings.
#[test]
fn to_string_list_accepts_a_vector_argument() {
    check_probes(vec![(
        "toStringList(vector([1, 2], 2, INTEGER64))",
        Want::Is(Value::List(
            vec![Value::String("1.0".into()), Value::String("2.0".into())].into(),
        )),
    )]);
}

// `toStringList()`: TypeConversion4's [9]/[10] dialect split — under an
// explicit CYPHER 5 preamble non-scalar elements become null; in Cypher 25
// they (and null itself) render in display form.
#[test]
fn to_string_list_converts_elements_per_dialect() {
    let g = Graph::default();
    let graph = ExecGraph::read(&g);
    let s = |v: &str| Value::String(v.into());
    let list = |xs: Vec<Value>| Value::List(xs.into());
    let mixed = || {
        list(vec![
            Value::Int(1),
            list(vec![Value::Int(2), Value::Int(3)]),
            Value::Null,
        ])
    };

    assert_eq!(
        bi_tostringlist(&graph, "toStringList", vec![mixed()], Some(25)),
        Ok(list(vec![s("1"), s("[2, 3]"), s("null")]))
    );
    assert_eq!(
        bi_tostringlist(&graph, "toStringList", vec![mixed()], Some(5)),
        Ok(list(vec![s("1"), Value::Null, Value::Null]))
    );
    // Null-in-null-out, and only a list is admissible.
    assert_eq!(
        bi_tostringlist(&graph, "toStringList", vec![Value::Null], None),
        Ok(Value::Null)
    );
    assert!(bi_tostringlist(&graph, "toStringList", vec![Value::Int(1)], None).is_err());
}

/// LABS-785: `toString`/`toStringOrNull` take exactly one argument. A second
/// argument is Neo4j's 42I13 arity error, never silently ignored.
#[test]
fn tostring_rejects_a_second_argument() {
    let g = Graph::default();
    let graph = ExecGraph::read(&g);
    for name in ["toString", "toStringOrNull"] {
        let err = function(
            &graph,
            &Row::new(),
            &BTreeMap::new(),
            name,
            false,
            &[Expr::Int(7), Expr::Int(8)],
            None,
        )
        .unwrap_err();
        assert!(
            err.to_string().contains("InvalidNumberOfArguments"),
            "{name}: {err}"
        );
    }
}

/// WB5: the arity contract is decided from the row *before* any argument is
/// evaluated, so `toString(1, boom())` reports the arity, never the
/// argument's own error. The `split` pair covers the exactly-two contract.
#[test]
fn arity_is_reported_before_any_argument_is_evaluated() {
    let g = Graph::default();
    let graph = ExecGraph::read(&g);
    let boom = Expr::Function {
        name: SmallName::from_name("no_such_function"),
        distinct: false,
        args: Vec::new(),
    };
    let string = |value: &str| Expr::String(value.to_string());
    for (name, args) in [
        ("toString", vec![Expr::Int(1), boom.clone()]),
        ("toStringOrNull", vec![Expr::Int(1), boom.clone()]),
        ("split", vec![string("a,b"), string(","), boom.clone()]),
        ("split", vec![string("a,b")]),
        ("split", vec![string("a"), string(","), string("b")]),
    ] {
        let err = function(
            &graph,
            &Row::new(),
            &BTreeMap::new(),
            name,
            false,
            &args,
            None,
        )
        .unwrap_err();
        assert!(
            err.to_string().contains("InvalidNumberOfArguments"),
            "{name}: {err}"
        );
    }
}