arrow_cast/cast/
mod.rs

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//! Cast kernels to convert [`ArrayRef`]  between supported datatypes.
19//!
20//! See [`cast_with_options`] for more information on specific conversions.
21//!
22//! Example:
23//!
24//! ```
25//! # use arrow_array::*;
26//! # use arrow_cast::cast;
27//! # use arrow_schema::DataType;
28//! # use std::sync::Arc;
29//! # use arrow_array::types::Float64Type;
30//! # use arrow_array::cast::AsArray;
31//! // int32 to float64
32//! let a = Int32Array::from(vec![5, 6, 7]);
33//! let b = cast(&a, &DataType::Float64).unwrap();
34//! let c = b.as_primitive::<Float64Type>();
35//! assert_eq!(5.0, c.value(0));
36//! assert_eq!(6.0, c.value(1));
37//! assert_eq!(7.0, c.value(2));
38//! ```
39
40mod decimal;
41mod dictionary;
42mod list;
43mod map;
44mod string;
45use crate::cast::decimal::*;
46use crate::cast::dictionary::*;
47use crate::cast::list::*;
48use crate::cast::map::*;
49use crate::cast::string::*;
50
51use arrow_buffer::IntervalMonthDayNano;
52use arrow_data::ByteView;
53use chrono::{NaiveTime, Offset, TimeZone, Utc};
54use std::cmp::Ordering;
55use std::sync::Arc;
56
57use crate::display::{ArrayFormatter, FormatOptions};
58use crate::parse::{
59    parse_interval_day_time, parse_interval_month_day_nano, parse_interval_year_month,
60    string_to_datetime, Parser,
61};
62use arrow_array::{builder::*, cast::*, temporal_conversions::*, timezone::Tz, types::*, *};
63use arrow_buffer::{i256, ArrowNativeType, OffsetBuffer};
64use arrow_data::transform::MutableArrayData;
65use arrow_data::ArrayData;
66use arrow_schema::*;
67use arrow_select::take::take;
68use num::cast::AsPrimitive;
69use num::{NumCast, ToPrimitive};
70
71/// CastOptions provides a way to override the default cast behaviors
72#[derive(Debug, Clone, PartialEq, Eq, Hash)]
73pub struct CastOptions<'a> {
74    /// how to handle cast failures, either return NULL (safe=true) or return ERR (safe=false)
75    pub safe: bool,
76    /// Formatting options when casting from temporal types to string
77    pub format_options: FormatOptions<'a>,
78}
79
80impl Default for CastOptions<'_> {
81    fn default() -> Self {
82        Self {
83            safe: true,
84            format_options: FormatOptions::default(),
85        }
86    }
87}
88
89/// Return true if a value of type `from_type` can be cast into a value of `to_type`.
90///
91/// See [`cast_with_options`] for more information
92pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
93    use self::DataType::*;
94    use self::IntervalUnit::*;
95    use self::TimeUnit::*;
96    if from_type == to_type {
97        return true;
98    }
99
100    match (from_type, to_type) {
101        (
102            Null,
103            Boolean
104            | Int8
105            | UInt8
106            | Int16
107            | UInt16
108            | Int32
109            | UInt32
110            | Float32
111            | Date32
112            | Time32(_)
113            | Int64
114            | UInt64
115            | Float64
116            | Date64
117            | Timestamp(_, _)
118            | Time64(_)
119            | Duration(_)
120            | Interval(_)
121            | FixedSizeBinary(_)
122            | Binary
123            | Utf8
124            | LargeBinary
125            | LargeUtf8
126            | BinaryView
127            | Utf8View
128            | List(_)
129            | LargeList(_)
130            | FixedSizeList(_, _)
131            | Struct(_)
132            | Map(_, _)
133            | Dictionary(_, _),
134        ) => true,
135        // Dictionary/List conditions should be put in front of others
136        (Dictionary(_, from_value_type), Dictionary(_, to_value_type)) => {
137            can_cast_types(from_value_type, to_value_type)
138        }
139        (Dictionary(_, value_type), _) => can_cast_types(value_type, to_type),
140        (_, Dictionary(_, value_type)) => can_cast_types(from_type, value_type),
141        (List(list_from) | LargeList(list_from), List(list_to) | LargeList(list_to)) => {
142            can_cast_types(list_from.data_type(), list_to.data_type())
143        }
144        (List(list_from) | LargeList(list_from), Utf8 | LargeUtf8) => {
145            can_cast_types(list_from.data_type(), to_type)
146        }
147        (List(list_from) | LargeList(list_from), FixedSizeList(list_to, _)) => {
148            can_cast_types(list_from.data_type(), list_to.data_type())
149        }
150        (List(_), _) => false,
151        (FixedSizeList(list_from,_), List(list_to)) |
152        (FixedSizeList(list_from,_), LargeList(list_to)) => {
153            can_cast_types(list_from.data_type(), list_to.data_type())
154        }
155        (FixedSizeList(inner, size), FixedSizeList(inner_to, size_to)) if size == size_to => {
156            can_cast_types(inner.data_type(), inner_to.data_type())
157        }
158        (_, List(list_to)) => can_cast_types(from_type, list_to.data_type()),
159        (_, LargeList(list_to)) => can_cast_types(from_type, list_to.data_type()),
160        (_, FixedSizeList(list_to,size)) if *size == 1 => {
161            can_cast_types(from_type, list_to.data_type())},
162        (FixedSizeList(list_from,size), _) if *size == 1 => {
163            can_cast_types(list_from.data_type(), to_type)},
164        (Map(from_entries,ordered_from), Map(to_entries, ordered_to)) if ordered_from == ordered_to =>
165            match (key_field(from_entries), key_field(to_entries), value_field(from_entries), value_field(to_entries)) {
166                (Some(from_key), Some(to_key), Some(from_value), Some(to_value)) =>
167                    can_cast_types(from_key.data_type(), to_key.data_type()) && can_cast_types(from_value.data_type(), to_value.data_type()),
168                _ => false
169            },
170        // cast one decimal type to another decimal type
171        (Decimal128(_, _), Decimal128(_, _)) => true,
172        (Decimal256(_, _), Decimal256(_, _)) => true,
173        (Decimal128(_, _), Decimal256(_, _)) => true,
174        (Decimal256(_, _), Decimal128(_, _)) => true,
175        // unsigned integer to decimal
176        (UInt8 | UInt16 | UInt32 | UInt64, Decimal128(_, _)) |
177        (UInt8 | UInt16 | UInt32 | UInt64, Decimal256(_, _)) |
178        // signed numeric to decimal
179        (Null | Int8 | Int16 | Int32 | Int64 | Float32 | Float64, Decimal128(_, _)) |
180        (Null | Int8 | Int16 | Int32 | Int64 | Float32 | Float64, Decimal256(_, _)) |
181        // decimal to unsigned numeric
182        (Decimal128(_, _) | Decimal256(_, _), UInt8 | UInt16 | UInt32 | UInt64) |
183        // decimal to signed numeric
184        (Decimal128(_, _) | Decimal256(_, _), Null | Int8 | Int16 | Int32 | Int64 | Float32 | Float64) => true,
185        // decimal to string
186        (Decimal128(_, _) | Decimal256(_, _), Utf8View | Utf8 | LargeUtf8) => true,
187        // string to decimal
188        (Utf8View | Utf8 | LargeUtf8, Decimal128(_, _) | Decimal256(_, _)) => true,
189        (Struct(from_fields), Struct(to_fields)) => {
190            from_fields.len() == to_fields.len() &&
191                from_fields.iter().zip(to_fields.iter()).all(|(f1, f2)| {
192                    // Assume that nullability between two structs are compatible, if not,
193                    // cast kernel will return error.
194                    can_cast_types(f1.data_type(), f2.data_type())
195                })
196        }
197        (Struct(_), _) => false,
198        (_, Struct(_)) => false,
199        (_, Boolean) => {
200            DataType::is_integer(from_type)
201                || DataType::is_floating(from_type)
202                || from_type == &Utf8View
203                || from_type == &Utf8
204                || from_type == &LargeUtf8
205        }
206        (Boolean, _) => {
207            DataType::is_integer(to_type)
208                || DataType::is_floating(to_type)
209                || to_type == &Utf8View
210                || to_type == &Utf8
211                || to_type == &LargeUtf8
212        }
213
214        (Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View ) => true,
215        (LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View ) => true,
216        (FixedSizeBinary(_), Binary | LargeBinary) => true,
217        (
218            Utf8 | LargeUtf8 | Utf8View,
219            Binary
220            | LargeBinary
221            | Utf8
222            | LargeUtf8
223            | Date32
224            | Date64
225            | Time32(Second)
226            | Time32(Millisecond)
227            | Time64(Microsecond)
228            | Time64(Nanosecond)
229            | Timestamp(Second, _)
230            | Timestamp(Millisecond, _)
231            | Timestamp(Microsecond, _)
232            | Timestamp(Nanosecond, _)
233            | Interval(_)
234            | BinaryView,
235        ) => true,
236        (Utf8 | LargeUtf8, Utf8View) => true,
237        (BinaryView, Binary | LargeBinary | Utf8 | LargeUtf8 | Utf8View) => true,
238        (Utf8View | Utf8 | LargeUtf8, _) => to_type.is_numeric() && to_type != &Float16,
239        (_, Utf8 | LargeUtf8) => from_type.is_primitive(),
240        (_, Utf8View) => from_type.is_numeric(),
241
242        (_, Binary | LargeBinary) => from_type.is_integer(),
243
244        // start numeric casts
245        (
246            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32 | Float64,
247            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32 | Float64,
248        ) => true,
249        // end numeric casts
250
251        // temporal casts
252        (Int32, Date32 | Date64 | Time32(_)) => true,
253        (Date32, Int32 | Int64) => true,
254        (Time32(_), Int32) => true,
255        (Int64, Date64 | Date32 | Time64(_)) => true,
256        (Date64, Int64 | Int32) => true,
257        (Time64(_), Int64) => true,
258        (Date32 | Date64, Date32 | Date64) => true,
259        // time casts
260        (Time32(_), Time32(_)) => true,
261        (Time32(_), Time64(_)) => true,
262        (Time64(_), Time64(_)) => true,
263        (Time64(_), Time32(to_unit)) => {
264            matches!(to_unit, Second | Millisecond)
265        }
266        (Timestamp(_, _), _) if to_type.is_numeric() => true,
267        (_, Timestamp(_, _)) if from_type.is_numeric() => true,
268        (Date64, Timestamp(_, _)) => true,
269        (Date32, Timestamp(_, _)) => true,
270        (
271            Timestamp(_, _),
272            Timestamp(_, _)
273            | Date32
274            | Date64
275            | Time32(Second)
276            | Time32(Millisecond)
277            | Time64(Microsecond)
278            | Time64(Nanosecond),
279        ) => true,
280        (_, Duration(_)) if from_type.is_numeric() => true,
281        (Duration(_), _) if to_type.is_numeric() => true,
282        (Duration(_), Duration(_)) => true,
283        (Interval(from_type), Int64) => {
284            match from_type {
285                YearMonth => true,
286                DayTime => true,
287                MonthDayNano => false, // Native type is i128
288            }
289        }
290        (Int32, Interval(to_type)) => match to_type {
291            YearMonth => true,
292            DayTime => false,
293            MonthDayNano => false,
294        },
295        (Duration(_), Interval(MonthDayNano)) => true,
296        (Interval(MonthDayNano), Duration(_)) => true,
297        (Interval(YearMonth), Interval(MonthDayNano)) => true,
298        (Interval(DayTime), Interval(MonthDayNano)) => true,
299        (_, _) => false,
300    }
301}
302
303/// Cast `array` to the provided data type and return a new Array with type `to_type`, if possible.
304///
305/// See [`cast_with_options`] for more information
306pub fn cast(array: &dyn Array, to_type: &DataType) -> Result<ArrayRef, ArrowError> {
307    cast_with_options(array, to_type, &CastOptions::default())
308}
309
310fn cast_integer_to_decimal<
311    T: ArrowPrimitiveType,
312    D: DecimalType + ArrowPrimitiveType<Native = M>,
313    M,
314>(
315    array: &PrimitiveArray<T>,
316    precision: u8,
317    scale: i8,
318    base: M,
319    cast_options: &CastOptions,
320) -> Result<ArrayRef, ArrowError>
321where
322    <T as ArrowPrimitiveType>::Native: AsPrimitive<M>,
323    M: ArrowNativeTypeOp,
324{
325    let scale_factor = base.pow_checked(scale.unsigned_abs() as u32).map_err(|_| {
326        ArrowError::CastError(format!(
327            "Cannot cast to {:?}({}, {}). The scale causes overflow.",
328            D::PREFIX,
329            precision,
330            scale,
331        ))
332    })?;
333
334    let array = if scale < 0 {
335        match cast_options.safe {
336            true => array.unary_opt::<_, D>(|v| {
337                v.as_()
338                    .div_checked(scale_factor)
339                    .ok()
340                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
341            }),
342            false => array.try_unary::<_, D, _>(|v| {
343                v.as_()
344                    .div_checked(scale_factor)
345                    .and_then(|v| D::validate_decimal_precision(v, precision).map(|_| v))
346            })?,
347        }
348    } else {
349        match cast_options.safe {
350            true => array.unary_opt::<_, D>(|v| {
351                v.as_()
352                    .mul_checked(scale_factor)
353                    .ok()
354                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
355            }),
356            false => array.try_unary::<_, D, _>(|v| {
357                v.as_()
358                    .mul_checked(scale_factor)
359                    .and_then(|v| D::validate_decimal_precision(v, precision).map(|_| v))
360            })?,
361        }
362    };
363
364    Ok(Arc::new(array.with_precision_and_scale(precision, scale)?))
365}
366
367/// Cast the array from interval year month to month day nano
368fn cast_interval_year_month_to_interval_month_day_nano(
369    array: &dyn Array,
370    _cast_options: &CastOptions,
371) -> Result<ArrayRef, ArrowError> {
372    let array = array.as_primitive::<IntervalYearMonthType>();
373
374    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
375        let months = IntervalYearMonthType::to_months(v);
376        IntervalMonthDayNanoType::make_value(months, 0, 0)
377    })))
378}
379
380/// Cast the array from interval day time to month day nano
381fn cast_interval_day_time_to_interval_month_day_nano(
382    array: &dyn Array,
383    _cast_options: &CastOptions,
384) -> Result<ArrayRef, ArrowError> {
385    let array = array.as_primitive::<IntervalDayTimeType>();
386    let mul = 1_000_000;
387
388    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
389        let (days, ms) = IntervalDayTimeType::to_parts(v);
390        IntervalMonthDayNanoType::make_value(0, days, ms as i64 * mul)
391    })))
392}
393
394/// Cast the array from interval to duration
395fn cast_month_day_nano_to_duration<D: ArrowTemporalType<Native = i64>>(
396    array: &dyn Array,
397    cast_options: &CastOptions,
398) -> Result<ArrayRef, ArrowError> {
399    let array = array.as_primitive::<IntervalMonthDayNanoType>();
400    let scale = match D::DATA_TYPE {
401        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
402        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
403        DataType::Duration(TimeUnit::Microsecond) => 1_000,
404        DataType::Duration(TimeUnit::Nanosecond) => 1,
405        _ => unreachable!(),
406    };
407
408    if cast_options.safe {
409        let iter = array.iter().map(|v| {
410            v.and_then(|v| (v.days == 0 && v.months == 0).then_some(v.nanoseconds / scale))
411        });
412        Ok(Arc::new(unsafe {
413            PrimitiveArray::<D>::from_trusted_len_iter(iter)
414        }))
415    } else {
416        let vec = array
417            .iter()
418            .map(|v| {
419                v.map(|v| match v.days == 0 && v.months == 0 {
420                    true => Ok((v.nanoseconds) / scale),
421                    _ => Err(ArrowError::ComputeError(
422                        "Cannot convert interval containing non-zero months or days to duration"
423                            .to_string(),
424                    )),
425                })
426                .transpose()
427            })
428            .collect::<Result<Vec<_>, _>>()?;
429        Ok(Arc::new(unsafe {
430            PrimitiveArray::<D>::from_trusted_len_iter(vec.iter())
431        }))
432    }
433}
434
435/// Cast the array from duration and interval
436fn cast_duration_to_interval<D: ArrowTemporalType<Native = i64>>(
437    array: &dyn Array,
438    cast_options: &CastOptions,
439) -> Result<ArrayRef, ArrowError> {
440    let array = array
441        .as_any()
442        .downcast_ref::<PrimitiveArray<D>>()
443        .ok_or_else(|| {
444            ArrowError::ComputeError(
445                "Internal Error: Cannot cast duration to DurationArray of expected type"
446                    .to_string(),
447            )
448        })?;
449
450    let scale = match array.data_type() {
451        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
452        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
453        DataType::Duration(TimeUnit::Microsecond) => 1_000,
454        DataType::Duration(TimeUnit::Nanosecond) => 1,
455        _ => unreachable!(),
456    };
457
458    if cast_options.safe {
459        let iter = array.iter().map(|v| {
460            v.and_then(|v| {
461                v.checked_mul(scale)
462                    .map(|v| IntervalMonthDayNano::new(0, 0, v))
463            })
464        });
465        Ok(Arc::new(unsafe {
466            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(iter)
467        }))
468    } else {
469        let vec = array
470            .iter()
471            .map(|v| {
472                v.map(|v| {
473                    if let Ok(v) = v.mul_checked(scale) {
474                        Ok(IntervalMonthDayNano::new(0, 0, v))
475                    } else {
476                        Err(ArrowError::ComputeError(format!(
477                            "Cannot cast to {:?}. Overflowing on {:?}",
478                            IntervalMonthDayNanoType::DATA_TYPE,
479                            v
480                        )))
481                    }
482                })
483                .transpose()
484            })
485            .collect::<Result<Vec<_>, _>>()?;
486        Ok(Arc::new(unsafe {
487            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(vec.iter())
488        }))
489    }
490}
491
492/// Cast the primitive array using [`PrimitiveArray::reinterpret_cast`]
493fn cast_reinterpret_arrays<I: ArrowPrimitiveType, O: ArrowPrimitiveType<Native = I::Native>>(
494    array: &dyn Array,
495) -> Result<ArrayRef, ArrowError> {
496    Ok(Arc::new(array.as_primitive::<I>().reinterpret_cast::<O>()))
497}
498
499fn make_timestamp_array(
500    array: &PrimitiveArray<Int64Type>,
501    unit: TimeUnit,
502    tz: Option<Arc<str>>,
503) -> ArrayRef {
504    match unit {
505        TimeUnit::Second => Arc::new(
506            array
507                .reinterpret_cast::<TimestampSecondType>()
508                .with_timezone_opt(tz),
509        ),
510        TimeUnit::Millisecond => Arc::new(
511            array
512                .reinterpret_cast::<TimestampMillisecondType>()
513                .with_timezone_opt(tz),
514        ),
515        TimeUnit::Microsecond => Arc::new(
516            array
517                .reinterpret_cast::<TimestampMicrosecondType>()
518                .with_timezone_opt(tz),
519        ),
520        TimeUnit::Nanosecond => Arc::new(
521            array
522                .reinterpret_cast::<TimestampNanosecondType>()
523                .with_timezone_opt(tz),
524        ),
525    }
526}
527
528fn make_duration_array(array: &PrimitiveArray<Int64Type>, unit: TimeUnit) -> ArrayRef {
529    match unit {
530        TimeUnit::Second => Arc::new(array.reinterpret_cast::<DurationSecondType>()),
531        TimeUnit::Millisecond => Arc::new(array.reinterpret_cast::<DurationMillisecondType>()),
532        TimeUnit::Microsecond => Arc::new(array.reinterpret_cast::<DurationMicrosecondType>()),
533        TimeUnit::Nanosecond => Arc::new(array.reinterpret_cast::<DurationNanosecondType>()),
534    }
535}
536
537fn as_time_res_with_timezone<T: ArrowPrimitiveType>(
538    v: i64,
539    tz: Option<Tz>,
540) -> Result<NaiveTime, ArrowError> {
541    let time = match tz {
542        Some(tz) => as_datetime_with_timezone::<T>(v, tz).map(|d| d.time()),
543        None => as_datetime::<T>(v).map(|d| d.time()),
544    };
545
546    time.ok_or_else(|| {
547        ArrowError::CastError(format!(
548            "Failed to create naive time with {} {}",
549            std::any::type_name::<T>(),
550            v
551        ))
552    })
553}
554
555fn timestamp_to_date32<T: ArrowTimestampType>(
556    array: &PrimitiveArray<T>,
557) -> Result<ArrayRef, ArrowError> {
558    let err = |x: i64| {
559        ArrowError::CastError(format!(
560            "Cannot convert {} {x} to datetime",
561            std::any::type_name::<T>()
562        ))
563    };
564
565    let array: Date32Array = match array.timezone() {
566        Some(tz) => {
567            let tz: Tz = tz.parse()?;
568            array.try_unary(|x| {
569                as_datetime_with_timezone::<T>(x, tz)
570                    .ok_or_else(|| err(x))
571                    .map(|d| Date32Type::from_naive_date(d.date_naive()))
572            })?
573        }
574        None => array.try_unary(|x| {
575            as_datetime::<T>(x)
576                .ok_or_else(|| err(x))
577                .map(|d| Date32Type::from_naive_date(d.date()))
578        })?,
579    };
580    Ok(Arc::new(array))
581}
582
583/// Try to cast `array` to `to_type` if possible.
584///
585/// Returns a new Array with type `to_type` if possible.
586///
587/// Accepts [`CastOptions`] to specify cast behavior. See also [`cast()`].
588///
589/// # Behavior
590/// * `Boolean` to `Utf8`: `true` => '1', `false` => `0`
591/// * `Utf8` to `Boolean`: `true`, `yes`, `on`, `1` => `true`, `false`, `no`, `off`, `0` => `false`,
592///   short variants are accepted, other strings return null or error
593/// * `Utf8` to Numeric: strings that can't be parsed to numbers return null, float strings
594///   in integer casts return null
595/// * Numeric to `Boolean`: 0 returns `false`, any other value returns `true`
596/// * `List` to `List`: the underlying data type is cast
597/// * `List` to `FixedSizeList`: the underlying data type is cast. If safe is true and a list element
598///   has the wrong length it will be replaced with NULL, otherwise an error will be returned
599/// * Primitive to `List`: a list array with 1 value per slot is created
600/// * `Date32` and `Date64`: precision lost when going to higher interval
601/// * `Time32 and `Time64`: precision lost when going to higher interval
602/// * `Timestamp` and `Date{32|64}`: precision lost when going to higher interval
603/// * Temporal to/from backing Primitive: zero-copy with data type change
604/// * `Float32/Float64` to `Decimal(precision, scale)` rounds to the `scale` decimals
605///   (i.e. casting `6.4999` to `Decimal(10, 1)` becomes `6.5`).
606///
607/// Unsupported Casts (check with `can_cast_types` before calling):
608/// * To or from `StructArray`
609/// * `List` to `Primitive`
610/// * `Interval` and `Duration`
611///
612/// # Timestamps and Timezones
613///
614/// Timestamps are stored with an optional timezone in Arrow.
615///
616/// ## Casting timestamps to a timestamp without timezone / UTC
617/// ```
618/// # use arrow_array::Int64Array;
619/// # use arrow_array::types::TimestampSecondType;
620/// # use arrow_cast::{cast, display};
621/// # use arrow_array::cast::AsArray;
622/// # use arrow_schema::{DataType, TimeUnit};
623/// // can use "UTC" if chrono-tz feature is enabled, here use offset based timezone
624/// let data_type = DataType::Timestamp(TimeUnit::Second, None);
625/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
626/// let b = cast(&a, &data_type).unwrap();
627/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
628/// assert_eq!(2_000_000_000, b.value(1)); // values are the same as the type has no timezone
629/// // use display to show them (note has no trailing Z)
630/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
631/// ```
632///
633/// ## Casting timestamps to a timestamp with timezone
634///
635/// Similarly to the previous example, if you cast numeric values to a timestamp
636/// with timezone, the cast kernel will not change the underlying values
637/// but display and other functions will interpret them as being in the provided timezone.
638///
639/// ```
640/// # use arrow_array::Int64Array;
641/// # use arrow_array::types::TimestampSecondType;
642/// # use arrow_cast::{cast, display};
643/// # use arrow_array::cast::AsArray;
644/// # use arrow_schema::{DataType, TimeUnit};
645/// // can use "Americas/New_York" if chrono-tz feature is enabled, here use offset based timezone
646/// let data_type = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
647/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
648/// let b = cast(&a, &data_type).unwrap();
649/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
650/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
651/// // displayed in the target timezone (note the offset -05:00)
652/// assert_eq!("2033-05-17T22:33:20-05:00", display::array_value_to_string(&b, 1).unwrap());
653/// ```
654/// # Casting timestamps without timezone to timestamps with timezone
655///
656/// When casting from a timestamp without timezone to a timestamp with
657/// timezone, the cast kernel interprets the timestamp values as being in
658/// the destination timezone and then adjusts the underlying value to UTC as required
659///
660/// However, note that when casting from a timestamp with timezone BACK to a
661/// timestamp without timezone the cast kernel does not adjust the values.
662///
663/// Thus round trip casting a timestamp without timezone to a timestamp with
664/// timezone and back to a timestamp without timezone results in different
665/// values than the starting values.
666///
667/// ```
668/// # use arrow_array::Int64Array;
669/// # use arrow_array::types::{TimestampSecondType};
670/// # use arrow_cast::{cast, display};
671/// # use arrow_array::cast::AsArray;
672/// # use arrow_schema::{DataType, TimeUnit};
673/// let data_type  = DataType::Timestamp(TimeUnit::Second, None);
674/// let data_type_tz = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
675/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
676/// let b = cast(&a, &data_type).unwrap(); // cast to timestamp without timezone
677/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
678/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
679/// // displayed without a timezone (note lack of offset or Z)
680/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
681///
682/// // Convert timestamps without a timezone to timestamps with a timezone
683/// let c = cast(&b, &data_type_tz).unwrap();
684/// let c = c.as_primitive::<TimestampSecondType>(); // downcast to result type
685/// assert_eq!(2_000_018_000, c.value(1)); // value has been adjusted by offset
686/// // displayed with the target timezone offset (-05:00)
687/// assert_eq!("2033-05-18T03:33:20-05:00", display::array_value_to_string(&c, 1).unwrap());
688///
689/// // Convert from timestamp with timezone back to timestamp without timezone
690/// let d = cast(&c, &data_type).unwrap();
691/// let d = d.as_primitive::<TimestampSecondType>(); // downcast to result type
692/// assert_eq!(2_000_018_000, d.value(1)); // value has not been adjusted
693/// // NOTE: the timestamp is adjusted (08:33:20 instead of 03:33:20 as in previous example)
694/// assert_eq!("2033-05-18T08:33:20", display::array_value_to_string(&d, 1).unwrap());
695/// ```
696pub fn cast_with_options(
697    array: &dyn Array,
698    to_type: &DataType,
699    cast_options: &CastOptions,
700) -> Result<ArrayRef, ArrowError> {
701    use DataType::*;
702    let from_type = array.data_type();
703    // clone array if types are the same
704    if from_type == to_type {
705        return Ok(make_array(array.to_data()));
706    }
707    match (from_type, to_type) {
708        (
709            Null,
710            Boolean
711            | Int8
712            | UInt8
713            | Int16
714            | UInt16
715            | Int32
716            | UInt32
717            | Float32
718            | Date32
719            | Time32(_)
720            | Int64
721            | UInt64
722            | Float64
723            | Date64
724            | Timestamp(_, _)
725            | Time64(_)
726            | Duration(_)
727            | Interval(_)
728            | FixedSizeBinary(_)
729            | Binary
730            | Utf8
731            | LargeBinary
732            | LargeUtf8
733            | BinaryView
734            | Utf8View
735            | List(_)
736            | LargeList(_)
737            | FixedSizeList(_, _)
738            | Struct(_)
739            | Map(_, _)
740            | Dictionary(_, _),
741        ) => Ok(new_null_array(to_type, array.len())),
742        (Dictionary(index_type, _), _) => match **index_type {
743            Int8 => dictionary_cast::<Int8Type>(array, to_type, cast_options),
744            Int16 => dictionary_cast::<Int16Type>(array, to_type, cast_options),
745            Int32 => dictionary_cast::<Int32Type>(array, to_type, cast_options),
746            Int64 => dictionary_cast::<Int64Type>(array, to_type, cast_options),
747            UInt8 => dictionary_cast::<UInt8Type>(array, to_type, cast_options),
748            UInt16 => dictionary_cast::<UInt16Type>(array, to_type, cast_options),
749            UInt32 => dictionary_cast::<UInt32Type>(array, to_type, cast_options),
750            UInt64 => dictionary_cast::<UInt64Type>(array, to_type, cast_options),
751            _ => Err(ArrowError::CastError(format!(
752                "Casting from dictionary type {from_type:?} to {to_type:?} not supported",
753            ))),
754        },
755        (_, Dictionary(index_type, value_type)) => match **index_type {
756            Int8 => cast_to_dictionary::<Int8Type>(array, value_type, cast_options),
757            Int16 => cast_to_dictionary::<Int16Type>(array, value_type, cast_options),
758            Int32 => cast_to_dictionary::<Int32Type>(array, value_type, cast_options),
759            Int64 => cast_to_dictionary::<Int64Type>(array, value_type, cast_options),
760            UInt8 => cast_to_dictionary::<UInt8Type>(array, value_type, cast_options),
761            UInt16 => cast_to_dictionary::<UInt16Type>(array, value_type, cast_options),
762            UInt32 => cast_to_dictionary::<UInt32Type>(array, value_type, cast_options),
763            UInt64 => cast_to_dictionary::<UInt64Type>(array, value_type, cast_options),
764            _ => Err(ArrowError::CastError(format!(
765                "Casting from type {from_type:?} to dictionary type {to_type:?} not supported",
766            ))),
767        },
768        (List(_), List(to)) => cast_list_values::<i32>(array, to, cast_options),
769        (LargeList(_), LargeList(to)) => cast_list_values::<i64>(array, to, cast_options),
770        (List(_), LargeList(list_to)) => cast_list::<i32, i64>(array, list_to, cast_options),
771        (LargeList(_), List(list_to)) => cast_list::<i64, i32>(array, list_to, cast_options),
772        (List(_), FixedSizeList(field, size)) => {
773            let array = array.as_list::<i32>();
774            cast_list_to_fixed_size_list::<i32>(array, field, *size, cast_options)
775        }
776        (LargeList(_), FixedSizeList(field, size)) => {
777            let array = array.as_list::<i64>();
778            cast_list_to_fixed_size_list::<i64>(array, field, *size, cast_options)
779        }
780        (List(_) | LargeList(_), _) => match to_type {
781            Utf8 => value_to_string::<i32>(array, cast_options),
782            LargeUtf8 => value_to_string::<i64>(array, cast_options),
783            _ => Err(ArrowError::CastError(
784                "Cannot cast list to non-list data types".to_string(),
785            )),
786        },
787        (FixedSizeList(list_from, size), List(list_to)) => {
788            if list_to.data_type() != list_from.data_type() {
789                // To transform inner type, can first cast to FSL with new inner type.
790                let fsl_to = DataType::FixedSizeList(list_to.clone(), *size);
791                let array = cast_with_options(array, &fsl_to, cast_options)?;
792                cast_fixed_size_list_to_list::<i32>(array.as_ref())
793            } else {
794                cast_fixed_size_list_to_list::<i32>(array)
795            }
796        }
797        (FixedSizeList(list_from, size), LargeList(list_to)) => {
798            if list_to.data_type() != list_from.data_type() {
799                // To transform inner type, can first cast to FSL with new inner type.
800                let fsl_to = DataType::FixedSizeList(list_to.clone(), *size);
801                let array = cast_with_options(array, &fsl_to, cast_options)?;
802                cast_fixed_size_list_to_list::<i64>(array.as_ref())
803            } else {
804                cast_fixed_size_list_to_list::<i64>(array)
805            }
806        }
807        (FixedSizeList(_, size_from), FixedSizeList(list_to, size_to)) => {
808            if size_from != size_to {
809                return Err(ArrowError::CastError(
810                    "cannot cast fixed-size-list to fixed-size-list with different size".into(),
811                ));
812            }
813            let array = array.as_any().downcast_ref::<FixedSizeListArray>().unwrap();
814            let values = cast_with_options(array.values(), list_to.data_type(), cast_options)?;
815            Ok(Arc::new(FixedSizeListArray::try_new(
816                list_to.clone(),
817                *size_from,
818                values,
819                array.nulls().cloned(),
820            )?))
821        }
822        (_, List(ref to)) => cast_values_to_list::<i32>(array, to, cast_options),
823        (_, LargeList(ref to)) => cast_values_to_list::<i64>(array, to, cast_options),
824        (_, FixedSizeList(ref to, size)) if *size == 1 => {
825            cast_values_to_fixed_size_list(array, to, *size, cast_options)
826        }
827        (FixedSizeList(_, size), _) if *size == 1 => {
828            cast_single_element_fixed_size_list_to_values(array, to_type, cast_options)
829        }
830        (Map(_, ordered1), Map(_, ordered2)) if ordered1 == ordered2 => {
831            cast_map_values(array.as_map(), to_type, cast_options, ordered1.to_owned())
832        }
833        // Decimal to decimal, same width
834        (Decimal128(p1, s1), Decimal128(p2, s2)) => {
835            cast_decimal_to_decimal_same_type::<Decimal128Type>(
836                array.as_primitive(),
837                *p1,
838                *s1,
839                *p2,
840                *s2,
841                cast_options,
842            )
843        }
844        (Decimal256(p1, s1), Decimal256(p2, s2)) => {
845            cast_decimal_to_decimal_same_type::<Decimal256Type>(
846                array.as_primitive(),
847                *p1,
848                *s1,
849                *p2,
850                *s2,
851                cast_options,
852            )
853        }
854        // Decimal to decimal, different width
855        (Decimal128(p1, s1), Decimal256(p2, s2)) => {
856            cast_decimal_to_decimal::<Decimal128Type, Decimal256Type>(
857                array.as_primitive(),
858                *p1,
859                *s1,
860                *p2,
861                *s2,
862                cast_options,
863            )
864        }
865        (Decimal256(p1, s1), Decimal128(p2, s2)) => {
866            cast_decimal_to_decimal::<Decimal256Type, Decimal128Type>(
867                array.as_primitive(),
868                *p1,
869                *s1,
870                *p2,
871                *s2,
872                cast_options,
873            )
874        }
875        // Decimal to non-decimal
876        (Decimal128(_, scale), _) if !to_type.is_temporal() => {
877            cast_from_decimal::<Decimal128Type, _>(
878                array,
879                10_i128,
880                scale,
881                from_type,
882                to_type,
883                |x: i128| x as f64,
884                cast_options,
885            )
886        }
887        (Decimal256(_, scale), _) if !to_type.is_temporal() => {
888            cast_from_decimal::<Decimal256Type, _>(
889                array,
890                i256::from_i128(10_i128),
891                scale,
892                from_type,
893                to_type,
894                |x: i256| x.to_f64().unwrap(),
895                cast_options,
896            )
897        }
898        // Non-decimal to decimal
899        (_, Decimal128(precision, scale)) if !from_type.is_temporal() => {
900            cast_to_decimal::<Decimal128Type, _>(
901                array,
902                10_i128,
903                precision,
904                scale,
905                from_type,
906                to_type,
907                cast_options,
908            )
909        }
910        (_, Decimal256(precision, scale)) if !from_type.is_temporal() => {
911            cast_to_decimal::<Decimal256Type, _>(
912                array,
913                i256::from_i128(10_i128),
914                precision,
915                scale,
916                from_type,
917                to_type,
918                cast_options,
919            )
920        }
921        (Struct(_), Struct(to_fields)) => {
922            let array = array.as_struct();
923            let fields = array
924                .columns()
925                .iter()
926                .zip(to_fields.iter())
927                .map(|(l, field)| cast_with_options(l, field.data_type(), cast_options))
928                .collect::<Result<Vec<ArrayRef>, ArrowError>>()?;
929            let array = StructArray::try_new(to_fields.clone(), fields, array.nulls().cloned())?;
930            Ok(Arc::new(array) as ArrayRef)
931        }
932        (Struct(_), _) => Err(ArrowError::CastError(format!(
933            "Casting from {from_type:?} to {to_type:?} not supported"
934        ))),
935        (_, Struct(_)) => Err(ArrowError::CastError(format!(
936            "Casting from {from_type:?} to {to_type:?} not supported"
937        ))),
938        (_, Boolean) => match from_type {
939            UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
940            UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
941            UInt32 => cast_numeric_to_bool::<UInt32Type>(array),
942            UInt64 => cast_numeric_to_bool::<UInt64Type>(array),
943            Int8 => cast_numeric_to_bool::<Int8Type>(array),
944            Int16 => cast_numeric_to_bool::<Int16Type>(array),
945            Int32 => cast_numeric_to_bool::<Int32Type>(array),
946            Int64 => cast_numeric_to_bool::<Int64Type>(array),
947            Float16 => cast_numeric_to_bool::<Float16Type>(array),
948            Float32 => cast_numeric_to_bool::<Float32Type>(array),
949            Float64 => cast_numeric_to_bool::<Float64Type>(array),
950            Utf8View => cast_utf8view_to_boolean(array, cast_options),
951            Utf8 => cast_utf8_to_boolean::<i32>(array, cast_options),
952            LargeUtf8 => cast_utf8_to_boolean::<i64>(array, cast_options),
953            _ => Err(ArrowError::CastError(format!(
954                "Casting from {from_type:?} to {to_type:?} not supported",
955            ))),
956        },
957        (Boolean, _) => match to_type {
958            UInt8 => cast_bool_to_numeric::<UInt8Type>(array, cast_options),
959            UInt16 => cast_bool_to_numeric::<UInt16Type>(array, cast_options),
960            UInt32 => cast_bool_to_numeric::<UInt32Type>(array, cast_options),
961            UInt64 => cast_bool_to_numeric::<UInt64Type>(array, cast_options),
962            Int8 => cast_bool_to_numeric::<Int8Type>(array, cast_options),
963            Int16 => cast_bool_to_numeric::<Int16Type>(array, cast_options),
964            Int32 => cast_bool_to_numeric::<Int32Type>(array, cast_options),
965            Int64 => cast_bool_to_numeric::<Int64Type>(array, cast_options),
966            Float16 => cast_bool_to_numeric::<Float16Type>(array, cast_options),
967            Float32 => cast_bool_to_numeric::<Float32Type>(array, cast_options),
968            Float64 => cast_bool_to_numeric::<Float64Type>(array, cast_options),
969            Utf8View => value_to_string_view(array, cast_options),
970            Utf8 => value_to_string::<i32>(array, cast_options),
971            LargeUtf8 => value_to_string::<i64>(array, cast_options),
972            _ => Err(ArrowError::CastError(format!(
973                "Casting from {from_type:?} to {to_type:?} not supported",
974            ))),
975        },
976        (Utf8, _) => match to_type {
977            UInt8 => parse_string::<UInt8Type, i32>(array, cast_options),
978            UInt16 => parse_string::<UInt16Type, i32>(array, cast_options),
979            UInt32 => parse_string::<UInt32Type, i32>(array, cast_options),
980            UInt64 => parse_string::<UInt64Type, i32>(array, cast_options),
981            Int8 => parse_string::<Int8Type, i32>(array, cast_options),
982            Int16 => parse_string::<Int16Type, i32>(array, cast_options),
983            Int32 => parse_string::<Int32Type, i32>(array, cast_options),
984            Int64 => parse_string::<Int64Type, i32>(array, cast_options),
985            Float32 => parse_string::<Float32Type, i32>(array, cast_options),
986            Float64 => parse_string::<Float64Type, i32>(array, cast_options),
987            Date32 => parse_string::<Date32Type, i32>(array, cast_options),
988            Date64 => parse_string::<Date64Type, i32>(array, cast_options),
989            Binary => Ok(Arc::new(BinaryArray::from(
990                array.as_string::<i32>().clone(),
991            ))),
992            LargeBinary => {
993                let binary = BinaryArray::from(array.as_string::<i32>().clone());
994                cast_byte_container::<BinaryType, LargeBinaryType>(&binary)
995            }
996            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i32>()))),
997            BinaryView => Ok(Arc::new(
998                StringViewArray::from(array.as_string::<i32>()).to_binary_view(),
999            )),
1000            LargeUtf8 => cast_byte_container::<Utf8Type, LargeUtf8Type>(array),
1001            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i32>(array, cast_options),
1002            Time32(TimeUnit::Millisecond) => {
1003                parse_string::<Time32MillisecondType, i32>(array, cast_options)
1004            }
1005            Time64(TimeUnit::Microsecond) => {
1006                parse_string::<Time64MicrosecondType, i32>(array, cast_options)
1007            }
1008            Time64(TimeUnit::Nanosecond) => {
1009                parse_string::<Time64NanosecondType, i32>(array, cast_options)
1010            }
1011            Timestamp(TimeUnit::Second, to_tz) => {
1012                cast_string_to_timestamp::<i32, TimestampSecondType>(array, to_tz, cast_options)
1013            }
1014            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1015                i32,
1016                TimestampMillisecondType,
1017            >(array, to_tz, cast_options),
1018            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1019                i32,
1020                TimestampMicrosecondType,
1021            >(array, to_tz, cast_options),
1022            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1023                cast_string_to_timestamp::<i32, TimestampNanosecondType>(array, to_tz, cast_options)
1024            }
1025            Interval(IntervalUnit::YearMonth) => {
1026                cast_string_to_year_month_interval::<i32>(array, cast_options)
1027            }
1028            Interval(IntervalUnit::DayTime) => {
1029                cast_string_to_day_time_interval::<i32>(array, cast_options)
1030            }
1031            Interval(IntervalUnit::MonthDayNano) => {
1032                cast_string_to_month_day_nano_interval::<i32>(array, cast_options)
1033            }
1034            _ => Err(ArrowError::CastError(format!(
1035                "Casting from {from_type:?} to {to_type:?} not supported",
1036            ))),
1037        },
1038        (Utf8View, _) => match to_type {
1039            UInt8 => parse_string_view::<UInt8Type>(array, cast_options),
1040            UInt16 => parse_string_view::<UInt16Type>(array, cast_options),
1041            UInt32 => parse_string_view::<UInt32Type>(array, cast_options),
1042            UInt64 => parse_string_view::<UInt64Type>(array, cast_options),
1043            Int8 => parse_string_view::<Int8Type>(array, cast_options),
1044            Int16 => parse_string_view::<Int16Type>(array, cast_options),
1045            Int32 => parse_string_view::<Int32Type>(array, cast_options),
1046            Int64 => parse_string_view::<Int64Type>(array, cast_options),
1047            Float32 => parse_string_view::<Float32Type>(array, cast_options),
1048            Float64 => parse_string_view::<Float64Type>(array, cast_options),
1049            Date32 => parse_string_view::<Date32Type>(array, cast_options),
1050            Date64 => parse_string_view::<Date64Type>(array, cast_options),
1051            Binary => cast_view_to_byte::<StringViewType, GenericBinaryType<i32>>(array),
1052            LargeBinary => cast_view_to_byte::<StringViewType, GenericBinaryType<i64>>(array),
1053            BinaryView => Ok(Arc::new(array.as_string_view().clone().to_binary_view())),
1054            Utf8 => cast_view_to_byte::<StringViewType, GenericStringType<i32>>(array),
1055            LargeUtf8 => cast_view_to_byte::<StringViewType, GenericStringType<i64>>(array),
1056            Time32(TimeUnit::Second) => parse_string_view::<Time32SecondType>(array, cast_options),
1057            Time32(TimeUnit::Millisecond) => {
1058                parse_string_view::<Time32MillisecondType>(array, cast_options)
1059            }
1060            Time64(TimeUnit::Microsecond) => {
1061                parse_string_view::<Time64MicrosecondType>(array, cast_options)
1062            }
1063            Time64(TimeUnit::Nanosecond) => {
1064                parse_string_view::<Time64NanosecondType>(array, cast_options)
1065            }
1066            Timestamp(TimeUnit::Second, to_tz) => {
1067                cast_view_to_timestamp::<TimestampSecondType>(array, to_tz, cast_options)
1068            }
1069            Timestamp(TimeUnit::Millisecond, to_tz) => {
1070                cast_view_to_timestamp::<TimestampMillisecondType>(array, to_tz, cast_options)
1071            }
1072            Timestamp(TimeUnit::Microsecond, to_tz) => {
1073                cast_view_to_timestamp::<TimestampMicrosecondType>(array, to_tz, cast_options)
1074            }
1075            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1076                cast_view_to_timestamp::<TimestampNanosecondType>(array, to_tz, cast_options)
1077            }
1078            Interval(IntervalUnit::YearMonth) => {
1079                cast_view_to_year_month_interval(array, cast_options)
1080            }
1081            Interval(IntervalUnit::DayTime) => cast_view_to_day_time_interval(array, cast_options),
1082            Interval(IntervalUnit::MonthDayNano) => {
1083                cast_view_to_month_day_nano_interval(array, cast_options)
1084            }
1085            _ => Err(ArrowError::CastError(format!(
1086                "Casting from {from_type:?} to {to_type:?} not supported",
1087            ))),
1088        },
1089        (LargeUtf8, _) => match to_type {
1090            UInt8 => parse_string::<UInt8Type, i64>(array, cast_options),
1091            UInt16 => parse_string::<UInt16Type, i64>(array, cast_options),
1092            UInt32 => parse_string::<UInt32Type, i64>(array, cast_options),
1093            UInt64 => parse_string::<UInt64Type, i64>(array, cast_options),
1094            Int8 => parse_string::<Int8Type, i64>(array, cast_options),
1095            Int16 => parse_string::<Int16Type, i64>(array, cast_options),
1096            Int32 => parse_string::<Int32Type, i64>(array, cast_options),
1097            Int64 => parse_string::<Int64Type, i64>(array, cast_options),
1098            Float32 => parse_string::<Float32Type, i64>(array, cast_options),
1099            Float64 => parse_string::<Float64Type, i64>(array, cast_options),
1100            Date32 => parse_string::<Date32Type, i64>(array, cast_options),
1101            Date64 => parse_string::<Date64Type, i64>(array, cast_options),
1102            Utf8 => cast_byte_container::<LargeUtf8Type, Utf8Type>(array),
1103            Binary => {
1104                let large_binary = LargeBinaryArray::from(array.as_string::<i64>().clone());
1105                cast_byte_container::<LargeBinaryType, BinaryType>(&large_binary)
1106            }
1107            LargeBinary => Ok(Arc::new(LargeBinaryArray::from(
1108                array.as_string::<i64>().clone(),
1109            ))),
1110            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i64>()))),
1111            BinaryView => Ok(Arc::new(BinaryViewArray::from(
1112                array
1113                    .as_string::<i64>()
1114                    .into_iter()
1115                    .map(|x| x.map(|x| x.as_bytes()))
1116                    .collect::<Vec<_>>(),
1117            ))),
1118            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i64>(array, cast_options),
1119            Time32(TimeUnit::Millisecond) => {
1120                parse_string::<Time32MillisecondType, i64>(array, cast_options)
1121            }
1122            Time64(TimeUnit::Microsecond) => {
1123                parse_string::<Time64MicrosecondType, i64>(array, cast_options)
1124            }
1125            Time64(TimeUnit::Nanosecond) => {
1126                parse_string::<Time64NanosecondType, i64>(array, cast_options)
1127            }
1128            Timestamp(TimeUnit::Second, to_tz) => {
1129                cast_string_to_timestamp::<i64, TimestampSecondType>(array, to_tz, cast_options)
1130            }
1131            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1132                i64,
1133                TimestampMillisecondType,
1134            >(array, to_tz, cast_options),
1135            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1136                i64,
1137                TimestampMicrosecondType,
1138            >(array, to_tz, cast_options),
1139            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1140                cast_string_to_timestamp::<i64, TimestampNanosecondType>(array, to_tz, cast_options)
1141            }
1142            Interval(IntervalUnit::YearMonth) => {
1143                cast_string_to_year_month_interval::<i64>(array, cast_options)
1144            }
1145            Interval(IntervalUnit::DayTime) => {
1146                cast_string_to_day_time_interval::<i64>(array, cast_options)
1147            }
1148            Interval(IntervalUnit::MonthDayNano) => {
1149                cast_string_to_month_day_nano_interval::<i64>(array, cast_options)
1150            }
1151            _ => Err(ArrowError::CastError(format!(
1152                "Casting from {from_type:?} to {to_type:?} not supported",
1153            ))),
1154        },
1155        (Binary, _) => match to_type {
1156            Utf8 => cast_binary_to_string::<i32>(array, cast_options),
1157            LargeUtf8 => {
1158                let array = cast_binary_to_string::<i32>(array, cast_options)?;
1159                cast_byte_container::<Utf8Type, LargeUtf8Type>(array.as_ref())
1160            }
1161            LargeBinary => cast_byte_container::<BinaryType, LargeBinaryType>(array),
1162            FixedSizeBinary(size) => {
1163                cast_binary_to_fixed_size_binary::<i32>(array, *size, cast_options)
1164            }
1165            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i32>()))),
1166            Utf8View => Ok(Arc::new(StringViewArray::from(
1167                cast_binary_to_string::<i32>(array, cast_options)?.as_string::<i32>(),
1168            ))),
1169            _ => Err(ArrowError::CastError(format!(
1170                "Casting from {from_type:?} to {to_type:?} not supported",
1171            ))),
1172        },
1173        (LargeBinary, _) => match to_type {
1174            Utf8 => {
1175                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1176                cast_byte_container::<LargeUtf8Type, Utf8Type>(array.as_ref())
1177            }
1178            LargeUtf8 => cast_binary_to_string::<i64>(array, cast_options),
1179            Binary => cast_byte_container::<LargeBinaryType, BinaryType>(array),
1180            FixedSizeBinary(size) => {
1181                cast_binary_to_fixed_size_binary::<i64>(array, *size, cast_options)
1182            }
1183            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i64>()))),
1184            Utf8View => {
1185                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1186                Ok(Arc::new(StringViewArray::from(array.as_string::<i64>())))
1187            }
1188            _ => Err(ArrowError::CastError(format!(
1189                "Casting from {from_type:?} to {to_type:?} not supported",
1190            ))),
1191        },
1192        (FixedSizeBinary(size), _) => match to_type {
1193            Binary => cast_fixed_size_binary_to_binary::<i32>(array, *size),
1194            LargeBinary => cast_fixed_size_binary_to_binary::<i64>(array, *size),
1195            _ => Err(ArrowError::CastError(format!(
1196                "Casting from {from_type:?} to {to_type:?} not supported",
1197            ))),
1198        },
1199        (BinaryView, Binary) => cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array),
1200        (BinaryView, LargeBinary) => {
1201            cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)
1202        }
1203        (BinaryView, Utf8) => {
1204            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array)?;
1205            cast_binary_to_string::<i32>(&binary_arr, cast_options)
1206        }
1207        (BinaryView, LargeUtf8) => {
1208            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)?;
1209            cast_binary_to_string::<i64>(&binary_arr, cast_options)
1210        }
1211        (BinaryView, Utf8View) => {
1212            Ok(Arc::new(array.as_binary_view().clone().to_string_view()?) as ArrayRef)
1213        }
1214        (BinaryView, _) => Err(ArrowError::CastError(format!(
1215            "Casting from {from_type:?} to {to_type:?} not supported",
1216        ))),
1217        (from_type, Utf8View) if from_type.is_primitive() => {
1218            value_to_string_view(array, cast_options)
1219        }
1220        (from_type, LargeUtf8) if from_type.is_primitive() => {
1221            value_to_string::<i64>(array, cast_options)
1222        }
1223        (from_type, Utf8) if from_type.is_primitive() => {
1224            value_to_string::<i32>(array, cast_options)
1225        }
1226        (from_type, Binary) if from_type.is_integer() => match from_type {
1227            UInt8 => cast_numeric_to_binary::<UInt8Type, i32>(array),
1228            UInt16 => cast_numeric_to_binary::<UInt16Type, i32>(array),
1229            UInt32 => cast_numeric_to_binary::<UInt32Type, i32>(array),
1230            UInt64 => cast_numeric_to_binary::<UInt64Type, i32>(array),
1231            Int8 => cast_numeric_to_binary::<Int8Type, i32>(array),
1232            Int16 => cast_numeric_to_binary::<Int16Type, i32>(array),
1233            Int32 => cast_numeric_to_binary::<Int32Type, i32>(array),
1234            Int64 => cast_numeric_to_binary::<Int64Type, i32>(array),
1235            _ => unreachable!(),
1236        },
1237        (from_type, LargeBinary) if from_type.is_integer() => match from_type {
1238            UInt8 => cast_numeric_to_binary::<UInt8Type, i64>(array),
1239            UInt16 => cast_numeric_to_binary::<UInt16Type, i64>(array),
1240            UInt32 => cast_numeric_to_binary::<UInt32Type, i64>(array),
1241            UInt64 => cast_numeric_to_binary::<UInt64Type, i64>(array),
1242            Int8 => cast_numeric_to_binary::<Int8Type, i64>(array),
1243            Int16 => cast_numeric_to_binary::<Int16Type, i64>(array),
1244            Int32 => cast_numeric_to_binary::<Int32Type, i64>(array),
1245            Int64 => cast_numeric_to_binary::<Int64Type, i64>(array),
1246            _ => unreachable!(),
1247        },
1248        // start numeric casts
1249        (UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array, cast_options),
1250        (UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array, cast_options),
1251        (UInt8, UInt64) => cast_numeric_arrays::<UInt8Type, UInt64Type>(array, cast_options),
1252        (UInt8, Int8) => cast_numeric_arrays::<UInt8Type, Int8Type>(array, cast_options),
1253        (UInt8, Int16) => cast_numeric_arrays::<UInt8Type, Int16Type>(array, cast_options),
1254        (UInt8, Int32) => cast_numeric_arrays::<UInt8Type, Int32Type>(array, cast_options),
1255        (UInt8, Int64) => cast_numeric_arrays::<UInt8Type, Int64Type>(array, cast_options),
1256        (UInt8, Float16) => cast_numeric_arrays::<UInt8Type, Float16Type>(array, cast_options),
1257        (UInt8, Float32) => cast_numeric_arrays::<UInt8Type, Float32Type>(array, cast_options),
1258        (UInt8, Float64) => cast_numeric_arrays::<UInt8Type, Float64Type>(array, cast_options),
1259
1260        (UInt16, UInt8) => cast_numeric_arrays::<UInt16Type, UInt8Type>(array, cast_options),
1261        (UInt16, UInt32) => cast_numeric_arrays::<UInt16Type, UInt32Type>(array, cast_options),
1262        (UInt16, UInt64) => cast_numeric_arrays::<UInt16Type, UInt64Type>(array, cast_options),
1263        (UInt16, Int8) => cast_numeric_arrays::<UInt16Type, Int8Type>(array, cast_options),
1264        (UInt16, Int16) => cast_numeric_arrays::<UInt16Type, Int16Type>(array, cast_options),
1265        (UInt16, Int32) => cast_numeric_arrays::<UInt16Type, Int32Type>(array, cast_options),
1266        (UInt16, Int64) => cast_numeric_arrays::<UInt16Type, Int64Type>(array, cast_options),
1267        (UInt16, Float16) => cast_numeric_arrays::<UInt16Type, Float16Type>(array, cast_options),
1268        (UInt16, Float32) => cast_numeric_arrays::<UInt16Type, Float32Type>(array, cast_options),
1269        (UInt16, Float64) => cast_numeric_arrays::<UInt16Type, Float64Type>(array, cast_options),
1270
1271        (UInt32, UInt8) => cast_numeric_arrays::<UInt32Type, UInt8Type>(array, cast_options),
1272        (UInt32, UInt16) => cast_numeric_arrays::<UInt32Type, UInt16Type>(array, cast_options),
1273        (UInt32, UInt64) => cast_numeric_arrays::<UInt32Type, UInt64Type>(array, cast_options),
1274        (UInt32, Int8) => cast_numeric_arrays::<UInt32Type, Int8Type>(array, cast_options),
1275        (UInt32, Int16) => cast_numeric_arrays::<UInt32Type, Int16Type>(array, cast_options),
1276        (UInt32, Int32) => cast_numeric_arrays::<UInt32Type, Int32Type>(array, cast_options),
1277        (UInt32, Int64) => cast_numeric_arrays::<UInt32Type, Int64Type>(array, cast_options),
1278        (UInt32, Float16) => cast_numeric_arrays::<UInt32Type, Float16Type>(array, cast_options),
1279        (UInt32, Float32) => cast_numeric_arrays::<UInt32Type, Float32Type>(array, cast_options),
1280        (UInt32, Float64) => cast_numeric_arrays::<UInt32Type, Float64Type>(array, cast_options),
1281
1282        (UInt64, UInt8) => cast_numeric_arrays::<UInt64Type, UInt8Type>(array, cast_options),
1283        (UInt64, UInt16) => cast_numeric_arrays::<UInt64Type, UInt16Type>(array, cast_options),
1284        (UInt64, UInt32) => cast_numeric_arrays::<UInt64Type, UInt32Type>(array, cast_options),
1285        (UInt64, Int8) => cast_numeric_arrays::<UInt64Type, Int8Type>(array, cast_options),
1286        (UInt64, Int16) => cast_numeric_arrays::<UInt64Type, Int16Type>(array, cast_options),
1287        (UInt64, Int32) => cast_numeric_arrays::<UInt64Type, Int32Type>(array, cast_options),
1288        (UInt64, Int64) => cast_numeric_arrays::<UInt64Type, Int64Type>(array, cast_options),
1289        (UInt64, Float16) => cast_numeric_arrays::<UInt64Type, Float16Type>(array, cast_options),
1290        (UInt64, Float32) => cast_numeric_arrays::<UInt64Type, Float32Type>(array, cast_options),
1291        (UInt64, Float64) => cast_numeric_arrays::<UInt64Type, Float64Type>(array, cast_options),
1292
1293        (Int8, UInt8) => cast_numeric_arrays::<Int8Type, UInt8Type>(array, cast_options),
1294        (Int8, UInt16) => cast_numeric_arrays::<Int8Type, UInt16Type>(array, cast_options),
1295        (Int8, UInt32) => cast_numeric_arrays::<Int8Type, UInt32Type>(array, cast_options),
1296        (Int8, UInt64) => cast_numeric_arrays::<Int8Type, UInt64Type>(array, cast_options),
1297        (Int8, Int16) => cast_numeric_arrays::<Int8Type, Int16Type>(array, cast_options),
1298        (Int8, Int32) => cast_numeric_arrays::<Int8Type, Int32Type>(array, cast_options),
1299        (Int8, Int64) => cast_numeric_arrays::<Int8Type, Int64Type>(array, cast_options),
1300        (Int8, Float16) => cast_numeric_arrays::<Int8Type, Float16Type>(array, cast_options),
1301        (Int8, Float32) => cast_numeric_arrays::<Int8Type, Float32Type>(array, cast_options),
1302        (Int8, Float64) => cast_numeric_arrays::<Int8Type, Float64Type>(array, cast_options),
1303
1304        (Int16, UInt8) => cast_numeric_arrays::<Int16Type, UInt8Type>(array, cast_options),
1305        (Int16, UInt16) => cast_numeric_arrays::<Int16Type, UInt16Type>(array, cast_options),
1306        (Int16, UInt32) => cast_numeric_arrays::<Int16Type, UInt32Type>(array, cast_options),
1307        (Int16, UInt64) => cast_numeric_arrays::<Int16Type, UInt64Type>(array, cast_options),
1308        (Int16, Int8) => cast_numeric_arrays::<Int16Type, Int8Type>(array, cast_options),
1309        (Int16, Int32) => cast_numeric_arrays::<Int16Type, Int32Type>(array, cast_options),
1310        (Int16, Int64) => cast_numeric_arrays::<Int16Type, Int64Type>(array, cast_options),
1311        (Int16, Float16) => cast_numeric_arrays::<Int16Type, Float16Type>(array, cast_options),
1312        (Int16, Float32) => cast_numeric_arrays::<Int16Type, Float32Type>(array, cast_options),
1313        (Int16, Float64) => cast_numeric_arrays::<Int16Type, Float64Type>(array, cast_options),
1314
1315        (Int32, UInt8) => cast_numeric_arrays::<Int32Type, UInt8Type>(array, cast_options),
1316        (Int32, UInt16) => cast_numeric_arrays::<Int32Type, UInt16Type>(array, cast_options),
1317        (Int32, UInt32) => cast_numeric_arrays::<Int32Type, UInt32Type>(array, cast_options),
1318        (Int32, UInt64) => cast_numeric_arrays::<Int32Type, UInt64Type>(array, cast_options),
1319        (Int32, Int8) => cast_numeric_arrays::<Int32Type, Int8Type>(array, cast_options),
1320        (Int32, Int16) => cast_numeric_arrays::<Int32Type, Int16Type>(array, cast_options),
1321        (Int32, Int64) => cast_numeric_arrays::<Int32Type, Int64Type>(array, cast_options),
1322        (Int32, Float16) => cast_numeric_arrays::<Int32Type, Float16Type>(array, cast_options),
1323        (Int32, Float32) => cast_numeric_arrays::<Int32Type, Float32Type>(array, cast_options),
1324        (Int32, Float64) => cast_numeric_arrays::<Int32Type, Float64Type>(array, cast_options),
1325
1326        (Int64, UInt8) => cast_numeric_arrays::<Int64Type, UInt8Type>(array, cast_options),
1327        (Int64, UInt16) => cast_numeric_arrays::<Int64Type, UInt16Type>(array, cast_options),
1328        (Int64, UInt32) => cast_numeric_arrays::<Int64Type, UInt32Type>(array, cast_options),
1329        (Int64, UInt64) => cast_numeric_arrays::<Int64Type, UInt64Type>(array, cast_options),
1330        (Int64, Int8) => cast_numeric_arrays::<Int64Type, Int8Type>(array, cast_options),
1331        (Int64, Int16) => cast_numeric_arrays::<Int64Type, Int16Type>(array, cast_options),
1332        (Int64, Int32) => cast_numeric_arrays::<Int64Type, Int32Type>(array, cast_options),
1333        (Int64, Float16) => cast_numeric_arrays::<Int64Type, Float16Type>(array, cast_options),
1334        (Int64, Float32) => cast_numeric_arrays::<Int64Type, Float32Type>(array, cast_options),
1335        (Int64, Float64) => cast_numeric_arrays::<Int64Type, Float64Type>(array, cast_options),
1336
1337        (Float16, UInt8) => cast_numeric_arrays::<Float16Type, UInt8Type>(array, cast_options),
1338        (Float16, UInt16) => cast_numeric_arrays::<Float16Type, UInt16Type>(array, cast_options),
1339        (Float16, UInt32) => cast_numeric_arrays::<Float16Type, UInt32Type>(array, cast_options),
1340        (Float16, UInt64) => cast_numeric_arrays::<Float16Type, UInt64Type>(array, cast_options),
1341        (Float16, Int8) => cast_numeric_arrays::<Float16Type, Int8Type>(array, cast_options),
1342        (Float16, Int16) => cast_numeric_arrays::<Float16Type, Int16Type>(array, cast_options),
1343        (Float16, Int32) => cast_numeric_arrays::<Float16Type, Int32Type>(array, cast_options),
1344        (Float16, Int64) => cast_numeric_arrays::<Float16Type, Int64Type>(array, cast_options),
1345        (Float16, Float32) => cast_numeric_arrays::<Float16Type, Float32Type>(array, cast_options),
1346        (Float16, Float64) => cast_numeric_arrays::<Float16Type, Float64Type>(array, cast_options),
1347
1348        (Float32, UInt8) => cast_numeric_arrays::<Float32Type, UInt8Type>(array, cast_options),
1349        (Float32, UInt16) => cast_numeric_arrays::<Float32Type, UInt16Type>(array, cast_options),
1350        (Float32, UInt32) => cast_numeric_arrays::<Float32Type, UInt32Type>(array, cast_options),
1351        (Float32, UInt64) => cast_numeric_arrays::<Float32Type, UInt64Type>(array, cast_options),
1352        (Float32, Int8) => cast_numeric_arrays::<Float32Type, Int8Type>(array, cast_options),
1353        (Float32, Int16) => cast_numeric_arrays::<Float32Type, Int16Type>(array, cast_options),
1354        (Float32, Int32) => cast_numeric_arrays::<Float32Type, Int32Type>(array, cast_options),
1355        (Float32, Int64) => cast_numeric_arrays::<Float32Type, Int64Type>(array, cast_options),
1356        (Float32, Float16) => cast_numeric_arrays::<Float32Type, Float16Type>(array, cast_options),
1357        (Float32, Float64) => cast_numeric_arrays::<Float32Type, Float64Type>(array, cast_options),
1358
1359        (Float64, UInt8) => cast_numeric_arrays::<Float64Type, UInt8Type>(array, cast_options),
1360        (Float64, UInt16) => cast_numeric_arrays::<Float64Type, UInt16Type>(array, cast_options),
1361        (Float64, UInt32) => cast_numeric_arrays::<Float64Type, UInt32Type>(array, cast_options),
1362        (Float64, UInt64) => cast_numeric_arrays::<Float64Type, UInt64Type>(array, cast_options),
1363        (Float64, Int8) => cast_numeric_arrays::<Float64Type, Int8Type>(array, cast_options),
1364        (Float64, Int16) => cast_numeric_arrays::<Float64Type, Int16Type>(array, cast_options),
1365        (Float64, Int32) => cast_numeric_arrays::<Float64Type, Int32Type>(array, cast_options),
1366        (Float64, Int64) => cast_numeric_arrays::<Float64Type, Int64Type>(array, cast_options),
1367        (Float64, Float16) => cast_numeric_arrays::<Float64Type, Float16Type>(array, cast_options),
1368        (Float64, Float32) => cast_numeric_arrays::<Float64Type, Float32Type>(array, cast_options),
1369        // end numeric casts
1370
1371        // temporal casts
1372        (Int32, Date32) => cast_reinterpret_arrays::<Int32Type, Date32Type>(array),
1373        (Int32, Date64) => cast_with_options(
1374            &cast_with_options(array, &Date32, cast_options)?,
1375            &Date64,
1376            cast_options,
1377        ),
1378        (Int32, Time32(TimeUnit::Second)) => {
1379            cast_reinterpret_arrays::<Int32Type, Time32SecondType>(array)
1380        }
1381        (Int32, Time32(TimeUnit::Millisecond)) => {
1382            cast_reinterpret_arrays::<Int32Type, Time32MillisecondType>(array)
1383        }
1384        // No support for microsecond/nanosecond with i32
1385        (Date32, Int32) => cast_reinterpret_arrays::<Date32Type, Int32Type>(array),
1386        (Date32, Int64) => cast_with_options(
1387            &cast_with_options(array, &Int32, cast_options)?,
1388            &Int64,
1389            cast_options,
1390        ),
1391        (Time32(TimeUnit::Second), Int32) => {
1392            cast_reinterpret_arrays::<Time32SecondType, Int32Type>(array)
1393        }
1394        (Time32(TimeUnit::Millisecond), Int32) => {
1395            cast_reinterpret_arrays::<Time32MillisecondType, Int32Type>(array)
1396        }
1397        (Int64, Date64) => cast_reinterpret_arrays::<Int64Type, Date64Type>(array),
1398        (Int64, Date32) => cast_with_options(
1399            &cast_with_options(array, &Int32, cast_options)?,
1400            &Date32,
1401            cast_options,
1402        ),
1403        // No support for second/milliseconds with i64
1404        (Int64, Time64(TimeUnit::Microsecond)) => {
1405            cast_reinterpret_arrays::<Int64Type, Time64MicrosecondType>(array)
1406        }
1407        (Int64, Time64(TimeUnit::Nanosecond)) => {
1408            cast_reinterpret_arrays::<Int64Type, Time64NanosecondType>(array)
1409        }
1410
1411        (Date64, Int64) => cast_reinterpret_arrays::<Date64Type, Int64Type>(array),
1412        (Date64, Int32) => cast_with_options(
1413            &cast_with_options(array, &Int64, cast_options)?,
1414            &Int32,
1415            cast_options,
1416        ),
1417        (Time64(TimeUnit::Microsecond), Int64) => {
1418            cast_reinterpret_arrays::<Time64MicrosecondType, Int64Type>(array)
1419        }
1420        (Time64(TimeUnit::Nanosecond), Int64) => {
1421            cast_reinterpret_arrays::<Time64NanosecondType, Int64Type>(array)
1422        }
1423        (Date32, Date64) => Ok(Arc::new(
1424            array
1425                .as_primitive::<Date32Type>()
1426                .unary::<_, Date64Type>(|x| x as i64 * MILLISECONDS_IN_DAY),
1427        )),
1428        (Date64, Date32) => Ok(Arc::new(
1429            array
1430                .as_primitive::<Date64Type>()
1431                .unary::<_, Date32Type>(|x| (x / MILLISECONDS_IN_DAY) as i32),
1432        )),
1433
1434        (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1435            array
1436                .as_primitive::<Time32SecondType>()
1437                .unary::<_, Time32MillisecondType>(|x| x * MILLISECONDS as i32),
1438        )),
1439        (Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1440            array
1441                .as_primitive::<Time32SecondType>()
1442                .unary::<_, Time64MicrosecondType>(|x| x as i64 * MICROSECONDS),
1443        )),
1444        (Time32(TimeUnit::Second), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1445            array
1446                .as_primitive::<Time32SecondType>()
1447                .unary::<_, Time64NanosecondType>(|x| x as i64 * NANOSECONDS),
1448        )),
1449
1450        (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1451            array
1452                .as_primitive::<Time32MillisecondType>()
1453                .unary::<_, Time32SecondType>(|x| x / MILLISECONDS as i32),
1454        )),
1455        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1456            array
1457                .as_primitive::<Time32MillisecondType>()
1458                .unary::<_, Time64MicrosecondType>(|x| x as i64 * (MICROSECONDS / MILLISECONDS)),
1459        )),
1460        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1461            array
1462                .as_primitive::<Time32MillisecondType>()
1463                .unary::<_, Time64NanosecondType>(|x| x as i64 * (MICROSECONDS / NANOSECONDS)),
1464        )),
1465
1466        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1467            array
1468                .as_primitive::<Time64MicrosecondType>()
1469                .unary::<_, Time32SecondType>(|x| (x / MICROSECONDS) as i32),
1470        )),
1471        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1472            array
1473                .as_primitive::<Time64MicrosecondType>()
1474                .unary::<_, Time32MillisecondType>(|x| (x / (MICROSECONDS / MILLISECONDS)) as i32),
1475        )),
1476        (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1477            array
1478                .as_primitive::<Time64MicrosecondType>()
1479                .unary::<_, Time64NanosecondType>(|x| x * (NANOSECONDS / MICROSECONDS)),
1480        )),
1481
1482        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1483            array
1484                .as_primitive::<Time64NanosecondType>()
1485                .unary::<_, Time32SecondType>(|x| (x / NANOSECONDS) as i32),
1486        )),
1487        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1488            array
1489                .as_primitive::<Time64NanosecondType>()
1490                .unary::<_, Time32MillisecondType>(|x| (x / (NANOSECONDS / MILLISECONDS)) as i32),
1491        )),
1492        (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1493            array
1494                .as_primitive::<Time64NanosecondType>()
1495                .unary::<_, Time64MicrosecondType>(|x| x / (NANOSECONDS / MICROSECONDS)),
1496        )),
1497
1498        // Timestamp to integer/floating/decimals
1499        (Timestamp(TimeUnit::Second, _), _) if to_type.is_numeric() => {
1500            let array = cast_reinterpret_arrays::<TimestampSecondType, Int64Type>(array)?;
1501            cast_with_options(&array, to_type, cast_options)
1502        }
1503        (Timestamp(TimeUnit::Millisecond, _), _) if to_type.is_numeric() => {
1504            let array = cast_reinterpret_arrays::<TimestampMillisecondType, Int64Type>(array)?;
1505            cast_with_options(&array, to_type, cast_options)
1506        }
1507        (Timestamp(TimeUnit::Microsecond, _), _) if to_type.is_numeric() => {
1508            let array = cast_reinterpret_arrays::<TimestampMicrosecondType, Int64Type>(array)?;
1509            cast_with_options(&array, to_type, cast_options)
1510        }
1511        (Timestamp(TimeUnit::Nanosecond, _), _) if to_type.is_numeric() => {
1512            let array = cast_reinterpret_arrays::<TimestampNanosecondType, Int64Type>(array)?;
1513            cast_with_options(&array, to_type, cast_options)
1514        }
1515
1516        (_, Timestamp(unit, tz)) if from_type.is_numeric() => {
1517            let array = cast_with_options(array, &Int64, cast_options)?;
1518            Ok(make_timestamp_array(
1519                array.as_primitive(),
1520                *unit,
1521                tz.clone(),
1522            ))
1523        }
1524
1525        (Timestamp(from_unit, from_tz), Timestamp(to_unit, to_tz)) => {
1526            let array = cast_with_options(array, &Int64, cast_options)?;
1527            let time_array = array.as_primitive::<Int64Type>();
1528            let from_size = time_unit_multiple(from_unit);
1529            let to_size = time_unit_multiple(to_unit);
1530            // we either divide or multiply, depending on size of each unit
1531            // units are never the same when the types are the same
1532            let converted = match from_size.cmp(&to_size) {
1533                Ordering::Greater => {
1534                    let divisor = from_size / to_size;
1535                    time_array.unary::<_, Int64Type>(|o| o / divisor)
1536                }
1537                Ordering::Equal => time_array.clone(),
1538                Ordering::Less => {
1539                    let mul = to_size / from_size;
1540                    if cast_options.safe {
1541                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1542                    } else {
1543                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1544                    }
1545                }
1546            };
1547            // Normalize timezone
1548            let adjusted = match (from_tz, to_tz) {
1549                // Only this case needs to be adjusted because we're casting from
1550                // unknown time offset to some time offset, we want the time to be
1551                // unchanged.
1552                //
1553                // i.e. Timestamp('2001-01-01T00:00', None) -> Timestamp('2001-01-01T00:00', '+0700')
1554                (None, Some(to_tz)) => {
1555                    let to_tz: Tz = to_tz.parse()?;
1556                    match to_unit {
1557                        TimeUnit::Second => adjust_timestamp_to_timezone::<TimestampSecondType>(
1558                            converted,
1559                            &to_tz,
1560                            cast_options,
1561                        )?,
1562                        TimeUnit::Millisecond => adjust_timestamp_to_timezone::<
1563                            TimestampMillisecondType,
1564                        >(
1565                            converted, &to_tz, cast_options
1566                        )?,
1567                        TimeUnit::Microsecond => adjust_timestamp_to_timezone::<
1568                            TimestampMicrosecondType,
1569                        >(
1570                            converted, &to_tz, cast_options
1571                        )?,
1572                        TimeUnit::Nanosecond => adjust_timestamp_to_timezone::<
1573                            TimestampNanosecondType,
1574                        >(
1575                            converted, &to_tz, cast_options
1576                        )?,
1577                    }
1578                }
1579                _ => converted,
1580            };
1581            Ok(make_timestamp_array(&adjusted, *to_unit, to_tz.clone()))
1582        }
1583        (Timestamp(TimeUnit::Microsecond, _), Date32) => {
1584            timestamp_to_date32(array.as_primitive::<TimestampMicrosecondType>())
1585        }
1586        (Timestamp(TimeUnit::Millisecond, _), Date32) => {
1587            timestamp_to_date32(array.as_primitive::<TimestampMillisecondType>())
1588        }
1589        (Timestamp(TimeUnit::Second, _), Date32) => {
1590            timestamp_to_date32(array.as_primitive::<TimestampSecondType>())
1591        }
1592        (Timestamp(TimeUnit::Nanosecond, _), Date32) => {
1593            timestamp_to_date32(array.as_primitive::<TimestampNanosecondType>())
1594        }
1595        (Timestamp(TimeUnit::Second, _), Date64) => Ok(Arc::new(match cast_options.safe {
1596            true => {
1597                // change error to None
1598                array
1599                    .as_primitive::<TimestampSecondType>()
1600                    .unary_opt::<_, Date64Type>(|x| x.checked_mul(MILLISECONDS))
1601            }
1602            false => array
1603                .as_primitive::<TimestampSecondType>()
1604                .try_unary::<_, Date64Type, _>(|x| x.mul_checked(MILLISECONDS))?,
1605        })),
1606        (Timestamp(TimeUnit::Millisecond, _), Date64) => {
1607            cast_reinterpret_arrays::<TimestampMillisecondType, Date64Type>(array)
1608        }
1609        (Timestamp(TimeUnit::Microsecond, _), Date64) => Ok(Arc::new(
1610            array
1611                .as_primitive::<TimestampMicrosecondType>()
1612                .unary::<_, Date64Type>(|x| x / (MICROSECONDS / MILLISECONDS)),
1613        )),
1614        (Timestamp(TimeUnit::Nanosecond, _), Date64) => Ok(Arc::new(
1615            array
1616                .as_primitive::<TimestampNanosecondType>()
1617                .unary::<_, Date64Type>(|x| x / (NANOSECONDS / MILLISECONDS)),
1618        )),
1619        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Microsecond)) => {
1620            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1621            Ok(Arc::new(
1622                array
1623                    .as_primitive::<TimestampSecondType>()
1624                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1625                        Ok(time_to_time64us(as_time_res_with_timezone::<
1626                            TimestampSecondType,
1627                        >(x, tz)?))
1628                    })?,
1629            ))
1630        }
1631        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Nanosecond)) => {
1632            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1633            Ok(Arc::new(
1634                array
1635                    .as_primitive::<TimestampSecondType>()
1636                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1637                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1638                            TimestampSecondType,
1639                        >(x, tz)?))
1640                    })?,
1641            ))
1642        }
1643        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Microsecond)) => {
1644            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1645            Ok(Arc::new(
1646                array
1647                    .as_primitive::<TimestampMillisecondType>()
1648                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1649                        Ok(time_to_time64us(as_time_res_with_timezone::<
1650                            TimestampMillisecondType,
1651                        >(x, tz)?))
1652                    })?,
1653            ))
1654        }
1655        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Nanosecond)) => {
1656            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1657            Ok(Arc::new(
1658                array
1659                    .as_primitive::<TimestampMillisecondType>()
1660                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1661                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1662                            TimestampMillisecondType,
1663                        >(x, tz)?))
1664                    })?,
1665            ))
1666        }
1667        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Microsecond)) => {
1668            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1669            Ok(Arc::new(
1670                array
1671                    .as_primitive::<TimestampMicrosecondType>()
1672                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1673                        Ok(time_to_time64us(as_time_res_with_timezone::<
1674                            TimestampMicrosecondType,
1675                        >(x, tz)?))
1676                    })?,
1677            ))
1678        }
1679        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Nanosecond)) => {
1680            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1681            Ok(Arc::new(
1682                array
1683                    .as_primitive::<TimestampMicrosecondType>()
1684                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1685                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1686                            TimestampMicrosecondType,
1687                        >(x, tz)?))
1688                    })?,
1689            ))
1690        }
1691        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Microsecond)) => {
1692            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1693            Ok(Arc::new(
1694                array
1695                    .as_primitive::<TimestampNanosecondType>()
1696                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1697                        Ok(time_to_time64us(as_time_res_with_timezone::<
1698                            TimestampNanosecondType,
1699                        >(x, tz)?))
1700                    })?,
1701            ))
1702        }
1703        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Nanosecond)) => {
1704            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1705            Ok(Arc::new(
1706                array
1707                    .as_primitive::<TimestampNanosecondType>()
1708                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1709                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1710                            TimestampNanosecondType,
1711                        >(x, tz)?))
1712                    })?,
1713            ))
1714        }
1715        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Second)) => {
1716            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1717            Ok(Arc::new(
1718                array
1719                    .as_primitive::<TimestampSecondType>()
1720                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1721                        Ok(time_to_time32s(as_time_res_with_timezone::<
1722                            TimestampSecondType,
1723                        >(x, tz)?))
1724                    })?,
1725            ))
1726        }
1727        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Millisecond)) => {
1728            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1729            Ok(Arc::new(
1730                array
1731                    .as_primitive::<TimestampSecondType>()
1732                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1733                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1734                            TimestampSecondType,
1735                        >(x, tz)?))
1736                    })?,
1737            ))
1738        }
1739        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Second)) => {
1740            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1741            Ok(Arc::new(
1742                array
1743                    .as_primitive::<TimestampMillisecondType>()
1744                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1745                        Ok(time_to_time32s(as_time_res_with_timezone::<
1746                            TimestampMillisecondType,
1747                        >(x, tz)?))
1748                    })?,
1749            ))
1750        }
1751        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Millisecond)) => {
1752            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1753            Ok(Arc::new(
1754                array
1755                    .as_primitive::<TimestampMillisecondType>()
1756                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1757                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1758                            TimestampMillisecondType,
1759                        >(x, tz)?))
1760                    })?,
1761            ))
1762        }
1763        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Second)) => {
1764            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1765            Ok(Arc::new(
1766                array
1767                    .as_primitive::<TimestampMicrosecondType>()
1768                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1769                        Ok(time_to_time32s(as_time_res_with_timezone::<
1770                            TimestampMicrosecondType,
1771                        >(x, tz)?))
1772                    })?,
1773            ))
1774        }
1775        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Millisecond)) => {
1776            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1777            Ok(Arc::new(
1778                array
1779                    .as_primitive::<TimestampMicrosecondType>()
1780                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1781                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1782                            TimestampMicrosecondType,
1783                        >(x, tz)?))
1784                    })?,
1785            ))
1786        }
1787        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Second)) => {
1788            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1789            Ok(Arc::new(
1790                array
1791                    .as_primitive::<TimestampNanosecondType>()
1792                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1793                        Ok(time_to_time32s(as_time_res_with_timezone::<
1794                            TimestampNanosecondType,
1795                        >(x, tz)?))
1796                    })?,
1797            ))
1798        }
1799        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Millisecond)) => {
1800            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1801            Ok(Arc::new(
1802                array
1803                    .as_primitive::<TimestampNanosecondType>()
1804                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1805                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1806                            TimestampNanosecondType,
1807                        >(x, tz)?))
1808                    })?,
1809            ))
1810        }
1811        (Date64, Timestamp(TimeUnit::Second, _)) => {
1812            let array = array
1813                .as_primitive::<Date64Type>()
1814                .unary::<_, TimestampSecondType>(|x| x / MILLISECONDS);
1815
1816            cast_with_options(&array, to_type, cast_options)
1817        }
1818        (Date64, Timestamp(TimeUnit::Millisecond, _)) => {
1819            let array = array
1820                .as_primitive::<Date64Type>()
1821                .reinterpret_cast::<TimestampMillisecondType>();
1822
1823            cast_with_options(&array, to_type, cast_options)
1824        }
1825
1826        (Date64, Timestamp(TimeUnit::Microsecond, _)) => {
1827            let array = array
1828                .as_primitive::<Date64Type>()
1829                .unary::<_, TimestampMicrosecondType>(|x| x * (MICROSECONDS / MILLISECONDS));
1830
1831            cast_with_options(&array, to_type, cast_options)
1832        }
1833        (Date64, Timestamp(TimeUnit::Nanosecond, _)) => {
1834            let array = array
1835                .as_primitive::<Date64Type>()
1836                .unary::<_, TimestampNanosecondType>(|x| x * (NANOSECONDS / MILLISECONDS));
1837
1838            cast_with_options(&array, to_type, cast_options)
1839        }
1840        (Date32, Timestamp(TimeUnit::Second, _)) => {
1841            let array = array
1842                .as_primitive::<Date32Type>()
1843                .unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY);
1844
1845            cast_with_options(&array, to_type, cast_options)
1846        }
1847        (Date32, Timestamp(TimeUnit::Millisecond, _)) => {
1848            let array = array
1849                .as_primitive::<Date32Type>()
1850                .unary::<_, TimestampMillisecondType>(|x| (x as i64) * MILLISECONDS_IN_DAY);
1851
1852            cast_with_options(&array, to_type, cast_options)
1853        }
1854        (Date32, Timestamp(TimeUnit::Microsecond, _)) => {
1855            let array = array
1856                .as_primitive::<Date32Type>()
1857                .unary::<_, TimestampMicrosecondType>(|x| (x as i64) * MICROSECONDS_IN_DAY);
1858
1859            cast_with_options(&array, to_type, cast_options)
1860        }
1861        (Date32, Timestamp(TimeUnit::Nanosecond, _)) => {
1862            let array = array
1863                .as_primitive::<Date32Type>()
1864                .unary::<_, TimestampNanosecondType>(|x| (x as i64) * NANOSECONDS_IN_DAY);
1865
1866            cast_with_options(&array, to_type, cast_options)
1867        }
1868
1869        (_, Duration(unit)) if from_type.is_numeric() => {
1870            let array = cast_with_options(array, &Int64, cast_options)?;
1871            Ok(make_duration_array(array.as_primitive(), *unit))
1872        }
1873        (Duration(TimeUnit::Second), _) if to_type.is_numeric() => {
1874            let array = cast_reinterpret_arrays::<DurationSecondType, Int64Type>(array)?;
1875            cast_with_options(&array, to_type, cast_options)
1876        }
1877        (Duration(TimeUnit::Millisecond), _) if to_type.is_numeric() => {
1878            let array = cast_reinterpret_arrays::<DurationMillisecondType, Int64Type>(array)?;
1879            cast_with_options(&array, to_type, cast_options)
1880        }
1881        (Duration(TimeUnit::Microsecond), _) if to_type.is_numeric() => {
1882            let array = cast_reinterpret_arrays::<DurationMicrosecondType, Int64Type>(array)?;
1883            cast_with_options(&array, to_type, cast_options)
1884        }
1885        (Duration(TimeUnit::Nanosecond), _) if to_type.is_numeric() => {
1886            let array = cast_reinterpret_arrays::<DurationNanosecondType, Int64Type>(array)?;
1887            cast_with_options(&array, to_type, cast_options)
1888        }
1889
1890        (Duration(from_unit), Duration(to_unit)) => {
1891            let array = cast_with_options(array, &Int64, cast_options)?;
1892            let time_array = array.as_primitive::<Int64Type>();
1893            let from_size = time_unit_multiple(from_unit);
1894            let to_size = time_unit_multiple(to_unit);
1895            // we either divide or multiply, depending on size of each unit
1896            // units are never the same when the types are the same
1897            let converted = match from_size.cmp(&to_size) {
1898                Ordering::Greater => {
1899                    let divisor = from_size / to_size;
1900                    time_array.unary::<_, Int64Type>(|o| o / divisor)
1901                }
1902                Ordering::Equal => time_array.clone(),
1903                Ordering::Less => {
1904                    let mul = to_size / from_size;
1905                    if cast_options.safe {
1906                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1907                    } else {
1908                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1909                    }
1910                }
1911            };
1912            Ok(make_duration_array(&converted, *to_unit))
1913        }
1914
1915        (Duration(TimeUnit::Second), Interval(IntervalUnit::MonthDayNano)) => {
1916            cast_duration_to_interval::<DurationSecondType>(array, cast_options)
1917        }
1918        (Duration(TimeUnit::Millisecond), Interval(IntervalUnit::MonthDayNano)) => {
1919            cast_duration_to_interval::<DurationMillisecondType>(array, cast_options)
1920        }
1921        (Duration(TimeUnit::Microsecond), Interval(IntervalUnit::MonthDayNano)) => {
1922            cast_duration_to_interval::<DurationMicrosecondType>(array, cast_options)
1923        }
1924        (Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano)) => {
1925            cast_duration_to_interval::<DurationNanosecondType>(array, cast_options)
1926        }
1927        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Second)) => {
1928            cast_month_day_nano_to_duration::<DurationSecondType>(array, cast_options)
1929        }
1930        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Millisecond)) => {
1931            cast_month_day_nano_to_duration::<DurationMillisecondType>(array, cast_options)
1932        }
1933        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Microsecond)) => {
1934            cast_month_day_nano_to_duration::<DurationMicrosecondType>(array, cast_options)
1935        }
1936        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Nanosecond)) => {
1937            cast_month_day_nano_to_duration::<DurationNanosecondType>(array, cast_options)
1938        }
1939        (Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
1940            cast_interval_year_month_to_interval_month_day_nano(array, cast_options)
1941        }
1942        (Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
1943            cast_interval_day_time_to_interval_month_day_nano(array, cast_options)
1944        }
1945        (Int32, Interval(IntervalUnit::YearMonth)) => {
1946            cast_reinterpret_arrays::<Int32Type, IntervalYearMonthType>(array)
1947        }
1948        (_, _) => Err(ArrowError::CastError(format!(
1949            "Casting from {from_type:?} to {to_type:?} not supported",
1950        ))),
1951    }
1952}
1953
1954fn cast_from_decimal<D, F>(
1955    array: &dyn Array,
1956    base: D::Native,
1957    scale: &i8,
1958    from_type: &DataType,
1959    to_type: &DataType,
1960    as_float: F,
1961    cast_options: &CastOptions,
1962) -> Result<ArrayRef, ArrowError>
1963where
1964    D: DecimalType + ArrowPrimitiveType,
1965    <D as ArrowPrimitiveType>::Native: ArrowNativeTypeOp + ToPrimitive,
1966    F: Fn(D::Native) -> f64,
1967{
1968    use DataType::*;
1969    // cast decimal to other type
1970    match to_type {
1971        UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, base, *scale, cast_options),
1972        UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, base, *scale, cast_options),
1973        UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, base, *scale, cast_options),
1974        UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, base, *scale, cast_options),
1975        Int8 => cast_decimal_to_integer::<D, Int8Type>(array, base, *scale, cast_options),
1976        Int16 => cast_decimal_to_integer::<D, Int16Type>(array, base, *scale, cast_options),
1977        Int32 => cast_decimal_to_integer::<D, Int32Type>(array, base, *scale, cast_options),
1978        Int64 => cast_decimal_to_integer::<D, Int64Type>(array, base, *scale, cast_options),
1979        Float32 => cast_decimal_to_float::<D, Float32Type, _>(array, |x| {
1980            (as_float(x) / 10_f64.powi(*scale as i32)) as f32
1981        }),
1982        Float64 => cast_decimal_to_float::<D, Float64Type, _>(array, |x| {
1983            as_float(x) / 10_f64.powi(*scale as i32)
1984        }),
1985        Utf8View => value_to_string_view(array, cast_options),
1986        Utf8 => value_to_string::<i32>(array, cast_options),
1987        LargeUtf8 => value_to_string::<i64>(array, cast_options),
1988        Null => Ok(new_null_array(to_type, array.len())),
1989        _ => Err(ArrowError::CastError(format!(
1990            "Casting from {from_type:?} to {to_type:?} not supported"
1991        ))),
1992    }
1993}
1994
1995fn cast_to_decimal<D, M>(
1996    array: &dyn Array,
1997    base: M,
1998    precision: &u8,
1999    scale: &i8,
2000    from_type: &DataType,
2001    to_type: &DataType,
2002    cast_options: &CastOptions,
2003) -> Result<ArrayRef, ArrowError>
2004where
2005    D: DecimalType + ArrowPrimitiveType<Native = M>,
2006    M: ArrowNativeTypeOp + DecimalCast,
2007    u8: num::traits::AsPrimitive<M>,
2008    u16: num::traits::AsPrimitive<M>,
2009    u32: num::traits::AsPrimitive<M>,
2010    u64: num::traits::AsPrimitive<M>,
2011    i8: num::traits::AsPrimitive<M>,
2012    i16: num::traits::AsPrimitive<M>,
2013    i32: num::traits::AsPrimitive<M>,
2014    i64: num::traits::AsPrimitive<M>,
2015{
2016    use DataType::*;
2017    // cast data to decimal
2018    match from_type {
2019        UInt8 => cast_integer_to_decimal::<_, D, M>(
2020            array.as_primitive::<UInt8Type>(),
2021            *precision,
2022            *scale,
2023            base,
2024            cast_options,
2025        ),
2026        UInt16 => cast_integer_to_decimal::<_, D, _>(
2027            array.as_primitive::<UInt16Type>(),
2028            *precision,
2029            *scale,
2030            base,
2031            cast_options,
2032        ),
2033        UInt32 => cast_integer_to_decimal::<_, D, _>(
2034            array.as_primitive::<UInt32Type>(),
2035            *precision,
2036            *scale,
2037            base,
2038            cast_options,
2039        ),
2040        UInt64 => cast_integer_to_decimal::<_, D, _>(
2041            array.as_primitive::<UInt64Type>(),
2042            *precision,
2043            *scale,
2044            base,
2045            cast_options,
2046        ),
2047        Int8 => cast_integer_to_decimal::<_, D, _>(
2048            array.as_primitive::<Int8Type>(),
2049            *precision,
2050            *scale,
2051            base,
2052            cast_options,
2053        ),
2054        Int16 => cast_integer_to_decimal::<_, D, _>(
2055            array.as_primitive::<Int16Type>(),
2056            *precision,
2057            *scale,
2058            base,
2059            cast_options,
2060        ),
2061        Int32 => cast_integer_to_decimal::<_, D, _>(
2062            array.as_primitive::<Int32Type>(),
2063            *precision,
2064            *scale,
2065            base,
2066            cast_options,
2067        ),
2068        Int64 => cast_integer_to_decimal::<_, D, _>(
2069            array.as_primitive::<Int64Type>(),
2070            *precision,
2071            *scale,
2072            base,
2073            cast_options,
2074        ),
2075        Float32 => cast_floating_point_to_decimal::<_, D>(
2076            array.as_primitive::<Float32Type>(),
2077            *precision,
2078            *scale,
2079            cast_options,
2080        ),
2081        Float64 => cast_floating_point_to_decimal::<_, D>(
2082            array.as_primitive::<Float64Type>(),
2083            *precision,
2084            *scale,
2085            cast_options,
2086        ),
2087        Utf8View | Utf8 => {
2088            cast_string_to_decimal::<D, i32>(array, *precision, *scale, cast_options)
2089        }
2090        LargeUtf8 => cast_string_to_decimal::<D, i64>(array, *precision, *scale, cast_options),
2091        Null => Ok(new_null_array(to_type, array.len())),
2092        _ => Err(ArrowError::CastError(format!(
2093            "Casting from {from_type:?} to {to_type:?} not supported"
2094        ))),
2095    }
2096}
2097
2098/// Get the time unit as a multiple of a second
2099const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
2100    match unit {
2101        TimeUnit::Second => 1,
2102        TimeUnit::Millisecond => MILLISECONDS,
2103        TimeUnit::Microsecond => MICROSECONDS,
2104        TimeUnit::Nanosecond => NANOSECONDS,
2105    }
2106}
2107
2108/// Convert Array into a PrimitiveArray of type, and apply numeric cast
2109fn cast_numeric_arrays<FROM, TO>(
2110    from: &dyn Array,
2111    cast_options: &CastOptions,
2112) -> Result<ArrayRef, ArrowError>
2113where
2114    FROM: ArrowPrimitiveType,
2115    TO: ArrowPrimitiveType,
2116    FROM::Native: NumCast,
2117    TO::Native: NumCast,
2118{
2119    if cast_options.safe {
2120        // If the value can't be casted to the `TO::Native`, return null
2121        Ok(Arc::new(numeric_cast::<FROM, TO>(
2122            from.as_primitive::<FROM>(),
2123        )))
2124    } else {
2125        // If the value can't be casted to the `TO::Native`, return error
2126        Ok(Arc::new(try_numeric_cast::<FROM, TO>(
2127            from.as_primitive::<FROM>(),
2128        )?))
2129    }
2130}
2131
2132// Natural cast between numeric types
2133// If the value of T can't be casted to R, will throw error
2134fn try_numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>, ArrowError>
2135where
2136    T: ArrowPrimitiveType,
2137    R: ArrowPrimitiveType,
2138    T::Native: NumCast,
2139    R::Native: NumCast,
2140{
2141    from.try_unary(|value| {
2142        num::cast::cast::<T::Native, R::Native>(value).ok_or_else(|| {
2143            ArrowError::CastError(format!(
2144                "Can't cast value {:?} to type {}",
2145                value,
2146                R::DATA_TYPE
2147            ))
2148        })
2149    })
2150}
2151
2152// Natural cast between numeric types
2153// If the value of T can't be casted to R, it will be converted to null
2154fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> PrimitiveArray<R>
2155where
2156    T: ArrowPrimitiveType,
2157    R: ArrowPrimitiveType,
2158    T::Native: NumCast,
2159    R::Native: NumCast,
2160{
2161    from.unary_opt::<_, R>(num::cast::cast::<T::Native, R::Native>)
2162}
2163
2164fn cast_numeric_to_binary<FROM: ArrowPrimitiveType, O: OffsetSizeTrait>(
2165    array: &dyn Array,
2166) -> Result<ArrayRef, ArrowError> {
2167    let array = array.as_primitive::<FROM>();
2168    let size = std::mem::size_of::<FROM::Native>();
2169    let offsets = OffsetBuffer::from_lengths(std::iter::repeat(size).take(array.len()));
2170    Ok(Arc::new(GenericBinaryArray::<O>::new(
2171        offsets,
2172        array.values().inner().clone(),
2173        array.nulls().cloned(),
2174    )))
2175}
2176
2177fn adjust_timestamp_to_timezone<T: ArrowTimestampType>(
2178    array: PrimitiveArray<Int64Type>,
2179    to_tz: &Tz,
2180    cast_options: &CastOptions,
2181) -> Result<PrimitiveArray<Int64Type>, ArrowError> {
2182    let adjust = |o| {
2183        let local = as_datetime::<T>(o)?;
2184        let offset = to_tz.offset_from_local_datetime(&local).single()?;
2185        T::make_value(local - offset.fix())
2186    };
2187    let adjusted = if cast_options.safe {
2188        array.unary_opt::<_, Int64Type>(adjust)
2189    } else {
2190        array.try_unary::<_, Int64Type, _>(|o| {
2191            adjust(o).ok_or_else(|| {
2192                ArrowError::CastError("Cannot cast timezone to different timezone".to_string())
2193            })
2194        })?
2195    };
2196    Ok(adjusted)
2197}
2198
2199/// Cast numeric types to Boolean
2200///
2201/// Any zero value returns `false` while non-zero returns `true`
2202fn cast_numeric_to_bool<FROM>(from: &dyn Array) -> Result<ArrayRef, ArrowError>
2203where
2204    FROM: ArrowPrimitiveType,
2205{
2206    numeric_to_bool_cast::<FROM>(from.as_primitive::<FROM>()).map(|to| Arc::new(to) as ArrayRef)
2207}
2208
2209fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray, ArrowError>
2210where
2211    T: ArrowPrimitiveType + ArrowPrimitiveType,
2212{
2213    let mut b = BooleanBuilder::with_capacity(from.len());
2214
2215    for i in 0..from.len() {
2216        if from.is_null(i) {
2217            b.append_null();
2218        } else if from.value(i) != T::default_value() {
2219            b.append_value(true);
2220        } else {
2221            b.append_value(false);
2222        }
2223    }
2224
2225    Ok(b.finish())
2226}
2227
2228/// Cast Boolean types to numeric
2229///
2230/// `false` returns 0 while `true` returns 1
2231fn cast_bool_to_numeric<TO>(
2232    from: &dyn Array,
2233    cast_options: &CastOptions,
2234) -> Result<ArrayRef, ArrowError>
2235where
2236    TO: ArrowPrimitiveType,
2237    TO::Native: num::cast::NumCast,
2238{
2239    Ok(Arc::new(bool_to_numeric_cast::<TO>(
2240        from.as_any().downcast_ref::<BooleanArray>().unwrap(),
2241        cast_options,
2242    )))
2243}
2244
2245fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions) -> PrimitiveArray<T>
2246where
2247    T: ArrowPrimitiveType,
2248    T::Native: num::NumCast,
2249{
2250    let iter = (0..from.len()).map(|i| {
2251        if from.is_null(i) {
2252            None
2253        } else if from.value(i) {
2254            // a workaround to cast a primitive to T::Native, infallible
2255            num::cast::cast(1)
2256        } else {
2257            Some(T::default_value())
2258        }
2259    });
2260    // Benefit:
2261    //     20% performance improvement
2262    // Soundness:
2263    //     The iterator is trustedLen because it comes from a Range
2264    unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
2265}
2266
2267/// Helper function to cast from one `BinaryArray` or 'LargeBinaryArray' to 'FixedSizeBinaryArray'.
2268fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
2269    array: &dyn Array,
2270    byte_width: i32,
2271    cast_options: &CastOptions,
2272) -> Result<ArrayRef, ArrowError> {
2273    let array = array.as_binary::<O>();
2274    let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(), byte_width);
2275
2276    for i in 0..array.len() {
2277        if array.is_null(i) {
2278            builder.append_null();
2279        } else {
2280            match builder.append_value(array.value(i)) {
2281                Ok(_) => {}
2282                Err(e) => match cast_options.safe {
2283                    true => builder.append_null(),
2284                    false => return Err(e),
2285                },
2286            }
2287        }
2288    }
2289
2290    Ok(Arc::new(builder.finish()))
2291}
2292
2293/// Helper function to cast from 'FixedSizeBinaryArray' to one `BinaryArray` or 'LargeBinaryArray'.
2294/// If the target one is too large for the source array it will return an Error.
2295fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
2296    array: &dyn Array,
2297    byte_width: i32,
2298) -> Result<ArrayRef, ArrowError> {
2299    let array = array
2300        .as_any()
2301        .downcast_ref::<FixedSizeBinaryArray>()
2302        .unwrap();
2303
2304    let offsets: i128 = byte_width as i128 * array.len() as i128;
2305
2306    let is_binary = matches!(GenericBinaryType::<O>::DATA_TYPE, DataType::Binary);
2307    if is_binary && offsets > i32::MAX as i128 {
2308        return Err(ArrowError::ComputeError(
2309            "FixedSizeBinary array too large to cast to Binary array".to_string(),
2310        ));
2311    } else if !is_binary && offsets > i64::MAX as i128 {
2312        return Err(ArrowError::ComputeError(
2313            "FixedSizeBinary array too large to cast to LargeBinary array".to_string(),
2314        ));
2315    }
2316
2317    let mut builder = GenericBinaryBuilder::<O>::with_capacity(array.len(), array.len());
2318
2319    for i in 0..array.len() {
2320        if array.is_null(i) {
2321            builder.append_null();
2322        } else {
2323            builder.append_value(array.value(i));
2324        }
2325    }
2326
2327    Ok(Arc::new(builder.finish()))
2328}
2329
2330/// Helper function to cast from one `ByteArrayType` to another and vice versa.
2331/// If the target one (e.g., `LargeUtf8`) is too large for the source array it will return an Error.
2332fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2333where
2334    FROM: ByteArrayType,
2335    TO: ByteArrayType<Native = FROM::Native>,
2336    FROM::Offset: OffsetSizeTrait + ToPrimitive,
2337    TO::Offset: OffsetSizeTrait + NumCast,
2338{
2339    let data = array.to_data();
2340    assert_eq!(data.data_type(), &FROM::DATA_TYPE);
2341    let str_values_buf = data.buffers()[1].clone();
2342    let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
2343
2344    let mut offset_builder = BufferBuilder::<TO::Offset>::new(offsets.len());
2345    offsets
2346        .iter()
2347        .try_for_each::<_, Result<_, ArrowError>>(|offset| {
2348            let offset =
2349                <<TO as ByteArrayType>::Offset as NumCast>::from(*offset).ok_or_else(|| {
2350                    ArrowError::ComputeError(format!(
2351                        "{}{} array too large to cast to {}{} array",
2352                        FROM::Offset::PREFIX,
2353                        FROM::PREFIX,
2354                        TO::Offset::PREFIX,
2355                        TO::PREFIX
2356                    ))
2357                })?;
2358            offset_builder.append(offset);
2359            Ok(())
2360        })?;
2361
2362    let offset_buffer = offset_builder.finish();
2363
2364    let dtype = TO::DATA_TYPE;
2365
2366    let builder = ArrayData::builder(dtype)
2367        .offset(array.offset())
2368        .len(array.len())
2369        .add_buffer(offset_buffer)
2370        .add_buffer(str_values_buf)
2371        .nulls(data.nulls().cloned());
2372
2373    let array_data = unsafe { builder.build_unchecked() };
2374
2375    Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
2376}
2377
2378/// Helper function to cast from one `ByteViewType` array to `ByteArrayType` array.
2379fn cast_view_to_byte<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2380where
2381    FROM: ByteViewType,
2382    TO: ByteArrayType,
2383    FROM::Native: AsRef<TO::Native>,
2384{
2385    let data = array.to_data();
2386    let view_array = GenericByteViewArray::<FROM>::from(data);
2387
2388    let len = view_array.len();
2389    let bytes = view_array
2390        .views()
2391        .iter()
2392        .map(|v| ByteView::from(*v).length as usize)
2393        .sum::<usize>();
2394
2395    let mut byte_array_builder = GenericByteBuilder::<TO>::with_capacity(len, bytes);
2396
2397    for val in view_array.iter() {
2398        byte_array_builder.append_option(val);
2399    }
2400
2401    Ok(Arc::new(byte_array_builder.finish()))
2402}
2403
2404#[cfg(test)]
2405mod tests {
2406    use super::*;
2407    use arrow_buffer::{Buffer, IntervalDayTime, NullBuffer};
2408    use chrono::NaiveDate;
2409    use half::f16;
2410
2411    #[derive(Clone)]
2412    struct DecimalCastTestConfig {
2413        input_prec: u8,
2414        input_scale: i8,
2415        input_repr: i128,
2416        output_prec: u8,
2417        output_scale: i8,
2418        expected_output_repr: Result<i128, String>, // the error variant can contain a string
2419                                                    // template where the "{}" will be
2420                                                    // replaced with the decimal type name
2421                                                    // (e.g. Decimal128)
2422    }
2423
2424    macro_rules! generate_cast_test_case {
2425        ($INPUT_ARRAY: expr, $OUTPUT_TYPE_ARRAY: ident, $OUTPUT_TYPE: expr, $OUTPUT_VALUES: expr) => {
2426            let output =
2427                $OUTPUT_TYPE_ARRAY::from($OUTPUT_VALUES).with_data_type($OUTPUT_TYPE.clone());
2428
2429            // assert cast type
2430            let input_array_type = $INPUT_ARRAY.data_type();
2431            assert!(can_cast_types(input_array_type, $OUTPUT_TYPE));
2432            let result = cast($INPUT_ARRAY, $OUTPUT_TYPE).unwrap();
2433            assert_eq!($OUTPUT_TYPE, result.data_type());
2434            assert_eq!(result.as_ref(), &output);
2435
2436            let cast_option = CastOptions {
2437                safe: false,
2438                format_options: FormatOptions::default(),
2439            };
2440            let result = cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap();
2441            assert_eq!($OUTPUT_TYPE, result.data_type());
2442            assert_eq!(result.as_ref(), &output);
2443        };
2444    }
2445
2446    fn run_decimal_cast_test_case<I, O>(t: DecimalCastTestConfig)
2447    where
2448        I: DecimalType,
2449        O: DecimalType,
2450        I::Native: DecimalCast,
2451        O::Native: DecimalCast,
2452    {
2453        let array = vec![I::Native::from_decimal(t.input_repr)];
2454        let array = array
2455            .into_iter()
2456            .collect::<PrimitiveArray<I>>()
2457            .with_precision_and_scale(t.input_prec, t.input_scale)
2458            .unwrap();
2459        let input_type = array.data_type();
2460        let output_type = O::TYPE_CONSTRUCTOR(t.output_prec, t.output_scale);
2461        assert!(can_cast_types(input_type, &output_type));
2462
2463        let options = CastOptions {
2464            safe: false,
2465            ..Default::default()
2466        };
2467        let result = cast_with_options(&array, &output_type, &options);
2468
2469        match t.expected_output_repr {
2470            Ok(v) => {
2471                let expected_array = vec![O::Native::from_decimal(v)];
2472                let expected_array = expected_array
2473                    .into_iter()
2474                    .collect::<PrimitiveArray<O>>()
2475                    .with_precision_and_scale(t.output_prec, t.output_scale)
2476                    .unwrap();
2477                assert_eq!(*result.unwrap(), expected_array);
2478            }
2479            Err(expected_output_message_template) => {
2480                assert!(result.is_err());
2481                let expected_error_message =
2482                    expected_output_message_template.replace("{}", O::PREFIX);
2483                assert_eq!(result.unwrap_err().to_string(), expected_error_message);
2484            }
2485        }
2486    }
2487
2488    fn create_decimal128_array(
2489        array: Vec<Option<i128>>,
2490        precision: u8,
2491        scale: i8,
2492    ) -> Result<Decimal128Array, ArrowError> {
2493        array
2494            .into_iter()
2495            .collect::<Decimal128Array>()
2496            .with_precision_and_scale(precision, scale)
2497    }
2498
2499    fn create_decimal256_array(
2500        array: Vec<Option<i256>>,
2501        precision: u8,
2502        scale: i8,
2503    ) -> Result<Decimal256Array, ArrowError> {
2504        array
2505            .into_iter()
2506            .collect::<Decimal256Array>()
2507            .with_precision_and_scale(precision, scale)
2508    }
2509
2510    #[test]
2511    #[cfg(not(feature = "force_validate"))]
2512    #[should_panic(
2513        expected = "Cannot cast to Decimal128(20, 3). Overflowing on 57896044618658097711785492504343953926634992332820282019728792003956564819967"
2514    )]
2515    fn test_cast_decimal_to_decimal_round_with_error() {
2516        // decimal256 to decimal128 overflow
2517        let array = vec![
2518            Some(i256::from_i128(1123454)),
2519            Some(i256::from_i128(2123456)),
2520            Some(i256::from_i128(-3123453)),
2521            Some(i256::from_i128(-3123456)),
2522            None,
2523            Some(i256::MAX),
2524            Some(i256::MIN),
2525        ];
2526        let input_decimal_array = create_decimal256_array(array, 76, 4).unwrap();
2527        let array = Arc::new(input_decimal_array) as ArrayRef;
2528        let input_type = DataType::Decimal256(76, 4);
2529        let output_type = DataType::Decimal128(20, 3);
2530        assert!(can_cast_types(&input_type, &output_type));
2531        generate_cast_test_case!(
2532            &array,
2533            Decimal128Array,
2534            &output_type,
2535            vec![
2536                Some(112345_i128),
2537                Some(212346_i128),
2538                Some(-312345_i128),
2539                Some(-312346_i128),
2540                None,
2541                None,
2542                None,
2543            ]
2544        );
2545    }
2546
2547    #[test]
2548    #[cfg(not(feature = "force_validate"))]
2549    fn test_cast_decimal_to_decimal_round() {
2550        let array = vec![
2551            Some(1123454),
2552            Some(2123456),
2553            Some(-3123453),
2554            Some(-3123456),
2555            None,
2556        ];
2557        let array = create_decimal128_array(array, 20, 4).unwrap();
2558        // decimal128 to decimal128
2559        let input_type = DataType::Decimal128(20, 4);
2560        let output_type = DataType::Decimal128(20, 3);
2561        assert!(can_cast_types(&input_type, &output_type));
2562        generate_cast_test_case!(
2563            &array,
2564            Decimal128Array,
2565            &output_type,
2566            vec![
2567                Some(112345_i128),
2568                Some(212346_i128),
2569                Some(-312345_i128),
2570                Some(-312346_i128),
2571                None
2572            ]
2573        );
2574
2575        // decimal128 to decimal256
2576        let input_type = DataType::Decimal128(20, 4);
2577        let output_type = DataType::Decimal256(20, 3);
2578        assert!(can_cast_types(&input_type, &output_type));
2579        generate_cast_test_case!(
2580            &array,
2581            Decimal256Array,
2582            &output_type,
2583            vec![
2584                Some(i256::from_i128(112345_i128)),
2585                Some(i256::from_i128(212346_i128)),
2586                Some(i256::from_i128(-312345_i128)),
2587                Some(i256::from_i128(-312346_i128)),
2588                None
2589            ]
2590        );
2591
2592        // decimal256
2593        let array = vec![
2594            Some(i256::from_i128(1123454)),
2595            Some(i256::from_i128(2123456)),
2596            Some(i256::from_i128(-3123453)),
2597            Some(i256::from_i128(-3123456)),
2598            None,
2599        ];
2600        let array = create_decimal256_array(array, 20, 4).unwrap();
2601
2602        // decimal256 to decimal256
2603        let input_type = DataType::Decimal256(20, 4);
2604        let output_type = DataType::Decimal256(20, 3);
2605        assert!(can_cast_types(&input_type, &output_type));
2606        generate_cast_test_case!(
2607            &array,
2608            Decimal256Array,
2609            &output_type,
2610            vec![
2611                Some(i256::from_i128(112345_i128)),
2612                Some(i256::from_i128(212346_i128)),
2613                Some(i256::from_i128(-312345_i128)),
2614                Some(i256::from_i128(-312346_i128)),
2615                None
2616            ]
2617        );
2618        // decimal256 to decimal128
2619        let input_type = DataType::Decimal256(20, 4);
2620        let output_type = DataType::Decimal128(20, 3);
2621        assert!(can_cast_types(&input_type, &output_type));
2622        generate_cast_test_case!(
2623            &array,
2624            Decimal128Array,
2625            &output_type,
2626            vec![
2627                Some(112345_i128),
2628                Some(212346_i128),
2629                Some(-312345_i128),
2630                Some(-312346_i128),
2631                None
2632            ]
2633        );
2634    }
2635
2636    #[test]
2637    fn test_cast_decimal128_to_decimal128() {
2638        let input_type = DataType::Decimal128(20, 3);
2639        let output_type = DataType::Decimal128(20, 4);
2640        assert!(can_cast_types(&input_type, &output_type));
2641        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2642        let array = create_decimal128_array(array, 20, 3).unwrap();
2643        generate_cast_test_case!(
2644            &array,
2645            Decimal128Array,
2646            &output_type,
2647            vec![
2648                Some(11234560_i128),
2649                Some(21234560_i128),
2650                Some(31234560_i128),
2651                None
2652            ]
2653        );
2654        // negative test
2655        let array = vec![Some(123456), None];
2656        let array = create_decimal128_array(array, 10, 0).unwrap();
2657        let result_safe = cast(&array, &DataType::Decimal128(2, 2));
2658        assert!(result_safe.is_ok());
2659        let options = CastOptions {
2660            safe: false,
2661            ..Default::default()
2662        };
2663
2664        let result_unsafe = cast_with_options(&array, &DataType::Decimal128(2, 2), &options);
2665        assert_eq!("Invalid argument error: 12345600 is too large to store in a Decimal128 of precision 2. Max is 99",
2666                   result_unsafe.unwrap_err().to_string());
2667    }
2668
2669    #[test]
2670    fn test_cast_decimal128_to_decimal128_dict() {
2671        let p = 20;
2672        let s = 3;
2673        let input_type = DataType::Decimal128(p, s);
2674        let output_type = DataType::Dictionary(
2675            Box::new(DataType::Int32),
2676            Box::new(DataType::Decimal128(p, s)),
2677        );
2678        assert!(can_cast_types(&input_type, &output_type));
2679        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2680        let array = create_decimal128_array(array, p, s).unwrap();
2681        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
2682        assert_eq!(cast_array.data_type(), &output_type);
2683    }
2684
2685    #[test]
2686    fn test_cast_decimal256_to_decimal256_dict() {
2687        let p = 20;
2688        let s = 3;
2689        let input_type = DataType::Decimal256(p, s);
2690        let output_type = DataType::Dictionary(
2691            Box::new(DataType::Int32),
2692            Box::new(DataType::Decimal256(p, s)),
2693        );
2694        assert!(can_cast_types(&input_type, &output_type));
2695        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2696        let array = create_decimal128_array(array, p, s).unwrap();
2697        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
2698        assert_eq!(cast_array.data_type(), &output_type);
2699    }
2700
2701    #[test]
2702    fn test_cast_decimal128_to_decimal128_overflow() {
2703        let input_type = DataType::Decimal128(38, 3);
2704        let output_type = DataType::Decimal128(38, 38);
2705        assert!(can_cast_types(&input_type, &output_type));
2706
2707        let array = vec![Some(i128::MAX)];
2708        let array = create_decimal128_array(array, 38, 3).unwrap();
2709        let result = cast_with_options(
2710            &array,
2711            &output_type,
2712            &CastOptions {
2713                safe: false,
2714                format_options: FormatOptions::default(),
2715            },
2716        );
2717        assert_eq!("Cast error: Cannot cast to Decimal128(38, 38). Overflowing on 170141183460469231731687303715884105727",
2718                   result.unwrap_err().to_string());
2719    }
2720
2721    #[test]
2722    fn test_cast_decimal128_to_decimal256_overflow() {
2723        let input_type = DataType::Decimal128(38, 3);
2724        let output_type = DataType::Decimal256(76, 76);
2725        assert!(can_cast_types(&input_type, &output_type));
2726
2727        let array = vec![Some(i128::MAX)];
2728        let array = create_decimal128_array(array, 38, 3).unwrap();
2729        let result = cast_with_options(
2730            &array,
2731            &output_type,
2732            &CastOptions {
2733                safe: false,
2734                format_options: FormatOptions::default(),
2735            },
2736        );
2737        assert_eq!("Cast error: Cannot cast to Decimal256(76, 76). Overflowing on 170141183460469231731687303715884105727",
2738                   result.unwrap_err().to_string());
2739    }
2740
2741    #[test]
2742    fn test_cast_decimal128_to_decimal256() {
2743        let input_type = DataType::Decimal128(20, 3);
2744        let output_type = DataType::Decimal256(20, 4);
2745        assert!(can_cast_types(&input_type, &output_type));
2746        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2747        let array = create_decimal128_array(array, 20, 3).unwrap();
2748        generate_cast_test_case!(
2749            &array,
2750            Decimal256Array,
2751            &output_type,
2752            vec![
2753                Some(i256::from_i128(11234560_i128)),
2754                Some(i256::from_i128(21234560_i128)),
2755                Some(i256::from_i128(31234560_i128)),
2756                None
2757            ]
2758        );
2759    }
2760
2761    #[test]
2762    fn test_cast_decimal256_to_decimal128_overflow() {
2763        let input_type = DataType::Decimal256(76, 5);
2764        let output_type = DataType::Decimal128(38, 7);
2765        assert!(can_cast_types(&input_type, &output_type));
2766        let array = vec![Some(i256::from_i128(i128::MAX))];
2767        let array = create_decimal256_array(array, 76, 5).unwrap();
2768        let result = cast_with_options(
2769            &array,
2770            &output_type,
2771            &CastOptions {
2772                safe: false,
2773                format_options: FormatOptions::default(),
2774            },
2775        );
2776        assert_eq!("Cast error: Cannot cast to Decimal128(38, 7). Overflowing on 170141183460469231731687303715884105727",
2777                   result.unwrap_err().to_string());
2778    }
2779
2780    #[test]
2781    fn test_cast_decimal256_to_decimal256_overflow() {
2782        let input_type = DataType::Decimal256(76, 5);
2783        let output_type = DataType::Decimal256(76, 55);
2784        assert!(can_cast_types(&input_type, &output_type));
2785        let array = vec![Some(i256::from_i128(i128::MAX))];
2786        let array = create_decimal256_array(array, 76, 5).unwrap();
2787        let result = cast_with_options(
2788            &array,
2789            &output_type,
2790            &CastOptions {
2791                safe: false,
2792                format_options: FormatOptions::default(),
2793            },
2794        );
2795        assert_eq!("Cast error: Cannot cast to Decimal256(76, 55). Overflowing on 170141183460469231731687303715884105727",
2796                   result.unwrap_err().to_string());
2797    }
2798
2799    #[test]
2800    fn test_cast_decimal256_to_decimal128() {
2801        let input_type = DataType::Decimal256(20, 3);
2802        let output_type = DataType::Decimal128(20, 4);
2803        assert!(can_cast_types(&input_type, &output_type));
2804        let array = vec![
2805            Some(i256::from_i128(1123456)),
2806            Some(i256::from_i128(2123456)),
2807            Some(i256::from_i128(3123456)),
2808            None,
2809        ];
2810        let array = create_decimal256_array(array, 20, 3).unwrap();
2811        generate_cast_test_case!(
2812            &array,
2813            Decimal128Array,
2814            &output_type,
2815            vec![
2816                Some(11234560_i128),
2817                Some(21234560_i128),
2818                Some(31234560_i128),
2819                None
2820            ]
2821        );
2822    }
2823
2824    #[test]
2825    fn test_cast_decimal256_to_decimal256() {
2826        let input_type = DataType::Decimal256(20, 3);
2827        let output_type = DataType::Decimal256(20, 4);
2828        assert!(can_cast_types(&input_type, &output_type));
2829        let array = vec![
2830            Some(i256::from_i128(1123456)),
2831            Some(i256::from_i128(2123456)),
2832            Some(i256::from_i128(3123456)),
2833            None,
2834        ];
2835        let array = create_decimal256_array(array, 20, 3).unwrap();
2836        generate_cast_test_case!(
2837            &array,
2838            Decimal256Array,
2839            &output_type,
2840            vec![
2841                Some(i256::from_i128(11234560_i128)),
2842                Some(i256::from_i128(21234560_i128)),
2843                Some(i256::from_i128(31234560_i128)),
2844                None
2845            ]
2846        );
2847    }
2848
2849    fn generate_decimal_to_numeric_cast_test_case<T>(array: &PrimitiveArray<T>)
2850    where
2851        T: ArrowPrimitiveType + DecimalType,
2852    {
2853        // u8
2854        generate_cast_test_case!(
2855            array,
2856            UInt8Array,
2857            &DataType::UInt8,
2858            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
2859        );
2860        // u16
2861        generate_cast_test_case!(
2862            array,
2863            UInt16Array,
2864            &DataType::UInt16,
2865            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
2866        );
2867        // u32
2868        generate_cast_test_case!(
2869            array,
2870            UInt32Array,
2871            &DataType::UInt32,
2872            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
2873        );
2874        // u64
2875        generate_cast_test_case!(
2876            array,
2877            UInt64Array,
2878            &DataType::UInt64,
2879            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
2880        );
2881        // i8
2882        generate_cast_test_case!(
2883            array,
2884            Int8Array,
2885            &DataType::Int8,
2886            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
2887        );
2888        // i16
2889        generate_cast_test_case!(
2890            array,
2891            Int16Array,
2892            &DataType::Int16,
2893            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
2894        );
2895        // i32
2896        generate_cast_test_case!(
2897            array,
2898            Int32Array,
2899            &DataType::Int32,
2900            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
2901        );
2902        // i64
2903        generate_cast_test_case!(
2904            array,
2905            Int64Array,
2906            &DataType::Int64,
2907            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
2908        );
2909        // f32
2910        generate_cast_test_case!(
2911            array,
2912            Float32Array,
2913            &DataType::Float32,
2914            vec![
2915                Some(1.25_f32),
2916                Some(2.25_f32),
2917                Some(3.25_f32),
2918                None,
2919                Some(5.25_f32)
2920            ]
2921        );
2922        // f64
2923        generate_cast_test_case!(
2924            array,
2925            Float64Array,
2926            &DataType::Float64,
2927            vec![
2928                Some(1.25_f64),
2929                Some(2.25_f64),
2930                Some(3.25_f64),
2931                None,
2932                Some(5.25_f64)
2933            ]
2934        );
2935    }
2936
2937    #[test]
2938    fn test_cast_decimal128_to_numeric() {
2939        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
2940        let array = create_decimal128_array(value_array, 38, 2).unwrap();
2941
2942        generate_decimal_to_numeric_cast_test_case(&array);
2943
2944        // overflow test: out of range of max u8
2945        let value_array: Vec<Option<i128>> = vec![Some(51300)];
2946        let array = create_decimal128_array(value_array, 38, 2).unwrap();
2947        let casted_array = cast_with_options(
2948            &array,
2949            &DataType::UInt8,
2950            &CastOptions {
2951                safe: false,
2952                format_options: FormatOptions::default(),
2953            },
2954        );
2955        assert_eq!(
2956            "Cast error: value of 513 is out of range UInt8".to_string(),
2957            casted_array.unwrap_err().to_string()
2958        );
2959
2960        let casted_array = cast_with_options(
2961            &array,
2962            &DataType::UInt8,
2963            &CastOptions {
2964                safe: true,
2965                format_options: FormatOptions::default(),
2966            },
2967        );
2968        assert!(casted_array.is_ok());
2969        assert!(casted_array.unwrap().is_null(0));
2970
2971        // overflow test: out of range of max i8
2972        let value_array: Vec<Option<i128>> = vec![Some(24400)];
2973        let array = create_decimal128_array(value_array, 38, 2).unwrap();
2974        let casted_array = cast_with_options(
2975            &array,
2976            &DataType::Int8,
2977            &CastOptions {
2978                safe: false,
2979                format_options: FormatOptions::default(),
2980            },
2981        );
2982        assert_eq!(
2983            "Cast error: value of 244 is out of range Int8".to_string(),
2984            casted_array.unwrap_err().to_string()
2985        );
2986
2987        let casted_array = cast_with_options(
2988            &array,
2989            &DataType::Int8,
2990            &CastOptions {
2991                safe: true,
2992                format_options: FormatOptions::default(),
2993            },
2994        );
2995        assert!(casted_array.is_ok());
2996        assert!(casted_array.unwrap().is_null(0));
2997
2998        // loss the precision: convert decimal to f32、f64
2999        // f32
3000        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3001        let value_array: Vec<Option<i128>> = vec![
3002            Some(125),
3003            Some(225),
3004            Some(325),
3005            None,
3006            Some(525),
3007            Some(112345678),
3008            Some(112345679),
3009        ];
3010        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3011        generate_cast_test_case!(
3012            &array,
3013            Float32Array,
3014            &DataType::Float32,
3015            vec![
3016                Some(1.25_f32),
3017                Some(2.25_f32),
3018                Some(3.25_f32),
3019                None,
3020                Some(5.25_f32),
3021                Some(1_123_456.7_f32),
3022                Some(1_123_456.7_f32)
3023            ]
3024        );
3025
3026        // f64
3027        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3028        let value_array: Vec<Option<i128>> = vec![
3029            Some(125),
3030            Some(225),
3031            Some(325),
3032            None,
3033            Some(525),
3034            Some(112345678901234568),
3035            Some(112345678901234560),
3036        ];
3037        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3038        generate_cast_test_case!(
3039            &array,
3040            Float64Array,
3041            &DataType::Float64,
3042            vec![
3043                Some(1.25_f64),
3044                Some(2.25_f64),
3045                Some(3.25_f64),
3046                None,
3047                Some(5.25_f64),
3048                Some(1_123_456_789_012_345.6_f64),
3049                Some(1_123_456_789_012_345.6_f64),
3050            ]
3051        );
3052    }
3053
3054    #[test]
3055    fn test_cast_decimal256_to_numeric() {
3056        let value_array: Vec<Option<i256>> = vec![
3057            Some(i256::from_i128(125)),
3058            Some(i256::from_i128(225)),
3059            Some(i256::from_i128(325)),
3060            None,
3061            Some(i256::from_i128(525)),
3062        ];
3063        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3064        // u8
3065        generate_cast_test_case!(
3066            &array,
3067            UInt8Array,
3068            &DataType::UInt8,
3069            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3070        );
3071        // u16
3072        generate_cast_test_case!(
3073            &array,
3074            UInt16Array,
3075            &DataType::UInt16,
3076            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3077        );
3078        // u32
3079        generate_cast_test_case!(
3080            &array,
3081            UInt32Array,
3082            &DataType::UInt32,
3083            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3084        );
3085        // u64
3086        generate_cast_test_case!(
3087            &array,
3088            UInt64Array,
3089            &DataType::UInt64,
3090            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3091        );
3092        // i8
3093        generate_cast_test_case!(
3094            &array,
3095            Int8Array,
3096            &DataType::Int8,
3097            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3098        );
3099        // i16
3100        generate_cast_test_case!(
3101            &array,
3102            Int16Array,
3103            &DataType::Int16,
3104            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3105        );
3106        // i32
3107        generate_cast_test_case!(
3108            &array,
3109            Int32Array,
3110            &DataType::Int32,
3111            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3112        );
3113        // i64
3114        generate_cast_test_case!(
3115            &array,
3116            Int64Array,
3117            &DataType::Int64,
3118            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3119        );
3120        // f32
3121        generate_cast_test_case!(
3122            &array,
3123            Float32Array,
3124            &DataType::Float32,
3125            vec![
3126                Some(1.25_f32),
3127                Some(2.25_f32),
3128                Some(3.25_f32),
3129                None,
3130                Some(5.25_f32)
3131            ]
3132        );
3133        // f64
3134        generate_cast_test_case!(
3135            &array,
3136            Float64Array,
3137            &DataType::Float64,
3138            vec![
3139                Some(1.25_f64),
3140                Some(2.25_f64),
3141                Some(3.25_f64),
3142                None,
3143                Some(5.25_f64)
3144            ]
3145        );
3146
3147        // overflow test: out of range of max i8
3148        let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128(24400))];
3149        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3150        let casted_array = cast_with_options(
3151            &array,
3152            &DataType::Int8,
3153            &CastOptions {
3154                safe: false,
3155                format_options: FormatOptions::default(),
3156            },
3157        );
3158        assert_eq!(
3159            "Cast error: value of 244 is out of range Int8".to_string(),
3160            casted_array.unwrap_err().to_string()
3161        );
3162
3163        let casted_array = cast_with_options(
3164            &array,
3165            &DataType::Int8,
3166            &CastOptions {
3167                safe: true,
3168                format_options: FormatOptions::default(),
3169            },
3170        );
3171        assert!(casted_array.is_ok());
3172        assert!(casted_array.unwrap().is_null(0));
3173
3174        // loss the precision: convert decimal to f32、f64
3175        // f32
3176        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3177        let value_array: Vec<Option<i256>> = vec![
3178            Some(i256::from_i128(125)),
3179            Some(i256::from_i128(225)),
3180            Some(i256::from_i128(325)),
3181            None,
3182            Some(i256::from_i128(525)),
3183            Some(i256::from_i128(112345678)),
3184            Some(i256::from_i128(112345679)),
3185        ];
3186        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3187        generate_cast_test_case!(
3188            &array,
3189            Float32Array,
3190            &DataType::Float32,
3191            vec![
3192                Some(1.25_f32),
3193                Some(2.25_f32),
3194                Some(3.25_f32),
3195                None,
3196                Some(5.25_f32),
3197                Some(1_123_456.7_f32),
3198                Some(1_123_456.7_f32)
3199            ]
3200        );
3201
3202        // f64
3203        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3204        let value_array: Vec<Option<i256>> = vec![
3205            Some(i256::from_i128(125)),
3206            Some(i256::from_i128(225)),
3207            Some(i256::from_i128(325)),
3208            None,
3209            Some(i256::from_i128(525)),
3210            Some(i256::from_i128(112345678901234568)),
3211            Some(i256::from_i128(112345678901234560)),
3212        ];
3213        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3214        generate_cast_test_case!(
3215            &array,
3216            Float64Array,
3217            &DataType::Float64,
3218            vec![
3219                Some(1.25_f64),
3220                Some(2.25_f64),
3221                Some(3.25_f64),
3222                None,
3223                Some(5.25_f64),
3224                Some(1_123_456_789_012_345.6_f64),
3225                Some(1_123_456_789_012_345.6_f64),
3226            ]
3227        );
3228    }
3229
3230    #[test]
3231    fn test_cast_numeric_to_decimal128() {
3232        let decimal_type = DataType::Decimal128(38, 6);
3233        // u8, u16, u32, u64
3234        let input_datas = vec![
3235            Arc::new(UInt8Array::from(vec![
3236                Some(1),
3237                Some(2),
3238                Some(3),
3239                None,
3240                Some(5),
3241            ])) as ArrayRef, // u8
3242            Arc::new(UInt16Array::from(vec![
3243                Some(1),
3244                Some(2),
3245                Some(3),
3246                None,
3247                Some(5),
3248            ])) as ArrayRef, // u16
3249            Arc::new(UInt32Array::from(vec![
3250                Some(1),
3251                Some(2),
3252                Some(3),
3253                None,
3254                Some(5),
3255            ])) as ArrayRef, // u32
3256            Arc::new(UInt64Array::from(vec![
3257                Some(1),
3258                Some(2),
3259                Some(3),
3260                None,
3261                Some(5),
3262            ])) as ArrayRef, // u64
3263        ];
3264
3265        for array in input_datas {
3266            generate_cast_test_case!(
3267                &array,
3268                Decimal128Array,
3269                &decimal_type,
3270                vec![
3271                    Some(1000000_i128),
3272                    Some(2000000_i128),
3273                    Some(3000000_i128),
3274                    None,
3275                    Some(5000000_i128)
3276                ]
3277            );
3278        }
3279
3280        // i8, i16, i32, i64
3281        let input_datas = vec![
3282            Arc::new(Int8Array::from(vec![
3283                Some(1),
3284                Some(2),
3285                Some(3),
3286                None,
3287                Some(5),
3288            ])) as ArrayRef, // i8
3289            Arc::new(Int16Array::from(vec![
3290                Some(1),
3291                Some(2),
3292                Some(3),
3293                None,
3294                Some(5),
3295            ])) as ArrayRef, // i16
3296            Arc::new(Int32Array::from(vec![
3297                Some(1),
3298                Some(2),
3299                Some(3),
3300                None,
3301                Some(5),
3302            ])) as ArrayRef, // i32
3303            Arc::new(Int64Array::from(vec![
3304                Some(1),
3305                Some(2),
3306                Some(3),
3307                None,
3308                Some(5),
3309            ])) as ArrayRef, // i64
3310        ];
3311        for array in input_datas {
3312            generate_cast_test_case!(
3313                &array,
3314                Decimal128Array,
3315                &decimal_type,
3316                vec![
3317                    Some(1000000_i128),
3318                    Some(2000000_i128),
3319                    Some(3000000_i128),
3320                    None,
3321                    Some(5000000_i128)
3322                ]
3323            );
3324        }
3325
3326        // test u8 to decimal type with overflow the result type
3327        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
3328        let array = UInt8Array::from(vec![1, 2, 3, 4, 100]);
3329        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
3330        assert!(casted_array.is_ok());
3331        let array = casted_array.unwrap();
3332        let array: &Decimal128Array = array.as_primitive();
3333        assert!(array.is_null(4));
3334
3335        // test i8 to decimal type with overflow the result type
3336        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
3337        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
3338        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
3339        assert!(casted_array.is_ok());
3340        let array = casted_array.unwrap();
3341        let array: &Decimal128Array = array.as_primitive();
3342        assert!(array.is_null(4));
3343
3344        // test f32 to decimal type
3345        let array = Float32Array::from(vec![
3346            Some(1.1),
3347            Some(2.2),
3348            Some(4.4),
3349            None,
3350            Some(1.123_456_4), // round down
3351            Some(1.123_456_7), // round up
3352        ]);
3353        let array = Arc::new(array) as ArrayRef;
3354        generate_cast_test_case!(
3355            &array,
3356            Decimal128Array,
3357            &decimal_type,
3358            vec![
3359                Some(1100000_i128),
3360                Some(2200000_i128),
3361                Some(4400000_i128),
3362                None,
3363                Some(1123456_i128), // round down
3364                Some(1123457_i128), // round up
3365            ]
3366        );
3367
3368        // test f64 to decimal type
3369        let array = Float64Array::from(vec![
3370            Some(1.1),
3371            Some(2.2),
3372            Some(4.4),
3373            None,
3374            Some(1.123_456_489_123_4),     // round up
3375            Some(1.123_456_789_123_4),     // round up
3376            Some(1.123_456_489_012_345_6), // round down
3377            Some(1.123_456_789_012_345_6), // round up
3378        ]);
3379        generate_cast_test_case!(
3380            &array,
3381            Decimal128Array,
3382            &decimal_type,
3383            vec![
3384                Some(1100000_i128),
3385                Some(2200000_i128),
3386                Some(4400000_i128),
3387                None,
3388                Some(1123456_i128), // round down
3389                Some(1123457_i128), // round up
3390                Some(1123456_i128), // round down
3391                Some(1123457_i128), // round up
3392            ]
3393        );
3394    }
3395
3396    #[test]
3397    fn test_cast_numeric_to_decimal256() {
3398        let decimal_type = DataType::Decimal256(76, 6);
3399        // u8, u16, u32, u64
3400        let input_datas = vec![
3401            Arc::new(UInt8Array::from(vec![
3402                Some(1),
3403                Some(2),
3404                Some(3),
3405                None,
3406                Some(5),
3407            ])) as ArrayRef, // u8
3408            Arc::new(UInt16Array::from(vec![
3409                Some(1),
3410                Some(2),
3411                Some(3),
3412                None,
3413                Some(5),
3414            ])) as ArrayRef, // u16
3415            Arc::new(UInt32Array::from(vec![
3416                Some(1),
3417                Some(2),
3418                Some(3),
3419                None,
3420                Some(5),
3421            ])) as ArrayRef, // u32
3422            Arc::new(UInt64Array::from(vec![
3423                Some(1),
3424                Some(2),
3425                Some(3),
3426                None,
3427                Some(5),
3428            ])) as ArrayRef, // u64
3429        ];
3430
3431        for array in input_datas {
3432            generate_cast_test_case!(
3433                &array,
3434                Decimal256Array,
3435                &decimal_type,
3436                vec![
3437                    Some(i256::from_i128(1000000_i128)),
3438                    Some(i256::from_i128(2000000_i128)),
3439                    Some(i256::from_i128(3000000_i128)),
3440                    None,
3441                    Some(i256::from_i128(5000000_i128))
3442                ]
3443            );
3444        }
3445
3446        // i8, i16, i32, i64
3447        let input_datas = vec![
3448            Arc::new(Int8Array::from(vec![
3449                Some(1),
3450                Some(2),
3451                Some(3),
3452                None,
3453                Some(5),
3454            ])) as ArrayRef, // i8
3455            Arc::new(Int16Array::from(vec![
3456                Some(1),
3457                Some(2),
3458                Some(3),
3459                None,
3460                Some(5),
3461            ])) as ArrayRef, // i16
3462            Arc::new(Int32Array::from(vec![
3463                Some(1),
3464                Some(2),
3465                Some(3),
3466                None,
3467                Some(5),
3468            ])) as ArrayRef, // i32
3469            Arc::new(Int64Array::from(vec![
3470                Some(1),
3471                Some(2),
3472                Some(3),
3473                None,
3474                Some(5),
3475            ])) as ArrayRef, // i64
3476        ];
3477        for array in input_datas {
3478            generate_cast_test_case!(
3479                &array,
3480                Decimal256Array,
3481                &decimal_type,
3482                vec![
3483                    Some(i256::from_i128(1000000_i128)),
3484                    Some(i256::from_i128(2000000_i128)),
3485                    Some(i256::from_i128(3000000_i128)),
3486                    None,
3487                    Some(i256::from_i128(5000000_i128))
3488                ]
3489            );
3490        }
3491
3492        // test i8 to decimal type with overflow the result type
3493        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
3494        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
3495        let array = Arc::new(array) as ArrayRef;
3496        let casted_array = cast(&array, &DataType::Decimal256(3, 1));
3497        assert!(casted_array.is_ok());
3498        let array = casted_array.unwrap();
3499        let array: &Decimal256Array = array.as_primitive();
3500        assert!(array.is_null(4));
3501
3502        // test f32 to decimal type
3503        let array = Float32Array::from(vec![
3504            Some(1.1),
3505            Some(2.2),
3506            Some(4.4),
3507            None,
3508            Some(1.123_456_4), // round down
3509            Some(1.123_456_7), // round up
3510        ]);
3511        generate_cast_test_case!(
3512            &array,
3513            Decimal256Array,
3514            &decimal_type,
3515            vec![
3516                Some(i256::from_i128(1100000_i128)),
3517                Some(i256::from_i128(2200000_i128)),
3518                Some(i256::from_i128(4400000_i128)),
3519                None,
3520                Some(i256::from_i128(1123456_i128)), // round down
3521                Some(i256::from_i128(1123457_i128)), // round up
3522            ]
3523        );
3524
3525        // test f64 to decimal type
3526        let array = Float64Array::from(vec![
3527            Some(1.1),
3528            Some(2.2),
3529            Some(4.4),
3530            None,
3531            Some(1.123_456_489_123_4),     // round down
3532            Some(1.123_456_789_123_4),     // round up
3533            Some(1.123_456_489_012_345_6), // round down
3534            Some(1.123_456_789_012_345_6), // round up
3535        ]);
3536        generate_cast_test_case!(
3537            &array,
3538            Decimal256Array,
3539            &decimal_type,
3540            vec![
3541                Some(i256::from_i128(1100000_i128)),
3542                Some(i256::from_i128(2200000_i128)),
3543                Some(i256::from_i128(4400000_i128)),
3544                None,
3545                Some(i256::from_i128(1123456_i128)), // round down
3546                Some(i256::from_i128(1123457_i128)), // round up
3547                Some(i256::from_i128(1123456_i128)), // round down
3548                Some(i256::from_i128(1123457_i128)), // round up
3549            ]
3550        );
3551    }
3552
3553    #[test]
3554    fn test_cast_i32_to_f64() {
3555        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
3556        let b = cast(&array, &DataType::Float64).unwrap();
3557        let c = b.as_primitive::<Float64Type>();
3558        assert_eq!(5.0, c.value(0));
3559        assert_eq!(6.0, c.value(1));
3560        assert_eq!(7.0, c.value(2));
3561        assert_eq!(8.0, c.value(3));
3562        assert_eq!(9.0, c.value(4));
3563    }
3564
3565    #[test]
3566    fn test_cast_i32_to_u8() {
3567        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
3568        let b = cast(&array, &DataType::UInt8).unwrap();
3569        let c = b.as_primitive::<UInt8Type>();
3570        assert!(!c.is_valid(0));
3571        assert_eq!(6, c.value(1));
3572        assert!(!c.is_valid(2));
3573        assert_eq!(8, c.value(3));
3574        // overflows return None
3575        assert!(!c.is_valid(4));
3576    }
3577
3578    #[test]
3579    #[should_panic(expected = "Can't cast value -5 to type UInt8")]
3580    fn test_cast_int32_to_u8_with_error() {
3581        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
3582        // overflow with the error
3583        let cast_option = CastOptions {
3584            safe: false,
3585            format_options: FormatOptions::default(),
3586        };
3587        let result = cast_with_options(&array, &DataType::UInt8, &cast_option);
3588        assert!(result.is_err());
3589        result.unwrap();
3590    }
3591
3592    #[test]
3593    fn test_cast_i32_to_u8_sliced() {
3594        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
3595        assert_eq!(0, array.offset());
3596        let array = array.slice(2, 3);
3597        let b = cast(&array, &DataType::UInt8).unwrap();
3598        assert_eq!(3, b.len());
3599        let c = b.as_primitive::<UInt8Type>();
3600        assert!(!c.is_valid(0));
3601        assert_eq!(8, c.value(1));
3602        // overflows return None
3603        assert!(!c.is_valid(2));
3604    }
3605
3606    #[test]
3607    fn test_cast_i32_to_i32() {
3608        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
3609        let b = cast(&array, &DataType::Int32).unwrap();
3610        let c = b.as_primitive::<Int32Type>();
3611        assert_eq!(5, c.value(0));
3612        assert_eq!(6, c.value(1));
3613        assert_eq!(7, c.value(2));
3614        assert_eq!(8, c.value(3));
3615        assert_eq!(9, c.value(4));
3616    }
3617
3618    #[test]
3619    fn test_cast_i32_to_list_i32() {
3620        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
3621        let b = cast(
3622            &array,
3623            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
3624        )
3625        .unwrap();
3626        assert_eq!(5, b.len());
3627        let arr = b.as_list::<i32>();
3628        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
3629        assert_eq!(1, arr.value_length(0));
3630        assert_eq!(1, arr.value_length(1));
3631        assert_eq!(1, arr.value_length(2));
3632        assert_eq!(1, arr.value_length(3));
3633        assert_eq!(1, arr.value_length(4));
3634        let c = arr.values().as_primitive::<Int32Type>();
3635        assert_eq!(5, c.value(0));
3636        assert_eq!(6, c.value(1));
3637        assert_eq!(7, c.value(2));
3638        assert_eq!(8, c.value(3));
3639        assert_eq!(9, c.value(4));
3640    }
3641
3642    #[test]
3643    fn test_cast_i32_to_list_i32_nullable() {
3644        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), Some(9)]);
3645        let b = cast(
3646            &array,
3647            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
3648        )
3649        .unwrap();
3650        assert_eq!(5, b.len());
3651        assert_eq!(0, b.null_count());
3652        let arr = b.as_list::<i32>();
3653        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
3654        assert_eq!(1, arr.value_length(0));
3655        assert_eq!(1, arr.value_length(1));
3656        assert_eq!(1, arr.value_length(2));
3657        assert_eq!(1, arr.value_length(3));
3658        assert_eq!(1, arr.value_length(4));
3659
3660        let c = arr.values().as_primitive::<Int32Type>();
3661        assert_eq!(1, c.null_count());
3662        assert_eq!(5, c.value(0));
3663        assert!(!c.is_valid(1));
3664        assert_eq!(7, c.value(2));
3665        assert_eq!(8, c.value(3));
3666        assert_eq!(9, c.value(4));
3667    }
3668
3669    #[test]
3670    fn test_cast_i32_to_list_f64_nullable_sliced() {
3671        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), None, Some(10)]);
3672        let array = array.slice(2, 4);
3673        let b = cast(
3674            &array,
3675            &DataType::List(Arc::new(Field::new_list_field(DataType::Float64, true))),
3676        )
3677        .unwrap();
3678        assert_eq!(4, b.len());
3679        assert_eq!(0, b.null_count());
3680        let arr = b.as_list::<i32>();
3681        assert_eq!(&[0, 1, 2, 3, 4], arr.value_offsets());
3682        assert_eq!(1, arr.value_length(0));
3683        assert_eq!(1, arr.value_length(1));
3684        assert_eq!(1, arr.value_length(2));
3685        assert_eq!(1, arr.value_length(3));
3686        let c = arr.values().as_primitive::<Float64Type>();
3687        assert_eq!(1, c.null_count());
3688        assert_eq!(7.0, c.value(0));
3689        assert_eq!(8.0, c.value(1));
3690        assert!(!c.is_valid(2));
3691        assert_eq!(10.0, c.value(3));
3692    }
3693
3694    #[test]
3695    fn test_cast_int_to_utf8view() {
3696        let inputs = vec![
3697            Arc::new(Int8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3698            Arc::new(Int16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3699            Arc::new(Int32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3700            Arc::new(Int64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3701            Arc::new(UInt8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3702            Arc::new(UInt16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3703            Arc::new(UInt32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3704            Arc::new(UInt64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3705        ];
3706        let expected: ArrayRef = Arc::new(StringViewArray::from(vec![
3707            None,
3708            Some("8"),
3709            Some("9"),
3710            Some("10"),
3711        ]));
3712
3713        for array in inputs {
3714            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
3715            let arr = cast(&array, &DataType::Utf8View).unwrap();
3716            assert_eq!(expected.as_ref(), arr.as_ref());
3717        }
3718    }
3719
3720    #[test]
3721    fn test_cast_float_to_utf8view() {
3722        let inputs = vec![
3723            Arc::new(Float16Array::from(vec![
3724                Some(f16::from_f64(1.5)),
3725                Some(f16::from_f64(2.5)),
3726                None,
3727            ])) as ArrayRef,
3728            Arc::new(Float32Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
3729            Arc::new(Float64Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
3730        ];
3731
3732        let expected: ArrayRef =
3733            Arc::new(StringViewArray::from(vec![Some("1.5"), Some("2.5"), None]));
3734
3735        for array in inputs {
3736            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
3737            let arr = cast(&array, &DataType::Utf8View).unwrap();
3738            assert_eq!(expected.as_ref(), arr.as_ref());
3739        }
3740    }
3741
3742    #[test]
3743    fn test_cast_utf8_to_i32() {
3744        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
3745        let b = cast(&array, &DataType::Int32).unwrap();
3746        let c = b.as_primitive::<Int32Type>();
3747        assert_eq!(5, c.value(0));
3748        assert_eq!(6, c.value(1));
3749        assert!(!c.is_valid(2));
3750        assert_eq!(8, c.value(3));
3751        assert!(!c.is_valid(4));
3752    }
3753
3754    #[test]
3755    fn test_cast_utf8view_to_i32() {
3756        let array = StringViewArray::from(vec!["5", "6", "seven", "8", "9.1"]);
3757        let b = cast(&array, &DataType::Int32).unwrap();
3758        let c = b.as_primitive::<Int32Type>();
3759        assert_eq!(5, c.value(0));
3760        assert_eq!(6, c.value(1));
3761        assert!(!c.is_valid(2));
3762        assert_eq!(8, c.value(3));
3763        assert!(!c.is_valid(4));
3764    }
3765
3766    #[test]
3767    fn test_cast_utf8view_to_f32() {
3768        let array = StringViewArray::from(vec!["3", "4.56", "seven", "8.9"]);
3769        let b = cast(&array, &DataType::Float32).unwrap();
3770        let c = b.as_primitive::<Float32Type>();
3771        assert_eq!(3.0, c.value(0));
3772        assert_eq!(4.56, c.value(1));
3773        assert!(!c.is_valid(2));
3774        assert_eq!(8.9, c.value(3));
3775    }
3776
3777    #[test]
3778    fn test_cast_utf8view_to_decimal128() {
3779        let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
3780        let arr = Arc::new(array) as ArrayRef;
3781        generate_cast_test_case!(
3782            &arr,
3783            Decimal128Array,
3784            &DataType::Decimal128(4, 2),
3785            vec![None, Some(400_i128), Some(560_i128), Some(789_i128)]
3786        );
3787    }
3788
3789    #[test]
3790    fn test_cast_with_options_utf8_to_i32() {
3791        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
3792        let result = cast_with_options(
3793            &array,
3794            &DataType::Int32,
3795            &CastOptions {
3796                safe: false,
3797                format_options: FormatOptions::default(),
3798            },
3799        );
3800        match result {
3801            Ok(_) => panic!("expected error"),
3802            Err(e) => {
3803                assert!(
3804                    e.to_string()
3805                        .contains("Cast error: Cannot cast string 'seven' to value of Int32 type",),
3806                    "Error: {e}"
3807                )
3808            }
3809        }
3810    }
3811
3812    #[test]
3813    fn test_cast_utf8_to_bool() {
3814        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
3815        let casted = cast(&strings, &DataType::Boolean).unwrap();
3816        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
3817        assert_eq!(*as_boolean_array(&casted), expected);
3818    }
3819
3820    #[test]
3821    fn test_cast_utf8view_to_bool() {
3822        let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
3823        let casted = cast(&strings, &DataType::Boolean).unwrap();
3824        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
3825        assert_eq!(*as_boolean_array(&casted), expected);
3826    }
3827
3828    #[test]
3829    fn test_cast_with_options_utf8_to_bool() {
3830        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
3831        let casted = cast_with_options(
3832            &strings,
3833            &DataType::Boolean,
3834            &CastOptions {
3835                safe: false,
3836                format_options: FormatOptions::default(),
3837            },
3838        );
3839        match casted {
3840            Ok(_) => panic!("expected error"),
3841            Err(e) => {
3842                assert!(e
3843                    .to_string()
3844                    .contains("Cast error: Cannot cast value 'invalid' to value of Boolean type"))
3845            }
3846        }
3847    }
3848
3849    #[test]
3850    fn test_cast_bool_to_i32() {
3851        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
3852        let b = cast(&array, &DataType::Int32).unwrap();
3853        let c = b.as_primitive::<Int32Type>();
3854        assert_eq!(1, c.value(0));
3855        assert_eq!(0, c.value(1));
3856        assert!(!c.is_valid(2));
3857    }
3858
3859    #[test]
3860    fn test_cast_bool_to_utf8view() {
3861        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
3862        let b = cast(&array, &DataType::Utf8View).unwrap();
3863        let c = b.as_any().downcast_ref::<StringViewArray>().unwrap();
3864        assert_eq!("true", c.value(0));
3865        assert_eq!("false", c.value(1));
3866        assert!(!c.is_valid(2));
3867    }
3868
3869    #[test]
3870    fn test_cast_bool_to_utf8() {
3871        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
3872        let b = cast(&array, &DataType::Utf8).unwrap();
3873        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
3874        assert_eq!("true", c.value(0));
3875        assert_eq!("false", c.value(1));
3876        assert!(!c.is_valid(2));
3877    }
3878
3879    #[test]
3880    fn test_cast_bool_to_large_utf8() {
3881        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
3882        let b = cast(&array, &DataType::LargeUtf8).unwrap();
3883        let c = b.as_any().downcast_ref::<LargeStringArray>().unwrap();
3884        assert_eq!("true", c.value(0));
3885        assert_eq!("false", c.value(1));
3886        assert!(!c.is_valid(2));
3887    }
3888
3889    #[test]
3890    fn test_cast_bool_to_f64() {
3891        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
3892        let b = cast(&array, &DataType::Float64).unwrap();
3893        let c = b.as_primitive::<Float64Type>();
3894        assert_eq!(1.0, c.value(0));
3895        assert_eq!(0.0, c.value(1));
3896        assert!(!c.is_valid(2));
3897    }
3898
3899    #[test]
3900    fn test_cast_integer_to_timestamp() {
3901        let array = Int64Array::from(vec![Some(2), Some(10), None]);
3902        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3903
3904        let array = Int8Array::from(vec![Some(2), Some(10), None]);
3905        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3906
3907        assert_eq!(&actual, &expected);
3908
3909        let array = Int16Array::from(vec![Some(2), Some(10), None]);
3910        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3911
3912        assert_eq!(&actual, &expected);
3913
3914        let array = Int32Array::from(vec![Some(2), Some(10), None]);
3915        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3916
3917        assert_eq!(&actual, &expected);
3918
3919        let array = UInt8Array::from(vec![Some(2), Some(10), None]);
3920        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3921
3922        assert_eq!(&actual, &expected);
3923
3924        let array = UInt16Array::from(vec![Some(2), Some(10), None]);
3925        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3926
3927        assert_eq!(&actual, &expected);
3928
3929        let array = UInt32Array::from(vec![Some(2), Some(10), None]);
3930        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3931
3932        assert_eq!(&actual, &expected);
3933
3934        let array = UInt64Array::from(vec![Some(2), Some(10), None]);
3935        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3936
3937        assert_eq!(&actual, &expected);
3938    }
3939
3940    #[test]
3941    fn test_cast_timestamp_to_integer() {
3942        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
3943            .with_timezone("UTC".to_string());
3944        let expected = cast(&array, &DataType::Int64).unwrap();
3945
3946        let actual = cast(&cast(&array, &DataType::Int8).unwrap(), &DataType::Int64).unwrap();
3947        assert_eq!(&actual, &expected);
3948
3949        let actual = cast(&cast(&array, &DataType::Int16).unwrap(), &DataType::Int64).unwrap();
3950        assert_eq!(&actual, &expected);
3951
3952        let actual = cast(&cast(&array, &DataType::Int32).unwrap(), &DataType::Int64).unwrap();
3953        assert_eq!(&actual, &expected);
3954
3955        let actual = cast(&cast(&array, &DataType::UInt8).unwrap(), &DataType::Int64).unwrap();
3956        assert_eq!(&actual, &expected);
3957
3958        let actual = cast(&cast(&array, &DataType::UInt16).unwrap(), &DataType::Int64).unwrap();
3959        assert_eq!(&actual, &expected);
3960
3961        let actual = cast(&cast(&array, &DataType::UInt32).unwrap(), &DataType::Int64).unwrap();
3962        assert_eq!(&actual, &expected);
3963
3964        let actual = cast(&cast(&array, &DataType::UInt64).unwrap(), &DataType::Int64).unwrap();
3965        assert_eq!(&actual, &expected);
3966    }
3967
3968    #[test]
3969    fn test_cast_floating_to_timestamp() {
3970        let array = Int64Array::from(vec![Some(2), Some(10), None]);
3971        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3972
3973        let array = Float16Array::from(vec![
3974            Some(f16::from_f32(2.0)),
3975            Some(f16::from_f32(10.6)),
3976            None,
3977        ]);
3978        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3979
3980        assert_eq!(&actual, &expected);
3981
3982        let array = Float32Array::from(vec![Some(2.0), Some(10.6), None]);
3983        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3984
3985        assert_eq!(&actual, &expected);
3986
3987        let array = Float64Array::from(vec![Some(2.1), Some(10.2), None]);
3988        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3989
3990        assert_eq!(&actual, &expected);
3991    }
3992
3993    #[test]
3994    fn test_cast_timestamp_to_floating() {
3995        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
3996            .with_timezone("UTC".to_string());
3997        let expected = cast(&array, &DataType::Int64).unwrap();
3998
3999        let actual = cast(&cast(&array, &DataType::Float16).unwrap(), &DataType::Int64).unwrap();
4000        assert_eq!(&actual, &expected);
4001
4002        let actual = cast(&cast(&array, &DataType::Float32).unwrap(), &DataType::Int64).unwrap();
4003        assert_eq!(&actual, &expected);
4004
4005        let actual = cast(&cast(&array, &DataType::Float64).unwrap(), &DataType::Int64).unwrap();
4006        assert_eq!(&actual, &expected);
4007    }
4008
4009    #[test]
4010    fn test_cast_decimal_to_timestamp() {
4011        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4012        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4013
4014        let array = Decimal128Array::from(vec![Some(200), Some(1000), None])
4015            .with_precision_and_scale(4, 2)
4016            .unwrap();
4017        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4018
4019        assert_eq!(&actual, &expected);
4020
4021        let array = Decimal256Array::from(vec![
4022            Some(i256::from_i128(2000)),
4023            Some(i256::from_i128(10000)),
4024            None,
4025        ])
4026        .with_precision_and_scale(5, 3)
4027        .unwrap();
4028        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4029
4030        assert_eq!(&actual, &expected);
4031    }
4032
4033    #[test]
4034    fn test_cast_timestamp_to_decimal() {
4035        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4036            .with_timezone("UTC".to_string());
4037        let expected = cast(&array, &DataType::Int64).unwrap();
4038
4039        let actual = cast(
4040            &cast(&array, &DataType::Decimal128(5, 2)).unwrap(),
4041            &DataType::Int64,
4042        )
4043        .unwrap();
4044        assert_eq!(&actual, &expected);
4045
4046        let actual = cast(
4047            &cast(&array, &DataType::Decimal256(10, 5)).unwrap(),
4048            &DataType::Int64,
4049        )
4050        .unwrap();
4051        assert_eq!(&actual, &expected);
4052    }
4053
4054    #[test]
4055    fn test_cast_list_i32_to_list_u16() {
4056        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 100000000]).into_data();
4057
4058        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
4059
4060        // Construct a list array from the above two
4061        // [[0,0,0], [-1, -2, -1], [2, 100000000]]
4062        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
4063        let list_data = ArrayData::builder(list_data_type)
4064            .len(3)
4065            .add_buffer(value_offsets)
4066            .add_child_data(value_data)
4067            .build()
4068            .unwrap();
4069        let list_array = ListArray::from(list_data);
4070
4071        let cast_array = cast(
4072            &list_array,
4073            &DataType::List(Arc::new(Field::new_list_field(DataType::UInt16, true))),
4074        )
4075        .unwrap();
4076
4077        // For the ListArray itself, there are no null values (as there were no nulls when they went in)
4078        //
4079        // 3 negative values should get lost when casting to unsigned,
4080        // 1 value should overflow
4081        assert_eq!(0, cast_array.null_count());
4082
4083        // offsets should be the same
4084        let array = cast_array.as_list::<i32>();
4085        assert_eq!(list_array.value_offsets(), array.value_offsets());
4086
4087        assert_eq!(DataType::UInt16, array.value_type());
4088        assert_eq!(3, array.value_length(0));
4089        assert_eq!(3, array.value_length(1));
4090        assert_eq!(2, array.value_length(2));
4091
4092        // expect 4 nulls: negative numbers and overflow
4093        let u16arr = array.values().as_primitive::<UInt16Type>();
4094        assert_eq!(4, u16arr.null_count());
4095
4096        // expect 4 nulls: negative numbers and overflow
4097        let expected: UInt16Array =
4098            vec![Some(0), Some(0), Some(0), None, None, None, Some(2), None]
4099                .into_iter()
4100                .collect();
4101
4102        assert_eq!(u16arr, &expected);
4103    }
4104
4105    #[test]
4106    fn test_cast_list_i32_to_list_timestamp() {
4107        // Construct a value array
4108        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 8, 100000000]).into_data();
4109
4110        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 9]);
4111
4112        // Construct a list array from the above two
4113        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
4114        let list_data = ArrayData::builder(list_data_type)
4115            .len(3)
4116            .add_buffer(value_offsets)
4117            .add_child_data(value_data)
4118            .build()
4119            .unwrap();
4120        let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
4121
4122        let actual = cast(
4123            &list_array,
4124            &DataType::List(Arc::new(Field::new_list_field(
4125                DataType::Timestamp(TimeUnit::Microsecond, None),
4126                true,
4127            ))),
4128        )
4129        .unwrap();
4130
4131        let expected = cast(
4132            &cast(
4133                &list_array,
4134                &DataType::List(Arc::new(Field::new_list_field(DataType::Int64, true))),
4135            )
4136            .unwrap(),
4137            &DataType::List(Arc::new(Field::new_list_field(
4138                DataType::Timestamp(TimeUnit::Microsecond, None),
4139                true,
4140            ))),
4141        )
4142        .unwrap();
4143
4144        assert_eq!(&actual, &expected);
4145    }
4146
4147    #[test]
4148    fn test_cast_date32_to_date64() {
4149        let a = Date32Array::from(vec![10000, 17890]);
4150        let array = Arc::new(a) as ArrayRef;
4151        let b = cast(&array, &DataType::Date64).unwrap();
4152        let c = b.as_primitive::<Date64Type>();
4153        assert_eq!(864000000000, c.value(0));
4154        assert_eq!(1545696000000, c.value(1));
4155    }
4156
4157    #[test]
4158    fn test_cast_date64_to_date32() {
4159        let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
4160        let array = Arc::new(a) as ArrayRef;
4161        let b = cast(&array, &DataType::Date32).unwrap();
4162        let c = b.as_primitive::<Date32Type>();
4163        assert_eq!(10000, c.value(0));
4164        assert_eq!(17890, c.value(1));
4165        assert!(c.is_null(2));
4166    }
4167
4168    #[test]
4169    fn test_cast_string_to_integral_overflow() {
4170        let str = Arc::new(StringArray::from(vec![
4171            Some("123"),
4172            Some("-123"),
4173            Some("86374"),
4174            None,
4175        ])) as ArrayRef;
4176
4177        let options = CastOptions {
4178            safe: true,
4179            format_options: FormatOptions::default(),
4180        };
4181        let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
4182        let expected =
4183            Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
4184        assert_eq!(&res, &expected);
4185    }
4186
4187    #[test]
4188    fn test_cast_string_to_timestamp() {
4189        let a0 = Arc::new(StringViewArray::from(vec![
4190            Some("2020-09-08T12:00:00.123456789+00:00"),
4191            Some("Not a valid date"),
4192            None,
4193        ])) as ArrayRef;
4194        let a1 = Arc::new(StringArray::from(vec![
4195            Some("2020-09-08T12:00:00.123456789+00:00"),
4196            Some("Not a valid date"),
4197            None,
4198        ])) as ArrayRef;
4199        let a2 = Arc::new(LargeStringArray::from(vec![
4200            Some("2020-09-08T12:00:00.123456789+00:00"),
4201            Some("Not a valid date"),
4202            None,
4203        ])) as ArrayRef;
4204        for array in &[a0, a1, a2] {
4205            for time_unit in &[
4206                TimeUnit::Second,
4207                TimeUnit::Millisecond,
4208                TimeUnit::Microsecond,
4209                TimeUnit::Nanosecond,
4210            ] {
4211                let to_type = DataType::Timestamp(*time_unit, None);
4212                let b = cast(array, &to_type).unwrap();
4213
4214                match time_unit {
4215                    TimeUnit::Second => {
4216                        let c = b.as_primitive::<TimestampSecondType>();
4217                        assert_eq!(1599566400, c.value(0));
4218                        assert!(c.is_null(1));
4219                        assert!(c.is_null(2));
4220                    }
4221                    TimeUnit::Millisecond => {
4222                        let c = b
4223                            .as_any()
4224                            .downcast_ref::<TimestampMillisecondArray>()
4225                            .unwrap();
4226                        assert_eq!(1599566400123, c.value(0));
4227                        assert!(c.is_null(1));
4228                        assert!(c.is_null(2));
4229                    }
4230                    TimeUnit::Microsecond => {
4231                        let c = b
4232                            .as_any()
4233                            .downcast_ref::<TimestampMicrosecondArray>()
4234                            .unwrap();
4235                        assert_eq!(1599566400123456, c.value(0));
4236                        assert!(c.is_null(1));
4237                        assert!(c.is_null(2));
4238                    }
4239                    TimeUnit::Nanosecond => {
4240                        let c = b
4241                            .as_any()
4242                            .downcast_ref::<TimestampNanosecondArray>()
4243                            .unwrap();
4244                        assert_eq!(1599566400123456789, c.value(0));
4245                        assert!(c.is_null(1));
4246                        assert!(c.is_null(2));
4247                    }
4248                }
4249
4250                let options = CastOptions {
4251                    safe: false,
4252                    format_options: FormatOptions::default(),
4253                };
4254                let err = cast_with_options(array, &to_type, &options).unwrap_err();
4255                assert_eq!(
4256                    err.to_string(),
4257                    "Parser error: Error parsing timestamp from 'Not a valid date': error parsing date"
4258                );
4259            }
4260        }
4261    }
4262
4263    #[test]
4264    fn test_cast_string_to_timestamp_overflow() {
4265        let array = StringArray::from(vec!["9800-09-08T12:00:00.123456789"]);
4266        let result = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
4267        let result = result.as_primitive::<TimestampSecondType>();
4268        assert_eq!(result.values(), &[247112596800]);
4269    }
4270
4271    #[test]
4272    fn test_cast_string_to_date32() {
4273        let a0 = Arc::new(StringViewArray::from(vec![
4274            Some("2018-12-25"),
4275            Some("Not a valid date"),
4276            None,
4277        ])) as ArrayRef;
4278        let a1 = Arc::new(StringArray::from(vec![
4279            Some("2018-12-25"),
4280            Some("Not a valid date"),
4281            None,
4282        ])) as ArrayRef;
4283        let a2 = Arc::new(LargeStringArray::from(vec![
4284            Some("2018-12-25"),
4285            Some("Not a valid date"),
4286            None,
4287        ])) as ArrayRef;
4288        for array in &[a0, a1, a2] {
4289            let to_type = DataType::Date32;
4290            let b = cast(array, &to_type).unwrap();
4291            let c = b.as_primitive::<Date32Type>();
4292            assert_eq!(17890, c.value(0));
4293            assert!(c.is_null(1));
4294            assert!(c.is_null(2));
4295
4296            let options = CastOptions {
4297                safe: false,
4298                format_options: FormatOptions::default(),
4299            };
4300            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4301            assert_eq!(
4302                err.to_string(),
4303                "Cast error: Cannot cast string 'Not a valid date' to value of Date32 type"
4304            );
4305        }
4306    }
4307
4308    #[test]
4309    fn test_cast_string_with_large_date_to_date32() {
4310        let array = Arc::new(StringArray::from(vec![
4311            Some("+10999-12-31"),
4312            Some("-0010-02-28"),
4313            Some("0010-02-28"),
4314            Some("0000-01-01"),
4315            Some("-0000-01-01"),
4316            Some("-0001-01-01"),
4317        ])) as ArrayRef;
4318        let to_type = DataType::Date32;
4319        let options = CastOptions {
4320            safe: false,
4321            format_options: FormatOptions::default(),
4322        };
4323        let b = cast_with_options(&array, &to_type, &options).unwrap();
4324        let c = b.as_primitive::<Date32Type>();
4325        assert_eq!(3298139, c.value(0)); // 10999-12-31
4326        assert_eq!(-723122, c.value(1)); // -0010-02-28
4327        assert_eq!(-715817, c.value(2)); // 0010-02-28
4328        assert_eq!(c.value(3), c.value(4)); // Expect 0000-01-01 and -0000-01-01 to be parsed the same
4329        assert_eq!(-719528, c.value(3)); // 0000-01-01
4330        assert_eq!(-719528, c.value(4)); // -0000-01-01
4331        assert_eq!(-719893, c.value(5)); // -0001-01-01
4332    }
4333
4334    #[test]
4335    fn test_cast_invalid_string_with_large_date_to_date32() {
4336        // Large dates need to be prefixed with a + or - sign, otherwise they are not parsed correctly
4337        let array = Arc::new(StringArray::from(vec![Some("10999-12-31")])) as ArrayRef;
4338        let to_type = DataType::Date32;
4339        let options = CastOptions {
4340            safe: false,
4341            format_options: FormatOptions::default(),
4342        };
4343        let err = cast_with_options(&array, &to_type, &options).unwrap_err();
4344        assert_eq!(
4345            err.to_string(),
4346            "Cast error: Cannot cast string '10999-12-31' to value of Date32 type"
4347        );
4348    }
4349
4350    #[test]
4351    fn test_cast_string_format_yyyymmdd_to_date32() {
4352        let a0 = Arc::new(StringViewArray::from(vec![
4353            Some("2020-12-25"),
4354            Some("20201117"),
4355        ])) as ArrayRef;
4356        let a1 = Arc::new(StringArray::from(vec![
4357            Some("2020-12-25"),
4358            Some("20201117"),
4359        ])) as ArrayRef;
4360        let a2 = Arc::new(LargeStringArray::from(vec![
4361            Some("2020-12-25"),
4362            Some("20201117"),
4363        ])) as ArrayRef;
4364
4365        for array in &[a0, a1, a2] {
4366            let to_type = DataType::Date32;
4367            let options = CastOptions {
4368                safe: false,
4369                format_options: FormatOptions::default(),
4370            };
4371            let result = cast_with_options(&array, &to_type, &options).unwrap();
4372            let c = result.as_primitive::<Date32Type>();
4373            assert_eq!(
4374                chrono::NaiveDate::from_ymd_opt(2020, 12, 25),
4375                c.value_as_date(0)
4376            );
4377            assert_eq!(
4378                chrono::NaiveDate::from_ymd_opt(2020, 11, 17),
4379                c.value_as_date(1)
4380            );
4381        }
4382    }
4383
4384    #[test]
4385    fn test_cast_string_to_time32second() {
4386        let a0 = Arc::new(StringViewArray::from(vec![
4387            Some("08:08:35.091323414"),
4388            Some("08:08:60.091323414"), // leap second
4389            Some("08:08:61.091323414"), // not valid
4390            Some("Not a valid time"),
4391            None,
4392        ])) as ArrayRef;
4393        let a1 = Arc::new(StringArray::from(vec![
4394            Some("08:08:35.091323414"),
4395            Some("08:08:60.091323414"), // leap second
4396            Some("08:08:61.091323414"), // not valid
4397            Some("Not a valid time"),
4398            None,
4399        ])) as ArrayRef;
4400        let a2 = Arc::new(LargeStringArray::from(vec![
4401            Some("08:08:35.091323414"),
4402            Some("08:08:60.091323414"), // leap second
4403            Some("08:08:61.091323414"), // not valid
4404            Some("Not a valid time"),
4405            None,
4406        ])) as ArrayRef;
4407        for array in &[a0, a1, a2] {
4408            let to_type = DataType::Time32(TimeUnit::Second);
4409            let b = cast(array, &to_type).unwrap();
4410            let c = b.as_primitive::<Time32SecondType>();
4411            assert_eq!(29315, c.value(0));
4412            assert_eq!(29340, c.value(1));
4413            assert!(c.is_null(2));
4414            assert!(c.is_null(3));
4415            assert!(c.is_null(4));
4416
4417            let options = CastOptions {
4418                safe: false,
4419                format_options: FormatOptions::default(),
4420            };
4421            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4422            assert_eq!(err.to_string(), "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(Second) type");
4423        }
4424    }
4425
4426    #[test]
4427    fn test_cast_string_to_time32millisecond() {
4428        let a0 = Arc::new(StringViewArray::from(vec![
4429            Some("08:08:35.091323414"),
4430            Some("08:08:60.091323414"), // leap second
4431            Some("08:08:61.091323414"), // not valid
4432            Some("Not a valid time"),
4433            None,
4434        ])) as ArrayRef;
4435        let a1 = Arc::new(StringArray::from(vec![
4436            Some("08:08:35.091323414"),
4437            Some("08:08:60.091323414"), // leap second
4438            Some("08:08:61.091323414"), // not valid
4439            Some("Not a valid time"),
4440            None,
4441        ])) as ArrayRef;
4442        let a2 = Arc::new(LargeStringArray::from(vec![
4443            Some("08:08:35.091323414"),
4444            Some("08:08:60.091323414"), // leap second
4445            Some("08:08:61.091323414"), // not valid
4446            Some("Not a valid time"),
4447            None,
4448        ])) as ArrayRef;
4449        for array in &[a0, a1, a2] {
4450            let to_type = DataType::Time32(TimeUnit::Millisecond);
4451            let b = cast(array, &to_type).unwrap();
4452            let c = b.as_primitive::<Time32MillisecondType>();
4453            assert_eq!(29315091, c.value(0));
4454            assert_eq!(29340091, c.value(1));
4455            assert!(c.is_null(2));
4456            assert!(c.is_null(3));
4457            assert!(c.is_null(4));
4458
4459            let options = CastOptions {
4460                safe: false,
4461                format_options: FormatOptions::default(),
4462            };
4463            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4464            assert_eq!(err.to_string(), "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(Millisecond) type");
4465        }
4466    }
4467
4468    #[test]
4469    fn test_cast_string_to_time64microsecond() {
4470        let a0 = Arc::new(StringViewArray::from(vec![
4471            Some("08:08:35.091323414"),
4472            Some("Not a valid time"),
4473            None,
4474        ])) as ArrayRef;
4475        let a1 = Arc::new(StringArray::from(vec![
4476            Some("08:08:35.091323414"),
4477            Some("Not a valid time"),
4478            None,
4479        ])) as ArrayRef;
4480        let a2 = Arc::new(LargeStringArray::from(vec![
4481            Some("08:08:35.091323414"),
4482            Some("Not a valid time"),
4483            None,
4484        ])) as ArrayRef;
4485        for array in &[a0, a1, a2] {
4486            let to_type = DataType::Time64(TimeUnit::Microsecond);
4487            let b = cast(array, &to_type).unwrap();
4488            let c = b.as_primitive::<Time64MicrosecondType>();
4489            assert_eq!(29315091323, c.value(0));
4490            assert!(c.is_null(1));
4491            assert!(c.is_null(2));
4492
4493            let options = CastOptions {
4494                safe: false,
4495                format_options: FormatOptions::default(),
4496            };
4497            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4498            assert_eq!(err.to_string(), "Cast error: Cannot cast string 'Not a valid time' to value of Time64(Microsecond) type");
4499        }
4500    }
4501
4502    #[test]
4503    fn test_cast_string_to_time64nanosecond() {
4504        let a0 = Arc::new(StringViewArray::from(vec![
4505            Some("08:08:35.091323414"),
4506            Some("Not a valid time"),
4507            None,
4508        ])) as ArrayRef;
4509        let a1 = Arc::new(StringArray::from(vec![
4510            Some("08:08:35.091323414"),
4511            Some("Not a valid time"),
4512            None,
4513        ])) as ArrayRef;
4514        let a2 = Arc::new(LargeStringArray::from(vec![
4515            Some("08:08:35.091323414"),
4516            Some("Not a valid time"),
4517            None,
4518        ])) as ArrayRef;
4519        for array in &[a0, a1, a2] {
4520            let to_type = DataType::Time64(TimeUnit::Nanosecond);
4521            let b = cast(array, &to_type).unwrap();
4522            let c = b.as_primitive::<Time64NanosecondType>();
4523            assert_eq!(29315091323414, c.value(0));
4524            assert!(c.is_null(1));
4525            assert!(c.is_null(2));
4526
4527            let options = CastOptions {
4528                safe: false,
4529                format_options: FormatOptions::default(),
4530            };
4531            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4532            assert_eq!(err.to_string(), "Cast error: Cannot cast string 'Not a valid time' to value of Time64(Nanosecond) type");
4533        }
4534    }
4535
4536    #[test]
4537    fn test_cast_string_to_date64() {
4538        let a0 = Arc::new(StringViewArray::from(vec![
4539            Some("2020-09-08T12:00:00"),
4540            Some("Not a valid date"),
4541            None,
4542        ])) as ArrayRef;
4543        let a1 = Arc::new(StringArray::from(vec![
4544            Some("2020-09-08T12:00:00"),
4545            Some("Not a valid date"),
4546            None,
4547        ])) as ArrayRef;
4548        let a2 = Arc::new(LargeStringArray::from(vec![
4549            Some("2020-09-08T12:00:00"),
4550            Some("Not a valid date"),
4551            None,
4552        ])) as ArrayRef;
4553        for array in &[a0, a1, a2] {
4554            let to_type = DataType::Date64;
4555            let b = cast(array, &to_type).unwrap();
4556            let c = b.as_primitive::<Date64Type>();
4557            assert_eq!(1599566400000, c.value(0));
4558            assert!(c.is_null(1));
4559            assert!(c.is_null(2));
4560
4561            let options = CastOptions {
4562                safe: false,
4563                format_options: FormatOptions::default(),
4564            };
4565            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4566            assert_eq!(
4567                err.to_string(),
4568                "Cast error: Cannot cast string 'Not a valid date' to value of Date64 type"
4569            );
4570        }
4571    }
4572
4573    macro_rules! test_safe_string_to_interval {
4574        ($data_vec:expr, $interval_unit:expr, $array_ty:ty, $expect_vec:expr) => {
4575            let source_string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
4576
4577            let options = CastOptions {
4578                safe: true,
4579                format_options: FormatOptions::default(),
4580            };
4581
4582            let target_interval_array = cast_with_options(
4583                &source_string_array.clone(),
4584                &DataType::Interval($interval_unit),
4585                &options,
4586            )
4587            .unwrap()
4588            .as_any()
4589            .downcast_ref::<$array_ty>()
4590            .unwrap()
4591            .clone() as $array_ty;
4592
4593            let target_string_array =
4594                cast_with_options(&target_interval_array, &DataType::Utf8, &options)
4595                    .unwrap()
4596                    .as_any()
4597                    .downcast_ref::<StringArray>()
4598                    .unwrap()
4599                    .clone();
4600
4601            let expect_string_array = StringArray::from($expect_vec);
4602
4603            assert_eq!(target_string_array, expect_string_array);
4604
4605            let target_large_string_array =
4606                cast_with_options(&target_interval_array, &DataType::LargeUtf8, &options)
4607                    .unwrap()
4608                    .as_any()
4609                    .downcast_ref::<LargeStringArray>()
4610                    .unwrap()
4611                    .clone();
4612
4613            let expect_large_string_array = LargeStringArray::from($expect_vec);
4614
4615            assert_eq!(target_large_string_array, expect_large_string_array);
4616        };
4617    }
4618
4619    #[test]
4620    fn test_cast_string_to_interval_year_month() {
4621        test_safe_string_to_interval!(
4622            vec![
4623                Some("1 year 1 month"),
4624                Some("1.5 years 13 month"),
4625                Some("30 days"),
4626                Some("31 days"),
4627                Some("2 months 31 days"),
4628                Some("2 months 31 days 1 second"),
4629                Some("foobar"),
4630            ],
4631            IntervalUnit::YearMonth,
4632            IntervalYearMonthArray,
4633            vec![
4634                Some("1 years 1 mons"),
4635                Some("2 years 7 mons"),
4636                None,
4637                None,
4638                None,
4639                None,
4640                None,
4641            ]
4642        );
4643    }
4644
4645    #[test]
4646    fn test_cast_string_to_interval_day_time() {
4647        test_safe_string_to_interval!(
4648            vec![
4649                Some("1 year 1 month"),
4650                Some("1.5 years 13 month"),
4651                Some("30 days"),
4652                Some("1 day 2 second 3.5 milliseconds"),
4653                Some("foobar"),
4654            ],
4655            IntervalUnit::DayTime,
4656            IntervalDayTimeArray,
4657            vec![
4658                Some("390 days"),
4659                Some("930 days"),
4660                Some("30 days"),
4661                None,
4662                None,
4663            ]
4664        );
4665    }
4666
4667    #[test]
4668    fn test_cast_string_to_interval_month_day_nano() {
4669        test_safe_string_to_interval!(
4670            vec![
4671                Some("1 year 1 month 1 day"),
4672                None,
4673                Some("1.5 years 13 month 35 days 1.4 milliseconds"),
4674                Some("3 days"),
4675                Some("8 seconds"),
4676                None,
4677                Some("1 day 29800 milliseconds"),
4678                Some("3 months 1 second"),
4679                Some("6 minutes 120 second"),
4680                Some("2 years 39 months 9 days 19 hours 1 minute 83 seconds 399222 milliseconds"),
4681                Some("foobar"),
4682            ],
4683            IntervalUnit::MonthDayNano,
4684            IntervalMonthDayNanoArray,
4685            vec![
4686                Some("13 mons 1 days"),
4687                None,
4688                Some("31 mons 35 days 0.001400000 secs"),
4689                Some("3 days"),
4690                Some("8.000000000 secs"),
4691                None,
4692                Some("1 days 29.800000000 secs"),
4693                Some("3 mons 1.000000000 secs"),
4694                Some("8 mins"),
4695                Some("63 mons 9 days 19 hours 9 mins 2.222000000 secs"),
4696                None,
4697            ]
4698        );
4699    }
4700
4701    macro_rules! test_unsafe_string_to_interval_err {
4702        ($data_vec:expr, $interval_unit:expr, $error_msg:expr) => {
4703            let string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
4704            let options = CastOptions {
4705                safe: false,
4706                format_options: FormatOptions::default(),
4707            };
4708            let arrow_err = cast_with_options(
4709                &string_array.clone(),
4710                &DataType::Interval($interval_unit),
4711                &options,
4712            )
4713            .unwrap_err();
4714            assert_eq!($error_msg, arrow_err.to_string());
4715        };
4716    }
4717
4718    #[test]
4719    fn test_cast_string_to_interval_err() {
4720        test_unsafe_string_to_interval_err!(
4721            vec![Some("foobar")],
4722            IntervalUnit::YearMonth,
4723            r#"Parser error: Invalid input syntax for type interval: "foobar""#
4724        );
4725        test_unsafe_string_to_interval_err!(
4726            vec![Some("foobar")],
4727            IntervalUnit::DayTime,
4728            r#"Parser error: Invalid input syntax for type interval: "foobar""#
4729        );
4730        test_unsafe_string_to_interval_err!(
4731            vec![Some("foobar")],
4732            IntervalUnit::MonthDayNano,
4733            r#"Parser error: Invalid input syntax for type interval: "foobar""#
4734        );
4735        test_unsafe_string_to_interval_err!(
4736            vec![Some("2 months 31 days 1 second")],
4737            IntervalUnit::YearMonth,
4738            r#"Cast error: Cannot cast 2 months 31 days 1 second to IntervalYearMonth. Only year and month fields are allowed."#
4739        );
4740        test_unsafe_string_to_interval_err!(
4741            vec![Some("1 day 1.5 milliseconds")],
4742            IntervalUnit::DayTime,
4743            r#"Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"#
4744        );
4745
4746        // overflow
4747        test_unsafe_string_to_interval_err!(
4748            vec![Some(format!(
4749                "{} century {} year {} month",
4750                i64::MAX - 2,
4751                i64::MAX - 2,
4752                i64::MAX - 2
4753            ))],
4754            IntervalUnit::DayTime,
4755            format!(
4756                "Arithmetic overflow: Overflow happened on: {} * 100",
4757                i64::MAX - 2
4758            )
4759        );
4760        test_unsafe_string_to_interval_err!(
4761            vec![Some(format!(
4762                "{} year {} month {} day",
4763                i64::MAX - 2,
4764                i64::MAX - 2,
4765                i64::MAX - 2
4766            ))],
4767            IntervalUnit::MonthDayNano,
4768            format!(
4769                "Arithmetic overflow: Overflow happened on: {} * 12",
4770                i64::MAX - 2
4771            )
4772        );
4773    }
4774
4775    #[test]
4776    fn test_cast_binary_to_fixed_size_binary() {
4777        let bytes_1 = "Hiiii".as_bytes();
4778        let bytes_2 = "Hello".as_bytes();
4779
4780        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
4781        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
4782        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
4783
4784        let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
4785        let down_cast = array_ref
4786            .as_any()
4787            .downcast_ref::<FixedSizeBinaryArray>()
4788            .unwrap();
4789        assert_eq!(bytes_1, down_cast.value(0));
4790        assert_eq!(bytes_2, down_cast.value(1));
4791        assert!(down_cast.is_null(2));
4792
4793        let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
4794        let down_cast = array_ref
4795            .as_any()
4796            .downcast_ref::<FixedSizeBinaryArray>()
4797            .unwrap();
4798        assert_eq!(bytes_1, down_cast.value(0));
4799        assert_eq!(bytes_2, down_cast.value(1));
4800        assert!(down_cast.is_null(2));
4801
4802        // test error cases when the length of binary are not same
4803        let bytes_1 = "Hi".as_bytes();
4804        let bytes_2 = "Hello".as_bytes();
4805
4806        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
4807        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
4808        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
4809
4810        let array_ref = cast_with_options(
4811            &a1,
4812            &DataType::FixedSizeBinary(5),
4813            &CastOptions {
4814                safe: false,
4815                format_options: FormatOptions::default(),
4816            },
4817        );
4818        assert!(array_ref.is_err());
4819
4820        let array_ref = cast_with_options(
4821            &a2,
4822            &DataType::FixedSizeBinary(5),
4823            &CastOptions {
4824                safe: false,
4825                format_options: FormatOptions::default(),
4826            },
4827        );
4828        assert!(array_ref.is_err());
4829    }
4830
4831    #[test]
4832    fn test_fixed_size_binary_to_binary() {
4833        let bytes_1 = "Hiiii".as_bytes();
4834        let bytes_2 = "Hello".as_bytes();
4835
4836        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
4837        let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
4838
4839        let array_ref = cast(&a1, &DataType::Binary).unwrap();
4840        let down_cast = array_ref.as_binary::<i32>();
4841        assert_eq!(bytes_1, down_cast.value(0));
4842        assert_eq!(bytes_2, down_cast.value(1));
4843        assert!(down_cast.is_null(2));
4844
4845        let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
4846        let down_cast = array_ref.as_binary::<i64>();
4847        assert_eq!(bytes_1, down_cast.value(0));
4848        assert_eq!(bytes_2, down_cast.value(1));
4849        assert!(down_cast.is_null(2));
4850    }
4851
4852    #[test]
4853    fn test_fixed_size_binary_to_dictionary() {
4854        let bytes_1 = "Hiiii".as_bytes();
4855        let bytes_2 = "Hello".as_bytes();
4856
4857        let binary_data = vec![Some(bytes_1), Some(bytes_2), Some(bytes_1), None];
4858        let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
4859
4860        let cast_type = DataType::Dictionary(
4861            Box::new(DataType::Int8),
4862            Box::new(DataType::FixedSizeBinary(5)),
4863        );
4864        let cast_array = cast(&a1, &cast_type).unwrap();
4865        assert_eq!(cast_array.data_type(), &cast_type);
4866        assert_eq!(
4867            array_to_strings(&cast_array),
4868            vec!["4869696969", "48656c6c6f", "4869696969", "null"]
4869        );
4870        // dictionary should only have two distinct values
4871        let dict_array = cast_array
4872            .as_any()
4873            .downcast_ref::<DictionaryArray<Int8Type>>()
4874            .unwrap();
4875        assert_eq!(dict_array.values().len(), 2);
4876    }
4877
4878    #[test]
4879    fn test_binary_to_dictionary() {
4880        let mut builder = GenericBinaryBuilder::<i32>::new();
4881        builder.append_value(b"hello");
4882        builder.append_value(b"hiiii");
4883        builder.append_value(b"hiiii"); // duplicate
4884        builder.append_null();
4885        builder.append_value(b"rustt");
4886
4887        let a1 = builder.finish();
4888
4889        let cast_type = DataType::Dictionary(
4890            Box::new(DataType::Int8),
4891            Box::new(DataType::FixedSizeBinary(5)),
4892        );
4893        let cast_array = cast(&a1, &cast_type).unwrap();
4894        assert_eq!(cast_array.data_type(), &cast_type);
4895        assert_eq!(
4896            array_to_strings(&cast_array),
4897            vec![
4898                "68656c6c6f",
4899                "6869696969",
4900                "6869696969",
4901                "null",
4902                "7275737474"
4903            ]
4904        );
4905        // dictionary should only have three distinct values
4906        let dict_array = cast_array
4907            .as_any()
4908            .downcast_ref::<DictionaryArray<Int8Type>>()
4909            .unwrap();
4910        assert_eq!(dict_array.values().len(), 3);
4911    }
4912
4913    #[test]
4914    fn test_numeric_to_binary() {
4915        let a = Int16Array::from(vec![Some(1), Some(511), None]);
4916
4917        let array_ref = cast(&a, &DataType::Binary).unwrap();
4918        let down_cast = array_ref.as_binary::<i32>();
4919        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
4920        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
4921        assert!(down_cast.is_null(2));
4922
4923        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
4924
4925        let array_ref = cast(&a, &DataType::Binary).unwrap();
4926        let down_cast = array_ref.as_binary::<i32>();
4927        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
4928        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
4929        assert!(down_cast.is_null(2));
4930    }
4931
4932    #[test]
4933    fn test_numeric_to_large_binary() {
4934        let a = Int16Array::from(vec![Some(1), Some(511), None]);
4935
4936        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
4937        let down_cast = array_ref.as_binary::<i64>();
4938        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
4939        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
4940        assert!(down_cast.is_null(2));
4941
4942        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
4943
4944        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
4945        let down_cast = array_ref.as_binary::<i64>();
4946        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
4947        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
4948        assert!(down_cast.is_null(2));
4949    }
4950
4951    #[test]
4952    fn test_cast_date32_to_int32() {
4953        let array = Date32Array::from(vec![10000, 17890]);
4954        let b = cast(&array, &DataType::Int32).unwrap();
4955        let c = b.as_primitive::<Int32Type>();
4956        assert_eq!(10000, c.value(0));
4957        assert_eq!(17890, c.value(1));
4958    }
4959
4960    #[test]
4961    fn test_cast_int32_to_date32() {
4962        let array = Int32Array::from(vec![10000, 17890]);
4963        let b = cast(&array, &DataType::Date32).unwrap();
4964        let c = b.as_primitive::<Date32Type>();
4965        assert_eq!(10000, c.value(0));
4966        assert_eq!(17890, c.value(1));
4967    }
4968
4969    #[test]
4970    fn test_cast_timestamp_to_date32() {
4971        let array =
4972            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
4973                .with_timezone("+00:00".to_string());
4974        let b = cast(&array, &DataType::Date32).unwrap();
4975        let c = b.as_primitive::<Date32Type>();
4976        assert_eq!(10000, c.value(0));
4977        assert_eq!(17890, c.value(1));
4978        assert!(c.is_null(2));
4979    }
4980    #[test]
4981    fn test_cast_timestamp_to_date32_zone() {
4982        let strings = StringArray::from_iter([
4983            Some("1970-01-01T00:00:01"),
4984            Some("1970-01-01T23:59:59"),
4985            None,
4986            Some("2020-03-01T02:00:23+00:00"),
4987        ]);
4988        let dt = DataType::Timestamp(TimeUnit::Millisecond, Some("-07:00".into()));
4989        let timestamps = cast(&strings, &dt).unwrap();
4990        let dates = cast(timestamps.as_ref(), &DataType::Date32).unwrap();
4991
4992        let c = dates.as_primitive::<Date32Type>();
4993        let expected = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
4994        assert_eq!(c.value_as_date(0).unwrap(), expected);
4995        assert_eq!(c.value_as_date(1).unwrap(), expected);
4996        assert!(c.is_null(2));
4997        let expected = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
4998        assert_eq!(c.value_as_date(3).unwrap(), expected);
4999    }
5000    #[test]
5001    fn test_cast_timestamp_to_date64() {
5002        let array =
5003            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None]);
5004        let b = cast(&array, &DataType::Date64).unwrap();
5005        let c = b.as_primitive::<Date64Type>();
5006        assert_eq!(864000000005, c.value(0));
5007        assert_eq!(1545696000001, c.value(1));
5008        assert!(c.is_null(2));
5009
5010        let array = TimestampSecondArray::from(vec![Some(864000000005), Some(1545696000001)]);
5011        let b = cast(&array, &DataType::Date64).unwrap();
5012        let c = b.as_primitive::<Date64Type>();
5013        assert_eq!(864000000005000, c.value(0));
5014        assert_eq!(1545696000001000, c.value(1));
5015
5016        // test overflow, safe cast
5017        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
5018        let b = cast(&array, &DataType::Date64).unwrap();
5019        assert!(b.is_null(0));
5020        // test overflow, unsafe cast
5021        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
5022        let options = CastOptions {
5023            safe: false,
5024            format_options: FormatOptions::default(),
5025        };
5026        let b = cast_with_options(&array, &DataType::Date64, &options);
5027        assert!(b.is_err());
5028    }
5029
5030    #[test]
5031    fn test_cast_timestamp_to_time64() {
5032        // test timestamp secs
5033        let array = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
5034            .with_timezone("+01:00".to_string());
5035        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5036        let c = b.as_primitive::<Time64MicrosecondType>();
5037        assert_eq!(3605000000, c.value(0));
5038        assert_eq!(3601000000, c.value(1));
5039        assert!(c.is_null(2));
5040        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5041        let c = b.as_primitive::<Time64NanosecondType>();
5042        assert_eq!(3605000000000, c.value(0));
5043        assert_eq!(3601000000000, c.value(1));
5044        assert!(c.is_null(2));
5045
5046        // test timestamp milliseconds
5047        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
5048            .with_timezone("+01:00".to_string());
5049        let array = Arc::new(a) as ArrayRef;
5050        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5051        let c = b.as_primitive::<Time64MicrosecondType>();
5052        assert_eq!(3605000000, c.value(0));
5053        assert_eq!(3601000000, c.value(1));
5054        assert!(c.is_null(2));
5055        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5056        let c = b.as_primitive::<Time64NanosecondType>();
5057        assert_eq!(3605000000000, c.value(0));
5058        assert_eq!(3601000000000, c.value(1));
5059        assert!(c.is_null(2));
5060
5061        // test timestamp microseconds
5062        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
5063            .with_timezone("+01:00".to_string());
5064        let array = Arc::new(a) as ArrayRef;
5065        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5066        let c = b.as_primitive::<Time64MicrosecondType>();
5067        assert_eq!(3605000000, c.value(0));
5068        assert_eq!(3601000000, c.value(1));
5069        assert!(c.is_null(2));
5070        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5071        let c = b.as_primitive::<Time64NanosecondType>();
5072        assert_eq!(3605000000000, c.value(0));
5073        assert_eq!(3601000000000, c.value(1));
5074        assert!(c.is_null(2));
5075
5076        // test timestamp nanoseconds
5077        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
5078            .with_timezone("+01:00".to_string());
5079        let array = Arc::new(a) as ArrayRef;
5080        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5081        let c = b.as_primitive::<Time64MicrosecondType>();
5082        assert_eq!(3605000000, c.value(0));
5083        assert_eq!(3601000000, c.value(1));
5084        assert!(c.is_null(2));
5085        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5086        let c = b.as_primitive::<Time64NanosecondType>();
5087        assert_eq!(3605000000000, c.value(0));
5088        assert_eq!(3601000000000, c.value(1));
5089        assert!(c.is_null(2));
5090
5091        // test overflow
5092        let a =
5093            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
5094        let array = Arc::new(a) as ArrayRef;
5095        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond));
5096        assert!(b.is_err());
5097        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond));
5098        assert!(b.is_err());
5099        let b = cast(&array, &DataType::Time64(TimeUnit::Millisecond));
5100        assert!(b.is_err());
5101    }
5102
5103    #[test]
5104    fn test_cast_timestamp_to_time32() {
5105        // test timestamp secs
5106        let a = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
5107            .with_timezone("+01:00".to_string());
5108        let array = Arc::new(a) as ArrayRef;
5109        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5110        let c = b.as_primitive::<Time32SecondType>();
5111        assert_eq!(3605, c.value(0));
5112        assert_eq!(3601, c.value(1));
5113        assert!(c.is_null(2));
5114        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5115        let c = b.as_primitive::<Time32MillisecondType>();
5116        assert_eq!(3605000, c.value(0));
5117        assert_eq!(3601000, c.value(1));
5118        assert!(c.is_null(2));
5119
5120        // test timestamp milliseconds
5121        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
5122            .with_timezone("+01:00".to_string());
5123        let array = Arc::new(a) as ArrayRef;
5124        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5125        let c = b.as_primitive::<Time32SecondType>();
5126        assert_eq!(3605, c.value(0));
5127        assert_eq!(3601, c.value(1));
5128        assert!(c.is_null(2));
5129        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5130        let c = b.as_primitive::<Time32MillisecondType>();
5131        assert_eq!(3605000, c.value(0));
5132        assert_eq!(3601000, c.value(1));
5133        assert!(c.is_null(2));
5134
5135        // test timestamp microseconds
5136        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
5137            .with_timezone("+01:00".to_string());
5138        let array = Arc::new(a) as ArrayRef;
5139        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5140        let c = b.as_primitive::<Time32SecondType>();
5141        assert_eq!(3605, c.value(0));
5142        assert_eq!(3601, c.value(1));
5143        assert!(c.is_null(2));
5144        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5145        let c = b.as_primitive::<Time32MillisecondType>();
5146        assert_eq!(3605000, c.value(0));
5147        assert_eq!(3601000, c.value(1));
5148        assert!(c.is_null(2));
5149
5150        // test timestamp nanoseconds
5151        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
5152            .with_timezone("+01:00".to_string());
5153        let array = Arc::new(a) as ArrayRef;
5154        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5155        let c = b.as_primitive::<Time32SecondType>();
5156        assert_eq!(3605, c.value(0));
5157        assert_eq!(3601, c.value(1));
5158        assert!(c.is_null(2));
5159        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5160        let c = b.as_primitive::<Time32MillisecondType>();
5161        assert_eq!(3605000, c.value(0));
5162        assert_eq!(3601000, c.value(1));
5163        assert!(c.is_null(2));
5164
5165        // test overflow
5166        let a =
5167            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
5168        let array = Arc::new(a) as ArrayRef;
5169        let b = cast(&array, &DataType::Time32(TimeUnit::Second));
5170        assert!(b.is_err());
5171        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond));
5172        assert!(b.is_err());
5173    }
5174
5175    // Cast Timestamp(_, None) -> Timestamp(_, Some(timezone))
5176    #[test]
5177    fn test_cast_timestamp_with_timezone_1() {
5178        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5179            Some("2000-01-01T00:00:00.123456789"),
5180            Some("2010-01-01T00:00:00.123456789"),
5181            None,
5182        ]));
5183        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
5184        let timestamp_array = cast(&string_array, &to_type).unwrap();
5185
5186        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
5187        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
5188
5189        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5190        let result = string_array.as_string::<i32>();
5191        assert_eq!("2000-01-01T00:00:00.123456+07:00", result.value(0));
5192        assert_eq!("2010-01-01T00:00:00.123456+07:00", result.value(1));
5193        assert!(result.is_null(2));
5194    }
5195
5196    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, None)
5197    #[test]
5198    fn test_cast_timestamp_with_timezone_2() {
5199        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5200            Some("2000-01-01T07:00:00.123456789"),
5201            Some("2010-01-01T07:00:00.123456789"),
5202            None,
5203        ]));
5204        let to_type = DataType::Timestamp(TimeUnit::Millisecond, Some("+0700".into()));
5205        let timestamp_array = cast(&string_array, &to_type).unwrap();
5206
5207        // Check intermediate representation is correct
5208        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5209        let result = string_array.as_string::<i32>();
5210        assert_eq!("2000-01-01T07:00:00.123+07:00", result.value(0));
5211        assert_eq!("2010-01-01T07:00:00.123+07:00", result.value(1));
5212        assert!(result.is_null(2));
5213
5214        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
5215        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
5216
5217        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5218        let result = string_array.as_string::<i32>();
5219        assert_eq!("2000-01-01T00:00:00.123", result.value(0));
5220        assert_eq!("2010-01-01T00:00:00.123", result.value(1));
5221        assert!(result.is_null(2));
5222    }
5223
5224    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, Some(timezone))
5225    #[test]
5226    fn test_cast_timestamp_with_timezone_3() {
5227        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5228            Some("2000-01-01T07:00:00.123456789"),
5229            Some("2010-01-01T07:00:00.123456789"),
5230            None,
5231        ]));
5232        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
5233        let timestamp_array = cast(&string_array, &to_type).unwrap();
5234
5235        // Check intermediate representation is correct
5236        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5237        let result = string_array.as_string::<i32>();
5238        assert_eq!("2000-01-01T07:00:00.123456+07:00", result.value(0));
5239        assert_eq!("2010-01-01T07:00:00.123456+07:00", result.value(1));
5240        assert!(result.is_null(2));
5241
5242        let to_type = DataType::Timestamp(TimeUnit::Second, Some("-08:00".into()));
5243        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
5244
5245        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5246        let result = string_array.as_string::<i32>();
5247        assert_eq!("1999-12-31T16:00:00-08:00", result.value(0));
5248        assert_eq!("2009-12-31T16:00:00-08:00", result.value(1));
5249        assert!(result.is_null(2));
5250    }
5251
5252    #[test]
5253    fn test_cast_date64_to_timestamp() {
5254        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5255        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5256        let c = b.as_primitive::<TimestampSecondType>();
5257        assert_eq!(864000000, c.value(0));
5258        assert_eq!(1545696000, c.value(1));
5259        assert!(c.is_null(2));
5260    }
5261
5262    #[test]
5263    fn test_cast_date64_to_timestamp_ms() {
5264        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5265        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
5266        let c = b
5267            .as_any()
5268            .downcast_ref::<TimestampMillisecondArray>()
5269            .unwrap();
5270        assert_eq!(864000000005, c.value(0));
5271        assert_eq!(1545696000001, c.value(1));
5272        assert!(c.is_null(2));
5273    }
5274
5275    #[test]
5276    fn test_cast_date64_to_timestamp_us() {
5277        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5278        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5279        let c = b
5280            .as_any()
5281            .downcast_ref::<TimestampMicrosecondArray>()
5282            .unwrap();
5283        assert_eq!(864000000005000, c.value(0));
5284        assert_eq!(1545696000001000, c.value(1));
5285        assert!(c.is_null(2));
5286    }
5287
5288    #[test]
5289    fn test_cast_date64_to_timestamp_ns() {
5290        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5291        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
5292        let c = b
5293            .as_any()
5294            .downcast_ref::<TimestampNanosecondArray>()
5295            .unwrap();
5296        assert_eq!(864000000005000000, c.value(0));
5297        assert_eq!(1545696000001000000, c.value(1));
5298        assert!(c.is_null(2));
5299    }
5300
5301    #[test]
5302    fn test_cast_timestamp_to_i64() {
5303        let array =
5304            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
5305                .with_timezone("UTC".to_string());
5306        let b = cast(&array, &DataType::Int64).unwrap();
5307        let c = b.as_primitive::<Int64Type>();
5308        assert_eq!(&DataType::Int64, c.data_type());
5309        assert_eq!(864000000005, c.value(0));
5310        assert_eq!(1545696000001, c.value(1));
5311        assert!(c.is_null(2));
5312    }
5313
5314    #[test]
5315    fn test_cast_date32_to_string() {
5316        let array = Date32Array::from(vec![10000, 17890]);
5317        let b = cast(&array, &DataType::Utf8).unwrap();
5318        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
5319        assert_eq!(&DataType::Utf8, c.data_type());
5320        assert_eq!("1997-05-19", c.value(0));
5321        assert_eq!("2018-12-25", c.value(1));
5322    }
5323
5324    #[test]
5325    fn test_cast_date64_to_string() {
5326        let array = Date64Array::from(vec![10000 * 86400000, 17890 * 86400000]);
5327        let b = cast(&array, &DataType::Utf8).unwrap();
5328        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
5329        assert_eq!(&DataType::Utf8, c.data_type());
5330        assert_eq!("1997-05-19T00:00:00", c.value(0));
5331        assert_eq!("2018-12-25T00:00:00", c.value(1));
5332    }
5333
5334    macro_rules! assert_cast_timestamp_to_string {
5335        ($array:expr, $datatype:expr, $output_array_type: ty, $expected:expr) => {{
5336            let out = cast(&$array, &$datatype).unwrap();
5337            let actual = out
5338                .as_any()
5339                .downcast_ref::<$output_array_type>()
5340                .unwrap()
5341                .into_iter()
5342                .collect::<Vec<_>>();
5343            assert_eq!(actual, $expected);
5344        }};
5345        ($array:expr, $datatype:expr, $output_array_type: ty, $options:expr, $expected:expr) => {{
5346            let out = cast_with_options(&$array, &$datatype, &$options).unwrap();
5347            let actual = out
5348                .as_any()
5349                .downcast_ref::<$output_array_type>()
5350                .unwrap()
5351                .into_iter()
5352                .collect::<Vec<_>>();
5353            assert_eq!(actual, $expected);
5354        }};
5355    }
5356
5357    #[test]
5358    fn test_cast_date32_to_timestamp_and_timestamp_with_timezone() {
5359        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5360        let a = Date32Array::from(vec![Some(18628), None, None]); // 2021-1-1, 2022-1-1
5361        let array = Arc::new(a) as ArrayRef;
5362
5363        let b = cast(
5364            &array,
5365            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
5366        )
5367        .unwrap();
5368        let c = b.as_primitive::<TimestampSecondType>();
5369        let string_array = cast(&c, &DataType::Utf8).unwrap();
5370        let result = string_array.as_string::<i32>();
5371        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5372
5373        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5374        let c = b.as_primitive::<TimestampSecondType>();
5375        let string_array = cast(&c, &DataType::Utf8).unwrap();
5376        let result = string_array.as_string::<i32>();
5377        assert_eq!("2021-01-01T00:00:00", result.value(0));
5378    }
5379
5380    #[test]
5381    fn test_cast_date32_to_timestamp_with_timezone() {
5382        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5383        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
5384        let array = Arc::new(a) as ArrayRef;
5385        let b = cast(
5386            &array,
5387            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
5388        )
5389        .unwrap();
5390        let c = b.as_primitive::<TimestampSecondType>();
5391        assert_eq!(1609438500, c.value(0));
5392        assert_eq!(1640974500, c.value(1));
5393        assert!(c.is_null(2));
5394
5395        let string_array = cast(&c, &DataType::Utf8).unwrap();
5396        let result = string_array.as_string::<i32>();
5397        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5398        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
5399    }
5400
5401    #[test]
5402    fn test_cast_date32_to_timestamp_with_timezone_ms() {
5403        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5404        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
5405        let array = Arc::new(a) as ArrayRef;
5406        let b = cast(
5407            &array,
5408            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
5409        )
5410        .unwrap();
5411        let c = b.as_primitive::<TimestampMillisecondType>();
5412        assert_eq!(1609438500000, c.value(0));
5413        assert_eq!(1640974500000, c.value(1));
5414        assert!(c.is_null(2));
5415
5416        let string_array = cast(&c, &DataType::Utf8).unwrap();
5417        let result = string_array.as_string::<i32>();
5418        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5419        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
5420    }
5421
5422    #[test]
5423    fn test_cast_date32_to_timestamp_with_timezone_us() {
5424        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5425        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
5426        let array = Arc::new(a) as ArrayRef;
5427        let b = cast(
5428            &array,
5429            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
5430        )
5431        .unwrap();
5432        let c = b.as_primitive::<TimestampMicrosecondType>();
5433        assert_eq!(1609438500000000, c.value(0));
5434        assert_eq!(1640974500000000, c.value(1));
5435        assert!(c.is_null(2));
5436
5437        let string_array = cast(&c, &DataType::Utf8).unwrap();
5438        let result = string_array.as_string::<i32>();
5439        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5440        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
5441    }
5442
5443    #[test]
5444    fn test_cast_date32_to_timestamp_with_timezone_ns() {
5445        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5446        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
5447        let array = Arc::new(a) as ArrayRef;
5448        let b = cast(
5449            &array,
5450            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
5451        )
5452        .unwrap();
5453        let c = b.as_primitive::<TimestampNanosecondType>();
5454        assert_eq!(1609438500000000000, c.value(0));
5455        assert_eq!(1640974500000000000, c.value(1));
5456        assert!(c.is_null(2));
5457
5458        let string_array = cast(&c, &DataType::Utf8).unwrap();
5459        let result = string_array.as_string::<i32>();
5460        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5461        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
5462    }
5463
5464    #[test]
5465    fn test_cast_date64_to_timestamp_with_timezone() {
5466        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5467        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5468        let b = cast(
5469            &array,
5470            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
5471        )
5472        .unwrap();
5473
5474        let c = b.as_primitive::<TimestampSecondType>();
5475        assert_eq!(863979300, c.value(0));
5476        assert_eq!(1545675300, c.value(1));
5477        assert!(c.is_null(2));
5478
5479        let string_array = cast(&c, &DataType::Utf8).unwrap();
5480        let result = string_array.as_string::<i32>();
5481        assert_eq!("1997-05-19T00:00:00+05:45", result.value(0));
5482        assert_eq!("2018-12-25T00:00:00+05:45", result.value(1));
5483    }
5484
5485    #[test]
5486    fn test_cast_date64_to_timestamp_with_timezone_ms() {
5487        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5488        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5489        let b = cast(
5490            &array,
5491            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
5492        )
5493        .unwrap();
5494
5495        let c = b.as_primitive::<TimestampMillisecondType>();
5496        assert_eq!(863979300005, c.value(0));
5497        assert_eq!(1545675300001, c.value(1));
5498        assert!(c.is_null(2));
5499
5500        let string_array = cast(&c, &DataType::Utf8).unwrap();
5501        let result = string_array.as_string::<i32>();
5502        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
5503        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
5504    }
5505
5506    #[test]
5507    fn test_cast_date64_to_timestamp_with_timezone_us() {
5508        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5509        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5510        let b = cast(
5511            &array,
5512            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
5513        )
5514        .unwrap();
5515
5516        let c = b.as_primitive::<TimestampMicrosecondType>();
5517        assert_eq!(863979300005000, c.value(0));
5518        assert_eq!(1545675300001000, c.value(1));
5519        assert!(c.is_null(2));
5520
5521        let string_array = cast(&c, &DataType::Utf8).unwrap();
5522        let result = string_array.as_string::<i32>();
5523        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
5524        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
5525    }
5526
5527    #[test]
5528    fn test_cast_date64_to_timestamp_with_timezone_ns() {
5529        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5530        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5531        let b = cast(
5532            &array,
5533            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
5534        )
5535        .unwrap();
5536
5537        let c = b.as_primitive::<TimestampNanosecondType>();
5538        assert_eq!(863979300005000000, c.value(0));
5539        assert_eq!(1545675300001000000, c.value(1));
5540        assert!(c.is_null(2));
5541
5542        let string_array = cast(&c, &DataType::Utf8).unwrap();
5543        let result = string_array.as_string::<i32>();
5544        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
5545        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
5546    }
5547
5548    #[test]
5549    fn test_cast_timestamp_to_strings() {
5550        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
5551        let array =
5552            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
5553        let expected = vec![
5554            Some("1997-05-19T00:00:03.005"),
5555            Some("2018-12-25T00:00:02.001"),
5556            None,
5557        ];
5558
5559        assert_cast_timestamp_to_string!(array, DataType::Utf8View, StringViewArray, expected);
5560        assert_cast_timestamp_to_string!(array, DataType::Utf8, StringArray, expected);
5561        assert_cast_timestamp_to_string!(array, DataType::LargeUtf8, LargeStringArray, expected);
5562    }
5563
5564    #[test]
5565    fn test_cast_timestamp_to_strings_opt() {
5566        let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
5567        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5568        let cast_options = CastOptions {
5569            safe: true,
5570            format_options: FormatOptions::default()
5571                .with_timestamp_format(Some(ts_format))
5572                .with_timestamp_tz_format(Some(ts_format)),
5573        };
5574
5575        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
5576        let array_without_tz =
5577            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
5578        let expected = vec![
5579            Some("1997-05-19 00:00:03.005000"),
5580            Some("2018-12-25 00:00:02.001000"),
5581            None,
5582        ];
5583        assert_cast_timestamp_to_string!(
5584            array_without_tz,
5585            DataType::Utf8View,
5586            StringViewArray,
5587            cast_options,
5588            expected
5589        );
5590        assert_cast_timestamp_to_string!(
5591            array_without_tz,
5592            DataType::Utf8,
5593            StringArray,
5594            cast_options,
5595            expected
5596        );
5597        assert_cast_timestamp_to_string!(
5598            array_without_tz,
5599            DataType::LargeUtf8,
5600            LargeStringArray,
5601            cast_options,
5602            expected
5603        );
5604
5605        let array_with_tz =
5606            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None])
5607                .with_timezone(tz.to_string());
5608        let expected = vec![
5609            Some("1997-05-19 05:45:03.005000"),
5610            Some("2018-12-25 05:45:02.001000"),
5611            None,
5612        ];
5613        assert_cast_timestamp_to_string!(
5614            array_with_tz,
5615            DataType::Utf8View,
5616            StringViewArray,
5617            cast_options,
5618            expected
5619        );
5620        assert_cast_timestamp_to_string!(
5621            array_with_tz,
5622            DataType::Utf8,
5623            StringArray,
5624            cast_options,
5625            expected
5626        );
5627        assert_cast_timestamp_to_string!(
5628            array_with_tz,
5629            DataType::LargeUtf8,
5630            LargeStringArray,
5631            cast_options,
5632            expected
5633        );
5634    }
5635
5636    #[test]
5637    fn test_cast_between_timestamps() {
5638        let array =
5639            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
5640        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5641        let c = b.as_primitive::<TimestampSecondType>();
5642        assert_eq!(864000003, c.value(0));
5643        assert_eq!(1545696002, c.value(1));
5644        assert!(c.is_null(2));
5645    }
5646
5647    #[test]
5648    fn test_cast_duration_to_i64() {
5649        let base = vec![5, 6, 7, 8, 100000000];
5650
5651        let duration_arrays = vec![
5652            Arc::new(DurationNanosecondArray::from(base.clone())) as ArrayRef,
5653            Arc::new(DurationMicrosecondArray::from(base.clone())) as ArrayRef,
5654            Arc::new(DurationMillisecondArray::from(base.clone())) as ArrayRef,
5655            Arc::new(DurationSecondArray::from(base.clone())) as ArrayRef,
5656        ];
5657
5658        for arr in duration_arrays {
5659            assert!(can_cast_types(arr.data_type(), &DataType::Int64));
5660            let result = cast(&arr, &DataType::Int64).unwrap();
5661            let result = result.as_primitive::<Int64Type>();
5662            assert_eq!(base.as_slice(), result.values());
5663        }
5664    }
5665
5666    #[test]
5667    fn test_cast_between_durations_and_numerics() {
5668        fn test_cast_between_durations<FromType, ToType>()
5669        where
5670            FromType: ArrowPrimitiveType<Native = i64>,
5671            ToType: ArrowPrimitiveType<Native = i64>,
5672            PrimitiveArray<FromType>: From<Vec<Option<i64>>>,
5673        {
5674            let from_unit = match FromType::DATA_TYPE {
5675                DataType::Duration(unit) => unit,
5676                _ => panic!("Expected a duration type"),
5677            };
5678            let to_unit = match ToType::DATA_TYPE {
5679                DataType::Duration(unit) => unit,
5680                _ => panic!("Expected a duration type"),
5681            };
5682            let from_size = time_unit_multiple(&from_unit);
5683            let to_size = time_unit_multiple(&to_unit);
5684
5685            let (v1_before, v2_before) = (8640003005, 1696002001);
5686            let (v1_after, v2_after) = if from_size >= to_size {
5687                (
5688                    v1_before / (from_size / to_size),
5689                    v2_before / (from_size / to_size),
5690                )
5691            } else {
5692                (
5693                    v1_before * (to_size / from_size),
5694                    v2_before * (to_size / from_size),
5695                )
5696            };
5697
5698            let array =
5699                PrimitiveArray::<FromType>::from(vec![Some(v1_before), Some(v2_before), None]);
5700            let b = cast(&array, &ToType::DATA_TYPE).unwrap();
5701            let c = b.as_primitive::<ToType>();
5702            assert_eq!(v1_after, c.value(0));
5703            assert_eq!(v2_after, c.value(1));
5704            assert!(c.is_null(2));
5705        }
5706
5707        // between each individual duration type
5708        test_cast_between_durations::<DurationSecondType, DurationMillisecondType>();
5709        test_cast_between_durations::<DurationSecondType, DurationMicrosecondType>();
5710        test_cast_between_durations::<DurationSecondType, DurationNanosecondType>();
5711        test_cast_between_durations::<DurationMillisecondType, DurationSecondType>();
5712        test_cast_between_durations::<DurationMillisecondType, DurationMicrosecondType>();
5713        test_cast_between_durations::<DurationMillisecondType, DurationNanosecondType>();
5714        test_cast_between_durations::<DurationMicrosecondType, DurationSecondType>();
5715        test_cast_between_durations::<DurationMicrosecondType, DurationMillisecondType>();
5716        test_cast_between_durations::<DurationMicrosecondType, DurationNanosecondType>();
5717        test_cast_between_durations::<DurationNanosecondType, DurationSecondType>();
5718        test_cast_between_durations::<DurationNanosecondType, DurationMillisecondType>();
5719        test_cast_between_durations::<DurationNanosecondType, DurationMicrosecondType>();
5720
5721        // cast failed
5722        let array = DurationSecondArray::from(vec![
5723            Some(i64::MAX),
5724            Some(8640203410378005),
5725            Some(10241096),
5726            None,
5727        ]);
5728        let b = cast(&array, &DataType::Duration(TimeUnit::Nanosecond)).unwrap();
5729        let c = b.as_primitive::<DurationNanosecondType>();
5730        assert!(c.is_null(0));
5731        assert!(c.is_null(1));
5732        assert_eq!(10241096000000000, c.value(2));
5733        assert!(c.is_null(3));
5734
5735        // durations to numerics
5736        let array = DurationSecondArray::from(vec![
5737            Some(i64::MAX),
5738            Some(8640203410378005),
5739            Some(10241096),
5740            None,
5741        ]);
5742        let b = cast(&array, &DataType::Int64).unwrap();
5743        let c = b.as_primitive::<Int64Type>();
5744        assert_eq!(i64::MAX, c.value(0));
5745        assert_eq!(8640203410378005, c.value(1));
5746        assert_eq!(10241096, c.value(2));
5747        assert!(c.is_null(3));
5748
5749        let b = cast(&array, &DataType::Int32).unwrap();
5750        let c = b.as_primitive::<Int32Type>();
5751        assert_eq!(0, c.value(0));
5752        assert_eq!(0, c.value(1));
5753        assert_eq!(10241096, c.value(2));
5754        assert!(c.is_null(3));
5755
5756        // numerics to durations
5757        let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), Some(10241096), None]);
5758        let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
5759        let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
5760        assert_eq!(i32::MAX as i64, c.value(0));
5761        assert_eq!(802034103, c.value(1));
5762        assert_eq!(10241096, c.value(2));
5763        assert!(c.is_null(3));
5764    }
5765
5766    #[test]
5767    fn test_cast_to_strings() {
5768        let a = Int32Array::from(vec![1, 2, 3]);
5769        let out = cast(&a, &DataType::Utf8).unwrap();
5770        let out = out
5771            .as_any()
5772            .downcast_ref::<StringArray>()
5773            .unwrap()
5774            .into_iter()
5775            .collect::<Vec<_>>();
5776        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
5777        let out = cast(&a, &DataType::LargeUtf8).unwrap();
5778        let out = out
5779            .as_any()
5780            .downcast_ref::<LargeStringArray>()
5781            .unwrap()
5782            .into_iter()
5783            .collect::<Vec<_>>();
5784        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
5785    }
5786
5787    #[test]
5788    fn test_str_to_str_casts() {
5789        for data in [
5790            vec![Some("foo"), Some("bar"), Some("ham")],
5791            vec![Some("foo"), None, Some("bar")],
5792        ] {
5793            let a = LargeStringArray::from(data.clone());
5794            let to = cast(&a, &DataType::Utf8).unwrap();
5795            let expect = a
5796                .as_any()
5797                .downcast_ref::<LargeStringArray>()
5798                .unwrap()
5799                .into_iter()
5800                .collect::<Vec<_>>();
5801            let out = to
5802                .as_any()
5803                .downcast_ref::<StringArray>()
5804                .unwrap()
5805                .into_iter()
5806                .collect::<Vec<_>>();
5807            assert_eq!(expect, out);
5808
5809            let a = StringArray::from(data);
5810            let to = cast(&a, &DataType::LargeUtf8).unwrap();
5811            let expect = a
5812                .as_any()
5813                .downcast_ref::<StringArray>()
5814                .unwrap()
5815                .into_iter()
5816                .collect::<Vec<_>>();
5817            let out = to
5818                .as_any()
5819                .downcast_ref::<LargeStringArray>()
5820                .unwrap()
5821                .into_iter()
5822                .collect::<Vec<_>>();
5823            assert_eq!(expect, out);
5824        }
5825    }
5826
5827    const VIEW_TEST_DATA: [Option<&str>; 5] = [
5828        Some("hello"),
5829        Some("repeated"),
5830        None,
5831        Some("large payload over 12 bytes"),
5832        Some("repeated"),
5833    ];
5834
5835    #[test]
5836    fn test_string_view_to_binary_view() {
5837        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
5838
5839        assert!(can_cast_types(
5840            string_view_array.data_type(),
5841            &DataType::BinaryView
5842        ));
5843
5844        let binary_view_array = cast(&string_view_array, &DataType::BinaryView).unwrap();
5845        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
5846
5847        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
5848        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
5849    }
5850
5851    #[test]
5852    fn test_binary_view_to_string_view() {
5853        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
5854
5855        assert!(can_cast_types(
5856            binary_view_array.data_type(),
5857            &DataType::Utf8View
5858        ));
5859
5860        let string_view_array = cast(&binary_view_array, &DataType::Utf8View).unwrap();
5861        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
5862
5863        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
5864        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
5865    }
5866
5867    #[test]
5868    fn test_string_to_view() {
5869        _test_string_to_view::<i32>();
5870        _test_string_to_view::<i64>();
5871    }
5872
5873    fn _test_string_to_view<O>()
5874    where
5875        O: OffsetSizeTrait,
5876    {
5877        let string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
5878
5879        assert!(can_cast_types(
5880            string_array.data_type(),
5881            &DataType::Utf8View
5882        ));
5883
5884        assert!(can_cast_types(
5885            string_array.data_type(),
5886            &DataType::BinaryView
5887        ));
5888
5889        let string_view_array = cast(&string_array, &DataType::Utf8View).unwrap();
5890        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
5891
5892        let binary_view_array = cast(&string_array, &DataType::BinaryView).unwrap();
5893        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
5894
5895        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
5896        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
5897
5898        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
5899        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
5900    }
5901
5902    #[test]
5903    fn test_bianry_to_view() {
5904        _test_binary_to_view::<i32>();
5905        _test_binary_to_view::<i64>();
5906    }
5907
5908    fn _test_binary_to_view<O>()
5909    where
5910        O: OffsetSizeTrait,
5911    {
5912        let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
5913
5914        assert!(can_cast_types(
5915            binary_array.data_type(),
5916            &DataType::Utf8View
5917        ));
5918
5919        assert!(can_cast_types(
5920            binary_array.data_type(),
5921            &DataType::BinaryView
5922        ));
5923
5924        let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
5925        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
5926
5927        let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
5928        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
5929
5930        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
5931        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
5932
5933        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
5934        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
5935    }
5936
5937    #[test]
5938    fn test_dict_to_view() {
5939        let values = StringArray::from_iter(VIEW_TEST_DATA);
5940        let keys = Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
5941        let string_dict_array =
5942            DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
5943        let typed_dict = string_dict_array.downcast_dict::<StringArray>().unwrap();
5944
5945        let string_view_array = {
5946            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
5947            for v in typed_dict.into_iter() {
5948                builder.append_option(v);
5949            }
5950            builder.finish()
5951        };
5952        let expected_string_array_type = string_view_array.data_type();
5953        let casted_string_array = cast(&string_dict_array, expected_string_array_type).unwrap();
5954        assert_eq!(casted_string_array.data_type(), expected_string_array_type);
5955        assert_eq!(casted_string_array.as_ref(), &string_view_array);
5956
5957        let binary_buffer = cast(&typed_dict.values(), &DataType::Binary).unwrap();
5958        let binary_dict_array =
5959            DictionaryArray::<Int8Type>::new(typed_dict.keys().clone(), binary_buffer);
5960        let typed_binary_dict = binary_dict_array.downcast_dict::<BinaryArray>().unwrap();
5961
5962        let binary_view_array = {
5963            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
5964            for v in typed_binary_dict.into_iter() {
5965                builder.append_option(v);
5966            }
5967            builder.finish()
5968        };
5969        let expected_binary_array_type = binary_view_array.data_type();
5970        let casted_binary_array = cast(&binary_dict_array, expected_binary_array_type).unwrap();
5971        assert_eq!(casted_binary_array.data_type(), expected_binary_array_type);
5972        assert_eq!(casted_binary_array.as_ref(), &binary_view_array);
5973    }
5974
5975    #[test]
5976    fn test_view_to_dict() {
5977        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
5978        let string_dict_array: DictionaryArray<Int8Type> = VIEW_TEST_DATA.into_iter().collect();
5979        let casted_type = string_dict_array.data_type();
5980        let casted_dict_array = cast(&string_view_array, casted_type).unwrap();
5981        assert_eq!(casted_dict_array.data_type(), casted_type);
5982        assert_eq!(casted_dict_array.as_ref(), &string_dict_array);
5983
5984        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
5985        let binary_dict_array = string_dict_array.downcast_dict::<StringArray>().unwrap();
5986        let binary_buffer = cast(&binary_dict_array.values(), &DataType::Binary).unwrap();
5987        let binary_dict_array =
5988            DictionaryArray::<Int8Type>::new(binary_dict_array.keys().clone(), binary_buffer);
5989        let casted_type = binary_dict_array.data_type();
5990        let casted_binary_array = cast(&binary_view_array, casted_type).unwrap();
5991        assert_eq!(casted_binary_array.data_type(), casted_type);
5992        assert_eq!(casted_binary_array.as_ref(), &binary_dict_array);
5993    }
5994
5995    #[test]
5996    fn test_view_to_string() {
5997        _test_view_to_string::<i32>();
5998        _test_view_to_string::<i64>();
5999    }
6000
6001    fn _test_view_to_string<O>()
6002    where
6003        O: OffsetSizeTrait,
6004    {
6005        let string_view_array = {
6006            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
6007            for s in VIEW_TEST_DATA.iter() {
6008                builder.append_option(*s);
6009            }
6010            builder.finish()
6011        };
6012
6013        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6014
6015        let expected_string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
6016        let expected_type = expected_string_array.data_type();
6017
6018        assert!(can_cast_types(string_view_array.data_type(), expected_type));
6019        assert!(can_cast_types(binary_view_array.data_type(), expected_type));
6020
6021        let string_view_casted_array = cast(&string_view_array, expected_type).unwrap();
6022        assert_eq!(string_view_casted_array.data_type(), expected_type);
6023        assert_eq!(string_view_casted_array.as_ref(), &expected_string_array);
6024
6025        let binary_view_casted_array = cast(&binary_view_array, expected_type).unwrap();
6026        assert_eq!(binary_view_casted_array.data_type(), expected_type);
6027        assert_eq!(binary_view_casted_array.as_ref(), &expected_string_array);
6028    }
6029
6030    #[test]
6031    fn test_view_to_binary() {
6032        _test_view_to_binary::<i32>();
6033        _test_view_to_binary::<i64>();
6034    }
6035
6036    fn _test_view_to_binary<O>()
6037    where
6038        O: OffsetSizeTrait,
6039    {
6040        let view_array = {
6041            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
6042            for s in VIEW_TEST_DATA.iter() {
6043                builder.append_option(*s);
6044            }
6045            builder.finish()
6046        };
6047
6048        let expected_binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
6049        let expected_type = expected_binary_array.data_type();
6050
6051        assert!(can_cast_types(view_array.data_type(), expected_type));
6052
6053        let binary_array = cast(&view_array, expected_type).unwrap();
6054        assert_eq!(binary_array.data_type(), expected_type);
6055
6056        assert_eq!(binary_array.as_ref(), &expected_binary_array);
6057    }
6058
6059    #[test]
6060    fn test_cast_from_f64() {
6061        let f64_values: Vec<f64> = vec![
6062            i64::MIN as f64,
6063            i32::MIN as f64,
6064            i16::MIN as f64,
6065            i8::MIN as f64,
6066            0_f64,
6067            u8::MAX as f64,
6068            u16::MAX as f64,
6069            u32::MAX as f64,
6070            u64::MAX as f64,
6071        ];
6072        let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values));
6073
6074        let f64_expected = vec![
6075            -9223372036854776000.0,
6076            -2147483648.0,
6077            -32768.0,
6078            -128.0,
6079            0.0,
6080            255.0,
6081            65535.0,
6082            4294967295.0,
6083            18446744073709552000.0,
6084        ];
6085        assert_eq!(
6086            f64_expected,
6087            get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
6088                .iter()
6089                .map(|i| i.parse::<f64>().unwrap())
6090                .collect::<Vec<f64>>()
6091        );
6092
6093        let f32_expected = vec![
6094            -9223372000000000000.0,
6095            -2147483600.0,
6096            -32768.0,
6097            -128.0,
6098            0.0,
6099            255.0,
6100            65535.0,
6101            4294967300.0,
6102            18446744000000000000.0,
6103        ];
6104        assert_eq!(
6105            f32_expected,
6106            get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
6107                .iter()
6108                .map(|i| i.parse::<f32>().unwrap())
6109                .collect::<Vec<f32>>()
6110        );
6111
6112        let f16_expected = vec![
6113            f16::from_f64(-9223372000000000000.0),
6114            f16::from_f64(-2147483600.0),
6115            f16::from_f64(-32768.0),
6116            f16::from_f64(-128.0),
6117            f16::from_f64(0.0),
6118            f16::from_f64(255.0),
6119            f16::from_f64(65535.0),
6120            f16::from_f64(4294967300.0),
6121            f16::from_f64(18446744000000000000.0),
6122        ];
6123        assert_eq!(
6124            f16_expected,
6125            get_cast_values::<Float16Type>(&f64_array, &DataType::Float16)
6126                .iter()
6127                .map(|i| i.parse::<f16>().unwrap())
6128                .collect::<Vec<f16>>()
6129        );
6130
6131        let i64_expected = vec![
6132            "-9223372036854775808",
6133            "-2147483648",
6134            "-32768",
6135            "-128",
6136            "0",
6137            "255",
6138            "65535",
6139            "4294967295",
6140            "null",
6141        ];
6142        assert_eq!(
6143            i64_expected,
6144            get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
6145        );
6146
6147        let i32_expected = vec![
6148            "null",
6149            "-2147483648",
6150            "-32768",
6151            "-128",
6152            "0",
6153            "255",
6154            "65535",
6155            "null",
6156            "null",
6157        ];
6158        assert_eq!(
6159            i32_expected,
6160            get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
6161        );
6162
6163        let i16_expected = vec![
6164            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
6165        ];
6166        assert_eq!(
6167            i16_expected,
6168            get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
6169        );
6170
6171        let i8_expected = vec![
6172            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
6173        ];
6174        assert_eq!(
6175            i8_expected,
6176            get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
6177        );
6178
6179        let u64_expected = vec![
6180            "null",
6181            "null",
6182            "null",
6183            "null",
6184            "0",
6185            "255",
6186            "65535",
6187            "4294967295",
6188            "null",
6189        ];
6190        assert_eq!(
6191            u64_expected,
6192            get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
6193        );
6194
6195        let u32_expected = vec![
6196            "null",
6197            "null",
6198            "null",
6199            "null",
6200            "0",
6201            "255",
6202            "65535",
6203            "4294967295",
6204            "null",
6205        ];
6206        assert_eq!(
6207            u32_expected,
6208            get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
6209        );
6210
6211        let u16_expected = vec![
6212            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
6213        ];
6214        assert_eq!(
6215            u16_expected,
6216            get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
6217        );
6218
6219        let u8_expected = vec![
6220            "null", "null", "null", "null", "0", "255", "null", "null", "null",
6221        ];
6222        assert_eq!(
6223            u8_expected,
6224            get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
6225        );
6226    }
6227
6228    #[test]
6229    fn test_cast_from_f32() {
6230        let f32_values: Vec<f32> = vec![
6231            i32::MIN as f32,
6232            i32::MIN as f32,
6233            i16::MIN as f32,
6234            i8::MIN as f32,
6235            0_f32,
6236            u8::MAX as f32,
6237            u16::MAX as f32,
6238            u32::MAX as f32,
6239            u32::MAX as f32,
6240        ];
6241        let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values));
6242
6243        let f64_expected = vec![
6244            "-2147483648.0",
6245            "-2147483648.0",
6246            "-32768.0",
6247            "-128.0",
6248            "0.0",
6249            "255.0",
6250            "65535.0",
6251            "4294967296.0",
6252            "4294967296.0",
6253        ];
6254        assert_eq!(
6255            f64_expected,
6256            get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
6257        );
6258
6259        let f32_expected = vec![
6260            "-2147483600.0",
6261            "-2147483600.0",
6262            "-32768.0",
6263            "-128.0",
6264            "0.0",
6265            "255.0",
6266            "65535.0",
6267            "4294967300.0",
6268            "4294967300.0",
6269        ];
6270        assert_eq!(
6271            f32_expected,
6272            get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
6273        );
6274
6275        let f16_expected = vec![
6276            "-inf", "-inf", "-32768.0", "-128.0", "0.0", "255.0", "inf", "inf", "inf",
6277        ];
6278        assert_eq!(
6279            f16_expected,
6280            get_cast_values::<Float16Type>(&f32_array, &DataType::Float16)
6281        );
6282
6283        let i64_expected = vec![
6284            "-2147483648",
6285            "-2147483648",
6286            "-32768",
6287            "-128",
6288            "0",
6289            "255",
6290            "65535",
6291            "4294967296",
6292            "4294967296",
6293        ];
6294        assert_eq!(
6295            i64_expected,
6296            get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
6297        );
6298
6299        let i32_expected = vec![
6300            "-2147483648",
6301            "-2147483648",
6302            "-32768",
6303            "-128",
6304            "0",
6305            "255",
6306            "65535",
6307            "null",
6308            "null",
6309        ];
6310        assert_eq!(
6311            i32_expected,
6312            get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
6313        );
6314
6315        let i16_expected = vec![
6316            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
6317        ];
6318        assert_eq!(
6319            i16_expected,
6320            get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
6321        );
6322
6323        let i8_expected = vec![
6324            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
6325        ];
6326        assert_eq!(
6327            i8_expected,
6328            get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
6329        );
6330
6331        let u64_expected = vec![
6332            "null",
6333            "null",
6334            "null",
6335            "null",
6336            "0",
6337            "255",
6338            "65535",
6339            "4294967296",
6340            "4294967296",
6341        ];
6342        assert_eq!(
6343            u64_expected,
6344            get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
6345        );
6346
6347        let u32_expected = vec![
6348            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
6349        ];
6350        assert_eq!(
6351            u32_expected,
6352            get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
6353        );
6354
6355        let u16_expected = vec![
6356            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
6357        ];
6358        assert_eq!(
6359            u16_expected,
6360            get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
6361        );
6362
6363        let u8_expected = vec![
6364            "null", "null", "null", "null", "0", "255", "null", "null", "null",
6365        ];
6366        assert_eq!(
6367            u8_expected,
6368            get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
6369        );
6370    }
6371
6372    #[test]
6373    fn test_cast_from_uint64() {
6374        let u64_values: Vec<u64> = vec![
6375            0,
6376            u8::MAX as u64,
6377            u16::MAX as u64,
6378            u32::MAX as u64,
6379            u64::MAX,
6380        ];
6381        let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values));
6382
6383        let f64_expected = vec![0.0, 255.0, 65535.0, 4294967295.0, 18446744073709552000.0];
6384        assert_eq!(
6385            f64_expected,
6386            get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
6387                .iter()
6388                .map(|i| i.parse::<f64>().unwrap())
6389                .collect::<Vec<f64>>()
6390        );
6391
6392        let f32_expected = vec![0.0, 255.0, 65535.0, 4294967300.0, 18446744000000000000.0];
6393        assert_eq!(
6394            f32_expected,
6395            get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
6396                .iter()
6397                .map(|i| i.parse::<f32>().unwrap())
6398                .collect::<Vec<f32>>()
6399        );
6400
6401        let f16_expected = vec![
6402            f16::from_f64(0.0),
6403            f16::from_f64(255.0),
6404            f16::from_f64(65535.0),
6405            f16::from_f64(4294967300.0),
6406            f16::from_f64(18446744000000000000.0),
6407        ];
6408        assert_eq!(
6409            f16_expected,
6410            get_cast_values::<Float16Type>(&u64_array, &DataType::Float16)
6411                .iter()
6412                .map(|i| i.parse::<f16>().unwrap())
6413                .collect::<Vec<f16>>()
6414        );
6415
6416        let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
6417        assert_eq!(
6418            i64_expected,
6419            get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
6420        );
6421
6422        let i32_expected = vec!["0", "255", "65535", "null", "null"];
6423        assert_eq!(
6424            i32_expected,
6425            get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
6426        );
6427
6428        let i16_expected = vec!["0", "255", "null", "null", "null"];
6429        assert_eq!(
6430            i16_expected,
6431            get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
6432        );
6433
6434        let i8_expected = vec!["0", "null", "null", "null", "null"];
6435        assert_eq!(
6436            i8_expected,
6437            get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
6438        );
6439
6440        let u64_expected = vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
6441        assert_eq!(
6442            u64_expected,
6443            get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
6444        );
6445
6446        let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
6447        assert_eq!(
6448            u32_expected,
6449            get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
6450        );
6451
6452        let u16_expected = vec!["0", "255", "65535", "null", "null"];
6453        assert_eq!(
6454            u16_expected,
6455            get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
6456        );
6457
6458        let u8_expected = vec!["0", "255", "null", "null", "null"];
6459        assert_eq!(
6460            u8_expected,
6461            get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
6462        );
6463    }
6464
6465    #[test]
6466    fn test_cast_from_uint32() {
6467        let u32_values: Vec<u32> = vec![0, u8::MAX as u32, u16::MAX as u32, u32::MAX];
6468        let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values));
6469
6470        let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
6471        assert_eq!(
6472            f64_expected,
6473            get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
6474        );
6475
6476        let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
6477        assert_eq!(
6478            f32_expected,
6479            get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
6480        );
6481
6482        let f16_expected = vec!["0.0", "255.0", "inf", "inf"];
6483        assert_eq!(
6484            f16_expected,
6485            get_cast_values::<Float16Type>(&u32_array, &DataType::Float16)
6486        );
6487
6488        let i64_expected = vec!["0", "255", "65535", "4294967295"];
6489        assert_eq!(
6490            i64_expected,
6491            get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
6492        );
6493
6494        let i32_expected = vec!["0", "255", "65535", "null"];
6495        assert_eq!(
6496            i32_expected,
6497            get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
6498        );
6499
6500        let i16_expected = vec!["0", "255", "null", "null"];
6501        assert_eq!(
6502            i16_expected,
6503            get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
6504        );
6505
6506        let i8_expected = vec!["0", "null", "null", "null"];
6507        assert_eq!(
6508            i8_expected,
6509            get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
6510        );
6511
6512        let u64_expected = vec!["0", "255", "65535", "4294967295"];
6513        assert_eq!(
6514            u64_expected,
6515            get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
6516        );
6517
6518        let u32_expected = vec!["0", "255", "65535", "4294967295"];
6519        assert_eq!(
6520            u32_expected,
6521            get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
6522        );
6523
6524        let u16_expected = vec!["0", "255", "65535", "null"];
6525        assert_eq!(
6526            u16_expected,
6527            get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
6528        );
6529
6530        let u8_expected = vec!["0", "255", "null", "null"];
6531        assert_eq!(
6532            u8_expected,
6533            get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
6534        );
6535    }
6536
6537    #[test]
6538    fn test_cast_from_uint16() {
6539        let u16_values: Vec<u16> = vec![0, u8::MAX as u16, u16::MAX];
6540        let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values));
6541
6542        let f64_expected = vec!["0.0", "255.0", "65535.0"];
6543        assert_eq!(
6544            f64_expected,
6545            get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
6546        );
6547
6548        let f32_expected = vec!["0.0", "255.0", "65535.0"];
6549        assert_eq!(
6550            f32_expected,
6551            get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
6552        );
6553
6554        let f16_expected = vec!["0.0", "255.0", "inf"];
6555        assert_eq!(
6556            f16_expected,
6557            get_cast_values::<Float16Type>(&u16_array, &DataType::Float16)
6558        );
6559
6560        let i64_expected = vec!["0", "255", "65535"];
6561        assert_eq!(
6562            i64_expected,
6563            get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
6564        );
6565
6566        let i32_expected = vec!["0", "255", "65535"];
6567        assert_eq!(
6568            i32_expected,
6569            get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
6570        );
6571
6572        let i16_expected = vec!["0", "255", "null"];
6573        assert_eq!(
6574            i16_expected,
6575            get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
6576        );
6577
6578        let i8_expected = vec!["0", "null", "null"];
6579        assert_eq!(
6580            i8_expected,
6581            get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
6582        );
6583
6584        let u64_expected = vec!["0", "255", "65535"];
6585        assert_eq!(
6586            u64_expected,
6587            get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
6588        );
6589
6590        let u32_expected = vec!["0", "255", "65535"];
6591        assert_eq!(
6592            u32_expected,
6593            get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
6594        );
6595
6596        let u16_expected = vec!["0", "255", "65535"];
6597        assert_eq!(
6598            u16_expected,
6599            get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
6600        );
6601
6602        let u8_expected = vec!["0", "255", "null"];
6603        assert_eq!(
6604            u8_expected,
6605            get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
6606        );
6607    }
6608
6609    #[test]
6610    fn test_cast_from_uint8() {
6611        let u8_values: Vec<u8> = vec![0, u8::MAX];
6612        let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values));
6613
6614        let f64_expected = vec!["0.0", "255.0"];
6615        assert_eq!(
6616            f64_expected,
6617            get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
6618        );
6619
6620        let f32_expected = vec!["0.0", "255.0"];
6621        assert_eq!(
6622            f32_expected,
6623            get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
6624        );
6625
6626        let f16_expected = vec!["0.0", "255.0"];
6627        assert_eq!(
6628            f16_expected,
6629            get_cast_values::<Float16Type>(&u8_array, &DataType::Float16)
6630        );
6631
6632        let i64_expected = vec!["0", "255"];
6633        assert_eq!(
6634            i64_expected,
6635            get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
6636        );
6637
6638        let i32_expected = vec!["0", "255"];
6639        assert_eq!(
6640            i32_expected,
6641            get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
6642        );
6643
6644        let i16_expected = vec!["0", "255"];
6645        assert_eq!(
6646            i16_expected,
6647            get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
6648        );
6649
6650        let i8_expected = vec!["0", "null"];
6651        assert_eq!(
6652            i8_expected,
6653            get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
6654        );
6655
6656        let u64_expected = vec!["0", "255"];
6657        assert_eq!(
6658            u64_expected,
6659            get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
6660        );
6661
6662        let u32_expected = vec!["0", "255"];
6663        assert_eq!(
6664            u32_expected,
6665            get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
6666        );
6667
6668        let u16_expected = vec!["0", "255"];
6669        assert_eq!(
6670            u16_expected,
6671            get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
6672        );
6673
6674        let u8_expected = vec!["0", "255"];
6675        assert_eq!(
6676            u8_expected,
6677            get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
6678        );
6679    }
6680
6681    #[test]
6682    fn test_cast_from_int64() {
6683        let i64_values: Vec<i64> = vec![
6684            i64::MIN,
6685            i32::MIN as i64,
6686            i16::MIN as i64,
6687            i8::MIN as i64,
6688            0,
6689            i8::MAX as i64,
6690            i16::MAX as i64,
6691            i32::MAX as i64,
6692            i64::MAX,
6693        ];
6694        let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values));
6695
6696        let f64_expected = vec![
6697            -9223372036854776000.0,
6698            -2147483648.0,
6699            -32768.0,
6700            -128.0,
6701            0.0,
6702            127.0,
6703            32767.0,
6704            2147483647.0,
6705            9223372036854776000.0,
6706        ];
6707        assert_eq!(
6708            f64_expected,
6709            get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
6710                .iter()
6711                .map(|i| i.parse::<f64>().unwrap())
6712                .collect::<Vec<f64>>()
6713        );
6714
6715        let f32_expected = vec![
6716            -9223372000000000000.0,
6717            -2147483600.0,
6718            -32768.0,
6719            -128.0,
6720            0.0,
6721            127.0,
6722            32767.0,
6723            2147483600.0,
6724            9223372000000000000.0,
6725        ];
6726        assert_eq!(
6727            f32_expected,
6728            get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
6729                .iter()
6730                .map(|i| i.parse::<f32>().unwrap())
6731                .collect::<Vec<f32>>()
6732        );
6733
6734        let f16_expected = vec![
6735            f16::from_f64(-9223372000000000000.0),
6736            f16::from_f64(-2147483600.0),
6737            f16::from_f64(-32768.0),
6738            f16::from_f64(-128.0),
6739            f16::from_f64(0.0),
6740            f16::from_f64(127.0),
6741            f16::from_f64(32767.0),
6742            f16::from_f64(2147483600.0),
6743            f16::from_f64(9223372000000000000.0),
6744        ];
6745        assert_eq!(
6746            f16_expected,
6747            get_cast_values::<Float16Type>(&i64_array, &DataType::Float16)
6748                .iter()
6749                .map(|i| i.parse::<f16>().unwrap())
6750                .collect::<Vec<f16>>()
6751        );
6752
6753        let i64_expected = vec![
6754            "-9223372036854775808",
6755            "-2147483648",
6756            "-32768",
6757            "-128",
6758            "0",
6759            "127",
6760            "32767",
6761            "2147483647",
6762            "9223372036854775807",
6763        ];
6764        assert_eq!(
6765            i64_expected,
6766            get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
6767        );
6768
6769        let i32_expected = vec![
6770            "null",
6771            "-2147483648",
6772            "-32768",
6773            "-128",
6774            "0",
6775            "127",
6776            "32767",
6777            "2147483647",
6778            "null",
6779        ];
6780        assert_eq!(
6781            i32_expected,
6782            get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
6783        );
6784
6785        assert_eq!(
6786            i32_expected,
6787            get_cast_values::<Date32Type>(&i64_array, &DataType::Date32)
6788        );
6789
6790        let i16_expected = vec![
6791            "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
6792        ];
6793        assert_eq!(
6794            i16_expected,
6795            get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
6796        );
6797
6798        let i8_expected = vec![
6799            "null", "null", "null", "-128", "0", "127", "null", "null", "null",
6800        ];
6801        assert_eq!(
6802            i8_expected,
6803            get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
6804        );
6805
6806        let u64_expected = vec![
6807            "null",
6808            "null",
6809            "null",
6810            "null",
6811            "0",
6812            "127",
6813            "32767",
6814            "2147483647",
6815            "9223372036854775807",
6816        ];
6817        assert_eq!(
6818            u64_expected,
6819            get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
6820        );
6821
6822        let u32_expected = vec![
6823            "null",
6824            "null",
6825            "null",
6826            "null",
6827            "0",
6828            "127",
6829            "32767",
6830            "2147483647",
6831            "null",
6832        ];
6833        assert_eq!(
6834            u32_expected,
6835            get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
6836        );
6837
6838        let u16_expected = vec![
6839            "null", "null", "null", "null", "0", "127", "32767", "null", "null",
6840        ];
6841        assert_eq!(
6842            u16_expected,
6843            get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
6844        );
6845
6846        let u8_expected = vec![
6847            "null", "null", "null", "null", "0", "127", "null", "null", "null",
6848        ];
6849        assert_eq!(
6850            u8_expected,
6851            get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
6852        );
6853    }
6854
6855    #[test]
6856    fn test_cast_from_int32() {
6857        let i32_values: Vec<i32> = vec![
6858            i32::MIN,
6859            i16::MIN as i32,
6860            i8::MIN as i32,
6861            0,
6862            i8::MAX as i32,
6863            i16::MAX as i32,
6864            i32::MAX,
6865        ];
6866        let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values));
6867
6868        let f64_expected = vec![
6869            "-2147483648.0",
6870            "-32768.0",
6871            "-128.0",
6872            "0.0",
6873            "127.0",
6874            "32767.0",
6875            "2147483647.0",
6876        ];
6877        assert_eq!(
6878            f64_expected,
6879            get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
6880        );
6881
6882        let f32_expected = vec![
6883            "-2147483600.0",
6884            "-32768.0",
6885            "-128.0",
6886            "0.0",
6887            "127.0",
6888            "32767.0",
6889            "2147483600.0",
6890        ];
6891        assert_eq!(
6892            f32_expected,
6893            get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
6894        );
6895
6896        let f16_expected = vec![
6897            f16::from_f64(-2147483600.0),
6898            f16::from_f64(-32768.0),
6899            f16::from_f64(-128.0),
6900            f16::from_f64(0.0),
6901            f16::from_f64(127.0),
6902            f16::from_f64(32767.0),
6903            f16::from_f64(2147483600.0),
6904        ];
6905        assert_eq!(
6906            f16_expected,
6907            get_cast_values::<Float16Type>(&i32_array, &DataType::Float16)
6908                .iter()
6909                .map(|i| i.parse::<f16>().unwrap())
6910                .collect::<Vec<f16>>()
6911        );
6912
6913        let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
6914        assert_eq!(
6915            i16_expected,
6916            get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
6917        );
6918
6919        let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
6920        assert_eq!(
6921            i8_expected,
6922            get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
6923        );
6924
6925        let u64_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
6926        assert_eq!(
6927            u64_expected,
6928            get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
6929        );
6930
6931        let u32_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
6932        assert_eq!(
6933            u32_expected,
6934            get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
6935        );
6936
6937        let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
6938        assert_eq!(
6939            u16_expected,
6940            get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
6941        );
6942
6943        let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
6944        assert_eq!(
6945            u8_expected,
6946            get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
6947        );
6948
6949        // The date32 to date64 cast increases the numerical values in order to keep the same dates.
6950        let i64_expected = vec![
6951            "-185542587187200000",
6952            "-2831155200000",
6953            "-11059200000",
6954            "0",
6955            "10972800000",
6956            "2831068800000",
6957            "185542587100800000",
6958        ];
6959        assert_eq!(
6960            i64_expected,
6961            get_cast_values::<Date64Type>(&i32_array, &DataType::Date64)
6962        );
6963    }
6964
6965    #[test]
6966    fn test_cast_from_int16() {
6967        let i16_values: Vec<i16> = vec![i16::MIN, i8::MIN as i16, 0, i8::MAX as i16, i16::MAX];
6968        let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values));
6969
6970        let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
6971        assert_eq!(
6972            f64_expected,
6973            get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
6974        );
6975
6976        let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
6977        assert_eq!(
6978            f32_expected,
6979            get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
6980        );
6981
6982        let f16_expected = vec![
6983            f16::from_f64(-32768.0),
6984            f16::from_f64(-128.0),
6985            f16::from_f64(0.0),
6986            f16::from_f64(127.0),
6987            f16::from_f64(32767.0),
6988        ];
6989        assert_eq!(
6990            f16_expected,
6991            get_cast_values::<Float16Type>(&i16_array, &DataType::Float16)
6992                .iter()
6993                .map(|i| i.parse::<f16>().unwrap())
6994                .collect::<Vec<f16>>()
6995        );
6996
6997        let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
6998        assert_eq!(
6999            i64_expected,
7000            get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
7001        );
7002
7003        let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
7004        assert_eq!(
7005            i32_expected,
7006            get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
7007        );
7008
7009        let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
7010        assert_eq!(
7011            i16_expected,
7012            get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
7013        );
7014
7015        let i8_expected = vec!["null", "-128", "0", "127", "null"];
7016        assert_eq!(
7017            i8_expected,
7018            get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
7019        );
7020
7021        let u64_expected = vec!["null", "null", "0", "127", "32767"];
7022        assert_eq!(
7023            u64_expected,
7024            get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
7025        );
7026
7027        let u32_expected = vec!["null", "null", "0", "127", "32767"];
7028        assert_eq!(
7029            u32_expected,
7030            get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
7031        );
7032
7033        let u16_expected = vec!["null", "null", "0", "127", "32767"];
7034        assert_eq!(
7035            u16_expected,
7036            get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
7037        );
7038
7039        let u8_expected = vec!["null", "null", "0", "127", "null"];
7040        assert_eq!(
7041            u8_expected,
7042            get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
7043        );
7044    }
7045
7046    #[test]
7047    fn test_cast_from_date32() {
7048        let i32_values: Vec<i32> = vec![
7049            i32::MIN,
7050            i16::MIN as i32,
7051            i8::MIN as i32,
7052            0,
7053            i8::MAX as i32,
7054            i16::MAX as i32,
7055            i32::MAX,
7056        ];
7057        let date32_array: ArrayRef = Arc::new(Date32Array::from(i32_values));
7058
7059        let i64_expected = vec![
7060            "-2147483648",
7061            "-32768",
7062            "-128",
7063            "0",
7064            "127",
7065            "32767",
7066            "2147483647",
7067        ];
7068        assert_eq!(
7069            i64_expected,
7070            get_cast_values::<Int64Type>(&date32_array, &DataType::Int64)
7071        );
7072    }
7073
7074    #[test]
7075    fn test_cast_from_int8() {
7076        let i8_values: Vec<i8> = vec![i8::MIN, 0, i8::MAX];
7077        let i8_array = Int8Array::from(i8_values);
7078
7079        let f64_expected = vec!["-128.0", "0.0", "127.0"];
7080        assert_eq!(
7081            f64_expected,
7082            get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
7083        );
7084
7085        let f32_expected = vec!["-128.0", "0.0", "127.0"];
7086        assert_eq!(
7087            f32_expected,
7088            get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
7089        );
7090
7091        let f16_expected = vec!["-128.0", "0.0", "127.0"];
7092        assert_eq!(
7093            f16_expected,
7094            get_cast_values::<Float16Type>(&i8_array, &DataType::Float16)
7095        );
7096
7097        let i64_expected = vec!["-128", "0", "127"];
7098        assert_eq!(
7099            i64_expected,
7100            get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
7101        );
7102
7103        let i32_expected = vec!["-128", "0", "127"];
7104        assert_eq!(
7105            i32_expected,
7106            get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
7107        );
7108
7109        let i16_expected = vec!["-128", "0", "127"];
7110        assert_eq!(
7111            i16_expected,
7112            get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
7113        );
7114
7115        let i8_expected = vec!["-128", "0", "127"];
7116        assert_eq!(
7117            i8_expected,
7118            get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
7119        );
7120
7121        let u64_expected = vec!["null", "0", "127"];
7122        assert_eq!(
7123            u64_expected,
7124            get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
7125        );
7126
7127        let u32_expected = vec!["null", "0", "127"];
7128        assert_eq!(
7129            u32_expected,
7130            get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
7131        );
7132
7133        let u16_expected = vec!["null", "0", "127"];
7134        assert_eq!(
7135            u16_expected,
7136            get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
7137        );
7138
7139        let u8_expected = vec!["null", "0", "127"];
7140        assert_eq!(
7141            u8_expected,
7142            get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
7143        );
7144    }
7145
7146    /// Convert `array` into a vector of strings by casting to data type dt
7147    fn get_cast_values<T>(array: &dyn Array, dt: &DataType) -> Vec<String>
7148    where
7149        T: ArrowPrimitiveType,
7150    {
7151        let c = cast(array, dt).unwrap();
7152        let a = c.as_primitive::<T>();
7153        let mut v: Vec<String> = vec![];
7154        for i in 0..array.len() {
7155            if a.is_null(i) {
7156                v.push("null".to_string())
7157            } else {
7158                v.push(format!("{:?}", a.value(i)));
7159            }
7160        }
7161        v
7162    }
7163
7164    #[test]
7165    fn test_cast_utf8_dict() {
7166        // FROM a dictionary with of Utf8 values
7167        use DataType::*;
7168
7169        let mut builder = StringDictionaryBuilder::<Int8Type>::new();
7170        builder.append("one").unwrap();
7171        builder.append_null();
7172        builder.append("three").unwrap();
7173        let array: ArrayRef = Arc::new(builder.finish());
7174
7175        let expected = vec!["one", "null", "three"];
7176
7177        // Test casting TO StringArray
7178        let cast_type = Utf8;
7179        let cast_array = cast(&array, &cast_type).expect("cast to UTF-8 failed");
7180        assert_eq!(cast_array.data_type(), &cast_type);
7181        assert_eq!(array_to_strings(&cast_array), expected);
7182
7183        // Test casting TO Dictionary (with different index sizes)
7184
7185        let cast_type = Dictionary(Box::new(Int16), Box::new(Utf8));
7186        let cast_array = cast(&array, &cast_type).expect("cast failed");
7187        assert_eq!(cast_array.data_type(), &cast_type);
7188        assert_eq!(array_to_strings(&cast_array), expected);
7189
7190        let cast_type = Dictionary(Box::new(Int32), Box::new(Utf8));
7191        let cast_array = cast(&array, &cast_type).expect("cast failed");
7192        assert_eq!(cast_array.data_type(), &cast_type);
7193        assert_eq!(array_to_strings(&cast_array), expected);
7194
7195        let cast_type = Dictionary(Box::new(Int64), Box::new(Utf8));
7196        let cast_array = cast(&array, &cast_type).expect("cast failed");
7197        assert_eq!(cast_array.data_type(), &cast_type);
7198        assert_eq!(array_to_strings(&cast_array), expected);
7199
7200        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
7201        let cast_array = cast(&array, &cast_type).expect("cast failed");
7202        assert_eq!(cast_array.data_type(), &cast_type);
7203        assert_eq!(array_to_strings(&cast_array), expected);
7204
7205        let cast_type = Dictionary(Box::new(UInt16), Box::new(Utf8));
7206        let cast_array = cast(&array, &cast_type).expect("cast failed");
7207        assert_eq!(cast_array.data_type(), &cast_type);
7208        assert_eq!(array_to_strings(&cast_array), expected);
7209
7210        let cast_type = Dictionary(Box::new(UInt32), Box::new(Utf8));
7211        let cast_array = cast(&array, &cast_type).expect("cast failed");
7212        assert_eq!(cast_array.data_type(), &cast_type);
7213        assert_eq!(array_to_strings(&cast_array), expected);
7214
7215        let cast_type = Dictionary(Box::new(UInt64), Box::new(Utf8));
7216        let cast_array = cast(&array, &cast_type).expect("cast failed");
7217        assert_eq!(cast_array.data_type(), &cast_type);
7218        assert_eq!(array_to_strings(&cast_array), expected);
7219    }
7220
7221    #[test]
7222    fn test_cast_dict_to_dict_bad_index_value_primitive() {
7223        use DataType::*;
7224        // test converting from an array that has indexes of a type
7225        // that are out of bounds for a particular other kind of
7226        // index.
7227
7228        let mut builder = PrimitiveDictionaryBuilder::<Int32Type, Int64Type>::new();
7229
7230        // add 200 distinct values (which can be stored by a
7231        // dictionary indexed by int32, but not a dictionary indexed
7232        // with int8)
7233        for i in 0..200 {
7234            builder.append(i).unwrap();
7235        }
7236        let array: ArrayRef = Arc::new(builder.finish());
7237
7238        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
7239        let res = cast(&array, &cast_type);
7240        assert!(res.is_err());
7241        let actual_error = format!("{res:?}");
7242        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
7243        assert!(
7244            actual_error.contains(expected_error),
7245            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
7246        );
7247    }
7248
7249    #[test]
7250    fn test_cast_dict_to_dict_bad_index_value_utf8() {
7251        use DataType::*;
7252        // Same test as test_cast_dict_to_dict_bad_index_value but use
7253        // string values (and encode the expected behavior here);
7254
7255        let mut builder = StringDictionaryBuilder::<Int32Type>::new();
7256
7257        // add 200 distinct values (which can be stored by a
7258        // dictionary indexed by int32, but not a dictionary indexed
7259        // with int8)
7260        for i in 0..200 {
7261            let val = format!("val{i}");
7262            builder.append(&val).unwrap();
7263        }
7264        let array = builder.finish();
7265
7266        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
7267        let res = cast(&array, &cast_type);
7268        assert!(res.is_err());
7269        let actual_error = format!("{res:?}");
7270        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
7271        assert!(
7272            actual_error.contains(expected_error),
7273            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
7274        );
7275    }
7276
7277    #[test]
7278    fn test_cast_primitive_dict() {
7279        // FROM a dictionary with of INT32 values
7280        use DataType::*;
7281
7282        let mut builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
7283        builder.append(1).unwrap();
7284        builder.append_null();
7285        builder.append(3).unwrap();
7286        let array: ArrayRef = Arc::new(builder.finish());
7287
7288        let expected = vec!["1", "null", "3"];
7289
7290        // Test casting TO PrimitiveArray, different dictionary type
7291        let cast_array = cast(&array, &Utf8).expect("cast to UTF-8 failed");
7292        assert_eq!(array_to_strings(&cast_array), expected);
7293        assert_eq!(cast_array.data_type(), &Utf8);
7294
7295        let cast_array = cast(&array, &Int64).expect("cast to int64 failed");
7296        assert_eq!(array_to_strings(&cast_array), expected);
7297        assert_eq!(cast_array.data_type(), &Int64);
7298    }
7299
7300    #[test]
7301    fn test_cast_primitive_array_to_dict() {
7302        use DataType::*;
7303
7304        let mut builder = PrimitiveBuilder::<Int32Type>::new();
7305        builder.append_value(1);
7306        builder.append_null();
7307        builder.append_value(3);
7308        let array: ArrayRef = Arc::new(builder.finish());
7309
7310        let expected = vec!["1", "null", "3"];
7311
7312        // Cast to a dictionary (same value type, Int32)
7313        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int32));
7314        let cast_array = cast(&array, &cast_type).expect("cast failed");
7315        assert_eq!(cast_array.data_type(), &cast_type);
7316        assert_eq!(array_to_strings(&cast_array), expected);
7317
7318        // Cast to a dictionary (different value type, Int8)
7319        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int8));
7320        let cast_array = cast(&array, &cast_type).expect("cast failed");
7321        assert_eq!(cast_array.data_type(), &cast_type);
7322        assert_eq!(array_to_strings(&cast_array), expected);
7323    }
7324
7325    #[test]
7326    fn test_cast_time_array_to_dict() {
7327        use DataType::*;
7328
7329        let array = Arc::new(Date32Array::from(vec![Some(1000), None, Some(2000)])) as ArrayRef;
7330
7331        let expected = vec!["1972-09-27", "null", "1975-06-24"];
7332
7333        let cast_type = Dictionary(Box::new(UInt8), Box::new(Date32));
7334        let cast_array = cast(&array, &cast_type).expect("cast failed");
7335        assert_eq!(cast_array.data_type(), &cast_type);
7336        assert_eq!(array_to_strings(&cast_array), expected);
7337    }
7338
7339    #[test]
7340    fn test_cast_timestamp_array_to_dict() {
7341        use DataType::*;
7342
7343        let array = Arc::new(
7344            TimestampSecondArray::from(vec![Some(1000), None, Some(2000)]).with_timezone_utc(),
7345        ) as ArrayRef;
7346
7347        let expected = vec!["1970-01-01T00:16:40", "null", "1970-01-01T00:33:20"];
7348
7349        let cast_type = Dictionary(Box::new(UInt8), Box::new(Timestamp(TimeUnit::Second, None)));
7350        let cast_array = cast(&array, &cast_type).expect("cast failed");
7351        assert_eq!(cast_array.data_type(), &cast_type);
7352        assert_eq!(array_to_strings(&cast_array), expected);
7353    }
7354
7355    #[test]
7356    fn test_cast_string_array_to_dict() {
7357        use DataType::*;
7358
7359        let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) as ArrayRef;
7360
7361        let expected = vec!["one", "null", "three"];
7362
7363        // Cast to a dictionary (same value type, Utf8)
7364        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
7365        let cast_array = cast(&array, &cast_type).expect("cast failed");
7366        assert_eq!(cast_array.data_type(), &cast_type);
7367        assert_eq!(array_to_strings(&cast_array), expected);
7368    }
7369
7370    #[test]
7371    fn test_cast_null_array_to_from_decimal_array() {
7372        let data_type = DataType::Decimal128(12, 4);
7373        let array = new_null_array(&DataType::Null, 4);
7374        assert_eq!(array.data_type(), &DataType::Null);
7375        let cast_array = cast(&array, &data_type).expect("cast failed");
7376        assert_eq!(cast_array.data_type(), &data_type);
7377        for i in 0..4 {
7378            assert!(cast_array.is_null(i));
7379        }
7380
7381        let array = new_null_array(&data_type, 4);
7382        assert_eq!(array.data_type(), &data_type);
7383        let cast_array = cast(&array, &DataType::Null).expect("cast failed");
7384        assert_eq!(cast_array.data_type(), &DataType::Null);
7385        assert_eq!(cast_array.len(), 4);
7386        assert_eq!(cast_array.logical_nulls().unwrap().null_count(), 4);
7387    }
7388
7389    #[test]
7390    fn test_cast_null_array_from_and_to_primitive_array() {
7391        macro_rules! typed_test {
7392            ($ARR_TYPE:ident, $DATATYPE:ident, $TYPE:tt) => {{
7393                {
7394                    let array = Arc::new(NullArray::new(6)) as ArrayRef;
7395                    let expected = $ARR_TYPE::from(vec![None; 6]);
7396                    let cast_type = DataType::$DATATYPE;
7397                    let cast_array = cast(&array, &cast_type).expect("cast failed");
7398                    let cast_array = cast_array.as_primitive::<$TYPE>();
7399                    assert_eq!(cast_array.data_type(), &cast_type);
7400                    assert_eq!(cast_array, &expected);
7401                }
7402            }};
7403        }
7404
7405        typed_test!(Int16Array, Int16, Int16Type);
7406        typed_test!(Int32Array, Int32, Int32Type);
7407        typed_test!(Int64Array, Int64, Int64Type);
7408
7409        typed_test!(UInt16Array, UInt16, UInt16Type);
7410        typed_test!(UInt32Array, UInt32, UInt32Type);
7411        typed_test!(UInt64Array, UInt64, UInt64Type);
7412
7413        typed_test!(Float32Array, Float32, Float32Type);
7414        typed_test!(Float64Array, Float64, Float64Type);
7415
7416        typed_test!(Date32Array, Date32, Date32Type);
7417        typed_test!(Date64Array, Date64, Date64Type);
7418    }
7419
7420    fn cast_from_null_to_other(data_type: &DataType) {
7421        // Cast from null to data_type
7422        {
7423            let array = new_null_array(&DataType::Null, 4);
7424            assert_eq!(array.data_type(), &DataType::Null);
7425            let cast_array = cast(&array, data_type).expect("cast failed");
7426            assert_eq!(cast_array.data_type(), data_type);
7427            for i in 0..4 {
7428                assert!(cast_array.is_null(i));
7429            }
7430        }
7431    }
7432
7433    #[test]
7434    fn test_cast_null_from_and_to_variable_sized() {
7435        cast_from_null_to_other(&DataType::Utf8);
7436        cast_from_null_to_other(&DataType::LargeUtf8);
7437        cast_from_null_to_other(&DataType::Binary);
7438        cast_from_null_to_other(&DataType::LargeBinary);
7439    }
7440
7441    #[test]
7442    fn test_cast_null_from_and_to_nested_type() {
7443        // Cast null from and to map
7444        let data_type = DataType::Map(
7445            Arc::new(Field::new_struct(
7446                "entry",
7447                vec![
7448                    Field::new("key", DataType::Utf8, false),
7449                    Field::new("value", DataType::Int32, true),
7450                ],
7451                false,
7452            )),
7453            false,
7454        );
7455        cast_from_null_to_other(&data_type);
7456
7457        // Cast null from and to list
7458        let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
7459        cast_from_null_to_other(&data_type);
7460        let data_type = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
7461        cast_from_null_to_other(&data_type);
7462        let data_type =
7463            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
7464        cast_from_null_to_other(&data_type);
7465
7466        // Cast null from and to dictionary
7467        let values = vec![None, None, None, None] as Vec<Option<&str>>;
7468        let array: DictionaryArray<Int8Type> = values.into_iter().collect();
7469        let array = Arc::new(array) as ArrayRef;
7470        let data_type = array.data_type().to_owned();
7471        cast_from_null_to_other(&data_type);
7472
7473        // Cast null from and to struct
7474        let data_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
7475        cast_from_null_to_other(&data_type);
7476    }
7477
7478    /// Print the `DictionaryArray` `array` as a vector of strings
7479    fn array_to_strings(array: &ArrayRef) -> Vec<String> {
7480        let options = FormatOptions::new().with_null("null");
7481        let formatter = ArrayFormatter::try_new(array.as_ref(), &options).unwrap();
7482        (0..array.len())
7483            .map(|i| formatter.value(i).to_string())
7484            .collect()
7485    }
7486
7487    #[test]
7488    fn test_cast_utf8_to_date32() {
7489        use chrono::NaiveDate;
7490        let from_ymd = chrono::NaiveDate::from_ymd_opt;
7491        let since = chrono::NaiveDate::signed_duration_since;
7492
7493        let a = StringArray::from(vec![
7494            "2000-01-01",          // valid date with leading 0s
7495            "2000-01-01T12:00:00", // valid datetime, will throw away the time part
7496            "2000-2-2",            // valid date without leading 0s
7497            "2000-00-00",          // invalid month and day
7498            "2000",                // just a year is invalid
7499        ]);
7500        let array = Arc::new(a) as ArrayRef;
7501        let b = cast(&array, &DataType::Date32).unwrap();
7502        let c = b.as_primitive::<Date32Type>();
7503
7504        // test valid inputs
7505        let date_value = since(
7506            NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
7507            from_ymd(1970, 1, 1).unwrap(),
7508        )
7509        .num_days() as i32;
7510        assert!(c.is_valid(0)); // "2000-01-01"
7511        assert_eq!(date_value, c.value(0));
7512
7513        assert!(c.is_valid(1)); // "2000-01-01T12:00:00"
7514        assert_eq!(date_value, c.value(1));
7515
7516        let date_value = since(
7517            NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
7518            from_ymd(1970, 1, 1).unwrap(),
7519        )
7520        .num_days() as i32;
7521        assert!(c.is_valid(2)); // "2000-2-2"
7522        assert_eq!(date_value, c.value(2));
7523
7524        // test invalid inputs
7525        assert!(!c.is_valid(3)); // "2000-00-00"
7526        assert!(!c.is_valid(4)); // "2000"
7527    }
7528
7529    #[test]
7530    fn test_cast_utf8_to_date64() {
7531        let a = StringArray::from(vec![
7532            "2000-01-01T12:00:00", // date + time valid
7533            "2020-12-15T12:34:56", // date + time valid
7534            "2020-2-2T12:34:56",   // valid date time without leading 0s
7535            "2000-00-00T12:00:00", // invalid month and day
7536            "2000-01-01 12:00:00", // missing the 'T'
7537            "2000-01-01",          // just a date is invalid
7538        ]);
7539        let array = Arc::new(a) as ArrayRef;
7540        let b = cast(&array, &DataType::Date64).unwrap();
7541        let c = b.as_primitive::<Date64Type>();
7542
7543        // test valid inputs
7544        assert!(c.is_valid(0)); // "2000-01-01T12:00:00"
7545        assert_eq!(946728000000, c.value(0));
7546        assert!(c.is_valid(1)); // "2020-12-15T12:34:56"
7547        assert_eq!(1608035696000, c.value(1));
7548        assert!(!c.is_valid(2)); // "2020-2-2T12:34:56"
7549
7550        assert!(!c.is_valid(3)); // "2000-00-00T12:00:00"
7551        assert!(c.is_valid(4)); // "2000-01-01 12:00:00"
7552        assert_eq!(946728000000, c.value(4));
7553        assert!(c.is_valid(5)); // "2000-01-01"
7554        assert_eq!(946684800000, c.value(5));
7555    }
7556
7557    #[test]
7558    fn test_can_cast_fsl_to_fsl() {
7559        let from_array = Arc::new(
7560            FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
7561                [Some([Some(1.0), Some(2.0)]), None],
7562                2,
7563            ),
7564        ) as ArrayRef;
7565        let to_array = Arc::new(
7566            FixedSizeListArray::from_iter_primitive::<Float16Type, _, _>(
7567                [
7568                    Some([Some(f16::from_f32(1.0)), Some(f16::from_f32(2.0))]),
7569                    None,
7570                ],
7571                2,
7572            ),
7573        ) as ArrayRef;
7574
7575        assert!(can_cast_types(from_array.data_type(), to_array.data_type()));
7576        let actual = cast(&from_array, to_array.data_type()).unwrap();
7577        assert_eq!(actual.data_type(), to_array.data_type());
7578
7579        let invalid_target =
7580            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Binary, true)), 2);
7581        assert!(!can_cast_types(from_array.data_type(), &invalid_target));
7582
7583        let invalid_size =
7584            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Float16, true)), 5);
7585        assert!(!can_cast_types(from_array.data_type(), &invalid_size));
7586    }
7587
7588    #[test]
7589    fn test_can_cast_types_fixed_size_list_to_list() {
7590        // DataType::List
7591        let array1 = Arc::new(make_fixed_size_list_array()) as ArrayRef;
7592        assert!(can_cast_types(
7593            array1.data_type(),
7594            &DataType::List(Arc::new(Field::new("", DataType::Int32, false)))
7595        ));
7596
7597        // DataType::LargeList
7598        let array2 = Arc::new(make_fixed_size_list_array_for_large_list()) as ArrayRef;
7599        assert!(can_cast_types(
7600            array2.data_type(),
7601            &DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false)))
7602        ));
7603    }
7604
7605    #[test]
7606    fn test_cast_fixed_size_list_to_list() {
7607        // Important cases:
7608        // 1. With/without nulls
7609        // 2. LargeList and List
7610        // 3. With and without inner casts
7611
7612        let cases = [
7613            // fixed_size_list<i32, 2> => list<i32>
7614            (
7615                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7616                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
7617                    2,
7618                )) as ArrayRef,
7619                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
7620                    Some([Some(1), Some(1)]),
7621                    Some([Some(2), Some(2)]),
7622                ])) as ArrayRef,
7623            ),
7624            // fixed_size_list<i32, 2> => list<i32> (nullable)
7625            (
7626                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7627                    [None, Some([Some(2), Some(2)])],
7628                    2,
7629                )) as ArrayRef,
7630                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
7631                    None,
7632                    Some([Some(2), Some(2)]),
7633                ])) as ArrayRef,
7634            ),
7635            // fixed_size_list<i32, 2> => large_list<i64>
7636            (
7637                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7638                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
7639                    2,
7640                )) as ArrayRef,
7641                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
7642                    Some([Some(1), Some(1)]),
7643                    Some([Some(2), Some(2)]),
7644                ])) as ArrayRef,
7645            ),
7646            // fixed_size_list<i32, 2> => large_list<i64> (nullable)
7647            (
7648                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7649                    [None, Some([Some(2), Some(2)])],
7650                    2,
7651                )) as ArrayRef,
7652                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
7653                    None,
7654                    Some([Some(2), Some(2)]),
7655                ])) as ArrayRef,
7656            ),
7657        ];
7658
7659        for (array, expected) in cases {
7660            let array = Arc::new(array) as ArrayRef;
7661
7662            assert!(
7663                can_cast_types(array.data_type(), expected.data_type()),
7664                "can_cast_types claims we cannot cast {:?} to {:?}",
7665                array.data_type(),
7666                expected.data_type()
7667            );
7668
7669            let list_array = cast(&array, expected.data_type())
7670                .unwrap_or_else(|_| panic!("Failed to cast {:?} to {:?}", array, expected));
7671            assert_eq!(
7672                list_array.as_ref(),
7673                &expected,
7674                "Incorrect result from casting {:?} to {:?}",
7675                array,
7676                expected
7677            );
7678        }
7679    }
7680
7681    #[test]
7682    fn test_cast_utf8_to_list() {
7683        // DataType::List
7684        let array = Arc::new(StringArray::from(vec!["5"])) as ArrayRef;
7685        let field = Arc::new(Field::new("", DataType::Int32, false));
7686        let list_array = cast(&array, &DataType::List(field.clone())).unwrap();
7687        let actual = list_array.as_list_opt::<i32>().unwrap();
7688        let expect = ListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
7689        assert_eq!(&expect.value(0), &actual.value(0));
7690
7691        // DataType::LargeList
7692        let list_array = cast(&array, &DataType::LargeList(field.clone())).unwrap();
7693        let actual = list_array.as_list_opt::<i64>().unwrap();
7694        let expect = LargeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
7695        assert_eq!(&expect.value(0), &actual.value(0));
7696
7697        // DataType::FixedSizeList
7698        let list_array = cast(&array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
7699        let actual = list_array.as_fixed_size_list_opt().unwrap();
7700        let expect =
7701            FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])], 1);
7702        assert_eq!(&expect.value(0), &actual.value(0));
7703    }
7704
7705    #[test]
7706    fn test_cast_single_element_fixed_size_list() {
7707        // FixedSizeList<T>[1] => T
7708        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
7709            [(Some([Some(5)]))],
7710            1,
7711        )) as ArrayRef;
7712        let casted_array = cast(&from_array, &DataType::Int32).unwrap();
7713        let actual: &Int32Array = casted_array.as_primitive();
7714        let expected = Int32Array::from(vec![Some(5)]);
7715        assert_eq!(&expected, actual);
7716
7717        // FixedSizeList<T>[1] => FixedSizeList<U>[1]
7718        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
7719            [(Some([Some(5)]))],
7720            1,
7721        )) as ArrayRef;
7722        let to_field = Arc::new(Field::new("dummy", DataType::Float32, false));
7723        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
7724        let expected = Arc::new(FixedSizeListArray::new(
7725            to_field.clone(),
7726            1,
7727            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
7728            None,
7729        )) as ArrayRef;
7730        assert_eq!(*expected, *actual);
7731
7732        // FixedSizeList<T>[1] => FixedSizeList<FixdSizedList<U>[1]>[1]
7733        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
7734            [(Some([Some(5)]))],
7735            1,
7736        )) as ArrayRef;
7737        let to_field_inner = Arc::new(Field::new_list_field(DataType::Float32, false));
7738        let to_field = Arc::new(Field::new(
7739            "dummy",
7740            DataType::FixedSizeList(to_field_inner.clone(), 1),
7741            false,
7742        ));
7743        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
7744        let expected = Arc::new(FixedSizeListArray::new(
7745            to_field.clone(),
7746            1,
7747            Arc::new(FixedSizeListArray::new(
7748                to_field_inner.clone(),
7749                1,
7750                Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
7751                None,
7752            )) as ArrayRef,
7753            None,
7754        )) as ArrayRef;
7755        assert_eq!(*expected, *actual);
7756
7757        // T => FixedSizeList<T>[1] (non-nullable)
7758        let field = Arc::new(Field::new("dummy", DataType::Float32, false));
7759        let from_array = Arc::new(Int8Array::from(vec![Some(5)])) as ArrayRef;
7760        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
7761        let actual = casted_array.as_fixed_size_list();
7762        let expected = Arc::new(FixedSizeListArray::new(
7763            field.clone(),
7764            1,
7765            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
7766            None,
7767        )) as ArrayRef;
7768        assert_eq!(expected.as_ref(), actual);
7769
7770        // T => FixedSizeList<T>[1] (nullable)
7771        let field = Arc::new(Field::new("nullable", DataType::Float32, true));
7772        let from_array = Arc::new(Int8Array::from(vec![None])) as ArrayRef;
7773        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
7774        let actual = casted_array.as_fixed_size_list();
7775        let expected = Arc::new(FixedSizeListArray::new(
7776            field.clone(),
7777            1,
7778            Arc::new(Float32Array::from(vec![None])) as ArrayRef,
7779            None,
7780        )) as ArrayRef;
7781        assert_eq!(expected.as_ref(), actual);
7782    }
7783
7784    #[test]
7785    fn test_cast_list_containers() {
7786        // large-list to list
7787        let array = Arc::new(make_large_list_array()) as ArrayRef;
7788        let list_array = cast(
7789            &array,
7790            &DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
7791        )
7792        .unwrap();
7793        let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
7794        let expected = array.as_any().downcast_ref::<LargeListArray>().unwrap();
7795
7796        assert_eq!(&expected.value(0), &actual.value(0));
7797        assert_eq!(&expected.value(1), &actual.value(1));
7798        assert_eq!(&expected.value(2), &actual.value(2));
7799
7800        // list to large-list
7801        let array = Arc::new(make_list_array()) as ArrayRef;
7802        let large_list_array = cast(
7803            &array,
7804            &DataType::LargeList(Arc::new(Field::new("", DataType::Int32, false))),
7805        )
7806        .unwrap();
7807        let actual = large_list_array
7808            .as_any()
7809            .downcast_ref::<LargeListArray>()
7810            .unwrap();
7811        let expected = array.as_any().downcast_ref::<ListArray>().unwrap();
7812
7813        assert_eq!(&expected.value(0), &actual.value(0));
7814        assert_eq!(&expected.value(1), &actual.value(1));
7815        assert_eq!(&expected.value(2), &actual.value(2));
7816    }
7817
7818    #[test]
7819    fn test_cast_list_to_fsl() {
7820        // There four noteworthy cases we should handle:
7821        // 1. No nulls
7822        // 2. Nulls that are always empty
7823        // 3. Nulls that have varying lengths
7824        // 4. Nulls that are correctly sized (same as target list size)
7825
7826        // Non-null case
7827        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
7828        let values = vec![
7829            Some(vec![Some(1), Some(2), Some(3)]),
7830            Some(vec![Some(4), Some(5), Some(6)]),
7831        ];
7832        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
7833            values.clone(),
7834        )) as ArrayRef;
7835        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7836            values, 3,
7837        )) as ArrayRef;
7838        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
7839        assert_eq!(expected.as_ref(), actual.as_ref());
7840
7841        // Null cases
7842        // Array is [[1, 2, 3], null, [4, 5, 6], null]
7843        let cases = [
7844            (
7845                // Zero-length nulls
7846                vec![1, 2, 3, 4, 5, 6],
7847                vec![3, 0, 3, 0],
7848            ),
7849            (
7850                // Varying-length nulls
7851                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
7852                vec![3, 2, 3, 1],
7853            ),
7854            (
7855                // Correctly-sized nulls
7856                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
7857                vec![3, 3, 3, 3],
7858            ),
7859            (
7860                // Mixed nulls
7861                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
7862                vec![3, 0, 3, 3],
7863            ),
7864        ];
7865        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
7866
7867        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7868            vec![
7869                Some(vec![Some(1), Some(2), Some(3)]),
7870                None,
7871                Some(vec![Some(4), Some(5), Some(6)]),
7872                None,
7873            ],
7874            3,
7875        )) as ArrayRef;
7876
7877        for (values, lengths) in cases.iter() {
7878            let array = Arc::new(ListArray::new(
7879                field.clone(),
7880                OffsetBuffer::from_lengths(lengths.clone()),
7881                Arc::new(Int32Array::from(values.clone())),
7882                Some(null_buffer.clone()),
7883            )) as ArrayRef;
7884            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
7885            assert_eq!(expected.as_ref(), actual.as_ref());
7886        }
7887    }
7888
7889    #[test]
7890    fn test_cast_list_to_fsl_safety() {
7891        let values = vec![
7892            Some(vec![Some(1), Some(2), Some(3)]),
7893            Some(vec![Some(4), Some(5)]),
7894            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
7895            Some(vec![Some(3), Some(4), Some(5)]),
7896        ];
7897        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
7898            values.clone(),
7899        )) as ArrayRef;
7900
7901        let res = cast_with_options(
7902            array.as_ref(),
7903            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
7904            &CastOptions {
7905                safe: false,
7906                ..Default::default()
7907            },
7908        );
7909        assert!(res.is_err());
7910        assert!(format!("{:?}", res)
7911            .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2"));
7912
7913        // When safe=true (default), the cast will fill nulls for lists that are
7914        // too short and truncate lists that are too long.
7915        let res = cast(
7916            array.as_ref(),
7917            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
7918        )
7919        .unwrap();
7920        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7921            vec![
7922                Some(vec![Some(1), Some(2), Some(3)]),
7923                None, // Too short -> replaced with null
7924                None, // Too long -> replaced with null
7925                Some(vec![Some(3), Some(4), Some(5)]),
7926            ],
7927            3,
7928        )) as ArrayRef;
7929        assert_eq!(expected.as_ref(), res.as_ref());
7930
7931        // The safe option is false and the source array contains a null list.
7932        // issue: https://github.com/apache/arrow-rs/issues/5642
7933        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
7934            Some(vec![Some(1), Some(2), Some(3)]),
7935            None,
7936        ])) as ArrayRef;
7937        let res = cast_with_options(
7938            array.as_ref(),
7939            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
7940            &CastOptions {
7941                safe: false,
7942                ..Default::default()
7943            },
7944        )
7945        .unwrap();
7946        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7947            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
7948            3,
7949        )) as ArrayRef;
7950        assert_eq!(expected.as_ref(), res.as_ref());
7951    }
7952
7953    #[test]
7954    fn test_cast_large_list_to_fsl() {
7955        let values = vec![Some(vec![Some(1), Some(2)]), Some(vec![Some(3), Some(4)])];
7956        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
7957            values.clone(),
7958        )) as ArrayRef;
7959        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7960            values, 2,
7961        )) as ArrayRef;
7962        let actual = cast(
7963            array.as_ref(),
7964            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 2),
7965        )
7966        .unwrap();
7967        assert_eq!(expected.as_ref(), actual.as_ref());
7968    }
7969
7970    #[test]
7971    fn test_cast_list_to_fsl_subcast() {
7972        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
7973            vec![
7974                Some(vec![Some(1), Some(2)]),
7975                Some(vec![Some(3), Some(i32::MAX)]),
7976            ],
7977        )) as ArrayRef;
7978        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
7979            vec![
7980                Some(vec![Some(1), Some(2)]),
7981                Some(vec![Some(3), Some(i32::MAX as i64)]),
7982            ],
7983            2,
7984        )) as ArrayRef;
7985        let actual = cast(
7986            array.as_ref(),
7987            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 2),
7988        )
7989        .unwrap();
7990        assert_eq!(expected.as_ref(), actual.as_ref());
7991
7992        let res = cast_with_options(
7993            array.as_ref(),
7994            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int16, true)), 2),
7995            &CastOptions {
7996                safe: false,
7997                ..Default::default()
7998            },
7999        );
8000        assert!(res.is_err());
8001        assert!(format!("{:?}", res).contains("Can't cast value 2147483647 to type Int16"));
8002    }
8003
8004    #[test]
8005    fn test_cast_list_to_fsl_empty() {
8006        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
8007        let array = new_empty_array(&DataType::List(field.clone()));
8008
8009        let target_type = DataType::FixedSizeList(field.clone(), 3);
8010        let expected = new_empty_array(&target_type);
8011
8012        let actual = cast(array.as_ref(), &target_type).unwrap();
8013        assert_eq!(expected.as_ref(), actual.as_ref());
8014    }
8015
8016    fn make_list_array() -> ListArray {
8017        // Construct a value array
8018        let value_data = ArrayData::builder(DataType::Int32)
8019            .len(8)
8020            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8021            .build()
8022            .unwrap();
8023
8024        // Construct a buffer for value offsets, for the nested array:
8025        //  [[0, 1, 2], [3, 4, 5], [6, 7]]
8026        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
8027
8028        // Construct a list array from the above two
8029        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
8030        let list_data = ArrayData::builder(list_data_type)
8031            .len(3)
8032            .add_buffer(value_offsets)
8033            .add_child_data(value_data)
8034            .build()
8035            .unwrap();
8036        ListArray::from(list_data)
8037    }
8038
8039    fn make_large_list_array() -> LargeListArray {
8040        // Construct a value array
8041        let value_data = ArrayData::builder(DataType::Int32)
8042            .len(8)
8043            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8044            .build()
8045            .unwrap();
8046
8047        // Construct a buffer for value offsets, for the nested array:
8048        //  [[0, 1, 2], [3, 4, 5], [6, 7]]
8049        let value_offsets = Buffer::from_slice_ref([0i64, 3, 6, 8]);
8050
8051        // Construct a list array from the above two
8052        let list_data_type =
8053            DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8054        let list_data = ArrayData::builder(list_data_type)
8055            .len(3)
8056            .add_buffer(value_offsets)
8057            .add_child_data(value_data)
8058            .build()
8059            .unwrap();
8060        LargeListArray::from(list_data)
8061    }
8062
8063    fn make_fixed_size_list_array() -> FixedSizeListArray {
8064        // Construct a value array
8065        let value_data = ArrayData::builder(DataType::Int32)
8066            .len(8)
8067            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8068            .build()
8069            .unwrap();
8070
8071        let list_data_type =
8072            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
8073        let list_data = ArrayData::builder(list_data_type)
8074            .len(2)
8075            .add_child_data(value_data)
8076            .build()
8077            .unwrap();
8078        FixedSizeListArray::from(list_data)
8079    }
8080
8081    fn make_fixed_size_list_array_for_large_list() -> FixedSizeListArray {
8082        // Construct a value array
8083        let value_data = ArrayData::builder(DataType::Int64)
8084            .len(8)
8085            .add_buffer(Buffer::from_slice_ref([0i64, 1, 2, 3, 4, 5, 6, 7]))
8086            .build()
8087            .unwrap();
8088
8089        let list_data_type =
8090            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 4);
8091        let list_data = ArrayData::builder(list_data_type)
8092            .len(2)
8093            .add_child_data(value_data)
8094            .build()
8095            .unwrap();
8096        FixedSizeListArray::from(list_data)
8097    }
8098
8099    #[test]
8100    fn test_cast_map_dont_allow_change_of_order() {
8101        let string_builder = StringBuilder::new();
8102        let value_builder = StringBuilder::new();
8103        let mut builder = MapBuilder::new(
8104            Some(MapFieldNames {
8105                entry: "entries".to_string(),
8106                key: "key".to_string(),
8107                value: "value".to_string(),
8108            }),
8109            string_builder,
8110            value_builder,
8111        );
8112
8113        builder.keys().append_value("0");
8114        builder.values().append_value("test_val_1");
8115        builder.append(true).unwrap();
8116        builder.keys().append_value("1");
8117        builder.values().append_value("test_val_2");
8118        builder.append(true).unwrap();
8119
8120        // map builder returns unsorted map by default
8121        let array = builder.finish();
8122
8123        let new_ordered = true;
8124        let new_type = DataType::Map(
8125            Arc::new(Field::new(
8126                "entries",
8127                DataType::Struct(
8128                    vec![
8129                        Field::new("key", DataType::Utf8, false),
8130                        Field::new("value", DataType::Utf8, false),
8131                    ]
8132                    .into(),
8133                ),
8134                false,
8135            )),
8136            new_ordered,
8137        );
8138
8139        let new_array_result = cast(&array, &new_type.clone());
8140        assert!(!can_cast_types(array.data_type(), &new_type));
8141        assert!(
8142            matches!(new_array_result, Err(ArrowError::CastError(t)) if t == r#"Casting from Map(Field { name: "entries", data_type: Struct([Field { name: "key", data_type: Utf8, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, Field { name: "value", data_type: Utf8, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }]), nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, false) to Map(Field { name: "entries", data_type: Struct([Field { name: "key", data_type: Utf8, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, Field { name: "value", data_type: Utf8, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }]), nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, true) not supported"#)
8143        );
8144    }
8145
8146    #[test]
8147    fn test_cast_map_dont_allow_when_container_cant_cast() {
8148        let string_builder = StringBuilder::new();
8149        let value_builder = IntervalDayTimeArray::builder(2);
8150        let mut builder = MapBuilder::new(
8151            Some(MapFieldNames {
8152                entry: "entries".to_string(),
8153                key: "key".to_string(),
8154                value: "value".to_string(),
8155            }),
8156            string_builder,
8157            value_builder,
8158        );
8159
8160        builder.keys().append_value("0");
8161        builder.values().append_value(IntervalDayTime::new(1, 1));
8162        builder.append(true).unwrap();
8163        builder.keys().append_value("1");
8164        builder.values().append_value(IntervalDayTime::new(2, 2));
8165        builder.append(true).unwrap();
8166
8167        // map builder returns unsorted map by default
8168        let array = builder.finish();
8169
8170        let new_ordered = true;
8171        let new_type = DataType::Map(
8172            Arc::new(Field::new(
8173                "entries",
8174                DataType::Struct(
8175                    vec![
8176                        Field::new("key", DataType::Utf8, false),
8177                        Field::new("value", DataType::Duration(TimeUnit::Second), false),
8178                    ]
8179                    .into(),
8180                ),
8181                false,
8182            )),
8183            new_ordered,
8184        );
8185
8186        let new_array_result = cast(&array, &new_type.clone());
8187        assert!(!can_cast_types(array.data_type(), &new_type));
8188        assert!(
8189            matches!(new_array_result, Err(ArrowError::CastError(t)) if t == r#"Casting from Map(Field { name: "entries", data_type: Struct([Field { name: "key", data_type: Utf8, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, Field { name: "value", data_type: Interval(DayTime), nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }]), nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, false) to Map(Field { name: "entries", data_type: Struct([Field { name: "key", data_type: Utf8, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, Field { name: "value", data_type: Duration(Second), nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }]), nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, true) not supported"#)
8190        );
8191    }
8192
8193    #[test]
8194    fn test_cast_map_field_names() {
8195        let string_builder = StringBuilder::new();
8196        let value_builder = StringBuilder::new();
8197        let mut builder = MapBuilder::new(
8198            Some(MapFieldNames {
8199                entry: "entries".to_string(),
8200                key: "key".to_string(),
8201                value: "value".to_string(),
8202            }),
8203            string_builder,
8204            value_builder,
8205        );
8206
8207        builder.keys().append_value("0");
8208        builder.values().append_value("test_val_1");
8209        builder.append(true).unwrap();
8210        builder.keys().append_value("1");
8211        builder.values().append_value("test_val_2");
8212        builder.append(true).unwrap();
8213        builder.append(false).unwrap();
8214
8215        let array = builder.finish();
8216
8217        let new_type = DataType::Map(
8218            Arc::new(Field::new(
8219                "entries_new",
8220                DataType::Struct(
8221                    vec![
8222                        Field::new("key_new", DataType::Utf8, false),
8223                        Field::new("value_values", DataType::Utf8, false),
8224                    ]
8225                    .into(),
8226                ),
8227                false,
8228            )),
8229            false,
8230        );
8231
8232        assert_ne!(new_type, array.data_type().clone());
8233
8234        let new_array = cast(&array, &new_type.clone()).unwrap();
8235        assert_eq!(new_type, new_array.data_type().clone());
8236        let map_array = new_array.as_map();
8237
8238        assert_ne!(new_type, array.data_type().clone());
8239        assert_eq!(new_type, map_array.data_type().clone());
8240
8241        let key_string = map_array
8242            .keys()
8243            .as_any()
8244            .downcast_ref::<StringArray>()
8245            .unwrap()
8246            .into_iter()
8247            .flatten()
8248            .collect::<Vec<_>>();
8249        assert_eq!(&key_string, &vec!["0", "1"]);
8250
8251        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
8252        let values_string = values_string_array
8253            .as_any()
8254            .downcast_ref::<StringArray>()
8255            .unwrap()
8256            .into_iter()
8257            .flatten()
8258            .collect::<Vec<_>>();
8259        assert_eq!(&values_string, &vec!["test_val_1", "test_val_2"]);
8260
8261        assert_eq!(
8262            map_array.nulls(),
8263            Some(&NullBuffer::from(vec![true, true, false]))
8264        );
8265    }
8266
8267    #[test]
8268    fn test_cast_map_contained_values() {
8269        let string_builder = StringBuilder::new();
8270        let value_builder = Int8Builder::new();
8271        let mut builder = MapBuilder::new(
8272            Some(MapFieldNames {
8273                entry: "entries".to_string(),
8274                key: "key".to_string(),
8275                value: "value".to_string(),
8276            }),
8277            string_builder,
8278            value_builder,
8279        );
8280
8281        builder.keys().append_value("0");
8282        builder.values().append_value(44);
8283        builder.append(true).unwrap();
8284        builder.keys().append_value("1");
8285        builder.values().append_value(22);
8286        builder.append(true).unwrap();
8287
8288        let array = builder.finish();
8289
8290        let new_type = DataType::Map(
8291            Arc::new(Field::new(
8292                "entries",
8293                DataType::Struct(
8294                    vec![
8295                        Field::new("key", DataType::Utf8, false),
8296                        Field::new("value", DataType::Utf8, false),
8297                    ]
8298                    .into(),
8299                ),
8300                false,
8301            )),
8302            false,
8303        );
8304
8305        let new_array = cast(&array, &new_type.clone()).unwrap();
8306        assert_eq!(new_type, new_array.data_type().clone());
8307        let map_array = new_array.as_map();
8308
8309        assert_ne!(new_type, array.data_type().clone());
8310        assert_eq!(new_type, map_array.data_type().clone());
8311
8312        let key_string = map_array
8313            .keys()
8314            .as_any()
8315            .downcast_ref::<StringArray>()
8316            .unwrap()
8317            .into_iter()
8318            .flatten()
8319            .collect::<Vec<_>>();
8320        assert_eq!(&key_string, &vec!["0", "1"]);
8321
8322        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
8323        let values_string = values_string_array
8324            .as_any()
8325            .downcast_ref::<StringArray>()
8326            .unwrap()
8327            .into_iter()
8328            .flatten()
8329            .collect::<Vec<_>>();
8330        assert_eq!(&values_string, &vec!["44", "22"]);
8331    }
8332
8333    #[test]
8334    fn test_utf8_cast_offsets() {
8335        // test if offset of the array is taken into account during cast
8336        let str_array = StringArray::from(vec!["a", "b", "c"]);
8337        let str_array = str_array.slice(1, 2);
8338
8339        let out = cast(&str_array, &DataType::LargeUtf8).unwrap();
8340
8341        let large_str_array = out.as_any().downcast_ref::<LargeStringArray>().unwrap();
8342        let strs = large_str_array.into_iter().flatten().collect::<Vec<_>>();
8343        assert_eq!(strs, &["b", "c"])
8344    }
8345
8346    #[test]
8347    fn test_list_cast_offsets() {
8348        // test if offset of the array is taken into account during cast
8349        let array1 = make_list_array().slice(1, 2);
8350        let array2 = Arc::new(make_list_array()) as ArrayRef;
8351
8352        let dt = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8353        let out1 = cast(&array1, &dt).unwrap();
8354        let out2 = cast(&array2, &dt).unwrap();
8355
8356        assert_eq!(&out1, &out2.slice(1, 2))
8357    }
8358
8359    #[test]
8360    fn test_list_to_string() {
8361        let str_array = StringArray::from(vec!["a", "b", "c", "d", "e", "f", "g", "h"]);
8362        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
8363        let value_data = str_array.into_data();
8364
8365        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Utf8, true)));
8366        let list_data = ArrayData::builder(list_data_type)
8367            .len(3)
8368            .add_buffer(value_offsets)
8369            .add_child_data(value_data)
8370            .build()
8371            .unwrap();
8372        let array = Arc::new(ListArray::from(list_data)) as ArrayRef;
8373
8374        let out = cast(&array, &DataType::Utf8).unwrap();
8375        let out = out
8376            .as_any()
8377            .downcast_ref::<StringArray>()
8378            .unwrap()
8379            .into_iter()
8380            .flatten()
8381            .collect::<Vec<_>>();
8382        assert_eq!(&out, &vec!["[a, b, c]", "[d, e, f]", "[g, h]"]);
8383
8384        let out = cast(&array, &DataType::LargeUtf8).unwrap();
8385        let out = out
8386            .as_any()
8387            .downcast_ref::<LargeStringArray>()
8388            .unwrap()
8389            .into_iter()
8390            .flatten()
8391            .collect::<Vec<_>>();
8392        assert_eq!(&out, &vec!["[a, b, c]", "[d, e, f]", "[g, h]"]);
8393
8394        let array = Arc::new(make_list_array()) as ArrayRef;
8395        let out = cast(&array, &DataType::Utf8).unwrap();
8396        let out = out
8397            .as_any()
8398            .downcast_ref::<StringArray>()
8399            .unwrap()
8400            .into_iter()
8401            .flatten()
8402            .collect::<Vec<_>>();
8403        assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
8404
8405        let array = Arc::new(make_large_list_array()) as ArrayRef;
8406        let out = cast(&array, &DataType::LargeUtf8).unwrap();
8407        let out = out
8408            .as_any()
8409            .downcast_ref::<LargeStringArray>()
8410            .unwrap()
8411            .into_iter()
8412            .flatten()
8413            .collect::<Vec<_>>();
8414        assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
8415    }
8416
8417    #[test]
8418    fn test_cast_f64_to_decimal128() {
8419        // to reproduce https://github.com/apache/arrow-rs/issues/2997
8420
8421        let decimal_type = DataType::Decimal128(18, 2);
8422        let array = Float64Array::from(vec![
8423            Some(0.0699999999),
8424            Some(0.0659999999),
8425            Some(0.0650000000),
8426            Some(0.0649999999),
8427        ]);
8428        let array = Arc::new(array) as ArrayRef;
8429        generate_cast_test_case!(
8430            &array,
8431            Decimal128Array,
8432            &decimal_type,
8433            vec![
8434                Some(7_i128), // round up
8435                Some(7_i128), // round up
8436                Some(7_i128), // round up
8437                Some(6_i128), // round down
8438            ]
8439        );
8440
8441        let decimal_type = DataType::Decimal128(18, 3);
8442        let array = Float64Array::from(vec![
8443            Some(0.0699999999),
8444            Some(0.0659999999),
8445            Some(0.0650000000),
8446            Some(0.0649999999),
8447        ]);
8448        let array = Arc::new(array) as ArrayRef;
8449        generate_cast_test_case!(
8450            &array,
8451            Decimal128Array,
8452            &decimal_type,
8453            vec![
8454                Some(70_i128), // round up
8455                Some(66_i128), // round up
8456                Some(65_i128), // round down
8457                Some(65_i128), // round up
8458            ]
8459        );
8460    }
8461
8462    #[test]
8463    fn test_cast_numeric_to_decimal128_overflow() {
8464        let array = Int64Array::from(vec![i64::MAX]);
8465        let array = Arc::new(array) as ArrayRef;
8466        let casted_array = cast_with_options(
8467            &array,
8468            &DataType::Decimal128(38, 30),
8469            &CastOptions {
8470                safe: true,
8471                format_options: FormatOptions::default(),
8472            },
8473        );
8474        assert!(casted_array.is_ok());
8475        assert!(casted_array.unwrap().is_null(0));
8476
8477        let casted_array = cast_with_options(
8478            &array,
8479            &DataType::Decimal128(38, 30),
8480            &CastOptions {
8481                safe: false,
8482                format_options: FormatOptions::default(),
8483            },
8484        );
8485        assert!(casted_array.is_err());
8486    }
8487
8488    #[test]
8489    fn test_cast_numeric_to_decimal256_overflow() {
8490        let array = Int64Array::from(vec![i64::MAX]);
8491        let array = Arc::new(array) as ArrayRef;
8492        let casted_array = cast_with_options(
8493            &array,
8494            &DataType::Decimal256(76, 76),
8495            &CastOptions {
8496                safe: true,
8497                format_options: FormatOptions::default(),
8498            },
8499        );
8500        assert!(casted_array.is_ok());
8501        assert!(casted_array.unwrap().is_null(0));
8502
8503        let casted_array = cast_with_options(
8504            &array,
8505            &DataType::Decimal256(76, 76),
8506            &CastOptions {
8507                safe: false,
8508                format_options: FormatOptions::default(),
8509            },
8510        );
8511        assert!(casted_array.is_err());
8512    }
8513
8514    #[test]
8515    fn test_cast_floating_point_to_decimal128_precision_overflow() {
8516        let array = Float64Array::from(vec![1.1]);
8517        let array = Arc::new(array) as ArrayRef;
8518        let casted_array = cast_with_options(
8519            &array,
8520            &DataType::Decimal128(2, 2),
8521            &CastOptions {
8522                safe: true,
8523                format_options: FormatOptions::default(),
8524            },
8525        );
8526        assert!(casted_array.is_ok());
8527        assert!(casted_array.unwrap().is_null(0));
8528
8529        let casted_array = cast_with_options(
8530            &array,
8531            &DataType::Decimal128(2, 2),
8532            &CastOptions {
8533                safe: false,
8534                format_options: FormatOptions::default(),
8535            },
8536        );
8537        let err = casted_array.unwrap_err().to_string();
8538        let expected_error = "Invalid argument error: 110 is too large to store in a Decimal128 of precision 2. Max is 99";
8539        assert!(
8540            err.contains(expected_error),
8541            "did not find expected error '{expected_error}' in actual error '{err}'"
8542        );
8543    }
8544
8545    #[test]
8546    fn test_cast_floating_point_to_decimal256_precision_overflow() {
8547        let array = Float64Array::from(vec![1.1]);
8548        let array = Arc::new(array) as ArrayRef;
8549        let casted_array = cast_with_options(
8550            &array,
8551            &DataType::Decimal256(2, 2),
8552            &CastOptions {
8553                safe: true,
8554                format_options: FormatOptions::default(),
8555            },
8556        );
8557        assert!(casted_array.is_ok());
8558        assert!(casted_array.unwrap().is_null(0));
8559
8560        let casted_array = cast_with_options(
8561            &array,
8562            &DataType::Decimal256(2, 2),
8563            &CastOptions {
8564                safe: false,
8565                format_options: FormatOptions::default(),
8566            },
8567        );
8568        let err = casted_array.unwrap_err().to_string();
8569        let expected_error = "Invalid argument error: 110 is too large to store in a Decimal256 of precision 2. Max is 99";
8570        assert!(
8571            err.contains(expected_error),
8572            "did not find expected error '{expected_error}' in actual error '{err}'"
8573        );
8574    }
8575
8576    #[test]
8577    fn test_cast_floating_point_to_decimal128_overflow() {
8578        let array = Float64Array::from(vec![f64::MAX]);
8579        let array = Arc::new(array) as ArrayRef;
8580        let casted_array = cast_with_options(
8581            &array,
8582            &DataType::Decimal128(38, 30),
8583            &CastOptions {
8584                safe: true,
8585                format_options: FormatOptions::default(),
8586            },
8587        );
8588        assert!(casted_array.is_ok());
8589        assert!(casted_array.unwrap().is_null(0));
8590
8591        let casted_array = cast_with_options(
8592            &array,
8593            &DataType::Decimal128(38, 30),
8594            &CastOptions {
8595                safe: false,
8596                format_options: FormatOptions::default(),
8597            },
8598        );
8599        let err = casted_array.unwrap_err().to_string();
8600        let expected_error = "Cast error: Cannot cast to Decimal128(38, 30)";
8601        assert!(
8602            err.contains(expected_error),
8603            "did not find expected error '{expected_error}' in actual error '{err}'"
8604        );
8605    }
8606
8607    #[test]
8608    fn test_cast_floating_point_to_decimal256_overflow() {
8609        let array = Float64Array::from(vec![f64::MAX]);
8610        let array = Arc::new(array) as ArrayRef;
8611        let casted_array = cast_with_options(
8612            &array,
8613            &DataType::Decimal256(76, 50),
8614            &CastOptions {
8615                safe: true,
8616                format_options: FormatOptions::default(),
8617            },
8618        );
8619        assert!(casted_array.is_ok());
8620        assert!(casted_array.unwrap().is_null(0));
8621
8622        let casted_array = cast_with_options(
8623            &array,
8624            &DataType::Decimal256(76, 50),
8625            &CastOptions {
8626                safe: false,
8627                format_options: FormatOptions::default(),
8628            },
8629        );
8630        let err = casted_array.unwrap_err().to_string();
8631        let expected_error = "Cast error: Cannot cast to Decimal256(76, 50)";
8632        assert!(
8633            err.contains(expected_error),
8634            "did not find expected error '{expected_error}' in actual error '{err}'"
8635        );
8636    }
8637
8638    #[test]
8639    fn test_cast_decimal128_to_decimal128_negative_scale() {
8640        let input_type = DataType::Decimal128(20, 0);
8641        let output_type = DataType::Decimal128(20, -1);
8642        assert!(can_cast_types(&input_type, &output_type));
8643        let array = vec![Some(1123450), Some(2123455), Some(3123456), None];
8644        let input_decimal_array = create_decimal128_array(array, 20, 0).unwrap();
8645        let array = Arc::new(input_decimal_array) as ArrayRef;
8646        generate_cast_test_case!(
8647            &array,
8648            Decimal128Array,
8649            &output_type,
8650            vec![
8651                Some(112345_i128),
8652                Some(212346_i128),
8653                Some(312346_i128),
8654                None
8655            ]
8656        );
8657
8658        let casted_array = cast(&array, &output_type).unwrap();
8659        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8660
8661        assert_eq!("1123450", decimal_arr.value_as_string(0));
8662        assert_eq!("2123460", decimal_arr.value_as_string(1));
8663        assert_eq!("3123460", decimal_arr.value_as_string(2));
8664    }
8665
8666    #[test]
8667    fn test_cast_numeric_to_decimal128_negative() {
8668        let decimal_type = DataType::Decimal128(38, -1);
8669        let array = Arc::new(Int32Array::from(vec![
8670            Some(1123456),
8671            Some(2123456),
8672            Some(3123456),
8673        ])) as ArrayRef;
8674
8675        let casted_array = cast(&array, &decimal_type).unwrap();
8676        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8677
8678        assert_eq!("1123450", decimal_arr.value_as_string(0));
8679        assert_eq!("2123450", decimal_arr.value_as_string(1));
8680        assert_eq!("3123450", decimal_arr.value_as_string(2));
8681
8682        let array = Arc::new(Float32Array::from(vec![
8683            Some(1123.456),
8684            Some(2123.456),
8685            Some(3123.456),
8686        ])) as ArrayRef;
8687
8688        let casted_array = cast(&array, &decimal_type).unwrap();
8689        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8690
8691        assert_eq!("1120", decimal_arr.value_as_string(0));
8692        assert_eq!("2120", decimal_arr.value_as_string(1));
8693        assert_eq!("3120", decimal_arr.value_as_string(2));
8694    }
8695
8696    #[test]
8697    fn test_cast_decimal128_to_decimal128_negative() {
8698        let input_type = DataType::Decimal128(10, -1);
8699        let output_type = DataType::Decimal128(10, -2);
8700        assert!(can_cast_types(&input_type, &output_type));
8701        let array = vec![Some(123)];
8702        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
8703        let array = Arc::new(input_decimal_array) as ArrayRef;
8704        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(12_i128),]);
8705
8706        let casted_array = cast(&array, &output_type).unwrap();
8707        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8708
8709        assert_eq!("1200", decimal_arr.value_as_string(0));
8710
8711        let array = vec![Some(125)];
8712        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
8713        let array = Arc::new(input_decimal_array) as ArrayRef;
8714        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(13_i128),]);
8715
8716        let casted_array = cast(&array, &output_type).unwrap();
8717        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8718
8719        assert_eq!("1300", decimal_arr.value_as_string(0));
8720    }
8721
8722    #[test]
8723    fn test_cast_decimal128_to_decimal256_negative() {
8724        let input_type = DataType::Decimal128(10, 3);
8725        let output_type = DataType::Decimal256(10, 5);
8726        assert!(can_cast_types(&input_type, &output_type));
8727        let array = vec![Some(123456), Some(-123456)];
8728        let input_decimal_array = create_decimal128_array(array, 10, 3).unwrap();
8729        let array = Arc::new(input_decimal_array) as ArrayRef;
8730
8731        let hundred = i256::from_i128(100);
8732        generate_cast_test_case!(
8733            &array,
8734            Decimal256Array,
8735            &output_type,
8736            vec![
8737                Some(i256::from_i128(123456).mul_wrapping(hundred)),
8738                Some(i256::from_i128(-123456).mul_wrapping(hundred))
8739            ]
8740        );
8741    }
8742
8743    #[test]
8744    fn test_parse_string_to_decimal() {
8745        assert_eq!(
8746            Decimal128Type::format_decimal(
8747                parse_string_to_decimal_native::<Decimal128Type>("123.45", 2).unwrap(),
8748                38,
8749                2,
8750            ),
8751            "123.45"
8752        );
8753        assert_eq!(
8754            Decimal128Type::format_decimal(
8755                parse_string_to_decimal_native::<Decimal128Type>("12345", 2).unwrap(),
8756                38,
8757                2,
8758            ),
8759            "12345.00"
8760        );
8761        assert_eq!(
8762            Decimal128Type::format_decimal(
8763                parse_string_to_decimal_native::<Decimal128Type>("0.12345", 2).unwrap(),
8764                38,
8765                2,
8766            ),
8767            "0.12"
8768        );
8769        assert_eq!(
8770            Decimal128Type::format_decimal(
8771                parse_string_to_decimal_native::<Decimal128Type>(".12345", 2).unwrap(),
8772                38,
8773                2,
8774            ),
8775            "0.12"
8776        );
8777        assert_eq!(
8778            Decimal128Type::format_decimal(
8779                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
8780                38,
8781                2,
8782            ),
8783            "0.13"
8784        );
8785        assert_eq!(
8786            Decimal128Type::format_decimal(
8787                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
8788                38,
8789                2,
8790            ),
8791            "0.13"
8792        );
8793
8794        assert_eq!(
8795            Decimal256Type::format_decimal(
8796                parse_string_to_decimal_native::<Decimal256Type>("123.45", 3).unwrap(),
8797                38,
8798                3,
8799            ),
8800            "123.450"
8801        );
8802        assert_eq!(
8803            Decimal256Type::format_decimal(
8804                parse_string_to_decimal_native::<Decimal256Type>("12345", 3).unwrap(),
8805                38,
8806                3,
8807            ),
8808            "12345.000"
8809        );
8810        assert_eq!(
8811            Decimal256Type::format_decimal(
8812                parse_string_to_decimal_native::<Decimal256Type>("0.12345", 3).unwrap(),
8813                38,
8814                3,
8815            ),
8816            "0.123"
8817        );
8818        assert_eq!(
8819            Decimal256Type::format_decimal(
8820                parse_string_to_decimal_native::<Decimal256Type>(".12345", 3).unwrap(),
8821                38,
8822                3,
8823            ),
8824            "0.123"
8825        );
8826        assert_eq!(
8827            Decimal256Type::format_decimal(
8828                parse_string_to_decimal_native::<Decimal256Type>(".1265", 3).unwrap(),
8829                38,
8830                3,
8831            ),
8832            "0.127"
8833        );
8834    }
8835
8836    fn test_cast_string_to_decimal(array: ArrayRef) {
8837        // Decimal128
8838        let output_type = DataType::Decimal128(38, 2);
8839        assert!(can_cast_types(array.data_type(), &output_type));
8840
8841        let casted_array = cast(&array, &output_type).unwrap();
8842        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8843
8844        assert_eq!("123.45", decimal_arr.value_as_string(0));
8845        assert_eq!("1.23", decimal_arr.value_as_string(1));
8846        assert_eq!("0.12", decimal_arr.value_as_string(2));
8847        assert_eq!("0.13", decimal_arr.value_as_string(3));
8848        assert_eq!("1.26", decimal_arr.value_as_string(4));
8849        assert_eq!("12345.00", decimal_arr.value_as_string(5));
8850        assert_eq!("12345.00", decimal_arr.value_as_string(6));
8851        assert_eq!("0.12", decimal_arr.value_as_string(7));
8852        assert_eq!("12.23", decimal_arr.value_as_string(8));
8853        assert!(decimal_arr.is_null(9));
8854        assert_eq!("0.00", decimal_arr.value_as_string(10));
8855        assert_eq!("0.00", decimal_arr.value_as_string(11));
8856        assert!(decimal_arr.is_null(12));
8857        assert_eq!("-1.23", decimal_arr.value_as_string(13));
8858        assert_eq!("-1.24", decimal_arr.value_as_string(14));
8859        assert_eq!("0.00", decimal_arr.value_as_string(15));
8860        assert_eq!("-123.00", decimal_arr.value_as_string(16));
8861        assert_eq!("-123.23", decimal_arr.value_as_string(17));
8862        assert_eq!("-0.12", decimal_arr.value_as_string(18));
8863        assert_eq!("1.23", decimal_arr.value_as_string(19));
8864        assert_eq!("1.24", decimal_arr.value_as_string(20));
8865        assert_eq!("0.00", decimal_arr.value_as_string(21));
8866        assert_eq!("123.00", decimal_arr.value_as_string(22));
8867        assert_eq!("123.23", decimal_arr.value_as_string(23));
8868        assert_eq!("0.12", decimal_arr.value_as_string(24));
8869        assert!(decimal_arr.is_null(25));
8870        assert!(decimal_arr.is_null(26));
8871        assert!(decimal_arr.is_null(27));
8872        assert_eq!("0.00", decimal_arr.value_as_string(28));
8873        assert_eq!("0.00", decimal_arr.value_as_string(29));
8874        assert_eq!("12345.00", decimal_arr.value_as_string(30));
8875        assert_eq!(decimal_arr.len(), 31);
8876
8877        // Decimal256
8878        let output_type = DataType::Decimal256(76, 3);
8879        assert!(can_cast_types(array.data_type(), &output_type));
8880
8881        let casted_array = cast(&array, &output_type).unwrap();
8882        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
8883
8884        assert_eq!("123.450", decimal_arr.value_as_string(0));
8885        assert_eq!("1.235", decimal_arr.value_as_string(1));
8886        assert_eq!("0.123", decimal_arr.value_as_string(2));
8887        assert_eq!("0.127", decimal_arr.value_as_string(3));
8888        assert_eq!("1.263", decimal_arr.value_as_string(4));
8889        assert_eq!("12345.000", decimal_arr.value_as_string(5));
8890        assert_eq!("12345.000", decimal_arr.value_as_string(6));
8891        assert_eq!("0.123", decimal_arr.value_as_string(7));
8892        assert_eq!("12.234", decimal_arr.value_as_string(8));
8893        assert!(decimal_arr.is_null(9));
8894        assert_eq!("0.000", decimal_arr.value_as_string(10));
8895        assert_eq!("0.000", decimal_arr.value_as_string(11));
8896        assert!(decimal_arr.is_null(12));
8897        assert_eq!("-1.235", decimal_arr.value_as_string(13));
8898        assert_eq!("-1.236", decimal_arr.value_as_string(14));
8899        assert_eq!("0.000", decimal_arr.value_as_string(15));
8900        assert_eq!("-123.000", decimal_arr.value_as_string(16));
8901        assert_eq!("-123.234", decimal_arr.value_as_string(17));
8902        assert_eq!("-0.123", decimal_arr.value_as_string(18));
8903        assert_eq!("1.235", decimal_arr.value_as_string(19));
8904        assert_eq!("1.236", decimal_arr.value_as_string(20));
8905        assert_eq!("0.000", decimal_arr.value_as_string(21));
8906        assert_eq!("123.000", decimal_arr.value_as_string(22));
8907        assert_eq!("123.234", decimal_arr.value_as_string(23));
8908        assert_eq!("0.123", decimal_arr.value_as_string(24));
8909        assert!(decimal_arr.is_null(25));
8910        assert!(decimal_arr.is_null(26));
8911        assert!(decimal_arr.is_null(27));
8912        assert_eq!("0.000", decimal_arr.value_as_string(28));
8913        assert_eq!("0.000", decimal_arr.value_as_string(29));
8914        assert_eq!("12345.000", decimal_arr.value_as_string(30));
8915        assert_eq!(decimal_arr.len(), 31);
8916    }
8917
8918    #[test]
8919    fn test_cast_utf8_to_decimal() {
8920        let str_array = StringArray::from(vec![
8921            Some("123.45"),
8922            Some("1.2345"),
8923            Some("0.12345"),
8924            Some("0.1267"),
8925            Some("1.263"),
8926            Some("12345.0"),
8927            Some("12345"),
8928            Some("000.123"),
8929            Some("12.234000"),
8930            None,
8931            Some(""),
8932            Some(" "),
8933            None,
8934            Some("-1.23499999"),
8935            Some("-1.23599999"),
8936            Some("-0.00001"),
8937            Some("-123"),
8938            Some("-123.234000"),
8939            Some("-000.123"),
8940            Some("+1.23499999"),
8941            Some("+1.23599999"),
8942            Some("+0.00001"),
8943            Some("+123"),
8944            Some("+123.234000"),
8945            Some("+000.123"),
8946            Some("1.-23499999"),
8947            Some("-1.-23499999"),
8948            Some("--1.23499999"),
8949            Some("0"),
8950            Some("000.000"),
8951            Some("0000000000000000012345.000"),
8952        ]);
8953        let array = Arc::new(str_array) as ArrayRef;
8954
8955        test_cast_string_to_decimal(array);
8956
8957        let test_cases = [
8958            (None, None),
8959            // (Some(""), None),
8960            // (Some("   "), None),
8961            (Some("0"), Some("0")),
8962            (Some("000.000"), Some("0")),
8963            (Some("12345"), Some("12345")),
8964            (Some("000000000000000000000000000012345"), Some("12345")),
8965            (Some("-123"), Some("-123")),
8966            (Some("+123"), Some("123")),
8967        ];
8968        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
8969        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
8970
8971        let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
8972        test_cast_string_to_decimal_scale_zero(array, &expected);
8973    }
8974
8975    #[test]
8976    fn test_cast_large_utf8_to_decimal() {
8977        let str_array = LargeStringArray::from(vec![
8978            Some("123.45"),
8979            Some("1.2345"),
8980            Some("0.12345"),
8981            Some("0.1267"),
8982            Some("1.263"),
8983            Some("12345.0"),
8984            Some("12345"),
8985            Some("000.123"),
8986            Some("12.234000"),
8987            None,
8988            Some(""),
8989            Some(" "),
8990            None,
8991            Some("-1.23499999"),
8992            Some("-1.23599999"),
8993            Some("-0.00001"),
8994            Some("-123"),
8995            Some("-123.234000"),
8996            Some("-000.123"),
8997            Some("+1.23499999"),
8998            Some("+1.23599999"),
8999            Some("+0.00001"),
9000            Some("+123"),
9001            Some("+123.234000"),
9002            Some("+000.123"),
9003            Some("1.-23499999"),
9004            Some("-1.-23499999"),
9005            Some("--1.23499999"),
9006            Some("0"),
9007            Some("000.000"),
9008            Some("0000000000000000012345.000"),
9009        ]);
9010        let array = Arc::new(str_array) as ArrayRef;
9011
9012        test_cast_string_to_decimal(array);
9013
9014        let test_cases = [
9015            (None, None),
9016            (Some(""), None),
9017            (Some("   "), None),
9018            (Some("0"), Some("0")),
9019            (Some("000.000"), Some("0")),
9020            (Some("12345"), Some("12345")),
9021            (Some("000000000000000000000000000012345"), Some("12345")),
9022            (Some("-123"), Some("-123")),
9023            (Some("+123"), Some("123")),
9024        ];
9025        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
9026        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
9027
9028        let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
9029        test_cast_string_to_decimal_scale_zero(array, &expected);
9030    }
9031
9032    fn test_cast_string_to_decimal_scale_zero(
9033        array: ArrayRef,
9034        expected_as_string: &[Option<&str>],
9035    ) {
9036        // Decimal128
9037        let output_type = DataType::Decimal128(38, 0);
9038        assert!(can_cast_types(array.data_type(), &output_type));
9039        let casted_array = cast(&array, &output_type).unwrap();
9040        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9041        assert_decimal_array_contents(decimal_arr, expected_as_string);
9042
9043        // Decimal256
9044        let output_type = DataType::Decimal256(76, 0);
9045        assert!(can_cast_types(array.data_type(), &output_type));
9046        let casted_array = cast(&array, &output_type).unwrap();
9047        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
9048        assert_decimal_array_contents(decimal_arr, expected_as_string);
9049    }
9050
9051    fn assert_decimal_array_contents<T>(
9052        array: &PrimitiveArray<T>,
9053        expected_as_string: &[Option<&str>],
9054    ) where
9055        T: DecimalType + ArrowPrimitiveType,
9056    {
9057        assert_eq!(array.len(), expected_as_string.len());
9058        for (i, expected) in expected_as_string.iter().enumerate() {
9059            let actual = if array.is_null(i) {
9060                None
9061            } else {
9062                Some(array.value_as_string(i))
9063            };
9064            let actual = actual.as_ref().map(|s| s.as_ref());
9065            assert_eq!(*expected, actual, "Expected at position {}", i);
9066        }
9067    }
9068
9069    #[test]
9070    fn test_cast_invalid_utf8_to_decimal() {
9071        let str_array = StringArray::from(vec!["4.4.5", ". 0.123"]);
9072        let array = Arc::new(str_array) as ArrayRef;
9073
9074        // Safe cast
9075        let output_type = DataType::Decimal128(38, 2);
9076        let casted_array = cast(&array, &output_type).unwrap();
9077        assert!(casted_array.is_null(0));
9078        assert!(casted_array.is_null(1));
9079
9080        let output_type = DataType::Decimal256(76, 2);
9081        let casted_array = cast(&array, &output_type).unwrap();
9082        assert!(casted_array.is_null(0));
9083        assert!(casted_array.is_null(1));
9084
9085        // Non-safe cast
9086        let output_type = DataType::Decimal128(38, 2);
9087        let str_array = StringArray::from(vec!["4.4.5"]);
9088        let array = Arc::new(str_array) as ArrayRef;
9089        let option = CastOptions {
9090            safe: false,
9091            format_options: FormatOptions::default(),
9092        };
9093        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
9094        assert!(casted_err
9095            .to_string()
9096            .contains("Cannot cast string '4.4.5' to value of Decimal128(38, 10) type"));
9097
9098        let str_array = StringArray::from(vec![". 0.123"]);
9099        let array = Arc::new(str_array) as ArrayRef;
9100        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
9101        assert!(casted_err
9102            .to_string()
9103            .contains("Cannot cast string '. 0.123' to value of Decimal128(38, 10) type"));
9104    }
9105
9106    fn test_cast_string_to_decimal128_overflow(overflow_array: ArrayRef) {
9107        let output_type = DataType::Decimal128(38, 2);
9108        let casted_array = cast(&overflow_array, &output_type).unwrap();
9109        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9110
9111        assert!(decimal_arr.is_null(0));
9112        assert!(decimal_arr.is_null(1));
9113        assert!(decimal_arr.is_null(2));
9114        assert_eq!(
9115            "999999999999999999999999999999999999.99",
9116            decimal_arr.value_as_string(3)
9117        );
9118        assert_eq!(
9119            "100000000000000000000000000000000000.00",
9120            decimal_arr.value_as_string(4)
9121        );
9122    }
9123
9124    #[test]
9125    fn test_cast_string_to_decimal128_precision_overflow() {
9126        let array = StringArray::from(vec!["1000".to_string()]);
9127        let array = Arc::new(array) as ArrayRef;
9128        let casted_array = cast_with_options(
9129            &array,
9130            &DataType::Decimal128(10, 8),
9131            &CastOptions {
9132                safe: true,
9133                format_options: FormatOptions::default(),
9134            },
9135        );
9136        assert!(casted_array.is_ok());
9137        assert!(casted_array.unwrap().is_null(0));
9138
9139        let err = cast_with_options(
9140            &array,
9141            &DataType::Decimal128(10, 8),
9142            &CastOptions {
9143                safe: false,
9144                format_options: FormatOptions::default(),
9145            },
9146        );
9147        assert_eq!("Invalid argument error: 100000000000 is too large to store in a Decimal128 of precision 10. Max is 9999999999", err.unwrap_err().to_string());
9148    }
9149
9150    #[test]
9151    fn test_cast_utf8_to_decimal128_overflow() {
9152        let overflow_str_array = StringArray::from(vec![
9153            i128::MAX.to_string(),
9154            i128::MIN.to_string(),
9155            "99999999999999999999999999999999999999".to_string(),
9156            "999999999999999999999999999999999999.99".to_string(),
9157            "99999999999999999999999999999999999.999".to_string(),
9158        ]);
9159        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9160
9161        test_cast_string_to_decimal128_overflow(overflow_array);
9162    }
9163
9164    #[test]
9165    fn test_cast_large_utf8_to_decimal128_overflow() {
9166        let overflow_str_array = LargeStringArray::from(vec![
9167            i128::MAX.to_string(),
9168            i128::MIN.to_string(),
9169            "99999999999999999999999999999999999999".to_string(),
9170            "999999999999999999999999999999999999.99".to_string(),
9171            "99999999999999999999999999999999999.999".to_string(),
9172        ]);
9173        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9174
9175        test_cast_string_to_decimal128_overflow(overflow_array);
9176    }
9177
9178    fn test_cast_string_to_decimal256_overflow(overflow_array: ArrayRef) {
9179        let output_type = DataType::Decimal256(76, 2);
9180        let casted_array = cast(&overflow_array, &output_type).unwrap();
9181        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
9182
9183        assert_eq!(
9184            "170141183460469231731687303715884105727.00",
9185            decimal_arr.value_as_string(0)
9186        );
9187        assert_eq!(
9188            "-170141183460469231731687303715884105728.00",
9189            decimal_arr.value_as_string(1)
9190        );
9191        assert_eq!(
9192            "99999999999999999999999999999999999999.00",
9193            decimal_arr.value_as_string(2)
9194        );
9195        assert_eq!(
9196            "999999999999999999999999999999999999.99",
9197            decimal_arr.value_as_string(3)
9198        );
9199        assert_eq!(
9200            "100000000000000000000000000000000000.00",
9201            decimal_arr.value_as_string(4)
9202        );
9203        assert!(decimal_arr.is_null(5));
9204        assert!(decimal_arr.is_null(6));
9205    }
9206
9207    #[test]
9208    fn test_cast_string_to_decimal256_precision_overflow() {
9209        let array = StringArray::from(vec!["1000".to_string()]);
9210        let array = Arc::new(array) as ArrayRef;
9211        let casted_array = cast_with_options(
9212            &array,
9213            &DataType::Decimal256(10, 8),
9214            &CastOptions {
9215                safe: true,
9216                format_options: FormatOptions::default(),
9217            },
9218        );
9219        assert!(casted_array.is_ok());
9220        assert!(casted_array.unwrap().is_null(0));
9221
9222        let err = cast_with_options(
9223            &array,
9224            &DataType::Decimal256(10, 8),
9225            &CastOptions {
9226                safe: false,
9227                format_options: FormatOptions::default(),
9228            },
9229        );
9230        assert_eq!("Invalid argument error: 100000000000 is too large to store in a Decimal256 of precision 10. Max is 9999999999", err.unwrap_err().to_string());
9231    }
9232
9233    #[test]
9234    fn test_cast_utf8_to_decimal256_overflow() {
9235        let overflow_str_array = StringArray::from(vec![
9236            i128::MAX.to_string(),
9237            i128::MIN.to_string(),
9238            "99999999999999999999999999999999999999".to_string(),
9239            "999999999999999999999999999999999999.99".to_string(),
9240            "99999999999999999999999999999999999.999".to_string(),
9241            i256::MAX.to_string(),
9242            i256::MIN.to_string(),
9243        ]);
9244        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9245
9246        test_cast_string_to_decimal256_overflow(overflow_array);
9247    }
9248
9249    #[test]
9250    fn test_cast_large_utf8_to_decimal256_overflow() {
9251        let overflow_str_array = LargeStringArray::from(vec![
9252            i128::MAX.to_string(),
9253            i128::MIN.to_string(),
9254            "99999999999999999999999999999999999999".to_string(),
9255            "999999999999999999999999999999999999.99".to_string(),
9256            "99999999999999999999999999999999999.999".to_string(),
9257            i256::MAX.to_string(),
9258            i256::MIN.to_string(),
9259        ]);
9260        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9261
9262        test_cast_string_to_decimal256_overflow(overflow_array);
9263    }
9264
9265    #[test]
9266    fn test_cast_outside_supported_range_for_nanoseconds() {
9267        const EXPECTED_ERROR_MESSAGE: &str = "The dates that can be represented as nanoseconds have to be between 1677-09-21T00:12:44.0 and 2262-04-11T23:47:16.854775804";
9268
9269        let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
9270
9271        let cast_options = CastOptions {
9272            safe: false,
9273            format_options: FormatOptions::default(),
9274        };
9275
9276        let result = cast_string_to_timestamp::<i32, TimestampNanosecondType>(
9277            &array,
9278            &None::<Arc<str>>,
9279            &cast_options,
9280        );
9281
9282        let err = result.unwrap_err();
9283        assert_eq!(
9284            err.to_string(),
9285            format!(
9286                "Cast error: Overflow converting {} to Nanosecond. {}",
9287                array.value(0),
9288                EXPECTED_ERROR_MESSAGE
9289            )
9290        );
9291    }
9292
9293    #[test]
9294    fn test_cast_date32_to_timestamp() {
9295        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
9296        let array = Arc::new(a) as ArrayRef;
9297        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
9298        let c = b.as_primitive::<TimestampSecondType>();
9299        assert_eq!(1609459200, c.value(0));
9300        assert_eq!(1640995200, c.value(1));
9301        assert!(c.is_null(2));
9302    }
9303
9304    #[test]
9305    fn test_cast_date32_to_timestamp_ms() {
9306        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
9307        let array = Arc::new(a) as ArrayRef;
9308        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
9309        let c = b
9310            .as_any()
9311            .downcast_ref::<TimestampMillisecondArray>()
9312            .unwrap();
9313        assert_eq!(1609459200000, c.value(0));
9314        assert_eq!(1640995200000, c.value(1));
9315        assert!(c.is_null(2));
9316    }
9317
9318    #[test]
9319    fn test_cast_date32_to_timestamp_us() {
9320        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
9321        let array = Arc::new(a) as ArrayRef;
9322        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
9323        let c = b
9324            .as_any()
9325            .downcast_ref::<TimestampMicrosecondArray>()
9326            .unwrap();
9327        assert_eq!(1609459200000000, c.value(0));
9328        assert_eq!(1640995200000000, c.value(1));
9329        assert!(c.is_null(2));
9330    }
9331
9332    #[test]
9333    fn test_cast_date32_to_timestamp_ns() {
9334        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
9335        let array = Arc::new(a) as ArrayRef;
9336        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
9337        let c = b
9338            .as_any()
9339            .downcast_ref::<TimestampNanosecondArray>()
9340            .unwrap();
9341        assert_eq!(1609459200000000000, c.value(0));
9342        assert_eq!(1640995200000000000, c.value(1));
9343        assert!(c.is_null(2));
9344    }
9345
9346    #[test]
9347    fn test_timezone_cast() {
9348        let a = StringArray::from(vec![
9349            "2000-01-01T12:00:00", // date + time valid
9350            "2020-12-15T12:34:56", // date + time valid
9351        ]);
9352        let array = Arc::new(a) as ArrayRef;
9353        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
9354        let v = b.as_primitive::<TimestampNanosecondType>();
9355
9356        assert_eq!(v.value(0), 946728000000000000);
9357        assert_eq!(v.value(1), 1608035696000000000);
9358
9359        let b = cast(
9360            &b,
9361            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
9362        )
9363        .unwrap();
9364        let v = b.as_primitive::<TimestampNanosecondType>();
9365
9366        assert_eq!(v.value(0), 946728000000000000);
9367        assert_eq!(v.value(1), 1608035696000000000);
9368
9369        let b = cast(
9370            &b,
9371            &DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
9372        )
9373        .unwrap();
9374        let v = b.as_primitive::<TimestampMillisecondType>();
9375
9376        assert_eq!(v.value(0), 946728000000);
9377        assert_eq!(v.value(1), 1608035696000);
9378    }
9379
9380    #[test]
9381    fn test_cast_utf8_to_timestamp() {
9382        fn test_tz(tz: Arc<str>) {
9383            let valid = StringArray::from(vec![
9384                "2023-01-01 04:05:06.789000-08:00",
9385                "2023-01-01 04:05:06.789000-07:00",
9386                "2023-01-01 04:05:06.789 -0800",
9387                "2023-01-01 04:05:06.789 -08:00",
9388                "2023-01-01 040506 +0730",
9389                "2023-01-01 040506 +07:30",
9390                "2023-01-01 04:05:06.789",
9391                "2023-01-01 04:05:06",
9392                "2023-01-01",
9393            ]);
9394
9395            let array = Arc::new(valid) as ArrayRef;
9396            let b = cast_with_options(
9397                &array,
9398                &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
9399                &CastOptions {
9400                    safe: false,
9401                    format_options: FormatOptions::default(),
9402                },
9403            )
9404            .unwrap();
9405
9406            let tz = tz.as_ref().parse().unwrap();
9407
9408            let as_tz =
9409                |v: i64| as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap();
9410
9411            let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
9412            let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
9413
9414            let values = b.as_primitive::<TimestampNanosecondType>().values();
9415            let utc_results: Vec<_> = values.iter().map(as_utc).collect();
9416            let local_results: Vec<_> = values.iter().map(as_local).collect();
9417
9418            // Absolute timestamps should be parsed preserving the same UTC instant
9419            assert_eq!(
9420                &utc_results[..6],
9421                &[
9422                    "2023-01-01 12:05:06.789".to_string(),
9423                    "2023-01-01 11:05:06.789".to_string(),
9424                    "2023-01-01 12:05:06.789".to_string(),
9425                    "2023-01-01 12:05:06.789".to_string(),
9426                    "2022-12-31 20:35:06".to_string(),
9427                    "2022-12-31 20:35:06".to_string(),
9428                ]
9429            );
9430            // Non-absolute timestamps should be parsed preserving the same local instant
9431            assert_eq!(
9432                &local_results[6..],
9433                &[
9434                    "2023-01-01 04:05:06.789".to_string(),
9435                    "2023-01-01 04:05:06".to_string(),
9436                    "2023-01-01 00:00:00".to_string()
9437                ]
9438            )
9439        }
9440
9441        test_tz("+00:00".into());
9442        test_tz("+02:00".into());
9443    }
9444
9445    #[test]
9446    fn test_cast_invalid_utf8() {
9447        let v1: &[u8] = b"\xFF invalid";
9448        let v2: &[u8] = b"\x00 Foo";
9449        let s = BinaryArray::from(vec![v1, v2]);
9450        let options = CastOptions {
9451            safe: true,
9452            format_options: FormatOptions::default(),
9453        };
9454        let array = cast_with_options(&s, &DataType::Utf8, &options).unwrap();
9455        let a = array.as_string::<i32>();
9456        a.to_data().validate_full().unwrap();
9457
9458        assert_eq!(a.null_count(), 1);
9459        assert_eq!(a.len(), 2);
9460        assert!(a.is_null(0));
9461        assert_eq!(a.value(0), "");
9462        assert_eq!(a.value(1), "\x00 Foo");
9463    }
9464
9465    #[test]
9466    fn test_cast_utf8_to_timestamptz() {
9467        let valid = StringArray::from(vec!["2023-01-01"]);
9468
9469        let array = Arc::new(valid) as ArrayRef;
9470        let b = cast(
9471            &array,
9472            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
9473        )
9474        .unwrap();
9475
9476        let expect = DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into()));
9477
9478        assert_eq!(b.data_type(), &expect);
9479        let c = b
9480            .as_any()
9481            .downcast_ref::<TimestampNanosecondArray>()
9482            .unwrap();
9483        assert_eq!(1672531200000000000, c.value(0));
9484    }
9485
9486    #[test]
9487    fn test_cast_decimal_to_string() {
9488        assert!(can_cast_types(
9489            &DataType::Decimal128(10, 4),
9490            &DataType::Utf8View
9491        ));
9492        assert!(can_cast_types(
9493            &DataType::Decimal256(38, 10),
9494            &DataType::Utf8View
9495        ));
9496
9497        macro_rules! assert_decimal_values {
9498            ($array:expr) => {
9499                let c = $array;
9500                assert_eq!("1123.454", c.value(0));
9501                assert_eq!("2123.456", c.value(1));
9502                assert_eq!("-3123.453", c.value(2));
9503                assert_eq!("-3123.456", c.value(3));
9504                assert_eq!("0.000", c.value(4));
9505                assert_eq!("0.123", c.value(5));
9506                assert_eq!("1234.567", c.value(6));
9507                assert_eq!("-1234.567", c.value(7));
9508                assert!(c.is_null(8));
9509            };
9510        }
9511
9512        fn test_decimal_to_string<IN: ArrowPrimitiveType, OffsetSize: OffsetSizeTrait>(
9513            output_type: DataType,
9514            array: PrimitiveArray<IN>,
9515        ) {
9516            let b = cast(&array, &output_type).unwrap();
9517
9518            assert_eq!(b.data_type(), &output_type);
9519            match b.data_type() {
9520                DataType::Utf8View => {
9521                    let c = b.as_string_view();
9522                    assert_decimal_values!(c);
9523                }
9524                DataType::Utf8 | DataType::LargeUtf8 => {
9525                    let c = b.as_string::<OffsetSize>();
9526                    assert_decimal_values!(c);
9527                }
9528                _ => (),
9529            }
9530        }
9531
9532        let array128: Vec<Option<i128>> = vec![
9533            Some(1123454),
9534            Some(2123456),
9535            Some(-3123453),
9536            Some(-3123456),
9537            Some(0),
9538            Some(123),
9539            Some(123456789),
9540            Some(-123456789),
9541            None,
9542        ];
9543        let array256: Vec<Option<i256>> = array128
9544            .iter()
9545            .map(|num| num.map(i256::from_i128))
9546            .collect();
9547
9548        test_decimal_to_string::<Decimal128Type, i32>(
9549            DataType::Utf8View,
9550            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
9551        );
9552        test_decimal_to_string::<Decimal128Type, i32>(
9553            DataType::Utf8,
9554            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
9555        );
9556        test_decimal_to_string::<Decimal128Type, i64>(
9557            DataType::LargeUtf8,
9558            create_decimal128_array(array128, 7, 3).unwrap(),
9559        );
9560
9561        test_decimal_to_string::<Decimal256Type, i32>(
9562            DataType::Utf8View,
9563            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
9564        );
9565        test_decimal_to_string::<Decimal256Type, i32>(
9566            DataType::Utf8,
9567            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
9568        );
9569        test_decimal_to_string::<Decimal256Type, i64>(
9570            DataType::LargeUtf8,
9571            create_decimal256_array(array256, 7, 3).unwrap(),
9572        );
9573    }
9574
9575    #[test]
9576    fn test_cast_numeric_to_decimal128_precision_overflow() {
9577        let array = Int64Array::from(vec![1234567]);
9578        let array = Arc::new(array) as ArrayRef;
9579        let casted_array = cast_with_options(
9580            &array,
9581            &DataType::Decimal128(7, 3),
9582            &CastOptions {
9583                safe: true,
9584                format_options: FormatOptions::default(),
9585            },
9586        );
9587        assert!(casted_array.is_ok());
9588        assert!(casted_array.unwrap().is_null(0));
9589
9590        let err = cast_with_options(
9591            &array,
9592            &DataType::Decimal128(7, 3),
9593            &CastOptions {
9594                safe: false,
9595                format_options: FormatOptions::default(),
9596            },
9597        );
9598        assert_eq!("Invalid argument error: 1234567000 is too large to store in a Decimal128 of precision 7. Max is 9999999", err.unwrap_err().to_string());
9599    }
9600
9601    #[test]
9602    fn test_cast_numeric_to_decimal256_precision_overflow() {
9603        let array = Int64Array::from(vec![1234567]);
9604        let array = Arc::new(array) as ArrayRef;
9605        let casted_array = cast_with_options(
9606            &array,
9607            &DataType::Decimal256(7, 3),
9608            &CastOptions {
9609                safe: true,
9610                format_options: FormatOptions::default(),
9611            },
9612        );
9613        assert!(casted_array.is_ok());
9614        assert!(casted_array.unwrap().is_null(0));
9615
9616        let err = cast_with_options(
9617            &array,
9618            &DataType::Decimal256(7, 3),
9619            &CastOptions {
9620                safe: false,
9621                format_options: FormatOptions::default(),
9622            },
9623        );
9624        assert_eq!("Invalid argument error: 1234567000 is too large to store in a Decimal256 of precision 7. Max is 9999999", err.unwrap_err().to_string());
9625    }
9626
9627    /// helper function to test casting from duration to interval
9628    fn cast_from_duration_to_interval<T: ArrowTemporalType<Native = i64>>(
9629        array: Vec<i64>,
9630        cast_options: &CastOptions,
9631    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
9632        let array = PrimitiveArray::<T>::new(array.into(), None);
9633        let array = Arc::new(array) as ArrayRef;
9634        let interval = DataType::Interval(IntervalUnit::MonthDayNano);
9635        let out = cast_with_options(&array, &interval, cast_options)?;
9636        let out = out.as_primitive::<IntervalMonthDayNanoType>().clone();
9637        Ok(out)
9638    }
9639
9640    #[test]
9641    fn test_cast_from_duration_to_interval() {
9642        // from duration second to interval month day nano
9643        let array = vec![1234567];
9644        let casted_array =
9645            cast_from_duration_to_interval::<DurationSecondType>(array, &CastOptions::default())
9646                .unwrap();
9647        assert_eq!(
9648            casted_array.data_type(),
9649            &DataType::Interval(IntervalUnit::MonthDayNano)
9650        );
9651        assert_eq!(
9652            casted_array.value(0),
9653            IntervalMonthDayNano::new(0, 0, 1234567000000000)
9654        );
9655
9656        let array = vec![i64::MAX];
9657        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
9658            array.clone(),
9659            &CastOptions::default(),
9660        )
9661        .unwrap();
9662        assert!(!casted_array.is_valid(0));
9663
9664        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
9665            array,
9666            &CastOptions {
9667                safe: false,
9668                format_options: FormatOptions::default(),
9669            },
9670        );
9671        assert!(casted_array.is_err());
9672
9673        // from duration millisecond to interval month day nano
9674        let array = vec![1234567];
9675        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
9676            array,
9677            &CastOptions::default(),
9678        )
9679        .unwrap();
9680        assert_eq!(
9681            casted_array.data_type(),
9682            &DataType::Interval(IntervalUnit::MonthDayNano)
9683        );
9684        assert_eq!(
9685            casted_array.value(0),
9686            IntervalMonthDayNano::new(0, 0, 1234567000000)
9687        );
9688
9689        let array = vec![i64::MAX];
9690        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
9691            array.clone(),
9692            &CastOptions::default(),
9693        )
9694        .unwrap();
9695        assert!(!casted_array.is_valid(0));
9696
9697        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
9698            array,
9699            &CastOptions {
9700                safe: false,
9701                format_options: FormatOptions::default(),
9702            },
9703        );
9704        assert!(casted_array.is_err());
9705
9706        // from duration microsecond to interval month day nano
9707        let array = vec![1234567];
9708        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
9709            array,
9710            &CastOptions::default(),
9711        )
9712        .unwrap();
9713        assert_eq!(
9714            casted_array.data_type(),
9715            &DataType::Interval(IntervalUnit::MonthDayNano)
9716        );
9717        assert_eq!(
9718            casted_array.value(0),
9719            IntervalMonthDayNano::new(0, 0, 1234567000)
9720        );
9721
9722        let array = vec![i64::MAX];
9723        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
9724            array.clone(),
9725            &CastOptions::default(),
9726        )
9727        .unwrap();
9728        assert!(!casted_array.is_valid(0));
9729
9730        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
9731            array,
9732            &CastOptions {
9733                safe: false,
9734                format_options: FormatOptions::default(),
9735            },
9736        );
9737        assert!(casted_array.is_err());
9738
9739        // from duration nanosecond to interval month day nano
9740        let array = vec![1234567];
9741        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
9742            array,
9743            &CastOptions::default(),
9744        )
9745        .unwrap();
9746        assert_eq!(
9747            casted_array.data_type(),
9748            &DataType::Interval(IntervalUnit::MonthDayNano)
9749        );
9750        assert_eq!(
9751            casted_array.value(0),
9752            IntervalMonthDayNano::new(0, 0, 1234567)
9753        );
9754
9755        let array = vec![i64::MAX];
9756        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
9757            array,
9758            &CastOptions {
9759                safe: false,
9760                format_options: FormatOptions::default(),
9761            },
9762        )
9763        .unwrap();
9764        assert_eq!(
9765            casted_array.value(0),
9766            IntervalMonthDayNano::new(0, 0, i64::MAX)
9767        );
9768    }
9769
9770    /// helper function to test casting from interval to duration
9771    fn cast_from_interval_to_duration<T: ArrowTemporalType>(
9772        array: &IntervalMonthDayNanoArray,
9773        cast_options: &CastOptions,
9774    ) -> Result<PrimitiveArray<T>, ArrowError> {
9775        let casted_array = cast_with_options(&array, &T::DATA_TYPE, cast_options)?;
9776        casted_array
9777            .as_any()
9778            .downcast_ref::<PrimitiveArray<T>>()
9779            .ok_or_else(|| {
9780                ArrowError::ComputeError(format!("Failed to downcast to {}", T::DATA_TYPE))
9781            })
9782            .cloned()
9783    }
9784
9785    #[test]
9786    fn test_cast_from_interval_to_duration() {
9787        let nullable = CastOptions::default();
9788        let fallible = CastOptions {
9789            safe: false,
9790            format_options: FormatOptions::default(),
9791        };
9792        let v = IntervalMonthDayNano::new(0, 0, 1234567);
9793
9794        // from interval month day nano to duration second
9795        let array = vec![v].into();
9796        let casted_array: DurationSecondArray =
9797            cast_from_interval_to_duration(&array, &nullable).unwrap();
9798        assert_eq!(casted_array.value(0), 0);
9799
9800        let array = vec![IntervalMonthDayNano::MAX].into();
9801        let casted_array: DurationSecondArray =
9802            cast_from_interval_to_duration(&array, &nullable).unwrap();
9803        assert!(!casted_array.is_valid(0));
9804
9805        let res = cast_from_interval_to_duration::<DurationSecondType>(&array, &fallible);
9806        assert!(res.is_err());
9807
9808        // from interval month day nano to duration millisecond
9809        let array = vec![v].into();
9810        let casted_array: DurationMillisecondArray =
9811            cast_from_interval_to_duration(&array, &nullable).unwrap();
9812        assert_eq!(casted_array.value(0), 1);
9813
9814        let array = vec![IntervalMonthDayNano::MAX].into();
9815        let casted_array: DurationMillisecondArray =
9816            cast_from_interval_to_duration(&array, &nullable).unwrap();
9817        assert!(!casted_array.is_valid(0));
9818
9819        let res = cast_from_interval_to_duration::<DurationMillisecondType>(&array, &fallible);
9820        assert!(res.is_err());
9821
9822        // from interval month day nano to duration microsecond
9823        let array = vec![v].into();
9824        let casted_array: DurationMicrosecondArray =
9825            cast_from_interval_to_duration(&array, &nullable).unwrap();
9826        assert_eq!(casted_array.value(0), 1234);
9827
9828        let array = vec![IntervalMonthDayNano::MAX].into();
9829        let casted_array =
9830            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &nullable).unwrap();
9831        assert!(!casted_array.is_valid(0));
9832
9833        let casted_array =
9834            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &fallible);
9835        assert!(casted_array.is_err());
9836
9837        // from interval month day nano to duration nanosecond
9838        let array = vec![v].into();
9839        let casted_array: DurationNanosecondArray =
9840            cast_from_interval_to_duration(&array, &nullable).unwrap();
9841        assert_eq!(casted_array.value(0), 1234567);
9842
9843        let array = vec![IntervalMonthDayNano::MAX].into();
9844        let casted_array: DurationNanosecondArray =
9845            cast_from_interval_to_duration(&array, &nullable).unwrap();
9846        assert!(!casted_array.is_valid(0));
9847
9848        let casted_array =
9849            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &fallible);
9850        assert!(casted_array.is_err());
9851
9852        let array = vec![
9853            IntervalMonthDayNanoType::make_value(0, 1, 0),
9854            IntervalMonthDayNanoType::make_value(-1, 0, 0),
9855            IntervalMonthDayNanoType::make_value(1, 1, 0),
9856            IntervalMonthDayNanoType::make_value(1, 0, 1),
9857            IntervalMonthDayNanoType::make_value(0, 0, -1),
9858        ]
9859        .into();
9860        let casted_array =
9861            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable).unwrap();
9862        assert!(!casted_array.is_valid(0));
9863        assert!(!casted_array.is_valid(1));
9864        assert!(!casted_array.is_valid(2));
9865        assert!(!casted_array.is_valid(3));
9866        assert!(casted_array.is_valid(4));
9867        assert_eq!(casted_array.value(4), -1);
9868    }
9869
9870    /// helper function to test casting from interval year month to interval month day nano
9871    fn cast_from_interval_year_month_to_interval_month_day_nano(
9872        array: Vec<i32>,
9873        cast_options: &CastOptions,
9874    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
9875        let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
9876        let array = Arc::new(array) as ArrayRef;
9877        let casted_array = cast_with_options(
9878            &array,
9879            &DataType::Interval(IntervalUnit::MonthDayNano),
9880            cast_options,
9881        )?;
9882        casted_array
9883            .as_any()
9884            .downcast_ref::<IntervalMonthDayNanoArray>()
9885            .ok_or_else(|| {
9886                ArrowError::ComputeError(
9887                    "Failed to downcast to IntervalMonthDayNanoArray".to_string(),
9888                )
9889            })
9890            .cloned()
9891    }
9892
9893    #[test]
9894    fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
9895        // from interval year month to interval month day nano
9896        let array = vec![1234567];
9897        let casted_array = cast_from_interval_year_month_to_interval_month_day_nano(
9898            array,
9899            &CastOptions::default(),
9900        )
9901        .unwrap();
9902        assert_eq!(
9903            casted_array.data_type(),
9904            &DataType::Interval(IntervalUnit::MonthDayNano)
9905        );
9906        assert_eq!(
9907            casted_array.value(0),
9908            IntervalMonthDayNano::new(1234567, 0, 0)
9909        );
9910    }
9911
9912    /// helper function to test casting from interval day time to interval month day nano
9913    fn cast_from_interval_day_time_to_interval_month_day_nano(
9914        array: Vec<IntervalDayTime>,
9915        cast_options: &CastOptions,
9916    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
9917        let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
9918        let array = Arc::new(array) as ArrayRef;
9919        let casted_array = cast_with_options(
9920            &array,
9921            &DataType::Interval(IntervalUnit::MonthDayNano),
9922            cast_options,
9923        )?;
9924        Ok(casted_array
9925            .as_primitive::<IntervalMonthDayNanoType>()
9926            .clone())
9927    }
9928
9929    #[test]
9930    fn test_cast_from_interval_day_time_to_interval_month_day_nano() {
9931        // from interval day time to interval month day nano
9932        let array = vec![IntervalDayTime::new(123, 0)];
9933        let casted_array =
9934            cast_from_interval_day_time_to_interval_month_day_nano(array, &CastOptions::default())
9935                .unwrap();
9936        assert_eq!(
9937            casted_array.data_type(),
9938            &DataType::Interval(IntervalUnit::MonthDayNano)
9939        );
9940        assert_eq!(casted_array.value(0), IntervalMonthDayNano::new(0, 123, 0));
9941    }
9942
9943    #[test]
9944    fn test_cast_below_unixtimestamp() {
9945        let valid = StringArray::from(vec![
9946            "1900-01-03 23:59:59",
9947            "1969-12-31 00:00:01",
9948            "1989-12-31 00:00:01",
9949        ]);
9950
9951        let array = Arc::new(valid) as ArrayRef;
9952        let casted_array = cast_with_options(
9953            &array,
9954            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
9955            &CastOptions {
9956                safe: false,
9957                format_options: FormatOptions::default(),
9958            },
9959        )
9960        .unwrap();
9961
9962        let ts_array = casted_array
9963            .as_primitive::<TimestampNanosecondType>()
9964            .values()
9965            .iter()
9966            .map(|ts| ts / 1_000_000)
9967            .collect::<Vec<_>>();
9968
9969        let array = TimestampMillisecondArray::from(ts_array).with_timezone("+00:00".to_string());
9970        let casted_array = cast(&array, &DataType::Date32).unwrap();
9971        let date_array = casted_array.as_primitive::<Date32Type>();
9972        let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
9973        let string_array = casted_array.as_string::<i32>();
9974        assert_eq!("1900-01-03", string_array.value(0));
9975        assert_eq!("1969-12-31", string_array.value(1));
9976        assert_eq!("1989-12-31", string_array.value(2));
9977    }
9978
9979    #[test]
9980    fn test_nested_list() {
9981        let mut list = ListBuilder::new(Int32Builder::new());
9982        list.append_value([Some(1), Some(2), Some(3)]);
9983        list.append_value([Some(4), None, Some(6)]);
9984        let list = list.finish();
9985
9986        let to_field = Field::new("nested", list.data_type().clone(), false);
9987        let to = DataType::List(Arc::new(to_field));
9988        let out = cast(&list, &to).unwrap();
9989        let opts = FormatOptions::default().with_null("null");
9990        let formatted = ArrayFormatter::try_new(out.as_ref(), &opts).unwrap();
9991
9992        assert_eq!(formatted.value(0).to_string(), "[[1], [2], [3]]");
9993        assert_eq!(formatted.value(1).to_string(), "[[4], [null], [6]]");
9994    }
9995
9996    #[test]
9997    fn test_nested_list_cast() {
9998        let mut builder = ListBuilder::new(ListBuilder::new(Int32Builder::new()));
9999        builder.append_value([Some([Some(1), Some(2), None]), None]);
10000        builder.append_value([None, Some([]), None]);
10001        builder.append_null();
10002        builder.append_value([Some([Some(2), Some(3)])]);
10003        let start = builder.finish();
10004
10005        let mut builder = LargeListBuilder::new(LargeListBuilder::new(Int8Builder::new()));
10006        builder.append_value([Some([Some(1), Some(2), None]), None]);
10007        builder.append_value([None, Some([]), None]);
10008        builder.append_null();
10009        builder.append_value([Some([Some(2), Some(3)])]);
10010        let expected = builder.finish();
10011
10012        let actual = cast(&start, expected.data_type()).unwrap();
10013        assert_eq!(actual.as_ref(), &expected);
10014    }
10015
10016    const CAST_OPTIONS: CastOptions<'static> = CastOptions {
10017        safe: true,
10018        format_options: FormatOptions::new(),
10019    };
10020
10021    #[test]
10022    #[allow(clippy::assertions_on_constants)]
10023    fn test_const_options() {
10024        assert!(CAST_OPTIONS.safe)
10025    }
10026
10027    #[test]
10028    fn test_list_format_options() {
10029        let options = CastOptions {
10030            safe: false,
10031            format_options: FormatOptions::default().with_null("null"),
10032        };
10033        let array = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
10034            Some(vec![Some(0), Some(1), Some(2)]),
10035            Some(vec![Some(0), None, Some(2)]),
10036        ]);
10037        let a = cast_with_options(&array, &DataType::Utf8, &options).unwrap();
10038        let r: Vec<_> = a.as_string::<i32>().iter().flatten().collect();
10039        assert_eq!(r, &["[0, 1, 2]", "[0, null, 2]"]);
10040    }
10041    #[test]
10042    fn test_cast_string_to_timestamp_invalid_tz() {
10043        // content after Z should be ignored
10044        let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
10045        let array = StringArray::from(vec![Some(bad_timestamp)]);
10046
10047        let data_types = [
10048            DataType::Timestamp(TimeUnit::Second, None),
10049            DataType::Timestamp(TimeUnit::Millisecond, None),
10050            DataType::Timestamp(TimeUnit::Microsecond, None),
10051            DataType::Timestamp(TimeUnit::Nanosecond, None),
10052        ];
10053
10054        let cast_options = CastOptions {
10055            safe: false,
10056            ..Default::default()
10057        };
10058
10059        for dt in data_types {
10060            assert_eq!(
10061                cast_with_options(&array, &dt, &cast_options)
10062                    .unwrap_err()
10063                    .to_string(),
10064                "Parser error: Invalid timezone \"ZZTOP\": only offset based timezones supported without chrono-tz feature"
10065            );
10066        }
10067    }
10068    #[test]
10069    fn test_cast_struct_to_struct() {
10070        let struct_type = DataType::Struct(
10071            vec![
10072                Field::new("a", DataType::Boolean, false),
10073                Field::new("b", DataType::Int32, false),
10074            ]
10075            .into(),
10076        );
10077        let to_type = DataType::Struct(
10078            vec![
10079                Field::new("a", DataType::Utf8, false),
10080                Field::new("b", DataType::Utf8, false),
10081            ]
10082            .into(),
10083        );
10084        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10085        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
10086        let struct_array = StructArray::from(vec![
10087            (
10088                Arc::new(Field::new("b", DataType::Boolean, false)),
10089                boolean.clone() as ArrayRef,
10090            ),
10091            (
10092                Arc::new(Field::new("c", DataType::Int32, false)),
10093                int.clone() as ArrayRef,
10094            ),
10095        ]);
10096        let casted_array = cast(&struct_array, &to_type).unwrap();
10097        let casted_array = casted_array.as_struct();
10098        assert_eq!(casted_array.data_type(), &to_type);
10099        let casted_boolean_array = casted_array
10100            .column(0)
10101            .as_string::<i32>()
10102            .into_iter()
10103            .flatten()
10104            .collect::<Vec<_>>();
10105        let casted_int_array = casted_array
10106            .column(1)
10107            .as_string::<i32>()
10108            .into_iter()
10109            .flatten()
10110            .collect::<Vec<_>>();
10111        assert_eq!(casted_boolean_array, vec!["false", "false", "true", "true"]);
10112        assert_eq!(casted_int_array, vec!["42", "28", "19", "31"]);
10113
10114        // test for can't cast
10115        let to_type = DataType::Struct(
10116            vec![
10117                Field::new("a", DataType::Date32, false),
10118                Field::new("b", DataType::Utf8, false),
10119            ]
10120            .into(),
10121        );
10122        assert!(!can_cast_types(&struct_type, &to_type));
10123        let result = cast(&struct_array, &to_type);
10124        assert_eq!(
10125            "Cast error: Casting from Boolean to Date32 not supported",
10126            result.unwrap_err().to_string()
10127        );
10128    }
10129
10130    #[test]
10131    fn test_cast_struct_to_struct_nullability() {
10132        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10133        let int = Arc::new(Int32Array::from(vec![Some(42), None, Some(19), None]));
10134        let struct_array = StructArray::from(vec![
10135            (
10136                Arc::new(Field::new("b", DataType::Boolean, false)),
10137                boolean.clone() as ArrayRef,
10138            ),
10139            (
10140                Arc::new(Field::new("c", DataType::Int32, true)),
10141                int.clone() as ArrayRef,
10142            ),
10143        ]);
10144
10145        // okay: nullable to nullable
10146        let to_type = DataType::Struct(
10147            vec![
10148                Field::new("a", DataType::Utf8, false),
10149                Field::new("b", DataType::Utf8, true),
10150            ]
10151            .into(),
10152        );
10153        cast(&struct_array, &to_type).expect("Cast nullable to nullable struct field should work");
10154
10155        // error: nullable to non-nullable
10156        let to_type = DataType::Struct(
10157            vec![
10158                Field::new("a", DataType::Utf8, false),
10159                Field::new("b", DataType::Utf8, false),
10160            ]
10161            .into(),
10162        );
10163        cast(&struct_array, &to_type)
10164            .expect_err("Cast nullable to non-nullable struct field should fail");
10165
10166        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10167        let int = Arc::new(Int32Array::from(vec![i32::MAX, 25, 1, 100]));
10168        let struct_array = StructArray::from(vec![
10169            (
10170                Arc::new(Field::new("b", DataType::Boolean, false)),
10171                boolean.clone() as ArrayRef,
10172            ),
10173            (
10174                Arc::new(Field::new("c", DataType::Int32, false)),
10175                int.clone() as ArrayRef,
10176            ),
10177        ]);
10178
10179        // okay: non-nullable to non-nullable
10180        let to_type = DataType::Struct(
10181            vec![
10182                Field::new("a", DataType::Utf8, false),
10183                Field::new("b", DataType::Utf8, false),
10184            ]
10185            .into(),
10186        );
10187        cast(&struct_array, &to_type)
10188            .expect("Cast non-nullable to non-nullable struct field should work");
10189
10190        // err: non-nullable to non-nullable but overflowing return null during casting
10191        let to_type = DataType::Struct(
10192            vec![
10193                Field::new("a", DataType::Utf8, false),
10194                Field::new("b", DataType::Int8, false),
10195            ]
10196            .into(),
10197        );
10198        cast(&struct_array, &to_type).expect_err(
10199            "Cast non-nullable to non-nullable struct field returning null should fail",
10200        );
10201    }
10202
10203    #[test]
10204    fn test_cast_struct_to_non_struct() {
10205        let boolean = Arc::new(BooleanArray::from(vec![true, false]));
10206        let struct_array = StructArray::from(vec![(
10207            Arc::new(Field::new("a", DataType::Boolean, false)),
10208            boolean.clone() as ArrayRef,
10209        )]);
10210        let to_type = DataType::Utf8;
10211        let result = cast(&struct_array, &to_type);
10212        assert_eq!(
10213            r#"Cast error: Casting from Struct([Field { name: "a", data_type: Boolean, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }]) to Utf8 not supported"#,
10214            result.unwrap_err().to_string()
10215        );
10216    }
10217
10218    #[test]
10219    fn test_cast_non_struct_to_struct() {
10220        let array = StringArray::from(vec!["a", "b"]);
10221        let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
10222        let result = cast(&array, &to_type);
10223        assert_eq!(
10224            r#"Cast error: Casting from Utf8 to Struct([Field { name: "a", data_type: Boolean, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }]) not supported"#,
10225            result.unwrap_err().to_string()
10226        );
10227    }
10228
10229    fn run_decimal_cast_test_case_between_multiple_types(t: DecimalCastTestConfig) {
10230        run_decimal_cast_test_case::<Decimal128Type, Decimal128Type>(t.clone());
10231        run_decimal_cast_test_case::<Decimal128Type, Decimal256Type>(t.clone());
10232        run_decimal_cast_test_case::<Decimal256Type, Decimal128Type>(t.clone());
10233        run_decimal_cast_test_case::<Decimal256Type, Decimal256Type>(t.clone());
10234    }
10235
10236    #[test]
10237    fn test_decimal_to_decimal_coverage() {
10238        let test_cases = [
10239            // increase precision, increase scale, infallible
10240            DecimalCastTestConfig {
10241                input_prec: 5,
10242                input_scale: 1,
10243                input_repr: 99999, // 9999.9
10244                output_prec: 10,
10245                output_scale: 6,
10246                expected_output_repr: Ok(9999900000), // 9999.900000
10247            },
10248            // increase precision, increase scale, fallible, safe
10249            DecimalCastTestConfig {
10250                input_prec: 5,
10251                input_scale: 1,
10252                input_repr: 99, // 9999.9
10253                output_prec: 7,
10254                output_scale: 6,
10255                expected_output_repr: Ok(9900000), // 9.900000
10256            },
10257            // increase precision, increase scale, fallible, unsafe
10258            DecimalCastTestConfig {
10259                input_prec: 5,
10260                input_scale: 1,
10261                input_repr: 99999, // 9999.9
10262                output_prec: 7,
10263                output_scale: 6,
10264                expected_output_repr: Err("Invalid argument error: 9999900000 is too large to store in a {} of precision 7. Max is 9999999".to_string()) // max is 9.999999
10265            },
10266            // increase precision, decrease scale, always infallible
10267            DecimalCastTestConfig {
10268                input_prec: 5,
10269                input_scale: 3,
10270                input_repr: 99999, // 99.999
10271                output_prec: 10,
10272                output_scale: 2,
10273                expected_output_repr: Ok(10000), // 100.00
10274            },
10275            // increase precision, decrease scale, no rouding
10276            DecimalCastTestConfig {
10277                input_prec: 5,
10278                input_scale: 3,
10279                input_repr: 99994, // 99.994
10280                output_prec: 10,
10281                output_scale: 2,
10282                expected_output_repr: Ok(9999), // 99.99
10283            },
10284            // increase precision, don't change scale, always infallible
10285            DecimalCastTestConfig {
10286                input_prec: 5,
10287                input_scale: 3,
10288                input_repr: 99999, // 99.999
10289                output_prec: 10,
10290                output_scale: 3,
10291                expected_output_repr: Ok(99999), // 99.999
10292            },
10293            // decrease precision, increase scale, safe
10294            DecimalCastTestConfig {
10295                input_prec: 10,
10296                input_scale: 5,
10297                input_repr: 999999, // 9.99999
10298                output_prec: 8,
10299                output_scale: 7,
10300                expected_output_repr: Ok(99999900), // 9.9999900
10301            },
10302            // decrease precision, increase scale, unsafe
10303            DecimalCastTestConfig {
10304                input_prec: 10,
10305                input_scale: 5,
10306                input_repr: 9999999, // 99.99999
10307                output_prec: 8,
10308                output_scale: 7,
10309                expected_output_repr: Err("Invalid argument error: 999999900 is too large to store in a {} of precision 8. Max is 99999999".to_string()) // max is 9.9999999
10310            },
10311            // decrease precision, decrease scale, safe, infallible
10312            DecimalCastTestConfig {
10313                input_prec: 7,
10314                input_scale: 4,
10315                input_repr: 9999999, // 999.9999
10316                output_prec: 6,
10317                output_scale: 2,
10318                expected_output_repr: Ok(100000),
10319            },
10320            // decrease precision, decrease scale, safe, fallible
10321            DecimalCastTestConfig {
10322                input_prec: 10,
10323                input_scale: 5,
10324                input_repr: 12345678, // 123.45678
10325                output_prec: 8,
10326                output_scale: 3,
10327                expected_output_repr: Ok(123457), // 123.457
10328            },
10329            // decrease precision, decrease scale, unsafe
10330            DecimalCastTestConfig {
10331                input_prec: 10,
10332                input_scale: 5,
10333                input_repr: 9999999, // 99.99999
10334                output_prec: 4,
10335                output_scale: 3,
10336                expected_output_repr: Err("Invalid argument error: 100000 is too large to store in a {} of precision 4. Max is 9999".to_string()) // max is 9.999
10337            },
10338            // decrease precision, same scale, safe
10339            DecimalCastTestConfig {
10340                input_prec: 10,
10341                input_scale: 5,
10342                input_repr: 999999, // 9.99999
10343                output_prec: 6,
10344                output_scale: 5,
10345                expected_output_repr: Ok(999999), // 9.99999
10346            },
10347            // decrease precision, same scale, unsafe
10348            DecimalCastTestConfig {
10349                input_prec: 10,
10350                input_scale: 5,
10351                input_repr: 9999999, // 99.99999
10352                output_prec: 6,
10353                output_scale: 5,
10354                expected_output_repr: Err("Invalid argument error: 9999999 is too large to store in a {} of precision 6. Max is 999999".to_string()) // max is 9.99999
10355            },
10356            // same precision, increase scale, safe
10357            DecimalCastTestConfig {
10358                input_prec: 7,
10359                input_scale: 4,
10360                input_repr: 12345, // 1.2345
10361                output_prec: 7,
10362                output_scale: 6,
10363                expected_output_repr: Ok(1234500), // 1.234500
10364            },
10365            // same precision, increase scale, unsafe
10366            DecimalCastTestConfig {
10367                input_prec: 7,
10368                input_scale: 4,
10369                input_repr: 123456, // 12.3456
10370                output_prec: 7,
10371                output_scale: 6,
10372                expected_output_repr: Err("Invalid argument error: 12345600 is too large to store in a {} of precision 7. Max is 9999999".to_string()) // max is 9.99999
10373            },
10374            // same precision, decrease scale, infallible
10375            DecimalCastTestConfig {
10376                input_prec: 7,
10377                input_scale: 5,
10378                input_repr: 1234567, // 12.34567
10379                output_prec: 7,
10380                output_scale: 4,
10381                expected_output_repr: Ok(123457), // 12.3457
10382            },
10383            // same precision, same scale, infallible
10384            DecimalCastTestConfig {
10385                input_prec: 7,
10386                input_scale: 5,
10387                input_repr: 9999999, // 99.99999
10388                output_prec: 7,
10389                output_scale: 5,
10390                expected_output_repr: Ok(9999999), // 99.99999
10391            },
10392            // precision increase, input scale & output scale = 0, infallible
10393            DecimalCastTestConfig {
10394                input_prec: 7,
10395                input_scale: 0,
10396                input_repr: 1234567, // 1234567
10397                output_prec: 8,
10398                output_scale: 0,
10399                expected_output_repr: Ok(1234567), // 1234567
10400            },
10401            // precision decrease, input scale & output scale = 0, failure
10402            DecimalCastTestConfig {
10403                input_prec: 7,
10404                input_scale: 0,
10405                input_repr: 1234567, // 1234567
10406                output_prec: 6,
10407                output_scale: 0,
10408                expected_output_repr: Err("Invalid argument error: 1234567 is too large to store in a {} of precision 6. Max is 999999".to_string())
10409            },
10410            // precision decrease, input scale & output scale = 0, success
10411            DecimalCastTestConfig {
10412                input_prec: 7,
10413                input_scale: 0,
10414                input_repr: 123456, // 123456
10415                output_prec: 6,
10416                output_scale: 0,
10417                expected_output_repr: Ok(123456), // 123456
10418            },
10419        ];
10420
10421        for t in test_cases {
10422            run_decimal_cast_test_case_between_multiple_types(t);
10423        }
10424    }
10425
10426    #[test]
10427    fn test_decimal_to_decimal_increase_scale_and_precision_unchecked() {
10428        let test_cases = [
10429            DecimalCastTestConfig {
10430                input_prec: 5,
10431                input_scale: 0,
10432                input_repr: 99999,
10433                output_prec: 10,
10434                output_scale: 5,
10435                expected_output_repr: Ok(9999900000),
10436            },
10437            DecimalCastTestConfig {
10438                input_prec: 5,
10439                input_scale: 0,
10440                input_repr: -99999,
10441                output_prec: 10,
10442                output_scale: 5,
10443                expected_output_repr: Ok(-9999900000),
10444            },
10445            DecimalCastTestConfig {
10446                input_prec: 5,
10447                input_scale: 2,
10448                input_repr: 99999,
10449                output_prec: 10,
10450                output_scale: 5,
10451                expected_output_repr: Ok(99999000),
10452            },
10453            DecimalCastTestConfig {
10454                input_prec: 5,
10455                input_scale: -2,
10456                input_repr: -99999,
10457                output_prec: 10,
10458                output_scale: 3,
10459                expected_output_repr: Ok(-9999900000),
10460            },
10461            DecimalCastTestConfig {
10462                input_prec: 5,
10463                input_scale: 3,
10464                input_repr: -12345,
10465                output_prec: 6,
10466                output_scale: 5,
10467                expected_output_repr: Err("Invalid argument error: -1234500 is too small to store in a {} of precision 6. Min is -999999".to_string())
10468            },
10469        ];
10470
10471        for t in test_cases {
10472            run_decimal_cast_test_case_between_multiple_types(t);
10473        }
10474    }
10475
10476    #[test]
10477    fn test_decimal_to_decimal_decrease_scale_and_precision_unchecked() {
10478        let test_cases = [
10479            DecimalCastTestConfig {
10480                input_prec: 5,
10481                input_scale: 0,
10482                input_repr: 99999,
10483                output_scale: -3,
10484                output_prec: 3,
10485                expected_output_repr: Ok(100),
10486            },
10487            DecimalCastTestConfig {
10488                input_prec: 5,
10489                input_scale: 0,
10490                input_repr: -99999,
10491                output_prec: 1,
10492                output_scale: -5,
10493                expected_output_repr: Ok(-1),
10494            },
10495            DecimalCastTestConfig {
10496                input_prec: 10,
10497                input_scale: 2,
10498                input_repr: 123456789,
10499                output_prec: 5,
10500                output_scale: -2,
10501                expected_output_repr: Ok(12346),
10502            },
10503            DecimalCastTestConfig {
10504                input_prec: 10,
10505                input_scale: 4,
10506                input_repr: -9876543210,
10507                output_prec: 7,
10508                output_scale: 0,
10509                expected_output_repr: Ok(-987654),
10510            },
10511            DecimalCastTestConfig {
10512                input_prec: 7,
10513                input_scale: 4,
10514                input_repr: 9999999,
10515                output_prec: 6,
10516                output_scale: 3,
10517                expected_output_repr:
10518                    Err("Invalid argument error: 1000000 is too large to store in a {} of precision 6. Max is 999999".to_string()),
10519            },
10520        ];
10521        for t in test_cases {
10522            run_decimal_cast_test_case_between_multiple_types(t);
10523        }
10524    }
10525
10526    #[test]
10527    fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
10528        let array = vec![Some(123456789)];
10529        let array = create_decimal128_array(array, 24, 2).unwrap();
10530        let input_type = DataType::Decimal128(24, 2);
10531        let output_type = DataType::Decimal128(6, 2);
10532        assert!(can_cast_types(&input_type, &output_type));
10533
10534        let options = CastOptions {
10535            safe: false,
10536            ..Default::default()
10537        };
10538        let result = cast_with_options(&array, &output_type, &options);
10539        assert_eq!(result.unwrap_err().to_string(),
10540                   "Invalid argument error: 123456789 is too large to store in a Decimal128 of precision 6. Max is 999999");
10541    }
10542
10543    #[test]
10544    fn test_decimal_to_decimal_same_scale() {
10545        let array = vec![Some(520)];
10546        let array = create_decimal128_array(array, 4, 2).unwrap();
10547        let input_type = DataType::Decimal128(4, 2);
10548        let output_type = DataType::Decimal128(3, 2);
10549        assert!(can_cast_types(&input_type, &output_type));
10550
10551        let options = CastOptions {
10552            safe: false,
10553            ..Default::default()
10554        };
10555        let result = cast_with_options(&array, &output_type, &options);
10556        assert_eq!(
10557            result.unwrap().as_primitive::<Decimal128Type>().value(0),
10558            520
10559        );
10560
10561        // Cast 0 of decimal(3, 0) type to decimal(2, 0)
10562        assert_eq!(
10563            &cast(
10564                &create_decimal128_array(vec![Some(0)], 3, 0).unwrap(),
10565                &DataType::Decimal128(2, 0)
10566            )
10567            .unwrap(),
10568            &(Arc::new(create_decimal128_array(vec![Some(0)], 2, 0).unwrap()) as ArrayRef)
10569        );
10570    }
10571
10572    #[test]
10573    fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale() {
10574        let array = vec![Some(123456789)];
10575        let array = create_decimal128_array(array, 24, 4).unwrap();
10576        let input_type = DataType::Decimal128(24, 4);
10577        let output_type = DataType::Decimal128(6, 2);
10578        assert!(can_cast_types(&input_type, &output_type));
10579
10580        let options = CastOptions {
10581            safe: false,
10582            ..Default::default()
10583        };
10584        let result = cast_with_options(&array, &output_type, &options);
10585        assert_eq!(result.unwrap_err().to_string(),
10586                   "Invalid argument error: 1234568 is too large to store in a Decimal128 of precision 6. Max is 999999");
10587    }
10588
10589    #[test]
10590    fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale() {
10591        let array = vec![Some(123456789)];
10592        let array = create_decimal128_array(array, 24, 2).unwrap();
10593        let input_type = DataType::Decimal128(24, 2);
10594        let output_type = DataType::Decimal128(6, 3);
10595        assert!(can_cast_types(&input_type, &output_type));
10596
10597        let options = CastOptions {
10598            safe: false,
10599            ..Default::default()
10600        };
10601        let result = cast_with_options(&array, &output_type, &options);
10602        assert_eq!(result.unwrap_err().to_string(),
10603                   "Invalid argument error: 1234567890 is too large to store in a Decimal128 of precision 6. Max is 999999");
10604    }
10605
10606    #[test]
10607    fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type() {
10608        let array = vec![Some(123456789)];
10609        let array = create_decimal128_array(array, 24, 2).unwrap();
10610        let input_type = DataType::Decimal128(24, 2);
10611        let output_type = DataType::Decimal256(6, 2);
10612        assert!(can_cast_types(&input_type, &output_type));
10613
10614        let options = CastOptions {
10615            safe: false,
10616            ..Default::default()
10617        };
10618        let result = cast_with_options(&array, &output_type, &options);
10619        assert_eq!(result.unwrap_err().to_string(),
10620                   "Invalid argument error: 123456789 is too large to store in a Decimal256 of precision 6. Max is 999999");
10621    }
10622
10623    #[test]
10624    fn test_first_none() {
10625        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
10626            None,
10627            Some(vec![Some(1), Some(2)]),
10628        ])) as ArrayRef;
10629        let data_type =
10630            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
10631        let opt = CastOptions::default();
10632        let r = cast_with_options(&array, &data_type, &opt).unwrap();
10633
10634        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
10635            vec![None, Some(vec![Some(1), Some(2)])],
10636            2,
10637        )) as ArrayRef;
10638        assert_eq!(*fixed_array, *r);
10639    }
10640
10641    #[test]
10642    fn test_first_last_none() {
10643        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
10644            None,
10645            Some(vec![Some(1), Some(2)]),
10646            None,
10647        ])) as ArrayRef;
10648        let data_type =
10649            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
10650        let opt = CastOptions::default();
10651        let r = cast_with_options(&array, &data_type, &opt).unwrap();
10652
10653        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
10654            vec![None, Some(vec![Some(1), Some(2)]), None],
10655            2,
10656        )) as ArrayRef;
10657        assert_eq!(*fixed_array, *r);
10658    }
10659}