1use crate::array::*;
21use crate::types::*;
22use arrow_data::ArrayData;
23
24#[doc(hidden)]
28pub mod __private {
29 pub use arrow_schema::{DataType, IntervalUnit, TimeUnit};
30}
31
32#[doc(hidden)]
34#[macro_export]
35macro_rules! repeat_pat {
36 ($e:pat, $v_:expr) => {
37 $e
38 };
39 ($e:pat, $v_:expr $(, $tail:expr)+) => {
40 ($e, $crate::repeat_pat!($e $(, $tail)+))
41 }
42}
43
44#[macro_export]
74macro_rules! downcast_integer {
75 ($($data_type:expr),+ => ($m:path $(, $args:tt)*), $($p:pat => $fallback:expr $(,)*)*) => {
76 match ($($data_type),+) {
77 $crate::repeat_pat!($crate::cast::__private::DataType::Int8, $($data_type),+) => {
78 $m!($crate::types::Int8Type $(, $args)*)
79 }
80 $crate::repeat_pat!($crate::cast::__private::DataType::Int16, $($data_type),+) => {
81 $m!($crate::types::Int16Type $(, $args)*)
82 }
83 $crate::repeat_pat!($crate::cast::__private::DataType::Int32, $($data_type),+) => {
84 $m!($crate::types::Int32Type $(, $args)*)
85 }
86 $crate::repeat_pat!($crate::cast::__private::DataType::Int64, $($data_type),+) => {
87 $m!($crate::types::Int64Type $(, $args)*)
88 }
89 $crate::repeat_pat!($crate::cast::__private::DataType::UInt8, $($data_type),+) => {
90 $m!($crate::types::UInt8Type $(, $args)*)
91 }
92 $crate::repeat_pat!($crate::cast::__private::DataType::UInt16, $($data_type),+) => {
93 $m!($crate::types::UInt16Type $(, $args)*)
94 }
95 $crate::repeat_pat!($crate::cast::__private::DataType::UInt32, $($data_type),+) => {
96 $m!($crate::types::UInt32Type $(, $args)*)
97 }
98 $crate::repeat_pat!($crate::cast::__private::DataType::UInt64, $($data_type),+) => {
99 $m!($crate::types::UInt64Type $(, $args)*)
100 }
101 $($p => $fallback,)*
102 }
103 };
104}
105
106#[macro_export]
137macro_rules! downcast_run_end_index {
138 ($($data_type:expr),+ => ($m:path $(, $args:tt)*), $($p:pat => $fallback:expr $(,)*)*) => {
139 match ($($data_type),+) {
140 $crate::repeat_pat!($crate::cast::__private::DataType::Int16, $($data_type),+) => {
141 $m!($crate::types::Int16Type $(, $args)*)
142 }
143 $crate::repeat_pat!($crate::cast::__private::DataType::Int32, $($data_type),+) => {
144 $m!($crate::types::Int32Type $(, $args)*)
145 }
146 $crate::repeat_pat!($crate::cast::__private::DataType::Int64, $($data_type),+) => {
147 $m!($crate::types::Int64Type $(, $args)*)
148 }
149 $($p => $fallback,)*
150 }
151 };
152}
153
154#[macro_export]
180macro_rules! downcast_temporal {
181 ($($data_type:expr),+ => ($m:path $(, $args:tt)*), $($p:pat => $fallback:expr $(,)*)*) => {
182 match ($($data_type),+) {
183 $crate::repeat_pat!($crate::cast::__private::DataType::Time32($crate::cast::__private::TimeUnit::Second), $($data_type),+) => {
184 $m!($crate::types::Time32SecondType $(, $args)*)
185 }
186 $crate::repeat_pat!($crate::cast::__private::DataType::Time32($crate::cast::__private::TimeUnit::Millisecond), $($data_type),+) => {
187 $m!($crate::types::Time32MillisecondType $(, $args)*)
188 }
189 $crate::repeat_pat!($crate::cast::__private::DataType::Time64($crate::cast::__private::TimeUnit::Microsecond), $($data_type),+) => {
190 $m!($crate::types::Time64MicrosecondType $(, $args)*)
191 }
192 $crate::repeat_pat!($crate::cast::__private::DataType::Time64($crate::cast::__private::TimeUnit::Nanosecond), $($data_type),+) => {
193 $m!($crate::types::Time64NanosecondType $(, $args)*)
194 }
195 $crate::repeat_pat!($crate::cast::__private::DataType::Date32, $($data_type),+) => {
196 $m!($crate::types::Date32Type $(, $args)*)
197 }
198 $crate::repeat_pat!($crate::cast::__private::DataType::Date64, $($data_type),+) => {
199 $m!($crate::types::Date64Type $(, $args)*)
200 }
201 $crate::repeat_pat!($crate::cast::__private::DataType::Timestamp($crate::cast::__private::TimeUnit::Second, _), $($data_type),+) => {
202 $m!($crate::types::TimestampSecondType $(, $args)*)
203 }
204 $crate::repeat_pat!($crate::cast::__private::DataType::Timestamp($crate::cast::__private::TimeUnit::Millisecond, _), $($data_type),+) => {
205 $m!($crate::types::TimestampMillisecondType $(, $args)*)
206 }
207 $crate::repeat_pat!($crate::cast::__private::DataType::Timestamp($crate::cast::__private::TimeUnit::Microsecond, _), $($data_type),+) => {
208 $m!($crate::types::TimestampMicrosecondType $(, $args)*)
209 }
210 $crate::repeat_pat!($crate::cast::__private::DataType::Timestamp($crate::cast::__private::TimeUnit::Nanosecond, _), $($data_type),+) => {
211 $m!($crate::types::TimestampNanosecondType $(, $args)*)
212 }
213 $($p => $fallback,)*
214 }
215 };
216}
217
218#[macro_export]
244macro_rules! downcast_temporal_array {
245 ($values:ident => $e:expr, $($p:pat => $fallback:expr $(,)*)*) => {
246 $crate::downcast_temporal_array!($values => {$e} $($p => $fallback)*)
247 };
248 (($($values:ident),+) => $e:expr, $($p:pat => $fallback:expr $(,)*)*) => {
249 $crate::downcast_temporal_array!($($values),+ => {$e} $($p => $fallback)*)
250 };
251 ($($values:ident),+ => $e:block $($p:pat => $fallback:expr $(,)*)*) => {
252 $crate::downcast_temporal_array!(($($values),+) => $e $($p => $fallback)*)
253 };
254 (($($values:ident),+) => $e:block $($p:pat => $fallback:expr $(,)*)*) => {
255 $crate::downcast_temporal!{
256 $($values.data_type()),+ => ($crate::downcast_primitive_array_helper, $($values),+, $e),
257 $($p => $fallback,)*
258 }
259 };
260}
261
262#[macro_export]
291macro_rules! downcast_primitive {
292 ($($data_type:expr),+ => ($m:path $(, $args:tt)*), $($p:pat => $fallback:expr $(,)*)*) => {
293 $crate::downcast_integer! {
294 $($data_type),+ => ($m $(, $args)*),
295 $crate::repeat_pat!($crate::cast::__private::DataType::Float16, $($data_type),+) => {
296 $m!($crate::types::Float16Type $(, $args)*)
297 }
298 $crate::repeat_pat!($crate::cast::__private::DataType::Float32, $($data_type),+) => {
299 $m!($crate::types::Float32Type $(, $args)*)
300 }
301 $crate::repeat_pat!($crate::cast::__private::DataType::Float64, $($data_type),+) => {
302 $m!($crate::types::Float64Type $(, $args)*)
303 }
304 $crate::repeat_pat!($crate::cast::__private::DataType::Decimal128(_, _), $($data_type),+) => {
305 $m!($crate::types::Decimal128Type $(, $args)*)
306 }
307 $crate::repeat_pat!($crate::cast::__private::DataType::Decimal256(_, _), $($data_type),+) => {
308 $m!($crate::types::Decimal256Type $(, $args)*)
309 }
310 $crate::repeat_pat!($crate::cast::__private::DataType::Interval($crate::cast::__private::IntervalUnit::YearMonth), $($data_type),+) => {
311 $m!($crate::types::IntervalYearMonthType $(, $args)*)
312 }
313 $crate::repeat_pat!($crate::cast::__private::DataType::Interval($crate::cast::__private::IntervalUnit::DayTime), $($data_type),+) => {
314 $m!($crate::types::IntervalDayTimeType $(, $args)*)
315 }
316 $crate::repeat_pat!($crate::cast::__private::DataType::Interval($crate::cast::__private::IntervalUnit::MonthDayNano), $($data_type),+) => {
317 $m!($crate::types::IntervalMonthDayNanoType $(, $args)*)
318 }
319 $crate::repeat_pat!($crate::cast::__private::DataType::Duration($crate::cast::__private::TimeUnit::Second), $($data_type),+) => {
320 $m!($crate::types::DurationSecondType $(, $args)*)
321 }
322 $crate::repeat_pat!($crate::cast::__private::DataType::Duration($crate::cast::__private::TimeUnit::Millisecond), $($data_type),+) => {
323 $m!($crate::types::DurationMillisecondType $(, $args)*)
324 }
325 $crate::repeat_pat!($crate::cast::__private::DataType::Duration($crate::cast::__private::TimeUnit::Microsecond), $($data_type),+) => {
326 $m!($crate::types::DurationMicrosecondType $(, $args)*)
327 }
328 $crate::repeat_pat!($crate::cast::__private::DataType::Duration($crate::cast::__private::TimeUnit::Nanosecond), $($data_type),+) => {
329 $m!($crate::types::DurationNanosecondType $(, $args)*)
330 }
331 _ => {
332 $crate::downcast_temporal! {
333 $($data_type),+ => ($m $(, $args)*),
334 $($p => $fallback,)*
335 }
336 }
337 }
338 };
339}
340
341#[macro_export]
342#[doc(hidden)]
343macro_rules! downcast_primitive_array_helper {
344 ($t:ty, $($values:ident),+, $e:block) => {{
345 $(let $values = $crate::cast::as_primitive_array::<$t>($values);)+
346 $e
347 }};
348}
349
350#[macro_export]
376macro_rules! downcast_primitive_array {
377 ($values:ident => $e:expr, $($p:pat => $fallback:expr $(,)*)*) => {
378 $crate::downcast_primitive_array!($values => {$e} $($p => $fallback)*)
379 };
380 (($($values:ident),+) => $e:expr, $($p:pat => $fallback:expr $(,)*)*) => {
381 $crate::downcast_primitive_array!($($values),+ => {$e} $($p => $fallback)*)
382 };
383 ($($values:ident),+ => $e:block $($p:pat => $fallback:expr $(,)*)*) => {
384 $crate::downcast_primitive_array!(($($values),+) => $e $($p => $fallback)*)
385 };
386 (($($values:ident),+) => $e:block $($p:pat => $fallback:expr $(,)*)*) => {
387 $crate::downcast_primitive!{
388 $($values.data_type()),+ => ($crate::downcast_primitive_array_helper, $($values),+, $e),
389 $($p => $fallback,)*
390 }
391 };
392}
393
394pub fn as_primitive_array<T>(arr: &dyn Array) -> &PrimitiveArray<T>
420where
421 T: ArrowPrimitiveType,
422{
423 arr.as_any()
424 .downcast_ref::<PrimitiveArray<T>>()
425 .expect("Unable to downcast to primitive array")
426}
427
428#[macro_export]
429#[doc(hidden)]
430macro_rules! downcast_dictionary_array_helper {
431 ($t:ty, $($values:ident),+, $e:block) => {{
432 $(let $values = $crate::cast::as_dictionary_array::<$t>($values);)+
433 $e
434 }};
435}
436
437#[macro_export]
466macro_rules! downcast_dictionary_array {
467 ($values:ident => $e:expr, $($p:pat => $fallback:expr $(,)*)*) => {
468 downcast_dictionary_array!($values => {$e} $($p => $fallback)*)
469 };
470
471 ($values:ident => $e:block $($p:pat => $fallback:expr $(,)*)*) => {
472 match $values.data_type() {
473 $crate::cast::__private::DataType::Dictionary(k, _) => {
474 $crate::downcast_integer! {
475 k.as_ref() => ($crate::downcast_dictionary_array_helper, $values, $e),
476 k => unreachable!("unsupported dictionary key type: {}", k)
477 }
478 }
479 $($p => $fallback,)*
480 }
481 }
482}
483
484pub fn as_dictionary_array<T>(arr: &dyn Array) -> &DictionaryArray<T>
499where
500 T: ArrowDictionaryKeyType,
501{
502 arr.as_any()
503 .downcast_ref::<DictionaryArray<T>>()
504 .expect("Unable to downcast to dictionary array")
505}
506
507pub fn as_run_array<T>(arr: &dyn Array) -> &RunArray<T>
522where
523 T: RunEndIndexType,
524{
525 arr.as_any()
526 .downcast_ref::<RunArray<T>>()
527 .expect("Unable to downcast to run array")
528}
529
530#[macro_export]
531#[doc(hidden)]
532macro_rules! downcast_run_array_helper {
533 ($t:ty, $($values:ident),+, $e:block) => {{
534 $(let $values = $crate::cast::as_run_array::<$t>($values);)+
535 $e
536 }};
537}
538
539#[macro_export]
568macro_rules! downcast_run_array {
569 ($values:ident => $e:expr, $($p:pat => $fallback:expr $(,)*)*) => {
570 downcast_run_array!($values => {$e} $($p => $fallback)*)
571 };
572
573 ($values:ident => $e:block $($p:pat => $fallback:expr $(,)*)*) => {
574 match $values.data_type() {
575 $crate::cast::__private::DataType::RunEndEncoded(k, _) => {
576 $crate::downcast_run_end_index! {
577 k.data_type() => ($crate::downcast_run_array_helper, $values, $e),
578 k => unreachable!("unsupported run end index type: {}", k)
579 }
580 }
581 $($p => $fallback,)*
582 }
583 }
584}
585
586pub fn as_generic_list_array<S: OffsetSizeTrait>(arr: &dyn Array) -> &GenericListArray<S> {
589 arr.as_any()
590 .downcast_ref::<GenericListArray<S>>()
591 .expect("Unable to downcast to list array")
592}
593
594#[inline]
597pub fn as_list_array(arr: &dyn Array) -> &ListArray {
598 as_generic_list_array::<i32>(arr)
599}
600
601#[inline]
604pub fn as_fixed_size_list_array(arr: &dyn Array) -> &FixedSizeListArray {
605 arr.as_any()
606 .downcast_ref::<FixedSizeListArray>()
607 .expect("Unable to downcast to fixed size list array")
608}
609
610#[inline]
613pub fn as_large_list_array(arr: &dyn Array) -> &LargeListArray {
614 as_generic_list_array::<i64>(arr)
615}
616
617#[inline]
620pub fn as_generic_binary_array<S: OffsetSizeTrait>(arr: &dyn Array) -> &GenericBinaryArray<S> {
621 arr.as_any()
622 .downcast_ref::<GenericBinaryArray<S>>()
623 .expect("Unable to downcast to binary array")
624}
625
626pub fn as_string_array(arr: &dyn Array) -> &StringArray {
640 arr.as_any()
641 .downcast_ref::<StringArray>()
642 .expect("Unable to downcast to StringArray")
643}
644
645pub fn as_boolean_array(arr: &dyn Array) -> &BooleanArray {
659 arr.as_any()
660 .downcast_ref::<BooleanArray>()
661 .expect("Unable to downcast to BooleanArray")
662}
663
664macro_rules! array_downcast_fn {
665 ($name: ident, $arrty: ty, $arrty_str:expr) => {
666 #[doc = "Force downcast of an [`Array`], such as an [`ArrayRef`] to "]
667 #[doc = $arrty_str]
668 pub fn $name(arr: &dyn Array) -> &$arrty {
669 arr.as_any().downcast_ref::<$arrty>().expect(concat!(
670 "Unable to downcast to typed array through ",
671 stringify!($name)
672 ))
673 }
674 };
675
676 ($name: ident, $arrty: ty) => {
678 array_downcast_fn!(
679 $name,
680 $arrty,
681 concat!("[`", stringify!($arrty), "`], panicking on failure.")
682 );
683 };
684}
685
686array_downcast_fn!(as_largestring_array, LargeStringArray);
687array_downcast_fn!(as_null_array, NullArray);
688array_downcast_fn!(as_struct_array, StructArray);
689array_downcast_fn!(as_union_array, UnionArray);
690array_downcast_fn!(as_map_array, MapArray);
691
692#[deprecated(note = "please use `as_primitive_array::<Decimal128Type>` instead")]
694pub fn as_decimal_array(arr: &dyn Array) -> &PrimitiveArray<Decimal128Type> {
695 as_primitive_array::<Decimal128Type>(arr)
696}
697
698pub fn downcast_array<T>(array: &dyn Array) -> T
724where
725 T: From<ArrayData>,
726{
727 T::from(array.to_data())
728}
729
730mod private {
731 pub trait Sealed {}
732}
733
734pub trait AsArray: private::Sealed {
745 fn as_boolean_opt(&self) -> Option<&BooleanArray>;
747
748 fn as_boolean(&self) -> &BooleanArray {
750 self.as_boolean_opt().expect("boolean array")
751 }
752
753 fn as_primitive_opt<T: ArrowPrimitiveType>(&self) -> Option<&PrimitiveArray<T>>;
755
756 fn as_primitive<T: ArrowPrimitiveType>(&self) -> &PrimitiveArray<T> {
758 self.as_primitive_opt().expect("primitive array")
759 }
760
761 fn as_bytes_opt<T: ByteArrayType>(&self) -> Option<&GenericByteArray<T>>;
763
764 fn as_bytes<T: ByteArrayType>(&self) -> &GenericByteArray<T> {
766 self.as_bytes_opt().expect("byte array")
767 }
768
769 fn as_string_opt<O: OffsetSizeTrait>(&self) -> Option<&GenericStringArray<O>> {
771 self.as_bytes_opt()
772 }
773
774 fn as_string<O: OffsetSizeTrait>(&self) -> &GenericStringArray<O> {
776 self.as_bytes_opt().expect("string array")
777 }
778
779 fn as_binary_opt<O: OffsetSizeTrait>(&self) -> Option<&GenericBinaryArray<O>> {
781 self.as_bytes_opt()
782 }
783
784 fn as_binary<O: OffsetSizeTrait>(&self) -> &GenericBinaryArray<O> {
786 self.as_bytes_opt().expect("binary array")
787 }
788
789 fn as_string_view_opt(&self) -> Option<&StringViewArray> {
791 self.as_byte_view_opt()
792 }
793
794 fn as_string_view(&self) -> &StringViewArray {
796 self.as_byte_view_opt().expect("string view array")
797 }
798
799 fn as_binary_view_opt(&self) -> Option<&BinaryViewArray> {
801 self.as_byte_view_opt()
802 }
803
804 fn as_binary_view(&self) -> &BinaryViewArray {
806 self.as_byte_view_opt().expect("binary view array")
807 }
808
809 fn as_byte_view_opt<T: ByteViewType>(&self) -> Option<&GenericByteViewArray<T>>;
811
812 fn as_byte_view<T: ByteViewType>(&self) -> &GenericByteViewArray<T> {
814 self.as_byte_view_opt().expect("byte view array")
815 }
816
817 fn as_struct_opt(&self) -> Option<&StructArray>;
819
820 fn as_struct(&self) -> &StructArray {
822 self.as_struct_opt().expect("struct array")
823 }
824
825 fn as_union_opt(&self) -> Option<&UnionArray>;
827
828 fn as_union(&self) -> &UnionArray {
830 self.as_union_opt().expect("union array")
831 }
832
833 fn as_list_opt<O: OffsetSizeTrait>(&self) -> Option<&GenericListArray<O>>;
835
836 fn as_list<O: OffsetSizeTrait>(&self) -> &GenericListArray<O> {
838 self.as_list_opt().expect("list array")
839 }
840
841 fn as_fixed_size_binary_opt(&self) -> Option<&FixedSizeBinaryArray>;
843
844 fn as_fixed_size_binary(&self) -> &FixedSizeBinaryArray {
846 self.as_fixed_size_binary_opt()
847 .expect("fixed size binary array")
848 }
849
850 fn as_fixed_size_list_opt(&self) -> Option<&FixedSizeListArray>;
852
853 fn as_fixed_size_list(&self) -> &FixedSizeListArray {
855 self.as_fixed_size_list_opt()
856 .expect("fixed size list array")
857 }
858
859 fn as_map_opt(&self) -> Option<&MapArray>;
861
862 fn as_map(&self) -> &MapArray {
864 self.as_map_opt().expect("map array")
865 }
866
867 fn as_dictionary_opt<K: ArrowDictionaryKeyType>(&self) -> Option<&DictionaryArray<K>>;
869
870 fn as_dictionary<K: ArrowDictionaryKeyType>(&self) -> &DictionaryArray<K> {
872 self.as_dictionary_opt().expect("dictionary array")
873 }
874
875 fn as_any_dictionary_opt(&self) -> Option<&dyn AnyDictionaryArray>;
877
878 fn as_any_dictionary(&self) -> &dyn AnyDictionaryArray {
880 self.as_any_dictionary_opt().expect("any dictionary array")
881 }
882}
883
884impl private::Sealed for dyn Array + '_ {}
885impl AsArray for dyn Array + '_ {
886 fn as_boolean_opt(&self) -> Option<&BooleanArray> {
887 self.as_any().downcast_ref()
888 }
889
890 fn as_primitive_opt<T: ArrowPrimitiveType>(&self) -> Option<&PrimitiveArray<T>> {
891 self.as_any().downcast_ref()
892 }
893
894 fn as_bytes_opt<T: ByteArrayType>(&self) -> Option<&GenericByteArray<T>> {
895 self.as_any().downcast_ref()
896 }
897
898 fn as_byte_view_opt<T: ByteViewType>(&self) -> Option<&GenericByteViewArray<T>> {
899 self.as_any().downcast_ref()
900 }
901
902 fn as_struct_opt(&self) -> Option<&StructArray> {
903 self.as_any().downcast_ref()
904 }
905
906 fn as_union_opt(&self) -> Option<&UnionArray> {
907 self.as_any().downcast_ref()
908 }
909
910 fn as_list_opt<O: OffsetSizeTrait>(&self) -> Option<&GenericListArray<O>> {
911 self.as_any().downcast_ref()
912 }
913
914 fn as_fixed_size_binary_opt(&self) -> Option<&FixedSizeBinaryArray> {
915 self.as_any().downcast_ref()
916 }
917
918 fn as_fixed_size_list_opt(&self) -> Option<&FixedSizeListArray> {
919 self.as_any().downcast_ref()
920 }
921
922 fn as_map_opt(&self) -> Option<&MapArray> {
923 self.as_any().downcast_ref()
924 }
925
926 fn as_dictionary_opt<K: ArrowDictionaryKeyType>(&self) -> Option<&DictionaryArray<K>> {
927 self.as_any().downcast_ref()
928 }
929
930 fn as_any_dictionary_opt(&self) -> Option<&dyn AnyDictionaryArray> {
931 let array = self;
932 downcast_dictionary_array! {
933 array => Some(array),
934 _ => None
935 }
936 }
937}
938
939impl private::Sealed for ArrayRef {}
940impl AsArray for ArrayRef {
941 fn as_boolean_opt(&self) -> Option<&BooleanArray> {
942 self.as_ref().as_boolean_opt()
943 }
944
945 fn as_primitive_opt<T: ArrowPrimitiveType>(&self) -> Option<&PrimitiveArray<T>> {
946 self.as_ref().as_primitive_opt()
947 }
948
949 fn as_bytes_opt<T: ByteArrayType>(&self) -> Option<&GenericByteArray<T>> {
950 self.as_ref().as_bytes_opt()
951 }
952
953 fn as_byte_view_opt<T: ByteViewType>(&self) -> Option<&GenericByteViewArray<T>> {
954 self.as_ref().as_byte_view_opt()
955 }
956
957 fn as_struct_opt(&self) -> Option<&StructArray> {
958 self.as_ref().as_struct_opt()
959 }
960
961 fn as_union_opt(&self) -> Option<&UnionArray> {
962 self.as_any().downcast_ref()
963 }
964
965 fn as_list_opt<O: OffsetSizeTrait>(&self) -> Option<&GenericListArray<O>> {
966 self.as_ref().as_list_opt()
967 }
968
969 fn as_fixed_size_binary_opt(&self) -> Option<&FixedSizeBinaryArray> {
970 self.as_ref().as_fixed_size_binary_opt()
971 }
972
973 fn as_fixed_size_list_opt(&self) -> Option<&FixedSizeListArray> {
974 self.as_ref().as_fixed_size_list_opt()
975 }
976
977 fn as_map_opt(&self) -> Option<&MapArray> {
978 self.as_any().downcast_ref()
979 }
980
981 fn as_dictionary_opt<K: ArrowDictionaryKeyType>(&self) -> Option<&DictionaryArray<K>> {
982 self.as_ref().as_dictionary_opt()
983 }
984
985 fn as_any_dictionary_opt(&self) -> Option<&dyn AnyDictionaryArray> {
986 self.as_ref().as_any_dictionary_opt()
987 }
988}
989
990#[cfg(test)]
991mod tests {
992 use arrow_buffer::i256;
993 use std::sync::Arc;
994
995 use super::*;
996
997 #[test]
998 fn test_as_primitive_array_ref() {
999 let array: Int32Array = vec![1, 2, 3].into_iter().map(Some).collect();
1000 assert!(!as_primitive_array::<Int32Type>(&array).is_empty());
1001
1002 let array: ArrayRef = Arc::new(array);
1004 assert!(!as_primitive_array::<Int32Type>(&array).is_empty());
1005 }
1006
1007 #[test]
1008 fn test_as_string_array_ref() {
1009 let array: StringArray = vec!["foo", "bar"].into_iter().map(Some).collect();
1010 assert!(!as_string_array(&array).is_empty());
1011
1012 let array: ArrayRef = Arc::new(array);
1014 assert!(!as_string_array(&array).is_empty())
1015 }
1016
1017 #[test]
1018 fn test_decimal128array() {
1019 let a = Decimal128Array::from_iter_values([1, 2, 4, 5]);
1020 assert!(!as_primitive_array::<Decimal128Type>(&a).is_empty());
1021 }
1022
1023 #[test]
1024 fn test_decimal256array() {
1025 let a = Decimal256Array::from_iter_values([1, 2, 4, 5].into_iter().map(i256::from_i128));
1026 assert!(!as_primitive_array::<Decimal256Type>(&a).is_empty());
1027 }
1028}