1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements.  See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership.  The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License.  You may obtain a copy of the License at
8 //
9 //   http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied.  See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 //! Defines cast kernels for `ArrayRef`, allowing casting arrays between supported
19 //! datatypes.
20 //!
21 //! Example:
22 //!
23 //! ```
24 //! use arrow::array::*;
25 //! use arrow::compute::cast;
26 //! use arrow::datatypes::DataType;
27 //! use std::sync::Arc;
28 //!
29 //! let a = Int32Array::from(vec![5, 6, 7]);
30 //! let array = Arc::new(a) as ArrayRef;
31 //! let b = cast(&array, &DataType::Float64).unwrap();
32 //! let c = b.as_any().downcast_ref::<Float64Array>().unwrap();
33 //! assert_eq!(5.0, c.value(0));
34 //! assert_eq!(6.0, c.value(1));
35 //! assert_eq!(7.0, c.value(2));
36 //! ```
37 
38 use std::str;
39 use std::sync::Arc;
40 
41 use crate::array::*;
42 use crate::buffer::Buffer;
43 use crate::compute::kernels::arithmetic::{divide, multiply};
44 use crate::datatypes::*;
45 use crate::error::{ArrowError, Result};
46 
47 /// Cast array to provided data type
48 ///
49 /// Behavior:
50 /// * Boolean to Utf8: `true` => '1', `false` => `0`
51 /// * Utf8 to numeric: strings that can't be parsed to numbers return null, float strings
52 ///   in integer casts return null
53 /// * Numeric to boolean: 0 returns `false`, any other value returns `true`
54 /// * List to List: the underlying data type is cast
55 /// * Primitive to List: a list array with 1 value per slot is created
56 /// * Date32 and Date64: precision lost when going to higher interval
57 /// * Time32 and Time64: precision lost when going to higher interval
58 /// * Timestamp and Date{32|64}: precision lost when going to higher interval
59 /// * Temporal to/from backing primitive: zero-copy with data type change
60 ///
61 /// Unsupported Casts
62 /// * To or from `StructArray`
63 /// * List to primitive
64 /// * Utf8 to boolean
65 /// * Interval and duration
cast(array: &ArrayRef, to_type: &DataType) -> Result<ArrayRef>66 pub fn cast(array: &ArrayRef, to_type: &DataType) -> Result<ArrayRef> {
67     use DataType::*;
68     let from_type = array.data_type();
69 
70     // clone array if types are the same
71     if from_type == to_type {
72         return Ok(array.clone());
73     }
74     match (from_type, to_type) {
75         (Struct(_), _) => Err(ArrowError::ComputeError(
76             "Cannot cast from struct to other types".to_string(),
77         )),
78         (_, Struct(_)) => Err(ArrowError::ComputeError(
79             "Cannot cast to struct from other types".to_string(),
80         )),
81         (List(_), List(ref to)) => {
82             let data = array.data_ref();
83             let underlying_array = make_array(data.child_data()[0].clone());
84             let cast_array = cast(&underlying_array, &to)?;
85             let array_data = ArrayData::new(
86                 *to.clone(),
87                 array.len(),
88                 Some(cast_array.null_count()),
89                 cast_array
90                     .data()
91                     .null_bitmap()
92                     .clone()
93                     .map(|bitmap| bitmap.bits),
94                 array.offset(),
95                 // reuse offset buffer
96                 data.buffers().to_vec(),
97                 vec![cast_array.data()],
98             );
99             let list = ListArray::from(Arc::new(array_data));
100             Ok(Arc::new(list) as ArrayRef)
101         }
102         (List(_), _) => Err(ArrowError::ComputeError(
103             "Cannot cast list to non-list data types".to_string(),
104         )),
105         (_, List(ref to)) => {
106             // cast primitive to list's primitive
107             let cast_array = cast(array, &to)?;
108             // create offsets, where if array.len() = 2, we have [0,1,2]
109             let offsets: Vec<i32> = (0..=array.len() as i32).collect();
110             let value_offsets = Buffer::from(offsets[..].to_byte_slice());
111             let list_data = ArrayData::new(
112                 *to.clone(),
113                 array.len(),
114                 Some(cast_array.null_count()),
115                 cast_array
116                     .data()
117                     .null_bitmap()
118                     .clone()
119                     .map(|bitmap| bitmap.bits),
120                 0,
121                 vec![value_offsets],
122                 vec![cast_array.data()],
123             );
124             let list_array = Arc::new(ListArray::from(Arc::new(list_data))) as ArrayRef;
125 
126             Ok(list_array)
127         }
128         (_, Boolean) => match from_type {
129             UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
130             UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
131             UInt32 => cast_numeric_to_bool::<UInt32Type>(array),
132             UInt64 => cast_numeric_to_bool::<UInt64Type>(array),
133             Int8 => cast_numeric_to_bool::<Int8Type>(array),
134             Int16 => cast_numeric_to_bool::<Int16Type>(array),
135             Int32 => cast_numeric_to_bool::<Int32Type>(array),
136             Int64 => cast_numeric_to_bool::<Int64Type>(array),
137             Float32 => cast_numeric_to_bool::<Float32Type>(array),
138             Float64 => cast_numeric_to_bool::<Float64Type>(array),
139             Utf8 => Err(ArrowError::ComputeError(format!(
140                 "Casting from {:?} to {:?} not supported",
141                 from_type, to_type,
142             ))),
143             _ => Err(ArrowError::ComputeError(format!(
144                 "Casting from {:?} to {:?} not supported",
145                 from_type, to_type,
146             ))),
147         },
148         (Boolean, _) => match to_type {
149             UInt8 => cast_bool_to_numeric::<UInt8Type>(array),
150             UInt16 => cast_bool_to_numeric::<UInt16Type>(array),
151             UInt32 => cast_bool_to_numeric::<UInt32Type>(array),
152             UInt64 => cast_bool_to_numeric::<UInt64Type>(array),
153             Int8 => cast_bool_to_numeric::<Int8Type>(array),
154             Int16 => cast_bool_to_numeric::<Int16Type>(array),
155             Int32 => cast_bool_to_numeric::<Int32Type>(array),
156             Int64 => cast_bool_to_numeric::<Int64Type>(array),
157             Float32 => cast_bool_to_numeric::<Float32Type>(array),
158             Float64 => cast_bool_to_numeric::<Float64Type>(array),
159             Utf8 => {
160                 let from = array.as_any().downcast_ref::<BooleanArray>().unwrap();
161                 let mut b = StringBuilder::new(array.len());
162                 for i in 0..array.len() {
163                     if array.is_null(i) {
164                         b.append(false)?;
165                     } else {
166                         b.append_value(if from.value(i) { "1" } else { "0" })?;
167                     }
168                 }
169 
170                 Ok(Arc::new(b.finish()) as ArrayRef)
171             }
172             _ => Err(ArrowError::ComputeError(format!(
173                 "Casting from {:?} to {:?} not supported",
174                 from_type, to_type,
175             ))),
176         },
177         (Utf8, _) => match to_type {
178             UInt8 => cast_string_to_numeric::<UInt8Type>(array),
179             UInt16 => cast_string_to_numeric::<UInt16Type>(array),
180             UInt32 => cast_string_to_numeric::<UInt32Type>(array),
181             UInt64 => cast_string_to_numeric::<UInt64Type>(array),
182             Int8 => cast_string_to_numeric::<Int8Type>(array),
183             Int16 => cast_string_to_numeric::<Int16Type>(array),
184             Int32 => cast_string_to_numeric::<Int32Type>(array),
185             Int64 => cast_string_to_numeric::<Int64Type>(array),
186             Float32 => cast_string_to_numeric::<Float32Type>(array),
187             Float64 => cast_string_to_numeric::<Float64Type>(array),
188             _ => Err(ArrowError::ComputeError(format!(
189                 "Casting from {:?} to {:?} not supported",
190                 from_type, to_type,
191             ))),
192         },
193         (_, Utf8) => match from_type {
194             UInt8 => cast_numeric_to_string::<UInt8Type>(array),
195             UInt16 => cast_numeric_to_string::<UInt16Type>(array),
196             UInt32 => cast_numeric_to_string::<UInt32Type>(array),
197             UInt64 => cast_numeric_to_string::<UInt64Type>(array),
198             Int8 => cast_numeric_to_string::<Int8Type>(array),
199             Int16 => cast_numeric_to_string::<Int16Type>(array),
200             Int32 => cast_numeric_to_string::<Int32Type>(array),
201             Int64 => cast_numeric_to_string::<Int64Type>(array),
202             Float32 => cast_numeric_to_string::<Float32Type>(array),
203             Float64 => cast_numeric_to_string::<Float64Type>(array),
204             Binary => {
205                 let from = array.as_any().downcast_ref::<BinaryArray>().unwrap();
206                 let mut b = StringBuilder::new(array.len());
207                 for i in 0..array.len() {
208                     if array.is_null(i) {
209                         b.append_null()?;
210                     } else {
211                         match str::from_utf8(from.value(i)) {
212                             Ok(s) => b.append_value(s)?,
213                             Err(_) => b.append_null()?, // not valid UTF8
214                         }
215                     }
216                 }
217 
218                 Ok(Arc::new(b.finish()) as ArrayRef)
219             }
220             _ => Err(ArrowError::ComputeError(format!(
221                 "Casting from {:?} to {:?} not supported",
222                 from_type, to_type,
223             ))),
224         },
225 
226         // start numeric casts
227         (UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array),
228         (UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array),
229         (UInt8, UInt64) => cast_numeric_arrays::<UInt8Type, UInt64Type>(array),
230         (UInt8, Int8) => cast_numeric_arrays::<UInt8Type, Int8Type>(array),
231         (UInt8, Int16) => cast_numeric_arrays::<UInt8Type, Int16Type>(array),
232         (UInt8, Int32) => cast_numeric_arrays::<UInt8Type, Int32Type>(array),
233         (UInt8, Int64) => cast_numeric_arrays::<UInt8Type, Int64Type>(array),
234         (UInt8, Float32) => cast_numeric_arrays::<UInt8Type, Float32Type>(array),
235         (UInt8, Float64) => cast_numeric_arrays::<UInt8Type, Float64Type>(array),
236 
237         (UInt16, UInt8) => cast_numeric_arrays::<UInt16Type, UInt8Type>(array),
238         (UInt16, UInt32) => cast_numeric_arrays::<UInt16Type, UInt32Type>(array),
239         (UInt16, UInt64) => cast_numeric_arrays::<UInt16Type, UInt64Type>(array),
240         (UInt16, Int8) => cast_numeric_arrays::<UInt16Type, Int8Type>(array),
241         (UInt16, Int16) => cast_numeric_arrays::<UInt16Type, Int16Type>(array),
242         (UInt16, Int32) => cast_numeric_arrays::<UInt16Type, Int32Type>(array),
243         (UInt16, Int64) => cast_numeric_arrays::<UInt16Type, Int64Type>(array),
244         (UInt16, Float32) => cast_numeric_arrays::<UInt16Type, Float32Type>(array),
245         (UInt16, Float64) => cast_numeric_arrays::<UInt16Type, Float64Type>(array),
246 
247         (UInt32, UInt8) => cast_numeric_arrays::<UInt32Type, UInt8Type>(array),
248         (UInt32, UInt16) => cast_numeric_arrays::<UInt32Type, UInt16Type>(array),
249         (UInt32, UInt64) => cast_numeric_arrays::<UInt32Type, UInt64Type>(array),
250         (UInt32, Int8) => cast_numeric_arrays::<UInt32Type, Int8Type>(array),
251         (UInt32, Int16) => cast_numeric_arrays::<UInt32Type, Int16Type>(array),
252         (UInt32, Int32) => cast_numeric_arrays::<UInt32Type, Int32Type>(array),
253         (UInt32, Int64) => cast_numeric_arrays::<UInt32Type, Int64Type>(array),
254         (UInt32, Float32) => cast_numeric_arrays::<UInt32Type, Float32Type>(array),
255         (UInt32, Float64) => cast_numeric_arrays::<UInt32Type, Float64Type>(array),
256 
257         (UInt64, UInt8) => cast_numeric_arrays::<UInt64Type, UInt8Type>(array),
258         (UInt64, UInt16) => cast_numeric_arrays::<UInt64Type, UInt16Type>(array),
259         (UInt64, UInt32) => cast_numeric_arrays::<UInt64Type, UInt32Type>(array),
260         (UInt64, Int8) => cast_numeric_arrays::<UInt64Type, Int8Type>(array),
261         (UInt64, Int16) => cast_numeric_arrays::<UInt64Type, Int16Type>(array),
262         (UInt64, Int32) => cast_numeric_arrays::<UInt64Type, Int32Type>(array),
263         (UInt64, Int64) => cast_numeric_arrays::<UInt64Type, Int64Type>(array),
264         (UInt64, Float32) => cast_numeric_arrays::<UInt64Type, Float32Type>(array),
265         (UInt64, Float64) => cast_numeric_arrays::<UInt64Type, Float64Type>(array),
266 
267         (Int8, UInt8) => cast_numeric_arrays::<Int8Type, UInt8Type>(array),
268         (Int8, UInt16) => cast_numeric_arrays::<Int8Type, UInt16Type>(array),
269         (Int8, UInt32) => cast_numeric_arrays::<Int8Type, UInt32Type>(array),
270         (Int8, UInt64) => cast_numeric_arrays::<Int8Type, UInt64Type>(array),
271         (Int8, Int16) => cast_numeric_arrays::<Int8Type, Int16Type>(array),
272         (Int8, Int32) => cast_numeric_arrays::<Int8Type, Int32Type>(array),
273         (Int8, Int64) => cast_numeric_arrays::<Int8Type, Int64Type>(array),
274         (Int8, Float32) => cast_numeric_arrays::<Int8Type, Float32Type>(array),
275         (Int8, Float64) => cast_numeric_arrays::<Int8Type, Float64Type>(array),
276 
277         (Int16, UInt8) => cast_numeric_arrays::<Int16Type, UInt8Type>(array),
278         (Int16, UInt16) => cast_numeric_arrays::<Int16Type, UInt16Type>(array),
279         (Int16, UInt32) => cast_numeric_arrays::<Int16Type, UInt32Type>(array),
280         (Int16, UInt64) => cast_numeric_arrays::<Int16Type, UInt64Type>(array),
281         (Int16, Int8) => cast_numeric_arrays::<Int16Type, Int8Type>(array),
282         (Int16, Int32) => cast_numeric_arrays::<Int16Type, Int32Type>(array),
283         (Int16, Int64) => cast_numeric_arrays::<Int16Type, Int64Type>(array),
284         (Int16, Float32) => cast_numeric_arrays::<Int16Type, Float32Type>(array),
285         (Int16, Float64) => cast_numeric_arrays::<Int16Type, Float64Type>(array),
286 
287         (Int32, UInt8) => cast_numeric_arrays::<Int32Type, UInt8Type>(array),
288         (Int32, UInt16) => cast_numeric_arrays::<Int32Type, UInt16Type>(array),
289         (Int32, UInt32) => cast_numeric_arrays::<Int32Type, UInt32Type>(array),
290         (Int32, UInt64) => cast_numeric_arrays::<Int32Type, UInt64Type>(array),
291         (Int32, Int8) => cast_numeric_arrays::<Int32Type, Int8Type>(array),
292         (Int32, Int16) => cast_numeric_arrays::<Int32Type, Int16Type>(array),
293         (Int32, Int64) => cast_numeric_arrays::<Int32Type, Int64Type>(array),
294         (Int32, Float32) => cast_numeric_arrays::<Int32Type, Float32Type>(array),
295         (Int32, Float64) => cast_numeric_arrays::<Int32Type, Float64Type>(array),
296 
297         (Int64, UInt8) => cast_numeric_arrays::<Int64Type, UInt8Type>(array),
298         (Int64, UInt16) => cast_numeric_arrays::<Int64Type, UInt16Type>(array),
299         (Int64, UInt32) => cast_numeric_arrays::<Int64Type, UInt32Type>(array),
300         (Int64, UInt64) => cast_numeric_arrays::<Int64Type, UInt64Type>(array),
301         (Int64, Int8) => cast_numeric_arrays::<Int64Type, Int8Type>(array),
302         (Int64, Int16) => cast_numeric_arrays::<Int64Type, Int16Type>(array),
303         (Int64, Int32) => cast_numeric_arrays::<Int64Type, Int32Type>(array),
304         (Int64, Float32) => cast_numeric_arrays::<Int64Type, Float32Type>(array),
305         (Int64, Float64) => cast_numeric_arrays::<Int64Type, Float64Type>(array),
306 
307         (Float32, UInt8) => cast_numeric_arrays::<Float32Type, UInt8Type>(array),
308         (Float32, UInt16) => cast_numeric_arrays::<Float32Type, UInt16Type>(array),
309         (Float32, UInt32) => cast_numeric_arrays::<Float32Type, UInt32Type>(array),
310         (Float32, UInt64) => cast_numeric_arrays::<Float32Type, UInt64Type>(array),
311         (Float32, Int8) => cast_numeric_arrays::<Float32Type, Int8Type>(array),
312         (Float32, Int16) => cast_numeric_arrays::<Float32Type, Int16Type>(array),
313         (Float32, Int32) => cast_numeric_arrays::<Float32Type, Int32Type>(array),
314         (Float32, Int64) => cast_numeric_arrays::<Float32Type, Int64Type>(array),
315         (Float32, Float64) => cast_numeric_arrays::<Float32Type, Float64Type>(array),
316 
317         (Float64, UInt8) => cast_numeric_arrays::<Float64Type, UInt8Type>(array),
318         (Float64, UInt16) => cast_numeric_arrays::<Float64Type, UInt16Type>(array),
319         (Float64, UInt32) => cast_numeric_arrays::<Float64Type, UInt32Type>(array),
320         (Float64, UInt64) => cast_numeric_arrays::<Float64Type, UInt64Type>(array),
321         (Float64, Int8) => cast_numeric_arrays::<Float64Type, Int8Type>(array),
322         (Float64, Int16) => cast_numeric_arrays::<Float64Type, Int16Type>(array),
323         (Float64, Int32) => cast_numeric_arrays::<Float64Type, Int32Type>(array),
324         (Float64, Int64) => cast_numeric_arrays::<Float64Type, Int64Type>(array),
325         (Float64, Float32) => cast_numeric_arrays::<Float64Type, Float32Type>(array),
326         // end numeric casts
327 
328         // temporal casts
329         (Int32, Date32(_)) => cast_array_data::<Date32Type>(array, to_type.clone()),
330         (Int32, Time32(_)) => cast_array_data::<Date32Type>(array, to_type.clone()),
331         (Date32(_), Int32) => cast_array_data::<Int32Type>(array, to_type.clone()),
332         (Time32(_), Int32) => cast_array_data::<Int32Type>(array, to_type.clone()),
333         (Int64, Date64(_)) => cast_array_data::<Date64Type>(array, to_type.clone()),
334         (Int64, Time64(_)) => cast_array_data::<Date64Type>(array, to_type.clone()),
335         (Date64(_), Int64) => cast_array_data::<Int64Type>(array, to_type.clone()),
336         (Time64(_), Int64) => cast_array_data::<Int64Type>(array, to_type.clone()),
337         (Date32(DateUnit::Day), Date64(DateUnit::Millisecond)) => {
338             let date_array = array.as_any().downcast_ref::<Date32Array>().unwrap();
339             let mut b = Date64Builder::new(array.len());
340             for i in 0..array.len() {
341                 if array.is_null(i) {
342                     b.append_null()?;
343                 } else {
344                     b.append_value(date_array.value(i) as i64 * MILLISECONDS_IN_DAY)?;
345                 }
346             }
347 
348             Ok(Arc::new(b.finish()) as ArrayRef)
349         }
350         (Date64(DateUnit::Millisecond), Date32(DateUnit::Day)) => {
351             let date_array = array.as_any().downcast_ref::<Date64Array>().unwrap();
352             let mut b = Date32Builder::new(array.len());
353             for i in 0..array.len() {
354                 if array.is_null(i) {
355                     b.append_null()?;
356                 } else {
357                     b.append_value((date_array.value(i) / MILLISECONDS_IN_DAY) as i32)?;
358                 }
359             }
360 
361             Ok(Arc::new(b.finish()) as ArrayRef)
362         }
363         (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => {
364             let time_array = Time32MillisecondArray::from(array.data());
365             let mult =
366                 Time32MillisecondArray::from(vec![MILLISECONDS as i32; array.len()]);
367             let time32_ms = multiply(&time_array, &mult)?;
368 
369             Ok(Arc::new(time32_ms) as ArrayRef)
370         }
371         (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => {
372             let time_array = Time32SecondArray::from(array.data());
373             let divisor = Time32SecondArray::from(vec![MILLISECONDS as i32; array.len()]);
374             let time32_s = divide(&time_array, &divisor)?;
375 
376             Ok(Arc::new(time32_s) as ArrayRef)
377         }
378         (Time32(from_unit), Time64(to_unit)) => {
379             let time_array = Int32Array::from(array.data());
380             // note: (numeric_cast + SIMD multiply) is faster than (cast & multiply)
381             let c: Int64Array = numeric_cast(&time_array)?;
382             let from_size = time_unit_multiple(&from_unit);
383             let to_size = time_unit_multiple(&to_unit);
384             // from is only smaller than to if 64milli/64second don't exist
385             let mult = Int64Array::from(vec![to_size / from_size; array.len()]);
386             let converted = multiply(&c, &mult)?;
387             let array_ref = Arc::new(converted) as ArrayRef;
388             use TimeUnit::*;
389             match to_unit {
390                 Microsecond => cast_array_data::<TimestampMicrosecondType>(
391                     &array_ref,
392                     to_type.clone(),
393                 ),
394                 Nanosecond => cast_array_data::<TimestampNanosecondType>(
395                     &array_ref,
396                     to_type.clone(),
397                 ),
398                 _ => unreachable!("array type not supported"),
399             }
400         }
401         (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => {
402             let time_array = Time64NanosecondArray::from(array.data());
403             let mult = Time64NanosecondArray::from(vec![MILLISECONDS; array.len()]);
404             let time64_ns = multiply(&time_array, &mult)?;
405 
406             Ok(Arc::new(time64_ns) as ArrayRef)
407         }
408         (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => {
409             let time_array = Time64MicrosecondArray::from(array.data());
410             let divisor = Time64MicrosecondArray::from(vec![MILLISECONDS; array.len()]);
411             let time64_us = divide(&time_array, &divisor)?;
412 
413             Ok(Arc::new(time64_us) as ArrayRef)
414         }
415         (Time64(from_unit), Time32(to_unit)) => {
416             let time_array = Int64Array::from(array.data());
417             let from_size = time_unit_multiple(&from_unit);
418             let to_size = time_unit_multiple(&to_unit);
419             let divisor = from_size / to_size;
420             match to_unit {
421                 TimeUnit::Second => {
422                     let mut b = Time32SecondBuilder::new(array.len());
423                     for i in 0..array.len() {
424                         if array.is_null(i) {
425                             b.append_null()?;
426                         } else {
427                             b.append_value(
428                                 (time_array.value(i) as i64 / divisor) as i32,
429                             )?;
430                         }
431                     }
432 
433                     Ok(Arc::new(b.finish()) as ArrayRef)
434                 }
435                 TimeUnit::Millisecond => {
436                     // currently can't dedup this builder [ARROW-4164]
437                     let mut b = Time32MillisecondBuilder::new(array.len());
438                     for i in 0..array.len() {
439                         if array.is_null(i) {
440                             b.append_null()?;
441                         } else {
442                             b.append_value(
443                                 (time_array.value(i) as i64 / divisor) as i32,
444                             )?;
445                         }
446                     }
447 
448                     Ok(Arc::new(b.finish()) as ArrayRef)
449                 }
450                 _ => unreachable!("array type not supported"),
451             }
452         }
453         (Timestamp(_, _), Int64) => cast_array_data::<Int64Type>(array, to_type.clone()),
454         (Int64, Timestamp(to_unit, _)) => {
455             use TimeUnit::*;
456             match to_unit {
457                 Second => cast_array_data::<TimestampSecondType>(array, to_type.clone()),
458                 Millisecond => {
459                     cast_array_data::<TimestampMillisecondType>(array, to_type.clone())
460                 }
461                 Microsecond => {
462                     cast_array_data::<TimestampMicrosecondType>(array, to_type.clone())
463                 }
464                 Nanosecond => {
465                     cast_array_data::<TimestampNanosecondType>(array, to_type.clone())
466                 }
467             }
468         }
469         (Timestamp(from_unit, _), Timestamp(to_unit, _)) => {
470             let time_array = Int64Array::from(array.data());
471             let from_size = time_unit_multiple(&from_unit);
472             let to_size = time_unit_multiple(&to_unit);
473             // we either divide or multiply, depending on size of each unit
474             // units are never the same when the types are the same
475             let converted = if from_size >= to_size {
476                 divide(
477                     &time_array,
478                     &Int64Array::from(vec![from_size / to_size; array.len()]),
479                 )?
480             } else {
481                 multiply(
482                     &time_array,
483                     &Int64Array::from(vec![to_size / from_size; array.len()]),
484                 )?
485             };
486             let array_ref = Arc::new(converted) as ArrayRef;
487             use TimeUnit::*;
488             match to_unit {
489                 Second => {
490                     cast_array_data::<TimestampSecondType>(&array_ref, to_type.clone())
491                 }
492                 Millisecond => cast_array_data::<TimestampMillisecondType>(
493                     &array_ref,
494                     to_type.clone(),
495                 ),
496                 Microsecond => cast_array_data::<TimestampMicrosecondType>(
497                     &array_ref,
498                     to_type.clone(),
499                 ),
500                 Nanosecond => cast_array_data::<TimestampNanosecondType>(
501                     &array_ref,
502                     to_type.clone(),
503                 ),
504             }
505         }
506         (Timestamp(from_unit, _), Date32(_)) => {
507             let time_array = Int64Array::from(array.data());
508             let from_size = time_unit_multiple(&from_unit) * SECONDS_IN_DAY;
509             let mut b = Date32Builder::new(array.len());
510             for i in 0..array.len() {
511                 if array.is_null(i) {
512                     b.append_null()?;
513                 } else {
514                     b.append_value((time_array.value(i) / from_size) as i32)?;
515                 }
516             }
517 
518             Ok(Arc::new(b.finish()) as ArrayRef)
519         }
520         (Timestamp(from_unit, _), Date64(_)) => {
521             let from_size = time_unit_multiple(&from_unit);
522             let to_size = MILLISECONDS;
523             if from_size != to_size {
524                 let time_array = Date64Array::from(array.data());
525                 Ok(Arc::new(divide(
526                     &time_array,
527                     &Date64Array::from(vec![from_size / to_size; array.len()]),
528                 )?) as ArrayRef)
529             } else {
530                 cast_array_data::<Date64Type>(array, to_type.clone())
531             }
532         }
533         // date64 to timestamp might not make sense,
534 
535         // end temporal casts
536         (_, _) => Err(ArrowError::ComputeError(format!(
537             "Casting from {:?} to {:?} not supported",
538             from_type, to_type,
539         ))),
540     }
541 }
542 
543 /// Get the time unit as a multiple of a second
time_unit_multiple(unit: &TimeUnit) -> i64544 fn time_unit_multiple(unit: &TimeUnit) -> i64 {
545     match unit {
546         TimeUnit::Second => 1,
547         TimeUnit::Millisecond => MILLISECONDS,
548         TimeUnit::Microsecond => MICROSECONDS,
549         TimeUnit::Nanosecond => NANOSECONDS,
550     }
551 }
552 
553 /// Number of seconds in a day
554 const SECONDS_IN_DAY: i64 = 86_400;
555 /// Number of milliseconds in a second
556 const MILLISECONDS: i64 = 1_000;
557 /// Number of microseconds in a second
558 const MICROSECONDS: i64 = 1_000_000;
559 /// Number of nanoseconds in a second
560 const NANOSECONDS: i64 = 1_000_000_000;
561 /// Number of milliseconds in a day
562 const MILLISECONDS_IN_DAY: i64 = SECONDS_IN_DAY * MILLISECONDS;
563 
564 /// Cast an array by changing its array_data type to the desired type
565 ///
566 /// Arrays should have the same primitive data type, otherwise this should fail.
567 /// We do not perform this check on primitive data types as we only use this
568 /// function internally, where it is guaranteed to be infallible.
cast_array_data<TO>(array: &ArrayRef, to_type: DataType) -> Result<ArrayRef> where TO: ArrowNumericType,569 fn cast_array_data<TO>(array: &ArrayRef, to_type: DataType) -> Result<ArrayRef>
570 where
571     TO: ArrowNumericType,
572 {
573     let data = Arc::new(ArrayData::new(
574         to_type,
575         array.len(),
576         Some(array.null_count()),
577         array.data().null_bitmap().clone().map(|bitmap| bitmap.bits),
578         array.data().offset(),
579         array.data().buffers().to_vec(),
580         vec![],
581     ));
582     Ok(Arc::new(PrimitiveArray::<TO>::from(data)) as ArrayRef)
583 }
584 
585 /// Convert Array into a PrimitiveArray of type, and apply numeric cast
cast_numeric_arrays<FROM, TO>(from: &ArrayRef) -> Result<ArrayRef> where FROM: ArrowNumericType, TO: ArrowNumericType, FROM::Native: num::NumCast, TO::Native: num::NumCast,586 fn cast_numeric_arrays<FROM, TO>(from: &ArrayRef) -> Result<ArrayRef>
587 where
588     FROM: ArrowNumericType,
589     TO: ArrowNumericType,
590     FROM::Native: num::NumCast,
591     TO::Native: num::NumCast,
592 {
593     numeric_cast::<FROM, TO>(
594         from.as_any()
595             .downcast_ref::<PrimitiveArray<FROM>>()
596             .unwrap(),
597     )
598     .map(|to| Arc::new(to) as ArrayRef)
599 }
600 
601 /// Natural cast between numeric types
numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>> where T: ArrowNumericType, R: ArrowNumericType, T::Native: num::NumCast, R::Native: num::NumCast,602 fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>>
603 where
604     T: ArrowNumericType,
605     R: ArrowNumericType,
606     T::Native: num::NumCast,
607     R::Native: num::NumCast,
608 {
609     let mut b = PrimitiveBuilder::<R>::new(from.len());
610 
611     for i in 0..from.len() {
612         if from.is_null(i) {
613             b.append_null()?;
614         } else {
615             // some casts return None, such as a negative value to u{8|16|32|64}
616             match num::cast::cast(from.value(i)) {
617                 Some(v) => b.append_value(v)?,
618                 None => b.append_null()?,
619             };
620         }
621     }
622 
623     Ok(b.finish())
624 }
625 
626 /// Cast numeric types to Utf8
cast_numeric_to_string<FROM>(array: &ArrayRef) -> Result<ArrayRef> where FROM: ArrowNumericType, FROM::Native: std::string::ToString,627 fn cast_numeric_to_string<FROM>(array: &ArrayRef) -> Result<ArrayRef>
628 where
629     FROM: ArrowNumericType,
630     FROM::Native: std::string::ToString,
631 {
632     numeric_to_string_cast::<FROM>(
633         array
634             .as_any()
635             .downcast_ref::<PrimitiveArray<FROM>>()
636             .unwrap(),
637     )
638     .map(|to| Arc::new(to) as ArrayRef)
639 }
640 
numeric_to_string_cast<T>(from: &PrimitiveArray<T>) -> Result<StringArray> where T: ArrowPrimitiveType + ArrowNumericType, T::Native: std::string::ToString,641 fn numeric_to_string_cast<T>(from: &PrimitiveArray<T>) -> Result<StringArray>
642 where
643     T: ArrowPrimitiveType + ArrowNumericType,
644     T::Native: std::string::ToString,
645 {
646     let mut b = StringBuilder::new(from.len());
647 
648     for i in 0..from.len() {
649         if from.is_null(i) {
650             b.append(false)?;
651         } else {
652             b.append_value(&from.value(i).to_string())?;
653         }
654     }
655 
656     Ok(b.finish())
657 }
658 
659 /// Cast numeric types to Utf8
cast_string_to_numeric<TO>(from: &ArrayRef) -> Result<ArrayRef> where TO: ArrowNumericType,660 fn cast_string_to_numeric<TO>(from: &ArrayRef) -> Result<ArrayRef>
661 where
662     TO: ArrowNumericType,
663 {
664     string_to_numeric_cast::<TO>(from.as_any().downcast_ref::<StringArray>().unwrap())
665         .map(|to| Arc::new(to) as ArrayRef)
666 }
667 
string_to_numeric_cast<T>(from: &StringArray) -> Result<PrimitiveArray<T>> where T: ArrowNumericType,668 fn string_to_numeric_cast<T>(from: &StringArray) -> Result<PrimitiveArray<T>>
669 where
670     T: ArrowNumericType,
671 {
672     let mut b = PrimitiveBuilder::<T>::new(from.len());
673 
674     for i in 0..from.len() {
675         if from.is_null(i) {
676             b.append_null()?;
677         } else {
678             match from.value(i).parse::<T::Native>() {
679                 Ok(v) => b.append_value(v)?,
680                 _ => b.append_null()?,
681             };
682         }
683     }
684 
685     Ok(b.finish())
686 }
687 
688 /// Cast numeric types to Boolean
689 ///
690 /// Any zero value returns `false` while non-zero returns `true`
cast_numeric_to_bool<FROM>(from: &ArrayRef) -> Result<ArrayRef> where FROM: ArrowNumericType,691 fn cast_numeric_to_bool<FROM>(from: &ArrayRef) -> Result<ArrayRef>
692 where
693     FROM: ArrowNumericType,
694 {
695     numeric_to_bool_cast::<FROM>(
696         from.as_any()
697             .downcast_ref::<PrimitiveArray<FROM>>()
698             .unwrap(),
699     )
700     .map(|to| Arc::new(to) as ArrayRef)
701 }
702 
numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray> where T: ArrowPrimitiveType + ArrowNumericType,703 fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray>
704 where
705     T: ArrowPrimitiveType + ArrowNumericType,
706 {
707     let mut b = BooleanBuilder::new(from.len());
708 
709     for i in 0..from.len() {
710         if from.is_null(i) {
711             b.append_null()?;
712         } else if from.value(i) != T::default_value() {
713             b.append_value(true)?;
714         } else {
715             b.append_value(false)?;
716         }
717     }
718 
719     Ok(b.finish())
720 }
721 
722 /// Cast Boolean types to numeric
723 ///
724 /// `false` returns 0 while `true` returns 1
cast_bool_to_numeric<TO>(from: &ArrayRef) -> Result<ArrayRef> where TO: ArrowNumericType, TO::Native: num::cast::NumCast,725 fn cast_bool_to_numeric<TO>(from: &ArrayRef) -> Result<ArrayRef>
726 where
727     TO: ArrowNumericType,
728     TO::Native: num::cast::NumCast,
729 {
730     bool_to_numeric_cast::<TO>(from.as_any().downcast_ref::<BooleanArray>().unwrap())
731         .map(|to| Arc::new(to) as ArrayRef)
732 }
733 
bool_to_numeric_cast<T>(from: &BooleanArray) -> Result<PrimitiveArray<T>> where T: ArrowNumericType, T::Native: num::NumCast,734 fn bool_to_numeric_cast<T>(from: &BooleanArray) -> Result<PrimitiveArray<T>>
735 where
736     T: ArrowNumericType,
737     T::Native: num::NumCast,
738 {
739     let mut b = PrimitiveBuilder::<T>::new(from.len());
740 
741     for i in 0..from.len() {
742         if from.is_null(i) {
743             b.append_null()?;
744         } else if from.value(i) {
745             // a workaround to cast a primitive to T::Native, infallible
746             match num::cast::cast(1) {
747                 Some(v) => b.append_value(v)?,
748                 None => b.append_null()?,
749             };
750         } else {
751             b.append_value(T::default_value())?;
752         }
753     }
754 
755     Ok(b.finish())
756 }
757 
758 #[cfg(test)]
759 mod tests {
760     use super::*;
761     use crate::buffer::Buffer;
762 
763     #[test]
test_cast_i32_to_f64()764     fn test_cast_i32_to_f64() {
765         let a = Int32Array::from(vec![5, 6, 7, 8, 9]);
766         let array = Arc::new(a) as ArrayRef;
767         let b = cast(&array, &DataType::Float64).unwrap();
768         let c = b.as_any().downcast_ref::<Float64Array>().unwrap();
769         assert_eq!(5.0, c.value(0));
770         assert_eq!(6.0, c.value(1));
771         assert_eq!(7.0, c.value(2));
772         assert_eq!(8.0, c.value(3));
773         assert_eq!(9.0, c.value(4));
774     }
775 
776     #[test]
test_cast_i32_to_u8()777     fn test_cast_i32_to_u8() {
778         let a = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
779         let array = Arc::new(a) as ArrayRef;
780         let b = cast(&array, &DataType::UInt8).unwrap();
781         let c = b.as_any().downcast_ref::<UInt8Array>().unwrap();
782         assert_eq!(false, c.is_valid(0));
783         assert_eq!(6, c.value(1));
784         assert_eq!(false, c.is_valid(2));
785         assert_eq!(8, c.value(3));
786         // overflows return None
787         assert_eq!(false, c.is_valid(4));
788     }
789 
790     #[test]
test_cast_i32_to_u8_sliced()791     fn test_cast_i32_to_u8_sliced() {
792         let a = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
793         let array = Arc::new(a) as ArrayRef;
794         assert_eq!(0, array.offset());
795         let array = array.slice(2, 3);
796         assert_eq!(2, array.offset());
797         let b = cast(&array, &DataType::UInt8).unwrap();
798         assert_eq!(3, b.len());
799         assert_eq!(0, b.offset());
800         let c = b.as_any().downcast_ref::<UInt8Array>().unwrap();
801         assert_eq!(false, c.is_valid(0));
802         assert_eq!(8, c.value(1));
803         // overflows return None
804         assert_eq!(false, c.is_valid(2));
805     }
806 
807     #[test]
test_cast_i32_to_i32()808     fn test_cast_i32_to_i32() {
809         let a = Int32Array::from(vec![5, 6, 7, 8, 9]);
810         let array = Arc::new(a) as ArrayRef;
811         let b = cast(&array, &DataType::Int32).unwrap();
812         let c = b.as_any().downcast_ref::<Int32Array>().unwrap();
813         assert_eq!(5, c.value(0));
814         assert_eq!(6, c.value(1));
815         assert_eq!(7, c.value(2));
816         assert_eq!(8, c.value(3));
817         assert_eq!(9, c.value(4));
818     }
819 
820     #[test]
test_cast_i32_to_list_i32()821     fn test_cast_i32_to_list_i32() {
822         let a = Int32Array::from(vec![5, 6, 7, 8, 9]);
823         let array = Arc::new(a) as ArrayRef;
824         let b = cast(&array, &DataType::List(Box::new(DataType::Int32))).unwrap();
825         assert_eq!(5, b.len());
826         let arr = b.as_any().downcast_ref::<ListArray>().unwrap();
827         assert_eq!(0, arr.value_offset(0));
828         assert_eq!(1, arr.value_offset(1));
829         assert_eq!(2, arr.value_offset(2));
830         assert_eq!(3, arr.value_offset(3));
831         assert_eq!(4, arr.value_offset(4));
832         assert_eq!(1, arr.value_length(0));
833         assert_eq!(1, arr.value_length(1));
834         assert_eq!(1, arr.value_length(2));
835         assert_eq!(1, arr.value_length(3));
836         assert_eq!(1, arr.value_length(4));
837         let values = arr.values();
838         let c = values.as_any().downcast_ref::<Int32Array>().unwrap();
839         assert_eq!(5, c.value(0));
840         assert_eq!(6, c.value(1));
841         assert_eq!(7, c.value(2));
842         assert_eq!(8, c.value(3));
843         assert_eq!(9, c.value(4));
844     }
845 
846     #[test]
test_cast_i32_to_list_i32_nullable()847     fn test_cast_i32_to_list_i32_nullable() {
848         let a = Int32Array::from(vec![Some(5), None, Some(7), Some(8), Some(9)]);
849         let array = Arc::new(a) as ArrayRef;
850         let b = cast(&array, &DataType::List(Box::new(DataType::Int32))).unwrap();
851         assert_eq!(5, b.len());
852         assert_eq!(1, b.null_count());
853         let arr = b.as_any().downcast_ref::<ListArray>().unwrap();
854         assert_eq!(0, arr.value_offset(0));
855         assert_eq!(1, arr.value_offset(1));
856         assert_eq!(2, arr.value_offset(2));
857         assert_eq!(3, arr.value_offset(3));
858         assert_eq!(4, arr.value_offset(4));
859         assert_eq!(1, arr.value_length(0));
860         assert_eq!(1, arr.value_length(1));
861         assert_eq!(1, arr.value_length(2));
862         assert_eq!(1, arr.value_length(3));
863         assert_eq!(1, arr.value_length(4));
864         let values = arr.values();
865         let c = values.as_any().downcast_ref::<Int32Array>().unwrap();
866         assert_eq!(1, c.null_count());
867         assert_eq!(5, c.value(0));
868         assert_eq!(false, c.is_valid(1));
869         assert_eq!(7, c.value(2));
870         assert_eq!(8, c.value(3));
871         assert_eq!(9, c.value(4));
872     }
873 
874     #[test]
test_cast_i32_to_list_f64_nullable_sliced()875     fn test_cast_i32_to_list_f64_nullable_sliced() {
876         let a = Int32Array::from(vec![Some(5), None, Some(7), Some(8), None, Some(10)]);
877         let array = Arc::new(a) as ArrayRef;
878         let array = array.slice(2, 4);
879         let b = cast(&array, &DataType::List(Box::new(DataType::Float64))).unwrap();
880         assert_eq!(4, b.len());
881         assert_eq!(1, b.null_count());
882         let arr = b.as_any().downcast_ref::<ListArray>().unwrap();
883         assert_eq!(0, arr.value_offset(0));
884         assert_eq!(1, arr.value_offset(1));
885         assert_eq!(2, arr.value_offset(2));
886         assert_eq!(3, arr.value_offset(3));
887         assert_eq!(1, arr.value_length(0));
888         assert_eq!(1, arr.value_length(1));
889         assert_eq!(1, arr.value_length(2));
890         assert_eq!(1, arr.value_length(3));
891         let values = arr.values();
892         let c = values.as_any().downcast_ref::<Float64Array>().unwrap();
893         assert_eq!(1, c.null_count());
894         assert_eq!(7.0, c.value(0));
895         assert_eq!(8.0, c.value(1));
896         assert_eq!(false, c.is_valid(2));
897         assert_eq!(10.0, c.value(3));
898     }
899 
900     #[test]
test_cast_utf8_to_i32()901     fn test_cast_utf8_to_i32() {
902         let a = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
903         let array = Arc::new(a) as ArrayRef;
904         let b = cast(&array, &DataType::Int32).unwrap();
905         let c = b.as_any().downcast_ref::<Int32Array>().unwrap();
906         assert_eq!(5, c.value(0));
907         assert_eq!(6, c.value(1));
908         assert_eq!(false, c.is_valid(2));
909         assert_eq!(8, c.value(3));
910         assert_eq!(false, c.is_valid(2));
911     }
912 
913     #[test]
test_cast_bool_to_i32()914     fn test_cast_bool_to_i32() {
915         let a = BooleanArray::from(vec![Some(true), Some(false), None]);
916         let array = Arc::new(a) as ArrayRef;
917         let b = cast(&array, &DataType::Int32).unwrap();
918         let c = b.as_any().downcast_ref::<Int32Array>().unwrap();
919         assert_eq!(1, c.value(0));
920         assert_eq!(0, c.value(1));
921         assert_eq!(false, c.is_valid(2));
922     }
923 
924     #[test]
test_cast_bool_to_f64()925     fn test_cast_bool_to_f64() {
926         let a = BooleanArray::from(vec![Some(true), Some(false), None]);
927         let array = Arc::new(a) as ArrayRef;
928         let b = cast(&array, &DataType::Float64).unwrap();
929         let c = b.as_any().downcast_ref::<Float64Array>().unwrap();
930         assert_eq!(1.0, c.value(0));
931         assert_eq!(0.0, c.value(1));
932         assert_eq!(false, c.is_valid(2));
933     }
934 
935     #[test]
936     #[should_panic(
937         expected = "Casting from Int32 to Timestamp(Microsecond, None) not supported"
938     )]
test_cast_int32_to_timestamp()939     fn test_cast_int32_to_timestamp() {
940         let a = Int32Array::from(vec![Some(2), Some(10), None]);
941         let array = Arc::new(a) as ArrayRef;
942         cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
943     }
944 
945     #[test]
test_cast_list_i32_to_list_u16()946     fn test_cast_list_i32_to_list_u16() {
947         // Construct a value array
948         let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 100000000]).data();
949 
950         let value_offsets = Buffer::from(&[0, 3, 6, 8].to_byte_slice());
951 
952         // Construct a list array from the above two
953         let list_data_type = DataType::List(Box::new(DataType::Int32));
954         let list_data = ArrayData::builder(list_data_type.clone())
955             .len(3)
956             .add_buffer(value_offsets.clone())
957             .add_child_data(value_data.clone())
958             .build();
959         let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
960 
961         let cast_array =
962             cast(&list_array, &DataType::List(Box::new(DataType::UInt16))).unwrap();
963         // 3 negative values should get lost when casting to unsigned,
964         // 1 value should overflow
965         assert_eq!(4, cast_array.null_count());
966         // offsets should be the same
967         assert_eq!(
968             list_array.data().buffers().to_vec(),
969             cast_array.data().buffers().to_vec()
970         );
971         let array = cast_array
972             .as_ref()
973             .as_any()
974             .downcast_ref::<ListArray>()
975             .unwrap();
976         assert_eq!(DataType::UInt16, array.value_type());
977         assert_eq!(4, array.values().null_count());
978         assert_eq!(3, array.value_length(0));
979         assert_eq!(3, array.value_length(1));
980         assert_eq!(2, array.value_length(2));
981         let values = array.values();
982         let u16arr = values.as_any().downcast_ref::<UInt16Array>().unwrap();
983         assert_eq!(8, u16arr.len());
984         assert_eq!(4, u16arr.null_count());
985 
986         assert_eq!(0, u16arr.value(0));
987         assert_eq!(0, u16arr.value(1));
988         assert_eq!(0, u16arr.value(2));
989         assert_eq!(false, u16arr.is_valid(3));
990         assert_eq!(false, u16arr.is_valid(4));
991         assert_eq!(false, u16arr.is_valid(5));
992         assert_eq!(2, u16arr.value(6));
993         assert_eq!(false, u16arr.is_valid(7));
994     }
995 
996     #[test]
997     #[should_panic(
998         expected = "Casting from Int32 to Timestamp(Microsecond, None) not supported"
999     )]
test_cast_list_i32_to_list_timestamp()1000     fn test_cast_list_i32_to_list_timestamp() {
1001         // Construct a value array
1002         let value_data =
1003             Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 8, 100000000]).data();
1004 
1005         let value_offsets = Buffer::from(&[0, 3, 6, 9].to_byte_slice());
1006 
1007         // Construct a list array from the above two
1008         let list_data_type = DataType::List(Box::new(DataType::Int32));
1009         let list_data = ArrayData::builder(list_data_type.clone())
1010             .len(3)
1011             .add_buffer(value_offsets.clone())
1012             .add_child_data(value_data.clone())
1013             .build();
1014         let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
1015 
1016         cast(
1017             &list_array,
1018             &DataType::List(Box::new(DataType::Timestamp(TimeUnit::Microsecond, None))),
1019         )
1020         .unwrap();
1021     }
1022 
1023     #[test]
test_cast_date32_to_date64()1024     fn test_cast_date32_to_date64() {
1025         let a = Date32Array::from(vec![10000, 17890]);
1026         let array = Arc::new(a) as ArrayRef;
1027         let b = cast(&array, &DataType::Date64(DateUnit::Millisecond)).unwrap();
1028         let c = b.as_any().downcast_ref::<Date64Array>().unwrap();
1029         assert_eq!(864000000000, c.value(0));
1030         assert_eq!(1545696000000, c.value(1));
1031     }
1032 
1033     #[test]
test_cast_date64_to_date32()1034     fn test_cast_date64_to_date32() {
1035         let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
1036         let array = Arc::new(a) as ArrayRef;
1037         let b = cast(&array, &DataType::Date32(DateUnit::Day)).unwrap();
1038         let c = b.as_any().downcast_ref::<Date32Array>().unwrap();
1039         assert_eq!(10000, c.value(0));
1040         assert_eq!(17890, c.value(1));
1041         assert!(c.is_null(2));
1042     }
1043 
1044     #[test]
test_cast_date32_to_int32()1045     fn test_cast_date32_to_int32() {
1046         let a = Date32Array::from(vec![10000, 17890]);
1047         let array = Arc::new(a) as ArrayRef;
1048         let b = cast(&array, &DataType::Int32).unwrap();
1049         let c = b.as_any().downcast_ref::<Int32Array>().unwrap();
1050         assert_eq!(10000, c.value(0));
1051         assert_eq!(17890, c.value(1));
1052     }
1053 
1054     #[test]
test_cast_int32_to_date32()1055     fn test_cast_int32_to_date32() {
1056         let a = Int32Array::from(vec![10000, 17890]);
1057         let array = Arc::new(a) as ArrayRef;
1058         let b = cast(&array, &DataType::Date32(DateUnit::Day)).unwrap();
1059         let c = b.as_any().downcast_ref::<Date32Array>().unwrap();
1060         assert_eq!(10000, c.value(0));
1061         assert_eq!(17890, c.value(1));
1062     }
1063 
1064     #[test]
test_cast_timestamp_to_date32()1065     fn test_cast_timestamp_to_date32() {
1066         let a = TimestampMillisecondArray::from_opt_vec(
1067             vec![Some(864000000005), Some(1545696000001), None],
1068             Some(Arc::new(String::from("UTC"))),
1069         );
1070         let array = Arc::new(a) as ArrayRef;
1071         let b = cast(&array, &DataType::Date32(DateUnit::Day)).unwrap();
1072         let c = b.as_any().downcast_ref::<Date32Array>().unwrap();
1073         assert_eq!(10000, c.value(0));
1074         assert_eq!(17890, c.value(1));
1075         assert!(c.is_null(2));
1076     }
1077 
1078     #[test]
test_cast_timestamp_to_date64()1079     fn test_cast_timestamp_to_date64() {
1080         let a = TimestampMillisecondArray::from_opt_vec(
1081             vec![Some(864000000005), Some(1545696000001), None],
1082             None,
1083         );
1084         let array = Arc::new(a) as ArrayRef;
1085         let b = cast(&array, &DataType::Date64(DateUnit::Millisecond)).unwrap();
1086         let c = b.as_any().downcast_ref::<Date64Array>().unwrap();
1087         assert_eq!(864000000005, c.value(0));
1088         assert_eq!(1545696000001, c.value(1));
1089         assert!(c.is_null(2));
1090     }
1091 
1092     #[test]
test_cast_timestamp_to_i64()1093     fn test_cast_timestamp_to_i64() {
1094         let a = TimestampMillisecondArray::from_opt_vec(
1095             vec![Some(864000000005), Some(1545696000001), None],
1096             Some(Arc::new("UTC".to_string())),
1097         );
1098         let array = Arc::new(a) as ArrayRef;
1099         let b = cast(&array, &DataType::Int64).unwrap();
1100         let c = b.as_any().downcast_ref::<Int64Array>().unwrap();
1101         assert_eq!(&DataType::Int64, c.data_type());
1102         assert_eq!(864000000005, c.value(0));
1103         assert_eq!(1545696000001, c.value(1));
1104         assert!(c.is_null(2));
1105     }
1106 
1107     #[test]
test_cast_between_timestamps()1108     fn test_cast_between_timestamps() {
1109         let a = TimestampMillisecondArray::from_opt_vec(
1110             vec![Some(864000003005), Some(1545696002001), None],
1111             None,
1112         );
1113         let array = Arc::new(a) as ArrayRef;
1114         let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
1115         let c = b.as_any().downcast_ref::<TimestampSecondArray>().unwrap();
1116         assert_eq!(864000003, c.value(0));
1117         assert_eq!(1545696002, c.value(1));
1118         assert!(c.is_null(2));
1119     }
1120 
1121     #[test]
test_cast_from_f64()1122     fn test_cast_from_f64() {
1123         let f64_values: Vec<f64> = vec![
1124             std::i64::MIN as f64,
1125             std::i32::MIN as f64,
1126             std::i16::MIN as f64,
1127             std::i8::MIN as f64,
1128             0_f64,
1129             std::u8::MAX as f64,
1130             std::u16::MAX as f64,
1131             std::u32::MAX as f64,
1132             std::u64::MAX as f64,
1133         ];
1134         let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values.clone()));
1135 
1136         let f64_expected = vec![
1137             "-9223372036854776000.0",
1138             "-2147483648.0",
1139             "-32768.0",
1140             "-128.0",
1141             "0.0",
1142             "255.0",
1143             "65535.0",
1144             "4294967295.0",
1145             "18446744073709552000.0",
1146         ];
1147         assert_eq!(
1148             f64_expected,
1149             get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
1150         );
1151 
1152         let f32_expected = vec![
1153             "-9223372000000000000.0",
1154             "-2147483600.0",
1155             "-32768.0",
1156             "-128.0",
1157             "0.0",
1158             "255.0",
1159             "65535.0",
1160             "4294967300.0",
1161             "18446744000000000000.0",
1162         ];
1163         assert_eq!(
1164             f32_expected,
1165             get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
1166         );
1167 
1168         let i64_expected = vec![
1169             "-9223372036854775808",
1170             "-2147483648",
1171             "-32768",
1172             "-128",
1173             "0",
1174             "255",
1175             "65535",
1176             "4294967295",
1177             "null",
1178         ];
1179         assert_eq!(
1180             i64_expected,
1181             get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
1182         );
1183 
1184         let i32_expected = vec![
1185             "null",
1186             "-2147483648",
1187             "-32768",
1188             "-128",
1189             "0",
1190             "255",
1191             "65535",
1192             "null",
1193             "null",
1194         ];
1195         assert_eq!(
1196             i32_expected,
1197             get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
1198         );
1199 
1200         let i16_expected = vec![
1201             "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
1202         ];
1203         assert_eq!(
1204             i16_expected,
1205             get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
1206         );
1207 
1208         let i8_expected = vec![
1209             "null", "null", "null", "-128", "0", "null", "null", "null", "null",
1210         ];
1211         assert_eq!(
1212             i8_expected,
1213             get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
1214         );
1215 
1216         let u64_expected = vec![
1217             "null",
1218             "null",
1219             "null",
1220             "null",
1221             "0",
1222             "255",
1223             "65535",
1224             "4294967295",
1225             "null",
1226         ];
1227         assert_eq!(
1228             u64_expected,
1229             get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
1230         );
1231 
1232         let u32_expected = vec![
1233             "null",
1234             "null",
1235             "null",
1236             "null",
1237             "0",
1238             "255",
1239             "65535",
1240             "4294967295",
1241             "null",
1242         ];
1243         assert_eq!(
1244             u32_expected,
1245             get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
1246         );
1247 
1248         let u16_expected = vec![
1249             "null", "null", "null", "null", "0", "255", "65535", "null", "null",
1250         ];
1251         assert_eq!(
1252             u16_expected,
1253             get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
1254         );
1255 
1256         let u8_expected = vec![
1257             "null", "null", "null", "null", "0", "255", "null", "null", "null",
1258         ];
1259         assert_eq!(
1260             u8_expected,
1261             get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
1262         );
1263     }
1264 
1265     #[test]
test_cast_from_f32()1266     fn test_cast_from_f32() {
1267         let f32_values: Vec<f32> = vec![
1268             std::i32::MIN as f32,
1269             std::i32::MIN as f32,
1270             std::i16::MIN as f32,
1271             std::i8::MIN as f32,
1272             0_f32,
1273             std::u8::MAX as f32,
1274             std::u16::MAX as f32,
1275             std::u32::MAX as f32,
1276             std::u32::MAX as f32,
1277         ];
1278         let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values.clone()));
1279 
1280         let f64_expected = vec![
1281             "-2147483648.0",
1282             "-2147483648.0",
1283             "-32768.0",
1284             "-128.0",
1285             "0.0",
1286             "255.0",
1287             "65535.0",
1288             "4294967296.0",
1289             "4294967296.0",
1290         ];
1291         assert_eq!(
1292             f64_expected,
1293             get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
1294         );
1295 
1296         let f32_expected = vec![
1297             "-2147483600.0",
1298             "-2147483600.0",
1299             "-32768.0",
1300             "-128.0",
1301             "0.0",
1302             "255.0",
1303             "65535.0",
1304             "4294967300.0",
1305             "4294967300.0",
1306         ];
1307         assert_eq!(
1308             f32_expected,
1309             get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
1310         );
1311 
1312         let i64_expected = vec![
1313             "-2147483648",
1314             "-2147483648",
1315             "-32768",
1316             "-128",
1317             "0",
1318             "255",
1319             "65535",
1320             "4294967296",
1321             "4294967296",
1322         ];
1323         assert_eq!(
1324             i64_expected,
1325             get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
1326         );
1327 
1328         let i32_expected = vec![
1329             "-2147483648",
1330             "-2147483648",
1331             "-32768",
1332             "-128",
1333             "0",
1334             "255",
1335             "65535",
1336             "null",
1337             "null",
1338         ];
1339         assert_eq!(
1340             i32_expected,
1341             get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
1342         );
1343 
1344         let i16_expected = vec![
1345             "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
1346         ];
1347         assert_eq!(
1348             i16_expected,
1349             get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
1350         );
1351 
1352         let i8_expected = vec![
1353             "null", "null", "null", "-128", "0", "null", "null", "null", "null",
1354         ];
1355         assert_eq!(
1356             i8_expected,
1357             get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
1358         );
1359 
1360         let u64_expected = vec![
1361             "null",
1362             "null",
1363             "null",
1364             "null",
1365             "0",
1366             "255",
1367             "65535",
1368             "4294967296",
1369             "4294967296",
1370         ];
1371         assert_eq!(
1372             u64_expected,
1373             get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
1374         );
1375 
1376         let u32_expected = vec![
1377             "null", "null", "null", "null", "0", "255", "65535", "null", "null",
1378         ];
1379         assert_eq!(
1380             u32_expected,
1381             get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
1382         );
1383 
1384         let u16_expected = vec![
1385             "null", "null", "null", "null", "0", "255", "65535", "null", "null",
1386         ];
1387         assert_eq!(
1388             u16_expected,
1389             get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
1390         );
1391 
1392         let u8_expected = vec![
1393             "null", "null", "null", "null", "0", "255", "null", "null", "null",
1394         ];
1395         assert_eq!(
1396             u8_expected,
1397             get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
1398         );
1399     }
1400 
1401     #[test]
test_cast_from_uint64()1402     fn test_cast_from_uint64() {
1403         let u64_values: Vec<u64> = vec![
1404             0,
1405             std::u8::MAX as u64,
1406             std::u16::MAX as u64,
1407             std::u32::MAX as u64,
1408             std::u64::MAX,
1409         ];
1410         let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values.clone()));
1411 
1412         let f64_expected = vec![
1413             "0.0",
1414             "255.0",
1415             "65535.0",
1416             "4294967295.0",
1417             "18446744073709552000.0",
1418         ];
1419         assert_eq!(
1420             f64_expected,
1421             get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
1422         );
1423 
1424         let f32_expected = vec![
1425             "0.0",
1426             "255.0",
1427             "65535.0",
1428             "4294967300.0",
1429             "18446744000000000000.0",
1430         ];
1431         assert_eq!(
1432             f32_expected,
1433             get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
1434         );
1435 
1436         let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
1437         assert_eq!(
1438             i64_expected,
1439             get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
1440         );
1441 
1442         let i32_expected = vec!["0", "255", "65535", "null", "null"];
1443         assert_eq!(
1444             i32_expected,
1445             get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
1446         );
1447 
1448         let i16_expected = vec!["0", "255", "null", "null", "null"];
1449         assert_eq!(
1450             i16_expected,
1451             get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
1452         );
1453 
1454         let i8_expected = vec!["0", "null", "null", "null", "null"];
1455         assert_eq!(
1456             i8_expected,
1457             get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
1458         );
1459 
1460         let u64_expected =
1461             vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
1462         assert_eq!(
1463             u64_expected,
1464             get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
1465         );
1466 
1467         let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
1468         assert_eq!(
1469             u32_expected,
1470             get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
1471         );
1472 
1473         let u16_expected = vec!["0", "255", "65535", "null", "null"];
1474         assert_eq!(
1475             u16_expected,
1476             get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
1477         );
1478 
1479         let u8_expected = vec!["0", "255", "null", "null", "null"];
1480         assert_eq!(
1481             u8_expected,
1482             get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
1483         );
1484     }
1485 
1486     #[test]
test_cast_from_uint32()1487     fn test_cast_from_uint32() {
1488         let u32_values: Vec<u32> = vec![
1489             0,
1490             std::u8::MAX as u32,
1491             std::u16::MAX as u32,
1492             std::u32::MAX as u32,
1493         ];
1494         let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values.clone()));
1495 
1496         let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
1497         assert_eq!(
1498             f64_expected,
1499             get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
1500         );
1501 
1502         let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
1503         assert_eq!(
1504             f32_expected,
1505             get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
1506         );
1507 
1508         let i64_expected = vec!["0", "255", "65535", "4294967295"];
1509         assert_eq!(
1510             i64_expected,
1511             get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
1512         );
1513 
1514         let i32_expected = vec!["0", "255", "65535", "null"];
1515         assert_eq!(
1516             i32_expected,
1517             get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
1518         );
1519 
1520         let i16_expected = vec!["0", "255", "null", "null"];
1521         assert_eq!(
1522             i16_expected,
1523             get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
1524         );
1525 
1526         let i8_expected = vec!["0", "null", "null", "null"];
1527         assert_eq!(
1528             i8_expected,
1529             get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
1530         );
1531 
1532         let u64_expected = vec!["0", "255", "65535", "4294967295"];
1533         assert_eq!(
1534             u64_expected,
1535             get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
1536         );
1537 
1538         let u32_expected = vec!["0", "255", "65535", "4294967295"];
1539         assert_eq!(
1540             u32_expected,
1541             get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
1542         );
1543 
1544         let u16_expected = vec!["0", "255", "65535", "null"];
1545         assert_eq!(
1546             u16_expected,
1547             get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
1548         );
1549 
1550         let u8_expected = vec!["0", "255", "null", "null"];
1551         assert_eq!(
1552             u8_expected,
1553             get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
1554         );
1555     }
1556 
1557     #[test]
test_cast_from_uint16()1558     fn test_cast_from_uint16() {
1559         let u16_values: Vec<u16> = vec![0, std::u8::MAX as u16, std::u16::MAX as u16];
1560         let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values.clone()));
1561 
1562         let f64_expected = vec!["0.0", "255.0", "65535.0"];
1563         assert_eq!(
1564             f64_expected,
1565             get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
1566         );
1567 
1568         let f32_expected = vec!["0.0", "255.0", "65535.0"];
1569         assert_eq!(
1570             f32_expected,
1571             get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
1572         );
1573 
1574         let i64_expected = vec!["0", "255", "65535"];
1575         assert_eq!(
1576             i64_expected,
1577             get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
1578         );
1579 
1580         let i32_expected = vec!["0", "255", "65535"];
1581         assert_eq!(
1582             i32_expected,
1583             get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
1584         );
1585 
1586         let i16_expected = vec!["0", "255", "null"];
1587         assert_eq!(
1588             i16_expected,
1589             get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
1590         );
1591 
1592         let i8_expected = vec!["0", "null", "null"];
1593         assert_eq!(
1594             i8_expected,
1595             get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
1596         );
1597 
1598         let u64_expected = vec!["0", "255", "65535"];
1599         assert_eq!(
1600             u64_expected,
1601             get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
1602         );
1603 
1604         let u32_expected = vec!["0", "255", "65535"];
1605         assert_eq!(
1606             u32_expected,
1607             get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
1608         );
1609 
1610         let u16_expected = vec!["0", "255", "65535"];
1611         assert_eq!(
1612             u16_expected,
1613             get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
1614         );
1615 
1616         let u8_expected = vec!["0", "255", "null"];
1617         assert_eq!(
1618             u8_expected,
1619             get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
1620         );
1621     }
1622 
1623     #[test]
test_cast_from_uint8()1624     fn test_cast_from_uint8() {
1625         let u8_values: Vec<u8> = vec![0, std::u8::MAX];
1626         let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values.clone()));
1627 
1628         let f64_expected = vec!["0.0", "255.0"];
1629         assert_eq!(
1630             f64_expected,
1631             get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
1632         );
1633 
1634         let f32_expected = vec!["0.0", "255.0"];
1635         assert_eq!(
1636             f32_expected,
1637             get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
1638         );
1639 
1640         let i64_expected = vec!["0", "255"];
1641         assert_eq!(
1642             i64_expected,
1643             get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
1644         );
1645 
1646         let i32_expected = vec!["0", "255"];
1647         assert_eq!(
1648             i32_expected,
1649             get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
1650         );
1651 
1652         let i16_expected = vec!["0", "255"];
1653         assert_eq!(
1654             i16_expected,
1655             get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
1656         );
1657 
1658         let i8_expected = vec!["0", "null"];
1659         assert_eq!(
1660             i8_expected,
1661             get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
1662         );
1663 
1664         let u64_expected = vec!["0", "255"];
1665         assert_eq!(
1666             u64_expected,
1667             get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
1668         );
1669 
1670         let u32_expected = vec!["0", "255"];
1671         assert_eq!(
1672             u32_expected,
1673             get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
1674         );
1675 
1676         let u16_expected = vec!["0", "255"];
1677         assert_eq!(
1678             u16_expected,
1679             get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
1680         );
1681 
1682         let u8_expected = vec!["0", "255"];
1683         assert_eq!(
1684             u8_expected,
1685             get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
1686         );
1687     }
1688 
1689     #[test]
test_cast_from_int64()1690     fn test_cast_from_int64() {
1691         let i64_values: Vec<i64> = vec![
1692             std::i64::MIN,
1693             std::i32::MIN as i64,
1694             std::i16::MIN as i64,
1695             std::i8::MIN as i64,
1696             0,
1697             std::i8::MAX as i64,
1698             std::i16::MAX as i64,
1699             std::i32::MAX as i64,
1700             std::i64::MAX,
1701         ];
1702         let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values.clone()));
1703 
1704         let f64_expected = vec![
1705             "-9223372036854776000.0",
1706             "-2147483648.0",
1707             "-32768.0",
1708             "-128.0",
1709             "0.0",
1710             "127.0",
1711             "32767.0",
1712             "2147483647.0",
1713             "9223372036854776000.0",
1714         ];
1715         assert_eq!(
1716             f64_expected,
1717             get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
1718         );
1719 
1720         let f32_expected = vec![
1721             "-9223372000000000000.0",
1722             "-2147483600.0",
1723             "-32768.0",
1724             "-128.0",
1725             "0.0",
1726             "127.0",
1727             "32767.0",
1728             "2147483600.0",
1729             "9223372000000000000.0",
1730         ];
1731         assert_eq!(
1732             f32_expected,
1733             get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
1734         );
1735 
1736         let i64_expected = vec![
1737             "-9223372036854775808",
1738             "-2147483648",
1739             "-32768",
1740             "-128",
1741             "0",
1742             "127",
1743             "32767",
1744             "2147483647",
1745             "9223372036854775807",
1746         ];
1747         assert_eq!(
1748             i64_expected,
1749             get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
1750         );
1751 
1752         let i32_expected = vec![
1753             "null",
1754             "-2147483648",
1755             "-32768",
1756             "-128",
1757             "0",
1758             "127",
1759             "32767",
1760             "2147483647",
1761             "null",
1762         ];
1763         assert_eq!(
1764             i32_expected,
1765             get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
1766         );
1767 
1768         let i16_expected = vec![
1769             "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
1770         ];
1771         assert_eq!(
1772             i16_expected,
1773             get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
1774         );
1775 
1776         let i8_expected = vec![
1777             "null", "null", "null", "-128", "0", "127", "null", "null", "null",
1778         ];
1779         assert_eq!(
1780             i8_expected,
1781             get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
1782         );
1783 
1784         let u64_expected = vec![
1785             "null",
1786             "null",
1787             "null",
1788             "null",
1789             "0",
1790             "127",
1791             "32767",
1792             "2147483647",
1793             "9223372036854775807",
1794         ];
1795         assert_eq!(
1796             u64_expected,
1797             get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
1798         );
1799 
1800         let u32_expected = vec![
1801             "null",
1802             "null",
1803             "null",
1804             "null",
1805             "0",
1806             "127",
1807             "32767",
1808             "2147483647",
1809             "null",
1810         ];
1811         assert_eq!(
1812             u32_expected,
1813             get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
1814         );
1815 
1816         let u16_expected = vec![
1817             "null", "null", "null", "null", "0", "127", "32767", "null", "null",
1818         ];
1819         assert_eq!(
1820             u16_expected,
1821             get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
1822         );
1823 
1824         let u8_expected = vec![
1825             "null", "null", "null", "null", "0", "127", "null", "null", "null",
1826         ];
1827         assert_eq!(
1828             u8_expected,
1829             get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
1830         );
1831     }
1832 
1833     #[test]
test_cast_from_int32()1834     fn test_cast_from_int32() {
1835         let i32_values: Vec<i32> = vec![
1836             std::i32::MIN as i32,
1837             std::i16::MIN as i32,
1838             std::i8::MIN as i32,
1839             0,
1840             std::i8::MAX as i32,
1841             std::i16::MAX as i32,
1842             std::i32::MAX as i32,
1843         ];
1844         let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values.clone()));
1845 
1846         let f64_expected = vec![
1847             "-2147483648.0",
1848             "-32768.0",
1849             "-128.0",
1850             "0.0",
1851             "127.0",
1852             "32767.0",
1853             "2147483647.0",
1854         ];
1855         assert_eq!(
1856             f64_expected,
1857             get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
1858         );
1859 
1860         let f32_expected = vec![
1861             "-2147483600.0",
1862             "-32768.0",
1863             "-128.0",
1864             "0.0",
1865             "127.0",
1866             "32767.0",
1867             "2147483600.0",
1868         ];
1869         assert_eq!(
1870             f32_expected,
1871             get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
1872         );
1873 
1874         let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
1875         assert_eq!(
1876             i16_expected,
1877             get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
1878         );
1879 
1880         let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
1881         assert_eq!(
1882             i8_expected,
1883             get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
1884         );
1885 
1886         let u64_expected =
1887             vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
1888         assert_eq!(
1889             u64_expected,
1890             get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
1891         );
1892 
1893         let u32_expected =
1894             vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
1895         assert_eq!(
1896             u32_expected,
1897             get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
1898         );
1899 
1900         let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
1901         assert_eq!(
1902             u16_expected,
1903             get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
1904         );
1905 
1906         let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
1907         assert_eq!(
1908             u8_expected,
1909             get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
1910         );
1911     }
1912 
1913     #[test]
test_cast_from_int16()1914     fn test_cast_from_int16() {
1915         let i16_values: Vec<i16> = vec![
1916             std::i16::MIN,
1917             std::i8::MIN as i16,
1918             0,
1919             std::i8::MAX as i16,
1920             std::i16::MAX,
1921         ];
1922         let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values.clone()));
1923 
1924         let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
1925         assert_eq!(
1926             f64_expected,
1927             get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
1928         );
1929 
1930         let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
1931         assert_eq!(
1932             f32_expected,
1933             get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
1934         );
1935 
1936         let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
1937         assert_eq!(
1938             i64_expected,
1939             get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
1940         );
1941 
1942         let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
1943         assert_eq!(
1944             i32_expected,
1945             get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
1946         );
1947 
1948         let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
1949         assert_eq!(
1950             i16_expected,
1951             get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
1952         );
1953 
1954         let i8_expected = vec!["null", "-128", "0", "127", "null"];
1955         assert_eq!(
1956             i8_expected,
1957             get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
1958         );
1959 
1960         let u64_expected = vec!["null", "null", "0", "127", "32767"];
1961         assert_eq!(
1962             u64_expected,
1963             get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
1964         );
1965 
1966         let u32_expected = vec!["null", "null", "0", "127", "32767"];
1967         assert_eq!(
1968             u32_expected,
1969             get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
1970         );
1971 
1972         let u16_expected = vec!["null", "null", "0", "127", "32767"];
1973         assert_eq!(
1974             u16_expected,
1975             get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
1976         );
1977 
1978         let u8_expected = vec!["null", "null", "0", "127", "null"];
1979         assert_eq!(
1980             u8_expected,
1981             get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
1982         );
1983     }
1984 
1985     #[test]
test_cast_from_int8()1986     fn test_cast_from_int8() {
1987         let i8_values: Vec<i8> = vec![std::i8::MIN, 0, std::i8::MAX];
1988         let i8_array: ArrayRef = Arc::new(Int8Array::from(i8_values.clone()));
1989 
1990         let f64_expected = vec!["-128.0", "0.0", "127.0"];
1991         assert_eq!(
1992             f64_expected,
1993             get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
1994         );
1995 
1996         let f32_expected = vec!["-128.0", "0.0", "127.0"];
1997         assert_eq!(
1998             f32_expected,
1999             get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
2000         );
2001 
2002         let i64_expected = vec!["-128", "0", "127"];
2003         assert_eq!(
2004             i64_expected,
2005             get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
2006         );
2007 
2008         let i32_expected = vec!["-128", "0", "127"];
2009         assert_eq!(
2010             i32_expected,
2011             get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
2012         );
2013 
2014         let i16_expected = vec!["-128", "0", "127"];
2015         assert_eq!(
2016             i16_expected,
2017             get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
2018         );
2019 
2020         let i8_expected = vec!["-128", "0", "127"];
2021         assert_eq!(
2022             i8_expected,
2023             get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
2024         );
2025 
2026         let u64_expected = vec!["null", "0", "127"];
2027         assert_eq!(
2028             u64_expected,
2029             get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
2030         );
2031 
2032         let u32_expected = vec!["null", "0", "127"];
2033         assert_eq!(
2034             u32_expected,
2035             get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
2036         );
2037 
2038         let u16_expected = vec!["null", "0", "127"];
2039         assert_eq!(
2040             u16_expected,
2041             get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
2042         );
2043 
2044         let u8_expected = vec!["null", "0", "127"];
2045         assert_eq!(
2046             u8_expected,
2047             get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
2048         );
2049     }
2050 
get_cast_values<T>(array: &ArrayRef, dt: &DataType) -> Vec<String> where T: ArrowNumericType,2051     fn get_cast_values<T>(array: &ArrayRef, dt: &DataType) -> Vec<String>
2052     where
2053         T: ArrowNumericType,
2054     {
2055         let c = cast(&array, dt).unwrap();
2056         let a = c.as_any().downcast_ref::<PrimitiveArray<T>>().unwrap();
2057         let mut v: Vec<String> = vec![];
2058         for i in 0..array.len() {
2059             if a.is_null(i) {
2060                 v.push("null".to_string())
2061             } else {
2062                 v.push(format!("{:?}", a.value(i)));
2063             }
2064         }
2065         v
2066     }
2067 }
2068