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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Lance Authors

//! Vector Operations
//!
//! Helpers for vector similarity search and distance computation

use crate::ast::DistanceMetric;
use crate::error::{GraphError, Result};
use arrow::array::{Array, ArrayRef, FixedSizeListArray, Float32Array, ListArray};

/// Extract vectors from Arrow ListArray or FixedSizeListArray
///
/// Accepts both types for user convenience:
/// - FixedSizeListArray: from Lance datasets or explicit construction
/// - ListArray: from natural table construction with nested lists
pub fn extract_vectors(array: &ArrayRef) -> Result<Vec<Vec<f32>>> {
    // Try FixedSizeListArray first (more common in Lance)
    if let Some(list_array) = array.as_any().downcast_ref::<FixedSizeListArray>() {
        let mut vectors = Vec::with_capacity(list_array.len());
        for i in 0..list_array.len() {
            if list_array.is_null(i) {
                return Err(GraphError::ExecutionError {
                    message: "Null vector in FixedSizeListArray".to_string(),
                    location: snafu::Location::new(file!(), line!(), column!()),
                });
            }
            let value_array = list_array.value(i);
            let float_array = value_array
                .as_any()
                .downcast_ref::<Float32Array>()
                .ok_or_else(|| GraphError::ExecutionError {
                    message: "Expected Float32Array in vector".to_string(),
                    location: snafu::Location::new(file!(), line!(), column!()),
                })?;

            let vec: Vec<f32> = (0..float_array.len())
                .map(|j| float_array.value(j))
                .collect();
            vectors.push(vec);
        }
        return Ok(vectors);
    }

    // Try ListArray (from nested list construction)
    if let Some(list_array) = array.as_any().downcast_ref::<ListArray>() {
        let mut vectors = Vec::with_capacity(list_array.len());
        for i in 0..list_array.len() {
            if list_array.is_null(i) {
                return Err(GraphError::ExecutionError {
                    message: "Null vector in ListArray".to_string(),
                    location: snafu::Location::new(file!(), line!(), column!()),
                });
            }
            let value_array = list_array.value(i);
            let float_array = value_array
                .as_any()
                .downcast_ref::<Float32Array>()
                .ok_or_else(|| GraphError::ExecutionError {
                    message: "Expected Float32Array in vector".to_string(),
                    location: snafu::Location::new(file!(), line!(), column!()),
                })?;

            let vec: Vec<f32> = (0..float_array.len())
                .map(|j| float_array.value(j))
                .collect();
            vectors.push(vec);
        }
        return Ok(vectors);
    }

    Err(GraphError::ExecutionError {
        message: "Expected ListArray or FixedSizeListArray for vector column".to_string(),
        location: snafu::Location::new(file!(), line!(), column!()),
    })
}

/// Extract a single vector from a ScalarValue
/// This avoids allocating a full array when we just need one vector
pub fn extract_single_vector_from_scalar(
    scalar: &datafusion::scalar::ScalarValue,
) -> Result<Vec<f32>> {
    // Convert scalar to a single-element array, then extract
    let array = scalar.to_array().map_err(|e| GraphError::ExecutionError {
        message: format!("Failed to convert scalar to array: {}", e),
        location: snafu::Location::new(file!(), line!(), column!()),
    })?;

    let list_array = array
        .as_any()
        .downcast_ref::<FixedSizeListArray>()
        .ok_or_else(|| GraphError::ExecutionError {
            message: "Expected FixedSizeListArray for vector scalar".to_string(),
            location: snafu::Location::new(file!(), line!(), column!()),
        })?;

    if list_array.is_empty() {
        return Err(GraphError::ExecutionError {
            message: "Empty vector array".to_string(),
            location: snafu::Location::new(file!(), line!(), column!()),
        });
    }

    let value_array = list_array.value(0);
    let float_array = value_array
        .as_any()
        .downcast_ref::<Float32Array>()
        .ok_or_else(|| GraphError::ExecutionError {
            message: "Expected Float32Array in vector".to_string(),
            location: snafu::Location::new(file!(), line!(), column!()),
        })?;

    Ok((0..float_array.len())
        .map(|j| float_array.value(j))
        .collect())
}

/// Compute L2 (Euclidean) distance between two vectors
pub fn l2_distance(a: &[f32], b: &[f32]) -> f32 {
    if a.len() != b.len() {
        // Dimension mismatch - return max distance
        return f32::MAX;
    }

    a.iter()
        .zip(b.iter())
        .map(|(x, y)| (x - y).powi(2))
        .sum::<f32>()
        .sqrt()
}

/// Compute cosine distance (1 - cosine_similarity) between two vectors
/// Returns a value in [0, 2] where 0 means identical and 2 means opposite
pub fn cosine_distance(a: &[f32], b: &[f32]) -> f32 {
    if a.len() != b.len() {
        // Dimension mismatch - return max distance
        return 2.0;
    }

    let dot: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
    let norm_a: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
    let norm_b: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();

    if norm_a == 0.0 || norm_b == 0.0 {
        return 2.0; // Maximum distance for zero vectors
    }

    let similarity = dot / (norm_a * norm_b);
    1.0 - similarity
}

/// Compute cosine similarity (for vector_similarity function)
/// Returns a value in [-1, 1] where 1 means identical and -1 means opposite
pub fn cosine_similarity(a: &[f32], b: &[f32]) -> f32 {
    if a.len() != b.len() {
        // Dimension mismatch - return minimum similarity
        return -1.0;
    }

    let dot: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
    let norm_a: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
    let norm_b: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();

    if norm_a == 0.0 || norm_b == 0.0 {
        return -1.0; // Minimum similarity for zero vectors
    }

    dot / (norm_a * norm_b)
}

/// Compute dot product between two vectors
/// For similarity search, we return the negative (so lower is better for sorting)
pub fn dot_product_distance(a: &[f32], b: &[f32]) -> f32 {
    if a.len() != b.len() {
        // Dimension mismatch - return worst distance to exclude from results
        return f32::MAX;
    }

    -a.iter().zip(b.iter()).map(|(x, y)| x * y).sum::<f32>()
}

/// Compute dot product similarity (for vector_similarity function)
pub fn dot_product_similarity(a: &[f32], b: &[f32]) -> f32 {
    if a.len() != b.len() {
        // Dimension mismatch - return worst similarity to exclude from results
        return f32::MIN;
    }

    a.iter().zip(b.iter()).map(|(x, y)| x * y).sum::<f32>()
}

/// Compute vector distance for an array of vectors against a single query vector
pub fn compute_vector_distances(
    vectors: &[Vec<f32>],
    query_vector: &[f32],
    metric: &DistanceMetric,
) -> Vec<f32> {
    vectors
        .iter()
        .map(|v| match metric {
            DistanceMetric::L2 => l2_distance(v, query_vector),
            DistanceMetric::Cosine => cosine_distance(v, query_vector),
            DistanceMetric::Dot => dot_product_distance(v, query_vector),
        })
        .collect()
}

/// Compute vector similarities for an array of vectors against a single query vector
pub fn compute_vector_similarities(
    vectors: &[Vec<f32>],
    query_vector: &[f32],
    metric: &DistanceMetric,
) -> Vec<f32> {
    vectors
        .iter()
        .map(|v| match metric {
            DistanceMetric::L2 => {
                // For L2, convert distance to similarity (inverse)
                let dist = l2_distance(v, query_vector);
                if dist == 0.0 {
                    1.0 // Perfect match
                } else {
                    1.0 / (1.0 + dist) // Similarity decreases as distance increases
                }
            }
            DistanceMetric::Cosine => cosine_similarity(v, query_vector),
            DistanceMetric::Dot => dot_product_similarity(v, query_vector),
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;

    #[test]
    fn test_l2_distance() {
        let a = vec![1.0, 0.0, 0.0];
        let b = vec![0.0, 1.0, 0.0];
        let dist = l2_distance(&a, &b);
        assert!((dist - 1.414).abs() < 0.01); // sqrt(2)
    }

    #[test]
    fn test_l2_distance_identical() {
        let a = vec![1.0, 2.0, 3.0];
        let b = vec![1.0, 2.0, 3.0];
        let dist = l2_distance(&a, &b);
        assert_eq!(dist, 0.0);
    }

    #[test]
    fn test_cosine_distance() {
        let a = vec![1.0, 0.0, 0.0];
        let b = vec![1.0, 0.0, 0.0];
        let dist = cosine_distance(&a, &b);
        assert_eq!(dist, 0.0); // Identical vectors
    }

    #[test]
    fn test_cosine_distance_orthogonal() {
        let a = vec![1.0, 0.0, 0.0];
        let b = vec![0.0, 1.0, 0.0];
        let dist = cosine_distance(&a, &b);
        assert_eq!(dist, 1.0); // Orthogonal vectors
    }

    #[test]
    fn test_cosine_similarity() {
        let a = vec![1.0, 0.0, 0.0];
        let b = vec![1.0, 0.0, 0.0];
        let sim = cosine_similarity(&a, &b);
        assert_eq!(sim, 1.0); // Identical
    }

    #[test]
    fn test_dot_product() {
        let a = vec![1.0, 2.0, 3.0];
        let b = vec![4.0, 5.0, 6.0];
        let sim = dot_product_similarity(&a, &b);
        assert_eq!(sim, 32.0); // 1*4 + 2*5 + 3*6 = 32
    }

    #[test]
    fn test_dimension_mismatch() {
        let a = vec![1.0, 2.0];
        let b = vec![1.0, 2.0, 3.0];

        let dist = l2_distance(&a, &b);
        assert_eq!(dist, f32::MAX);

        let dist = cosine_distance(&a, &b);
        assert_eq!(dist, 2.0);

        let dist = dot_product_distance(&a, &b);
        assert_eq!(dist, f32::MAX);

        let sim = dot_product_similarity(&a, &b);
        assert_eq!(sim, f32::MIN);
    }

    #[test]
    fn test_extract_single_vector_from_scalar() {
        use arrow::array::FixedSizeListArray;
        use arrow::datatypes::{DataType, Field};
        use datafusion::scalar::ScalarValue;

        // Create a FixedSizeList scalar value with a 3D vector [1.0, 2.0, 3.0]
        let field = Arc::new(Field::new("item", DataType::Float32, true));
        let values = Arc::new(Float32Array::from(vec![1.0, 2.0, 3.0]));
        let list_array = FixedSizeListArray::try_new(field.clone(), 3, values, None).unwrap();

        // Create a scalar from the first element
        let scalar = ScalarValue::try_from_array(&list_array, 0).unwrap();

        // Extract the vector
        let result = extract_single_vector_from_scalar(&scalar);
        assert!(result.is_ok());

        let vec = result.unwrap();
        assert_eq!(vec.len(), 3);
        assert_eq!(vec[0], 1.0);
        assert_eq!(vec[1], 2.0);
        assert_eq!(vec[2], 3.0);
    }

    #[test]
    fn test_extract_single_vector_from_scalar_different_dimensions() {
        use arrow::array::FixedSizeListArray;
        use arrow::datatypes::{DataType, Field};
        use datafusion::scalar::ScalarValue;

        // Create a 5D vector [0.1, 0.2, 0.3, 0.4, 0.5]
        let field = Arc::new(Field::new("item", DataType::Float32, true));
        let values = Arc::new(Float32Array::from(vec![0.1, 0.2, 0.3, 0.4, 0.5]));
        let list_array = FixedSizeListArray::try_new(field.clone(), 5, values, None).unwrap();

        let scalar = ScalarValue::try_from_array(&list_array, 0).unwrap();
        let result = extract_single_vector_from_scalar(&scalar);
        assert!(result.is_ok());

        let vec = result.unwrap();
        assert_eq!(vec.len(), 5);
        assert!((vec[0] - 0.1).abs() < 0.001);
        assert!((vec[4] - 0.5).abs() < 0.001);
    }

    #[test]
    fn test_compute_vector_distances_broadcast() {
        // Test that compute_vector_distances properly broadcasts a single query vector
        let data_vectors = vec![
            vec![1.0, 0.0, 0.0],
            vec![0.0, 1.0, 0.0],
            vec![0.0, 0.0, 1.0],
        ];
        let query_vector = vec![1.0, 0.0, 0.0];

        let distances = compute_vector_distances(&data_vectors, &query_vector, &DistanceMetric::L2);

        assert_eq!(distances.len(), 3);
        assert_eq!(distances[0], 0.0); // Same as query
        assert!((distances[1] - 1.414).abs() < 0.01); // Orthogonal
        assert!((distances[2] - 1.414).abs() < 0.01); // Orthogonal
    }

    #[test]
    fn test_compute_vector_similarities_broadcast() {
        // Test that compute_vector_similarities properly broadcasts a single query vector
        let data_vectors = vec![
            vec![1.0, 0.0, 0.0],
            vec![0.0, 1.0, 0.0],
            vec![0.5, 0.5, 0.0], // 45 degrees from x-axis
        ];
        let query_vector = vec![1.0, 0.0, 0.0];

        let similarities =
            compute_vector_similarities(&data_vectors, &query_vector, &DistanceMetric::Cosine);

        assert_eq!(similarities.len(), 3);
        assert_eq!(similarities[0], 1.0); // Same as query
        assert_eq!(similarities[1], 0.0); // Orthogonal
        assert!((similarities[2] - 0.707).abs() < 0.01); // cos(45°) ≈ 0.707
    }

    #[test]
    fn test_extract_vectors_from_fixed_size_list() {
        use arrow::datatypes::{DataType, Field};

        // Create FixedSizeListArray with 3D vectors
        let field = Arc::new(Field::new("item", DataType::Float32, true));
        let values = Arc::new(Float32Array::from(vec![
            1.0, 0.0, 0.0, // Vector 1
            0.0, 1.0, 0.0, // Vector 2
            0.0, 0.0, 1.0, // Vector 3
        ]));
        let list_array = FixedSizeListArray::try_new(field, 3, values, None).unwrap();
        let array_ref: ArrayRef = Arc::new(list_array);

        let result = extract_vectors(&array_ref);
        assert!(result.is_ok());

        let vectors = result.unwrap();
        assert_eq!(vectors.len(), 3);
        assert_eq!(vectors[0], vec![1.0, 0.0, 0.0]);
        assert_eq!(vectors[1], vec![0.0, 1.0, 0.0]);
        assert_eq!(vectors[2], vec![0.0, 0.0, 1.0]);
    }

    #[test]
    fn test_extract_vectors_from_list_array() {
        use arrow::array::ListBuilder;

        // Create ListArray with variable-length vectors (though we use same length)
        let values_builder = Float32Array::builder(9);
        let mut list_builder = ListBuilder::new(values_builder);

        // Add first vector [1.0, 0.0, 0.0]
        list_builder.values().append_value(1.0);
        list_builder.values().append_value(0.0);
        list_builder.values().append_value(0.0);
        list_builder.append(true);

        // Add second vector [0.0, 1.0, 0.0]
        list_builder.values().append_value(0.0);
        list_builder.values().append_value(1.0);
        list_builder.values().append_value(0.0);
        list_builder.append(true);

        // Add third vector [0.5, 0.5, 0.0]
        list_builder.values().append_value(0.5);
        list_builder.values().append_value(0.5);
        list_builder.values().append_value(0.0);
        list_builder.append(true);

        let list_array = list_builder.finish();
        let array_ref: ArrayRef = Arc::new(list_array);

        let result = extract_vectors(&array_ref);
        assert!(result.is_ok());

        let vectors = result.unwrap();
        assert_eq!(vectors.len(), 3);
        assert_eq!(vectors[0], vec![1.0, 0.0, 0.0]);
        assert_eq!(vectors[1], vec![0.0, 1.0, 0.0]);
        assert_eq!(vectors[2], vec![0.5, 0.5, 0.0]);
    }

    #[test]
    fn test_extract_vectors_rejects_invalid_type() {
        // Test that extract_vectors rejects non-list arrays
        let float_array = Float32Array::from(vec![1.0, 2.0, 3.0]);
        let array_ref: ArrayRef = Arc::new(float_array);

        let result = extract_vectors(&array_ref);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Expected ListArray or FixedSizeListArray"));
    }

    #[test]
    fn test_extract_vectors_rejects_null_in_fixed_size_list() {
        use arrow::datatypes::{DataType, Field};

        // Create FixedSizeListArray with a null vector
        let field = Arc::new(Field::new("item", DataType::Float32, true));
        let values = Arc::new(Float32Array::from(vec![
            1.0, 0.0, 0.0, // Vector 1
            0.0, 1.0, 0.0, // Vector 2 (will be null)
            0.0, 0.0, 1.0, // Vector 3
        ]));
        let null_buffer = arrow::buffer::NullBuffer::from(vec![true, false, true]);
        let list_array = FixedSizeListArray::try_new(field, 3, values, Some(null_buffer)).unwrap();
        let array_ref: ArrayRef = Arc::new(list_array);

        let result = extract_vectors(&array_ref);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Null vector in FixedSizeListArray"));
    }
}