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
use vortex_error::{vortex_bail, vortex_err, VortexError, VortexResult};
use vortex_scalar::Scalar;

use crate::encoding::Encoding;
use crate::validity::ArrayValidity;
use crate::{ArrayDType, ArrayData};

/// Implementation of scalar_at for an encoding.
///
/// SAFETY: the index is guaranteed to be within the bounds of the [ArrayData].
pub trait ScalarAtFn<Array> {
    fn scalar_at(&self, array: &Array, index: usize) -> VortexResult<Scalar>;
}

impl<E: Encoding> ScalarAtFn<ArrayData> for E
where
    E: ScalarAtFn<E::Array>,
    for<'a> &'a E::Array: TryFrom<&'a ArrayData, Error = VortexError>,
{
    fn scalar_at(&self, array: &ArrayData, index: usize) -> VortexResult<Scalar> {
        let array_ref = <&E::Array>::try_from(array)?;
        let encoding = array
            .encoding()
            .as_any()
            .downcast_ref::<E>()
            .ok_or_else(|| vortex_err!("Mismatched encoding"))?;
        ScalarAtFn::scalar_at(encoding, array_ref, index)
    }
}

pub fn scalar_at(array: impl AsRef<ArrayData>, index: usize) -> VortexResult<Scalar> {
    let array = array.as_ref();
    if index >= array.len() {
        vortex_bail!(OutOfBounds: index, 0, array.len());
    }

    if !array.is_valid(index) {
        return Ok(Scalar::null(array.dtype().clone()));
    }

    let scalar = array
        .encoding()
        .scalar_at_fn()
        .map(|f| f.scalar_at(array, index))
        .unwrap_or_else(|| Err(vortex_err!(NotImplemented: "scalar_at", array.encoding().id())))?;

    debug_assert_eq!(
        scalar.dtype(),
        array.dtype(),
        "ScalarAt dtype mismatch {}",
        array.encoding().id()
    );

    Ok(scalar)
}