vortex-array 0.94.0

Vortex in memory columnar data format
Documentation
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

//! Conformance and canonical fallbacks for decimal widening.
//!
//! Arithmetic, zip, chunked and interleave execution preserve the logical decimal dtype and nulls.

use rstest::rstest;
use vortex_buffer::buffer;
use vortex_error::VortexResult;

use super::SESSION;
use super::canonical;
use crate::ArrayRef;
use crate::Canonical;
use crate::IntoArray;
use crate::VortexSessionExecute;
use crate::arrays::ChunkedArray;
use crate::arrays::DecimalArray;
use crate::arrays::Widen;
use crate::arrays::WidenArray;
use crate::assert_arrays_eq;
use crate::builtins::ArrayBuiltins;
use crate::compute::conformance::binary_numeric::test_binary_numeric_array;
use crate::compute::conformance::cast::test_cast_conformance;
use crate::compute::conformance::consistency::test_array_consistency;
use crate::compute::conformance::filter::test_filter_conformance;
use crate::compute::conformance::mask::test_mask_conformance;
use crate::compute::conformance::take::test_take_conformance;
use crate::dtype::DType;
use crate::dtype::DecimalDType;
use crate::scalar::NumericOperator;
use crate::validity::Validity;

fn decimal_widen(validity: Validity) -> VortexResult<ArrayRef> {
    let values = DecimalArray::new(
        buffer![-125i16, 50, 50, 250, 900],
        DecimalDType::new(4, 2),
        validity,
        &mut SESSION.create_execution_ctx(),
    );
    let dtype = DType::Decimal(DecimalDType::new(18, 2), values.dtype().nullability());

    Ok(WidenArray::try_new(values.into_array(), dtype)?.into_array())
}

#[rstest]
#[case::non_nullable(Validity::NonNullable)]
#[case::partially_valid(Validity::from_iter([true, false, true, true, false]))]
fn test_decimal_conformance(#[case] validity: Validity) -> VortexResult<()> {
    let mut ctx = SESSION.create_execution_ctx();
    let array = decimal_widen(validity)?;
    assert!(array.is::<Widen>());

    test_array_consistency(&array, &mut ctx);
    test_cast_conformance(&array, &mut ctx);
    test_filter_conformance(&array, &mut ctx);
    test_take_conformance(&array, &mut ctx);
    test_mask_conformance(&array, &mut ctx);
    test_binary_numeric_array(&array, &mut ctx);

    Ok(())
}

#[rstest]
fn test_decimal_arithmetic_falls_back_to_canonical(
    #[values(
        NumericOperator::Add,
        NumericOperator::Sub,
        NumericOperator::Mul,
        NumericOperator::Div
    )]
    operator: NumericOperator,
    #[values(false, true)] widen_on_right: bool,
) -> VortexResult<()> {
    let mut ctx = SESSION.create_execution_ctx();
    let array = decimal_widen(Validity::from_iter([true, false, true, true, false]))?;
    let other = DecimalArray::from_iter([25i64, 50, -100, 125, -200], DecimalDType::new(18, 2))
        .into_array();
    let (lhs, rhs) = if widen_on_right {
        (other, array)
    } else {
        (array, other)
    };
    let expected = canonical(&lhs, &mut ctx)?
        .numeric(canonical(&rhs, &mut ctx)?, operator)?
        .execute::<Canonical>(&mut ctx)?;

    let actual = lhs.numeric(rhs, operator)?.execute::<Canonical>(&mut ctx)?;

    assert_arrays_eq!(actual, expected, &mut ctx);

    Ok(())
}

#[test]
fn test_decimal_chunked_execution_matches_canonical() -> VortexResult<()> {
    let mut ctx = SESSION.create_execution_ctx();
    let array = decimal_widen(Validity::from_iter([true, false, true, true, false]))?;
    let expected = canonical(&array, &mut ctx)?;
    let chunks = ChunkedArray::from_iter([
        array.slice(0..2)?,
        expected.slice(2..3)?,
        array.slice(3..5)?,
    ])
    .into_array();

    let actual = chunks.execute::<DecimalArray>(&mut ctx)?;

    assert_arrays_eq!(actual, expected, &mut ctx);

    Ok(())
}

#[test]
fn test_decimal_interleave_falls_back_to_canonical() -> VortexResult<()> {
    let mut ctx = SESSION.create_execution_ctx();
    let array = decimal_widen(Validity::from_iter([true, false, true, true, false]))?;
    let other = DecimalArray::from_iter([500i64, -600, 700, -800, 950], DecimalDType::new(18, 2))
        .into_array();
    let array_indices = buffer![0u8, 1, 0, 1, 0, 0, 1].into_array();
    let row_indices = buffer![4u32, 0, 1, 4, 0, 4, 1].into_array();
    let expected = array_indices
        .interleave(
            [canonical(&array, &mut ctx)?, other.clone()],
            row_indices.clone(),
        )?
        .execute::<DecimalArray>(&mut ctx)?;

    let actual = array_indices
        .interleave([array, other], row_indices)?
        .execute::<DecimalArray>(&mut ctx)?;

    assert_arrays_eq!(actual, expected, &mut ctx);

    Ok(())
}