Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 90 additions & 2 deletions vortex-arrow/src/executor/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use arrow_array::types::*;
use arrow_buffer::ArrowNativeType;
use arrow_schema::DataType;
use arrow_schema::Field;
use arrow_schema::TimeUnit as ArrowTimeUnit;
use vortex_array::AnyCanonical;
use vortex_array::ArrayRef;
use vortex_array::ExecutionCtx;
Expand Down Expand Up @@ -317,8 +318,8 @@ const SPARSE_VALUES_PER_ROW: usize = 4;
/// them is cheap, and the dictionary keeps the values and the codes of its leaves as they are.
const SPARSE_MIN_VALUES: usize = 1 << 16;

/// Whether Arrow can hash decoded values of `data_type` into a dictionary, as listed by
/// `arrow_cast`'s dictionary packing.
/// Whether `decode_to_dictionary` can hash decoded values of `data_type` into a dictionary
/// (via `arrow_cast`'s dictionary packing, plus `Duration` reinterpreted through `Int64`).
fn arrow_packs_dictionary(data_type: &DataType) -> bool {
matches!(
data_type,
Expand All @@ -342,6 +343,7 @@ fn arrow_packs_dictionary(data_type: &DataType) -> bool {
| DataType::Time32(_)
| DataType::Time64(_)
| DataType::Timestamp(..)
| DataType::Duration(_)
| DataType::Utf8
| DataType::LargeUtf8
| DataType::Utf8View
Expand Down Expand Up @@ -406,6 +408,47 @@ fn decode_to_dictionary<K: ArrowDictionaryKeyType>(
};
// Arrow hides this functionality inside the cast module!
let array = array.execute_arrow_with_options(Some(values_field), options, ctx)?;
if let DataType::Duration(unit) = values_field.data_type() {
// `arrow_cast`'s dictionary packing omits `Duration`, even though Arrow Duration arrays
// have identical `i64` physical layout to `Int64`. Pack as `Int64` and reinterpret the
// dictionary values back to `Duration` without copying.
let i64_values: ArrowArrayRef = Arc::new(match unit {
ArrowTimeUnit::Second => array
.as_primitive::<DurationSecondType>()
.reinterpret_cast::<Int64Type>(),
ArrowTimeUnit::Millisecond => array
.as_primitive::<DurationMillisecondType>()
.reinterpret_cast::<Int64Type>(),
ArrowTimeUnit::Microsecond => array
.as_primitive::<DurationMicrosecondType>()
.reinterpret_cast::<Int64Type>(),
ArrowTimeUnit::Nanosecond => array
.as_primitive::<DurationNanosecondType>()
.reinterpret_cast::<Int64Type>(),
});
let i64_dict = arrow_cast::cast(
&i64_values,
&DataType::Dictionary(Box::new(K::DATA_TYPE), Box::new(DataType::Int64)),
)?;
let i64_dict = i64_dict.as_dictionary::<K>();
let values_i64 = i64_dict.values().as_primitive::<Int64Type>();
let duration_values: ArrowArrayRef = match unit {
ArrowTimeUnit::Second => Arc::new(values_i64.reinterpret_cast::<DurationSecondType>()),
ArrowTimeUnit::Millisecond => {
Arc::new(values_i64.reinterpret_cast::<DurationMillisecondType>())
}
ArrowTimeUnit::Microsecond => {
Arc::new(values_i64.reinterpret_cast::<DurationMicrosecondType>())
}
ArrowTimeUnit::Nanosecond => {
Arc::new(values_i64.reinterpret_cast::<DurationNanosecondType>())
}
};
return Ok(Arc::new(DictionaryArray::<K>::try_new(
i64_dict.keys().clone(),
duration_values,
)?));
}
arrow_cast::cast(
&array,
&DataType::Dictionary(
Expand All @@ -431,13 +474,15 @@ mod tests {
use arrow_array::types::UInt32Type;
use arrow_schema::DataType;
use arrow_schema::Field;
use arrow_schema::TimeUnit as ArrowTimeUnit;
use rstest::rstest;
use vortex_array::IntoArray;
use vortex_array::VortexSessionExecute;
use vortex_array::arrays::BoolArray;
use vortex_array::arrays::ChunkedArray;
use vortex_array::arrays::ConstantArray;
use vortex_array::arrays::DictArray;
use vortex_array::arrays::ExtensionArray;
use vortex_array::arrays::FixedSizeListArray;
use vortex_array::arrays::PrimitiveArray;
use vortex_array::arrays::VarBinViewArray;
Expand All @@ -446,6 +491,8 @@ mod tests {
use vortex_array::dtype::Nullability::NonNullable;
use vortex_array::dtype::Nullability::Nullable;
use vortex_array::dtype::PType;
use vortex_array::extension::datetime::Duration;
use vortex_array::extension::datetime::TimeUnit;
use vortex_array::scalar::Scalar;
use vortex_array::scalar_fn::builtin::mask::Mask;
use vortex_array::validity::Validity;
Expand Down Expand Up @@ -957,4 +1004,45 @@ mod tests {
fn fixed_size_list_values_round_trip(#[case] len: usize) -> VortexResult<()> {
check_fixed_size_list_dictionary_round_trip(len)
}

#[rstest]
#[case::seconds(TimeUnit::Seconds, ArrowTimeUnit::Second)]
#[case::milliseconds(TimeUnit::Milliseconds, ArrowTimeUnit::Millisecond)]
#[case::microseconds(TimeUnit::Microseconds, ArrowTimeUnit::Microsecond)]
#[case::nanoseconds(TimeUnit::Nanoseconds, ArrowTimeUnit::Nanosecond)]
fn sparse_duration_dict_slices_repack_to_arrow_dictionary(
#[case] unit: TimeUnit,
#[case] arrow_unit: ArrowTimeUnit,
) -> VortexResult<()> {
let mut ctx = SESSION.create_execution_ctx();
let duration_values = ExtensionArray::try_new(
Duration::new(unit, NonNullable).erased(),
PrimitiveArray::from_iter((0..70_000i64).map(|i| i % 3)).into_array(),
)?
.into_array();
let dict = DictArray::try_new(
PrimitiveArray::from_iter(0..70_000u32).into_array(),
duration_values,
)?
.into_array();
let source = ChunkedArray::try_new(
vec![dict.slice(0..2)?, dict.slice(69_998..70_000)?],
dict.dtype().clone(),
)?
.into_array();

let field = Field::new(
"duration_dict",
dict_type(DataType::Int32, DataType::Duration(arrow_unit)),
false,
);
let exported = source.clone().execute_arrow(Some(&field), &mut ctx)?;
assert_eq!(exported.data_type(), field.data_type());
// Arrow packs Duration values into a deduplicated dictionary rather than joining 140k rows.
assert_eq!(exported.as_any_dictionary().values().len(), 3);

let imported = SESSION.arrow().from_arrow_array(exported, &field)?;
vortex_array::assert_arrays_eq!(imported, source, &mut ctx);
Ok(())
}
}
Loading