Skip to content

Commit b7ea7d5

Browse files
authored
Supplement as_*_array functions (#13580)
* Supplement as_*_array functions These functions are useful when implementing UDFs. It would be helpful for downstream projects to provide graceful cast functions for all data types. * add as_large_string_array * add as_large_binary_array
1 parent f6c92fe commit b7ea7d5

File tree

1 file changed

+47
-14
lines changed

1 file changed

+47
-14
lines changed

datafusion/common/src/cast.rs

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,66 @@ use arrow::{
3636
},
3737
datatypes::{ArrowDictionaryKeyType, ArrowPrimitiveType},
3838
};
39-
use arrow_array::{BinaryViewArray, StringViewArray};
39+
use arrow_array::{
40+
BinaryViewArray, Float16Array, Int16Array, Int8Array, LargeBinaryArray,
41+
LargeStringArray, StringViewArray, UInt16Array,
42+
};
4043

4144
// Downcast ArrayRef to Date32Array
4245
pub fn as_date32_array(array: &dyn Array) -> Result<&Date32Array> {
4346
Ok(downcast_value!(array, Date32Array))
4447
}
4548

49+
// Downcast ArrayRef to Date64Array
50+
pub fn as_date64_array(array: &dyn Array) -> Result<&Date64Array> {
51+
Ok(downcast_value!(array, Date64Array))
52+
}
53+
4654
// Downcast ArrayRef to StructArray
4755
pub fn as_struct_array(array: &dyn Array) -> Result<&StructArray> {
4856
Ok(downcast_value!(array, StructArray))
4957
}
5058

59+
// Downcast ArrayRef to Int8Array
60+
pub fn as_int8_array(array: &dyn Array) -> Result<&Int8Array> {
61+
Ok(downcast_value!(array, Int8Array))
62+
}
63+
5164
// Downcast ArrayRef to UInt8Array
5265
pub fn as_uint8_array(array: &dyn Array) -> Result<&UInt8Array> {
5366
Ok(downcast_value!(array, UInt8Array))
5467
}
5568

69+
// Downcast ArrayRef to Int16Array
70+
pub fn as_int16_array(array: &dyn Array) -> Result<&Int16Array> {
71+
Ok(downcast_value!(array, Int16Array))
72+
}
73+
74+
// Downcast ArrayRef to UInt16Array
75+
pub fn as_uint16_array(array: &dyn Array) -> Result<&UInt16Array> {
76+
Ok(downcast_value!(array, UInt16Array))
77+
}
78+
5679
// Downcast ArrayRef to Int32Array
5780
pub fn as_int32_array(array: &dyn Array) -> Result<&Int32Array> {
5881
Ok(downcast_value!(array, Int32Array))
5982
}
6083

84+
// Downcast ArrayRef to UInt32Array
85+
pub fn as_uint32_array(array: &dyn Array) -> Result<&UInt32Array> {
86+
Ok(downcast_value!(array, UInt32Array))
87+
}
88+
6189
// Downcast ArrayRef to Int64Array
6290
pub fn as_int64_array(array: &dyn Array) -> Result<&Int64Array> {
6391
Ok(downcast_value!(array, Int64Array))
6492
}
6593

94+
// Downcast ArrayRef to UInt64Array
95+
pub fn as_uint64_array(array: &dyn Array) -> Result<&UInt64Array> {
96+
Ok(downcast_value!(array, UInt64Array))
97+
}
98+
6699
// Downcast ArrayRef to Decimal128Array
67100
pub fn as_decimal128_array(array: &dyn Array) -> Result<&Decimal128Array> {
68101
Ok(downcast_value!(array, Decimal128Array))
@@ -73,6 +106,11 @@ pub fn as_decimal256_array(array: &dyn Array) -> Result<&Decimal256Array> {
73106
Ok(downcast_value!(array, Decimal256Array))
74107
}
75108

109+
// Downcast ArrayRef to Float16Array
110+
pub fn as_float16_array(array: &dyn Array) -> Result<&Float16Array> {
111+
Ok(downcast_value!(array, Float16Array))
112+
}
113+
76114
// Downcast ArrayRef to Float32Array
77115
pub fn as_float32_array(array: &dyn Array) -> Result<&Float32Array> {
78116
Ok(downcast_value!(array, Float32Array))
@@ -93,14 +131,9 @@ pub fn as_string_view_array(array: &dyn Array) -> Result<&StringViewArray> {
93131
Ok(downcast_value!(array, StringViewArray))
94132
}
95133

96-
// Downcast ArrayRef to UInt32Array
97-
pub fn as_uint32_array(array: &dyn Array) -> Result<&UInt32Array> {
98-
Ok(downcast_value!(array, UInt32Array))
99-
}
100-
101-
// Downcast ArrayRef to UInt64Array
102-
pub fn as_uint64_array(array: &dyn Array) -> Result<&UInt64Array> {
103-
Ok(downcast_value!(array, UInt64Array))
134+
// Downcast ArrayRef to LargeStringArray
135+
pub fn as_large_string_array(array: &dyn Array) -> Result<&LargeStringArray> {
136+
Ok(downcast_value!(array, LargeStringArray))
104137
}
105138

106139
// Downcast ArrayRef to BooleanArray
@@ -232,6 +265,11 @@ pub fn as_binary_view_array(array: &dyn Array) -> Result<&BinaryViewArray> {
232265
Ok(downcast_value!(array, BinaryViewArray))
233266
}
234267

268+
// Downcast ArrayRef to LargeBinaryArray
269+
pub fn as_large_binary_array(array: &dyn Array) -> Result<&LargeBinaryArray> {
270+
Ok(downcast_value!(array, LargeBinaryArray))
271+
}
272+
235273
// Downcast ArrayRef to FixedSizeListArray
236274
pub fn as_fixed_size_list_array(array: &dyn Array) -> Result<&FixedSizeListArray> {
237275
Ok(downcast_value!(array, FixedSizeListArray))
@@ -242,11 +280,6 @@ pub fn as_fixed_size_binary_array(array: &dyn Array) -> Result<&FixedSizeBinaryA
242280
Ok(downcast_value!(array, FixedSizeBinaryArray))
243281
}
244282

245-
// Downcast ArrayRef to Date64Array
246-
pub fn as_date64_array(array: &dyn Array) -> Result<&Date64Array> {
247-
Ok(downcast_value!(array, Date64Array))
248-
}
249-
250283
// Downcast ArrayRef to GenericBinaryArray
251284
pub fn as_generic_string_array<T: OffsetSizeTrait>(
252285
array: &dyn Array,

0 commit comments

Comments
 (0)