Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Added wrapping_cast to cast kernels #254

Merged
merged 10 commits into from
Aug 8, 2021
6 changes: 3 additions & 3 deletions benches/cast_kernels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use rand::distributions::Uniform;
use rand::Rng;

use arrow2::array::*;
use arrow2::compute::cast;
use arrow2::compute::cast::{self, CastOptions};
use arrow2::datatypes::*;
use arrow2::util::bench_util::*;

Expand Down Expand Up @@ -72,15 +72,15 @@ fn build_utf8_date_time_array(size: usize, with_nulls: bool) -> Utf8Array<i32> {

// cast array from specified primitive array type to desired data type
fn cast_array(array: &dyn Array, to_type: DataType) {
criterion::black_box(cast::cast(array, &to_type).unwrap());
criterion::black_box(cast::cast(array, &to_type, CastOptions::default()).unwrap());
}

fn add_benchmark(c: &mut Criterion) {
let size = 512;
let i32_array = create_primitive_array::<i32>(size, DataType::Int32, 0.1);
let i64_array = create_primitive_array::<i64>(size, DataType::Int64, 0.1);
let f32_array = create_primitive_array::<f32>(size, DataType::Float32, 0.1);
let f32_utf8_array = cast::cast(&f32_array, &DataType::Utf8).unwrap();
let f32_utf8_array = cast::cast(&f32_array, &DataType::Utf8, CastOptions::default()).unwrap();

let f64_array = create_primitive_array::<f64>(size, DataType::Float64, 0.1);
let date64_array = create_primitive_array::<i64>(size, DataType::Date64, 0.1);
Expand Down
15 changes: 9 additions & 6 deletions src/compute/cast/dictionary_to.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{cast, primitive_to_primitive};
use super::{primitive_to_primitive, CastOptions};
use crate::{
array::{Array, DictionaryArray, DictionaryKey, PrimitiveArray},
compute::take::take,
compute::{cast::cast_with_options, take::take},
datatypes::DataType,
error::{ArrowError, Result},
};
Expand All @@ -28,11 +28,12 @@ macro_rules! key_cast {
pub fn dictionary_to_dictionary_values<K: DictionaryKey>(
from: &DictionaryArray<K>,
values_type: &DataType,
options: CastOptions,
) -> Result<DictionaryArray<K>> {
let keys = from.keys();
let values = from.values();

let values = cast(values.as_ref(), values_type)?.into();
let values = cast_with_options(values.as_ref(), values_type, options)?.into();
Ok(DictionaryArray::from_data(keys.clone(), values))
}

Expand Down Expand Up @@ -63,14 +64,15 @@ where
pub(super) fn dictionary_cast_dyn<K: DictionaryKey>(
array: &dyn Array,
to_type: &DataType,
options: CastOptions,
) -> Result<Box<dyn Array>> {
let array = array.as_any().downcast_ref::<DictionaryArray<K>>().unwrap();
let keys = array.keys();
let values = array.values();

match to_type {
DataType::Dictionary(to_keys_type, to_values_type) => {
let values = cast(values.as_ref(), to_values_type)?.into();
let values = cast_with_options(values.as_ref(), to_values_type, options)?.into();

// create the appropriate array type
match to_keys_type.as_ref() {
Expand All @@ -85,7 +87,7 @@ pub(super) fn dictionary_cast_dyn<K: DictionaryKey>(
_ => unreachable!(),
}
}
_ => unpack_dictionary::<K>(keys, values.as_ref(), to_type),
_ => unpack_dictionary::<K>(keys, values.as_ref(), to_type, options),
}
}

Expand All @@ -94,13 +96,14 @@ fn unpack_dictionary<K>(
keys: &PrimitiveArray<K>,
values: &dyn Array,
to_type: &DataType,
options: CastOptions,
) -> Result<Box<dyn Array>>
where
K: DictionaryKey,
{
// attempt to cast the dict values to the target type
// use the take kernel to expand out the dictionary
let values = cast(values, to_type)?;
let values = cast_with_options(values, to_type, options)?;

// take requires first casting i32
let indices = primitive_to_primitive::<_, i32>(keys, &DataType::Int32);
Expand Down
Loading