vortex-arrow 0.94.0

Apache Arrow interoperability for Vortex arrays
Documentation
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

use std::sync::Arc;

use arrow_array::ArrayRef as ArrowArrayRef;
use arrow_array::DictionaryArray;
use arrow_array::PrimitiveArray;
use arrow_array::cast::AsArray;
use arrow_array::new_null_array;
use arrow_array::types::*;
use arrow_schema::DataType;
use arrow_schema::Field;
use vortex_array::ArrayRef;
use vortex_array::ExecutionCtx;
use vortex_array::arrays::Constant;
use vortex_array::arrays::ConstantArray;
use vortex_array::arrays::Dict;
use vortex_array::arrays::DictArray;
use vortex_array::arrays::dict::DictArraySlotsExt;
use vortex_array::matcher::Matcher;
use vortex_error::VortexError;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;

use crate::ArrowExportOptions;
use crate::ExecuteArrowExt;
use crate::executor::execute_arrow_naive;

/// Matches the encodings [`to_arrow_dictionary`] requires for export.
struct ArrowDictExportable;

impl Matcher for ArrowDictExportable {
    type Match<'a> = &'a ArrayRef;

    fn try_match(array: &ArrayRef) -> Option<Self::Match<'_>> {
        (array.is::<Dict>() || array.is::<Constant>()).then_some(array)
    }
}

pub(super) fn to_arrow_dictionary(
    array: ArrayRef,
    codes_type: &DataType,
    values_field: &Field,
    options: &ArrowExportOptions,
    ctx: &mut ExecutionCtx,
) -> VortexResult<ArrowArrayRef> {
    // Keep an existing empty dictionary: canonicalization can discard its codes, and Arrow
    // cannot repack every extension's storage type into a dictionary.
    let array = if array.is::<Dict>() || array.is::<Constant>() {
        array
    } else {
        array.execute_until::<ArrowDictExportable>(ctx)?
    };

    let array = match array.try_downcast::<Dict>() {
        Ok(dict) => return dict_to_dict(dict, codes_type, values_field, options, ctx),
        Err(array) => array,
    };
    let array = match array.try_downcast::<Constant>() {
        Ok(constant) => return constant_to_dict(constant, codes_type, values_field, options, ctx),
        Err(array) => array,
    };

    // Otherwise, we should try and build a dictionary.
    // Arrow hides this functionality inside the cast module!
    let array = array.execute_arrow_with_options(Some(values_field), options, ctx)?;
    arrow_cast::cast(
        &array,
        &DataType::Dictionary(
            Box::new(codes_type.clone()),
            Box::new(values_field.data_type().clone()),
        ),
    )
    .map_err(VortexError::from)
}

/// Convert a constant array to a dictionary with a single entry.
fn constant_to_dict(
    array: ConstantArray,
    codes_type: &DataType,
    values_field: &Field,
    options: &ArrowExportOptions,
    ctx: &mut ExecutionCtx,
) -> VortexResult<ArrowArrayRef> {
    let len = array.len();
    let scalar = array.scalar();
    if scalar.is_null() {
        let dict_type = DataType::Dictionary(
            Box::new(codes_type.clone()),
            Box::new(values_field.data_type().clone()),
        );
        return Ok(new_null_array(&dict_type, len));
    }

    let values = ConstantArray::new(scalar.clone(), 1).execute_arrow_with_options(
        Some(values_field),
        options,
        ctx,
    )?;
    let codes = zeroed_codes_array(codes_type, len)?;
    make_dict_array(codes_type, codes, values, DictKeys::InBounds)
}

/// Convert a Vortex dictionary array to an Arrow dictionary array.
fn dict_to_dict(
    array: DictArray,
    codes_type: &DataType,
    values_field: &Field,
    options: &ArrowExportOptions,
    ctx: &mut ExecutionCtx,
) -> VortexResult<ArrowArrayRef> {
    let codes = execute_arrow_naive(array.codes().clone(), Some(codes_type), options, ctx)?;
    let values =
        array
            .values()
            .clone()
            .execute_arrow_with_options(Some(values_field), options, ctx)?;

    make_dict_array(codes_type, codes, values, DictKeys::Untrusted)
}

/// Construct a zeroed Arrow primitive array directly.
fn zeroed_codes_array(codes_type: &DataType, len: usize) -> VortexResult<ArrowArrayRef> {
    Ok(match codes_type {
        DataType::Int8 => Arc::new(PrimitiveArray::<Int8Type>::from_value(0, len)),
        DataType::Int16 => Arc::new(PrimitiveArray::<Int16Type>::from_value(0, len)),
        DataType::Int32 => Arc::new(PrimitiveArray::<Int32Type>::from_value(0, len)),
        DataType::Int64 => Arc::new(PrimitiveArray::<Int64Type>::from_value(0, len)),
        DataType::UInt8 => Arc::new(PrimitiveArray::<UInt8Type>::from_value(0, len)),
        DataType::UInt16 => Arc::new(PrimitiveArray::<UInt16Type>::from_value(0, len)),
        DataType::UInt32 => Arc::new(PrimitiveArray::<UInt32Type>::from_value(0, len)),
        DataType::UInt64 => Arc::new(PrimitiveArray::<UInt64Type>::from_value(0, len)),
        _ => vortex_bail!("Unsupported dictionary codes type: {:?}", codes_type),
    })
}

/// Tells [`make_dict_array`] if the codes are known to be valid indices into the values.
#[derive(Clone, Copy)]
enum DictKeys {
    /// The codes come from a Vortex array, which can come from a file. Arrow must check them.
    Untrusted,
    /// This module made the codes, and they are all in bounds of the values.
    InBounds,
}

/// Construct an Arrow `DictionaryArray` from pre-built codes and values arrays.
fn make_dict_array(
    codes_type: &DataType,
    codes: ArrowArrayRef,
    values: ArrowArrayRef,
    keys: DictKeys,
) -> VortexResult<ArrowArrayRef> {
    match codes_type {
        DataType::Int8 => typed_dict_array::<Int8Type>(&codes, values, keys),
        DataType::Int16 => typed_dict_array::<Int16Type>(&codes, values, keys),
        DataType::Int32 => typed_dict_array::<Int32Type>(&codes, values, keys),
        DataType::Int64 => typed_dict_array::<Int64Type>(&codes, values, keys),
        DataType::UInt8 => typed_dict_array::<UInt8Type>(&codes, values, keys),
        DataType::UInt16 => typed_dict_array::<UInt16Type>(&codes, values, keys),
        DataType::UInt32 => typed_dict_array::<UInt32Type>(&codes, values, keys),
        DataType::UInt64 => typed_dict_array::<UInt64Type>(&codes, values, keys),
        _ => vortex_bail!("Unsupported dictionary codes type: {:?}", codes_type),
    }
}

fn typed_dict_array<K: ArrowDictionaryKeyType>(
    codes: &ArrowArrayRef,
    values: ArrowArrayRef,
    keys: DictKeys,
) -> VortexResult<ArrowArrayRef> {
    let codes = codes.as_primitive::<K>().clone();

    Ok(match keys {
        // Arrow kernels read the values with the codes and do no bounds check, so a code that
        // is negative or not less than the values length must cause an error here.
        DictKeys::Untrusted => Arc::new(DictionaryArray::try_new(codes, values)?),
        // SAFETY: The caller made the codes, and every code is in bounds of the values.
        DictKeys::InBounds => Arc::new(unsafe { DictionaryArray::new_unchecked(codes, values) }),
    })
}

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

    use arrow_array::DictionaryArray as ArrowDictArray;
    use arrow_array::StringArray;
    use arrow_array::types::UInt8Type;
    use arrow_array::types::UInt32Type;
    use arrow_schema::DataType;
    use arrow_schema::Field;
    use rstest::rstest;
    use vortex_array::IntoArray;
    use vortex_array::VortexSessionExecute;
    use vortex_array::arrays::BoolArray;
    use vortex_array::arrays::FixedSizeListArray;
    use vortex_array::arrays::PrimitiveArray;
    use vortex_array::arrays::VarBinViewArray;
    use vortex_array::dtype::DType;
    use vortex_array::dtype::Nullability::Nullable;
    use vortex_array::scalar::Scalar;
    use vortex_array::scalar_fn::builtin::mask::Mask;
    use vortex_array::validity::Validity;
    use vortex_buffer::buffer;
    use vortex_error::VortexResult;
    use vortex_session::VortexSession;

    use crate::ArrowSessionExt;
    use crate::ExecuteArrowExt;
    use crate::executor::dictionary::ConstantArray;
    use crate::executor::dictionary::DictArray;

    #[track_caller]
    fn check_fixed_size_list_dictionary_round_trip(len: usize) -> VortexResult<()> {
        let mut ctx = SESSION.create_execution_ctx();
        let values = FixedSizeListArray::try_new(
            PrimitiveArray::from_iter(0..i32::try_from(len * 2)?).into_array(),
            2,
            Validity::NonNullable,
            len,
        )?
        .into_array();
        let source = DictArray::try_new(
            PrimitiveArray::from_iter(0..u32::try_from(len)?).into_array(),
            values,
        )?
        .into_array();
        let arrow = SESSION.arrow();
        let values_field = arrow.to_arrow_field("item", source.dtype())?;
        let field = Field::new(
            "dict",
            dict_type(DataType::Int32, values_field.data_type().clone()),
            false,
        );

        let exported = source.clone().execute_arrow(Some(&field), &mut ctx)?;
        assert_eq!(exported.data_type(), field.data_type());
        let imported = arrow.from_arrow_array(exported, &field)?;
        vortex_array::assert_arrays_eq!(imported, source, &mut ctx);

        Ok(())
    }

    static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);

    fn dict_type(codes: DataType, values: DataType) -> DataType {
        DataType::Dictionary(Box::new(codes), Box::new(values))
    }

    fn execute(
        array: vortex_array::ArrayRef,
        dt: &DataType,
    ) -> VortexResult<arrow_array::ArrayRef> {
        let field = Field::new("", dt.clone(), array.dtype().is_nullable());
        array.execute_arrow(Some(&field), &mut SESSION.create_execution_ctx())
    }

    fn dict_basic_input() -> vortex_array::ArrayRef {
        DictArray::try_new(
            buffer![0u8, 1, 0].into_array(),
            VarBinViewArray::from_iter_str(["a", "b"]).into_array(),
        )
        .expect("valid dictionary input")
        .into_array()
    }

    fn dict_with_null_codes_input() -> vortex_array::ArrayRef {
        DictArray::try_new(
            PrimitiveArray::from_option_iter(vec![Some(0u8), None, Some(1)]).into_array(),
            VarBinViewArray::from_iter_str(["a", "b"]).into_array(),
        )
        .expect("valid dictionary input with null codes")
        .into_array()
    }

    #[rstest]
    #[case::constant_null(
        ConstantArray::new(Scalar::null(DType::Utf8(Nullable)), 4).into_array(),
        dict_type(DataType::UInt32, DataType::Utf8),
        Arc::new(vec![None::<&str>, None, None, None].into_iter().collect::<ArrowDictArray<UInt32Type>>()) as arrow_array::ArrayRef,
    )]
    #[case::constant_non_null(
        ConstantArray::new(Scalar::from("hello"), 5).into_array(),
        dict_type(DataType::UInt32, DataType::Utf8),
        Arc::new(vec![Some("hello"); 5].into_iter().collect::<ArrowDictArray<UInt32Type>>()) as arrow_array::ArrayRef,
    )]
    #[case::dict_basic(
        dict_basic_input(),
        dict_type(DataType::UInt8, DataType::Utf8),
        Arc::new(vec![Some("a"), Some("b"), Some("a")].into_iter().collect::<ArrowDictArray<UInt8Type>>()) as arrow_array::ArrayRef,
    )]
    #[case::dict_with_null_codes(
        dict_with_null_codes_input(),
        dict_type(DataType::UInt8, DataType::Utf8),
        Arc::new(vec![Some("a"), None, Some("b")].into_iter().collect::<ArrowDictArray<UInt8Type>>()) as arrow_array::ArrayRef,
    )]
    #[case::varbinview_fallback(
        [Some("a"), None, Some("a"), Some("b"), Some("a")].into_iter().collect::<VarBinViewArray>().into_array(),
        dict_type(DataType::UInt8, DataType::Utf8),
        Arc::new(vec![Some("a"), None, Some("a"), Some("b"), Some("a")].into_iter().collect::<ArrowDictArray<UInt8Type>>()) as arrow_array::ArrayRef,
    )]
    fn to_arrow_dictionary(
        #[case] input: vortex_array::ArrayRef,
        #[case] target_type: DataType,
        #[case] expected: arrow_array::ArrayRef,
    ) -> VortexResult<()> {
        let actual = execute(input, &target_type)?;
        assert_eq!(expected.as_ref(), actual.as_ref());
        Ok(())
    }

    #[test]
    fn dict_with_out_of_bounds_code_fails() {
        // SAFETY: The code 5 is out of bounds on purpose, because the export must reject it.
        let dict = unsafe {
            DictArray::new_unchecked(
                buffer![0u8, 5].into_array(),
                VarBinViewArray::from_iter_str(["a", "b"]).into_array(),
            )
        };

        let result = execute(
            dict.into_array(),
            &dict_type(DataType::UInt8, DataType::Utf8),
        );

        assert!(result.is_err());
    }

    #[test]
    fn mask_wrapped_dict_exports() -> VortexResult<()> {
        // Dictionary behind a lazy `mask` scalar-fn — the shape a scan produces when a row
        // mask is applied to a dict-encoded column.
        let dict = DictArray::try_new(
            buffer![0u8, 1, 0].into_array(),
            VarBinViewArray::from_iter_str(["a", "b"]).into_array(),
        )?;
        let mask = BoolArray::from_iter([true, false, true]);
        let masked = Mask::try_new(dict.into_array(), mask.into_array())?.into_array();

        let actual = execute(masked, &dict_type(DataType::UInt8, DataType::Utf8))?;
        let flat = arrow_cast::cast(&actual, &DataType::Utf8)?;

        let expected = StringArray::from(vec![Some("a"), None, Some("a")]);
        assert_eq!(flat.as_ref(), &expected as &dyn arrow_array::Array);
        Ok(())
    }

    #[rstest]
    #[case::empty(0)]
    #[case::nonempty(3)]
    fn fixed_size_list_values_round_trip(#[case] len: usize) -> VortexResult<()> {
        check_fixed_size_list_dictionary_round_trip(len)
    }
}