use arrow_array::Array as ArrowArray;
use arrow_array::ArrayRef as ArrowArrayRef;
use arrow_array::RunArray;
use arrow_array::types::Int16Type;
use arrow_array::types::Int32Type;
use arrow_array::types::Int64Type;
use arrow_array::types::RunEndIndexType;
use arrow_schema::DataType;
use vortex_array::ArrayRef;
use vortex_array::IntoArray;
use vortex_array::arrays::PrimitiveArray;
use vortex_array::arrays::RunEnd;
use vortex_array::arrays::primitive::PrimitiveArrayExt;
use vortex_array::dtype::NativePType;
use vortex_array::validity::Validity;
use vortex_buffer::Buffer;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_error::vortex_ensure_eq;
use vortex_error::vortex_err;
pub(crate) fn run_end_from_arrow_dyn(
array: &dyn ArrowArray,
ends_type: &DataType,
import_values: impl FnOnce(ArrowArrayRef) -> VortexResult<ArrayRef>,
) -> VortexResult<ArrayRef> {
match ends_type {
DataType::Int16 => run_end_from_arrow::<Int16Type>(array, import_values),
DataType::Int32 => run_end_from_arrow::<Int32Type>(array, import_values),
DataType::Int64 => run_end_from_arrow::<Int64Type>(array, import_values),
dtype => {
vortex_bail!("Arrow run ends must be Int16, Int32, or Int64, got {dtype}")
}
}
}
fn run_end_from_arrow<R: RunEndIndexType>(
array: &dyn ArrowArray,
import_values: impl FnOnce(ArrowArrayRef) -> VortexResult<ArrayRef>,
) -> VortexResult<ArrayRef>
where
R::Native: NativePType,
{
let array = array
.as_any()
.downcast_ref::<RunArray<R>>()
.ok_or_else(|| {
vortex_err!(
"Arrow run-end import requires a RunArray with {} run ends, got {}",
R::DATA_TYPE,
array.data_type()
)
})?;
let (runs, offset) = if array.is_empty() {
(0..0, 0)
} else {
let start = array.get_start_physical_index();
let end = array.get_end_physical_index() + 1;
(start..end, array.run_ends().offset())
};
let ends = array.run_ends().inner().slice(runs.start, runs.len());
let ends = Buffer::<R::Native>::from_arrow_scalar_buffer(ends);
let ends = PrimitiveArray::new(ends, Validity::NonNullable)
.reinterpret_cast(R::Native::PTYPE.to_unsigned())
.into_array();
let values = import_values(array.values().slice(runs.start, runs.len()))?;
vortex_ensure_eq!(
ends.len(),
values.len(),
"Arrow run-end array must have one value per run end"
);
Ok(unsafe { RunEnd::new_unchecked(ends, values, offset, array.len()) }.into_array())
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use std::sync::LazyLock;
use arrow_array::Array as _;
use arrow_array::Float64Array;
use arrow_array::Int32Array;
use arrow_array::Int64Array;
use arrow_array::RunArray;
use arrow_array::types::Int32Type;
use arrow_array::types::Int64Type;
use arrow_array::types::RunEndIndexType;
use arrow_schema::DataType;
use arrow_schema::Field;
use rstest::rstest;
use vortex_array::ArrayRef;
use vortex_array::IntoArray as _;
use vortex_array::VortexSessionExecute as _;
use vortex_array::arrays::PrimitiveArray;
use vortex_array::arrays::RunEnd;
use vortex_array::assert_arrays_eq;
use vortex_array::dtype::DType;
use vortex_array::dtype::NativePType;
use vortex_array::dtype::Nullability;
use vortex_array::dtype::PType;
use vortex_buffer::Buffer;
use vortex_buffer::buffer;
use vortex_error::VortexResult;
use vortex_session::VortexSession;
use crate::ArrowSessionExt;
use crate::ExecuteArrowExt;
static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);
fn decode_run_array<R: RunEndIndexType>(
array: &RunArray<R>,
nullable: bool,
) -> VortexResult<ArrayRef>
where
R::Native: NativePType,
{
SESSION
.arrow()
.from_arrow_array(Arc::new(array.clone()), nullable)
}
#[test]
fn test_arrow_run_array_to_vortex() -> VortexResult<()> {
let run_ends = Int32Array::from(vec![3i32, 5, 8]);
let values = Int32Array::from(vec![10, 20, 30]);
let arrow_run_array = RunArray::<Int32Type>::try_new(&run_ends, &values)?;
let vortex_array = decode_run_array(&arrow_run_array, false)?;
assert_arrays_eq!(
vortex_array.into_array(),
buffer![10i32, 10, 10, 20, 20, 30, 30, 30].into_array(),
&mut SESSION.create_execution_ctx()
);
Ok(())
}
#[test]
fn test_arrow_run_array_with_nulls_to_vortex() -> VortexResult<()> {
let run_ends = Int32Array::from(vec![2i32, 4, 6]);
let values = Int32Array::from(vec![Some(100), None, Some(300)]);
let arrow_run_array = RunArray::<Int32Type>::try_new(&run_ends, &values)?;
let vortex_array = decode_run_array(&arrow_run_array, true)?;
assert_arrays_eq!(
vortex_array.into_array(),
PrimitiveArray::from_option_iter([
Some(100i32),
Some(100i32),
None,
None,
Some(300i32),
Some(300i32)
]),
&mut SESSION.create_execution_ctx()
);
Ok(())
}
#[test]
fn test_arrow_run_array_with_different_types() -> VortexResult<()> {
let run_ends = Int64Array::from(vec![1i64, 3, 4]);
let values = Float64Array::from(vec![1.5, 2.5, 3.5]);
let arrow_run_array = RunArray::<Int64Type>::try_new(&run_ends, &values)?;
let vortex_array = decode_run_array(&arrow_run_array, false)?;
assert_arrays_eq!(
vortex_array,
buffer![1.5f64, 2.5, 2.5, 3.5].into_array(),
&mut SESSION.create_execution_ctx()
);
Ok(())
}
#[test]
fn test_sliced_arrow_run_array_to_vortex() -> VortexResult<()> {
let run_ends = Int32Array::from(vec![2i32, 5, 8, 10]);
let values = Int32Array::from(vec![100, 200, 300, 400]);
let arrow_run_array = RunArray::<Int32Type>::try_new(&run_ends, &values)?;
let sliced_array = arrow_run_array.slice(1, 6);
let vortex_array = decode_run_array(&sliced_array, false)?;
assert_arrays_eq!(
vortex_array,
buffer![100, 200, 200, 200, 300, 300].into_array(),
&mut SESSION.create_execution_ctx()
);
Ok(())
}
#[rstest]
#[case::prefix(0, 2)]
#[case::suffix(4, 2)]
#[case::empty(3, 0)]
#[case::empty_at_end(6, 0)]
fn test_sliced_run_array_ignores_nulls_outside_its_rows(
#[case] start: usize,
#[case] len: usize,
) -> VortexResult<()> {
let run_ends = Int32Array::from(vec![2, 4, 6]);
let values = Int32Array::from(vec![Some(10), None, Some(30)]);
let array = RunArray::<Int32Type>::try_new(&run_ends, &values)?.slice(start, len);
let imported = decode_run_array(&array, false)?;
let expected =
Buffer::from_iter(std::iter::repeat_n(if start < 2 { 10i32 } else { 30 }, len))
.into_array();
assert_arrays_eq!(imported, expected, &mut SESSION.create_execution_ctx());
Ok(())
}
#[test]
fn test_sliced_arrow_run_array_with_nulls_to_vortex() -> VortexResult<()> {
let run_ends = Int64Array::from(vec![3i64, 6, 9, 12]);
let values = Int64Array::from(vec![Some(10), None, Some(30), Some(40)]);
let arrow_run_array = RunArray::<Int64Type>::try_new(&run_ends, &values)?;
let sliced_array = arrow_run_array.slice(4, 6);
let vortex_array = decode_run_array(&sliced_array, true)?;
assert_arrays_eq!(
vortex_array,
PrimitiveArray::from_option_iter([
None,
None,
Some(30i64),
Some(30),
Some(30),
Some(40),
]),
&mut SESSION.create_execution_ctx()
);
Ok(())
}
#[test]
fn test_sliced_to_0_arrow_run_array_with_nulls_to_vortex() -> VortexResult<()> {
let run_ends = Int64Array::from(vec![3i64, 6, 9, 12]);
let values = Int64Array::from(vec![Some(10), None, Some(30), Some(40)]);
let arrow_run_array = RunArray::<Int64Type>::try_new(&run_ends, &values)?;
let sliced_array = arrow_run_array.slice(4, 0);
let vortex_array = decode_run_array(&sliced_array, true)?;
assert_eq!(vortex_array.len(), 0);
assert_eq!(
vortex_array.dtype(),
&DType::Primitive(PType::I64, Nullability::Nullable)
);
assert_arrays_eq!(
vortex_array,
PrimitiveArray::from_option_iter([] as [Option<i64>; 0]),
&mut SESSION.create_execution_ctx()
);
Ok(())
}
fn ree_type(ends: DataType, values_dtype: DataType) -> DataType {
DataType::RunEndEncoded(
Arc::new(Field::new("run_ends", ends, false)),
Arc::new(Field::new("values", values_dtype, true)),
)
}
fn execute(array: ArrayRef, dt: &DataType) -> VortexResult<arrow_array::ArrayRef> {
let field = Field::new("", dt.clone(), true);
array.execute_arrow(Some(&field), &mut SESSION.create_execution_ctx())
}
#[test]
fn test_roundtrip_arrow_to_vortex_to_arrow() -> VortexResult<()> {
let original = RunArray::<Int32Type>::try_new(
&Int32Array::from(vec![3i32, 5, 8]),
&Int32Array::from(vec![10, 20, 30]),
)?;
let vortex_array = decode_run_array(&original, false)?;
let target = ree_type(DataType::Int32, DataType::Int32);
let result = execute(vortex_array.into_array(), &target)?;
let expected = RunArray::<Int32Type>::try_new(
&Int32Array::from(vec![3, 5, 8]),
&Int32Array::from(vec![10, 20, 30]),
)?;
assert_eq!(result.as_ref(), &expected);
Ok(())
}
#[rstest]
#[case::nonzero_offset(
&[10i32, 10, 20, 20, 20, 30, 30],
1..5usize,
&[1i32, 4],
&[10i32, 20],
)]
#[case::zero_offset_excess_runs(
&[10i32, 10, 10, 20, 20, 30, 30, 30, 30, 30],
0..4usize,
&[3i32, 4],
&[10i32, 20],
)]
fn sliced_runend_to_arrow_ree(
#[case] input: &[i32],
#[case] slice_range: std::ops::Range<usize>,
#[case] expected_ends: &[i32],
#[case] expected_values: &[i32],
) -> VortexResult<()> {
let mut ctx = SESSION.create_execution_ctx();
let array = RunEnd::encode(
PrimitiveArray::from_iter(input.iter().copied()).into_array(),
&mut ctx,
)?;
let sliced = array.into_array().slice(slice_range.clone())?;
let target = ree_type(DataType::Int32, DataType::Int32);
let result = execute(sliced, &target)?;
assert_eq!(result.len(), slice_range.len());
let expected = RunArray::<Int32Type>::try_new(
&Int32Array::from(expected_ends.to_vec()),
&Int32Array::from(expected_values.to_vec()),
)?;
assert_eq!(result.as_ref(), &expected);
Ok(())
}
}