lance-graph 0.5.4

Graph query engine for Lance datasets with Cypher support
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Lance Authors

//! End-to-end integration tests for vector similarity search

use arrow_array::{Array, FixedSizeListArray, Float32Array, Int64Array, RecordBatch, StringArray};
use arrow_schema::{DataType, Field, FieldRef, Schema};
use lance_graph::config::GraphConfig;
use lance_graph::{CypherQuery, ExecutionStrategy, Result};
use std::collections::HashMap;
use std::sync::Arc;

/// Helper function to create a test graph with vector embeddings
fn create_person_graph_with_embeddings() -> (GraphConfig, HashMap<String, RecordBatch>) {
    // Create schema with 3D embeddings for simplicity
    let schema = Arc::new(Schema::new(vec![
        Field::new("id", DataType::Int64, false),
        Field::new("name", DataType::Utf8, false),
        Field::new("age", DataType::Int64, false),
        Field::new(
            "embedding",
            DataType::FixedSizeList(
                Arc::new(Field::new("item", DataType::Float32, true)),
                3, // 3-dimensional vectors for testing
            ),
            false,
        ),
    ]));

    // Create test data with embeddings
    // Person vectors are chosen to have clear similarity relationships:
    // - Alice [1, 0, 0] and Bob [0.9, 0.1, 0] are very similar
    // - Carol [0, 1, 0] is orthogonal to Alice
    // - David [0, 0, 1] is orthogonal to both Alice and Carol
    // - Eve [0.5, 0.5, 0] is in between Alice and Carol
    let embedding_data = vec![
        1.0, 0.0, 0.0, // Alice
        0.9, 0.1, 0.0, // Bob
        0.0, 1.0, 0.0, // Carol
        0.0, 0.0, 1.0, // David
        0.5, 0.5, 0.0, // Eve
    ];

    // Create FixedSizeListArray using standard Arrow API
    let field = Arc::new(Field::new("item", DataType::Float32, true)) as FieldRef;
    let values = Arc::new(Float32Array::from(embedding_data));
    let embeddings = FixedSizeListArray::try_new(field, 3, values, None).unwrap();

    let batch = RecordBatch::try_new(
        schema,
        vec![
            Arc::new(Int64Array::from(vec![1, 2, 3, 4, 5])),
            Arc::new(StringArray::from(vec![
                "Alice", "Bob", "Carol", "David", "Eve",
            ])),
            Arc::new(Int64Array::from(vec![30, 25, 35, 28, 32])),
            Arc::new(embeddings),
        ],
    )
    .unwrap();

    let config = GraphConfig::builder()
        .with_node_label("Person", "id")
        .build()
        .unwrap();

    let mut datasets = HashMap::new();
    datasets.insert("Person".to_string(), batch);

    (config, datasets)
}

#[tokio::test]
async fn test_vector_distance_l2_simple() -> Result<()> {
    let (config, datasets) = create_person_graph_with_embeddings();

    // Simpler test: just return vector distance in SELECT (not in WHERE)
    // Compare each person's embedding to Alice's
    let query = CypherQuery::new(
        "MATCH (p:Person), (alice:Person {name: 'Alice'}) \
         RETURN p.name, vector_distance(p.embedding, alice.embedding, l2) AS dist \
         ORDER BY dist",
    )?
    .with_config(config);

    let result = query
        .execute(datasets, Some(ExecutionStrategy::DataFusion))
        .await?;

    // Should return all 5 people ordered by distance to Alice
    assert_eq!(result.num_rows(), 5);
    let names = result
        .column(0)
        .as_any()
        .downcast_ref::<StringArray>()
        .unwrap();

    // Alice should be first (distance=0)
    assert_eq!(names.value(0), "Alice");

    Ok(())
}

#[tokio::test]
async fn test_vector_distance_where_no_cross_product() -> Result<()> {
    let (config, datasets) = create_person_graph_with_embeddings();

    // Test WHERE clause without cross product - self-comparison
    let query = CypherQuery::new(
        "MATCH (p:Person) \
         WHERE vector_distance(p.embedding, p.embedding, l2) < 0.1 \
         RETURN p.name",
    )?
    .with_config(config);

    let result = query
        .execute(datasets, Some(ExecutionStrategy::DataFusion))
        .await?;

    // Self-distance is always 0, so all should match
    assert_eq!(result.num_rows(), 5);

    Ok(())
}

#[tokio::test]
async fn test_vector_distance_l2() -> Result<()> {
    let (config, datasets) = create_person_graph_with_embeddings();

    // Find people with L2 distance < 0.5 from Alice (should find Bob and Alice herself)
    let query = CypherQuery::new(
        "MATCH (p:Person), (alice:Person {name: 'Alice'}) \
         WHERE vector_distance(p.embedding, alice.embedding, l2) < 0.5 \
         RETURN p.name \
         ORDER BY p.name",
    )?
    .with_config(config);

    let result = query
        .execute(datasets, Some(ExecutionStrategy::DataFusion))
        .await?;

    // Should return Alice (distance=0) and Bob (distance≈0.14)
    assert_eq!(result.num_rows(), 2);
    let names = result
        .column(0)
        .as_any()
        .downcast_ref::<StringArray>()
        .unwrap();

    assert_eq!(names.value(0), "Alice");
    assert_eq!(names.value(1), "Bob");

    Ok(())
}

#[tokio::test]
async fn test_vector_distance_l2_with_literal() -> Result<()> {
    let (config, datasets) = create_person_graph_with_embeddings();

    // Same test as above but using vector literal instead of cross product
    // Find people similar to [1, 0, 0] (Alice's embedding)
    let query = CypherQuery::new(
        "MATCH (p:Person) \
         WHERE vector_distance(p.embedding, [1.0, 0.0, 0.0], l2) < 0.2 \
         RETURN p.name ORDER BY p.name",
    )?
    .with_config(config);

    let result = query
        .execute(datasets, Some(ExecutionStrategy::DataFusion))
        .await?;

    // Should return Alice (exact match) and Bob (very similar)
    assert_eq!(result.num_rows(), 2);
    let names = result
        .column(0)
        .as_any()
        .downcast_ref::<StringArray>()
        .unwrap();
    assert_eq!(names.value(0), "Alice");
    assert_eq!(names.value(1), "Bob");

    Ok(())
}

#[tokio::test]
async fn test_vector_distance_cosine() -> Result<()> {
    let (config, datasets) = create_person_graph_with_embeddings();

    // Find people with cosine distance < 0.1 from Alice
    // Cosine distance between Alice [1,0,0] and Bob [0.9,0.1,0] ≈ 0.005
    let query = CypherQuery::new(
        "MATCH (p:Person), (alice:Person {name: 'Alice'}) \
         WHERE vector_distance(p.embedding, alice.embedding, cosine) < 0.1 \
         RETURN p.name \
         ORDER BY p.name",
    )?
    .with_config(config);

    let result = query
        .execute(datasets, Some(ExecutionStrategy::DataFusion))
        .await?;

    assert_eq!(result.num_rows(), 2);
    let names = result
        .column(0)
        .as_any()
        .downcast_ref::<StringArray>()
        .unwrap();

    assert_eq!(names.value(0), "Alice");
    assert_eq!(names.value(1), "Bob");

    Ok(())
}

#[tokio::test]
async fn test_vector_similarity_cosine() -> Result<()> {
    let (config, datasets) = create_person_graph_with_embeddings();

    // Find people with cosine similarity > 0.9 to Alice
    let query = CypherQuery::new(
        "MATCH (p:Person), (alice:Person {name: 'Alice'}) \
         WHERE vector_similarity(p.embedding, alice.embedding, cosine) > 0.9 \
         RETURN p.name \
         ORDER BY p.name",
    )?
    .with_config(config);

    let result = query
        .execute(datasets, Some(ExecutionStrategy::DataFusion))
        .await?;

    // Alice has similarity 1.0, Bob has similarity ≈ 0.995
    assert_eq!(result.num_rows(), 2);
    let names = result
        .column(0)
        .as_any()
        .downcast_ref::<StringArray>()
        .unwrap();

    assert_eq!(names.value(0), "Alice");
    assert_eq!(names.value(1), "Bob");

    Ok(())
}

#[tokio::test]
async fn test_vector_distance_order_by() -> Result<()> {
    let (config, datasets) = create_person_graph_with_embeddings();

    // Order all people by distance to Alice
    let query = CypherQuery::new(
        "MATCH (p:Person), (alice:Person {name: 'Alice'}) \
         RETURN p.name, vector_distance(p.embedding, alice.embedding, l2) AS dist \
         ORDER BY dist ASC",
    )?
    .with_config(config);

    let result = query
        .execute(datasets, Some(ExecutionStrategy::DataFusion))
        .await?;

    assert_eq!(result.num_rows(), 5);
    let names = result
        .column(0)
        .as_any()
        .downcast_ref::<StringArray>()
        .unwrap();

    let distances = result
        .column(1)
        .as_any()
        .downcast_ref::<Float32Array>()
        .unwrap();

    // Alice should be first (distance=0)
    assert_eq!(names.value(0), "Alice");
    assert_eq!(distances.value(0), 0.0);

    // Bob should be second (closest to Alice)
    assert_eq!(names.value(1), "Bob");
    assert!(distances.value(1) < 0.2);

    // Distances should be in ascending order
    for i in 1..distances.len() {
        assert!(distances.value(i) >= distances.value(i - 1));
    }

    Ok(())
}

#[tokio::test]
async fn test_vector_distance_order_by_with_literal() -> Result<()> {
    let (config, datasets) = create_person_graph_with_embeddings();

    // Same as above but using vector literal - order by distance to [1,0,0] (Alice's vector)
    let query = CypherQuery::new(
        "MATCH (p:Person) \
         RETURN p.name \
         ORDER BY vector_distance(p.embedding, [1.0, 0.0, 0.0], cosine) ASC \
         LIMIT 3",
    )?
    .with_config(config);

    let result = query
        .execute(datasets, Some(ExecutionStrategy::DataFusion))
        .await?;

    // Should return Alice (closest), Bob (second), Eve (third)
    assert_eq!(result.num_rows(), 3);
    let names = result
        .column(0)
        .as_any()
        .downcast_ref::<StringArray>()
        .unwrap();
    assert_eq!(names.value(0), "Alice");
    assert_eq!(names.value(1), "Bob");
    assert_eq!(names.value(2), "Eve");

    Ok(())
}

#[tokio::test]
async fn test_vector_similarity_order_by() -> Result<()> {
    let (config, datasets) = create_person_graph_with_embeddings();

    // Order all people by similarity to Carol [0,1,0]
    let query = CypherQuery::new(
        "MATCH (p:Person), (carol:Person {name: 'Carol'}) \
         RETURN p.name, vector_similarity(p.embedding, carol.embedding, cosine) AS sim \
         ORDER BY sim DESC \
         LIMIT 3",
    )?
    .with_config(config);

    let result = query
        .execute(datasets, Some(ExecutionStrategy::DataFusion))
        .await?;

    assert_eq!(result.num_rows(), 3);
    let names = result
        .column(0)
        .as_any()
        .downcast_ref::<StringArray>()
        .unwrap();

    let similarities = result
        .column(1)
        .as_any()
        .downcast_ref::<Float32Array>()
        .unwrap();

    // Carol should be first (similarity=1.0)
    assert_eq!(names.value(0), "Carol");
    assert!((similarities.value(0) - 1.0).abs() < 0.01);

    // Eve [0.5, 0.5, 0] should be second (has y component)
    assert_eq!(names.value(1), "Eve");

    // Similarities should be in descending order
    for i in 1..similarities.len() {
        assert!(similarities.value(i) <= similarities.value(i - 1));
    }

    Ok(())
}

#[tokio::test]
async fn test_hybrid_query_property_and_vector() -> Result<()> {
    let (config, datasets) = create_person_graph_with_embeddings();

    // Find people over 26 who are similar to Alice
    let query = CypherQuery::new(
        "MATCH (p:Person), (alice:Person {name: 'Alice'}) \
         WHERE p.age > 26 \
           AND vector_distance(p.embedding, alice.embedding, l2) < 1.0 \
         RETURN p.name, p.age \
         ORDER BY p.name",
    )?
    .with_config(config);

    let result = query
        .execute(datasets, Some(ExecutionStrategy::DataFusion))
        .await?;

    // Should find Alice (age=30) and Eve (age=32)
    // Bob is excluded (age=25), Carol and David are too far
    assert!(result.num_rows() >= 1); // At least Alice
    let names = result
        .column(0)
        .as_any()
        .downcast_ref::<StringArray>()
        .unwrap();

    // Alice should be in results
    let alice_found = (0..names.len()).any(|i| names.value(i) == "Alice");
    assert!(alice_found);

    // Verify all results meet age criteria
    let ages = result
        .column(1)
        .as_any()
        .downcast_ref::<Int64Array>()
        .unwrap();
    for i in 0..ages.len() {
        assert!(ages.value(i) > 26);
    }

    Ok(())
}

#[tokio::test]
async fn test_hybrid_query_with_vector_literal() -> Result<()> {
    let (config, datasets) = create_person_graph_with_embeddings();

    // Combine property filter with vector literal search
    let query = CypherQuery::new(
        "MATCH (p:Person) \
         WHERE p.age > 25 \
           AND vector_distance(p.embedding, [1.0, 0.0, 0.0], l2) < 0.3 \
         RETURN p.name ORDER BY p.name",
    )?
    .with_config(config);

    let result = query
        .execute(datasets, Some(ExecutionStrategy::DataFusion))
        .await?;

    // Should return only Alice (age 30 > 25, close to [1,0,0])
    // Bob is age 25, not > 25
    assert_eq!(result.num_rows(), 1);
    let names = result
        .column(0)
        .as_any()
        .downcast_ref::<StringArray>()
        .unwrap();
    assert_eq!(names.value(0), "Alice");

    Ok(())
}

#[tokio::test]
async fn test_vector_distance_dot_product() -> Result<()> {
    let (config, datasets) = create_person_graph_with_embeddings();

    // Test dot product metric
    // Dot product between Alice [1,0,0] and Bob [0.9,0.1,0] = 0.9
    // We negate it for distance, so distance = -0.9
    let query = CypherQuery::new(
        "MATCH (alice:Person {name: 'Alice'}), (bob:Person {name: 'Bob'}) \
         RETURN vector_distance(alice.embedding, bob.embedding, dot) AS dist",
    )?
    .with_config(config);

    let result = query
        .execute(datasets, Some(ExecutionStrategy::DataFusion))
        .await?;

    assert_eq!(result.num_rows(), 1);
    let distance = result
        .column(0)
        .as_any()
        .downcast_ref::<Float32Array>()
        .unwrap()
        .value(0);

    // Distance should be negative of dot product: -0.9
    assert!((distance + 0.9).abs() < 0.01);

    Ok(())
}

#[tokio::test]
async fn test_vector_similarity_dot_product() -> Result<()> {
    let (config, datasets) = create_person_graph_with_embeddings();

    // Test dot product similarity
    let query = CypherQuery::new(
        "MATCH (alice:Person {name: 'Alice'}), (bob:Person {name: 'Bob'}) \
         RETURN vector_similarity(alice.embedding, bob.embedding, dot) AS sim",
    )?
    .with_config(config);

    let result = query
        .execute(datasets, Some(ExecutionStrategy::DataFusion))
        .await?;

    assert_eq!(result.num_rows(), 1);
    let similarity = result
        .column(0)
        .as_any()
        .downcast_ref::<Float32Array>()
        .unwrap()
        .value(0);

    // Similarity should be positive dot product: 0.9
    assert!((similarity - 0.9).abs() < 0.01);

    Ok(())
}

#[tokio::test]
async fn test_vector_search_with_limit() -> Result<()> {
    let (config, datasets) = create_person_graph_with_embeddings();

    // Find top 2 most similar people to Alice (excluding herself)
    let query = CypherQuery::new(
        "MATCH (p:Person), (alice:Person {name: 'Alice'}) \
         WHERE p.name <> 'Alice' \
         RETURN p.name, vector_distance(p.embedding, alice.embedding, cosine) AS dist \
         ORDER BY dist ASC \
         LIMIT 2",
    )?
    .with_config(config);

    let result = query
        .execute(datasets, Some(ExecutionStrategy::DataFusion))
        .await?;

    assert_eq!(result.num_rows(), 2);
    let names = result
        .column(0)
        .as_any()
        .downcast_ref::<StringArray>()
        .unwrap();

    // Bob should be first (most similar to Alice)
    assert_eq!(names.value(0), "Bob");

    Ok(())
}

#[tokio::test]
async fn test_vector_distance_between_different_people() -> Result<()> {
    let (config, datasets) = create_person_graph_with_embeddings();

    // Compute distance between Carol and David (orthogonal vectors)
    let query = CypherQuery::new(
        "MATCH (carol:Person {name: 'Carol'}), (david:Person {name: 'David'}) \
         RETURN vector_distance(carol.embedding, david.embedding, cosine) AS dist",
    )?
    .with_config(config);

    let result = query
        .execute(datasets, Some(ExecutionStrategy::DataFusion))
        .await?;

    assert_eq!(result.num_rows(), 1);
    let distance = result
        .column(0)
        .as_any()
        .downcast_ref::<Float32Array>()
        .unwrap()
        .value(0);

    // Carol [0,1,0] and David [0,0,1] are orthogonal
    // Cosine distance should be 1.0
    assert!((distance - 1.0).abs() < 0.01);

    Ok(())
}

#[tokio::test]
async fn test_multiple_vector_comparisons() -> Result<()> {
    let (config, datasets) = create_person_graph_with_embeddings();

    // Find people similar to either Alice or Carol
    let query = CypherQuery::new(
        "MATCH (p:Person), (alice:Person {name: 'Alice'}), (carol:Person {name: 'Carol'}) \
         WHERE vector_distance(p.embedding, alice.embedding, l2) < 0.3 \
            OR vector_distance(p.embedding, carol.embedding, l2) < 0.3 \
         RETURN DISTINCT p.name \
         ORDER BY p.name",
    )?
    .with_config(config);

    let result = query
        .execute(datasets, Some(ExecutionStrategy::DataFusion))
        .await?;

    // Should find at least Alice, Bob, and Carol
    assert!(result.num_rows() >= 3);
    let names = result
        .column(0)
        .as_any()
        .downcast_ref::<StringArray>()
        .unwrap();

    let name_vec: Vec<&str> = (0..names.len()).map(|i| names.value(i)).collect();

    assert!(name_vec.contains(&"Alice"));
    assert!(name_vec.contains(&"Bob"));
    assert!(name_vec.contains(&"Carol"));

    Ok(())
}

#[tokio::test]
async fn test_vector_similarity_l2_conversion() -> Result<()> {
    let (config, datasets) = create_person_graph_with_embeddings();

    // Test L2 similarity (converted from distance: 1/(1+dist))
    let query = CypherQuery::new(
        "MATCH (alice:Person {name: 'Alice'}), (bob:Person {name: 'Bob'}) \
         RETURN vector_similarity(alice.embedding, bob.embedding, l2) AS sim",
    )?
    .with_config(config);

    let result = query
        .execute(datasets, Some(ExecutionStrategy::DataFusion))
        .await?;

    assert_eq!(result.num_rows(), 1);
    let similarity = result
        .column(0)
        .as_any()
        .downcast_ref::<Float32Array>()
        .unwrap()
        .value(0);

    // L2 distance between Alice and Bob ≈ 0.14
    // Similarity = 1/(1+0.14) ≈ 0.877
    assert!(similarity > 0.8 && similarity < 1.0);

    Ok(())
}

#[tokio::test]
async fn test_vector_search_with_aggregation() -> Result<()> {
    let (config, datasets) = create_person_graph_with_embeddings();

    // Count how many people are similar to Alice (distance < 0.5)
    let query = CypherQuery::new(
        "MATCH (p:Person), (alice:Person {name: 'Alice'}) \
         WHERE vector_distance(p.embedding, alice.embedding, l2) < 0.5 \
         RETURN count(p) AS similar_count",
    )?
    .with_config(config);

    let result = query
        .execute(datasets, Some(ExecutionStrategy::DataFusion))
        .await?;

    assert_eq!(result.num_rows(), 1);
    let count = result
        .column(0)
        .as_any()
        .downcast_ref::<Int64Array>()
        .unwrap()
        .value(0);

    // Should find Alice and Bob
    assert_eq!(count, 2);

    Ok(())
}

#[tokio::test]
async fn test_vector_distance_self_comparison() -> Result<()> {
    let (config, datasets) = create_person_graph_with_embeddings();

    // Vector distance from a person to themselves should be 0
    let query = CypherQuery::new(
        "MATCH (p:Person {name: 'Carol'}) \
         RETURN vector_distance(p.embedding, p.embedding, l2) AS dist",
    )?
    .with_config(config);

    let result = query
        .execute(datasets, Some(ExecutionStrategy::DataFusion))
        .await?;

    assert_eq!(result.num_rows(), 1);
    let distance = result
        .column(0)
        .as_any()
        .downcast_ref::<Float32Array>()
        .unwrap()
        .value(0);

    assert_eq!(distance, 0.0);

    Ok(())
}

#[tokio::test]
async fn test_vector_similarity_self_comparison() -> Result<()> {
    let (config, datasets) = create_person_graph_with_embeddings();

    // Vector similarity from a person to themselves should be 1.0 (for cosine)
    let query = CypherQuery::new(
        "MATCH (p:Person {name: 'David'}) \
         RETURN vector_similarity(p.embedding, p.embedding, cosine) AS sim",
    )?
    .with_config(config);

    let result = query
        .execute(datasets, Some(ExecutionStrategy::DataFusion))
        .await?;

    assert_eq!(result.num_rows(), 1);
    let similarity = result
        .column(0)
        .as_any()
        .downcast_ref::<Float32Array>()
        .unwrap()
        .value(0);

    assert!((similarity - 1.0).abs() < 0.001);

    Ok(())
}

#[tokio::test]
async fn test_vector_literal_in_return_clause() -> Result<()> {
    let (config, datasets) = create_person_graph_with_embeddings();

    // Use vector literal in RETURN to compute distances
    let query = CypherQuery::new(
        "MATCH (p:Person) \
         RETURN p.name, vector_distance(p.embedding, [0.5, 0.5, 0.0], l2) AS dist \
         ORDER BY dist ASC \
         LIMIT 1",
    )?
    .with_config(config);

    let result = query
        .execute(datasets, Some(ExecutionStrategy::DataFusion))
        .await?;

    // Should return Eve (closest to [0.5, 0.5, 0.0])
    assert_eq!(result.num_rows(), 1);
    let names = result
        .column(0)
        .as_any()
        .downcast_ref::<StringArray>()
        .unwrap();
    assert_eq!(names.value(0), "Eve");

    // Distance should be 0 (exact match)
    let distances = result
        .column(1)
        .as_any()
        .downcast_ref::<Float32Array>()
        .unwrap();
    assert!(distances.value(0) < 0.001);

    Ok(())
}