kglite 0.16.5

Pure-Rust embedded Cypher knowledge graph engine with in-memory, mmap, and disk storage, and agent-facing schema introspection
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
//! List values: parsing, slicing, indexing, sizing, and the
//! any/all/none/single quantifier predicates.

use super::*;

// ========================================================================
// parse_list_value + split_top_level_commas tests
// ========================================================================

#[test]
fn test_parse_list_value_simple_ints() {
    let val = Value::String("[1, 2, 3]".to_string());
    let items = parse_list_value(&val);
    assert_eq!(items.len(), 3);
    assert_eq!(items[0], Value::Int64(1));
    assert_eq!(items[1], Value::Int64(2));
    assert_eq!(items[2], Value::Int64(3));
}

#[test]
fn test_parse_list_value_strings() {
    let val = Value::String(r#"["hello", "world"]"#.to_string());
    let items = parse_list_value(&val);
    assert_eq!(items.len(), 2);
    assert_eq!(items[0], Value::String("hello".to_string()));
    assert_eq!(items[1], Value::String("world".to_string()));
}

#[test]
fn test_parse_list_value_empty() {
    let val = Value::String("[]".to_string());
    let items = parse_list_value(&val);
    assert!(items.is_empty());
}

#[test]
fn test_parse_list_value_json_objects() {
    // This is the critical test — JSON objects must not be split on inner commas
    let val =
        Value::String(r#"[{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]"#.to_string());
    let items = parse_list_value(&val);
    assert_eq!(items.len(), 2);
    // Each item should be a complete JSON object string
    match &items[0] {
        Value::String(s) => assert!(s.contains("Alice"), "first item: {}", s),
        other => panic!("Expected String, got {:?}", other),
    }
}

#[test]
fn test_parse_list_value_booleans() {
    let val = Value::String("[true, false, null]".to_string());
    let items = parse_list_value(&val);
    assert_eq!(items.len(), 3);
    assert_eq!(items[0], Value::Boolean(true));
    assert_eq!(items[1], Value::Boolean(false));
    assert_eq!(items[2], Value::Null);
}

#[test]
fn test_parse_list_value_non_list() {
    let val = Value::String("not a list".to_string());
    let items = parse_list_value(&val);
    assert!(items.is_empty());
}

#[test]
fn test_parse_list_value_non_string() {
    let val = Value::Int64(42);
    let items = parse_list_value(&val);
    assert!(items.is_empty());
}

#[test]
fn test_split_top_level_commas_simple() {
    let items = split_top_level_commas("a, b, c");
    assert_eq!(items, vec!["a", " b", " c"]);
}

#[test]
fn test_split_top_level_commas_nested_braces() {
    let items = split_top_level_commas(r#"{"a": 1, "b": 2}, {"c": 3}"#);
    assert_eq!(items.len(), 2);
    assert!(items[0].contains("\"a\": 1"));
    assert!(items[1].contains("\"c\": 3"));
}

#[test]
fn test_split_top_level_commas_nested_brackets() {
    let items = split_top_level_commas("[1, 2], [3, 4]");
    assert_eq!(items.len(), 2);
}

#[test]
fn test_split_top_level_commas_quoted_strings() {
    let items = split_top_level_commas(r#""hello, world", "foo""#);
    assert_eq!(items.len(), 2);
    assert_eq!(items[0].trim(), r#""hello, world""#);
}

#[test]
fn test_list_slice_basic() {
    let graph = DirGraph::new();
    let no_params = HashMap::new();
    let executor = CypherExecutor::with_params(&graph, &no_params, None);

    // Phase A.1 / C4 — slice now returns Value::List, not JSON string.

    // [start..end]
    let q = parser::parse_cypher("RETURN [1,2,3,4,5][1..3]").unwrap();
    let result = executor.execute(&q).unwrap();
    assert_eq!(
        result.rows[0].first(),
        Some(&Value::List(vec![Value::Int64(2), Value::Int64(3)]))
    );

    // [..end]
    let q = parser::parse_cypher("RETURN [1,2,3][..2]").unwrap();
    let result = executor.execute(&q).unwrap();
    assert_eq!(
        result.rows[0].first(),
        Some(&Value::List(vec![Value::Int64(1), Value::Int64(2)]))
    );

    // [start..]
    let q = parser::parse_cypher("RETURN [1,2,3][1..]").unwrap();
    let result = executor.execute(&q).unwrap();
    assert_eq!(
        result.rows[0].first(),
        Some(&Value::List(vec![Value::Int64(2), Value::Int64(3)]))
    );
}

#[test]
fn test_list_slice_edge_cases() {
    let graph = DirGraph::new();
    let no_params = HashMap::new();
    let executor = CypherExecutor::with_params(&graph, &no_params, None);

    // Phase A.1 / C4 — slice returns native Value::List.

    // Out of bounds — clamps to available
    let q = parser::parse_cypher("RETURN [1,2,3][..100]").unwrap();
    let result = executor.execute(&q).unwrap();
    assert_eq!(
        result.rows[0].first(),
        Some(&Value::List(vec![
            Value::Int64(1),
            Value::Int64(2),
            Value::Int64(3)
        ]))
    );

    // Empty slice (start >= end)
    let q = parser::parse_cypher("RETURN [1,2,3][3..1]").unwrap();
    let result = executor.execute(&q).unwrap();
    assert_eq!(result.rows[0].first(), Some(&Value::List(Vec::new())));

    // Negative index in slice
    let q = parser::parse_cypher("RETURN [1,2,3,4,5][-3..]").unwrap();
    let result = executor.execute(&q).unwrap();
    assert_eq!(
        result.rows[0].first(),
        Some(&Value::List(vec![
            Value::Int64(3),
            Value::Int64(4),
            Value::Int64(5)
        ]))
    );
}

#[test]
fn test_list_index_still_works() {
    // Verify plain indexing is unbroken
    let graph = DirGraph::new();
    let no_params = HashMap::new();
    let executor = CypherExecutor::with_params(&graph, &no_params, None);

    let q = parser::parse_cypher("RETURN [10,20,30][0]").unwrap();
    let result = executor.execute(&q).unwrap();
    assert_eq!(result.rows[0].first(), Some(&Value::Int64(10)));

    let q = parser::parse_cypher("RETURN [10,20,30][-1]").unwrap();
    let result = executor.execute(&q).unwrap();
    assert_eq!(result.rows[0].first(), Some(&Value::Int64(30)));
}

#[test]
fn test_node_list_property_index_borrow_path() {
    // The borrow fast-path in evaluate_index_access indexes a stored list
    // property off a node binding without cloning the whole list. It must
    // stay bit-identical to the owned path for positive, negative, and
    // out-of-range indices, and inside a reduce loop (the vector-scoring
    // shape that motivated the change).
    let mut graph = DirGraph::new();
    let setup = parser::parse_cypher("CREATE (n:V {id: 1, arr: [10, 20, 30]})").unwrap();
    execute_mutable(
        &mut graph,
        &setup,
        HashMap::new(),
        crate::graph::algorithms::Interrupt::default(),
    )
    .unwrap();

    let no_params = HashMap::new();
    let executor = CypherExecutor::with_params(&graph, &no_params, None);
    let q = parser::parse_cypher(
        "MATCH (n:V) RETURN n.arr[0] AS a, n.arr[-1] AS b, n.arr[5] AS d, \
         reduce(s = 0, i IN range(0, 2) | s + n.arr[i]) AS total",
    )
    .unwrap();
    let result = executor.execute(&q).unwrap();
    let row = &result.rows[0];
    assert_eq!(row[0], Value::Int64(10)); // first element
    assert_eq!(row[1], Value::Int64(30)); // negative index from the end
    assert_eq!(row[2], Value::Null); // out of range
    assert_eq!(row[3], Value::Int64(60)); // 10 + 20 + 30 via per-element subscript
}

#[test]
fn test_projected_and_parameter_list_index_borrow_path() {
    // The other two borrowable container shapes: a variable projected into
    // the row (WITH … AS q) and a query parameter. Both must index without
    // cloning the whole list and match the owned path.
    let graph = DirGraph::new();
    let mut params = HashMap::new();
    params.insert(
        "p".to_string(),
        Value::List(vec![Value::Int64(7), Value::Int64(8), Value::Int64(9)]),
    );
    let executor = CypherExecutor::with_params(&graph, &params, None);
    let q = parser::parse_cypher(
        "WITH [100, 200, 300] AS q RETURN q[1] AS from_projected, $p[-1] AS from_param",
    )
    .unwrap();
    let result = executor.execute(&q).unwrap();
    let row = &result.rows[0];
    assert_eq!(row[0], Value::Int64(200));
    assert_eq!(row[1], Value::Int64(9));
}

#[test]
fn test_list_slice_with_collect() {
    let mut graph = DirGraph::new();
    let setup = parser::parse_cypher(
        "CREATE (a:Item {name: 'A'}), (b:Item {name: 'B'}), \
         (c:Item {name: 'C'}), (d:Item {name: 'D'})",
    )
    .unwrap();
    execute_mutable(
        &mut graph,
        &setup,
        HashMap::new(),
        crate::graph::algorithms::Interrupt::default(),
    )
    .unwrap();

    let q = parser::parse_cypher("MATCH (n:Item) WITH collect(n.name) AS names RETURN names[..2]")
        .unwrap();
    let no_params = HashMap::new();
    let executor = CypherExecutor::with_params(&graph, &no_params, None);
    let result = executor.execute(&q).unwrap();

    // Should return a list with exactly 2 elements
    let val = result.rows[0].first().unwrap();
    let items = parse_list_value(val);
    assert_eq!(items.len(), 2);
}

#[test]
fn test_size_on_list() {
    let graph = DirGraph::new();
    let no_params = HashMap::new();
    let executor = CypherExecutor::with_params(&graph, &no_params, None);

    // size() on a list literal should return element count, not string length
    let q = parser::parse_cypher("RETURN size([1,2,3])").unwrap();
    let result = executor.execute(&q).unwrap();
    assert_eq!(result.rows[0].first(), Some(&Value::Int64(3)));

    // size() on a plain string should return character count
    let q = parser::parse_cypher("RETURN size('hello')").unwrap();
    let result = executor.execute(&q).unwrap();
    assert_eq!(result.rows[0].first(), Some(&Value::Int64(5)));

    // size() on empty list
    let q = parser::parse_cypher("RETURN size([])").unwrap();
    let result = executor.execute(&q).unwrap();
    assert_eq!(result.rows[0].first(), Some(&Value::Int64(0)));
}

#[test]
fn test_length_on_list() {
    let graph = DirGraph::new();
    let no_params = HashMap::new();
    let executor = CypherExecutor::with_params(&graph, &no_params, None);

    // length() on a list should return element count
    let q = parser::parse_cypher("RETURN length([10,20,30,40])").unwrap();
    let result = executor.execute(&q).unwrap();
    assert_eq!(result.rows[0].first(), Some(&Value::Int64(4)));
}

#[test]
fn test_size_on_collect_result() {
    let mut graph = DirGraph::new();
    let setup = parser::parse_cypher(
        "CREATE (a:Item {name: 'A'}), (b:Item {name: 'B'}), (c:Item {name: 'C'})",
    )
    .unwrap();
    execute_mutable(
        &mut graph,
        &setup,
        HashMap::new(),
        crate::graph::algorithms::Interrupt::default(),
    )
    .unwrap();

    let q = parser::parse_cypher("MATCH (n:Item) WITH collect(n.name) AS names RETURN size(names)")
        .unwrap();
    let no_params = HashMap::new();
    let executor = CypherExecutor::with_params(&graph, &no_params, None);
    let result = executor.execute(&q).unwrap();
    assert_eq!(result.rows[0].first(), Some(&Value::Int64(3)));
}

#[test]
fn test_aggregate_with_slice() {
    // collect(...)[0..N] in RETURN with aggregation
    let mut graph = DirGraph::new();
    let setup = parser::parse_cypher(
        "CREATE (a:Item {cat: 'X', name: 'A'}), (b:Item {cat: 'X', name: 'B'}), \
         (c:Item {cat: 'X', name: 'C'}), (d:Item {cat: 'Y', name: 'D'})",
    )
    .unwrap();
    execute_mutable(
        &mut graph,
        &setup,
        HashMap::new(),
        crate::graph::algorithms::Interrupt::default(),
    )
    .unwrap();

    let q = parser::parse_cypher(
        "MATCH (n:Item) \
         RETURN n.cat AS cat, count(n) AS cnt, collect(n.name)[..2] AS sample \
         ORDER BY cat",
    )
    .unwrap();
    let no_params = HashMap::new();
    let executor = CypherExecutor::with_params(&graph, &no_params, None);
    let result = executor.execute(&q).unwrap();

    assert_eq!(result.rows.len(), 2);
    // Group X has 3 items, sliced to 2
    let x_row = &result.rows[0];
    assert_eq!(x_row.first(), Some(&Value::String("X".into())));
    assert_eq!(x_row.get(1), Some(&Value::Int64(3)));
    let sample = parse_list_value(x_row.get(2).unwrap());
    assert_eq!(sample.len(), 2);

    // Group Y has 1 item, sliced to at most 2
    let y_row = &result.rows[1];
    assert_eq!(y_row.first(), Some(&Value::String("Y".into())));
    assert_eq!(y_row.get(1), Some(&Value::Int64(1)));
    let sample_y = parse_list_value(y_row.get(2).unwrap());
    assert_eq!(sample_y.len(), 1);
}

#[test]
fn test_aggregate_arithmetic() {
    // count(*) + 1 in RETURN with aggregation
    let mut graph = DirGraph::new();
    let setup = parser::parse_cypher(
        "CREATE (a:Item {name: 'A'}), (b:Item {name: 'B'}), (c:Item {name: 'C'})",
    )
    .unwrap();
    execute_mutable(
        &mut graph,
        &setup,
        HashMap::new(),
        crate::graph::algorithms::Interrupt::default(),
    )
    .unwrap();

    let q = parser::parse_cypher("MATCH (n:Item) RETURN count(n) + 1 AS cnt_plus").unwrap();
    let no_params = HashMap::new();
    let executor = CypherExecutor::with_params(&graph, &no_params, None);
    let result = executor.execute(&q).unwrap();
    // count(n)=3, 3+1=4.0 (float because add_values promotes)
    let val = result.rows[0].first().unwrap();
    match val {
        Value::Int64(i) => assert_eq!(*i, 4),
        Value::Float64(f) => assert!((f - 4.0).abs() < 0.001),
        _ => panic!("Expected numeric, got {:?}", val),
    }
}

#[test]
fn test_size_of_collect_in_return() {
    // size(collect(...)) in RETURN — non-aggregate wrapping aggregate
    let mut graph = DirGraph::new();
    let setup = parser::parse_cypher(
        "CREATE (a:Item {name: 'A'}), (b:Item {name: 'B'}), (c:Item {name: 'C'})",
    )
    .unwrap();
    execute_mutable(
        &mut graph,
        &setup,
        HashMap::new(),
        crate::graph::algorithms::Interrupt::default(),
    )
    .unwrap();

    // No grouping — all rows aggregated
    let q = parser::parse_cypher("MATCH (n:Item) RETURN size(collect(n.name)) AS cnt").unwrap();
    let no_params = HashMap::new();
    let executor = CypherExecutor::with_params(&graph, &no_params, None);
    let result = executor.execute(&q).unwrap();
    assert_eq!(result.rows[0].first(), Some(&Value::Int64(3)));
}

#[test]
fn test_size_of_collect_grouped() {
    // size(collect(...)) with grouping
    let mut graph = DirGraph::new();
    let setup = parser::parse_cypher(
        "CREATE (a:Item {cat: 'X', name: 'A'}), (b:Item {cat: 'X', name: 'B'}), \
         (c:Item {cat: 'X', name: 'C'}), (d:Item {cat: 'Y', name: 'D'})",
    )
    .unwrap();
    execute_mutable(
        &mut graph,
        &setup,
        HashMap::new(),
        crate::graph::algorithms::Interrupt::default(),
    )
    .unwrap();

    let q = parser::parse_cypher(
        "MATCH (n:Item) \
         RETURN n.cat AS cat, size(collect(n.name)) AS cnt \
         ORDER BY cat",
    )
    .unwrap();
    let no_params = HashMap::new();
    let executor = CypherExecutor::with_params(&graph, &no_params, None);
    let result = executor.execute(&q).unwrap();
    assert_eq!(result.rows.len(), 2);
    assert_eq!(result.rows[0].get(1), Some(&Value::Int64(3))); // X: 3
    assert_eq!(result.rows[1].get(1), Some(&Value::Int64(1))); // Y: 1
}

// ========================================================================
// List Quantifier Predicate Tests
// ========================================================================

#[test]
fn test_list_predicate_any() {
    let graph = DirGraph::new();
    let q = parser::parse_cypher(
        "WITH [1, 2, 3, 4, 5] AS nums \
         RETURN any(x IN nums WHERE x > 3) AS result",
    )
    .unwrap();
    let no_params = HashMap::new();
    let executor = CypherExecutor::with_params(&graph, &no_params, None);
    let result = executor.execute(&q).unwrap();
    assert_eq!(result.rows.len(), 1);
    assert_eq!(result.rows[0].first(), Some(&Value::Boolean(true)));
}

#[test]
fn test_list_predicate_any_false() {
    let graph = DirGraph::new();
    let q = parser::parse_cypher(
        "WITH [1, 2, 3] AS nums \
         RETURN any(x IN nums WHERE x > 10) AS result",
    )
    .unwrap();
    let no_params = HashMap::new();
    let executor = CypherExecutor::with_params(&graph, &no_params, None);
    let result = executor.execute(&q).unwrap();
    assert_eq!(result.rows.len(), 1);
    assert_eq!(result.rows[0].first(), Some(&Value::Boolean(false)));
}

#[test]
fn test_list_predicate_all() {
    let graph = DirGraph::new();
    let q = parser::parse_cypher(
        "WITH [2, 4, 6] AS nums \
         RETURN all(x IN nums WHERE x > 0) AS result",
    )
    .unwrap();
    let no_params = HashMap::new();
    let executor = CypherExecutor::with_params(&graph, &no_params, None);
    let result = executor.execute(&q).unwrap();
    assert_eq!(result.rows.len(), 1);
    assert_eq!(result.rows[0].first(), Some(&Value::Boolean(true)));
}

#[test]
fn test_list_predicate_all_false() {
    let graph = DirGraph::new();
    let q = parser::parse_cypher(
        "WITH [2, 4, 6] AS nums \
         RETURN all(x IN nums WHERE x > 3) AS result",
    )
    .unwrap();
    let no_params = HashMap::new();
    let executor = CypherExecutor::with_params(&graph, &no_params, None);
    let result = executor.execute(&q).unwrap();
    assert_eq!(result.rows.len(), 1);
    assert_eq!(result.rows[0].first(), Some(&Value::Boolean(false)));
}

#[test]
fn test_list_predicate_none() {
    let graph = DirGraph::new();
    let q = parser::parse_cypher(
        "WITH [1, 2, 3] AS nums \
         RETURN none(x IN nums WHERE x > 10) AS result",
    )
    .unwrap();
    let no_params = HashMap::new();
    let executor = CypherExecutor::with_params(&graph, &no_params, None);
    let result = executor.execute(&q).unwrap();
    assert_eq!(result.rows.len(), 1);
    assert_eq!(result.rows[0].first(), Some(&Value::Boolean(true)));
}

#[test]
fn test_list_predicate_none_false() {
    let graph = DirGraph::new();
    let q = parser::parse_cypher(
        "WITH [1, 2, 3] AS nums \
         RETURN none(x IN nums WHERE x > 2) AS result",
    )
    .unwrap();
    let no_params = HashMap::new();
    let executor = CypherExecutor::with_params(&graph, &no_params, None);
    let result = executor.execute(&q).unwrap();
    assert_eq!(result.rows.len(), 1);
    assert_eq!(result.rows[0].first(), Some(&Value::Boolean(false)));
}

#[test]
fn test_list_predicate_single() {
    let graph = DirGraph::new();
    let q = parser::parse_cypher(
        "WITH [1, 2, 3] AS nums \
         RETURN single(x IN nums WHERE x > 2) AS result",
    )
    .unwrap();
    let no_params = HashMap::new();
    let executor = CypherExecutor::with_params(&graph, &no_params, None);
    let result = executor.execute(&q).unwrap();
    assert_eq!(result.rows.len(), 1);
    assert_eq!(result.rows[0].first(), Some(&Value::Boolean(true)));
}

#[test]
fn test_list_predicate_single_false_multiple() {
    let graph = DirGraph::new();
    let q = parser::parse_cypher(
        "WITH [1, 2, 3] AS nums \
         RETURN single(x IN nums WHERE x > 1) AS result",
    )
    .unwrap();
    let no_params = HashMap::new();
    let executor = CypherExecutor::with_params(&graph, &no_params, None);
    let result = executor.execute(&q).unwrap();
    assert_eq!(result.rows.len(), 1);
    assert_eq!(result.rows[0].first(), Some(&Value::Boolean(false)));
}

#[test]
fn test_list_predicate_in_where_clause() {
    // The user's actual use case: any(w IN list WHERE w.prop IS NOT NULL)
    let mut graph = DirGraph::new();
    let setup = parser::parse_cypher(
        "CREATE (a:Well {name: 'W1', depth: 100}), \
         (b:Well {name: 'W2'}), \
         (c:Well {name: 'W3', depth: 300})",
    )
    .unwrap();
    execute_mutable(
        &mut graph,
        &setup,
        HashMap::new(),
        crate::graph::algorithms::Interrupt::default(),
    )
    .unwrap();

    let q = parser::parse_cypher(
        "MATCH (w:Well) \
         WITH collect(w.depth) AS depths \
         WHERE any(d IN depths WHERE d IS NOT NULL) \
         RETURN size(depths) AS count",
    )
    .unwrap();
    let no_params = HashMap::new();
    let executor = CypherExecutor::with_params(&graph, &no_params, None);
    let result = executor.execute(&q).unwrap();
    // any(d IN depths WHERE d IS NOT NULL) should be true (W1 and W3 have depth)
    assert_eq!(result.rows.len(), 1);
}

#[test]
fn test_list_predicate_with_is_not_null() {
    // Matches the user's real use case: any(w IN values WHERE w IS NOT NULL)
    let graph = DirGraph::new();
    let q = parser::parse_cypher(
        "WITH [1, null, 3, null, 5] AS values \
         RETURN any(v IN values WHERE v IS NOT NULL) AS has_value, \
                all(v IN values WHERE v IS NOT NULL) AS all_present, \
                none(v IN values WHERE v IS NOT NULL) AS none_present",
    )
    .unwrap();
    let no_params = HashMap::new();
    let executor = CypherExecutor::with_params(&graph, &no_params, None);
    let result = executor.execute(&q).unwrap();
    assert_eq!(result.rows.len(), 1);
    assert_eq!(result.rows[0].first(), Some(&Value::Boolean(true))); // any: true
    assert_eq!(result.rows[0].get(1), Some(&Value::Boolean(false))); // all: false
    assert_eq!(result.rows[0].get(2), Some(&Value::Boolean(false))); // none: false
}

#[test]
fn test_list_predicate_collected_nodes_property_access() {
    // User's exact pattern: collect nodes, then any(w IN wells WHERE w.prop IS NOT NULL)
    let mut graph = DirGraph::new();
    let setup = parser::parse_cypher(
        "CREATE (a:Well {name: 'W1', formation: 'Sandstone'}), \
         (b:Well {name: 'W2'}), \
         (c:Well {name: 'W3', formation: 'Limestone'})",
    )
    .unwrap();
    execute_mutable(
        &mut graph,
        &setup,
        HashMap::new(),
        crate::graph::algorithms::Interrupt::default(),
    )
    .unwrap();

    // any() with collected node property access
    let q = parser::parse_cypher(
        "MATCH (w:Well) \
         WITH collect(w) AS wells \
         RETURN any(x IN wells WHERE x.formation IS NOT NULL) AS has_formation",
    )
    .unwrap();
    let no_params = HashMap::new();
    let executor = CypherExecutor::with_params(&graph, &no_params, None);
    let result = executor.execute(&q).unwrap();
    assert_eq!(result.rows.len(), 1);
    assert_eq!(result.rows[0].first(), Some(&Value::Boolean(true)));

    // all() — should be false (W2 has no formation)
    let q2 = parser::parse_cypher(
        "MATCH (w:Well) \
         WITH collect(w) AS wells \
         RETURN all(x IN wells WHERE x.formation IS NOT NULL) AS all_have",
    )
    .unwrap();
    let executor2 = CypherExecutor::with_params(&graph, &no_params, None);
    let result2 = executor2.execute(&q2).unwrap();
    assert_eq!(result2.rows.len(), 1);
    assert_eq!(result2.rows[0].first(), Some(&Value::Boolean(false)));
}