Skip to content

Commit 65242a6

Browse files
authored
Improve error message when ScalarValue fails to cast array (apache#16670)
* Improve error message when ScalarValue fails to cast array The `as_*_array` functions and the `downcast_value!` have the benefit of reporting the array type when there is a mismatch. This makes the error message more actionable. * test
1 parent 50dc83a commit 65242a6

3 files changed

Lines changed: 235 additions & 188 deletions

File tree

datafusion/common/src/cast.rs

Lines changed: 75 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
2323
use crate::{downcast_value, Result};
2424
use arrow::array::{
25-
BinaryViewArray, Float16Array, Int16Array, Int8Array, LargeBinaryArray,
26-
LargeStringArray, StringViewArray, UInt16Array,
25+
BinaryViewArray, DurationMicrosecondArray, DurationMillisecondArray,
26+
DurationNanosecondArray, DurationSecondArray, Float16Array, Int16Array, Int8Array,
27+
LargeBinaryArray, LargeStringArray, StringViewArray, UInt16Array,
2728
};
2829
use arrow::{
2930
array::{
@@ -41,246 +42,272 @@ use arrow::{
4142
datatypes::{ArrowDictionaryKeyType, ArrowPrimitiveType},
4243
};
4344

44-
// Downcast ArrayRef to Date32Array
45+
// Downcast Array to Date32Array
4546
pub fn as_date32_array(array: &dyn Array) -> Result<&Date32Array> {
4647
Ok(downcast_value!(array, Date32Array))
4748
}
4849

49-
// Downcast ArrayRef to Date64Array
50+
// Downcast Array to Date64Array
5051
pub fn as_date64_array(array: &dyn Array) -> Result<&Date64Array> {
5152
Ok(downcast_value!(array, Date64Array))
5253
}
5354

54-
// Downcast ArrayRef to StructArray
55+
// Downcast Array to StructArray
5556
pub fn as_struct_array(array: &dyn Array) -> Result<&StructArray> {
5657
Ok(downcast_value!(array, StructArray))
5758
}
5859

59-
// Downcast ArrayRef to Int8Array
60+
// Downcast Array to Int8Array
6061
pub fn as_int8_array(array: &dyn Array) -> Result<&Int8Array> {
6162
Ok(downcast_value!(array, Int8Array))
6263
}
6364

64-
// Downcast ArrayRef to UInt8Array
65+
// Downcast Array to UInt8Array
6566
pub fn as_uint8_array(array: &dyn Array) -> Result<&UInt8Array> {
6667
Ok(downcast_value!(array, UInt8Array))
6768
}
6869

69-
// Downcast ArrayRef to Int16Array
70+
// Downcast Array to Int16Array
7071
pub fn as_int16_array(array: &dyn Array) -> Result<&Int16Array> {
7172
Ok(downcast_value!(array, Int16Array))
7273
}
7374

74-
// Downcast ArrayRef to UInt16Array
75+
// Downcast Array to UInt16Array
7576
pub fn as_uint16_array(array: &dyn Array) -> Result<&UInt16Array> {
7677
Ok(downcast_value!(array, UInt16Array))
7778
}
7879

79-
// Downcast ArrayRef to Int32Array
80+
// Downcast Array to Int32Array
8081
pub fn as_int32_array(array: &dyn Array) -> Result<&Int32Array> {
8182
Ok(downcast_value!(array, Int32Array))
8283
}
8384

84-
// Downcast ArrayRef to UInt32Array
85+
// Downcast Array to UInt32Array
8586
pub fn as_uint32_array(array: &dyn Array) -> Result<&UInt32Array> {
8687
Ok(downcast_value!(array, UInt32Array))
8788
}
8889

89-
// Downcast ArrayRef to Int64Array
90+
// Downcast Array to Int64Array
9091
pub fn as_int64_array(array: &dyn Array) -> Result<&Int64Array> {
9192
Ok(downcast_value!(array, Int64Array))
9293
}
9394

94-
// Downcast ArrayRef to UInt64Array
95+
// Downcast Array to UInt64Array
9596
pub fn as_uint64_array(array: &dyn Array) -> Result<&UInt64Array> {
9697
Ok(downcast_value!(array, UInt64Array))
9798
}
9899

99-
// Downcast ArrayRef to Decimal128Array
100+
// Downcast Array to Decimal128Array
100101
pub fn as_decimal128_array(array: &dyn Array) -> Result<&Decimal128Array> {
101102
Ok(downcast_value!(array, Decimal128Array))
102103
}
103104

104-
// Downcast ArrayRef to Decimal256Array
105+
// Downcast Array to Decimal256Array
105106
pub fn as_decimal256_array(array: &dyn Array) -> Result<&Decimal256Array> {
106107
Ok(downcast_value!(array, Decimal256Array))
107108
}
108109

109-
// Downcast ArrayRef to Float16Array
110+
// Downcast Array to Float16Array
110111
pub fn as_float16_array(array: &dyn Array) -> Result<&Float16Array> {
111112
Ok(downcast_value!(array, Float16Array))
112113
}
113114

114-
// Downcast ArrayRef to Float32Array
115+
// Downcast Array to Float32Array
115116
pub fn as_float32_array(array: &dyn Array) -> Result<&Float32Array> {
116117
Ok(downcast_value!(array, Float32Array))
117118
}
118119

119-
// Downcast ArrayRef to Float64Array
120+
// Downcast Array to Float64Array
120121
pub fn as_float64_array(array: &dyn Array) -> Result<&Float64Array> {
121122
Ok(downcast_value!(array, Float64Array))
122123
}
123124

124-
// Downcast ArrayRef to StringArray
125+
// Downcast Array to StringArray
125126
pub fn as_string_array(array: &dyn Array) -> Result<&StringArray> {
126127
Ok(downcast_value!(array, StringArray))
127128
}
128129

129-
// Downcast ArrayRef to StringViewArray
130+
// Downcast Array to StringViewArray
130131
pub fn as_string_view_array(array: &dyn Array) -> Result<&StringViewArray> {
131132
Ok(downcast_value!(array, StringViewArray))
132133
}
133134

134-
// Downcast ArrayRef to LargeStringArray
135+
// Downcast Array to LargeStringArray
135136
pub fn as_large_string_array(array: &dyn Array) -> Result<&LargeStringArray> {
136137
Ok(downcast_value!(array, LargeStringArray))
137138
}
138139

139-
// Downcast ArrayRef to BooleanArray
140+
// Downcast Array to BooleanArray
140141
pub fn as_boolean_array(array: &dyn Array) -> Result<&BooleanArray> {
141142
Ok(downcast_value!(array, BooleanArray))
142143
}
143144

144-
// Downcast ArrayRef to ListArray
145+
// Downcast Array to ListArray
145146
pub fn as_list_array(array: &dyn Array) -> Result<&ListArray> {
146147
Ok(downcast_value!(array, ListArray))
147148
}
148149

149-
// Downcast ArrayRef to DictionaryArray
150+
// Downcast Array to DictionaryArray
150151
pub fn as_dictionary_array<T: ArrowDictionaryKeyType>(
151152
array: &dyn Array,
152153
) -> Result<&DictionaryArray<T>> {
153154
Ok(downcast_value!(array, DictionaryArray, T))
154155
}
155156

156-
// Downcast ArrayRef to GenericBinaryArray
157+
// Downcast Array to GenericBinaryArray
157158
pub fn as_generic_binary_array<T: OffsetSizeTrait>(
158159
array: &dyn Array,
159160
) -> Result<&GenericBinaryArray<T>> {
160161
Ok(downcast_value!(array, GenericBinaryArray, T))
161162
}
162163

163-
// Downcast ArrayRef to GenericListArray
164+
// Downcast Array to GenericListArray
164165
pub fn as_generic_list_array<T: OffsetSizeTrait>(
165166
array: &dyn Array,
166167
) -> Result<&GenericListArray<T>> {
167168
Ok(downcast_value!(array, GenericListArray, T))
168169
}
169170

170-
// Downcast ArrayRef to LargeListArray
171+
// Downcast Array to LargeListArray
171172
pub fn as_large_list_array(array: &dyn Array) -> Result<&LargeListArray> {
172173
Ok(downcast_value!(array, LargeListArray))
173174
}
174175

175-
// Downcast ArrayRef to PrimitiveArray
176+
// Downcast Array to PrimitiveArray
176177
pub fn as_primitive_array<T: ArrowPrimitiveType>(
177178
array: &dyn Array,
178179
) -> Result<&PrimitiveArray<T>> {
179180
Ok(downcast_value!(array, PrimitiveArray, T))
180181
}
181182

182-
// Downcast ArrayRef to MapArray
183+
// Downcast Array to MapArray
183184
pub fn as_map_array(array: &dyn Array) -> Result<&MapArray> {
184185
Ok(downcast_value!(array, MapArray))
185186
}
186187

187-
// Downcast ArrayRef to NullArray
188+
// Downcast Array to NullArray
188189
pub fn as_null_array(array: &dyn Array) -> Result<&NullArray> {
189190
Ok(downcast_value!(array, NullArray))
190191
}
191192

192-
// Downcast ArrayRef to NullArray
193+
// Downcast Array to NullArray
193194
pub fn as_union_array(array: &dyn Array) -> Result<&UnionArray> {
194195
Ok(downcast_value!(array, UnionArray))
195196
}
196197

197-
// Downcast ArrayRef to Time32SecondArray
198+
// Downcast Array to Time32SecondArray
198199
pub fn as_time32_second_array(array: &dyn Array) -> Result<&Time32SecondArray> {
199200
Ok(downcast_value!(array, Time32SecondArray))
200201
}
201202

202-
// Downcast ArrayRef to Time32MillisecondArray
203+
// Downcast Array to Time32MillisecondArray
203204
pub fn as_time32_millisecond_array(array: &dyn Array) -> Result<&Time32MillisecondArray> {
204205
Ok(downcast_value!(array, Time32MillisecondArray))
205206
}
206207

207-
// Downcast ArrayRef to Time64MicrosecondArray
208+
// Downcast Array to Time64MicrosecondArray
208209
pub fn as_time64_microsecond_array(array: &dyn Array) -> Result<&Time64MicrosecondArray> {
209210
Ok(downcast_value!(array, Time64MicrosecondArray))
210211
}
211212

212-
// Downcast ArrayRef to Time64NanosecondArray
213+
// Downcast Array to Time64NanosecondArray
213214
pub fn as_time64_nanosecond_array(array: &dyn Array) -> Result<&Time64NanosecondArray> {
214215
Ok(downcast_value!(array, Time64NanosecondArray))
215216
}
216217

217-
// Downcast ArrayRef to TimestampNanosecondArray
218+
// Downcast Array to TimestampNanosecondArray
218219
pub fn as_timestamp_nanosecond_array(
219220
array: &dyn Array,
220221
) -> Result<&TimestampNanosecondArray> {
221222
Ok(downcast_value!(array, TimestampNanosecondArray))
222223
}
223224

224-
// Downcast ArrayRef to TimestampMillisecondArray
225+
// Downcast Array to TimestampMillisecondArray
225226
pub fn as_timestamp_millisecond_array(
226227
array: &dyn Array,
227228
) -> Result<&TimestampMillisecondArray> {
228229
Ok(downcast_value!(array, TimestampMillisecondArray))
229230
}
230231

231-
// Downcast ArrayRef to TimestampMicrosecondArray
232+
// Downcast Array to TimestampMicrosecondArray
232233
pub fn as_timestamp_microsecond_array(
233234
array: &dyn Array,
234235
) -> Result<&TimestampMicrosecondArray> {
235236
Ok(downcast_value!(array, TimestampMicrosecondArray))
236237
}
237238

238-
// Downcast ArrayRef to TimestampSecondArray
239+
// Downcast Array to TimestampSecondArray
239240
pub fn as_timestamp_second_array(array: &dyn Array) -> Result<&TimestampSecondArray> {
240241
Ok(downcast_value!(array, TimestampSecondArray))
241242
}
242243

243-
// Downcast ArrayRef to IntervalYearMonthArray
244+
// Downcast Array to IntervalYearMonthArray
244245
pub fn as_interval_ym_array(array: &dyn Array) -> Result<&IntervalYearMonthArray> {
245246
Ok(downcast_value!(array, IntervalYearMonthArray))
246247
}
247248

248-
// Downcast ArrayRef to IntervalDayTimeArray
249+
// Downcast Array to IntervalDayTimeArray
249250
pub fn as_interval_dt_array(array: &dyn Array) -> Result<&IntervalDayTimeArray> {
250251
Ok(downcast_value!(array, IntervalDayTimeArray))
251252
}
252253

253-
// Downcast ArrayRef to IntervalMonthDayNanoArray
254+
// Downcast Array to IntervalMonthDayNanoArray
254255
pub fn as_interval_mdn_array(array: &dyn Array) -> Result<&IntervalMonthDayNanoArray> {
255256
Ok(downcast_value!(array, IntervalMonthDayNanoArray))
256257
}
257258

258-
// Downcast ArrayRef to BinaryArray
259+
// Downcast Array to DurationSecondArray
260+
pub fn as_duration_second_array(array: &dyn Array) -> Result<&DurationSecondArray> {
261+
Ok(downcast_value!(array, DurationSecondArray))
262+
}
263+
264+
// Downcast Array to DurationMillisecondArray
265+
pub fn as_duration_millisecond_array(
266+
array: &dyn Array,
267+
) -> Result<&DurationMillisecondArray> {
268+
Ok(downcast_value!(array, DurationMillisecondArray))
269+
}
270+
271+
// Downcast Array to DurationMicrosecondArray
272+
pub fn as_duration_microsecond_array(
273+
array: &dyn Array,
274+
) -> Result<&DurationMicrosecondArray> {
275+
Ok(downcast_value!(array, DurationMicrosecondArray))
276+
}
277+
278+
// Downcast Array to DurationNanosecondArray
279+
pub fn as_duration_nanosecond_array(
280+
array: &dyn Array,
281+
) -> Result<&DurationNanosecondArray> {
282+
Ok(downcast_value!(array, DurationNanosecondArray))
283+
}
284+
285+
// Downcast Array to BinaryArray
259286
pub fn as_binary_array(array: &dyn Array) -> Result<&BinaryArray> {
260287
Ok(downcast_value!(array, BinaryArray))
261288
}
262289

263-
// Downcast ArrayRef to BinaryViewArray
290+
// Downcast Array to BinaryViewArray
264291
pub fn as_binary_view_array(array: &dyn Array) -> Result<&BinaryViewArray> {
265292
Ok(downcast_value!(array, BinaryViewArray))
266293
}
267294

268-
// Downcast ArrayRef to LargeBinaryArray
295+
// Downcast Array to LargeBinaryArray
269296
pub fn as_large_binary_array(array: &dyn Array) -> Result<&LargeBinaryArray> {
270297
Ok(downcast_value!(array, LargeBinaryArray))
271298
}
272299

273-
// Downcast ArrayRef to FixedSizeListArray
300+
// Downcast Array to FixedSizeListArray
274301
pub fn as_fixed_size_list_array(array: &dyn Array) -> Result<&FixedSizeListArray> {
275302
Ok(downcast_value!(array, FixedSizeListArray))
276303
}
277304

278-
// Downcast ArrayRef to FixedSizeListArray
305+
// Downcast Array to FixedSizeListArray
279306
pub fn as_fixed_size_binary_array(array: &dyn Array) -> Result<&FixedSizeBinaryArray> {
280307
Ok(downcast_value!(array, FixedSizeBinaryArray))
281308
}
282309

283-
// Downcast ArrayRef to GenericBinaryArray
310+
// Downcast Array to GenericBinaryArray
284311
pub fn as_generic_string_array<T: OffsetSizeTrait>(
285312
array: &dyn Array,
286313
) -> Result<&GenericStringArray<T>> {

datafusion/common/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,12 @@ pub mod __private {
139139
impl<T: Array + ?Sized> DowncastArrayHelper for T {
140140
fn downcast_array_helper<U: Any>(&self) -> Result<&U> {
141141
self.as_any().downcast_ref().ok_or_else(|| {
142+
let actual_type = self.data_type();
143+
let desired_type_name = type_name::<U>();
142144
_internal_datafusion_err!(
143145
"could not cast array of type {} to {}",
144-
self.data_type(),
145-
type_name::<U>()
146+
actual_type,
147+
desired_type_name
146148
)
147149
})
148150
}

0 commit comments

Comments
 (0)