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 | BinaryView) => 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            BinaryView => cast_fixed_size_binary_to_binary_view(array, *size),
1196            _ => Err(ArrowError::CastError(format!(
1197                "Casting from {from_type:?} to {to_type:?} not supported",
1198            ))),
1199        },
1200        (BinaryView, Binary) => cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array),
1201        (BinaryView, LargeBinary) => {
1202            cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)
1203        }
1204        (BinaryView, Utf8) => {
1205            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array)?;
1206            cast_binary_to_string::<i32>(&binary_arr, cast_options)
1207        }
1208        (BinaryView, LargeUtf8) => {
1209            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)?;
1210            cast_binary_to_string::<i64>(&binary_arr, cast_options)
1211        }
1212        (BinaryView, Utf8View) => {
1213            Ok(Arc::new(array.as_binary_view().clone().to_string_view()?) as ArrayRef)
1214        }
1215        (BinaryView, _) => Err(ArrowError::CastError(format!(
1216            "Casting from {from_type:?} to {to_type:?} not supported",
1217        ))),
1218        (from_type, Utf8View) if from_type.is_primitive() => {
1219            value_to_string_view(array, cast_options)
1220        }
1221        (from_type, LargeUtf8) if from_type.is_primitive() => {
1222            value_to_string::<i64>(array, cast_options)
1223        }
1224        (from_type, Utf8) if from_type.is_primitive() => {
1225            value_to_string::<i32>(array, cast_options)
1226        }
1227        (from_type, Binary) if from_type.is_integer() => match from_type {
1228            UInt8 => cast_numeric_to_binary::<UInt8Type, i32>(array),
1229            UInt16 => cast_numeric_to_binary::<UInt16Type, i32>(array),
1230            UInt32 => cast_numeric_to_binary::<UInt32Type, i32>(array),
1231            UInt64 => cast_numeric_to_binary::<UInt64Type, i32>(array),
1232            Int8 => cast_numeric_to_binary::<Int8Type, i32>(array),
1233            Int16 => cast_numeric_to_binary::<Int16Type, i32>(array),
1234            Int32 => cast_numeric_to_binary::<Int32Type, i32>(array),
1235            Int64 => cast_numeric_to_binary::<Int64Type, i32>(array),
1236            _ => unreachable!(),
1237        },
1238        (from_type, LargeBinary) if from_type.is_integer() => match from_type {
1239            UInt8 => cast_numeric_to_binary::<UInt8Type, i64>(array),
1240            UInt16 => cast_numeric_to_binary::<UInt16Type, i64>(array),
1241            UInt32 => cast_numeric_to_binary::<UInt32Type, i64>(array),
1242            UInt64 => cast_numeric_to_binary::<UInt64Type, i64>(array),
1243            Int8 => cast_numeric_to_binary::<Int8Type, i64>(array),
1244            Int16 => cast_numeric_to_binary::<Int16Type, i64>(array),
1245            Int32 => cast_numeric_to_binary::<Int32Type, i64>(array),
1246            Int64 => cast_numeric_to_binary::<Int64Type, i64>(array),
1247            _ => unreachable!(),
1248        },
1249        // start numeric casts
1250        (UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array, cast_options),
1251        (UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array, cast_options),
1252        (UInt8, UInt64) => cast_numeric_arrays::<UInt8Type, UInt64Type>(array, cast_options),
1253        (UInt8, Int8) => cast_numeric_arrays::<UInt8Type, Int8Type>(array, cast_options),
1254        (UInt8, Int16) => cast_numeric_arrays::<UInt8Type, Int16Type>(array, cast_options),
1255        (UInt8, Int32) => cast_numeric_arrays::<UInt8Type, Int32Type>(array, cast_options),
1256        (UInt8, Int64) => cast_numeric_arrays::<UInt8Type, Int64Type>(array, cast_options),
1257        (UInt8, Float16) => cast_numeric_arrays::<UInt8Type, Float16Type>(array, cast_options),
1258        (UInt8, Float32) => cast_numeric_arrays::<UInt8Type, Float32Type>(array, cast_options),
1259        (UInt8, Float64) => cast_numeric_arrays::<UInt8Type, Float64Type>(array, cast_options),
1260
1261        (UInt16, UInt8) => cast_numeric_arrays::<UInt16Type, UInt8Type>(array, cast_options),
1262        (UInt16, UInt32) => cast_numeric_arrays::<UInt16Type, UInt32Type>(array, cast_options),
1263        (UInt16, UInt64) => cast_numeric_arrays::<UInt16Type, UInt64Type>(array, cast_options),
1264        (UInt16, Int8) => cast_numeric_arrays::<UInt16Type, Int8Type>(array, cast_options),
1265        (UInt16, Int16) => cast_numeric_arrays::<UInt16Type, Int16Type>(array, cast_options),
1266        (UInt16, Int32) => cast_numeric_arrays::<UInt16Type, Int32Type>(array, cast_options),
1267        (UInt16, Int64) => cast_numeric_arrays::<UInt16Type, Int64Type>(array, cast_options),
1268        (UInt16, Float16) => cast_numeric_arrays::<UInt16Type, Float16Type>(array, cast_options),
1269        (UInt16, Float32) => cast_numeric_arrays::<UInt16Type, Float32Type>(array, cast_options),
1270        (UInt16, Float64) => cast_numeric_arrays::<UInt16Type, Float64Type>(array, cast_options),
1271
1272        (UInt32, UInt8) => cast_numeric_arrays::<UInt32Type, UInt8Type>(array, cast_options),
1273        (UInt32, UInt16) => cast_numeric_arrays::<UInt32Type, UInt16Type>(array, cast_options),
1274        (UInt32, UInt64) => cast_numeric_arrays::<UInt32Type, UInt64Type>(array, cast_options),
1275        (UInt32, Int8) => cast_numeric_arrays::<UInt32Type, Int8Type>(array, cast_options),
1276        (UInt32, Int16) => cast_numeric_arrays::<UInt32Type, Int16Type>(array, cast_options),
1277        (UInt32, Int32) => cast_numeric_arrays::<UInt32Type, Int32Type>(array, cast_options),
1278        (UInt32, Int64) => cast_numeric_arrays::<UInt32Type, Int64Type>(array, cast_options),
1279        (UInt32, Float16) => cast_numeric_arrays::<UInt32Type, Float16Type>(array, cast_options),
1280        (UInt32, Float32) => cast_numeric_arrays::<UInt32Type, Float32Type>(array, cast_options),
1281        (UInt32, Float64) => cast_numeric_arrays::<UInt32Type, Float64Type>(array, cast_options),
1282
1283        (UInt64, UInt8) => cast_numeric_arrays::<UInt64Type, UInt8Type>(array, cast_options),
1284        (UInt64, UInt16) => cast_numeric_arrays::<UInt64Type, UInt16Type>(array, cast_options),
1285        (UInt64, UInt32) => cast_numeric_arrays::<UInt64Type, UInt32Type>(array, cast_options),
1286        (UInt64, Int8) => cast_numeric_arrays::<UInt64Type, Int8Type>(array, cast_options),
1287        (UInt64, Int16) => cast_numeric_arrays::<UInt64Type, Int16Type>(array, cast_options),
1288        (UInt64, Int32) => cast_numeric_arrays::<UInt64Type, Int32Type>(array, cast_options),
1289        (UInt64, Int64) => cast_numeric_arrays::<UInt64Type, Int64Type>(array, cast_options),
1290        (UInt64, Float16) => cast_numeric_arrays::<UInt64Type, Float16Type>(array, cast_options),
1291        (UInt64, Float32) => cast_numeric_arrays::<UInt64Type, Float32Type>(array, cast_options),
1292        (UInt64, Float64) => cast_numeric_arrays::<UInt64Type, Float64Type>(array, cast_options),
1293
1294        (Int8, UInt8) => cast_numeric_arrays::<Int8Type, UInt8Type>(array, cast_options),
1295        (Int8, UInt16) => cast_numeric_arrays::<Int8Type, UInt16Type>(array, cast_options),
1296        (Int8, UInt32) => cast_numeric_arrays::<Int8Type, UInt32Type>(array, cast_options),
1297        (Int8, UInt64) => cast_numeric_arrays::<Int8Type, UInt64Type>(array, cast_options),
1298        (Int8, Int16) => cast_numeric_arrays::<Int8Type, Int16Type>(array, cast_options),
1299        (Int8, Int32) => cast_numeric_arrays::<Int8Type, Int32Type>(array, cast_options),
1300        (Int8, Int64) => cast_numeric_arrays::<Int8Type, Int64Type>(array, cast_options),
1301        (Int8, Float16) => cast_numeric_arrays::<Int8Type, Float16Type>(array, cast_options),
1302        (Int8, Float32) => cast_numeric_arrays::<Int8Type, Float32Type>(array, cast_options),
1303        (Int8, Float64) => cast_numeric_arrays::<Int8Type, Float64Type>(array, cast_options),
1304
1305        (Int16, UInt8) => cast_numeric_arrays::<Int16Type, UInt8Type>(array, cast_options),
1306        (Int16, UInt16) => cast_numeric_arrays::<Int16Type, UInt16Type>(array, cast_options),
1307        (Int16, UInt32) => cast_numeric_arrays::<Int16Type, UInt32Type>(array, cast_options),
1308        (Int16, UInt64) => cast_numeric_arrays::<Int16Type, UInt64Type>(array, cast_options),
1309        (Int16, Int8) => cast_numeric_arrays::<Int16Type, Int8Type>(array, cast_options),
1310        (Int16, Int32) => cast_numeric_arrays::<Int16Type, Int32Type>(array, cast_options),
1311        (Int16, Int64) => cast_numeric_arrays::<Int16Type, Int64Type>(array, cast_options),
1312        (Int16, Float16) => cast_numeric_arrays::<Int16Type, Float16Type>(array, cast_options),
1313        (Int16, Float32) => cast_numeric_arrays::<Int16Type, Float32Type>(array, cast_options),
1314        (Int16, Float64) => cast_numeric_arrays::<Int16Type, Float64Type>(array, cast_options),
1315
1316        (Int32, UInt8) => cast_numeric_arrays::<Int32Type, UInt8Type>(array, cast_options),
1317        (Int32, UInt16) => cast_numeric_arrays::<Int32Type, UInt16Type>(array, cast_options),
1318        (Int32, UInt32) => cast_numeric_arrays::<Int32Type, UInt32Type>(array, cast_options),
1319        (Int32, UInt64) => cast_numeric_arrays::<Int32Type, UInt64Type>(array, cast_options),
1320        (Int32, Int8) => cast_numeric_arrays::<Int32Type, Int8Type>(array, cast_options),
1321        (Int32, Int16) => cast_numeric_arrays::<Int32Type, Int16Type>(array, cast_options),
1322        (Int32, Int64) => cast_numeric_arrays::<Int32Type, Int64Type>(array, cast_options),
1323        (Int32, Float16) => cast_numeric_arrays::<Int32Type, Float16Type>(array, cast_options),
1324        (Int32, Float32) => cast_numeric_arrays::<Int32Type, Float32Type>(array, cast_options),
1325        (Int32, Float64) => cast_numeric_arrays::<Int32Type, Float64Type>(array, cast_options),
1326
1327        (Int64, UInt8) => cast_numeric_arrays::<Int64Type, UInt8Type>(array, cast_options),
1328        (Int64, UInt16) => cast_numeric_arrays::<Int64Type, UInt16Type>(array, cast_options),
1329        (Int64, UInt32) => cast_numeric_arrays::<Int64Type, UInt32Type>(array, cast_options),
1330        (Int64, UInt64) => cast_numeric_arrays::<Int64Type, UInt64Type>(array, cast_options),
1331        (Int64, Int8) => cast_numeric_arrays::<Int64Type, Int8Type>(array, cast_options),
1332        (Int64, Int16) => cast_numeric_arrays::<Int64Type, Int16Type>(array, cast_options),
1333        (Int64, Int32) => cast_numeric_arrays::<Int64Type, Int32Type>(array, cast_options),
1334        (Int64, Float16) => cast_numeric_arrays::<Int64Type, Float16Type>(array, cast_options),
1335        (Int64, Float32) => cast_numeric_arrays::<Int64Type, Float32Type>(array, cast_options),
1336        (Int64, Float64) => cast_numeric_arrays::<Int64Type, Float64Type>(array, cast_options),
1337
1338        (Float16, UInt8) => cast_numeric_arrays::<Float16Type, UInt8Type>(array, cast_options),
1339        (Float16, UInt16) => cast_numeric_arrays::<Float16Type, UInt16Type>(array, cast_options),
1340        (Float16, UInt32) => cast_numeric_arrays::<Float16Type, UInt32Type>(array, cast_options),
1341        (Float16, UInt64) => cast_numeric_arrays::<Float16Type, UInt64Type>(array, cast_options),
1342        (Float16, Int8) => cast_numeric_arrays::<Float16Type, Int8Type>(array, cast_options),
1343        (Float16, Int16) => cast_numeric_arrays::<Float16Type, Int16Type>(array, cast_options),
1344        (Float16, Int32) => cast_numeric_arrays::<Float16Type, Int32Type>(array, cast_options),
1345        (Float16, Int64) => cast_numeric_arrays::<Float16Type, Int64Type>(array, cast_options),
1346        (Float16, Float32) => cast_numeric_arrays::<Float16Type, Float32Type>(array, cast_options),
1347        (Float16, Float64) => cast_numeric_arrays::<Float16Type, Float64Type>(array, cast_options),
1348
1349        (Float32, UInt8) => cast_numeric_arrays::<Float32Type, UInt8Type>(array, cast_options),
1350        (Float32, UInt16) => cast_numeric_arrays::<Float32Type, UInt16Type>(array, cast_options),
1351        (Float32, UInt32) => cast_numeric_arrays::<Float32Type, UInt32Type>(array, cast_options),
1352        (Float32, UInt64) => cast_numeric_arrays::<Float32Type, UInt64Type>(array, cast_options),
1353        (Float32, Int8) => cast_numeric_arrays::<Float32Type, Int8Type>(array, cast_options),
1354        (Float32, Int16) => cast_numeric_arrays::<Float32Type, Int16Type>(array, cast_options),
1355        (Float32, Int32) => cast_numeric_arrays::<Float32Type, Int32Type>(array, cast_options),
1356        (Float32, Int64) => cast_numeric_arrays::<Float32Type, Int64Type>(array, cast_options),
1357        (Float32, Float16) => cast_numeric_arrays::<Float32Type, Float16Type>(array, cast_options),
1358        (Float32, Float64) => cast_numeric_arrays::<Float32Type, Float64Type>(array, cast_options),
1359
1360        (Float64, UInt8) => cast_numeric_arrays::<Float64Type, UInt8Type>(array, cast_options),
1361        (Float64, UInt16) => cast_numeric_arrays::<Float64Type, UInt16Type>(array, cast_options),
1362        (Float64, UInt32) => cast_numeric_arrays::<Float64Type, UInt32Type>(array, cast_options),
1363        (Float64, UInt64) => cast_numeric_arrays::<Float64Type, UInt64Type>(array, cast_options),
1364        (Float64, Int8) => cast_numeric_arrays::<Float64Type, Int8Type>(array, cast_options),
1365        (Float64, Int16) => cast_numeric_arrays::<Float64Type, Int16Type>(array, cast_options),
1366        (Float64, Int32) => cast_numeric_arrays::<Float64Type, Int32Type>(array, cast_options),
1367        (Float64, Int64) => cast_numeric_arrays::<Float64Type, Int64Type>(array, cast_options),
1368        (Float64, Float16) => cast_numeric_arrays::<Float64Type, Float16Type>(array, cast_options),
1369        (Float64, Float32) => cast_numeric_arrays::<Float64Type, Float32Type>(array, cast_options),
1370        // end numeric casts
1371
1372        // temporal casts
1373        (Int32, Date32) => cast_reinterpret_arrays::<Int32Type, Date32Type>(array),
1374        (Int32, Date64) => cast_with_options(
1375            &cast_with_options(array, &Date32, cast_options)?,
1376            &Date64,
1377            cast_options,
1378        ),
1379        (Int32, Time32(TimeUnit::Second)) => {
1380            cast_reinterpret_arrays::<Int32Type, Time32SecondType>(array)
1381        }
1382        (Int32, Time32(TimeUnit::Millisecond)) => {
1383            cast_reinterpret_arrays::<Int32Type, Time32MillisecondType>(array)
1384        }
1385        // No support for microsecond/nanosecond with i32
1386        (Date32, Int32) => cast_reinterpret_arrays::<Date32Type, Int32Type>(array),
1387        (Date32, Int64) => cast_with_options(
1388            &cast_with_options(array, &Int32, cast_options)?,
1389            &Int64,
1390            cast_options,
1391        ),
1392        (Time32(TimeUnit::Second), Int32) => {
1393            cast_reinterpret_arrays::<Time32SecondType, Int32Type>(array)
1394        }
1395        (Time32(TimeUnit::Millisecond), Int32) => {
1396            cast_reinterpret_arrays::<Time32MillisecondType, Int32Type>(array)
1397        }
1398        (Int64, Date64) => cast_reinterpret_arrays::<Int64Type, Date64Type>(array),
1399        (Int64, Date32) => cast_with_options(
1400            &cast_with_options(array, &Int32, cast_options)?,
1401            &Date32,
1402            cast_options,
1403        ),
1404        // No support for second/milliseconds with i64
1405        (Int64, Time64(TimeUnit::Microsecond)) => {
1406            cast_reinterpret_arrays::<Int64Type, Time64MicrosecondType>(array)
1407        }
1408        (Int64, Time64(TimeUnit::Nanosecond)) => {
1409            cast_reinterpret_arrays::<Int64Type, Time64NanosecondType>(array)
1410        }
1411
1412        (Date64, Int64) => cast_reinterpret_arrays::<Date64Type, Int64Type>(array),
1413        (Date64, Int32) => cast_with_options(
1414            &cast_with_options(array, &Int64, cast_options)?,
1415            &Int32,
1416            cast_options,
1417        ),
1418        (Time64(TimeUnit::Microsecond), Int64) => {
1419            cast_reinterpret_arrays::<Time64MicrosecondType, Int64Type>(array)
1420        }
1421        (Time64(TimeUnit::Nanosecond), Int64) => {
1422            cast_reinterpret_arrays::<Time64NanosecondType, Int64Type>(array)
1423        }
1424        (Date32, Date64) => Ok(Arc::new(
1425            array
1426                .as_primitive::<Date32Type>()
1427                .unary::<_, Date64Type>(|x| x as i64 * MILLISECONDS_IN_DAY),
1428        )),
1429        (Date64, Date32) => Ok(Arc::new(
1430            array
1431                .as_primitive::<Date64Type>()
1432                .unary::<_, Date32Type>(|x| (x / MILLISECONDS_IN_DAY) as i32),
1433        )),
1434
1435        (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1436            array
1437                .as_primitive::<Time32SecondType>()
1438                .unary::<_, Time32MillisecondType>(|x| x * MILLISECONDS as i32),
1439        )),
1440        (Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1441            array
1442                .as_primitive::<Time32SecondType>()
1443                .unary::<_, Time64MicrosecondType>(|x| x as i64 * MICROSECONDS),
1444        )),
1445        (Time32(TimeUnit::Second), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1446            array
1447                .as_primitive::<Time32SecondType>()
1448                .unary::<_, Time64NanosecondType>(|x| x as i64 * NANOSECONDS),
1449        )),
1450
1451        (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1452            array
1453                .as_primitive::<Time32MillisecondType>()
1454                .unary::<_, Time32SecondType>(|x| x / MILLISECONDS as i32),
1455        )),
1456        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1457            array
1458                .as_primitive::<Time32MillisecondType>()
1459                .unary::<_, Time64MicrosecondType>(|x| x as i64 * (MICROSECONDS / MILLISECONDS)),
1460        )),
1461        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1462            array
1463                .as_primitive::<Time32MillisecondType>()
1464                .unary::<_, Time64NanosecondType>(|x| x as i64 * (MICROSECONDS / NANOSECONDS)),
1465        )),
1466
1467        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1468            array
1469                .as_primitive::<Time64MicrosecondType>()
1470                .unary::<_, Time32SecondType>(|x| (x / MICROSECONDS) as i32),
1471        )),
1472        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1473            array
1474                .as_primitive::<Time64MicrosecondType>()
1475                .unary::<_, Time32MillisecondType>(|x| (x / (MICROSECONDS / MILLISECONDS)) as i32),
1476        )),
1477        (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1478            array
1479                .as_primitive::<Time64MicrosecondType>()
1480                .unary::<_, Time64NanosecondType>(|x| x * (NANOSECONDS / MICROSECONDS)),
1481        )),
1482
1483        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1484            array
1485                .as_primitive::<Time64NanosecondType>()
1486                .unary::<_, Time32SecondType>(|x| (x / NANOSECONDS) as i32),
1487        )),
1488        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1489            array
1490                .as_primitive::<Time64NanosecondType>()
1491                .unary::<_, Time32MillisecondType>(|x| (x / (NANOSECONDS / MILLISECONDS)) as i32),
1492        )),
1493        (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1494            array
1495                .as_primitive::<Time64NanosecondType>()
1496                .unary::<_, Time64MicrosecondType>(|x| x / (NANOSECONDS / MICROSECONDS)),
1497        )),
1498
1499        // Timestamp to integer/floating/decimals
1500        (Timestamp(TimeUnit::Second, _), _) if to_type.is_numeric() => {
1501            let array = cast_reinterpret_arrays::<TimestampSecondType, Int64Type>(array)?;
1502            cast_with_options(&array, to_type, cast_options)
1503        }
1504        (Timestamp(TimeUnit::Millisecond, _), _) if to_type.is_numeric() => {
1505            let array = cast_reinterpret_arrays::<TimestampMillisecondType, Int64Type>(array)?;
1506            cast_with_options(&array, to_type, cast_options)
1507        }
1508        (Timestamp(TimeUnit::Microsecond, _), _) if to_type.is_numeric() => {
1509            let array = cast_reinterpret_arrays::<TimestampMicrosecondType, Int64Type>(array)?;
1510            cast_with_options(&array, to_type, cast_options)
1511        }
1512        (Timestamp(TimeUnit::Nanosecond, _), _) if to_type.is_numeric() => {
1513            let array = cast_reinterpret_arrays::<TimestampNanosecondType, Int64Type>(array)?;
1514            cast_with_options(&array, to_type, cast_options)
1515        }
1516
1517        (_, Timestamp(unit, tz)) if from_type.is_numeric() => {
1518            let array = cast_with_options(array, &Int64, cast_options)?;
1519            Ok(make_timestamp_array(
1520                array.as_primitive(),
1521                *unit,
1522                tz.clone(),
1523            ))
1524        }
1525
1526        (Timestamp(from_unit, from_tz), Timestamp(to_unit, to_tz)) => {
1527            let array = cast_with_options(array, &Int64, cast_options)?;
1528            let time_array = array.as_primitive::<Int64Type>();
1529            let from_size = time_unit_multiple(from_unit);
1530            let to_size = time_unit_multiple(to_unit);
1531            // we either divide or multiply, depending on size of each unit
1532            // units are never the same when the types are the same
1533            let converted = match from_size.cmp(&to_size) {
1534                Ordering::Greater => {
1535                    let divisor = from_size / to_size;
1536                    time_array.unary::<_, Int64Type>(|o| o / divisor)
1537                }
1538                Ordering::Equal => time_array.clone(),
1539                Ordering::Less => {
1540                    let mul = to_size / from_size;
1541                    if cast_options.safe {
1542                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1543                    } else {
1544                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1545                    }
1546                }
1547            };
1548            // Normalize timezone
1549            let adjusted = match (from_tz, to_tz) {
1550                // Only this case needs to be adjusted because we're casting from
1551                // unknown time offset to some time offset, we want the time to be
1552                // unchanged.
1553                //
1554                // i.e. Timestamp('2001-01-01T00:00', None) -> Timestamp('2001-01-01T00:00', '+0700')
1555                (None, Some(to_tz)) => {
1556                    let to_tz: Tz = to_tz.parse()?;
1557                    match to_unit {
1558                        TimeUnit::Second => adjust_timestamp_to_timezone::<TimestampSecondType>(
1559                            converted,
1560                            &to_tz,
1561                            cast_options,
1562                        )?,
1563                        TimeUnit::Millisecond => adjust_timestamp_to_timezone::<
1564                            TimestampMillisecondType,
1565                        >(
1566                            converted, &to_tz, cast_options
1567                        )?,
1568                        TimeUnit::Microsecond => adjust_timestamp_to_timezone::<
1569                            TimestampMicrosecondType,
1570                        >(
1571                            converted, &to_tz, cast_options
1572                        )?,
1573                        TimeUnit::Nanosecond => adjust_timestamp_to_timezone::<
1574                            TimestampNanosecondType,
1575                        >(
1576                            converted, &to_tz, cast_options
1577                        )?,
1578                    }
1579                }
1580                _ => converted,
1581            };
1582            Ok(make_timestamp_array(&adjusted, *to_unit, to_tz.clone()))
1583        }
1584        (Timestamp(TimeUnit::Microsecond, _), Date32) => {
1585            timestamp_to_date32(array.as_primitive::<TimestampMicrosecondType>())
1586        }
1587        (Timestamp(TimeUnit::Millisecond, _), Date32) => {
1588            timestamp_to_date32(array.as_primitive::<TimestampMillisecondType>())
1589        }
1590        (Timestamp(TimeUnit::Second, _), Date32) => {
1591            timestamp_to_date32(array.as_primitive::<TimestampSecondType>())
1592        }
1593        (Timestamp(TimeUnit::Nanosecond, _), Date32) => {
1594            timestamp_to_date32(array.as_primitive::<TimestampNanosecondType>())
1595        }
1596        (Timestamp(TimeUnit::Second, _), Date64) => Ok(Arc::new(match cast_options.safe {
1597            true => {
1598                // change error to None
1599                array
1600                    .as_primitive::<TimestampSecondType>()
1601                    .unary_opt::<_, Date64Type>(|x| x.checked_mul(MILLISECONDS))
1602            }
1603            false => array
1604                .as_primitive::<TimestampSecondType>()
1605                .try_unary::<_, Date64Type, _>(|x| x.mul_checked(MILLISECONDS))?,
1606        })),
1607        (Timestamp(TimeUnit::Millisecond, _), Date64) => {
1608            cast_reinterpret_arrays::<TimestampMillisecondType, Date64Type>(array)
1609        }
1610        (Timestamp(TimeUnit::Microsecond, _), Date64) => Ok(Arc::new(
1611            array
1612                .as_primitive::<TimestampMicrosecondType>()
1613                .unary::<_, Date64Type>(|x| x / (MICROSECONDS / MILLISECONDS)),
1614        )),
1615        (Timestamp(TimeUnit::Nanosecond, _), Date64) => Ok(Arc::new(
1616            array
1617                .as_primitive::<TimestampNanosecondType>()
1618                .unary::<_, Date64Type>(|x| x / (NANOSECONDS / MILLISECONDS)),
1619        )),
1620        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Microsecond)) => {
1621            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1622            Ok(Arc::new(
1623                array
1624                    .as_primitive::<TimestampSecondType>()
1625                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1626                        Ok(time_to_time64us(as_time_res_with_timezone::<
1627                            TimestampSecondType,
1628                        >(x, tz)?))
1629                    })?,
1630            ))
1631        }
1632        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Nanosecond)) => {
1633            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1634            Ok(Arc::new(
1635                array
1636                    .as_primitive::<TimestampSecondType>()
1637                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1638                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1639                            TimestampSecondType,
1640                        >(x, tz)?))
1641                    })?,
1642            ))
1643        }
1644        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Microsecond)) => {
1645            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1646            Ok(Arc::new(
1647                array
1648                    .as_primitive::<TimestampMillisecondType>()
1649                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1650                        Ok(time_to_time64us(as_time_res_with_timezone::<
1651                            TimestampMillisecondType,
1652                        >(x, tz)?))
1653                    })?,
1654            ))
1655        }
1656        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Nanosecond)) => {
1657            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1658            Ok(Arc::new(
1659                array
1660                    .as_primitive::<TimestampMillisecondType>()
1661                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1662                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1663                            TimestampMillisecondType,
1664                        >(x, tz)?))
1665                    })?,
1666            ))
1667        }
1668        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Microsecond)) => {
1669            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1670            Ok(Arc::new(
1671                array
1672                    .as_primitive::<TimestampMicrosecondType>()
1673                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1674                        Ok(time_to_time64us(as_time_res_with_timezone::<
1675                            TimestampMicrosecondType,
1676                        >(x, tz)?))
1677                    })?,
1678            ))
1679        }
1680        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Nanosecond)) => {
1681            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1682            Ok(Arc::new(
1683                array
1684                    .as_primitive::<TimestampMicrosecondType>()
1685                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1686                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1687                            TimestampMicrosecondType,
1688                        >(x, tz)?))
1689                    })?,
1690            ))
1691        }
1692        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Microsecond)) => {
1693            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1694            Ok(Arc::new(
1695                array
1696                    .as_primitive::<TimestampNanosecondType>()
1697                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1698                        Ok(time_to_time64us(as_time_res_with_timezone::<
1699                            TimestampNanosecondType,
1700                        >(x, tz)?))
1701                    })?,
1702            ))
1703        }
1704        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Nanosecond)) => {
1705            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1706            Ok(Arc::new(
1707                array
1708                    .as_primitive::<TimestampNanosecondType>()
1709                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1710                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1711                            TimestampNanosecondType,
1712                        >(x, tz)?))
1713                    })?,
1714            ))
1715        }
1716        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Second)) => {
1717            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1718            Ok(Arc::new(
1719                array
1720                    .as_primitive::<TimestampSecondType>()
1721                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1722                        Ok(time_to_time32s(as_time_res_with_timezone::<
1723                            TimestampSecondType,
1724                        >(x, tz)?))
1725                    })?,
1726            ))
1727        }
1728        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Millisecond)) => {
1729            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1730            Ok(Arc::new(
1731                array
1732                    .as_primitive::<TimestampSecondType>()
1733                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1734                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1735                            TimestampSecondType,
1736                        >(x, tz)?))
1737                    })?,
1738            ))
1739        }
1740        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Second)) => {
1741            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1742            Ok(Arc::new(
1743                array
1744                    .as_primitive::<TimestampMillisecondType>()
1745                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1746                        Ok(time_to_time32s(as_time_res_with_timezone::<
1747                            TimestampMillisecondType,
1748                        >(x, tz)?))
1749                    })?,
1750            ))
1751        }
1752        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Millisecond)) => {
1753            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1754            Ok(Arc::new(
1755                array
1756                    .as_primitive::<TimestampMillisecondType>()
1757                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1758                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1759                            TimestampMillisecondType,
1760                        >(x, tz)?))
1761                    })?,
1762            ))
1763        }
1764        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Second)) => {
1765            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1766            Ok(Arc::new(
1767                array
1768                    .as_primitive::<TimestampMicrosecondType>()
1769                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1770                        Ok(time_to_time32s(as_time_res_with_timezone::<
1771                            TimestampMicrosecondType,
1772                        >(x, tz)?))
1773                    })?,
1774            ))
1775        }
1776        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Millisecond)) => {
1777            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1778            Ok(Arc::new(
1779                array
1780                    .as_primitive::<TimestampMicrosecondType>()
1781                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1782                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1783                            TimestampMicrosecondType,
1784                        >(x, tz)?))
1785                    })?,
1786            ))
1787        }
1788        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Second)) => {
1789            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1790            Ok(Arc::new(
1791                array
1792                    .as_primitive::<TimestampNanosecondType>()
1793                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1794                        Ok(time_to_time32s(as_time_res_with_timezone::<
1795                            TimestampNanosecondType,
1796                        >(x, tz)?))
1797                    })?,
1798            ))
1799        }
1800        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Millisecond)) => {
1801            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1802            Ok(Arc::new(
1803                array
1804                    .as_primitive::<TimestampNanosecondType>()
1805                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1806                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1807                            TimestampNanosecondType,
1808                        >(x, tz)?))
1809                    })?,
1810            ))
1811        }
1812        (Date64, Timestamp(TimeUnit::Second, _)) => {
1813            let array = array
1814                .as_primitive::<Date64Type>()
1815                .unary::<_, TimestampSecondType>(|x| x / MILLISECONDS);
1816
1817            cast_with_options(&array, to_type, cast_options)
1818        }
1819        (Date64, Timestamp(TimeUnit::Millisecond, _)) => {
1820            let array = array
1821                .as_primitive::<Date64Type>()
1822                .reinterpret_cast::<TimestampMillisecondType>();
1823
1824            cast_with_options(&array, to_type, cast_options)
1825        }
1826
1827        (Date64, Timestamp(TimeUnit::Microsecond, _)) => {
1828            let array = array
1829                .as_primitive::<Date64Type>()
1830                .unary::<_, TimestampMicrosecondType>(|x| x * (MICROSECONDS / MILLISECONDS));
1831
1832            cast_with_options(&array, to_type, cast_options)
1833        }
1834        (Date64, Timestamp(TimeUnit::Nanosecond, _)) => {
1835            let array = array
1836                .as_primitive::<Date64Type>()
1837                .unary::<_, TimestampNanosecondType>(|x| x * (NANOSECONDS / MILLISECONDS));
1838
1839            cast_with_options(&array, to_type, cast_options)
1840        }
1841        (Date32, Timestamp(TimeUnit::Second, _)) => {
1842            let array = array
1843                .as_primitive::<Date32Type>()
1844                .unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY);
1845
1846            cast_with_options(&array, to_type, cast_options)
1847        }
1848        (Date32, Timestamp(TimeUnit::Millisecond, _)) => {
1849            let array = array
1850                .as_primitive::<Date32Type>()
1851                .unary::<_, TimestampMillisecondType>(|x| (x as i64) * MILLISECONDS_IN_DAY);
1852
1853            cast_with_options(&array, to_type, cast_options)
1854        }
1855        (Date32, Timestamp(TimeUnit::Microsecond, _)) => {
1856            let array = array
1857                .as_primitive::<Date32Type>()
1858                .unary::<_, TimestampMicrosecondType>(|x| (x as i64) * MICROSECONDS_IN_DAY);
1859
1860            cast_with_options(&array, to_type, cast_options)
1861        }
1862        (Date32, Timestamp(TimeUnit::Nanosecond, _)) => {
1863            let array = array
1864                .as_primitive::<Date32Type>()
1865                .unary::<_, TimestampNanosecondType>(|x| (x as i64) * NANOSECONDS_IN_DAY);
1866
1867            cast_with_options(&array, to_type, cast_options)
1868        }
1869
1870        (_, Duration(unit)) if from_type.is_numeric() => {
1871            let array = cast_with_options(array, &Int64, cast_options)?;
1872            Ok(make_duration_array(array.as_primitive(), *unit))
1873        }
1874        (Duration(TimeUnit::Second), _) if to_type.is_numeric() => {
1875            let array = cast_reinterpret_arrays::<DurationSecondType, Int64Type>(array)?;
1876            cast_with_options(&array, to_type, cast_options)
1877        }
1878        (Duration(TimeUnit::Millisecond), _) if to_type.is_numeric() => {
1879            let array = cast_reinterpret_arrays::<DurationMillisecondType, Int64Type>(array)?;
1880            cast_with_options(&array, to_type, cast_options)
1881        }
1882        (Duration(TimeUnit::Microsecond), _) if to_type.is_numeric() => {
1883            let array = cast_reinterpret_arrays::<DurationMicrosecondType, Int64Type>(array)?;
1884            cast_with_options(&array, to_type, cast_options)
1885        }
1886        (Duration(TimeUnit::Nanosecond), _) if to_type.is_numeric() => {
1887            let array = cast_reinterpret_arrays::<DurationNanosecondType, Int64Type>(array)?;
1888            cast_with_options(&array, to_type, cast_options)
1889        }
1890
1891        (Duration(from_unit), Duration(to_unit)) => {
1892            let array = cast_with_options(array, &Int64, cast_options)?;
1893            let time_array = array.as_primitive::<Int64Type>();
1894            let from_size = time_unit_multiple(from_unit);
1895            let to_size = time_unit_multiple(to_unit);
1896            // we either divide or multiply, depending on size of each unit
1897            // units are never the same when the types are the same
1898            let converted = match from_size.cmp(&to_size) {
1899                Ordering::Greater => {
1900                    let divisor = from_size / to_size;
1901                    time_array.unary::<_, Int64Type>(|o| o / divisor)
1902                }
1903                Ordering::Equal => time_array.clone(),
1904                Ordering::Less => {
1905                    let mul = to_size / from_size;
1906                    if cast_options.safe {
1907                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1908                    } else {
1909                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1910                    }
1911                }
1912            };
1913            Ok(make_duration_array(&converted, *to_unit))
1914        }
1915
1916        (Duration(TimeUnit::Second), Interval(IntervalUnit::MonthDayNano)) => {
1917            cast_duration_to_interval::<DurationSecondType>(array, cast_options)
1918        }
1919        (Duration(TimeUnit::Millisecond), Interval(IntervalUnit::MonthDayNano)) => {
1920            cast_duration_to_interval::<DurationMillisecondType>(array, cast_options)
1921        }
1922        (Duration(TimeUnit::Microsecond), Interval(IntervalUnit::MonthDayNano)) => {
1923            cast_duration_to_interval::<DurationMicrosecondType>(array, cast_options)
1924        }
1925        (Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano)) => {
1926            cast_duration_to_interval::<DurationNanosecondType>(array, cast_options)
1927        }
1928        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Second)) => {
1929            cast_month_day_nano_to_duration::<DurationSecondType>(array, cast_options)
1930        }
1931        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Millisecond)) => {
1932            cast_month_day_nano_to_duration::<DurationMillisecondType>(array, cast_options)
1933        }
1934        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Microsecond)) => {
1935            cast_month_day_nano_to_duration::<DurationMicrosecondType>(array, cast_options)
1936        }
1937        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Nanosecond)) => {
1938            cast_month_day_nano_to_duration::<DurationNanosecondType>(array, cast_options)
1939        }
1940        (Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
1941            cast_interval_year_month_to_interval_month_day_nano(array, cast_options)
1942        }
1943        (Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
1944            cast_interval_day_time_to_interval_month_day_nano(array, cast_options)
1945        }
1946        (Int32, Interval(IntervalUnit::YearMonth)) => {
1947            cast_reinterpret_arrays::<Int32Type, IntervalYearMonthType>(array)
1948        }
1949        (_, _) => Err(ArrowError::CastError(format!(
1950            "Casting from {from_type:?} to {to_type:?} not supported",
1951        ))),
1952    }
1953}
1954
1955fn cast_from_decimal<D, F>(
1956    array: &dyn Array,
1957    base: D::Native,
1958    scale: &i8,
1959    from_type: &DataType,
1960    to_type: &DataType,
1961    as_float: F,
1962    cast_options: &CastOptions,
1963) -> Result<ArrayRef, ArrowError>
1964where
1965    D: DecimalType + ArrowPrimitiveType,
1966    <D as ArrowPrimitiveType>::Native: ArrowNativeTypeOp + ToPrimitive,
1967    F: Fn(D::Native) -> f64,
1968{
1969    use DataType::*;
1970    // cast decimal to other type
1971    match to_type {
1972        UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, base, *scale, cast_options),
1973        UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, base, *scale, cast_options),
1974        UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, base, *scale, cast_options),
1975        UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, base, *scale, cast_options),
1976        Int8 => cast_decimal_to_integer::<D, Int8Type>(array, base, *scale, cast_options),
1977        Int16 => cast_decimal_to_integer::<D, Int16Type>(array, base, *scale, cast_options),
1978        Int32 => cast_decimal_to_integer::<D, Int32Type>(array, base, *scale, cast_options),
1979        Int64 => cast_decimal_to_integer::<D, Int64Type>(array, base, *scale, cast_options),
1980        Float32 => cast_decimal_to_float::<D, Float32Type, _>(array, |x| {
1981            (as_float(x) / 10_f64.powi(*scale as i32)) as f32
1982        }),
1983        Float64 => cast_decimal_to_float::<D, Float64Type, _>(array, |x| {
1984            as_float(x) / 10_f64.powi(*scale as i32)
1985        }),
1986        Utf8View => value_to_string_view(array, cast_options),
1987        Utf8 => value_to_string::<i32>(array, cast_options),
1988        LargeUtf8 => value_to_string::<i64>(array, cast_options),
1989        Null => Ok(new_null_array(to_type, array.len())),
1990        _ => Err(ArrowError::CastError(format!(
1991            "Casting from {from_type:?} to {to_type:?} not supported"
1992        ))),
1993    }
1994}
1995
1996fn cast_to_decimal<D, M>(
1997    array: &dyn Array,
1998    base: M,
1999    precision: &u8,
2000    scale: &i8,
2001    from_type: &DataType,
2002    to_type: &DataType,
2003    cast_options: &CastOptions,
2004) -> Result<ArrayRef, ArrowError>
2005where
2006    D: DecimalType + ArrowPrimitiveType<Native = M>,
2007    M: ArrowNativeTypeOp + DecimalCast,
2008    u8: num::traits::AsPrimitive<M>,
2009    u16: num::traits::AsPrimitive<M>,
2010    u32: num::traits::AsPrimitive<M>,
2011    u64: num::traits::AsPrimitive<M>,
2012    i8: num::traits::AsPrimitive<M>,
2013    i16: num::traits::AsPrimitive<M>,
2014    i32: num::traits::AsPrimitive<M>,
2015    i64: num::traits::AsPrimitive<M>,
2016{
2017    use DataType::*;
2018    // cast data to decimal
2019    match from_type {
2020        UInt8 => cast_integer_to_decimal::<_, D, M>(
2021            array.as_primitive::<UInt8Type>(),
2022            *precision,
2023            *scale,
2024            base,
2025            cast_options,
2026        ),
2027        UInt16 => cast_integer_to_decimal::<_, D, _>(
2028            array.as_primitive::<UInt16Type>(),
2029            *precision,
2030            *scale,
2031            base,
2032            cast_options,
2033        ),
2034        UInt32 => cast_integer_to_decimal::<_, D, _>(
2035            array.as_primitive::<UInt32Type>(),
2036            *precision,
2037            *scale,
2038            base,
2039            cast_options,
2040        ),
2041        UInt64 => cast_integer_to_decimal::<_, D, _>(
2042            array.as_primitive::<UInt64Type>(),
2043            *precision,
2044            *scale,
2045            base,
2046            cast_options,
2047        ),
2048        Int8 => cast_integer_to_decimal::<_, D, _>(
2049            array.as_primitive::<Int8Type>(),
2050            *precision,
2051            *scale,
2052            base,
2053            cast_options,
2054        ),
2055        Int16 => cast_integer_to_decimal::<_, D, _>(
2056            array.as_primitive::<Int16Type>(),
2057            *precision,
2058            *scale,
2059            base,
2060            cast_options,
2061        ),
2062        Int32 => cast_integer_to_decimal::<_, D, _>(
2063            array.as_primitive::<Int32Type>(),
2064            *precision,
2065            *scale,
2066            base,
2067            cast_options,
2068        ),
2069        Int64 => cast_integer_to_decimal::<_, D, _>(
2070            array.as_primitive::<Int64Type>(),
2071            *precision,
2072            *scale,
2073            base,
2074            cast_options,
2075        ),
2076        Float32 => cast_floating_point_to_decimal::<_, D>(
2077            array.as_primitive::<Float32Type>(),
2078            *precision,
2079            *scale,
2080            cast_options,
2081        ),
2082        Float64 => cast_floating_point_to_decimal::<_, D>(
2083            array.as_primitive::<Float64Type>(),
2084            *precision,
2085            *scale,
2086            cast_options,
2087        ),
2088        Utf8View | Utf8 => {
2089            cast_string_to_decimal::<D, i32>(array, *precision, *scale, cast_options)
2090        }
2091        LargeUtf8 => cast_string_to_decimal::<D, i64>(array, *precision, *scale, cast_options),
2092        Null => Ok(new_null_array(to_type, array.len())),
2093        _ => Err(ArrowError::CastError(format!(
2094            "Casting from {from_type:?} to {to_type:?} not supported"
2095        ))),
2096    }
2097}
2098
2099/// Get the time unit as a multiple of a second
2100const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
2101    match unit {
2102        TimeUnit::Second => 1,
2103        TimeUnit::Millisecond => MILLISECONDS,
2104        TimeUnit::Microsecond => MICROSECONDS,
2105        TimeUnit::Nanosecond => NANOSECONDS,
2106    }
2107}
2108
2109/// Convert Array into a PrimitiveArray of type, and apply numeric cast
2110fn cast_numeric_arrays<FROM, TO>(
2111    from: &dyn Array,
2112    cast_options: &CastOptions,
2113) -> Result<ArrayRef, ArrowError>
2114where
2115    FROM: ArrowPrimitiveType,
2116    TO: ArrowPrimitiveType,
2117    FROM::Native: NumCast,
2118    TO::Native: NumCast,
2119{
2120    if cast_options.safe {
2121        // If the value can't be casted to the `TO::Native`, return null
2122        Ok(Arc::new(numeric_cast::<FROM, TO>(
2123            from.as_primitive::<FROM>(),
2124        )))
2125    } else {
2126        // If the value can't be casted to the `TO::Native`, return error
2127        Ok(Arc::new(try_numeric_cast::<FROM, TO>(
2128            from.as_primitive::<FROM>(),
2129        )?))
2130    }
2131}
2132
2133// Natural cast between numeric types
2134// If the value of T can't be casted to R, will throw error
2135fn try_numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>, ArrowError>
2136where
2137    T: ArrowPrimitiveType,
2138    R: ArrowPrimitiveType,
2139    T::Native: NumCast,
2140    R::Native: NumCast,
2141{
2142    from.try_unary(|value| {
2143        num::cast::cast::<T::Native, R::Native>(value).ok_or_else(|| {
2144            ArrowError::CastError(format!(
2145                "Can't cast value {:?} to type {}",
2146                value,
2147                R::DATA_TYPE
2148            ))
2149        })
2150    })
2151}
2152
2153// Natural cast between numeric types
2154// If the value of T can't be casted to R, it will be converted to null
2155fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> PrimitiveArray<R>
2156where
2157    T: ArrowPrimitiveType,
2158    R: ArrowPrimitiveType,
2159    T::Native: NumCast,
2160    R::Native: NumCast,
2161{
2162    from.unary_opt::<_, R>(num::cast::cast::<T::Native, R::Native>)
2163}
2164
2165fn cast_numeric_to_binary<FROM: ArrowPrimitiveType, O: OffsetSizeTrait>(
2166    array: &dyn Array,
2167) -> Result<ArrayRef, ArrowError> {
2168    let array = array.as_primitive::<FROM>();
2169    let size = std::mem::size_of::<FROM::Native>();
2170    let offsets = OffsetBuffer::from_lengths(std::iter::repeat(size).take(array.len()));
2171    Ok(Arc::new(GenericBinaryArray::<O>::new(
2172        offsets,
2173        array.values().inner().clone(),
2174        array.nulls().cloned(),
2175    )))
2176}
2177
2178fn adjust_timestamp_to_timezone<T: ArrowTimestampType>(
2179    array: PrimitiveArray<Int64Type>,
2180    to_tz: &Tz,
2181    cast_options: &CastOptions,
2182) -> Result<PrimitiveArray<Int64Type>, ArrowError> {
2183    let adjust = |o| {
2184        let local = as_datetime::<T>(o)?;
2185        let offset = to_tz.offset_from_local_datetime(&local).single()?;
2186        T::make_value(local - offset.fix())
2187    };
2188    let adjusted = if cast_options.safe {
2189        array.unary_opt::<_, Int64Type>(adjust)
2190    } else {
2191        array.try_unary::<_, Int64Type, _>(|o| {
2192            adjust(o).ok_or_else(|| {
2193                ArrowError::CastError("Cannot cast timezone to different timezone".to_string())
2194            })
2195        })?
2196    };
2197    Ok(adjusted)
2198}
2199
2200/// Cast numeric types to Boolean
2201///
2202/// Any zero value returns `false` while non-zero returns `true`
2203fn cast_numeric_to_bool<FROM>(from: &dyn Array) -> Result<ArrayRef, ArrowError>
2204where
2205    FROM: ArrowPrimitiveType,
2206{
2207    numeric_to_bool_cast::<FROM>(from.as_primitive::<FROM>()).map(|to| Arc::new(to) as ArrayRef)
2208}
2209
2210fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray, ArrowError>
2211where
2212    T: ArrowPrimitiveType + ArrowPrimitiveType,
2213{
2214    let mut b = BooleanBuilder::with_capacity(from.len());
2215
2216    for i in 0..from.len() {
2217        if from.is_null(i) {
2218            b.append_null();
2219        } else if from.value(i) != T::default_value() {
2220            b.append_value(true);
2221        } else {
2222            b.append_value(false);
2223        }
2224    }
2225
2226    Ok(b.finish())
2227}
2228
2229/// Cast Boolean types to numeric
2230///
2231/// `false` returns 0 while `true` returns 1
2232fn cast_bool_to_numeric<TO>(
2233    from: &dyn Array,
2234    cast_options: &CastOptions,
2235) -> Result<ArrayRef, ArrowError>
2236where
2237    TO: ArrowPrimitiveType,
2238    TO::Native: num::cast::NumCast,
2239{
2240    Ok(Arc::new(bool_to_numeric_cast::<TO>(
2241        from.as_any().downcast_ref::<BooleanArray>().unwrap(),
2242        cast_options,
2243    )))
2244}
2245
2246fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions) -> PrimitiveArray<T>
2247where
2248    T: ArrowPrimitiveType,
2249    T::Native: num::NumCast,
2250{
2251    let iter = (0..from.len()).map(|i| {
2252        if from.is_null(i) {
2253            None
2254        } else if from.value(i) {
2255            // a workaround to cast a primitive to T::Native, infallible
2256            num::cast::cast(1)
2257        } else {
2258            Some(T::default_value())
2259        }
2260    });
2261    // Benefit:
2262    //     20% performance improvement
2263    // Soundness:
2264    //     The iterator is trustedLen because it comes from a Range
2265    unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
2266}
2267
2268/// Helper function to cast from one `BinaryArray` or 'LargeBinaryArray' to 'FixedSizeBinaryArray'.
2269fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
2270    array: &dyn Array,
2271    byte_width: i32,
2272    cast_options: &CastOptions,
2273) -> Result<ArrayRef, ArrowError> {
2274    let array = array.as_binary::<O>();
2275    let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(), byte_width);
2276
2277    for i in 0..array.len() {
2278        if array.is_null(i) {
2279            builder.append_null();
2280        } else {
2281            match builder.append_value(array.value(i)) {
2282                Ok(_) => {}
2283                Err(e) => match cast_options.safe {
2284                    true => builder.append_null(),
2285                    false => return Err(e),
2286                },
2287            }
2288        }
2289    }
2290
2291    Ok(Arc::new(builder.finish()))
2292}
2293
2294/// Helper function to cast from 'FixedSizeBinaryArray' to one `BinaryArray` or 'LargeBinaryArray'.
2295/// If the target one is too large for the source array it will return an Error.
2296fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
2297    array: &dyn Array,
2298    byte_width: i32,
2299) -> Result<ArrayRef, ArrowError> {
2300    let array = array
2301        .as_any()
2302        .downcast_ref::<FixedSizeBinaryArray>()
2303        .unwrap();
2304
2305    let offsets: i128 = byte_width as i128 * array.len() as i128;
2306
2307    let is_binary = matches!(GenericBinaryType::<O>::DATA_TYPE, DataType::Binary);
2308    if is_binary && offsets > i32::MAX as i128 {
2309        return Err(ArrowError::ComputeError(
2310            "FixedSizeBinary array too large to cast to Binary array".to_string(),
2311        ));
2312    } else if !is_binary && offsets > i64::MAX as i128 {
2313        return Err(ArrowError::ComputeError(
2314            "FixedSizeBinary array too large to cast to LargeBinary array".to_string(),
2315        ));
2316    }
2317
2318    let mut builder = GenericBinaryBuilder::<O>::with_capacity(array.len(), array.len());
2319
2320    for i in 0..array.len() {
2321        if array.is_null(i) {
2322            builder.append_null();
2323        } else {
2324            builder.append_value(array.value(i));
2325        }
2326    }
2327
2328    Ok(Arc::new(builder.finish()))
2329}
2330
2331fn cast_fixed_size_binary_to_binary_view(
2332    array: &dyn Array,
2333    _byte_width: i32,
2334) -> Result<ArrayRef, ArrowError> {
2335    let array = array
2336        .as_any()
2337        .downcast_ref::<FixedSizeBinaryArray>()
2338        .unwrap();
2339
2340    let mut builder = BinaryViewBuilder::with_capacity(array.len());
2341    for i in 0..array.len() {
2342        if array.is_null(i) {
2343            builder.append_null();
2344        } else {
2345            builder.append_value(array.value(i));
2346        }
2347    }
2348
2349    Ok(Arc::new(builder.finish()))
2350}
2351
2352/// Helper function to cast from one `ByteArrayType` to another and vice versa.
2353/// If the target one (e.g., `LargeUtf8`) is too large for the source array it will return an Error.
2354fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2355where
2356    FROM: ByteArrayType,
2357    TO: ByteArrayType<Native = FROM::Native>,
2358    FROM::Offset: OffsetSizeTrait + ToPrimitive,
2359    TO::Offset: OffsetSizeTrait + NumCast,
2360{
2361    let data = array.to_data();
2362    assert_eq!(data.data_type(), &FROM::DATA_TYPE);
2363    let str_values_buf = data.buffers()[1].clone();
2364    let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
2365
2366    let mut offset_builder = BufferBuilder::<TO::Offset>::new(offsets.len());
2367    offsets
2368        .iter()
2369        .try_for_each::<_, Result<_, ArrowError>>(|offset| {
2370            let offset =
2371                <<TO as ByteArrayType>::Offset as NumCast>::from(*offset).ok_or_else(|| {
2372                    ArrowError::ComputeError(format!(
2373                        "{}{} array too large to cast to {}{} array",
2374                        FROM::Offset::PREFIX,
2375                        FROM::PREFIX,
2376                        TO::Offset::PREFIX,
2377                        TO::PREFIX
2378                    ))
2379                })?;
2380            offset_builder.append(offset);
2381            Ok(())
2382        })?;
2383
2384    let offset_buffer = offset_builder.finish();
2385
2386    let dtype = TO::DATA_TYPE;
2387
2388    let builder = ArrayData::builder(dtype)
2389        .offset(array.offset())
2390        .len(array.len())
2391        .add_buffer(offset_buffer)
2392        .add_buffer(str_values_buf)
2393        .nulls(data.nulls().cloned());
2394
2395    let array_data = unsafe { builder.build_unchecked() };
2396
2397    Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
2398}
2399
2400/// Helper function to cast from one `ByteViewType` array to `ByteArrayType` array.
2401fn cast_view_to_byte<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2402where
2403    FROM: ByteViewType,
2404    TO: ByteArrayType,
2405    FROM::Native: AsRef<TO::Native>,
2406{
2407    let data = array.to_data();
2408    let view_array = GenericByteViewArray::<FROM>::from(data);
2409
2410    let len = view_array.len();
2411    let bytes = view_array
2412        .views()
2413        .iter()
2414        .map(|v| ByteView::from(*v).length as usize)
2415        .sum::<usize>();
2416
2417    let mut byte_array_builder = GenericByteBuilder::<TO>::with_capacity(len, bytes);
2418
2419    for val in view_array.iter() {
2420        byte_array_builder.append_option(val);
2421    }
2422
2423    Ok(Arc::new(byte_array_builder.finish()))
2424}
2425
2426#[cfg(test)]
2427mod tests {
2428    use super::*;
2429    use arrow_buffer::{Buffer, IntervalDayTime, NullBuffer};
2430    use chrono::NaiveDate;
2431    use half::f16;
2432
2433    #[derive(Clone)]
2434    struct DecimalCastTestConfig {
2435        input_prec: u8,
2436        input_scale: i8,
2437        input_repr: i128,
2438        output_prec: u8,
2439        output_scale: i8,
2440        expected_output_repr: Result<i128, String>, // the error variant can contain a string
2441                                                    // template where the "{}" will be
2442                                                    // replaced with the decimal type name
2443                                                    // (e.g. Decimal128)
2444    }
2445
2446    macro_rules! generate_cast_test_case {
2447        ($INPUT_ARRAY: expr, $OUTPUT_TYPE_ARRAY: ident, $OUTPUT_TYPE: expr, $OUTPUT_VALUES: expr) => {
2448            let output =
2449                $OUTPUT_TYPE_ARRAY::from($OUTPUT_VALUES).with_data_type($OUTPUT_TYPE.clone());
2450
2451            // assert cast type
2452            let input_array_type = $INPUT_ARRAY.data_type();
2453            assert!(can_cast_types(input_array_type, $OUTPUT_TYPE));
2454            let result = cast($INPUT_ARRAY, $OUTPUT_TYPE).unwrap();
2455            assert_eq!($OUTPUT_TYPE, result.data_type());
2456            assert_eq!(result.as_ref(), &output);
2457
2458            let cast_option = CastOptions {
2459                safe: false,
2460                format_options: FormatOptions::default(),
2461            };
2462            let result = cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap();
2463            assert_eq!($OUTPUT_TYPE, result.data_type());
2464            assert_eq!(result.as_ref(), &output);
2465        };
2466    }
2467
2468    fn run_decimal_cast_test_case<I, O>(t: DecimalCastTestConfig)
2469    where
2470        I: DecimalType,
2471        O: DecimalType,
2472        I::Native: DecimalCast,
2473        O::Native: DecimalCast,
2474    {
2475        let array = vec![I::Native::from_decimal(t.input_repr)];
2476        let array = array
2477            .into_iter()
2478            .collect::<PrimitiveArray<I>>()
2479            .with_precision_and_scale(t.input_prec, t.input_scale)
2480            .unwrap();
2481        let input_type = array.data_type();
2482        let output_type = O::TYPE_CONSTRUCTOR(t.output_prec, t.output_scale);
2483        assert!(can_cast_types(input_type, &output_type));
2484
2485        let options = CastOptions {
2486            safe: false,
2487            ..Default::default()
2488        };
2489        let result = cast_with_options(&array, &output_type, &options);
2490
2491        match t.expected_output_repr {
2492            Ok(v) => {
2493                let expected_array = vec![O::Native::from_decimal(v)];
2494                let expected_array = expected_array
2495                    .into_iter()
2496                    .collect::<PrimitiveArray<O>>()
2497                    .with_precision_and_scale(t.output_prec, t.output_scale)
2498                    .unwrap();
2499                assert_eq!(*result.unwrap(), expected_array);
2500            }
2501            Err(expected_output_message_template) => {
2502                assert!(result.is_err());
2503                let expected_error_message =
2504                    expected_output_message_template.replace("{}", O::PREFIX);
2505                assert_eq!(result.unwrap_err().to_string(), expected_error_message);
2506            }
2507        }
2508    }
2509
2510    fn create_decimal128_array(
2511        array: Vec<Option<i128>>,
2512        precision: u8,
2513        scale: i8,
2514    ) -> Result<Decimal128Array, ArrowError> {
2515        array
2516            .into_iter()
2517            .collect::<Decimal128Array>()
2518            .with_precision_and_scale(precision, scale)
2519    }
2520
2521    fn create_decimal256_array(
2522        array: Vec<Option<i256>>,
2523        precision: u8,
2524        scale: i8,
2525    ) -> Result<Decimal256Array, ArrowError> {
2526        array
2527            .into_iter()
2528            .collect::<Decimal256Array>()
2529            .with_precision_and_scale(precision, scale)
2530    }
2531
2532    #[test]
2533    #[cfg(not(feature = "force_validate"))]
2534    #[should_panic(
2535        expected = "Cannot cast to Decimal128(20, 3). Overflowing on 57896044618658097711785492504343953926634992332820282019728792003956564819967"
2536    )]
2537    fn test_cast_decimal_to_decimal_round_with_error() {
2538        // decimal256 to decimal128 overflow
2539        let array = vec![
2540            Some(i256::from_i128(1123454)),
2541            Some(i256::from_i128(2123456)),
2542            Some(i256::from_i128(-3123453)),
2543            Some(i256::from_i128(-3123456)),
2544            None,
2545            Some(i256::MAX),
2546            Some(i256::MIN),
2547        ];
2548        let input_decimal_array = create_decimal256_array(array, 76, 4).unwrap();
2549        let array = Arc::new(input_decimal_array) as ArrayRef;
2550        let input_type = DataType::Decimal256(76, 4);
2551        let output_type = DataType::Decimal128(20, 3);
2552        assert!(can_cast_types(&input_type, &output_type));
2553        generate_cast_test_case!(
2554            &array,
2555            Decimal128Array,
2556            &output_type,
2557            vec![
2558                Some(112345_i128),
2559                Some(212346_i128),
2560                Some(-312345_i128),
2561                Some(-312346_i128),
2562                None,
2563                None,
2564                None,
2565            ]
2566        );
2567    }
2568
2569    #[test]
2570    #[cfg(not(feature = "force_validate"))]
2571    fn test_cast_decimal_to_decimal_round() {
2572        let array = vec![
2573            Some(1123454),
2574            Some(2123456),
2575            Some(-3123453),
2576            Some(-3123456),
2577            None,
2578        ];
2579        let array = create_decimal128_array(array, 20, 4).unwrap();
2580        // decimal128 to decimal128
2581        let input_type = DataType::Decimal128(20, 4);
2582        let output_type = DataType::Decimal128(20, 3);
2583        assert!(can_cast_types(&input_type, &output_type));
2584        generate_cast_test_case!(
2585            &array,
2586            Decimal128Array,
2587            &output_type,
2588            vec![
2589                Some(112345_i128),
2590                Some(212346_i128),
2591                Some(-312345_i128),
2592                Some(-312346_i128),
2593                None
2594            ]
2595        );
2596
2597        // decimal128 to decimal256
2598        let input_type = DataType::Decimal128(20, 4);
2599        let output_type = DataType::Decimal256(20, 3);
2600        assert!(can_cast_types(&input_type, &output_type));
2601        generate_cast_test_case!(
2602            &array,
2603            Decimal256Array,
2604            &output_type,
2605            vec![
2606                Some(i256::from_i128(112345_i128)),
2607                Some(i256::from_i128(212346_i128)),
2608                Some(i256::from_i128(-312345_i128)),
2609                Some(i256::from_i128(-312346_i128)),
2610                None
2611            ]
2612        );
2613
2614        // decimal256
2615        let array = vec![
2616            Some(i256::from_i128(1123454)),
2617            Some(i256::from_i128(2123456)),
2618            Some(i256::from_i128(-3123453)),
2619            Some(i256::from_i128(-3123456)),
2620            None,
2621        ];
2622        let array = create_decimal256_array(array, 20, 4).unwrap();
2623
2624        // decimal256 to decimal256
2625        let input_type = DataType::Decimal256(20, 4);
2626        let output_type = DataType::Decimal256(20, 3);
2627        assert!(can_cast_types(&input_type, &output_type));
2628        generate_cast_test_case!(
2629            &array,
2630            Decimal256Array,
2631            &output_type,
2632            vec![
2633                Some(i256::from_i128(112345_i128)),
2634                Some(i256::from_i128(212346_i128)),
2635                Some(i256::from_i128(-312345_i128)),
2636                Some(i256::from_i128(-312346_i128)),
2637                None
2638            ]
2639        );
2640        // decimal256 to decimal128
2641        let input_type = DataType::Decimal256(20, 4);
2642        let output_type = DataType::Decimal128(20, 3);
2643        assert!(can_cast_types(&input_type, &output_type));
2644        generate_cast_test_case!(
2645            &array,
2646            Decimal128Array,
2647            &output_type,
2648            vec![
2649                Some(112345_i128),
2650                Some(212346_i128),
2651                Some(-312345_i128),
2652                Some(-312346_i128),
2653                None
2654            ]
2655        );
2656    }
2657
2658    #[test]
2659    fn test_cast_decimal128_to_decimal128() {
2660        let input_type = DataType::Decimal128(20, 3);
2661        let output_type = DataType::Decimal128(20, 4);
2662        assert!(can_cast_types(&input_type, &output_type));
2663        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2664        let array = create_decimal128_array(array, 20, 3).unwrap();
2665        generate_cast_test_case!(
2666            &array,
2667            Decimal128Array,
2668            &output_type,
2669            vec![
2670                Some(11234560_i128),
2671                Some(21234560_i128),
2672                Some(31234560_i128),
2673                None
2674            ]
2675        );
2676        // negative test
2677        let array = vec![Some(123456), None];
2678        let array = create_decimal128_array(array, 10, 0).unwrap();
2679        let result_safe = cast(&array, &DataType::Decimal128(2, 2));
2680        assert!(result_safe.is_ok());
2681        let options = CastOptions {
2682            safe: false,
2683            ..Default::default()
2684        };
2685
2686        let result_unsafe = cast_with_options(&array, &DataType::Decimal128(2, 2), &options);
2687        assert_eq!("Invalid argument error: 12345600 is too large to store in a Decimal128 of precision 2. Max is 99",
2688                   result_unsafe.unwrap_err().to_string());
2689    }
2690
2691    #[test]
2692    fn test_cast_decimal128_to_decimal128_dict() {
2693        let p = 20;
2694        let s = 3;
2695        let input_type = DataType::Decimal128(p, s);
2696        let output_type = DataType::Dictionary(
2697            Box::new(DataType::Int32),
2698            Box::new(DataType::Decimal128(p, s)),
2699        );
2700        assert!(can_cast_types(&input_type, &output_type));
2701        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2702        let array = create_decimal128_array(array, p, s).unwrap();
2703        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
2704        assert_eq!(cast_array.data_type(), &output_type);
2705    }
2706
2707    #[test]
2708    fn test_cast_decimal256_to_decimal256_dict() {
2709        let p = 20;
2710        let s = 3;
2711        let input_type = DataType::Decimal256(p, s);
2712        let output_type = DataType::Dictionary(
2713            Box::new(DataType::Int32),
2714            Box::new(DataType::Decimal256(p, s)),
2715        );
2716        assert!(can_cast_types(&input_type, &output_type));
2717        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2718        let array = create_decimal128_array(array, p, s).unwrap();
2719        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
2720        assert_eq!(cast_array.data_type(), &output_type);
2721    }
2722
2723    #[test]
2724    fn test_cast_decimal128_to_decimal128_overflow() {
2725        let input_type = DataType::Decimal128(38, 3);
2726        let output_type = DataType::Decimal128(38, 38);
2727        assert!(can_cast_types(&input_type, &output_type));
2728
2729        let array = vec![Some(i128::MAX)];
2730        let array = create_decimal128_array(array, 38, 3).unwrap();
2731        let result = cast_with_options(
2732            &array,
2733            &output_type,
2734            &CastOptions {
2735                safe: false,
2736                format_options: FormatOptions::default(),
2737            },
2738        );
2739        assert_eq!("Cast error: Cannot cast to Decimal128(38, 38). Overflowing on 170141183460469231731687303715884105727",
2740                   result.unwrap_err().to_string());
2741    }
2742
2743    #[test]
2744    fn test_cast_decimal128_to_decimal256_overflow() {
2745        let input_type = DataType::Decimal128(38, 3);
2746        let output_type = DataType::Decimal256(76, 76);
2747        assert!(can_cast_types(&input_type, &output_type));
2748
2749        let array = vec![Some(i128::MAX)];
2750        let array = create_decimal128_array(array, 38, 3).unwrap();
2751        let result = cast_with_options(
2752            &array,
2753            &output_type,
2754            &CastOptions {
2755                safe: false,
2756                format_options: FormatOptions::default(),
2757            },
2758        );
2759        assert_eq!("Cast error: Cannot cast to Decimal256(76, 76). Overflowing on 170141183460469231731687303715884105727",
2760                   result.unwrap_err().to_string());
2761    }
2762
2763    #[test]
2764    fn test_cast_decimal128_to_decimal256() {
2765        let input_type = DataType::Decimal128(20, 3);
2766        let output_type = DataType::Decimal256(20, 4);
2767        assert!(can_cast_types(&input_type, &output_type));
2768        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2769        let array = create_decimal128_array(array, 20, 3).unwrap();
2770        generate_cast_test_case!(
2771            &array,
2772            Decimal256Array,
2773            &output_type,
2774            vec![
2775                Some(i256::from_i128(11234560_i128)),
2776                Some(i256::from_i128(21234560_i128)),
2777                Some(i256::from_i128(31234560_i128)),
2778                None
2779            ]
2780        );
2781    }
2782
2783    #[test]
2784    fn test_cast_decimal256_to_decimal128_overflow() {
2785        let input_type = DataType::Decimal256(76, 5);
2786        let output_type = DataType::Decimal128(38, 7);
2787        assert!(can_cast_types(&input_type, &output_type));
2788        let array = vec![Some(i256::from_i128(i128::MAX))];
2789        let array = create_decimal256_array(array, 76, 5).unwrap();
2790        let result = cast_with_options(
2791            &array,
2792            &output_type,
2793            &CastOptions {
2794                safe: false,
2795                format_options: FormatOptions::default(),
2796            },
2797        );
2798        assert_eq!("Cast error: Cannot cast to Decimal128(38, 7). Overflowing on 170141183460469231731687303715884105727",
2799                   result.unwrap_err().to_string());
2800    }
2801
2802    #[test]
2803    fn test_cast_decimal256_to_decimal256_overflow() {
2804        let input_type = DataType::Decimal256(76, 5);
2805        let output_type = DataType::Decimal256(76, 55);
2806        assert!(can_cast_types(&input_type, &output_type));
2807        let array = vec![Some(i256::from_i128(i128::MAX))];
2808        let array = create_decimal256_array(array, 76, 5).unwrap();
2809        let result = cast_with_options(
2810            &array,
2811            &output_type,
2812            &CastOptions {
2813                safe: false,
2814                format_options: FormatOptions::default(),
2815            },
2816        );
2817        assert_eq!("Cast error: Cannot cast to Decimal256(76, 55). Overflowing on 170141183460469231731687303715884105727",
2818                   result.unwrap_err().to_string());
2819    }
2820
2821    #[test]
2822    fn test_cast_decimal256_to_decimal128() {
2823        let input_type = DataType::Decimal256(20, 3);
2824        let output_type = DataType::Decimal128(20, 4);
2825        assert!(can_cast_types(&input_type, &output_type));
2826        let array = vec![
2827            Some(i256::from_i128(1123456)),
2828            Some(i256::from_i128(2123456)),
2829            Some(i256::from_i128(3123456)),
2830            None,
2831        ];
2832        let array = create_decimal256_array(array, 20, 3).unwrap();
2833        generate_cast_test_case!(
2834            &array,
2835            Decimal128Array,
2836            &output_type,
2837            vec![
2838                Some(11234560_i128),
2839                Some(21234560_i128),
2840                Some(31234560_i128),
2841                None
2842            ]
2843        );
2844    }
2845
2846    #[test]
2847    fn test_cast_decimal256_to_decimal256() {
2848        let input_type = DataType::Decimal256(20, 3);
2849        let output_type = DataType::Decimal256(20, 4);
2850        assert!(can_cast_types(&input_type, &output_type));
2851        let array = vec![
2852            Some(i256::from_i128(1123456)),
2853            Some(i256::from_i128(2123456)),
2854            Some(i256::from_i128(3123456)),
2855            None,
2856        ];
2857        let array = create_decimal256_array(array, 20, 3).unwrap();
2858        generate_cast_test_case!(
2859            &array,
2860            Decimal256Array,
2861            &output_type,
2862            vec![
2863                Some(i256::from_i128(11234560_i128)),
2864                Some(i256::from_i128(21234560_i128)),
2865                Some(i256::from_i128(31234560_i128)),
2866                None
2867            ]
2868        );
2869    }
2870
2871    fn generate_decimal_to_numeric_cast_test_case<T>(array: &PrimitiveArray<T>)
2872    where
2873        T: ArrowPrimitiveType + DecimalType,
2874    {
2875        // u8
2876        generate_cast_test_case!(
2877            array,
2878            UInt8Array,
2879            &DataType::UInt8,
2880            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
2881        );
2882        // u16
2883        generate_cast_test_case!(
2884            array,
2885            UInt16Array,
2886            &DataType::UInt16,
2887            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
2888        );
2889        // u32
2890        generate_cast_test_case!(
2891            array,
2892            UInt32Array,
2893            &DataType::UInt32,
2894            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
2895        );
2896        // u64
2897        generate_cast_test_case!(
2898            array,
2899            UInt64Array,
2900            &DataType::UInt64,
2901            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
2902        );
2903        // i8
2904        generate_cast_test_case!(
2905            array,
2906            Int8Array,
2907            &DataType::Int8,
2908            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
2909        );
2910        // i16
2911        generate_cast_test_case!(
2912            array,
2913            Int16Array,
2914            &DataType::Int16,
2915            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
2916        );
2917        // i32
2918        generate_cast_test_case!(
2919            array,
2920            Int32Array,
2921            &DataType::Int32,
2922            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
2923        );
2924        // i64
2925        generate_cast_test_case!(
2926            array,
2927            Int64Array,
2928            &DataType::Int64,
2929            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
2930        );
2931        // f32
2932        generate_cast_test_case!(
2933            array,
2934            Float32Array,
2935            &DataType::Float32,
2936            vec![
2937                Some(1.25_f32),
2938                Some(2.25_f32),
2939                Some(3.25_f32),
2940                None,
2941                Some(5.25_f32)
2942            ]
2943        );
2944        // f64
2945        generate_cast_test_case!(
2946            array,
2947            Float64Array,
2948            &DataType::Float64,
2949            vec![
2950                Some(1.25_f64),
2951                Some(2.25_f64),
2952                Some(3.25_f64),
2953                None,
2954                Some(5.25_f64)
2955            ]
2956        );
2957    }
2958
2959    #[test]
2960    fn test_cast_decimal128_to_numeric() {
2961        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
2962        let array = create_decimal128_array(value_array, 38, 2).unwrap();
2963
2964        generate_decimal_to_numeric_cast_test_case(&array);
2965
2966        // overflow test: out of range of max u8
2967        let value_array: Vec<Option<i128>> = vec![Some(51300)];
2968        let array = create_decimal128_array(value_array, 38, 2).unwrap();
2969        let casted_array = cast_with_options(
2970            &array,
2971            &DataType::UInt8,
2972            &CastOptions {
2973                safe: false,
2974                format_options: FormatOptions::default(),
2975            },
2976        );
2977        assert_eq!(
2978            "Cast error: value of 513 is out of range UInt8".to_string(),
2979            casted_array.unwrap_err().to_string()
2980        );
2981
2982        let casted_array = cast_with_options(
2983            &array,
2984            &DataType::UInt8,
2985            &CastOptions {
2986                safe: true,
2987                format_options: FormatOptions::default(),
2988            },
2989        );
2990        assert!(casted_array.is_ok());
2991        assert!(casted_array.unwrap().is_null(0));
2992
2993        // overflow test: out of range of max i8
2994        let value_array: Vec<Option<i128>> = vec![Some(24400)];
2995        let array = create_decimal128_array(value_array, 38, 2).unwrap();
2996        let casted_array = cast_with_options(
2997            &array,
2998            &DataType::Int8,
2999            &CastOptions {
3000                safe: false,
3001                format_options: FormatOptions::default(),
3002            },
3003        );
3004        assert_eq!(
3005            "Cast error: value of 244 is out of range Int8".to_string(),
3006            casted_array.unwrap_err().to_string()
3007        );
3008
3009        let casted_array = cast_with_options(
3010            &array,
3011            &DataType::Int8,
3012            &CastOptions {
3013                safe: true,
3014                format_options: FormatOptions::default(),
3015            },
3016        );
3017        assert!(casted_array.is_ok());
3018        assert!(casted_array.unwrap().is_null(0));
3019
3020        // loss the precision: convert decimal to f32、f64
3021        // f32
3022        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3023        let value_array: Vec<Option<i128>> = vec![
3024            Some(125),
3025            Some(225),
3026            Some(325),
3027            None,
3028            Some(525),
3029            Some(112345678),
3030            Some(112345679),
3031        ];
3032        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3033        generate_cast_test_case!(
3034            &array,
3035            Float32Array,
3036            &DataType::Float32,
3037            vec![
3038                Some(1.25_f32),
3039                Some(2.25_f32),
3040                Some(3.25_f32),
3041                None,
3042                Some(5.25_f32),
3043                Some(1_123_456.7_f32),
3044                Some(1_123_456.7_f32)
3045            ]
3046        );
3047
3048        // f64
3049        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3050        let value_array: Vec<Option<i128>> = vec![
3051            Some(125),
3052            Some(225),
3053            Some(325),
3054            None,
3055            Some(525),
3056            Some(112345678901234568),
3057            Some(112345678901234560),
3058        ];
3059        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3060        generate_cast_test_case!(
3061            &array,
3062            Float64Array,
3063            &DataType::Float64,
3064            vec![
3065                Some(1.25_f64),
3066                Some(2.25_f64),
3067                Some(3.25_f64),
3068                None,
3069                Some(5.25_f64),
3070                Some(1_123_456_789_012_345.6_f64),
3071                Some(1_123_456_789_012_345.6_f64),
3072            ]
3073        );
3074    }
3075
3076    #[test]
3077    fn test_cast_decimal256_to_numeric() {
3078        let value_array: Vec<Option<i256>> = vec![
3079            Some(i256::from_i128(125)),
3080            Some(i256::from_i128(225)),
3081            Some(i256::from_i128(325)),
3082            None,
3083            Some(i256::from_i128(525)),
3084        ];
3085        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3086        // u8
3087        generate_cast_test_case!(
3088            &array,
3089            UInt8Array,
3090            &DataType::UInt8,
3091            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3092        );
3093        // u16
3094        generate_cast_test_case!(
3095            &array,
3096            UInt16Array,
3097            &DataType::UInt16,
3098            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3099        );
3100        // u32
3101        generate_cast_test_case!(
3102            &array,
3103            UInt32Array,
3104            &DataType::UInt32,
3105            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3106        );
3107        // u64
3108        generate_cast_test_case!(
3109            &array,
3110            UInt64Array,
3111            &DataType::UInt64,
3112            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3113        );
3114        // i8
3115        generate_cast_test_case!(
3116            &array,
3117            Int8Array,
3118            &DataType::Int8,
3119            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3120        );
3121        // i16
3122        generate_cast_test_case!(
3123            &array,
3124            Int16Array,
3125            &DataType::Int16,
3126            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3127        );
3128        // i32
3129        generate_cast_test_case!(
3130            &array,
3131            Int32Array,
3132            &DataType::Int32,
3133            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3134        );
3135        // i64
3136        generate_cast_test_case!(
3137            &array,
3138            Int64Array,
3139            &DataType::Int64,
3140            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3141        );
3142        // f32
3143        generate_cast_test_case!(
3144            &array,
3145            Float32Array,
3146            &DataType::Float32,
3147            vec![
3148                Some(1.25_f32),
3149                Some(2.25_f32),
3150                Some(3.25_f32),
3151                None,
3152                Some(5.25_f32)
3153            ]
3154        );
3155        // f64
3156        generate_cast_test_case!(
3157            &array,
3158            Float64Array,
3159            &DataType::Float64,
3160            vec![
3161                Some(1.25_f64),
3162                Some(2.25_f64),
3163                Some(3.25_f64),
3164                None,
3165                Some(5.25_f64)
3166            ]
3167        );
3168
3169        // overflow test: out of range of max i8
3170        let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128(24400))];
3171        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3172        let casted_array = cast_with_options(
3173            &array,
3174            &DataType::Int8,
3175            &CastOptions {
3176                safe: false,
3177                format_options: FormatOptions::default(),
3178            },
3179        );
3180        assert_eq!(
3181            "Cast error: value of 244 is out of range Int8".to_string(),
3182            casted_array.unwrap_err().to_string()
3183        );
3184
3185        let casted_array = cast_with_options(
3186            &array,
3187            &DataType::Int8,
3188            &CastOptions {
3189                safe: true,
3190                format_options: FormatOptions::default(),
3191            },
3192        );
3193        assert!(casted_array.is_ok());
3194        assert!(casted_array.unwrap().is_null(0));
3195
3196        // loss the precision: convert decimal to f32、f64
3197        // f32
3198        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3199        let value_array: Vec<Option<i256>> = vec![
3200            Some(i256::from_i128(125)),
3201            Some(i256::from_i128(225)),
3202            Some(i256::from_i128(325)),
3203            None,
3204            Some(i256::from_i128(525)),
3205            Some(i256::from_i128(112345678)),
3206            Some(i256::from_i128(112345679)),
3207        ];
3208        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3209        generate_cast_test_case!(
3210            &array,
3211            Float32Array,
3212            &DataType::Float32,
3213            vec![
3214                Some(1.25_f32),
3215                Some(2.25_f32),
3216                Some(3.25_f32),
3217                None,
3218                Some(5.25_f32),
3219                Some(1_123_456.7_f32),
3220                Some(1_123_456.7_f32)
3221            ]
3222        );
3223
3224        // f64
3225        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3226        let value_array: Vec<Option<i256>> = vec![
3227            Some(i256::from_i128(125)),
3228            Some(i256::from_i128(225)),
3229            Some(i256::from_i128(325)),
3230            None,
3231            Some(i256::from_i128(525)),
3232            Some(i256::from_i128(112345678901234568)),
3233            Some(i256::from_i128(112345678901234560)),
3234        ];
3235        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3236        generate_cast_test_case!(
3237            &array,
3238            Float64Array,
3239            &DataType::Float64,
3240            vec![
3241                Some(1.25_f64),
3242                Some(2.25_f64),
3243                Some(3.25_f64),
3244                None,
3245                Some(5.25_f64),
3246                Some(1_123_456_789_012_345.6_f64),
3247                Some(1_123_456_789_012_345.6_f64),
3248            ]
3249        );
3250    }
3251
3252    #[test]
3253    fn test_cast_numeric_to_decimal128() {
3254        let decimal_type = DataType::Decimal128(38, 6);
3255        // u8, u16, u32, u64
3256        let input_datas = vec![
3257            Arc::new(UInt8Array::from(vec![
3258                Some(1),
3259                Some(2),
3260                Some(3),
3261                None,
3262                Some(5),
3263            ])) as ArrayRef, // u8
3264            Arc::new(UInt16Array::from(vec![
3265                Some(1),
3266                Some(2),
3267                Some(3),
3268                None,
3269                Some(5),
3270            ])) as ArrayRef, // u16
3271            Arc::new(UInt32Array::from(vec![
3272                Some(1),
3273                Some(2),
3274                Some(3),
3275                None,
3276                Some(5),
3277            ])) as ArrayRef, // u32
3278            Arc::new(UInt64Array::from(vec![
3279                Some(1),
3280                Some(2),
3281                Some(3),
3282                None,
3283                Some(5),
3284            ])) as ArrayRef, // u64
3285        ];
3286
3287        for array in input_datas {
3288            generate_cast_test_case!(
3289                &array,
3290                Decimal128Array,
3291                &decimal_type,
3292                vec![
3293                    Some(1000000_i128),
3294                    Some(2000000_i128),
3295                    Some(3000000_i128),
3296                    None,
3297                    Some(5000000_i128)
3298                ]
3299            );
3300        }
3301
3302        // i8, i16, i32, i64
3303        let input_datas = vec![
3304            Arc::new(Int8Array::from(vec![
3305                Some(1),
3306                Some(2),
3307                Some(3),
3308                None,
3309                Some(5),
3310            ])) as ArrayRef, // i8
3311            Arc::new(Int16Array::from(vec![
3312                Some(1),
3313                Some(2),
3314                Some(3),
3315                None,
3316                Some(5),
3317            ])) as ArrayRef, // i16
3318            Arc::new(Int32Array::from(vec![
3319                Some(1),
3320                Some(2),
3321                Some(3),
3322                None,
3323                Some(5),
3324            ])) as ArrayRef, // i32
3325            Arc::new(Int64Array::from(vec![
3326                Some(1),
3327                Some(2),
3328                Some(3),
3329                None,
3330                Some(5),
3331            ])) as ArrayRef, // i64
3332        ];
3333        for array in input_datas {
3334            generate_cast_test_case!(
3335                &array,
3336                Decimal128Array,
3337                &decimal_type,
3338                vec![
3339                    Some(1000000_i128),
3340                    Some(2000000_i128),
3341                    Some(3000000_i128),
3342                    None,
3343                    Some(5000000_i128)
3344                ]
3345            );
3346        }
3347
3348        // test u8 to decimal type with overflow the result type
3349        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
3350        let array = UInt8Array::from(vec![1, 2, 3, 4, 100]);
3351        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
3352        assert!(casted_array.is_ok());
3353        let array = casted_array.unwrap();
3354        let array: &Decimal128Array = array.as_primitive();
3355        assert!(array.is_null(4));
3356
3357        // test i8 to decimal type with overflow the result type
3358        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
3359        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
3360        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
3361        assert!(casted_array.is_ok());
3362        let array = casted_array.unwrap();
3363        let array: &Decimal128Array = array.as_primitive();
3364        assert!(array.is_null(4));
3365
3366        // test f32 to decimal type
3367        let array = Float32Array::from(vec![
3368            Some(1.1),
3369            Some(2.2),
3370            Some(4.4),
3371            None,
3372            Some(1.123_456_4), // round down
3373            Some(1.123_456_7), // round up
3374        ]);
3375        let array = Arc::new(array) as ArrayRef;
3376        generate_cast_test_case!(
3377            &array,
3378            Decimal128Array,
3379            &decimal_type,
3380            vec![
3381                Some(1100000_i128),
3382                Some(2200000_i128),
3383                Some(4400000_i128),
3384                None,
3385                Some(1123456_i128), // round down
3386                Some(1123457_i128), // round up
3387            ]
3388        );
3389
3390        // test f64 to decimal type
3391        let array = Float64Array::from(vec![
3392            Some(1.1),
3393            Some(2.2),
3394            Some(4.4),
3395            None,
3396            Some(1.123_456_489_123_4),     // round up
3397            Some(1.123_456_789_123_4),     // round up
3398            Some(1.123_456_489_012_345_6), // round down
3399            Some(1.123_456_789_012_345_6), // round up
3400        ]);
3401        generate_cast_test_case!(
3402            &array,
3403            Decimal128Array,
3404            &decimal_type,
3405            vec![
3406                Some(1100000_i128),
3407                Some(2200000_i128),
3408                Some(4400000_i128),
3409                None,
3410                Some(1123456_i128), // round down
3411                Some(1123457_i128), // round up
3412                Some(1123456_i128), // round down
3413                Some(1123457_i128), // round up
3414            ]
3415        );
3416    }
3417
3418    #[test]
3419    fn test_cast_numeric_to_decimal256() {
3420        let decimal_type = DataType::Decimal256(76, 6);
3421        // u8, u16, u32, u64
3422        let input_datas = vec![
3423            Arc::new(UInt8Array::from(vec![
3424                Some(1),
3425                Some(2),
3426                Some(3),
3427                None,
3428                Some(5),
3429            ])) as ArrayRef, // u8
3430            Arc::new(UInt16Array::from(vec![
3431                Some(1),
3432                Some(2),
3433                Some(3),
3434                None,
3435                Some(5),
3436            ])) as ArrayRef, // u16
3437            Arc::new(UInt32Array::from(vec![
3438                Some(1),
3439                Some(2),
3440                Some(3),
3441                None,
3442                Some(5),
3443            ])) as ArrayRef, // u32
3444            Arc::new(UInt64Array::from(vec![
3445                Some(1),
3446                Some(2),
3447                Some(3),
3448                None,
3449                Some(5),
3450            ])) as ArrayRef, // u64
3451        ];
3452
3453        for array in input_datas {
3454            generate_cast_test_case!(
3455                &array,
3456                Decimal256Array,
3457                &decimal_type,
3458                vec![
3459                    Some(i256::from_i128(1000000_i128)),
3460                    Some(i256::from_i128(2000000_i128)),
3461                    Some(i256::from_i128(3000000_i128)),
3462                    None,
3463                    Some(i256::from_i128(5000000_i128))
3464                ]
3465            );
3466        }
3467
3468        // i8, i16, i32, i64
3469        let input_datas = vec![
3470            Arc::new(Int8Array::from(vec![
3471                Some(1),
3472                Some(2),
3473                Some(3),
3474                None,
3475                Some(5),
3476            ])) as ArrayRef, // i8
3477            Arc::new(Int16Array::from(vec![
3478                Some(1),
3479                Some(2),
3480                Some(3),
3481                None,
3482                Some(5),
3483            ])) as ArrayRef, // i16
3484            Arc::new(Int32Array::from(vec![
3485                Some(1),
3486                Some(2),
3487                Some(3),
3488                None,
3489                Some(5),
3490            ])) as ArrayRef, // i32
3491            Arc::new(Int64Array::from(vec![
3492                Some(1),
3493                Some(2),
3494                Some(3),
3495                None,
3496                Some(5),
3497            ])) as ArrayRef, // i64
3498        ];
3499        for array in input_datas {
3500            generate_cast_test_case!(
3501                &array,
3502                Decimal256Array,
3503                &decimal_type,
3504                vec![
3505                    Some(i256::from_i128(1000000_i128)),
3506                    Some(i256::from_i128(2000000_i128)),
3507                    Some(i256::from_i128(3000000_i128)),
3508                    None,
3509                    Some(i256::from_i128(5000000_i128))
3510                ]
3511            );
3512        }
3513
3514        // test i8 to decimal type with overflow the result type
3515        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
3516        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
3517        let array = Arc::new(array) as ArrayRef;
3518        let casted_array = cast(&array, &DataType::Decimal256(3, 1));
3519        assert!(casted_array.is_ok());
3520        let array = casted_array.unwrap();
3521        let array: &Decimal256Array = array.as_primitive();
3522        assert!(array.is_null(4));
3523
3524        // test f32 to decimal type
3525        let array = Float32Array::from(vec![
3526            Some(1.1),
3527            Some(2.2),
3528            Some(4.4),
3529            None,
3530            Some(1.123_456_4), // round down
3531            Some(1.123_456_7), // round up
3532        ]);
3533        generate_cast_test_case!(
3534            &array,
3535            Decimal256Array,
3536            &decimal_type,
3537            vec![
3538                Some(i256::from_i128(1100000_i128)),
3539                Some(i256::from_i128(2200000_i128)),
3540                Some(i256::from_i128(4400000_i128)),
3541                None,
3542                Some(i256::from_i128(1123456_i128)), // round down
3543                Some(i256::from_i128(1123457_i128)), // round up
3544            ]
3545        );
3546
3547        // test f64 to decimal type
3548        let array = Float64Array::from(vec![
3549            Some(1.1),
3550            Some(2.2),
3551            Some(4.4),
3552            None,
3553            Some(1.123_456_489_123_4),     // round down
3554            Some(1.123_456_789_123_4),     // round up
3555            Some(1.123_456_489_012_345_6), // round down
3556            Some(1.123_456_789_012_345_6), // round up
3557        ]);
3558        generate_cast_test_case!(
3559            &array,
3560            Decimal256Array,
3561            &decimal_type,
3562            vec![
3563                Some(i256::from_i128(1100000_i128)),
3564                Some(i256::from_i128(2200000_i128)),
3565                Some(i256::from_i128(4400000_i128)),
3566                None,
3567                Some(i256::from_i128(1123456_i128)), // round down
3568                Some(i256::from_i128(1123457_i128)), // round up
3569                Some(i256::from_i128(1123456_i128)), // round down
3570                Some(i256::from_i128(1123457_i128)), // round up
3571            ]
3572        );
3573    }
3574
3575    #[test]
3576    fn test_cast_i32_to_f64() {
3577        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
3578        let b = cast(&array, &DataType::Float64).unwrap();
3579        let c = b.as_primitive::<Float64Type>();
3580        assert_eq!(5.0, c.value(0));
3581        assert_eq!(6.0, c.value(1));
3582        assert_eq!(7.0, c.value(2));
3583        assert_eq!(8.0, c.value(3));
3584        assert_eq!(9.0, c.value(4));
3585    }
3586
3587    #[test]
3588    fn test_cast_i32_to_u8() {
3589        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
3590        let b = cast(&array, &DataType::UInt8).unwrap();
3591        let c = b.as_primitive::<UInt8Type>();
3592        assert!(!c.is_valid(0));
3593        assert_eq!(6, c.value(1));
3594        assert!(!c.is_valid(2));
3595        assert_eq!(8, c.value(3));
3596        // overflows return None
3597        assert!(!c.is_valid(4));
3598    }
3599
3600    #[test]
3601    #[should_panic(expected = "Can't cast value -5 to type UInt8")]
3602    fn test_cast_int32_to_u8_with_error() {
3603        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
3604        // overflow with the error
3605        let cast_option = CastOptions {
3606            safe: false,
3607            format_options: FormatOptions::default(),
3608        };
3609        let result = cast_with_options(&array, &DataType::UInt8, &cast_option);
3610        assert!(result.is_err());
3611        result.unwrap();
3612    }
3613
3614    #[test]
3615    fn test_cast_i32_to_u8_sliced() {
3616        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
3617        assert_eq!(0, array.offset());
3618        let array = array.slice(2, 3);
3619        let b = cast(&array, &DataType::UInt8).unwrap();
3620        assert_eq!(3, b.len());
3621        let c = b.as_primitive::<UInt8Type>();
3622        assert!(!c.is_valid(0));
3623        assert_eq!(8, c.value(1));
3624        // overflows return None
3625        assert!(!c.is_valid(2));
3626    }
3627
3628    #[test]
3629    fn test_cast_i32_to_i32() {
3630        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
3631        let b = cast(&array, &DataType::Int32).unwrap();
3632        let c = b.as_primitive::<Int32Type>();
3633        assert_eq!(5, c.value(0));
3634        assert_eq!(6, c.value(1));
3635        assert_eq!(7, c.value(2));
3636        assert_eq!(8, c.value(3));
3637        assert_eq!(9, c.value(4));
3638    }
3639
3640    #[test]
3641    fn test_cast_i32_to_list_i32() {
3642        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
3643        let b = cast(
3644            &array,
3645            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
3646        )
3647        .unwrap();
3648        assert_eq!(5, b.len());
3649        let arr = b.as_list::<i32>();
3650        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
3651        assert_eq!(1, arr.value_length(0));
3652        assert_eq!(1, arr.value_length(1));
3653        assert_eq!(1, arr.value_length(2));
3654        assert_eq!(1, arr.value_length(3));
3655        assert_eq!(1, arr.value_length(4));
3656        let c = arr.values().as_primitive::<Int32Type>();
3657        assert_eq!(5, c.value(0));
3658        assert_eq!(6, c.value(1));
3659        assert_eq!(7, c.value(2));
3660        assert_eq!(8, c.value(3));
3661        assert_eq!(9, c.value(4));
3662    }
3663
3664    #[test]
3665    fn test_cast_i32_to_list_i32_nullable() {
3666        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), Some(9)]);
3667        let b = cast(
3668            &array,
3669            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
3670        )
3671        .unwrap();
3672        assert_eq!(5, b.len());
3673        assert_eq!(0, b.null_count());
3674        let arr = b.as_list::<i32>();
3675        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
3676        assert_eq!(1, arr.value_length(0));
3677        assert_eq!(1, arr.value_length(1));
3678        assert_eq!(1, arr.value_length(2));
3679        assert_eq!(1, arr.value_length(3));
3680        assert_eq!(1, arr.value_length(4));
3681
3682        let c = arr.values().as_primitive::<Int32Type>();
3683        assert_eq!(1, c.null_count());
3684        assert_eq!(5, c.value(0));
3685        assert!(!c.is_valid(1));
3686        assert_eq!(7, c.value(2));
3687        assert_eq!(8, c.value(3));
3688        assert_eq!(9, c.value(4));
3689    }
3690
3691    #[test]
3692    fn test_cast_i32_to_list_f64_nullable_sliced() {
3693        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), None, Some(10)]);
3694        let array = array.slice(2, 4);
3695        let b = cast(
3696            &array,
3697            &DataType::List(Arc::new(Field::new_list_field(DataType::Float64, true))),
3698        )
3699        .unwrap();
3700        assert_eq!(4, b.len());
3701        assert_eq!(0, b.null_count());
3702        let arr = b.as_list::<i32>();
3703        assert_eq!(&[0, 1, 2, 3, 4], arr.value_offsets());
3704        assert_eq!(1, arr.value_length(0));
3705        assert_eq!(1, arr.value_length(1));
3706        assert_eq!(1, arr.value_length(2));
3707        assert_eq!(1, arr.value_length(3));
3708        let c = arr.values().as_primitive::<Float64Type>();
3709        assert_eq!(1, c.null_count());
3710        assert_eq!(7.0, c.value(0));
3711        assert_eq!(8.0, c.value(1));
3712        assert!(!c.is_valid(2));
3713        assert_eq!(10.0, c.value(3));
3714    }
3715
3716    #[test]
3717    fn test_cast_int_to_utf8view() {
3718        let inputs = vec![
3719            Arc::new(Int8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3720            Arc::new(Int16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3721            Arc::new(Int32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3722            Arc::new(Int64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3723            Arc::new(UInt8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3724            Arc::new(UInt16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3725            Arc::new(UInt32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3726            Arc::new(UInt64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
3727        ];
3728        let expected: ArrayRef = Arc::new(StringViewArray::from(vec![
3729            None,
3730            Some("8"),
3731            Some("9"),
3732            Some("10"),
3733        ]));
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_float_to_utf8view() {
3744        let inputs = vec![
3745            Arc::new(Float16Array::from(vec![
3746                Some(f16::from_f64(1.5)),
3747                Some(f16::from_f64(2.5)),
3748                None,
3749            ])) as ArrayRef,
3750            Arc::new(Float32Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
3751            Arc::new(Float64Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
3752        ];
3753
3754        let expected: ArrayRef =
3755            Arc::new(StringViewArray::from(vec![Some("1.5"), Some("2.5"), None]));
3756
3757        for array in inputs {
3758            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
3759            let arr = cast(&array, &DataType::Utf8View).unwrap();
3760            assert_eq!(expected.as_ref(), arr.as_ref());
3761        }
3762    }
3763
3764    #[test]
3765    fn test_cast_utf8_to_i32() {
3766        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
3767        let b = cast(&array, &DataType::Int32).unwrap();
3768        let c = b.as_primitive::<Int32Type>();
3769        assert_eq!(5, c.value(0));
3770        assert_eq!(6, c.value(1));
3771        assert!(!c.is_valid(2));
3772        assert_eq!(8, c.value(3));
3773        assert!(!c.is_valid(4));
3774    }
3775
3776    #[test]
3777    fn test_cast_utf8view_to_i32() {
3778        let array = StringViewArray::from(vec!["5", "6", "seven", "8", "9.1"]);
3779        let b = cast(&array, &DataType::Int32).unwrap();
3780        let c = b.as_primitive::<Int32Type>();
3781        assert_eq!(5, c.value(0));
3782        assert_eq!(6, c.value(1));
3783        assert!(!c.is_valid(2));
3784        assert_eq!(8, c.value(3));
3785        assert!(!c.is_valid(4));
3786    }
3787
3788    #[test]
3789    fn test_cast_utf8view_to_f32() {
3790        let array = StringViewArray::from(vec!["3", "4.56", "seven", "8.9"]);
3791        let b = cast(&array, &DataType::Float32).unwrap();
3792        let c = b.as_primitive::<Float32Type>();
3793        assert_eq!(3.0, c.value(0));
3794        assert_eq!(4.56, c.value(1));
3795        assert!(!c.is_valid(2));
3796        assert_eq!(8.9, c.value(3));
3797    }
3798
3799    #[test]
3800    fn test_cast_utf8view_to_decimal128() {
3801        let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
3802        let arr = Arc::new(array) as ArrayRef;
3803        generate_cast_test_case!(
3804            &arr,
3805            Decimal128Array,
3806            &DataType::Decimal128(4, 2),
3807            vec![None, Some(400_i128), Some(560_i128), Some(789_i128)]
3808        );
3809    }
3810
3811    #[test]
3812    fn test_cast_with_options_utf8_to_i32() {
3813        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
3814        let result = cast_with_options(
3815            &array,
3816            &DataType::Int32,
3817            &CastOptions {
3818                safe: false,
3819                format_options: FormatOptions::default(),
3820            },
3821        );
3822        match result {
3823            Ok(_) => panic!("expected error"),
3824            Err(e) => {
3825                assert!(
3826                    e.to_string()
3827                        .contains("Cast error: Cannot cast string 'seven' to value of Int32 type",),
3828                    "Error: {e}"
3829                )
3830            }
3831        }
3832    }
3833
3834    #[test]
3835    fn test_cast_utf8_to_bool() {
3836        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
3837        let casted = cast(&strings, &DataType::Boolean).unwrap();
3838        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
3839        assert_eq!(*as_boolean_array(&casted), expected);
3840    }
3841
3842    #[test]
3843    fn test_cast_utf8view_to_bool() {
3844        let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
3845        let casted = cast(&strings, &DataType::Boolean).unwrap();
3846        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
3847        assert_eq!(*as_boolean_array(&casted), expected);
3848    }
3849
3850    #[test]
3851    fn test_cast_with_options_utf8_to_bool() {
3852        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
3853        let casted = cast_with_options(
3854            &strings,
3855            &DataType::Boolean,
3856            &CastOptions {
3857                safe: false,
3858                format_options: FormatOptions::default(),
3859            },
3860        );
3861        match casted {
3862            Ok(_) => panic!("expected error"),
3863            Err(e) => {
3864                assert!(e
3865                    .to_string()
3866                    .contains("Cast error: Cannot cast value 'invalid' to value of Boolean type"))
3867            }
3868        }
3869    }
3870
3871    #[test]
3872    fn test_cast_bool_to_i32() {
3873        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
3874        let b = cast(&array, &DataType::Int32).unwrap();
3875        let c = b.as_primitive::<Int32Type>();
3876        assert_eq!(1, c.value(0));
3877        assert_eq!(0, c.value(1));
3878        assert!(!c.is_valid(2));
3879    }
3880
3881    #[test]
3882    fn test_cast_bool_to_utf8view() {
3883        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
3884        let b = cast(&array, &DataType::Utf8View).unwrap();
3885        let c = b.as_any().downcast_ref::<StringViewArray>().unwrap();
3886        assert_eq!("true", c.value(0));
3887        assert_eq!("false", c.value(1));
3888        assert!(!c.is_valid(2));
3889    }
3890
3891    #[test]
3892    fn test_cast_bool_to_utf8() {
3893        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
3894        let b = cast(&array, &DataType::Utf8).unwrap();
3895        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
3896        assert_eq!("true", c.value(0));
3897        assert_eq!("false", c.value(1));
3898        assert!(!c.is_valid(2));
3899    }
3900
3901    #[test]
3902    fn test_cast_bool_to_large_utf8() {
3903        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
3904        let b = cast(&array, &DataType::LargeUtf8).unwrap();
3905        let c = b.as_any().downcast_ref::<LargeStringArray>().unwrap();
3906        assert_eq!("true", c.value(0));
3907        assert_eq!("false", c.value(1));
3908        assert!(!c.is_valid(2));
3909    }
3910
3911    #[test]
3912    fn test_cast_bool_to_f64() {
3913        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
3914        let b = cast(&array, &DataType::Float64).unwrap();
3915        let c = b.as_primitive::<Float64Type>();
3916        assert_eq!(1.0, c.value(0));
3917        assert_eq!(0.0, c.value(1));
3918        assert!(!c.is_valid(2));
3919    }
3920
3921    #[test]
3922    fn test_cast_integer_to_timestamp() {
3923        let array = Int64Array::from(vec![Some(2), Some(10), None]);
3924        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3925
3926        let array = Int8Array::from(vec![Some(2), Some(10), None]);
3927        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3928
3929        assert_eq!(&actual, &expected);
3930
3931        let array = Int16Array::from(vec![Some(2), Some(10), None]);
3932        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3933
3934        assert_eq!(&actual, &expected);
3935
3936        let array = Int32Array::from(vec![Some(2), Some(10), None]);
3937        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3938
3939        assert_eq!(&actual, &expected);
3940
3941        let array = UInt8Array::from(vec![Some(2), Some(10), None]);
3942        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3943
3944        assert_eq!(&actual, &expected);
3945
3946        let array = UInt16Array::from(vec![Some(2), Some(10), None]);
3947        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3948
3949        assert_eq!(&actual, &expected);
3950
3951        let array = UInt32Array::from(vec![Some(2), Some(10), None]);
3952        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3953
3954        assert_eq!(&actual, &expected);
3955
3956        let array = UInt64Array::from(vec![Some(2), Some(10), None]);
3957        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3958
3959        assert_eq!(&actual, &expected);
3960    }
3961
3962    #[test]
3963    fn test_cast_timestamp_to_integer() {
3964        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
3965            .with_timezone("UTC".to_string());
3966        let expected = cast(&array, &DataType::Int64).unwrap();
3967
3968        let actual = cast(&cast(&array, &DataType::Int8).unwrap(), &DataType::Int64).unwrap();
3969        assert_eq!(&actual, &expected);
3970
3971        let actual = cast(&cast(&array, &DataType::Int16).unwrap(), &DataType::Int64).unwrap();
3972        assert_eq!(&actual, &expected);
3973
3974        let actual = cast(&cast(&array, &DataType::Int32).unwrap(), &DataType::Int64).unwrap();
3975        assert_eq!(&actual, &expected);
3976
3977        let actual = cast(&cast(&array, &DataType::UInt8).unwrap(), &DataType::Int64).unwrap();
3978        assert_eq!(&actual, &expected);
3979
3980        let actual = cast(&cast(&array, &DataType::UInt16).unwrap(), &DataType::Int64).unwrap();
3981        assert_eq!(&actual, &expected);
3982
3983        let actual = cast(&cast(&array, &DataType::UInt32).unwrap(), &DataType::Int64).unwrap();
3984        assert_eq!(&actual, &expected);
3985
3986        let actual = cast(&cast(&array, &DataType::UInt64).unwrap(), &DataType::Int64).unwrap();
3987        assert_eq!(&actual, &expected);
3988    }
3989
3990    #[test]
3991    fn test_cast_floating_to_timestamp() {
3992        let array = Int64Array::from(vec![Some(2), Some(10), None]);
3993        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
3994
3995        let array = Float16Array::from(vec![
3996            Some(f16::from_f32(2.0)),
3997            Some(f16::from_f32(10.6)),
3998            None,
3999        ]);
4000        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4001
4002        assert_eq!(&actual, &expected);
4003
4004        let array = Float32Array::from(vec![Some(2.0), Some(10.6), None]);
4005        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4006
4007        assert_eq!(&actual, &expected);
4008
4009        let array = Float64Array::from(vec![Some(2.1), Some(10.2), None]);
4010        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4011
4012        assert_eq!(&actual, &expected);
4013    }
4014
4015    #[test]
4016    fn test_cast_timestamp_to_floating() {
4017        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4018            .with_timezone("UTC".to_string());
4019        let expected = cast(&array, &DataType::Int64).unwrap();
4020
4021        let actual = cast(&cast(&array, &DataType::Float16).unwrap(), &DataType::Int64).unwrap();
4022        assert_eq!(&actual, &expected);
4023
4024        let actual = cast(&cast(&array, &DataType::Float32).unwrap(), &DataType::Int64).unwrap();
4025        assert_eq!(&actual, &expected);
4026
4027        let actual = cast(&cast(&array, &DataType::Float64).unwrap(), &DataType::Int64).unwrap();
4028        assert_eq!(&actual, &expected);
4029    }
4030
4031    #[test]
4032    fn test_cast_decimal_to_timestamp() {
4033        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4034        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4035
4036        let array = Decimal128Array::from(vec![Some(200), Some(1000), None])
4037            .with_precision_and_scale(4, 2)
4038            .unwrap();
4039        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4040
4041        assert_eq!(&actual, &expected);
4042
4043        let array = Decimal256Array::from(vec![
4044            Some(i256::from_i128(2000)),
4045            Some(i256::from_i128(10000)),
4046            None,
4047        ])
4048        .with_precision_and_scale(5, 3)
4049        .unwrap();
4050        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4051
4052        assert_eq!(&actual, &expected);
4053    }
4054
4055    #[test]
4056    fn test_cast_timestamp_to_decimal() {
4057        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4058            .with_timezone("UTC".to_string());
4059        let expected = cast(&array, &DataType::Int64).unwrap();
4060
4061        let actual = cast(
4062            &cast(&array, &DataType::Decimal128(5, 2)).unwrap(),
4063            &DataType::Int64,
4064        )
4065        .unwrap();
4066        assert_eq!(&actual, &expected);
4067
4068        let actual = cast(
4069            &cast(&array, &DataType::Decimal256(10, 5)).unwrap(),
4070            &DataType::Int64,
4071        )
4072        .unwrap();
4073        assert_eq!(&actual, &expected);
4074    }
4075
4076    #[test]
4077    fn test_cast_list_i32_to_list_u16() {
4078        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 100000000]).into_data();
4079
4080        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
4081
4082        // Construct a list array from the above two
4083        // [[0,0,0], [-1, -2, -1], [2, 100000000]]
4084        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
4085        let list_data = ArrayData::builder(list_data_type)
4086            .len(3)
4087            .add_buffer(value_offsets)
4088            .add_child_data(value_data)
4089            .build()
4090            .unwrap();
4091        let list_array = ListArray::from(list_data);
4092
4093        let cast_array = cast(
4094            &list_array,
4095            &DataType::List(Arc::new(Field::new_list_field(DataType::UInt16, true))),
4096        )
4097        .unwrap();
4098
4099        // For the ListArray itself, there are no null values (as there were no nulls when they went in)
4100        //
4101        // 3 negative values should get lost when casting to unsigned,
4102        // 1 value should overflow
4103        assert_eq!(0, cast_array.null_count());
4104
4105        // offsets should be the same
4106        let array = cast_array.as_list::<i32>();
4107        assert_eq!(list_array.value_offsets(), array.value_offsets());
4108
4109        assert_eq!(DataType::UInt16, array.value_type());
4110        assert_eq!(3, array.value_length(0));
4111        assert_eq!(3, array.value_length(1));
4112        assert_eq!(2, array.value_length(2));
4113
4114        // expect 4 nulls: negative numbers and overflow
4115        let u16arr = array.values().as_primitive::<UInt16Type>();
4116        assert_eq!(4, u16arr.null_count());
4117
4118        // expect 4 nulls: negative numbers and overflow
4119        let expected: UInt16Array =
4120            vec![Some(0), Some(0), Some(0), None, None, None, Some(2), None]
4121                .into_iter()
4122                .collect();
4123
4124        assert_eq!(u16arr, &expected);
4125    }
4126
4127    #[test]
4128    fn test_cast_list_i32_to_list_timestamp() {
4129        // Construct a value array
4130        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 8, 100000000]).into_data();
4131
4132        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 9]);
4133
4134        // Construct a list array from the above two
4135        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
4136        let list_data = ArrayData::builder(list_data_type)
4137            .len(3)
4138            .add_buffer(value_offsets)
4139            .add_child_data(value_data)
4140            .build()
4141            .unwrap();
4142        let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
4143
4144        let actual = cast(
4145            &list_array,
4146            &DataType::List(Arc::new(Field::new_list_field(
4147                DataType::Timestamp(TimeUnit::Microsecond, None),
4148                true,
4149            ))),
4150        )
4151        .unwrap();
4152
4153        let expected = cast(
4154            &cast(
4155                &list_array,
4156                &DataType::List(Arc::new(Field::new_list_field(DataType::Int64, true))),
4157            )
4158            .unwrap(),
4159            &DataType::List(Arc::new(Field::new_list_field(
4160                DataType::Timestamp(TimeUnit::Microsecond, None),
4161                true,
4162            ))),
4163        )
4164        .unwrap();
4165
4166        assert_eq!(&actual, &expected);
4167    }
4168
4169    #[test]
4170    fn test_cast_date32_to_date64() {
4171        let a = Date32Array::from(vec![10000, 17890]);
4172        let array = Arc::new(a) as ArrayRef;
4173        let b = cast(&array, &DataType::Date64).unwrap();
4174        let c = b.as_primitive::<Date64Type>();
4175        assert_eq!(864000000000, c.value(0));
4176        assert_eq!(1545696000000, c.value(1));
4177    }
4178
4179    #[test]
4180    fn test_cast_date64_to_date32() {
4181        let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
4182        let array = Arc::new(a) as ArrayRef;
4183        let b = cast(&array, &DataType::Date32).unwrap();
4184        let c = b.as_primitive::<Date32Type>();
4185        assert_eq!(10000, c.value(0));
4186        assert_eq!(17890, c.value(1));
4187        assert!(c.is_null(2));
4188    }
4189
4190    #[test]
4191    fn test_cast_string_to_integral_overflow() {
4192        let str = Arc::new(StringArray::from(vec![
4193            Some("123"),
4194            Some("-123"),
4195            Some("86374"),
4196            None,
4197        ])) as ArrayRef;
4198
4199        let options = CastOptions {
4200            safe: true,
4201            format_options: FormatOptions::default(),
4202        };
4203        let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
4204        let expected =
4205            Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
4206        assert_eq!(&res, &expected);
4207    }
4208
4209    #[test]
4210    fn test_cast_string_to_timestamp() {
4211        let a0 = Arc::new(StringViewArray::from(vec![
4212            Some("2020-09-08T12:00:00.123456789+00:00"),
4213            Some("Not a valid date"),
4214            None,
4215        ])) as ArrayRef;
4216        let a1 = Arc::new(StringArray::from(vec![
4217            Some("2020-09-08T12:00:00.123456789+00:00"),
4218            Some("Not a valid date"),
4219            None,
4220        ])) as ArrayRef;
4221        let a2 = Arc::new(LargeStringArray::from(vec![
4222            Some("2020-09-08T12:00:00.123456789+00:00"),
4223            Some("Not a valid date"),
4224            None,
4225        ])) as ArrayRef;
4226        for array in &[a0, a1, a2] {
4227            for time_unit in &[
4228                TimeUnit::Second,
4229                TimeUnit::Millisecond,
4230                TimeUnit::Microsecond,
4231                TimeUnit::Nanosecond,
4232            ] {
4233                let to_type = DataType::Timestamp(*time_unit, None);
4234                let b = cast(array, &to_type).unwrap();
4235
4236                match time_unit {
4237                    TimeUnit::Second => {
4238                        let c = b.as_primitive::<TimestampSecondType>();
4239                        assert_eq!(1599566400, c.value(0));
4240                        assert!(c.is_null(1));
4241                        assert!(c.is_null(2));
4242                    }
4243                    TimeUnit::Millisecond => {
4244                        let c = b
4245                            .as_any()
4246                            .downcast_ref::<TimestampMillisecondArray>()
4247                            .unwrap();
4248                        assert_eq!(1599566400123, c.value(0));
4249                        assert!(c.is_null(1));
4250                        assert!(c.is_null(2));
4251                    }
4252                    TimeUnit::Microsecond => {
4253                        let c = b
4254                            .as_any()
4255                            .downcast_ref::<TimestampMicrosecondArray>()
4256                            .unwrap();
4257                        assert_eq!(1599566400123456, c.value(0));
4258                        assert!(c.is_null(1));
4259                        assert!(c.is_null(2));
4260                    }
4261                    TimeUnit::Nanosecond => {
4262                        let c = b
4263                            .as_any()
4264                            .downcast_ref::<TimestampNanosecondArray>()
4265                            .unwrap();
4266                        assert_eq!(1599566400123456789, c.value(0));
4267                        assert!(c.is_null(1));
4268                        assert!(c.is_null(2));
4269                    }
4270                }
4271
4272                let options = CastOptions {
4273                    safe: false,
4274                    format_options: FormatOptions::default(),
4275                };
4276                let err = cast_with_options(array, &to_type, &options).unwrap_err();
4277                assert_eq!(
4278                    err.to_string(),
4279                    "Parser error: Error parsing timestamp from 'Not a valid date': error parsing date"
4280                );
4281            }
4282        }
4283    }
4284
4285    #[test]
4286    fn test_cast_string_to_timestamp_overflow() {
4287        let array = StringArray::from(vec!["9800-09-08T12:00:00.123456789"]);
4288        let result = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
4289        let result = result.as_primitive::<TimestampSecondType>();
4290        assert_eq!(result.values(), &[247112596800]);
4291    }
4292
4293    #[test]
4294    fn test_cast_string_to_date32() {
4295        let a0 = Arc::new(StringViewArray::from(vec![
4296            Some("2018-12-25"),
4297            Some("Not a valid date"),
4298            None,
4299        ])) as ArrayRef;
4300        let a1 = Arc::new(StringArray::from(vec![
4301            Some("2018-12-25"),
4302            Some("Not a valid date"),
4303            None,
4304        ])) as ArrayRef;
4305        let a2 = Arc::new(LargeStringArray::from(vec![
4306            Some("2018-12-25"),
4307            Some("Not a valid date"),
4308            None,
4309        ])) as ArrayRef;
4310        for array in &[a0, a1, a2] {
4311            let to_type = DataType::Date32;
4312            let b = cast(array, &to_type).unwrap();
4313            let c = b.as_primitive::<Date32Type>();
4314            assert_eq!(17890, c.value(0));
4315            assert!(c.is_null(1));
4316            assert!(c.is_null(2));
4317
4318            let options = CastOptions {
4319                safe: false,
4320                format_options: FormatOptions::default(),
4321            };
4322            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4323            assert_eq!(
4324                err.to_string(),
4325                "Cast error: Cannot cast string 'Not a valid date' to value of Date32 type"
4326            );
4327        }
4328    }
4329
4330    #[test]
4331    fn test_cast_string_with_large_date_to_date32() {
4332        let array = Arc::new(StringArray::from(vec![
4333            Some("+10999-12-31"),
4334            Some("-0010-02-28"),
4335            Some("0010-02-28"),
4336            Some("0000-01-01"),
4337            Some("-0000-01-01"),
4338            Some("-0001-01-01"),
4339        ])) as ArrayRef;
4340        let to_type = DataType::Date32;
4341        let options = CastOptions {
4342            safe: false,
4343            format_options: FormatOptions::default(),
4344        };
4345        let b = cast_with_options(&array, &to_type, &options).unwrap();
4346        let c = b.as_primitive::<Date32Type>();
4347        assert_eq!(3298139, c.value(0)); // 10999-12-31
4348        assert_eq!(-723122, c.value(1)); // -0010-02-28
4349        assert_eq!(-715817, c.value(2)); // 0010-02-28
4350        assert_eq!(c.value(3), c.value(4)); // Expect 0000-01-01 and -0000-01-01 to be parsed the same
4351        assert_eq!(-719528, c.value(3)); // 0000-01-01
4352        assert_eq!(-719528, c.value(4)); // -0000-01-01
4353        assert_eq!(-719893, c.value(5)); // -0001-01-01
4354    }
4355
4356    #[test]
4357    fn test_cast_invalid_string_with_large_date_to_date32() {
4358        // Large dates need to be prefixed with a + or - sign, otherwise they are not parsed correctly
4359        let array = Arc::new(StringArray::from(vec![Some("10999-12-31")])) as ArrayRef;
4360        let to_type = DataType::Date32;
4361        let options = CastOptions {
4362            safe: false,
4363            format_options: FormatOptions::default(),
4364        };
4365        let err = cast_with_options(&array, &to_type, &options).unwrap_err();
4366        assert_eq!(
4367            err.to_string(),
4368            "Cast error: Cannot cast string '10999-12-31' to value of Date32 type"
4369        );
4370    }
4371
4372    #[test]
4373    fn test_cast_string_format_yyyymmdd_to_date32() {
4374        let a0 = Arc::new(StringViewArray::from(vec![
4375            Some("2020-12-25"),
4376            Some("20201117"),
4377        ])) as ArrayRef;
4378        let a1 = Arc::new(StringArray::from(vec![
4379            Some("2020-12-25"),
4380            Some("20201117"),
4381        ])) as ArrayRef;
4382        let a2 = Arc::new(LargeStringArray::from(vec![
4383            Some("2020-12-25"),
4384            Some("20201117"),
4385        ])) as ArrayRef;
4386
4387        for array in &[a0, a1, a2] {
4388            let to_type = DataType::Date32;
4389            let options = CastOptions {
4390                safe: false,
4391                format_options: FormatOptions::default(),
4392            };
4393            let result = cast_with_options(&array, &to_type, &options).unwrap();
4394            let c = result.as_primitive::<Date32Type>();
4395            assert_eq!(
4396                chrono::NaiveDate::from_ymd_opt(2020, 12, 25),
4397                c.value_as_date(0)
4398            );
4399            assert_eq!(
4400                chrono::NaiveDate::from_ymd_opt(2020, 11, 17),
4401                c.value_as_date(1)
4402            );
4403        }
4404    }
4405
4406    #[test]
4407    fn test_cast_string_to_time32second() {
4408        let a0 = Arc::new(StringViewArray::from(vec![
4409            Some("08:08:35.091323414"),
4410            Some("08:08:60.091323414"), // leap second
4411            Some("08:08:61.091323414"), // not valid
4412            Some("Not a valid time"),
4413            None,
4414        ])) as ArrayRef;
4415        let a1 = Arc::new(StringArray::from(vec![
4416            Some("08:08:35.091323414"),
4417            Some("08:08:60.091323414"), // leap second
4418            Some("08:08:61.091323414"), // not valid
4419            Some("Not a valid time"),
4420            None,
4421        ])) as ArrayRef;
4422        let a2 = Arc::new(LargeStringArray::from(vec![
4423            Some("08:08:35.091323414"),
4424            Some("08:08:60.091323414"), // leap second
4425            Some("08:08:61.091323414"), // not valid
4426            Some("Not a valid time"),
4427            None,
4428        ])) as ArrayRef;
4429        for array in &[a0, a1, a2] {
4430            let to_type = DataType::Time32(TimeUnit::Second);
4431            let b = cast(array, &to_type).unwrap();
4432            let c = b.as_primitive::<Time32SecondType>();
4433            assert_eq!(29315, c.value(0));
4434            assert_eq!(29340, c.value(1));
4435            assert!(c.is_null(2));
4436            assert!(c.is_null(3));
4437            assert!(c.is_null(4));
4438
4439            let options = CastOptions {
4440                safe: false,
4441                format_options: FormatOptions::default(),
4442            };
4443            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4444            assert_eq!(err.to_string(), "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(Second) type");
4445        }
4446    }
4447
4448    #[test]
4449    fn test_cast_string_to_time32millisecond() {
4450        let a0 = Arc::new(StringViewArray::from(vec![
4451            Some("08:08:35.091323414"),
4452            Some("08:08:60.091323414"), // leap second
4453            Some("08:08:61.091323414"), // not valid
4454            Some("Not a valid time"),
4455            None,
4456        ])) as ArrayRef;
4457        let a1 = Arc::new(StringArray::from(vec![
4458            Some("08:08:35.091323414"),
4459            Some("08:08:60.091323414"), // leap second
4460            Some("08:08:61.091323414"), // not valid
4461            Some("Not a valid time"),
4462            None,
4463        ])) as ArrayRef;
4464        let a2 = Arc::new(LargeStringArray::from(vec![
4465            Some("08:08:35.091323414"),
4466            Some("08:08:60.091323414"), // leap second
4467            Some("08:08:61.091323414"), // not valid
4468            Some("Not a valid time"),
4469            None,
4470        ])) as ArrayRef;
4471        for array in &[a0, a1, a2] {
4472            let to_type = DataType::Time32(TimeUnit::Millisecond);
4473            let b = cast(array, &to_type).unwrap();
4474            let c = b.as_primitive::<Time32MillisecondType>();
4475            assert_eq!(29315091, c.value(0));
4476            assert_eq!(29340091, c.value(1));
4477            assert!(c.is_null(2));
4478            assert!(c.is_null(3));
4479            assert!(c.is_null(4));
4480
4481            let options = CastOptions {
4482                safe: false,
4483                format_options: FormatOptions::default(),
4484            };
4485            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4486            assert_eq!(err.to_string(), "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(Millisecond) type");
4487        }
4488    }
4489
4490    #[test]
4491    fn test_cast_string_to_time64microsecond() {
4492        let a0 = Arc::new(StringViewArray::from(vec![
4493            Some("08:08:35.091323414"),
4494            Some("Not a valid time"),
4495            None,
4496        ])) as ArrayRef;
4497        let a1 = Arc::new(StringArray::from(vec![
4498            Some("08:08:35.091323414"),
4499            Some("Not a valid time"),
4500            None,
4501        ])) as ArrayRef;
4502        let a2 = Arc::new(LargeStringArray::from(vec![
4503            Some("08:08:35.091323414"),
4504            Some("Not a valid time"),
4505            None,
4506        ])) as ArrayRef;
4507        for array in &[a0, a1, a2] {
4508            let to_type = DataType::Time64(TimeUnit::Microsecond);
4509            let b = cast(array, &to_type).unwrap();
4510            let c = b.as_primitive::<Time64MicrosecondType>();
4511            assert_eq!(29315091323, c.value(0));
4512            assert!(c.is_null(1));
4513            assert!(c.is_null(2));
4514
4515            let options = CastOptions {
4516                safe: false,
4517                format_options: FormatOptions::default(),
4518            };
4519            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4520            assert_eq!(err.to_string(), "Cast error: Cannot cast string 'Not a valid time' to value of Time64(Microsecond) type");
4521        }
4522    }
4523
4524    #[test]
4525    fn test_cast_string_to_time64nanosecond() {
4526        let a0 = Arc::new(StringViewArray::from(vec![
4527            Some("08:08:35.091323414"),
4528            Some("Not a valid time"),
4529            None,
4530        ])) as ArrayRef;
4531        let a1 = Arc::new(StringArray::from(vec![
4532            Some("08:08:35.091323414"),
4533            Some("Not a valid time"),
4534            None,
4535        ])) as ArrayRef;
4536        let a2 = Arc::new(LargeStringArray::from(vec![
4537            Some("08:08:35.091323414"),
4538            Some("Not a valid time"),
4539            None,
4540        ])) as ArrayRef;
4541        for array in &[a0, a1, a2] {
4542            let to_type = DataType::Time64(TimeUnit::Nanosecond);
4543            let b = cast(array, &to_type).unwrap();
4544            let c = b.as_primitive::<Time64NanosecondType>();
4545            assert_eq!(29315091323414, c.value(0));
4546            assert!(c.is_null(1));
4547            assert!(c.is_null(2));
4548
4549            let options = CastOptions {
4550                safe: false,
4551                format_options: FormatOptions::default(),
4552            };
4553            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4554            assert_eq!(err.to_string(), "Cast error: Cannot cast string 'Not a valid time' to value of Time64(Nanosecond) type");
4555        }
4556    }
4557
4558    #[test]
4559    fn test_cast_string_to_date64() {
4560        let a0 = Arc::new(StringViewArray::from(vec![
4561            Some("2020-09-08T12:00:00"),
4562            Some("Not a valid date"),
4563            None,
4564        ])) as ArrayRef;
4565        let a1 = Arc::new(StringArray::from(vec![
4566            Some("2020-09-08T12:00:00"),
4567            Some("Not a valid date"),
4568            None,
4569        ])) as ArrayRef;
4570        let a2 = Arc::new(LargeStringArray::from(vec![
4571            Some("2020-09-08T12:00:00"),
4572            Some("Not a valid date"),
4573            None,
4574        ])) as ArrayRef;
4575        for array in &[a0, a1, a2] {
4576            let to_type = DataType::Date64;
4577            let b = cast(array, &to_type).unwrap();
4578            let c = b.as_primitive::<Date64Type>();
4579            assert_eq!(1599566400000, c.value(0));
4580            assert!(c.is_null(1));
4581            assert!(c.is_null(2));
4582
4583            let options = CastOptions {
4584                safe: false,
4585                format_options: FormatOptions::default(),
4586            };
4587            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4588            assert_eq!(
4589                err.to_string(),
4590                "Cast error: Cannot cast string 'Not a valid date' to value of Date64 type"
4591            );
4592        }
4593    }
4594
4595    macro_rules! test_safe_string_to_interval {
4596        ($data_vec:expr, $interval_unit:expr, $array_ty:ty, $expect_vec:expr) => {
4597            let source_string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
4598
4599            let options = CastOptions {
4600                safe: true,
4601                format_options: FormatOptions::default(),
4602            };
4603
4604            let target_interval_array = cast_with_options(
4605                &source_string_array.clone(),
4606                &DataType::Interval($interval_unit),
4607                &options,
4608            )
4609            .unwrap()
4610            .as_any()
4611            .downcast_ref::<$array_ty>()
4612            .unwrap()
4613            .clone() as $array_ty;
4614
4615            let target_string_array =
4616                cast_with_options(&target_interval_array, &DataType::Utf8, &options)
4617                    .unwrap()
4618                    .as_any()
4619                    .downcast_ref::<StringArray>()
4620                    .unwrap()
4621                    .clone();
4622
4623            let expect_string_array = StringArray::from($expect_vec);
4624
4625            assert_eq!(target_string_array, expect_string_array);
4626
4627            let target_large_string_array =
4628                cast_with_options(&target_interval_array, &DataType::LargeUtf8, &options)
4629                    .unwrap()
4630                    .as_any()
4631                    .downcast_ref::<LargeStringArray>()
4632                    .unwrap()
4633                    .clone();
4634
4635            let expect_large_string_array = LargeStringArray::from($expect_vec);
4636
4637            assert_eq!(target_large_string_array, expect_large_string_array);
4638        };
4639    }
4640
4641    #[test]
4642    fn test_cast_string_to_interval_year_month() {
4643        test_safe_string_to_interval!(
4644            vec![
4645                Some("1 year 1 month"),
4646                Some("1.5 years 13 month"),
4647                Some("30 days"),
4648                Some("31 days"),
4649                Some("2 months 31 days"),
4650                Some("2 months 31 days 1 second"),
4651                Some("foobar"),
4652            ],
4653            IntervalUnit::YearMonth,
4654            IntervalYearMonthArray,
4655            vec![
4656                Some("1 years 1 mons"),
4657                Some("2 years 7 mons"),
4658                None,
4659                None,
4660                None,
4661                None,
4662                None,
4663            ]
4664        );
4665    }
4666
4667    #[test]
4668    fn test_cast_string_to_interval_day_time() {
4669        test_safe_string_to_interval!(
4670            vec![
4671                Some("1 year 1 month"),
4672                Some("1.5 years 13 month"),
4673                Some("30 days"),
4674                Some("1 day 2 second 3.5 milliseconds"),
4675                Some("foobar"),
4676            ],
4677            IntervalUnit::DayTime,
4678            IntervalDayTimeArray,
4679            vec![
4680                Some("390 days"),
4681                Some("930 days"),
4682                Some("30 days"),
4683                None,
4684                None,
4685            ]
4686        );
4687    }
4688
4689    #[test]
4690    fn test_cast_string_to_interval_month_day_nano() {
4691        test_safe_string_to_interval!(
4692            vec![
4693                Some("1 year 1 month 1 day"),
4694                None,
4695                Some("1.5 years 13 month 35 days 1.4 milliseconds"),
4696                Some("3 days"),
4697                Some("8 seconds"),
4698                None,
4699                Some("1 day 29800 milliseconds"),
4700                Some("3 months 1 second"),
4701                Some("6 minutes 120 second"),
4702                Some("2 years 39 months 9 days 19 hours 1 minute 83 seconds 399222 milliseconds"),
4703                Some("foobar"),
4704            ],
4705            IntervalUnit::MonthDayNano,
4706            IntervalMonthDayNanoArray,
4707            vec![
4708                Some("13 mons 1 days"),
4709                None,
4710                Some("31 mons 35 days 0.001400000 secs"),
4711                Some("3 days"),
4712                Some("8.000000000 secs"),
4713                None,
4714                Some("1 days 29.800000000 secs"),
4715                Some("3 mons 1.000000000 secs"),
4716                Some("8 mins"),
4717                Some("63 mons 9 days 19 hours 9 mins 2.222000000 secs"),
4718                None,
4719            ]
4720        );
4721    }
4722
4723    macro_rules! test_unsafe_string_to_interval_err {
4724        ($data_vec:expr, $interval_unit:expr, $error_msg:expr) => {
4725            let string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
4726            let options = CastOptions {
4727                safe: false,
4728                format_options: FormatOptions::default(),
4729            };
4730            let arrow_err = cast_with_options(
4731                &string_array.clone(),
4732                &DataType::Interval($interval_unit),
4733                &options,
4734            )
4735            .unwrap_err();
4736            assert_eq!($error_msg, arrow_err.to_string());
4737        };
4738    }
4739
4740    #[test]
4741    fn test_cast_string_to_interval_err() {
4742        test_unsafe_string_to_interval_err!(
4743            vec![Some("foobar")],
4744            IntervalUnit::YearMonth,
4745            r#"Parser error: Invalid input syntax for type interval: "foobar""#
4746        );
4747        test_unsafe_string_to_interval_err!(
4748            vec![Some("foobar")],
4749            IntervalUnit::DayTime,
4750            r#"Parser error: Invalid input syntax for type interval: "foobar""#
4751        );
4752        test_unsafe_string_to_interval_err!(
4753            vec![Some("foobar")],
4754            IntervalUnit::MonthDayNano,
4755            r#"Parser error: Invalid input syntax for type interval: "foobar""#
4756        );
4757        test_unsafe_string_to_interval_err!(
4758            vec![Some("2 months 31 days 1 second")],
4759            IntervalUnit::YearMonth,
4760            r#"Cast error: Cannot cast 2 months 31 days 1 second to IntervalYearMonth. Only year and month fields are allowed."#
4761        );
4762        test_unsafe_string_to_interval_err!(
4763            vec![Some("1 day 1.5 milliseconds")],
4764            IntervalUnit::DayTime,
4765            r#"Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"#
4766        );
4767
4768        // overflow
4769        test_unsafe_string_to_interval_err!(
4770            vec![Some(format!(
4771                "{} century {} year {} month",
4772                i64::MAX - 2,
4773                i64::MAX - 2,
4774                i64::MAX - 2
4775            ))],
4776            IntervalUnit::DayTime,
4777            format!(
4778                "Arithmetic overflow: Overflow happened on: {} * 100",
4779                i64::MAX - 2
4780            )
4781        );
4782        test_unsafe_string_to_interval_err!(
4783            vec![Some(format!(
4784                "{} year {} month {} day",
4785                i64::MAX - 2,
4786                i64::MAX - 2,
4787                i64::MAX - 2
4788            ))],
4789            IntervalUnit::MonthDayNano,
4790            format!(
4791                "Arithmetic overflow: Overflow happened on: {} * 12",
4792                i64::MAX - 2
4793            )
4794        );
4795    }
4796
4797    #[test]
4798    fn test_cast_binary_to_fixed_size_binary() {
4799        let bytes_1 = "Hiiii".as_bytes();
4800        let bytes_2 = "Hello".as_bytes();
4801
4802        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
4803        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
4804        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
4805
4806        let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
4807        let down_cast = array_ref
4808            .as_any()
4809            .downcast_ref::<FixedSizeBinaryArray>()
4810            .unwrap();
4811        assert_eq!(bytes_1, down_cast.value(0));
4812        assert_eq!(bytes_2, down_cast.value(1));
4813        assert!(down_cast.is_null(2));
4814
4815        let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
4816        let down_cast = array_ref
4817            .as_any()
4818            .downcast_ref::<FixedSizeBinaryArray>()
4819            .unwrap();
4820        assert_eq!(bytes_1, down_cast.value(0));
4821        assert_eq!(bytes_2, down_cast.value(1));
4822        assert!(down_cast.is_null(2));
4823
4824        // test error cases when the length of binary are not same
4825        let bytes_1 = "Hi".as_bytes();
4826        let bytes_2 = "Hello".as_bytes();
4827
4828        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
4829        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
4830        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
4831
4832        let array_ref = cast_with_options(
4833            &a1,
4834            &DataType::FixedSizeBinary(5),
4835            &CastOptions {
4836                safe: false,
4837                format_options: FormatOptions::default(),
4838            },
4839        );
4840        assert!(array_ref.is_err());
4841
4842        let array_ref = cast_with_options(
4843            &a2,
4844            &DataType::FixedSizeBinary(5),
4845            &CastOptions {
4846                safe: false,
4847                format_options: FormatOptions::default(),
4848            },
4849        );
4850        assert!(array_ref.is_err());
4851    }
4852
4853    #[test]
4854    fn test_fixed_size_binary_to_binary() {
4855        let bytes_1 = "Hiiii".as_bytes();
4856        let bytes_2 = "Hello".as_bytes();
4857
4858        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
4859        let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
4860
4861        let array_ref = cast(&a1, &DataType::Binary).unwrap();
4862        let down_cast = array_ref.as_binary::<i32>();
4863        assert_eq!(bytes_1, down_cast.value(0));
4864        assert_eq!(bytes_2, down_cast.value(1));
4865        assert!(down_cast.is_null(2));
4866
4867        let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
4868        let down_cast = array_ref.as_binary::<i64>();
4869        assert_eq!(bytes_1, down_cast.value(0));
4870        assert_eq!(bytes_2, down_cast.value(1));
4871        assert!(down_cast.is_null(2));
4872
4873        let array_ref = cast(&a1, &DataType::BinaryView).unwrap();
4874        let down_cast = array_ref.as_binary_view();
4875        assert_eq!(bytes_1, down_cast.value(0));
4876        assert_eq!(bytes_2, down_cast.value(1));
4877        assert!(down_cast.is_null(2));
4878    }
4879
4880    #[test]
4881    fn test_fixed_size_binary_to_dictionary() {
4882        let bytes_1 = "Hiiii".as_bytes();
4883        let bytes_2 = "Hello".as_bytes();
4884
4885        let binary_data = vec![Some(bytes_1), Some(bytes_2), Some(bytes_1), None];
4886        let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
4887
4888        let cast_type = DataType::Dictionary(
4889            Box::new(DataType::Int8),
4890            Box::new(DataType::FixedSizeBinary(5)),
4891        );
4892        let cast_array = cast(&a1, &cast_type).unwrap();
4893        assert_eq!(cast_array.data_type(), &cast_type);
4894        assert_eq!(
4895            array_to_strings(&cast_array),
4896            vec!["4869696969", "48656c6c6f", "4869696969", "null"]
4897        );
4898        // dictionary should only have two distinct values
4899        let dict_array = cast_array
4900            .as_any()
4901            .downcast_ref::<DictionaryArray<Int8Type>>()
4902            .unwrap();
4903        assert_eq!(dict_array.values().len(), 2);
4904    }
4905
4906    #[test]
4907    fn test_binary_to_dictionary() {
4908        let mut builder = GenericBinaryBuilder::<i32>::new();
4909        builder.append_value(b"hello");
4910        builder.append_value(b"hiiii");
4911        builder.append_value(b"hiiii"); // duplicate
4912        builder.append_null();
4913        builder.append_value(b"rustt");
4914
4915        let a1 = builder.finish();
4916
4917        let cast_type = DataType::Dictionary(
4918            Box::new(DataType::Int8),
4919            Box::new(DataType::FixedSizeBinary(5)),
4920        );
4921        let cast_array = cast(&a1, &cast_type).unwrap();
4922        assert_eq!(cast_array.data_type(), &cast_type);
4923        assert_eq!(
4924            array_to_strings(&cast_array),
4925            vec![
4926                "68656c6c6f",
4927                "6869696969",
4928                "6869696969",
4929                "null",
4930                "7275737474"
4931            ]
4932        );
4933        // dictionary should only have three distinct values
4934        let dict_array = cast_array
4935            .as_any()
4936            .downcast_ref::<DictionaryArray<Int8Type>>()
4937            .unwrap();
4938        assert_eq!(dict_array.values().len(), 3);
4939    }
4940
4941    #[test]
4942    fn test_numeric_to_binary() {
4943        let a = Int16Array::from(vec![Some(1), Some(511), None]);
4944
4945        let array_ref = cast(&a, &DataType::Binary).unwrap();
4946        let down_cast = array_ref.as_binary::<i32>();
4947        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
4948        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
4949        assert!(down_cast.is_null(2));
4950
4951        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
4952
4953        let array_ref = cast(&a, &DataType::Binary).unwrap();
4954        let down_cast = array_ref.as_binary::<i32>();
4955        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
4956        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
4957        assert!(down_cast.is_null(2));
4958    }
4959
4960    #[test]
4961    fn test_numeric_to_large_binary() {
4962        let a = Int16Array::from(vec![Some(1), Some(511), None]);
4963
4964        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
4965        let down_cast = array_ref.as_binary::<i64>();
4966        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
4967        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
4968        assert!(down_cast.is_null(2));
4969
4970        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
4971
4972        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
4973        let down_cast = array_ref.as_binary::<i64>();
4974        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
4975        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
4976        assert!(down_cast.is_null(2));
4977    }
4978
4979    #[test]
4980    fn test_cast_date32_to_int32() {
4981        let array = Date32Array::from(vec![10000, 17890]);
4982        let b = cast(&array, &DataType::Int32).unwrap();
4983        let c = b.as_primitive::<Int32Type>();
4984        assert_eq!(10000, c.value(0));
4985        assert_eq!(17890, c.value(1));
4986    }
4987
4988    #[test]
4989    fn test_cast_int32_to_date32() {
4990        let array = Int32Array::from(vec![10000, 17890]);
4991        let b = cast(&array, &DataType::Date32).unwrap();
4992        let c = b.as_primitive::<Date32Type>();
4993        assert_eq!(10000, c.value(0));
4994        assert_eq!(17890, c.value(1));
4995    }
4996
4997    #[test]
4998    fn test_cast_timestamp_to_date32() {
4999        let array =
5000            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
5001                .with_timezone("+00:00".to_string());
5002        let b = cast(&array, &DataType::Date32).unwrap();
5003        let c = b.as_primitive::<Date32Type>();
5004        assert_eq!(10000, c.value(0));
5005        assert_eq!(17890, c.value(1));
5006        assert!(c.is_null(2));
5007    }
5008    #[test]
5009    fn test_cast_timestamp_to_date32_zone() {
5010        let strings = StringArray::from_iter([
5011            Some("1970-01-01T00:00:01"),
5012            Some("1970-01-01T23:59:59"),
5013            None,
5014            Some("2020-03-01T02:00:23+00:00"),
5015        ]);
5016        let dt = DataType::Timestamp(TimeUnit::Millisecond, Some("-07:00".into()));
5017        let timestamps = cast(&strings, &dt).unwrap();
5018        let dates = cast(timestamps.as_ref(), &DataType::Date32).unwrap();
5019
5020        let c = dates.as_primitive::<Date32Type>();
5021        let expected = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
5022        assert_eq!(c.value_as_date(0).unwrap(), expected);
5023        assert_eq!(c.value_as_date(1).unwrap(), expected);
5024        assert!(c.is_null(2));
5025        let expected = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
5026        assert_eq!(c.value_as_date(3).unwrap(), expected);
5027    }
5028    #[test]
5029    fn test_cast_timestamp_to_date64() {
5030        let array =
5031            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None]);
5032        let b = cast(&array, &DataType::Date64).unwrap();
5033        let c = b.as_primitive::<Date64Type>();
5034        assert_eq!(864000000005, c.value(0));
5035        assert_eq!(1545696000001, c.value(1));
5036        assert!(c.is_null(2));
5037
5038        let array = TimestampSecondArray::from(vec![Some(864000000005), Some(1545696000001)]);
5039        let b = cast(&array, &DataType::Date64).unwrap();
5040        let c = b.as_primitive::<Date64Type>();
5041        assert_eq!(864000000005000, c.value(0));
5042        assert_eq!(1545696000001000, c.value(1));
5043
5044        // test overflow, safe cast
5045        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
5046        let b = cast(&array, &DataType::Date64).unwrap();
5047        assert!(b.is_null(0));
5048        // test overflow, unsafe cast
5049        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
5050        let options = CastOptions {
5051            safe: false,
5052            format_options: FormatOptions::default(),
5053        };
5054        let b = cast_with_options(&array, &DataType::Date64, &options);
5055        assert!(b.is_err());
5056    }
5057
5058    #[test]
5059    fn test_cast_timestamp_to_time64() {
5060        // test timestamp secs
5061        let array = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
5062            .with_timezone("+01:00".to_string());
5063        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5064        let c = b.as_primitive::<Time64MicrosecondType>();
5065        assert_eq!(3605000000, c.value(0));
5066        assert_eq!(3601000000, c.value(1));
5067        assert!(c.is_null(2));
5068        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5069        let c = b.as_primitive::<Time64NanosecondType>();
5070        assert_eq!(3605000000000, c.value(0));
5071        assert_eq!(3601000000000, c.value(1));
5072        assert!(c.is_null(2));
5073
5074        // test timestamp milliseconds
5075        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
5076            .with_timezone("+01:00".to_string());
5077        let array = Arc::new(a) as ArrayRef;
5078        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5079        let c = b.as_primitive::<Time64MicrosecondType>();
5080        assert_eq!(3605000000, c.value(0));
5081        assert_eq!(3601000000, c.value(1));
5082        assert!(c.is_null(2));
5083        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5084        let c = b.as_primitive::<Time64NanosecondType>();
5085        assert_eq!(3605000000000, c.value(0));
5086        assert_eq!(3601000000000, c.value(1));
5087        assert!(c.is_null(2));
5088
5089        // test timestamp microseconds
5090        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
5091            .with_timezone("+01:00".to_string());
5092        let array = Arc::new(a) as ArrayRef;
5093        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5094        let c = b.as_primitive::<Time64MicrosecondType>();
5095        assert_eq!(3605000000, c.value(0));
5096        assert_eq!(3601000000, c.value(1));
5097        assert!(c.is_null(2));
5098        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5099        let c = b.as_primitive::<Time64NanosecondType>();
5100        assert_eq!(3605000000000, c.value(0));
5101        assert_eq!(3601000000000, c.value(1));
5102        assert!(c.is_null(2));
5103
5104        // test timestamp nanoseconds
5105        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
5106            .with_timezone("+01:00".to_string());
5107        let array = Arc::new(a) as ArrayRef;
5108        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5109        let c = b.as_primitive::<Time64MicrosecondType>();
5110        assert_eq!(3605000000, c.value(0));
5111        assert_eq!(3601000000, c.value(1));
5112        assert!(c.is_null(2));
5113        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5114        let c = b.as_primitive::<Time64NanosecondType>();
5115        assert_eq!(3605000000000, c.value(0));
5116        assert_eq!(3601000000000, c.value(1));
5117        assert!(c.is_null(2));
5118
5119        // test overflow
5120        let a =
5121            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
5122        let array = Arc::new(a) as ArrayRef;
5123        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond));
5124        assert!(b.is_err());
5125        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond));
5126        assert!(b.is_err());
5127        let b = cast(&array, &DataType::Time64(TimeUnit::Millisecond));
5128        assert!(b.is_err());
5129    }
5130
5131    #[test]
5132    fn test_cast_timestamp_to_time32() {
5133        // test timestamp secs
5134        let a = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
5135            .with_timezone("+01:00".to_string());
5136        let array = Arc::new(a) as ArrayRef;
5137        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5138        let c = b.as_primitive::<Time32SecondType>();
5139        assert_eq!(3605, c.value(0));
5140        assert_eq!(3601, c.value(1));
5141        assert!(c.is_null(2));
5142        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5143        let c = b.as_primitive::<Time32MillisecondType>();
5144        assert_eq!(3605000, c.value(0));
5145        assert_eq!(3601000, c.value(1));
5146        assert!(c.is_null(2));
5147
5148        // test timestamp milliseconds
5149        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
5150            .with_timezone("+01:00".to_string());
5151        let array = Arc::new(a) as ArrayRef;
5152        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5153        let c = b.as_primitive::<Time32SecondType>();
5154        assert_eq!(3605, c.value(0));
5155        assert_eq!(3601, c.value(1));
5156        assert!(c.is_null(2));
5157        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5158        let c = b.as_primitive::<Time32MillisecondType>();
5159        assert_eq!(3605000, c.value(0));
5160        assert_eq!(3601000, c.value(1));
5161        assert!(c.is_null(2));
5162
5163        // test timestamp microseconds
5164        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
5165            .with_timezone("+01:00".to_string());
5166        let array = Arc::new(a) as ArrayRef;
5167        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5168        let c = b.as_primitive::<Time32SecondType>();
5169        assert_eq!(3605, c.value(0));
5170        assert_eq!(3601, c.value(1));
5171        assert!(c.is_null(2));
5172        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5173        let c = b.as_primitive::<Time32MillisecondType>();
5174        assert_eq!(3605000, c.value(0));
5175        assert_eq!(3601000, c.value(1));
5176        assert!(c.is_null(2));
5177
5178        // test timestamp nanoseconds
5179        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
5180            .with_timezone("+01:00".to_string());
5181        let array = Arc::new(a) as ArrayRef;
5182        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5183        let c = b.as_primitive::<Time32SecondType>();
5184        assert_eq!(3605, c.value(0));
5185        assert_eq!(3601, c.value(1));
5186        assert!(c.is_null(2));
5187        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5188        let c = b.as_primitive::<Time32MillisecondType>();
5189        assert_eq!(3605000, c.value(0));
5190        assert_eq!(3601000, c.value(1));
5191        assert!(c.is_null(2));
5192
5193        // test overflow
5194        let a =
5195            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
5196        let array = Arc::new(a) as ArrayRef;
5197        let b = cast(&array, &DataType::Time32(TimeUnit::Second));
5198        assert!(b.is_err());
5199        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond));
5200        assert!(b.is_err());
5201    }
5202
5203    // Cast Timestamp(_, None) -> Timestamp(_, Some(timezone))
5204    #[test]
5205    fn test_cast_timestamp_with_timezone_1() {
5206        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5207            Some("2000-01-01T00:00:00.123456789"),
5208            Some("2010-01-01T00:00:00.123456789"),
5209            None,
5210        ]));
5211        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
5212        let timestamp_array = cast(&string_array, &to_type).unwrap();
5213
5214        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
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.123456+07:00", result.value(0));
5220        assert_eq!("2010-01-01T00:00:00.123456+07:00", result.value(1));
5221        assert!(result.is_null(2));
5222    }
5223
5224    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, None)
5225    #[test]
5226    fn test_cast_timestamp_with_timezone_2() {
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::Millisecond, 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.123+07:00", result.value(0));
5239        assert_eq!("2010-01-01T07:00:00.123+07:00", result.value(1));
5240        assert!(result.is_null(2));
5241
5242        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
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!("2000-01-01T00:00:00.123", result.value(0));
5248        assert_eq!("2010-01-01T00:00:00.123", result.value(1));
5249        assert!(result.is_null(2));
5250    }
5251
5252    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, Some(timezone))
5253    #[test]
5254    fn test_cast_timestamp_with_timezone_3() {
5255        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5256            Some("2000-01-01T07:00:00.123456789"),
5257            Some("2010-01-01T07:00:00.123456789"),
5258            None,
5259        ]));
5260        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
5261        let timestamp_array = cast(&string_array, &to_type).unwrap();
5262
5263        // Check intermediate representation is correct
5264        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5265        let result = string_array.as_string::<i32>();
5266        assert_eq!("2000-01-01T07:00:00.123456+07:00", result.value(0));
5267        assert_eq!("2010-01-01T07:00:00.123456+07:00", result.value(1));
5268        assert!(result.is_null(2));
5269
5270        let to_type = DataType::Timestamp(TimeUnit::Second, Some("-08:00".into()));
5271        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
5272
5273        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5274        let result = string_array.as_string::<i32>();
5275        assert_eq!("1999-12-31T16:00:00-08:00", result.value(0));
5276        assert_eq!("2009-12-31T16:00:00-08:00", result.value(1));
5277        assert!(result.is_null(2));
5278    }
5279
5280    #[test]
5281    fn test_cast_date64_to_timestamp() {
5282        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5283        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5284        let c = b.as_primitive::<TimestampSecondType>();
5285        assert_eq!(864000000, c.value(0));
5286        assert_eq!(1545696000, c.value(1));
5287        assert!(c.is_null(2));
5288    }
5289
5290    #[test]
5291    fn test_cast_date64_to_timestamp_ms() {
5292        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5293        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
5294        let c = b
5295            .as_any()
5296            .downcast_ref::<TimestampMillisecondArray>()
5297            .unwrap();
5298        assert_eq!(864000000005, c.value(0));
5299        assert_eq!(1545696000001, c.value(1));
5300        assert!(c.is_null(2));
5301    }
5302
5303    #[test]
5304    fn test_cast_date64_to_timestamp_us() {
5305        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5306        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5307        let c = b
5308            .as_any()
5309            .downcast_ref::<TimestampMicrosecondArray>()
5310            .unwrap();
5311        assert_eq!(864000000005000, c.value(0));
5312        assert_eq!(1545696000001000, c.value(1));
5313        assert!(c.is_null(2));
5314    }
5315
5316    #[test]
5317    fn test_cast_date64_to_timestamp_ns() {
5318        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5319        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
5320        let c = b
5321            .as_any()
5322            .downcast_ref::<TimestampNanosecondArray>()
5323            .unwrap();
5324        assert_eq!(864000000005000000, c.value(0));
5325        assert_eq!(1545696000001000000, c.value(1));
5326        assert!(c.is_null(2));
5327    }
5328
5329    #[test]
5330    fn test_cast_timestamp_to_i64() {
5331        let array =
5332            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
5333                .with_timezone("UTC".to_string());
5334        let b = cast(&array, &DataType::Int64).unwrap();
5335        let c = b.as_primitive::<Int64Type>();
5336        assert_eq!(&DataType::Int64, c.data_type());
5337        assert_eq!(864000000005, c.value(0));
5338        assert_eq!(1545696000001, c.value(1));
5339        assert!(c.is_null(2));
5340    }
5341
5342    #[test]
5343    fn test_cast_date32_to_string() {
5344        let array = Date32Array::from(vec![10000, 17890]);
5345        let b = cast(&array, &DataType::Utf8).unwrap();
5346        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
5347        assert_eq!(&DataType::Utf8, c.data_type());
5348        assert_eq!("1997-05-19", c.value(0));
5349        assert_eq!("2018-12-25", c.value(1));
5350    }
5351
5352    #[test]
5353    fn test_cast_date64_to_string() {
5354        let array = Date64Array::from(vec![10000 * 86400000, 17890 * 86400000]);
5355        let b = cast(&array, &DataType::Utf8).unwrap();
5356        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
5357        assert_eq!(&DataType::Utf8, c.data_type());
5358        assert_eq!("1997-05-19T00:00:00", c.value(0));
5359        assert_eq!("2018-12-25T00:00:00", c.value(1));
5360    }
5361
5362    macro_rules! assert_cast_timestamp_to_string {
5363        ($array:expr, $datatype:expr, $output_array_type: ty, $expected:expr) => {{
5364            let out = cast(&$array, &$datatype).unwrap();
5365            let actual = out
5366                .as_any()
5367                .downcast_ref::<$output_array_type>()
5368                .unwrap()
5369                .into_iter()
5370                .collect::<Vec<_>>();
5371            assert_eq!(actual, $expected);
5372        }};
5373        ($array:expr, $datatype:expr, $output_array_type: ty, $options:expr, $expected:expr) => {{
5374            let out = cast_with_options(&$array, &$datatype, &$options).unwrap();
5375            let actual = out
5376                .as_any()
5377                .downcast_ref::<$output_array_type>()
5378                .unwrap()
5379                .into_iter()
5380                .collect::<Vec<_>>();
5381            assert_eq!(actual, $expected);
5382        }};
5383    }
5384
5385    #[test]
5386    fn test_cast_date32_to_timestamp_and_timestamp_with_timezone() {
5387        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5388        let a = Date32Array::from(vec![Some(18628), None, None]); // 2021-1-1, 2022-1-1
5389        let array = Arc::new(a) as ArrayRef;
5390
5391        let b = cast(
5392            &array,
5393            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
5394        )
5395        .unwrap();
5396        let c = b.as_primitive::<TimestampSecondType>();
5397        let string_array = cast(&c, &DataType::Utf8).unwrap();
5398        let result = string_array.as_string::<i32>();
5399        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5400
5401        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5402        let c = b.as_primitive::<TimestampSecondType>();
5403        let string_array = cast(&c, &DataType::Utf8).unwrap();
5404        let result = string_array.as_string::<i32>();
5405        assert_eq!("2021-01-01T00:00:00", result.value(0));
5406    }
5407
5408    #[test]
5409    fn test_cast_date32_to_timestamp_with_timezone() {
5410        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5411        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
5412        let array = Arc::new(a) as ArrayRef;
5413        let b = cast(
5414            &array,
5415            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
5416        )
5417        .unwrap();
5418        let c = b.as_primitive::<TimestampSecondType>();
5419        assert_eq!(1609438500, c.value(0));
5420        assert_eq!(1640974500, c.value(1));
5421        assert!(c.is_null(2));
5422
5423        let string_array = cast(&c, &DataType::Utf8).unwrap();
5424        let result = string_array.as_string::<i32>();
5425        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5426        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
5427    }
5428
5429    #[test]
5430    fn test_cast_date32_to_timestamp_with_timezone_ms() {
5431        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5432        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
5433        let array = Arc::new(a) as ArrayRef;
5434        let b = cast(
5435            &array,
5436            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
5437        )
5438        .unwrap();
5439        let c = b.as_primitive::<TimestampMillisecondType>();
5440        assert_eq!(1609438500000, c.value(0));
5441        assert_eq!(1640974500000, c.value(1));
5442        assert!(c.is_null(2));
5443
5444        let string_array = cast(&c, &DataType::Utf8).unwrap();
5445        let result = string_array.as_string::<i32>();
5446        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5447        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
5448    }
5449
5450    #[test]
5451    fn test_cast_date32_to_timestamp_with_timezone_us() {
5452        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5453        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
5454        let array = Arc::new(a) as ArrayRef;
5455        let b = cast(
5456            &array,
5457            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
5458        )
5459        .unwrap();
5460        let c = b.as_primitive::<TimestampMicrosecondType>();
5461        assert_eq!(1609438500000000, c.value(0));
5462        assert_eq!(1640974500000000, c.value(1));
5463        assert!(c.is_null(2));
5464
5465        let string_array = cast(&c, &DataType::Utf8).unwrap();
5466        let result = string_array.as_string::<i32>();
5467        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5468        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
5469    }
5470
5471    #[test]
5472    fn test_cast_date32_to_timestamp_with_timezone_ns() {
5473        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5474        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
5475        let array = Arc::new(a) as ArrayRef;
5476        let b = cast(
5477            &array,
5478            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
5479        )
5480        .unwrap();
5481        let c = b.as_primitive::<TimestampNanosecondType>();
5482        assert_eq!(1609438500000000000, c.value(0));
5483        assert_eq!(1640974500000000000, c.value(1));
5484        assert!(c.is_null(2));
5485
5486        let string_array = cast(&c, &DataType::Utf8).unwrap();
5487        let result = string_array.as_string::<i32>();
5488        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
5489        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
5490    }
5491
5492    #[test]
5493    fn test_cast_date64_to_timestamp_with_timezone() {
5494        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5495        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5496        let b = cast(
5497            &array,
5498            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
5499        )
5500        .unwrap();
5501
5502        let c = b.as_primitive::<TimestampSecondType>();
5503        assert_eq!(863979300, c.value(0));
5504        assert_eq!(1545675300, c.value(1));
5505        assert!(c.is_null(2));
5506
5507        let string_array = cast(&c, &DataType::Utf8).unwrap();
5508        let result = string_array.as_string::<i32>();
5509        assert_eq!("1997-05-19T00:00:00+05:45", result.value(0));
5510        assert_eq!("2018-12-25T00:00:00+05:45", result.value(1));
5511    }
5512
5513    #[test]
5514    fn test_cast_date64_to_timestamp_with_timezone_ms() {
5515        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5516        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5517        let b = cast(
5518            &array,
5519            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
5520        )
5521        .unwrap();
5522
5523        let c = b.as_primitive::<TimestampMillisecondType>();
5524        assert_eq!(863979300005, c.value(0));
5525        assert_eq!(1545675300001, c.value(1));
5526        assert!(c.is_null(2));
5527
5528        let string_array = cast(&c, &DataType::Utf8).unwrap();
5529        let result = string_array.as_string::<i32>();
5530        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
5531        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
5532    }
5533
5534    #[test]
5535    fn test_cast_date64_to_timestamp_with_timezone_us() {
5536        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5537        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5538        let b = cast(
5539            &array,
5540            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
5541        )
5542        .unwrap();
5543
5544        let c = b.as_primitive::<TimestampMicrosecondType>();
5545        assert_eq!(863979300005000, c.value(0));
5546        assert_eq!(1545675300001000, c.value(1));
5547        assert!(c.is_null(2));
5548
5549        let string_array = cast(&c, &DataType::Utf8).unwrap();
5550        let result = string_array.as_string::<i32>();
5551        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
5552        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
5553    }
5554
5555    #[test]
5556    fn test_cast_date64_to_timestamp_with_timezone_ns() {
5557        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5558        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5559        let b = cast(
5560            &array,
5561            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
5562        )
5563        .unwrap();
5564
5565        let c = b.as_primitive::<TimestampNanosecondType>();
5566        assert_eq!(863979300005000000, c.value(0));
5567        assert_eq!(1545675300001000000, c.value(1));
5568        assert!(c.is_null(2));
5569
5570        let string_array = cast(&c, &DataType::Utf8).unwrap();
5571        let result = string_array.as_string::<i32>();
5572        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
5573        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
5574    }
5575
5576    #[test]
5577    fn test_cast_timestamp_to_strings() {
5578        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
5579        let array =
5580            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
5581        let expected = vec![
5582            Some("1997-05-19T00:00:03.005"),
5583            Some("2018-12-25T00:00:02.001"),
5584            None,
5585        ];
5586
5587        assert_cast_timestamp_to_string!(array, DataType::Utf8View, StringViewArray, expected);
5588        assert_cast_timestamp_to_string!(array, DataType::Utf8, StringArray, expected);
5589        assert_cast_timestamp_to_string!(array, DataType::LargeUtf8, LargeStringArray, expected);
5590    }
5591
5592    #[test]
5593    fn test_cast_timestamp_to_strings_opt() {
5594        let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
5595        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5596        let cast_options = CastOptions {
5597            safe: true,
5598            format_options: FormatOptions::default()
5599                .with_timestamp_format(Some(ts_format))
5600                .with_timestamp_tz_format(Some(ts_format)),
5601        };
5602
5603        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
5604        let array_without_tz =
5605            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
5606        let expected = vec![
5607            Some("1997-05-19 00:00:03.005000"),
5608            Some("2018-12-25 00:00:02.001000"),
5609            None,
5610        ];
5611        assert_cast_timestamp_to_string!(
5612            array_without_tz,
5613            DataType::Utf8View,
5614            StringViewArray,
5615            cast_options,
5616            expected
5617        );
5618        assert_cast_timestamp_to_string!(
5619            array_without_tz,
5620            DataType::Utf8,
5621            StringArray,
5622            cast_options,
5623            expected
5624        );
5625        assert_cast_timestamp_to_string!(
5626            array_without_tz,
5627            DataType::LargeUtf8,
5628            LargeStringArray,
5629            cast_options,
5630            expected
5631        );
5632
5633        let array_with_tz =
5634            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None])
5635                .with_timezone(tz.to_string());
5636        let expected = vec![
5637            Some("1997-05-19 05:45:03.005000"),
5638            Some("2018-12-25 05:45:02.001000"),
5639            None,
5640        ];
5641        assert_cast_timestamp_to_string!(
5642            array_with_tz,
5643            DataType::Utf8View,
5644            StringViewArray,
5645            cast_options,
5646            expected
5647        );
5648        assert_cast_timestamp_to_string!(
5649            array_with_tz,
5650            DataType::Utf8,
5651            StringArray,
5652            cast_options,
5653            expected
5654        );
5655        assert_cast_timestamp_to_string!(
5656            array_with_tz,
5657            DataType::LargeUtf8,
5658            LargeStringArray,
5659            cast_options,
5660            expected
5661        );
5662    }
5663
5664    #[test]
5665    fn test_cast_between_timestamps() {
5666        let array =
5667            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
5668        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5669        let c = b.as_primitive::<TimestampSecondType>();
5670        assert_eq!(864000003, c.value(0));
5671        assert_eq!(1545696002, c.value(1));
5672        assert!(c.is_null(2));
5673    }
5674
5675    #[test]
5676    fn test_cast_duration_to_i64() {
5677        let base = vec![5, 6, 7, 8, 100000000];
5678
5679        let duration_arrays = vec![
5680            Arc::new(DurationNanosecondArray::from(base.clone())) as ArrayRef,
5681            Arc::new(DurationMicrosecondArray::from(base.clone())) as ArrayRef,
5682            Arc::new(DurationMillisecondArray::from(base.clone())) as ArrayRef,
5683            Arc::new(DurationSecondArray::from(base.clone())) as ArrayRef,
5684        ];
5685
5686        for arr in duration_arrays {
5687            assert!(can_cast_types(arr.data_type(), &DataType::Int64));
5688            let result = cast(&arr, &DataType::Int64).unwrap();
5689            let result = result.as_primitive::<Int64Type>();
5690            assert_eq!(base.as_slice(), result.values());
5691        }
5692    }
5693
5694    #[test]
5695    fn test_cast_between_durations_and_numerics() {
5696        fn test_cast_between_durations<FromType, ToType>()
5697        where
5698            FromType: ArrowPrimitiveType<Native = i64>,
5699            ToType: ArrowPrimitiveType<Native = i64>,
5700            PrimitiveArray<FromType>: From<Vec<Option<i64>>>,
5701        {
5702            let from_unit = match FromType::DATA_TYPE {
5703                DataType::Duration(unit) => unit,
5704                _ => panic!("Expected a duration type"),
5705            };
5706            let to_unit = match ToType::DATA_TYPE {
5707                DataType::Duration(unit) => unit,
5708                _ => panic!("Expected a duration type"),
5709            };
5710            let from_size = time_unit_multiple(&from_unit);
5711            let to_size = time_unit_multiple(&to_unit);
5712
5713            let (v1_before, v2_before) = (8640003005, 1696002001);
5714            let (v1_after, v2_after) = if from_size >= to_size {
5715                (
5716                    v1_before / (from_size / to_size),
5717                    v2_before / (from_size / to_size),
5718                )
5719            } else {
5720                (
5721                    v1_before * (to_size / from_size),
5722                    v2_before * (to_size / from_size),
5723                )
5724            };
5725
5726            let array =
5727                PrimitiveArray::<FromType>::from(vec![Some(v1_before), Some(v2_before), None]);
5728            let b = cast(&array, &ToType::DATA_TYPE).unwrap();
5729            let c = b.as_primitive::<ToType>();
5730            assert_eq!(v1_after, c.value(0));
5731            assert_eq!(v2_after, c.value(1));
5732            assert!(c.is_null(2));
5733        }
5734
5735        // between each individual duration type
5736        test_cast_between_durations::<DurationSecondType, DurationMillisecondType>();
5737        test_cast_between_durations::<DurationSecondType, DurationMicrosecondType>();
5738        test_cast_between_durations::<DurationSecondType, DurationNanosecondType>();
5739        test_cast_between_durations::<DurationMillisecondType, DurationSecondType>();
5740        test_cast_between_durations::<DurationMillisecondType, DurationMicrosecondType>();
5741        test_cast_between_durations::<DurationMillisecondType, DurationNanosecondType>();
5742        test_cast_between_durations::<DurationMicrosecondType, DurationSecondType>();
5743        test_cast_between_durations::<DurationMicrosecondType, DurationMillisecondType>();
5744        test_cast_between_durations::<DurationMicrosecondType, DurationNanosecondType>();
5745        test_cast_between_durations::<DurationNanosecondType, DurationSecondType>();
5746        test_cast_between_durations::<DurationNanosecondType, DurationMillisecondType>();
5747        test_cast_between_durations::<DurationNanosecondType, DurationMicrosecondType>();
5748
5749        // cast failed
5750        let array = DurationSecondArray::from(vec![
5751            Some(i64::MAX),
5752            Some(8640203410378005),
5753            Some(10241096),
5754            None,
5755        ]);
5756        let b = cast(&array, &DataType::Duration(TimeUnit::Nanosecond)).unwrap();
5757        let c = b.as_primitive::<DurationNanosecondType>();
5758        assert!(c.is_null(0));
5759        assert!(c.is_null(1));
5760        assert_eq!(10241096000000000, c.value(2));
5761        assert!(c.is_null(3));
5762
5763        // durations to numerics
5764        let array = DurationSecondArray::from(vec![
5765            Some(i64::MAX),
5766            Some(8640203410378005),
5767            Some(10241096),
5768            None,
5769        ]);
5770        let b = cast(&array, &DataType::Int64).unwrap();
5771        let c = b.as_primitive::<Int64Type>();
5772        assert_eq!(i64::MAX, c.value(0));
5773        assert_eq!(8640203410378005, c.value(1));
5774        assert_eq!(10241096, c.value(2));
5775        assert!(c.is_null(3));
5776
5777        let b = cast(&array, &DataType::Int32).unwrap();
5778        let c = b.as_primitive::<Int32Type>();
5779        assert_eq!(0, c.value(0));
5780        assert_eq!(0, c.value(1));
5781        assert_eq!(10241096, c.value(2));
5782        assert!(c.is_null(3));
5783
5784        // numerics to durations
5785        let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), Some(10241096), None]);
5786        let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
5787        let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
5788        assert_eq!(i32::MAX as i64, c.value(0));
5789        assert_eq!(802034103, c.value(1));
5790        assert_eq!(10241096, c.value(2));
5791        assert!(c.is_null(3));
5792    }
5793
5794    #[test]
5795    fn test_cast_to_strings() {
5796        let a = Int32Array::from(vec![1, 2, 3]);
5797        let out = cast(&a, &DataType::Utf8).unwrap();
5798        let out = out
5799            .as_any()
5800            .downcast_ref::<StringArray>()
5801            .unwrap()
5802            .into_iter()
5803            .collect::<Vec<_>>();
5804        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
5805        let out = cast(&a, &DataType::LargeUtf8).unwrap();
5806        let out = out
5807            .as_any()
5808            .downcast_ref::<LargeStringArray>()
5809            .unwrap()
5810            .into_iter()
5811            .collect::<Vec<_>>();
5812        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
5813    }
5814
5815    #[test]
5816    fn test_str_to_str_casts() {
5817        for data in [
5818            vec![Some("foo"), Some("bar"), Some("ham")],
5819            vec![Some("foo"), None, Some("bar")],
5820        ] {
5821            let a = LargeStringArray::from(data.clone());
5822            let to = cast(&a, &DataType::Utf8).unwrap();
5823            let expect = a
5824                .as_any()
5825                .downcast_ref::<LargeStringArray>()
5826                .unwrap()
5827                .into_iter()
5828                .collect::<Vec<_>>();
5829            let out = to
5830                .as_any()
5831                .downcast_ref::<StringArray>()
5832                .unwrap()
5833                .into_iter()
5834                .collect::<Vec<_>>();
5835            assert_eq!(expect, out);
5836
5837            let a = StringArray::from(data);
5838            let to = cast(&a, &DataType::LargeUtf8).unwrap();
5839            let expect = a
5840                .as_any()
5841                .downcast_ref::<StringArray>()
5842                .unwrap()
5843                .into_iter()
5844                .collect::<Vec<_>>();
5845            let out = to
5846                .as_any()
5847                .downcast_ref::<LargeStringArray>()
5848                .unwrap()
5849                .into_iter()
5850                .collect::<Vec<_>>();
5851            assert_eq!(expect, out);
5852        }
5853    }
5854
5855    const VIEW_TEST_DATA: [Option<&str>; 5] = [
5856        Some("hello"),
5857        Some("repeated"),
5858        None,
5859        Some("large payload over 12 bytes"),
5860        Some("repeated"),
5861    ];
5862
5863    #[test]
5864    fn test_string_view_to_binary_view() {
5865        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
5866
5867        assert!(can_cast_types(
5868            string_view_array.data_type(),
5869            &DataType::BinaryView
5870        ));
5871
5872        let binary_view_array = cast(&string_view_array, &DataType::BinaryView).unwrap();
5873        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
5874
5875        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
5876        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
5877    }
5878
5879    #[test]
5880    fn test_binary_view_to_string_view() {
5881        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
5882
5883        assert!(can_cast_types(
5884            binary_view_array.data_type(),
5885            &DataType::Utf8View
5886        ));
5887
5888        let string_view_array = cast(&binary_view_array, &DataType::Utf8View).unwrap();
5889        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
5890
5891        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
5892        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
5893    }
5894
5895    #[test]
5896    fn test_string_to_view() {
5897        _test_string_to_view::<i32>();
5898        _test_string_to_view::<i64>();
5899    }
5900
5901    fn _test_string_to_view<O>()
5902    where
5903        O: OffsetSizeTrait,
5904    {
5905        let string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
5906
5907        assert!(can_cast_types(
5908            string_array.data_type(),
5909            &DataType::Utf8View
5910        ));
5911
5912        assert!(can_cast_types(
5913            string_array.data_type(),
5914            &DataType::BinaryView
5915        ));
5916
5917        let string_view_array = cast(&string_array, &DataType::Utf8View).unwrap();
5918        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
5919
5920        let binary_view_array = cast(&string_array, &DataType::BinaryView).unwrap();
5921        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
5922
5923        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
5924        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
5925
5926        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
5927        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
5928    }
5929
5930    #[test]
5931    fn test_bianry_to_view() {
5932        _test_binary_to_view::<i32>();
5933        _test_binary_to_view::<i64>();
5934    }
5935
5936    fn _test_binary_to_view<O>()
5937    where
5938        O: OffsetSizeTrait,
5939    {
5940        let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
5941
5942        assert!(can_cast_types(
5943            binary_array.data_type(),
5944            &DataType::Utf8View
5945        ));
5946
5947        assert!(can_cast_types(
5948            binary_array.data_type(),
5949            &DataType::BinaryView
5950        ));
5951
5952        let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
5953        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
5954
5955        let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
5956        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
5957
5958        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
5959        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
5960
5961        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
5962        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
5963    }
5964
5965    #[test]
5966    fn test_dict_to_view() {
5967        let values = StringArray::from_iter(VIEW_TEST_DATA);
5968        let keys = Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
5969        let string_dict_array =
5970            DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
5971        let typed_dict = string_dict_array.downcast_dict::<StringArray>().unwrap();
5972
5973        let string_view_array = {
5974            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
5975            for v in typed_dict.into_iter() {
5976                builder.append_option(v);
5977            }
5978            builder.finish()
5979        };
5980        let expected_string_array_type = string_view_array.data_type();
5981        let casted_string_array = cast(&string_dict_array, expected_string_array_type).unwrap();
5982        assert_eq!(casted_string_array.data_type(), expected_string_array_type);
5983        assert_eq!(casted_string_array.as_ref(), &string_view_array);
5984
5985        let binary_buffer = cast(&typed_dict.values(), &DataType::Binary).unwrap();
5986        let binary_dict_array =
5987            DictionaryArray::<Int8Type>::new(typed_dict.keys().clone(), binary_buffer);
5988        let typed_binary_dict = binary_dict_array.downcast_dict::<BinaryArray>().unwrap();
5989
5990        let binary_view_array = {
5991            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
5992            for v in typed_binary_dict.into_iter() {
5993                builder.append_option(v);
5994            }
5995            builder.finish()
5996        };
5997        let expected_binary_array_type = binary_view_array.data_type();
5998        let casted_binary_array = cast(&binary_dict_array, expected_binary_array_type).unwrap();
5999        assert_eq!(casted_binary_array.data_type(), expected_binary_array_type);
6000        assert_eq!(casted_binary_array.as_ref(), &binary_view_array);
6001    }
6002
6003    #[test]
6004    fn test_view_to_dict() {
6005        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6006        let string_dict_array: DictionaryArray<Int8Type> = VIEW_TEST_DATA.into_iter().collect();
6007        let casted_type = string_dict_array.data_type();
6008        let casted_dict_array = cast(&string_view_array, casted_type).unwrap();
6009        assert_eq!(casted_dict_array.data_type(), casted_type);
6010        assert_eq!(casted_dict_array.as_ref(), &string_dict_array);
6011
6012        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6013        let binary_dict_array = string_dict_array.downcast_dict::<StringArray>().unwrap();
6014        let binary_buffer = cast(&binary_dict_array.values(), &DataType::Binary).unwrap();
6015        let binary_dict_array =
6016            DictionaryArray::<Int8Type>::new(binary_dict_array.keys().clone(), binary_buffer);
6017        let casted_type = binary_dict_array.data_type();
6018        let casted_binary_array = cast(&binary_view_array, casted_type).unwrap();
6019        assert_eq!(casted_binary_array.data_type(), casted_type);
6020        assert_eq!(casted_binary_array.as_ref(), &binary_dict_array);
6021    }
6022
6023    #[test]
6024    fn test_view_to_string() {
6025        _test_view_to_string::<i32>();
6026        _test_view_to_string::<i64>();
6027    }
6028
6029    fn _test_view_to_string<O>()
6030    where
6031        O: OffsetSizeTrait,
6032    {
6033        let string_view_array = {
6034            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
6035            for s in VIEW_TEST_DATA.iter() {
6036                builder.append_option(*s);
6037            }
6038            builder.finish()
6039        };
6040
6041        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6042
6043        let expected_string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
6044        let expected_type = expected_string_array.data_type();
6045
6046        assert!(can_cast_types(string_view_array.data_type(), expected_type));
6047        assert!(can_cast_types(binary_view_array.data_type(), expected_type));
6048
6049        let string_view_casted_array = cast(&string_view_array, expected_type).unwrap();
6050        assert_eq!(string_view_casted_array.data_type(), expected_type);
6051        assert_eq!(string_view_casted_array.as_ref(), &expected_string_array);
6052
6053        let binary_view_casted_array = cast(&binary_view_array, expected_type).unwrap();
6054        assert_eq!(binary_view_casted_array.data_type(), expected_type);
6055        assert_eq!(binary_view_casted_array.as_ref(), &expected_string_array);
6056    }
6057
6058    #[test]
6059    fn test_view_to_binary() {
6060        _test_view_to_binary::<i32>();
6061        _test_view_to_binary::<i64>();
6062    }
6063
6064    fn _test_view_to_binary<O>()
6065    where
6066        O: OffsetSizeTrait,
6067    {
6068        let view_array = {
6069            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
6070            for s in VIEW_TEST_DATA.iter() {
6071                builder.append_option(*s);
6072            }
6073            builder.finish()
6074        };
6075
6076        let expected_binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
6077        let expected_type = expected_binary_array.data_type();
6078
6079        assert!(can_cast_types(view_array.data_type(), expected_type));
6080
6081        let binary_array = cast(&view_array, expected_type).unwrap();
6082        assert_eq!(binary_array.data_type(), expected_type);
6083
6084        assert_eq!(binary_array.as_ref(), &expected_binary_array);
6085    }
6086
6087    #[test]
6088    fn test_cast_from_f64() {
6089        let f64_values: Vec<f64> = vec![
6090            i64::MIN as f64,
6091            i32::MIN as f64,
6092            i16::MIN as f64,
6093            i8::MIN as f64,
6094            0_f64,
6095            u8::MAX as f64,
6096            u16::MAX as f64,
6097            u32::MAX as f64,
6098            u64::MAX as f64,
6099        ];
6100        let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values));
6101
6102        let f64_expected = vec![
6103            -9223372036854776000.0,
6104            -2147483648.0,
6105            -32768.0,
6106            -128.0,
6107            0.0,
6108            255.0,
6109            65535.0,
6110            4294967295.0,
6111            18446744073709552000.0,
6112        ];
6113        assert_eq!(
6114            f64_expected,
6115            get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
6116                .iter()
6117                .map(|i| i.parse::<f64>().unwrap())
6118                .collect::<Vec<f64>>()
6119        );
6120
6121        let f32_expected = vec![
6122            -9223372000000000000.0,
6123            -2147483600.0,
6124            -32768.0,
6125            -128.0,
6126            0.0,
6127            255.0,
6128            65535.0,
6129            4294967300.0,
6130            18446744000000000000.0,
6131        ];
6132        assert_eq!(
6133            f32_expected,
6134            get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
6135                .iter()
6136                .map(|i| i.parse::<f32>().unwrap())
6137                .collect::<Vec<f32>>()
6138        );
6139
6140        let f16_expected = vec![
6141            f16::from_f64(-9223372000000000000.0),
6142            f16::from_f64(-2147483600.0),
6143            f16::from_f64(-32768.0),
6144            f16::from_f64(-128.0),
6145            f16::from_f64(0.0),
6146            f16::from_f64(255.0),
6147            f16::from_f64(65535.0),
6148            f16::from_f64(4294967300.0),
6149            f16::from_f64(18446744000000000000.0),
6150        ];
6151        assert_eq!(
6152            f16_expected,
6153            get_cast_values::<Float16Type>(&f64_array, &DataType::Float16)
6154                .iter()
6155                .map(|i| i.parse::<f16>().unwrap())
6156                .collect::<Vec<f16>>()
6157        );
6158
6159        let i64_expected = vec![
6160            "-9223372036854775808",
6161            "-2147483648",
6162            "-32768",
6163            "-128",
6164            "0",
6165            "255",
6166            "65535",
6167            "4294967295",
6168            "null",
6169        ];
6170        assert_eq!(
6171            i64_expected,
6172            get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
6173        );
6174
6175        let i32_expected = vec![
6176            "null",
6177            "-2147483648",
6178            "-32768",
6179            "-128",
6180            "0",
6181            "255",
6182            "65535",
6183            "null",
6184            "null",
6185        ];
6186        assert_eq!(
6187            i32_expected,
6188            get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
6189        );
6190
6191        let i16_expected = vec![
6192            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
6193        ];
6194        assert_eq!(
6195            i16_expected,
6196            get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
6197        );
6198
6199        let i8_expected = vec![
6200            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
6201        ];
6202        assert_eq!(
6203            i8_expected,
6204            get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
6205        );
6206
6207        let u64_expected = vec![
6208            "null",
6209            "null",
6210            "null",
6211            "null",
6212            "0",
6213            "255",
6214            "65535",
6215            "4294967295",
6216            "null",
6217        ];
6218        assert_eq!(
6219            u64_expected,
6220            get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
6221        );
6222
6223        let u32_expected = vec![
6224            "null",
6225            "null",
6226            "null",
6227            "null",
6228            "0",
6229            "255",
6230            "65535",
6231            "4294967295",
6232            "null",
6233        ];
6234        assert_eq!(
6235            u32_expected,
6236            get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
6237        );
6238
6239        let u16_expected = vec![
6240            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
6241        ];
6242        assert_eq!(
6243            u16_expected,
6244            get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
6245        );
6246
6247        let u8_expected = vec![
6248            "null", "null", "null", "null", "0", "255", "null", "null", "null",
6249        ];
6250        assert_eq!(
6251            u8_expected,
6252            get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
6253        );
6254    }
6255
6256    #[test]
6257    fn test_cast_from_f32() {
6258        let f32_values: Vec<f32> = vec![
6259            i32::MIN as f32,
6260            i32::MIN as f32,
6261            i16::MIN as f32,
6262            i8::MIN as f32,
6263            0_f32,
6264            u8::MAX as f32,
6265            u16::MAX as f32,
6266            u32::MAX as f32,
6267            u32::MAX as f32,
6268        ];
6269        let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values));
6270
6271        let f64_expected = vec![
6272            "-2147483648.0",
6273            "-2147483648.0",
6274            "-32768.0",
6275            "-128.0",
6276            "0.0",
6277            "255.0",
6278            "65535.0",
6279            "4294967296.0",
6280            "4294967296.0",
6281        ];
6282        assert_eq!(
6283            f64_expected,
6284            get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
6285        );
6286
6287        let f32_expected = vec![
6288            "-2147483600.0",
6289            "-2147483600.0",
6290            "-32768.0",
6291            "-128.0",
6292            "0.0",
6293            "255.0",
6294            "65535.0",
6295            "4294967300.0",
6296            "4294967300.0",
6297        ];
6298        assert_eq!(
6299            f32_expected,
6300            get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
6301        );
6302
6303        let f16_expected = vec![
6304            "-inf", "-inf", "-32768.0", "-128.0", "0.0", "255.0", "inf", "inf", "inf",
6305        ];
6306        assert_eq!(
6307            f16_expected,
6308            get_cast_values::<Float16Type>(&f32_array, &DataType::Float16)
6309        );
6310
6311        let i64_expected = vec![
6312            "-2147483648",
6313            "-2147483648",
6314            "-32768",
6315            "-128",
6316            "0",
6317            "255",
6318            "65535",
6319            "4294967296",
6320            "4294967296",
6321        ];
6322        assert_eq!(
6323            i64_expected,
6324            get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
6325        );
6326
6327        let i32_expected = vec![
6328            "-2147483648",
6329            "-2147483648",
6330            "-32768",
6331            "-128",
6332            "0",
6333            "255",
6334            "65535",
6335            "null",
6336            "null",
6337        ];
6338        assert_eq!(
6339            i32_expected,
6340            get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
6341        );
6342
6343        let i16_expected = vec![
6344            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
6345        ];
6346        assert_eq!(
6347            i16_expected,
6348            get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
6349        );
6350
6351        let i8_expected = vec![
6352            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
6353        ];
6354        assert_eq!(
6355            i8_expected,
6356            get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
6357        );
6358
6359        let u64_expected = vec![
6360            "null",
6361            "null",
6362            "null",
6363            "null",
6364            "0",
6365            "255",
6366            "65535",
6367            "4294967296",
6368            "4294967296",
6369        ];
6370        assert_eq!(
6371            u64_expected,
6372            get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
6373        );
6374
6375        let u32_expected = vec![
6376            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
6377        ];
6378        assert_eq!(
6379            u32_expected,
6380            get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
6381        );
6382
6383        let u16_expected = vec![
6384            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
6385        ];
6386        assert_eq!(
6387            u16_expected,
6388            get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
6389        );
6390
6391        let u8_expected = vec![
6392            "null", "null", "null", "null", "0", "255", "null", "null", "null",
6393        ];
6394        assert_eq!(
6395            u8_expected,
6396            get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
6397        );
6398    }
6399
6400    #[test]
6401    fn test_cast_from_uint64() {
6402        let u64_values: Vec<u64> = vec![
6403            0,
6404            u8::MAX as u64,
6405            u16::MAX as u64,
6406            u32::MAX as u64,
6407            u64::MAX,
6408        ];
6409        let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values));
6410
6411        let f64_expected = vec![0.0, 255.0, 65535.0, 4294967295.0, 18446744073709552000.0];
6412        assert_eq!(
6413            f64_expected,
6414            get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
6415                .iter()
6416                .map(|i| i.parse::<f64>().unwrap())
6417                .collect::<Vec<f64>>()
6418        );
6419
6420        let f32_expected = vec![0.0, 255.0, 65535.0, 4294967300.0, 18446744000000000000.0];
6421        assert_eq!(
6422            f32_expected,
6423            get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
6424                .iter()
6425                .map(|i| i.parse::<f32>().unwrap())
6426                .collect::<Vec<f32>>()
6427        );
6428
6429        let f16_expected = vec![
6430            f16::from_f64(0.0),
6431            f16::from_f64(255.0),
6432            f16::from_f64(65535.0),
6433            f16::from_f64(4294967300.0),
6434            f16::from_f64(18446744000000000000.0),
6435        ];
6436        assert_eq!(
6437            f16_expected,
6438            get_cast_values::<Float16Type>(&u64_array, &DataType::Float16)
6439                .iter()
6440                .map(|i| i.parse::<f16>().unwrap())
6441                .collect::<Vec<f16>>()
6442        );
6443
6444        let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
6445        assert_eq!(
6446            i64_expected,
6447            get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
6448        );
6449
6450        let i32_expected = vec!["0", "255", "65535", "null", "null"];
6451        assert_eq!(
6452            i32_expected,
6453            get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
6454        );
6455
6456        let i16_expected = vec!["0", "255", "null", "null", "null"];
6457        assert_eq!(
6458            i16_expected,
6459            get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
6460        );
6461
6462        let i8_expected = vec!["0", "null", "null", "null", "null"];
6463        assert_eq!(
6464            i8_expected,
6465            get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
6466        );
6467
6468        let u64_expected = vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
6469        assert_eq!(
6470            u64_expected,
6471            get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
6472        );
6473
6474        let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
6475        assert_eq!(
6476            u32_expected,
6477            get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
6478        );
6479
6480        let u16_expected = vec!["0", "255", "65535", "null", "null"];
6481        assert_eq!(
6482            u16_expected,
6483            get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
6484        );
6485
6486        let u8_expected = vec!["0", "255", "null", "null", "null"];
6487        assert_eq!(
6488            u8_expected,
6489            get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
6490        );
6491    }
6492
6493    #[test]
6494    fn test_cast_from_uint32() {
6495        let u32_values: Vec<u32> = vec![0, u8::MAX as u32, u16::MAX as u32, u32::MAX];
6496        let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values));
6497
6498        let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
6499        assert_eq!(
6500            f64_expected,
6501            get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
6502        );
6503
6504        let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
6505        assert_eq!(
6506            f32_expected,
6507            get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
6508        );
6509
6510        let f16_expected = vec!["0.0", "255.0", "inf", "inf"];
6511        assert_eq!(
6512            f16_expected,
6513            get_cast_values::<Float16Type>(&u32_array, &DataType::Float16)
6514        );
6515
6516        let i64_expected = vec!["0", "255", "65535", "4294967295"];
6517        assert_eq!(
6518            i64_expected,
6519            get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
6520        );
6521
6522        let i32_expected = vec!["0", "255", "65535", "null"];
6523        assert_eq!(
6524            i32_expected,
6525            get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
6526        );
6527
6528        let i16_expected = vec!["0", "255", "null", "null"];
6529        assert_eq!(
6530            i16_expected,
6531            get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
6532        );
6533
6534        let i8_expected = vec!["0", "null", "null", "null"];
6535        assert_eq!(
6536            i8_expected,
6537            get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
6538        );
6539
6540        let u64_expected = vec!["0", "255", "65535", "4294967295"];
6541        assert_eq!(
6542            u64_expected,
6543            get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
6544        );
6545
6546        let u32_expected = vec!["0", "255", "65535", "4294967295"];
6547        assert_eq!(
6548            u32_expected,
6549            get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
6550        );
6551
6552        let u16_expected = vec!["0", "255", "65535", "null"];
6553        assert_eq!(
6554            u16_expected,
6555            get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
6556        );
6557
6558        let u8_expected = vec!["0", "255", "null", "null"];
6559        assert_eq!(
6560            u8_expected,
6561            get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
6562        );
6563    }
6564
6565    #[test]
6566    fn test_cast_from_uint16() {
6567        let u16_values: Vec<u16> = vec![0, u8::MAX as u16, u16::MAX];
6568        let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values));
6569
6570        let f64_expected = vec!["0.0", "255.0", "65535.0"];
6571        assert_eq!(
6572            f64_expected,
6573            get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
6574        );
6575
6576        let f32_expected = vec!["0.0", "255.0", "65535.0"];
6577        assert_eq!(
6578            f32_expected,
6579            get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
6580        );
6581
6582        let f16_expected = vec!["0.0", "255.0", "inf"];
6583        assert_eq!(
6584            f16_expected,
6585            get_cast_values::<Float16Type>(&u16_array, &DataType::Float16)
6586        );
6587
6588        let i64_expected = vec!["0", "255", "65535"];
6589        assert_eq!(
6590            i64_expected,
6591            get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
6592        );
6593
6594        let i32_expected = vec!["0", "255", "65535"];
6595        assert_eq!(
6596            i32_expected,
6597            get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
6598        );
6599
6600        let i16_expected = vec!["0", "255", "null"];
6601        assert_eq!(
6602            i16_expected,
6603            get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
6604        );
6605
6606        let i8_expected = vec!["0", "null", "null"];
6607        assert_eq!(
6608            i8_expected,
6609            get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
6610        );
6611
6612        let u64_expected = vec!["0", "255", "65535"];
6613        assert_eq!(
6614            u64_expected,
6615            get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
6616        );
6617
6618        let u32_expected = vec!["0", "255", "65535"];
6619        assert_eq!(
6620            u32_expected,
6621            get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
6622        );
6623
6624        let u16_expected = vec!["0", "255", "65535"];
6625        assert_eq!(
6626            u16_expected,
6627            get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
6628        );
6629
6630        let u8_expected = vec!["0", "255", "null"];
6631        assert_eq!(
6632            u8_expected,
6633            get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
6634        );
6635    }
6636
6637    #[test]
6638    fn test_cast_from_uint8() {
6639        let u8_values: Vec<u8> = vec![0, u8::MAX];
6640        let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values));
6641
6642        let f64_expected = vec!["0.0", "255.0"];
6643        assert_eq!(
6644            f64_expected,
6645            get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
6646        );
6647
6648        let f32_expected = vec!["0.0", "255.0"];
6649        assert_eq!(
6650            f32_expected,
6651            get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
6652        );
6653
6654        let f16_expected = vec!["0.0", "255.0"];
6655        assert_eq!(
6656            f16_expected,
6657            get_cast_values::<Float16Type>(&u8_array, &DataType::Float16)
6658        );
6659
6660        let i64_expected = vec!["0", "255"];
6661        assert_eq!(
6662            i64_expected,
6663            get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
6664        );
6665
6666        let i32_expected = vec!["0", "255"];
6667        assert_eq!(
6668            i32_expected,
6669            get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
6670        );
6671
6672        let i16_expected = vec!["0", "255"];
6673        assert_eq!(
6674            i16_expected,
6675            get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
6676        );
6677
6678        let i8_expected = vec!["0", "null"];
6679        assert_eq!(
6680            i8_expected,
6681            get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
6682        );
6683
6684        let u64_expected = vec!["0", "255"];
6685        assert_eq!(
6686            u64_expected,
6687            get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
6688        );
6689
6690        let u32_expected = vec!["0", "255"];
6691        assert_eq!(
6692            u32_expected,
6693            get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
6694        );
6695
6696        let u16_expected = vec!["0", "255"];
6697        assert_eq!(
6698            u16_expected,
6699            get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
6700        );
6701
6702        let u8_expected = vec!["0", "255"];
6703        assert_eq!(
6704            u8_expected,
6705            get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
6706        );
6707    }
6708
6709    #[test]
6710    fn test_cast_from_int64() {
6711        let i64_values: Vec<i64> = vec![
6712            i64::MIN,
6713            i32::MIN as i64,
6714            i16::MIN as i64,
6715            i8::MIN as i64,
6716            0,
6717            i8::MAX as i64,
6718            i16::MAX as i64,
6719            i32::MAX as i64,
6720            i64::MAX,
6721        ];
6722        let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values));
6723
6724        let f64_expected = vec![
6725            -9223372036854776000.0,
6726            -2147483648.0,
6727            -32768.0,
6728            -128.0,
6729            0.0,
6730            127.0,
6731            32767.0,
6732            2147483647.0,
6733            9223372036854776000.0,
6734        ];
6735        assert_eq!(
6736            f64_expected,
6737            get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
6738                .iter()
6739                .map(|i| i.parse::<f64>().unwrap())
6740                .collect::<Vec<f64>>()
6741        );
6742
6743        let f32_expected = vec![
6744            -9223372000000000000.0,
6745            -2147483600.0,
6746            -32768.0,
6747            -128.0,
6748            0.0,
6749            127.0,
6750            32767.0,
6751            2147483600.0,
6752            9223372000000000000.0,
6753        ];
6754        assert_eq!(
6755            f32_expected,
6756            get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
6757                .iter()
6758                .map(|i| i.parse::<f32>().unwrap())
6759                .collect::<Vec<f32>>()
6760        );
6761
6762        let f16_expected = vec![
6763            f16::from_f64(-9223372000000000000.0),
6764            f16::from_f64(-2147483600.0),
6765            f16::from_f64(-32768.0),
6766            f16::from_f64(-128.0),
6767            f16::from_f64(0.0),
6768            f16::from_f64(127.0),
6769            f16::from_f64(32767.0),
6770            f16::from_f64(2147483600.0),
6771            f16::from_f64(9223372000000000000.0),
6772        ];
6773        assert_eq!(
6774            f16_expected,
6775            get_cast_values::<Float16Type>(&i64_array, &DataType::Float16)
6776                .iter()
6777                .map(|i| i.parse::<f16>().unwrap())
6778                .collect::<Vec<f16>>()
6779        );
6780
6781        let i64_expected = vec![
6782            "-9223372036854775808",
6783            "-2147483648",
6784            "-32768",
6785            "-128",
6786            "0",
6787            "127",
6788            "32767",
6789            "2147483647",
6790            "9223372036854775807",
6791        ];
6792        assert_eq!(
6793            i64_expected,
6794            get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
6795        );
6796
6797        let i32_expected = vec![
6798            "null",
6799            "-2147483648",
6800            "-32768",
6801            "-128",
6802            "0",
6803            "127",
6804            "32767",
6805            "2147483647",
6806            "null",
6807        ];
6808        assert_eq!(
6809            i32_expected,
6810            get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
6811        );
6812
6813        assert_eq!(
6814            i32_expected,
6815            get_cast_values::<Date32Type>(&i64_array, &DataType::Date32)
6816        );
6817
6818        let i16_expected = vec![
6819            "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
6820        ];
6821        assert_eq!(
6822            i16_expected,
6823            get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
6824        );
6825
6826        let i8_expected = vec![
6827            "null", "null", "null", "-128", "0", "127", "null", "null", "null",
6828        ];
6829        assert_eq!(
6830            i8_expected,
6831            get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
6832        );
6833
6834        let u64_expected = vec![
6835            "null",
6836            "null",
6837            "null",
6838            "null",
6839            "0",
6840            "127",
6841            "32767",
6842            "2147483647",
6843            "9223372036854775807",
6844        ];
6845        assert_eq!(
6846            u64_expected,
6847            get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
6848        );
6849
6850        let u32_expected = vec![
6851            "null",
6852            "null",
6853            "null",
6854            "null",
6855            "0",
6856            "127",
6857            "32767",
6858            "2147483647",
6859            "null",
6860        ];
6861        assert_eq!(
6862            u32_expected,
6863            get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
6864        );
6865
6866        let u16_expected = vec![
6867            "null", "null", "null", "null", "0", "127", "32767", "null", "null",
6868        ];
6869        assert_eq!(
6870            u16_expected,
6871            get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
6872        );
6873
6874        let u8_expected = vec![
6875            "null", "null", "null", "null", "0", "127", "null", "null", "null",
6876        ];
6877        assert_eq!(
6878            u8_expected,
6879            get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
6880        );
6881    }
6882
6883    #[test]
6884    fn test_cast_from_int32() {
6885        let i32_values: Vec<i32> = vec![
6886            i32::MIN,
6887            i16::MIN as i32,
6888            i8::MIN as i32,
6889            0,
6890            i8::MAX as i32,
6891            i16::MAX as i32,
6892            i32::MAX,
6893        ];
6894        let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values));
6895
6896        let f64_expected = vec![
6897            "-2147483648.0",
6898            "-32768.0",
6899            "-128.0",
6900            "0.0",
6901            "127.0",
6902            "32767.0",
6903            "2147483647.0",
6904        ];
6905        assert_eq!(
6906            f64_expected,
6907            get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
6908        );
6909
6910        let f32_expected = vec![
6911            "-2147483600.0",
6912            "-32768.0",
6913            "-128.0",
6914            "0.0",
6915            "127.0",
6916            "32767.0",
6917            "2147483600.0",
6918        ];
6919        assert_eq!(
6920            f32_expected,
6921            get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
6922        );
6923
6924        let f16_expected = vec![
6925            f16::from_f64(-2147483600.0),
6926            f16::from_f64(-32768.0),
6927            f16::from_f64(-128.0),
6928            f16::from_f64(0.0),
6929            f16::from_f64(127.0),
6930            f16::from_f64(32767.0),
6931            f16::from_f64(2147483600.0),
6932        ];
6933        assert_eq!(
6934            f16_expected,
6935            get_cast_values::<Float16Type>(&i32_array, &DataType::Float16)
6936                .iter()
6937                .map(|i| i.parse::<f16>().unwrap())
6938                .collect::<Vec<f16>>()
6939        );
6940
6941        let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
6942        assert_eq!(
6943            i16_expected,
6944            get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
6945        );
6946
6947        let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
6948        assert_eq!(
6949            i8_expected,
6950            get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
6951        );
6952
6953        let u64_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
6954        assert_eq!(
6955            u64_expected,
6956            get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
6957        );
6958
6959        let u32_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
6960        assert_eq!(
6961            u32_expected,
6962            get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
6963        );
6964
6965        let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
6966        assert_eq!(
6967            u16_expected,
6968            get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
6969        );
6970
6971        let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
6972        assert_eq!(
6973            u8_expected,
6974            get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
6975        );
6976
6977        // The date32 to date64 cast increases the numerical values in order to keep the same dates.
6978        let i64_expected = vec![
6979            "-185542587187200000",
6980            "-2831155200000",
6981            "-11059200000",
6982            "0",
6983            "10972800000",
6984            "2831068800000",
6985            "185542587100800000",
6986        ];
6987        assert_eq!(
6988            i64_expected,
6989            get_cast_values::<Date64Type>(&i32_array, &DataType::Date64)
6990        );
6991    }
6992
6993    #[test]
6994    fn test_cast_from_int16() {
6995        let i16_values: Vec<i16> = vec![i16::MIN, i8::MIN as i16, 0, i8::MAX as i16, i16::MAX];
6996        let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values));
6997
6998        let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
6999        assert_eq!(
7000            f64_expected,
7001            get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
7002        );
7003
7004        let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
7005        assert_eq!(
7006            f32_expected,
7007            get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
7008        );
7009
7010        let f16_expected = vec![
7011            f16::from_f64(-32768.0),
7012            f16::from_f64(-128.0),
7013            f16::from_f64(0.0),
7014            f16::from_f64(127.0),
7015            f16::from_f64(32767.0),
7016        ];
7017        assert_eq!(
7018            f16_expected,
7019            get_cast_values::<Float16Type>(&i16_array, &DataType::Float16)
7020                .iter()
7021                .map(|i| i.parse::<f16>().unwrap())
7022                .collect::<Vec<f16>>()
7023        );
7024
7025        let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
7026        assert_eq!(
7027            i64_expected,
7028            get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
7029        );
7030
7031        let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
7032        assert_eq!(
7033            i32_expected,
7034            get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
7035        );
7036
7037        let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
7038        assert_eq!(
7039            i16_expected,
7040            get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
7041        );
7042
7043        let i8_expected = vec!["null", "-128", "0", "127", "null"];
7044        assert_eq!(
7045            i8_expected,
7046            get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
7047        );
7048
7049        let u64_expected = vec!["null", "null", "0", "127", "32767"];
7050        assert_eq!(
7051            u64_expected,
7052            get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
7053        );
7054
7055        let u32_expected = vec!["null", "null", "0", "127", "32767"];
7056        assert_eq!(
7057            u32_expected,
7058            get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
7059        );
7060
7061        let u16_expected = vec!["null", "null", "0", "127", "32767"];
7062        assert_eq!(
7063            u16_expected,
7064            get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
7065        );
7066
7067        let u8_expected = vec!["null", "null", "0", "127", "null"];
7068        assert_eq!(
7069            u8_expected,
7070            get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
7071        );
7072    }
7073
7074    #[test]
7075    fn test_cast_from_date32() {
7076        let i32_values: Vec<i32> = vec![
7077            i32::MIN,
7078            i16::MIN as i32,
7079            i8::MIN as i32,
7080            0,
7081            i8::MAX as i32,
7082            i16::MAX as i32,
7083            i32::MAX,
7084        ];
7085        let date32_array: ArrayRef = Arc::new(Date32Array::from(i32_values));
7086
7087        let i64_expected = vec![
7088            "-2147483648",
7089            "-32768",
7090            "-128",
7091            "0",
7092            "127",
7093            "32767",
7094            "2147483647",
7095        ];
7096        assert_eq!(
7097            i64_expected,
7098            get_cast_values::<Int64Type>(&date32_array, &DataType::Int64)
7099        );
7100    }
7101
7102    #[test]
7103    fn test_cast_from_int8() {
7104        let i8_values: Vec<i8> = vec![i8::MIN, 0, i8::MAX];
7105        let i8_array = Int8Array::from(i8_values);
7106
7107        let f64_expected = vec!["-128.0", "0.0", "127.0"];
7108        assert_eq!(
7109            f64_expected,
7110            get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
7111        );
7112
7113        let f32_expected = vec!["-128.0", "0.0", "127.0"];
7114        assert_eq!(
7115            f32_expected,
7116            get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
7117        );
7118
7119        let f16_expected = vec!["-128.0", "0.0", "127.0"];
7120        assert_eq!(
7121            f16_expected,
7122            get_cast_values::<Float16Type>(&i8_array, &DataType::Float16)
7123        );
7124
7125        let i64_expected = vec!["-128", "0", "127"];
7126        assert_eq!(
7127            i64_expected,
7128            get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
7129        );
7130
7131        let i32_expected = vec!["-128", "0", "127"];
7132        assert_eq!(
7133            i32_expected,
7134            get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
7135        );
7136
7137        let i16_expected = vec!["-128", "0", "127"];
7138        assert_eq!(
7139            i16_expected,
7140            get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
7141        );
7142
7143        let i8_expected = vec!["-128", "0", "127"];
7144        assert_eq!(
7145            i8_expected,
7146            get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
7147        );
7148
7149        let u64_expected = vec!["null", "0", "127"];
7150        assert_eq!(
7151            u64_expected,
7152            get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
7153        );
7154
7155        let u32_expected = vec!["null", "0", "127"];
7156        assert_eq!(
7157            u32_expected,
7158            get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
7159        );
7160
7161        let u16_expected = vec!["null", "0", "127"];
7162        assert_eq!(
7163            u16_expected,
7164            get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
7165        );
7166
7167        let u8_expected = vec!["null", "0", "127"];
7168        assert_eq!(
7169            u8_expected,
7170            get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
7171        );
7172    }
7173
7174    /// Convert `array` into a vector of strings by casting to data type dt
7175    fn get_cast_values<T>(array: &dyn Array, dt: &DataType) -> Vec<String>
7176    where
7177        T: ArrowPrimitiveType,
7178    {
7179        let c = cast(array, dt).unwrap();
7180        let a = c.as_primitive::<T>();
7181        let mut v: Vec<String> = vec![];
7182        for i in 0..array.len() {
7183            if a.is_null(i) {
7184                v.push("null".to_string())
7185            } else {
7186                v.push(format!("{:?}", a.value(i)));
7187            }
7188        }
7189        v
7190    }
7191
7192    #[test]
7193    fn test_cast_utf8_dict() {
7194        // FROM a dictionary with of Utf8 values
7195        use DataType::*;
7196
7197        let mut builder = StringDictionaryBuilder::<Int8Type>::new();
7198        builder.append("one").unwrap();
7199        builder.append_null();
7200        builder.append("three").unwrap();
7201        let array: ArrayRef = Arc::new(builder.finish());
7202
7203        let expected = vec!["one", "null", "three"];
7204
7205        // Test casting TO StringArray
7206        let cast_type = Utf8;
7207        let cast_array = cast(&array, &cast_type).expect("cast to UTF-8 failed");
7208        assert_eq!(cast_array.data_type(), &cast_type);
7209        assert_eq!(array_to_strings(&cast_array), expected);
7210
7211        // Test casting TO Dictionary (with different index sizes)
7212
7213        let cast_type = Dictionary(Box::new(Int16), Box::new(Utf8));
7214        let cast_array = cast(&array, &cast_type).expect("cast failed");
7215        assert_eq!(cast_array.data_type(), &cast_type);
7216        assert_eq!(array_to_strings(&cast_array), expected);
7217
7218        let cast_type = Dictionary(Box::new(Int32), Box::new(Utf8));
7219        let cast_array = cast(&array, &cast_type).expect("cast failed");
7220        assert_eq!(cast_array.data_type(), &cast_type);
7221        assert_eq!(array_to_strings(&cast_array), expected);
7222
7223        let cast_type = Dictionary(Box::new(Int64), Box::new(Utf8));
7224        let cast_array = cast(&array, &cast_type).expect("cast failed");
7225        assert_eq!(cast_array.data_type(), &cast_type);
7226        assert_eq!(array_to_strings(&cast_array), expected);
7227
7228        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
7229        let cast_array = cast(&array, &cast_type).expect("cast failed");
7230        assert_eq!(cast_array.data_type(), &cast_type);
7231        assert_eq!(array_to_strings(&cast_array), expected);
7232
7233        let cast_type = Dictionary(Box::new(UInt16), Box::new(Utf8));
7234        let cast_array = cast(&array, &cast_type).expect("cast failed");
7235        assert_eq!(cast_array.data_type(), &cast_type);
7236        assert_eq!(array_to_strings(&cast_array), expected);
7237
7238        let cast_type = Dictionary(Box::new(UInt32), Box::new(Utf8));
7239        let cast_array = cast(&array, &cast_type).expect("cast failed");
7240        assert_eq!(cast_array.data_type(), &cast_type);
7241        assert_eq!(array_to_strings(&cast_array), expected);
7242
7243        let cast_type = Dictionary(Box::new(UInt64), Box::new(Utf8));
7244        let cast_array = cast(&array, &cast_type).expect("cast failed");
7245        assert_eq!(cast_array.data_type(), &cast_type);
7246        assert_eq!(array_to_strings(&cast_array), expected);
7247    }
7248
7249    #[test]
7250    fn test_cast_dict_to_dict_bad_index_value_primitive() {
7251        use DataType::*;
7252        // test converting from an array that has indexes of a type
7253        // that are out of bounds for a particular other kind of
7254        // index.
7255
7256        let mut builder = PrimitiveDictionaryBuilder::<Int32Type, Int64Type>::new();
7257
7258        // add 200 distinct values (which can be stored by a
7259        // dictionary indexed by int32, but not a dictionary indexed
7260        // with int8)
7261        for i in 0..200 {
7262            builder.append(i).unwrap();
7263        }
7264        let array: ArrayRef = Arc::new(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_dict_to_dict_bad_index_value_utf8() {
7279        use DataType::*;
7280        // Same test as test_cast_dict_to_dict_bad_index_value but use
7281        // string values (and encode the expected behavior here);
7282
7283        let mut builder = StringDictionaryBuilder::<Int32Type>::new();
7284
7285        // add 200 distinct values (which can be stored by a
7286        // dictionary indexed by int32, but not a dictionary indexed
7287        // with int8)
7288        for i in 0..200 {
7289            let val = format!("val{i}");
7290            builder.append(&val).unwrap();
7291        }
7292        let array = builder.finish();
7293
7294        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
7295        let res = cast(&array, &cast_type);
7296        assert!(res.is_err());
7297        let actual_error = format!("{res:?}");
7298        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
7299        assert!(
7300            actual_error.contains(expected_error),
7301            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
7302        );
7303    }
7304
7305    #[test]
7306    fn test_cast_primitive_dict() {
7307        // FROM a dictionary with of INT32 values
7308        use DataType::*;
7309
7310        let mut builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
7311        builder.append(1).unwrap();
7312        builder.append_null();
7313        builder.append(3).unwrap();
7314        let array: ArrayRef = Arc::new(builder.finish());
7315
7316        let expected = vec!["1", "null", "3"];
7317
7318        // Test casting TO PrimitiveArray, different dictionary type
7319        let cast_array = cast(&array, &Utf8).expect("cast to UTF-8 failed");
7320        assert_eq!(array_to_strings(&cast_array), expected);
7321        assert_eq!(cast_array.data_type(), &Utf8);
7322
7323        let cast_array = cast(&array, &Int64).expect("cast to int64 failed");
7324        assert_eq!(array_to_strings(&cast_array), expected);
7325        assert_eq!(cast_array.data_type(), &Int64);
7326    }
7327
7328    #[test]
7329    fn test_cast_primitive_array_to_dict() {
7330        use DataType::*;
7331
7332        let mut builder = PrimitiveBuilder::<Int32Type>::new();
7333        builder.append_value(1);
7334        builder.append_null();
7335        builder.append_value(3);
7336        let array: ArrayRef = Arc::new(builder.finish());
7337
7338        let expected = vec!["1", "null", "3"];
7339
7340        // Cast to a dictionary (same value type, Int32)
7341        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int32));
7342        let cast_array = cast(&array, &cast_type).expect("cast failed");
7343        assert_eq!(cast_array.data_type(), &cast_type);
7344        assert_eq!(array_to_strings(&cast_array), expected);
7345
7346        // Cast to a dictionary (different value type, Int8)
7347        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int8));
7348        let cast_array = cast(&array, &cast_type).expect("cast failed");
7349        assert_eq!(cast_array.data_type(), &cast_type);
7350        assert_eq!(array_to_strings(&cast_array), expected);
7351    }
7352
7353    #[test]
7354    fn test_cast_time_array_to_dict() {
7355        use DataType::*;
7356
7357        let array = Arc::new(Date32Array::from(vec![Some(1000), None, Some(2000)])) as ArrayRef;
7358
7359        let expected = vec!["1972-09-27", "null", "1975-06-24"];
7360
7361        let cast_type = Dictionary(Box::new(UInt8), Box::new(Date32));
7362        let cast_array = cast(&array, &cast_type).expect("cast failed");
7363        assert_eq!(cast_array.data_type(), &cast_type);
7364        assert_eq!(array_to_strings(&cast_array), expected);
7365    }
7366
7367    #[test]
7368    fn test_cast_timestamp_array_to_dict() {
7369        use DataType::*;
7370
7371        let array = Arc::new(
7372            TimestampSecondArray::from(vec![Some(1000), None, Some(2000)]).with_timezone_utc(),
7373        ) as ArrayRef;
7374
7375        let expected = vec!["1970-01-01T00:16:40", "null", "1970-01-01T00:33:20"];
7376
7377        let cast_type = Dictionary(Box::new(UInt8), Box::new(Timestamp(TimeUnit::Second, None)));
7378        let cast_array = cast(&array, &cast_type).expect("cast failed");
7379        assert_eq!(cast_array.data_type(), &cast_type);
7380        assert_eq!(array_to_strings(&cast_array), expected);
7381    }
7382
7383    #[test]
7384    fn test_cast_string_array_to_dict() {
7385        use DataType::*;
7386
7387        let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) as ArrayRef;
7388
7389        let expected = vec!["one", "null", "three"];
7390
7391        // Cast to a dictionary (same value type, Utf8)
7392        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
7393        let cast_array = cast(&array, &cast_type).expect("cast failed");
7394        assert_eq!(cast_array.data_type(), &cast_type);
7395        assert_eq!(array_to_strings(&cast_array), expected);
7396    }
7397
7398    #[test]
7399    fn test_cast_null_array_to_from_decimal_array() {
7400        let data_type = DataType::Decimal128(12, 4);
7401        let array = new_null_array(&DataType::Null, 4);
7402        assert_eq!(array.data_type(), &DataType::Null);
7403        let cast_array = cast(&array, &data_type).expect("cast failed");
7404        assert_eq!(cast_array.data_type(), &data_type);
7405        for i in 0..4 {
7406            assert!(cast_array.is_null(i));
7407        }
7408
7409        let array = new_null_array(&data_type, 4);
7410        assert_eq!(array.data_type(), &data_type);
7411        let cast_array = cast(&array, &DataType::Null).expect("cast failed");
7412        assert_eq!(cast_array.data_type(), &DataType::Null);
7413        assert_eq!(cast_array.len(), 4);
7414        assert_eq!(cast_array.logical_nulls().unwrap().null_count(), 4);
7415    }
7416
7417    #[test]
7418    fn test_cast_null_array_from_and_to_primitive_array() {
7419        macro_rules! typed_test {
7420            ($ARR_TYPE:ident, $DATATYPE:ident, $TYPE:tt) => {{
7421                {
7422                    let array = Arc::new(NullArray::new(6)) as ArrayRef;
7423                    let expected = $ARR_TYPE::from(vec![None; 6]);
7424                    let cast_type = DataType::$DATATYPE;
7425                    let cast_array = cast(&array, &cast_type).expect("cast failed");
7426                    let cast_array = cast_array.as_primitive::<$TYPE>();
7427                    assert_eq!(cast_array.data_type(), &cast_type);
7428                    assert_eq!(cast_array, &expected);
7429                }
7430            }};
7431        }
7432
7433        typed_test!(Int16Array, Int16, Int16Type);
7434        typed_test!(Int32Array, Int32, Int32Type);
7435        typed_test!(Int64Array, Int64, Int64Type);
7436
7437        typed_test!(UInt16Array, UInt16, UInt16Type);
7438        typed_test!(UInt32Array, UInt32, UInt32Type);
7439        typed_test!(UInt64Array, UInt64, UInt64Type);
7440
7441        typed_test!(Float32Array, Float32, Float32Type);
7442        typed_test!(Float64Array, Float64, Float64Type);
7443
7444        typed_test!(Date32Array, Date32, Date32Type);
7445        typed_test!(Date64Array, Date64, Date64Type);
7446    }
7447
7448    fn cast_from_null_to_other(data_type: &DataType) {
7449        // Cast from null to data_type
7450        {
7451            let array = new_null_array(&DataType::Null, 4);
7452            assert_eq!(array.data_type(), &DataType::Null);
7453            let cast_array = cast(&array, data_type).expect("cast failed");
7454            assert_eq!(cast_array.data_type(), data_type);
7455            for i in 0..4 {
7456                assert!(cast_array.is_null(i));
7457            }
7458        }
7459    }
7460
7461    #[test]
7462    fn test_cast_null_from_and_to_variable_sized() {
7463        cast_from_null_to_other(&DataType::Utf8);
7464        cast_from_null_to_other(&DataType::LargeUtf8);
7465        cast_from_null_to_other(&DataType::Binary);
7466        cast_from_null_to_other(&DataType::LargeBinary);
7467    }
7468
7469    #[test]
7470    fn test_cast_null_from_and_to_nested_type() {
7471        // Cast null from and to map
7472        let data_type = DataType::Map(
7473            Arc::new(Field::new_struct(
7474                "entry",
7475                vec![
7476                    Field::new("key", DataType::Utf8, false),
7477                    Field::new("value", DataType::Int32, true),
7478                ],
7479                false,
7480            )),
7481            false,
7482        );
7483        cast_from_null_to_other(&data_type);
7484
7485        // Cast null from and to list
7486        let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
7487        cast_from_null_to_other(&data_type);
7488        let data_type = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
7489        cast_from_null_to_other(&data_type);
7490        let data_type =
7491            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
7492        cast_from_null_to_other(&data_type);
7493
7494        // Cast null from and to dictionary
7495        let values = vec![None, None, None, None] as Vec<Option<&str>>;
7496        let array: DictionaryArray<Int8Type> = values.into_iter().collect();
7497        let array = Arc::new(array) as ArrayRef;
7498        let data_type = array.data_type().to_owned();
7499        cast_from_null_to_other(&data_type);
7500
7501        // Cast null from and to struct
7502        let data_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
7503        cast_from_null_to_other(&data_type);
7504    }
7505
7506    /// Print the `DictionaryArray` `array` as a vector of strings
7507    fn array_to_strings(array: &ArrayRef) -> Vec<String> {
7508        let options = FormatOptions::new().with_null("null");
7509        let formatter = ArrayFormatter::try_new(array.as_ref(), &options).unwrap();
7510        (0..array.len())
7511            .map(|i| formatter.value(i).to_string())
7512            .collect()
7513    }
7514
7515    #[test]
7516    fn test_cast_utf8_to_date32() {
7517        use chrono::NaiveDate;
7518        let from_ymd = chrono::NaiveDate::from_ymd_opt;
7519        let since = chrono::NaiveDate::signed_duration_since;
7520
7521        let a = StringArray::from(vec![
7522            "2000-01-01",          // valid date with leading 0s
7523            "2000-01-01T12:00:00", // valid datetime, will throw away the time part
7524            "2000-2-2",            // valid date without leading 0s
7525            "2000-00-00",          // invalid month and day
7526            "2000",                // just a year is invalid
7527        ]);
7528        let array = Arc::new(a) as ArrayRef;
7529        let b = cast(&array, &DataType::Date32).unwrap();
7530        let c = b.as_primitive::<Date32Type>();
7531
7532        // test valid inputs
7533        let date_value = since(
7534            NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
7535            from_ymd(1970, 1, 1).unwrap(),
7536        )
7537        .num_days() as i32;
7538        assert!(c.is_valid(0)); // "2000-01-01"
7539        assert_eq!(date_value, c.value(0));
7540
7541        assert!(c.is_valid(1)); // "2000-01-01T12:00:00"
7542        assert_eq!(date_value, c.value(1));
7543
7544        let date_value = since(
7545            NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
7546            from_ymd(1970, 1, 1).unwrap(),
7547        )
7548        .num_days() as i32;
7549        assert!(c.is_valid(2)); // "2000-2-2"
7550        assert_eq!(date_value, c.value(2));
7551
7552        // test invalid inputs
7553        assert!(!c.is_valid(3)); // "2000-00-00"
7554        assert!(!c.is_valid(4)); // "2000"
7555    }
7556
7557    #[test]
7558    fn test_cast_utf8_to_date64() {
7559        let a = StringArray::from(vec![
7560            "2000-01-01T12:00:00", // date + time valid
7561            "2020-12-15T12:34:56", // date + time valid
7562            "2020-2-2T12:34:56",   // valid date time without leading 0s
7563            "2000-00-00T12:00:00", // invalid month and day
7564            "2000-01-01 12:00:00", // missing the 'T'
7565            "2000-01-01",          // just a date is invalid
7566        ]);
7567        let array = Arc::new(a) as ArrayRef;
7568        let b = cast(&array, &DataType::Date64).unwrap();
7569        let c = b.as_primitive::<Date64Type>();
7570
7571        // test valid inputs
7572        assert!(c.is_valid(0)); // "2000-01-01T12:00:00"
7573        assert_eq!(946728000000, c.value(0));
7574        assert!(c.is_valid(1)); // "2020-12-15T12:34:56"
7575        assert_eq!(1608035696000, c.value(1));
7576        assert!(!c.is_valid(2)); // "2020-2-2T12:34:56"
7577
7578        assert!(!c.is_valid(3)); // "2000-00-00T12:00:00"
7579        assert!(c.is_valid(4)); // "2000-01-01 12:00:00"
7580        assert_eq!(946728000000, c.value(4));
7581        assert!(c.is_valid(5)); // "2000-01-01"
7582        assert_eq!(946684800000, c.value(5));
7583    }
7584
7585    #[test]
7586    fn test_can_cast_fsl_to_fsl() {
7587        let from_array = Arc::new(
7588            FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
7589                [Some([Some(1.0), Some(2.0)]), None],
7590                2,
7591            ),
7592        ) as ArrayRef;
7593        let to_array = Arc::new(
7594            FixedSizeListArray::from_iter_primitive::<Float16Type, _, _>(
7595                [
7596                    Some([Some(f16::from_f32(1.0)), Some(f16::from_f32(2.0))]),
7597                    None,
7598                ],
7599                2,
7600            ),
7601        ) as ArrayRef;
7602
7603        assert!(can_cast_types(from_array.data_type(), to_array.data_type()));
7604        let actual = cast(&from_array, to_array.data_type()).unwrap();
7605        assert_eq!(actual.data_type(), to_array.data_type());
7606
7607        let invalid_target =
7608            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Binary, true)), 2);
7609        assert!(!can_cast_types(from_array.data_type(), &invalid_target));
7610
7611        let invalid_size =
7612            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Float16, true)), 5);
7613        assert!(!can_cast_types(from_array.data_type(), &invalid_size));
7614    }
7615
7616    #[test]
7617    fn test_can_cast_types_fixed_size_list_to_list() {
7618        // DataType::List
7619        let array1 = Arc::new(make_fixed_size_list_array()) as ArrayRef;
7620        assert!(can_cast_types(
7621            array1.data_type(),
7622            &DataType::List(Arc::new(Field::new("", DataType::Int32, false)))
7623        ));
7624
7625        // DataType::LargeList
7626        let array2 = Arc::new(make_fixed_size_list_array_for_large_list()) as ArrayRef;
7627        assert!(can_cast_types(
7628            array2.data_type(),
7629            &DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false)))
7630        ));
7631    }
7632
7633    #[test]
7634    fn test_cast_fixed_size_list_to_list() {
7635        // Important cases:
7636        // 1. With/without nulls
7637        // 2. LargeList and List
7638        // 3. With and without inner casts
7639
7640        let cases = [
7641            // fixed_size_list<i32, 2> => list<i32>
7642            (
7643                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7644                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
7645                    2,
7646                )) as ArrayRef,
7647                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
7648                    Some([Some(1), Some(1)]),
7649                    Some([Some(2), Some(2)]),
7650                ])) as ArrayRef,
7651            ),
7652            // fixed_size_list<i32, 2> => list<i32> (nullable)
7653            (
7654                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7655                    [None, Some([Some(2), Some(2)])],
7656                    2,
7657                )) as ArrayRef,
7658                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
7659                    None,
7660                    Some([Some(2), Some(2)]),
7661                ])) as ArrayRef,
7662            ),
7663            // fixed_size_list<i32, 2> => large_list<i64>
7664            (
7665                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7666                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
7667                    2,
7668                )) as ArrayRef,
7669                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
7670                    Some([Some(1), Some(1)]),
7671                    Some([Some(2), Some(2)]),
7672                ])) as ArrayRef,
7673            ),
7674            // fixed_size_list<i32, 2> => large_list<i64> (nullable)
7675            (
7676                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7677                    [None, Some([Some(2), Some(2)])],
7678                    2,
7679                )) as ArrayRef,
7680                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
7681                    None,
7682                    Some([Some(2), Some(2)]),
7683                ])) as ArrayRef,
7684            ),
7685        ];
7686
7687        for (array, expected) in cases {
7688            let array = Arc::new(array) as ArrayRef;
7689
7690            assert!(
7691                can_cast_types(array.data_type(), expected.data_type()),
7692                "can_cast_types claims we cannot cast {:?} to {:?}",
7693                array.data_type(),
7694                expected.data_type()
7695            );
7696
7697            let list_array = cast(&array, expected.data_type())
7698                .unwrap_or_else(|_| panic!("Failed to cast {array:?} to {expected:?}"));
7699            assert_eq!(
7700                list_array.as_ref(),
7701                &expected,
7702                "Incorrect result from casting {array:?} to {expected:?}",
7703            );
7704        }
7705    }
7706
7707    #[test]
7708    fn test_cast_utf8_to_list() {
7709        // DataType::List
7710        let array = Arc::new(StringArray::from(vec!["5"])) as ArrayRef;
7711        let field = Arc::new(Field::new("", DataType::Int32, false));
7712        let list_array = cast(&array, &DataType::List(field.clone())).unwrap();
7713        let actual = list_array.as_list_opt::<i32>().unwrap();
7714        let expect = ListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
7715        assert_eq!(&expect.value(0), &actual.value(0));
7716
7717        // DataType::LargeList
7718        let list_array = cast(&array, &DataType::LargeList(field.clone())).unwrap();
7719        let actual = list_array.as_list_opt::<i64>().unwrap();
7720        let expect = LargeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
7721        assert_eq!(&expect.value(0), &actual.value(0));
7722
7723        // DataType::FixedSizeList
7724        let list_array = cast(&array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
7725        let actual = list_array.as_fixed_size_list_opt().unwrap();
7726        let expect =
7727            FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])], 1);
7728        assert_eq!(&expect.value(0), &actual.value(0));
7729    }
7730
7731    #[test]
7732    fn test_cast_single_element_fixed_size_list() {
7733        // FixedSizeList<T>[1] => T
7734        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
7735            [(Some([Some(5)]))],
7736            1,
7737        )) as ArrayRef;
7738        let casted_array = cast(&from_array, &DataType::Int32).unwrap();
7739        let actual: &Int32Array = casted_array.as_primitive();
7740        let expected = Int32Array::from(vec![Some(5)]);
7741        assert_eq!(&expected, actual);
7742
7743        // FixedSizeList<T>[1] => FixedSizeList<U>[1]
7744        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
7745            [(Some([Some(5)]))],
7746            1,
7747        )) as ArrayRef;
7748        let to_field = Arc::new(Field::new("dummy", DataType::Float32, false));
7749        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
7750        let expected = Arc::new(FixedSizeListArray::new(
7751            to_field.clone(),
7752            1,
7753            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
7754            None,
7755        )) as ArrayRef;
7756        assert_eq!(*expected, *actual);
7757
7758        // FixedSizeList<T>[1] => FixedSizeList<FixdSizedList<U>[1]>[1]
7759        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
7760            [(Some([Some(5)]))],
7761            1,
7762        )) as ArrayRef;
7763        let to_field_inner = Arc::new(Field::new_list_field(DataType::Float32, false));
7764        let to_field = Arc::new(Field::new(
7765            "dummy",
7766            DataType::FixedSizeList(to_field_inner.clone(), 1),
7767            false,
7768        ));
7769        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
7770        let expected = Arc::new(FixedSizeListArray::new(
7771            to_field.clone(),
7772            1,
7773            Arc::new(FixedSizeListArray::new(
7774                to_field_inner.clone(),
7775                1,
7776                Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
7777                None,
7778            )) as ArrayRef,
7779            None,
7780        )) as ArrayRef;
7781        assert_eq!(*expected, *actual);
7782
7783        // T => FixedSizeList<T>[1] (non-nullable)
7784        let field = Arc::new(Field::new("dummy", DataType::Float32, false));
7785        let from_array = Arc::new(Int8Array::from(vec![Some(5)])) as ArrayRef;
7786        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
7787        let actual = casted_array.as_fixed_size_list();
7788        let expected = Arc::new(FixedSizeListArray::new(
7789            field.clone(),
7790            1,
7791            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
7792            None,
7793        )) as ArrayRef;
7794        assert_eq!(expected.as_ref(), actual);
7795
7796        // T => FixedSizeList<T>[1] (nullable)
7797        let field = Arc::new(Field::new("nullable", DataType::Float32, true));
7798        let from_array = Arc::new(Int8Array::from(vec![None])) as ArrayRef;
7799        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
7800        let actual = casted_array.as_fixed_size_list();
7801        let expected = Arc::new(FixedSizeListArray::new(
7802            field.clone(),
7803            1,
7804            Arc::new(Float32Array::from(vec![None])) as ArrayRef,
7805            None,
7806        )) as ArrayRef;
7807        assert_eq!(expected.as_ref(), actual);
7808    }
7809
7810    #[test]
7811    fn test_cast_list_containers() {
7812        // large-list to list
7813        let array = Arc::new(make_large_list_array()) as ArrayRef;
7814        let list_array = cast(
7815            &array,
7816            &DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
7817        )
7818        .unwrap();
7819        let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
7820        let expected = array.as_any().downcast_ref::<LargeListArray>().unwrap();
7821
7822        assert_eq!(&expected.value(0), &actual.value(0));
7823        assert_eq!(&expected.value(1), &actual.value(1));
7824        assert_eq!(&expected.value(2), &actual.value(2));
7825
7826        // list to large-list
7827        let array = Arc::new(make_list_array()) as ArrayRef;
7828        let large_list_array = cast(
7829            &array,
7830            &DataType::LargeList(Arc::new(Field::new("", DataType::Int32, false))),
7831        )
7832        .unwrap();
7833        let actual = large_list_array
7834            .as_any()
7835            .downcast_ref::<LargeListArray>()
7836            .unwrap();
7837        let expected = array.as_any().downcast_ref::<ListArray>().unwrap();
7838
7839        assert_eq!(&expected.value(0), &actual.value(0));
7840        assert_eq!(&expected.value(1), &actual.value(1));
7841        assert_eq!(&expected.value(2), &actual.value(2));
7842    }
7843
7844    #[test]
7845    fn test_cast_list_to_fsl() {
7846        // There four noteworthy cases we should handle:
7847        // 1. No nulls
7848        // 2. Nulls that are always empty
7849        // 3. Nulls that have varying lengths
7850        // 4. Nulls that are correctly sized (same as target list size)
7851
7852        // Non-null case
7853        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
7854        let values = vec![
7855            Some(vec![Some(1), Some(2), Some(3)]),
7856            Some(vec![Some(4), Some(5), Some(6)]),
7857        ];
7858        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
7859            values.clone(),
7860        )) as ArrayRef;
7861        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7862            values, 3,
7863        )) as ArrayRef;
7864        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
7865        assert_eq!(expected.as_ref(), actual.as_ref());
7866
7867        // Null cases
7868        // Array is [[1, 2, 3], null, [4, 5, 6], null]
7869        let cases = [
7870            (
7871                // Zero-length nulls
7872                vec![1, 2, 3, 4, 5, 6],
7873                vec![3, 0, 3, 0],
7874            ),
7875            (
7876                // Varying-length nulls
7877                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
7878                vec![3, 2, 3, 1],
7879            ),
7880            (
7881                // Correctly-sized nulls
7882                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
7883                vec![3, 3, 3, 3],
7884            ),
7885            (
7886                // Mixed nulls
7887                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
7888                vec![3, 0, 3, 3],
7889            ),
7890        ];
7891        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
7892
7893        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7894            vec![
7895                Some(vec![Some(1), Some(2), Some(3)]),
7896                None,
7897                Some(vec![Some(4), Some(5), Some(6)]),
7898                None,
7899            ],
7900            3,
7901        )) as ArrayRef;
7902
7903        for (values, lengths) in cases.iter() {
7904            let array = Arc::new(ListArray::new(
7905                field.clone(),
7906                OffsetBuffer::from_lengths(lengths.clone()),
7907                Arc::new(Int32Array::from(values.clone())),
7908                Some(null_buffer.clone()),
7909            )) as ArrayRef;
7910            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
7911            assert_eq!(expected.as_ref(), actual.as_ref());
7912        }
7913    }
7914
7915    #[test]
7916    fn test_cast_list_to_fsl_safety() {
7917        let values = vec![
7918            Some(vec![Some(1), Some(2), Some(3)]),
7919            Some(vec![Some(4), Some(5)]),
7920            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
7921            Some(vec![Some(3), Some(4), Some(5)]),
7922        ];
7923        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
7924            values.clone(),
7925        )) as ArrayRef;
7926
7927        let res = cast_with_options(
7928            array.as_ref(),
7929            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
7930            &CastOptions {
7931                safe: false,
7932                ..Default::default()
7933            },
7934        );
7935        assert!(res.is_err());
7936        assert!(format!("{res:?}")
7937            .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2"));
7938
7939        // When safe=true (default), the cast will fill nulls for lists that are
7940        // too short and truncate lists that are too long.
7941        let res = cast(
7942            array.as_ref(),
7943            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
7944        )
7945        .unwrap();
7946        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7947            vec![
7948                Some(vec![Some(1), Some(2), Some(3)]),
7949                None, // Too short -> replaced with null
7950                None, // Too long -> replaced with null
7951                Some(vec![Some(3), Some(4), Some(5)]),
7952            ],
7953            3,
7954        )) as ArrayRef;
7955        assert_eq!(expected.as_ref(), res.as_ref());
7956
7957        // The safe option is false and the source array contains a null list.
7958        // issue: https://github.com/apache/arrow-rs/issues/5642
7959        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
7960            Some(vec![Some(1), Some(2), Some(3)]),
7961            None,
7962        ])) as ArrayRef;
7963        let res = cast_with_options(
7964            array.as_ref(),
7965            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
7966            &CastOptions {
7967                safe: false,
7968                ..Default::default()
7969            },
7970        )
7971        .unwrap();
7972        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7973            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
7974            3,
7975        )) as ArrayRef;
7976        assert_eq!(expected.as_ref(), res.as_ref());
7977    }
7978
7979    #[test]
7980    fn test_cast_large_list_to_fsl() {
7981        let values = vec![Some(vec![Some(1), Some(2)]), Some(vec![Some(3), Some(4)])];
7982        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
7983            values.clone(),
7984        )) as ArrayRef;
7985        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
7986            values, 2,
7987        )) as ArrayRef;
7988        let actual = cast(
7989            array.as_ref(),
7990            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 2),
7991        )
7992        .unwrap();
7993        assert_eq!(expected.as_ref(), actual.as_ref());
7994    }
7995
7996    #[test]
7997    fn test_cast_list_to_fsl_subcast() {
7998        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
7999            vec![
8000                Some(vec![Some(1), Some(2)]),
8001                Some(vec![Some(3), Some(i32::MAX)]),
8002            ],
8003        )) as ArrayRef;
8004        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
8005            vec![
8006                Some(vec![Some(1), Some(2)]),
8007                Some(vec![Some(3), Some(i32::MAX as i64)]),
8008            ],
8009            2,
8010        )) as ArrayRef;
8011        let actual = cast(
8012            array.as_ref(),
8013            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 2),
8014        )
8015        .unwrap();
8016        assert_eq!(expected.as_ref(), actual.as_ref());
8017
8018        let res = cast_with_options(
8019            array.as_ref(),
8020            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int16, true)), 2),
8021            &CastOptions {
8022                safe: false,
8023                ..Default::default()
8024            },
8025        );
8026        assert!(res.is_err());
8027        assert!(format!("{res:?}").contains("Can't cast value 2147483647 to type Int16"));
8028    }
8029
8030    #[test]
8031    fn test_cast_list_to_fsl_empty() {
8032        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
8033        let array = new_empty_array(&DataType::List(field.clone()));
8034
8035        let target_type = DataType::FixedSizeList(field.clone(), 3);
8036        let expected = new_empty_array(&target_type);
8037
8038        let actual = cast(array.as_ref(), &target_type).unwrap();
8039        assert_eq!(expected.as_ref(), actual.as_ref());
8040    }
8041
8042    fn make_list_array() -> ListArray {
8043        // Construct a value array
8044        let value_data = ArrayData::builder(DataType::Int32)
8045            .len(8)
8046            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8047            .build()
8048            .unwrap();
8049
8050        // Construct a buffer for value offsets, for the nested array:
8051        //  [[0, 1, 2], [3, 4, 5], [6, 7]]
8052        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
8053
8054        // Construct a list array from the above two
8055        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
8056        let list_data = ArrayData::builder(list_data_type)
8057            .len(3)
8058            .add_buffer(value_offsets)
8059            .add_child_data(value_data)
8060            .build()
8061            .unwrap();
8062        ListArray::from(list_data)
8063    }
8064
8065    fn make_large_list_array() -> LargeListArray {
8066        // Construct a value array
8067        let value_data = ArrayData::builder(DataType::Int32)
8068            .len(8)
8069            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8070            .build()
8071            .unwrap();
8072
8073        // Construct a buffer for value offsets, for the nested array:
8074        //  [[0, 1, 2], [3, 4, 5], [6, 7]]
8075        let value_offsets = Buffer::from_slice_ref([0i64, 3, 6, 8]);
8076
8077        // Construct a list array from the above two
8078        let list_data_type =
8079            DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8080        let list_data = ArrayData::builder(list_data_type)
8081            .len(3)
8082            .add_buffer(value_offsets)
8083            .add_child_data(value_data)
8084            .build()
8085            .unwrap();
8086        LargeListArray::from(list_data)
8087    }
8088
8089    fn make_fixed_size_list_array() -> FixedSizeListArray {
8090        // Construct a value array
8091        let value_data = ArrayData::builder(DataType::Int32)
8092            .len(8)
8093            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8094            .build()
8095            .unwrap();
8096
8097        let list_data_type =
8098            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
8099        let list_data = ArrayData::builder(list_data_type)
8100            .len(2)
8101            .add_child_data(value_data)
8102            .build()
8103            .unwrap();
8104        FixedSizeListArray::from(list_data)
8105    }
8106
8107    fn make_fixed_size_list_array_for_large_list() -> FixedSizeListArray {
8108        // Construct a value array
8109        let value_data = ArrayData::builder(DataType::Int64)
8110            .len(8)
8111            .add_buffer(Buffer::from_slice_ref([0i64, 1, 2, 3, 4, 5, 6, 7]))
8112            .build()
8113            .unwrap();
8114
8115        let list_data_type =
8116            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 4);
8117        let list_data = ArrayData::builder(list_data_type)
8118            .len(2)
8119            .add_child_data(value_data)
8120            .build()
8121            .unwrap();
8122        FixedSizeListArray::from(list_data)
8123    }
8124
8125    #[test]
8126    fn test_cast_map_dont_allow_change_of_order() {
8127        let string_builder = StringBuilder::new();
8128        let value_builder = StringBuilder::new();
8129        let mut builder = MapBuilder::new(
8130            Some(MapFieldNames {
8131                entry: "entries".to_string(),
8132                key: "key".to_string(),
8133                value: "value".to_string(),
8134            }),
8135            string_builder,
8136            value_builder,
8137        );
8138
8139        builder.keys().append_value("0");
8140        builder.values().append_value("test_val_1");
8141        builder.append(true).unwrap();
8142        builder.keys().append_value("1");
8143        builder.values().append_value("test_val_2");
8144        builder.append(true).unwrap();
8145
8146        // map builder returns unsorted map by default
8147        let array = builder.finish();
8148
8149        let new_ordered = true;
8150        let new_type = DataType::Map(
8151            Arc::new(Field::new(
8152                "entries",
8153                DataType::Struct(
8154                    vec![
8155                        Field::new("key", DataType::Utf8, false),
8156                        Field::new("value", DataType::Utf8, false),
8157                    ]
8158                    .into(),
8159                ),
8160                false,
8161            )),
8162            new_ordered,
8163        );
8164
8165        let new_array_result = cast(&array, &new_type.clone());
8166        assert!(!can_cast_types(array.data_type(), &new_type));
8167        assert!(
8168            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"#)
8169        );
8170    }
8171
8172    #[test]
8173    fn test_cast_map_dont_allow_when_container_cant_cast() {
8174        let string_builder = StringBuilder::new();
8175        let value_builder = IntervalDayTimeArray::builder(2);
8176        let mut builder = MapBuilder::new(
8177            Some(MapFieldNames {
8178                entry: "entries".to_string(),
8179                key: "key".to_string(),
8180                value: "value".to_string(),
8181            }),
8182            string_builder,
8183            value_builder,
8184        );
8185
8186        builder.keys().append_value("0");
8187        builder.values().append_value(IntervalDayTime::new(1, 1));
8188        builder.append(true).unwrap();
8189        builder.keys().append_value("1");
8190        builder.values().append_value(IntervalDayTime::new(2, 2));
8191        builder.append(true).unwrap();
8192
8193        // map builder returns unsorted map by default
8194        let array = builder.finish();
8195
8196        let new_ordered = true;
8197        let new_type = DataType::Map(
8198            Arc::new(Field::new(
8199                "entries",
8200                DataType::Struct(
8201                    vec![
8202                        Field::new("key", DataType::Utf8, false),
8203                        Field::new("value", DataType::Duration(TimeUnit::Second), false),
8204                    ]
8205                    .into(),
8206                ),
8207                false,
8208            )),
8209            new_ordered,
8210        );
8211
8212        let new_array_result = cast(&array, &new_type.clone());
8213        assert!(!can_cast_types(array.data_type(), &new_type));
8214        assert!(
8215            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"#)
8216        );
8217    }
8218
8219    #[test]
8220    fn test_cast_map_field_names() {
8221        let string_builder = StringBuilder::new();
8222        let value_builder = StringBuilder::new();
8223        let mut builder = MapBuilder::new(
8224            Some(MapFieldNames {
8225                entry: "entries".to_string(),
8226                key: "key".to_string(),
8227                value: "value".to_string(),
8228            }),
8229            string_builder,
8230            value_builder,
8231        );
8232
8233        builder.keys().append_value("0");
8234        builder.values().append_value("test_val_1");
8235        builder.append(true).unwrap();
8236        builder.keys().append_value("1");
8237        builder.values().append_value("test_val_2");
8238        builder.append(true).unwrap();
8239        builder.append(false).unwrap();
8240
8241        let array = builder.finish();
8242
8243        let new_type = DataType::Map(
8244            Arc::new(Field::new(
8245                "entries_new",
8246                DataType::Struct(
8247                    vec![
8248                        Field::new("key_new", DataType::Utf8, false),
8249                        Field::new("value_values", DataType::Utf8, false),
8250                    ]
8251                    .into(),
8252                ),
8253                false,
8254            )),
8255            false,
8256        );
8257
8258        assert_ne!(new_type, array.data_type().clone());
8259
8260        let new_array = cast(&array, &new_type.clone()).unwrap();
8261        assert_eq!(new_type, new_array.data_type().clone());
8262        let map_array = new_array.as_map();
8263
8264        assert_ne!(new_type, array.data_type().clone());
8265        assert_eq!(new_type, map_array.data_type().clone());
8266
8267        let key_string = map_array
8268            .keys()
8269            .as_any()
8270            .downcast_ref::<StringArray>()
8271            .unwrap()
8272            .into_iter()
8273            .flatten()
8274            .collect::<Vec<_>>();
8275        assert_eq!(&key_string, &vec!["0", "1"]);
8276
8277        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
8278        let values_string = values_string_array
8279            .as_any()
8280            .downcast_ref::<StringArray>()
8281            .unwrap()
8282            .into_iter()
8283            .flatten()
8284            .collect::<Vec<_>>();
8285        assert_eq!(&values_string, &vec!["test_val_1", "test_val_2"]);
8286
8287        assert_eq!(
8288            map_array.nulls(),
8289            Some(&NullBuffer::from(vec![true, true, false]))
8290        );
8291    }
8292
8293    #[test]
8294    fn test_cast_map_contained_values() {
8295        let string_builder = StringBuilder::new();
8296        let value_builder = Int8Builder::new();
8297        let mut builder = MapBuilder::new(
8298            Some(MapFieldNames {
8299                entry: "entries".to_string(),
8300                key: "key".to_string(),
8301                value: "value".to_string(),
8302            }),
8303            string_builder,
8304            value_builder,
8305        );
8306
8307        builder.keys().append_value("0");
8308        builder.values().append_value(44);
8309        builder.append(true).unwrap();
8310        builder.keys().append_value("1");
8311        builder.values().append_value(22);
8312        builder.append(true).unwrap();
8313
8314        let array = builder.finish();
8315
8316        let new_type = DataType::Map(
8317            Arc::new(Field::new(
8318                "entries",
8319                DataType::Struct(
8320                    vec![
8321                        Field::new("key", DataType::Utf8, false),
8322                        Field::new("value", DataType::Utf8, false),
8323                    ]
8324                    .into(),
8325                ),
8326                false,
8327            )),
8328            false,
8329        );
8330
8331        let new_array = cast(&array, &new_type.clone()).unwrap();
8332        assert_eq!(new_type, new_array.data_type().clone());
8333        let map_array = new_array.as_map();
8334
8335        assert_ne!(new_type, array.data_type().clone());
8336        assert_eq!(new_type, map_array.data_type().clone());
8337
8338        let key_string = map_array
8339            .keys()
8340            .as_any()
8341            .downcast_ref::<StringArray>()
8342            .unwrap()
8343            .into_iter()
8344            .flatten()
8345            .collect::<Vec<_>>();
8346        assert_eq!(&key_string, &vec!["0", "1"]);
8347
8348        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
8349        let values_string = values_string_array
8350            .as_any()
8351            .downcast_ref::<StringArray>()
8352            .unwrap()
8353            .into_iter()
8354            .flatten()
8355            .collect::<Vec<_>>();
8356        assert_eq!(&values_string, &vec!["44", "22"]);
8357    }
8358
8359    #[test]
8360    fn test_utf8_cast_offsets() {
8361        // test if offset of the array is taken into account during cast
8362        let str_array = StringArray::from(vec!["a", "b", "c"]);
8363        let str_array = str_array.slice(1, 2);
8364
8365        let out = cast(&str_array, &DataType::LargeUtf8).unwrap();
8366
8367        let large_str_array = out.as_any().downcast_ref::<LargeStringArray>().unwrap();
8368        let strs = large_str_array.into_iter().flatten().collect::<Vec<_>>();
8369        assert_eq!(strs, &["b", "c"])
8370    }
8371
8372    #[test]
8373    fn test_list_cast_offsets() {
8374        // test if offset of the array is taken into account during cast
8375        let array1 = make_list_array().slice(1, 2);
8376        let array2 = Arc::new(make_list_array()) as ArrayRef;
8377
8378        let dt = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8379        let out1 = cast(&array1, &dt).unwrap();
8380        let out2 = cast(&array2, &dt).unwrap();
8381
8382        assert_eq!(&out1, &out2.slice(1, 2))
8383    }
8384
8385    #[test]
8386    fn test_list_to_string() {
8387        let str_array = StringArray::from(vec!["a", "b", "c", "d", "e", "f", "g", "h"]);
8388        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
8389        let value_data = str_array.into_data();
8390
8391        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Utf8, true)));
8392        let list_data = ArrayData::builder(list_data_type)
8393            .len(3)
8394            .add_buffer(value_offsets)
8395            .add_child_data(value_data)
8396            .build()
8397            .unwrap();
8398        let array = Arc::new(ListArray::from(list_data)) as ArrayRef;
8399
8400        let out = cast(&array, &DataType::Utf8).unwrap();
8401        let out = out
8402            .as_any()
8403            .downcast_ref::<StringArray>()
8404            .unwrap()
8405            .into_iter()
8406            .flatten()
8407            .collect::<Vec<_>>();
8408        assert_eq!(&out, &vec!["[a, b, c]", "[d, e, f]", "[g, h]"]);
8409
8410        let out = cast(&array, &DataType::LargeUtf8).unwrap();
8411        let out = out
8412            .as_any()
8413            .downcast_ref::<LargeStringArray>()
8414            .unwrap()
8415            .into_iter()
8416            .flatten()
8417            .collect::<Vec<_>>();
8418        assert_eq!(&out, &vec!["[a, b, c]", "[d, e, f]", "[g, h]"]);
8419
8420        let array = Arc::new(make_list_array()) as ArrayRef;
8421        let out = cast(&array, &DataType::Utf8).unwrap();
8422        let out = out
8423            .as_any()
8424            .downcast_ref::<StringArray>()
8425            .unwrap()
8426            .into_iter()
8427            .flatten()
8428            .collect::<Vec<_>>();
8429        assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
8430
8431        let array = Arc::new(make_large_list_array()) as ArrayRef;
8432        let out = cast(&array, &DataType::LargeUtf8).unwrap();
8433        let out = out
8434            .as_any()
8435            .downcast_ref::<LargeStringArray>()
8436            .unwrap()
8437            .into_iter()
8438            .flatten()
8439            .collect::<Vec<_>>();
8440        assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
8441    }
8442
8443    #[test]
8444    fn test_cast_f64_to_decimal128() {
8445        // to reproduce https://github.com/apache/arrow-rs/issues/2997
8446
8447        let decimal_type = DataType::Decimal128(18, 2);
8448        let array = Float64Array::from(vec![
8449            Some(0.0699999999),
8450            Some(0.0659999999),
8451            Some(0.0650000000),
8452            Some(0.0649999999),
8453        ]);
8454        let array = Arc::new(array) as ArrayRef;
8455        generate_cast_test_case!(
8456            &array,
8457            Decimal128Array,
8458            &decimal_type,
8459            vec![
8460                Some(7_i128), // round up
8461                Some(7_i128), // round up
8462                Some(7_i128), // round up
8463                Some(6_i128), // round down
8464            ]
8465        );
8466
8467        let decimal_type = DataType::Decimal128(18, 3);
8468        let array = Float64Array::from(vec![
8469            Some(0.0699999999),
8470            Some(0.0659999999),
8471            Some(0.0650000000),
8472            Some(0.0649999999),
8473        ]);
8474        let array = Arc::new(array) as ArrayRef;
8475        generate_cast_test_case!(
8476            &array,
8477            Decimal128Array,
8478            &decimal_type,
8479            vec![
8480                Some(70_i128), // round up
8481                Some(66_i128), // round up
8482                Some(65_i128), // round down
8483                Some(65_i128), // round up
8484            ]
8485        );
8486    }
8487
8488    #[test]
8489    fn test_cast_numeric_to_decimal128_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::Decimal128(38, 30),
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::Decimal128(38, 30),
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_numeric_to_decimal256_overflow() {
8516        let array = Int64Array::from(vec![i64::MAX]);
8517        let array = Arc::new(array) as ArrayRef;
8518        let casted_array = cast_with_options(
8519            &array,
8520            &DataType::Decimal256(76, 76),
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::Decimal256(76, 76),
8532            &CastOptions {
8533                safe: false,
8534                format_options: FormatOptions::default(),
8535            },
8536        );
8537        assert!(casted_array.is_err());
8538    }
8539
8540    #[test]
8541    fn test_cast_floating_point_to_decimal128_precision_overflow() {
8542        let array = Float64Array::from(vec![1.1]);
8543        let array = Arc::new(array) as ArrayRef;
8544        let casted_array = cast_with_options(
8545            &array,
8546            &DataType::Decimal128(2, 2),
8547            &CastOptions {
8548                safe: true,
8549                format_options: FormatOptions::default(),
8550            },
8551        );
8552        assert!(casted_array.is_ok());
8553        assert!(casted_array.unwrap().is_null(0));
8554
8555        let casted_array = cast_with_options(
8556            &array,
8557            &DataType::Decimal128(2, 2),
8558            &CastOptions {
8559                safe: false,
8560                format_options: FormatOptions::default(),
8561            },
8562        );
8563        let err = casted_array.unwrap_err().to_string();
8564        let expected_error = "Invalid argument error: 110 is too large to store in a Decimal128 of precision 2. Max is 99";
8565        assert!(
8566            err.contains(expected_error),
8567            "did not find expected error '{expected_error}' in actual error '{err}'"
8568        );
8569    }
8570
8571    #[test]
8572    fn test_cast_floating_point_to_decimal256_precision_overflow() {
8573        let array = Float64Array::from(vec![1.1]);
8574        let array = Arc::new(array) as ArrayRef;
8575        let casted_array = cast_with_options(
8576            &array,
8577            &DataType::Decimal256(2, 2),
8578            &CastOptions {
8579                safe: true,
8580                format_options: FormatOptions::default(),
8581            },
8582        );
8583        assert!(casted_array.is_ok());
8584        assert!(casted_array.unwrap().is_null(0));
8585
8586        let casted_array = cast_with_options(
8587            &array,
8588            &DataType::Decimal256(2, 2),
8589            &CastOptions {
8590                safe: false,
8591                format_options: FormatOptions::default(),
8592            },
8593        );
8594        let err = casted_array.unwrap_err().to_string();
8595        let expected_error = "Invalid argument error: 110 is too large to store in a Decimal256 of precision 2. Max is 99";
8596        assert!(
8597            err.contains(expected_error),
8598            "did not find expected error '{expected_error}' in actual error '{err}'"
8599        );
8600    }
8601
8602    #[test]
8603    fn test_cast_floating_point_to_decimal128_overflow() {
8604        let array = Float64Array::from(vec![f64::MAX]);
8605        let array = Arc::new(array) as ArrayRef;
8606        let casted_array = cast_with_options(
8607            &array,
8608            &DataType::Decimal128(38, 30),
8609            &CastOptions {
8610                safe: true,
8611                format_options: FormatOptions::default(),
8612            },
8613        );
8614        assert!(casted_array.is_ok());
8615        assert!(casted_array.unwrap().is_null(0));
8616
8617        let casted_array = cast_with_options(
8618            &array,
8619            &DataType::Decimal128(38, 30),
8620            &CastOptions {
8621                safe: false,
8622                format_options: FormatOptions::default(),
8623            },
8624        );
8625        let err = casted_array.unwrap_err().to_string();
8626        let expected_error = "Cast error: Cannot cast to Decimal128(38, 30)";
8627        assert!(
8628            err.contains(expected_error),
8629            "did not find expected error '{expected_error}' in actual error '{err}'"
8630        );
8631    }
8632
8633    #[test]
8634    fn test_cast_floating_point_to_decimal256_overflow() {
8635        let array = Float64Array::from(vec![f64::MAX]);
8636        let array = Arc::new(array) as ArrayRef;
8637        let casted_array = cast_with_options(
8638            &array,
8639            &DataType::Decimal256(76, 50),
8640            &CastOptions {
8641                safe: true,
8642                format_options: FormatOptions::default(),
8643            },
8644        );
8645        assert!(casted_array.is_ok());
8646        assert!(casted_array.unwrap().is_null(0));
8647
8648        let casted_array = cast_with_options(
8649            &array,
8650            &DataType::Decimal256(76, 50),
8651            &CastOptions {
8652                safe: false,
8653                format_options: FormatOptions::default(),
8654            },
8655        );
8656        let err = casted_array.unwrap_err().to_string();
8657        let expected_error = "Cast error: Cannot cast to Decimal256(76, 50)";
8658        assert!(
8659            err.contains(expected_error),
8660            "did not find expected error '{expected_error}' in actual error '{err}'"
8661        );
8662    }
8663
8664    #[test]
8665    fn test_cast_decimal128_to_decimal128_negative_scale() {
8666        let input_type = DataType::Decimal128(20, 0);
8667        let output_type = DataType::Decimal128(20, -1);
8668        assert!(can_cast_types(&input_type, &output_type));
8669        let array = vec![Some(1123450), Some(2123455), Some(3123456), None];
8670        let input_decimal_array = create_decimal128_array(array, 20, 0).unwrap();
8671        let array = Arc::new(input_decimal_array) as ArrayRef;
8672        generate_cast_test_case!(
8673            &array,
8674            Decimal128Array,
8675            &output_type,
8676            vec![
8677                Some(112345_i128),
8678                Some(212346_i128),
8679                Some(312346_i128),
8680                None
8681            ]
8682        );
8683
8684        let casted_array = cast(&array, &output_type).unwrap();
8685        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8686
8687        assert_eq!("1123450", decimal_arr.value_as_string(0));
8688        assert_eq!("2123460", decimal_arr.value_as_string(1));
8689        assert_eq!("3123460", decimal_arr.value_as_string(2));
8690    }
8691
8692    #[test]
8693    fn test_cast_numeric_to_decimal128_negative() {
8694        let decimal_type = DataType::Decimal128(38, -1);
8695        let array = Arc::new(Int32Array::from(vec![
8696            Some(1123456),
8697            Some(2123456),
8698            Some(3123456),
8699        ])) as ArrayRef;
8700
8701        let casted_array = cast(&array, &decimal_type).unwrap();
8702        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8703
8704        assert_eq!("1123450", decimal_arr.value_as_string(0));
8705        assert_eq!("2123450", decimal_arr.value_as_string(1));
8706        assert_eq!("3123450", decimal_arr.value_as_string(2));
8707
8708        let array = Arc::new(Float32Array::from(vec![
8709            Some(1123.456),
8710            Some(2123.456),
8711            Some(3123.456),
8712        ])) as ArrayRef;
8713
8714        let casted_array = cast(&array, &decimal_type).unwrap();
8715        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8716
8717        assert_eq!("1120", decimal_arr.value_as_string(0));
8718        assert_eq!("2120", decimal_arr.value_as_string(1));
8719        assert_eq!("3120", decimal_arr.value_as_string(2));
8720    }
8721
8722    #[test]
8723    fn test_cast_decimal128_to_decimal128_negative() {
8724        let input_type = DataType::Decimal128(10, -1);
8725        let output_type = DataType::Decimal128(10, -2);
8726        assert!(can_cast_types(&input_type, &output_type));
8727        let array = vec![Some(123)];
8728        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
8729        let array = Arc::new(input_decimal_array) as ArrayRef;
8730        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(12_i128),]);
8731
8732        let casted_array = cast(&array, &output_type).unwrap();
8733        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8734
8735        assert_eq!("1200", decimal_arr.value_as_string(0));
8736
8737        let array = vec![Some(125)];
8738        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
8739        let array = Arc::new(input_decimal_array) as ArrayRef;
8740        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(13_i128),]);
8741
8742        let casted_array = cast(&array, &output_type).unwrap();
8743        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8744
8745        assert_eq!("1300", decimal_arr.value_as_string(0));
8746    }
8747
8748    #[test]
8749    fn test_cast_decimal128_to_decimal256_negative() {
8750        let input_type = DataType::Decimal128(10, 3);
8751        let output_type = DataType::Decimal256(10, 5);
8752        assert!(can_cast_types(&input_type, &output_type));
8753        let array = vec![Some(123456), Some(-123456)];
8754        let input_decimal_array = create_decimal128_array(array, 10, 3).unwrap();
8755        let array = Arc::new(input_decimal_array) as ArrayRef;
8756
8757        let hundred = i256::from_i128(100);
8758        generate_cast_test_case!(
8759            &array,
8760            Decimal256Array,
8761            &output_type,
8762            vec![
8763                Some(i256::from_i128(123456).mul_wrapping(hundred)),
8764                Some(i256::from_i128(-123456).mul_wrapping(hundred))
8765            ]
8766        );
8767    }
8768
8769    #[test]
8770    fn test_parse_string_to_decimal() {
8771        assert_eq!(
8772            Decimal128Type::format_decimal(
8773                parse_string_to_decimal_native::<Decimal128Type>("123.45", 2).unwrap(),
8774                38,
8775                2,
8776            ),
8777            "123.45"
8778        );
8779        assert_eq!(
8780            Decimal128Type::format_decimal(
8781                parse_string_to_decimal_native::<Decimal128Type>("12345", 2).unwrap(),
8782                38,
8783                2,
8784            ),
8785            "12345.00"
8786        );
8787        assert_eq!(
8788            Decimal128Type::format_decimal(
8789                parse_string_to_decimal_native::<Decimal128Type>("0.12345", 2).unwrap(),
8790                38,
8791                2,
8792            ),
8793            "0.12"
8794        );
8795        assert_eq!(
8796            Decimal128Type::format_decimal(
8797                parse_string_to_decimal_native::<Decimal128Type>(".12345", 2).unwrap(),
8798                38,
8799                2,
8800            ),
8801            "0.12"
8802        );
8803        assert_eq!(
8804            Decimal128Type::format_decimal(
8805                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
8806                38,
8807                2,
8808            ),
8809            "0.13"
8810        );
8811        assert_eq!(
8812            Decimal128Type::format_decimal(
8813                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
8814                38,
8815                2,
8816            ),
8817            "0.13"
8818        );
8819
8820        assert_eq!(
8821            Decimal256Type::format_decimal(
8822                parse_string_to_decimal_native::<Decimal256Type>("123.45", 3).unwrap(),
8823                38,
8824                3,
8825            ),
8826            "123.450"
8827        );
8828        assert_eq!(
8829            Decimal256Type::format_decimal(
8830                parse_string_to_decimal_native::<Decimal256Type>("12345", 3).unwrap(),
8831                38,
8832                3,
8833            ),
8834            "12345.000"
8835        );
8836        assert_eq!(
8837            Decimal256Type::format_decimal(
8838                parse_string_to_decimal_native::<Decimal256Type>("0.12345", 3).unwrap(),
8839                38,
8840                3,
8841            ),
8842            "0.123"
8843        );
8844        assert_eq!(
8845            Decimal256Type::format_decimal(
8846                parse_string_to_decimal_native::<Decimal256Type>(".12345", 3).unwrap(),
8847                38,
8848                3,
8849            ),
8850            "0.123"
8851        );
8852        assert_eq!(
8853            Decimal256Type::format_decimal(
8854                parse_string_to_decimal_native::<Decimal256Type>(".1265", 3).unwrap(),
8855                38,
8856                3,
8857            ),
8858            "0.127"
8859        );
8860    }
8861
8862    fn test_cast_string_to_decimal(array: ArrayRef) {
8863        // Decimal128
8864        let output_type = DataType::Decimal128(38, 2);
8865        assert!(can_cast_types(array.data_type(), &output_type));
8866
8867        let casted_array = cast(&array, &output_type).unwrap();
8868        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
8869
8870        assert_eq!("123.45", decimal_arr.value_as_string(0));
8871        assert_eq!("1.23", decimal_arr.value_as_string(1));
8872        assert_eq!("0.12", decimal_arr.value_as_string(2));
8873        assert_eq!("0.13", decimal_arr.value_as_string(3));
8874        assert_eq!("1.26", decimal_arr.value_as_string(4));
8875        assert_eq!("12345.00", decimal_arr.value_as_string(5));
8876        assert_eq!("12345.00", decimal_arr.value_as_string(6));
8877        assert_eq!("0.12", decimal_arr.value_as_string(7));
8878        assert_eq!("12.23", decimal_arr.value_as_string(8));
8879        assert!(decimal_arr.is_null(9));
8880        assert_eq!("0.00", decimal_arr.value_as_string(10));
8881        assert_eq!("0.00", decimal_arr.value_as_string(11));
8882        assert!(decimal_arr.is_null(12));
8883        assert_eq!("-1.23", decimal_arr.value_as_string(13));
8884        assert_eq!("-1.24", decimal_arr.value_as_string(14));
8885        assert_eq!("0.00", decimal_arr.value_as_string(15));
8886        assert_eq!("-123.00", decimal_arr.value_as_string(16));
8887        assert_eq!("-123.23", decimal_arr.value_as_string(17));
8888        assert_eq!("-0.12", decimal_arr.value_as_string(18));
8889        assert_eq!("1.23", decimal_arr.value_as_string(19));
8890        assert_eq!("1.24", decimal_arr.value_as_string(20));
8891        assert_eq!("0.00", decimal_arr.value_as_string(21));
8892        assert_eq!("123.00", decimal_arr.value_as_string(22));
8893        assert_eq!("123.23", decimal_arr.value_as_string(23));
8894        assert_eq!("0.12", decimal_arr.value_as_string(24));
8895        assert!(decimal_arr.is_null(25));
8896        assert!(decimal_arr.is_null(26));
8897        assert!(decimal_arr.is_null(27));
8898        assert_eq!("0.00", decimal_arr.value_as_string(28));
8899        assert_eq!("0.00", decimal_arr.value_as_string(29));
8900        assert_eq!("12345.00", decimal_arr.value_as_string(30));
8901        assert_eq!(decimal_arr.len(), 31);
8902
8903        // Decimal256
8904        let output_type = DataType::Decimal256(76, 3);
8905        assert!(can_cast_types(array.data_type(), &output_type));
8906
8907        let casted_array = cast(&array, &output_type).unwrap();
8908        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
8909
8910        assert_eq!("123.450", decimal_arr.value_as_string(0));
8911        assert_eq!("1.235", decimal_arr.value_as_string(1));
8912        assert_eq!("0.123", decimal_arr.value_as_string(2));
8913        assert_eq!("0.127", decimal_arr.value_as_string(3));
8914        assert_eq!("1.263", decimal_arr.value_as_string(4));
8915        assert_eq!("12345.000", decimal_arr.value_as_string(5));
8916        assert_eq!("12345.000", decimal_arr.value_as_string(6));
8917        assert_eq!("0.123", decimal_arr.value_as_string(7));
8918        assert_eq!("12.234", decimal_arr.value_as_string(8));
8919        assert!(decimal_arr.is_null(9));
8920        assert_eq!("0.000", decimal_arr.value_as_string(10));
8921        assert_eq!("0.000", decimal_arr.value_as_string(11));
8922        assert!(decimal_arr.is_null(12));
8923        assert_eq!("-1.235", decimal_arr.value_as_string(13));
8924        assert_eq!("-1.236", decimal_arr.value_as_string(14));
8925        assert_eq!("0.000", decimal_arr.value_as_string(15));
8926        assert_eq!("-123.000", decimal_arr.value_as_string(16));
8927        assert_eq!("-123.234", decimal_arr.value_as_string(17));
8928        assert_eq!("-0.123", decimal_arr.value_as_string(18));
8929        assert_eq!("1.235", decimal_arr.value_as_string(19));
8930        assert_eq!("1.236", decimal_arr.value_as_string(20));
8931        assert_eq!("0.000", decimal_arr.value_as_string(21));
8932        assert_eq!("123.000", decimal_arr.value_as_string(22));
8933        assert_eq!("123.234", decimal_arr.value_as_string(23));
8934        assert_eq!("0.123", decimal_arr.value_as_string(24));
8935        assert!(decimal_arr.is_null(25));
8936        assert!(decimal_arr.is_null(26));
8937        assert!(decimal_arr.is_null(27));
8938        assert_eq!("0.000", decimal_arr.value_as_string(28));
8939        assert_eq!("0.000", decimal_arr.value_as_string(29));
8940        assert_eq!("12345.000", decimal_arr.value_as_string(30));
8941        assert_eq!(decimal_arr.len(), 31);
8942    }
8943
8944    #[test]
8945    fn test_cast_utf8_to_decimal() {
8946        let str_array = StringArray::from(vec![
8947            Some("123.45"),
8948            Some("1.2345"),
8949            Some("0.12345"),
8950            Some("0.1267"),
8951            Some("1.263"),
8952            Some("12345.0"),
8953            Some("12345"),
8954            Some("000.123"),
8955            Some("12.234000"),
8956            None,
8957            Some(""),
8958            Some(" "),
8959            None,
8960            Some("-1.23499999"),
8961            Some("-1.23599999"),
8962            Some("-0.00001"),
8963            Some("-123"),
8964            Some("-123.234000"),
8965            Some("-000.123"),
8966            Some("+1.23499999"),
8967            Some("+1.23599999"),
8968            Some("+0.00001"),
8969            Some("+123"),
8970            Some("+123.234000"),
8971            Some("+000.123"),
8972            Some("1.-23499999"),
8973            Some("-1.-23499999"),
8974            Some("--1.23499999"),
8975            Some("0"),
8976            Some("000.000"),
8977            Some("0000000000000000012345.000"),
8978        ]);
8979        let array = Arc::new(str_array) as ArrayRef;
8980
8981        test_cast_string_to_decimal(array);
8982
8983        let test_cases = [
8984            (None, None),
8985            // (Some(""), None),
8986            // (Some("   "), None),
8987            (Some("0"), Some("0")),
8988            (Some("000.000"), Some("0")),
8989            (Some("12345"), Some("12345")),
8990            (Some("000000000000000000000000000012345"), Some("12345")),
8991            (Some("-123"), Some("-123")),
8992            (Some("+123"), Some("123")),
8993        ];
8994        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
8995        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
8996
8997        let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
8998        test_cast_string_to_decimal_scale_zero(array, &expected);
8999    }
9000
9001    #[test]
9002    fn test_cast_large_utf8_to_decimal() {
9003        let str_array = LargeStringArray::from(vec![
9004            Some("123.45"),
9005            Some("1.2345"),
9006            Some("0.12345"),
9007            Some("0.1267"),
9008            Some("1.263"),
9009            Some("12345.0"),
9010            Some("12345"),
9011            Some("000.123"),
9012            Some("12.234000"),
9013            None,
9014            Some(""),
9015            Some(" "),
9016            None,
9017            Some("-1.23499999"),
9018            Some("-1.23599999"),
9019            Some("-0.00001"),
9020            Some("-123"),
9021            Some("-123.234000"),
9022            Some("-000.123"),
9023            Some("+1.23499999"),
9024            Some("+1.23599999"),
9025            Some("+0.00001"),
9026            Some("+123"),
9027            Some("+123.234000"),
9028            Some("+000.123"),
9029            Some("1.-23499999"),
9030            Some("-1.-23499999"),
9031            Some("--1.23499999"),
9032            Some("0"),
9033            Some("000.000"),
9034            Some("0000000000000000012345.000"),
9035        ]);
9036        let array = Arc::new(str_array) as ArrayRef;
9037
9038        test_cast_string_to_decimal(array);
9039
9040        let test_cases = [
9041            (None, None),
9042            (Some(""), None),
9043            (Some("   "), None),
9044            (Some("0"), Some("0")),
9045            (Some("000.000"), Some("0")),
9046            (Some("12345"), Some("12345")),
9047            (Some("000000000000000000000000000012345"), Some("12345")),
9048            (Some("-123"), Some("-123")),
9049            (Some("+123"), Some("123")),
9050        ];
9051        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
9052        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
9053
9054        let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
9055        test_cast_string_to_decimal_scale_zero(array, &expected);
9056    }
9057
9058    fn test_cast_string_to_decimal_scale_zero(
9059        array: ArrayRef,
9060        expected_as_string: &[Option<&str>],
9061    ) {
9062        // Decimal128
9063        let output_type = DataType::Decimal128(38, 0);
9064        assert!(can_cast_types(array.data_type(), &output_type));
9065        let casted_array = cast(&array, &output_type).unwrap();
9066        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9067        assert_decimal_array_contents(decimal_arr, expected_as_string);
9068
9069        // Decimal256
9070        let output_type = DataType::Decimal256(76, 0);
9071        assert!(can_cast_types(array.data_type(), &output_type));
9072        let casted_array = cast(&array, &output_type).unwrap();
9073        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
9074        assert_decimal_array_contents(decimal_arr, expected_as_string);
9075    }
9076
9077    fn assert_decimal_array_contents<T>(
9078        array: &PrimitiveArray<T>,
9079        expected_as_string: &[Option<&str>],
9080    ) where
9081        T: DecimalType + ArrowPrimitiveType,
9082    {
9083        assert_eq!(array.len(), expected_as_string.len());
9084        for (i, expected) in expected_as_string.iter().enumerate() {
9085            let actual = if array.is_null(i) {
9086                None
9087            } else {
9088                Some(array.value_as_string(i))
9089            };
9090            let actual = actual.as_ref().map(|s| s.as_ref());
9091            assert_eq!(*expected, actual, "Expected at position {i}");
9092        }
9093    }
9094
9095    #[test]
9096    fn test_cast_invalid_utf8_to_decimal() {
9097        let str_array = StringArray::from(vec!["4.4.5", ". 0.123"]);
9098        let array = Arc::new(str_array) as ArrayRef;
9099
9100        // Safe cast
9101        let output_type = DataType::Decimal128(38, 2);
9102        let casted_array = cast(&array, &output_type).unwrap();
9103        assert!(casted_array.is_null(0));
9104        assert!(casted_array.is_null(1));
9105
9106        let output_type = DataType::Decimal256(76, 2);
9107        let casted_array = cast(&array, &output_type).unwrap();
9108        assert!(casted_array.is_null(0));
9109        assert!(casted_array.is_null(1));
9110
9111        // Non-safe cast
9112        let output_type = DataType::Decimal128(38, 2);
9113        let str_array = StringArray::from(vec!["4.4.5"]);
9114        let array = Arc::new(str_array) as ArrayRef;
9115        let option = CastOptions {
9116            safe: false,
9117            format_options: FormatOptions::default(),
9118        };
9119        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
9120        assert!(casted_err
9121            .to_string()
9122            .contains("Cannot cast string '4.4.5' to value of Decimal128(38, 10) type"));
9123
9124        let str_array = StringArray::from(vec![". 0.123"]);
9125        let array = Arc::new(str_array) as ArrayRef;
9126        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
9127        assert!(casted_err
9128            .to_string()
9129            .contains("Cannot cast string '. 0.123' to value of Decimal128(38, 10) type"));
9130    }
9131
9132    fn test_cast_string_to_decimal128_overflow(overflow_array: ArrayRef) {
9133        let output_type = DataType::Decimal128(38, 2);
9134        let casted_array = cast(&overflow_array, &output_type).unwrap();
9135        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9136
9137        assert!(decimal_arr.is_null(0));
9138        assert!(decimal_arr.is_null(1));
9139        assert!(decimal_arr.is_null(2));
9140        assert_eq!(
9141            "999999999999999999999999999999999999.99",
9142            decimal_arr.value_as_string(3)
9143        );
9144        assert_eq!(
9145            "100000000000000000000000000000000000.00",
9146            decimal_arr.value_as_string(4)
9147        );
9148    }
9149
9150    #[test]
9151    fn test_cast_string_to_decimal128_precision_overflow() {
9152        let array = StringArray::from(vec!["1000".to_string()]);
9153        let array = Arc::new(array) as ArrayRef;
9154        let casted_array = cast_with_options(
9155            &array,
9156            &DataType::Decimal128(10, 8),
9157            &CastOptions {
9158                safe: true,
9159                format_options: FormatOptions::default(),
9160            },
9161        );
9162        assert!(casted_array.is_ok());
9163        assert!(casted_array.unwrap().is_null(0));
9164
9165        let err = cast_with_options(
9166            &array,
9167            &DataType::Decimal128(10, 8),
9168            &CastOptions {
9169                safe: false,
9170                format_options: FormatOptions::default(),
9171            },
9172        );
9173        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());
9174    }
9175
9176    #[test]
9177    fn test_cast_utf8_to_decimal128_overflow() {
9178        let overflow_str_array = StringArray::from(vec![
9179            i128::MAX.to_string(),
9180            i128::MIN.to_string(),
9181            "99999999999999999999999999999999999999".to_string(),
9182            "999999999999999999999999999999999999.99".to_string(),
9183            "99999999999999999999999999999999999.999".to_string(),
9184        ]);
9185        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9186
9187        test_cast_string_to_decimal128_overflow(overflow_array);
9188    }
9189
9190    #[test]
9191    fn test_cast_large_utf8_to_decimal128_overflow() {
9192        let overflow_str_array = LargeStringArray::from(vec![
9193            i128::MAX.to_string(),
9194            i128::MIN.to_string(),
9195            "99999999999999999999999999999999999999".to_string(),
9196            "999999999999999999999999999999999999.99".to_string(),
9197            "99999999999999999999999999999999999.999".to_string(),
9198        ]);
9199        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9200
9201        test_cast_string_to_decimal128_overflow(overflow_array);
9202    }
9203
9204    fn test_cast_string_to_decimal256_overflow(overflow_array: ArrayRef) {
9205        let output_type = DataType::Decimal256(76, 2);
9206        let casted_array = cast(&overflow_array, &output_type).unwrap();
9207        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
9208
9209        assert_eq!(
9210            "170141183460469231731687303715884105727.00",
9211            decimal_arr.value_as_string(0)
9212        );
9213        assert_eq!(
9214            "-170141183460469231731687303715884105728.00",
9215            decimal_arr.value_as_string(1)
9216        );
9217        assert_eq!(
9218            "99999999999999999999999999999999999999.00",
9219            decimal_arr.value_as_string(2)
9220        );
9221        assert_eq!(
9222            "999999999999999999999999999999999999.99",
9223            decimal_arr.value_as_string(3)
9224        );
9225        assert_eq!(
9226            "100000000000000000000000000000000000.00",
9227            decimal_arr.value_as_string(4)
9228        );
9229        assert!(decimal_arr.is_null(5));
9230        assert!(decimal_arr.is_null(6));
9231    }
9232
9233    #[test]
9234    fn test_cast_string_to_decimal256_precision_overflow() {
9235        let array = StringArray::from(vec!["1000".to_string()]);
9236        let array = Arc::new(array) as ArrayRef;
9237        let casted_array = cast_with_options(
9238            &array,
9239            &DataType::Decimal256(10, 8),
9240            &CastOptions {
9241                safe: true,
9242                format_options: FormatOptions::default(),
9243            },
9244        );
9245        assert!(casted_array.is_ok());
9246        assert!(casted_array.unwrap().is_null(0));
9247
9248        let err = cast_with_options(
9249            &array,
9250            &DataType::Decimal256(10, 8),
9251            &CastOptions {
9252                safe: false,
9253                format_options: FormatOptions::default(),
9254            },
9255        );
9256        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());
9257    }
9258
9259    #[test]
9260    fn test_cast_utf8_to_decimal256_overflow() {
9261        let overflow_str_array = StringArray::from(vec![
9262            i128::MAX.to_string(),
9263            i128::MIN.to_string(),
9264            "99999999999999999999999999999999999999".to_string(),
9265            "999999999999999999999999999999999999.99".to_string(),
9266            "99999999999999999999999999999999999.999".to_string(),
9267            i256::MAX.to_string(),
9268            i256::MIN.to_string(),
9269        ]);
9270        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9271
9272        test_cast_string_to_decimal256_overflow(overflow_array);
9273    }
9274
9275    #[test]
9276    fn test_cast_large_utf8_to_decimal256_overflow() {
9277        let overflow_str_array = LargeStringArray::from(vec![
9278            i128::MAX.to_string(),
9279            i128::MIN.to_string(),
9280            "99999999999999999999999999999999999999".to_string(),
9281            "999999999999999999999999999999999999.99".to_string(),
9282            "99999999999999999999999999999999999.999".to_string(),
9283            i256::MAX.to_string(),
9284            i256::MIN.to_string(),
9285        ]);
9286        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9287
9288        test_cast_string_to_decimal256_overflow(overflow_array);
9289    }
9290
9291    #[test]
9292    fn test_cast_outside_supported_range_for_nanoseconds() {
9293        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";
9294
9295        let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
9296
9297        let cast_options = CastOptions {
9298            safe: false,
9299            format_options: FormatOptions::default(),
9300        };
9301
9302        let result = cast_string_to_timestamp::<i32, TimestampNanosecondType>(
9303            &array,
9304            &None::<Arc<str>>,
9305            &cast_options,
9306        );
9307
9308        let err = result.unwrap_err();
9309        assert_eq!(
9310            err.to_string(),
9311            format!(
9312                "Cast error: Overflow converting {} to Nanosecond. {}",
9313                array.value(0),
9314                EXPECTED_ERROR_MESSAGE
9315            )
9316        );
9317    }
9318
9319    #[test]
9320    fn test_cast_date32_to_timestamp() {
9321        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
9322        let array = Arc::new(a) as ArrayRef;
9323        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
9324        let c = b.as_primitive::<TimestampSecondType>();
9325        assert_eq!(1609459200, c.value(0));
9326        assert_eq!(1640995200, c.value(1));
9327        assert!(c.is_null(2));
9328    }
9329
9330    #[test]
9331    fn test_cast_date32_to_timestamp_ms() {
9332        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
9333        let array = Arc::new(a) as ArrayRef;
9334        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
9335        let c = b
9336            .as_any()
9337            .downcast_ref::<TimestampMillisecondArray>()
9338            .unwrap();
9339        assert_eq!(1609459200000, c.value(0));
9340        assert_eq!(1640995200000, c.value(1));
9341        assert!(c.is_null(2));
9342    }
9343
9344    #[test]
9345    fn test_cast_date32_to_timestamp_us() {
9346        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
9347        let array = Arc::new(a) as ArrayRef;
9348        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
9349        let c = b
9350            .as_any()
9351            .downcast_ref::<TimestampMicrosecondArray>()
9352            .unwrap();
9353        assert_eq!(1609459200000000, c.value(0));
9354        assert_eq!(1640995200000000, c.value(1));
9355        assert!(c.is_null(2));
9356    }
9357
9358    #[test]
9359    fn test_cast_date32_to_timestamp_ns() {
9360        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
9361        let array = Arc::new(a) as ArrayRef;
9362        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
9363        let c = b
9364            .as_any()
9365            .downcast_ref::<TimestampNanosecondArray>()
9366            .unwrap();
9367        assert_eq!(1609459200000000000, c.value(0));
9368        assert_eq!(1640995200000000000, c.value(1));
9369        assert!(c.is_null(2));
9370    }
9371
9372    #[test]
9373    fn test_timezone_cast() {
9374        let a = StringArray::from(vec![
9375            "2000-01-01T12:00:00", // date + time valid
9376            "2020-12-15T12:34:56", // date + time valid
9377        ]);
9378        let array = Arc::new(a) as ArrayRef;
9379        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
9380        let v = b.as_primitive::<TimestampNanosecondType>();
9381
9382        assert_eq!(v.value(0), 946728000000000000);
9383        assert_eq!(v.value(1), 1608035696000000000);
9384
9385        let b = cast(
9386            &b,
9387            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
9388        )
9389        .unwrap();
9390        let v = b.as_primitive::<TimestampNanosecondType>();
9391
9392        assert_eq!(v.value(0), 946728000000000000);
9393        assert_eq!(v.value(1), 1608035696000000000);
9394
9395        let b = cast(
9396            &b,
9397            &DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
9398        )
9399        .unwrap();
9400        let v = b.as_primitive::<TimestampMillisecondType>();
9401
9402        assert_eq!(v.value(0), 946728000000);
9403        assert_eq!(v.value(1), 1608035696000);
9404    }
9405
9406    #[test]
9407    fn test_cast_utf8_to_timestamp() {
9408        fn test_tz(tz: Arc<str>) {
9409            let valid = StringArray::from(vec![
9410                "2023-01-01 04:05:06.789000-08:00",
9411                "2023-01-01 04:05:06.789000-07:00",
9412                "2023-01-01 04:05:06.789 -0800",
9413                "2023-01-01 04:05:06.789 -08:00",
9414                "2023-01-01 040506 +0730",
9415                "2023-01-01 040506 +07:30",
9416                "2023-01-01 04:05:06.789",
9417                "2023-01-01 04:05:06",
9418                "2023-01-01",
9419            ]);
9420
9421            let array = Arc::new(valid) as ArrayRef;
9422            let b = cast_with_options(
9423                &array,
9424                &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
9425                &CastOptions {
9426                    safe: false,
9427                    format_options: FormatOptions::default(),
9428                },
9429            )
9430            .unwrap();
9431
9432            let tz = tz.as_ref().parse().unwrap();
9433
9434            let as_tz =
9435                |v: i64| as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap();
9436
9437            let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
9438            let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
9439
9440            let values = b.as_primitive::<TimestampNanosecondType>().values();
9441            let utc_results: Vec<_> = values.iter().map(as_utc).collect();
9442            let local_results: Vec<_> = values.iter().map(as_local).collect();
9443
9444            // Absolute timestamps should be parsed preserving the same UTC instant
9445            assert_eq!(
9446                &utc_results[..6],
9447                &[
9448                    "2023-01-01 12:05:06.789".to_string(),
9449                    "2023-01-01 11:05:06.789".to_string(),
9450                    "2023-01-01 12:05:06.789".to_string(),
9451                    "2023-01-01 12:05:06.789".to_string(),
9452                    "2022-12-31 20:35:06".to_string(),
9453                    "2022-12-31 20:35:06".to_string(),
9454                ]
9455            );
9456            // Non-absolute timestamps should be parsed preserving the same local instant
9457            assert_eq!(
9458                &local_results[6..],
9459                &[
9460                    "2023-01-01 04:05:06.789".to_string(),
9461                    "2023-01-01 04:05:06".to_string(),
9462                    "2023-01-01 00:00:00".to_string()
9463                ]
9464            )
9465        }
9466
9467        test_tz("+00:00".into());
9468        test_tz("+02:00".into());
9469    }
9470
9471    #[test]
9472    fn test_cast_invalid_utf8() {
9473        let v1: &[u8] = b"\xFF invalid";
9474        let v2: &[u8] = b"\x00 Foo";
9475        let s = BinaryArray::from(vec![v1, v2]);
9476        let options = CastOptions {
9477            safe: true,
9478            format_options: FormatOptions::default(),
9479        };
9480        let array = cast_with_options(&s, &DataType::Utf8, &options).unwrap();
9481        let a = array.as_string::<i32>();
9482        a.to_data().validate_full().unwrap();
9483
9484        assert_eq!(a.null_count(), 1);
9485        assert_eq!(a.len(), 2);
9486        assert!(a.is_null(0));
9487        assert_eq!(a.value(0), "");
9488        assert_eq!(a.value(1), "\x00 Foo");
9489    }
9490
9491    #[test]
9492    fn test_cast_utf8_to_timestamptz() {
9493        let valid = StringArray::from(vec!["2023-01-01"]);
9494
9495        let array = Arc::new(valid) as ArrayRef;
9496        let b = cast(
9497            &array,
9498            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
9499        )
9500        .unwrap();
9501
9502        let expect = DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into()));
9503
9504        assert_eq!(b.data_type(), &expect);
9505        let c = b
9506            .as_any()
9507            .downcast_ref::<TimestampNanosecondArray>()
9508            .unwrap();
9509        assert_eq!(1672531200000000000, c.value(0));
9510    }
9511
9512    #[test]
9513    fn test_cast_decimal_to_string() {
9514        assert!(can_cast_types(
9515            &DataType::Decimal128(10, 4),
9516            &DataType::Utf8View
9517        ));
9518        assert!(can_cast_types(
9519            &DataType::Decimal256(38, 10),
9520            &DataType::Utf8View
9521        ));
9522
9523        macro_rules! assert_decimal_values {
9524            ($array:expr) => {
9525                let c = $array;
9526                assert_eq!("1123.454", c.value(0));
9527                assert_eq!("2123.456", c.value(1));
9528                assert_eq!("-3123.453", c.value(2));
9529                assert_eq!("-3123.456", c.value(3));
9530                assert_eq!("0.000", c.value(4));
9531                assert_eq!("0.123", c.value(5));
9532                assert_eq!("1234.567", c.value(6));
9533                assert_eq!("-1234.567", c.value(7));
9534                assert!(c.is_null(8));
9535            };
9536        }
9537
9538        fn test_decimal_to_string<IN: ArrowPrimitiveType, OffsetSize: OffsetSizeTrait>(
9539            output_type: DataType,
9540            array: PrimitiveArray<IN>,
9541        ) {
9542            let b = cast(&array, &output_type).unwrap();
9543
9544            assert_eq!(b.data_type(), &output_type);
9545            match b.data_type() {
9546                DataType::Utf8View => {
9547                    let c = b.as_string_view();
9548                    assert_decimal_values!(c);
9549                }
9550                DataType::Utf8 | DataType::LargeUtf8 => {
9551                    let c = b.as_string::<OffsetSize>();
9552                    assert_decimal_values!(c);
9553                }
9554                _ => (),
9555            }
9556        }
9557
9558        let array128: Vec<Option<i128>> = vec![
9559            Some(1123454),
9560            Some(2123456),
9561            Some(-3123453),
9562            Some(-3123456),
9563            Some(0),
9564            Some(123),
9565            Some(123456789),
9566            Some(-123456789),
9567            None,
9568        ];
9569        let array256: Vec<Option<i256>> = array128
9570            .iter()
9571            .map(|num| num.map(i256::from_i128))
9572            .collect();
9573
9574        test_decimal_to_string::<Decimal128Type, i32>(
9575            DataType::Utf8View,
9576            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
9577        );
9578        test_decimal_to_string::<Decimal128Type, i32>(
9579            DataType::Utf8,
9580            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
9581        );
9582        test_decimal_to_string::<Decimal128Type, i64>(
9583            DataType::LargeUtf8,
9584            create_decimal128_array(array128, 7, 3).unwrap(),
9585        );
9586
9587        test_decimal_to_string::<Decimal256Type, i32>(
9588            DataType::Utf8View,
9589            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
9590        );
9591        test_decimal_to_string::<Decimal256Type, i32>(
9592            DataType::Utf8,
9593            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
9594        );
9595        test_decimal_to_string::<Decimal256Type, i64>(
9596            DataType::LargeUtf8,
9597            create_decimal256_array(array256, 7, 3).unwrap(),
9598        );
9599    }
9600
9601    #[test]
9602    fn test_cast_numeric_to_decimal128_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::Decimal128(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::Decimal128(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 Decimal128 of precision 7. Max is 9999999", err.unwrap_err().to_string());
9625    }
9626
9627    #[test]
9628    fn test_cast_numeric_to_decimal256_precision_overflow() {
9629        let array = Int64Array::from(vec![1234567]);
9630        let array = Arc::new(array) as ArrayRef;
9631        let casted_array = cast_with_options(
9632            &array,
9633            &DataType::Decimal256(7, 3),
9634            &CastOptions {
9635                safe: true,
9636                format_options: FormatOptions::default(),
9637            },
9638        );
9639        assert!(casted_array.is_ok());
9640        assert!(casted_array.unwrap().is_null(0));
9641
9642        let err = cast_with_options(
9643            &array,
9644            &DataType::Decimal256(7, 3),
9645            &CastOptions {
9646                safe: false,
9647                format_options: FormatOptions::default(),
9648            },
9649        );
9650        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());
9651    }
9652
9653    /// helper function to test casting from duration to interval
9654    fn cast_from_duration_to_interval<T: ArrowTemporalType<Native = i64>>(
9655        array: Vec<i64>,
9656        cast_options: &CastOptions,
9657    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
9658        let array = PrimitiveArray::<T>::new(array.into(), None);
9659        let array = Arc::new(array) as ArrayRef;
9660        let interval = DataType::Interval(IntervalUnit::MonthDayNano);
9661        let out = cast_with_options(&array, &interval, cast_options)?;
9662        let out = out.as_primitive::<IntervalMonthDayNanoType>().clone();
9663        Ok(out)
9664    }
9665
9666    #[test]
9667    fn test_cast_from_duration_to_interval() {
9668        // from duration second to interval month day nano
9669        let array = vec![1234567];
9670        let casted_array =
9671            cast_from_duration_to_interval::<DurationSecondType>(array, &CastOptions::default())
9672                .unwrap();
9673        assert_eq!(
9674            casted_array.data_type(),
9675            &DataType::Interval(IntervalUnit::MonthDayNano)
9676        );
9677        assert_eq!(
9678            casted_array.value(0),
9679            IntervalMonthDayNano::new(0, 0, 1234567000000000)
9680        );
9681
9682        let array = vec![i64::MAX];
9683        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
9684            array.clone(),
9685            &CastOptions::default(),
9686        )
9687        .unwrap();
9688        assert!(!casted_array.is_valid(0));
9689
9690        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
9691            array,
9692            &CastOptions {
9693                safe: false,
9694                format_options: FormatOptions::default(),
9695            },
9696        );
9697        assert!(casted_array.is_err());
9698
9699        // from duration millisecond to interval month day nano
9700        let array = vec![1234567];
9701        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
9702            array,
9703            &CastOptions::default(),
9704        )
9705        .unwrap();
9706        assert_eq!(
9707            casted_array.data_type(),
9708            &DataType::Interval(IntervalUnit::MonthDayNano)
9709        );
9710        assert_eq!(
9711            casted_array.value(0),
9712            IntervalMonthDayNano::new(0, 0, 1234567000000)
9713        );
9714
9715        let array = vec![i64::MAX];
9716        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
9717            array.clone(),
9718            &CastOptions::default(),
9719        )
9720        .unwrap();
9721        assert!(!casted_array.is_valid(0));
9722
9723        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
9724            array,
9725            &CastOptions {
9726                safe: false,
9727                format_options: FormatOptions::default(),
9728            },
9729        );
9730        assert!(casted_array.is_err());
9731
9732        // from duration microsecond to interval month day nano
9733        let array = vec![1234567];
9734        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
9735            array,
9736            &CastOptions::default(),
9737        )
9738        .unwrap();
9739        assert_eq!(
9740            casted_array.data_type(),
9741            &DataType::Interval(IntervalUnit::MonthDayNano)
9742        );
9743        assert_eq!(
9744            casted_array.value(0),
9745            IntervalMonthDayNano::new(0, 0, 1234567000)
9746        );
9747
9748        let array = vec![i64::MAX];
9749        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
9750            array.clone(),
9751            &CastOptions::default(),
9752        )
9753        .unwrap();
9754        assert!(!casted_array.is_valid(0));
9755
9756        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
9757            array,
9758            &CastOptions {
9759                safe: false,
9760                format_options: FormatOptions::default(),
9761            },
9762        );
9763        assert!(casted_array.is_err());
9764
9765        // from duration nanosecond to interval month day nano
9766        let array = vec![1234567];
9767        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
9768            array,
9769            &CastOptions::default(),
9770        )
9771        .unwrap();
9772        assert_eq!(
9773            casted_array.data_type(),
9774            &DataType::Interval(IntervalUnit::MonthDayNano)
9775        );
9776        assert_eq!(
9777            casted_array.value(0),
9778            IntervalMonthDayNano::new(0, 0, 1234567)
9779        );
9780
9781        let array = vec![i64::MAX];
9782        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
9783            array,
9784            &CastOptions {
9785                safe: false,
9786                format_options: FormatOptions::default(),
9787            },
9788        )
9789        .unwrap();
9790        assert_eq!(
9791            casted_array.value(0),
9792            IntervalMonthDayNano::new(0, 0, i64::MAX)
9793        );
9794    }
9795
9796    /// helper function to test casting from interval to duration
9797    fn cast_from_interval_to_duration<T: ArrowTemporalType>(
9798        array: &IntervalMonthDayNanoArray,
9799        cast_options: &CastOptions,
9800    ) -> Result<PrimitiveArray<T>, ArrowError> {
9801        let casted_array = cast_with_options(&array, &T::DATA_TYPE, cast_options)?;
9802        casted_array
9803            .as_any()
9804            .downcast_ref::<PrimitiveArray<T>>()
9805            .ok_or_else(|| {
9806                ArrowError::ComputeError(format!("Failed to downcast to {}", T::DATA_TYPE))
9807            })
9808            .cloned()
9809    }
9810
9811    #[test]
9812    fn test_cast_from_interval_to_duration() {
9813        let nullable = CastOptions::default();
9814        let fallible = CastOptions {
9815            safe: false,
9816            format_options: FormatOptions::default(),
9817        };
9818        let v = IntervalMonthDayNano::new(0, 0, 1234567);
9819
9820        // from interval month day nano to duration second
9821        let array = vec![v].into();
9822        let casted_array: DurationSecondArray =
9823            cast_from_interval_to_duration(&array, &nullable).unwrap();
9824        assert_eq!(casted_array.value(0), 0);
9825
9826        let array = vec![IntervalMonthDayNano::MAX].into();
9827        let casted_array: DurationSecondArray =
9828            cast_from_interval_to_duration(&array, &nullable).unwrap();
9829        assert!(!casted_array.is_valid(0));
9830
9831        let res = cast_from_interval_to_duration::<DurationSecondType>(&array, &fallible);
9832        assert!(res.is_err());
9833
9834        // from interval month day nano to duration millisecond
9835        let array = vec![v].into();
9836        let casted_array: DurationMillisecondArray =
9837            cast_from_interval_to_duration(&array, &nullable).unwrap();
9838        assert_eq!(casted_array.value(0), 1);
9839
9840        let array = vec![IntervalMonthDayNano::MAX].into();
9841        let casted_array: DurationMillisecondArray =
9842            cast_from_interval_to_duration(&array, &nullable).unwrap();
9843        assert!(!casted_array.is_valid(0));
9844
9845        let res = cast_from_interval_to_duration::<DurationMillisecondType>(&array, &fallible);
9846        assert!(res.is_err());
9847
9848        // from interval month day nano to duration microsecond
9849        let array = vec![v].into();
9850        let casted_array: DurationMicrosecondArray =
9851            cast_from_interval_to_duration(&array, &nullable).unwrap();
9852        assert_eq!(casted_array.value(0), 1234);
9853
9854        let array = vec![IntervalMonthDayNano::MAX].into();
9855        let casted_array =
9856            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &nullable).unwrap();
9857        assert!(!casted_array.is_valid(0));
9858
9859        let casted_array =
9860            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &fallible);
9861        assert!(casted_array.is_err());
9862
9863        // from interval month day nano to duration nanosecond
9864        let array = vec![v].into();
9865        let casted_array: DurationNanosecondArray =
9866            cast_from_interval_to_duration(&array, &nullable).unwrap();
9867        assert_eq!(casted_array.value(0), 1234567);
9868
9869        let array = vec![IntervalMonthDayNano::MAX].into();
9870        let casted_array: DurationNanosecondArray =
9871            cast_from_interval_to_duration(&array, &nullable).unwrap();
9872        assert!(!casted_array.is_valid(0));
9873
9874        let casted_array =
9875            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &fallible);
9876        assert!(casted_array.is_err());
9877
9878        let array = vec![
9879            IntervalMonthDayNanoType::make_value(0, 1, 0),
9880            IntervalMonthDayNanoType::make_value(-1, 0, 0),
9881            IntervalMonthDayNanoType::make_value(1, 1, 0),
9882            IntervalMonthDayNanoType::make_value(1, 0, 1),
9883            IntervalMonthDayNanoType::make_value(0, 0, -1),
9884        ]
9885        .into();
9886        let casted_array =
9887            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable).unwrap();
9888        assert!(!casted_array.is_valid(0));
9889        assert!(!casted_array.is_valid(1));
9890        assert!(!casted_array.is_valid(2));
9891        assert!(!casted_array.is_valid(3));
9892        assert!(casted_array.is_valid(4));
9893        assert_eq!(casted_array.value(4), -1);
9894    }
9895
9896    /// helper function to test casting from interval year month to interval month day nano
9897    fn cast_from_interval_year_month_to_interval_month_day_nano(
9898        array: Vec<i32>,
9899        cast_options: &CastOptions,
9900    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
9901        let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
9902        let array = Arc::new(array) as ArrayRef;
9903        let casted_array = cast_with_options(
9904            &array,
9905            &DataType::Interval(IntervalUnit::MonthDayNano),
9906            cast_options,
9907        )?;
9908        casted_array
9909            .as_any()
9910            .downcast_ref::<IntervalMonthDayNanoArray>()
9911            .ok_or_else(|| {
9912                ArrowError::ComputeError(
9913                    "Failed to downcast to IntervalMonthDayNanoArray".to_string(),
9914                )
9915            })
9916            .cloned()
9917    }
9918
9919    #[test]
9920    fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
9921        // from interval year month to interval month day nano
9922        let array = vec![1234567];
9923        let casted_array = cast_from_interval_year_month_to_interval_month_day_nano(
9924            array,
9925            &CastOptions::default(),
9926        )
9927        .unwrap();
9928        assert_eq!(
9929            casted_array.data_type(),
9930            &DataType::Interval(IntervalUnit::MonthDayNano)
9931        );
9932        assert_eq!(
9933            casted_array.value(0),
9934            IntervalMonthDayNano::new(1234567, 0, 0)
9935        );
9936    }
9937
9938    /// helper function to test casting from interval day time to interval month day nano
9939    fn cast_from_interval_day_time_to_interval_month_day_nano(
9940        array: Vec<IntervalDayTime>,
9941        cast_options: &CastOptions,
9942    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
9943        let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
9944        let array = Arc::new(array) as ArrayRef;
9945        let casted_array = cast_with_options(
9946            &array,
9947            &DataType::Interval(IntervalUnit::MonthDayNano),
9948            cast_options,
9949        )?;
9950        Ok(casted_array
9951            .as_primitive::<IntervalMonthDayNanoType>()
9952            .clone())
9953    }
9954
9955    #[test]
9956    fn test_cast_from_interval_day_time_to_interval_month_day_nano() {
9957        // from interval day time to interval month day nano
9958        let array = vec![IntervalDayTime::new(123, 0)];
9959        let casted_array =
9960            cast_from_interval_day_time_to_interval_month_day_nano(array, &CastOptions::default())
9961                .unwrap();
9962        assert_eq!(
9963            casted_array.data_type(),
9964            &DataType::Interval(IntervalUnit::MonthDayNano)
9965        );
9966        assert_eq!(casted_array.value(0), IntervalMonthDayNano::new(0, 123, 0));
9967    }
9968
9969    #[test]
9970    fn test_cast_below_unixtimestamp() {
9971        let valid = StringArray::from(vec![
9972            "1900-01-03 23:59:59",
9973            "1969-12-31 00:00:01",
9974            "1989-12-31 00:00:01",
9975        ]);
9976
9977        let array = Arc::new(valid) as ArrayRef;
9978        let casted_array = cast_with_options(
9979            &array,
9980            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
9981            &CastOptions {
9982                safe: false,
9983                format_options: FormatOptions::default(),
9984            },
9985        )
9986        .unwrap();
9987
9988        let ts_array = casted_array
9989            .as_primitive::<TimestampNanosecondType>()
9990            .values()
9991            .iter()
9992            .map(|ts| ts / 1_000_000)
9993            .collect::<Vec<_>>();
9994
9995        let array = TimestampMillisecondArray::from(ts_array).with_timezone("+00:00".to_string());
9996        let casted_array = cast(&array, &DataType::Date32).unwrap();
9997        let date_array = casted_array.as_primitive::<Date32Type>();
9998        let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
9999        let string_array = casted_array.as_string::<i32>();
10000        assert_eq!("1900-01-03", string_array.value(0));
10001        assert_eq!("1969-12-31", string_array.value(1));
10002        assert_eq!("1989-12-31", string_array.value(2));
10003    }
10004
10005    #[test]
10006    fn test_nested_list() {
10007        let mut list = ListBuilder::new(Int32Builder::new());
10008        list.append_value([Some(1), Some(2), Some(3)]);
10009        list.append_value([Some(4), None, Some(6)]);
10010        let list = list.finish();
10011
10012        let to_field = Field::new("nested", list.data_type().clone(), false);
10013        let to = DataType::List(Arc::new(to_field));
10014        let out = cast(&list, &to).unwrap();
10015        let opts = FormatOptions::default().with_null("null");
10016        let formatted = ArrayFormatter::try_new(out.as_ref(), &opts).unwrap();
10017
10018        assert_eq!(formatted.value(0).to_string(), "[[1], [2], [3]]");
10019        assert_eq!(formatted.value(1).to_string(), "[[4], [null], [6]]");
10020    }
10021
10022    #[test]
10023    fn test_nested_list_cast() {
10024        let mut builder = ListBuilder::new(ListBuilder::new(Int32Builder::new()));
10025        builder.append_value([Some([Some(1), Some(2), None]), None]);
10026        builder.append_value([None, Some([]), None]);
10027        builder.append_null();
10028        builder.append_value([Some([Some(2), Some(3)])]);
10029        let start = builder.finish();
10030
10031        let mut builder = LargeListBuilder::new(LargeListBuilder::new(Int8Builder::new()));
10032        builder.append_value([Some([Some(1), Some(2), None]), None]);
10033        builder.append_value([None, Some([]), None]);
10034        builder.append_null();
10035        builder.append_value([Some([Some(2), Some(3)])]);
10036        let expected = builder.finish();
10037
10038        let actual = cast(&start, expected.data_type()).unwrap();
10039        assert_eq!(actual.as_ref(), &expected);
10040    }
10041
10042    const CAST_OPTIONS: CastOptions<'static> = CastOptions {
10043        safe: true,
10044        format_options: FormatOptions::new(),
10045    };
10046
10047    #[test]
10048    #[allow(clippy::assertions_on_constants)]
10049    fn test_const_options() {
10050        assert!(CAST_OPTIONS.safe)
10051    }
10052
10053    #[test]
10054    fn test_list_format_options() {
10055        let options = CastOptions {
10056            safe: false,
10057            format_options: FormatOptions::default().with_null("null"),
10058        };
10059        let array = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
10060            Some(vec![Some(0), Some(1), Some(2)]),
10061            Some(vec![Some(0), None, Some(2)]),
10062        ]);
10063        let a = cast_with_options(&array, &DataType::Utf8, &options).unwrap();
10064        let r: Vec<_> = a.as_string::<i32>().iter().flatten().collect();
10065        assert_eq!(r, &["[0, 1, 2]", "[0, null, 2]"]);
10066    }
10067    #[test]
10068    fn test_cast_string_to_timestamp_invalid_tz() {
10069        // content after Z should be ignored
10070        let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
10071        let array = StringArray::from(vec![Some(bad_timestamp)]);
10072
10073        let data_types = [
10074            DataType::Timestamp(TimeUnit::Second, None),
10075            DataType::Timestamp(TimeUnit::Millisecond, None),
10076            DataType::Timestamp(TimeUnit::Microsecond, None),
10077            DataType::Timestamp(TimeUnit::Nanosecond, None),
10078        ];
10079
10080        let cast_options = CastOptions {
10081            safe: false,
10082            ..Default::default()
10083        };
10084
10085        for dt in data_types {
10086            assert_eq!(
10087                cast_with_options(&array, &dt, &cast_options)
10088                    .unwrap_err()
10089                    .to_string(),
10090                "Parser error: Invalid timezone \"ZZTOP\": only offset based timezones supported without chrono-tz feature"
10091            );
10092        }
10093    }
10094    #[test]
10095    fn test_cast_struct_to_struct() {
10096        let struct_type = DataType::Struct(
10097            vec![
10098                Field::new("a", DataType::Boolean, false),
10099                Field::new("b", DataType::Int32, false),
10100            ]
10101            .into(),
10102        );
10103        let to_type = DataType::Struct(
10104            vec![
10105                Field::new("a", DataType::Utf8, false),
10106                Field::new("b", DataType::Utf8, false),
10107            ]
10108            .into(),
10109        );
10110        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10111        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
10112        let struct_array = StructArray::from(vec![
10113            (
10114                Arc::new(Field::new("b", DataType::Boolean, false)),
10115                boolean.clone() as ArrayRef,
10116            ),
10117            (
10118                Arc::new(Field::new("c", DataType::Int32, false)),
10119                int.clone() as ArrayRef,
10120            ),
10121        ]);
10122        let casted_array = cast(&struct_array, &to_type).unwrap();
10123        let casted_array = casted_array.as_struct();
10124        assert_eq!(casted_array.data_type(), &to_type);
10125        let casted_boolean_array = casted_array
10126            .column(0)
10127            .as_string::<i32>()
10128            .into_iter()
10129            .flatten()
10130            .collect::<Vec<_>>();
10131        let casted_int_array = casted_array
10132            .column(1)
10133            .as_string::<i32>()
10134            .into_iter()
10135            .flatten()
10136            .collect::<Vec<_>>();
10137        assert_eq!(casted_boolean_array, vec!["false", "false", "true", "true"]);
10138        assert_eq!(casted_int_array, vec!["42", "28", "19", "31"]);
10139
10140        // test for can't cast
10141        let to_type = DataType::Struct(
10142            vec![
10143                Field::new("a", DataType::Date32, false),
10144                Field::new("b", DataType::Utf8, false),
10145            ]
10146            .into(),
10147        );
10148        assert!(!can_cast_types(&struct_type, &to_type));
10149        let result = cast(&struct_array, &to_type);
10150        assert_eq!(
10151            "Cast error: Casting from Boolean to Date32 not supported",
10152            result.unwrap_err().to_string()
10153        );
10154    }
10155
10156    #[test]
10157    fn test_cast_struct_to_struct_nullability() {
10158        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10159        let int = Arc::new(Int32Array::from(vec![Some(42), None, Some(19), None]));
10160        let struct_array = StructArray::from(vec![
10161            (
10162                Arc::new(Field::new("b", DataType::Boolean, false)),
10163                boolean.clone() as ArrayRef,
10164            ),
10165            (
10166                Arc::new(Field::new("c", DataType::Int32, true)),
10167                int.clone() as ArrayRef,
10168            ),
10169        ]);
10170
10171        // okay: nullable to nullable
10172        let to_type = DataType::Struct(
10173            vec![
10174                Field::new("a", DataType::Utf8, false),
10175                Field::new("b", DataType::Utf8, true),
10176            ]
10177            .into(),
10178        );
10179        cast(&struct_array, &to_type).expect("Cast nullable to nullable struct field should work");
10180
10181        // error: nullable to non-nullable
10182        let to_type = DataType::Struct(
10183            vec![
10184                Field::new("a", DataType::Utf8, false),
10185                Field::new("b", DataType::Utf8, false),
10186            ]
10187            .into(),
10188        );
10189        cast(&struct_array, &to_type)
10190            .expect_err("Cast nullable to non-nullable struct field should fail");
10191
10192        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10193        let int = Arc::new(Int32Array::from(vec![i32::MAX, 25, 1, 100]));
10194        let struct_array = StructArray::from(vec![
10195            (
10196                Arc::new(Field::new("b", DataType::Boolean, false)),
10197                boolean.clone() as ArrayRef,
10198            ),
10199            (
10200                Arc::new(Field::new("c", DataType::Int32, false)),
10201                int.clone() as ArrayRef,
10202            ),
10203        ]);
10204
10205        // okay: non-nullable to non-nullable
10206        let to_type = DataType::Struct(
10207            vec![
10208                Field::new("a", DataType::Utf8, false),
10209                Field::new("b", DataType::Utf8, false),
10210            ]
10211            .into(),
10212        );
10213        cast(&struct_array, &to_type)
10214            .expect("Cast non-nullable to non-nullable struct field should work");
10215
10216        // err: non-nullable to non-nullable but overflowing return null during casting
10217        let to_type = DataType::Struct(
10218            vec![
10219                Field::new("a", DataType::Utf8, false),
10220                Field::new("b", DataType::Int8, false),
10221            ]
10222            .into(),
10223        );
10224        cast(&struct_array, &to_type).expect_err(
10225            "Cast non-nullable to non-nullable struct field returning null should fail",
10226        );
10227    }
10228
10229    #[test]
10230    fn test_cast_struct_to_non_struct() {
10231        let boolean = Arc::new(BooleanArray::from(vec![true, false]));
10232        let struct_array = StructArray::from(vec![(
10233            Arc::new(Field::new("a", DataType::Boolean, false)),
10234            boolean.clone() as ArrayRef,
10235        )]);
10236        let to_type = DataType::Utf8;
10237        let result = cast(&struct_array, &to_type);
10238        assert_eq!(
10239            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"#,
10240            result.unwrap_err().to_string()
10241        );
10242    }
10243
10244    #[test]
10245    fn test_cast_non_struct_to_struct() {
10246        let array = StringArray::from(vec!["a", "b"]);
10247        let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
10248        let result = cast(&array, &to_type);
10249        assert_eq!(
10250            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"#,
10251            result.unwrap_err().to_string()
10252        );
10253    }
10254
10255    fn run_decimal_cast_test_case_between_multiple_types(t: DecimalCastTestConfig) {
10256        run_decimal_cast_test_case::<Decimal128Type, Decimal128Type>(t.clone());
10257        run_decimal_cast_test_case::<Decimal128Type, Decimal256Type>(t.clone());
10258        run_decimal_cast_test_case::<Decimal256Type, Decimal128Type>(t.clone());
10259        run_decimal_cast_test_case::<Decimal256Type, Decimal256Type>(t.clone());
10260    }
10261
10262    #[test]
10263    fn test_decimal_to_decimal_coverage() {
10264        let test_cases = [
10265            // increase precision, increase scale, infallible
10266            DecimalCastTestConfig {
10267                input_prec: 5,
10268                input_scale: 1,
10269                input_repr: 99999, // 9999.9
10270                output_prec: 10,
10271                output_scale: 6,
10272                expected_output_repr: Ok(9999900000), // 9999.900000
10273            },
10274            // increase precision, increase scale, fallible, safe
10275            DecimalCastTestConfig {
10276                input_prec: 5,
10277                input_scale: 1,
10278                input_repr: 99, // 9999.9
10279                output_prec: 7,
10280                output_scale: 6,
10281                expected_output_repr: Ok(9900000), // 9.900000
10282            },
10283            // increase precision, increase scale, fallible, unsafe
10284            DecimalCastTestConfig {
10285                input_prec: 5,
10286                input_scale: 1,
10287                input_repr: 99999, // 9999.9
10288                output_prec: 7,
10289                output_scale: 6,
10290                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
10291            },
10292            // increase precision, decrease scale, always infallible
10293            DecimalCastTestConfig {
10294                input_prec: 5,
10295                input_scale: 3,
10296                input_repr: 99999, // 99.999
10297                output_prec: 10,
10298                output_scale: 2,
10299                expected_output_repr: Ok(10000), // 100.00
10300            },
10301            // increase precision, decrease scale, no rouding
10302            DecimalCastTestConfig {
10303                input_prec: 5,
10304                input_scale: 3,
10305                input_repr: 99994, // 99.994
10306                output_prec: 10,
10307                output_scale: 2,
10308                expected_output_repr: Ok(9999), // 99.99
10309            },
10310            // increase precision, don't change scale, always infallible
10311            DecimalCastTestConfig {
10312                input_prec: 5,
10313                input_scale: 3,
10314                input_repr: 99999, // 99.999
10315                output_prec: 10,
10316                output_scale: 3,
10317                expected_output_repr: Ok(99999), // 99.999
10318            },
10319            // decrease precision, increase scale, safe
10320            DecimalCastTestConfig {
10321                input_prec: 10,
10322                input_scale: 5,
10323                input_repr: 999999, // 9.99999
10324                output_prec: 8,
10325                output_scale: 7,
10326                expected_output_repr: Ok(99999900), // 9.9999900
10327            },
10328            // decrease precision, increase scale, unsafe
10329            DecimalCastTestConfig {
10330                input_prec: 10,
10331                input_scale: 5,
10332                input_repr: 9999999, // 99.99999
10333                output_prec: 8,
10334                output_scale: 7,
10335                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
10336            },
10337            // decrease precision, decrease scale, safe, infallible
10338            DecimalCastTestConfig {
10339                input_prec: 7,
10340                input_scale: 4,
10341                input_repr: 9999999, // 999.9999
10342                output_prec: 6,
10343                output_scale: 2,
10344                expected_output_repr: Ok(100000),
10345            },
10346            // decrease precision, decrease scale, safe, fallible
10347            DecimalCastTestConfig {
10348                input_prec: 10,
10349                input_scale: 5,
10350                input_repr: 12345678, // 123.45678
10351                output_prec: 8,
10352                output_scale: 3,
10353                expected_output_repr: Ok(123457), // 123.457
10354            },
10355            // decrease precision, decrease scale, unsafe
10356            DecimalCastTestConfig {
10357                input_prec: 10,
10358                input_scale: 5,
10359                input_repr: 9999999, // 99.99999
10360                output_prec: 4,
10361                output_scale: 3,
10362                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
10363            },
10364            // decrease precision, same scale, safe
10365            DecimalCastTestConfig {
10366                input_prec: 10,
10367                input_scale: 5,
10368                input_repr: 999999, // 9.99999
10369                output_prec: 6,
10370                output_scale: 5,
10371                expected_output_repr: Ok(999999), // 9.99999
10372            },
10373            // decrease precision, same scale, unsafe
10374            DecimalCastTestConfig {
10375                input_prec: 10,
10376                input_scale: 5,
10377                input_repr: 9999999, // 99.99999
10378                output_prec: 6,
10379                output_scale: 5,
10380                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
10381            },
10382            // same precision, increase scale, safe
10383            DecimalCastTestConfig {
10384                input_prec: 7,
10385                input_scale: 4,
10386                input_repr: 12345, // 1.2345
10387                output_prec: 7,
10388                output_scale: 6,
10389                expected_output_repr: Ok(1234500), // 1.234500
10390            },
10391            // same precision, increase scale, unsafe
10392            DecimalCastTestConfig {
10393                input_prec: 7,
10394                input_scale: 4,
10395                input_repr: 123456, // 12.3456
10396                output_prec: 7,
10397                output_scale: 6,
10398                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
10399            },
10400            // same precision, decrease scale, infallible
10401            DecimalCastTestConfig {
10402                input_prec: 7,
10403                input_scale: 5,
10404                input_repr: 1234567, // 12.34567
10405                output_prec: 7,
10406                output_scale: 4,
10407                expected_output_repr: Ok(123457), // 12.3457
10408            },
10409            // same precision, same scale, infallible
10410            DecimalCastTestConfig {
10411                input_prec: 7,
10412                input_scale: 5,
10413                input_repr: 9999999, // 99.99999
10414                output_prec: 7,
10415                output_scale: 5,
10416                expected_output_repr: Ok(9999999), // 99.99999
10417            },
10418            // precision increase, input scale & output scale = 0, infallible
10419            DecimalCastTestConfig {
10420                input_prec: 7,
10421                input_scale: 0,
10422                input_repr: 1234567, // 1234567
10423                output_prec: 8,
10424                output_scale: 0,
10425                expected_output_repr: Ok(1234567), // 1234567
10426            },
10427            // precision decrease, input scale & output scale = 0, failure
10428            DecimalCastTestConfig {
10429                input_prec: 7,
10430                input_scale: 0,
10431                input_repr: 1234567, // 1234567
10432                output_prec: 6,
10433                output_scale: 0,
10434                expected_output_repr: Err("Invalid argument error: 1234567 is too large to store in a {} of precision 6. Max is 999999".to_string())
10435            },
10436            // precision decrease, input scale & output scale = 0, success
10437            DecimalCastTestConfig {
10438                input_prec: 7,
10439                input_scale: 0,
10440                input_repr: 123456, // 123456
10441                output_prec: 6,
10442                output_scale: 0,
10443                expected_output_repr: Ok(123456), // 123456
10444            },
10445        ];
10446
10447        for t in test_cases {
10448            run_decimal_cast_test_case_between_multiple_types(t);
10449        }
10450    }
10451
10452    #[test]
10453    fn test_decimal_to_decimal_increase_scale_and_precision_unchecked() {
10454        let test_cases = [
10455            DecimalCastTestConfig {
10456                input_prec: 5,
10457                input_scale: 0,
10458                input_repr: 99999,
10459                output_prec: 10,
10460                output_scale: 5,
10461                expected_output_repr: Ok(9999900000),
10462            },
10463            DecimalCastTestConfig {
10464                input_prec: 5,
10465                input_scale: 0,
10466                input_repr: -99999,
10467                output_prec: 10,
10468                output_scale: 5,
10469                expected_output_repr: Ok(-9999900000),
10470            },
10471            DecimalCastTestConfig {
10472                input_prec: 5,
10473                input_scale: 2,
10474                input_repr: 99999,
10475                output_prec: 10,
10476                output_scale: 5,
10477                expected_output_repr: Ok(99999000),
10478            },
10479            DecimalCastTestConfig {
10480                input_prec: 5,
10481                input_scale: -2,
10482                input_repr: -99999,
10483                output_prec: 10,
10484                output_scale: 3,
10485                expected_output_repr: Ok(-9999900000),
10486            },
10487            DecimalCastTestConfig {
10488                input_prec: 5,
10489                input_scale: 3,
10490                input_repr: -12345,
10491                output_prec: 6,
10492                output_scale: 5,
10493                expected_output_repr: Err("Invalid argument error: -1234500 is too small to store in a {} of precision 6. Min is -999999".to_string())
10494            },
10495        ];
10496
10497        for t in test_cases {
10498            run_decimal_cast_test_case_between_multiple_types(t);
10499        }
10500    }
10501
10502    #[test]
10503    fn test_decimal_to_decimal_decrease_scale_and_precision_unchecked() {
10504        let test_cases = [
10505            DecimalCastTestConfig {
10506                input_prec: 5,
10507                input_scale: 0,
10508                input_repr: 99999,
10509                output_scale: -3,
10510                output_prec: 3,
10511                expected_output_repr: Ok(100),
10512            },
10513            DecimalCastTestConfig {
10514                input_prec: 5,
10515                input_scale: 0,
10516                input_repr: -99999,
10517                output_prec: 1,
10518                output_scale: -5,
10519                expected_output_repr: Ok(-1),
10520            },
10521            DecimalCastTestConfig {
10522                input_prec: 10,
10523                input_scale: 2,
10524                input_repr: 123456789,
10525                output_prec: 5,
10526                output_scale: -2,
10527                expected_output_repr: Ok(12346),
10528            },
10529            DecimalCastTestConfig {
10530                input_prec: 10,
10531                input_scale: 4,
10532                input_repr: -9876543210,
10533                output_prec: 7,
10534                output_scale: 0,
10535                expected_output_repr: Ok(-987654),
10536            },
10537            DecimalCastTestConfig {
10538                input_prec: 7,
10539                input_scale: 4,
10540                input_repr: 9999999,
10541                output_prec: 6,
10542                output_scale: 3,
10543                expected_output_repr:
10544                    Err("Invalid argument error: 1000000 is too large to store in a {} of precision 6. Max is 999999".to_string()),
10545            },
10546        ];
10547        for t in test_cases {
10548            run_decimal_cast_test_case_between_multiple_types(t);
10549        }
10550    }
10551
10552    #[test]
10553    fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
10554        let array = vec![Some(123456789)];
10555        let array = create_decimal128_array(array, 24, 2).unwrap();
10556        let input_type = DataType::Decimal128(24, 2);
10557        let output_type = DataType::Decimal128(6, 2);
10558        assert!(can_cast_types(&input_type, &output_type));
10559
10560        let options = CastOptions {
10561            safe: false,
10562            ..Default::default()
10563        };
10564        let result = cast_with_options(&array, &output_type, &options);
10565        assert_eq!(result.unwrap_err().to_string(),
10566                   "Invalid argument error: 123456789 is too large to store in a Decimal128 of precision 6. Max is 999999");
10567    }
10568
10569    #[test]
10570    fn test_decimal_to_decimal_same_scale() {
10571        let array = vec![Some(520)];
10572        let array = create_decimal128_array(array, 4, 2).unwrap();
10573        let input_type = DataType::Decimal128(4, 2);
10574        let output_type = DataType::Decimal128(3, 2);
10575        assert!(can_cast_types(&input_type, &output_type));
10576
10577        let options = CastOptions {
10578            safe: false,
10579            ..Default::default()
10580        };
10581        let result = cast_with_options(&array, &output_type, &options);
10582        assert_eq!(
10583            result.unwrap().as_primitive::<Decimal128Type>().value(0),
10584            520
10585        );
10586
10587        // Cast 0 of decimal(3, 0) type to decimal(2, 0)
10588        assert_eq!(
10589            &cast(
10590                &create_decimal128_array(vec![Some(0)], 3, 0).unwrap(),
10591                &DataType::Decimal128(2, 0)
10592            )
10593            .unwrap(),
10594            &(Arc::new(create_decimal128_array(vec![Some(0)], 2, 0).unwrap()) as ArrayRef)
10595        );
10596    }
10597
10598    #[test]
10599    fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale() {
10600        let array = vec![Some(123456789)];
10601        let array = create_decimal128_array(array, 24, 4).unwrap();
10602        let input_type = DataType::Decimal128(24, 4);
10603        let output_type = DataType::Decimal128(6, 2);
10604        assert!(can_cast_types(&input_type, &output_type));
10605
10606        let options = CastOptions {
10607            safe: false,
10608            ..Default::default()
10609        };
10610        let result = cast_with_options(&array, &output_type, &options);
10611        assert_eq!(result.unwrap_err().to_string(),
10612                   "Invalid argument error: 1234568 is too large to store in a Decimal128 of precision 6. Max is 999999");
10613    }
10614
10615    #[test]
10616    fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale() {
10617        let array = vec![Some(123456789)];
10618        let array = create_decimal128_array(array, 24, 2).unwrap();
10619        let input_type = DataType::Decimal128(24, 2);
10620        let output_type = DataType::Decimal128(6, 3);
10621        assert!(can_cast_types(&input_type, &output_type));
10622
10623        let options = CastOptions {
10624            safe: false,
10625            ..Default::default()
10626        };
10627        let result = cast_with_options(&array, &output_type, &options);
10628        assert_eq!(result.unwrap_err().to_string(),
10629                   "Invalid argument error: 1234567890 is too large to store in a Decimal128 of precision 6. Max is 999999");
10630    }
10631
10632    #[test]
10633    fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type() {
10634        let array = vec![Some(123456789)];
10635        let array = create_decimal128_array(array, 24, 2).unwrap();
10636        let input_type = DataType::Decimal128(24, 2);
10637        let output_type = DataType::Decimal256(6, 2);
10638        assert!(can_cast_types(&input_type, &output_type));
10639
10640        let options = CastOptions {
10641            safe: false,
10642            ..Default::default()
10643        };
10644        let result = cast_with_options(&array, &output_type, &options);
10645        assert_eq!(result.unwrap_err().to_string(),
10646                   "Invalid argument error: 123456789 is too large to store in a Decimal256 of precision 6. Max is 999999");
10647    }
10648
10649    #[test]
10650    fn test_first_none() {
10651        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
10652            None,
10653            Some(vec![Some(1), Some(2)]),
10654        ])) as ArrayRef;
10655        let data_type =
10656            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
10657        let opt = CastOptions::default();
10658        let r = cast_with_options(&array, &data_type, &opt).unwrap();
10659
10660        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
10661            vec![None, Some(vec![Some(1), Some(2)])],
10662            2,
10663        )) as ArrayRef;
10664        assert_eq!(*fixed_array, *r);
10665    }
10666
10667    #[test]
10668    fn test_first_last_none() {
10669        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
10670            None,
10671            Some(vec![Some(1), Some(2)]),
10672            None,
10673        ])) as ArrayRef;
10674        let data_type =
10675            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
10676        let opt = CastOptions::default();
10677        let r = cast_with_options(&array, &data_type, &opt).unwrap();
10678
10679        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
10680            vec![None, Some(vec![Some(1), Some(2)]), None],
10681            2,
10682        )) as ArrayRef;
10683        assert_eq!(*fixed_array, *r);
10684    }
10685}