lance-arrow-scalar 58.0.0

Arrow scalar type with Ord, Hash, and Eq 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Lance Authors

//! A scalar type backed by a single-element Arrow array with [`Ord`], [`Hash`],
//! and [`Eq`] support.
//!
//! Comparisons and hashing are delegated to [`arrow_row::OwnedRow`], which
//! provides a correct total ordering for all Arrow types (including proper NaN
//! handling for floats and null ordering).

mod convert;
pub mod serde;

use std::cmp::Ordering;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::sync::Arc;

use arrow_array::{ArrayRef, make_array, new_null_array};
use arrow_cast::display::ArrayFormatter;
use arrow_data::transform::MutableArrayData;
use arrow_row::{OwnedRow, RowConverter, SortField};
use arrow_schema::{ArrowError, DataType};

type Result<T> = std::result::Result<T, ArrowError>;

/// A scalar value backed by a length-1 Arrow array.
///
/// `ArrowScalar` provides [`Eq`], [`Ord`], and [`Hash`] by caching an
/// [`OwnedRow`] at construction time. This means comparisons and hashing are
/// O(1) row-byte operations rather than per-type dispatch.
///
/// # Cross-type comparison
///
/// Comparing scalars of different data types produces an arbitrary but
/// consistent ordering based on the underlying row bytes. This is intentional
/// — it allows scalars to be used as keys in sorted collections regardless of
/// type, but the ordering across types is not semantically meaningful.
///
/// # Examples
///
/// ```
/// use lance_arrow_scalar::ArrowScalar;
///
/// let a = ArrowScalar::from(1i32);
/// let b = ArrowScalar::from(2i32);
/// assert!(a < b);
///
/// let c = ArrowScalar::from("hello");
/// assert_eq!(c, ArrowScalar::from("hello"));
/// ```
pub struct ArrowScalar {
    array: ArrayRef,
    row: OwnedRow,
}

impl ArrowScalar {
    /// Create a scalar by extracting the element at `offset` from `array`.
    pub fn try_new(array: &ArrayRef, offset: usize) -> Result<Self> {
        if offset >= array.len() {
            return Err(ArrowError::InvalidArgumentError(
                "Scalar index out of bounds".to_string(),
            ));
        }

        let data = array.to_data();
        let mut mutable = MutableArrayData::new(vec![&data], true, 1);
        mutable.extend(0, offset, offset + 1);
        let single = make_array(mutable.freeze());
        Self::try_from_array(single)
    }

    /// Create a scalar from a length-1 array.
    pub fn try_from_array(array: ArrayRef) -> Result<Self> {
        if array.len() != 1 {
            return Err(ArrowError::InvalidArgumentError(format!(
                "ArrowScalar requires a length-1 array, got length {}",
                array.len()
            )));
        }

        let row = Self::compute_row(&array)?;
        Ok(Self { array, row })
    }

    /// Create a null scalar of the given data type.
    pub fn new_null(data_type: &DataType) -> Result<Self> {
        Self::try_from_array(new_null_array(data_type, 1))
    }

    fn compute_row(array: &ArrayRef) -> Result<OwnedRow> {
        let sort_field = SortField::new(array.data_type().clone());
        let converter = RowConverter::new(vec![sort_field])?;
        let rows = converter.convert_columns(&[Arc::clone(array)])?;
        Ok(rows.row(0).owned())
    }

    /// Returns a reference to the underlying length-1 array.
    pub fn as_array(&self) -> &ArrayRef {
        &self.array
    }

    /// Returns the data type of this scalar.
    pub fn data_type(&self) -> &DataType {
        self.array.data_type()
    }

    /// Returns `true` if this scalar is null.
    pub fn is_null(&self) -> bool {
        self.array.null_count() == 1
    }
}

impl PartialEq for ArrowScalar {
    fn eq(&self, other: &Self) -> bool {
        self.row == other.row
    }
}

impl Eq for ArrowScalar {}

impl PartialOrd for ArrowScalar {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for ArrowScalar {
    fn cmp(&self, other: &Self) -> Ordering {
        self.row.cmp(&other.row)
    }
}

impl Hash for ArrowScalar {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.row.hash(state);
    }
}

impl fmt::Display for ArrowScalar {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.is_null() {
            return write!(f, "null");
        }
        let formatter =
            ArrayFormatter::try_new(&self.array, &Default::default()).map_err(|_| fmt::Error)?;
        write!(f, "{}", formatter.value(0))
    }
}

impl fmt::Debug for ArrowScalar {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "ArrowScalar({}: {})", self.data_type(), self)
    }
}

impl Clone for ArrowScalar {
    fn clone(&self) -> Self {
        Self {
            array: Arc::clone(&self.array),
            row: self.row.clone(),
        }
    }
}

#[cfg(test)]
mod tests {
    use std::collections::{BTreeSet, HashSet};
    use std::sync::Arc;

    use arrow_array::*;
    use rstest::rstest;

    use super::*;

    #[test]
    fn test_try_new_extracts_element() {
        let array: ArrayRef = Arc::new(Int32Array::from(vec![10, 20, 30]));
        let s = ArrowScalar::try_new(&array, 1).unwrap();
        assert_eq!(format!("{s}"), "20");
    }

    #[test]
    fn test_try_new_out_of_bounds() {
        let array: ArrayRef = Arc::new(Int32Array::from(vec![1]));
        assert!(ArrowScalar::try_new(&array, 5).is_err());
    }

    #[test]
    fn test_try_from_array_wrong_length() {
        let array: ArrayRef = Arc::new(Int32Array::from(vec![1, 2]));
        assert!(ArrowScalar::try_from_array(array).is_err());
    }

    #[test]
    fn test_equality() {
        let a = ArrowScalar::from(42i32);
        let b = ArrowScalar::from(42i32);
        let c = ArrowScalar::from(99i32);
        assert_eq!(a, b);
        assert_ne!(a, c);
    }

    #[test]
    fn test_ordering() {
        let a = ArrowScalar::from(1i32);
        let b = ArrowScalar::from(2i32);
        let c = ArrowScalar::from(3i32);
        assert!(a < b);
        assert!(b < c);
        assert_eq!(a.cmp(&a), Ordering::Equal);
    }

    #[test]
    fn test_hash_consistent_with_eq() {
        use std::hash::DefaultHasher;

        let a = ArrowScalar::from(42i32);
        let b = ArrowScalar::from(42i32);
        let hash_a = {
            let mut h = DefaultHasher::new();
            a.hash(&mut h);
            h.finish()
        };
        let hash_b = {
            let mut h = DefaultHasher::new();
            b.hash(&mut h);
            h.finish()
        };
        assert_eq!(hash_a, hash_b);
    }

    #[test]
    fn test_in_hashset() {
        let mut set = HashSet::new();
        set.insert(ArrowScalar::from(1i32));
        set.insert(ArrowScalar::from(2i32));
        set.insert(ArrowScalar::from(1i32));
        assert_eq!(set.len(), 2);
    }

    #[test]
    fn test_in_btreeset() {
        let mut set = BTreeSet::new();
        set.insert(ArrowScalar::from(3i32));
        set.insert(ArrowScalar::from(1i32));
        set.insert(ArrowScalar::from(2i32));
        let values: Vec<_> = set.iter().map(|s| format!("{s}")).collect();
        assert_eq!(values, vec!["1", "2", "3"]);
    }

    #[test]
    fn test_null_scalar() {
        let array: ArrayRef = Arc::new(Int32Array::from(vec![None]));
        let s = ArrowScalar::try_from_array(array).unwrap();
        assert!(s.is_null());
        assert_eq!(format!("{s}"), "null");
    }

    #[test]
    fn test_null_sorts_first() {
        let null_scalar = {
            let array: ArrayRef = Arc::new(Int32Array::from(vec![None]));
            ArrowScalar::try_from_array(array).unwrap()
        };
        let value_scalar = ArrowScalar::from(0i32);
        assert!(null_scalar < value_scalar);
    }

    #[rstest]
    #[case::float_nan(
        ArrowScalar::from(f64::NAN),
        ArrowScalar::from(f64::INFINITY),
        Ordering::Greater
    )]
    #[case::float_normal(ArrowScalar::from(1.0f64), ArrowScalar::from(2.0f64), Ordering::Less)]
    fn test_float_ordering(
        #[case] a: ArrowScalar,
        #[case] b: ArrowScalar,
        #[case] expected: Ordering,
    ) {
        assert_eq!(a.cmp(&b), expected);
    }

    #[test]
    fn test_display_string() {
        let s = ArrowScalar::from("hello world");
        assert_eq!(format!("{s}"), "hello world");
    }

    #[test]
    fn test_debug() {
        let s = ArrowScalar::from(42i32);
        let debug = format!("{s:?}");
        assert!(debug.contains("ArrowScalar"));
        assert!(debug.contains("42"));
    }

    #[test]
    fn test_clone() {
        let a = ArrowScalar::from(42i32);
        let b = a.clone();
        assert_eq!(a, b);
    }

    #[test]
    fn test_data_type() {
        let s = ArrowScalar::from(42i32);
        assert_eq!(s.data_type(), &DataType::Int32);
    }

    #[test]
    fn test_boolean_roundtrip() {
        let t = ArrowScalar::from(true);
        let f = ArrowScalar::from(false);
        assert_eq!(t.data_type(), &DataType::Boolean);
        assert!(!t.is_null());
        assert_eq!(format!("{t}"), "true");
        assert_eq!(format!("{f}"), "false");

        // Extract from multi-element array
        let array: ArrayRef = Arc::new(BooleanArray::from(vec![true, false, true]));
        let s = ArrowScalar::try_new(&array, 1).unwrap();
        assert_eq!(format!("{s}"), "false");
        assert_eq!(s.data_type(), &DataType::Boolean);
    }

    #[test]
    fn test_boolean_equality_and_ordering() {
        let t1 = ArrowScalar::from(true);
        let t2 = ArrowScalar::from(true);
        let f1 = ArrowScalar::from(false);
        assert_eq!(t1, t2);
        assert_ne!(t1, f1);
        // false < true in arrow row encoding
        assert!(f1 < t1);
    }

    #[test]
    fn test_boolean_null() {
        let array: ArrayRef = Arc::new(BooleanArray::from(vec![None]));
        let scalar = ArrowScalar::try_from_array(array).unwrap();
        assert!(scalar.is_null());
        assert_eq!(scalar.data_type(), &DataType::Boolean);
        assert_eq!(format!("{scalar}"), "null");

        // null sorts before false
        let f = ArrowScalar::from(false);
        assert!(scalar < f);
    }

    #[test]
    fn test_string_view_roundtrip() {
        let array: ArrayRef = Arc::new(StringViewArray::from(vec![
            "hello world, this is a long string view",
        ]));
        let scalar = ArrowScalar::try_from_array(array).unwrap();
        assert_eq!(scalar.data_type(), &DataType::Utf8View);
        assert!(!scalar.is_null());
        assert_eq!(
            format!("{scalar}"),
            "hello world, this is a long string view"
        );

        // Extract from multi-element array
        let array: ArrayRef = Arc::new(StringViewArray::from(vec!["alpha", "beta", "gamma"]));
        let s = ArrowScalar::try_new(&array, 1).unwrap();
        assert_eq!(format!("{s}"), "beta");
        assert_eq!(s.data_type(), &DataType::Utf8View);
    }

    #[test]
    fn test_binary_view_roundtrip() {
        let values: Vec<&[u8]> = vec![b"\xDE\xAD\xBE\xEF"];
        let array: ArrayRef = Arc::new(BinaryViewArray::from(values));
        let scalar = ArrowScalar::try_from_array(array).unwrap();
        assert_eq!(scalar.data_type(), &DataType::BinaryView);
        assert!(!scalar.is_null());

        // Extract from multi-element array
        let values: Vec<&[u8]> = vec![b"aaa", b"bbb", b"ccc"];
        let array: ArrayRef = Arc::new(BinaryViewArray::from(values));
        let s = ArrowScalar::try_new(&array, 2).unwrap();
        assert_eq!(s.data_type(), &DataType::BinaryView);
    }

    #[test]
    fn test_string_view_equality_and_ordering() {
        let mk = |s: &str| {
            let array: ArrayRef = Arc::new(StringViewArray::from(vec![s]));
            ArrowScalar::try_from_array(array).unwrap()
        };
        let a = mk("apple");
        let b = mk("apple");
        let c = mk("banana");
        assert_eq!(a, b);
        assert_ne!(a, c);
        assert!(a < c);
    }

    #[test]
    fn test_binary_view_equality_and_ordering() {
        let mk = |b: &[u8]| {
            let values: Vec<&[u8]> = vec![b];
            let array: ArrayRef = Arc::new(BinaryViewArray::from(values));
            ArrowScalar::try_from_array(array).unwrap()
        };
        let a = mk(b"\x01\x02");
        let b = mk(b"\x01\x02");
        let c = mk(b"\x01\x03");
        assert_eq!(a, b);
        assert_ne!(a, c);
        assert!(a < c);
    }

    #[test]
    fn test_string_view_in_collections() {
        let mk = |s: &str| {
            let array: ArrayRef = Arc::new(StringViewArray::from(vec![s]));
            ArrowScalar::try_from_array(array).unwrap()
        };

        let mut hset = HashSet::new();
        hset.insert(mk("foo"));
        hset.insert(mk("bar"));
        hset.insert(mk("foo"));
        assert_eq!(hset.len(), 2);

        let mut bset = BTreeSet::new();
        bset.insert(mk("cherry"));
        bset.insert(mk("apple"));
        bset.insert(mk("banana"));
        let sorted: Vec<_> = bset.iter().map(|s| format!("{s}")).collect();
        assert_eq!(sorted, vec!["apple", "banana", "cherry"]);
    }

    #[test]
    fn test_string_view_null() {
        let array: ArrayRef = Arc::new(StringViewArray::from(vec![Option::<&str>::None]));
        let scalar = ArrowScalar::try_from_array(array).unwrap();
        assert!(scalar.is_null());
        assert_eq!(scalar.data_type(), &DataType::Utf8View);
        assert_eq!(format!("{scalar}"), "null");
    }

    #[test]
    fn test_binary_view_null() {
        let array: ArrayRef = Arc::new(BinaryViewArray::from(vec![Option::<&[u8]>::None]));
        let scalar = ArrowScalar::try_from_array(array).unwrap();
        assert!(scalar.is_null());
        assert_eq!(scalar.data_type(), &DataType::BinaryView);
    }

    #[test]
    fn test_cross_type_comparison_is_consistent() {
        let int_scalar = ArrowScalar::from(42i32);
        let str_scalar = ArrowScalar::from("hello");
        // The ordering is arbitrary but must be consistent
        let ord1 = int_scalar.cmp(&str_scalar);
        let ord2 = int_scalar.cmp(&str_scalar);
        assert_eq!(ord1, ord2);
        // And the reverse should be opposite
        assert_eq!(str_scalar.cmp(&int_scalar), ord1.reverse());
    }
}

#[cfg(test)]
mod prop_tests {
    use std::sync::Arc;

    use arrow_array::*;
    use arrow_ord::sort::sort;
    use arrow_schema::SortOptions;
    use proptest::prelude::*;

    use super::ArrowScalar;

    /// Generate an arbitrary Arrow array of a randomly chosen type, including
    /// nulls. Covers primitives, booleans, string/binary types and their view
    /// variants.
    fn arbitrary_array() -> BoxedStrategy<ArrayRef> {
        let len = 0..=100usize;

        prop_oneof![
            // --- integer types ---
            proptest::collection::vec(proptest::option::of(any::<i8>()), len.clone())
                .prop_map(|v| Arc::new(Int8Array::from(v)) as ArrayRef),
            proptest::collection::vec(proptest::option::of(any::<i16>()), len.clone())
                .prop_map(|v| Arc::new(Int16Array::from(v)) as ArrayRef),
            proptest::collection::vec(proptest::option::of(any::<i32>()), len.clone())
                .prop_map(|v| Arc::new(Int32Array::from(v)) as ArrayRef),
            proptest::collection::vec(proptest::option::of(any::<i64>()), len.clone())
                .prop_map(|v| Arc::new(Int64Array::from(v)) as ArrayRef),
            proptest::collection::vec(proptest::option::of(any::<u8>()), len.clone())
                .prop_map(|v| Arc::new(UInt8Array::from(v)) as ArrayRef),
            proptest::collection::vec(proptest::option::of(any::<u16>()), len.clone())
                .prop_map(|v| Arc::new(UInt16Array::from(v)) as ArrayRef),
            proptest::collection::vec(proptest::option::of(any::<u32>()), len.clone())
                .prop_map(|v| Arc::new(UInt32Array::from(v)) as ArrayRef),
            proptest::collection::vec(proptest::option::of(any::<u64>()), len.clone())
                .prop_map(|v| Arc::new(UInt64Array::from(v)) as ArrayRef),
            // --- float types ---
            proptest::collection::vec(proptest::option::of(any::<f32>()), len.clone())
                .prop_map(|v| Arc::new(Float32Array::from(v)) as ArrayRef),
            proptest::collection::vec(proptest::option::of(any::<f64>()), len.clone())
                .prop_map(|v| Arc::new(Float64Array::from(v)) as ArrayRef),
            // --- boolean ---
            proptest::collection::vec(proptest::option::of(any::<bool>()), len.clone())
                .prop_map(|v| Arc::new(BooleanArray::from(v)) as ArrayRef),
            // --- string types ---
            proptest::collection::vec(proptest::option::of(any::<String>()), len.clone()).prop_map(
                |v| {
                    let refs: Vec<Option<&str>> = v.iter().map(|o| o.as_deref()).collect();
                    Arc::new(StringArray::from(refs)) as ArrayRef
                }
            ),
            proptest::collection::vec(proptest::option::of(any::<String>()), len.clone()).prop_map(
                |v| {
                    let refs: Vec<Option<&str>> = v.iter().map(|o| o.as_deref()).collect();
                    Arc::new(LargeStringArray::from(refs)) as ArrayRef
                }
            ),
            proptest::collection::vec(proptest::option::of(any::<String>()), len.clone()).prop_map(
                |v| {
                    let refs: Vec<Option<&str>> = v.iter().map(|o| o.as_deref()).collect();
                    Arc::new(StringViewArray::from(refs)) as ArrayRef
                }
            ),
            // --- binary types ---
            proptest::collection::vec(
                proptest::option::of(proptest::collection::vec(any::<u8>(), 0..50)),
                len.clone(),
            )
            .prop_map(|v| {
                let refs: Vec<Option<&[u8]>> = v.iter().map(|o| o.as_deref()).collect();
                Arc::new(BinaryArray::from(refs)) as ArrayRef
            }),
            proptest::collection::vec(
                proptest::option::of(proptest::collection::vec(any::<u8>(), 0..50)),
                len.clone(),
            )
            .prop_map(|v| {
                let refs: Vec<Option<&[u8]>> = v.iter().map(|o| o.as_deref()).collect();
                Arc::new(LargeBinaryArray::from(refs)) as ArrayRef
            }),
            proptest::collection::vec(
                proptest::option::of(proptest::collection::vec(any::<u8>(), 0..50)),
                len,
            )
            .prop_map(|v| {
                let refs: Vec<Option<&[u8]>> = v.iter().map(|o| o.as_deref()).collect();
                Arc::new(BinaryViewArray::from(refs)) as ArrayRef
            }),
        ]
        .boxed()
    }

    proptest::proptest! {
        #[test]
        fn sorted_array_produces_sorted_scalars(array in arbitrary_array()) {
            let sorted = sort(
                &array,
                Some(SortOptions { descending: false, nulls_first: true }),
            )
            .unwrap();

            let scalars: Vec<ArrowScalar> = (0..sorted.len())
                .map(|i| ArrowScalar::try_new(&sorted, i).unwrap())
                .collect();

            for i in 1..scalars.len() {
                prop_assert!(
                    scalars[i - 1] <= scalars[i],
                    "scalar[{}] ({:?}) should be <= scalar[{}] ({:?})",
                    i - 1, scalars[i - 1], i, scalars[i],
                );
            }
        }
    }
}