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 run_array;
45mod string;
46use crate::cast::decimal::*;
47use crate::cast::dictionary::*;
48use crate::cast::list::*;
49use crate::cast::map::*;
50use crate::cast::run_array::*;
51use crate::cast::string::*;
52
53use arrow_buffer::IntervalMonthDayNano;
54use arrow_data::ByteView;
55use chrono::{NaiveTime, Offset, TimeZone, Utc};
56use std::cmp::Ordering;
57use std::sync::Arc;
58
59use crate::display::{ArrayFormatter, FormatOptions};
60use crate::parse::{
61    Parser, parse_interval_day_time, parse_interval_month_day_nano, parse_interval_year_month,
62    string_to_datetime,
63};
64use arrow_array::{builder::*, cast::*, temporal_conversions::*, timezone::Tz, types::*, *};
65use arrow_buffer::{ArrowNativeType, OffsetBuffer, i256};
66use arrow_data::ArrayData;
67use arrow_data::transform::MutableArrayData;
68use arrow_schema::*;
69use arrow_select::take::take;
70use num_traits::{NumCast, ToPrimitive, cast::AsPrimitive};
71
72pub use decimal::{DecimalCast, rescale_decimal};
73
74/// CastOptions provides a way to override the default cast behaviors
75#[derive(Debug, Clone, PartialEq, Eq, Hash)]
76pub struct CastOptions<'a> {
77    /// how to handle cast failures, either return NULL (safe=true) or return ERR (safe=false)
78    pub safe: bool,
79    /// Formatting options when casting from temporal types to string
80    pub format_options: FormatOptions<'a>,
81}
82
83impl Default for CastOptions<'_> {
84    fn default() -> Self {
85        Self {
86            safe: true,
87            format_options: FormatOptions::default(),
88        }
89    }
90}
91
92/// Return true if a value of type `from_type` can be cast into a value of `to_type`.
93///
94/// See [`cast_with_options`] for more information
95pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
96    use self::DataType::*;
97    use self::IntervalUnit::*;
98    use self::TimeUnit::*;
99    if from_type == to_type {
100        return true;
101    }
102
103    match (from_type, to_type) {
104        (
105            Null,
106            Boolean
107            | Int8
108            | UInt8
109            | Int16
110            | UInt16
111            | Float16
112            | Int32
113            | UInt32
114            | Float32
115            | Date32
116            | Time32(_)
117            | Int64
118            | UInt64
119            | Float64
120            | Date64
121            | Timestamp(_, _)
122            | Time64(_)
123            | Duration(_)
124            | Interval(_)
125            | FixedSizeBinary(_)
126            | Binary
127            | Utf8
128            | LargeBinary
129            | LargeUtf8
130            | BinaryView
131            | Utf8View
132            | List(_)
133            | LargeList(_)
134            | FixedSizeList(_, _)
135            | Struct(_)
136            | Map(_, _)
137            | Dictionary(_, _),
138        ) => true,
139        // Dictionary/List conditions should be put in front of others
140        (Dictionary(_, from_value_type), Dictionary(_, to_value_type)) => {
141            can_cast_types(from_value_type, to_value_type)
142        }
143        (Dictionary(_, value_type), _) => can_cast_types(value_type, to_type),
144        (RunEndEncoded(_, value_type), _) => can_cast_types(value_type.data_type(), to_type),
145        (_, RunEndEncoded(_, value_type)) => can_cast_types(from_type, value_type.data_type()),
146        (_, Dictionary(_, value_type)) => can_cast_types(from_type, value_type),
147        (List(list_from) | LargeList(list_from), List(list_to) | LargeList(list_to)) => {
148            can_cast_types(list_from.data_type(), list_to.data_type())
149        }
150        (List(list_from) | LargeList(list_from), Utf8 | LargeUtf8) => {
151            can_cast_types(list_from.data_type(), to_type)
152        }
153        (List(list_from) | LargeList(list_from), FixedSizeList(list_to, _)) => {
154            can_cast_types(list_from.data_type(), list_to.data_type())
155        }
156        (List(_), _) => false,
157        (FixedSizeList(list_from, _), List(list_to))
158        | (FixedSizeList(list_from, _), LargeList(list_to)) => {
159            can_cast_types(list_from.data_type(), list_to.data_type())
160        }
161        (FixedSizeList(inner, size), FixedSizeList(inner_to, size_to)) if size == size_to => {
162            can_cast_types(inner.data_type(), inner_to.data_type())
163        }
164        (_, List(list_to)) => can_cast_types(from_type, list_to.data_type()),
165        (_, LargeList(list_to)) => can_cast_types(from_type, list_to.data_type()),
166        (_, FixedSizeList(list_to, size)) if *size == 1 => {
167            can_cast_types(from_type, list_to.data_type())
168        }
169        (FixedSizeList(list_from, size), _) if *size == 1 => {
170            can_cast_types(list_from.data_type(), to_type)
171        }
172        (Map(from_entries, ordered_from), Map(to_entries, ordered_to))
173            if ordered_from == ordered_to =>
174        {
175            match (
176                key_field(from_entries),
177                key_field(to_entries),
178                value_field(from_entries),
179                value_field(to_entries),
180            ) {
181                (Some(from_key), Some(to_key), Some(from_value), Some(to_value)) => {
182                    can_cast_types(from_key.data_type(), to_key.data_type())
183                        && can_cast_types(from_value.data_type(), to_value.data_type())
184                }
185                _ => false,
186            }
187        }
188        // cast one decimal type to another decimal type
189        (
190            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
191            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
192        ) => true,
193        // unsigned integer to decimal
194        (
195            UInt8 | UInt16 | UInt32 | UInt64,
196            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
197        ) => true,
198        // signed numeric to decimal
199        (
200            Null | Int8 | Int16 | Int32 | Int64 | Float32 | Float64,
201            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
202        ) => true,
203        // decimal to unsigned numeric
204        (
205            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
206            UInt8 | UInt16 | UInt32 | UInt64,
207        ) => true,
208        // decimal to signed numeric
209        (
210            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
211            Null | Int8 | Int16 | Int32 | Int64 | Float32 | Float64,
212        ) => true,
213        // decimal to string
214        (
215            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
216            Utf8View | Utf8 | LargeUtf8,
217        ) => true,
218        // string to decimal
219        (
220            Utf8View | Utf8 | LargeUtf8,
221            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
222        ) => true,
223        (Struct(from_fields), Struct(to_fields)) => {
224            if from_fields.len() != to_fields.len() {
225                return false;
226            }
227
228            // fast path, all field names are in the same order and same number of fields
229            if from_fields
230                .iter()
231                .zip(to_fields.iter())
232                .all(|(f1, f2)| f1.name() == f2.name())
233            {
234                return from_fields.iter().zip(to_fields.iter()).all(|(f1, f2)| {
235                    // Assume that nullability between two structs are compatible, if not,
236                    // cast kernel will return error.
237                    can_cast_types(f1.data_type(), f2.data_type())
238                });
239            }
240
241            // slow path, we match the fields by name
242            to_fields.iter().all(|to_field| {
243                from_fields
244                    .iter()
245                    .find(|from_field| from_field.name() == to_field.name())
246                    .is_some_and(|from_field| {
247                        // Assume that nullability between two structs are compatible, if not,
248                        // cast kernel will return error.
249                        can_cast_types(from_field.data_type(), to_field.data_type())
250                    })
251            })
252        }
253        (Struct(_), _) => false,
254        (_, Struct(_)) => false,
255        (_, Boolean) => {
256            DataType::is_integer(from_type)
257                || DataType::is_floating(from_type)
258                || from_type == &Utf8View
259                || from_type == &Utf8
260                || from_type == &LargeUtf8
261        }
262        (Boolean, _) => {
263            DataType::is_integer(to_type)
264                || DataType::is_floating(to_type)
265                || to_type == &Utf8View
266                || to_type == &Utf8
267                || to_type == &LargeUtf8
268        }
269
270        (Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
271            true
272        }
273        (LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
274            true
275        }
276        (FixedSizeBinary(_), Binary | LargeBinary | BinaryView) => true,
277        (
278            Utf8 | LargeUtf8 | Utf8View,
279            Binary
280            | LargeBinary
281            | Utf8
282            | LargeUtf8
283            | Date32
284            | Date64
285            | Time32(Second)
286            | Time32(Millisecond)
287            | Time64(Microsecond)
288            | Time64(Nanosecond)
289            | Timestamp(Second, _)
290            | Timestamp(Millisecond, _)
291            | Timestamp(Microsecond, _)
292            | Timestamp(Nanosecond, _)
293            | Interval(_)
294            | BinaryView,
295        ) => true,
296        (Utf8 | LargeUtf8, Utf8View) => true,
297        (BinaryView, Binary | LargeBinary | Utf8 | LargeUtf8 | Utf8View) => true,
298        (Utf8View | Utf8 | LargeUtf8, _) => to_type.is_numeric() && to_type != &Float16,
299        (_, Utf8 | Utf8View | LargeUtf8) => from_type.is_primitive(),
300
301        (_, Binary | LargeBinary) => from_type.is_integer(),
302
303        // start numeric casts
304        (
305            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
306            | Float64,
307            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
308            | Float64,
309        ) => true,
310        // end numeric casts
311
312        // temporal casts
313        (Int32, Date32 | Date64 | Time32(_)) => true,
314        (Date32, Int32 | Int64) => true,
315        (Time32(_), Int32) => true,
316        (Int64, Date64 | Date32 | Time64(_)) => true,
317        (Date64, Int64 | Int32) => true,
318        (Time64(_), Int64) => true,
319        (Date32 | Date64, Date32 | Date64) => true,
320        // time casts
321        (Time32(_), Time32(_)) => true,
322        (Time32(_), Time64(_)) => true,
323        (Time64(_), Time64(_)) => true,
324        (Time64(_), Time32(to_unit)) => {
325            matches!(to_unit, Second | Millisecond)
326        }
327        (Timestamp(_, _), _) if to_type.is_numeric() => true,
328        (_, Timestamp(_, _)) if from_type.is_numeric() => true,
329        (Date64, Timestamp(_, _)) => true,
330        (Date32, Timestamp(_, _)) => true,
331        (
332            Timestamp(_, _),
333            Timestamp(_, _)
334            | Date32
335            | Date64
336            | Time32(Second)
337            | Time32(Millisecond)
338            | Time64(Microsecond)
339            | Time64(Nanosecond),
340        ) => true,
341        (_, Duration(_)) if from_type.is_numeric() => true,
342        (Duration(_), _) if to_type.is_numeric() => true,
343        (Duration(_), Duration(_)) => true,
344        (Interval(from_type), Int64) => {
345            match from_type {
346                YearMonth => true,
347                DayTime => true,
348                MonthDayNano => false, // Native type is i128
349            }
350        }
351        (Int32, Interval(to_type)) => match to_type {
352            YearMonth => true,
353            DayTime => false,
354            MonthDayNano => false,
355        },
356        (Duration(_), Interval(MonthDayNano)) => true,
357        (Interval(MonthDayNano), Duration(_)) => true,
358        (Interval(YearMonth), Interval(MonthDayNano)) => true,
359        (Interval(DayTime), Interval(MonthDayNano)) => true,
360        (_, _) => false,
361    }
362}
363
364/// Cast `array` to the provided data type and return a new Array with type `to_type`, if possible.
365///
366/// See [`cast_with_options`] for more information
367pub fn cast(array: &dyn Array, to_type: &DataType) -> Result<ArrayRef, ArrowError> {
368    cast_with_options(array, to_type, &CastOptions::default())
369}
370
371fn cast_integer_to_decimal<
372    T: ArrowPrimitiveType,
373    D: DecimalType + ArrowPrimitiveType<Native = M>,
374    M,
375>(
376    array: &PrimitiveArray<T>,
377    precision: u8,
378    scale: i8,
379    base: M,
380    cast_options: &CastOptions,
381) -> Result<ArrayRef, ArrowError>
382where
383    <T as ArrowPrimitiveType>::Native: AsPrimitive<M>,
384    M: ArrowNativeTypeOp,
385{
386    let scale_factor = base.pow_checked(scale.unsigned_abs() as u32).map_err(|_| {
387        ArrowError::CastError(format!(
388            "Cannot cast to {:?}({}, {}). The scale causes overflow.",
389            D::PREFIX,
390            precision,
391            scale,
392        ))
393    })?;
394
395    let array = if scale < 0 {
396        match cast_options.safe {
397            true => array.unary_opt::<_, D>(|v| {
398                v.as_()
399                    .div_checked(scale_factor)
400                    .ok()
401                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
402            }),
403            false => array.try_unary::<_, D, _>(|v| {
404                v.as_()
405                    .div_checked(scale_factor)
406                    .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|_| v))
407            })?,
408        }
409    } else {
410        match cast_options.safe {
411            true => array.unary_opt::<_, D>(|v| {
412                v.as_()
413                    .mul_checked(scale_factor)
414                    .ok()
415                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
416            }),
417            false => array.try_unary::<_, D, _>(|v| {
418                v.as_()
419                    .mul_checked(scale_factor)
420                    .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|_| v))
421            })?,
422        }
423    };
424
425    Ok(Arc::new(array.with_precision_and_scale(precision, scale)?))
426}
427
428/// Cast the array from interval year month to month day nano
429fn cast_interval_year_month_to_interval_month_day_nano(
430    array: &dyn Array,
431    _cast_options: &CastOptions,
432) -> Result<ArrayRef, ArrowError> {
433    let array = array.as_primitive::<IntervalYearMonthType>();
434
435    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
436        let months = IntervalYearMonthType::to_months(v);
437        IntervalMonthDayNanoType::make_value(months, 0, 0)
438    })))
439}
440
441/// Cast the array from interval day time to month day nano
442fn cast_interval_day_time_to_interval_month_day_nano(
443    array: &dyn Array,
444    _cast_options: &CastOptions,
445) -> Result<ArrayRef, ArrowError> {
446    let array = array.as_primitive::<IntervalDayTimeType>();
447    let mul = 1_000_000;
448
449    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
450        let (days, ms) = IntervalDayTimeType::to_parts(v);
451        IntervalMonthDayNanoType::make_value(0, days, ms as i64 * mul)
452    })))
453}
454
455/// Cast the array from interval to duration
456fn cast_month_day_nano_to_duration<D: ArrowTemporalType<Native = i64>>(
457    array: &dyn Array,
458    cast_options: &CastOptions,
459) -> Result<ArrayRef, ArrowError> {
460    let array = array.as_primitive::<IntervalMonthDayNanoType>();
461    let scale = match D::DATA_TYPE {
462        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
463        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
464        DataType::Duration(TimeUnit::Microsecond) => 1_000,
465        DataType::Duration(TimeUnit::Nanosecond) => 1,
466        _ => unreachable!(),
467    };
468
469    if cast_options.safe {
470        let iter = array.iter().map(|v| {
471            v.and_then(|v| (v.days == 0 && v.months == 0).then_some(v.nanoseconds / scale))
472        });
473        Ok(Arc::new(unsafe {
474            PrimitiveArray::<D>::from_trusted_len_iter(iter)
475        }))
476    } else {
477        let vec = array
478            .iter()
479            .map(|v| {
480                v.map(|v| match v.days == 0 && v.months == 0 {
481                    true => Ok((v.nanoseconds) / scale),
482                    _ => Err(ArrowError::ComputeError(
483                        "Cannot convert interval containing non-zero months or days to duration"
484                            .to_string(),
485                    )),
486                })
487                .transpose()
488            })
489            .collect::<Result<Vec<_>, _>>()?;
490        Ok(Arc::new(unsafe {
491            PrimitiveArray::<D>::from_trusted_len_iter(vec.iter())
492        }))
493    }
494}
495
496/// Cast the array from duration and interval
497fn cast_duration_to_interval<D: ArrowTemporalType<Native = i64>>(
498    array: &dyn Array,
499    cast_options: &CastOptions,
500) -> Result<ArrayRef, ArrowError> {
501    let array = array
502        .as_any()
503        .downcast_ref::<PrimitiveArray<D>>()
504        .ok_or_else(|| {
505            ArrowError::ComputeError(
506                "Internal Error: Cannot cast duration to DurationArray of expected type"
507                    .to_string(),
508            )
509        })?;
510
511    let scale = match array.data_type() {
512        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
513        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
514        DataType::Duration(TimeUnit::Microsecond) => 1_000,
515        DataType::Duration(TimeUnit::Nanosecond) => 1,
516        _ => unreachable!(),
517    };
518
519    if cast_options.safe {
520        let iter = array.iter().map(|v| {
521            v.and_then(|v| {
522                v.checked_mul(scale)
523                    .map(|v| IntervalMonthDayNano::new(0, 0, v))
524            })
525        });
526        Ok(Arc::new(unsafe {
527            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(iter)
528        }))
529    } else {
530        let vec = array
531            .iter()
532            .map(|v| {
533                v.map(|v| {
534                    if let Ok(v) = v.mul_checked(scale) {
535                        Ok(IntervalMonthDayNano::new(0, 0, v))
536                    } else {
537                        Err(ArrowError::ComputeError(format!(
538                            "Cannot cast to {:?}. Overflowing on {:?}",
539                            IntervalMonthDayNanoType::DATA_TYPE,
540                            v
541                        )))
542                    }
543                })
544                .transpose()
545            })
546            .collect::<Result<Vec<_>, _>>()?;
547        Ok(Arc::new(unsafe {
548            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(vec.iter())
549        }))
550    }
551}
552
553/// Cast the primitive array using [`PrimitiveArray::reinterpret_cast`]
554fn cast_reinterpret_arrays<I: ArrowPrimitiveType, O: ArrowPrimitiveType<Native = I::Native>>(
555    array: &dyn Array,
556) -> Result<ArrayRef, ArrowError> {
557    Ok(Arc::new(array.as_primitive::<I>().reinterpret_cast::<O>()))
558}
559
560fn make_timestamp_array(
561    array: &PrimitiveArray<Int64Type>,
562    unit: TimeUnit,
563    tz: Option<Arc<str>>,
564) -> ArrayRef {
565    match unit {
566        TimeUnit::Second => Arc::new(
567            array
568                .reinterpret_cast::<TimestampSecondType>()
569                .with_timezone_opt(tz),
570        ),
571        TimeUnit::Millisecond => Arc::new(
572            array
573                .reinterpret_cast::<TimestampMillisecondType>()
574                .with_timezone_opt(tz),
575        ),
576        TimeUnit::Microsecond => Arc::new(
577            array
578                .reinterpret_cast::<TimestampMicrosecondType>()
579                .with_timezone_opt(tz),
580        ),
581        TimeUnit::Nanosecond => Arc::new(
582            array
583                .reinterpret_cast::<TimestampNanosecondType>()
584                .with_timezone_opt(tz),
585        ),
586    }
587}
588
589fn make_duration_array(array: &PrimitiveArray<Int64Type>, unit: TimeUnit) -> ArrayRef {
590    match unit {
591        TimeUnit::Second => Arc::new(array.reinterpret_cast::<DurationSecondType>()),
592        TimeUnit::Millisecond => Arc::new(array.reinterpret_cast::<DurationMillisecondType>()),
593        TimeUnit::Microsecond => Arc::new(array.reinterpret_cast::<DurationMicrosecondType>()),
594        TimeUnit::Nanosecond => Arc::new(array.reinterpret_cast::<DurationNanosecondType>()),
595    }
596}
597
598fn as_time_res_with_timezone<T: ArrowPrimitiveType>(
599    v: i64,
600    tz: Option<Tz>,
601) -> Result<NaiveTime, ArrowError> {
602    let time = match tz {
603        Some(tz) => as_datetime_with_timezone::<T>(v, tz).map(|d| d.time()),
604        None => as_datetime::<T>(v).map(|d| d.time()),
605    };
606
607    time.ok_or_else(|| {
608        ArrowError::CastError(format!(
609            "Failed to create naive time with {} {}",
610            std::any::type_name::<T>(),
611            v
612        ))
613    })
614}
615
616fn timestamp_to_date32<T: ArrowTimestampType>(
617    array: &PrimitiveArray<T>,
618) -> Result<ArrayRef, ArrowError> {
619    let err = |x: i64| {
620        ArrowError::CastError(format!(
621            "Cannot convert {} {x} to datetime",
622            std::any::type_name::<T>()
623        ))
624    };
625
626    let array: Date32Array = match array.timezone() {
627        Some(tz) => {
628            let tz: Tz = tz.parse()?;
629            array.try_unary(|x| {
630                as_datetime_with_timezone::<T>(x, tz)
631                    .ok_or_else(|| err(x))
632                    .map(|d| Date32Type::from_naive_date(d.date_naive()))
633            })?
634        }
635        None => array.try_unary(|x| {
636            as_datetime::<T>(x)
637                .ok_or_else(|| err(x))
638                .map(|d| Date32Type::from_naive_date(d.date()))
639        })?,
640    };
641    Ok(Arc::new(array))
642}
643
644/// Try to cast `array` to `to_type` if possible.
645///
646/// Returns a new Array with type `to_type` if possible.
647///
648/// Accepts [`CastOptions`] to specify cast behavior. See also [`cast()`].
649///
650/// # Behavior
651/// * `Boolean` to `Utf8`: `true` => '1', `false` => `0`
652/// * `Utf8` to `Boolean`: `true`, `yes`, `on`, `1` => `true`, `false`, `no`, `off`, `0` => `false`,
653///   short variants are accepted, other strings return null or error
654/// * `Utf8` to Numeric: strings that can't be parsed to numbers return null, float strings
655///   in integer casts return null
656/// * Numeric to `Boolean`: 0 returns `false`, any other value returns `true`
657/// * `List` to `List`: the underlying data type is cast
658/// * `List` to `FixedSizeList`: the underlying data type is cast. If safe is true and a list element
659///   has the wrong length it will be replaced with NULL, otherwise an error will be returned
660/// * Primitive to `List`: a list array with 1 value per slot is created
661/// * `Date32` and `Date64`: precision lost when going to higher interval
662/// * `Time32 and `Time64`: precision lost when going to higher interval
663/// * `Timestamp` and `Date{32|64}`: precision lost when going to higher interval
664/// * Temporal to/from backing Primitive: zero-copy with data type change
665/// * `Float32/Float64` to `Decimal(precision, scale)` rounds to the `scale` decimals
666///   (i.e. casting `6.4999` to `Decimal(10, 1)` becomes `6.5`).
667/// * `Decimal` to `Float32/Float64` is lossy and values outside the representable
668///   range become `INFINITY` or `-INFINITY` without error.
669///
670/// Unsupported Casts (check with `can_cast_types` before calling):
671/// * To or from `StructArray`
672/// * `List` to `Primitive`
673/// * `Interval` and `Duration`
674///
675/// # Durations and Intervals
676///
677/// Casting integer types directly to interval types such as
678/// [`IntervalMonthDayNano`] is not supported because the meaning of the integer
679/// is ambiguous. For example, the integer  could represent either nanoseconds
680/// or months.
681///
682/// To cast an integer type to an interval type, first convert to a Duration
683/// type, and then cast that to the desired interval type.
684///
685/// For example, to convert an `Int64` representing nanoseconds to an
686/// `IntervalMonthDayNano` you would first convert the `Int64` to a
687/// `DurationNanoseconds`, and then cast that to `IntervalMonthDayNano`.
688///
689/// # Timestamps and Timezones
690///
691/// Timestamps are stored with an optional timezone in Arrow.
692///
693/// ## Casting timestamps to a timestamp without timezone / UTC
694/// ```
695/// # use arrow_array::Int64Array;
696/// # use arrow_array::types::TimestampSecondType;
697/// # use arrow_cast::{cast, display};
698/// # use arrow_array::cast::AsArray;
699/// # use arrow_schema::{DataType, TimeUnit};
700/// // can use "UTC" if chrono-tz feature is enabled, here use offset based timezone
701/// let data_type = DataType::Timestamp(TimeUnit::Second, None);
702/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
703/// let b = cast(&a, &data_type).unwrap();
704/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
705/// assert_eq!(2_000_000_000, b.value(1)); // values are the same as the type has no timezone
706/// // use display to show them (note has no trailing Z)
707/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
708/// ```
709///
710/// ## Casting timestamps to a timestamp with timezone
711///
712/// Similarly to the previous example, if you cast numeric values to a timestamp
713/// with timezone, the cast kernel will not change the underlying values
714/// but display and other functions will interpret them as being in the provided timezone.
715///
716/// ```
717/// # use arrow_array::Int64Array;
718/// # use arrow_array::types::TimestampSecondType;
719/// # use arrow_cast::{cast, display};
720/// # use arrow_array::cast::AsArray;
721/// # use arrow_schema::{DataType, TimeUnit};
722/// // can use "Americas/New_York" if chrono-tz feature is enabled, here use offset based timezone
723/// let data_type = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
724/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
725/// let b = cast(&a, &data_type).unwrap();
726/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
727/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
728/// // displayed in the target timezone (note the offset -05:00)
729/// assert_eq!("2033-05-17T22:33:20-05:00", display::array_value_to_string(&b, 1).unwrap());
730/// ```
731/// # Casting timestamps without timezone to timestamps with timezone
732///
733/// When casting from a timestamp without timezone to a timestamp with
734/// timezone, the cast kernel interprets the timestamp values as being in
735/// the destination timezone and then adjusts the underlying value to UTC as required
736///
737/// However, note that when casting from a timestamp with timezone BACK to a
738/// timestamp without timezone the cast kernel does not adjust the values.
739///
740/// Thus round trip casting a timestamp without timezone to a timestamp with
741/// timezone and back to a timestamp without timezone results in different
742/// values than the starting values.
743///
744/// ```
745/// # use arrow_array::Int64Array;
746/// # use arrow_array::types::{TimestampSecondType};
747/// # use arrow_cast::{cast, display};
748/// # use arrow_array::cast::AsArray;
749/// # use arrow_schema::{DataType, TimeUnit};
750/// let data_type  = DataType::Timestamp(TimeUnit::Second, None);
751/// let data_type_tz = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
752/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
753/// let b = cast(&a, &data_type).unwrap(); // cast to timestamp without timezone
754/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
755/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
756/// // displayed without a timezone (note lack of offset or Z)
757/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
758///
759/// // Convert timestamps without a timezone to timestamps with a timezone
760/// let c = cast(&b, &data_type_tz).unwrap();
761/// let c = c.as_primitive::<TimestampSecondType>(); // downcast to result type
762/// assert_eq!(2_000_018_000, c.value(1)); // value has been adjusted by offset
763/// // displayed with the target timezone offset (-05:00)
764/// assert_eq!("2033-05-18T03:33:20-05:00", display::array_value_to_string(&c, 1).unwrap());
765///
766/// // Convert from timestamp with timezone back to timestamp without timezone
767/// let d = cast(&c, &data_type).unwrap();
768/// let d = d.as_primitive::<TimestampSecondType>(); // downcast to result type
769/// assert_eq!(2_000_018_000, d.value(1)); // value has not been adjusted
770/// // NOTE: the timestamp is adjusted (08:33:20 instead of 03:33:20 as in previous example)
771/// assert_eq!("2033-05-18T08:33:20", display::array_value_to_string(&d, 1).unwrap());
772/// ```
773pub fn cast_with_options(
774    array: &dyn Array,
775    to_type: &DataType,
776    cast_options: &CastOptions,
777) -> Result<ArrayRef, ArrowError> {
778    use DataType::*;
779    let from_type = array.data_type();
780    // clone array if types are the same
781    if from_type == to_type {
782        return Ok(make_array(array.to_data()));
783    }
784    match (from_type, to_type) {
785        (
786            Null,
787            Boolean
788            | Int8
789            | UInt8
790            | Int16
791            | UInt16
792            | Float16
793            | Int32
794            | UInt32
795            | Float32
796            | Date32
797            | Time32(_)
798            | Int64
799            | UInt64
800            | Float64
801            | Date64
802            | Timestamp(_, _)
803            | Time64(_)
804            | Duration(_)
805            | Interval(_)
806            | FixedSizeBinary(_)
807            | Binary
808            | Utf8
809            | LargeBinary
810            | LargeUtf8
811            | BinaryView
812            | Utf8View
813            | List(_)
814            | LargeList(_)
815            | FixedSizeList(_, _)
816            | Struct(_)
817            | Map(_, _)
818            | Dictionary(_, _),
819        ) => Ok(new_null_array(to_type, array.len())),
820        (RunEndEncoded(index_type, _), _) => match index_type.data_type() {
821            Int16 => run_end_encoded_cast::<Int16Type>(array, to_type, cast_options),
822            Int32 => run_end_encoded_cast::<Int32Type>(array, to_type, cast_options),
823            Int64 => run_end_encoded_cast::<Int64Type>(array, to_type, cast_options),
824            _ => Err(ArrowError::CastError(format!(
825                "Casting from run end encoded type {from_type:?} to {to_type:?} not supported",
826            ))),
827        },
828        (_, RunEndEncoded(index_type, value_type)) => {
829            let array_ref = make_array(array.to_data());
830            match index_type.data_type() {
831                Int16 => cast_to_run_end_encoded::<Int16Type>(
832                    &array_ref,
833                    value_type.data_type(),
834                    cast_options,
835                ),
836                Int32 => cast_to_run_end_encoded::<Int32Type>(
837                    &array_ref,
838                    value_type.data_type(),
839                    cast_options,
840                ),
841                Int64 => cast_to_run_end_encoded::<Int64Type>(
842                    &array_ref,
843                    value_type.data_type(),
844                    cast_options,
845                ),
846                _ => Err(ArrowError::CastError(format!(
847                    "Casting from type {from_type:?} to run end encoded type {to_type:?} not supported",
848                ))),
849            }
850        }
851        (Dictionary(index_type, _), _) => match **index_type {
852            Int8 => dictionary_cast::<Int8Type>(array, to_type, cast_options),
853            Int16 => dictionary_cast::<Int16Type>(array, to_type, cast_options),
854            Int32 => dictionary_cast::<Int32Type>(array, to_type, cast_options),
855            Int64 => dictionary_cast::<Int64Type>(array, to_type, cast_options),
856            UInt8 => dictionary_cast::<UInt8Type>(array, to_type, cast_options),
857            UInt16 => dictionary_cast::<UInt16Type>(array, to_type, cast_options),
858            UInt32 => dictionary_cast::<UInt32Type>(array, to_type, cast_options),
859            UInt64 => dictionary_cast::<UInt64Type>(array, to_type, cast_options),
860            _ => Err(ArrowError::CastError(format!(
861                "Casting from dictionary type {from_type} to {to_type} not supported",
862            ))),
863        },
864        (_, Dictionary(index_type, value_type)) => match **index_type {
865            Int8 => cast_to_dictionary::<Int8Type>(array, value_type, cast_options),
866            Int16 => cast_to_dictionary::<Int16Type>(array, value_type, cast_options),
867            Int32 => cast_to_dictionary::<Int32Type>(array, value_type, cast_options),
868            Int64 => cast_to_dictionary::<Int64Type>(array, value_type, cast_options),
869            UInt8 => cast_to_dictionary::<UInt8Type>(array, value_type, cast_options),
870            UInt16 => cast_to_dictionary::<UInt16Type>(array, value_type, cast_options),
871            UInt32 => cast_to_dictionary::<UInt32Type>(array, value_type, cast_options),
872            UInt64 => cast_to_dictionary::<UInt64Type>(array, value_type, cast_options),
873            _ => Err(ArrowError::CastError(format!(
874                "Casting from type {from_type} to dictionary type {to_type} not supported",
875            ))),
876        },
877        (List(_), List(to)) => cast_list_values::<i32>(array, to, cast_options),
878        (LargeList(_), LargeList(to)) => cast_list_values::<i64>(array, to, cast_options),
879        (List(_), LargeList(list_to)) => cast_list::<i32, i64>(array, list_to, cast_options),
880        (LargeList(_), List(list_to)) => cast_list::<i64, i32>(array, list_to, cast_options),
881        (List(_), FixedSizeList(field, size)) => {
882            let array = array.as_list::<i32>();
883            cast_list_to_fixed_size_list::<i32>(array, field, *size, cast_options)
884        }
885        (LargeList(_), FixedSizeList(field, size)) => {
886            let array = array.as_list::<i64>();
887            cast_list_to_fixed_size_list::<i64>(array, field, *size, cast_options)
888        }
889        (List(_) | LargeList(_), _) => match to_type {
890            Utf8 => value_to_string::<i32>(array, cast_options),
891            LargeUtf8 => value_to_string::<i64>(array, cast_options),
892            _ => Err(ArrowError::CastError(
893                "Cannot cast list to non-list data types".to_string(),
894            )),
895        },
896        (FixedSizeList(list_from, size), List(list_to)) => {
897            if list_to.data_type() != list_from.data_type() {
898                // To transform inner type, can first cast to FSL with new inner type.
899                let fsl_to = DataType::FixedSizeList(list_to.clone(), *size);
900                let array = cast_with_options(array, &fsl_to, cast_options)?;
901                cast_fixed_size_list_to_list::<i32>(array.as_ref())
902            } else {
903                cast_fixed_size_list_to_list::<i32>(array)
904            }
905        }
906        (FixedSizeList(list_from, size), LargeList(list_to)) => {
907            if list_to.data_type() != list_from.data_type() {
908                // To transform inner type, can first cast to FSL with new inner type.
909                let fsl_to = DataType::FixedSizeList(list_to.clone(), *size);
910                let array = cast_with_options(array, &fsl_to, cast_options)?;
911                cast_fixed_size_list_to_list::<i64>(array.as_ref())
912            } else {
913                cast_fixed_size_list_to_list::<i64>(array)
914            }
915        }
916        (FixedSizeList(_, size_from), FixedSizeList(list_to, size_to)) => {
917            if size_from != size_to {
918                return Err(ArrowError::CastError(
919                    "cannot cast fixed-size-list to fixed-size-list with different size".into(),
920                ));
921            }
922            let array = array.as_any().downcast_ref::<FixedSizeListArray>().unwrap();
923            let values = cast_with_options(array.values(), list_to.data_type(), cast_options)?;
924            Ok(Arc::new(FixedSizeListArray::try_new(
925                list_to.clone(),
926                *size_from,
927                values,
928                array.nulls().cloned(),
929            )?))
930        }
931        (_, List(to)) => cast_values_to_list::<i32>(array, to, cast_options),
932        (_, LargeList(to)) => cast_values_to_list::<i64>(array, to, cast_options),
933        (_, FixedSizeList(to, size)) if *size == 1 => {
934            cast_values_to_fixed_size_list(array, to, *size, cast_options)
935        }
936        (FixedSizeList(_, size), _) if *size == 1 => {
937            cast_single_element_fixed_size_list_to_values(array, to_type, cast_options)
938        }
939        (Map(_, ordered1), Map(_, ordered2)) if ordered1 == ordered2 => {
940            cast_map_values(array.as_map(), to_type, cast_options, ordered1.to_owned())
941        }
942        // Decimal to decimal, same width
943        (Decimal32(p1, s1), Decimal32(p2, s2)) => {
944            cast_decimal_to_decimal_same_type::<Decimal32Type>(
945                array.as_primitive(),
946                *p1,
947                *s1,
948                *p2,
949                *s2,
950                cast_options,
951            )
952        }
953        (Decimal64(p1, s1), Decimal64(p2, s2)) => {
954            cast_decimal_to_decimal_same_type::<Decimal64Type>(
955                array.as_primitive(),
956                *p1,
957                *s1,
958                *p2,
959                *s2,
960                cast_options,
961            )
962        }
963        (Decimal128(p1, s1), Decimal128(p2, s2)) => {
964            cast_decimal_to_decimal_same_type::<Decimal128Type>(
965                array.as_primitive(),
966                *p1,
967                *s1,
968                *p2,
969                *s2,
970                cast_options,
971            )
972        }
973        (Decimal256(p1, s1), Decimal256(p2, s2)) => {
974            cast_decimal_to_decimal_same_type::<Decimal256Type>(
975                array.as_primitive(),
976                *p1,
977                *s1,
978                *p2,
979                *s2,
980                cast_options,
981            )
982        }
983        // Decimal to decimal, different width
984        (Decimal32(p1, s1), Decimal64(p2, s2)) => {
985            cast_decimal_to_decimal::<Decimal32Type, Decimal64Type>(
986                array.as_primitive(),
987                *p1,
988                *s1,
989                *p2,
990                *s2,
991                cast_options,
992            )
993        }
994        (Decimal32(p1, s1), Decimal128(p2, s2)) => {
995            cast_decimal_to_decimal::<Decimal32Type, Decimal128Type>(
996                array.as_primitive(),
997                *p1,
998                *s1,
999                *p2,
1000                *s2,
1001                cast_options,
1002            )
1003        }
1004        (Decimal32(p1, s1), Decimal256(p2, s2)) => {
1005            cast_decimal_to_decimal::<Decimal32Type, Decimal256Type>(
1006                array.as_primitive(),
1007                *p1,
1008                *s1,
1009                *p2,
1010                *s2,
1011                cast_options,
1012            )
1013        }
1014        (Decimal64(p1, s1), Decimal32(p2, s2)) => {
1015            cast_decimal_to_decimal::<Decimal64Type, Decimal32Type>(
1016                array.as_primitive(),
1017                *p1,
1018                *s1,
1019                *p2,
1020                *s2,
1021                cast_options,
1022            )
1023        }
1024        (Decimal64(p1, s1), Decimal128(p2, s2)) => {
1025            cast_decimal_to_decimal::<Decimal64Type, Decimal128Type>(
1026                array.as_primitive(),
1027                *p1,
1028                *s1,
1029                *p2,
1030                *s2,
1031                cast_options,
1032            )
1033        }
1034        (Decimal64(p1, s1), Decimal256(p2, s2)) => {
1035            cast_decimal_to_decimal::<Decimal64Type, Decimal256Type>(
1036                array.as_primitive(),
1037                *p1,
1038                *s1,
1039                *p2,
1040                *s2,
1041                cast_options,
1042            )
1043        }
1044        (Decimal128(p1, s1), Decimal32(p2, s2)) => {
1045            cast_decimal_to_decimal::<Decimal128Type, Decimal32Type>(
1046                array.as_primitive(),
1047                *p1,
1048                *s1,
1049                *p2,
1050                *s2,
1051                cast_options,
1052            )
1053        }
1054        (Decimal128(p1, s1), Decimal64(p2, s2)) => {
1055            cast_decimal_to_decimal::<Decimal128Type, Decimal64Type>(
1056                array.as_primitive(),
1057                *p1,
1058                *s1,
1059                *p2,
1060                *s2,
1061                cast_options,
1062            )
1063        }
1064        (Decimal128(p1, s1), Decimal256(p2, s2)) => {
1065            cast_decimal_to_decimal::<Decimal128Type, Decimal256Type>(
1066                array.as_primitive(),
1067                *p1,
1068                *s1,
1069                *p2,
1070                *s2,
1071                cast_options,
1072            )
1073        }
1074        (Decimal256(p1, s1), Decimal32(p2, s2)) => {
1075            cast_decimal_to_decimal::<Decimal256Type, Decimal32Type>(
1076                array.as_primitive(),
1077                *p1,
1078                *s1,
1079                *p2,
1080                *s2,
1081                cast_options,
1082            )
1083        }
1084        (Decimal256(p1, s1), Decimal64(p2, s2)) => {
1085            cast_decimal_to_decimal::<Decimal256Type, Decimal64Type>(
1086                array.as_primitive(),
1087                *p1,
1088                *s1,
1089                *p2,
1090                *s2,
1091                cast_options,
1092            )
1093        }
1094        (Decimal256(p1, s1), Decimal128(p2, s2)) => {
1095            cast_decimal_to_decimal::<Decimal256Type, Decimal128Type>(
1096                array.as_primitive(),
1097                *p1,
1098                *s1,
1099                *p2,
1100                *s2,
1101                cast_options,
1102            )
1103        }
1104        // Decimal to non-decimal
1105        (Decimal32(_, scale), _) if !to_type.is_temporal() => {
1106            cast_from_decimal::<Decimal32Type, _>(
1107                array,
1108                10_i32,
1109                scale,
1110                from_type,
1111                to_type,
1112                |x: i32| x as f64,
1113                cast_options,
1114            )
1115        }
1116        (Decimal64(_, scale), _) if !to_type.is_temporal() => {
1117            cast_from_decimal::<Decimal64Type, _>(
1118                array,
1119                10_i64,
1120                scale,
1121                from_type,
1122                to_type,
1123                |x: i64| x as f64,
1124                cast_options,
1125            )
1126        }
1127        (Decimal128(_, scale), _) if !to_type.is_temporal() => {
1128            cast_from_decimal::<Decimal128Type, _>(
1129                array,
1130                10_i128,
1131                scale,
1132                from_type,
1133                to_type,
1134                |x: i128| x as f64,
1135                cast_options,
1136            )
1137        }
1138        (Decimal256(_, scale), _) if !to_type.is_temporal() => {
1139            cast_from_decimal::<Decimal256Type, _>(
1140                array,
1141                i256::from_i128(10_i128),
1142                scale,
1143                from_type,
1144                to_type,
1145                |x: i256| x.to_f64().expect("All i256 values fit in f64"),
1146                cast_options,
1147            )
1148        }
1149        // Non-decimal to decimal
1150        (_, Decimal32(precision, scale)) if !from_type.is_temporal() => {
1151            cast_to_decimal::<Decimal32Type, _>(
1152                array,
1153                10_i32,
1154                precision,
1155                scale,
1156                from_type,
1157                to_type,
1158                cast_options,
1159            )
1160        }
1161        (_, Decimal64(precision, scale)) if !from_type.is_temporal() => {
1162            cast_to_decimal::<Decimal64Type, _>(
1163                array,
1164                10_i64,
1165                precision,
1166                scale,
1167                from_type,
1168                to_type,
1169                cast_options,
1170            )
1171        }
1172        (_, Decimal128(precision, scale)) if !from_type.is_temporal() => {
1173            cast_to_decimal::<Decimal128Type, _>(
1174                array,
1175                10_i128,
1176                precision,
1177                scale,
1178                from_type,
1179                to_type,
1180                cast_options,
1181            )
1182        }
1183        (_, Decimal256(precision, scale)) if !from_type.is_temporal() => {
1184            cast_to_decimal::<Decimal256Type, _>(
1185                array,
1186                i256::from_i128(10_i128),
1187                precision,
1188                scale,
1189                from_type,
1190                to_type,
1191                cast_options,
1192            )
1193        }
1194        (Struct(from_fields), Struct(to_fields)) => {
1195            let array = array.as_struct();
1196
1197            // Fast path: if field names are in the same order, we can just zip and cast
1198            let fields_match_order = from_fields.len() == to_fields.len()
1199                && from_fields
1200                    .iter()
1201                    .zip(to_fields.iter())
1202                    .all(|(f1, f2)| f1.name() == f2.name());
1203
1204            let fields = if fields_match_order {
1205                // Fast path: cast columns in order
1206                array
1207                    .columns()
1208                    .iter()
1209                    .zip(to_fields.iter())
1210                    .map(|(column, field)| {
1211                        cast_with_options(column, field.data_type(), cast_options)
1212                    })
1213                    .collect::<Result<Vec<ArrayRef>, ArrowError>>()?
1214            } else {
1215                // Slow path: match fields by name and reorder
1216                to_fields
1217                    .iter()
1218                    .map(|to_field| {
1219                        let from_field_idx = from_fields
1220                            .iter()
1221                            .position(|from_field| from_field.name() == to_field.name())
1222                            .ok_or_else(|| {
1223                                ArrowError::CastError(format!(
1224                                    "Field '{}' not found in source struct",
1225                                    to_field.name()
1226                                ))
1227                            })?;
1228                        let column = array.column(from_field_idx);
1229                        cast_with_options(column, to_field.data_type(), cast_options)
1230                    })
1231                    .collect::<Result<Vec<ArrayRef>, ArrowError>>()?
1232            };
1233
1234            let array = StructArray::try_new(to_fields.clone(), fields, array.nulls().cloned())?;
1235            Ok(Arc::new(array) as ArrayRef)
1236        }
1237        (Struct(_), _) => Err(ArrowError::CastError(format!(
1238            "Casting from {from_type} to {to_type} not supported"
1239        ))),
1240        (_, Struct(_)) => Err(ArrowError::CastError(format!(
1241            "Casting from {from_type} to {to_type} not supported"
1242        ))),
1243        (_, Boolean) => match from_type {
1244            UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
1245            UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
1246            UInt32 => cast_numeric_to_bool::<UInt32Type>(array),
1247            UInt64 => cast_numeric_to_bool::<UInt64Type>(array),
1248            Int8 => cast_numeric_to_bool::<Int8Type>(array),
1249            Int16 => cast_numeric_to_bool::<Int16Type>(array),
1250            Int32 => cast_numeric_to_bool::<Int32Type>(array),
1251            Int64 => cast_numeric_to_bool::<Int64Type>(array),
1252            Float16 => cast_numeric_to_bool::<Float16Type>(array),
1253            Float32 => cast_numeric_to_bool::<Float32Type>(array),
1254            Float64 => cast_numeric_to_bool::<Float64Type>(array),
1255            Utf8View => cast_utf8view_to_boolean(array, cast_options),
1256            Utf8 => cast_utf8_to_boolean::<i32>(array, cast_options),
1257            LargeUtf8 => cast_utf8_to_boolean::<i64>(array, cast_options),
1258            _ => Err(ArrowError::CastError(format!(
1259                "Casting from {from_type} to {to_type} not supported",
1260            ))),
1261        },
1262        (Boolean, _) => match to_type {
1263            UInt8 => cast_bool_to_numeric::<UInt8Type>(array, cast_options),
1264            UInt16 => cast_bool_to_numeric::<UInt16Type>(array, cast_options),
1265            UInt32 => cast_bool_to_numeric::<UInt32Type>(array, cast_options),
1266            UInt64 => cast_bool_to_numeric::<UInt64Type>(array, cast_options),
1267            Int8 => cast_bool_to_numeric::<Int8Type>(array, cast_options),
1268            Int16 => cast_bool_to_numeric::<Int16Type>(array, cast_options),
1269            Int32 => cast_bool_to_numeric::<Int32Type>(array, cast_options),
1270            Int64 => cast_bool_to_numeric::<Int64Type>(array, cast_options),
1271            Float16 => cast_bool_to_numeric::<Float16Type>(array, cast_options),
1272            Float32 => cast_bool_to_numeric::<Float32Type>(array, cast_options),
1273            Float64 => cast_bool_to_numeric::<Float64Type>(array, cast_options),
1274            Utf8View => value_to_string_view(array, cast_options),
1275            Utf8 => value_to_string::<i32>(array, cast_options),
1276            LargeUtf8 => value_to_string::<i64>(array, cast_options),
1277            _ => Err(ArrowError::CastError(format!(
1278                "Casting from {from_type} to {to_type} not supported",
1279            ))),
1280        },
1281        (Utf8, _) => match to_type {
1282            UInt8 => parse_string::<UInt8Type, i32>(array, cast_options),
1283            UInt16 => parse_string::<UInt16Type, i32>(array, cast_options),
1284            UInt32 => parse_string::<UInt32Type, i32>(array, cast_options),
1285            UInt64 => parse_string::<UInt64Type, i32>(array, cast_options),
1286            Int8 => parse_string::<Int8Type, i32>(array, cast_options),
1287            Int16 => parse_string::<Int16Type, i32>(array, cast_options),
1288            Int32 => parse_string::<Int32Type, i32>(array, cast_options),
1289            Int64 => parse_string::<Int64Type, i32>(array, cast_options),
1290            Float32 => parse_string::<Float32Type, i32>(array, cast_options),
1291            Float64 => parse_string::<Float64Type, i32>(array, cast_options),
1292            Date32 => parse_string::<Date32Type, i32>(array, cast_options),
1293            Date64 => parse_string::<Date64Type, i32>(array, cast_options),
1294            Binary => Ok(Arc::new(BinaryArray::from(
1295                array.as_string::<i32>().clone(),
1296            ))),
1297            LargeBinary => {
1298                let binary = BinaryArray::from(array.as_string::<i32>().clone());
1299                cast_byte_container::<BinaryType, LargeBinaryType>(&binary)
1300            }
1301            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i32>()))),
1302            BinaryView => Ok(Arc::new(
1303                StringViewArray::from(array.as_string::<i32>()).to_binary_view(),
1304            )),
1305            LargeUtf8 => cast_byte_container::<Utf8Type, LargeUtf8Type>(array),
1306            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i32>(array, cast_options),
1307            Time32(TimeUnit::Millisecond) => {
1308                parse_string::<Time32MillisecondType, i32>(array, cast_options)
1309            }
1310            Time64(TimeUnit::Microsecond) => {
1311                parse_string::<Time64MicrosecondType, i32>(array, cast_options)
1312            }
1313            Time64(TimeUnit::Nanosecond) => {
1314                parse_string::<Time64NanosecondType, i32>(array, cast_options)
1315            }
1316            Timestamp(TimeUnit::Second, to_tz) => {
1317                cast_string_to_timestamp::<i32, TimestampSecondType>(array, to_tz, cast_options)
1318            }
1319            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1320                i32,
1321                TimestampMillisecondType,
1322            >(array, to_tz, cast_options),
1323            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1324                i32,
1325                TimestampMicrosecondType,
1326            >(array, to_tz, cast_options),
1327            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1328                cast_string_to_timestamp::<i32, TimestampNanosecondType>(array, to_tz, cast_options)
1329            }
1330            Interval(IntervalUnit::YearMonth) => {
1331                cast_string_to_year_month_interval::<i32>(array, cast_options)
1332            }
1333            Interval(IntervalUnit::DayTime) => {
1334                cast_string_to_day_time_interval::<i32>(array, cast_options)
1335            }
1336            Interval(IntervalUnit::MonthDayNano) => {
1337                cast_string_to_month_day_nano_interval::<i32>(array, cast_options)
1338            }
1339            _ => Err(ArrowError::CastError(format!(
1340                "Casting from {from_type} to {to_type} not supported",
1341            ))),
1342        },
1343        (Utf8View, _) => match to_type {
1344            UInt8 => parse_string_view::<UInt8Type>(array, cast_options),
1345            UInt16 => parse_string_view::<UInt16Type>(array, cast_options),
1346            UInt32 => parse_string_view::<UInt32Type>(array, cast_options),
1347            UInt64 => parse_string_view::<UInt64Type>(array, cast_options),
1348            Int8 => parse_string_view::<Int8Type>(array, cast_options),
1349            Int16 => parse_string_view::<Int16Type>(array, cast_options),
1350            Int32 => parse_string_view::<Int32Type>(array, cast_options),
1351            Int64 => parse_string_view::<Int64Type>(array, cast_options),
1352            Float32 => parse_string_view::<Float32Type>(array, cast_options),
1353            Float64 => parse_string_view::<Float64Type>(array, cast_options),
1354            Date32 => parse_string_view::<Date32Type>(array, cast_options),
1355            Date64 => parse_string_view::<Date64Type>(array, cast_options),
1356            Binary => cast_view_to_byte::<StringViewType, GenericBinaryType<i32>>(array),
1357            LargeBinary => cast_view_to_byte::<StringViewType, GenericBinaryType<i64>>(array),
1358            BinaryView => Ok(Arc::new(array.as_string_view().clone().to_binary_view())),
1359            Utf8 => cast_view_to_byte::<StringViewType, GenericStringType<i32>>(array),
1360            LargeUtf8 => cast_view_to_byte::<StringViewType, GenericStringType<i64>>(array),
1361            Time32(TimeUnit::Second) => parse_string_view::<Time32SecondType>(array, cast_options),
1362            Time32(TimeUnit::Millisecond) => {
1363                parse_string_view::<Time32MillisecondType>(array, cast_options)
1364            }
1365            Time64(TimeUnit::Microsecond) => {
1366                parse_string_view::<Time64MicrosecondType>(array, cast_options)
1367            }
1368            Time64(TimeUnit::Nanosecond) => {
1369                parse_string_view::<Time64NanosecondType>(array, cast_options)
1370            }
1371            Timestamp(TimeUnit::Second, to_tz) => {
1372                cast_view_to_timestamp::<TimestampSecondType>(array, to_tz, cast_options)
1373            }
1374            Timestamp(TimeUnit::Millisecond, to_tz) => {
1375                cast_view_to_timestamp::<TimestampMillisecondType>(array, to_tz, cast_options)
1376            }
1377            Timestamp(TimeUnit::Microsecond, to_tz) => {
1378                cast_view_to_timestamp::<TimestampMicrosecondType>(array, to_tz, cast_options)
1379            }
1380            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1381                cast_view_to_timestamp::<TimestampNanosecondType>(array, to_tz, cast_options)
1382            }
1383            Interval(IntervalUnit::YearMonth) => {
1384                cast_view_to_year_month_interval(array, cast_options)
1385            }
1386            Interval(IntervalUnit::DayTime) => cast_view_to_day_time_interval(array, cast_options),
1387            Interval(IntervalUnit::MonthDayNano) => {
1388                cast_view_to_month_day_nano_interval(array, cast_options)
1389            }
1390            _ => Err(ArrowError::CastError(format!(
1391                "Casting from {from_type} to {to_type} not supported",
1392            ))),
1393        },
1394        (LargeUtf8, _) => match to_type {
1395            UInt8 => parse_string::<UInt8Type, i64>(array, cast_options),
1396            UInt16 => parse_string::<UInt16Type, i64>(array, cast_options),
1397            UInt32 => parse_string::<UInt32Type, i64>(array, cast_options),
1398            UInt64 => parse_string::<UInt64Type, i64>(array, cast_options),
1399            Int8 => parse_string::<Int8Type, i64>(array, cast_options),
1400            Int16 => parse_string::<Int16Type, i64>(array, cast_options),
1401            Int32 => parse_string::<Int32Type, i64>(array, cast_options),
1402            Int64 => parse_string::<Int64Type, i64>(array, cast_options),
1403            Float32 => parse_string::<Float32Type, i64>(array, cast_options),
1404            Float64 => parse_string::<Float64Type, i64>(array, cast_options),
1405            Date32 => parse_string::<Date32Type, i64>(array, cast_options),
1406            Date64 => parse_string::<Date64Type, i64>(array, cast_options),
1407            Utf8 => cast_byte_container::<LargeUtf8Type, Utf8Type>(array),
1408            Binary => {
1409                let large_binary = LargeBinaryArray::from(array.as_string::<i64>().clone());
1410                cast_byte_container::<LargeBinaryType, BinaryType>(&large_binary)
1411            }
1412            LargeBinary => Ok(Arc::new(LargeBinaryArray::from(
1413                array.as_string::<i64>().clone(),
1414            ))),
1415            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i64>()))),
1416            BinaryView => Ok(Arc::new(BinaryViewArray::from(
1417                array
1418                    .as_string::<i64>()
1419                    .into_iter()
1420                    .map(|x| x.map(|x| x.as_bytes()))
1421                    .collect::<Vec<_>>(),
1422            ))),
1423            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i64>(array, cast_options),
1424            Time32(TimeUnit::Millisecond) => {
1425                parse_string::<Time32MillisecondType, i64>(array, cast_options)
1426            }
1427            Time64(TimeUnit::Microsecond) => {
1428                parse_string::<Time64MicrosecondType, i64>(array, cast_options)
1429            }
1430            Time64(TimeUnit::Nanosecond) => {
1431                parse_string::<Time64NanosecondType, i64>(array, cast_options)
1432            }
1433            Timestamp(TimeUnit::Second, to_tz) => {
1434                cast_string_to_timestamp::<i64, TimestampSecondType>(array, to_tz, cast_options)
1435            }
1436            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1437                i64,
1438                TimestampMillisecondType,
1439            >(array, to_tz, cast_options),
1440            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1441                i64,
1442                TimestampMicrosecondType,
1443            >(array, to_tz, cast_options),
1444            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1445                cast_string_to_timestamp::<i64, TimestampNanosecondType>(array, to_tz, cast_options)
1446            }
1447            Interval(IntervalUnit::YearMonth) => {
1448                cast_string_to_year_month_interval::<i64>(array, cast_options)
1449            }
1450            Interval(IntervalUnit::DayTime) => {
1451                cast_string_to_day_time_interval::<i64>(array, cast_options)
1452            }
1453            Interval(IntervalUnit::MonthDayNano) => {
1454                cast_string_to_month_day_nano_interval::<i64>(array, cast_options)
1455            }
1456            _ => Err(ArrowError::CastError(format!(
1457                "Casting from {from_type} to {to_type} not supported",
1458            ))),
1459        },
1460        (Binary, _) => match to_type {
1461            Utf8 => cast_binary_to_string::<i32>(array, cast_options),
1462            LargeUtf8 => {
1463                let array = cast_binary_to_string::<i32>(array, cast_options)?;
1464                cast_byte_container::<Utf8Type, LargeUtf8Type>(array.as_ref())
1465            }
1466            LargeBinary => cast_byte_container::<BinaryType, LargeBinaryType>(array),
1467            FixedSizeBinary(size) => {
1468                cast_binary_to_fixed_size_binary::<i32>(array, *size, cast_options)
1469            }
1470            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i32>()))),
1471            Utf8View => Ok(Arc::new(StringViewArray::from(
1472                cast_binary_to_string::<i32>(array, cast_options)?.as_string::<i32>(),
1473            ))),
1474            _ => Err(ArrowError::CastError(format!(
1475                "Casting from {from_type} to {to_type} not supported",
1476            ))),
1477        },
1478        (LargeBinary, _) => match to_type {
1479            Utf8 => {
1480                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1481                cast_byte_container::<LargeUtf8Type, Utf8Type>(array.as_ref())
1482            }
1483            LargeUtf8 => cast_binary_to_string::<i64>(array, cast_options),
1484            Binary => cast_byte_container::<LargeBinaryType, BinaryType>(array),
1485            FixedSizeBinary(size) => {
1486                cast_binary_to_fixed_size_binary::<i64>(array, *size, cast_options)
1487            }
1488            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i64>()))),
1489            Utf8View => {
1490                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1491                Ok(Arc::new(StringViewArray::from(array.as_string::<i64>())))
1492            }
1493            _ => Err(ArrowError::CastError(format!(
1494                "Casting from {from_type} to {to_type} not supported",
1495            ))),
1496        },
1497        (FixedSizeBinary(size), _) => match to_type {
1498            Binary => cast_fixed_size_binary_to_binary::<i32>(array, *size),
1499            LargeBinary => cast_fixed_size_binary_to_binary::<i64>(array, *size),
1500            BinaryView => cast_fixed_size_binary_to_binary_view(array, *size),
1501            _ => Err(ArrowError::CastError(format!(
1502                "Casting from {from_type} to {to_type} not supported",
1503            ))),
1504        },
1505        (BinaryView, Binary) => cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array),
1506        (BinaryView, LargeBinary) => {
1507            cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)
1508        }
1509        (BinaryView, Utf8) => {
1510            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array)?;
1511            cast_binary_to_string::<i32>(&binary_arr, cast_options)
1512        }
1513        (BinaryView, LargeUtf8) => {
1514            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)?;
1515            cast_binary_to_string::<i64>(&binary_arr, cast_options)
1516        }
1517        (BinaryView, Utf8View) => cast_binary_view_to_string_view(array, cast_options),
1518        (BinaryView, _) => Err(ArrowError::CastError(format!(
1519            "Casting from {from_type} to {to_type} not supported",
1520        ))),
1521        (from_type, Utf8View) if from_type.is_primitive() => {
1522            value_to_string_view(array, cast_options)
1523        }
1524        (from_type, LargeUtf8) if from_type.is_primitive() => {
1525            value_to_string::<i64>(array, cast_options)
1526        }
1527        (from_type, Utf8) if from_type.is_primitive() => {
1528            value_to_string::<i32>(array, cast_options)
1529        }
1530        (from_type, Binary) if from_type.is_integer() => match from_type {
1531            UInt8 => cast_numeric_to_binary::<UInt8Type, i32>(array),
1532            UInt16 => cast_numeric_to_binary::<UInt16Type, i32>(array),
1533            UInt32 => cast_numeric_to_binary::<UInt32Type, i32>(array),
1534            UInt64 => cast_numeric_to_binary::<UInt64Type, i32>(array),
1535            Int8 => cast_numeric_to_binary::<Int8Type, i32>(array),
1536            Int16 => cast_numeric_to_binary::<Int16Type, i32>(array),
1537            Int32 => cast_numeric_to_binary::<Int32Type, i32>(array),
1538            Int64 => cast_numeric_to_binary::<Int64Type, i32>(array),
1539            _ => unreachable!(),
1540        },
1541        (from_type, LargeBinary) if from_type.is_integer() => match from_type {
1542            UInt8 => cast_numeric_to_binary::<UInt8Type, i64>(array),
1543            UInt16 => cast_numeric_to_binary::<UInt16Type, i64>(array),
1544            UInt32 => cast_numeric_to_binary::<UInt32Type, i64>(array),
1545            UInt64 => cast_numeric_to_binary::<UInt64Type, i64>(array),
1546            Int8 => cast_numeric_to_binary::<Int8Type, i64>(array),
1547            Int16 => cast_numeric_to_binary::<Int16Type, i64>(array),
1548            Int32 => cast_numeric_to_binary::<Int32Type, i64>(array),
1549            Int64 => cast_numeric_to_binary::<Int64Type, i64>(array),
1550            _ => unreachable!(),
1551        },
1552        // start numeric casts
1553        (UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array, cast_options),
1554        (UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array, cast_options),
1555        (UInt8, UInt64) => cast_numeric_arrays::<UInt8Type, UInt64Type>(array, cast_options),
1556        (UInt8, Int8) => cast_numeric_arrays::<UInt8Type, Int8Type>(array, cast_options),
1557        (UInt8, Int16) => cast_numeric_arrays::<UInt8Type, Int16Type>(array, cast_options),
1558        (UInt8, Int32) => cast_numeric_arrays::<UInt8Type, Int32Type>(array, cast_options),
1559        (UInt8, Int64) => cast_numeric_arrays::<UInt8Type, Int64Type>(array, cast_options),
1560        (UInt8, Float16) => cast_numeric_arrays::<UInt8Type, Float16Type>(array, cast_options),
1561        (UInt8, Float32) => cast_numeric_arrays::<UInt8Type, Float32Type>(array, cast_options),
1562        (UInt8, Float64) => cast_numeric_arrays::<UInt8Type, Float64Type>(array, cast_options),
1563
1564        (UInt16, UInt8) => cast_numeric_arrays::<UInt16Type, UInt8Type>(array, cast_options),
1565        (UInt16, UInt32) => cast_numeric_arrays::<UInt16Type, UInt32Type>(array, cast_options),
1566        (UInt16, UInt64) => cast_numeric_arrays::<UInt16Type, UInt64Type>(array, cast_options),
1567        (UInt16, Int8) => cast_numeric_arrays::<UInt16Type, Int8Type>(array, cast_options),
1568        (UInt16, Int16) => cast_numeric_arrays::<UInt16Type, Int16Type>(array, cast_options),
1569        (UInt16, Int32) => cast_numeric_arrays::<UInt16Type, Int32Type>(array, cast_options),
1570        (UInt16, Int64) => cast_numeric_arrays::<UInt16Type, Int64Type>(array, cast_options),
1571        (UInt16, Float16) => cast_numeric_arrays::<UInt16Type, Float16Type>(array, cast_options),
1572        (UInt16, Float32) => cast_numeric_arrays::<UInt16Type, Float32Type>(array, cast_options),
1573        (UInt16, Float64) => cast_numeric_arrays::<UInt16Type, Float64Type>(array, cast_options),
1574
1575        (UInt32, UInt8) => cast_numeric_arrays::<UInt32Type, UInt8Type>(array, cast_options),
1576        (UInt32, UInt16) => cast_numeric_arrays::<UInt32Type, UInt16Type>(array, cast_options),
1577        (UInt32, UInt64) => cast_numeric_arrays::<UInt32Type, UInt64Type>(array, cast_options),
1578        (UInt32, Int8) => cast_numeric_arrays::<UInt32Type, Int8Type>(array, cast_options),
1579        (UInt32, Int16) => cast_numeric_arrays::<UInt32Type, Int16Type>(array, cast_options),
1580        (UInt32, Int32) => cast_numeric_arrays::<UInt32Type, Int32Type>(array, cast_options),
1581        (UInt32, Int64) => cast_numeric_arrays::<UInt32Type, Int64Type>(array, cast_options),
1582        (UInt32, Float16) => cast_numeric_arrays::<UInt32Type, Float16Type>(array, cast_options),
1583        (UInt32, Float32) => cast_numeric_arrays::<UInt32Type, Float32Type>(array, cast_options),
1584        (UInt32, Float64) => cast_numeric_arrays::<UInt32Type, Float64Type>(array, cast_options),
1585
1586        (UInt64, UInt8) => cast_numeric_arrays::<UInt64Type, UInt8Type>(array, cast_options),
1587        (UInt64, UInt16) => cast_numeric_arrays::<UInt64Type, UInt16Type>(array, cast_options),
1588        (UInt64, UInt32) => cast_numeric_arrays::<UInt64Type, UInt32Type>(array, cast_options),
1589        (UInt64, Int8) => cast_numeric_arrays::<UInt64Type, Int8Type>(array, cast_options),
1590        (UInt64, Int16) => cast_numeric_arrays::<UInt64Type, Int16Type>(array, cast_options),
1591        (UInt64, Int32) => cast_numeric_arrays::<UInt64Type, Int32Type>(array, cast_options),
1592        (UInt64, Int64) => cast_numeric_arrays::<UInt64Type, Int64Type>(array, cast_options),
1593        (UInt64, Float16) => cast_numeric_arrays::<UInt64Type, Float16Type>(array, cast_options),
1594        (UInt64, Float32) => cast_numeric_arrays::<UInt64Type, Float32Type>(array, cast_options),
1595        (UInt64, Float64) => cast_numeric_arrays::<UInt64Type, Float64Type>(array, cast_options),
1596
1597        (Int8, UInt8) => cast_numeric_arrays::<Int8Type, UInt8Type>(array, cast_options),
1598        (Int8, UInt16) => cast_numeric_arrays::<Int8Type, UInt16Type>(array, cast_options),
1599        (Int8, UInt32) => cast_numeric_arrays::<Int8Type, UInt32Type>(array, cast_options),
1600        (Int8, UInt64) => cast_numeric_arrays::<Int8Type, UInt64Type>(array, cast_options),
1601        (Int8, Int16) => cast_numeric_arrays::<Int8Type, Int16Type>(array, cast_options),
1602        (Int8, Int32) => cast_numeric_arrays::<Int8Type, Int32Type>(array, cast_options),
1603        (Int8, Int64) => cast_numeric_arrays::<Int8Type, Int64Type>(array, cast_options),
1604        (Int8, Float16) => cast_numeric_arrays::<Int8Type, Float16Type>(array, cast_options),
1605        (Int8, Float32) => cast_numeric_arrays::<Int8Type, Float32Type>(array, cast_options),
1606        (Int8, Float64) => cast_numeric_arrays::<Int8Type, Float64Type>(array, cast_options),
1607
1608        (Int16, UInt8) => cast_numeric_arrays::<Int16Type, UInt8Type>(array, cast_options),
1609        (Int16, UInt16) => cast_numeric_arrays::<Int16Type, UInt16Type>(array, cast_options),
1610        (Int16, UInt32) => cast_numeric_arrays::<Int16Type, UInt32Type>(array, cast_options),
1611        (Int16, UInt64) => cast_numeric_arrays::<Int16Type, UInt64Type>(array, cast_options),
1612        (Int16, Int8) => cast_numeric_arrays::<Int16Type, Int8Type>(array, cast_options),
1613        (Int16, Int32) => cast_numeric_arrays::<Int16Type, Int32Type>(array, cast_options),
1614        (Int16, Int64) => cast_numeric_arrays::<Int16Type, Int64Type>(array, cast_options),
1615        (Int16, Float16) => cast_numeric_arrays::<Int16Type, Float16Type>(array, cast_options),
1616        (Int16, Float32) => cast_numeric_arrays::<Int16Type, Float32Type>(array, cast_options),
1617        (Int16, Float64) => cast_numeric_arrays::<Int16Type, Float64Type>(array, cast_options),
1618
1619        (Int32, UInt8) => cast_numeric_arrays::<Int32Type, UInt8Type>(array, cast_options),
1620        (Int32, UInt16) => cast_numeric_arrays::<Int32Type, UInt16Type>(array, cast_options),
1621        (Int32, UInt32) => cast_numeric_arrays::<Int32Type, UInt32Type>(array, cast_options),
1622        (Int32, UInt64) => cast_numeric_arrays::<Int32Type, UInt64Type>(array, cast_options),
1623        (Int32, Int8) => cast_numeric_arrays::<Int32Type, Int8Type>(array, cast_options),
1624        (Int32, Int16) => cast_numeric_arrays::<Int32Type, Int16Type>(array, cast_options),
1625        (Int32, Int64) => cast_numeric_arrays::<Int32Type, Int64Type>(array, cast_options),
1626        (Int32, Float16) => cast_numeric_arrays::<Int32Type, Float16Type>(array, cast_options),
1627        (Int32, Float32) => cast_numeric_arrays::<Int32Type, Float32Type>(array, cast_options),
1628        (Int32, Float64) => cast_numeric_arrays::<Int32Type, Float64Type>(array, cast_options),
1629
1630        (Int64, UInt8) => cast_numeric_arrays::<Int64Type, UInt8Type>(array, cast_options),
1631        (Int64, UInt16) => cast_numeric_arrays::<Int64Type, UInt16Type>(array, cast_options),
1632        (Int64, UInt32) => cast_numeric_arrays::<Int64Type, UInt32Type>(array, cast_options),
1633        (Int64, UInt64) => cast_numeric_arrays::<Int64Type, UInt64Type>(array, cast_options),
1634        (Int64, Int8) => cast_numeric_arrays::<Int64Type, Int8Type>(array, cast_options),
1635        (Int64, Int16) => cast_numeric_arrays::<Int64Type, Int16Type>(array, cast_options),
1636        (Int64, Int32) => cast_numeric_arrays::<Int64Type, Int32Type>(array, cast_options),
1637        (Int64, Float16) => cast_numeric_arrays::<Int64Type, Float16Type>(array, cast_options),
1638        (Int64, Float32) => cast_numeric_arrays::<Int64Type, Float32Type>(array, cast_options),
1639        (Int64, Float64) => cast_numeric_arrays::<Int64Type, Float64Type>(array, cast_options),
1640
1641        (Float16, UInt8) => cast_numeric_arrays::<Float16Type, UInt8Type>(array, cast_options),
1642        (Float16, UInt16) => cast_numeric_arrays::<Float16Type, UInt16Type>(array, cast_options),
1643        (Float16, UInt32) => cast_numeric_arrays::<Float16Type, UInt32Type>(array, cast_options),
1644        (Float16, UInt64) => cast_numeric_arrays::<Float16Type, UInt64Type>(array, cast_options),
1645        (Float16, Int8) => cast_numeric_arrays::<Float16Type, Int8Type>(array, cast_options),
1646        (Float16, Int16) => cast_numeric_arrays::<Float16Type, Int16Type>(array, cast_options),
1647        (Float16, Int32) => cast_numeric_arrays::<Float16Type, Int32Type>(array, cast_options),
1648        (Float16, Int64) => cast_numeric_arrays::<Float16Type, Int64Type>(array, cast_options),
1649        (Float16, Float32) => cast_numeric_arrays::<Float16Type, Float32Type>(array, cast_options),
1650        (Float16, Float64) => cast_numeric_arrays::<Float16Type, Float64Type>(array, cast_options),
1651
1652        (Float32, UInt8) => cast_numeric_arrays::<Float32Type, UInt8Type>(array, cast_options),
1653        (Float32, UInt16) => cast_numeric_arrays::<Float32Type, UInt16Type>(array, cast_options),
1654        (Float32, UInt32) => cast_numeric_arrays::<Float32Type, UInt32Type>(array, cast_options),
1655        (Float32, UInt64) => cast_numeric_arrays::<Float32Type, UInt64Type>(array, cast_options),
1656        (Float32, Int8) => cast_numeric_arrays::<Float32Type, Int8Type>(array, cast_options),
1657        (Float32, Int16) => cast_numeric_arrays::<Float32Type, Int16Type>(array, cast_options),
1658        (Float32, Int32) => cast_numeric_arrays::<Float32Type, Int32Type>(array, cast_options),
1659        (Float32, Int64) => cast_numeric_arrays::<Float32Type, Int64Type>(array, cast_options),
1660        (Float32, Float16) => cast_numeric_arrays::<Float32Type, Float16Type>(array, cast_options),
1661        (Float32, Float64) => cast_numeric_arrays::<Float32Type, Float64Type>(array, cast_options),
1662
1663        (Float64, UInt8) => cast_numeric_arrays::<Float64Type, UInt8Type>(array, cast_options),
1664        (Float64, UInt16) => cast_numeric_arrays::<Float64Type, UInt16Type>(array, cast_options),
1665        (Float64, UInt32) => cast_numeric_arrays::<Float64Type, UInt32Type>(array, cast_options),
1666        (Float64, UInt64) => cast_numeric_arrays::<Float64Type, UInt64Type>(array, cast_options),
1667        (Float64, Int8) => cast_numeric_arrays::<Float64Type, Int8Type>(array, cast_options),
1668        (Float64, Int16) => cast_numeric_arrays::<Float64Type, Int16Type>(array, cast_options),
1669        (Float64, Int32) => cast_numeric_arrays::<Float64Type, Int32Type>(array, cast_options),
1670        (Float64, Int64) => cast_numeric_arrays::<Float64Type, Int64Type>(array, cast_options),
1671        (Float64, Float16) => cast_numeric_arrays::<Float64Type, Float16Type>(array, cast_options),
1672        (Float64, Float32) => cast_numeric_arrays::<Float64Type, Float32Type>(array, cast_options),
1673        // end numeric casts
1674
1675        // temporal casts
1676        (Int32, Date32) => cast_reinterpret_arrays::<Int32Type, Date32Type>(array),
1677        (Int32, Date64) => cast_with_options(
1678            &cast_with_options(array, &Date32, cast_options)?,
1679            &Date64,
1680            cast_options,
1681        ),
1682        (Int32, Time32(TimeUnit::Second)) => {
1683            cast_reinterpret_arrays::<Int32Type, Time32SecondType>(array)
1684        }
1685        (Int32, Time32(TimeUnit::Millisecond)) => {
1686            cast_reinterpret_arrays::<Int32Type, Time32MillisecondType>(array)
1687        }
1688        // No support for microsecond/nanosecond with i32
1689        (Date32, Int32) => cast_reinterpret_arrays::<Date32Type, Int32Type>(array),
1690        (Date32, Int64) => cast_with_options(
1691            &cast_with_options(array, &Int32, cast_options)?,
1692            &Int64,
1693            cast_options,
1694        ),
1695        (Time32(TimeUnit::Second), Int32) => {
1696            cast_reinterpret_arrays::<Time32SecondType, Int32Type>(array)
1697        }
1698        (Time32(TimeUnit::Millisecond), Int32) => {
1699            cast_reinterpret_arrays::<Time32MillisecondType, Int32Type>(array)
1700        }
1701        (Int64, Date64) => cast_reinterpret_arrays::<Int64Type, Date64Type>(array),
1702        (Int64, Date32) => cast_with_options(
1703            &cast_with_options(array, &Int32, cast_options)?,
1704            &Date32,
1705            cast_options,
1706        ),
1707        // No support for second/milliseconds with i64
1708        (Int64, Time64(TimeUnit::Microsecond)) => {
1709            cast_reinterpret_arrays::<Int64Type, Time64MicrosecondType>(array)
1710        }
1711        (Int64, Time64(TimeUnit::Nanosecond)) => {
1712            cast_reinterpret_arrays::<Int64Type, Time64NanosecondType>(array)
1713        }
1714
1715        (Date64, Int64) => cast_reinterpret_arrays::<Date64Type, Int64Type>(array),
1716        (Date64, Int32) => cast_with_options(
1717            &cast_with_options(array, &Int64, cast_options)?,
1718            &Int32,
1719            cast_options,
1720        ),
1721        (Time64(TimeUnit::Microsecond), Int64) => {
1722            cast_reinterpret_arrays::<Time64MicrosecondType, Int64Type>(array)
1723        }
1724        (Time64(TimeUnit::Nanosecond), Int64) => {
1725            cast_reinterpret_arrays::<Time64NanosecondType, Int64Type>(array)
1726        }
1727        (Date32, Date64) => Ok(Arc::new(
1728            array
1729                .as_primitive::<Date32Type>()
1730                .unary::<_, Date64Type>(|x| x as i64 * MILLISECONDS_IN_DAY),
1731        )),
1732        (Date64, Date32) => Ok(Arc::new(
1733            array
1734                .as_primitive::<Date64Type>()
1735                .unary::<_, Date32Type>(|x| (x / MILLISECONDS_IN_DAY) as i32),
1736        )),
1737
1738        (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1739            array
1740                .as_primitive::<Time32SecondType>()
1741                .unary::<_, Time32MillisecondType>(|x| x * MILLISECONDS as i32),
1742        )),
1743        (Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1744            array
1745                .as_primitive::<Time32SecondType>()
1746                .unary::<_, Time64MicrosecondType>(|x| x as i64 * MICROSECONDS),
1747        )),
1748        (Time32(TimeUnit::Second), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1749            array
1750                .as_primitive::<Time32SecondType>()
1751                .unary::<_, Time64NanosecondType>(|x| x as i64 * NANOSECONDS),
1752        )),
1753
1754        (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1755            array
1756                .as_primitive::<Time32MillisecondType>()
1757                .unary::<_, Time32SecondType>(|x| x / MILLISECONDS as i32),
1758        )),
1759        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1760            array
1761                .as_primitive::<Time32MillisecondType>()
1762                .unary::<_, Time64MicrosecondType>(|x| x as i64 * (MICROSECONDS / MILLISECONDS)),
1763        )),
1764        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1765            array
1766                .as_primitive::<Time32MillisecondType>()
1767                .unary::<_, Time64NanosecondType>(|x| x as i64 * (MICROSECONDS / NANOSECONDS)),
1768        )),
1769
1770        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1771            array
1772                .as_primitive::<Time64MicrosecondType>()
1773                .unary::<_, Time32SecondType>(|x| (x / MICROSECONDS) as i32),
1774        )),
1775        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1776            array
1777                .as_primitive::<Time64MicrosecondType>()
1778                .unary::<_, Time32MillisecondType>(|x| (x / (MICROSECONDS / MILLISECONDS)) as i32),
1779        )),
1780        (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1781            array
1782                .as_primitive::<Time64MicrosecondType>()
1783                .unary::<_, Time64NanosecondType>(|x| x * (NANOSECONDS / MICROSECONDS)),
1784        )),
1785
1786        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1787            array
1788                .as_primitive::<Time64NanosecondType>()
1789                .unary::<_, Time32SecondType>(|x| (x / NANOSECONDS) as i32),
1790        )),
1791        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1792            array
1793                .as_primitive::<Time64NanosecondType>()
1794                .unary::<_, Time32MillisecondType>(|x| (x / (NANOSECONDS / MILLISECONDS)) as i32),
1795        )),
1796        (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1797            array
1798                .as_primitive::<Time64NanosecondType>()
1799                .unary::<_, Time64MicrosecondType>(|x| x / (NANOSECONDS / MICROSECONDS)),
1800        )),
1801
1802        // Timestamp to integer/floating/decimals
1803        (Timestamp(TimeUnit::Second, _), _) if to_type.is_numeric() => {
1804            let array = cast_reinterpret_arrays::<TimestampSecondType, Int64Type>(array)?;
1805            cast_with_options(&array, to_type, cast_options)
1806        }
1807        (Timestamp(TimeUnit::Millisecond, _), _) if to_type.is_numeric() => {
1808            let array = cast_reinterpret_arrays::<TimestampMillisecondType, Int64Type>(array)?;
1809            cast_with_options(&array, to_type, cast_options)
1810        }
1811        (Timestamp(TimeUnit::Microsecond, _), _) if to_type.is_numeric() => {
1812            let array = cast_reinterpret_arrays::<TimestampMicrosecondType, Int64Type>(array)?;
1813            cast_with_options(&array, to_type, cast_options)
1814        }
1815        (Timestamp(TimeUnit::Nanosecond, _), _) if to_type.is_numeric() => {
1816            let array = cast_reinterpret_arrays::<TimestampNanosecondType, Int64Type>(array)?;
1817            cast_with_options(&array, to_type, cast_options)
1818        }
1819
1820        (_, Timestamp(unit, tz)) if from_type.is_numeric() => {
1821            let array = cast_with_options(array, &Int64, cast_options)?;
1822            Ok(make_timestamp_array(
1823                array.as_primitive(),
1824                *unit,
1825                tz.clone(),
1826            ))
1827        }
1828
1829        (Timestamp(from_unit, from_tz), Timestamp(to_unit, to_tz)) => {
1830            let array = cast_with_options(array, &Int64, cast_options)?;
1831            let time_array = array.as_primitive::<Int64Type>();
1832            let from_size = time_unit_multiple(from_unit);
1833            let to_size = time_unit_multiple(to_unit);
1834            // we either divide or multiply, depending on size of each unit
1835            // units are never the same when the types are the same
1836            let converted = match from_size.cmp(&to_size) {
1837                Ordering::Greater => {
1838                    let divisor = from_size / to_size;
1839                    time_array.unary::<_, Int64Type>(|o| o / divisor)
1840                }
1841                Ordering::Equal => time_array.clone(),
1842                Ordering::Less => {
1843                    let mul = to_size / from_size;
1844                    if cast_options.safe {
1845                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1846                    } else {
1847                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1848                    }
1849                }
1850            };
1851            // Normalize timezone
1852            let adjusted = match (from_tz, to_tz) {
1853                // Only this case needs to be adjusted because we're casting from
1854                // unknown time offset to some time offset, we want the time to be
1855                // unchanged.
1856                //
1857                // i.e. Timestamp('2001-01-01T00:00', None) -> Timestamp('2001-01-01T00:00', '+0700')
1858                (None, Some(to_tz)) => {
1859                    let to_tz: Tz = to_tz.parse()?;
1860                    match to_unit {
1861                        TimeUnit::Second => adjust_timestamp_to_timezone::<TimestampSecondType>(
1862                            converted,
1863                            &to_tz,
1864                            cast_options,
1865                        )?,
1866                        TimeUnit::Millisecond => adjust_timestamp_to_timezone::<
1867                            TimestampMillisecondType,
1868                        >(
1869                            converted, &to_tz, cast_options
1870                        )?,
1871                        TimeUnit::Microsecond => adjust_timestamp_to_timezone::<
1872                            TimestampMicrosecondType,
1873                        >(
1874                            converted, &to_tz, cast_options
1875                        )?,
1876                        TimeUnit::Nanosecond => adjust_timestamp_to_timezone::<
1877                            TimestampNanosecondType,
1878                        >(
1879                            converted, &to_tz, cast_options
1880                        )?,
1881                    }
1882                }
1883                _ => converted,
1884            };
1885            Ok(make_timestamp_array(&adjusted, *to_unit, to_tz.clone()))
1886        }
1887        (Timestamp(TimeUnit::Microsecond, _), Date32) => {
1888            timestamp_to_date32(array.as_primitive::<TimestampMicrosecondType>())
1889        }
1890        (Timestamp(TimeUnit::Millisecond, _), Date32) => {
1891            timestamp_to_date32(array.as_primitive::<TimestampMillisecondType>())
1892        }
1893        (Timestamp(TimeUnit::Second, _), Date32) => {
1894            timestamp_to_date32(array.as_primitive::<TimestampSecondType>())
1895        }
1896        (Timestamp(TimeUnit::Nanosecond, _), Date32) => {
1897            timestamp_to_date32(array.as_primitive::<TimestampNanosecondType>())
1898        }
1899        (Timestamp(TimeUnit::Second, _), Date64) => Ok(Arc::new(match cast_options.safe {
1900            true => {
1901                // change error to None
1902                array
1903                    .as_primitive::<TimestampSecondType>()
1904                    .unary_opt::<_, Date64Type>(|x| x.checked_mul(MILLISECONDS))
1905            }
1906            false => array
1907                .as_primitive::<TimestampSecondType>()
1908                .try_unary::<_, Date64Type, _>(|x| x.mul_checked(MILLISECONDS))?,
1909        })),
1910        (Timestamp(TimeUnit::Millisecond, _), Date64) => {
1911            cast_reinterpret_arrays::<TimestampMillisecondType, Date64Type>(array)
1912        }
1913        (Timestamp(TimeUnit::Microsecond, _), Date64) => Ok(Arc::new(
1914            array
1915                .as_primitive::<TimestampMicrosecondType>()
1916                .unary::<_, Date64Type>(|x| x / (MICROSECONDS / MILLISECONDS)),
1917        )),
1918        (Timestamp(TimeUnit::Nanosecond, _), Date64) => Ok(Arc::new(
1919            array
1920                .as_primitive::<TimestampNanosecondType>()
1921                .unary::<_, Date64Type>(|x| x / (NANOSECONDS / MILLISECONDS)),
1922        )),
1923        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Microsecond)) => {
1924            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1925            Ok(Arc::new(
1926                array
1927                    .as_primitive::<TimestampSecondType>()
1928                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1929                        Ok(time_to_time64us(as_time_res_with_timezone::<
1930                            TimestampSecondType,
1931                        >(x, tz)?))
1932                    })?,
1933            ))
1934        }
1935        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Nanosecond)) => {
1936            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1937            Ok(Arc::new(
1938                array
1939                    .as_primitive::<TimestampSecondType>()
1940                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1941                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1942                            TimestampSecondType,
1943                        >(x, tz)?))
1944                    })?,
1945            ))
1946        }
1947        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Microsecond)) => {
1948            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1949            Ok(Arc::new(
1950                array
1951                    .as_primitive::<TimestampMillisecondType>()
1952                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1953                        Ok(time_to_time64us(as_time_res_with_timezone::<
1954                            TimestampMillisecondType,
1955                        >(x, tz)?))
1956                    })?,
1957            ))
1958        }
1959        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Nanosecond)) => {
1960            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1961            Ok(Arc::new(
1962                array
1963                    .as_primitive::<TimestampMillisecondType>()
1964                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1965                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1966                            TimestampMillisecondType,
1967                        >(x, tz)?))
1968                    })?,
1969            ))
1970        }
1971        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Microsecond)) => {
1972            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1973            Ok(Arc::new(
1974                array
1975                    .as_primitive::<TimestampMicrosecondType>()
1976                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1977                        Ok(time_to_time64us(as_time_res_with_timezone::<
1978                            TimestampMicrosecondType,
1979                        >(x, tz)?))
1980                    })?,
1981            ))
1982        }
1983        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Nanosecond)) => {
1984            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1985            Ok(Arc::new(
1986                array
1987                    .as_primitive::<TimestampMicrosecondType>()
1988                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1989                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1990                            TimestampMicrosecondType,
1991                        >(x, tz)?))
1992                    })?,
1993            ))
1994        }
1995        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Microsecond)) => {
1996            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1997            Ok(Arc::new(
1998                array
1999                    .as_primitive::<TimestampNanosecondType>()
2000                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
2001                        Ok(time_to_time64us(as_time_res_with_timezone::<
2002                            TimestampNanosecondType,
2003                        >(x, tz)?))
2004                    })?,
2005            ))
2006        }
2007        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Nanosecond)) => {
2008            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2009            Ok(Arc::new(
2010                array
2011                    .as_primitive::<TimestampNanosecondType>()
2012                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
2013                        Ok(time_to_time64ns(as_time_res_with_timezone::<
2014                            TimestampNanosecondType,
2015                        >(x, tz)?))
2016                    })?,
2017            ))
2018        }
2019        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Second)) => {
2020            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2021            Ok(Arc::new(
2022                array
2023                    .as_primitive::<TimestampSecondType>()
2024                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2025                        Ok(time_to_time32s(as_time_res_with_timezone::<
2026                            TimestampSecondType,
2027                        >(x, tz)?))
2028                    })?,
2029            ))
2030        }
2031        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Millisecond)) => {
2032            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2033            Ok(Arc::new(
2034                array
2035                    .as_primitive::<TimestampSecondType>()
2036                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2037                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2038                            TimestampSecondType,
2039                        >(x, tz)?))
2040                    })?,
2041            ))
2042        }
2043        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Second)) => {
2044            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2045            Ok(Arc::new(
2046                array
2047                    .as_primitive::<TimestampMillisecondType>()
2048                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2049                        Ok(time_to_time32s(as_time_res_with_timezone::<
2050                            TimestampMillisecondType,
2051                        >(x, tz)?))
2052                    })?,
2053            ))
2054        }
2055        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Millisecond)) => {
2056            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2057            Ok(Arc::new(
2058                array
2059                    .as_primitive::<TimestampMillisecondType>()
2060                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2061                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2062                            TimestampMillisecondType,
2063                        >(x, tz)?))
2064                    })?,
2065            ))
2066        }
2067        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Second)) => {
2068            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2069            Ok(Arc::new(
2070                array
2071                    .as_primitive::<TimestampMicrosecondType>()
2072                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2073                        Ok(time_to_time32s(as_time_res_with_timezone::<
2074                            TimestampMicrosecondType,
2075                        >(x, tz)?))
2076                    })?,
2077            ))
2078        }
2079        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Millisecond)) => {
2080            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2081            Ok(Arc::new(
2082                array
2083                    .as_primitive::<TimestampMicrosecondType>()
2084                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2085                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2086                            TimestampMicrosecondType,
2087                        >(x, tz)?))
2088                    })?,
2089            ))
2090        }
2091        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Second)) => {
2092            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2093            Ok(Arc::new(
2094                array
2095                    .as_primitive::<TimestampNanosecondType>()
2096                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2097                        Ok(time_to_time32s(as_time_res_with_timezone::<
2098                            TimestampNanosecondType,
2099                        >(x, tz)?))
2100                    })?,
2101            ))
2102        }
2103        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Millisecond)) => {
2104            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2105            Ok(Arc::new(
2106                array
2107                    .as_primitive::<TimestampNanosecondType>()
2108                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2109                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2110                            TimestampNanosecondType,
2111                        >(x, tz)?))
2112                    })?,
2113            ))
2114        }
2115        (Date64, Timestamp(TimeUnit::Second, _)) => {
2116            let array = array
2117                .as_primitive::<Date64Type>()
2118                .unary::<_, TimestampSecondType>(|x| x / MILLISECONDS);
2119
2120            cast_with_options(&array, to_type, cast_options)
2121        }
2122        (Date64, Timestamp(TimeUnit::Millisecond, _)) => {
2123            let array = array
2124                .as_primitive::<Date64Type>()
2125                .reinterpret_cast::<TimestampMillisecondType>();
2126
2127            cast_with_options(&array, to_type, cast_options)
2128        }
2129
2130        (Date64, Timestamp(TimeUnit::Microsecond, _)) => {
2131            let array = array
2132                .as_primitive::<Date64Type>()
2133                .unary::<_, TimestampMicrosecondType>(|x| x * (MICROSECONDS / MILLISECONDS));
2134
2135            cast_with_options(&array, to_type, cast_options)
2136        }
2137        (Date64, Timestamp(TimeUnit::Nanosecond, _)) => {
2138            let array = array
2139                .as_primitive::<Date64Type>()
2140                .unary::<_, TimestampNanosecondType>(|x| x * (NANOSECONDS / MILLISECONDS));
2141
2142            cast_with_options(&array, to_type, cast_options)
2143        }
2144        (Date32, Timestamp(TimeUnit::Second, _)) => {
2145            let array = array
2146                .as_primitive::<Date32Type>()
2147                .unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY);
2148
2149            cast_with_options(&array, to_type, cast_options)
2150        }
2151        (Date32, Timestamp(TimeUnit::Millisecond, _)) => {
2152            let array = array
2153                .as_primitive::<Date32Type>()
2154                .unary::<_, TimestampMillisecondType>(|x| (x as i64) * MILLISECONDS_IN_DAY);
2155
2156            cast_with_options(&array, to_type, cast_options)
2157        }
2158        (Date32, Timestamp(TimeUnit::Microsecond, _)) => {
2159            let array = array
2160                .as_primitive::<Date32Type>()
2161                .unary::<_, TimestampMicrosecondType>(|x| (x as i64) * MICROSECONDS_IN_DAY);
2162
2163            cast_with_options(&array, to_type, cast_options)
2164        }
2165        (Date32, Timestamp(TimeUnit::Nanosecond, _)) => {
2166            let array = array
2167                .as_primitive::<Date32Type>()
2168                .unary::<_, TimestampNanosecondType>(|x| (x as i64) * NANOSECONDS_IN_DAY);
2169
2170            cast_with_options(&array, to_type, cast_options)
2171        }
2172
2173        (_, Duration(unit)) if from_type.is_numeric() => {
2174            let array = cast_with_options(array, &Int64, cast_options)?;
2175            Ok(make_duration_array(array.as_primitive(), *unit))
2176        }
2177        (Duration(TimeUnit::Second), _) if to_type.is_numeric() => {
2178            let array = cast_reinterpret_arrays::<DurationSecondType, Int64Type>(array)?;
2179            cast_with_options(&array, to_type, cast_options)
2180        }
2181        (Duration(TimeUnit::Millisecond), _) if to_type.is_numeric() => {
2182            let array = cast_reinterpret_arrays::<DurationMillisecondType, Int64Type>(array)?;
2183            cast_with_options(&array, to_type, cast_options)
2184        }
2185        (Duration(TimeUnit::Microsecond), _) if to_type.is_numeric() => {
2186            let array = cast_reinterpret_arrays::<DurationMicrosecondType, Int64Type>(array)?;
2187            cast_with_options(&array, to_type, cast_options)
2188        }
2189        (Duration(TimeUnit::Nanosecond), _) if to_type.is_numeric() => {
2190            let array = cast_reinterpret_arrays::<DurationNanosecondType, Int64Type>(array)?;
2191            cast_with_options(&array, to_type, cast_options)
2192        }
2193
2194        (Duration(from_unit), Duration(to_unit)) => {
2195            let array = cast_with_options(array, &Int64, cast_options)?;
2196            let time_array = array.as_primitive::<Int64Type>();
2197            let from_size = time_unit_multiple(from_unit);
2198            let to_size = time_unit_multiple(to_unit);
2199            // we either divide or multiply, depending on size of each unit
2200            // units are never the same when the types are the same
2201            let converted = match from_size.cmp(&to_size) {
2202                Ordering::Greater => {
2203                    let divisor = from_size / to_size;
2204                    time_array.unary::<_, Int64Type>(|o| o / divisor)
2205                }
2206                Ordering::Equal => time_array.clone(),
2207                Ordering::Less => {
2208                    let mul = to_size / from_size;
2209                    if cast_options.safe {
2210                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
2211                    } else {
2212                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
2213                    }
2214                }
2215            };
2216            Ok(make_duration_array(&converted, *to_unit))
2217        }
2218
2219        (Duration(TimeUnit::Second), Interval(IntervalUnit::MonthDayNano)) => {
2220            cast_duration_to_interval::<DurationSecondType>(array, cast_options)
2221        }
2222        (Duration(TimeUnit::Millisecond), Interval(IntervalUnit::MonthDayNano)) => {
2223            cast_duration_to_interval::<DurationMillisecondType>(array, cast_options)
2224        }
2225        (Duration(TimeUnit::Microsecond), Interval(IntervalUnit::MonthDayNano)) => {
2226            cast_duration_to_interval::<DurationMicrosecondType>(array, cast_options)
2227        }
2228        (Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano)) => {
2229            cast_duration_to_interval::<DurationNanosecondType>(array, cast_options)
2230        }
2231        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Second)) => {
2232            cast_month_day_nano_to_duration::<DurationSecondType>(array, cast_options)
2233        }
2234        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Millisecond)) => {
2235            cast_month_day_nano_to_duration::<DurationMillisecondType>(array, cast_options)
2236        }
2237        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Microsecond)) => {
2238            cast_month_day_nano_to_duration::<DurationMicrosecondType>(array, cast_options)
2239        }
2240        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Nanosecond)) => {
2241            cast_month_day_nano_to_duration::<DurationNanosecondType>(array, cast_options)
2242        }
2243        (Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
2244            cast_interval_year_month_to_interval_month_day_nano(array, cast_options)
2245        }
2246        (Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
2247            cast_interval_day_time_to_interval_month_day_nano(array, cast_options)
2248        }
2249        (Int32, Interval(IntervalUnit::YearMonth)) => {
2250            cast_reinterpret_arrays::<Int32Type, IntervalYearMonthType>(array)
2251        }
2252        (_, _) => Err(ArrowError::CastError(format!(
2253            "Casting from {from_type} to {to_type} not supported",
2254        ))),
2255    }
2256}
2257
2258fn cast_from_decimal<D, F>(
2259    array: &dyn Array,
2260    base: D::Native,
2261    scale: &i8,
2262    from_type: &DataType,
2263    to_type: &DataType,
2264    as_float: F,
2265    cast_options: &CastOptions,
2266) -> Result<ArrayRef, ArrowError>
2267where
2268    D: DecimalType + ArrowPrimitiveType,
2269    <D as ArrowPrimitiveType>::Native: ArrowNativeTypeOp + ToPrimitive,
2270    F: Fn(D::Native) -> f64,
2271{
2272    use DataType::*;
2273    // cast decimal to other type
2274    match to_type {
2275        UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, base, *scale, cast_options),
2276        UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, base, *scale, cast_options),
2277        UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, base, *scale, cast_options),
2278        UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, base, *scale, cast_options),
2279        Int8 => cast_decimal_to_integer::<D, Int8Type>(array, base, *scale, cast_options),
2280        Int16 => cast_decimal_to_integer::<D, Int16Type>(array, base, *scale, cast_options),
2281        Int32 => cast_decimal_to_integer::<D, Int32Type>(array, base, *scale, cast_options),
2282        Int64 => cast_decimal_to_integer::<D, Int64Type>(array, base, *scale, cast_options),
2283        Float32 => cast_decimal_to_float::<D, Float32Type, _>(array, |x| {
2284            (as_float(x) / 10_f64.powi(*scale as i32)) as f32
2285        }),
2286        Float64 => cast_decimal_to_float::<D, Float64Type, _>(array, |x| {
2287            as_float(x) / 10_f64.powi(*scale as i32)
2288        }),
2289        Utf8View => value_to_string_view(array, cast_options),
2290        Utf8 => value_to_string::<i32>(array, cast_options),
2291        LargeUtf8 => value_to_string::<i64>(array, cast_options),
2292        Null => Ok(new_null_array(to_type, array.len())),
2293        _ => Err(ArrowError::CastError(format!(
2294            "Casting from {from_type} to {to_type} not supported"
2295        ))),
2296    }
2297}
2298
2299fn cast_to_decimal<D, M>(
2300    array: &dyn Array,
2301    base: M,
2302    precision: &u8,
2303    scale: &i8,
2304    from_type: &DataType,
2305    to_type: &DataType,
2306    cast_options: &CastOptions,
2307) -> Result<ArrayRef, ArrowError>
2308where
2309    D: DecimalType + ArrowPrimitiveType<Native = M>,
2310    M: ArrowNativeTypeOp + DecimalCast,
2311    u8: num_traits::AsPrimitive<M>,
2312    u16: num_traits::AsPrimitive<M>,
2313    u32: num_traits::AsPrimitive<M>,
2314    u64: num_traits::AsPrimitive<M>,
2315    i8: num_traits::AsPrimitive<M>,
2316    i16: num_traits::AsPrimitive<M>,
2317    i32: num_traits::AsPrimitive<M>,
2318    i64: num_traits::AsPrimitive<M>,
2319{
2320    use DataType::*;
2321    // cast data to decimal
2322    match from_type {
2323        UInt8 => cast_integer_to_decimal::<_, D, M>(
2324            array.as_primitive::<UInt8Type>(),
2325            *precision,
2326            *scale,
2327            base,
2328            cast_options,
2329        ),
2330        UInt16 => cast_integer_to_decimal::<_, D, _>(
2331            array.as_primitive::<UInt16Type>(),
2332            *precision,
2333            *scale,
2334            base,
2335            cast_options,
2336        ),
2337        UInt32 => cast_integer_to_decimal::<_, D, _>(
2338            array.as_primitive::<UInt32Type>(),
2339            *precision,
2340            *scale,
2341            base,
2342            cast_options,
2343        ),
2344        UInt64 => cast_integer_to_decimal::<_, D, _>(
2345            array.as_primitive::<UInt64Type>(),
2346            *precision,
2347            *scale,
2348            base,
2349            cast_options,
2350        ),
2351        Int8 => cast_integer_to_decimal::<_, D, _>(
2352            array.as_primitive::<Int8Type>(),
2353            *precision,
2354            *scale,
2355            base,
2356            cast_options,
2357        ),
2358        Int16 => cast_integer_to_decimal::<_, D, _>(
2359            array.as_primitive::<Int16Type>(),
2360            *precision,
2361            *scale,
2362            base,
2363            cast_options,
2364        ),
2365        Int32 => cast_integer_to_decimal::<_, D, _>(
2366            array.as_primitive::<Int32Type>(),
2367            *precision,
2368            *scale,
2369            base,
2370            cast_options,
2371        ),
2372        Int64 => cast_integer_to_decimal::<_, D, _>(
2373            array.as_primitive::<Int64Type>(),
2374            *precision,
2375            *scale,
2376            base,
2377            cast_options,
2378        ),
2379        Float32 => cast_floating_point_to_decimal::<_, D>(
2380            array.as_primitive::<Float32Type>(),
2381            *precision,
2382            *scale,
2383            cast_options,
2384        ),
2385        Float64 => cast_floating_point_to_decimal::<_, D>(
2386            array.as_primitive::<Float64Type>(),
2387            *precision,
2388            *scale,
2389            cast_options,
2390        ),
2391        Utf8View | Utf8 => {
2392            cast_string_to_decimal::<D, i32>(array, *precision, *scale, cast_options)
2393        }
2394        LargeUtf8 => cast_string_to_decimal::<D, i64>(array, *precision, *scale, cast_options),
2395        Null => Ok(new_null_array(to_type, array.len())),
2396        _ => Err(ArrowError::CastError(format!(
2397            "Casting from {from_type} to {to_type} not supported"
2398        ))),
2399    }
2400}
2401
2402/// Get the time unit as a multiple of a second
2403const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
2404    match unit {
2405        TimeUnit::Second => 1,
2406        TimeUnit::Millisecond => MILLISECONDS,
2407        TimeUnit::Microsecond => MICROSECONDS,
2408        TimeUnit::Nanosecond => NANOSECONDS,
2409    }
2410}
2411
2412/// Convert Array into a PrimitiveArray of type, and apply numeric cast
2413fn cast_numeric_arrays<FROM, TO>(
2414    from: &dyn Array,
2415    cast_options: &CastOptions,
2416) -> Result<ArrayRef, ArrowError>
2417where
2418    FROM: ArrowPrimitiveType,
2419    TO: ArrowPrimitiveType,
2420    FROM::Native: NumCast,
2421    TO::Native: NumCast,
2422{
2423    if cast_options.safe {
2424        // If the value can't be casted to the `TO::Native`, return null
2425        Ok(Arc::new(numeric_cast::<FROM, TO>(
2426            from.as_primitive::<FROM>(),
2427        )))
2428    } else {
2429        // If the value can't be casted to the `TO::Native`, return error
2430        Ok(Arc::new(try_numeric_cast::<FROM, TO>(
2431            from.as_primitive::<FROM>(),
2432        )?))
2433    }
2434}
2435
2436// Natural cast between numeric types
2437// If the value of T can't be casted to R, will throw error
2438fn try_numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>, ArrowError>
2439where
2440    T: ArrowPrimitiveType,
2441    R: ArrowPrimitiveType,
2442    T::Native: NumCast,
2443    R::Native: NumCast,
2444{
2445    from.try_unary(|value| {
2446        num_traits::cast::cast::<T::Native, R::Native>(value).ok_or_else(|| {
2447            ArrowError::CastError(format!(
2448                "Can't cast value {:?} to type {}",
2449                value,
2450                R::DATA_TYPE
2451            ))
2452        })
2453    })
2454}
2455
2456// Natural cast between numeric types
2457// If the value of T can't be casted to R, it will be converted to null
2458fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> PrimitiveArray<R>
2459where
2460    T: ArrowPrimitiveType,
2461    R: ArrowPrimitiveType,
2462    T::Native: NumCast,
2463    R::Native: NumCast,
2464{
2465    from.unary_opt::<_, R>(num_traits::cast::cast::<T::Native, R::Native>)
2466}
2467
2468fn cast_numeric_to_binary<FROM: ArrowPrimitiveType, O: OffsetSizeTrait>(
2469    array: &dyn Array,
2470) -> Result<ArrayRef, ArrowError> {
2471    let array = array.as_primitive::<FROM>();
2472    let size = std::mem::size_of::<FROM::Native>();
2473    let offsets = OffsetBuffer::from_repeated_length(size, array.len());
2474    Ok(Arc::new(GenericBinaryArray::<O>::try_new(
2475        offsets,
2476        array.values().inner().clone(),
2477        array.nulls().cloned(),
2478    )?))
2479}
2480
2481fn adjust_timestamp_to_timezone<T: ArrowTimestampType>(
2482    array: PrimitiveArray<Int64Type>,
2483    to_tz: &Tz,
2484    cast_options: &CastOptions,
2485) -> Result<PrimitiveArray<Int64Type>, ArrowError> {
2486    let adjust = |o| {
2487        let local = as_datetime::<T>(o)?;
2488        let offset = to_tz.offset_from_local_datetime(&local).single()?;
2489        T::make_value(local - offset.fix())
2490    };
2491    let adjusted = if cast_options.safe {
2492        array.unary_opt::<_, Int64Type>(adjust)
2493    } else {
2494        array.try_unary::<_, Int64Type, _>(|o| {
2495            adjust(o).ok_or_else(|| {
2496                ArrowError::CastError("Cannot cast timezone to different timezone".to_string())
2497            })
2498        })?
2499    };
2500    Ok(adjusted)
2501}
2502
2503/// Cast numeric types to Boolean
2504///
2505/// Any zero value returns `false` while non-zero returns `true`
2506fn cast_numeric_to_bool<FROM>(from: &dyn Array) -> Result<ArrayRef, ArrowError>
2507where
2508    FROM: ArrowPrimitiveType,
2509{
2510    numeric_to_bool_cast::<FROM>(from.as_primitive::<FROM>()).map(|to| Arc::new(to) as ArrayRef)
2511}
2512
2513fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray, ArrowError>
2514where
2515    T: ArrowPrimitiveType + ArrowPrimitiveType,
2516{
2517    let mut b = BooleanBuilder::with_capacity(from.len());
2518
2519    for i in 0..from.len() {
2520        if from.is_null(i) {
2521            b.append_null();
2522        } else if from.value(i) != T::default_value() {
2523            b.append_value(true);
2524        } else {
2525            b.append_value(false);
2526        }
2527    }
2528
2529    Ok(b.finish())
2530}
2531
2532/// Cast Boolean types to numeric
2533///
2534/// `false` returns 0 while `true` returns 1
2535fn cast_bool_to_numeric<TO>(
2536    from: &dyn Array,
2537    cast_options: &CastOptions,
2538) -> Result<ArrayRef, ArrowError>
2539where
2540    TO: ArrowPrimitiveType,
2541    TO::Native: num_traits::cast::NumCast,
2542{
2543    Ok(Arc::new(bool_to_numeric_cast::<TO>(
2544        from.as_any().downcast_ref::<BooleanArray>().unwrap(),
2545        cast_options,
2546    )))
2547}
2548
2549fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions) -> PrimitiveArray<T>
2550where
2551    T: ArrowPrimitiveType,
2552    T::Native: num_traits::NumCast,
2553{
2554    let iter = (0..from.len()).map(|i| {
2555        if from.is_null(i) {
2556            None
2557        } else if from.value(i) {
2558            // a workaround to cast a primitive to T::Native, infallible
2559            num_traits::cast::cast(1)
2560        } else {
2561            Some(T::default_value())
2562        }
2563    });
2564    // Benefit:
2565    //     20% performance improvement
2566    // Soundness:
2567    //     The iterator is trustedLen because it comes from a Range
2568    unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
2569}
2570
2571/// Helper function to cast from one `BinaryArray` or 'LargeBinaryArray' to 'FixedSizeBinaryArray'.
2572fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
2573    array: &dyn Array,
2574    byte_width: i32,
2575    cast_options: &CastOptions,
2576) -> Result<ArrayRef, ArrowError> {
2577    let array = array.as_binary::<O>();
2578    let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(), byte_width);
2579
2580    for i in 0..array.len() {
2581        if array.is_null(i) {
2582            builder.append_null();
2583        } else {
2584            match builder.append_value(array.value(i)) {
2585                Ok(_) => {}
2586                Err(e) => match cast_options.safe {
2587                    true => builder.append_null(),
2588                    false => return Err(e),
2589                },
2590            }
2591        }
2592    }
2593
2594    Ok(Arc::new(builder.finish()))
2595}
2596
2597/// Helper function to cast from 'FixedSizeBinaryArray' to one `BinaryArray` or 'LargeBinaryArray'.
2598/// If the target one is too large for the source array it will return an Error.
2599fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
2600    array: &dyn Array,
2601    byte_width: i32,
2602) -> Result<ArrayRef, ArrowError> {
2603    let array = array
2604        .as_any()
2605        .downcast_ref::<FixedSizeBinaryArray>()
2606        .unwrap();
2607
2608    let offsets: i128 = byte_width as i128 * array.len() as i128;
2609
2610    let is_binary = matches!(GenericBinaryType::<O>::DATA_TYPE, DataType::Binary);
2611    if is_binary && offsets > i32::MAX as i128 {
2612        return Err(ArrowError::ComputeError(
2613            "FixedSizeBinary array too large to cast to Binary array".to_string(),
2614        ));
2615    } else if !is_binary && offsets > i64::MAX as i128 {
2616        return Err(ArrowError::ComputeError(
2617            "FixedSizeBinary array too large to cast to LargeBinary array".to_string(),
2618        ));
2619    }
2620
2621    let mut builder = GenericBinaryBuilder::<O>::with_capacity(array.len(), array.len());
2622
2623    for i in 0..array.len() {
2624        if array.is_null(i) {
2625            builder.append_null();
2626        } else {
2627            builder.append_value(array.value(i));
2628        }
2629    }
2630
2631    Ok(Arc::new(builder.finish()))
2632}
2633
2634fn cast_fixed_size_binary_to_binary_view(
2635    array: &dyn Array,
2636    _byte_width: i32,
2637) -> Result<ArrayRef, ArrowError> {
2638    let array = array
2639        .as_any()
2640        .downcast_ref::<FixedSizeBinaryArray>()
2641        .unwrap();
2642
2643    let mut builder = BinaryViewBuilder::with_capacity(array.len());
2644    for i in 0..array.len() {
2645        if array.is_null(i) {
2646            builder.append_null();
2647        } else {
2648            builder.append_value(array.value(i));
2649        }
2650    }
2651
2652    Ok(Arc::new(builder.finish()))
2653}
2654
2655/// Helper function to cast from one `ByteArrayType` to another and vice versa.
2656/// If the target one (e.g., `LargeUtf8`) is too large for the source array it will return an Error.
2657fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2658where
2659    FROM: ByteArrayType,
2660    TO: ByteArrayType<Native = FROM::Native>,
2661    FROM::Offset: OffsetSizeTrait + ToPrimitive,
2662    TO::Offset: OffsetSizeTrait + NumCast,
2663{
2664    let data = array.to_data();
2665    assert_eq!(data.data_type(), &FROM::DATA_TYPE);
2666    let str_values_buf = data.buffers()[1].clone();
2667    let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
2668
2669    let mut offset_builder = BufferBuilder::<TO::Offset>::new(offsets.len());
2670    offsets
2671        .iter()
2672        .try_for_each::<_, Result<_, ArrowError>>(|offset| {
2673            let offset =
2674                <<TO as ByteArrayType>::Offset as NumCast>::from(*offset).ok_or_else(|| {
2675                    ArrowError::ComputeError(format!(
2676                        "{}{} array too large to cast to {}{} array",
2677                        FROM::Offset::PREFIX,
2678                        FROM::PREFIX,
2679                        TO::Offset::PREFIX,
2680                        TO::PREFIX
2681                    ))
2682                })?;
2683            offset_builder.append(offset);
2684            Ok(())
2685        })?;
2686
2687    let offset_buffer = offset_builder.finish();
2688
2689    let dtype = TO::DATA_TYPE;
2690
2691    let builder = ArrayData::builder(dtype)
2692        .offset(array.offset())
2693        .len(array.len())
2694        .add_buffer(offset_buffer)
2695        .add_buffer(str_values_buf)
2696        .nulls(data.nulls().cloned());
2697
2698    let array_data = unsafe { builder.build_unchecked() };
2699
2700    Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
2701}
2702
2703/// Helper function to cast from one `ByteViewType` array to `ByteArrayType` array.
2704fn cast_view_to_byte<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2705where
2706    FROM: ByteViewType,
2707    TO: ByteArrayType,
2708    FROM::Native: AsRef<TO::Native>,
2709{
2710    let data = array.to_data();
2711    let view_array = GenericByteViewArray::<FROM>::from(data);
2712
2713    let len = view_array.len();
2714    let bytes = view_array
2715        .views()
2716        .iter()
2717        .map(|v| ByteView::from(*v).length as usize)
2718        .sum::<usize>();
2719
2720    let mut byte_array_builder = GenericByteBuilder::<TO>::with_capacity(len, bytes);
2721
2722    for val in view_array.iter() {
2723        byte_array_builder.append_option(val);
2724    }
2725
2726    Ok(Arc::new(byte_array_builder.finish()))
2727}
2728
2729#[cfg(test)]
2730mod tests {
2731    use super::*;
2732    use DataType::*;
2733    use arrow_array::{Int64Array, RunArray, StringArray};
2734    use arrow_buffer::i256;
2735    use arrow_buffer::{Buffer, IntervalDayTime, NullBuffer};
2736    use arrow_schema::{DataType, Field};
2737    use chrono::NaiveDate;
2738    use half::f16;
2739    use std::sync::Arc;
2740
2741    #[derive(Clone)]
2742    struct DecimalCastTestConfig {
2743        input_prec: u8,
2744        input_scale: i8,
2745        input_repr: i128,
2746        output_prec: u8,
2747        output_scale: i8,
2748        expected_output_repr: Result<i128, String>, // the error variant can contain a string
2749                                                    // template where the "{}" will be
2750                                                    // replaced with the decimal type name
2751                                                    // (e.g. Decimal128)
2752    }
2753
2754    macro_rules! generate_cast_test_case {
2755        ($INPUT_ARRAY: expr, $OUTPUT_TYPE_ARRAY: ident, $OUTPUT_TYPE: expr, $OUTPUT_VALUES: expr) => {
2756            let output =
2757                $OUTPUT_TYPE_ARRAY::from($OUTPUT_VALUES).with_data_type($OUTPUT_TYPE.clone());
2758
2759            // assert cast type
2760            let input_array_type = $INPUT_ARRAY.data_type();
2761            assert!(can_cast_types(input_array_type, $OUTPUT_TYPE));
2762            let result = cast($INPUT_ARRAY, $OUTPUT_TYPE).unwrap();
2763            assert_eq!($OUTPUT_TYPE, result.data_type());
2764            assert_eq!(result.as_ref(), &output);
2765
2766            let cast_option = CastOptions {
2767                safe: false,
2768                format_options: FormatOptions::default(),
2769            };
2770            let result = cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap();
2771            assert_eq!($OUTPUT_TYPE, result.data_type());
2772            assert_eq!(result.as_ref(), &output);
2773        };
2774    }
2775
2776    fn run_decimal_cast_test_case<I, O>(t: DecimalCastTestConfig)
2777    where
2778        I: DecimalType,
2779        O: DecimalType,
2780        I::Native: DecimalCast,
2781        O::Native: DecimalCast,
2782    {
2783        let array = vec![I::Native::from_decimal(t.input_repr)];
2784        let array = array
2785            .into_iter()
2786            .collect::<PrimitiveArray<I>>()
2787            .with_precision_and_scale(t.input_prec, t.input_scale)
2788            .unwrap();
2789        let input_type = array.data_type();
2790        let output_type = O::TYPE_CONSTRUCTOR(t.output_prec, t.output_scale);
2791        assert!(can_cast_types(input_type, &output_type));
2792
2793        let options = CastOptions {
2794            safe: false,
2795            ..Default::default()
2796        };
2797        let result = cast_with_options(&array, &output_type, &options);
2798
2799        match t.expected_output_repr {
2800            Ok(v) => {
2801                let expected_array = vec![O::Native::from_decimal(v)];
2802                let expected_array = expected_array
2803                    .into_iter()
2804                    .collect::<PrimitiveArray<O>>()
2805                    .with_precision_and_scale(t.output_prec, t.output_scale)
2806                    .unwrap();
2807                assert_eq!(*result.unwrap(), expected_array);
2808            }
2809            Err(expected_output_message_template) => {
2810                assert!(result.is_err());
2811                let expected_error_message =
2812                    expected_output_message_template.replace("{}", O::PREFIX);
2813                assert_eq!(result.unwrap_err().to_string(), expected_error_message);
2814            }
2815        }
2816    }
2817
2818    fn create_decimal32_array(
2819        array: Vec<Option<i32>>,
2820        precision: u8,
2821        scale: i8,
2822    ) -> Result<Decimal32Array, ArrowError> {
2823        array
2824            .into_iter()
2825            .collect::<Decimal32Array>()
2826            .with_precision_and_scale(precision, scale)
2827    }
2828
2829    fn create_decimal64_array(
2830        array: Vec<Option<i64>>,
2831        precision: u8,
2832        scale: i8,
2833    ) -> Result<Decimal64Array, ArrowError> {
2834        array
2835            .into_iter()
2836            .collect::<Decimal64Array>()
2837            .with_precision_and_scale(precision, scale)
2838    }
2839
2840    fn create_decimal128_array(
2841        array: Vec<Option<i128>>,
2842        precision: u8,
2843        scale: i8,
2844    ) -> Result<Decimal128Array, ArrowError> {
2845        array
2846            .into_iter()
2847            .collect::<Decimal128Array>()
2848            .with_precision_and_scale(precision, scale)
2849    }
2850
2851    fn create_decimal256_array(
2852        array: Vec<Option<i256>>,
2853        precision: u8,
2854        scale: i8,
2855    ) -> Result<Decimal256Array, ArrowError> {
2856        array
2857            .into_iter()
2858            .collect::<Decimal256Array>()
2859            .with_precision_and_scale(precision, scale)
2860    }
2861
2862    #[test]
2863    #[cfg(not(feature = "force_validate"))]
2864    #[should_panic(
2865        expected = "Cannot cast to Decimal128(20, 3). Overflowing on 57896044618658097711785492504343953926634992332820282019728792003956564819967"
2866    )]
2867    fn test_cast_decimal_to_decimal_round_with_error() {
2868        // decimal256 to decimal128 overflow
2869        let array = vec![
2870            Some(i256::from_i128(1123454)),
2871            Some(i256::from_i128(2123456)),
2872            Some(i256::from_i128(-3123453)),
2873            Some(i256::from_i128(-3123456)),
2874            None,
2875            Some(i256::MAX),
2876            Some(i256::MIN),
2877        ];
2878        let input_decimal_array = create_decimal256_array(array, 76, 4).unwrap();
2879        let array = Arc::new(input_decimal_array) as ArrayRef;
2880        let input_type = DataType::Decimal256(76, 4);
2881        let output_type = DataType::Decimal128(20, 3);
2882        assert!(can_cast_types(&input_type, &output_type));
2883        generate_cast_test_case!(
2884            &array,
2885            Decimal128Array,
2886            &output_type,
2887            vec![
2888                Some(112345_i128),
2889                Some(212346_i128),
2890                Some(-312345_i128),
2891                Some(-312346_i128),
2892                None,
2893                None,
2894                None,
2895            ]
2896        );
2897    }
2898
2899    #[test]
2900    #[cfg(not(feature = "force_validate"))]
2901    fn test_cast_decimal_to_decimal_round() {
2902        let array = vec![
2903            Some(1123454),
2904            Some(2123456),
2905            Some(-3123453),
2906            Some(-3123456),
2907            None,
2908        ];
2909        let array = create_decimal128_array(array, 20, 4).unwrap();
2910        // decimal128 to decimal128
2911        let input_type = DataType::Decimal128(20, 4);
2912        let output_type = DataType::Decimal128(20, 3);
2913        assert!(can_cast_types(&input_type, &output_type));
2914        generate_cast_test_case!(
2915            &array,
2916            Decimal128Array,
2917            &output_type,
2918            vec![
2919                Some(112345_i128),
2920                Some(212346_i128),
2921                Some(-312345_i128),
2922                Some(-312346_i128),
2923                None
2924            ]
2925        );
2926
2927        // decimal128 to decimal256
2928        let input_type = DataType::Decimal128(20, 4);
2929        let output_type = DataType::Decimal256(20, 3);
2930        assert!(can_cast_types(&input_type, &output_type));
2931        generate_cast_test_case!(
2932            &array,
2933            Decimal256Array,
2934            &output_type,
2935            vec![
2936                Some(i256::from_i128(112345_i128)),
2937                Some(i256::from_i128(212346_i128)),
2938                Some(i256::from_i128(-312345_i128)),
2939                Some(i256::from_i128(-312346_i128)),
2940                None
2941            ]
2942        );
2943
2944        // decimal256
2945        let array = vec![
2946            Some(i256::from_i128(1123454)),
2947            Some(i256::from_i128(2123456)),
2948            Some(i256::from_i128(-3123453)),
2949            Some(i256::from_i128(-3123456)),
2950            None,
2951        ];
2952        let array = create_decimal256_array(array, 20, 4).unwrap();
2953
2954        // decimal256 to decimal256
2955        let input_type = DataType::Decimal256(20, 4);
2956        let output_type = DataType::Decimal256(20, 3);
2957        assert!(can_cast_types(&input_type, &output_type));
2958        generate_cast_test_case!(
2959            &array,
2960            Decimal256Array,
2961            &output_type,
2962            vec![
2963                Some(i256::from_i128(112345_i128)),
2964                Some(i256::from_i128(212346_i128)),
2965                Some(i256::from_i128(-312345_i128)),
2966                Some(i256::from_i128(-312346_i128)),
2967                None
2968            ]
2969        );
2970        // decimal256 to decimal128
2971        let input_type = DataType::Decimal256(20, 4);
2972        let output_type = DataType::Decimal128(20, 3);
2973        assert!(can_cast_types(&input_type, &output_type));
2974        generate_cast_test_case!(
2975            &array,
2976            Decimal128Array,
2977            &output_type,
2978            vec![
2979                Some(112345_i128),
2980                Some(212346_i128),
2981                Some(-312345_i128),
2982                Some(-312346_i128),
2983                None
2984            ]
2985        );
2986    }
2987
2988    #[test]
2989    fn test_cast_decimal32_to_decimal32() {
2990        // test changing precision
2991        let input_type = DataType::Decimal32(9, 3);
2992        let output_type = DataType::Decimal32(9, 4);
2993        assert!(can_cast_types(&input_type, &output_type));
2994        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2995        let array = create_decimal32_array(array, 9, 3).unwrap();
2996        generate_cast_test_case!(
2997            &array,
2998            Decimal32Array,
2999            &output_type,
3000            vec![
3001                Some(11234560_i32),
3002                Some(21234560_i32),
3003                Some(31234560_i32),
3004                None
3005            ]
3006        );
3007        // negative test
3008        let array = vec![Some(123456), None];
3009        let array = create_decimal32_array(array, 9, 0).unwrap();
3010        let result_safe = cast(&array, &DataType::Decimal32(2, 2));
3011        assert!(result_safe.is_ok());
3012        let options = CastOptions {
3013            safe: false,
3014            ..Default::default()
3015        };
3016
3017        let result_unsafe = cast_with_options(&array, &DataType::Decimal32(2, 2), &options);
3018        assert_eq!(
3019            "Invalid argument error: 123456.00 is too large to store in a Decimal32 of precision 2. Max is 0.99",
3020            result_unsafe.unwrap_err().to_string()
3021        );
3022    }
3023
3024    #[test]
3025    fn test_cast_decimal64_to_decimal64() {
3026        // test changing precision
3027        let input_type = DataType::Decimal64(17, 3);
3028        let output_type = DataType::Decimal64(17, 4);
3029        assert!(can_cast_types(&input_type, &output_type));
3030        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3031        let array = create_decimal64_array(array, 17, 3).unwrap();
3032        generate_cast_test_case!(
3033            &array,
3034            Decimal64Array,
3035            &output_type,
3036            vec![
3037                Some(11234560_i64),
3038                Some(21234560_i64),
3039                Some(31234560_i64),
3040                None
3041            ]
3042        );
3043        // negative test
3044        let array = vec![Some(123456), None];
3045        let array = create_decimal64_array(array, 9, 0).unwrap();
3046        let result_safe = cast(&array, &DataType::Decimal64(2, 2));
3047        assert!(result_safe.is_ok());
3048        let options = CastOptions {
3049            safe: false,
3050            ..Default::default()
3051        };
3052
3053        let result_unsafe = cast_with_options(&array, &DataType::Decimal64(2, 2), &options);
3054        assert_eq!(
3055            "Invalid argument error: 123456.00 is too large to store in a Decimal64 of precision 2. Max is 0.99",
3056            result_unsafe.unwrap_err().to_string()
3057        );
3058    }
3059
3060    #[test]
3061    fn test_cast_decimal128_to_decimal128() {
3062        // test changing precision
3063        let input_type = DataType::Decimal128(20, 3);
3064        let output_type = DataType::Decimal128(20, 4);
3065        assert!(can_cast_types(&input_type, &output_type));
3066        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3067        let array = create_decimal128_array(array, 20, 3).unwrap();
3068        generate_cast_test_case!(
3069            &array,
3070            Decimal128Array,
3071            &output_type,
3072            vec![
3073                Some(11234560_i128),
3074                Some(21234560_i128),
3075                Some(31234560_i128),
3076                None
3077            ]
3078        );
3079        // negative test
3080        let array = vec![Some(123456), None];
3081        let array = create_decimal128_array(array, 10, 0).unwrap();
3082        let result_safe = cast(&array, &DataType::Decimal128(2, 2));
3083        assert!(result_safe.is_ok());
3084        let options = CastOptions {
3085            safe: false,
3086            ..Default::default()
3087        };
3088
3089        let result_unsafe = cast_with_options(&array, &DataType::Decimal128(2, 2), &options);
3090        assert_eq!(
3091            "Invalid argument error: 123456.00 is too large to store in a Decimal128 of precision 2. Max is 0.99",
3092            result_unsafe.unwrap_err().to_string()
3093        );
3094    }
3095
3096    #[test]
3097    fn test_cast_decimal32_to_decimal32_dict() {
3098        let p = 9;
3099        let s = 3;
3100        let input_type = DataType::Decimal32(p, s);
3101        let output_type = DataType::Dictionary(
3102            Box::new(DataType::Int32),
3103            Box::new(DataType::Decimal32(p, s)),
3104        );
3105        assert!(can_cast_types(&input_type, &output_type));
3106        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3107        let array = create_decimal32_array(array, p, s).unwrap();
3108        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3109        assert_eq!(cast_array.data_type(), &output_type);
3110    }
3111
3112    #[test]
3113    fn test_cast_decimal64_to_decimal64_dict() {
3114        let p = 15;
3115        let s = 3;
3116        let input_type = DataType::Decimal64(p, s);
3117        let output_type = DataType::Dictionary(
3118            Box::new(DataType::Int32),
3119            Box::new(DataType::Decimal64(p, s)),
3120        );
3121        assert!(can_cast_types(&input_type, &output_type));
3122        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3123        let array = create_decimal64_array(array, p, s).unwrap();
3124        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3125        assert_eq!(cast_array.data_type(), &output_type);
3126    }
3127
3128    #[test]
3129    fn test_cast_decimal128_to_decimal128_dict() {
3130        let p = 20;
3131        let s = 3;
3132        let input_type = DataType::Decimal128(p, s);
3133        let output_type = DataType::Dictionary(
3134            Box::new(DataType::Int32),
3135            Box::new(DataType::Decimal128(p, s)),
3136        );
3137        assert!(can_cast_types(&input_type, &output_type));
3138        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3139        let array = create_decimal128_array(array, p, s).unwrap();
3140        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3141        assert_eq!(cast_array.data_type(), &output_type);
3142    }
3143
3144    #[test]
3145    fn test_cast_decimal256_to_decimal256_dict() {
3146        let p = 20;
3147        let s = 3;
3148        let input_type = DataType::Decimal256(p, s);
3149        let output_type = DataType::Dictionary(
3150            Box::new(DataType::Int32),
3151            Box::new(DataType::Decimal256(p, s)),
3152        );
3153        assert!(can_cast_types(&input_type, &output_type));
3154        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3155        let array = create_decimal128_array(array, p, s).unwrap();
3156        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3157        assert_eq!(cast_array.data_type(), &output_type);
3158    }
3159
3160    #[test]
3161    fn test_cast_decimal32_to_decimal32_overflow() {
3162        let input_type = DataType::Decimal32(9, 3);
3163        let output_type = DataType::Decimal32(9, 9);
3164        assert!(can_cast_types(&input_type, &output_type));
3165
3166        let array = vec![Some(i32::MAX)];
3167        let array = create_decimal32_array(array, 9, 3).unwrap();
3168        let result = cast_with_options(
3169            &array,
3170            &output_type,
3171            &CastOptions {
3172                safe: false,
3173                format_options: FormatOptions::default(),
3174            },
3175        );
3176        assert_eq!(
3177            "Cast error: Cannot cast to Decimal32(9, 9). Overflowing on 2147483647",
3178            result.unwrap_err().to_string()
3179        );
3180    }
3181
3182    #[test]
3183    fn test_cast_decimal32_to_decimal32_large_scale_reduction() {
3184        let array = vec![Some(-999999999), Some(0), Some(999999999), None];
3185        let array = create_decimal32_array(array, 9, 3).unwrap();
3186
3187        // Divide out all digits of precision -- rounding could still produce +/- 1
3188        let output_type = DataType::Decimal32(9, -6);
3189        assert!(can_cast_types(array.data_type(), &output_type));
3190        generate_cast_test_case!(
3191            &array,
3192            Decimal32Array,
3193            &output_type,
3194            vec![Some(-1), Some(0), Some(1), None]
3195        );
3196
3197        // Divide out more digits than we have precision -- all-zero result
3198        let output_type = DataType::Decimal32(9, -7);
3199        assert!(can_cast_types(array.data_type(), &output_type));
3200        generate_cast_test_case!(
3201            &array,
3202            Decimal32Array,
3203            &output_type,
3204            vec![Some(0), Some(0), Some(0), None]
3205        );
3206    }
3207
3208    #[test]
3209    fn test_cast_decimal64_to_decimal64_overflow() {
3210        let input_type = DataType::Decimal64(18, 3);
3211        let output_type = DataType::Decimal64(18, 18);
3212        assert!(can_cast_types(&input_type, &output_type));
3213
3214        let array = vec![Some(i64::MAX)];
3215        let array = create_decimal64_array(array, 18, 3).unwrap();
3216        let result = cast_with_options(
3217            &array,
3218            &output_type,
3219            &CastOptions {
3220                safe: false,
3221                format_options: FormatOptions::default(),
3222            },
3223        );
3224        assert_eq!(
3225            "Cast error: Cannot cast to Decimal64(18, 18). Overflowing on 9223372036854775807",
3226            result.unwrap_err().to_string()
3227        );
3228    }
3229
3230    #[test]
3231    fn test_cast_decimal64_to_decimal64_large_scale_reduction() {
3232        let array = vec![
3233            Some(-999999999999999999),
3234            Some(0),
3235            Some(999999999999999999),
3236            None,
3237        ];
3238        let array = create_decimal64_array(array, 18, 3).unwrap();
3239
3240        // Divide out all digits of precision -- rounding could still produce +/- 1
3241        let output_type = DataType::Decimal64(18, -15);
3242        assert!(can_cast_types(array.data_type(), &output_type));
3243        generate_cast_test_case!(
3244            &array,
3245            Decimal64Array,
3246            &output_type,
3247            vec![Some(-1), Some(0), Some(1), None]
3248        );
3249
3250        // Divide out more digits than we have precision -- all-zero result
3251        let output_type = DataType::Decimal64(18, -16);
3252        assert!(can_cast_types(array.data_type(), &output_type));
3253        generate_cast_test_case!(
3254            &array,
3255            Decimal64Array,
3256            &output_type,
3257            vec![Some(0), Some(0), Some(0), None]
3258        );
3259    }
3260
3261    #[test]
3262    fn test_cast_floating_to_decimals() {
3263        for output_type in [
3264            DataType::Decimal32(9, 3),
3265            DataType::Decimal64(9, 3),
3266            DataType::Decimal128(9, 3),
3267            DataType::Decimal256(9, 3),
3268        ] {
3269            let input_type = DataType::Float64;
3270            assert!(can_cast_types(&input_type, &output_type));
3271
3272            let array = vec![Some(1.1_f64)];
3273            let array = PrimitiveArray::<Float64Type>::from_iter(array);
3274            let result = cast_with_options(
3275                &array,
3276                &output_type,
3277                &CastOptions {
3278                    safe: false,
3279                    format_options: FormatOptions::default(),
3280                },
3281            );
3282            assert!(
3283                result.is_ok(),
3284                "Failed to cast to {output_type} with: {}",
3285                result.unwrap_err()
3286            );
3287        }
3288    }
3289
3290    #[test]
3291    fn test_cast_decimal128_to_decimal128_overflow() {
3292        let input_type = DataType::Decimal128(38, 3);
3293        let output_type = DataType::Decimal128(38, 38);
3294        assert!(can_cast_types(&input_type, &output_type));
3295
3296        let array = vec![Some(i128::MAX)];
3297        let array = create_decimal128_array(array, 38, 3).unwrap();
3298        let result = cast_with_options(
3299            &array,
3300            &output_type,
3301            &CastOptions {
3302                safe: false,
3303                format_options: FormatOptions::default(),
3304            },
3305        );
3306        assert_eq!(
3307            "Cast error: Cannot cast to Decimal128(38, 38). Overflowing on 170141183460469231731687303715884105727",
3308            result.unwrap_err().to_string()
3309        );
3310    }
3311
3312    #[test]
3313    fn test_cast_decimal128_to_decimal256_overflow() {
3314        let input_type = DataType::Decimal128(38, 3);
3315        let output_type = DataType::Decimal256(76, 76);
3316        assert!(can_cast_types(&input_type, &output_type));
3317
3318        let array = vec![Some(i128::MAX)];
3319        let array = create_decimal128_array(array, 38, 3).unwrap();
3320        let result = cast_with_options(
3321            &array,
3322            &output_type,
3323            &CastOptions {
3324                safe: false,
3325                format_options: FormatOptions::default(),
3326            },
3327        );
3328        assert_eq!(
3329            "Cast error: Cannot cast to Decimal256(76, 76). Overflowing on 170141183460469231731687303715884105727",
3330            result.unwrap_err().to_string()
3331        );
3332    }
3333
3334    #[test]
3335    fn test_cast_decimal32_to_decimal256() {
3336        let input_type = DataType::Decimal32(8, 3);
3337        let output_type = DataType::Decimal256(20, 4);
3338        assert!(can_cast_types(&input_type, &output_type));
3339        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3340        let array = create_decimal32_array(array, 8, 3).unwrap();
3341        generate_cast_test_case!(
3342            &array,
3343            Decimal256Array,
3344            &output_type,
3345            vec![
3346                Some(i256::from_i128(11234560_i128)),
3347                Some(i256::from_i128(21234560_i128)),
3348                Some(i256::from_i128(31234560_i128)),
3349                None
3350            ]
3351        );
3352    }
3353    #[test]
3354    fn test_cast_decimal64_to_decimal256() {
3355        let input_type = DataType::Decimal64(12, 3);
3356        let output_type = DataType::Decimal256(20, 4);
3357        assert!(can_cast_types(&input_type, &output_type));
3358        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3359        let array = create_decimal64_array(array, 12, 3).unwrap();
3360        generate_cast_test_case!(
3361            &array,
3362            Decimal256Array,
3363            &output_type,
3364            vec![
3365                Some(i256::from_i128(11234560_i128)),
3366                Some(i256::from_i128(21234560_i128)),
3367                Some(i256::from_i128(31234560_i128)),
3368                None
3369            ]
3370        );
3371    }
3372    #[test]
3373    fn test_cast_decimal128_to_decimal256() {
3374        let input_type = DataType::Decimal128(20, 3);
3375        let output_type = DataType::Decimal256(20, 4);
3376        assert!(can_cast_types(&input_type, &output_type));
3377        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3378        let array = create_decimal128_array(array, 20, 3).unwrap();
3379        generate_cast_test_case!(
3380            &array,
3381            Decimal256Array,
3382            &output_type,
3383            vec![
3384                Some(i256::from_i128(11234560_i128)),
3385                Some(i256::from_i128(21234560_i128)),
3386                Some(i256::from_i128(31234560_i128)),
3387                None
3388            ]
3389        );
3390    }
3391
3392    #[test]
3393    fn test_cast_decimal256_to_decimal128_overflow() {
3394        let input_type = DataType::Decimal256(76, 5);
3395        let output_type = DataType::Decimal128(38, 7);
3396        assert!(can_cast_types(&input_type, &output_type));
3397        let array = vec![Some(i256::from_i128(i128::MAX))];
3398        let array = create_decimal256_array(array, 76, 5).unwrap();
3399        let result = cast_with_options(
3400            &array,
3401            &output_type,
3402            &CastOptions {
3403                safe: false,
3404                format_options: FormatOptions::default(),
3405            },
3406        );
3407        assert_eq!(
3408            "Cast error: Cannot cast to Decimal128(38, 7). Overflowing on 170141183460469231731687303715884105727",
3409            result.unwrap_err().to_string()
3410        );
3411    }
3412
3413    #[test]
3414    fn test_cast_decimal256_to_decimal256_overflow() {
3415        let input_type = DataType::Decimal256(76, 5);
3416        let output_type = DataType::Decimal256(76, 55);
3417        assert!(can_cast_types(&input_type, &output_type));
3418        let array = vec![Some(i256::from_i128(i128::MAX))];
3419        let array = create_decimal256_array(array, 76, 5).unwrap();
3420        let result = cast_with_options(
3421            &array,
3422            &output_type,
3423            &CastOptions {
3424                safe: false,
3425                format_options: FormatOptions::default(),
3426            },
3427        );
3428        assert_eq!(
3429            "Cast error: Cannot cast to Decimal256(76, 55). Overflowing on 170141183460469231731687303715884105727",
3430            result.unwrap_err().to_string()
3431        );
3432    }
3433
3434    #[test]
3435    fn test_cast_decimal256_to_decimal128() {
3436        let input_type = DataType::Decimal256(20, 3);
3437        let output_type = DataType::Decimal128(20, 4);
3438        assert!(can_cast_types(&input_type, &output_type));
3439        let array = vec![
3440            Some(i256::from_i128(1123456)),
3441            Some(i256::from_i128(2123456)),
3442            Some(i256::from_i128(3123456)),
3443            None,
3444        ];
3445        let array = create_decimal256_array(array, 20, 3).unwrap();
3446        generate_cast_test_case!(
3447            &array,
3448            Decimal128Array,
3449            &output_type,
3450            vec![
3451                Some(11234560_i128),
3452                Some(21234560_i128),
3453                Some(31234560_i128),
3454                None
3455            ]
3456        );
3457    }
3458
3459    #[test]
3460    fn test_cast_decimal256_to_decimal256() {
3461        let input_type = DataType::Decimal256(20, 3);
3462        let output_type = DataType::Decimal256(20, 4);
3463        assert!(can_cast_types(&input_type, &output_type));
3464        let array = vec![
3465            Some(i256::from_i128(1123456)),
3466            Some(i256::from_i128(2123456)),
3467            Some(i256::from_i128(3123456)),
3468            None,
3469        ];
3470        let array = create_decimal256_array(array, 20, 3).unwrap();
3471        generate_cast_test_case!(
3472            &array,
3473            Decimal256Array,
3474            &output_type,
3475            vec![
3476                Some(i256::from_i128(11234560_i128)),
3477                Some(i256::from_i128(21234560_i128)),
3478                Some(i256::from_i128(31234560_i128)),
3479                None
3480            ]
3481        );
3482    }
3483
3484    fn generate_decimal_to_numeric_cast_test_case<T>(array: &PrimitiveArray<T>)
3485    where
3486        T: ArrowPrimitiveType + DecimalType,
3487    {
3488        // u8
3489        generate_cast_test_case!(
3490            array,
3491            UInt8Array,
3492            &DataType::UInt8,
3493            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3494        );
3495        // u16
3496        generate_cast_test_case!(
3497            array,
3498            UInt16Array,
3499            &DataType::UInt16,
3500            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3501        );
3502        // u32
3503        generate_cast_test_case!(
3504            array,
3505            UInt32Array,
3506            &DataType::UInt32,
3507            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3508        );
3509        // u64
3510        generate_cast_test_case!(
3511            array,
3512            UInt64Array,
3513            &DataType::UInt64,
3514            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3515        );
3516        // i8
3517        generate_cast_test_case!(
3518            array,
3519            Int8Array,
3520            &DataType::Int8,
3521            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3522        );
3523        // i16
3524        generate_cast_test_case!(
3525            array,
3526            Int16Array,
3527            &DataType::Int16,
3528            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3529        );
3530        // i32
3531        generate_cast_test_case!(
3532            array,
3533            Int32Array,
3534            &DataType::Int32,
3535            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3536        );
3537        // i64
3538        generate_cast_test_case!(
3539            array,
3540            Int64Array,
3541            &DataType::Int64,
3542            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3543        );
3544        // f32
3545        generate_cast_test_case!(
3546            array,
3547            Float32Array,
3548            &DataType::Float32,
3549            vec![
3550                Some(1.25_f32),
3551                Some(2.25_f32),
3552                Some(3.25_f32),
3553                None,
3554                Some(5.25_f32)
3555            ]
3556        );
3557        // f64
3558        generate_cast_test_case!(
3559            array,
3560            Float64Array,
3561            &DataType::Float64,
3562            vec![
3563                Some(1.25_f64),
3564                Some(2.25_f64),
3565                Some(3.25_f64),
3566                None,
3567                Some(5.25_f64)
3568            ]
3569        );
3570    }
3571
3572    #[test]
3573    fn test_cast_decimal32_to_numeric() {
3574        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3575        let array = create_decimal32_array(value_array, 8, 2).unwrap();
3576
3577        generate_decimal_to_numeric_cast_test_case(&array);
3578    }
3579
3580    #[test]
3581    fn test_cast_decimal64_to_numeric() {
3582        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3583        let array = create_decimal64_array(value_array, 8, 2).unwrap();
3584
3585        generate_decimal_to_numeric_cast_test_case(&array);
3586    }
3587
3588    #[test]
3589    fn test_cast_decimal128_to_numeric() {
3590        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3591        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3592
3593        generate_decimal_to_numeric_cast_test_case(&array);
3594
3595        // overflow test: out of range of max u8
3596        let value_array: Vec<Option<i128>> = vec![Some(51300)];
3597        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3598        let casted_array = cast_with_options(
3599            &array,
3600            &DataType::UInt8,
3601            &CastOptions {
3602                safe: false,
3603                format_options: FormatOptions::default(),
3604            },
3605        );
3606        assert_eq!(
3607            "Cast error: value of 513 is out of range UInt8".to_string(),
3608            casted_array.unwrap_err().to_string()
3609        );
3610
3611        let casted_array = cast_with_options(
3612            &array,
3613            &DataType::UInt8,
3614            &CastOptions {
3615                safe: true,
3616                format_options: FormatOptions::default(),
3617            },
3618        );
3619        assert!(casted_array.is_ok());
3620        assert!(casted_array.unwrap().is_null(0));
3621
3622        // overflow test: out of range of max i8
3623        let value_array: Vec<Option<i128>> = vec![Some(24400)];
3624        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3625        let casted_array = cast_with_options(
3626            &array,
3627            &DataType::Int8,
3628            &CastOptions {
3629                safe: false,
3630                format_options: FormatOptions::default(),
3631            },
3632        );
3633        assert_eq!(
3634            "Cast error: value of 244 is out of range Int8".to_string(),
3635            casted_array.unwrap_err().to_string()
3636        );
3637
3638        let casted_array = cast_with_options(
3639            &array,
3640            &DataType::Int8,
3641            &CastOptions {
3642                safe: true,
3643                format_options: FormatOptions::default(),
3644            },
3645        );
3646        assert!(casted_array.is_ok());
3647        assert!(casted_array.unwrap().is_null(0));
3648
3649        // loss the precision: convert decimal to f32、f64
3650        // f32
3651        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3652        let value_array: Vec<Option<i128>> = vec![
3653            Some(125),
3654            Some(225),
3655            Some(325),
3656            None,
3657            Some(525),
3658            Some(112345678),
3659            Some(112345679),
3660        ];
3661        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3662        generate_cast_test_case!(
3663            &array,
3664            Float32Array,
3665            &DataType::Float32,
3666            vec![
3667                Some(1.25_f32),
3668                Some(2.25_f32),
3669                Some(3.25_f32),
3670                None,
3671                Some(5.25_f32),
3672                Some(1_123_456.7_f32),
3673                Some(1_123_456.7_f32)
3674            ]
3675        );
3676
3677        // f64
3678        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3679        let value_array: Vec<Option<i128>> = vec![
3680            Some(125),
3681            Some(225),
3682            Some(325),
3683            None,
3684            Some(525),
3685            Some(112345678901234568),
3686            Some(112345678901234560),
3687        ];
3688        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3689        generate_cast_test_case!(
3690            &array,
3691            Float64Array,
3692            &DataType::Float64,
3693            vec![
3694                Some(1.25_f64),
3695                Some(2.25_f64),
3696                Some(3.25_f64),
3697                None,
3698                Some(5.25_f64),
3699                Some(1_123_456_789_012_345.6_f64),
3700                Some(1_123_456_789_012_345.6_f64),
3701            ]
3702        );
3703    }
3704
3705    #[test]
3706    fn test_cast_decimal256_to_numeric() {
3707        let value_array: Vec<Option<i256>> = vec![
3708            Some(i256::from_i128(125)),
3709            Some(i256::from_i128(225)),
3710            Some(i256::from_i128(325)),
3711            None,
3712            Some(i256::from_i128(525)),
3713        ];
3714        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3715        // u8
3716        generate_cast_test_case!(
3717            &array,
3718            UInt8Array,
3719            &DataType::UInt8,
3720            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3721        );
3722        // u16
3723        generate_cast_test_case!(
3724            &array,
3725            UInt16Array,
3726            &DataType::UInt16,
3727            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3728        );
3729        // u32
3730        generate_cast_test_case!(
3731            &array,
3732            UInt32Array,
3733            &DataType::UInt32,
3734            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3735        );
3736        // u64
3737        generate_cast_test_case!(
3738            &array,
3739            UInt64Array,
3740            &DataType::UInt64,
3741            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3742        );
3743        // i8
3744        generate_cast_test_case!(
3745            &array,
3746            Int8Array,
3747            &DataType::Int8,
3748            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3749        );
3750        // i16
3751        generate_cast_test_case!(
3752            &array,
3753            Int16Array,
3754            &DataType::Int16,
3755            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3756        );
3757        // i32
3758        generate_cast_test_case!(
3759            &array,
3760            Int32Array,
3761            &DataType::Int32,
3762            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3763        );
3764        // i64
3765        generate_cast_test_case!(
3766            &array,
3767            Int64Array,
3768            &DataType::Int64,
3769            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3770        );
3771        // f32
3772        generate_cast_test_case!(
3773            &array,
3774            Float32Array,
3775            &DataType::Float32,
3776            vec![
3777                Some(1.25_f32),
3778                Some(2.25_f32),
3779                Some(3.25_f32),
3780                None,
3781                Some(5.25_f32)
3782            ]
3783        );
3784        // f64
3785        generate_cast_test_case!(
3786            &array,
3787            Float64Array,
3788            &DataType::Float64,
3789            vec![
3790                Some(1.25_f64),
3791                Some(2.25_f64),
3792                Some(3.25_f64),
3793                None,
3794                Some(5.25_f64)
3795            ]
3796        );
3797
3798        // overflow test: out of range of max i8
3799        let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128(24400))];
3800        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3801        let casted_array = cast_with_options(
3802            &array,
3803            &DataType::Int8,
3804            &CastOptions {
3805                safe: false,
3806                format_options: FormatOptions::default(),
3807            },
3808        );
3809        assert_eq!(
3810            "Cast error: value of 244 is out of range Int8".to_string(),
3811            casted_array.unwrap_err().to_string()
3812        );
3813
3814        let casted_array = cast_with_options(
3815            &array,
3816            &DataType::Int8,
3817            &CastOptions {
3818                safe: true,
3819                format_options: FormatOptions::default(),
3820            },
3821        );
3822        assert!(casted_array.is_ok());
3823        assert!(casted_array.unwrap().is_null(0));
3824
3825        // loss the precision: convert decimal to f32、f64
3826        // f32
3827        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3828        let value_array: Vec<Option<i256>> = vec![
3829            Some(i256::from_i128(125)),
3830            Some(i256::from_i128(225)),
3831            Some(i256::from_i128(325)),
3832            None,
3833            Some(i256::from_i128(525)),
3834            Some(i256::from_i128(112345678)),
3835            Some(i256::from_i128(112345679)),
3836        ];
3837        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3838        generate_cast_test_case!(
3839            &array,
3840            Float32Array,
3841            &DataType::Float32,
3842            vec![
3843                Some(1.25_f32),
3844                Some(2.25_f32),
3845                Some(3.25_f32),
3846                None,
3847                Some(5.25_f32),
3848                Some(1_123_456.7_f32),
3849                Some(1_123_456.7_f32)
3850            ]
3851        );
3852
3853        // f64
3854        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3855        let value_array: Vec<Option<i256>> = vec![
3856            Some(i256::from_i128(125)),
3857            Some(i256::from_i128(225)),
3858            Some(i256::from_i128(325)),
3859            None,
3860            Some(i256::from_i128(525)),
3861            Some(i256::from_i128(112345678901234568)),
3862            Some(i256::from_i128(112345678901234560)),
3863        ];
3864        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3865        generate_cast_test_case!(
3866            &array,
3867            Float64Array,
3868            &DataType::Float64,
3869            vec![
3870                Some(1.25_f64),
3871                Some(2.25_f64),
3872                Some(3.25_f64),
3873                None,
3874                Some(5.25_f64),
3875                Some(1_123_456_789_012_345.6_f64),
3876                Some(1_123_456_789_012_345.6_f64),
3877            ]
3878        );
3879    }
3880
3881    #[test]
3882    fn test_cast_numeric_to_decimal128() {
3883        let decimal_type = DataType::Decimal128(38, 6);
3884        // u8, u16, u32, u64
3885        let input_datas = vec![
3886            Arc::new(UInt8Array::from(vec![
3887                Some(1),
3888                Some(2),
3889                Some(3),
3890                None,
3891                Some(5),
3892            ])) as ArrayRef, // u8
3893            Arc::new(UInt16Array::from(vec![
3894                Some(1),
3895                Some(2),
3896                Some(3),
3897                None,
3898                Some(5),
3899            ])) as ArrayRef, // u16
3900            Arc::new(UInt32Array::from(vec![
3901                Some(1),
3902                Some(2),
3903                Some(3),
3904                None,
3905                Some(5),
3906            ])) as ArrayRef, // u32
3907            Arc::new(UInt64Array::from(vec![
3908                Some(1),
3909                Some(2),
3910                Some(3),
3911                None,
3912                Some(5),
3913            ])) as ArrayRef, // u64
3914        ];
3915
3916        for array in input_datas {
3917            generate_cast_test_case!(
3918                &array,
3919                Decimal128Array,
3920                &decimal_type,
3921                vec![
3922                    Some(1000000_i128),
3923                    Some(2000000_i128),
3924                    Some(3000000_i128),
3925                    None,
3926                    Some(5000000_i128)
3927                ]
3928            );
3929        }
3930
3931        // i8, i16, i32, i64
3932        let input_datas = vec![
3933            Arc::new(Int8Array::from(vec![
3934                Some(1),
3935                Some(2),
3936                Some(3),
3937                None,
3938                Some(5),
3939            ])) as ArrayRef, // i8
3940            Arc::new(Int16Array::from(vec![
3941                Some(1),
3942                Some(2),
3943                Some(3),
3944                None,
3945                Some(5),
3946            ])) as ArrayRef, // i16
3947            Arc::new(Int32Array::from(vec![
3948                Some(1),
3949                Some(2),
3950                Some(3),
3951                None,
3952                Some(5),
3953            ])) as ArrayRef, // i32
3954            Arc::new(Int64Array::from(vec![
3955                Some(1),
3956                Some(2),
3957                Some(3),
3958                None,
3959                Some(5),
3960            ])) as ArrayRef, // i64
3961        ];
3962        for array in input_datas {
3963            generate_cast_test_case!(
3964                &array,
3965                Decimal128Array,
3966                &decimal_type,
3967                vec![
3968                    Some(1000000_i128),
3969                    Some(2000000_i128),
3970                    Some(3000000_i128),
3971                    None,
3972                    Some(5000000_i128)
3973                ]
3974            );
3975        }
3976
3977        // test u8 to decimal type with overflow the result type
3978        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
3979        let array = UInt8Array::from(vec![1, 2, 3, 4, 100]);
3980        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
3981        assert!(casted_array.is_ok());
3982        let array = casted_array.unwrap();
3983        let array: &Decimal128Array = array.as_primitive();
3984        assert!(array.is_null(4));
3985
3986        // test i8 to decimal type with overflow the result type
3987        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
3988        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
3989        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
3990        assert!(casted_array.is_ok());
3991        let array = casted_array.unwrap();
3992        let array: &Decimal128Array = array.as_primitive();
3993        assert!(array.is_null(4));
3994
3995        // test f32 to decimal type
3996        let array = Float32Array::from(vec![
3997            Some(1.1),
3998            Some(2.2),
3999            Some(4.4),
4000            None,
4001            Some(1.123_456_4), // round down
4002            Some(1.123_456_7), // round up
4003        ]);
4004        let array = Arc::new(array) as ArrayRef;
4005        generate_cast_test_case!(
4006            &array,
4007            Decimal128Array,
4008            &decimal_type,
4009            vec![
4010                Some(1100000_i128),
4011                Some(2200000_i128),
4012                Some(4400000_i128),
4013                None,
4014                Some(1123456_i128), // round down
4015                Some(1123457_i128), // round up
4016            ]
4017        );
4018
4019        // test f64 to decimal type
4020        let array = Float64Array::from(vec![
4021            Some(1.1),
4022            Some(2.2),
4023            Some(4.4),
4024            None,
4025            Some(1.123_456_489_123_4),     // round up
4026            Some(1.123_456_789_123_4),     // round up
4027            Some(1.123_456_489_012_345_6), // round down
4028            Some(1.123_456_789_012_345_6), // round up
4029        ]);
4030        generate_cast_test_case!(
4031            &array,
4032            Decimal128Array,
4033            &decimal_type,
4034            vec![
4035                Some(1100000_i128),
4036                Some(2200000_i128),
4037                Some(4400000_i128),
4038                None,
4039                Some(1123456_i128), // round down
4040                Some(1123457_i128), // round up
4041                Some(1123456_i128), // round down
4042                Some(1123457_i128), // round up
4043            ]
4044        );
4045    }
4046
4047    #[test]
4048    fn test_cast_numeric_to_decimal256() {
4049        let decimal_type = DataType::Decimal256(76, 6);
4050        // u8, u16, u32, u64
4051        let input_datas = vec![
4052            Arc::new(UInt8Array::from(vec![
4053                Some(1),
4054                Some(2),
4055                Some(3),
4056                None,
4057                Some(5),
4058            ])) as ArrayRef, // u8
4059            Arc::new(UInt16Array::from(vec![
4060                Some(1),
4061                Some(2),
4062                Some(3),
4063                None,
4064                Some(5),
4065            ])) as ArrayRef, // u16
4066            Arc::new(UInt32Array::from(vec![
4067                Some(1),
4068                Some(2),
4069                Some(3),
4070                None,
4071                Some(5),
4072            ])) as ArrayRef, // u32
4073            Arc::new(UInt64Array::from(vec![
4074                Some(1),
4075                Some(2),
4076                Some(3),
4077                None,
4078                Some(5),
4079            ])) as ArrayRef, // u64
4080        ];
4081
4082        for array in input_datas {
4083            generate_cast_test_case!(
4084                &array,
4085                Decimal256Array,
4086                &decimal_type,
4087                vec![
4088                    Some(i256::from_i128(1000000_i128)),
4089                    Some(i256::from_i128(2000000_i128)),
4090                    Some(i256::from_i128(3000000_i128)),
4091                    None,
4092                    Some(i256::from_i128(5000000_i128))
4093                ]
4094            );
4095        }
4096
4097        // i8, i16, i32, i64
4098        let input_datas = vec![
4099            Arc::new(Int8Array::from(vec![
4100                Some(1),
4101                Some(2),
4102                Some(3),
4103                None,
4104                Some(5),
4105            ])) as ArrayRef, // i8
4106            Arc::new(Int16Array::from(vec![
4107                Some(1),
4108                Some(2),
4109                Some(3),
4110                None,
4111                Some(5),
4112            ])) as ArrayRef, // i16
4113            Arc::new(Int32Array::from(vec![
4114                Some(1),
4115                Some(2),
4116                Some(3),
4117                None,
4118                Some(5),
4119            ])) as ArrayRef, // i32
4120            Arc::new(Int64Array::from(vec![
4121                Some(1),
4122                Some(2),
4123                Some(3),
4124                None,
4125                Some(5),
4126            ])) as ArrayRef, // i64
4127        ];
4128        for array in input_datas {
4129            generate_cast_test_case!(
4130                &array,
4131                Decimal256Array,
4132                &decimal_type,
4133                vec![
4134                    Some(i256::from_i128(1000000_i128)),
4135                    Some(i256::from_i128(2000000_i128)),
4136                    Some(i256::from_i128(3000000_i128)),
4137                    None,
4138                    Some(i256::from_i128(5000000_i128))
4139                ]
4140            );
4141        }
4142
4143        // test i8 to decimal type with overflow the result type
4144        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4145        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4146        let array = Arc::new(array) as ArrayRef;
4147        let casted_array = cast(&array, &DataType::Decimal256(3, 1));
4148        assert!(casted_array.is_ok());
4149        let array = casted_array.unwrap();
4150        let array: &Decimal256Array = array.as_primitive();
4151        assert!(array.is_null(4));
4152
4153        // test f32 to decimal type
4154        let array = Float32Array::from(vec![
4155            Some(1.1),
4156            Some(2.2),
4157            Some(4.4),
4158            None,
4159            Some(1.123_456_4), // round down
4160            Some(1.123_456_7), // round up
4161        ]);
4162        generate_cast_test_case!(
4163            &array,
4164            Decimal256Array,
4165            &decimal_type,
4166            vec![
4167                Some(i256::from_i128(1100000_i128)),
4168                Some(i256::from_i128(2200000_i128)),
4169                Some(i256::from_i128(4400000_i128)),
4170                None,
4171                Some(i256::from_i128(1123456_i128)), // round down
4172                Some(i256::from_i128(1123457_i128)), // round up
4173            ]
4174        );
4175
4176        // test f64 to decimal type
4177        let array = Float64Array::from(vec![
4178            Some(1.1),
4179            Some(2.2),
4180            Some(4.4),
4181            None,
4182            Some(1.123_456_489_123_4),     // round down
4183            Some(1.123_456_789_123_4),     // round up
4184            Some(1.123_456_489_012_345_6), // round down
4185            Some(1.123_456_789_012_345_6), // round up
4186        ]);
4187        generate_cast_test_case!(
4188            &array,
4189            Decimal256Array,
4190            &decimal_type,
4191            vec![
4192                Some(i256::from_i128(1100000_i128)),
4193                Some(i256::from_i128(2200000_i128)),
4194                Some(i256::from_i128(4400000_i128)),
4195                None,
4196                Some(i256::from_i128(1123456_i128)), // round down
4197                Some(i256::from_i128(1123457_i128)), // round up
4198                Some(i256::from_i128(1123456_i128)), // round down
4199                Some(i256::from_i128(1123457_i128)), // round up
4200            ]
4201        );
4202    }
4203
4204    #[test]
4205    fn test_cast_i32_to_f64() {
4206        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4207        let b = cast(&array, &DataType::Float64).unwrap();
4208        let c = b.as_primitive::<Float64Type>();
4209        assert_eq!(5.0, c.value(0));
4210        assert_eq!(6.0, c.value(1));
4211        assert_eq!(7.0, c.value(2));
4212        assert_eq!(8.0, c.value(3));
4213        assert_eq!(9.0, c.value(4));
4214    }
4215
4216    #[test]
4217    fn test_cast_i32_to_u8() {
4218        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4219        let b = cast(&array, &DataType::UInt8).unwrap();
4220        let c = b.as_primitive::<UInt8Type>();
4221        assert!(!c.is_valid(0));
4222        assert_eq!(6, c.value(1));
4223        assert!(!c.is_valid(2));
4224        assert_eq!(8, c.value(3));
4225        // overflows return None
4226        assert!(!c.is_valid(4));
4227    }
4228
4229    #[test]
4230    #[should_panic(expected = "Can't cast value -5 to type UInt8")]
4231    fn test_cast_int32_to_u8_with_error() {
4232        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4233        // overflow with the error
4234        let cast_option = CastOptions {
4235            safe: false,
4236            format_options: FormatOptions::default(),
4237        };
4238        let result = cast_with_options(&array, &DataType::UInt8, &cast_option);
4239        assert!(result.is_err());
4240        result.unwrap();
4241    }
4242
4243    #[test]
4244    fn test_cast_i32_to_u8_sliced() {
4245        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4246        assert_eq!(0, array.offset());
4247        let array = array.slice(2, 3);
4248        let b = cast(&array, &DataType::UInt8).unwrap();
4249        assert_eq!(3, b.len());
4250        let c = b.as_primitive::<UInt8Type>();
4251        assert!(!c.is_valid(0));
4252        assert_eq!(8, c.value(1));
4253        // overflows return None
4254        assert!(!c.is_valid(2));
4255    }
4256
4257    #[test]
4258    fn test_cast_i32_to_i32() {
4259        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4260        let b = cast(&array, &DataType::Int32).unwrap();
4261        let c = b.as_primitive::<Int32Type>();
4262        assert_eq!(5, c.value(0));
4263        assert_eq!(6, c.value(1));
4264        assert_eq!(7, c.value(2));
4265        assert_eq!(8, c.value(3));
4266        assert_eq!(9, c.value(4));
4267    }
4268
4269    #[test]
4270    fn test_cast_i32_to_list_i32() {
4271        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4272        let b = cast(
4273            &array,
4274            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4275        )
4276        .unwrap();
4277        assert_eq!(5, b.len());
4278        let arr = b.as_list::<i32>();
4279        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4280        assert_eq!(1, arr.value_length(0));
4281        assert_eq!(1, arr.value_length(1));
4282        assert_eq!(1, arr.value_length(2));
4283        assert_eq!(1, arr.value_length(3));
4284        assert_eq!(1, arr.value_length(4));
4285        let c = arr.values().as_primitive::<Int32Type>();
4286        assert_eq!(5, c.value(0));
4287        assert_eq!(6, c.value(1));
4288        assert_eq!(7, c.value(2));
4289        assert_eq!(8, c.value(3));
4290        assert_eq!(9, c.value(4));
4291    }
4292
4293    #[test]
4294    fn test_cast_i32_to_list_i32_nullable() {
4295        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), Some(9)]);
4296        let b = cast(
4297            &array,
4298            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4299        )
4300        .unwrap();
4301        assert_eq!(5, b.len());
4302        assert_eq!(0, b.null_count());
4303        let arr = b.as_list::<i32>();
4304        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4305        assert_eq!(1, arr.value_length(0));
4306        assert_eq!(1, arr.value_length(1));
4307        assert_eq!(1, arr.value_length(2));
4308        assert_eq!(1, arr.value_length(3));
4309        assert_eq!(1, arr.value_length(4));
4310
4311        let c = arr.values().as_primitive::<Int32Type>();
4312        assert_eq!(1, c.null_count());
4313        assert_eq!(5, c.value(0));
4314        assert!(!c.is_valid(1));
4315        assert_eq!(7, c.value(2));
4316        assert_eq!(8, c.value(3));
4317        assert_eq!(9, c.value(4));
4318    }
4319
4320    #[test]
4321    fn test_cast_i32_to_list_f64_nullable_sliced() {
4322        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), None, Some(10)]);
4323        let array = array.slice(2, 4);
4324        let b = cast(
4325            &array,
4326            &DataType::List(Arc::new(Field::new_list_field(DataType::Float64, true))),
4327        )
4328        .unwrap();
4329        assert_eq!(4, b.len());
4330        assert_eq!(0, b.null_count());
4331        let arr = b.as_list::<i32>();
4332        assert_eq!(&[0, 1, 2, 3, 4], arr.value_offsets());
4333        assert_eq!(1, arr.value_length(0));
4334        assert_eq!(1, arr.value_length(1));
4335        assert_eq!(1, arr.value_length(2));
4336        assert_eq!(1, arr.value_length(3));
4337        let c = arr.values().as_primitive::<Float64Type>();
4338        assert_eq!(1, c.null_count());
4339        assert_eq!(7.0, c.value(0));
4340        assert_eq!(8.0, c.value(1));
4341        assert!(!c.is_valid(2));
4342        assert_eq!(10.0, c.value(3));
4343    }
4344
4345    #[test]
4346    fn test_cast_int_to_utf8view() {
4347        let inputs = vec![
4348            Arc::new(Int8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4349            Arc::new(Int16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4350            Arc::new(Int32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4351            Arc::new(Int64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4352            Arc::new(UInt8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4353            Arc::new(UInt16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4354            Arc::new(UInt32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4355            Arc::new(UInt64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4356        ];
4357        let expected: ArrayRef = Arc::new(StringViewArray::from(vec![
4358            None,
4359            Some("8"),
4360            Some("9"),
4361            Some("10"),
4362        ]));
4363
4364        for array in inputs {
4365            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4366            let arr = cast(&array, &DataType::Utf8View).unwrap();
4367            assert_eq!(expected.as_ref(), arr.as_ref());
4368        }
4369    }
4370
4371    #[test]
4372    fn test_cast_float_to_utf8view() {
4373        let inputs = vec![
4374            Arc::new(Float16Array::from(vec![
4375                Some(f16::from_f64(1.5)),
4376                Some(f16::from_f64(2.5)),
4377                None,
4378            ])) as ArrayRef,
4379            Arc::new(Float32Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4380            Arc::new(Float64Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4381        ];
4382
4383        let expected: ArrayRef =
4384            Arc::new(StringViewArray::from(vec![Some("1.5"), Some("2.5"), None]));
4385
4386        for array in inputs {
4387            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4388            let arr = cast(&array, &DataType::Utf8View).unwrap();
4389            assert_eq!(expected.as_ref(), arr.as_ref());
4390        }
4391    }
4392
4393    #[test]
4394    fn test_cast_utf8_to_i32() {
4395        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4396        let b = cast(&array, &DataType::Int32).unwrap();
4397        let c = b.as_primitive::<Int32Type>();
4398        assert_eq!(5, c.value(0));
4399        assert_eq!(6, c.value(1));
4400        assert!(!c.is_valid(2));
4401        assert_eq!(8, c.value(3));
4402        assert!(!c.is_valid(4));
4403    }
4404
4405    #[test]
4406    fn test_cast_utf8view_to_i32() {
4407        let array = StringViewArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4408        let b = cast(&array, &DataType::Int32).unwrap();
4409        let c = b.as_primitive::<Int32Type>();
4410        assert_eq!(5, c.value(0));
4411        assert_eq!(6, c.value(1));
4412        assert!(!c.is_valid(2));
4413        assert_eq!(8, c.value(3));
4414        assert!(!c.is_valid(4));
4415    }
4416
4417    #[test]
4418    fn test_cast_utf8view_to_f32() {
4419        let array = StringViewArray::from(vec!["3", "4.56", "seven", "8.9"]);
4420        let b = cast(&array, &DataType::Float32).unwrap();
4421        let c = b.as_primitive::<Float32Type>();
4422        assert_eq!(3.0, c.value(0));
4423        assert_eq!(4.56, c.value(1));
4424        assert!(!c.is_valid(2));
4425        assert_eq!(8.9, c.value(3));
4426    }
4427
4428    #[test]
4429    fn test_cast_utf8view_to_decimal128() {
4430        let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
4431        let arr = Arc::new(array) as ArrayRef;
4432        generate_cast_test_case!(
4433            &arr,
4434            Decimal128Array,
4435            &DataType::Decimal128(4, 2),
4436            vec![None, Some(400_i128), Some(560_i128), Some(789_i128)]
4437        );
4438    }
4439
4440    #[test]
4441    fn test_cast_with_options_utf8_to_i32() {
4442        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4443        let result = cast_with_options(
4444            &array,
4445            &DataType::Int32,
4446            &CastOptions {
4447                safe: false,
4448                format_options: FormatOptions::default(),
4449            },
4450        );
4451        match result {
4452            Ok(_) => panic!("expected error"),
4453            Err(e) => {
4454                assert!(
4455                    e.to_string()
4456                        .contains("Cast error: Cannot cast string 'seven' to value of Int32 type",),
4457                    "Error: {e}"
4458                )
4459            }
4460        }
4461    }
4462
4463    #[test]
4464    fn test_cast_utf8_to_bool() {
4465        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4466        let casted = cast(&strings, &DataType::Boolean).unwrap();
4467        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4468        assert_eq!(*as_boolean_array(&casted), expected);
4469    }
4470
4471    #[test]
4472    fn test_cast_utf8view_to_bool() {
4473        let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4474        let casted = cast(&strings, &DataType::Boolean).unwrap();
4475        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4476        assert_eq!(*as_boolean_array(&casted), expected);
4477    }
4478
4479    #[test]
4480    fn test_cast_with_options_utf8_to_bool() {
4481        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4482        let casted = cast_with_options(
4483            &strings,
4484            &DataType::Boolean,
4485            &CastOptions {
4486                safe: false,
4487                format_options: FormatOptions::default(),
4488            },
4489        );
4490        match casted {
4491            Ok(_) => panic!("expected error"),
4492            Err(e) => {
4493                assert!(
4494                    e.to_string().contains(
4495                        "Cast error: Cannot cast value 'invalid' to value of Boolean type"
4496                    )
4497                )
4498            }
4499        }
4500    }
4501
4502    #[test]
4503    fn test_cast_bool_to_i32() {
4504        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4505        let b = cast(&array, &DataType::Int32).unwrap();
4506        let c = b.as_primitive::<Int32Type>();
4507        assert_eq!(1, c.value(0));
4508        assert_eq!(0, c.value(1));
4509        assert!(!c.is_valid(2));
4510    }
4511
4512    #[test]
4513    fn test_cast_bool_to_utf8view() {
4514        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4515        let b = cast(&array, &DataType::Utf8View).unwrap();
4516        let c = b.as_any().downcast_ref::<StringViewArray>().unwrap();
4517        assert_eq!("true", c.value(0));
4518        assert_eq!("false", c.value(1));
4519        assert!(!c.is_valid(2));
4520    }
4521
4522    #[test]
4523    fn test_cast_bool_to_utf8() {
4524        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4525        let b = cast(&array, &DataType::Utf8).unwrap();
4526        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
4527        assert_eq!("true", c.value(0));
4528        assert_eq!("false", c.value(1));
4529        assert!(!c.is_valid(2));
4530    }
4531
4532    #[test]
4533    fn test_cast_bool_to_large_utf8() {
4534        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4535        let b = cast(&array, &DataType::LargeUtf8).unwrap();
4536        let c = b.as_any().downcast_ref::<LargeStringArray>().unwrap();
4537        assert_eq!("true", c.value(0));
4538        assert_eq!("false", c.value(1));
4539        assert!(!c.is_valid(2));
4540    }
4541
4542    #[test]
4543    fn test_cast_bool_to_f64() {
4544        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4545        let b = cast(&array, &DataType::Float64).unwrap();
4546        let c = b.as_primitive::<Float64Type>();
4547        assert_eq!(1.0, c.value(0));
4548        assert_eq!(0.0, c.value(1));
4549        assert!(!c.is_valid(2));
4550    }
4551
4552    #[test]
4553    fn test_cast_integer_to_timestamp() {
4554        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4555        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4556
4557        let array = Int8Array::from(vec![Some(2), Some(10), None]);
4558        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4559
4560        assert_eq!(&actual, &expected);
4561
4562        let array = Int16Array::from(vec![Some(2), Some(10), None]);
4563        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4564
4565        assert_eq!(&actual, &expected);
4566
4567        let array = Int32Array::from(vec![Some(2), Some(10), None]);
4568        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4569
4570        assert_eq!(&actual, &expected);
4571
4572        let array = UInt8Array::from(vec![Some(2), Some(10), None]);
4573        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4574
4575        assert_eq!(&actual, &expected);
4576
4577        let array = UInt16Array::from(vec![Some(2), Some(10), None]);
4578        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4579
4580        assert_eq!(&actual, &expected);
4581
4582        let array = UInt32Array::from(vec![Some(2), Some(10), None]);
4583        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4584
4585        assert_eq!(&actual, &expected);
4586
4587        let array = UInt64Array::from(vec![Some(2), Some(10), None]);
4588        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4589
4590        assert_eq!(&actual, &expected);
4591    }
4592
4593    #[test]
4594    fn test_cast_timestamp_to_integer() {
4595        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4596            .with_timezone("UTC".to_string());
4597        let expected = cast(&array, &DataType::Int64).unwrap();
4598
4599        let actual = cast(&cast(&array, &DataType::Int8).unwrap(), &DataType::Int64).unwrap();
4600        assert_eq!(&actual, &expected);
4601
4602        let actual = cast(&cast(&array, &DataType::Int16).unwrap(), &DataType::Int64).unwrap();
4603        assert_eq!(&actual, &expected);
4604
4605        let actual = cast(&cast(&array, &DataType::Int32).unwrap(), &DataType::Int64).unwrap();
4606        assert_eq!(&actual, &expected);
4607
4608        let actual = cast(&cast(&array, &DataType::UInt8).unwrap(), &DataType::Int64).unwrap();
4609        assert_eq!(&actual, &expected);
4610
4611        let actual = cast(&cast(&array, &DataType::UInt16).unwrap(), &DataType::Int64).unwrap();
4612        assert_eq!(&actual, &expected);
4613
4614        let actual = cast(&cast(&array, &DataType::UInt32).unwrap(), &DataType::Int64).unwrap();
4615        assert_eq!(&actual, &expected);
4616
4617        let actual = cast(&cast(&array, &DataType::UInt64).unwrap(), &DataType::Int64).unwrap();
4618        assert_eq!(&actual, &expected);
4619    }
4620
4621    #[test]
4622    fn test_cast_floating_to_timestamp() {
4623        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4624        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4625
4626        let array = Float16Array::from(vec![
4627            Some(f16::from_f32(2.0)),
4628            Some(f16::from_f32(10.6)),
4629            None,
4630        ]);
4631        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4632
4633        assert_eq!(&actual, &expected);
4634
4635        let array = Float32Array::from(vec![Some(2.0), Some(10.6), None]);
4636        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4637
4638        assert_eq!(&actual, &expected);
4639
4640        let array = Float64Array::from(vec![Some(2.1), Some(10.2), None]);
4641        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4642
4643        assert_eq!(&actual, &expected);
4644    }
4645
4646    #[test]
4647    fn test_cast_timestamp_to_floating() {
4648        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4649            .with_timezone("UTC".to_string());
4650        let expected = cast(&array, &DataType::Int64).unwrap();
4651
4652        let actual = cast(&cast(&array, &DataType::Float16).unwrap(), &DataType::Int64).unwrap();
4653        assert_eq!(&actual, &expected);
4654
4655        let actual = cast(&cast(&array, &DataType::Float32).unwrap(), &DataType::Int64).unwrap();
4656        assert_eq!(&actual, &expected);
4657
4658        let actual = cast(&cast(&array, &DataType::Float64).unwrap(), &DataType::Int64).unwrap();
4659        assert_eq!(&actual, &expected);
4660    }
4661
4662    #[test]
4663    fn test_cast_decimal_to_timestamp() {
4664        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4665        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4666
4667        let array = Decimal128Array::from(vec![Some(200), Some(1000), None])
4668            .with_precision_and_scale(4, 2)
4669            .unwrap();
4670        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4671
4672        assert_eq!(&actual, &expected);
4673
4674        let array = Decimal256Array::from(vec![
4675            Some(i256::from_i128(2000)),
4676            Some(i256::from_i128(10000)),
4677            None,
4678        ])
4679        .with_precision_and_scale(5, 3)
4680        .unwrap();
4681        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4682
4683        assert_eq!(&actual, &expected);
4684    }
4685
4686    #[test]
4687    fn test_cast_timestamp_to_decimal() {
4688        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4689            .with_timezone("UTC".to_string());
4690        let expected = cast(&array, &DataType::Int64).unwrap();
4691
4692        let actual = cast(
4693            &cast(&array, &DataType::Decimal128(5, 2)).unwrap(),
4694            &DataType::Int64,
4695        )
4696        .unwrap();
4697        assert_eq!(&actual, &expected);
4698
4699        let actual = cast(
4700            &cast(&array, &DataType::Decimal256(10, 5)).unwrap(),
4701            &DataType::Int64,
4702        )
4703        .unwrap();
4704        assert_eq!(&actual, &expected);
4705    }
4706
4707    #[test]
4708    fn test_cast_list_i32_to_list_u16() {
4709        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 100000000]).into_data();
4710
4711        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
4712
4713        // Construct a list array from the above two
4714        // [[0,0,0], [-1, -2, -1], [2, 100000000]]
4715        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
4716        let list_data = ArrayData::builder(list_data_type)
4717            .len(3)
4718            .add_buffer(value_offsets)
4719            .add_child_data(value_data)
4720            .build()
4721            .unwrap();
4722        let list_array = ListArray::from(list_data);
4723
4724        let cast_array = cast(
4725            &list_array,
4726            &DataType::List(Arc::new(Field::new_list_field(DataType::UInt16, true))),
4727        )
4728        .unwrap();
4729
4730        // For the ListArray itself, there are no null values (as there were no nulls when they went in)
4731        //
4732        // 3 negative values should get lost when casting to unsigned,
4733        // 1 value should overflow
4734        assert_eq!(0, cast_array.null_count());
4735
4736        // offsets should be the same
4737        let array = cast_array.as_list::<i32>();
4738        assert_eq!(list_array.value_offsets(), array.value_offsets());
4739
4740        assert_eq!(DataType::UInt16, array.value_type());
4741        assert_eq!(3, array.value_length(0));
4742        assert_eq!(3, array.value_length(1));
4743        assert_eq!(2, array.value_length(2));
4744
4745        // expect 4 nulls: negative numbers and overflow
4746        let u16arr = array.values().as_primitive::<UInt16Type>();
4747        assert_eq!(4, u16arr.null_count());
4748
4749        // expect 4 nulls: negative numbers and overflow
4750        let expected: UInt16Array =
4751            vec![Some(0), Some(0), Some(0), None, None, None, Some(2), None]
4752                .into_iter()
4753                .collect();
4754
4755        assert_eq!(u16arr, &expected);
4756    }
4757
4758    #[test]
4759    fn test_cast_list_i32_to_list_timestamp() {
4760        // Construct a value array
4761        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 8, 100000000]).into_data();
4762
4763        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 9]);
4764
4765        // Construct a list array from the above two
4766        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
4767        let list_data = ArrayData::builder(list_data_type)
4768            .len(3)
4769            .add_buffer(value_offsets)
4770            .add_child_data(value_data)
4771            .build()
4772            .unwrap();
4773        let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
4774
4775        let actual = cast(
4776            &list_array,
4777            &DataType::List(Arc::new(Field::new_list_field(
4778                DataType::Timestamp(TimeUnit::Microsecond, None),
4779                true,
4780            ))),
4781        )
4782        .unwrap();
4783
4784        let expected = cast(
4785            &cast(
4786                &list_array,
4787                &DataType::List(Arc::new(Field::new_list_field(DataType::Int64, true))),
4788            )
4789            .unwrap(),
4790            &DataType::List(Arc::new(Field::new_list_field(
4791                DataType::Timestamp(TimeUnit::Microsecond, None),
4792                true,
4793            ))),
4794        )
4795        .unwrap();
4796
4797        assert_eq!(&actual, &expected);
4798    }
4799
4800    #[test]
4801    fn test_cast_date32_to_date64() {
4802        let a = Date32Array::from(vec![10000, 17890]);
4803        let array = Arc::new(a) as ArrayRef;
4804        let b = cast(&array, &DataType::Date64).unwrap();
4805        let c = b.as_primitive::<Date64Type>();
4806        assert_eq!(864000000000, c.value(0));
4807        assert_eq!(1545696000000, c.value(1));
4808    }
4809
4810    #[test]
4811    fn test_cast_date64_to_date32() {
4812        let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
4813        let array = Arc::new(a) as ArrayRef;
4814        let b = cast(&array, &DataType::Date32).unwrap();
4815        let c = b.as_primitive::<Date32Type>();
4816        assert_eq!(10000, c.value(0));
4817        assert_eq!(17890, c.value(1));
4818        assert!(c.is_null(2));
4819    }
4820
4821    #[test]
4822    fn test_cast_string_to_integral_overflow() {
4823        let str = Arc::new(StringArray::from(vec![
4824            Some("123"),
4825            Some("-123"),
4826            Some("86374"),
4827            None,
4828        ])) as ArrayRef;
4829
4830        let options = CastOptions {
4831            safe: true,
4832            format_options: FormatOptions::default(),
4833        };
4834        let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
4835        let expected =
4836            Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
4837        assert_eq!(&res, &expected);
4838    }
4839
4840    #[test]
4841    fn test_cast_string_to_timestamp() {
4842        let a0 = Arc::new(StringViewArray::from(vec![
4843            Some("2020-09-08T12:00:00.123456789+00:00"),
4844            Some("Not a valid date"),
4845            None,
4846        ])) as ArrayRef;
4847        let a1 = Arc::new(StringArray::from(vec![
4848            Some("2020-09-08T12:00:00.123456789+00:00"),
4849            Some("Not a valid date"),
4850            None,
4851        ])) as ArrayRef;
4852        let a2 = Arc::new(LargeStringArray::from(vec![
4853            Some("2020-09-08T12:00:00.123456789+00:00"),
4854            Some("Not a valid date"),
4855            None,
4856        ])) as ArrayRef;
4857        for array in &[a0, a1, a2] {
4858            for time_unit in &[
4859                TimeUnit::Second,
4860                TimeUnit::Millisecond,
4861                TimeUnit::Microsecond,
4862                TimeUnit::Nanosecond,
4863            ] {
4864                let to_type = DataType::Timestamp(*time_unit, None);
4865                let b = cast(array, &to_type).unwrap();
4866
4867                match time_unit {
4868                    TimeUnit::Second => {
4869                        let c = b.as_primitive::<TimestampSecondType>();
4870                        assert_eq!(1599566400, c.value(0));
4871                        assert!(c.is_null(1));
4872                        assert!(c.is_null(2));
4873                    }
4874                    TimeUnit::Millisecond => {
4875                        let c = b
4876                            .as_any()
4877                            .downcast_ref::<TimestampMillisecondArray>()
4878                            .unwrap();
4879                        assert_eq!(1599566400123, c.value(0));
4880                        assert!(c.is_null(1));
4881                        assert!(c.is_null(2));
4882                    }
4883                    TimeUnit::Microsecond => {
4884                        let c = b
4885                            .as_any()
4886                            .downcast_ref::<TimestampMicrosecondArray>()
4887                            .unwrap();
4888                        assert_eq!(1599566400123456, c.value(0));
4889                        assert!(c.is_null(1));
4890                        assert!(c.is_null(2));
4891                    }
4892                    TimeUnit::Nanosecond => {
4893                        let c = b
4894                            .as_any()
4895                            .downcast_ref::<TimestampNanosecondArray>()
4896                            .unwrap();
4897                        assert_eq!(1599566400123456789, c.value(0));
4898                        assert!(c.is_null(1));
4899                        assert!(c.is_null(2));
4900                    }
4901                }
4902
4903                let options = CastOptions {
4904                    safe: false,
4905                    format_options: FormatOptions::default(),
4906                };
4907                let err = cast_with_options(array, &to_type, &options).unwrap_err();
4908                assert_eq!(
4909                    err.to_string(),
4910                    "Parser error: Error parsing timestamp from 'Not a valid date': error parsing date"
4911                );
4912            }
4913        }
4914    }
4915
4916    #[test]
4917    fn test_cast_string_to_timestamp_overflow() {
4918        let array = StringArray::from(vec!["9800-09-08T12:00:00.123456789"]);
4919        let result = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
4920        let result = result.as_primitive::<TimestampSecondType>();
4921        assert_eq!(result.values(), &[247112596800]);
4922    }
4923
4924    #[test]
4925    fn test_cast_string_to_date32() {
4926        let a0 = Arc::new(StringViewArray::from(vec![
4927            Some("2018-12-25"),
4928            Some("Not a valid date"),
4929            None,
4930        ])) as ArrayRef;
4931        let a1 = Arc::new(StringArray::from(vec![
4932            Some("2018-12-25"),
4933            Some("Not a valid date"),
4934            None,
4935        ])) as ArrayRef;
4936        let a2 = Arc::new(LargeStringArray::from(vec![
4937            Some("2018-12-25"),
4938            Some("Not a valid date"),
4939            None,
4940        ])) as ArrayRef;
4941        for array in &[a0, a1, a2] {
4942            let to_type = DataType::Date32;
4943            let b = cast(array, &to_type).unwrap();
4944            let c = b.as_primitive::<Date32Type>();
4945            assert_eq!(17890, c.value(0));
4946            assert!(c.is_null(1));
4947            assert!(c.is_null(2));
4948
4949            let options = CastOptions {
4950                safe: false,
4951                format_options: FormatOptions::default(),
4952            };
4953            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4954            assert_eq!(
4955                err.to_string(),
4956                "Cast error: Cannot cast string 'Not a valid date' to value of Date32 type"
4957            );
4958        }
4959    }
4960
4961    #[test]
4962    fn test_cast_string_with_large_date_to_date32() {
4963        let array = Arc::new(StringArray::from(vec![
4964            Some("+10999-12-31"),
4965            Some("-0010-02-28"),
4966            Some("0010-02-28"),
4967            Some("0000-01-01"),
4968            Some("-0000-01-01"),
4969            Some("-0001-01-01"),
4970        ])) as ArrayRef;
4971        let to_type = DataType::Date32;
4972        let options = CastOptions {
4973            safe: false,
4974            format_options: FormatOptions::default(),
4975        };
4976        let b = cast_with_options(&array, &to_type, &options).unwrap();
4977        let c = b.as_primitive::<Date32Type>();
4978        assert_eq!(3298139, c.value(0)); // 10999-12-31
4979        assert_eq!(-723122, c.value(1)); // -0010-02-28
4980        assert_eq!(-715817, c.value(2)); // 0010-02-28
4981        assert_eq!(c.value(3), c.value(4)); // Expect 0000-01-01 and -0000-01-01 to be parsed the same
4982        assert_eq!(-719528, c.value(3)); // 0000-01-01
4983        assert_eq!(-719528, c.value(4)); // -0000-01-01
4984        assert_eq!(-719893, c.value(5)); // -0001-01-01
4985    }
4986
4987    #[test]
4988    fn test_cast_invalid_string_with_large_date_to_date32() {
4989        // Large dates need to be prefixed with a + or - sign, otherwise they are not parsed correctly
4990        let array = Arc::new(StringArray::from(vec![Some("10999-12-31")])) as ArrayRef;
4991        let to_type = DataType::Date32;
4992        let options = CastOptions {
4993            safe: false,
4994            format_options: FormatOptions::default(),
4995        };
4996        let err = cast_with_options(&array, &to_type, &options).unwrap_err();
4997        assert_eq!(
4998            err.to_string(),
4999            "Cast error: Cannot cast string '10999-12-31' to value of Date32 type"
5000        );
5001    }
5002
5003    #[test]
5004    fn test_cast_string_format_yyyymmdd_to_date32() {
5005        let a0 = Arc::new(StringViewArray::from(vec![
5006            Some("2020-12-25"),
5007            Some("20201117"),
5008        ])) as ArrayRef;
5009        let a1 = Arc::new(StringArray::from(vec![
5010            Some("2020-12-25"),
5011            Some("20201117"),
5012        ])) as ArrayRef;
5013        let a2 = Arc::new(LargeStringArray::from(vec![
5014            Some("2020-12-25"),
5015            Some("20201117"),
5016        ])) as ArrayRef;
5017
5018        for array in &[a0, a1, a2] {
5019            let to_type = DataType::Date32;
5020            let options = CastOptions {
5021                safe: false,
5022                format_options: FormatOptions::default(),
5023            };
5024            let result = cast_with_options(&array, &to_type, &options).unwrap();
5025            let c = result.as_primitive::<Date32Type>();
5026            assert_eq!(
5027                chrono::NaiveDate::from_ymd_opt(2020, 12, 25),
5028                c.value_as_date(0)
5029            );
5030            assert_eq!(
5031                chrono::NaiveDate::from_ymd_opt(2020, 11, 17),
5032                c.value_as_date(1)
5033            );
5034        }
5035    }
5036
5037    #[test]
5038    fn test_cast_string_to_time32second() {
5039        let a0 = Arc::new(StringViewArray::from(vec![
5040            Some("08:08:35.091323414"),
5041            Some("08:08:60.091323414"), // leap second
5042            Some("08:08:61.091323414"), // not valid
5043            Some("Not a valid time"),
5044            None,
5045        ])) as ArrayRef;
5046        let a1 = Arc::new(StringArray::from(vec![
5047            Some("08:08:35.091323414"),
5048            Some("08:08:60.091323414"), // leap second
5049            Some("08:08:61.091323414"), // not valid
5050            Some("Not a valid time"),
5051            None,
5052        ])) as ArrayRef;
5053        let a2 = Arc::new(LargeStringArray::from(vec![
5054            Some("08:08:35.091323414"),
5055            Some("08:08:60.091323414"), // leap second
5056            Some("08:08:61.091323414"), // not valid
5057            Some("Not a valid time"),
5058            None,
5059        ])) as ArrayRef;
5060        for array in &[a0, a1, a2] {
5061            let to_type = DataType::Time32(TimeUnit::Second);
5062            let b = cast(array, &to_type).unwrap();
5063            let c = b.as_primitive::<Time32SecondType>();
5064            assert_eq!(29315, c.value(0));
5065            assert_eq!(29340, c.value(1));
5066            assert!(c.is_null(2));
5067            assert!(c.is_null(3));
5068            assert!(c.is_null(4));
5069
5070            let options = CastOptions {
5071                safe: false,
5072                format_options: FormatOptions::default(),
5073            };
5074            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5075            assert_eq!(
5076                err.to_string(),
5077                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(s) type"
5078            );
5079        }
5080    }
5081
5082    #[test]
5083    fn test_cast_string_to_time32millisecond() {
5084        let a0 = Arc::new(StringViewArray::from(vec![
5085            Some("08:08:35.091323414"),
5086            Some("08:08:60.091323414"), // leap second
5087            Some("08:08:61.091323414"), // not valid
5088            Some("Not a valid time"),
5089            None,
5090        ])) as ArrayRef;
5091        let a1 = Arc::new(StringArray::from(vec![
5092            Some("08:08:35.091323414"),
5093            Some("08:08:60.091323414"), // leap second
5094            Some("08:08:61.091323414"), // not valid
5095            Some("Not a valid time"),
5096            None,
5097        ])) as ArrayRef;
5098        let a2 = Arc::new(LargeStringArray::from(vec![
5099            Some("08:08:35.091323414"),
5100            Some("08:08:60.091323414"), // leap second
5101            Some("08:08:61.091323414"), // not valid
5102            Some("Not a valid time"),
5103            None,
5104        ])) as ArrayRef;
5105        for array in &[a0, a1, a2] {
5106            let to_type = DataType::Time32(TimeUnit::Millisecond);
5107            let b = cast(array, &to_type).unwrap();
5108            let c = b.as_primitive::<Time32MillisecondType>();
5109            assert_eq!(29315091, c.value(0));
5110            assert_eq!(29340091, c.value(1));
5111            assert!(c.is_null(2));
5112            assert!(c.is_null(3));
5113            assert!(c.is_null(4));
5114
5115            let options = CastOptions {
5116                safe: false,
5117                format_options: FormatOptions::default(),
5118            };
5119            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5120            assert_eq!(
5121                err.to_string(),
5122                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(ms) type"
5123            );
5124        }
5125    }
5126
5127    #[test]
5128    fn test_cast_string_to_time64microsecond() {
5129        let a0 = Arc::new(StringViewArray::from(vec![
5130            Some("08:08:35.091323414"),
5131            Some("Not a valid time"),
5132            None,
5133        ])) as ArrayRef;
5134        let a1 = Arc::new(StringArray::from(vec![
5135            Some("08:08:35.091323414"),
5136            Some("Not a valid time"),
5137            None,
5138        ])) as ArrayRef;
5139        let a2 = Arc::new(LargeStringArray::from(vec![
5140            Some("08:08:35.091323414"),
5141            Some("Not a valid time"),
5142            None,
5143        ])) as ArrayRef;
5144        for array in &[a0, a1, a2] {
5145            let to_type = DataType::Time64(TimeUnit::Microsecond);
5146            let b = cast(array, &to_type).unwrap();
5147            let c = b.as_primitive::<Time64MicrosecondType>();
5148            assert_eq!(29315091323, c.value(0));
5149            assert!(c.is_null(1));
5150            assert!(c.is_null(2));
5151
5152            let options = CastOptions {
5153                safe: false,
5154                format_options: FormatOptions::default(),
5155            };
5156            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5157            assert_eq!(
5158                err.to_string(),
5159                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(µs) type"
5160            );
5161        }
5162    }
5163
5164    #[test]
5165    fn test_cast_string_to_time64nanosecond() {
5166        let a0 = Arc::new(StringViewArray::from(vec![
5167            Some("08:08:35.091323414"),
5168            Some("Not a valid time"),
5169            None,
5170        ])) as ArrayRef;
5171        let a1 = Arc::new(StringArray::from(vec![
5172            Some("08:08:35.091323414"),
5173            Some("Not a valid time"),
5174            None,
5175        ])) as ArrayRef;
5176        let a2 = Arc::new(LargeStringArray::from(vec![
5177            Some("08:08:35.091323414"),
5178            Some("Not a valid time"),
5179            None,
5180        ])) as ArrayRef;
5181        for array in &[a0, a1, a2] {
5182            let to_type = DataType::Time64(TimeUnit::Nanosecond);
5183            let b = cast(array, &to_type).unwrap();
5184            let c = b.as_primitive::<Time64NanosecondType>();
5185            assert_eq!(29315091323414, c.value(0));
5186            assert!(c.is_null(1));
5187            assert!(c.is_null(2));
5188
5189            let options = CastOptions {
5190                safe: false,
5191                format_options: FormatOptions::default(),
5192            };
5193            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5194            assert_eq!(
5195                err.to_string(),
5196                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(ns) type"
5197            );
5198        }
5199    }
5200
5201    #[test]
5202    fn test_cast_string_to_date64() {
5203        let a0 = Arc::new(StringViewArray::from(vec![
5204            Some("2020-09-08T12:00:00"),
5205            Some("Not a valid date"),
5206            None,
5207        ])) as ArrayRef;
5208        let a1 = Arc::new(StringArray::from(vec![
5209            Some("2020-09-08T12:00:00"),
5210            Some("Not a valid date"),
5211            None,
5212        ])) as ArrayRef;
5213        let a2 = Arc::new(LargeStringArray::from(vec![
5214            Some("2020-09-08T12:00:00"),
5215            Some("Not a valid date"),
5216            None,
5217        ])) as ArrayRef;
5218        for array in &[a0, a1, a2] {
5219            let to_type = DataType::Date64;
5220            let b = cast(array, &to_type).unwrap();
5221            let c = b.as_primitive::<Date64Type>();
5222            assert_eq!(1599566400000, c.value(0));
5223            assert!(c.is_null(1));
5224            assert!(c.is_null(2));
5225
5226            let options = CastOptions {
5227                safe: false,
5228                format_options: FormatOptions::default(),
5229            };
5230            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5231            assert_eq!(
5232                err.to_string(),
5233                "Cast error: Cannot cast string 'Not a valid date' to value of Date64 type"
5234            );
5235        }
5236    }
5237
5238    macro_rules! test_safe_string_to_interval {
5239        ($data_vec:expr, $interval_unit:expr, $array_ty:ty, $expect_vec:expr) => {
5240            let source_string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5241
5242            let options = CastOptions {
5243                safe: true,
5244                format_options: FormatOptions::default(),
5245            };
5246
5247            let target_interval_array = cast_with_options(
5248                &source_string_array.clone(),
5249                &DataType::Interval($interval_unit),
5250                &options,
5251            )
5252            .unwrap()
5253            .as_any()
5254            .downcast_ref::<$array_ty>()
5255            .unwrap()
5256            .clone() as $array_ty;
5257
5258            let target_string_array =
5259                cast_with_options(&target_interval_array, &DataType::Utf8, &options)
5260                    .unwrap()
5261                    .as_any()
5262                    .downcast_ref::<StringArray>()
5263                    .unwrap()
5264                    .clone();
5265
5266            let expect_string_array = StringArray::from($expect_vec);
5267
5268            assert_eq!(target_string_array, expect_string_array);
5269
5270            let target_large_string_array =
5271                cast_with_options(&target_interval_array, &DataType::LargeUtf8, &options)
5272                    .unwrap()
5273                    .as_any()
5274                    .downcast_ref::<LargeStringArray>()
5275                    .unwrap()
5276                    .clone();
5277
5278            let expect_large_string_array = LargeStringArray::from($expect_vec);
5279
5280            assert_eq!(target_large_string_array, expect_large_string_array);
5281        };
5282    }
5283
5284    #[test]
5285    fn test_cast_string_to_interval_year_month() {
5286        test_safe_string_to_interval!(
5287            vec![
5288                Some("1 year 1 month"),
5289                Some("1.5 years 13 month"),
5290                Some("30 days"),
5291                Some("31 days"),
5292                Some("2 months 31 days"),
5293                Some("2 months 31 days 1 second"),
5294                Some("foobar"),
5295            ],
5296            IntervalUnit::YearMonth,
5297            IntervalYearMonthArray,
5298            vec![
5299                Some("1 years 1 mons"),
5300                Some("2 years 7 mons"),
5301                None,
5302                None,
5303                None,
5304                None,
5305                None,
5306            ]
5307        );
5308    }
5309
5310    #[test]
5311    fn test_cast_string_to_interval_day_time() {
5312        test_safe_string_to_interval!(
5313            vec![
5314                Some("1 year 1 month"),
5315                Some("1.5 years 13 month"),
5316                Some("30 days"),
5317                Some("1 day 2 second 3.5 milliseconds"),
5318                Some("foobar"),
5319            ],
5320            IntervalUnit::DayTime,
5321            IntervalDayTimeArray,
5322            vec![
5323                Some("390 days"),
5324                Some("930 days"),
5325                Some("30 days"),
5326                None,
5327                None,
5328            ]
5329        );
5330    }
5331
5332    #[test]
5333    fn test_cast_string_to_interval_month_day_nano() {
5334        test_safe_string_to_interval!(
5335            vec![
5336                Some("1 year 1 month 1 day"),
5337                None,
5338                Some("1.5 years 13 month 35 days 1.4 milliseconds"),
5339                Some("3 days"),
5340                Some("8 seconds"),
5341                None,
5342                Some("1 day 29800 milliseconds"),
5343                Some("3 months 1 second"),
5344                Some("6 minutes 120 second"),
5345                Some("2 years 39 months 9 days 19 hours 1 minute 83 seconds 399222 milliseconds"),
5346                Some("foobar"),
5347            ],
5348            IntervalUnit::MonthDayNano,
5349            IntervalMonthDayNanoArray,
5350            vec![
5351                Some("13 mons 1 days"),
5352                None,
5353                Some("31 mons 35 days 0.001400000 secs"),
5354                Some("3 days"),
5355                Some("8.000000000 secs"),
5356                None,
5357                Some("1 days 29.800000000 secs"),
5358                Some("3 mons 1.000000000 secs"),
5359                Some("8 mins"),
5360                Some("63 mons 9 days 19 hours 9 mins 2.222000000 secs"),
5361                None,
5362            ]
5363        );
5364    }
5365
5366    macro_rules! test_unsafe_string_to_interval_err {
5367        ($data_vec:expr, $interval_unit:expr, $error_msg:expr) => {
5368            let string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5369            let options = CastOptions {
5370                safe: false,
5371                format_options: FormatOptions::default(),
5372            };
5373            let arrow_err = cast_with_options(
5374                &string_array.clone(),
5375                &DataType::Interval($interval_unit),
5376                &options,
5377            )
5378            .unwrap_err();
5379            assert_eq!($error_msg, arrow_err.to_string());
5380        };
5381    }
5382
5383    #[test]
5384    fn test_cast_string_to_interval_err() {
5385        test_unsafe_string_to_interval_err!(
5386            vec![Some("foobar")],
5387            IntervalUnit::YearMonth,
5388            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5389        );
5390        test_unsafe_string_to_interval_err!(
5391            vec![Some("foobar")],
5392            IntervalUnit::DayTime,
5393            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5394        );
5395        test_unsafe_string_to_interval_err!(
5396            vec![Some("foobar")],
5397            IntervalUnit::MonthDayNano,
5398            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5399        );
5400        test_unsafe_string_to_interval_err!(
5401            vec![Some("2 months 31 days 1 second")],
5402            IntervalUnit::YearMonth,
5403            r#"Cast error: Cannot cast 2 months 31 days 1 second to IntervalYearMonth. Only year and month fields are allowed."#
5404        );
5405        test_unsafe_string_to_interval_err!(
5406            vec![Some("1 day 1.5 milliseconds")],
5407            IntervalUnit::DayTime,
5408            r#"Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"#
5409        );
5410
5411        // overflow
5412        test_unsafe_string_to_interval_err!(
5413            vec![Some(format!(
5414                "{} century {} year {} month",
5415                i64::MAX - 2,
5416                i64::MAX - 2,
5417                i64::MAX - 2
5418            ))],
5419            IntervalUnit::DayTime,
5420            format!(
5421                "Arithmetic overflow: Overflow happened on: {} * 100",
5422                i64::MAX - 2
5423            )
5424        );
5425        test_unsafe_string_to_interval_err!(
5426            vec![Some(format!(
5427                "{} year {} month {} day",
5428                i64::MAX - 2,
5429                i64::MAX - 2,
5430                i64::MAX - 2
5431            ))],
5432            IntervalUnit::MonthDayNano,
5433            format!(
5434                "Arithmetic overflow: Overflow happened on: {} * 12",
5435                i64::MAX - 2
5436            )
5437        );
5438    }
5439
5440    #[test]
5441    fn test_cast_binary_to_fixed_size_binary() {
5442        let bytes_1 = "Hiiii".as_bytes();
5443        let bytes_2 = "Hello".as_bytes();
5444
5445        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5446        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5447        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5448
5449        let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
5450        let down_cast = array_ref
5451            .as_any()
5452            .downcast_ref::<FixedSizeBinaryArray>()
5453            .unwrap();
5454        assert_eq!(bytes_1, down_cast.value(0));
5455        assert_eq!(bytes_2, down_cast.value(1));
5456        assert!(down_cast.is_null(2));
5457
5458        let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
5459        let down_cast = array_ref
5460            .as_any()
5461            .downcast_ref::<FixedSizeBinaryArray>()
5462            .unwrap();
5463        assert_eq!(bytes_1, down_cast.value(0));
5464        assert_eq!(bytes_2, down_cast.value(1));
5465        assert!(down_cast.is_null(2));
5466
5467        // test error cases when the length of binary are not same
5468        let bytes_1 = "Hi".as_bytes();
5469        let bytes_2 = "Hello".as_bytes();
5470
5471        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5472        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5473        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5474
5475        let array_ref = cast_with_options(
5476            &a1,
5477            &DataType::FixedSizeBinary(5),
5478            &CastOptions {
5479                safe: false,
5480                format_options: FormatOptions::default(),
5481            },
5482        );
5483        assert!(array_ref.is_err());
5484
5485        let array_ref = cast_with_options(
5486            &a2,
5487            &DataType::FixedSizeBinary(5),
5488            &CastOptions {
5489                safe: false,
5490                format_options: FormatOptions::default(),
5491            },
5492        );
5493        assert!(array_ref.is_err());
5494    }
5495
5496    #[test]
5497    fn test_fixed_size_binary_to_binary() {
5498        let bytes_1 = "Hiiii".as_bytes();
5499        let bytes_2 = "Hello".as_bytes();
5500
5501        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5502        let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
5503
5504        let array_ref = cast(&a1, &DataType::Binary).unwrap();
5505        let down_cast = array_ref.as_binary::<i32>();
5506        assert_eq!(bytes_1, down_cast.value(0));
5507        assert_eq!(bytes_2, down_cast.value(1));
5508        assert!(down_cast.is_null(2));
5509
5510        let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
5511        let down_cast = array_ref.as_binary::<i64>();
5512        assert_eq!(bytes_1, down_cast.value(0));
5513        assert_eq!(bytes_2, down_cast.value(1));
5514        assert!(down_cast.is_null(2));
5515
5516        let array_ref = cast(&a1, &DataType::BinaryView).unwrap();
5517        let down_cast = array_ref.as_binary_view();
5518        assert_eq!(bytes_1, down_cast.value(0));
5519        assert_eq!(bytes_2, down_cast.value(1));
5520        assert!(down_cast.is_null(2));
5521    }
5522
5523    #[test]
5524    fn test_fixed_size_binary_to_dictionary() {
5525        let bytes_1 = "Hiiii".as_bytes();
5526        let bytes_2 = "Hello".as_bytes();
5527
5528        let binary_data = vec![Some(bytes_1), Some(bytes_2), Some(bytes_1), None];
5529        let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
5530
5531        let cast_type = DataType::Dictionary(
5532            Box::new(DataType::Int8),
5533            Box::new(DataType::FixedSizeBinary(5)),
5534        );
5535        let cast_array = cast(&a1, &cast_type).unwrap();
5536        assert_eq!(cast_array.data_type(), &cast_type);
5537        assert_eq!(
5538            array_to_strings(&cast_array),
5539            vec!["4869696969", "48656c6c6f", "4869696969", "null"]
5540        );
5541        // dictionary should only have two distinct values
5542        let dict_array = cast_array
5543            .as_any()
5544            .downcast_ref::<DictionaryArray<Int8Type>>()
5545            .unwrap();
5546        assert_eq!(dict_array.values().len(), 2);
5547    }
5548
5549    #[test]
5550    fn test_binary_to_dictionary() {
5551        let mut builder = GenericBinaryBuilder::<i32>::new();
5552        builder.append_value(b"hello");
5553        builder.append_value(b"hiiii");
5554        builder.append_value(b"hiiii"); // duplicate
5555        builder.append_null();
5556        builder.append_value(b"rustt");
5557
5558        let a1 = builder.finish();
5559
5560        let cast_type = DataType::Dictionary(
5561            Box::new(DataType::Int8),
5562            Box::new(DataType::FixedSizeBinary(5)),
5563        );
5564        let cast_array = cast(&a1, &cast_type).unwrap();
5565        assert_eq!(cast_array.data_type(), &cast_type);
5566        assert_eq!(
5567            array_to_strings(&cast_array),
5568            vec![
5569                "68656c6c6f",
5570                "6869696969",
5571                "6869696969",
5572                "null",
5573                "7275737474"
5574            ]
5575        );
5576        // dictionary should only have three distinct values
5577        let dict_array = cast_array
5578            .as_any()
5579            .downcast_ref::<DictionaryArray<Int8Type>>()
5580            .unwrap();
5581        assert_eq!(dict_array.values().len(), 3);
5582    }
5583
5584    #[test]
5585    fn test_numeric_to_binary() {
5586        let a = Int16Array::from(vec![Some(1), Some(511), None]);
5587
5588        let array_ref = cast(&a, &DataType::Binary).unwrap();
5589        let down_cast = array_ref.as_binary::<i32>();
5590        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
5591        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
5592        assert!(down_cast.is_null(2));
5593
5594        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
5595
5596        let array_ref = cast(&a, &DataType::Binary).unwrap();
5597        let down_cast = array_ref.as_binary::<i32>();
5598        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
5599        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
5600        assert!(down_cast.is_null(2));
5601    }
5602
5603    #[test]
5604    fn test_numeric_to_large_binary() {
5605        let a = Int16Array::from(vec![Some(1), Some(511), None]);
5606
5607        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
5608        let down_cast = array_ref.as_binary::<i64>();
5609        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
5610        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
5611        assert!(down_cast.is_null(2));
5612
5613        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
5614
5615        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
5616        let down_cast = array_ref.as_binary::<i64>();
5617        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
5618        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
5619        assert!(down_cast.is_null(2));
5620    }
5621
5622    #[test]
5623    fn test_cast_date32_to_int32() {
5624        let array = Date32Array::from(vec![10000, 17890]);
5625        let b = cast(&array, &DataType::Int32).unwrap();
5626        let c = b.as_primitive::<Int32Type>();
5627        assert_eq!(10000, c.value(0));
5628        assert_eq!(17890, c.value(1));
5629    }
5630
5631    #[test]
5632    fn test_cast_int32_to_date32() {
5633        let array = Int32Array::from(vec![10000, 17890]);
5634        let b = cast(&array, &DataType::Date32).unwrap();
5635        let c = b.as_primitive::<Date32Type>();
5636        assert_eq!(10000, c.value(0));
5637        assert_eq!(17890, c.value(1));
5638    }
5639
5640    #[test]
5641    fn test_cast_timestamp_to_date32() {
5642        let array =
5643            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
5644                .with_timezone("+00:00".to_string());
5645        let b = cast(&array, &DataType::Date32).unwrap();
5646        let c = b.as_primitive::<Date32Type>();
5647        assert_eq!(10000, c.value(0));
5648        assert_eq!(17890, c.value(1));
5649        assert!(c.is_null(2));
5650    }
5651    #[test]
5652    fn test_cast_timestamp_to_date32_zone() {
5653        let strings = StringArray::from_iter([
5654            Some("1970-01-01T00:00:01"),
5655            Some("1970-01-01T23:59:59"),
5656            None,
5657            Some("2020-03-01T02:00:23+00:00"),
5658        ]);
5659        let dt = DataType::Timestamp(TimeUnit::Millisecond, Some("-07:00".into()));
5660        let timestamps = cast(&strings, &dt).unwrap();
5661        let dates = cast(timestamps.as_ref(), &DataType::Date32).unwrap();
5662
5663        let c = dates.as_primitive::<Date32Type>();
5664        let expected = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
5665        assert_eq!(c.value_as_date(0).unwrap(), expected);
5666        assert_eq!(c.value_as_date(1).unwrap(), expected);
5667        assert!(c.is_null(2));
5668        let expected = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
5669        assert_eq!(c.value_as_date(3).unwrap(), expected);
5670    }
5671    #[test]
5672    fn test_cast_timestamp_to_date64() {
5673        let array =
5674            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None]);
5675        let b = cast(&array, &DataType::Date64).unwrap();
5676        let c = b.as_primitive::<Date64Type>();
5677        assert_eq!(864000000005, c.value(0));
5678        assert_eq!(1545696000001, c.value(1));
5679        assert!(c.is_null(2));
5680
5681        let array = TimestampSecondArray::from(vec![Some(864000000005), Some(1545696000001)]);
5682        let b = cast(&array, &DataType::Date64).unwrap();
5683        let c = b.as_primitive::<Date64Type>();
5684        assert_eq!(864000000005000, c.value(0));
5685        assert_eq!(1545696000001000, c.value(1));
5686
5687        // test overflow, safe cast
5688        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
5689        let b = cast(&array, &DataType::Date64).unwrap();
5690        assert!(b.is_null(0));
5691        // test overflow, unsafe cast
5692        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
5693        let options = CastOptions {
5694            safe: false,
5695            format_options: FormatOptions::default(),
5696        };
5697        let b = cast_with_options(&array, &DataType::Date64, &options);
5698        assert!(b.is_err());
5699    }
5700
5701    #[test]
5702    fn test_cast_timestamp_to_time64() {
5703        // test timestamp secs
5704        let array = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
5705            .with_timezone("+01:00".to_string());
5706        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5707        let c = b.as_primitive::<Time64MicrosecondType>();
5708        assert_eq!(3605000000, c.value(0));
5709        assert_eq!(3601000000, c.value(1));
5710        assert!(c.is_null(2));
5711        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5712        let c = b.as_primitive::<Time64NanosecondType>();
5713        assert_eq!(3605000000000, c.value(0));
5714        assert_eq!(3601000000000, c.value(1));
5715        assert!(c.is_null(2));
5716
5717        // test timestamp milliseconds
5718        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
5719            .with_timezone("+01:00".to_string());
5720        let array = Arc::new(a) as ArrayRef;
5721        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5722        let c = b.as_primitive::<Time64MicrosecondType>();
5723        assert_eq!(3605000000, c.value(0));
5724        assert_eq!(3601000000, c.value(1));
5725        assert!(c.is_null(2));
5726        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5727        let c = b.as_primitive::<Time64NanosecondType>();
5728        assert_eq!(3605000000000, c.value(0));
5729        assert_eq!(3601000000000, c.value(1));
5730        assert!(c.is_null(2));
5731
5732        // test timestamp microseconds
5733        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
5734            .with_timezone("+01:00".to_string());
5735        let array = Arc::new(a) as ArrayRef;
5736        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5737        let c = b.as_primitive::<Time64MicrosecondType>();
5738        assert_eq!(3605000000, c.value(0));
5739        assert_eq!(3601000000, c.value(1));
5740        assert!(c.is_null(2));
5741        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5742        let c = b.as_primitive::<Time64NanosecondType>();
5743        assert_eq!(3605000000000, c.value(0));
5744        assert_eq!(3601000000000, c.value(1));
5745        assert!(c.is_null(2));
5746
5747        // test timestamp nanoseconds
5748        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
5749            .with_timezone("+01:00".to_string());
5750        let array = Arc::new(a) as ArrayRef;
5751        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5752        let c = b.as_primitive::<Time64MicrosecondType>();
5753        assert_eq!(3605000000, c.value(0));
5754        assert_eq!(3601000000, c.value(1));
5755        assert!(c.is_null(2));
5756        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5757        let c = b.as_primitive::<Time64NanosecondType>();
5758        assert_eq!(3605000000000, c.value(0));
5759        assert_eq!(3601000000000, c.value(1));
5760        assert!(c.is_null(2));
5761
5762        // test overflow
5763        let a =
5764            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
5765        let array = Arc::new(a) as ArrayRef;
5766        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond));
5767        assert!(b.is_err());
5768        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond));
5769        assert!(b.is_err());
5770        let b = cast(&array, &DataType::Time64(TimeUnit::Millisecond));
5771        assert!(b.is_err());
5772    }
5773
5774    #[test]
5775    fn test_cast_timestamp_to_time32() {
5776        // test timestamp secs
5777        let a = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
5778            .with_timezone("+01:00".to_string());
5779        let array = Arc::new(a) as ArrayRef;
5780        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5781        let c = b.as_primitive::<Time32SecondType>();
5782        assert_eq!(3605, c.value(0));
5783        assert_eq!(3601, c.value(1));
5784        assert!(c.is_null(2));
5785        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5786        let c = b.as_primitive::<Time32MillisecondType>();
5787        assert_eq!(3605000, c.value(0));
5788        assert_eq!(3601000, c.value(1));
5789        assert!(c.is_null(2));
5790
5791        // test timestamp milliseconds
5792        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
5793            .with_timezone("+01:00".to_string());
5794        let array = Arc::new(a) as ArrayRef;
5795        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5796        let c = b.as_primitive::<Time32SecondType>();
5797        assert_eq!(3605, c.value(0));
5798        assert_eq!(3601, c.value(1));
5799        assert!(c.is_null(2));
5800        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5801        let c = b.as_primitive::<Time32MillisecondType>();
5802        assert_eq!(3605000, c.value(0));
5803        assert_eq!(3601000, c.value(1));
5804        assert!(c.is_null(2));
5805
5806        // test timestamp microseconds
5807        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
5808            .with_timezone("+01:00".to_string());
5809        let array = Arc::new(a) as ArrayRef;
5810        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5811        let c = b.as_primitive::<Time32SecondType>();
5812        assert_eq!(3605, c.value(0));
5813        assert_eq!(3601, c.value(1));
5814        assert!(c.is_null(2));
5815        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5816        let c = b.as_primitive::<Time32MillisecondType>();
5817        assert_eq!(3605000, c.value(0));
5818        assert_eq!(3601000, c.value(1));
5819        assert!(c.is_null(2));
5820
5821        // test timestamp nanoseconds
5822        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
5823            .with_timezone("+01:00".to_string());
5824        let array = Arc::new(a) as ArrayRef;
5825        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5826        let c = b.as_primitive::<Time32SecondType>();
5827        assert_eq!(3605, c.value(0));
5828        assert_eq!(3601, c.value(1));
5829        assert!(c.is_null(2));
5830        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5831        let c = b.as_primitive::<Time32MillisecondType>();
5832        assert_eq!(3605000, c.value(0));
5833        assert_eq!(3601000, c.value(1));
5834        assert!(c.is_null(2));
5835
5836        // test overflow
5837        let a =
5838            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
5839        let array = Arc::new(a) as ArrayRef;
5840        let b = cast(&array, &DataType::Time32(TimeUnit::Second));
5841        assert!(b.is_err());
5842        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond));
5843        assert!(b.is_err());
5844    }
5845
5846    // Cast Timestamp(_, None) -> Timestamp(_, Some(timezone))
5847    #[test]
5848    fn test_cast_timestamp_with_timezone_1() {
5849        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5850            Some("2000-01-01T00:00:00.123456789"),
5851            Some("2010-01-01T00:00:00.123456789"),
5852            None,
5853        ]));
5854        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
5855        let timestamp_array = cast(&string_array, &to_type).unwrap();
5856
5857        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
5858        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
5859
5860        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5861        let result = string_array.as_string::<i32>();
5862        assert_eq!("2000-01-01T00:00:00.123456+07:00", result.value(0));
5863        assert_eq!("2010-01-01T00:00:00.123456+07:00", result.value(1));
5864        assert!(result.is_null(2));
5865    }
5866
5867    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, None)
5868    #[test]
5869    fn test_cast_timestamp_with_timezone_2() {
5870        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5871            Some("2000-01-01T07:00:00.123456789"),
5872            Some("2010-01-01T07:00:00.123456789"),
5873            None,
5874        ]));
5875        let to_type = DataType::Timestamp(TimeUnit::Millisecond, Some("+0700".into()));
5876        let timestamp_array = cast(&string_array, &to_type).unwrap();
5877
5878        // Check intermediate representation is correct
5879        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5880        let result = string_array.as_string::<i32>();
5881        assert_eq!("2000-01-01T07:00:00.123+07:00", result.value(0));
5882        assert_eq!("2010-01-01T07:00:00.123+07:00", result.value(1));
5883        assert!(result.is_null(2));
5884
5885        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
5886        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
5887
5888        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5889        let result = string_array.as_string::<i32>();
5890        assert_eq!("2000-01-01T00:00:00.123", result.value(0));
5891        assert_eq!("2010-01-01T00:00:00.123", result.value(1));
5892        assert!(result.is_null(2));
5893    }
5894
5895    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, Some(timezone))
5896    #[test]
5897    fn test_cast_timestamp_with_timezone_3() {
5898        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5899            Some("2000-01-01T07:00:00.123456789"),
5900            Some("2010-01-01T07:00:00.123456789"),
5901            None,
5902        ]));
5903        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
5904        let timestamp_array = cast(&string_array, &to_type).unwrap();
5905
5906        // Check intermediate representation is correct
5907        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5908        let result = string_array.as_string::<i32>();
5909        assert_eq!("2000-01-01T07:00:00.123456+07:00", result.value(0));
5910        assert_eq!("2010-01-01T07:00:00.123456+07:00", result.value(1));
5911        assert!(result.is_null(2));
5912
5913        let to_type = DataType::Timestamp(TimeUnit::Second, Some("-08:00".into()));
5914        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
5915
5916        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5917        let result = string_array.as_string::<i32>();
5918        assert_eq!("1999-12-31T16:00:00-08:00", result.value(0));
5919        assert_eq!("2009-12-31T16:00:00-08:00", result.value(1));
5920        assert!(result.is_null(2));
5921    }
5922
5923    #[test]
5924    fn test_cast_date64_to_timestamp() {
5925        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5926        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5927        let c = b.as_primitive::<TimestampSecondType>();
5928        assert_eq!(864000000, c.value(0));
5929        assert_eq!(1545696000, c.value(1));
5930        assert!(c.is_null(2));
5931    }
5932
5933    #[test]
5934    fn test_cast_date64_to_timestamp_ms() {
5935        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5936        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
5937        let c = b
5938            .as_any()
5939            .downcast_ref::<TimestampMillisecondArray>()
5940            .unwrap();
5941        assert_eq!(864000000005, c.value(0));
5942        assert_eq!(1545696000001, c.value(1));
5943        assert!(c.is_null(2));
5944    }
5945
5946    #[test]
5947    fn test_cast_date64_to_timestamp_us() {
5948        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5949        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5950        let c = b
5951            .as_any()
5952            .downcast_ref::<TimestampMicrosecondArray>()
5953            .unwrap();
5954        assert_eq!(864000000005000, c.value(0));
5955        assert_eq!(1545696000001000, c.value(1));
5956        assert!(c.is_null(2));
5957    }
5958
5959    #[test]
5960    fn test_cast_date64_to_timestamp_ns() {
5961        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5962        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
5963        let c = b
5964            .as_any()
5965            .downcast_ref::<TimestampNanosecondArray>()
5966            .unwrap();
5967        assert_eq!(864000000005000000, c.value(0));
5968        assert_eq!(1545696000001000000, c.value(1));
5969        assert!(c.is_null(2));
5970    }
5971
5972    #[test]
5973    fn test_cast_timestamp_to_i64() {
5974        let array =
5975            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
5976                .with_timezone("UTC".to_string());
5977        let b = cast(&array, &DataType::Int64).unwrap();
5978        let c = b.as_primitive::<Int64Type>();
5979        assert_eq!(&DataType::Int64, c.data_type());
5980        assert_eq!(864000000005, c.value(0));
5981        assert_eq!(1545696000001, c.value(1));
5982        assert!(c.is_null(2));
5983    }
5984
5985    macro_rules! assert_cast {
5986        ($array:expr, $datatype:expr, $output_array_type: ty, $expected:expr) => {{
5987            assert!(can_cast_types($array.data_type(), &$datatype));
5988            let out = cast(&$array, &$datatype).unwrap();
5989            let actual = out
5990                .as_any()
5991                .downcast_ref::<$output_array_type>()
5992                .unwrap()
5993                .into_iter()
5994                .collect::<Vec<_>>();
5995            assert_eq!(actual, $expected);
5996        }};
5997        ($array:expr, $datatype:expr, $output_array_type: ty, $options:expr, $expected:expr) => {{
5998            assert!(can_cast_types($array.data_type(), &$datatype));
5999            let out = cast_with_options(&$array, &$datatype, &$options).unwrap();
6000            let actual = out
6001                .as_any()
6002                .downcast_ref::<$output_array_type>()
6003                .unwrap()
6004                .into_iter()
6005                .collect::<Vec<_>>();
6006            assert_eq!(actual, $expected);
6007        }};
6008    }
6009
6010    #[test]
6011    fn test_cast_date32_to_string() {
6012        let array = Date32Array::from(vec![Some(0), Some(10000), Some(13036), Some(17890), None]);
6013        let expected = vec![
6014            Some("1970-01-01"),
6015            Some("1997-05-19"),
6016            Some("2005-09-10"),
6017            Some("2018-12-25"),
6018            None,
6019        ];
6020
6021        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6022        assert_cast!(array, DataType::Utf8, StringArray, expected);
6023        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6024    }
6025
6026    #[test]
6027    fn test_cast_date64_to_string() {
6028        let array = Date64Array::from(vec![
6029            Some(0),
6030            Some(10000 * 86400000),
6031            Some(13036 * 86400000),
6032            Some(17890 * 86400000),
6033            None,
6034        ]);
6035        let expected = vec![
6036            Some("1970-01-01T00:00:00"),
6037            Some("1997-05-19T00:00:00"),
6038            Some("2005-09-10T00:00:00"),
6039            Some("2018-12-25T00:00:00"),
6040            None,
6041        ];
6042
6043        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6044        assert_cast!(array, DataType::Utf8, StringArray, expected);
6045        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6046    }
6047
6048    #[test]
6049    fn test_cast_date32_to_timestamp_and_timestamp_with_timezone() {
6050        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6051        let a = Date32Array::from(vec![Some(18628), None, None]); // 2021-1-1, 2022-1-1
6052        let array = Arc::new(a) as ArrayRef;
6053
6054        let b = cast(
6055            &array,
6056            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6057        )
6058        .unwrap();
6059        let c = b.as_primitive::<TimestampSecondType>();
6060        let string_array = cast(&c, &DataType::Utf8).unwrap();
6061        let result = string_array.as_string::<i32>();
6062        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6063
6064        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6065        let c = b.as_primitive::<TimestampSecondType>();
6066        let string_array = cast(&c, &DataType::Utf8).unwrap();
6067        let result = string_array.as_string::<i32>();
6068        assert_eq!("2021-01-01T00:00:00", result.value(0));
6069    }
6070
6071    #[test]
6072    fn test_cast_date32_to_timestamp_with_timezone() {
6073        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6074        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6075        let array = Arc::new(a) as ArrayRef;
6076        let b = cast(
6077            &array,
6078            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6079        )
6080        .unwrap();
6081        let c = b.as_primitive::<TimestampSecondType>();
6082        assert_eq!(1609438500, c.value(0));
6083        assert_eq!(1640974500, c.value(1));
6084        assert!(c.is_null(2));
6085
6086        let string_array = cast(&c, &DataType::Utf8).unwrap();
6087        let result = string_array.as_string::<i32>();
6088        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6089        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6090    }
6091
6092    #[test]
6093    fn test_cast_date32_to_timestamp_with_timezone_ms() {
6094        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6095        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6096        let array = Arc::new(a) as ArrayRef;
6097        let b = cast(
6098            &array,
6099            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
6100        )
6101        .unwrap();
6102        let c = b.as_primitive::<TimestampMillisecondType>();
6103        assert_eq!(1609438500000, c.value(0));
6104        assert_eq!(1640974500000, c.value(1));
6105        assert!(c.is_null(2));
6106
6107        let string_array = cast(&c, &DataType::Utf8).unwrap();
6108        let result = string_array.as_string::<i32>();
6109        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6110        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6111    }
6112
6113    #[test]
6114    fn test_cast_date32_to_timestamp_with_timezone_us() {
6115        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6116        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6117        let array = Arc::new(a) as ArrayRef;
6118        let b = cast(
6119            &array,
6120            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
6121        )
6122        .unwrap();
6123        let c = b.as_primitive::<TimestampMicrosecondType>();
6124        assert_eq!(1609438500000000, c.value(0));
6125        assert_eq!(1640974500000000, c.value(1));
6126        assert!(c.is_null(2));
6127
6128        let string_array = cast(&c, &DataType::Utf8).unwrap();
6129        let result = string_array.as_string::<i32>();
6130        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6131        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6132    }
6133
6134    #[test]
6135    fn test_cast_date32_to_timestamp_with_timezone_ns() {
6136        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6137        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6138        let array = Arc::new(a) as ArrayRef;
6139        let b = cast(
6140            &array,
6141            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
6142        )
6143        .unwrap();
6144        let c = b.as_primitive::<TimestampNanosecondType>();
6145        assert_eq!(1609438500000000000, c.value(0));
6146        assert_eq!(1640974500000000000, c.value(1));
6147        assert!(c.is_null(2));
6148
6149        let string_array = cast(&c, &DataType::Utf8).unwrap();
6150        let result = string_array.as_string::<i32>();
6151        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6152        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6153    }
6154
6155    #[test]
6156    fn test_cast_date64_to_timestamp_with_timezone() {
6157        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6158        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6159        let b = cast(
6160            &array,
6161            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6162        )
6163        .unwrap();
6164
6165        let c = b.as_primitive::<TimestampSecondType>();
6166        assert_eq!(863979300, c.value(0));
6167        assert_eq!(1545675300, c.value(1));
6168        assert!(c.is_null(2));
6169
6170        let string_array = cast(&c, &DataType::Utf8).unwrap();
6171        let result = string_array.as_string::<i32>();
6172        assert_eq!("1997-05-19T00:00:00+05:45", result.value(0));
6173        assert_eq!("2018-12-25T00:00:00+05:45", result.value(1));
6174    }
6175
6176    #[test]
6177    fn test_cast_date64_to_timestamp_with_timezone_ms() {
6178        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6179        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6180        let b = cast(
6181            &array,
6182            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
6183        )
6184        .unwrap();
6185
6186        let c = b.as_primitive::<TimestampMillisecondType>();
6187        assert_eq!(863979300005, c.value(0));
6188        assert_eq!(1545675300001, c.value(1));
6189        assert!(c.is_null(2));
6190
6191        let string_array = cast(&c, &DataType::Utf8).unwrap();
6192        let result = string_array.as_string::<i32>();
6193        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6194        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6195    }
6196
6197    #[test]
6198    fn test_cast_date64_to_timestamp_with_timezone_us() {
6199        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6200        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6201        let b = cast(
6202            &array,
6203            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
6204        )
6205        .unwrap();
6206
6207        let c = b.as_primitive::<TimestampMicrosecondType>();
6208        assert_eq!(863979300005000, c.value(0));
6209        assert_eq!(1545675300001000, c.value(1));
6210        assert!(c.is_null(2));
6211
6212        let string_array = cast(&c, &DataType::Utf8).unwrap();
6213        let result = string_array.as_string::<i32>();
6214        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6215        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6216    }
6217
6218    #[test]
6219    fn test_cast_date64_to_timestamp_with_timezone_ns() {
6220        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6221        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6222        let b = cast(
6223            &array,
6224            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
6225        )
6226        .unwrap();
6227
6228        let c = b.as_primitive::<TimestampNanosecondType>();
6229        assert_eq!(863979300005000000, c.value(0));
6230        assert_eq!(1545675300001000000, c.value(1));
6231        assert!(c.is_null(2));
6232
6233        let string_array = cast(&c, &DataType::Utf8).unwrap();
6234        let result = string_array.as_string::<i32>();
6235        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6236        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6237    }
6238
6239    #[test]
6240    fn test_cast_timestamp_to_strings() {
6241        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
6242        let array =
6243            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6244        let expected = vec![
6245            Some("1997-05-19T00:00:03.005"),
6246            Some("2018-12-25T00:00:02.001"),
6247            None,
6248        ];
6249
6250        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6251        assert_cast!(array, DataType::Utf8, StringArray, expected);
6252        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6253    }
6254
6255    #[test]
6256    fn test_cast_timestamp_to_strings_opt() {
6257        let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
6258        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6259        let cast_options = CastOptions {
6260            safe: true,
6261            format_options: FormatOptions::default()
6262                .with_timestamp_format(Some(ts_format))
6263                .with_timestamp_tz_format(Some(ts_format)),
6264        };
6265
6266        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
6267        let array_without_tz =
6268            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6269        let expected = vec![
6270            Some("1997-05-19 00:00:03.005000"),
6271            Some("2018-12-25 00:00:02.001000"),
6272            None,
6273        ];
6274        assert_cast!(
6275            array_without_tz,
6276            DataType::Utf8View,
6277            StringViewArray,
6278            cast_options,
6279            expected
6280        );
6281        assert_cast!(
6282            array_without_tz,
6283            DataType::Utf8,
6284            StringArray,
6285            cast_options,
6286            expected
6287        );
6288        assert_cast!(
6289            array_without_tz,
6290            DataType::LargeUtf8,
6291            LargeStringArray,
6292            cast_options,
6293            expected
6294        );
6295
6296        let array_with_tz =
6297            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None])
6298                .with_timezone(tz.to_string());
6299        let expected = vec![
6300            Some("1997-05-19 05:45:03.005000"),
6301            Some("2018-12-25 05:45:02.001000"),
6302            None,
6303        ];
6304        assert_cast!(
6305            array_with_tz,
6306            DataType::Utf8View,
6307            StringViewArray,
6308            cast_options,
6309            expected
6310        );
6311        assert_cast!(
6312            array_with_tz,
6313            DataType::Utf8,
6314            StringArray,
6315            cast_options,
6316            expected
6317        );
6318        assert_cast!(
6319            array_with_tz,
6320            DataType::LargeUtf8,
6321            LargeStringArray,
6322            cast_options,
6323            expected
6324        );
6325    }
6326
6327    #[test]
6328    fn test_cast_between_timestamps() {
6329        let array =
6330            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6331        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6332        let c = b.as_primitive::<TimestampSecondType>();
6333        assert_eq!(864000003, c.value(0));
6334        assert_eq!(1545696002, c.value(1));
6335        assert!(c.is_null(2));
6336    }
6337
6338    #[test]
6339    fn test_cast_duration_to_i64() {
6340        let base = vec![5, 6, 7, 8, 100000000];
6341
6342        let duration_arrays = vec![
6343            Arc::new(DurationNanosecondArray::from(base.clone())) as ArrayRef,
6344            Arc::new(DurationMicrosecondArray::from(base.clone())) as ArrayRef,
6345            Arc::new(DurationMillisecondArray::from(base.clone())) as ArrayRef,
6346            Arc::new(DurationSecondArray::from(base.clone())) as ArrayRef,
6347        ];
6348
6349        for arr in duration_arrays {
6350            assert!(can_cast_types(arr.data_type(), &DataType::Int64));
6351            let result = cast(&arr, &DataType::Int64).unwrap();
6352            let result = result.as_primitive::<Int64Type>();
6353            assert_eq!(base.as_slice(), result.values());
6354        }
6355    }
6356
6357    #[test]
6358    fn test_cast_between_durations_and_numerics() {
6359        fn test_cast_between_durations<FromType, ToType>()
6360        where
6361            FromType: ArrowPrimitiveType<Native = i64>,
6362            ToType: ArrowPrimitiveType<Native = i64>,
6363            PrimitiveArray<FromType>: From<Vec<Option<i64>>>,
6364        {
6365            let from_unit = match FromType::DATA_TYPE {
6366                DataType::Duration(unit) => unit,
6367                _ => panic!("Expected a duration type"),
6368            };
6369            let to_unit = match ToType::DATA_TYPE {
6370                DataType::Duration(unit) => unit,
6371                _ => panic!("Expected a duration type"),
6372            };
6373            let from_size = time_unit_multiple(&from_unit);
6374            let to_size = time_unit_multiple(&to_unit);
6375
6376            let (v1_before, v2_before) = (8640003005, 1696002001);
6377            let (v1_after, v2_after) = if from_size >= to_size {
6378                (
6379                    v1_before / (from_size / to_size),
6380                    v2_before / (from_size / to_size),
6381                )
6382            } else {
6383                (
6384                    v1_before * (to_size / from_size),
6385                    v2_before * (to_size / from_size),
6386                )
6387            };
6388
6389            let array =
6390                PrimitiveArray::<FromType>::from(vec![Some(v1_before), Some(v2_before), None]);
6391            let b = cast(&array, &ToType::DATA_TYPE).unwrap();
6392            let c = b.as_primitive::<ToType>();
6393            assert_eq!(v1_after, c.value(0));
6394            assert_eq!(v2_after, c.value(1));
6395            assert!(c.is_null(2));
6396        }
6397
6398        // between each individual duration type
6399        test_cast_between_durations::<DurationSecondType, DurationMillisecondType>();
6400        test_cast_between_durations::<DurationSecondType, DurationMicrosecondType>();
6401        test_cast_between_durations::<DurationSecondType, DurationNanosecondType>();
6402        test_cast_between_durations::<DurationMillisecondType, DurationSecondType>();
6403        test_cast_between_durations::<DurationMillisecondType, DurationMicrosecondType>();
6404        test_cast_between_durations::<DurationMillisecondType, DurationNanosecondType>();
6405        test_cast_between_durations::<DurationMicrosecondType, DurationSecondType>();
6406        test_cast_between_durations::<DurationMicrosecondType, DurationMillisecondType>();
6407        test_cast_between_durations::<DurationMicrosecondType, DurationNanosecondType>();
6408        test_cast_between_durations::<DurationNanosecondType, DurationSecondType>();
6409        test_cast_between_durations::<DurationNanosecondType, DurationMillisecondType>();
6410        test_cast_between_durations::<DurationNanosecondType, DurationMicrosecondType>();
6411
6412        // cast failed
6413        let array = DurationSecondArray::from(vec![
6414            Some(i64::MAX),
6415            Some(8640203410378005),
6416            Some(10241096),
6417            None,
6418        ]);
6419        let b = cast(&array, &DataType::Duration(TimeUnit::Nanosecond)).unwrap();
6420        let c = b.as_primitive::<DurationNanosecondType>();
6421        assert!(c.is_null(0));
6422        assert!(c.is_null(1));
6423        assert_eq!(10241096000000000, c.value(2));
6424        assert!(c.is_null(3));
6425
6426        // durations to numerics
6427        let array = DurationSecondArray::from(vec![
6428            Some(i64::MAX),
6429            Some(8640203410378005),
6430            Some(10241096),
6431            None,
6432        ]);
6433        let b = cast(&array, &DataType::Int64).unwrap();
6434        let c = b.as_primitive::<Int64Type>();
6435        assert_eq!(i64::MAX, c.value(0));
6436        assert_eq!(8640203410378005, c.value(1));
6437        assert_eq!(10241096, c.value(2));
6438        assert!(c.is_null(3));
6439
6440        let b = cast(&array, &DataType::Int32).unwrap();
6441        let c = b.as_primitive::<Int32Type>();
6442        assert_eq!(0, c.value(0));
6443        assert_eq!(0, c.value(1));
6444        assert_eq!(10241096, c.value(2));
6445        assert!(c.is_null(3));
6446
6447        // numerics to durations
6448        let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), Some(10241096), None]);
6449        let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
6450        let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
6451        assert_eq!(i32::MAX as i64, c.value(0));
6452        assert_eq!(802034103, c.value(1));
6453        assert_eq!(10241096, c.value(2));
6454        assert!(c.is_null(3));
6455    }
6456
6457    #[test]
6458    fn test_cast_to_strings() {
6459        let a = Int32Array::from(vec![1, 2, 3]);
6460        let out = cast(&a, &DataType::Utf8).unwrap();
6461        let out = out
6462            .as_any()
6463            .downcast_ref::<StringArray>()
6464            .unwrap()
6465            .into_iter()
6466            .collect::<Vec<_>>();
6467        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
6468        let out = cast(&a, &DataType::LargeUtf8).unwrap();
6469        let out = out
6470            .as_any()
6471            .downcast_ref::<LargeStringArray>()
6472            .unwrap()
6473            .into_iter()
6474            .collect::<Vec<_>>();
6475        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
6476    }
6477
6478    #[test]
6479    fn test_str_to_str_casts() {
6480        for data in [
6481            vec![Some("foo"), Some("bar"), Some("ham")],
6482            vec![Some("foo"), None, Some("bar")],
6483        ] {
6484            let a = LargeStringArray::from(data.clone());
6485            let to = cast(&a, &DataType::Utf8).unwrap();
6486            let expect = a
6487                .as_any()
6488                .downcast_ref::<LargeStringArray>()
6489                .unwrap()
6490                .into_iter()
6491                .collect::<Vec<_>>();
6492            let out = to
6493                .as_any()
6494                .downcast_ref::<StringArray>()
6495                .unwrap()
6496                .into_iter()
6497                .collect::<Vec<_>>();
6498            assert_eq!(expect, out);
6499
6500            let a = StringArray::from(data);
6501            let to = cast(&a, &DataType::LargeUtf8).unwrap();
6502            let expect = a
6503                .as_any()
6504                .downcast_ref::<StringArray>()
6505                .unwrap()
6506                .into_iter()
6507                .collect::<Vec<_>>();
6508            let out = to
6509                .as_any()
6510                .downcast_ref::<LargeStringArray>()
6511                .unwrap()
6512                .into_iter()
6513                .collect::<Vec<_>>();
6514            assert_eq!(expect, out);
6515        }
6516    }
6517
6518    const VIEW_TEST_DATA: [Option<&str>; 5] = [
6519        Some("hello"),
6520        Some("repeated"),
6521        None,
6522        Some("large payload over 12 bytes"),
6523        Some("repeated"),
6524    ];
6525
6526    #[test]
6527    fn test_string_view_to_binary_view() {
6528        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6529
6530        assert!(can_cast_types(
6531            string_view_array.data_type(),
6532            &DataType::BinaryView
6533        ));
6534
6535        let binary_view_array = cast(&string_view_array, &DataType::BinaryView).unwrap();
6536        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
6537
6538        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6539        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
6540    }
6541
6542    #[test]
6543    fn test_binary_view_to_string_view() {
6544        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6545
6546        assert!(can_cast_types(
6547            binary_view_array.data_type(),
6548            &DataType::Utf8View
6549        ));
6550
6551        let string_view_array = cast(&binary_view_array, &DataType::Utf8View).unwrap();
6552        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6553
6554        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6555        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
6556    }
6557
6558    #[test]
6559    fn test_binary_view_to_string_view_with_invalid_utf8() {
6560        let binary_view_array = BinaryViewArray::from_iter(vec![
6561            Some("valid".as_bytes()),
6562            Some(&[0xff]),
6563            Some("utf8".as_bytes()),
6564            None,
6565        ]);
6566
6567        let strict_options = CastOptions {
6568            safe: false,
6569            ..Default::default()
6570        };
6571
6572        assert!(
6573            cast_with_options(&binary_view_array, &DataType::Utf8View, &strict_options).is_err()
6574        );
6575
6576        let safe_options = CastOptions {
6577            safe: true,
6578            ..Default::default()
6579        };
6580
6581        let string_view_array =
6582            cast_with_options(&binary_view_array, &DataType::Utf8View, &safe_options).unwrap();
6583        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6584
6585        let values: Vec<_> = string_view_array.as_string_view().iter().collect();
6586
6587        assert_eq!(values, vec![Some("valid"), None, Some("utf8"), None]);
6588    }
6589
6590    #[test]
6591    fn test_string_to_view() {
6592        _test_string_to_view::<i32>();
6593        _test_string_to_view::<i64>();
6594    }
6595
6596    fn _test_string_to_view<O>()
6597    where
6598        O: OffsetSizeTrait,
6599    {
6600        let string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
6601
6602        assert!(can_cast_types(
6603            string_array.data_type(),
6604            &DataType::Utf8View
6605        ));
6606
6607        assert!(can_cast_types(
6608            string_array.data_type(),
6609            &DataType::BinaryView
6610        ));
6611
6612        let string_view_array = cast(&string_array, &DataType::Utf8View).unwrap();
6613        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6614
6615        let binary_view_array = cast(&string_array, &DataType::BinaryView).unwrap();
6616        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
6617
6618        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6619        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
6620
6621        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6622        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
6623    }
6624
6625    #[test]
6626    fn test_bianry_to_view() {
6627        _test_binary_to_view::<i32>();
6628        _test_binary_to_view::<i64>();
6629    }
6630
6631    fn _test_binary_to_view<O>()
6632    where
6633        O: OffsetSizeTrait,
6634    {
6635        let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
6636
6637        assert!(can_cast_types(
6638            binary_array.data_type(),
6639            &DataType::Utf8View
6640        ));
6641
6642        assert!(can_cast_types(
6643            binary_array.data_type(),
6644            &DataType::BinaryView
6645        ));
6646
6647        let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
6648        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6649
6650        let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
6651        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
6652
6653        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6654        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
6655
6656        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6657        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
6658    }
6659
6660    #[test]
6661    fn test_dict_to_view() {
6662        let values = StringArray::from_iter(VIEW_TEST_DATA);
6663        let keys = Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
6664        let string_dict_array =
6665            DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
6666        let typed_dict = string_dict_array.downcast_dict::<StringArray>().unwrap();
6667
6668        let string_view_array = {
6669            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
6670            for v in typed_dict.into_iter() {
6671                builder.append_option(v);
6672            }
6673            builder.finish()
6674        };
6675        let expected_string_array_type = string_view_array.data_type();
6676        let casted_string_array = cast(&string_dict_array, expected_string_array_type).unwrap();
6677        assert_eq!(casted_string_array.data_type(), expected_string_array_type);
6678        assert_eq!(casted_string_array.as_ref(), &string_view_array);
6679
6680        let binary_buffer = cast(&typed_dict.values(), &DataType::Binary).unwrap();
6681        let binary_dict_array =
6682            DictionaryArray::<Int8Type>::new(typed_dict.keys().clone(), binary_buffer);
6683        let typed_binary_dict = binary_dict_array.downcast_dict::<BinaryArray>().unwrap();
6684
6685        let binary_view_array = {
6686            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
6687            for v in typed_binary_dict.into_iter() {
6688                builder.append_option(v);
6689            }
6690            builder.finish()
6691        };
6692        let expected_binary_array_type = binary_view_array.data_type();
6693        let casted_binary_array = cast(&binary_dict_array, expected_binary_array_type).unwrap();
6694        assert_eq!(casted_binary_array.data_type(), expected_binary_array_type);
6695        assert_eq!(casted_binary_array.as_ref(), &binary_view_array);
6696    }
6697
6698    #[test]
6699    fn test_view_to_dict() {
6700        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6701        let string_dict_array: DictionaryArray<Int8Type> = VIEW_TEST_DATA.into_iter().collect();
6702        let casted_type = string_dict_array.data_type();
6703        let casted_dict_array = cast(&string_view_array, casted_type).unwrap();
6704        assert_eq!(casted_dict_array.data_type(), casted_type);
6705        assert_eq!(casted_dict_array.as_ref(), &string_dict_array);
6706
6707        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6708        let binary_dict_array = string_dict_array.downcast_dict::<StringArray>().unwrap();
6709        let binary_buffer = cast(&binary_dict_array.values(), &DataType::Binary).unwrap();
6710        let binary_dict_array =
6711            DictionaryArray::<Int8Type>::new(binary_dict_array.keys().clone(), binary_buffer);
6712        let casted_type = binary_dict_array.data_type();
6713        let casted_binary_array = cast(&binary_view_array, casted_type).unwrap();
6714        assert_eq!(casted_binary_array.data_type(), casted_type);
6715        assert_eq!(casted_binary_array.as_ref(), &binary_dict_array);
6716    }
6717
6718    #[test]
6719    fn test_view_to_string() {
6720        _test_view_to_string::<i32>();
6721        _test_view_to_string::<i64>();
6722    }
6723
6724    fn _test_view_to_string<O>()
6725    where
6726        O: OffsetSizeTrait,
6727    {
6728        let string_view_array = {
6729            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
6730            for s in VIEW_TEST_DATA.iter() {
6731                builder.append_option(*s);
6732            }
6733            builder.finish()
6734        };
6735
6736        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6737
6738        let expected_string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
6739        let expected_type = expected_string_array.data_type();
6740
6741        assert!(can_cast_types(string_view_array.data_type(), expected_type));
6742        assert!(can_cast_types(binary_view_array.data_type(), expected_type));
6743
6744        let string_view_casted_array = cast(&string_view_array, expected_type).unwrap();
6745        assert_eq!(string_view_casted_array.data_type(), expected_type);
6746        assert_eq!(string_view_casted_array.as_ref(), &expected_string_array);
6747
6748        let binary_view_casted_array = cast(&binary_view_array, expected_type).unwrap();
6749        assert_eq!(binary_view_casted_array.data_type(), expected_type);
6750        assert_eq!(binary_view_casted_array.as_ref(), &expected_string_array);
6751    }
6752
6753    #[test]
6754    fn test_view_to_binary() {
6755        _test_view_to_binary::<i32>();
6756        _test_view_to_binary::<i64>();
6757    }
6758
6759    fn _test_view_to_binary<O>()
6760    where
6761        O: OffsetSizeTrait,
6762    {
6763        let view_array = {
6764            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
6765            for s in VIEW_TEST_DATA.iter() {
6766                builder.append_option(*s);
6767            }
6768            builder.finish()
6769        };
6770
6771        let expected_binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
6772        let expected_type = expected_binary_array.data_type();
6773
6774        assert!(can_cast_types(view_array.data_type(), expected_type));
6775
6776        let binary_array = cast(&view_array, expected_type).unwrap();
6777        assert_eq!(binary_array.data_type(), expected_type);
6778
6779        assert_eq!(binary_array.as_ref(), &expected_binary_array);
6780    }
6781
6782    #[test]
6783    fn test_cast_from_f64() {
6784        let f64_values: Vec<f64> = vec![
6785            i64::MIN as f64,
6786            i32::MIN as f64,
6787            i16::MIN as f64,
6788            i8::MIN as f64,
6789            0_f64,
6790            u8::MAX as f64,
6791            u16::MAX as f64,
6792            u32::MAX as f64,
6793            u64::MAX as f64,
6794        ];
6795        let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values));
6796
6797        let f64_expected = vec![
6798            -9223372036854776000.0,
6799            -2147483648.0,
6800            -32768.0,
6801            -128.0,
6802            0.0,
6803            255.0,
6804            65535.0,
6805            4294967295.0,
6806            18446744073709552000.0,
6807        ];
6808        assert_eq!(
6809            f64_expected,
6810            get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
6811                .iter()
6812                .map(|i| i.parse::<f64>().unwrap())
6813                .collect::<Vec<f64>>()
6814        );
6815
6816        let f32_expected = vec![
6817            -9223372000000000000.0,
6818            -2147483600.0,
6819            -32768.0,
6820            -128.0,
6821            0.0,
6822            255.0,
6823            65535.0,
6824            4294967300.0,
6825            18446744000000000000.0,
6826        ];
6827        assert_eq!(
6828            f32_expected,
6829            get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
6830                .iter()
6831                .map(|i| i.parse::<f32>().unwrap())
6832                .collect::<Vec<f32>>()
6833        );
6834
6835        let f16_expected = vec![
6836            f16::from_f64(-9223372000000000000.0),
6837            f16::from_f64(-2147483600.0),
6838            f16::from_f64(-32768.0),
6839            f16::from_f64(-128.0),
6840            f16::from_f64(0.0),
6841            f16::from_f64(255.0),
6842            f16::from_f64(65535.0),
6843            f16::from_f64(4294967300.0),
6844            f16::from_f64(18446744000000000000.0),
6845        ];
6846        assert_eq!(
6847            f16_expected,
6848            get_cast_values::<Float16Type>(&f64_array, &DataType::Float16)
6849                .iter()
6850                .map(|i| i.parse::<f16>().unwrap())
6851                .collect::<Vec<f16>>()
6852        );
6853
6854        let i64_expected = vec![
6855            "-9223372036854775808",
6856            "-2147483648",
6857            "-32768",
6858            "-128",
6859            "0",
6860            "255",
6861            "65535",
6862            "4294967295",
6863            "null",
6864        ];
6865        assert_eq!(
6866            i64_expected,
6867            get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
6868        );
6869
6870        let i32_expected = vec![
6871            "null",
6872            "-2147483648",
6873            "-32768",
6874            "-128",
6875            "0",
6876            "255",
6877            "65535",
6878            "null",
6879            "null",
6880        ];
6881        assert_eq!(
6882            i32_expected,
6883            get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
6884        );
6885
6886        let i16_expected = vec![
6887            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
6888        ];
6889        assert_eq!(
6890            i16_expected,
6891            get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
6892        );
6893
6894        let i8_expected = vec![
6895            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
6896        ];
6897        assert_eq!(
6898            i8_expected,
6899            get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
6900        );
6901
6902        let u64_expected = vec![
6903            "null",
6904            "null",
6905            "null",
6906            "null",
6907            "0",
6908            "255",
6909            "65535",
6910            "4294967295",
6911            "null",
6912        ];
6913        assert_eq!(
6914            u64_expected,
6915            get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
6916        );
6917
6918        let u32_expected = vec![
6919            "null",
6920            "null",
6921            "null",
6922            "null",
6923            "0",
6924            "255",
6925            "65535",
6926            "4294967295",
6927            "null",
6928        ];
6929        assert_eq!(
6930            u32_expected,
6931            get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
6932        );
6933
6934        let u16_expected = vec![
6935            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
6936        ];
6937        assert_eq!(
6938            u16_expected,
6939            get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
6940        );
6941
6942        let u8_expected = vec![
6943            "null", "null", "null", "null", "0", "255", "null", "null", "null",
6944        ];
6945        assert_eq!(
6946            u8_expected,
6947            get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
6948        );
6949    }
6950
6951    #[test]
6952    fn test_cast_from_f32() {
6953        let f32_values: Vec<f32> = vec![
6954            i32::MIN as f32,
6955            i32::MIN as f32,
6956            i16::MIN as f32,
6957            i8::MIN as f32,
6958            0_f32,
6959            u8::MAX as f32,
6960            u16::MAX as f32,
6961            u32::MAX as f32,
6962            u32::MAX as f32,
6963        ];
6964        let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values));
6965
6966        let f64_expected = vec![
6967            "-2147483648.0",
6968            "-2147483648.0",
6969            "-32768.0",
6970            "-128.0",
6971            "0.0",
6972            "255.0",
6973            "65535.0",
6974            "4294967296.0",
6975            "4294967296.0",
6976        ];
6977        assert_eq!(
6978            f64_expected,
6979            get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
6980        );
6981
6982        let f32_expected = vec![
6983            "-2147483600.0",
6984            "-2147483600.0",
6985            "-32768.0",
6986            "-128.0",
6987            "0.0",
6988            "255.0",
6989            "65535.0",
6990            "4294967300.0",
6991            "4294967300.0",
6992        ];
6993        assert_eq!(
6994            f32_expected,
6995            get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
6996        );
6997
6998        let f16_expected = vec![
6999            "-inf", "-inf", "-32768.0", "-128.0", "0.0", "255.0", "inf", "inf", "inf",
7000        ];
7001        assert_eq!(
7002            f16_expected,
7003            get_cast_values::<Float16Type>(&f32_array, &DataType::Float16)
7004        );
7005
7006        let i64_expected = vec![
7007            "-2147483648",
7008            "-2147483648",
7009            "-32768",
7010            "-128",
7011            "0",
7012            "255",
7013            "65535",
7014            "4294967296",
7015            "4294967296",
7016        ];
7017        assert_eq!(
7018            i64_expected,
7019            get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
7020        );
7021
7022        let i32_expected = vec![
7023            "-2147483648",
7024            "-2147483648",
7025            "-32768",
7026            "-128",
7027            "0",
7028            "255",
7029            "65535",
7030            "null",
7031            "null",
7032        ];
7033        assert_eq!(
7034            i32_expected,
7035            get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
7036        );
7037
7038        let i16_expected = vec![
7039            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7040        ];
7041        assert_eq!(
7042            i16_expected,
7043            get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
7044        );
7045
7046        let i8_expected = vec![
7047            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7048        ];
7049        assert_eq!(
7050            i8_expected,
7051            get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
7052        );
7053
7054        let u64_expected = vec![
7055            "null",
7056            "null",
7057            "null",
7058            "null",
7059            "0",
7060            "255",
7061            "65535",
7062            "4294967296",
7063            "4294967296",
7064        ];
7065        assert_eq!(
7066            u64_expected,
7067            get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
7068        );
7069
7070        let u32_expected = vec![
7071            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7072        ];
7073        assert_eq!(
7074            u32_expected,
7075            get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
7076        );
7077
7078        let u16_expected = vec![
7079            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7080        ];
7081        assert_eq!(
7082            u16_expected,
7083            get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
7084        );
7085
7086        let u8_expected = vec![
7087            "null", "null", "null", "null", "0", "255", "null", "null", "null",
7088        ];
7089        assert_eq!(
7090            u8_expected,
7091            get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
7092        );
7093    }
7094
7095    #[test]
7096    fn test_cast_from_uint64() {
7097        let u64_values: Vec<u64> = vec![
7098            0,
7099            u8::MAX as u64,
7100            u16::MAX as u64,
7101            u32::MAX as u64,
7102            u64::MAX,
7103        ];
7104        let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values));
7105
7106        let f64_expected = vec![0.0, 255.0, 65535.0, 4294967295.0, 18446744073709552000.0];
7107        assert_eq!(
7108            f64_expected,
7109            get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
7110                .iter()
7111                .map(|i| i.parse::<f64>().unwrap())
7112                .collect::<Vec<f64>>()
7113        );
7114
7115        let f32_expected = vec![0.0, 255.0, 65535.0, 4294967300.0, 18446744000000000000.0];
7116        assert_eq!(
7117            f32_expected,
7118            get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
7119                .iter()
7120                .map(|i| i.parse::<f32>().unwrap())
7121                .collect::<Vec<f32>>()
7122        );
7123
7124        let f16_expected = vec![
7125            f16::from_f64(0.0),
7126            f16::from_f64(255.0),
7127            f16::from_f64(65535.0),
7128            f16::from_f64(4294967300.0),
7129            f16::from_f64(18446744000000000000.0),
7130        ];
7131        assert_eq!(
7132            f16_expected,
7133            get_cast_values::<Float16Type>(&u64_array, &DataType::Float16)
7134                .iter()
7135                .map(|i| i.parse::<f16>().unwrap())
7136                .collect::<Vec<f16>>()
7137        );
7138
7139        let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
7140        assert_eq!(
7141            i64_expected,
7142            get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
7143        );
7144
7145        let i32_expected = vec!["0", "255", "65535", "null", "null"];
7146        assert_eq!(
7147            i32_expected,
7148            get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
7149        );
7150
7151        let i16_expected = vec!["0", "255", "null", "null", "null"];
7152        assert_eq!(
7153            i16_expected,
7154            get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
7155        );
7156
7157        let i8_expected = vec!["0", "null", "null", "null", "null"];
7158        assert_eq!(
7159            i8_expected,
7160            get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
7161        );
7162
7163        let u64_expected = vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
7164        assert_eq!(
7165            u64_expected,
7166            get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
7167        );
7168
7169        let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
7170        assert_eq!(
7171            u32_expected,
7172            get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
7173        );
7174
7175        let u16_expected = vec!["0", "255", "65535", "null", "null"];
7176        assert_eq!(
7177            u16_expected,
7178            get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
7179        );
7180
7181        let u8_expected = vec!["0", "255", "null", "null", "null"];
7182        assert_eq!(
7183            u8_expected,
7184            get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
7185        );
7186    }
7187
7188    #[test]
7189    fn test_cast_from_uint32() {
7190        let u32_values: Vec<u32> = vec![0, u8::MAX as u32, u16::MAX as u32, u32::MAX];
7191        let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values));
7192
7193        let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
7194        assert_eq!(
7195            f64_expected,
7196            get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
7197        );
7198
7199        let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
7200        assert_eq!(
7201            f32_expected,
7202            get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
7203        );
7204
7205        let f16_expected = vec!["0.0", "255.0", "inf", "inf"];
7206        assert_eq!(
7207            f16_expected,
7208            get_cast_values::<Float16Type>(&u32_array, &DataType::Float16)
7209        );
7210
7211        let i64_expected = vec!["0", "255", "65535", "4294967295"];
7212        assert_eq!(
7213            i64_expected,
7214            get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
7215        );
7216
7217        let i32_expected = vec!["0", "255", "65535", "null"];
7218        assert_eq!(
7219            i32_expected,
7220            get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
7221        );
7222
7223        let i16_expected = vec!["0", "255", "null", "null"];
7224        assert_eq!(
7225            i16_expected,
7226            get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
7227        );
7228
7229        let i8_expected = vec!["0", "null", "null", "null"];
7230        assert_eq!(
7231            i8_expected,
7232            get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
7233        );
7234
7235        let u64_expected = vec!["0", "255", "65535", "4294967295"];
7236        assert_eq!(
7237            u64_expected,
7238            get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
7239        );
7240
7241        let u32_expected = vec!["0", "255", "65535", "4294967295"];
7242        assert_eq!(
7243            u32_expected,
7244            get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
7245        );
7246
7247        let u16_expected = vec!["0", "255", "65535", "null"];
7248        assert_eq!(
7249            u16_expected,
7250            get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
7251        );
7252
7253        let u8_expected = vec!["0", "255", "null", "null"];
7254        assert_eq!(
7255            u8_expected,
7256            get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
7257        );
7258    }
7259
7260    #[test]
7261    fn test_cast_from_uint16() {
7262        let u16_values: Vec<u16> = vec![0, u8::MAX as u16, u16::MAX];
7263        let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values));
7264
7265        let f64_expected = vec!["0.0", "255.0", "65535.0"];
7266        assert_eq!(
7267            f64_expected,
7268            get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
7269        );
7270
7271        let f32_expected = vec!["0.0", "255.0", "65535.0"];
7272        assert_eq!(
7273            f32_expected,
7274            get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
7275        );
7276
7277        let f16_expected = vec!["0.0", "255.0", "inf"];
7278        assert_eq!(
7279            f16_expected,
7280            get_cast_values::<Float16Type>(&u16_array, &DataType::Float16)
7281        );
7282
7283        let i64_expected = vec!["0", "255", "65535"];
7284        assert_eq!(
7285            i64_expected,
7286            get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
7287        );
7288
7289        let i32_expected = vec!["0", "255", "65535"];
7290        assert_eq!(
7291            i32_expected,
7292            get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
7293        );
7294
7295        let i16_expected = vec!["0", "255", "null"];
7296        assert_eq!(
7297            i16_expected,
7298            get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
7299        );
7300
7301        let i8_expected = vec!["0", "null", "null"];
7302        assert_eq!(
7303            i8_expected,
7304            get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
7305        );
7306
7307        let u64_expected = vec!["0", "255", "65535"];
7308        assert_eq!(
7309            u64_expected,
7310            get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
7311        );
7312
7313        let u32_expected = vec!["0", "255", "65535"];
7314        assert_eq!(
7315            u32_expected,
7316            get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
7317        );
7318
7319        let u16_expected = vec!["0", "255", "65535"];
7320        assert_eq!(
7321            u16_expected,
7322            get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
7323        );
7324
7325        let u8_expected = vec!["0", "255", "null"];
7326        assert_eq!(
7327            u8_expected,
7328            get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
7329        );
7330    }
7331
7332    #[test]
7333    fn test_cast_from_uint8() {
7334        let u8_values: Vec<u8> = vec![0, u8::MAX];
7335        let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values));
7336
7337        let f64_expected = vec!["0.0", "255.0"];
7338        assert_eq!(
7339            f64_expected,
7340            get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
7341        );
7342
7343        let f32_expected = vec!["0.0", "255.0"];
7344        assert_eq!(
7345            f32_expected,
7346            get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
7347        );
7348
7349        let f16_expected = vec!["0.0", "255.0"];
7350        assert_eq!(
7351            f16_expected,
7352            get_cast_values::<Float16Type>(&u8_array, &DataType::Float16)
7353        );
7354
7355        let i64_expected = vec!["0", "255"];
7356        assert_eq!(
7357            i64_expected,
7358            get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
7359        );
7360
7361        let i32_expected = vec!["0", "255"];
7362        assert_eq!(
7363            i32_expected,
7364            get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
7365        );
7366
7367        let i16_expected = vec!["0", "255"];
7368        assert_eq!(
7369            i16_expected,
7370            get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
7371        );
7372
7373        let i8_expected = vec!["0", "null"];
7374        assert_eq!(
7375            i8_expected,
7376            get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
7377        );
7378
7379        let u64_expected = vec!["0", "255"];
7380        assert_eq!(
7381            u64_expected,
7382            get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
7383        );
7384
7385        let u32_expected = vec!["0", "255"];
7386        assert_eq!(
7387            u32_expected,
7388            get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
7389        );
7390
7391        let u16_expected = vec!["0", "255"];
7392        assert_eq!(
7393            u16_expected,
7394            get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
7395        );
7396
7397        let u8_expected = vec!["0", "255"];
7398        assert_eq!(
7399            u8_expected,
7400            get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
7401        );
7402    }
7403
7404    #[test]
7405    fn test_cast_from_int64() {
7406        let i64_values: Vec<i64> = vec![
7407            i64::MIN,
7408            i32::MIN as i64,
7409            i16::MIN as i64,
7410            i8::MIN as i64,
7411            0,
7412            i8::MAX as i64,
7413            i16::MAX as i64,
7414            i32::MAX as i64,
7415            i64::MAX,
7416        ];
7417        let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values));
7418
7419        let f64_expected = vec![
7420            -9223372036854776000.0,
7421            -2147483648.0,
7422            -32768.0,
7423            -128.0,
7424            0.0,
7425            127.0,
7426            32767.0,
7427            2147483647.0,
7428            9223372036854776000.0,
7429        ];
7430        assert_eq!(
7431            f64_expected,
7432            get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
7433                .iter()
7434                .map(|i| i.parse::<f64>().unwrap())
7435                .collect::<Vec<f64>>()
7436        );
7437
7438        let f32_expected = vec![
7439            -9223372000000000000.0,
7440            -2147483600.0,
7441            -32768.0,
7442            -128.0,
7443            0.0,
7444            127.0,
7445            32767.0,
7446            2147483600.0,
7447            9223372000000000000.0,
7448        ];
7449        assert_eq!(
7450            f32_expected,
7451            get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
7452                .iter()
7453                .map(|i| i.parse::<f32>().unwrap())
7454                .collect::<Vec<f32>>()
7455        );
7456
7457        let f16_expected = vec![
7458            f16::from_f64(-9223372000000000000.0),
7459            f16::from_f64(-2147483600.0),
7460            f16::from_f64(-32768.0),
7461            f16::from_f64(-128.0),
7462            f16::from_f64(0.0),
7463            f16::from_f64(127.0),
7464            f16::from_f64(32767.0),
7465            f16::from_f64(2147483600.0),
7466            f16::from_f64(9223372000000000000.0),
7467        ];
7468        assert_eq!(
7469            f16_expected,
7470            get_cast_values::<Float16Type>(&i64_array, &DataType::Float16)
7471                .iter()
7472                .map(|i| i.parse::<f16>().unwrap())
7473                .collect::<Vec<f16>>()
7474        );
7475
7476        let i64_expected = vec![
7477            "-9223372036854775808",
7478            "-2147483648",
7479            "-32768",
7480            "-128",
7481            "0",
7482            "127",
7483            "32767",
7484            "2147483647",
7485            "9223372036854775807",
7486        ];
7487        assert_eq!(
7488            i64_expected,
7489            get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
7490        );
7491
7492        let i32_expected = vec![
7493            "null",
7494            "-2147483648",
7495            "-32768",
7496            "-128",
7497            "0",
7498            "127",
7499            "32767",
7500            "2147483647",
7501            "null",
7502        ];
7503        assert_eq!(
7504            i32_expected,
7505            get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
7506        );
7507
7508        assert_eq!(
7509            i32_expected,
7510            get_cast_values::<Date32Type>(&i64_array, &DataType::Date32)
7511        );
7512
7513        let i16_expected = vec![
7514            "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
7515        ];
7516        assert_eq!(
7517            i16_expected,
7518            get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
7519        );
7520
7521        let i8_expected = vec![
7522            "null", "null", "null", "-128", "0", "127", "null", "null", "null",
7523        ];
7524        assert_eq!(
7525            i8_expected,
7526            get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
7527        );
7528
7529        let u64_expected = vec![
7530            "null",
7531            "null",
7532            "null",
7533            "null",
7534            "0",
7535            "127",
7536            "32767",
7537            "2147483647",
7538            "9223372036854775807",
7539        ];
7540        assert_eq!(
7541            u64_expected,
7542            get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
7543        );
7544
7545        let u32_expected = vec![
7546            "null",
7547            "null",
7548            "null",
7549            "null",
7550            "0",
7551            "127",
7552            "32767",
7553            "2147483647",
7554            "null",
7555        ];
7556        assert_eq!(
7557            u32_expected,
7558            get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
7559        );
7560
7561        let u16_expected = vec![
7562            "null", "null", "null", "null", "0", "127", "32767", "null", "null",
7563        ];
7564        assert_eq!(
7565            u16_expected,
7566            get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
7567        );
7568
7569        let u8_expected = vec![
7570            "null", "null", "null", "null", "0", "127", "null", "null", "null",
7571        ];
7572        assert_eq!(
7573            u8_expected,
7574            get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
7575        );
7576    }
7577
7578    #[test]
7579    fn test_cast_from_int32() {
7580        let i32_values: Vec<i32> = vec![
7581            i32::MIN,
7582            i16::MIN as i32,
7583            i8::MIN as i32,
7584            0,
7585            i8::MAX as i32,
7586            i16::MAX as i32,
7587            i32::MAX,
7588        ];
7589        let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values));
7590
7591        let f64_expected = vec![
7592            "-2147483648.0",
7593            "-32768.0",
7594            "-128.0",
7595            "0.0",
7596            "127.0",
7597            "32767.0",
7598            "2147483647.0",
7599        ];
7600        assert_eq!(
7601            f64_expected,
7602            get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
7603        );
7604
7605        let f32_expected = vec![
7606            "-2147483600.0",
7607            "-32768.0",
7608            "-128.0",
7609            "0.0",
7610            "127.0",
7611            "32767.0",
7612            "2147483600.0",
7613        ];
7614        assert_eq!(
7615            f32_expected,
7616            get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
7617        );
7618
7619        let f16_expected = vec![
7620            f16::from_f64(-2147483600.0),
7621            f16::from_f64(-32768.0),
7622            f16::from_f64(-128.0),
7623            f16::from_f64(0.0),
7624            f16::from_f64(127.0),
7625            f16::from_f64(32767.0),
7626            f16::from_f64(2147483600.0),
7627        ];
7628        assert_eq!(
7629            f16_expected,
7630            get_cast_values::<Float16Type>(&i32_array, &DataType::Float16)
7631                .iter()
7632                .map(|i| i.parse::<f16>().unwrap())
7633                .collect::<Vec<f16>>()
7634        );
7635
7636        let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
7637        assert_eq!(
7638            i16_expected,
7639            get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
7640        );
7641
7642        let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
7643        assert_eq!(
7644            i8_expected,
7645            get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
7646        );
7647
7648        let u64_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
7649        assert_eq!(
7650            u64_expected,
7651            get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
7652        );
7653
7654        let u32_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
7655        assert_eq!(
7656            u32_expected,
7657            get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
7658        );
7659
7660        let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
7661        assert_eq!(
7662            u16_expected,
7663            get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
7664        );
7665
7666        let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
7667        assert_eq!(
7668            u8_expected,
7669            get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
7670        );
7671
7672        // The date32 to date64 cast increases the numerical values in order to keep the same dates.
7673        let i64_expected = vec![
7674            "-185542587187200000",
7675            "-2831155200000",
7676            "-11059200000",
7677            "0",
7678            "10972800000",
7679            "2831068800000",
7680            "185542587100800000",
7681        ];
7682        assert_eq!(
7683            i64_expected,
7684            get_cast_values::<Date64Type>(&i32_array, &DataType::Date64)
7685        );
7686    }
7687
7688    #[test]
7689    fn test_cast_from_int16() {
7690        let i16_values: Vec<i16> = vec![i16::MIN, i8::MIN as i16, 0, i8::MAX as i16, i16::MAX];
7691        let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values));
7692
7693        let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
7694        assert_eq!(
7695            f64_expected,
7696            get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
7697        );
7698
7699        let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
7700        assert_eq!(
7701            f32_expected,
7702            get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
7703        );
7704
7705        let f16_expected = vec![
7706            f16::from_f64(-32768.0),
7707            f16::from_f64(-128.0),
7708            f16::from_f64(0.0),
7709            f16::from_f64(127.0),
7710            f16::from_f64(32767.0),
7711        ];
7712        assert_eq!(
7713            f16_expected,
7714            get_cast_values::<Float16Type>(&i16_array, &DataType::Float16)
7715                .iter()
7716                .map(|i| i.parse::<f16>().unwrap())
7717                .collect::<Vec<f16>>()
7718        );
7719
7720        let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
7721        assert_eq!(
7722            i64_expected,
7723            get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
7724        );
7725
7726        let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
7727        assert_eq!(
7728            i32_expected,
7729            get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
7730        );
7731
7732        let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
7733        assert_eq!(
7734            i16_expected,
7735            get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
7736        );
7737
7738        let i8_expected = vec!["null", "-128", "0", "127", "null"];
7739        assert_eq!(
7740            i8_expected,
7741            get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
7742        );
7743
7744        let u64_expected = vec!["null", "null", "0", "127", "32767"];
7745        assert_eq!(
7746            u64_expected,
7747            get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
7748        );
7749
7750        let u32_expected = vec!["null", "null", "0", "127", "32767"];
7751        assert_eq!(
7752            u32_expected,
7753            get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
7754        );
7755
7756        let u16_expected = vec!["null", "null", "0", "127", "32767"];
7757        assert_eq!(
7758            u16_expected,
7759            get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
7760        );
7761
7762        let u8_expected = vec!["null", "null", "0", "127", "null"];
7763        assert_eq!(
7764            u8_expected,
7765            get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
7766        );
7767    }
7768
7769    #[test]
7770    fn test_cast_from_date32() {
7771        let i32_values: Vec<i32> = vec![
7772            i32::MIN,
7773            i16::MIN as i32,
7774            i8::MIN as i32,
7775            0,
7776            i8::MAX as i32,
7777            i16::MAX as i32,
7778            i32::MAX,
7779        ];
7780        let date32_array: ArrayRef = Arc::new(Date32Array::from(i32_values));
7781
7782        let i64_expected = vec![
7783            "-2147483648",
7784            "-32768",
7785            "-128",
7786            "0",
7787            "127",
7788            "32767",
7789            "2147483647",
7790        ];
7791        assert_eq!(
7792            i64_expected,
7793            get_cast_values::<Int64Type>(&date32_array, &DataType::Int64)
7794        );
7795    }
7796
7797    #[test]
7798    fn test_cast_from_int8() {
7799        let i8_values: Vec<i8> = vec![i8::MIN, 0, i8::MAX];
7800        let i8_array = Int8Array::from(i8_values);
7801
7802        let f64_expected = vec!["-128.0", "0.0", "127.0"];
7803        assert_eq!(
7804            f64_expected,
7805            get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
7806        );
7807
7808        let f32_expected = vec!["-128.0", "0.0", "127.0"];
7809        assert_eq!(
7810            f32_expected,
7811            get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
7812        );
7813
7814        let f16_expected = vec!["-128.0", "0.0", "127.0"];
7815        assert_eq!(
7816            f16_expected,
7817            get_cast_values::<Float16Type>(&i8_array, &DataType::Float16)
7818        );
7819
7820        let i64_expected = vec!["-128", "0", "127"];
7821        assert_eq!(
7822            i64_expected,
7823            get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
7824        );
7825
7826        let i32_expected = vec!["-128", "0", "127"];
7827        assert_eq!(
7828            i32_expected,
7829            get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
7830        );
7831
7832        let i16_expected = vec!["-128", "0", "127"];
7833        assert_eq!(
7834            i16_expected,
7835            get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
7836        );
7837
7838        let i8_expected = vec!["-128", "0", "127"];
7839        assert_eq!(
7840            i8_expected,
7841            get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
7842        );
7843
7844        let u64_expected = vec!["null", "0", "127"];
7845        assert_eq!(
7846            u64_expected,
7847            get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
7848        );
7849
7850        let u32_expected = vec!["null", "0", "127"];
7851        assert_eq!(
7852            u32_expected,
7853            get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
7854        );
7855
7856        let u16_expected = vec!["null", "0", "127"];
7857        assert_eq!(
7858            u16_expected,
7859            get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
7860        );
7861
7862        let u8_expected = vec!["null", "0", "127"];
7863        assert_eq!(
7864            u8_expected,
7865            get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
7866        );
7867    }
7868
7869    /// Convert `array` into a vector of strings by casting to data type dt
7870    fn get_cast_values<T>(array: &dyn Array, dt: &DataType) -> Vec<String>
7871    where
7872        T: ArrowPrimitiveType,
7873    {
7874        let c = cast(array, dt).unwrap();
7875        let a = c.as_primitive::<T>();
7876        let mut v: Vec<String> = vec![];
7877        for i in 0..array.len() {
7878            if a.is_null(i) {
7879                v.push("null".to_string())
7880            } else {
7881                v.push(format!("{:?}", a.value(i)));
7882            }
7883        }
7884        v
7885    }
7886
7887    #[test]
7888    fn test_cast_utf8_dict() {
7889        // FROM a dictionary with of Utf8 values
7890        let mut builder = StringDictionaryBuilder::<Int8Type>::new();
7891        builder.append("one").unwrap();
7892        builder.append_null();
7893        builder.append("three").unwrap();
7894        let array: ArrayRef = Arc::new(builder.finish());
7895
7896        let expected = vec!["one", "null", "three"];
7897
7898        // Test casting TO StringArray
7899        let cast_type = Utf8;
7900        let cast_array = cast(&array, &cast_type).expect("cast to UTF-8 failed");
7901        assert_eq!(cast_array.data_type(), &cast_type);
7902        assert_eq!(array_to_strings(&cast_array), expected);
7903
7904        // Test casting TO Dictionary (with different index sizes)
7905
7906        let cast_type = Dictionary(Box::new(Int16), Box::new(Utf8));
7907        let cast_array = cast(&array, &cast_type).expect("cast failed");
7908        assert_eq!(cast_array.data_type(), &cast_type);
7909        assert_eq!(array_to_strings(&cast_array), expected);
7910
7911        let cast_type = Dictionary(Box::new(Int32), Box::new(Utf8));
7912        let cast_array = cast(&array, &cast_type).expect("cast failed");
7913        assert_eq!(cast_array.data_type(), &cast_type);
7914        assert_eq!(array_to_strings(&cast_array), expected);
7915
7916        let cast_type = Dictionary(Box::new(Int64), Box::new(Utf8));
7917        let cast_array = cast(&array, &cast_type).expect("cast failed");
7918        assert_eq!(cast_array.data_type(), &cast_type);
7919        assert_eq!(array_to_strings(&cast_array), expected);
7920
7921        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
7922        let cast_array = cast(&array, &cast_type).expect("cast failed");
7923        assert_eq!(cast_array.data_type(), &cast_type);
7924        assert_eq!(array_to_strings(&cast_array), expected);
7925
7926        let cast_type = Dictionary(Box::new(UInt16), Box::new(Utf8));
7927        let cast_array = cast(&array, &cast_type).expect("cast failed");
7928        assert_eq!(cast_array.data_type(), &cast_type);
7929        assert_eq!(array_to_strings(&cast_array), expected);
7930
7931        let cast_type = Dictionary(Box::new(UInt32), Box::new(Utf8));
7932        let cast_array = cast(&array, &cast_type).expect("cast failed");
7933        assert_eq!(cast_array.data_type(), &cast_type);
7934        assert_eq!(array_to_strings(&cast_array), expected);
7935
7936        let cast_type = Dictionary(Box::new(UInt64), Box::new(Utf8));
7937        let cast_array = cast(&array, &cast_type).expect("cast failed");
7938        assert_eq!(cast_array.data_type(), &cast_type);
7939        assert_eq!(array_to_strings(&cast_array), expected);
7940    }
7941
7942    #[test]
7943    fn test_cast_dict_to_dict_bad_index_value_primitive() {
7944        // test converting from an array that has indexes of a type
7945        // that are out of bounds for a particular other kind of
7946        // index.
7947
7948        let mut builder = PrimitiveDictionaryBuilder::<Int32Type, Int64Type>::new();
7949
7950        // add 200 distinct values (which can be stored by a
7951        // dictionary indexed by int32, but not a dictionary indexed
7952        // with int8)
7953        for i in 0..200 {
7954            builder.append(i).unwrap();
7955        }
7956        let array: ArrayRef = Arc::new(builder.finish());
7957
7958        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
7959        let res = cast(&array, &cast_type);
7960        assert!(res.is_err());
7961        let actual_error = format!("{res:?}");
7962        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
7963        assert!(
7964            actual_error.contains(expected_error),
7965            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
7966        );
7967    }
7968
7969    #[test]
7970    fn test_cast_dict_to_dict_bad_index_value_utf8() {
7971        // Same test as test_cast_dict_to_dict_bad_index_value but use
7972        // string values (and encode the expected behavior here);
7973
7974        let mut builder = StringDictionaryBuilder::<Int32Type>::new();
7975
7976        // add 200 distinct values (which can be stored by a
7977        // dictionary indexed by int32, but not a dictionary indexed
7978        // with int8)
7979        for i in 0..200 {
7980            let val = format!("val{i}");
7981            builder.append(&val).unwrap();
7982        }
7983        let array = builder.finish();
7984
7985        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
7986        let res = cast(&array, &cast_type);
7987        assert!(res.is_err());
7988        let actual_error = format!("{res:?}");
7989        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
7990        assert!(
7991            actual_error.contains(expected_error),
7992            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
7993        );
7994    }
7995
7996    #[test]
7997    fn test_cast_primitive_dict() {
7998        // FROM a dictionary with of INT32 values
7999        let mut builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
8000        builder.append(1).unwrap();
8001        builder.append_null();
8002        builder.append(3).unwrap();
8003        let array: ArrayRef = Arc::new(builder.finish());
8004
8005        let expected = vec!["1", "null", "3"];
8006
8007        // Test casting TO PrimitiveArray, different dictionary type
8008        let cast_array = cast(&array, &Utf8).expect("cast to UTF-8 failed");
8009        assert_eq!(array_to_strings(&cast_array), expected);
8010        assert_eq!(cast_array.data_type(), &Utf8);
8011
8012        let cast_array = cast(&array, &Int64).expect("cast to int64 failed");
8013        assert_eq!(array_to_strings(&cast_array), expected);
8014        assert_eq!(cast_array.data_type(), &Int64);
8015    }
8016
8017    #[test]
8018    fn test_cast_primitive_array_to_dict() {
8019        let mut builder = PrimitiveBuilder::<Int32Type>::new();
8020        builder.append_value(1);
8021        builder.append_null();
8022        builder.append_value(3);
8023        let array: ArrayRef = Arc::new(builder.finish());
8024
8025        let expected = vec!["1", "null", "3"];
8026
8027        // Cast to a dictionary (same value type, Int32)
8028        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int32));
8029        let cast_array = cast(&array, &cast_type).expect("cast failed");
8030        assert_eq!(cast_array.data_type(), &cast_type);
8031        assert_eq!(array_to_strings(&cast_array), expected);
8032
8033        // Cast to a dictionary (different value type, Int8)
8034        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int8));
8035        let cast_array = cast(&array, &cast_type).expect("cast failed");
8036        assert_eq!(cast_array.data_type(), &cast_type);
8037        assert_eq!(array_to_strings(&cast_array), expected);
8038    }
8039
8040    #[test]
8041    fn test_cast_time_array_to_dict() {
8042        use DataType::*;
8043
8044        let array = Arc::new(Date32Array::from(vec![Some(1000), None, Some(2000)])) as ArrayRef;
8045
8046        let expected = vec!["1972-09-27", "null", "1975-06-24"];
8047
8048        let cast_type = Dictionary(Box::new(UInt8), Box::new(Date32));
8049        let cast_array = cast(&array, &cast_type).expect("cast failed");
8050        assert_eq!(cast_array.data_type(), &cast_type);
8051        assert_eq!(array_to_strings(&cast_array), expected);
8052    }
8053
8054    #[test]
8055    fn test_cast_timestamp_array_to_dict() {
8056        use DataType::*;
8057
8058        let array = Arc::new(
8059            TimestampSecondArray::from(vec![Some(1000), None, Some(2000)]).with_timezone_utc(),
8060        ) as ArrayRef;
8061
8062        let expected = vec!["1970-01-01T00:16:40", "null", "1970-01-01T00:33:20"];
8063
8064        let cast_type = Dictionary(Box::new(UInt8), Box::new(Timestamp(TimeUnit::Second, None)));
8065        let cast_array = cast(&array, &cast_type).expect("cast failed");
8066        assert_eq!(cast_array.data_type(), &cast_type);
8067        assert_eq!(array_to_strings(&cast_array), expected);
8068    }
8069
8070    #[test]
8071    fn test_cast_string_array_to_dict() {
8072        use DataType::*;
8073
8074        let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) as ArrayRef;
8075
8076        let expected = vec!["one", "null", "three"];
8077
8078        // Cast to a dictionary (same value type, Utf8)
8079        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8080        let cast_array = cast(&array, &cast_type).expect("cast failed");
8081        assert_eq!(cast_array.data_type(), &cast_type);
8082        assert_eq!(array_to_strings(&cast_array), expected);
8083    }
8084
8085    #[test]
8086    fn test_cast_null_array_to_from_decimal_array() {
8087        let data_type = DataType::Decimal128(12, 4);
8088        let array = new_null_array(&DataType::Null, 4);
8089        assert_eq!(array.data_type(), &DataType::Null);
8090        let cast_array = cast(&array, &data_type).expect("cast failed");
8091        assert_eq!(cast_array.data_type(), &data_type);
8092        for i in 0..4 {
8093            assert!(cast_array.is_null(i));
8094        }
8095
8096        let array = new_null_array(&data_type, 4);
8097        assert_eq!(array.data_type(), &data_type);
8098        let cast_array = cast(&array, &DataType::Null).expect("cast failed");
8099        assert_eq!(cast_array.data_type(), &DataType::Null);
8100        assert_eq!(cast_array.len(), 4);
8101        assert_eq!(cast_array.logical_nulls().unwrap().null_count(), 4);
8102    }
8103
8104    #[test]
8105    fn test_cast_null_array_from_and_to_primitive_array() {
8106        macro_rules! typed_test {
8107            ($ARR_TYPE:ident, $DATATYPE:ident, $TYPE:tt) => {{
8108                {
8109                    let array = Arc::new(NullArray::new(6)) as ArrayRef;
8110                    let expected = $ARR_TYPE::from(vec![None; 6]);
8111                    let cast_type = DataType::$DATATYPE;
8112                    let cast_array = cast(&array, &cast_type).expect("cast failed");
8113                    let cast_array = cast_array.as_primitive::<$TYPE>();
8114                    assert_eq!(cast_array.data_type(), &cast_type);
8115                    assert_eq!(cast_array, &expected);
8116                }
8117            }};
8118        }
8119
8120        typed_test!(Int16Array, Int16, Int16Type);
8121        typed_test!(Int32Array, Int32, Int32Type);
8122        typed_test!(Int64Array, Int64, Int64Type);
8123
8124        typed_test!(UInt16Array, UInt16, UInt16Type);
8125        typed_test!(UInt32Array, UInt32, UInt32Type);
8126        typed_test!(UInt64Array, UInt64, UInt64Type);
8127
8128        typed_test!(Float16Array, Float16, Float16Type);
8129        typed_test!(Float32Array, Float32, Float32Type);
8130        typed_test!(Float64Array, Float64, Float64Type);
8131
8132        typed_test!(Date32Array, Date32, Date32Type);
8133        typed_test!(Date64Array, Date64, Date64Type);
8134    }
8135
8136    fn cast_from_null_to_other(data_type: &DataType) {
8137        // Cast from null to data_type
8138        {
8139            let array = new_null_array(&DataType::Null, 4);
8140            assert_eq!(array.data_type(), &DataType::Null);
8141            let cast_array = cast(&array, data_type).expect("cast failed");
8142            assert_eq!(cast_array.data_type(), data_type);
8143            for i in 0..4 {
8144                assert!(cast_array.is_null(i));
8145            }
8146        }
8147    }
8148
8149    #[test]
8150    fn test_cast_null_from_and_to_variable_sized() {
8151        cast_from_null_to_other(&DataType::Utf8);
8152        cast_from_null_to_other(&DataType::LargeUtf8);
8153        cast_from_null_to_other(&DataType::Binary);
8154        cast_from_null_to_other(&DataType::LargeBinary);
8155    }
8156
8157    #[test]
8158    fn test_cast_null_from_and_to_nested_type() {
8159        // Cast null from and to map
8160        let data_type = DataType::Map(
8161            Arc::new(Field::new_struct(
8162                "entry",
8163                vec![
8164                    Field::new("key", DataType::Utf8, false),
8165                    Field::new("value", DataType::Int32, true),
8166                ],
8167                false,
8168            )),
8169            false,
8170        );
8171        cast_from_null_to_other(&data_type);
8172
8173        // Cast null from and to list
8174        let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
8175        cast_from_null_to_other(&data_type);
8176        let data_type = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8177        cast_from_null_to_other(&data_type);
8178        let data_type =
8179            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
8180        cast_from_null_to_other(&data_type);
8181
8182        // Cast null from and to dictionary
8183        let values = vec![None, None, None, None] as Vec<Option<&str>>;
8184        let array: DictionaryArray<Int8Type> = values.into_iter().collect();
8185        let array = Arc::new(array) as ArrayRef;
8186        let data_type = array.data_type().to_owned();
8187        cast_from_null_to_other(&data_type);
8188
8189        // Cast null from and to struct
8190        let data_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
8191        cast_from_null_to_other(&data_type);
8192    }
8193
8194    /// Print the `DictionaryArray` `array` as a vector of strings
8195    fn array_to_strings(array: &ArrayRef) -> Vec<String> {
8196        let options = FormatOptions::new().with_null("null");
8197        let formatter = ArrayFormatter::try_new(array.as_ref(), &options).unwrap();
8198        (0..array.len())
8199            .map(|i| formatter.value(i).to_string())
8200            .collect()
8201    }
8202
8203    #[test]
8204    fn test_cast_utf8_to_date32() {
8205        use chrono::NaiveDate;
8206        let from_ymd = chrono::NaiveDate::from_ymd_opt;
8207        let since = chrono::NaiveDate::signed_duration_since;
8208
8209        let a = StringArray::from(vec![
8210            "2000-01-01",          // valid date with leading 0s
8211            "2000-01-01T12:00:00", // valid datetime, will throw away the time part
8212            "2000-2-2",            // valid date without leading 0s
8213            "2000-00-00",          // invalid month and day
8214            "2000",                // just a year is invalid
8215        ]);
8216        let array = Arc::new(a) as ArrayRef;
8217        let b = cast(&array, &DataType::Date32).unwrap();
8218        let c = b.as_primitive::<Date32Type>();
8219
8220        // test valid inputs
8221        let date_value = since(
8222            NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
8223            from_ymd(1970, 1, 1).unwrap(),
8224        )
8225        .num_days() as i32;
8226        assert!(c.is_valid(0)); // "2000-01-01"
8227        assert_eq!(date_value, c.value(0));
8228
8229        assert!(c.is_valid(1)); // "2000-01-01T12:00:00"
8230        assert_eq!(date_value, c.value(1));
8231
8232        let date_value = since(
8233            NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
8234            from_ymd(1970, 1, 1).unwrap(),
8235        )
8236        .num_days() as i32;
8237        assert!(c.is_valid(2)); // "2000-2-2"
8238        assert_eq!(date_value, c.value(2));
8239
8240        // test invalid inputs
8241        assert!(!c.is_valid(3)); // "2000-00-00"
8242        assert!(!c.is_valid(4)); // "2000"
8243    }
8244
8245    #[test]
8246    fn test_cast_utf8_to_date64() {
8247        let a = StringArray::from(vec![
8248            "2000-01-01T12:00:00", // date + time valid
8249            "2020-12-15T12:34:56", // date + time valid
8250            "2020-2-2T12:34:56",   // valid date time without leading 0s
8251            "2000-00-00T12:00:00", // invalid month and day
8252            "2000-01-01 12:00:00", // missing the 'T'
8253            "2000-01-01",          // just a date is invalid
8254        ]);
8255        let array = Arc::new(a) as ArrayRef;
8256        let b = cast(&array, &DataType::Date64).unwrap();
8257        let c = b.as_primitive::<Date64Type>();
8258
8259        // test valid inputs
8260        assert!(c.is_valid(0)); // "2000-01-01T12:00:00"
8261        assert_eq!(946728000000, c.value(0));
8262        assert!(c.is_valid(1)); // "2020-12-15T12:34:56"
8263        assert_eq!(1608035696000, c.value(1));
8264        assert!(!c.is_valid(2)); // "2020-2-2T12:34:56"
8265
8266        assert!(!c.is_valid(3)); // "2000-00-00T12:00:00"
8267        assert!(c.is_valid(4)); // "2000-01-01 12:00:00"
8268        assert_eq!(946728000000, c.value(4));
8269        assert!(c.is_valid(5)); // "2000-01-01"
8270        assert_eq!(946684800000, c.value(5));
8271    }
8272
8273    #[test]
8274    fn test_can_cast_fsl_to_fsl() {
8275        let from_array = Arc::new(
8276            FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
8277                [Some([Some(1.0), Some(2.0)]), None],
8278                2,
8279            ),
8280        ) as ArrayRef;
8281        let to_array = Arc::new(
8282            FixedSizeListArray::from_iter_primitive::<Float16Type, _, _>(
8283                [
8284                    Some([Some(f16::from_f32(1.0)), Some(f16::from_f32(2.0))]),
8285                    None,
8286                ],
8287                2,
8288            ),
8289        ) as ArrayRef;
8290
8291        assert!(can_cast_types(from_array.data_type(), to_array.data_type()));
8292        let actual = cast(&from_array, to_array.data_type()).unwrap();
8293        assert_eq!(actual.data_type(), to_array.data_type());
8294
8295        let invalid_target =
8296            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Binary, true)), 2);
8297        assert!(!can_cast_types(from_array.data_type(), &invalid_target));
8298
8299        let invalid_size =
8300            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Float16, true)), 5);
8301        assert!(!can_cast_types(from_array.data_type(), &invalid_size));
8302    }
8303
8304    #[test]
8305    fn test_can_cast_types_fixed_size_list_to_list() {
8306        // DataType::List
8307        let array1 = Arc::new(make_fixed_size_list_array()) as ArrayRef;
8308        assert!(can_cast_types(
8309            array1.data_type(),
8310            &DataType::List(Arc::new(Field::new("", DataType::Int32, false)))
8311        ));
8312
8313        // DataType::LargeList
8314        let array2 = Arc::new(make_fixed_size_list_array_for_large_list()) as ArrayRef;
8315        assert!(can_cast_types(
8316            array2.data_type(),
8317            &DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false)))
8318        ));
8319    }
8320
8321    #[test]
8322    fn test_cast_fixed_size_list_to_list() {
8323        // Important cases:
8324        // 1. With/without nulls
8325        // 2. LargeList and List
8326        // 3. With and without inner casts
8327
8328        let cases = [
8329            // fixed_size_list<i32, 2> => list<i32>
8330            (
8331                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8332                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8333                    2,
8334                )) as ArrayRef,
8335                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
8336                    Some([Some(1), Some(1)]),
8337                    Some([Some(2), Some(2)]),
8338                ])) as ArrayRef,
8339            ),
8340            // fixed_size_list<i32, 2> => list<i32> (nullable)
8341            (
8342                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8343                    [None, Some([Some(2), Some(2)])],
8344                    2,
8345                )) as ArrayRef,
8346                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
8347                    None,
8348                    Some([Some(2), Some(2)]),
8349                ])) as ArrayRef,
8350            ),
8351            // fixed_size_list<i32, 2> => large_list<i64>
8352            (
8353                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8354                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8355                    2,
8356                )) as ArrayRef,
8357                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
8358                    Some([Some(1), Some(1)]),
8359                    Some([Some(2), Some(2)]),
8360                ])) as ArrayRef,
8361            ),
8362            // fixed_size_list<i32, 2> => large_list<i64> (nullable)
8363            (
8364                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8365                    [None, Some([Some(2), Some(2)])],
8366                    2,
8367                )) as ArrayRef,
8368                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
8369                    None,
8370                    Some([Some(2), Some(2)]),
8371                ])) as ArrayRef,
8372            ),
8373        ];
8374
8375        for (array, expected) in cases {
8376            let array = Arc::new(array) as ArrayRef;
8377
8378            assert!(
8379                can_cast_types(array.data_type(), expected.data_type()),
8380                "can_cast_types claims we cannot cast {:?} to {:?}",
8381                array.data_type(),
8382                expected.data_type()
8383            );
8384
8385            let list_array = cast(&array, expected.data_type())
8386                .unwrap_or_else(|_| panic!("Failed to cast {array:?} to {expected:?}"));
8387            assert_eq!(
8388                list_array.as_ref(),
8389                &expected,
8390                "Incorrect result from casting {array:?} to {expected:?}",
8391            );
8392        }
8393    }
8394
8395    #[test]
8396    fn test_cast_utf8_to_list() {
8397        // DataType::List
8398        let array = Arc::new(StringArray::from(vec!["5"])) as ArrayRef;
8399        let field = Arc::new(Field::new("", DataType::Int32, false));
8400        let list_array = cast(&array, &DataType::List(field.clone())).unwrap();
8401        let actual = list_array.as_list_opt::<i32>().unwrap();
8402        let expect = ListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
8403        assert_eq!(&expect.value(0), &actual.value(0));
8404
8405        // DataType::LargeList
8406        let list_array = cast(&array, &DataType::LargeList(field.clone())).unwrap();
8407        let actual = list_array.as_list_opt::<i64>().unwrap();
8408        let expect = LargeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
8409        assert_eq!(&expect.value(0), &actual.value(0));
8410
8411        // DataType::FixedSizeList
8412        let list_array = cast(&array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
8413        let actual = list_array.as_fixed_size_list_opt().unwrap();
8414        let expect =
8415            FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])], 1);
8416        assert_eq!(&expect.value(0), &actual.value(0));
8417    }
8418
8419    #[test]
8420    fn test_cast_single_element_fixed_size_list() {
8421        // FixedSizeList<T>[1] => T
8422        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
8423            [(Some([Some(5)]))],
8424            1,
8425        )) as ArrayRef;
8426        let casted_array = cast(&from_array, &DataType::Int32).unwrap();
8427        let actual: &Int32Array = casted_array.as_primitive();
8428        let expected = Int32Array::from(vec![Some(5)]);
8429        assert_eq!(&expected, actual);
8430
8431        // FixedSizeList<T>[1] => FixedSizeList<U>[1]
8432        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
8433            [(Some([Some(5)]))],
8434            1,
8435        )) as ArrayRef;
8436        let to_field = Arc::new(Field::new("dummy", DataType::Float32, false));
8437        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
8438        let expected = Arc::new(FixedSizeListArray::new(
8439            to_field.clone(),
8440            1,
8441            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
8442            None,
8443        )) as ArrayRef;
8444        assert_eq!(*expected, *actual);
8445
8446        // FixedSizeList<T>[1] => FixedSizeList<FixdSizedList<U>[1]>[1]
8447        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
8448            [(Some([Some(5)]))],
8449            1,
8450        )) as ArrayRef;
8451        let to_field_inner = Arc::new(Field::new_list_field(DataType::Float32, false));
8452        let to_field = Arc::new(Field::new(
8453            "dummy",
8454            DataType::FixedSizeList(to_field_inner.clone(), 1),
8455            false,
8456        ));
8457        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
8458        let expected = Arc::new(FixedSizeListArray::new(
8459            to_field.clone(),
8460            1,
8461            Arc::new(FixedSizeListArray::new(
8462                to_field_inner.clone(),
8463                1,
8464                Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
8465                None,
8466            )) as ArrayRef,
8467            None,
8468        )) as ArrayRef;
8469        assert_eq!(*expected, *actual);
8470
8471        // T => FixedSizeList<T>[1] (non-nullable)
8472        let field = Arc::new(Field::new("dummy", DataType::Float32, false));
8473        let from_array = Arc::new(Int8Array::from(vec![Some(5)])) as ArrayRef;
8474        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
8475        let actual = casted_array.as_fixed_size_list();
8476        let expected = Arc::new(FixedSizeListArray::new(
8477            field.clone(),
8478            1,
8479            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
8480            None,
8481        )) as ArrayRef;
8482        assert_eq!(expected.as_ref(), actual);
8483
8484        // T => FixedSizeList<T>[1] (nullable)
8485        let field = Arc::new(Field::new("nullable", DataType::Float32, true));
8486        let from_array = Arc::new(Int8Array::from(vec![None])) as ArrayRef;
8487        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
8488        let actual = casted_array.as_fixed_size_list();
8489        let expected = Arc::new(FixedSizeListArray::new(
8490            field.clone(),
8491            1,
8492            Arc::new(Float32Array::from(vec![None])) as ArrayRef,
8493            None,
8494        )) as ArrayRef;
8495        assert_eq!(expected.as_ref(), actual);
8496    }
8497
8498    #[test]
8499    fn test_cast_list_containers() {
8500        // large-list to list
8501        let array = Arc::new(make_large_list_array()) as ArrayRef;
8502        let list_array = cast(
8503            &array,
8504            &DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
8505        )
8506        .unwrap();
8507        let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
8508        let expected = array.as_any().downcast_ref::<LargeListArray>().unwrap();
8509
8510        assert_eq!(&expected.value(0), &actual.value(0));
8511        assert_eq!(&expected.value(1), &actual.value(1));
8512        assert_eq!(&expected.value(2), &actual.value(2));
8513
8514        // list to large-list
8515        let array = Arc::new(make_list_array()) as ArrayRef;
8516        let large_list_array = cast(
8517            &array,
8518            &DataType::LargeList(Arc::new(Field::new("", DataType::Int32, false))),
8519        )
8520        .unwrap();
8521        let actual = large_list_array
8522            .as_any()
8523            .downcast_ref::<LargeListArray>()
8524            .unwrap();
8525        let expected = array.as_any().downcast_ref::<ListArray>().unwrap();
8526
8527        assert_eq!(&expected.value(0), &actual.value(0));
8528        assert_eq!(&expected.value(1), &actual.value(1));
8529        assert_eq!(&expected.value(2), &actual.value(2));
8530    }
8531
8532    #[test]
8533    fn test_cast_list_to_fsl() {
8534        // There four noteworthy cases we should handle:
8535        // 1. No nulls
8536        // 2. Nulls that are always empty
8537        // 3. Nulls that have varying lengths
8538        // 4. Nulls that are correctly sized (same as target list size)
8539
8540        // Non-null case
8541        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
8542        let values = vec![
8543            Some(vec![Some(1), Some(2), Some(3)]),
8544            Some(vec![Some(4), Some(5), Some(6)]),
8545        ];
8546        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
8547            values.clone(),
8548        )) as ArrayRef;
8549        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8550            values, 3,
8551        )) as ArrayRef;
8552        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
8553        assert_eq!(expected.as_ref(), actual.as_ref());
8554
8555        // Null cases
8556        // Array is [[1, 2, 3], null, [4, 5, 6], null]
8557        let cases = [
8558            (
8559                // Zero-length nulls
8560                vec![1, 2, 3, 4, 5, 6],
8561                vec![3, 0, 3, 0],
8562            ),
8563            (
8564                // Varying-length nulls
8565                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
8566                vec![3, 2, 3, 1],
8567            ),
8568            (
8569                // Correctly-sized nulls
8570                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
8571                vec![3, 3, 3, 3],
8572            ),
8573            (
8574                // Mixed nulls
8575                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
8576                vec![3, 0, 3, 3],
8577            ),
8578        ];
8579        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
8580
8581        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8582            vec![
8583                Some(vec![Some(1), Some(2), Some(3)]),
8584                None,
8585                Some(vec![Some(4), Some(5), Some(6)]),
8586                None,
8587            ],
8588            3,
8589        )) as ArrayRef;
8590
8591        for (values, lengths) in cases.iter() {
8592            let array = Arc::new(ListArray::new(
8593                field.clone(),
8594                OffsetBuffer::from_lengths(lengths.clone()),
8595                Arc::new(Int32Array::from(values.clone())),
8596                Some(null_buffer.clone()),
8597            )) as ArrayRef;
8598            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
8599            assert_eq!(expected.as_ref(), actual.as_ref());
8600        }
8601    }
8602
8603    #[test]
8604    fn test_cast_list_to_fsl_safety() {
8605        let values = vec![
8606            Some(vec![Some(1), Some(2), Some(3)]),
8607            Some(vec![Some(4), Some(5)]),
8608            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
8609            Some(vec![Some(3), Some(4), Some(5)]),
8610        ];
8611        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
8612            values.clone(),
8613        )) as ArrayRef;
8614
8615        let res = cast_with_options(
8616            array.as_ref(),
8617            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
8618            &CastOptions {
8619                safe: false,
8620                ..Default::default()
8621            },
8622        );
8623        assert!(res.is_err());
8624        assert!(
8625            format!("{res:?}")
8626                .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
8627        );
8628
8629        // When safe=true (default), the cast will fill nulls for lists that are
8630        // too short and truncate lists that are too long.
8631        let res = cast(
8632            array.as_ref(),
8633            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
8634        )
8635        .unwrap();
8636        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8637            vec![
8638                Some(vec![Some(1), Some(2), Some(3)]),
8639                None, // Too short -> replaced with null
8640                None, // Too long -> replaced with null
8641                Some(vec![Some(3), Some(4), Some(5)]),
8642            ],
8643            3,
8644        )) as ArrayRef;
8645        assert_eq!(expected.as_ref(), res.as_ref());
8646
8647        // The safe option is false and the source array contains a null list.
8648        // issue: https://github.com/apache/arrow-rs/issues/5642
8649        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
8650            Some(vec![Some(1), Some(2), Some(3)]),
8651            None,
8652        ])) as ArrayRef;
8653        let res = cast_with_options(
8654            array.as_ref(),
8655            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
8656            &CastOptions {
8657                safe: false,
8658                ..Default::default()
8659            },
8660        )
8661        .unwrap();
8662        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8663            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
8664            3,
8665        )) as ArrayRef;
8666        assert_eq!(expected.as_ref(), res.as_ref());
8667    }
8668
8669    #[test]
8670    fn test_cast_large_list_to_fsl() {
8671        let values = vec![Some(vec![Some(1), Some(2)]), Some(vec![Some(3), Some(4)])];
8672        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
8673            values.clone(),
8674        )) as ArrayRef;
8675        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8676            values, 2,
8677        )) as ArrayRef;
8678        let actual = cast(
8679            array.as_ref(),
8680            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 2),
8681        )
8682        .unwrap();
8683        assert_eq!(expected.as_ref(), actual.as_ref());
8684    }
8685
8686    #[test]
8687    fn test_cast_list_to_fsl_subcast() {
8688        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
8689            vec![
8690                Some(vec![Some(1), Some(2)]),
8691                Some(vec![Some(3), Some(i32::MAX)]),
8692            ],
8693        )) as ArrayRef;
8694        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
8695            vec![
8696                Some(vec![Some(1), Some(2)]),
8697                Some(vec![Some(3), Some(i32::MAX as i64)]),
8698            ],
8699            2,
8700        )) as ArrayRef;
8701        let actual = cast(
8702            array.as_ref(),
8703            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 2),
8704        )
8705        .unwrap();
8706        assert_eq!(expected.as_ref(), actual.as_ref());
8707
8708        let res = cast_with_options(
8709            array.as_ref(),
8710            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int16, true)), 2),
8711            &CastOptions {
8712                safe: false,
8713                ..Default::default()
8714            },
8715        );
8716        assert!(res.is_err());
8717        assert!(format!("{res:?}").contains("Can't cast value 2147483647 to type Int16"));
8718    }
8719
8720    #[test]
8721    fn test_cast_list_to_fsl_empty() {
8722        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
8723        let array = new_empty_array(&DataType::List(field.clone()));
8724
8725        let target_type = DataType::FixedSizeList(field.clone(), 3);
8726        let expected = new_empty_array(&target_type);
8727
8728        let actual = cast(array.as_ref(), &target_type).unwrap();
8729        assert_eq!(expected.as_ref(), actual.as_ref());
8730    }
8731
8732    fn make_list_array() -> ListArray {
8733        // Construct a value array
8734        let value_data = ArrayData::builder(DataType::Int32)
8735            .len(8)
8736            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8737            .build()
8738            .unwrap();
8739
8740        // Construct a buffer for value offsets, for the nested array:
8741        //  [[0, 1, 2], [3, 4, 5], [6, 7]]
8742        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
8743
8744        // Construct a list array from the above two
8745        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
8746        let list_data = ArrayData::builder(list_data_type)
8747            .len(3)
8748            .add_buffer(value_offsets)
8749            .add_child_data(value_data)
8750            .build()
8751            .unwrap();
8752        ListArray::from(list_data)
8753    }
8754
8755    fn make_large_list_array() -> LargeListArray {
8756        // Construct a value array
8757        let value_data = ArrayData::builder(DataType::Int32)
8758            .len(8)
8759            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8760            .build()
8761            .unwrap();
8762
8763        // Construct a buffer for value offsets, for the nested array:
8764        //  [[0, 1, 2], [3, 4, 5], [6, 7]]
8765        let value_offsets = Buffer::from_slice_ref([0i64, 3, 6, 8]);
8766
8767        // Construct a list array from the above two
8768        let list_data_type =
8769            DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8770        let list_data = ArrayData::builder(list_data_type)
8771            .len(3)
8772            .add_buffer(value_offsets)
8773            .add_child_data(value_data)
8774            .build()
8775            .unwrap();
8776        LargeListArray::from(list_data)
8777    }
8778
8779    fn make_fixed_size_list_array() -> FixedSizeListArray {
8780        // Construct a value array
8781        let value_data = ArrayData::builder(DataType::Int32)
8782            .len(8)
8783            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8784            .build()
8785            .unwrap();
8786
8787        let list_data_type =
8788            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
8789        let list_data = ArrayData::builder(list_data_type)
8790            .len(2)
8791            .add_child_data(value_data)
8792            .build()
8793            .unwrap();
8794        FixedSizeListArray::from(list_data)
8795    }
8796
8797    fn make_fixed_size_list_array_for_large_list() -> FixedSizeListArray {
8798        // Construct a value array
8799        let value_data = ArrayData::builder(DataType::Int64)
8800            .len(8)
8801            .add_buffer(Buffer::from_slice_ref([0i64, 1, 2, 3, 4, 5, 6, 7]))
8802            .build()
8803            .unwrap();
8804
8805        let list_data_type =
8806            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 4);
8807        let list_data = ArrayData::builder(list_data_type)
8808            .len(2)
8809            .add_child_data(value_data)
8810            .build()
8811            .unwrap();
8812        FixedSizeListArray::from(list_data)
8813    }
8814
8815    #[test]
8816    fn test_cast_map_dont_allow_change_of_order() {
8817        let string_builder = StringBuilder::new();
8818        let value_builder = StringBuilder::new();
8819        let mut builder = MapBuilder::new(
8820            Some(MapFieldNames {
8821                entry: "entries".to_string(),
8822                key: "key".to_string(),
8823                value: "value".to_string(),
8824            }),
8825            string_builder,
8826            value_builder,
8827        );
8828
8829        builder.keys().append_value("0");
8830        builder.values().append_value("test_val_1");
8831        builder.append(true).unwrap();
8832        builder.keys().append_value("1");
8833        builder.values().append_value("test_val_2");
8834        builder.append(true).unwrap();
8835
8836        // map builder returns unsorted map by default
8837        let array = builder.finish();
8838
8839        let new_ordered = true;
8840        let new_type = DataType::Map(
8841            Arc::new(Field::new(
8842                "entries",
8843                DataType::Struct(
8844                    vec![
8845                        Field::new("key", DataType::Utf8, false),
8846                        Field::new("value", DataType::Utf8, false),
8847                    ]
8848                    .into(),
8849                ),
8850                false,
8851            )),
8852            new_ordered,
8853        );
8854
8855        let new_array_result = cast(&array, &new_type.clone());
8856        assert!(!can_cast_types(array.data_type(), &new_type));
8857        let Err(ArrowError::CastError(t)) = new_array_result else {
8858            panic!();
8859        };
8860        assert_eq!(
8861            t,
8862            r#"Casting from Map("entries": non-null Struct("key": non-null Utf8, "value": Utf8), unsorted) to Map("entries": non-null Struct("key": non-null Utf8, "value": non-null Utf8), sorted) not supported"#
8863        );
8864    }
8865
8866    #[test]
8867    fn test_cast_map_dont_allow_when_container_cant_cast() {
8868        let string_builder = StringBuilder::new();
8869        let value_builder = IntervalDayTimeArray::builder(2);
8870        let mut builder = MapBuilder::new(
8871            Some(MapFieldNames {
8872                entry: "entries".to_string(),
8873                key: "key".to_string(),
8874                value: "value".to_string(),
8875            }),
8876            string_builder,
8877            value_builder,
8878        );
8879
8880        builder.keys().append_value("0");
8881        builder.values().append_value(IntervalDayTime::new(1, 1));
8882        builder.append(true).unwrap();
8883        builder.keys().append_value("1");
8884        builder.values().append_value(IntervalDayTime::new(2, 2));
8885        builder.append(true).unwrap();
8886
8887        // map builder returns unsorted map by default
8888        let array = builder.finish();
8889
8890        let new_ordered = true;
8891        let new_type = DataType::Map(
8892            Arc::new(Field::new(
8893                "entries",
8894                DataType::Struct(
8895                    vec![
8896                        Field::new("key", DataType::Utf8, false),
8897                        Field::new("value", DataType::Duration(TimeUnit::Second), false),
8898                    ]
8899                    .into(),
8900                ),
8901                false,
8902            )),
8903            new_ordered,
8904        );
8905
8906        let new_array_result = cast(&array, &new_type.clone());
8907        assert!(!can_cast_types(array.data_type(), &new_type));
8908        let Err(ArrowError::CastError(t)) = new_array_result else {
8909            panic!();
8910        };
8911        assert_eq!(
8912            t,
8913            r#"Casting from Map("entries": non-null Struct("key": non-null Utf8, "value": Interval(DayTime)), unsorted) to Map("entries": non-null Struct("key": non-null Utf8, "value": non-null Duration(s)), sorted) not supported"#
8914        );
8915    }
8916
8917    #[test]
8918    fn test_cast_map_field_names() {
8919        let string_builder = StringBuilder::new();
8920        let value_builder = StringBuilder::new();
8921        let mut builder = MapBuilder::new(
8922            Some(MapFieldNames {
8923                entry: "entries".to_string(),
8924                key: "key".to_string(),
8925                value: "value".to_string(),
8926            }),
8927            string_builder,
8928            value_builder,
8929        );
8930
8931        builder.keys().append_value("0");
8932        builder.values().append_value("test_val_1");
8933        builder.append(true).unwrap();
8934        builder.keys().append_value("1");
8935        builder.values().append_value("test_val_2");
8936        builder.append(true).unwrap();
8937        builder.append(false).unwrap();
8938
8939        let array = builder.finish();
8940
8941        let new_type = DataType::Map(
8942            Arc::new(Field::new(
8943                "entries_new",
8944                DataType::Struct(
8945                    vec![
8946                        Field::new("key_new", DataType::Utf8, false),
8947                        Field::new("value_values", DataType::Utf8, false),
8948                    ]
8949                    .into(),
8950                ),
8951                false,
8952            )),
8953            false,
8954        );
8955
8956        assert_ne!(new_type, array.data_type().clone());
8957
8958        let new_array = cast(&array, &new_type.clone()).unwrap();
8959        assert_eq!(new_type, new_array.data_type().clone());
8960        let map_array = new_array.as_map();
8961
8962        assert_ne!(new_type, array.data_type().clone());
8963        assert_eq!(new_type, map_array.data_type().clone());
8964
8965        let key_string = map_array
8966            .keys()
8967            .as_any()
8968            .downcast_ref::<StringArray>()
8969            .unwrap()
8970            .into_iter()
8971            .flatten()
8972            .collect::<Vec<_>>();
8973        assert_eq!(&key_string, &vec!["0", "1"]);
8974
8975        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
8976        let values_string = values_string_array
8977            .as_any()
8978            .downcast_ref::<StringArray>()
8979            .unwrap()
8980            .into_iter()
8981            .flatten()
8982            .collect::<Vec<_>>();
8983        assert_eq!(&values_string, &vec!["test_val_1", "test_val_2"]);
8984
8985        assert_eq!(
8986            map_array.nulls(),
8987            Some(&NullBuffer::from(vec![true, true, false]))
8988        );
8989    }
8990
8991    #[test]
8992    fn test_cast_map_contained_values() {
8993        let string_builder = StringBuilder::new();
8994        let value_builder = Int8Builder::new();
8995        let mut builder = MapBuilder::new(
8996            Some(MapFieldNames {
8997                entry: "entries".to_string(),
8998                key: "key".to_string(),
8999                value: "value".to_string(),
9000            }),
9001            string_builder,
9002            value_builder,
9003        );
9004
9005        builder.keys().append_value("0");
9006        builder.values().append_value(44);
9007        builder.append(true).unwrap();
9008        builder.keys().append_value("1");
9009        builder.values().append_value(22);
9010        builder.append(true).unwrap();
9011
9012        let array = builder.finish();
9013
9014        let new_type = DataType::Map(
9015            Arc::new(Field::new(
9016                "entries",
9017                DataType::Struct(
9018                    vec![
9019                        Field::new("key", DataType::Utf8, false),
9020                        Field::new("value", DataType::Utf8, false),
9021                    ]
9022                    .into(),
9023                ),
9024                false,
9025            )),
9026            false,
9027        );
9028
9029        let new_array = cast(&array, &new_type.clone()).unwrap();
9030        assert_eq!(new_type, new_array.data_type().clone());
9031        let map_array = new_array.as_map();
9032
9033        assert_ne!(new_type, array.data_type().clone());
9034        assert_eq!(new_type, map_array.data_type().clone());
9035
9036        let key_string = map_array
9037            .keys()
9038            .as_any()
9039            .downcast_ref::<StringArray>()
9040            .unwrap()
9041            .into_iter()
9042            .flatten()
9043            .collect::<Vec<_>>();
9044        assert_eq!(&key_string, &vec!["0", "1"]);
9045
9046        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
9047        let values_string = values_string_array
9048            .as_any()
9049            .downcast_ref::<StringArray>()
9050            .unwrap()
9051            .into_iter()
9052            .flatten()
9053            .collect::<Vec<_>>();
9054        assert_eq!(&values_string, &vec!["44", "22"]);
9055    }
9056
9057    #[test]
9058    fn test_utf8_cast_offsets() {
9059        // test if offset of the array is taken into account during cast
9060        let str_array = StringArray::from(vec!["a", "b", "c"]);
9061        let str_array = str_array.slice(1, 2);
9062
9063        let out = cast(&str_array, &DataType::LargeUtf8).unwrap();
9064
9065        let large_str_array = out.as_any().downcast_ref::<LargeStringArray>().unwrap();
9066        let strs = large_str_array.into_iter().flatten().collect::<Vec<_>>();
9067        assert_eq!(strs, &["b", "c"])
9068    }
9069
9070    #[test]
9071    fn test_list_cast_offsets() {
9072        // test if offset of the array is taken into account during cast
9073        let array1 = make_list_array().slice(1, 2);
9074        let array2 = Arc::new(make_list_array()) as ArrayRef;
9075
9076        let dt = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
9077        let out1 = cast(&array1, &dt).unwrap();
9078        let out2 = cast(&array2, &dt).unwrap();
9079
9080        assert_eq!(&out1, &out2.slice(1, 2))
9081    }
9082
9083    #[test]
9084    fn test_list_to_string() {
9085        let str_array = StringArray::from(vec!["a", "b", "c", "d", "e", "f", "g", "h"]);
9086        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
9087        let value_data = str_array.into_data();
9088
9089        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Utf8, true)));
9090        let list_data = ArrayData::builder(list_data_type)
9091            .len(3)
9092            .add_buffer(value_offsets)
9093            .add_child_data(value_data)
9094            .build()
9095            .unwrap();
9096        let array = Arc::new(ListArray::from(list_data)) as ArrayRef;
9097
9098        let out = cast(&array, &DataType::Utf8).unwrap();
9099        let out = out
9100            .as_any()
9101            .downcast_ref::<StringArray>()
9102            .unwrap()
9103            .into_iter()
9104            .flatten()
9105            .collect::<Vec<_>>();
9106        assert_eq!(&out, &vec!["[a, b, c]", "[d, e, f]", "[g, h]"]);
9107
9108        let out = cast(&array, &DataType::LargeUtf8).unwrap();
9109        let out = out
9110            .as_any()
9111            .downcast_ref::<LargeStringArray>()
9112            .unwrap()
9113            .into_iter()
9114            .flatten()
9115            .collect::<Vec<_>>();
9116        assert_eq!(&out, &vec!["[a, b, c]", "[d, e, f]", "[g, h]"]);
9117
9118        let array = Arc::new(make_list_array()) as ArrayRef;
9119        let out = cast(&array, &DataType::Utf8).unwrap();
9120        let out = out
9121            .as_any()
9122            .downcast_ref::<StringArray>()
9123            .unwrap()
9124            .into_iter()
9125            .flatten()
9126            .collect::<Vec<_>>();
9127        assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
9128
9129        let array = Arc::new(make_large_list_array()) as ArrayRef;
9130        let out = cast(&array, &DataType::LargeUtf8).unwrap();
9131        let out = out
9132            .as_any()
9133            .downcast_ref::<LargeStringArray>()
9134            .unwrap()
9135            .into_iter()
9136            .flatten()
9137            .collect::<Vec<_>>();
9138        assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
9139    }
9140
9141    #[test]
9142    fn test_cast_f64_to_decimal128() {
9143        // to reproduce https://github.com/apache/arrow-rs/issues/2997
9144
9145        let decimal_type = DataType::Decimal128(18, 2);
9146        let array = Float64Array::from(vec![
9147            Some(0.0699999999),
9148            Some(0.0659999999),
9149            Some(0.0650000000),
9150            Some(0.0649999999),
9151        ]);
9152        let array = Arc::new(array) as ArrayRef;
9153        generate_cast_test_case!(
9154            &array,
9155            Decimal128Array,
9156            &decimal_type,
9157            vec![
9158                Some(7_i128), // round up
9159                Some(7_i128), // round up
9160                Some(7_i128), // round up
9161                Some(6_i128), // round down
9162            ]
9163        );
9164
9165        let decimal_type = DataType::Decimal128(18, 3);
9166        let array = Float64Array::from(vec![
9167            Some(0.0699999999),
9168            Some(0.0659999999),
9169            Some(0.0650000000),
9170            Some(0.0649999999),
9171        ]);
9172        let array = Arc::new(array) as ArrayRef;
9173        generate_cast_test_case!(
9174            &array,
9175            Decimal128Array,
9176            &decimal_type,
9177            vec![
9178                Some(70_i128), // round up
9179                Some(66_i128), // round up
9180                Some(65_i128), // round down
9181                Some(65_i128), // round up
9182            ]
9183        );
9184    }
9185
9186    #[test]
9187    fn test_cast_numeric_to_decimal128_overflow() {
9188        let array = Int64Array::from(vec![i64::MAX]);
9189        let array = Arc::new(array) as ArrayRef;
9190        let casted_array = cast_with_options(
9191            &array,
9192            &DataType::Decimal128(38, 30),
9193            &CastOptions {
9194                safe: true,
9195                format_options: FormatOptions::default(),
9196            },
9197        );
9198        assert!(casted_array.is_ok());
9199        assert!(casted_array.unwrap().is_null(0));
9200
9201        let casted_array = cast_with_options(
9202            &array,
9203            &DataType::Decimal128(38, 30),
9204            &CastOptions {
9205                safe: false,
9206                format_options: FormatOptions::default(),
9207            },
9208        );
9209        assert!(casted_array.is_err());
9210    }
9211
9212    #[test]
9213    fn test_cast_numeric_to_decimal256_overflow() {
9214        let array = Int64Array::from(vec![i64::MAX]);
9215        let array = Arc::new(array) as ArrayRef;
9216        let casted_array = cast_with_options(
9217            &array,
9218            &DataType::Decimal256(76, 76),
9219            &CastOptions {
9220                safe: true,
9221                format_options: FormatOptions::default(),
9222            },
9223        );
9224        assert!(casted_array.is_ok());
9225        assert!(casted_array.unwrap().is_null(0));
9226
9227        let casted_array = cast_with_options(
9228            &array,
9229            &DataType::Decimal256(76, 76),
9230            &CastOptions {
9231                safe: false,
9232                format_options: FormatOptions::default(),
9233            },
9234        );
9235        assert!(casted_array.is_err());
9236    }
9237
9238    #[test]
9239    fn test_cast_floating_point_to_decimal128_precision_overflow() {
9240        let array = Float64Array::from(vec![1.1]);
9241        let array = Arc::new(array) as ArrayRef;
9242        let casted_array = cast_with_options(
9243            &array,
9244            &DataType::Decimal128(2, 2),
9245            &CastOptions {
9246                safe: true,
9247                format_options: FormatOptions::default(),
9248            },
9249        );
9250        assert!(casted_array.is_ok());
9251        assert!(casted_array.unwrap().is_null(0));
9252
9253        let casted_array = cast_with_options(
9254            &array,
9255            &DataType::Decimal128(2, 2),
9256            &CastOptions {
9257                safe: false,
9258                format_options: FormatOptions::default(),
9259            },
9260        );
9261        let err = casted_array.unwrap_err().to_string();
9262        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
9263        assert!(
9264            err.contains(expected_error),
9265            "did not find expected error '{expected_error}' in actual error '{err}'"
9266        );
9267    }
9268
9269    #[test]
9270    fn test_cast_floating_point_to_decimal256_precision_overflow() {
9271        let array = Float64Array::from(vec![1.1]);
9272        let array = Arc::new(array) as ArrayRef;
9273        let casted_array = cast_with_options(
9274            &array,
9275            &DataType::Decimal256(2, 2),
9276            &CastOptions {
9277                safe: true,
9278                format_options: FormatOptions::default(),
9279            },
9280        );
9281        assert!(casted_array.is_ok());
9282        assert!(casted_array.unwrap().is_null(0));
9283
9284        let casted_array = cast_with_options(
9285            &array,
9286            &DataType::Decimal256(2, 2),
9287            &CastOptions {
9288                safe: false,
9289                format_options: FormatOptions::default(),
9290            },
9291        );
9292        let err = casted_array.unwrap_err().to_string();
9293        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
9294        assert_eq!(err, expected_error);
9295    }
9296
9297    #[test]
9298    fn test_cast_floating_point_to_decimal128_overflow() {
9299        let array = Float64Array::from(vec![f64::MAX]);
9300        let array = Arc::new(array) as ArrayRef;
9301        let casted_array = cast_with_options(
9302            &array,
9303            &DataType::Decimal128(38, 30),
9304            &CastOptions {
9305                safe: true,
9306                format_options: FormatOptions::default(),
9307            },
9308        );
9309        assert!(casted_array.is_ok());
9310        assert!(casted_array.unwrap().is_null(0));
9311
9312        let casted_array = cast_with_options(
9313            &array,
9314            &DataType::Decimal128(38, 30),
9315            &CastOptions {
9316                safe: false,
9317                format_options: FormatOptions::default(),
9318            },
9319        );
9320        let err = casted_array.unwrap_err().to_string();
9321        let expected_error = "Cast error: Cannot cast to Decimal128(38, 30)";
9322        assert!(
9323            err.contains(expected_error),
9324            "did not find expected error '{expected_error}' in actual error '{err}'"
9325        );
9326    }
9327
9328    #[test]
9329    fn test_cast_floating_point_to_decimal256_overflow() {
9330        let array = Float64Array::from(vec![f64::MAX]);
9331        let array = Arc::new(array) as ArrayRef;
9332        let casted_array = cast_with_options(
9333            &array,
9334            &DataType::Decimal256(76, 50),
9335            &CastOptions {
9336                safe: true,
9337                format_options: FormatOptions::default(),
9338            },
9339        );
9340        assert!(casted_array.is_ok());
9341        assert!(casted_array.unwrap().is_null(0));
9342
9343        let casted_array = cast_with_options(
9344            &array,
9345            &DataType::Decimal256(76, 50),
9346            &CastOptions {
9347                safe: false,
9348                format_options: FormatOptions::default(),
9349            },
9350        );
9351        let err = casted_array.unwrap_err().to_string();
9352        let expected_error = "Cast error: Cannot cast to Decimal256(76, 50)";
9353        assert!(
9354            err.contains(expected_error),
9355            "did not find expected error '{expected_error}' in actual error '{err}'"
9356        );
9357    }
9358    #[test]
9359    fn test_cast_decimal256_to_f64_no_overflow() {
9360        // Test casting i256::MAX: should produce a large finite positive value
9361        let array = vec![Some(i256::MAX)];
9362        let array = create_decimal256_array(array, 76, 2).unwrap();
9363        let array = Arc::new(array) as ArrayRef;
9364
9365        let result = cast(&array, &DataType::Float64).unwrap();
9366        let result = result.as_primitive::<Float64Type>();
9367        assert!(result.value(0).is_finite());
9368        assert!(result.value(0) > 0.0); // Positive result
9369
9370        // Test casting i256::MIN: should produce a large finite negative value
9371        let array = vec![Some(i256::MIN)];
9372        let array = create_decimal256_array(array, 76, 2).unwrap();
9373        let array = Arc::new(array) as ArrayRef;
9374
9375        let result = cast(&array, &DataType::Float64).unwrap();
9376        let result = result.as_primitive::<Float64Type>();
9377        assert!(result.value(0).is_finite());
9378        assert!(result.value(0) < 0.0); // Negative result
9379    }
9380
9381    #[test]
9382    fn test_cast_decimal128_to_decimal128_negative_scale() {
9383        let input_type = DataType::Decimal128(20, 0);
9384        let output_type = DataType::Decimal128(20, -1);
9385        assert!(can_cast_types(&input_type, &output_type));
9386        let array = vec![Some(1123450), Some(2123455), Some(3123456), None];
9387        let input_decimal_array = create_decimal128_array(array, 20, 0).unwrap();
9388        let array = Arc::new(input_decimal_array) as ArrayRef;
9389        generate_cast_test_case!(
9390            &array,
9391            Decimal128Array,
9392            &output_type,
9393            vec![
9394                Some(112345_i128),
9395                Some(212346_i128),
9396                Some(312346_i128),
9397                None
9398            ]
9399        );
9400
9401        let casted_array = cast(&array, &output_type).unwrap();
9402        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9403
9404        assert_eq!("1123450", decimal_arr.value_as_string(0));
9405        assert_eq!("2123460", decimal_arr.value_as_string(1));
9406        assert_eq!("3123460", decimal_arr.value_as_string(2));
9407    }
9408
9409    #[test]
9410    fn decimal128_min_max_to_f64() {
9411        // Ensure Decimal128 i128::MIN/MAX round-trip cast
9412        let min128 = i128::MIN;
9413        let max128 = i128::MAX;
9414        assert_eq!(min128 as f64, min128 as f64);
9415        assert_eq!(max128 as f64, max128 as f64);
9416    }
9417
9418    #[test]
9419    fn test_cast_numeric_to_decimal128_negative() {
9420        let decimal_type = DataType::Decimal128(38, -1);
9421        let array = Arc::new(Int32Array::from(vec![
9422            Some(1123456),
9423            Some(2123456),
9424            Some(3123456),
9425        ])) as ArrayRef;
9426
9427        let casted_array = cast(&array, &decimal_type).unwrap();
9428        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9429
9430        assert_eq!("1123450", decimal_arr.value_as_string(0));
9431        assert_eq!("2123450", decimal_arr.value_as_string(1));
9432        assert_eq!("3123450", decimal_arr.value_as_string(2));
9433
9434        let array = Arc::new(Float32Array::from(vec![
9435            Some(1123.456),
9436            Some(2123.456),
9437            Some(3123.456),
9438        ])) as ArrayRef;
9439
9440        let casted_array = cast(&array, &decimal_type).unwrap();
9441        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9442
9443        assert_eq!("1120", decimal_arr.value_as_string(0));
9444        assert_eq!("2120", decimal_arr.value_as_string(1));
9445        assert_eq!("3120", decimal_arr.value_as_string(2));
9446    }
9447
9448    #[test]
9449    fn test_cast_decimal128_to_decimal128_negative() {
9450        let input_type = DataType::Decimal128(10, -1);
9451        let output_type = DataType::Decimal128(10, -2);
9452        assert!(can_cast_types(&input_type, &output_type));
9453        let array = vec![Some(123)];
9454        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
9455        let array = Arc::new(input_decimal_array) as ArrayRef;
9456        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(12_i128),]);
9457
9458        let casted_array = cast(&array, &output_type).unwrap();
9459        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9460
9461        assert_eq!("1200", decimal_arr.value_as_string(0));
9462
9463        let array = vec![Some(125)];
9464        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
9465        let array = Arc::new(input_decimal_array) as ArrayRef;
9466        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(13_i128),]);
9467
9468        let casted_array = cast(&array, &output_type).unwrap();
9469        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9470
9471        assert_eq!("1300", decimal_arr.value_as_string(0));
9472    }
9473
9474    #[test]
9475    fn test_cast_decimal128_to_decimal256_negative() {
9476        let input_type = DataType::Decimal128(10, 3);
9477        let output_type = DataType::Decimal256(10, 5);
9478        assert!(can_cast_types(&input_type, &output_type));
9479        let array = vec![Some(123456), Some(-123456)];
9480        let input_decimal_array = create_decimal128_array(array, 10, 3).unwrap();
9481        let array = Arc::new(input_decimal_array) as ArrayRef;
9482
9483        let hundred = i256::from_i128(100);
9484        generate_cast_test_case!(
9485            &array,
9486            Decimal256Array,
9487            &output_type,
9488            vec![
9489                Some(i256::from_i128(123456).mul_wrapping(hundred)),
9490                Some(i256::from_i128(-123456).mul_wrapping(hundred))
9491            ]
9492        );
9493    }
9494
9495    #[test]
9496    fn test_parse_string_to_decimal() {
9497        assert_eq!(
9498            Decimal128Type::format_decimal(
9499                parse_string_to_decimal_native::<Decimal128Type>("123.45", 2).unwrap(),
9500                38,
9501                2,
9502            ),
9503            "123.45"
9504        );
9505        assert_eq!(
9506            Decimal128Type::format_decimal(
9507                parse_string_to_decimal_native::<Decimal128Type>("12345", 2).unwrap(),
9508                38,
9509                2,
9510            ),
9511            "12345.00"
9512        );
9513        assert_eq!(
9514            Decimal128Type::format_decimal(
9515                parse_string_to_decimal_native::<Decimal128Type>("0.12345", 2).unwrap(),
9516                38,
9517                2,
9518            ),
9519            "0.12"
9520        );
9521        assert_eq!(
9522            Decimal128Type::format_decimal(
9523                parse_string_to_decimal_native::<Decimal128Type>(".12345", 2).unwrap(),
9524                38,
9525                2,
9526            ),
9527            "0.12"
9528        );
9529        assert_eq!(
9530            Decimal128Type::format_decimal(
9531                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
9532                38,
9533                2,
9534            ),
9535            "0.13"
9536        );
9537        assert_eq!(
9538            Decimal128Type::format_decimal(
9539                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
9540                38,
9541                2,
9542            ),
9543            "0.13"
9544        );
9545
9546        assert_eq!(
9547            Decimal256Type::format_decimal(
9548                parse_string_to_decimal_native::<Decimal256Type>("123.45", 3).unwrap(),
9549                38,
9550                3,
9551            ),
9552            "123.450"
9553        );
9554        assert_eq!(
9555            Decimal256Type::format_decimal(
9556                parse_string_to_decimal_native::<Decimal256Type>("12345", 3).unwrap(),
9557                38,
9558                3,
9559            ),
9560            "12345.000"
9561        );
9562        assert_eq!(
9563            Decimal256Type::format_decimal(
9564                parse_string_to_decimal_native::<Decimal256Type>("0.12345", 3).unwrap(),
9565                38,
9566                3,
9567            ),
9568            "0.123"
9569        );
9570        assert_eq!(
9571            Decimal256Type::format_decimal(
9572                parse_string_to_decimal_native::<Decimal256Type>(".12345", 3).unwrap(),
9573                38,
9574                3,
9575            ),
9576            "0.123"
9577        );
9578        assert_eq!(
9579            Decimal256Type::format_decimal(
9580                parse_string_to_decimal_native::<Decimal256Type>(".1265", 3).unwrap(),
9581                38,
9582                3,
9583            ),
9584            "0.127"
9585        );
9586    }
9587
9588    fn test_cast_string_to_decimal(array: ArrayRef) {
9589        // Decimal128
9590        let output_type = DataType::Decimal128(38, 2);
9591        assert!(can_cast_types(array.data_type(), &output_type));
9592
9593        let casted_array = cast(&array, &output_type).unwrap();
9594        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9595
9596        assert_eq!("123.45", decimal_arr.value_as_string(0));
9597        assert_eq!("1.23", decimal_arr.value_as_string(1));
9598        assert_eq!("0.12", decimal_arr.value_as_string(2));
9599        assert_eq!("0.13", decimal_arr.value_as_string(3));
9600        assert_eq!("1.26", decimal_arr.value_as_string(4));
9601        assert_eq!("12345.00", decimal_arr.value_as_string(5));
9602        assert_eq!("12345.00", decimal_arr.value_as_string(6));
9603        assert_eq!("0.12", decimal_arr.value_as_string(7));
9604        assert_eq!("12.23", decimal_arr.value_as_string(8));
9605        assert!(decimal_arr.is_null(9));
9606        assert_eq!("0.00", decimal_arr.value_as_string(10));
9607        assert_eq!("0.00", decimal_arr.value_as_string(11));
9608        assert!(decimal_arr.is_null(12));
9609        assert_eq!("-1.23", decimal_arr.value_as_string(13));
9610        assert_eq!("-1.24", decimal_arr.value_as_string(14));
9611        assert_eq!("0.00", decimal_arr.value_as_string(15));
9612        assert_eq!("-123.00", decimal_arr.value_as_string(16));
9613        assert_eq!("-123.23", decimal_arr.value_as_string(17));
9614        assert_eq!("-0.12", decimal_arr.value_as_string(18));
9615        assert_eq!("1.23", decimal_arr.value_as_string(19));
9616        assert_eq!("1.24", decimal_arr.value_as_string(20));
9617        assert_eq!("0.00", decimal_arr.value_as_string(21));
9618        assert_eq!("123.00", decimal_arr.value_as_string(22));
9619        assert_eq!("123.23", decimal_arr.value_as_string(23));
9620        assert_eq!("0.12", decimal_arr.value_as_string(24));
9621        assert!(decimal_arr.is_null(25));
9622        assert!(decimal_arr.is_null(26));
9623        assert!(decimal_arr.is_null(27));
9624        assert_eq!("0.00", decimal_arr.value_as_string(28));
9625        assert_eq!("0.00", decimal_arr.value_as_string(29));
9626        assert_eq!("12345.00", decimal_arr.value_as_string(30));
9627        assert_eq!(decimal_arr.len(), 31);
9628
9629        // Decimal256
9630        let output_type = DataType::Decimal256(76, 3);
9631        assert!(can_cast_types(array.data_type(), &output_type));
9632
9633        let casted_array = cast(&array, &output_type).unwrap();
9634        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
9635
9636        assert_eq!("123.450", decimal_arr.value_as_string(0));
9637        assert_eq!("1.235", decimal_arr.value_as_string(1));
9638        assert_eq!("0.123", decimal_arr.value_as_string(2));
9639        assert_eq!("0.127", decimal_arr.value_as_string(3));
9640        assert_eq!("1.263", decimal_arr.value_as_string(4));
9641        assert_eq!("12345.000", decimal_arr.value_as_string(5));
9642        assert_eq!("12345.000", decimal_arr.value_as_string(6));
9643        assert_eq!("0.123", decimal_arr.value_as_string(7));
9644        assert_eq!("12.234", decimal_arr.value_as_string(8));
9645        assert!(decimal_arr.is_null(9));
9646        assert_eq!("0.000", decimal_arr.value_as_string(10));
9647        assert_eq!("0.000", decimal_arr.value_as_string(11));
9648        assert!(decimal_arr.is_null(12));
9649        assert_eq!("-1.235", decimal_arr.value_as_string(13));
9650        assert_eq!("-1.236", decimal_arr.value_as_string(14));
9651        assert_eq!("0.000", decimal_arr.value_as_string(15));
9652        assert_eq!("-123.000", decimal_arr.value_as_string(16));
9653        assert_eq!("-123.234", decimal_arr.value_as_string(17));
9654        assert_eq!("-0.123", decimal_arr.value_as_string(18));
9655        assert_eq!("1.235", decimal_arr.value_as_string(19));
9656        assert_eq!("1.236", decimal_arr.value_as_string(20));
9657        assert_eq!("0.000", decimal_arr.value_as_string(21));
9658        assert_eq!("123.000", decimal_arr.value_as_string(22));
9659        assert_eq!("123.234", decimal_arr.value_as_string(23));
9660        assert_eq!("0.123", decimal_arr.value_as_string(24));
9661        assert!(decimal_arr.is_null(25));
9662        assert!(decimal_arr.is_null(26));
9663        assert!(decimal_arr.is_null(27));
9664        assert_eq!("0.000", decimal_arr.value_as_string(28));
9665        assert_eq!("0.000", decimal_arr.value_as_string(29));
9666        assert_eq!("12345.000", decimal_arr.value_as_string(30));
9667        assert_eq!(decimal_arr.len(), 31);
9668    }
9669
9670    #[test]
9671    fn test_cast_utf8_to_decimal() {
9672        let str_array = StringArray::from(vec![
9673            Some("123.45"),
9674            Some("1.2345"),
9675            Some("0.12345"),
9676            Some("0.1267"),
9677            Some("1.263"),
9678            Some("12345.0"),
9679            Some("12345"),
9680            Some("000.123"),
9681            Some("12.234000"),
9682            None,
9683            Some(""),
9684            Some(" "),
9685            None,
9686            Some("-1.23499999"),
9687            Some("-1.23599999"),
9688            Some("-0.00001"),
9689            Some("-123"),
9690            Some("-123.234000"),
9691            Some("-000.123"),
9692            Some("+1.23499999"),
9693            Some("+1.23599999"),
9694            Some("+0.00001"),
9695            Some("+123"),
9696            Some("+123.234000"),
9697            Some("+000.123"),
9698            Some("1.-23499999"),
9699            Some("-1.-23499999"),
9700            Some("--1.23499999"),
9701            Some("0"),
9702            Some("000.000"),
9703            Some("0000000000000000012345.000"),
9704        ]);
9705        let array = Arc::new(str_array) as ArrayRef;
9706
9707        test_cast_string_to_decimal(array);
9708
9709        let test_cases = [
9710            (None, None),
9711            // (Some(""), None),
9712            // (Some("   "), None),
9713            (Some("0"), Some("0")),
9714            (Some("000.000"), Some("0")),
9715            (Some("12345"), Some("12345")),
9716            (Some("000000000000000000000000000012345"), Some("12345")),
9717            (Some("-123"), Some("-123")),
9718            (Some("+123"), Some("123")),
9719        ];
9720        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
9721        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
9722
9723        let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
9724        test_cast_string_to_decimal_scale_zero(array, &expected);
9725    }
9726
9727    #[test]
9728    fn test_cast_large_utf8_to_decimal() {
9729        let str_array = LargeStringArray::from(vec![
9730            Some("123.45"),
9731            Some("1.2345"),
9732            Some("0.12345"),
9733            Some("0.1267"),
9734            Some("1.263"),
9735            Some("12345.0"),
9736            Some("12345"),
9737            Some("000.123"),
9738            Some("12.234000"),
9739            None,
9740            Some(""),
9741            Some(" "),
9742            None,
9743            Some("-1.23499999"),
9744            Some("-1.23599999"),
9745            Some("-0.00001"),
9746            Some("-123"),
9747            Some("-123.234000"),
9748            Some("-000.123"),
9749            Some("+1.23499999"),
9750            Some("+1.23599999"),
9751            Some("+0.00001"),
9752            Some("+123"),
9753            Some("+123.234000"),
9754            Some("+000.123"),
9755            Some("1.-23499999"),
9756            Some("-1.-23499999"),
9757            Some("--1.23499999"),
9758            Some("0"),
9759            Some("000.000"),
9760            Some("0000000000000000012345.000"),
9761        ]);
9762        let array = Arc::new(str_array) as ArrayRef;
9763
9764        test_cast_string_to_decimal(array);
9765
9766        let test_cases = [
9767            (None, None),
9768            (Some(""), None),
9769            (Some("   "), None),
9770            (Some("0"), Some("0")),
9771            (Some("000.000"), Some("0")),
9772            (Some("12345"), Some("12345")),
9773            (Some("000000000000000000000000000012345"), Some("12345")),
9774            (Some("-123"), Some("-123")),
9775            (Some("+123"), Some("123")),
9776        ];
9777        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
9778        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
9779
9780        let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
9781        test_cast_string_to_decimal_scale_zero(array, &expected);
9782    }
9783
9784    fn test_cast_string_to_decimal_scale_zero(
9785        array: ArrayRef,
9786        expected_as_string: &[Option<&str>],
9787    ) {
9788        // Decimal128
9789        let output_type = DataType::Decimal128(38, 0);
9790        assert!(can_cast_types(array.data_type(), &output_type));
9791        let casted_array = cast(&array, &output_type).unwrap();
9792        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9793        assert_decimal_array_contents(decimal_arr, expected_as_string);
9794
9795        // Decimal256
9796        let output_type = DataType::Decimal256(76, 0);
9797        assert!(can_cast_types(array.data_type(), &output_type));
9798        let casted_array = cast(&array, &output_type).unwrap();
9799        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
9800        assert_decimal_array_contents(decimal_arr, expected_as_string);
9801    }
9802
9803    fn assert_decimal_array_contents<T>(
9804        array: &PrimitiveArray<T>,
9805        expected_as_string: &[Option<&str>],
9806    ) where
9807        T: DecimalType + ArrowPrimitiveType,
9808    {
9809        assert_eq!(array.len(), expected_as_string.len());
9810        for (i, expected) in expected_as_string.iter().enumerate() {
9811            let actual = if array.is_null(i) {
9812                None
9813            } else {
9814                Some(array.value_as_string(i))
9815            };
9816            let actual = actual.as_ref().map(|s| s.as_ref());
9817            assert_eq!(*expected, actual, "Expected at position {i}");
9818        }
9819    }
9820
9821    #[test]
9822    fn test_cast_invalid_utf8_to_decimal() {
9823        let str_array = StringArray::from(vec!["4.4.5", ". 0.123"]);
9824        let array = Arc::new(str_array) as ArrayRef;
9825
9826        // Safe cast
9827        let output_type = DataType::Decimal128(38, 2);
9828        let casted_array = cast(&array, &output_type).unwrap();
9829        assert!(casted_array.is_null(0));
9830        assert!(casted_array.is_null(1));
9831
9832        let output_type = DataType::Decimal256(76, 2);
9833        let casted_array = cast(&array, &output_type).unwrap();
9834        assert!(casted_array.is_null(0));
9835        assert!(casted_array.is_null(1));
9836
9837        // Non-safe cast
9838        let output_type = DataType::Decimal128(38, 2);
9839        let str_array = StringArray::from(vec!["4.4.5"]);
9840        let array = Arc::new(str_array) as ArrayRef;
9841        let option = CastOptions {
9842            safe: false,
9843            format_options: FormatOptions::default(),
9844        };
9845        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
9846        assert!(
9847            casted_err
9848                .to_string()
9849                .contains("Cannot cast string '4.4.5' to value of Decimal128(38, 10) type")
9850        );
9851
9852        let str_array = StringArray::from(vec![". 0.123"]);
9853        let array = Arc::new(str_array) as ArrayRef;
9854        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
9855        assert!(
9856            casted_err
9857                .to_string()
9858                .contains("Cannot cast string '. 0.123' to value of Decimal128(38, 10) type")
9859        );
9860    }
9861
9862    fn test_cast_string_to_decimal128_overflow(overflow_array: ArrayRef) {
9863        let output_type = DataType::Decimal128(38, 2);
9864        let casted_array = cast(&overflow_array, &output_type).unwrap();
9865        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9866
9867        assert!(decimal_arr.is_null(0));
9868        assert!(decimal_arr.is_null(1));
9869        assert!(decimal_arr.is_null(2));
9870        assert_eq!(
9871            "999999999999999999999999999999999999.99",
9872            decimal_arr.value_as_string(3)
9873        );
9874        assert_eq!(
9875            "100000000000000000000000000000000000.00",
9876            decimal_arr.value_as_string(4)
9877        );
9878    }
9879
9880    #[test]
9881    fn test_cast_string_to_decimal128_precision_overflow() {
9882        let array = StringArray::from(vec!["1000".to_string()]);
9883        let array = Arc::new(array) as ArrayRef;
9884        let casted_array = cast_with_options(
9885            &array,
9886            &DataType::Decimal128(10, 8),
9887            &CastOptions {
9888                safe: true,
9889                format_options: FormatOptions::default(),
9890            },
9891        );
9892        assert!(casted_array.is_ok());
9893        assert!(casted_array.unwrap().is_null(0));
9894
9895        let err = cast_with_options(
9896            &array,
9897            &DataType::Decimal128(10, 8),
9898            &CastOptions {
9899                safe: false,
9900                format_options: FormatOptions::default(),
9901            },
9902        );
9903        assert_eq!(
9904            "Invalid argument error: 1000.00000000 is too large to store in a Decimal128 of precision 10. Max is 99.99999999",
9905            err.unwrap_err().to_string()
9906        );
9907    }
9908
9909    #[test]
9910    fn test_cast_utf8_to_decimal128_overflow() {
9911        let overflow_str_array = StringArray::from(vec![
9912            i128::MAX.to_string(),
9913            i128::MIN.to_string(),
9914            "99999999999999999999999999999999999999".to_string(),
9915            "999999999999999999999999999999999999.99".to_string(),
9916            "99999999999999999999999999999999999.999".to_string(),
9917        ]);
9918        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9919
9920        test_cast_string_to_decimal128_overflow(overflow_array);
9921    }
9922
9923    #[test]
9924    fn test_cast_large_utf8_to_decimal128_overflow() {
9925        let overflow_str_array = LargeStringArray::from(vec![
9926            i128::MAX.to_string(),
9927            i128::MIN.to_string(),
9928            "99999999999999999999999999999999999999".to_string(),
9929            "999999999999999999999999999999999999.99".to_string(),
9930            "99999999999999999999999999999999999.999".to_string(),
9931        ]);
9932        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9933
9934        test_cast_string_to_decimal128_overflow(overflow_array);
9935    }
9936
9937    fn test_cast_string_to_decimal256_overflow(overflow_array: ArrayRef) {
9938        let output_type = DataType::Decimal256(76, 2);
9939        let casted_array = cast(&overflow_array, &output_type).unwrap();
9940        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
9941
9942        assert_eq!(
9943            "170141183460469231731687303715884105727.00",
9944            decimal_arr.value_as_string(0)
9945        );
9946        assert_eq!(
9947            "-170141183460469231731687303715884105728.00",
9948            decimal_arr.value_as_string(1)
9949        );
9950        assert_eq!(
9951            "99999999999999999999999999999999999999.00",
9952            decimal_arr.value_as_string(2)
9953        );
9954        assert_eq!(
9955            "999999999999999999999999999999999999.99",
9956            decimal_arr.value_as_string(3)
9957        );
9958        assert_eq!(
9959            "100000000000000000000000000000000000.00",
9960            decimal_arr.value_as_string(4)
9961        );
9962        assert!(decimal_arr.is_null(5));
9963        assert!(decimal_arr.is_null(6));
9964    }
9965
9966    #[test]
9967    fn test_cast_string_to_decimal256_precision_overflow() {
9968        let array = StringArray::from(vec!["1000".to_string()]);
9969        let array = Arc::new(array) as ArrayRef;
9970        let casted_array = cast_with_options(
9971            &array,
9972            &DataType::Decimal256(10, 8),
9973            &CastOptions {
9974                safe: true,
9975                format_options: FormatOptions::default(),
9976            },
9977        );
9978        assert!(casted_array.is_ok());
9979        assert!(casted_array.unwrap().is_null(0));
9980
9981        let err = cast_with_options(
9982            &array,
9983            &DataType::Decimal256(10, 8),
9984            &CastOptions {
9985                safe: false,
9986                format_options: FormatOptions::default(),
9987            },
9988        );
9989        assert_eq!(
9990            "Invalid argument error: 1000.00000000 is too large to store in a Decimal256 of precision 10. Max is 99.99999999",
9991            err.unwrap_err().to_string()
9992        );
9993    }
9994
9995    #[test]
9996    fn test_cast_utf8_to_decimal256_overflow() {
9997        let overflow_str_array = StringArray::from(vec![
9998            i128::MAX.to_string(),
9999            i128::MIN.to_string(),
10000            "99999999999999999999999999999999999999".to_string(),
10001            "999999999999999999999999999999999999.99".to_string(),
10002            "99999999999999999999999999999999999.999".to_string(),
10003            i256::MAX.to_string(),
10004            i256::MIN.to_string(),
10005        ]);
10006        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10007
10008        test_cast_string_to_decimal256_overflow(overflow_array);
10009    }
10010
10011    #[test]
10012    fn test_cast_large_utf8_to_decimal256_overflow() {
10013        let overflow_str_array = LargeStringArray::from(vec![
10014            i128::MAX.to_string(),
10015            i128::MIN.to_string(),
10016            "99999999999999999999999999999999999999".to_string(),
10017            "999999999999999999999999999999999999.99".to_string(),
10018            "99999999999999999999999999999999999.999".to_string(),
10019            i256::MAX.to_string(),
10020            i256::MIN.to_string(),
10021        ]);
10022        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10023
10024        test_cast_string_to_decimal256_overflow(overflow_array);
10025    }
10026
10027    #[test]
10028    fn test_cast_outside_supported_range_for_nanoseconds() {
10029        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";
10030
10031        let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
10032
10033        let cast_options = CastOptions {
10034            safe: false,
10035            format_options: FormatOptions::default(),
10036        };
10037
10038        let result = cast_string_to_timestamp::<i32, TimestampNanosecondType>(
10039            &array,
10040            &None::<Arc<str>>,
10041            &cast_options,
10042        );
10043
10044        let err = result.unwrap_err();
10045        assert_eq!(
10046            err.to_string(),
10047            format!(
10048                "Cast error: Overflow converting {} to Nanosecond. {}",
10049                array.value(0),
10050                EXPECTED_ERROR_MESSAGE
10051            )
10052        );
10053    }
10054
10055    #[test]
10056    fn test_cast_date32_to_timestamp() {
10057        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10058        let array = Arc::new(a) as ArrayRef;
10059        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
10060        let c = b.as_primitive::<TimestampSecondType>();
10061        assert_eq!(1609459200, c.value(0));
10062        assert_eq!(1640995200, c.value(1));
10063        assert!(c.is_null(2));
10064    }
10065
10066    #[test]
10067    fn test_cast_date32_to_timestamp_ms() {
10068        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10069        let array = Arc::new(a) as ArrayRef;
10070        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
10071        let c = b
10072            .as_any()
10073            .downcast_ref::<TimestampMillisecondArray>()
10074            .unwrap();
10075        assert_eq!(1609459200000, c.value(0));
10076        assert_eq!(1640995200000, c.value(1));
10077        assert!(c.is_null(2));
10078    }
10079
10080    #[test]
10081    fn test_cast_date32_to_timestamp_us() {
10082        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10083        let array = Arc::new(a) as ArrayRef;
10084        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
10085        let c = b
10086            .as_any()
10087            .downcast_ref::<TimestampMicrosecondArray>()
10088            .unwrap();
10089        assert_eq!(1609459200000000, c.value(0));
10090        assert_eq!(1640995200000000, c.value(1));
10091        assert!(c.is_null(2));
10092    }
10093
10094    #[test]
10095    fn test_cast_date32_to_timestamp_ns() {
10096        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10097        let array = Arc::new(a) as ArrayRef;
10098        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
10099        let c = b
10100            .as_any()
10101            .downcast_ref::<TimestampNanosecondArray>()
10102            .unwrap();
10103        assert_eq!(1609459200000000000, c.value(0));
10104        assert_eq!(1640995200000000000, c.value(1));
10105        assert!(c.is_null(2));
10106    }
10107
10108    #[test]
10109    fn test_timezone_cast() {
10110        let a = StringArray::from(vec![
10111            "2000-01-01T12:00:00", // date + time valid
10112            "2020-12-15T12:34:56", // date + time valid
10113        ]);
10114        let array = Arc::new(a) as ArrayRef;
10115        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
10116        let v = b.as_primitive::<TimestampNanosecondType>();
10117
10118        assert_eq!(v.value(0), 946728000000000000);
10119        assert_eq!(v.value(1), 1608035696000000000);
10120
10121        let b = cast(
10122            &b,
10123            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10124        )
10125        .unwrap();
10126        let v = b.as_primitive::<TimestampNanosecondType>();
10127
10128        assert_eq!(v.value(0), 946728000000000000);
10129        assert_eq!(v.value(1), 1608035696000000000);
10130
10131        let b = cast(
10132            &b,
10133            &DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
10134        )
10135        .unwrap();
10136        let v = b.as_primitive::<TimestampMillisecondType>();
10137
10138        assert_eq!(v.value(0), 946728000000);
10139        assert_eq!(v.value(1), 1608035696000);
10140    }
10141
10142    #[test]
10143    fn test_cast_utf8_to_timestamp() {
10144        fn test_tz(tz: Arc<str>) {
10145            let valid = StringArray::from(vec![
10146                "2023-01-01 04:05:06.789000-08:00",
10147                "2023-01-01 04:05:06.789000-07:00",
10148                "2023-01-01 04:05:06.789 -0800",
10149                "2023-01-01 04:05:06.789 -08:00",
10150                "2023-01-01 040506 +0730",
10151                "2023-01-01 040506 +07:30",
10152                "2023-01-01 04:05:06.789",
10153                "2023-01-01 04:05:06",
10154                "2023-01-01",
10155            ]);
10156
10157            let array = Arc::new(valid) as ArrayRef;
10158            let b = cast_with_options(
10159                &array,
10160                &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
10161                &CastOptions {
10162                    safe: false,
10163                    format_options: FormatOptions::default(),
10164                },
10165            )
10166            .unwrap();
10167
10168            let tz = tz.as_ref().parse().unwrap();
10169
10170            let as_tz =
10171                |v: i64| as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap();
10172
10173            let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
10174            let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
10175
10176            let values = b.as_primitive::<TimestampNanosecondType>().values();
10177            let utc_results: Vec<_> = values.iter().map(as_utc).collect();
10178            let local_results: Vec<_> = values.iter().map(as_local).collect();
10179
10180            // Absolute timestamps should be parsed preserving the same UTC instant
10181            assert_eq!(
10182                &utc_results[..6],
10183                &[
10184                    "2023-01-01 12:05:06.789".to_string(),
10185                    "2023-01-01 11:05:06.789".to_string(),
10186                    "2023-01-01 12:05:06.789".to_string(),
10187                    "2023-01-01 12:05:06.789".to_string(),
10188                    "2022-12-31 20:35:06".to_string(),
10189                    "2022-12-31 20:35:06".to_string(),
10190                ]
10191            );
10192            // Non-absolute timestamps should be parsed preserving the same local instant
10193            assert_eq!(
10194                &local_results[6..],
10195                &[
10196                    "2023-01-01 04:05:06.789".to_string(),
10197                    "2023-01-01 04:05:06".to_string(),
10198                    "2023-01-01 00:00:00".to_string()
10199                ]
10200            )
10201        }
10202
10203        test_tz("+00:00".into());
10204        test_tz("+02:00".into());
10205    }
10206
10207    #[test]
10208    fn test_cast_invalid_utf8() {
10209        let v1: &[u8] = b"\xFF invalid";
10210        let v2: &[u8] = b"\x00 Foo";
10211        let s = BinaryArray::from(vec![v1, v2]);
10212        let options = CastOptions {
10213            safe: true,
10214            format_options: FormatOptions::default(),
10215        };
10216        let array = cast_with_options(&s, &DataType::Utf8, &options).unwrap();
10217        let a = array.as_string::<i32>();
10218        a.to_data().validate_full().unwrap();
10219
10220        assert_eq!(a.null_count(), 1);
10221        assert_eq!(a.len(), 2);
10222        assert!(a.is_null(0));
10223        assert_eq!(a.value(0), "");
10224        assert_eq!(a.value(1), "\x00 Foo");
10225    }
10226
10227    #[test]
10228    fn test_cast_utf8_to_timestamptz() {
10229        let valid = StringArray::from(vec!["2023-01-01"]);
10230
10231        let array = Arc::new(valid) as ArrayRef;
10232        let b = cast(
10233            &array,
10234            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10235        )
10236        .unwrap();
10237
10238        let expect = DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into()));
10239
10240        assert_eq!(b.data_type(), &expect);
10241        let c = b
10242            .as_any()
10243            .downcast_ref::<TimestampNanosecondArray>()
10244            .unwrap();
10245        assert_eq!(1672531200000000000, c.value(0));
10246    }
10247
10248    #[test]
10249    fn test_cast_decimal_to_string() {
10250        assert!(can_cast_types(
10251            &DataType::Decimal32(9, 4),
10252            &DataType::Utf8View
10253        ));
10254        assert!(can_cast_types(
10255            &DataType::Decimal64(16, 4),
10256            &DataType::Utf8View
10257        ));
10258        assert!(can_cast_types(
10259            &DataType::Decimal128(10, 4),
10260            &DataType::Utf8View
10261        ));
10262        assert!(can_cast_types(
10263            &DataType::Decimal256(38, 10),
10264            &DataType::Utf8View
10265        ));
10266
10267        macro_rules! assert_decimal_values {
10268            ($array:expr) => {
10269                let c = $array;
10270                assert_eq!("1123.454", c.value(0));
10271                assert_eq!("2123.456", c.value(1));
10272                assert_eq!("-3123.453", c.value(2));
10273                assert_eq!("-3123.456", c.value(3));
10274                assert_eq!("0.000", c.value(4));
10275                assert_eq!("0.123", c.value(5));
10276                assert_eq!("1234.567", c.value(6));
10277                assert_eq!("-1234.567", c.value(7));
10278                assert!(c.is_null(8));
10279            };
10280        }
10281
10282        fn test_decimal_to_string<IN: ArrowPrimitiveType, OffsetSize: OffsetSizeTrait>(
10283            output_type: DataType,
10284            array: PrimitiveArray<IN>,
10285        ) {
10286            let b = cast(&array, &output_type).unwrap();
10287
10288            assert_eq!(b.data_type(), &output_type);
10289            match b.data_type() {
10290                DataType::Utf8View => {
10291                    let c = b.as_string_view();
10292                    assert_decimal_values!(c);
10293                }
10294                DataType::Utf8 | DataType::LargeUtf8 => {
10295                    let c = b.as_string::<OffsetSize>();
10296                    assert_decimal_values!(c);
10297                }
10298                _ => (),
10299            }
10300        }
10301
10302        let array32: Vec<Option<i32>> = vec![
10303            Some(1123454),
10304            Some(2123456),
10305            Some(-3123453),
10306            Some(-3123456),
10307            Some(0),
10308            Some(123),
10309            Some(123456789),
10310            Some(-123456789),
10311            None,
10312        ];
10313        let array64: Vec<Option<i64>> = array32.iter().map(|num| num.map(|x| x as i64)).collect();
10314        let array128: Vec<Option<i128>> =
10315            array64.iter().map(|num| num.map(|x| x as i128)).collect();
10316        let array256: Vec<Option<i256>> = array128
10317            .iter()
10318            .map(|num| num.map(i256::from_i128))
10319            .collect();
10320
10321        test_decimal_to_string::<Decimal32Type, i32>(
10322            DataType::Utf8View,
10323            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
10324        );
10325        test_decimal_to_string::<Decimal32Type, i32>(
10326            DataType::Utf8,
10327            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
10328        );
10329        test_decimal_to_string::<Decimal32Type, i64>(
10330            DataType::LargeUtf8,
10331            create_decimal32_array(array32, 7, 3).unwrap(),
10332        );
10333
10334        test_decimal_to_string::<Decimal64Type, i32>(
10335            DataType::Utf8View,
10336            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
10337        );
10338        test_decimal_to_string::<Decimal64Type, i32>(
10339            DataType::Utf8,
10340            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
10341        );
10342        test_decimal_to_string::<Decimal64Type, i64>(
10343            DataType::LargeUtf8,
10344            create_decimal64_array(array64, 7, 3).unwrap(),
10345        );
10346
10347        test_decimal_to_string::<Decimal128Type, i32>(
10348            DataType::Utf8View,
10349            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
10350        );
10351        test_decimal_to_string::<Decimal128Type, i32>(
10352            DataType::Utf8,
10353            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
10354        );
10355        test_decimal_to_string::<Decimal128Type, i64>(
10356            DataType::LargeUtf8,
10357            create_decimal128_array(array128, 7, 3).unwrap(),
10358        );
10359
10360        test_decimal_to_string::<Decimal256Type, i32>(
10361            DataType::Utf8View,
10362            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
10363        );
10364        test_decimal_to_string::<Decimal256Type, i32>(
10365            DataType::Utf8,
10366            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
10367        );
10368        test_decimal_to_string::<Decimal256Type, i64>(
10369            DataType::LargeUtf8,
10370            create_decimal256_array(array256, 7, 3).unwrap(),
10371        );
10372    }
10373
10374    #[test]
10375    fn test_cast_numeric_to_decimal128_precision_overflow() {
10376        let array = Int64Array::from(vec![1234567]);
10377        let array = Arc::new(array) as ArrayRef;
10378        let casted_array = cast_with_options(
10379            &array,
10380            &DataType::Decimal128(7, 3),
10381            &CastOptions {
10382                safe: true,
10383                format_options: FormatOptions::default(),
10384            },
10385        );
10386        assert!(casted_array.is_ok());
10387        assert!(casted_array.unwrap().is_null(0));
10388
10389        let err = cast_with_options(
10390            &array,
10391            &DataType::Decimal128(7, 3),
10392            &CastOptions {
10393                safe: false,
10394                format_options: FormatOptions::default(),
10395            },
10396        );
10397        assert_eq!(
10398            "Invalid argument error: 1234567.000 is too large to store in a Decimal128 of precision 7. Max is 9999.999",
10399            err.unwrap_err().to_string()
10400        );
10401    }
10402
10403    #[test]
10404    fn test_cast_numeric_to_decimal256_precision_overflow() {
10405        let array = Int64Array::from(vec![1234567]);
10406        let array = Arc::new(array) as ArrayRef;
10407        let casted_array = cast_with_options(
10408            &array,
10409            &DataType::Decimal256(7, 3),
10410            &CastOptions {
10411                safe: true,
10412                format_options: FormatOptions::default(),
10413            },
10414        );
10415        assert!(casted_array.is_ok());
10416        assert!(casted_array.unwrap().is_null(0));
10417
10418        let err = cast_with_options(
10419            &array,
10420            &DataType::Decimal256(7, 3),
10421            &CastOptions {
10422                safe: false,
10423                format_options: FormatOptions::default(),
10424            },
10425        );
10426        assert_eq!(
10427            "Invalid argument error: 1234567.000 is too large to store in a Decimal256 of precision 7. Max is 9999.999",
10428            err.unwrap_err().to_string()
10429        );
10430    }
10431
10432    /// helper function to test casting from duration to interval
10433    fn cast_from_duration_to_interval<T: ArrowTemporalType<Native = i64>>(
10434        array: Vec<i64>,
10435        cast_options: &CastOptions,
10436    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
10437        let array = PrimitiveArray::<T>::new(array.into(), None);
10438        let array = Arc::new(array) as ArrayRef;
10439        let interval = DataType::Interval(IntervalUnit::MonthDayNano);
10440        let out = cast_with_options(&array, &interval, cast_options)?;
10441        let out = out.as_primitive::<IntervalMonthDayNanoType>().clone();
10442        Ok(out)
10443    }
10444
10445    #[test]
10446    fn test_cast_from_duration_to_interval() {
10447        // from duration second to interval month day nano
10448        let array = vec![1234567];
10449        let casted_array =
10450            cast_from_duration_to_interval::<DurationSecondType>(array, &CastOptions::default())
10451                .unwrap();
10452        assert_eq!(
10453            casted_array.data_type(),
10454            &DataType::Interval(IntervalUnit::MonthDayNano)
10455        );
10456        assert_eq!(
10457            casted_array.value(0),
10458            IntervalMonthDayNano::new(0, 0, 1234567000000000)
10459        );
10460
10461        let array = vec![i64::MAX];
10462        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
10463            array.clone(),
10464            &CastOptions::default(),
10465        )
10466        .unwrap();
10467        assert!(!casted_array.is_valid(0));
10468
10469        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
10470            array,
10471            &CastOptions {
10472                safe: false,
10473                format_options: FormatOptions::default(),
10474            },
10475        );
10476        assert!(casted_array.is_err());
10477
10478        // from duration millisecond to interval month day nano
10479        let array = vec![1234567];
10480        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
10481            array,
10482            &CastOptions::default(),
10483        )
10484        .unwrap();
10485        assert_eq!(
10486            casted_array.data_type(),
10487            &DataType::Interval(IntervalUnit::MonthDayNano)
10488        );
10489        assert_eq!(
10490            casted_array.value(0),
10491            IntervalMonthDayNano::new(0, 0, 1234567000000)
10492        );
10493
10494        let array = vec![i64::MAX];
10495        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
10496            array.clone(),
10497            &CastOptions::default(),
10498        )
10499        .unwrap();
10500        assert!(!casted_array.is_valid(0));
10501
10502        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
10503            array,
10504            &CastOptions {
10505                safe: false,
10506                format_options: FormatOptions::default(),
10507            },
10508        );
10509        assert!(casted_array.is_err());
10510
10511        // from duration microsecond to interval month day nano
10512        let array = vec![1234567];
10513        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
10514            array,
10515            &CastOptions::default(),
10516        )
10517        .unwrap();
10518        assert_eq!(
10519            casted_array.data_type(),
10520            &DataType::Interval(IntervalUnit::MonthDayNano)
10521        );
10522        assert_eq!(
10523            casted_array.value(0),
10524            IntervalMonthDayNano::new(0, 0, 1234567000)
10525        );
10526
10527        let array = vec![i64::MAX];
10528        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
10529            array.clone(),
10530            &CastOptions::default(),
10531        )
10532        .unwrap();
10533        assert!(!casted_array.is_valid(0));
10534
10535        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
10536            array,
10537            &CastOptions {
10538                safe: false,
10539                format_options: FormatOptions::default(),
10540            },
10541        );
10542        assert!(casted_array.is_err());
10543
10544        // from duration nanosecond to interval month day nano
10545        let array = vec![1234567];
10546        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
10547            array,
10548            &CastOptions::default(),
10549        )
10550        .unwrap();
10551        assert_eq!(
10552            casted_array.data_type(),
10553            &DataType::Interval(IntervalUnit::MonthDayNano)
10554        );
10555        assert_eq!(
10556            casted_array.value(0),
10557            IntervalMonthDayNano::new(0, 0, 1234567)
10558        );
10559
10560        let array = vec![i64::MAX];
10561        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
10562            array,
10563            &CastOptions {
10564                safe: false,
10565                format_options: FormatOptions::default(),
10566            },
10567        )
10568        .unwrap();
10569        assert_eq!(
10570            casted_array.value(0),
10571            IntervalMonthDayNano::new(0, 0, i64::MAX)
10572        );
10573    }
10574
10575    /// helper function to test casting from interval to duration
10576    fn cast_from_interval_to_duration<T: ArrowTemporalType>(
10577        array: &IntervalMonthDayNanoArray,
10578        cast_options: &CastOptions,
10579    ) -> Result<PrimitiveArray<T>, ArrowError> {
10580        let casted_array = cast_with_options(&array, &T::DATA_TYPE, cast_options)?;
10581        casted_array
10582            .as_any()
10583            .downcast_ref::<PrimitiveArray<T>>()
10584            .ok_or_else(|| {
10585                ArrowError::ComputeError(format!("Failed to downcast to {}", T::DATA_TYPE))
10586            })
10587            .cloned()
10588    }
10589
10590    #[test]
10591    fn test_cast_from_interval_to_duration() {
10592        let nullable = CastOptions::default();
10593        let fallible = CastOptions {
10594            safe: false,
10595            format_options: FormatOptions::default(),
10596        };
10597        let v = IntervalMonthDayNano::new(0, 0, 1234567);
10598
10599        // from interval month day nano to duration second
10600        let array = vec![v].into();
10601        let casted_array: DurationSecondArray =
10602            cast_from_interval_to_duration(&array, &nullable).unwrap();
10603        assert_eq!(casted_array.value(0), 0);
10604
10605        let array = vec![IntervalMonthDayNano::MAX].into();
10606        let casted_array: DurationSecondArray =
10607            cast_from_interval_to_duration(&array, &nullable).unwrap();
10608        assert!(!casted_array.is_valid(0));
10609
10610        let res = cast_from_interval_to_duration::<DurationSecondType>(&array, &fallible);
10611        assert!(res.is_err());
10612
10613        // from interval month day nano to duration millisecond
10614        let array = vec![v].into();
10615        let casted_array: DurationMillisecondArray =
10616            cast_from_interval_to_duration(&array, &nullable).unwrap();
10617        assert_eq!(casted_array.value(0), 1);
10618
10619        let array = vec![IntervalMonthDayNano::MAX].into();
10620        let casted_array: DurationMillisecondArray =
10621            cast_from_interval_to_duration(&array, &nullable).unwrap();
10622        assert!(!casted_array.is_valid(0));
10623
10624        let res = cast_from_interval_to_duration::<DurationMillisecondType>(&array, &fallible);
10625        assert!(res.is_err());
10626
10627        // from interval month day nano to duration microsecond
10628        let array = vec![v].into();
10629        let casted_array: DurationMicrosecondArray =
10630            cast_from_interval_to_duration(&array, &nullable).unwrap();
10631        assert_eq!(casted_array.value(0), 1234);
10632
10633        let array = vec![IntervalMonthDayNano::MAX].into();
10634        let casted_array =
10635            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &nullable).unwrap();
10636        assert!(!casted_array.is_valid(0));
10637
10638        let casted_array =
10639            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &fallible);
10640        assert!(casted_array.is_err());
10641
10642        // from interval month day nano to duration nanosecond
10643        let array = vec![v].into();
10644        let casted_array: DurationNanosecondArray =
10645            cast_from_interval_to_duration(&array, &nullable).unwrap();
10646        assert_eq!(casted_array.value(0), 1234567);
10647
10648        let array = vec![IntervalMonthDayNano::MAX].into();
10649        let casted_array: DurationNanosecondArray =
10650            cast_from_interval_to_duration(&array, &nullable).unwrap();
10651        assert!(!casted_array.is_valid(0));
10652
10653        let casted_array =
10654            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &fallible);
10655        assert!(casted_array.is_err());
10656
10657        let array = vec![
10658            IntervalMonthDayNanoType::make_value(0, 1, 0),
10659            IntervalMonthDayNanoType::make_value(-1, 0, 0),
10660            IntervalMonthDayNanoType::make_value(1, 1, 0),
10661            IntervalMonthDayNanoType::make_value(1, 0, 1),
10662            IntervalMonthDayNanoType::make_value(0, 0, -1),
10663        ]
10664        .into();
10665        let casted_array =
10666            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable).unwrap();
10667        assert!(!casted_array.is_valid(0));
10668        assert!(!casted_array.is_valid(1));
10669        assert!(!casted_array.is_valid(2));
10670        assert!(!casted_array.is_valid(3));
10671        assert!(casted_array.is_valid(4));
10672        assert_eq!(casted_array.value(4), -1);
10673    }
10674
10675    /// helper function to test casting from interval year month to interval month day nano
10676    fn cast_from_interval_year_month_to_interval_month_day_nano(
10677        array: Vec<i32>,
10678        cast_options: &CastOptions,
10679    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
10680        let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
10681        let array = Arc::new(array) as ArrayRef;
10682        let casted_array = cast_with_options(
10683            &array,
10684            &DataType::Interval(IntervalUnit::MonthDayNano),
10685            cast_options,
10686        )?;
10687        casted_array
10688            .as_any()
10689            .downcast_ref::<IntervalMonthDayNanoArray>()
10690            .ok_or_else(|| {
10691                ArrowError::ComputeError(
10692                    "Failed to downcast to IntervalMonthDayNanoArray".to_string(),
10693                )
10694            })
10695            .cloned()
10696    }
10697
10698    #[test]
10699    fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
10700        // from interval year month to interval month day nano
10701        let array = vec![1234567];
10702        let casted_array = cast_from_interval_year_month_to_interval_month_day_nano(
10703            array,
10704            &CastOptions::default(),
10705        )
10706        .unwrap();
10707        assert_eq!(
10708            casted_array.data_type(),
10709            &DataType::Interval(IntervalUnit::MonthDayNano)
10710        );
10711        assert_eq!(
10712            casted_array.value(0),
10713            IntervalMonthDayNano::new(1234567, 0, 0)
10714        );
10715    }
10716
10717    /// helper function to test casting from interval day time to interval month day nano
10718    fn cast_from_interval_day_time_to_interval_month_day_nano(
10719        array: Vec<IntervalDayTime>,
10720        cast_options: &CastOptions,
10721    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
10722        let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
10723        let array = Arc::new(array) as ArrayRef;
10724        let casted_array = cast_with_options(
10725            &array,
10726            &DataType::Interval(IntervalUnit::MonthDayNano),
10727            cast_options,
10728        )?;
10729        Ok(casted_array
10730            .as_primitive::<IntervalMonthDayNanoType>()
10731            .clone())
10732    }
10733
10734    #[test]
10735    fn test_cast_from_interval_day_time_to_interval_month_day_nano() {
10736        // from interval day time to interval month day nano
10737        let array = vec![IntervalDayTime::new(123, 0)];
10738        let casted_array =
10739            cast_from_interval_day_time_to_interval_month_day_nano(array, &CastOptions::default())
10740                .unwrap();
10741        assert_eq!(
10742            casted_array.data_type(),
10743            &DataType::Interval(IntervalUnit::MonthDayNano)
10744        );
10745        assert_eq!(casted_array.value(0), IntervalMonthDayNano::new(0, 123, 0));
10746    }
10747
10748    #[test]
10749    fn test_cast_below_unixtimestamp() {
10750        let valid = StringArray::from(vec![
10751            "1900-01-03 23:59:59",
10752            "1969-12-31 00:00:01",
10753            "1989-12-31 00:00:01",
10754        ]);
10755
10756        let array = Arc::new(valid) as ArrayRef;
10757        let casted_array = cast_with_options(
10758            &array,
10759            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10760            &CastOptions {
10761                safe: false,
10762                format_options: FormatOptions::default(),
10763            },
10764        )
10765        .unwrap();
10766
10767        let ts_array = casted_array
10768            .as_primitive::<TimestampNanosecondType>()
10769            .values()
10770            .iter()
10771            .map(|ts| ts / 1_000_000)
10772            .collect::<Vec<_>>();
10773
10774        let array = TimestampMillisecondArray::from(ts_array).with_timezone("+00:00".to_string());
10775        let casted_array = cast(&array, &DataType::Date32).unwrap();
10776        let date_array = casted_array.as_primitive::<Date32Type>();
10777        let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
10778        let string_array = casted_array.as_string::<i32>();
10779        assert_eq!("1900-01-03", string_array.value(0));
10780        assert_eq!("1969-12-31", string_array.value(1));
10781        assert_eq!("1989-12-31", string_array.value(2));
10782    }
10783
10784    #[test]
10785    fn test_nested_list() {
10786        let mut list = ListBuilder::new(Int32Builder::new());
10787        list.append_value([Some(1), Some(2), Some(3)]);
10788        list.append_value([Some(4), None, Some(6)]);
10789        let list = list.finish();
10790
10791        let to_field = Field::new("nested", list.data_type().clone(), false);
10792        let to = DataType::List(Arc::new(to_field));
10793        let out = cast(&list, &to).unwrap();
10794        let opts = FormatOptions::default().with_null("null");
10795        let formatted = ArrayFormatter::try_new(out.as_ref(), &opts).unwrap();
10796
10797        assert_eq!(formatted.value(0).to_string(), "[[1], [2], [3]]");
10798        assert_eq!(formatted.value(1).to_string(), "[[4], [null], [6]]");
10799    }
10800
10801    #[test]
10802    fn test_nested_list_cast() {
10803        let mut builder = ListBuilder::new(ListBuilder::new(Int32Builder::new()));
10804        builder.append_value([Some([Some(1), Some(2), None]), None]);
10805        builder.append_value([None, Some([]), None]);
10806        builder.append_null();
10807        builder.append_value([Some([Some(2), Some(3)])]);
10808        let start = builder.finish();
10809
10810        let mut builder = LargeListBuilder::new(LargeListBuilder::new(Int8Builder::new()));
10811        builder.append_value([Some([Some(1), Some(2), None]), None]);
10812        builder.append_value([None, Some([]), None]);
10813        builder.append_null();
10814        builder.append_value([Some([Some(2), Some(3)])]);
10815        let expected = builder.finish();
10816
10817        let actual = cast(&start, expected.data_type()).unwrap();
10818        assert_eq!(actual.as_ref(), &expected);
10819    }
10820
10821    const CAST_OPTIONS: CastOptions<'static> = CastOptions {
10822        safe: true,
10823        format_options: FormatOptions::new(),
10824    };
10825
10826    #[test]
10827    #[allow(clippy::assertions_on_constants)]
10828    fn test_const_options() {
10829        assert!(CAST_OPTIONS.safe)
10830    }
10831
10832    #[test]
10833    fn test_list_format_options() {
10834        let options = CastOptions {
10835            safe: false,
10836            format_options: FormatOptions::default().with_null("null"),
10837        };
10838        let array = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
10839            Some(vec![Some(0), Some(1), Some(2)]),
10840            Some(vec![Some(0), None, Some(2)]),
10841        ]);
10842        let a = cast_with_options(&array, &DataType::Utf8, &options).unwrap();
10843        let r: Vec<_> = a.as_string::<i32>().iter().flatten().collect();
10844        assert_eq!(r, &["[0, 1, 2]", "[0, null, 2]"]);
10845    }
10846    #[test]
10847    fn test_cast_string_to_timestamp_invalid_tz() {
10848        // content after Z should be ignored
10849        let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
10850        let array = StringArray::from(vec![Some(bad_timestamp)]);
10851
10852        let data_types = [
10853            DataType::Timestamp(TimeUnit::Second, None),
10854            DataType::Timestamp(TimeUnit::Millisecond, None),
10855            DataType::Timestamp(TimeUnit::Microsecond, None),
10856            DataType::Timestamp(TimeUnit::Nanosecond, None),
10857        ];
10858
10859        let cast_options = CastOptions {
10860            safe: false,
10861            ..Default::default()
10862        };
10863
10864        for dt in data_types {
10865            assert_eq!(
10866                cast_with_options(&array, &dt, &cast_options)
10867                    .unwrap_err()
10868                    .to_string(),
10869                "Parser error: Invalid timezone \"ZZTOP\": only offset based timezones supported without chrono-tz feature"
10870            );
10871        }
10872    }
10873    #[test]
10874    fn test_cast_struct_to_struct() {
10875        let struct_type = DataType::Struct(
10876            vec![
10877                Field::new("a", DataType::Boolean, false),
10878                Field::new("b", DataType::Int32, false),
10879            ]
10880            .into(),
10881        );
10882        let to_type = DataType::Struct(
10883            vec![
10884                Field::new("a", DataType::Utf8, false),
10885                Field::new("b", DataType::Utf8, false),
10886            ]
10887            .into(),
10888        );
10889        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10890        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
10891        let struct_array = StructArray::from(vec![
10892            (
10893                Arc::new(Field::new("a", DataType::Boolean, false)),
10894                boolean.clone() as ArrayRef,
10895            ),
10896            (
10897                Arc::new(Field::new("b", DataType::Int32, false)),
10898                int.clone() as ArrayRef,
10899            ),
10900        ]);
10901        let casted_array = cast(&struct_array, &to_type).unwrap();
10902        let casted_array = casted_array.as_struct();
10903        assert_eq!(casted_array.data_type(), &to_type);
10904        let casted_boolean_array = casted_array
10905            .column(0)
10906            .as_string::<i32>()
10907            .into_iter()
10908            .flatten()
10909            .collect::<Vec<_>>();
10910        let casted_int_array = casted_array
10911            .column(1)
10912            .as_string::<i32>()
10913            .into_iter()
10914            .flatten()
10915            .collect::<Vec<_>>();
10916        assert_eq!(casted_boolean_array, vec!["false", "false", "true", "true"]);
10917        assert_eq!(casted_int_array, vec!["42", "28", "19", "31"]);
10918
10919        // test for can't cast
10920        let to_type = DataType::Struct(
10921            vec![
10922                Field::new("a", DataType::Date32, false),
10923                Field::new("b", DataType::Utf8, false),
10924            ]
10925            .into(),
10926        );
10927        assert!(!can_cast_types(&struct_type, &to_type));
10928        let result = cast(&struct_array, &to_type);
10929        assert_eq!(
10930            "Cast error: Casting from Boolean to Date32 not supported",
10931            result.unwrap_err().to_string()
10932        );
10933    }
10934
10935    #[test]
10936    fn test_cast_struct_to_struct_nullability() {
10937        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10938        let int = Arc::new(Int32Array::from(vec![Some(42), None, Some(19), None]));
10939        let struct_array = StructArray::from(vec![
10940            (
10941                Arc::new(Field::new("a", DataType::Boolean, false)),
10942                boolean.clone() as ArrayRef,
10943            ),
10944            (
10945                Arc::new(Field::new("b", DataType::Int32, true)),
10946                int.clone() as ArrayRef,
10947            ),
10948        ]);
10949
10950        // okay: nullable to nullable
10951        let to_type = DataType::Struct(
10952            vec![
10953                Field::new("a", DataType::Utf8, false),
10954                Field::new("b", DataType::Utf8, true),
10955            ]
10956            .into(),
10957        );
10958        cast(&struct_array, &to_type).expect("Cast nullable to nullable struct field should work");
10959
10960        // error: nullable to non-nullable
10961        let to_type = DataType::Struct(
10962            vec![
10963                Field::new("a", DataType::Utf8, false),
10964                Field::new("b", DataType::Utf8, false),
10965            ]
10966            .into(),
10967        );
10968        cast(&struct_array, &to_type)
10969            .expect_err("Cast nullable to non-nullable struct field should fail");
10970
10971        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10972        let int = Arc::new(Int32Array::from(vec![i32::MAX, 25, 1, 100]));
10973        let struct_array = StructArray::from(vec![
10974            (
10975                Arc::new(Field::new("a", DataType::Boolean, false)),
10976                boolean.clone() as ArrayRef,
10977            ),
10978            (
10979                Arc::new(Field::new("b", DataType::Int32, false)),
10980                int.clone() as ArrayRef,
10981            ),
10982        ]);
10983
10984        // okay: non-nullable to non-nullable
10985        let to_type = DataType::Struct(
10986            vec![
10987                Field::new("a", DataType::Utf8, false),
10988                Field::new("b", DataType::Utf8, false),
10989            ]
10990            .into(),
10991        );
10992        cast(&struct_array, &to_type)
10993            .expect("Cast non-nullable to non-nullable struct field should work");
10994
10995        // err: non-nullable to non-nullable but overflowing return null during casting
10996        let to_type = DataType::Struct(
10997            vec![
10998                Field::new("a", DataType::Utf8, false),
10999                Field::new("b", DataType::Int8, false),
11000            ]
11001            .into(),
11002        );
11003        cast(&struct_array, &to_type).expect_err(
11004            "Cast non-nullable to non-nullable struct field returning null should fail",
11005        );
11006    }
11007
11008    #[test]
11009    fn test_cast_struct_to_non_struct() {
11010        let boolean = Arc::new(BooleanArray::from(vec![true, false]));
11011        let struct_array = StructArray::from(vec![(
11012            Arc::new(Field::new("a", DataType::Boolean, false)),
11013            boolean.clone() as ArrayRef,
11014        )]);
11015        let to_type = DataType::Utf8;
11016        let result = cast(&struct_array, &to_type);
11017        assert_eq!(
11018            r#"Cast error: Casting from Struct("a": non-null Boolean) to Utf8 not supported"#,
11019            result.unwrap_err().to_string()
11020        );
11021    }
11022
11023    #[test]
11024    fn test_cast_non_struct_to_struct() {
11025        let array = StringArray::from(vec!["a", "b"]);
11026        let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
11027        let result = cast(&array, &to_type);
11028        assert_eq!(
11029            r#"Cast error: Casting from Utf8 to Struct("a": non-null Boolean) not supported"#,
11030            result.unwrap_err().to_string()
11031        );
11032    }
11033
11034    #[test]
11035    fn test_cast_struct_with_different_field_order() {
11036        // Test slow path: fields are in different order
11037        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11038        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
11039        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
11040
11041        let struct_array = StructArray::from(vec![
11042            (
11043                Arc::new(Field::new("a", DataType::Boolean, false)),
11044                boolean.clone() as ArrayRef,
11045            ),
11046            (
11047                Arc::new(Field::new("b", DataType::Int32, false)),
11048                int.clone() as ArrayRef,
11049            ),
11050            (
11051                Arc::new(Field::new("c", DataType::Utf8, false)),
11052                string.clone() as ArrayRef,
11053            ),
11054        ]);
11055
11056        // Target has fields in different order: c, a, b instead of a, b, c
11057        let to_type = DataType::Struct(
11058            vec![
11059                Field::new("c", DataType::Utf8, false),
11060                Field::new("a", DataType::Utf8, false), // Boolean to Utf8
11061                Field::new("b", DataType::Utf8, false), // Int32 to Utf8
11062            ]
11063            .into(),
11064        );
11065
11066        let result = cast(&struct_array, &to_type).unwrap();
11067        let result_struct = result.as_struct();
11068
11069        assert_eq!(result_struct.data_type(), &to_type);
11070        assert_eq!(result_struct.num_columns(), 3);
11071
11072        // Verify field "c" (originally position 2, now position 0) remains Utf8
11073        let c_column = result_struct.column(0).as_string::<i32>();
11074        assert_eq!(
11075            c_column.into_iter().flatten().collect::<Vec<_>>(),
11076            vec!["foo", "bar", "baz", "qux"]
11077        );
11078
11079        // Verify field "a" (originally position 0, now position 1) was cast from Boolean to Utf8
11080        let a_column = result_struct.column(1).as_string::<i32>();
11081        assert_eq!(
11082            a_column.into_iter().flatten().collect::<Vec<_>>(),
11083            vec!["false", "false", "true", "true"]
11084        );
11085
11086        // Verify field "b" (originally position 1, now position 2) was cast from Int32 to Utf8
11087        let b_column = result_struct.column(2).as_string::<i32>();
11088        assert_eq!(
11089            b_column.into_iter().flatten().collect::<Vec<_>>(),
11090            vec!["42", "28", "19", "31"]
11091        );
11092    }
11093
11094    #[test]
11095    fn test_cast_struct_with_missing_field() {
11096        // Test that casting fails when target has a field not present in source
11097        let boolean = Arc::new(BooleanArray::from(vec![false, true]));
11098        let struct_array = StructArray::from(vec![(
11099            Arc::new(Field::new("a", DataType::Boolean, false)),
11100            boolean.clone() as ArrayRef,
11101        )]);
11102
11103        let to_type = DataType::Struct(
11104            vec![
11105                Field::new("a", DataType::Utf8, false),
11106                Field::new("b", DataType::Int32, false), // Field "b" doesn't exist in source
11107            ]
11108            .into(),
11109        );
11110
11111        let result = cast(&struct_array, &to_type);
11112        assert!(result.is_err());
11113        assert_eq!(
11114            result.unwrap_err().to_string(),
11115            "Cast error: Field 'b' not found in source struct"
11116        );
11117    }
11118
11119    #[test]
11120    fn test_cast_struct_with_subset_of_fields() {
11121        // Test casting to a struct with fewer fields (selecting a subset)
11122        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11123        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
11124        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
11125
11126        let struct_array = StructArray::from(vec![
11127            (
11128                Arc::new(Field::new("a", DataType::Boolean, false)),
11129                boolean.clone() as ArrayRef,
11130            ),
11131            (
11132                Arc::new(Field::new("b", DataType::Int32, false)),
11133                int.clone() as ArrayRef,
11134            ),
11135            (
11136                Arc::new(Field::new("c", DataType::Utf8, false)),
11137                string.clone() as ArrayRef,
11138            ),
11139        ]);
11140
11141        // Target has only fields "c" and "a", omitting "b"
11142        let to_type = DataType::Struct(
11143            vec![
11144                Field::new("c", DataType::Utf8, false),
11145                Field::new("a", DataType::Utf8, false),
11146            ]
11147            .into(),
11148        );
11149
11150        let result = cast(&struct_array, &to_type).unwrap();
11151        let result_struct = result.as_struct();
11152
11153        assert_eq!(result_struct.data_type(), &to_type);
11154        assert_eq!(result_struct.num_columns(), 2);
11155
11156        // Verify field "c" remains Utf8
11157        let c_column = result_struct.column(0).as_string::<i32>();
11158        assert_eq!(
11159            c_column.into_iter().flatten().collect::<Vec<_>>(),
11160            vec!["foo", "bar", "baz", "qux"]
11161        );
11162
11163        // Verify field "a" was cast from Boolean to Utf8
11164        let a_column = result_struct.column(1).as_string::<i32>();
11165        assert_eq!(
11166            a_column.into_iter().flatten().collect::<Vec<_>>(),
11167            vec!["false", "false", "true", "true"]
11168        );
11169    }
11170
11171    #[test]
11172    fn test_can_cast_struct_with_missing_field() {
11173        // Test that can_cast_types returns false when target has a field not in source
11174        let from_type = DataType::Struct(
11175            vec![
11176                Field::new("a", DataType::Int32, false),
11177                Field::new("b", DataType::Utf8, false),
11178            ]
11179            .into(),
11180        );
11181
11182        let to_type = DataType::Struct(
11183            vec![
11184                Field::new("a", DataType::Int64, false),
11185                Field::new("c", DataType::Boolean, false), // Field "c" not in source
11186            ]
11187            .into(),
11188        );
11189
11190        assert!(!can_cast_types(&from_type, &to_type));
11191    }
11192
11193    fn run_decimal_cast_test_case_between_multiple_types(t: DecimalCastTestConfig) {
11194        run_decimal_cast_test_case::<Decimal128Type, Decimal128Type>(t.clone());
11195        run_decimal_cast_test_case::<Decimal128Type, Decimal256Type>(t.clone());
11196        run_decimal_cast_test_case::<Decimal256Type, Decimal128Type>(t.clone());
11197        run_decimal_cast_test_case::<Decimal256Type, Decimal256Type>(t.clone());
11198    }
11199
11200    #[test]
11201    fn test_decimal_to_decimal_coverage() {
11202        let test_cases = [
11203            // increase precision, increase scale, infallible
11204            DecimalCastTestConfig {
11205                input_prec: 5,
11206                input_scale: 1,
11207                input_repr: 99999, // 9999.9
11208                output_prec: 10,
11209                output_scale: 6,
11210                expected_output_repr: Ok(9999900000), // 9999.900000
11211            },
11212            // increase precision, increase scale, fallible, safe
11213            DecimalCastTestConfig {
11214                input_prec: 5,
11215                input_scale: 1,
11216                input_repr: 99, // 9999.9
11217                output_prec: 7,
11218                output_scale: 6,
11219                expected_output_repr: Ok(9900000), // 9.900000
11220            },
11221            // increase precision, increase scale, fallible, unsafe
11222            DecimalCastTestConfig {
11223                input_prec: 5,
11224                input_scale: 1,
11225                input_repr: 99999, // 9999.9
11226                output_prec: 7,
11227                output_scale: 6,
11228                expected_output_repr: Err("Invalid argument error: 9999.900000 is too large to store in a {} of precision 7. Max is 9.999999".to_string()) // max is 9.999999
11229            },
11230            // increase precision, decrease scale, always infallible
11231            DecimalCastTestConfig {
11232                input_prec: 5,
11233                input_scale: 3,
11234                input_repr: 99999, // 99.999
11235                output_prec: 10,
11236                output_scale: 2,
11237                expected_output_repr: Ok(10000), // 100.00
11238            },
11239            // increase precision, decrease scale, no rouding
11240            DecimalCastTestConfig {
11241                input_prec: 5,
11242                input_scale: 3,
11243                input_repr: 99994, // 99.994
11244                output_prec: 10,
11245                output_scale: 2,
11246                expected_output_repr: Ok(9999), // 99.99
11247            },
11248            // increase precision, don't change scale, always infallible
11249            DecimalCastTestConfig {
11250                input_prec: 5,
11251                input_scale: 3,
11252                input_repr: 99999, // 99.999
11253                output_prec: 10,
11254                output_scale: 3,
11255                expected_output_repr: Ok(99999), // 99.999
11256            },
11257            // decrease precision, increase scale, safe
11258            DecimalCastTestConfig {
11259                input_prec: 10,
11260                input_scale: 5,
11261                input_repr: 999999, // 9.99999
11262                output_prec: 8,
11263                output_scale: 7,
11264                expected_output_repr: Ok(99999900), // 9.9999900
11265            },
11266            // decrease precision, increase scale, unsafe
11267            DecimalCastTestConfig {
11268                input_prec: 10,
11269                input_scale: 5,
11270                input_repr: 9999999, // 99.99999
11271                output_prec: 8,
11272                output_scale: 7,
11273                expected_output_repr: Err("Invalid argument error: 99.9999900 is too large to store in a {} of precision 8. Max is 9.9999999".to_string()) // max is 9.9999999
11274            },
11275            // decrease precision, decrease scale, safe, infallible
11276            DecimalCastTestConfig {
11277                input_prec: 7,
11278                input_scale: 4,
11279                input_repr: 9999999, // 999.9999
11280                output_prec: 6,
11281                output_scale: 2,
11282                expected_output_repr: Ok(100000),
11283            },
11284            // decrease precision, decrease scale, safe, fallible
11285            DecimalCastTestConfig {
11286                input_prec: 10,
11287                input_scale: 5,
11288                input_repr: 12345678, // 123.45678
11289                output_prec: 8,
11290                output_scale: 3,
11291                expected_output_repr: Ok(123457), // 123.457
11292            },
11293            // decrease precision, decrease scale, unsafe
11294            DecimalCastTestConfig {
11295                input_prec: 10,
11296                input_scale: 5,
11297                input_repr: 9999999, // 99.99999
11298                output_prec: 4,
11299                output_scale: 3,
11300                expected_output_repr: Err("Invalid argument error: 100.000 is too large to store in a {} of precision 4. Max is 9.999".to_string()) // max is 9.999
11301            },
11302            // decrease precision, same scale, safe
11303            DecimalCastTestConfig {
11304                input_prec: 10,
11305                input_scale: 5,
11306                input_repr: 999999, // 9.99999
11307                output_prec: 6,
11308                output_scale: 5,
11309                expected_output_repr: Ok(999999), // 9.99999
11310            },
11311            // decrease precision, same scale, unsafe
11312            DecimalCastTestConfig {
11313                input_prec: 10,
11314                input_scale: 5,
11315                input_repr: 9999999, // 99.99999
11316                output_prec: 6,
11317                output_scale: 5,
11318                expected_output_repr: Err("Invalid argument error: 99.99999 is too large to store in a {} of precision 6. Max is 9.99999".to_string()) // max is 9.99999
11319            },
11320            // same precision, increase scale, safe
11321            DecimalCastTestConfig {
11322                input_prec: 7,
11323                input_scale: 4,
11324                input_repr: 12345, // 1.2345
11325                output_prec: 7,
11326                output_scale: 6,
11327                expected_output_repr: Ok(1234500), // 1.234500
11328            },
11329            // same precision, increase scale, unsafe
11330            DecimalCastTestConfig {
11331                input_prec: 7,
11332                input_scale: 4,
11333                input_repr: 123456, // 12.3456
11334                output_prec: 7,
11335                output_scale: 6,
11336                expected_output_repr: Err("Invalid argument error: 12.345600 is too large to store in a {} of precision 7. Max is 9.999999".to_string()) // max is 9.99999
11337            },
11338            // same precision, decrease scale, infallible
11339            DecimalCastTestConfig {
11340                input_prec: 7,
11341                input_scale: 5,
11342                input_repr: 1234567, // 12.34567
11343                output_prec: 7,
11344                output_scale: 4,
11345                expected_output_repr: Ok(123457), // 12.3457
11346            },
11347            // same precision, same scale, infallible
11348            DecimalCastTestConfig {
11349                input_prec: 7,
11350                input_scale: 5,
11351                input_repr: 9999999, // 99.99999
11352                output_prec: 7,
11353                output_scale: 5,
11354                expected_output_repr: Ok(9999999), // 99.99999
11355            },
11356            // precision increase, input scale & output scale = 0, infallible
11357            DecimalCastTestConfig {
11358                input_prec: 7,
11359                input_scale: 0,
11360                input_repr: 1234567, // 1234567
11361                output_prec: 8,
11362                output_scale: 0,
11363                expected_output_repr: Ok(1234567), // 1234567
11364            },
11365            // precision decrease, input scale & output scale = 0, failure
11366            DecimalCastTestConfig {
11367                input_prec: 7,
11368                input_scale: 0,
11369                input_repr: 1234567, // 1234567
11370                output_prec: 6,
11371                output_scale: 0,
11372                expected_output_repr: Err("Invalid argument error: 1234567 is too large to store in a {} of precision 6. Max is 999999".to_string())
11373            },
11374            // precision decrease, input scale & output scale = 0, success
11375            DecimalCastTestConfig {
11376                input_prec: 7,
11377                input_scale: 0,
11378                input_repr: 123456, // 123456
11379                output_prec: 6,
11380                output_scale: 0,
11381                expected_output_repr: Ok(123456), // 123456
11382            },
11383        ];
11384
11385        for t in test_cases {
11386            run_decimal_cast_test_case_between_multiple_types(t);
11387        }
11388    }
11389
11390    #[test]
11391    fn test_decimal_to_decimal_increase_scale_and_precision_unchecked() {
11392        let test_cases = [
11393            DecimalCastTestConfig {
11394                input_prec: 5,
11395                input_scale: 0,
11396                input_repr: 99999,
11397                output_prec: 10,
11398                output_scale: 5,
11399                expected_output_repr: Ok(9999900000),
11400            },
11401            DecimalCastTestConfig {
11402                input_prec: 5,
11403                input_scale: 0,
11404                input_repr: -99999,
11405                output_prec: 10,
11406                output_scale: 5,
11407                expected_output_repr: Ok(-9999900000),
11408            },
11409            DecimalCastTestConfig {
11410                input_prec: 5,
11411                input_scale: 2,
11412                input_repr: 99999,
11413                output_prec: 10,
11414                output_scale: 5,
11415                expected_output_repr: Ok(99999000),
11416            },
11417            DecimalCastTestConfig {
11418                input_prec: 5,
11419                input_scale: -2,
11420                input_repr: -99999,
11421                output_prec: 10,
11422                output_scale: 3,
11423                expected_output_repr: Ok(-9999900000),
11424            },
11425            DecimalCastTestConfig {
11426                input_prec: 5,
11427                input_scale: 3,
11428                input_repr: -12345,
11429                output_prec: 6,
11430                output_scale: 5,
11431                expected_output_repr: Err("Invalid argument error: -12.34500 is too small to store in a {} of precision 6. Min is -9.99999".to_string())
11432            },
11433        ];
11434
11435        for t in test_cases {
11436            run_decimal_cast_test_case_between_multiple_types(t);
11437        }
11438    }
11439
11440    #[test]
11441    fn test_decimal_to_decimal_decrease_scale_and_precision_unchecked() {
11442        let test_cases = [
11443            DecimalCastTestConfig {
11444                input_prec: 5,
11445                input_scale: 0,
11446                input_repr: 99999,
11447                output_scale: -3,
11448                output_prec: 3,
11449                expected_output_repr: Ok(100),
11450            },
11451            DecimalCastTestConfig {
11452                input_prec: 5,
11453                input_scale: 0,
11454                input_repr: -99999,
11455                output_prec: 1,
11456                output_scale: -5,
11457                expected_output_repr: Ok(-1),
11458            },
11459            DecimalCastTestConfig {
11460                input_prec: 10,
11461                input_scale: 2,
11462                input_repr: 123456789,
11463                output_prec: 5,
11464                output_scale: -2,
11465                expected_output_repr: Ok(12346),
11466            },
11467            DecimalCastTestConfig {
11468                input_prec: 10,
11469                input_scale: 4,
11470                input_repr: -9876543210,
11471                output_prec: 7,
11472                output_scale: 0,
11473                expected_output_repr: Ok(-987654),
11474            },
11475            DecimalCastTestConfig {
11476                input_prec: 7,
11477                input_scale: 4,
11478                input_repr: 9999999,
11479                output_prec: 6,
11480                output_scale: 3,
11481                expected_output_repr:
11482                    Err("Invalid argument error: 1000.000 is too large to store in a {} of precision 6. Max is 999.999".to_string()),
11483            },
11484        ];
11485        for t in test_cases {
11486            run_decimal_cast_test_case_between_multiple_types(t);
11487        }
11488    }
11489
11490    #[test]
11491    fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
11492        let array = vec![Some(123456789)];
11493        let array = create_decimal128_array(array, 24, 2).unwrap();
11494        let input_type = DataType::Decimal128(24, 2);
11495        let output_type = DataType::Decimal128(6, 2);
11496        assert!(can_cast_types(&input_type, &output_type));
11497
11498        let options = CastOptions {
11499            safe: false,
11500            ..Default::default()
11501        };
11502        let result = cast_with_options(&array, &output_type, &options);
11503        assert_eq!(
11504            result.unwrap_err().to_string(),
11505            "Invalid argument error: 1234567.89 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
11506        );
11507    }
11508
11509    #[test]
11510    fn test_decimal_to_decimal_same_scale() {
11511        let array = vec![Some(520)];
11512        let array = create_decimal128_array(array, 4, 2).unwrap();
11513        let input_type = DataType::Decimal128(4, 2);
11514        let output_type = DataType::Decimal128(3, 2);
11515        assert!(can_cast_types(&input_type, &output_type));
11516
11517        let options = CastOptions {
11518            safe: false,
11519            ..Default::default()
11520        };
11521        let result = cast_with_options(&array, &output_type, &options);
11522        assert_eq!(
11523            result.unwrap().as_primitive::<Decimal128Type>().value(0),
11524            520
11525        );
11526
11527        // Cast 0 of decimal(3, 0) type to decimal(2, 0)
11528        assert_eq!(
11529            &cast(
11530                &create_decimal128_array(vec![Some(0)], 3, 0).unwrap(),
11531                &DataType::Decimal128(2, 0)
11532            )
11533            .unwrap(),
11534            &(Arc::new(create_decimal128_array(vec![Some(0)], 2, 0).unwrap()) as ArrayRef)
11535        );
11536    }
11537
11538    #[test]
11539    fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale() {
11540        let array = vec![Some(123456789)];
11541        let array = create_decimal128_array(array, 24, 4).unwrap();
11542        let input_type = DataType::Decimal128(24, 4);
11543        let output_type = DataType::Decimal128(6, 2);
11544        assert!(can_cast_types(&input_type, &output_type));
11545
11546        let options = CastOptions {
11547            safe: false,
11548            ..Default::default()
11549        };
11550        let result = cast_with_options(&array, &output_type, &options);
11551        assert_eq!(
11552            result.unwrap_err().to_string(),
11553            "Invalid argument error: 12345.68 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
11554        );
11555    }
11556
11557    #[test]
11558    fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale() {
11559        let array = vec![Some(123456789)];
11560        let array = create_decimal128_array(array, 24, 2).unwrap();
11561        let input_type = DataType::Decimal128(24, 2);
11562        let output_type = DataType::Decimal128(6, 3);
11563        assert!(can_cast_types(&input_type, &output_type));
11564
11565        let options = CastOptions {
11566            safe: false,
11567            ..Default::default()
11568        };
11569        let result = cast_with_options(&array, &output_type, &options);
11570        assert_eq!(
11571            result.unwrap_err().to_string(),
11572            "Invalid argument error: 1234567.890 is too large to store in a Decimal128 of precision 6. Max is 999.999"
11573        );
11574    }
11575
11576    #[test]
11577    fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type() {
11578        let array = vec![Some(123456789)];
11579        let array = create_decimal128_array(array, 24, 2).unwrap();
11580        let input_type = DataType::Decimal128(24, 2);
11581        let output_type = DataType::Decimal256(6, 2);
11582        assert!(can_cast_types(&input_type, &output_type));
11583
11584        let options = CastOptions {
11585            safe: false,
11586            ..Default::default()
11587        };
11588        let result = cast_with_options(&array, &output_type, &options).unwrap_err();
11589        assert_eq!(
11590            result.to_string(),
11591            "Invalid argument error: 1234567.89 is too large to store in a Decimal256 of precision 6. Max is 9999.99"
11592        );
11593    }
11594
11595    #[test]
11596    fn test_first_none() {
11597        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
11598            None,
11599            Some(vec![Some(1), Some(2)]),
11600        ])) as ArrayRef;
11601        let data_type =
11602            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
11603        let opt = CastOptions::default();
11604        let r = cast_with_options(&array, &data_type, &opt).unwrap();
11605
11606        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
11607            vec![None, Some(vec![Some(1), Some(2)])],
11608            2,
11609        )) as ArrayRef;
11610        assert_eq!(*fixed_array, *r);
11611    }
11612
11613    #[test]
11614    fn test_first_last_none() {
11615        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
11616            None,
11617            Some(vec![Some(1), Some(2)]),
11618            None,
11619        ])) as ArrayRef;
11620        let data_type =
11621            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
11622        let opt = CastOptions::default();
11623        let r = cast_with_options(&array, &data_type, &opt).unwrap();
11624
11625        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
11626            vec![None, Some(vec![Some(1), Some(2)]), None],
11627            2,
11628        )) as ArrayRef;
11629        assert_eq!(*fixed_array, *r);
11630    }
11631
11632    #[test]
11633    fn test_cast_decimal_error_output() {
11634        let array = Int64Array::from(vec![1]);
11635        let error = cast_with_options(
11636            &array,
11637            &DataType::Decimal32(1, 1),
11638            &CastOptions {
11639                safe: false,
11640                format_options: FormatOptions::default(),
11641            },
11642        )
11643        .unwrap_err();
11644        assert_eq!(
11645            error.to_string(),
11646            "Invalid argument error: 1.0 is too large to store in a Decimal32 of precision 1. Max is 0.9"
11647        );
11648
11649        let array = Int64Array::from(vec![-1]);
11650        let error = cast_with_options(
11651            &array,
11652            &DataType::Decimal32(1, 1),
11653            &CastOptions {
11654                safe: false,
11655                format_options: FormatOptions::default(),
11656            },
11657        )
11658        .unwrap_err();
11659        assert_eq!(
11660            error.to_string(),
11661            "Invalid argument error: -1.0 is too small to store in a Decimal32 of precision 1. Min is -0.9"
11662        );
11663    }
11664
11665    #[test]
11666    fn test_run_end_encoded_to_primitive() {
11667        // Create a RunEndEncoded array: [1, 1, 2, 2, 2, 3]
11668        let run_ends = Int32Array::from(vec![2, 5, 6]);
11669        let values = Int32Array::from(vec![1, 2, 3]);
11670        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
11671        let array_ref = Arc::new(run_array) as ArrayRef;
11672        // Cast to Int64
11673        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
11674        // Verify the result is a RunArray with Int64 values
11675        let result_run_array = cast_result.as_any().downcast_ref::<Int64Array>().unwrap();
11676        assert_eq!(
11677            result_run_array.values(),
11678            &[1i64, 1i64, 2i64, 2i64, 2i64, 3i64]
11679        );
11680    }
11681
11682    #[test]
11683    fn test_run_end_encoded_to_string() {
11684        let run_ends = Int32Array::from(vec![2, 3, 5]);
11685        let values = Int32Array::from(vec![10, 20, 30]);
11686        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
11687        let array_ref = Arc::new(run_array) as ArrayRef;
11688
11689        // Cast to String
11690        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
11691
11692        // Verify the result is a RunArray with String values
11693        let result_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
11694        // Check that values are correct
11695        assert_eq!(result_array.value(0), "10");
11696        assert_eq!(result_array.value(1), "10");
11697        assert_eq!(result_array.value(2), "20");
11698    }
11699
11700    #[test]
11701    fn test_primitive_to_run_end_encoded() {
11702        // Create an Int32 array with repeated values: [1, 1, 2, 2, 2, 3]
11703        let source_array = Int32Array::from(vec![1, 1, 2, 2, 2, 3]);
11704        let array_ref = Arc::new(source_array) as ArrayRef;
11705
11706        // Cast to RunEndEncoded<Int32, Int32>
11707        let target_type = DataType::RunEndEncoded(
11708            Arc::new(Field::new("run_ends", DataType::Int32, false)),
11709            Arc::new(Field::new("values", DataType::Int32, true)),
11710        );
11711        let cast_result = cast(&array_ref, &target_type).unwrap();
11712
11713        // Verify the result is a RunArray
11714        let result_run_array = cast_result
11715            .as_any()
11716            .downcast_ref::<RunArray<Int32Type>>()
11717            .unwrap();
11718
11719        // Check run structure: runs should end at positions [2, 5, 6]
11720        assert_eq!(result_run_array.run_ends().values(), &[2, 5, 6]);
11721
11722        // Check values: should be [1, 2, 3]
11723        let values_array = result_run_array.values().as_primitive::<Int32Type>();
11724        assert_eq!(values_array.values(), &[1, 2, 3]);
11725    }
11726
11727    #[test]
11728    fn test_primitive_to_run_end_encoded_with_nulls() {
11729        let source_array = Int32Array::from(vec![
11730            Some(1),
11731            Some(1),
11732            None,
11733            None,
11734            Some(2),
11735            Some(2),
11736            Some(3),
11737            Some(3),
11738            None,
11739            None,
11740            Some(4),
11741            Some(4),
11742            Some(5),
11743            Some(5),
11744            None,
11745            None,
11746        ]);
11747        let array_ref = Arc::new(source_array) as ArrayRef;
11748        let target_type = DataType::RunEndEncoded(
11749            Arc::new(Field::new("run_ends", DataType::Int32, false)),
11750            Arc::new(Field::new("values", DataType::Int32, true)),
11751        );
11752        let cast_result = cast(&array_ref, &target_type).unwrap();
11753        let result_run_array = cast_result
11754            .as_any()
11755            .downcast_ref::<RunArray<Int32Type>>()
11756            .unwrap();
11757        assert_eq!(
11758            result_run_array.run_ends().values(),
11759            &[2, 4, 6, 8, 10, 12, 14, 16]
11760        );
11761        assert_eq!(
11762            result_run_array
11763                .values()
11764                .as_primitive::<Int32Type>()
11765                .values(),
11766            &[1, 0, 2, 3, 0, 4, 5, 0]
11767        );
11768        assert_eq!(result_run_array.values().null_count(), 3);
11769    }
11770
11771    #[test]
11772    fn test_primitive_to_run_end_encoded_with_nulls_consecutive() {
11773        let source_array = Int64Array::from(vec![
11774            Some(1),
11775            Some(1),
11776            None,
11777            None,
11778            None,
11779            None,
11780            None,
11781            None,
11782            None,
11783            None,
11784            Some(4),
11785            Some(20),
11786            Some(500),
11787            Some(500),
11788            None,
11789            None,
11790        ]);
11791        let array_ref = Arc::new(source_array) as ArrayRef;
11792        let target_type = DataType::RunEndEncoded(
11793            Arc::new(Field::new("run_ends", DataType::Int16, false)),
11794            Arc::new(Field::new("values", DataType::Int64, true)),
11795        );
11796        let cast_result = cast(&array_ref, &target_type).unwrap();
11797        let result_run_array = cast_result
11798            .as_any()
11799            .downcast_ref::<RunArray<Int16Type>>()
11800            .unwrap();
11801        assert_eq!(
11802            result_run_array.run_ends().values(),
11803            &[2, 10, 11, 12, 14, 16]
11804        );
11805        assert_eq!(
11806            result_run_array
11807                .values()
11808                .as_primitive::<Int64Type>()
11809                .values(),
11810            &[1, 0, 4, 20, 500, 0]
11811        );
11812        assert_eq!(result_run_array.values().null_count(), 2);
11813    }
11814
11815    #[test]
11816    fn test_string_to_run_end_encoded() {
11817        // Create a String array with repeated values: ["a", "a", "b", "c", "c"]
11818        let source_array = StringArray::from(vec!["a", "a", "b", "c", "c"]);
11819        let array_ref = Arc::new(source_array) as ArrayRef;
11820
11821        // Cast to RunEndEncoded<Int32, String>
11822        let target_type = DataType::RunEndEncoded(
11823            Arc::new(Field::new("run_ends", DataType::Int32, false)),
11824            Arc::new(Field::new("values", DataType::Utf8, true)),
11825        );
11826        let cast_result = cast(&array_ref, &target_type).unwrap();
11827
11828        // Verify the result is a RunArray
11829        let result_run_array = cast_result
11830            .as_any()
11831            .downcast_ref::<RunArray<Int32Type>>()
11832            .unwrap();
11833
11834        // Check run structure: runs should end at positions [2, 3, 5]
11835        assert_eq!(result_run_array.run_ends().values(), &[2, 3, 5]);
11836
11837        // Check values: should be ["a", "b", "c"]
11838        let values_array = result_run_array.values().as_string::<i32>();
11839        assert_eq!(values_array.value(0), "a");
11840        assert_eq!(values_array.value(1), "b");
11841        assert_eq!(values_array.value(2), "c");
11842    }
11843
11844    #[test]
11845    fn test_empty_array_to_run_end_encoded() {
11846        // Create an empty Int32 array
11847        let source_array = Int32Array::from(Vec::<i32>::new());
11848        let array_ref = Arc::new(source_array) as ArrayRef;
11849
11850        // Cast to RunEndEncoded<Int32, Int32>
11851        let target_type = DataType::RunEndEncoded(
11852            Arc::new(Field::new("run_ends", DataType::Int32, false)),
11853            Arc::new(Field::new("values", DataType::Int32, true)),
11854        );
11855        let cast_result = cast(&array_ref, &target_type).unwrap();
11856
11857        // Verify the result is an empty RunArray
11858        let result_run_array = cast_result
11859            .as_any()
11860            .downcast_ref::<RunArray<Int32Type>>()
11861            .unwrap();
11862
11863        // Check that both run_ends and values are empty
11864        assert_eq!(result_run_array.run_ends().len(), 0);
11865        assert_eq!(result_run_array.values().len(), 0);
11866    }
11867
11868    #[test]
11869    fn test_run_end_encoded_with_nulls() {
11870        // Create a RunEndEncoded array with nulls: [1, 1, null, 2, 2]
11871        let run_ends = Int32Array::from(vec![2, 3, 5]);
11872        let values = Int32Array::from(vec![Some(1), None, Some(2)]);
11873        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
11874        let array_ref = Arc::new(run_array) as ArrayRef;
11875
11876        // Cast to String
11877        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
11878
11879        // Verify the result preserves nulls
11880        let result_run_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
11881        assert_eq!(result_run_array.value(0), "1");
11882        assert!(result_run_array.is_null(2));
11883        assert_eq!(result_run_array.value(4), "2");
11884    }
11885
11886    #[test]
11887    fn test_different_index_types() {
11888        // Test with Int16 index type
11889        let source_array = Int32Array::from(vec![1, 1, 2, 3, 3]);
11890        let array_ref = Arc::new(source_array) as ArrayRef;
11891
11892        let target_type = DataType::RunEndEncoded(
11893            Arc::new(Field::new("run_ends", DataType::Int16, false)),
11894            Arc::new(Field::new("values", DataType::Int32, true)),
11895        );
11896        let cast_result = cast(&array_ref, &target_type).unwrap();
11897        assert_eq!(cast_result.data_type(), &target_type);
11898
11899        // Verify the cast worked correctly: values are [1, 2, 3]
11900        // and run-ends are [2, 3, 5]
11901        let run_array = cast_result
11902            .as_any()
11903            .downcast_ref::<RunArray<Int16Type>>()
11904            .unwrap();
11905        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
11906        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
11907        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
11908        assert_eq!(run_array.run_ends().values(), &[2i16, 3i16, 5i16]);
11909
11910        // Test again with Int64 index type
11911        let target_type = DataType::RunEndEncoded(
11912            Arc::new(Field::new("run_ends", DataType::Int64, false)),
11913            Arc::new(Field::new("values", DataType::Int32, true)),
11914        );
11915        let cast_result = cast(&array_ref, &target_type).unwrap();
11916        assert_eq!(cast_result.data_type(), &target_type);
11917
11918        // Verify the cast worked correctly: values are [1, 2, 3]
11919        // and run-ends are [2, 3, 5]
11920        let run_array = cast_result
11921            .as_any()
11922            .downcast_ref::<RunArray<Int64Type>>()
11923            .unwrap();
11924        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
11925        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
11926        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
11927        assert_eq!(run_array.run_ends().values(), &[2i64, 3i64, 5i64]);
11928    }
11929
11930    #[test]
11931    fn test_unsupported_cast_to_run_end_encoded() {
11932        // Create a Struct array - complex nested type that might not be supported
11933        let field = Field::new("item", DataType::Int32, false);
11934        let struct_array = StructArray::from(vec![(
11935            Arc::new(field),
11936            Arc::new(Int32Array::from(vec![1, 2, 3])) as ArrayRef,
11937        )]);
11938        let array_ref = Arc::new(struct_array) as ArrayRef;
11939
11940        // This should fail because:
11941        // 1. The target type is not RunEndEncoded
11942        // 2. The target type is not supported for casting from StructArray
11943        let cast_result = cast(&array_ref, &DataType::FixedSizeBinary(10));
11944
11945        // Expect this to fail
11946        assert!(cast_result.is_err());
11947    }
11948
11949    /// Test casting RunEndEncoded<Int64, String> to RunEndEncoded<Int16, String> should fail
11950    #[test]
11951    fn test_cast_run_end_encoded_int64_to_int16_should_fail() {
11952        // Construct a valid REE array with Int64 run-ends
11953        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
11954        let values = StringArray::from(vec!["a", "b", "c"]);
11955
11956        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
11957        let array_ref = Arc::new(ree_array) as ArrayRef;
11958
11959        // Attempt to cast to RunEndEncoded<Int16, Utf8>
11960        let target_type = DataType::RunEndEncoded(
11961            Arc::new(Field::new("run_ends", DataType::Int16, false)),
11962            Arc::new(Field::new("values", DataType::Utf8, true)),
11963        );
11964        let cast_options = CastOptions {
11965            safe: false, // This should make it fail instead of returning nulls
11966            format_options: FormatOptions::default(),
11967        };
11968
11969        // This should fail due to run-end overflow
11970        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
11971            cast_with_options(&array_ref, &target_type, &cast_options);
11972
11973        let e = result.expect_err("Cast should have failed but succeeded");
11974        assert!(
11975            e.to_string()
11976                .contains("Cast error: Can't cast value 100000 to type Int16")
11977        );
11978    }
11979
11980    #[test]
11981    fn test_cast_run_end_encoded_int64_to_int16_with_safe_should_fail_with_null_invalid_error() {
11982        // Construct a valid REE array with Int64 run-ends
11983        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
11984        let values = StringArray::from(vec!["a", "b", "c"]);
11985
11986        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
11987        let array_ref = Arc::new(ree_array) as ArrayRef;
11988
11989        // Attempt to cast to RunEndEncoded<Int16, Utf8>
11990        let target_type = DataType::RunEndEncoded(
11991            Arc::new(Field::new("run_ends", DataType::Int16, false)),
11992            Arc::new(Field::new("values", DataType::Utf8, true)),
11993        );
11994        let cast_options = CastOptions {
11995            safe: true,
11996            format_options: FormatOptions::default(),
11997        };
11998
11999        // This fails even though safe is true because the run_ends array has null values
12000        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12001            cast_with_options(&array_ref, &target_type, &cast_options);
12002        let e = result.expect_err("Cast should have failed but succeeded");
12003        assert!(
12004            e.to_string()
12005                .contains("Invalid argument error: Found null values in run_ends array. The run_ends array should not have null values.")
12006        );
12007    }
12008
12009    /// Test casting RunEndEncoded<Int16, String> to RunEndEncoded<Int64, String> should succeed
12010    #[test]
12011    fn test_cast_run_end_encoded_int16_to_int64_should_succeed() {
12012        // Construct a valid REE array with Int16 run-ends
12013        let run_ends = Int16Array::from(vec![2, 5, 8]); // values that fit in Int16
12014        let values = StringArray::from(vec!["a", "b", "c"]);
12015
12016        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
12017        let array_ref = Arc::new(ree_array) as ArrayRef;
12018
12019        // Attempt to cast to RunEndEncoded<Int64, Utf8> (upcast should succeed)
12020        let target_type = DataType::RunEndEncoded(
12021            Arc::new(Field::new("run_ends", DataType::Int64, false)),
12022            Arc::new(Field::new("values", DataType::Utf8, true)),
12023        );
12024        let cast_options = CastOptions {
12025            safe: false,
12026            format_options: FormatOptions::default(),
12027        };
12028
12029        // This should succeed due to valid upcast
12030        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12031            cast_with_options(&array_ref, &target_type, &cast_options);
12032
12033        let array_ref = result.expect("Cast should have succeeded but failed");
12034        // Downcast to RunArray<Int64Type>
12035        let run_array = array_ref
12036            .as_any()
12037            .downcast_ref::<RunArray<Int64Type>>()
12038            .unwrap();
12039
12040        // Verify the cast worked correctly
12041        // Assert the values were cast correctly
12042        assert_eq!(run_array.run_ends().values(), &[2i64, 5i64, 8i64]);
12043        assert_eq!(run_array.values().as_string::<i32>().value(0), "a");
12044        assert_eq!(run_array.values().as_string::<i32>().value(1), "b");
12045        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
12046    }
12047
12048    #[test]
12049    fn test_cast_run_end_encoded_dictionary_to_run_end_encoded() {
12050        // Construct a valid dictionary encoded array
12051        let values = StringArray::from_iter([Some("a"), Some("b"), Some("c")]);
12052        let keys = UInt64Array::from_iter(vec![1, 1, 1, 0, 0, 0, 2, 2, 2]);
12053        let array_ref = Arc::new(DictionaryArray::new(keys, Arc::new(values))) as ArrayRef;
12054
12055        // Attempt to cast to RunEndEncoded<Int64, Utf8>
12056        let target_type = DataType::RunEndEncoded(
12057            Arc::new(Field::new("run_ends", DataType::Int64, false)),
12058            Arc::new(Field::new("values", DataType::Utf8, true)),
12059        );
12060        let cast_options = CastOptions {
12061            safe: false,
12062            format_options: FormatOptions::default(),
12063        };
12064
12065        // This should succeed
12066        let result = cast_with_options(&array_ref, &target_type, &cast_options)
12067            .expect("Cast should have succeeded but failed");
12068
12069        // Verify the cast worked correctly
12070        // Assert the values were cast correctly
12071        let run_array = result
12072            .as_any()
12073            .downcast_ref::<RunArray<Int64Type>>()
12074            .unwrap();
12075        assert_eq!(run_array.values().as_string::<i32>().value(0), "b");
12076        assert_eq!(run_array.values().as_string::<i32>().value(1), "a");
12077        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
12078
12079        // Verify the run-ends were cast correctly (run ends at 3, 6, 9)
12080        assert_eq!(run_array.run_ends().values(), &[3i64, 6i64, 9i64]);
12081    }
12082}