diff --git a/arrow-arith/src/aggregate.rs b/arrow-arith/src/aggregate.rs index 9ed6dee516a4..2833300ddc07 100644 --- a/arrow-arith/src/aggregate.rs +++ b/arrow-arith/src/aggregate.rs @@ -759,6 +759,7 @@ mod tests { use super::*; use crate::arithmetic::add; use arrow_array::types::*; + use std::sync::Arc; #[test] fn test_primitive_array_sum() { @@ -1142,9 +1143,10 @@ mod tests { #[test] fn test_sum_dyn() { let values = Int8Array::from_iter_values([10_i8, 11, 12, 13, 14, 15, 16, 17]); + let values = Arc::new(values) as ArrayRef; let keys = Int8Array::from_iter_values([2_i8, 3, 4]); - let dict_array = DictionaryArray::try_new(&keys, &values).unwrap(); + let dict_array = DictionaryArray::new(keys, values.clone()); let array = dict_array.downcast_dict::().unwrap(); assert_eq!(39, sum_array::(array).unwrap()); @@ -1152,12 +1154,12 @@ mod tests { assert_eq!(15, sum_array::(&a).unwrap()); let keys = Int8Array::from(vec![Some(2_i8), None, Some(4)]); - let dict_array = DictionaryArray::try_new(&keys, &values).unwrap(); + let dict_array = DictionaryArray::new(keys, values.clone()); let array = dict_array.downcast_dict::().unwrap(); assert_eq!(26, sum_array::(array).unwrap()); let keys = Int8Array::from(vec![None, None, None]); - let dict_array = DictionaryArray::try_new(&keys, &values).unwrap(); + let dict_array = DictionaryArray::new(keys, values.clone()); let array = dict_array.downcast_dict::().unwrap(); assert!(sum_array::(array).is_none()); } @@ -1166,8 +1168,9 @@ mod tests { fn test_max_min_dyn() { let values = Int8Array::from_iter_values([10_i8, 11, 12, 13, 14, 15, 16, 17]); let keys = Int8Array::from_iter_values([2_i8, 3, 4]); + let values = Arc::new(values) as ArrayRef; - let dict_array = DictionaryArray::try_new(&keys, &values).unwrap(); + let dict_array = DictionaryArray::new(keys, values.clone()); let array = dict_array.downcast_dict::().unwrap(); assert_eq!(14, max_array::(array).unwrap()); @@ -1179,14 +1182,14 @@ mod tests { assert_eq!(1, min_array::(&a).unwrap()); let keys = Int8Array::from(vec![Some(2_i8), None, Some(7)]); - let dict_array = DictionaryArray::try_new(&keys, &values).unwrap(); + let dict_array = DictionaryArray::new(keys, values.clone()); let array = dict_array.downcast_dict::().unwrap(); assert_eq!(17, max_array::(array).unwrap()); let array = dict_array.downcast_dict::().unwrap(); assert_eq!(12, min_array::(array).unwrap()); let keys = Int8Array::from(vec![None, None, None]); - let dict_array = DictionaryArray::try_new(&keys, &values).unwrap(); + let dict_array = DictionaryArray::new(keys, values.clone()); let array = dict_array.downcast_dict::().unwrap(); assert!(max_array::(array).is_none()); let array = dict_array.downcast_dict::().unwrap(); @@ -1198,7 +1201,7 @@ mod tests { let values = Float32Array::from(vec![5.0_f32, 2.0_f32, f32::NAN]); let keys = Int8Array::from_iter_values([0_i8, 1, 2]); - let dict_array = DictionaryArray::try_new(&keys, &values).unwrap(); + let dict_array = DictionaryArray::new(keys, Arc::new(values)); let array = dict_array.downcast_dict::().unwrap(); assert!(max_array::(array).unwrap().is_nan()); diff --git a/arrow-arith/src/arithmetic.rs b/arrow-arith/src/arithmetic.rs index 6c0fd497efbe..a8ff163aa1d2 100644 --- a/arrow-arith/src/arithmetic.rs +++ b/arrow-arith/src/arithmetic.rs @@ -3426,11 +3426,11 @@ mod tests { fn test_dict_decimal() { let values = Decimal128Array::from_iter_values([0, 1, 2, 3, 4, 5]); let keys = Int8Array::from_iter_values([1_i8, 2, 5, 4, 3, 0]); - let array1 = DictionaryArray::try_new(&keys, &values).unwrap(); + let array1 = DictionaryArray::new(keys, Arc::new(values)); let values = Decimal128Array::from_iter_values([7, -3, 4, 3, 5]); let keys = Int8Array::from_iter_values([0_i8, 0, 1, 2, 3, 4]); - let array2 = DictionaryArray::try_new(&keys, &values).unwrap(); + let array2 = DictionaryArray::new(keys, Arc::new(values)); let result = add_dyn(&array1, &array2).unwrap(); let expected = @@ -3452,7 +3452,7 @@ mod tests { ]); let keys = Int8Array::from(vec![Some(1_i8), None, Some(5), Some(4), Some(3), None]); - let array1 = DictionaryArray::try_new(&keys, &values).unwrap(); + let array1 = DictionaryArray::new(keys, Arc::new(values)); let values = Decimal256Array::from_iter_values([ i256::from_i128(7), @@ -3463,7 +3463,7 @@ mod tests { ]); let keys = Int8Array::from(vec![Some(0_i8), Some(0), None, Some(2), Some(3), Some(4)]); - let array2 = DictionaryArray::try_new(&keys, &values).unwrap(); + let array2 = DictionaryArray::new(keys, Arc::new(values)); let result = add_dyn(&array1, &array2).unwrap(); let expected = Arc::new(Decimal256Array::from(vec![ diff --git a/arrow-arith/src/arity.rs b/arrow-arith/src/arity.rs index d69bbde8d056..2f1f6c345b32 100644 --- a/arrow-arith/src/arity.rs +++ b/arrow-arith/src/arity.rs @@ -603,10 +603,7 @@ mod tests { fn test_unary_dict_mut() { let values = Int32Array::from(vec![Some(10), Some(20), None]); let keys = Int8Array::from_iter_values([0, 0, 1, 2]); - let dictionary = DictionaryArray::::try_new(&keys, &values).unwrap(); - - drop(keys); - drop(values); + let dictionary = DictionaryArray::new(keys, Arc::new(values)); let updated = dictionary.unary_mut::<_, Int32Type>(|x| x + 1).unwrap(); let typed = updated.downcast_dict::().unwrap(); diff --git a/arrow-arith/src/temporal.rs b/arrow-arith/src/temporal.rs index ac76358ef2dd..f62e7e9a653a 100644 --- a/arrow-arith/src/temporal.rs +++ b/arrow-arith/src/temporal.rs @@ -17,9 +17,10 @@ //! Defines temporal kernels for time and date related functions. -use chrono::{DateTime, Datelike, NaiveDateTime, NaiveTime, Offset, Timelike}; use std::sync::Arc; +use chrono::{DateTime, Datelike, NaiveDateTime, NaiveTime, Offset, Timelike}; + use arrow_array::builder::*; use arrow_array::iterator::ArrayIter; use arrow_array::temporal_conversions::{ @@ -970,12 +971,14 @@ mod tests { .with_timezone("+01:00".to_string()); let keys = Int8Array::from_iter_values([0_i8, 0, 1, 2, 1]); - let dict = DictionaryArray::try_new(&keys, &a).unwrap(); + let dict = DictionaryArray::try_new(keys.clone(), Arc::new(a)).unwrap(); let b = hour_dyn(&dict).unwrap(); - let expected_dict = - DictionaryArray::try_new(&keys, &Int32Array::from(vec![11, 21, 7])).unwrap(); + let expected_dict = DictionaryArray::new( + keys.clone(), + Arc::new(Int32Array::from(vec![11, 21, 7])), + ); let expected = Arc::new(expected_dict) as ArrayRef; assert_eq!(&expected, &b); @@ -984,7 +987,7 @@ mod tests { let b_old = minute_dyn(&dict).unwrap(); let expected_dict = - DictionaryArray::try_new(&keys, &Int32Array::from(vec![1, 2, 3])).unwrap(); + DictionaryArray::new(keys.clone(), Arc::new(Int32Array::from(vec![1, 2, 3]))); let expected = Arc::new(expected_dict) as ArrayRef; assert_eq!(&expected, &b); assert_eq!(&expected, &b_old); @@ -994,7 +997,7 @@ mod tests { let b_old = second_dyn(&dict).unwrap(); let expected_dict = - DictionaryArray::try_new(&keys, &Int32Array::from(vec![1, 2, 3])).unwrap(); + DictionaryArray::new(keys.clone(), Arc::new(Int32Array::from(vec![1, 2, 3]))); let expected = Arc::new(expected_dict) as ArrayRef; assert_eq!(&expected, &b); assert_eq!(&expected, &b_old); @@ -1003,8 +1006,7 @@ mod tests { time_fraction_dyn(&dict, "nanosecond", |t| t.nanosecond() as i32).unwrap(); let expected_dict = - DictionaryArray::try_new(&keys, &Int32Array::from(vec![0, 0, 0, 0, 0])) - .unwrap(); + DictionaryArray::new(keys, Arc::new(Int32Array::from(vec![0, 0, 0, 0, 0]))); let expected = Arc::new(expected_dict) as ArrayRef; assert_eq!(&expected, &b); } @@ -1015,15 +1017,14 @@ mod tests { vec![Some(1514764800000), Some(1550636625000)].into(); let keys = Int8Array::from_iter_values([0_i8, 1, 1, 0]); - let dict = DictionaryArray::try_new(&keys, &a).unwrap(); + let dict = DictionaryArray::new(keys.clone(), Arc::new(a)); let b = year_dyn(&dict).unwrap(); - let expected_dict = DictionaryArray::try_new( - &keys, - &Int32Array::from(vec![2018, 2019, 2019, 2018]), - ) - .unwrap(); + let expected_dict = DictionaryArray::new( + keys, + Arc::new(Int32Array::from(vec![2018, 2019, 2019, 2018])), + ); let expected = Arc::new(expected_dict) as ArrayRef; assert_eq!(&expected, &b); } @@ -1036,21 +1037,21 @@ mod tests { vec![Some(1514764800000), Some(1566275025000)].into(); let keys = Int8Array::from_iter_values([0_i8, 1, 1, 0]); - let dict = DictionaryArray::try_new(&keys, &a).unwrap(); + let dict = DictionaryArray::new(keys.clone(), Arc::new(a)); let b = quarter_dyn(&dict).unwrap(); - let expected_dict = - DictionaryArray::try_new(&keys, &Int32Array::from(vec![1, 3, 3, 1])).unwrap(); - let expected = Arc::new(expected_dict) as ArrayRef; - assert_eq!(&expected, &b); + let expected = DictionaryArray::new( + keys.clone(), + Arc::new(Int32Array::from(vec![1, 3, 3, 1])), + ); + assert_eq!(b.as_ref(), &expected); let b = month_dyn(&dict).unwrap(); - let expected_dict = - DictionaryArray::try_new(&keys, &Int32Array::from(vec![1, 8, 8, 1])).unwrap(); - let expected = Arc::new(expected_dict) as ArrayRef; - assert_eq!(&expected, &b); + let expected = + DictionaryArray::new(keys, Arc::new(Int32Array::from(vec![1, 8, 8, 1]))); + assert_eq!(b.as_ref(), &expected); } #[test] @@ -1061,57 +1062,37 @@ mod tests { vec![Some(1514764800000), Some(1550636625000)].into(); let keys = Int8Array::from(vec![Some(0_i8), Some(1), Some(1), Some(0), None]); - let dict = DictionaryArray::try_new(&keys, &a).unwrap(); + let dict = DictionaryArray::new(keys.clone(), Arc::new(a)); let b = num_days_from_monday_dyn(&dict).unwrap(); - let expected_dict = DictionaryArray::try_new( - &keys, - &Int32Array::from(vec![Some(0), Some(2), Some(2), Some(0), None]), - ) - .unwrap(); - let expected = Arc::new(expected_dict) as ArrayRef; - assert_eq!(&expected, &b); + let a = Int32Array::from(vec![Some(0), Some(2), Some(2), Some(0), None]); + let expected = DictionaryArray::new(keys.clone(), Arc::new(a)); + assert_eq!(b.as_ref(), &expected); let b = num_days_from_sunday_dyn(&dict).unwrap(); - let expected_dict = DictionaryArray::try_new( - &keys, - &Int32Array::from(vec![Some(1), Some(3), Some(3), Some(1), None]), - ) - .unwrap(); - let expected = Arc::new(expected_dict) as ArrayRef; - assert_eq!(&expected, &b); + let a = Int32Array::from(vec![Some(1), Some(3), Some(3), Some(1), None]); + let expected = DictionaryArray::new(keys.clone(), Arc::new(a)); + assert_eq!(b.as_ref(), &expected); let b = day_dyn(&dict).unwrap(); - let expected_dict = DictionaryArray::try_new( - &keys, - &Int32Array::from(vec![Some(1), Some(20), Some(20), Some(1), None]), - ) - .unwrap(); - let expected = Arc::new(expected_dict) as ArrayRef; - assert_eq!(&expected, &b); + let a = Int32Array::from(vec![Some(1), Some(20), Some(20), Some(1), None]); + let expected = DictionaryArray::new(keys.clone(), Arc::new(a)); + assert_eq!(b.as_ref(), &expected); let b = doy_dyn(&dict).unwrap(); - let expected_dict = DictionaryArray::try_new( - &keys, - &Int32Array::from(vec![Some(1), Some(51), Some(51), Some(1), None]), - ) - .unwrap(); - let expected = Arc::new(expected_dict) as ArrayRef; - assert_eq!(&expected, &b); + let a = Int32Array::from(vec![Some(1), Some(51), Some(51), Some(1), None]); + let expected = DictionaryArray::new(keys.clone(), Arc::new(a)); + assert_eq!(b.as_ref(), &expected); let b = week_dyn(&dict).unwrap(); - let expected_dict = DictionaryArray::try_new( - &keys, - &Int32Array::from(vec![Some(1), Some(8), Some(8), Some(1), None]), - ) - .unwrap(); - let expected = Arc::new(expected_dict) as ArrayRef; - assert_eq!(&expected, &b); + let a = Int32Array::from(vec![Some(1), Some(8), Some(8), Some(1), None]); + let expected = DictionaryArray::new(keys, Arc::new(a)); + assert_eq!(b.as_ref(), &expected); } #[test] @@ -1129,14 +1110,11 @@ mod tests { assert_eq!(453_000_000, b.value(1)); let keys = Int8Array::from(vec![Some(0_i8), Some(1), Some(1)]); - let dict = DictionaryArray::try_new(&keys, &a).unwrap(); + let dict = DictionaryArray::new(keys.clone(), Arc::new(a)); let b = nanosecond_dyn(&dict).unwrap(); - let expected_dict = DictionaryArray::try_new( - &keys, - &Int32Array::from(vec![None, Some(453_000_000)]), - ) - .unwrap(); + let a = Int32Array::from(vec![None, Some(453_000_000)]); + let expected_dict = DictionaryArray::new(keys, Arc::new(a)); let expected = Arc::new(expected_dict) as ArrayRef; assert_eq!(&expected, &b); } diff --git a/arrow-array/src/array/dictionary_array.rs b/arrow-array/src/array/dictionary_array.rs index f25a077a81ba..75fd4c6d0d68 100644 --- a/arrow-array/src/array/dictionary_array.rs +++ b/arrow-array/src/array/dictionary_array.rs @@ -20,8 +20,8 @@ use crate::cast::AsArray; use crate::iterator::ArrayIter; use crate::types::*; use crate::{ - make_array, Array, ArrayAccessor, ArrayRef, ArrowPrimitiveType, PrimitiveArray, - StringArray, + make_array, Array, ArrayAccessor, ArrayRef, ArrowNativeTypeOp, ArrowPrimitiveType, + PrimitiveArray, StringArray, }; use arrow_buffer::buffer::NullBuffer; use arrow_buffer::ArrowNativeType; @@ -196,12 +196,13 @@ pub type UInt64DictionaryArray = DictionaryArray; /// Example from existing arrays: /// /// ``` +/// use std::sync::Arc; /// use arrow_array::{DictionaryArray, Int8Array, StringArray, types::Int8Type}; /// // You can form your own DictionaryArray by providing the /// // values (dictionary) and keys (indexes into the dictionary): /// let values = StringArray::from_iter_values(["a", "b", "c"]); /// let keys = Int8Array::from_iter_values([0, 0, 1, 2]); -/// let array = DictionaryArray::::try_new(&keys, &values).unwrap(); +/// let array = DictionaryArray::::try_new(keys, Arc::new(values)).unwrap(); /// let expected: DictionaryArray:: = vec!["a", "a", "b", "c"] /// .into_iter() /// .collect(); @@ -237,33 +238,72 @@ impl Clone for DictionaryArray { impl DictionaryArray { /// Attempt to create a new DictionaryArray with a specified keys /// (indexes into the dictionary) and values (dictionary) - /// array. Returns an error if there are any keys that are outside - /// of the dictionary array. + /// array. + /// + /// # Panics + /// + /// Panics if [`Self::try_new`] returns an error + pub fn new(keys: PrimitiveArray, values: ArrayRef) -> Self { + Self::try_new(keys, values).unwrap() + } + + /// Attempt to create a new DictionaryArray with a specified keys + /// (indexes into the dictionary) and values (dictionary) + /// array. + /// + /// # Errors + /// + /// Returns an error if any `keys[i] >= values.len() || keys[i] < 0` pub fn try_new( - keys: &PrimitiveArray, - values: &dyn Array, + keys: PrimitiveArray, + values: ArrayRef, ) -> Result { - let dict_data_type = DataType::Dictionary( + let data_type = DataType::Dictionary( Box::new(keys.data_type().clone()), Box::new(values.data_type().clone()), ); - // Note: This use the ArrayDataBuilder::build_unchecked and afterwards - // call the new function which only validates that the keys are in bounds. - let data = keys.to_data(); - let builder = data - .into_builder() - .data_type(dict_data_type) - .add_child_data(values.to_data()); + let zero = K::Native::usize_as(0); + let values_len = values.len(); - // Safety: `validate` ensures key type is correct, and - // `validate_values` ensures all offsets are within range - let array = unsafe { builder.build_unchecked() }; + if let Some((idx, v)) = keys.values().iter().enumerate().find(|(idx, v)| { + (v.is_lt(zero) || v.as_usize() >= values_len) && keys.is_valid(*idx) + }) { + return Err(ArrowError::InvalidArgumentError(format!( + "Invalid dictionary key {v:?} at index {idx}, expected 0 <= key < {values_len}", + ))); + } - array.validate()?; - array.validate_values()?; + Ok(Self { + data_type, + keys, + values, + is_ordered: false, + }) + } + + /// Create a new [`DictionaryArray`] without performing validation + /// + /// # Safety + /// + /// Safe provided [`Self::try_new`] would not return an error + pub unsafe fn new_unchecked(keys: PrimitiveArray, values: ArrayRef) -> Self { + let data_type = DataType::Dictionary( + Box::new(keys.data_type().clone()), + Box::new(values.data_type().clone()), + ); + + Self { + data_type, + keys, + values, + is_ordered: false, + } + } - Ok(array.into()) + /// Deconstruct this array into its constituent parts + pub fn into_parts(self) -> (PrimitiveArray, ArrayRef) { + (self.keys, self.values) } /// Return an array view of the keys of this dictionary as a PrimitiveArray. @@ -362,9 +402,9 @@ impl DictionaryArray { /// Panics if `values` has a length less than the current values /// /// ``` - /// use arrow_array::builder::PrimitiveDictionaryBuilder; - /// use arrow_array::{Int8Array, Int64Array, ArrayAccessor}; - /// use arrow_array::types::{Int32Type, Int8Type}; + /// # use arrow_array::builder::PrimitiveDictionaryBuilder; + /// # use arrow_array::{Int8Array, Int64Array, ArrayAccessor}; + /// # use arrow_array::types::{Int32Type, Int8Type}; /// /// // Construct a Dict(Int32, Int8) /// let mut builder = PrimitiveDictionaryBuilder::::with_capacity(2, 200); @@ -431,13 +471,13 @@ impl DictionaryArray { PrimitiveDictionaryBuilder::new_from_builders(key_builder, value_builder) }), (Err(key_array), Ok(mut value_builder)) => { - Err(Self::try_new(&key_array, &value_builder.finish()).unwrap()) + Err(Self::try_new(key_array, Arc::new(value_builder.finish())).unwrap()) } (Ok(mut key_builder), Err(value_array)) => { - Err(Self::try_new(&key_builder.finish(), &value_array).unwrap()) + Err(Self::try_new(key_builder.finish(), Arc::new(value_array)).unwrap()) } (Err(key_array), Err(value_array)) => { - Err(Self::try_new(&key_array, &value_array).unwrap()) + Err(Self::try_new(key_array, Arc::new(value_array)).unwrap()) } } } @@ -453,13 +493,12 @@ impl DictionaryArray { /// or this function may panic. /// # Example /// ``` - /// use arrow_array::{Array, ArrayAccessor, DictionaryArray, StringArray, types::{Int8Type, Int32Type}}; - /// use arrow_array::{Int8Array, Int32Array}; + /// # use std::sync::Arc; + /// # use arrow_array::{Array, ArrayAccessor, DictionaryArray, StringArray, types::{Int8Type, Int32Type}}; + /// # use arrow_array::{Int8Array, Int32Array}; /// let values = Int32Array::from(vec![Some(10), Some(20), None]); /// let keys = Int8Array::from_iter_values([0, 0, 1, 2]); - /// let dictionary = DictionaryArray::::try_new(&keys, &values).unwrap(); - /// drop(keys); - /// drop(values); + /// let dictionary = DictionaryArray::::try_new(keys, Arc::new(values)).unwrap(); /// let c = dictionary.unary_mut::<_, Int32Type>(|x| x + 1).unwrap(); /// let typed = c.downcast_dict::().unwrap(); /// assert_eq!(typed.value(0), 11); @@ -807,7 +846,7 @@ mod tests { use super::*; use crate::builder::PrimitiveDictionaryBuilder; use crate::cast::as_dictionary_array; - use crate::types::{Int16Type, Int32Type, Int8Type, UInt32Type, UInt8Type}; + use crate::types::{Int32Type, Int8Type, UInt32Type, UInt8Type}; use crate::{Int16Array, Int32Array, Int8Array}; use arrow_buffer::{Buffer, ToByteSlice}; use std::sync::Arc; @@ -974,7 +1013,7 @@ mod tests { let keys = Int16Array::from_iter_values([2_i16, 3, 4]); // Construct a dictionary array from the above two - let dict_array = DictionaryArray::::try_new(&keys, &values).unwrap(); + let dict_array = DictionaryArray::new(keys, Arc::new(values)); let mut key_iter = dict_array.keys_iter(); assert_eq!(2, key_iter.next().unwrap().unwrap()); @@ -1021,7 +1060,7 @@ mod tests { let keys = Int8Array::from(vec![Some(2), None, Some(1)]); let values = StringArray::from(vec!["foo", "bar", "baz", "blarg"]); - let array = DictionaryArray::try_new(&keys, &values).unwrap(); + let array = DictionaryArray::new(keys, Arc::new(values)); assert_eq!(array.key(0), Some(2)); assert_eq!(array.key(1), None); assert_eq!(array.key(2), Some(1)); @@ -1034,7 +1073,7 @@ mod tests { .collect(); let keys: Int32Array = [Some(0), Some(2), None, Some(1)].into_iter().collect(); - let array = DictionaryArray::::try_new(&keys, &values).unwrap(); + let array = DictionaryArray::new(keys, Arc::new(values)); assert_eq!(array.keys().data_type(), &DataType::Int32); assert_eq!(array.values().data_type(), &DataType::Utf8); @@ -1057,23 +1096,23 @@ mod tests { #[test] #[should_panic( - expected = "Value at position 1 out of bounds: 3 (should be in [0, 1])" + expected = "Invalid dictionary key 3 at index 1, expected 0 <= key < 2" )] fn test_try_new_index_too_large() { let values: StringArray = [Some("foo"), Some("bar")].into_iter().collect(); // dictionary only has 2 values, so offset 3 is out of bounds let keys: Int32Array = [Some(0), Some(3)].into_iter().collect(); - DictionaryArray::::try_new(&keys, &values).unwrap(); + DictionaryArray::new(keys, Arc::new(values)); } #[test] #[should_panic( - expected = "Value at position 0 out of bounds: -100 (should be in [0, 1])" + expected = "Invalid dictionary key -100 at index 0, expected 0 <= key < 2" )] fn test_try_new_index_too_small() { let values: StringArray = [Some("foo"), Some("bar")].into_iter().collect(); let keys: Int32Array = [Some(-100)].into_iter().collect(); - DictionaryArray::::try_new(&keys, &values).unwrap(); + DictionaryArray::new(keys, Arc::new(values)); } #[test] @@ -1090,14 +1129,12 @@ mod tests { let values = Int32Array::from_iter_values([10_i32, 12, 15]); let keys = Int8Array::from_iter_values([1_i8, 0, 2, 0]); - let dict_array = DictionaryArray::::try_new(&keys, &values).unwrap(); + let dict_array = DictionaryArray::new(keys, Arc::new(values)); let boxed: ArrayRef = Arc::new(dict_array); let col: DictionaryArray = as_dictionary_array(&boxed).clone(); drop(boxed); - drop(keys); - drop(values); let mut builder = col.into_primitive_dict_builder::().unwrap(); @@ -1111,7 +1148,7 @@ mod tests { let values = Int32Array::from_iter_values([4_i32, 2, 1]); let keys = Int8Array::from_iter_values([1_i8, 0, 2, 0]); - let expected = DictionaryArray::::try_new(&keys, &values).unwrap(); + let expected = DictionaryArray::new(keys, Arc::new(values)); let new_array = builder.finish(); assert_eq!(expected, new_array); @@ -1122,7 +1159,7 @@ mod tests { let values = Int32Array::from_iter_values([10_i32, 12, 15]); let keys = Int8Array::from_iter_values([1_i8, 0, 2, 0]); - let dict_array = DictionaryArray::::try_new(&keys, &values).unwrap(); + let dict_array = DictionaryArray::new(keys, Arc::new(values)); let boxed: ArrayRef = Arc::new(dict_array); @@ -1135,7 +1172,7 @@ mod tests { let values = Int32Array::from_iter_values([10_i32, 12, 15]); let keys = Int8Array::from_iter_values([1_i8, 0, 2, 0]); - let expected = DictionaryArray::::try_new(&keys, &values).unwrap(); + let expected = DictionaryArray::new(keys, Arc::new(values)); assert_eq!(expected, returned); } } diff --git a/arrow-flight/src/encode.rs b/arrow-flight/src/encode.rs index f8915a96320d..f97311d6f9e3 100644 --- a/arrow-flight/src/encode.rs +++ b/arrow-flight/src/encode.rs @@ -719,7 +719,7 @@ mod tests { // large dictionary (1024 distinct values) that are used throughout the array let values = StringArray::from_iter_values((0..1024).map(|i| "******".repeat(i))); let keys = Int32Array::from_iter_values((0..3000).map(|i| (3000 - i) % 1024)); - let array = DictionaryArray::::try_new(&keys, &values).unwrap(); + let array = DictionaryArray::new(keys, Arc::new(values)); let batch = RecordBatch::try_from_iter(vec![("a1", Arc::new(array) as _)]).unwrap(); diff --git a/arrow-ipc/src/reader.rs b/arrow-ipc/src/reader.rs index 3d803b62728c..e41119937339 100644 --- a/arrow-ipc/src/reader.rs +++ b/arrow-ipc/src/reader.rs @@ -1380,8 +1380,7 @@ mod tests { let array12_values = StringArray::from(vec!["x", "yy", "zzz"]); let array12_keys = Int8Array::from_iter_values([1, 1, 2]); - let array12 = - DictionaryArray::::try_new(&array12_keys, &array12_values).unwrap(); + let array12 = DictionaryArray::new(array12_keys, Arc::new(array12_values)); let array13 = StringArray::from(vec!["a", "bb", "ccc"]); @@ -1693,13 +1692,12 @@ mod tests { #[test] fn test_roundtrip_stream_nested_dict_of_map_of_dict() { let values = StringArray::from(vec![Some("a"), None, Some("b"), Some("c")]); + let values = Arc::new(values) as ArrayRef; let value_dict_keys = Int8Array::from_iter_values([0, 1, 1, 2, 3, 1]); - let value_dict_array = - DictionaryArray::::try_new(&value_dict_keys, &values).unwrap(); + let value_dict_array = DictionaryArray::new(value_dict_keys, values.clone()); let key_dict_keys = Int8Array::from_iter_values([0, 0, 2, 1, 1, 3]); - let key_dict_array = - DictionaryArray::::try_new(&key_dict_keys, &values).unwrap(); + let key_dict_array = DictionaryArray::new(key_dict_keys, values); let keys_field = Field::new_dict( "keys", @@ -1738,8 +1736,7 @@ mod tests { let map_array = MapArray::from(map_data); let dict_keys = Int8Array::from_iter_values([0, 1, 1, 2, 2, 1]); - let dict_dict_array = - DictionaryArray::::try_new(&dict_keys, &map_array).unwrap(); + let dict_dict_array = DictionaryArray::new(dict_keys, Arc::new(map_array)); let schema = Arc::new(Schema::new(vec![Field::new( "f1", @@ -1761,7 +1758,7 @@ mod tests { ) { let values = StringArray::from(vec![Some("a"), None, Some("c"), None]); let keys = Int8Array::from_iter_values([0, 0, 1, 2, 0, 1, 3]); - let dict_array = DictionaryArray::::try_new(&keys, &values).unwrap(); + let dict_array = DictionaryArray::new(keys, Arc::new(values)); let dict_data = dict_array.to_data(); let value_offsets = Buffer::from_slice_ref(offsets); @@ -1775,8 +1772,7 @@ mod tests { let list_array = GenericListArray::::from(list_data); let keys_for_dict = Int8Array::from_iter_values([0, 3, 0, 1, 1, 2, 0, 1, 3]); - let dict_dict_array = - DictionaryArray::::try_new(&keys_for_dict, &list_array).unwrap(); + let dict_dict_array = DictionaryArray::new(keys_for_dict, Arc::new(list_array)); let schema = Arc::new(Schema::new(vec![Field::new( "f1", @@ -1824,8 +1820,8 @@ mod tests { fn test_roundtrip_stream_dict_of_fixed_size_list_of_dict() { let values = StringArray::from(vec![Some("a"), None, Some("c"), None]); let keys = Int8Array::from_iter_values([0, 0, 1, 2, 0, 1, 3, 1, 2]); - let dict_array = DictionaryArray::::try_new(&keys, &values).unwrap(); - let dict_data = dict_array.to_data(); + let dict_array = DictionaryArray::new(keys, Arc::new(values)); + let dict_data = dict_array.into_data(); let list_data_type = DataType::FixedSizeList( Arc::new(Field::new_dict( @@ -1845,8 +1841,7 @@ mod tests { let list_array = FixedSizeListArray::from(list_data); let keys_for_dict = Int8Array::from_iter_values([0, 1, 0, 1, 1, 2, 0, 1, 2]); - let dict_dict_array = - DictionaryArray::::try_new(&keys_for_dict, &list_array).unwrap(); + let dict_dict_array = DictionaryArray::new(keys_for_dict, Arc::new(list_array)); let schema = Arc::new(Schema::new(vec![Field::new( "f1", diff --git a/arrow-ipc/src/writer.rs b/arrow-ipc/src/writer.rs index 08ddd1812bb7..0c9ca17b7426 100644 --- a/arrow-ipc/src/writer.rs +++ b/arrow-ipc/src/writer.rs @@ -1841,7 +1841,7 @@ mod tests { let keys: Int32Array = [Some(0), Some(2), None, Some(1)].into_iter().collect(); - let array = DictionaryArray::::try_new(&keys, &values).unwrap(); + let array = DictionaryArray::new(keys, Arc::new(values)); let schema = Schema::new(vec![Field::new("dict", array.data_type().clone(), true)]); diff --git a/arrow-ord/src/comparison.rs b/arrow-ord/src/comparison.rs index 2927354da291..c83956960050 100644 --- a/arrow-ord/src/comparison.rs +++ b/arrow-ord/src/comparison.rs @@ -4616,11 +4616,12 @@ mod tests { fn test_eq_dyn_neq_dyn_dictionary_i8_array() { // Construct a value array let values = Int8Array::from_iter_values([10_i8, 11, 12, 13, 14, 15, 16, 17]); + let values = Arc::new(values) as ArrayRef; let keys1 = Int8Array::from_iter_values([2_i8, 3, 4]); let keys2 = Int8Array::from_iter_values([2_i8, 4, 4]); - let dict_array1 = DictionaryArray::try_new(&keys1, &values).unwrap(); - let dict_array2 = DictionaryArray::try_new(&keys2, &values).unwrap(); + let dict_array1 = DictionaryArray::new(keys1, values.clone()); + let dict_array2 = DictionaryArray::new(keys2, values.clone()); let result = eq_dyn(&dict_array1, &dict_array2); assert_eq!(result.unwrap(), BooleanArray::from(vec![true, false, true])); @@ -4636,13 +4637,12 @@ mod tests { #[cfg(feature = "dyn_cmp_dict")] fn test_eq_dyn_neq_dyn_dictionary_u64_array() { let values = UInt64Array::from_iter_values([10_u64, 11, 12, 13, 14, 15, 16, 17]); + let values = Arc::new(values) as ArrayRef; let keys1 = UInt64Array::from_iter_values([1_u64, 3, 4]); let keys2 = UInt64Array::from_iter_values([2_u64, 3, 5]); - let dict_array1 = - DictionaryArray::::try_new(&keys1, &values).unwrap(); - let dict_array2 = - DictionaryArray::::try_new(&keys2, &values).unwrap(); + let dict_array1 = DictionaryArray::new(keys1, values.clone()); + let dict_array2 = DictionaryArray::new(keys2, values.clone()); let result = eq_dyn(&dict_array1, &dict_array2); assert_eq!( @@ -4689,13 +4689,12 @@ mod tests { .into_iter() .map(|b| Some(b.as_bytes())) .collect(); + let values = Arc::new(values) as ArrayRef; let keys1 = UInt64Array::from_iter_values([0_u64, 1, 2]); let keys2 = UInt64Array::from_iter_values([0_u64, 2, 1]); - let dict_array1 = - DictionaryArray::::try_new(&keys1, &values).unwrap(); - let dict_array2 = - DictionaryArray::::try_new(&keys2, &values).unwrap(); + let dict_array1 = DictionaryArray::new(keys1, values.clone()); + let dict_array2 = DictionaryArray::new(keys2, values.clone()); let result = eq_dyn(&dict_array1, &dict_array2); assert_eq!( @@ -4711,13 +4710,12 @@ mod tests { #[cfg(feature = "dyn_cmp_dict")] fn test_eq_dyn_neq_dyn_dictionary_interval_array() { let values = IntervalDayTimeArray::from(vec![1, 6, 10, 2, 3, 5]); + let values = Arc::new(values) as ArrayRef; let keys1 = UInt64Array::from_iter_values([1_u64, 0, 3]); let keys2 = UInt64Array::from_iter_values([2_u64, 0, 3]); - let dict_array1 = - DictionaryArray::::try_new(&keys1, &values).unwrap(); - let dict_array2 = - DictionaryArray::::try_new(&keys2, &values).unwrap(); + let dict_array1 = DictionaryArray::new(keys1, values.clone()); + let dict_array2 = DictionaryArray::new(keys2, values.clone()); let result = eq_dyn(&dict_array1, &dict_array2); assert_eq!(result.unwrap(), BooleanArray::from(vec![false, true, true])); @@ -4733,13 +4731,12 @@ mod tests { #[cfg(feature = "dyn_cmp_dict")] fn test_eq_dyn_neq_dyn_dictionary_date_array() { let values = Date32Array::from(vec![1, 6, 10, 2, 3, 5]); + let values = Arc::new(values) as ArrayRef; let keys1 = UInt64Array::from_iter_values([1_u64, 0, 3]); let keys2 = UInt64Array::from_iter_values([2_u64, 0, 3]); - let dict_array1 = - DictionaryArray::::try_new(&keys1, &values).unwrap(); - let dict_array2 = - DictionaryArray::::try_new(&keys2, &values).unwrap(); + let dict_array1 = DictionaryArray::new(keys1, values.clone()); + let dict_array2 = DictionaryArray::new(keys2, values.clone()); let result = eq_dyn(&dict_array1, &dict_array2); assert_eq!(result.unwrap(), BooleanArray::from(vec![false, true, true])); @@ -4755,13 +4752,12 @@ mod tests { #[cfg(feature = "dyn_cmp_dict")] fn test_eq_dyn_neq_dyn_dictionary_bool_array() { let values = BooleanArray::from(vec![true, false]); + let values = Arc::new(values) as ArrayRef; let keys1 = UInt64Array::from_iter_values([1_u64, 1, 1]); let keys2 = UInt64Array::from_iter_values([0_u64, 1, 0]); - let dict_array1 = - DictionaryArray::::try_new(&keys1, &values).unwrap(); - let dict_array2 = - DictionaryArray::::try_new(&keys2, &values).unwrap(); + let dict_array1 = DictionaryArray::new(keys1, values.clone()); + let dict_array2 = DictionaryArray::new(keys2, values.clone()); let result = eq_dyn(&dict_array1, &dict_array2); assert_eq!( @@ -4778,11 +4774,12 @@ mod tests { fn test_lt_dyn_gt_dyn_dictionary_i8_array() { // Construct a value array let values = Int8Array::from_iter_values([10_i8, 11, 12, 13, 14, 15, 16, 17]); + let values = Arc::new(values) as ArrayRef; let keys1 = Int8Array::from_iter_values([3_i8, 4, 4]); let keys2 = Int8Array::from_iter_values([4_i8, 3, 4]); - let dict_array1 = DictionaryArray::try_new(&keys1, &values).unwrap(); - let dict_array2 = DictionaryArray::try_new(&keys2, &values).unwrap(); + let dict_array1 = DictionaryArray::new(keys1, values.clone()); + let dict_array2 = DictionaryArray::new(keys2, values.clone()); let result = lt_dyn(&dict_array1, &dict_array2); assert_eq!( @@ -4807,13 +4804,12 @@ mod tests { #[cfg(feature = "dyn_cmp_dict")] fn test_lt_dyn_gt_dyn_dictionary_bool_array() { let values = BooleanArray::from(vec![true, false]); + let values = Arc::new(values) as ArrayRef; let keys1 = UInt64Array::from_iter_values([1_u64, 1, 0]); let keys2 = UInt64Array::from_iter_values([0_u64, 1, 1]); - let dict_array1 = - DictionaryArray::::try_new(&keys1, &values).unwrap(); - let dict_array2 = - DictionaryArray::::try_new(&keys2, &values).unwrap(); + let dict_array1 = DictionaryArray::new(keys1, values.clone()); + let dict_array2 = DictionaryArray::new(keys2, values.clone()); let result = lt_dyn(&dict_array1, &dict_array2); assert_eq!( @@ -4852,7 +4848,7 @@ mod tests { let values = Int8Array::from_iter_values([10_i8, 11, 12, 13, 14, 15, 16, 17]); let keys = Int8Array::from_iter_values([2_i8, 3, 4]); - let dict_array = DictionaryArray::try_new(&keys, &values).unwrap(); + let dict_array = DictionaryArray::new(keys, Arc::new(values)); let array = Int8Array::from_iter([Some(12_i8), None, Some(14)]); @@ -4887,7 +4883,7 @@ mod tests { let values = Int8Array::from_iter_values([10_i8, 11, 12, 13, 14, 15, 16, 17]); let keys = Int8Array::from_iter_values([2_i8, 3, 4]); - let dict_array = DictionaryArray::try_new(&keys, &values).unwrap(); + let dict_array = DictionaryArray::new(keys, Arc::new(values)); let array = Int8Array::from_iter([Some(12_i8), None, Some(11)]); @@ -5497,7 +5493,7 @@ mod tests { .collect(); let keys = UInt64Array::from(vec![Some(0_u64), None, Some(2), Some(2)]); - let dict_array = DictionaryArray::::try_new(&keys, &values).unwrap(); + let dict_array = DictionaryArray::new(keys, Arc::new(values)); let array: BinaryArray = ["hello", "", "parquet", "test"] .into_iter() @@ -5538,7 +5534,7 @@ mod tests { .collect(); let keys = UInt64Array::from(vec![Some(0_u64), None, Some(2), Some(2)]); - let dict_array = DictionaryArray::::try_new(&keys, &values).unwrap(); + let dict_array = DictionaryArray::new(keys, Arc::new(values)); let array: BinaryArray = ["hello", "", "parquet", "test"] .into_iter() @@ -5604,7 +5600,7 @@ mod tests { let values = Float16Array::from(vec![f16::NAN, f16::from_f32(8.0), f16::from_f32(10.0)]); let keys = Int8Array::from_iter_values([0_i8, 0, 1, 1, 2]); - let array2 = DictionaryArray::try_new(&keys, &values).unwrap(); + let array2 = DictionaryArray::new(keys, Arc::new(values)); let expected = BooleanArray::from( vec![Some(true), Some(false), Some(true), Some(true), Some(true)], @@ -5622,7 +5618,7 @@ mod tests { .collect(); let values = Float32Array::from(vec![f32::NAN, 8.0, 10.0]); let keys = Int8Array::from_iter_values([0_i8, 0, 1, 1, 2]); - let array2 = DictionaryArray::try_new(&keys, &values).unwrap(); + let array2 = DictionaryArray::new(keys, Arc::new(values)); let expected = BooleanArray::from( vec![Some(true), Some(false), Some(true), Some(true), Some(true)], @@ -5640,7 +5636,7 @@ mod tests { .collect(); let values = Float64Array::from(vec![f64::NAN, 8.0, 10.0]); let keys = Int8Array::from_iter_values([0_i8, 0, 1, 1, 2]); - let array2 = DictionaryArray::try_new(&keys, &values).unwrap(); + let array2 = DictionaryArray::new(keys, Arc::new(values)); let expected = BooleanArray::from( vec![Some(true), Some(false), Some(true), Some(true), Some(true)], @@ -5662,7 +5658,7 @@ mod tests { .collect(); let values = Float16Array::from(vec![f16::NAN, f16::from_f32(8.0), f16::from_f32(9.0), f16::from_f32(10.0), f16::from_f32(1.0)]); let keys = Int8Array::from_iter_values([0_i8, 0, 1, 2, 3, 4]); - let array2 = DictionaryArray::try_new(&keys, &values).unwrap(); + let array2 = DictionaryArray::new(keys, Arc::new(values)); let expected = BooleanArray::from( vec![Some(false), Some(true), Some(false), Some(true), Some(false), Some(false)], @@ -5680,7 +5676,7 @@ mod tests { .collect(); let values = Float32Array::from(vec![f32::NAN, 8.0, 9.0, 10.0, 1.0]); let keys = Int8Array::from_iter_values([0_i8, 0, 1, 2, 3, 4]); - let array2 = DictionaryArray::try_new(&keys, &values).unwrap(); + let array2 = DictionaryArray::new(keys, Arc::new(values)); let expected = BooleanArray::from( vec![Some(false), Some(true), Some(false), Some(true), Some(false), Some(false)], @@ -5698,7 +5694,7 @@ mod tests { .collect(); let values = Float64Array::from(vec![f64::NAN, 8.0, 9.0, 10.0, 1.0]); let keys = Int8Array::from_iter_values([0_i8, 0, 1, 2, 3, 4]); - let array2 = DictionaryArray::try_new(&keys, &values).unwrap(); + let array2 = DictionaryArray::new(keys, Arc::new(values)); let expected = BooleanArray::from( vec![Some(false), Some(true), Some(false), Some(true), Some(false), Some(false)], @@ -5720,7 +5716,7 @@ mod tests { .collect(); let values = Float16Array::from(vec![f16::NAN, f16::from_f32(8.0), f16::from_f32(9.0), f16::from_f32(10.0), f16::from_f32(1.0)]); let keys = Int8Array::from_iter_values([0_i8, 0, 1, 2, 3, 4]); - let array2 = DictionaryArray::try_new(&keys, &values).unwrap(); + let array2 = DictionaryArray::new(keys, Arc::new(values)); let expected = BooleanArray::from( vec![Some(false), Some(false), Some(false), Some(false), Some(true), Some(true)], @@ -5738,7 +5734,7 @@ mod tests { .collect(); let values = Float32Array::from(vec![f32::NAN, 8.0, 9.0, 10.0, 1.0]); let keys = Int8Array::from_iter_values([0_i8, 0, 1, 2, 3, 4]); - let array2 = DictionaryArray::try_new(&keys, &values).unwrap(); + let array2 = DictionaryArray::new(keys, Arc::new(values)); let expected = BooleanArray::from( vec![Some(false), Some(false), Some(false), Some(false), Some(true), Some(true)], @@ -5756,7 +5752,7 @@ mod tests { .collect(); let values = Float64Array::from(vec![f64::NAN, 8.0, 9.0, 10.0, 1.0]); let keys = Int8Array::from_iter_values([0_i8, 0, 1, 2, 3, 4]); - let array2 = DictionaryArray::try_new(&keys, &values).unwrap(); + let array2 = DictionaryArray::new(keys, Arc::new(values)); let expected = BooleanArray::from( vec![Some(false), Some(false), Some(false), Some(false), Some(true), Some(true)], @@ -5777,7 +5773,7 @@ mod tests { let values = BooleanArray::from(test1); let keys = Int8Array::from_iter_values([0_i8, 0, 1, 2]); - let dict_array = DictionaryArray::try_new(&keys, &values).unwrap(); + let dict_array = DictionaryArray::new(keys, Arc::new(values)); let array: BooleanArray = test2.iter().collect(); @@ -5814,7 +5810,7 @@ mod tests { let values = BooleanArray::from(test1); let keys = Int8Array::from_iter_values([0_i8, 0, 1, 2]); - let dict_array = DictionaryArray::try_new(&keys, &values).unwrap(); + let dict_array = DictionaryArray::new(keys, Arc::new(values)); let array: BooleanArray = test2.iter().collect(); @@ -5872,11 +5868,11 @@ mod tests { fn test_cmp_dict_decimal128() { let values = Decimal128Array::from_iter_values([0, 1, 2, 3, 4, 5]); let keys = Int8Array::from_iter_values([1_i8, 2, 5, 4, 3, 0]); - let array1 = DictionaryArray::try_new(&keys, &values).unwrap(); + let array1 = DictionaryArray::new(keys, Arc::new(values)); let values = Decimal128Array::from_iter_values([7, -3, 4, 3, 5]); let keys = Int8Array::from_iter_values([0_i8, 0, 1, 2, 3, 4]); - let array2 = DictionaryArray::try_new(&keys, &values).unwrap(); + let array2 = DictionaryArray::new(keys, Arc::new(values)); let expected = BooleanArray::from( vec![Some(false), Some(false), Some(false), Some(true), Some(true), Some(false)], @@ -5912,7 +5908,7 @@ mod tests { let values = Decimal128Array::from_iter_values([7, -3, 4, 3, 5]); let keys = Int8Array::from_iter_values([0_i8, 0, 1, 2, 3, 4]); - let array2 = DictionaryArray::try_new(&keys, &values).unwrap(); + let array2 = DictionaryArray::new(keys, Arc::new(values)); let expected = BooleanArray::from( vec![Some(false), Some(false), Some(false), Some(true), Some(true), Some(false)], @@ -5947,13 +5943,13 @@ mod tests { [0, 1, 2, 3, 4, 5].into_iter().map(i256::from_i128), ); let keys = Int8Array::from_iter_values([1_i8, 2, 5, 4, 3, 0]); - let array1 = DictionaryArray::try_new(&keys, &values).unwrap(); + let array1 = DictionaryArray::new(keys, Arc::new(values)); let values = Decimal256Array::from_iter_values( [7, -3, 4, 3, 5].into_iter().map(i256::from_i128), ); let keys = Int8Array::from_iter_values([0_i8, 0, 1, 2, 3, 4]); - let array2 = DictionaryArray::try_new(&keys, &values).unwrap(); + let array2 = DictionaryArray::new(keys, Arc::new(values)); let expected = BooleanArray::from( vec![Some(false), Some(false), Some(false), Some(true), Some(true), Some(false)], @@ -5992,7 +5988,7 @@ mod tests { [7, -3, 4, 3, 5].into_iter().map(i256::from_i128), ); let keys = Int8Array::from_iter_values([0_i8, 0, 1, 2, 3, 4]); - let array2 = DictionaryArray::try_new(&keys, &values).unwrap(); + let array2 = DictionaryArray::new(keys, Arc::new(values)); let expected = BooleanArray::from( vec![Some(false), Some(false), Some(false), Some(true), Some(true), Some(false)], diff --git a/arrow-ord/src/ord.rs b/arrow-ord/src/ord.rs index bfe74d9e3e7a..a33ead8ab041 100644 --- a/arrow-ord/src/ord.rs +++ b/arrow-ord/src/ord.rs @@ -283,6 +283,7 @@ pub mod tests { use arrow_buffer::i256; use half::f16; use std::cmp::Ordering; + use std::sync::Arc; #[test] fn test_fixed_size_binary() { @@ -423,11 +424,11 @@ pub mod tests { fn test_primitive_dict() { let values = Int32Array::from(vec![1_i32, 0, 2, 5]); let keys = Int8Array::from_iter_values([0, 0, 1, 3]); - let array1 = DictionaryArray::::try_new(&keys, &values).unwrap(); + let array1 = DictionaryArray::new(keys, Arc::new(values)); let values = Int32Array::from(vec![2_i32, 3, 4, 5]); let keys = Int8Array::from_iter_values([0, 1, 1, 3]); - let array2 = DictionaryArray::::try_new(&keys, &values).unwrap(); + let array2 = DictionaryArray::new(keys, Arc::new(values)); let cmp = build_compare(&array1, &array2).unwrap(); @@ -442,11 +443,11 @@ pub mod tests { fn test_float_dict() { let values = Float32Array::from(vec![1.0, 0.5, 2.1, 5.5]); let keys = Int8Array::from_iter_values([0, 0, 1, 3]); - let array1 = DictionaryArray::::try_new(&keys, &values).unwrap(); + let array1 = DictionaryArray::try_new(keys, Arc::new(values)).unwrap(); let values = Float32Array::from(vec![1.2, 3.2, 4.0, 5.5]); let keys = Int8Array::from_iter_values([0, 1, 1, 3]); - let array2 = DictionaryArray::::try_new(&keys, &values).unwrap(); + let array2 = DictionaryArray::new(keys, Arc::new(values)); let cmp = build_compare(&array1, &array2).unwrap(); @@ -461,11 +462,11 @@ pub mod tests { fn test_timestamp_dict() { let values = TimestampSecondArray::from(vec![1, 0, 2, 5]); let keys = Int8Array::from_iter_values([0, 0, 1, 3]); - let array1 = DictionaryArray::::try_new(&keys, &values).unwrap(); + let array1 = DictionaryArray::new(keys, Arc::new(values)); let values = TimestampSecondArray::from(vec![2, 3, 4, 5]); let keys = Int8Array::from_iter_values([0, 1, 1, 3]); - let array2 = DictionaryArray::::try_new(&keys, &values).unwrap(); + let array2 = DictionaryArray::new(keys, Arc::new(values)); let cmp = build_compare(&array1, &array2).unwrap(); @@ -480,11 +481,11 @@ pub mod tests { fn test_interval_dict() { let values = IntervalDayTimeArray::from(vec![1, 0, 2, 5]); let keys = Int8Array::from_iter_values([0, 0, 1, 3]); - let array1 = DictionaryArray::::try_new(&keys, &values).unwrap(); + let array1 = DictionaryArray::new(keys, Arc::new(values)); let values = IntervalDayTimeArray::from(vec![2, 3, 4, 5]); let keys = Int8Array::from_iter_values([0, 1, 1, 3]); - let array2 = DictionaryArray::::try_new(&keys, &values).unwrap(); + let array2 = DictionaryArray::new(keys, Arc::new(values)); let cmp = build_compare(&array1, &array2).unwrap(); @@ -499,11 +500,11 @@ pub mod tests { fn test_duration_dict() { let values = DurationSecondArray::from(vec![1, 0, 2, 5]); let keys = Int8Array::from_iter_values([0, 0, 1, 3]); - let array1 = DictionaryArray::::try_new(&keys, &values).unwrap(); + let array1 = DictionaryArray::new(keys, Arc::new(values)); let values = DurationSecondArray::from(vec![2, 3, 4, 5]); let keys = Int8Array::from_iter_values([0, 1, 1, 3]); - let array2 = DictionaryArray::::try_new(&keys, &values).unwrap(); + let array2 = DictionaryArray::new(keys, Arc::new(values)); let cmp = build_compare(&array1, &array2).unwrap(); @@ -518,11 +519,11 @@ pub mod tests { fn test_decimal_dict() { let values = Decimal128Array::from(vec![1, 0, 2, 5]); let keys = Int8Array::from_iter_values([0, 0, 1, 3]); - let array1 = DictionaryArray::::try_new(&keys, &values).unwrap(); + let array1 = DictionaryArray::new(keys, Arc::new(values)); let values = Decimal128Array::from(vec![2, 3, 4, 5]); let keys = Int8Array::from_iter_values([0, 1, 1, 3]); - let array2 = DictionaryArray::::try_new(&keys, &values).unwrap(); + let array2 = DictionaryArray::new(keys, Arc::new(values)); let cmp = build_compare(&array1, &array2).unwrap(); @@ -542,7 +543,7 @@ pub mod tests { i256::from_i128(5), ]); let keys = Int8Array::from_iter_values([0, 0, 1, 3]); - let array1 = DictionaryArray::::try_new(&keys, &values).unwrap(); + let array1 = DictionaryArray::new(keys, Arc::new(values)); let values = Decimal256Array::from(vec![ i256::from_i128(2), @@ -551,7 +552,7 @@ pub mod tests { i256::from_i128(5), ]); let keys = Int8Array::from_iter_values([0, 1, 1, 3]); - let array2 = DictionaryArray::::try_new(&keys, &values).unwrap(); + let array2 = DictionaryArray::new(keys, Arc::new(values)); let cmp = build_compare(&array1, &array2).unwrap(); diff --git a/arrow-ord/src/sort.rs b/arrow-ord/src/sort.rs index 7661479291c6..a44d9a910f5d 100644 --- a/arrow-ord/src/sort.rs +++ b/arrow-ord/src/sort.rs @@ -1480,12 +1480,9 @@ mod tests { ) where PrimitiveArray: From>>, { - let array = DictionaryArray::::try_new(&keys, &values).unwrap(); + let array = DictionaryArray::::new(keys, Arc::new(values)); let array_values = array.values().clone(); - let dict = array_values - .as_any() - .downcast_ref::>() - .expect("Unable to get dictionary values"); + let dict = array_values.as_primitive::(); let sorted = match limit { Some(_) => { diff --git a/arrow-string/src/length.rs b/arrow-string/src/length.rs index bd022532d6e1..90efdd7b67cc 100644 --- a/arrow-string/src/length.rs +++ b/arrow-string/src/length.rs @@ -66,7 +66,7 @@ macro_rules! kernel_dict { stringify!($gt), $array.data_type()) }); let values = $kernel(dict.values())?; - let result = DictionaryArray::try_new(dict.keys(), &values)?; + let result = DictionaryArray::try_new(dict.keys().clone(), values)?; Ok(Arc::new(result)) }, )* diff --git a/arrow-string/src/substring.rs b/arrow-string/src/substring.rs index 997b26361587..a8250c75d287 100644 --- a/arrow-string/src/substring.rs +++ b/arrow-string/src/substring.rs @@ -83,7 +83,7 @@ pub fn substring( stringify!($gt), array.data_type()) }); let values = substring(dict.values(), start, length)?; - let result = DictionaryArray::try_new(dict.keys(), &values)?; + let result = DictionaryArray::try_new(dict.keys().clone(), values)?; Ok(Arc::new(result)) }, )* diff --git a/arrow/src/array/ffi.rs b/arrow/src/array/ffi.rs index d4c284ad2cd1..1611dc5303d6 100644 --- a/arrow/src/array/ffi.rs +++ b/arrow/src/array/ffi.rs @@ -167,7 +167,7 @@ mod tests { Some(1), None, ]); - let array = DictionaryArray::try_new(&keys, &values)?; + let array = DictionaryArray::new(keys, Arc::new(values)); let data = array.into_data(); test_round_trip(&data)