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            from_fields.len() == to_fields.len()
225                && from_fields.iter().zip(to_fields.iter()).all(|(f1, f2)| {
226                    // Assume that nullability between two structs are compatible, if not,
227                    // cast kernel will return error.
228                    can_cast_types(f1.data_type(), f2.data_type())
229                })
230        }
231        (Struct(_), _) => false,
232        (_, Struct(_)) => false,
233        (_, Boolean) => {
234            DataType::is_integer(from_type)
235                || DataType::is_floating(from_type)
236                || from_type == &Utf8View
237                || from_type == &Utf8
238                || from_type == &LargeUtf8
239        }
240        (Boolean, _) => {
241            DataType::is_integer(to_type)
242                || DataType::is_floating(to_type)
243                || to_type == &Utf8View
244                || to_type == &Utf8
245                || to_type == &LargeUtf8
246        }
247
248        (Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
249            true
250        }
251        (LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
252            true
253        }
254        (FixedSizeBinary(_), Binary | LargeBinary | BinaryView) => true,
255        (
256            Utf8 | LargeUtf8 | Utf8View,
257            Binary
258            | LargeBinary
259            | Utf8
260            | LargeUtf8
261            | Date32
262            | Date64
263            | Time32(Second)
264            | Time32(Millisecond)
265            | Time64(Microsecond)
266            | Time64(Nanosecond)
267            | Timestamp(Second, _)
268            | Timestamp(Millisecond, _)
269            | Timestamp(Microsecond, _)
270            | Timestamp(Nanosecond, _)
271            | Interval(_)
272            | BinaryView,
273        ) => true,
274        (Utf8 | LargeUtf8, Utf8View) => true,
275        (BinaryView, Binary | LargeBinary | Utf8 | LargeUtf8 | Utf8View) => true,
276        (Utf8View | Utf8 | LargeUtf8, _) => to_type.is_numeric() && to_type != &Float16,
277        (_, Utf8 | Utf8View | LargeUtf8) => from_type.is_primitive(),
278
279        (_, Binary | LargeBinary) => from_type.is_integer(),
280
281        // start numeric casts
282        (
283            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
284            | Float64,
285            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
286            | Float64,
287        ) => true,
288        // end numeric casts
289
290        // temporal casts
291        (Int32, Date32 | Date64 | Time32(_)) => true,
292        (Date32, Int32 | Int64) => true,
293        (Time32(_), Int32) => true,
294        (Int64, Date64 | Date32 | Time64(_)) => true,
295        (Date64, Int64 | Int32) => true,
296        (Time64(_), Int64) => true,
297        (Date32 | Date64, Date32 | Date64) => true,
298        // time casts
299        (Time32(_), Time32(_)) => true,
300        (Time32(_), Time64(_)) => true,
301        (Time64(_), Time64(_)) => true,
302        (Time64(_), Time32(to_unit)) => {
303            matches!(to_unit, Second | Millisecond)
304        }
305        (Timestamp(_, _), _) if to_type.is_numeric() => true,
306        (_, Timestamp(_, _)) if from_type.is_numeric() => true,
307        (Date64, Timestamp(_, _)) => true,
308        (Date32, Timestamp(_, _)) => true,
309        (
310            Timestamp(_, _),
311            Timestamp(_, _)
312            | Date32
313            | Date64
314            | Time32(Second)
315            | Time32(Millisecond)
316            | Time64(Microsecond)
317            | Time64(Nanosecond),
318        ) => true,
319        (_, Duration(_)) if from_type.is_numeric() => true,
320        (Duration(_), _) if to_type.is_numeric() => true,
321        (Duration(_), Duration(_)) => true,
322        (Interval(from_type), Int64) => {
323            match from_type {
324                YearMonth => true,
325                DayTime => true,
326                MonthDayNano => false, // Native type is i128
327            }
328        }
329        (Int32, Interval(to_type)) => match to_type {
330            YearMonth => true,
331            DayTime => false,
332            MonthDayNano => false,
333        },
334        (Duration(_), Interval(MonthDayNano)) => true,
335        (Interval(MonthDayNano), Duration(_)) => true,
336        (Interval(YearMonth), Interval(MonthDayNano)) => true,
337        (Interval(DayTime), Interval(MonthDayNano)) => true,
338        (_, _) => false,
339    }
340}
341
342/// Cast `array` to the provided data type and return a new Array with type `to_type`, if possible.
343///
344/// See [`cast_with_options`] for more information
345pub fn cast(array: &dyn Array, to_type: &DataType) -> Result<ArrayRef, ArrowError> {
346    cast_with_options(array, to_type, &CastOptions::default())
347}
348
349fn cast_integer_to_decimal<
350    T: ArrowPrimitiveType,
351    D: DecimalType + ArrowPrimitiveType<Native = M>,
352    M,
353>(
354    array: &PrimitiveArray<T>,
355    precision: u8,
356    scale: i8,
357    base: M,
358    cast_options: &CastOptions,
359) -> Result<ArrayRef, ArrowError>
360where
361    <T as ArrowPrimitiveType>::Native: AsPrimitive<M>,
362    M: ArrowNativeTypeOp,
363{
364    let scale_factor = base.pow_checked(scale.unsigned_abs() as u32).map_err(|_| {
365        ArrowError::CastError(format!(
366            "Cannot cast to {:?}({}, {}). The scale causes overflow.",
367            D::PREFIX,
368            precision,
369            scale,
370        ))
371    })?;
372
373    let array = if scale < 0 {
374        match cast_options.safe {
375            true => array.unary_opt::<_, D>(|v| {
376                v.as_()
377                    .div_checked(scale_factor)
378                    .ok()
379                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
380            }),
381            false => array.try_unary::<_, D, _>(|v| {
382                v.as_()
383                    .div_checked(scale_factor)
384                    .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|_| v))
385            })?,
386        }
387    } else {
388        match cast_options.safe {
389            true => array.unary_opt::<_, D>(|v| {
390                v.as_()
391                    .mul_checked(scale_factor)
392                    .ok()
393                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
394            }),
395            false => array.try_unary::<_, D, _>(|v| {
396                v.as_()
397                    .mul_checked(scale_factor)
398                    .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|_| v))
399            })?,
400        }
401    };
402
403    Ok(Arc::new(array.with_precision_and_scale(precision, scale)?))
404}
405
406/// Cast the array from interval year month to month day nano
407fn cast_interval_year_month_to_interval_month_day_nano(
408    array: &dyn Array,
409    _cast_options: &CastOptions,
410) -> Result<ArrayRef, ArrowError> {
411    let array = array.as_primitive::<IntervalYearMonthType>();
412
413    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
414        let months = IntervalYearMonthType::to_months(v);
415        IntervalMonthDayNanoType::make_value(months, 0, 0)
416    })))
417}
418
419/// Cast the array from interval day time to month day nano
420fn cast_interval_day_time_to_interval_month_day_nano(
421    array: &dyn Array,
422    _cast_options: &CastOptions,
423) -> Result<ArrayRef, ArrowError> {
424    let array = array.as_primitive::<IntervalDayTimeType>();
425    let mul = 1_000_000;
426
427    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
428        let (days, ms) = IntervalDayTimeType::to_parts(v);
429        IntervalMonthDayNanoType::make_value(0, days, ms as i64 * mul)
430    })))
431}
432
433/// Cast the array from interval to duration
434fn cast_month_day_nano_to_duration<D: ArrowTemporalType<Native = i64>>(
435    array: &dyn Array,
436    cast_options: &CastOptions,
437) -> Result<ArrayRef, ArrowError> {
438    let array = array.as_primitive::<IntervalMonthDayNanoType>();
439    let scale = match D::DATA_TYPE {
440        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
441        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
442        DataType::Duration(TimeUnit::Microsecond) => 1_000,
443        DataType::Duration(TimeUnit::Nanosecond) => 1,
444        _ => unreachable!(),
445    };
446
447    if cast_options.safe {
448        let iter = array.iter().map(|v| {
449            v.and_then(|v| (v.days == 0 && v.months == 0).then_some(v.nanoseconds / scale))
450        });
451        Ok(Arc::new(unsafe {
452            PrimitiveArray::<D>::from_trusted_len_iter(iter)
453        }))
454    } else {
455        let vec = array
456            .iter()
457            .map(|v| {
458                v.map(|v| match v.days == 0 && v.months == 0 {
459                    true => Ok((v.nanoseconds) / scale),
460                    _ => Err(ArrowError::ComputeError(
461                        "Cannot convert interval containing non-zero months or days to duration"
462                            .to_string(),
463                    )),
464                })
465                .transpose()
466            })
467            .collect::<Result<Vec<_>, _>>()?;
468        Ok(Arc::new(unsafe {
469            PrimitiveArray::<D>::from_trusted_len_iter(vec.iter())
470        }))
471    }
472}
473
474/// Cast the array from duration and interval
475fn cast_duration_to_interval<D: ArrowTemporalType<Native = i64>>(
476    array: &dyn Array,
477    cast_options: &CastOptions,
478) -> Result<ArrayRef, ArrowError> {
479    let array = array
480        .as_any()
481        .downcast_ref::<PrimitiveArray<D>>()
482        .ok_or_else(|| {
483            ArrowError::ComputeError(
484                "Internal Error: Cannot cast duration to DurationArray of expected type"
485                    .to_string(),
486            )
487        })?;
488
489    let scale = match array.data_type() {
490        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
491        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
492        DataType::Duration(TimeUnit::Microsecond) => 1_000,
493        DataType::Duration(TimeUnit::Nanosecond) => 1,
494        _ => unreachable!(),
495    };
496
497    if cast_options.safe {
498        let iter = array.iter().map(|v| {
499            v.and_then(|v| {
500                v.checked_mul(scale)
501                    .map(|v| IntervalMonthDayNano::new(0, 0, v))
502            })
503        });
504        Ok(Arc::new(unsafe {
505            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(iter)
506        }))
507    } else {
508        let vec = array
509            .iter()
510            .map(|v| {
511                v.map(|v| {
512                    if let Ok(v) = v.mul_checked(scale) {
513                        Ok(IntervalMonthDayNano::new(0, 0, v))
514                    } else {
515                        Err(ArrowError::ComputeError(format!(
516                            "Cannot cast to {:?}. Overflowing on {:?}",
517                            IntervalMonthDayNanoType::DATA_TYPE,
518                            v
519                        )))
520                    }
521                })
522                .transpose()
523            })
524            .collect::<Result<Vec<_>, _>>()?;
525        Ok(Arc::new(unsafe {
526            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(vec.iter())
527        }))
528    }
529}
530
531/// Cast the primitive array using [`PrimitiveArray::reinterpret_cast`]
532fn cast_reinterpret_arrays<I: ArrowPrimitiveType, O: ArrowPrimitiveType<Native = I::Native>>(
533    array: &dyn Array,
534) -> Result<ArrayRef, ArrowError> {
535    Ok(Arc::new(array.as_primitive::<I>().reinterpret_cast::<O>()))
536}
537
538fn make_timestamp_array(
539    array: &PrimitiveArray<Int64Type>,
540    unit: TimeUnit,
541    tz: Option<Arc<str>>,
542) -> ArrayRef {
543    match unit {
544        TimeUnit::Second => Arc::new(
545            array
546                .reinterpret_cast::<TimestampSecondType>()
547                .with_timezone_opt(tz),
548        ),
549        TimeUnit::Millisecond => Arc::new(
550            array
551                .reinterpret_cast::<TimestampMillisecondType>()
552                .with_timezone_opt(tz),
553        ),
554        TimeUnit::Microsecond => Arc::new(
555            array
556                .reinterpret_cast::<TimestampMicrosecondType>()
557                .with_timezone_opt(tz),
558        ),
559        TimeUnit::Nanosecond => Arc::new(
560            array
561                .reinterpret_cast::<TimestampNanosecondType>()
562                .with_timezone_opt(tz),
563        ),
564    }
565}
566
567fn make_duration_array(array: &PrimitiveArray<Int64Type>, unit: TimeUnit) -> ArrayRef {
568    match unit {
569        TimeUnit::Second => Arc::new(array.reinterpret_cast::<DurationSecondType>()),
570        TimeUnit::Millisecond => Arc::new(array.reinterpret_cast::<DurationMillisecondType>()),
571        TimeUnit::Microsecond => Arc::new(array.reinterpret_cast::<DurationMicrosecondType>()),
572        TimeUnit::Nanosecond => Arc::new(array.reinterpret_cast::<DurationNanosecondType>()),
573    }
574}
575
576fn as_time_res_with_timezone<T: ArrowPrimitiveType>(
577    v: i64,
578    tz: Option<Tz>,
579) -> Result<NaiveTime, ArrowError> {
580    let time = match tz {
581        Some(tz) => as_datetime_with_timezone::<T>(v, tz).map(|d| d.time()),
582        None => as_datetime::<T>(v).map(|d| d.time()),
583    };
584
585    time.ok_or_else(|| {
586        ArrowError::CastError(format!(
587            "Failed to create naive time with {} {}",
588            std::any::type_name::<T>(),
589            v
590        ))
591    })
592}
593
594fn timestamp_to_date32<T: ArrowTimestampType>(
595    array: &PrimitiveArray<T>,
596) -> Result<ArrayRef, ArrowError> {
597    let err = |x: i64| {
598        ArrowError::CastError(format!(
599            "Cannot convert {} {x} to datetime",
600            std::any::type_name::<T>()
601        ))
602    };
603
604    let array: Date32Array = match array.timezone() {
605        Some(tz) => {
606            let tz: Tz = tz.parse()?;
607            array.try_unary(|x| {
608                as_datetime_with_timezone::<T>(x, tz)
609                    .ok_or_else(|| err(x))
610                    .map(|d| Date32Type::from_naive_date(d.date_naive()))
611            })?
612        }
613        None => array.try_unary(|x| {
614            as_datetime::<T>(x)
615                .ok_or_else(|| err(x))
616                .map(|d| Date32Type::from_naive_date(d.date()))
617        })?,
618    };
619    Ok(Arc::new(array))
620}
621
622/// Try to cast `array` to `to_type` if possible.
623///
624/// Returns a new Array with type `to_type` if possible.
625///
626/// Accepts [`CastOptions`] to specify cast behavior. See also [`cast()`].
627///
628/// # Behavior
629/// * `Boolean` to `Utf8`: `true` => '1', `false` => `0`
630/// * `Utf8` to `Boolean`: `true`, `yes`, `on`, `1` => `true`, `false`, `no`, `off`, `0` => `false`,
631///   short variants are accepted, other strings return null or error
632/// * `Utf8` to Numeric: strings that can't be parsed to numbers return null, float strings
633///   in integer casts return null
634/// * Numeric to `Boolean`: 0 returns `false`, any other value returns `true`
635/// * `List` to `List`: the underlying data type is cast
636/// * `List` to `FixedSizeList`: the underlying data type is cast. If safe is true and a list element
637///   has the wrong length it will be replaced with NULL, otherwise an error will be returned
638/// * Primitive to `List`: a list array with 1 value per slot is created
639/// * `Date32` and `Date64`: precision lost when going to higher interval
640/// * `Time32 and `Time64`: precision lost when going to higher interval
641/// * `Timestamp` and `Date{32|64}`: precision lost when going to higher interval
642/// * Temporal to/from backing Primitive: zero-copy with data type change
643/// * `Float32/Float64` to `Decimal(precision, scale)` rounds to the `scale` decimals
644///   (i.e. casting `6.4999` to `Decimal(10, 1)` becomes `6.5`).
645/// * `Decimal` to `Float32/Float64` is lossy and values outside the representable
646///   range become `INFINITY` or `-INFINITY` without error.
647///
648/// Unsupported Casts (check with `can_cast_types` before calling):
649/// * To or from `StructArray`
650/// * `List` to `Primitive`
651/// * `Interval` and `Duration`
652///
653/// # Durations and Intervals
654///
655/// Casting integer types directly to interval types such as
656/// [`IntervalMonthDayNano`] is not supported because the meaning of the integer
657/// is ambiguous. For example, the integer  could represent either nanoseconds
658/// or months.
659///
660/// To cast an integer type to an interval type, first convert to a Duration
661/// type, and then cast that to the desired interval type.
662///
663/// For example, to convert an `Int64` representing nanoseconds to an
664/// `IntervalMonthDayNano` you would first convert the `Int64` to a
665/// `DurationNanoseconds`, and then cast that to `IntervalMonthDayNano`.
666///
667/// # Timestamps and Timezones
668///
669/// Timestamps are stored with an optional timezone in Arrow.
670///
671/// ## Casting timestamps to a timestamp without timezone / UTC
672/// ```
673/// # use arrow_array::Int64Array;
674/// # use arrow_array::types::TimestampSecondType;
675/// # use arrow_cast::{cast, display};
676/// # use arrow_array::cast::AsArray;
677/// # use arrow_schema::{DataType, TimeUnit};
678/// // can use "UTC" if chrono-tz feature is enabled, here use offset based timezone
679/// let data_type = DataType::Timestamp(TimeUnit::Second, None);
680/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
681/// let b = cast(&a, &data_type).unwrap();
682/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
683/// assert_eq!(2_000_000_000, b.value(1)); // values are the same as the type has no timezone
684/// // use display to show them (note has no trailing Z)
685/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
686/// ```
687///
688/// ## Casting timestamps to a timestamp with timezone
689///
690/// Similarly to the previous example, if you cast numeric values to a timestamp
691/// with timezone, the cast kernel will not change the underlying values
692/// but display and other functions will interpret them as being in the provided timezone.
693///
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 "Americas/New_York" if chrono-tz feature is enabled, here use offset based timezone
701/// let data_type = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
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 still the same
706/// // displayed in the target timezone (note the offset -05:00)
707/// assert_eq!("2033-05-17T22:33:20-05:00", display::array_value_to_string(&b, 1).unwrap());
708/// ```
709/// # Casting timestamps without timezone to timestamps with timezone
710///
711/// When casting from a timestamp without timezone to a timestamp with
712/// timezone, the cast kernel interprets the timestamp values as being in
713/// the destination timezone and then adjusts the underlying value to UTC as required
714///
715/// However, note that when casting from a timestamp with timezone BACK to a
716/// timestamp without timezone the cast kernel does not adjust the values.
717///
718/// Thus round trip casting a timestamp without timezone to a timestamp with
719/// timezone and back to a timestamp without timezone results in different
720/// values than the starting values.
721///
722/// ```
723/// # use arrow_array::Int64Array;
724/// # use arrow_array::types::{TimestampSecondType};
725/// # use arrow_cast::{cast, display};
726/// # use arrow_array::cast::AsArray;
727/// # use arrow_schema::{DataType, TimeUnit};
728/// let data_type  = DataType::Timestamp(TimeUnit::Second, None);
729/// let data_type_tz = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
730/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
731/// let b = cast(&a, &data_type).unwrap(); // cast to timestamp without timezone
732/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
733/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
734/// // displayed without a timezone (note lack of offset or Z)
735/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
736///
737/// // Convert timestamps without a timezone to timestamps with a timezone
738/// let c = cast(&b, &data_type_tz).unwrap();
739/// let c = c.as_primitive::<TimestampSecondType>(); // downcast to result type
740/// assert_eq!(2_000_018_000, c.value(1)); // value has been adjusted by offset
741/// // displayed with the target timezone offset (-05:00)
742/// assert_eq!("2033-05-18T03:33:20-05:00", display::array_value_to_string(&c, 1).unwrap());
743///
744/// // Convert from timestamp with timezone back to timestamp without timezone
745/// let d = cast(&c, &data_type).unwrap();
746/// let d = d.as_primitive::<TimestampSecondType>(); // downcast to result type
747/// assert_eq!(2_000_018_000, d.value(1)); // value has not been adjusted
748/// // NOTE: the timestamp is adjusted (08:33:20 instead of 03:33:20 as in previous example)
749/// assert_eq!("2033-05-18T08:33:20", display::array_value_to_string(&d, 1).unwrap());
750/// ```
751pub fn cast_with_options(
752    array: &dyn Array,
753    to_type: &DataType,
754    cast_options: &CastOptions,
755) -> Result<ArrayRef, ArrowError> {
756    use DataType::*;
757    let from_type = array.data_type();
758    // clone array if types are the same
759    if from_type == to_type {
760        return Ok(make_array(array.to_data()));
761    }
762    match (from_type, to_type) {
763        (
764            Null,
765            Boolean
766            | Int8
767            | UInt8
768            | Int16
769            | UInt16
770            | Float16
771            | Int32
772            | UInt32
773            | Float32
774            | Date32
775            | Time32(_)
776            | Int64
777            | UInt64
778            | Float64
779            | Date64
780            | Timestamp(_, _)
781            | Time64(_)
782            | Duration(_)
783            | Interval(_)
784            | FixedSizeBinary(_)
785            | Binary
786            | Utf8
787            | LargeBinary
788            | LargeUtf8
789            | BinaryView
790            | Utf8View
791            | List(_)
792            | LargeList(_)
793            | FixedSizeList(_, _)
794            | Struct(_)
795            | Map(_, _)
796            | Dictionary(_, _),
797        ) => Ok(new_null_array(to_type, array.len())),
798        (RunEndEncoded(index_type, _), _) => match index_type.data_type() {
799            Int16 => run_end_encoded_cast::<Int16Type>(array, to_type, cast_options),
800            Int32 => run_end_encoded_cast::<Int32Type>(array, to_type, cast_options),
801            Int64 => run_end_encoded_cast::<Int64Type>(array, to_type, cast_options),
802            _ => Err(ArrowError::CastError(format!(
803                "Casting from run end encoded type {from_type:?} to {to_type:?} not supported",
804            ))),
805        },
806        (_, RunEndEncoded(index_type, value_type)) => {
807            let array_ref = make_array(array.to_data());
808            match index_type.data_type() {
809                Int16 => cast_to_run_end_encoded::<Int16Type>(
810                    &array_ref,
811                    value_type.data_type(),
812                    cast_options,
813                ),
814                Int32 => cast_to_run_end_encoded::<Int32Type>(
815                    &array_ref,
816                    value_type.data_type(),
817                    cast_options,
818                ),
819                Int64 => cast_to_run_end_encoded::<Int64Type>(
820                    &array_ref,
821                    value_type.data_type(),
822                    cast_options,
823                ),
824                _ => Err(ArrowError::CastError(format!(
825                    "Casting from type {from_type:?} to run end encoded type {to_type:?} not supported",
826                ))),
827            }
828        }
829        (Dictionary(index_type, _), _) => match **index_type {
830            Int8 => dictionary_cast::<Int8Type>(array, to_type, cast_options),
831            Int16 => dictionary_cast::<Int16Type>(array, to_type, cast_options),
832            Int32 => dictionary_cast::<Int32Type>(array, to_type, cast_options),
833            Int64 => dictionary_cast::<Int64Type>(array, to_type, cast_options),
834            UInt8 => dictionary_cast::<UInt8Type>(array, to_type, cast_options),
835            UInt16 => dictionary_cast::<UInt16Type>(array, to_type, cast_options),
836            UInt32 => dictionary_cast::<UInt32Type>(array, to_type, cast_options),
837            UInt64 => dictionary_cast::<UInt64Type>(array, to_type, cast_options),
838            _ => Err(ArrowError::CastError(format!(
839                "Casting from dictionary type {from_type} to {to_type} not supported",
840            ))),
841        },
842        (_, Dictionary(index_type, value_type)) => match **index_type {
843            Int8 => cast_to_dictionary::<Int8Type>(array, value_type, cast_options),
844            Int16 => cast_to_dictionary::<Int16Type>(array, value_type, cast_options),
845            Int32 => cast_to_dictionary::<Int32Type>(array, value_type, cast_options),
846            Int64 => cast_to_dictionary::<Int64Type>(array, value_type, cast_options),
847            UInt8 => cast_to_dictionary::<UInt8Type>(array, value_type, cast_options),
848            UInt16 => cast_to_dictionary::<UInt16Type>(array, value_type, cast_options),
849            UInt32 => cast_to_dictionary::<UInt32Type>(array, value_type, cast_options),
850            UInt64 => cast_to_dictionary::<UInt64Type>(array, value_type, cast_options),
851            _ => Err(ArrowError::CastError(format!(
852                "Casting from type {from_type} to dictionary type {to_type} not supported",
853            ))),
854        },
855        (List(_), List(to)) => cast_list_values::<i32>(array, to, cast_options),
856        (LargeList(_), LargeList(to)) => cast_list_values::<i64>(array, to, cast_options),
857        (List(_), LargeList(list_to)) => cast_list::<i32, i64>(array, list_to, cast_options),
858        (LargeList(_), List(list_to)) => cast_list::<i64, i32>(array, list_to, cast_options),
859        (List(_), FixedSizeList(field, size)) => {
860            let array = array.as_list::<i32>();
861            cast_list_to_fixed_size_list::<i32>(array, field, *size, cast_options)
862        }
863        (LargeList(_), FixedSizeList(field, size)) => {
864            let array = array.as_list::<i64>();
865            cast_list_to_fixed_size_list::<i64>(array, field, *size, cast_options)
866        }
867        (List(_) | LargeList(_), _) => match to_type {
868            Utf8 => value_to_string::<i32>(array, cast_options),
869            LargeUtf8 => value_to_string::<i64>(array, cast_options),
870            _ => Err(ArrowError::CastError(
871                "Cannot cast list to non-list data types".to_string(),
872            )),
873        },
874        (FixedSizeList(list_from, size), List(list_to)) => {
875            if list_to.data_type() != list_from.data_type() {
876                // To transform inner type, can first cast to FSL with new inner type.
877                let fsl_to = DataType::FixedSizeList(list_to.clone(), *size);
878                let array = cast_with_options(array, &fsl_to, cast_options)?;
879                cast_fixed_size_list_to_list::<i32>(array.as_ref())
880            } else {
881                cast_fixed_size_list_to_list::<i32>(array)
882            }
883        }
884        (FixedSizeList(list_from, size), LargeList(list_to)) => {
885            if list_to.data_type() != list_from.data_type() {
886                // To transform inner type, can first cast to FSL with new inner type.
887                let fsl_to = DataType::FixedSizeList(list_to.clone(), *size);
888                let array = cast_with_options(array, &fsl_to, cast_options)?;
889                cast_fixed_size_list_to_list::<i64>(array.as_ref())
890            } else {
891                cast_fixed_size_list_to_list::<i64>(array)
892            }
893        }
894        (FixedSizeList(_, size_from), FixedSizeList(list_to, size_to)) => {
895            if size_from != size_to {
896                return Err(ArrowError::CastError(
897                    "cannot cast fixed-size-list to fixed-size-list with different size".into(),
898                ));
899            }
900            let array = array.as_any().downcast_ref::<FixedSizeListArray>().unwrap();
901            let values = cast_with_options(array.values(), list_to.data_type(), cast_options)?;
902            Ok(Arc::new(FixedSizeListArray::try_new(
903                list_to.clone(),
904                *size_from,
905                values,
906                array.nulls().cloned(),
907            )?))
908        }
909        (_, List(to)) => cast_values_to_list::<i32>(array, to, cast_options),
910        (_, LargeList(to)) => cast_values_to_list::<i64>(array, to, cast_options),
911        (_, FixedSizeList(to, size)) if *size == 1 => {
912            cast_values_to_fixed_size_list(array, to, *size, cast_options)
913        }
914        (FixedSizeList(_, size), _) if *size == 1 => {
915            cast_single_element_fixed_size_list_to_values(array, to_type, cast_options)
916        }
917        (Map(_, ordered1), Map(_, ordered2)) if ordered1 == ordered2 => {
918            cast_map_values(array.as_map(), to_type, cast_options, ordered1.to_owned())
919        }
920        // Decimal to decimal, same width
921        (Decimal32(p1, s1), Decimal32(p2, s2)) => {
922            cast_decimal_to_decimal_same_type::<Decimal32Type>(
923                array.as_primitive(),
924                *p1,
925                *s1,
926                *p2,
927                *s2,
928                cast_options,
929            )
930        }
931        (Decimal64(p1, s1), Decimal64(p2, s2)) => {
932            cast_decimal_to_decimal_same_type::<Decimal64Type>(
933                array.as_primitive(),
934                *p1,
935                *s1,
936                *p2,
937                *s2,
938                cast_options,
939            )
940        }
941        (Decimal128(p1, s1), Decimal128(p2, s2)) => {
942            cast_decimal_to_decimal_same_type::<Decimal128Type>(
943                array.as_primitive(),
944                *p1,
945                *s1,
946                *p2,
947                *s2,
948                cast_options,
949            )
950        }
951        (Decimal256(p1, s1), Decimal256(p2, s2)) => {
952            cast_decimal_to_decimal_same_type::<Decimal256Type>(
953                array.as_primitive(),
954                *p1,
955                *s1,
956                *p2,
957                *s2,
958                cast_options,
959            )
960        }
961        // Decimal to decimal, different width
962        (Decimal32(p1, s1), Decimal64(p2, s2)) => {
963            cast_decimal_to_decimal::<Decimal32Type, Decimal64Type>(
964                array.as_primitive(),
965                *p1,
966                *s1,
967                *p2,
968                *s2,
969                cast_options,
970            )
971        }
972        (Decimal32(p1, s1), Decimal128(p2, s2)) => {
973            cast_decimal_to_decimal::<Decimal32Type, Decimal128Type>(
974                array.as_primitive(),
975                *p1,
976                *s1,
977                *p2,
978                *s2,
979                cast_options,
980            )
981        }
982        (Decimal32(p1, s1), Decimal256(p2, s2)) => {
983            cast_decimal_to_decimal::<Decimal32Type, Decimal256Type>(
984                array.as_primitive(),
985                *p1,
986                *s1,
987                *p2,
988                *s2,
989                cast_options,
990            )
991        }
992        (Decimal64(p1, s1), Decimal32(p2, s2)) => {
993            cast_decimal_to_decimal::<Decimal64Type, Decimal32Type>(
994                array.as_primitive(),
995                *p1,
996                *s1,
997                *p2,
998                *s2,
999                cast_options,
1000            )
1001        }
1002        (Decimal64(p1, s1), Decimal128(p2, s2)) => {
1003            cast_decimal_to_decimal::<Decimal64Type, Decimal128Type>(
1004                array.as_primitive(),
1005                *p1,
1006                *s1,
1007                *p2,
1008                *s2,
1009                cast_options,
1010            )
1011        }
1012        (Decimal64(p1, s1), Decimal256(p2, s2)) => {
1013            cast_decimal_to_decimal::<Decimal64Type, Decimal256Type>(
1014                array.as_primitive(),
1015                *p1,
1016                *s1,
1017                *p2,
1018                *s2,
1019                cast_options,
1020            )
1021        }
1022        (Decimal128(p1, s1), Decimal32(p2, s2)) => {
1023            cast_decimal_to_decimal::<Decimal128Type, Decimal32Type>(
1024                array.as_primitive(),
1025                *p1,
1026                *s1,
1027                *p2,
1028                *s2,
1029                cast_options,
1030            )
1031        }
1032        (Decimal128(p1, s1), Decimal64(p2, s2)) => {
1033            cast_decimal_to_decimal::<Decimal128Type, Decimal64Type>(
1034                array.as_primitive(),
1035                *p1,
1036                *s1,
1037                *p2,
1038                *s2,
1039                cast_options,
1040            )
1041        }
1042        (Decimal128(p1, s1), Decimal256(p2, s2)) => {
1043            cast_decimal_to_decimal::<Decimal128Type, Decimal256Type>(
1044                array.as_primitive(),
1045                *p1,
1046                *s1,
1047                *p2,
1048                *s2,
1049                cast_options,
1050            )
1051        }
1052        (Decimal256(p1, s1), Decimal32(p2, s2)) => {
1053            cast_decimal_to_decimal::<Decimal256Type, Decimal32Type>(
1054                array.as_primitive(),
1055                *p1,
1056                *s1,
1057                *p2,
1058                *s2,
1059                cast_options,
1060            )
1061        }
1062        (Decimal256(p1, s1), Decimal64(p2, s2)) => {
1063            cast_decimal_to_decimal::<Decimal256Type, Decimal64Type>(
1064                array.as_primitive(),
1065                *p1,
1066                *s1,
1067                *p2,
1068                *s2,
1069                cast_options,
1070            )
1071        }
1072        (Decimal256(p1, s1), Decimal128(p2, s2)) => {
1073            cast_decimal_to_decimal::<Decimal256Type, Decimal128Type>(
1074                array.as_primitive(),
1075                *p1,
1076                *s1,
1077                *p2,
1078                *s2,
1079                cast_options,
1080            )
1081        }
1082        // Decimal to non-decimal
1083        (Decimal32(_, scale), _) if !to_type.is_temporal() => {
1084            cast_from_decimal::<Decimal32Type, _>(
1085                array,
1086                10_i32,
1087                scale,
1088                from_type,
1089                to_type,
1090                |x: i32| x as f64,
1091                cast_options,
1092            )
1093        }
1094        (Decimal64(_, scale), _) if !to_type.is_temporal() => {
1095            cast_from_decimal::<Decimal64Type, _>(
1096                array,
1097                10_i64,
1098                scale,
1099                from_type,
1100                to_type,
1101                |x: i64| x as f64,
1102                cast_options,
1103            )
1104        }
1105        (Decimal128(_, scale), _) if !to_type.is_temporal() => {
1106            cast_from_decimal::<Decimal128Type, _>(
1107                array,
1108                10_i128,
1109                scale,
1110                from_type,
1111                to_type,
1112                |x: i128| x as f64,
1113                cast_options,
1114            )
1115        }
1116        (Decimal256(_, scale), _) if !to_type.is_temporal() => {
1117            cast_from_decimal::<Decimal256Type, _>(
1118                array,
1119                i256::from_i128(10_i128),
1120                scale,
1121                from_type,
1122                to_type,
1123                |x: i256| x.to_f64().expect("All i256 values fit in f64"),
1124                cast_options,
1125            )
1126        }
1127        // Non-decimal to decimal
1128        (_, Decimal32(precision, scale)) if !from_type.is_temporal() => {
1129            cast_to_decimal::<Decimal32Type, _>(
1130                array,
1131                10_i32,
1132                precision,
1133                scale,
1134                from_type,
1135                to_type,
1136                cast_options,
1137            )
1138        }
1139        (_, Decimal64(precision, scale)) if !from_type.is_temporal() => {
1140            cast_to_decimal::<Decimal64Type, _>(
1141                array,
1142                10_i64,
1143                precision,
1144                scale,
1145                from_type,
1146                to_type,
1147                cast_options,
1148            )
1149        }
1150        (_, Decimal128(precision, scale)) if !from_type.is_temporal() => {
1151            cast_to_decimal::<Decimal128Type, _>(
1152                array,
1153                10_i128,
1154                precision,
1155                scale,
1156                from_type,
1157                to_type,
1158                cast_options,
1159            )
1160        }
1161        (_, Decimal256(precision, scale)) if !from_type.is_temporal() => {
1162            cast_to_decimal::<Decimal256Type, _>(
1163                array,
1164                i256::from_i128(10_i128),
1165                precision,
1166                scale,
1167                from_type,
1168                to_type,
1169                cast_options,
1170            )
1171        }
1172        (Struct(_), Struct(to_fields)) => {
1173            let array = array.as_struct();
1174            let fields = array
1175                .columns()
1176                .iter()
1177                .zip(to_fields.iter())
1178                .map(|(l, field)| cast_with_options(l, field.data_type(), cast_options))
1179                .collect::<Result<Vec<ArrayRef>, ArrowError>>()?;
1180            let array = StructArray::try_new(to_fields.clone(), fields, array.nulls().cloned())?;
1181            Ok(Arc::new(array) as ArrayRef)
1182        }
1183        (Struct(_), _) => Err(ArrowError::CastError(format!(
1184            "Casting from {from_type} to {to_type} not supported"
1185        ))),
1186        (_, Struct(_)) => Err(ArrowError::CastError(format!(
1187            "Casting from {from_type} to {to_type} not supported"
1188        ))),
1189        (_, Boolean) => match from_type {
1190            UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
1191            UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
1192            UInt32 => cast_numeric_to_bool::<UInt32Type>(array),
1193            UInt64 => cast_numeric_to_bool::<UInt64Type>(array),
1194            Int8 => cast_numeric_to_bool::<Int8Type>(array),
1195            Int16 => cast_numeric_to_bool::<Int16Type>(array),
1196            Int32 => cast_numeric_to_bool::<Int32Type>(array),
1197            Int64 => cast_numeric_to_bool::<Int64Type>(array),
1198            Float16 => cast_numeric_to_bool::<Float16Type>(array),
1199            Float32 => cast_numeric_to_bool::<Float32Type>(array),
1200            Float64 => cast_numeric_to_bool::<Float64Type>(array),
1201            Utf8View => cast_utf8view_to_boolean(array, cast_options),
1202            Utf8 => cast_utf8_to_boolean::<i32>(array, cast_options),
1203            LargeUtf8 => cast_utf8_to_boolean::<i64>(array, cast_options),
1204            _ => Err(ArrowError::CastError(format!(
1205                "Casting from {from_type} to {to_type} not supported",
1206            ))),
1207        },
1208        (Boolean, _) => match to_type {
1209            UInt8 => cast_bool_to_numeric::<UInt8Type>(array, cast_options),
1210            UInt16 => cast_bool_to_numeric::<UInt16Type>(array, cast_options),
1211            UInt32 => cast_bool_to_numeric::<UInt32Type>(array, cast_options),
1212            UInt64 => cast_bool_to_numeric::<UInt64Type>(array, cast_options),
1213            Int8 => cast_bool_to_numeric::<Int8Type>(array, cast_options),
1214            Int16 => cast_bool_to_numeric::<Int16Type>(array, cast_options),
1215            Int32 => cast_bool_to_numeric::<Int32Type>(array, cast_options),
1216            Int64 => cast_bool_to_numeric::<Int64Type>(array, cast_options),
1217            Float16 => cast_bool_to_numeric::<Float16Type>(array, cast_options),
1218            Float32 => cast_bool_to_numeric::<Float32Type>(array, cast_options),
1219            Float64 => cast_bool_to_numeric::<Float64Type>(array, cast_options),
1220            Utf8View => value_to_string_view(array, cast_options),
1221            Utf8 => value_to_string::<i32>(array, cast_options),
1222            LargeUtf8 => value_to_string::<i64>(array, cast_options),
1223            _ => Err(ArrowError::CastError(format!(
1224                "Casting from {from_type} to {to_type} not supported",
1225            ))),
1226        },
1227        (Utf8, _) => match to_type {
1228            UInt8 => parse_string::<UInt8Type, i32>(array, cast_options),
1229            UInt16 => parse_string::<UInt16Type, i32>(array, cast_options),
1230            UInt32 => parse_string::<UInt32Type, i32>(array, cast_options),
1231            UInt64 => parse_string::<UInt64Type, i32>(array, cast_options),
1232            Int8 => parse_string::<Int8Type, i32>(array, cast_options),
1233            Int16 => parse_string::<Int16Type, i32>(array, cast_options),
1234            Int32 => parse_string::<Int32Type, i32>(array, cast_options),
1235            Int64 => parse_string::<Int64Type, i32>(array, cast_options),
1236            Float32 => parse_string::<Float32Type, i32>(array, cast_options),
1237            Float64 => parse_string::<Float64Type, i32>(array, cast_options),
1238            Date32 => parse_string::<Date32Type, i32>(array, cast_options),
1239            Date64 => parse_string::<Date64Type, i32>(array, cast_options),
1240            Binary => Ok(Arc::new(BinaryArray::from(
1241                array.as_string::<i32>().clone(),
1242            ))),
1243            LargeBinary => {
1244                let binary = BinaryArray::from(array.as_string::<i32>().clone());
1245                cast_byte_container::<BinaryType, LargeBinaryType>(&binary)
1246            }
1247            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i32>()))),
1248            BinaryView => Ok(Arc::new(
1249                StringViewArray::from(array.as_string::<i32>()).to_binary_view(),
1250            )),
1251            LargeUtf8 => cast_byte_container::<Utf8Type, LargeUtf8Type>(array),
1252            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i32>(array, cast_options),
1253            Time32(TimeUnit::Millisecond) => {
1254                parse_string::<Time32MillisecondType, i32>(array, cast_options)
1255            }
1256            Time64(TimeUnit::Microsecond) => {
1257                parse_string::<Time64MicrosecondType, i32>(array, cast_options)
1258            }
1259            Time64(TimeUnit::Nanosecond) => {
1260                parse_string::<Time64NanosecondType, i32>(array, cast_options)
1261            }
1262            Timestamp(TimeUnit::Second, to_tz) => {
1263                cast_string_to_timestamp::<i32, TimestampSecondType>(array, to_tz, cast_options)
1264            }
1265            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1266                i32,
1267                TimestampMillisecondType,
1268            >(array, to_tz, cast_options),
1269            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1270                i32,
1271                TimestampMicrosecondType,
1272            >(array, to_tz, cast_options),
1273            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1274                cast_string_to_timestamp::<i32, TimestampNanosecondType>(array, to_tz, cast_options)
1275            }
1276            Interval(IntervalUnit::YearMonth) => {
1277                cast_string_to_year_month_interval::<i32>(array, cast_options)
1278            }
1279            Interval(IntervalUnit::DayTime) => {
1280                cast_string_to_day_time_interval::<i32>(array, cast_options)
1281            }
1282            Interval(IntervalUnit::MonthDayNano) => {
1283                cast_string_to_month_day_nano_interval::<i32>(array, cast_options)
1284            }
1285            _ => Err(ArrowError::CastError(format!(
1286                "Casting from {from_type} to {to_type} not supported",
1287            ))),
1288        },
1289        (Utf8View, _) => match to_type {
1290            UInt8 => parse_string_view::<UInt8Type>(array, cast_options),
1291            UInt16 => parse_string_view::<UInt16Type>(array, cast_options),
1292            UInt32 => parse_string_view::<UInt32Type>(array, cast_options),
1293            UInt64 => parse_string_view::<UInt64Type>(array, cast_options),
1294            Int8 => parse_string_view::<Int8Type>(array, cast_options),
1295            Int16 => parse_string_view::<Int16Type>(array, cast_options),
1296            Int32 => parse_string_view::<Int32Type>(array, cast_options),
1297            Int64 => parse_string_view::<Int64Type>(array, cast_options),
1298            Float32 => parse_string_view::<Float32Type>(array, cast_options),
1299            Float64 => parse_string_view::<Float64Type>(array, cast_options),
1300            Date32 => parse_string_view::<Date32Type>(array, cast_options),
1301            Date64 => parse_string_view::<Date64Type>(array, cast_options),
1302            Binary => cast_view_to_byte::<StringViewType, GenericBinaryType<i32>>(array),
1303            LargeBinary => cast_view_to_byte::<StringViewType, GenericBinaryType<i64>>(array),
1304            BinaryView => Ok(Arc::new(array.as_string_view().clone().to_binary_view())),
1305            Utf8 => cast_view_to_byte::<StringViewType, GenericStringType<i32>>(array),
1306            LargeUtf8 => cast_view_to_byte::<StringViewType, GenericStringType<i64>>(array),
1307            Time32(TimeUnit::Second) => parse_string_view::<Time32SecondType>(array, cast_options),
1308            Time32(TimeUnit::Millisecond) => {
1309                parse_string_view::<Time32MillisecondType>(array, cast_options)
1310            }
1311            Time64(TimeUnit::Microsecond) => {
1312                parse_string_view::<Time64MicrosecondType>(array, cast_options)
1313            }
1314            Time64(TimeUnit::Nanosecond) => {
1315                parse_string_view::<Time64NanosecondType>(array, cast_options)
1316            }
1317            Timestamp(TimeUnit::Second, to_tz) => {
1318                cast_view_to_timestamp::<TimestampSecondType>(array, to_tz, cast_options)
1319            }
1320            Timestamp(TimeUnit::Millisecond, to_tz) => {
1321                cast_view_to_timestamp::<TimestampMillisecondType>(array, to_tz, cast_options)
1322            }
1323            Timestamp(TimeUnit::Microsecond, to_tz) => {
1324                cast_view_to_timestamp::<TimestampMicrosecondType>(array, to_tz, cast_options)
1325            }
1326            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1327                cast_view_to_timestamp::<TimestampNanosecondType>(array, to_tz, cast_options)
1328            }
1329            Interval(IntervalUnit::YearMonth) => {
1330                cast_view_to_year_month_interval(array, cast_options)
1331            }
1332            Interval(IntervalUnit::DayTime) => cast_view_to_day_time_interval(array, cast_options),
1333            Interval(IntervalUnit::MonthDayNano) => {
1334                cast_view_to_month_day_nano_interval(array, cast_options)
1335            }
1336            _ => Err(ArrowError::CastError(format!(
1337                "Casting from {from_type} to {to_type} not supported",
1338            ))),
1339        },
1340        (LargeUtf8, _) => match to_type {
1341            UInt8 => parse_string::<UInt8Type, i64>(array, cast_options),
1342            UInt16 => parse_string::<UInt16Type, i64>(array, cast_options),
1343            UInt32 => parse_string::<UInt32Type, i64>(array, cast_options),
1344            UInt64 => parse_string::<UInt64Type, i64>(array, cast_options),
1345            Int8 => parse_string::<Int8Type, i64>(array, cast_options),
1346            Int16 => parse_string::<Int16Type, i64>(array, cast_options),
1347            Int32 => parse_string::<Int32Type, i64>(array, cast_options),
1348            Int64 => parse_string::<Int64Type, i64>(array, cast_options),
1349            Float32 => parse_string::<Float32Type, i64>(array, cast_options),
1350            Float64 => parse_string::<Float64Type, i64>(array, cast_options),
1351            Date32 => parse_string::<Date32Type, i64>(array, cast_options),
1352            Date64 => parse_string::<Date64Type, i64>(array, cast_options),
1353            Utf8 => cast_byte_container::<LargeUtf8Type, Utf8Type>(array),
1354            Binary => {
1355                let large_binary = LargeBinaryArray::from(array.as_string::<i64>().clone());
1356                cast_byte_container::<LargeBinaryType, BinaryType>(&large_binary)
1357            }
1358            LargeBinary => Ok(Arc::new(LargeBinaryArray::from(
1359                array.as_string::<i64>().clone(),
1360            ))),
1361            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i64>()))),
1362            BinaryView => Ok(Arc::new(BinaryViewArray::from(
1363                array
1364                    .as_string::<i64>()
1365                    .into_iter()
1366                    .map(|x| x.map(|x| x.as_bytes()))
1367                    .collect::<Vec<_>>(),
1368            ))),
1369            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i64>(array, cast_options),
1370            Time32(TimeUnit::Millisecond) => {
1371                parse_string::<Time32MillisecondType, i64>(array, cast_options)
1372            }
1373            Time64(TimeUnit::Microsecond) => {
1374                parse_string::<Time64MicrosecondType, i64>(array, cast_options)
1375            }
1376            Time64(TimeUnit::Nanosecond) => {
1377                parse_string::<Time64NanosecondType, i64>(array, cast_options)
1378            }
1379            Timestamp(TimeUnit::Second, to_tz) => {
1380                cast_string_to_timestamp::<i64, TimestampSecondType>(array, to_tz, cast_options)
1381            }
1382            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1383                i64,
1384                TimestampMillisecondType,
1385            >(array, to_tz, cast_options),
1386            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1387                i64,
1388                TimestampMicrosecondType,
1389            >(array, to_tz, cast_options),
1390            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1391                cast_string_to_timestamp::<i64, TimestampNanosecondType>(array, to_tz, cast_options)
1392            }
1393            Interval(IntervalUnit::YearMonth) => {
1394                cast_string_to_year_month_interval::<i64>(array, cast_options)
1395            }
1396            Interval(IntervalUnit::DayTime) => {
1397                cast_string_to_day_time_interval::<i64>(array, cast_options)
1398            }
1399            Interval(IntervalUnit::MonthDayNano) => {
1400                cast_string_to_month_day_nano_interval::<i64>(array, cast_options)
1401            }
1402            _ => Err(ArrowError::CastError(format!(
1403                "Casting from {from_type} to {to_type} not supported",
1404            ))),
1405        },
1406        (Binary, _) => match to_type {
1407            Utf8 => cast_binary_to_string::<i32>(array, cast_options),
1408            LargeUtf8 => {
1409                let array = cast_binary_to_string::<i32>(array, cast_options)?;
1410                cast_byte_container::<Utf8Type, LargeUtf8Type>(array.as_ref())
1411            }
1412            LargeBinary => cast_byte_container::<BinaryType, LargeBinaryType>(array),
1413            FixedSizeBinary(size) => {
1414                cast_binary_to_fixed_size_binary::<i32>(array, *size, cast_options)
1415            }
1416            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i32>()))),
1417            Utf8View => Ok(Arc::new(StringViewArray::from(
1418                cast_binary_to_string::<i32>(array, cast_options)?.as_string::<i32>(),
1419            ))),
1420            _ => Err(ArrowError::CastError(format!(
1421                "Casting from {from_type} to {to_type} not supported",
1422            ))),
1423        },
1424        (LargeBinary, _) => match to_type {
1425            Utf8 => {
1426                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1427                cast_byte_container::<LargeUtf8Type, Utf8Type>(array.as_ref())
1428            }
1429            LargeUtf8 => cast_binary_to_string::<i64>(array, cast_options),
1430            Binary => cast_byte_container::<LargeBinaryType, BinaryType>(array),
1431            FixedSizeBinary(size) => {
1432                cast_binary_to_fixed_size_binary::<i64>(array, *size, cast_options)
1433            }
1434            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i64>()))),
1435            Utf8View => {
1436                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1437                Ok(Arc::new(StringViewArray::from(array.as_string::<i64>())))
1438            }
1439            _ => Err(ArrowError::CastError(format!(
1440                "Casting from {from_type} to {to_type} not supported",
1441            ))),
1442        },
1443        (FixedSizeBinary(size), _) => match to_type {
1444            Binary => cast_fixed_size_binary_to_binary::<i32>(array, *size),
1445            LargeBinary => cast_fixed_size_binary_to_binary::<i64>(array, *size),
1446            BinaryView => cast_fixed_size_binary_to_binary_view(array, *size),
1447            _ => Err(ArrowError::CastError(format!(
1448                "Casting from {from_type} to {to_type} not supported",
1449            ))),
1450        },
1451        (BinaryView, Binary) => cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array),
1452        (BinaryView, LargeBinary) => {
1453            cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)
1454        }
1455        (BinaryView, Utf8) => {
1456            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array)?;
1457            cast_binary_to_string::<i32>(&binary_arr, cast_options)
1458        }
1459        (BinaryView, LargeUtf8) => {
1460            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)?;
1461            cast_binary_to_string::<i64>(&binary_arr, cast_options)
1462        }
1463        (BinaryView, Utf8View) => cast_binary_view_to_string_view(array, cast_options),
1464        (BinaryView, _) => Err(ArrowError::CastError(format!(
1465            "Casting from {from_type} to {to_type} not supported",
1466        ))),
1467        (from_type, Utf8View) if from_type.is_primitive() => {
1468            value_to_string_view(array, cast_options)
1469        }
1470        (from_type, LargeUtf8) if from_type.is_primitive() => {
1471            value_to_string::<i64>(array, cast_options)
1472        }
1473        (from_type, Utf8) if from_type.is_primitive() => {
1474            value_to_string::<i32>(array, cast_options)
1475        }
1476        (from_type, Binary) if from_type.is_integer() => match from_type {
1477            UInt8 => cast_numeric_to_binary::<UInt8Type, i32>(array),
1478            UInt16 => cast_numeric_to_binary::<UInt16Type, i32>(array),
1479            UInt32 => cast_numeric_to_binary::<UInt32Type, i32>(array),
1480            UInt64 => cast_numeric_to_binary::<UInt64Type, i32>(array),
1481            Int8 => cast_numeric_to_binary::<Int8Type, i32>(array),
1482            Int16 => cast_numeric_to_binary::<Int16Type, i32>(array),
1483            Int32 => cast_numeric_to_binary::<Int32Type, i32>(array),
1484            Int64 => cast_numeric_to_binary::<Int64Type, i32>(array),
1485            _ => unreachable!(),
1486        },
1487        (from_type, LargeBinary) if from_type.is_integer() => match from_type {
1488            UInt8 => cast_numeric_to_binary::<UInt8Type, i64>(array),
1489            UInt16 => cast_numeric_to_binary::<UInt16Type, i64>(array),
1490            UInt32 => cast_numeric_to_binary::<UInt32Type, i64>(array),
1491            UInt64 => cast_numeric_to_binary::<UInt64Type, i64>(array),
1492            Int8 => cast_numeric_to_binary::<Int8Type, i64>(array),
1493            Int16 => cast_numeric_to_binary::<Int16Type, i64>(array),
1494            Int32 => cast_numeric_to_binary::<Int32Type, i64>(array),
1495            Int64 => cast_numeric_to_binary::<Int64Type, i64>(array),
1496            _ => unreachable!(),
1497        },
1498        // start numeric casts
1499        (UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array, cast_options),
1500        (UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array, cast_options),
1501        (UInt8, UInt64) => cast_numeric_arrays::<UInt8Type, UInt64Type>(array, cast_options),
1502        (UInt8, Int8) => cast_numeric_arrays::<UInt8Type, Int8Type>(array, cast_options),
1503        (UInt8, Int16) => cast_numeric_arrays::<UInt8Type, Int16Type>(array, cast_options),
1504        (UInt8, Int32) => cast_numeric_arrays::<UInt8Type, Int32Type>(array, cast_options),
1505        (UInt8, Int64) => cast_numeric_arrays::<UInt8Type, Int64Type>(array, cast_options),
1506        (UInt8, Float16) => cast_numeric_arrays::<UInt8Type, Float16Type>(array, cast_options),
1507        (UInt8, Float32) => cast_numeric_arrays::<UInt8Type, Float32Type>(array, cast_options),
1508        (UInt8, Float64) => cast_numeric_arrays::<UInt8Type, Float64Type>(array, cast_options),
1509
1510        (UInt16, UInt8) => cast_numeric_arrays::<UInt16Type, UInt8Type>(array, cast_options),
1511        (UInt16, UInt32) => cast_numeric_arrays::<UInt16Type, UInt32Type>(array, cast_options),
1512        (UInt16, UInt64) => cast_numeric_arrays::<UInt16Type, UInt64Type>(array, cast_options),
1513        (UInt16, Int8) => cast_numeric_arrays::<UInt16Type, Int8Type>(array, cast_options),
1514        (UInt16, Int16) => cast_numeric_arrays::<UInt16Type, Int16Type>(array, cast_options),
1515        (UInt16, Int32) => cast_numeric_arrays::<UInt16Type, Int32Type>(array, cast_options),
1516        (UInt16, Int64) => cast_numeric_arrays::<UInt16Type, Int64Type>(array, cast_options),
1517        (UInt16, Float16) => cast_numeric_arrays::<UInt16Type, Float16Type>(array, cast_options),
1518        (UInt16, Float32) => cast_numeric_arrays::<UInt16Type, Float32Type>(array, cast_options),
1519        (UInt16, Float64) => cast_numeric_arrays::<UInt16Type, Float64Type>(array, cast_options),
1520
1521        (UInt32, UInt8) => cast_numeric_arrays::<UInt32Type, UInt8Type>(array, cast_options),
1522        (UInt32, UInt16) => cast_numeric_arrays::<UInt32Type, UInt16Type>(array, cast_options),
1523        (UInt32, UInt64) => cast_numeric_arrays::<UInt32Type, UInt64Type>(array, cast_options),
1524        (UInt32, Int8) => cast_numeric_arrays::<UInt32Type, Int8Type>(array, cast_options),
1525        (UInt32, Int16) => cast_numeric_arrays::<UInt32Type, Int16Type>(array, cast_options),
1526        (UInt32, Int32) => cast_numeric_arrays::<UInt32Type, Int32Type>(array, cast_options),
1527        (UInt32, Int64) => cast_numeric_arrays::<UInt32Type, Int64Type>(array, cast_options),
1528        (UInt32, Float16) => cast_numeric_arrays::<UInt32Type, Float16Type>(array, cast_options),
1529        (UInt32, Float32) => cast_numeric_arrays::<UInt32Type, Float32Type>(array, cast_options),
1530        (UInt32, Float64) => cast_numeric_arrays::<UInt32Type, Float64Type>(array, cast_options),
1531
1532        (UInt64, UInt8) => cast_numeric_arrays::<UInt64Type, UInt8Type>(array, cast_options),
1533        (UInt64, UInt16) => cast_numeric_arrays::<UInt64Type, UInt16Type>(array, cast_options),
1534        (UInt64, UInt32) => cast_numeric_arrays::<UInt64Type, UInt32Type>(array, cast_options),
1535        (UInt64, Int8) => cast_numeric_arrays::<UInt64Type, Int8Type>(array, cast_options),
1536        (UInt64, Int16) => cast_numeric_arrays::<UInt64Type, Int16Type>(array, cast_options),
1537        (UInt64, Int32) => cast_numeric_arrays::<UInt64Type, Int32Type>(array, cast_options),
1538        (UInt64, Int64) => cast_numeric_arrays::<UInt64Type, Int64Type>(array, cast_options),
1539        (UInt64, Float16) => cast_numeric_arrays::<UInt64Type, Float16Type>(array, cast_options),
1540        (UInt64, Float32) => cast_numeric_arrays::<UInt64Type, Float32Type>(array, cast_options),
1541        (UInt64, Float64) => cast_numeric_arrays::<UInt64Type, Float64Type>(array, cast_options),
1542
1543        (Int8, UInt8) => cast_numeric_arrays::<Int8Type, UInt8Type>(array, cast_options),
1544        (Int8, UInt16) => cast_numeric_arrays::<Int8Type, UInt16Type>(array, cast_options),
1545        (Int8, UInt32) => cast_numeric_arrays::<Int8Type, UInt32Type>(array, cast_options),
1546        (Int8, UInt64) => cast_numeric_arrays::<Int8Type, UInt64Type>(array, cast_options),
1547        (Int8, Int16) => cast_numeric_arrays::<Int8Type, Int16Type>(array, cast_options),
1548        (Int8, Int32) => cast_numeric_arrays::<Int8Type, Int32Type>(array, cast_options),
1549        (Int8, Int64) => cast_numeric_arrays::<Int8Type, Int64Type>(array, cast_options),
1550        (Int8, Float16) => cast_numeric_arrays::<Int8Type, Float16Type>(array, cast_options),
1551        (Int8, Float32) => cast_numeric_arrays::<Int8Type, Float32Type>(array, cast_options),
1552        (Int8, Float64) => cast_numeric_arrays::<Int8Type, Float64Type>(array, cast_options),
1553
1554        (Int16, UInt8) => cast_numeric_arrays::<Int16Type, UInt8Type>(array, cast_options),
1555        (Int16, UInt16) => cast_numeric_arrays::<Int16Type, UInt16Type>(array, cast_options),
1556        (Int16, UInt32) => cast_numeric_arrays::<Int16Type, UInt32Type>(array, cast_options),
1557        (Int16, UInt64) => cast_numeric_arrays::<Int16Type, UInt64Type>(array, cast_options),
1558        (Int16, Int8) => cast_numeric_arrays::<Int16Type, Int8Type>(array, cast_options),
1559        (Int16, Int32) => cast_numeric_arrays::<Int16Type, Int32Type>(array, cast_options),
1560        (Int16, Int64) => cast_numeric_arrays::<Int16Type, Int64Type>(array, cast_options),
1561        (Int16, Float16) => cast_numeric_arrays::<Int16Type, Float16Type>(array, cast_options),
1562        (Int16, Float32) => cast_numeric_arrays::<Int16Type, Float32Type>(array, cast_options),
1563        (Int16, Float64) => cast_numeric_arrays::<Int16Type, Float64Type>(array, cast_options),
1564
1565        (Int32, UInt8) => cast_numeric_arrays::<Int32Type, UInt8Type>(array, cast_options),
1566        (Int32, UInt16) => cast_numeric_arrays::<Int32Type, UInt16Type>(array, cast_options),
1567        (Int32, UInt32) => cast_numeric_arrays::<Int32Type, UInt32Type>(array, cast_options),
1568        (Int32, UInt64) => cast_numeric_arrays::<Int32Type, UInt64Type>(array, cast_options),
1569        (Int32, Int8) => cast_numeric_arrays::<Int32Type, Int8Type>(array, cast_options),
1570        (Int32, Int16) => cast_numeric_arrays::<Int32Type, Int16Type>(array, cast_options),
1571        (Int32, Int64) => cast_numeric_arrays::<Int32Type, Int64Type>(array, cast_options),
1572        (Int32, Float16) => cast_numeric_arrays::<Int32Type, Float16Type>(array, cast_options),
1573        (Int32, Float32) => cast_numeric_arrays::<Int32Type, Float32Type>(array, cast_options),
1574        (Int32, Float64) => cast_numeric_arrays::<Int32Type, Float64Type>(array, cast_options),
1575
1576        (Int64, UInt8) => cast_numeric_arrays::<Int64Type, UInt8Type>(array, cast_options),
1577        (Int64, UInt16) => cast_numeric_arrays::<Int64Type, UInt16Type>(array, cast_options),
1578        (Int64, UInt32) => cast_numeric_arrays::<Int64Type, UInt32Type>(array, cast_options),
1579        (Int64, UInt64) => cast_numeric_arrays::<Int64Type, UInt64Type>(array, cast_options),
1580        (Int64, Int8) => cast_numeric_arrays::<Int64Type, Int8Type>(array, cast_options),
1581        (Int64, Int16) => cast_numeric_arrays::<Int64Type, Int16Type>(array, cast_options),
1582        (Int64, Int32) => cast_numeric_arrays::<Int64Type, Int32Type>(array, cast_options),
1583        (Int64, Float16) => cast_numeric_arrays::<Int64Type, Float16Type>(array, cast_options),
1584        (Int64, Float32) => cast_numeric_arrays::<Int64Type, Float32Type>(array, cast_options),
1585        (Int64, Float64) => cast_numeric_arrays::<Int64Type, Float64Type>(array, cast_options),
1586
1587        (Float16, UInt8) => cast_numeric_arrays::<Float16Type, UInt8Type>(array, cast_options),
1588        (Float16, UInt16) => cast_numeric_arrays::<Float16Type, UInt16Type>(array, cast_options),
1589        (Float16, UInt32) => cast_numeric_arrays::<Float16Type, UInt32Type>(array, cast_options),
1590        (Float16, UInt64) => cast_numeric_arrays::<Float16Type, UInt64Type>(array, cast_options),
1591        (Float16, Int8) => cast_numeric_arrays::<Float16Type, Int8Type>(array, cast_options),
1592        (Float16, Int16) => cast_numeric_arrays::<Float16Type, Int16Type>(array, cast_options),
1593        (Float16, Int32) => cast_numeric_arrays::<Float16Type, Int32Type>(array, cast_options),
1594        (Float16, Int64) => cast_numeric_arrays::<Float16Type, Int64Type>(array, cast_options),
1595        (Float16, Float32) => cast_numeric_arrays::<Float16Type, Float32Type>(array, cast_options),
1596        (Float16, Float64) => cast_numeric_arrays::<Float16Type, Float64Type>(array, cast_options),
1597
1598        (Float32, UInt8) => cast_numeric_arrays::<Float32Type, UInt8Type>(array, cast_options),
1599        (Float32, UInt16) => cast_numeric_arrays::<Float32Type, UInt16Type>(array, cast_options),
1600        (Float32, UInt32) => cast_numeric_arrays::<Float32Type, UInt32Type>(array, cast_options),
1601        (Float32, UInt64) => cast_numeric_arrays::<Float32Type, UInt64Type>(array, cast_options),
1602        (Float32, Int8) => cast_numeric_arrays::<Float32Type, Int8Type>(array, cast_options),
1603        (Float32, Int16) => cast_numeric_arrays::<Float32Type, Int16Type>(array, cast_options),
1604        (Float32, Int32) => cast_numeric_arrays::<Float32Type, Int32Type>(array, cast_options),
1605        (Float32, Int64) => cast_numeric_arrays::<Float32Type, Int64Type>(array, cast_options),
1606        (Float32, Float16) => cast_numeric_arrays::<Float32Type, Float16Type>(array, cast_options),
1607        (Float32, Float64) => cast_numeric_arrays::<Float32Type, Float64Type>(array, cast_options),
1608
1609        (Float64, UInt8) => cast_numeric_arrays::<Float64Type, UInt8Type>(array, cast_options),
1610        (Float64, UInt16) => cast_numeric_arrays::<Float64Type, UInt16Type>(array, cast_options),
1611        (Float64, UInt32) => cast_numeric_arrays::<Float64Type, UInt32Type>(array, cast_options),
1612        (Float64, UInt64) => cast_numeric_arrays::<Float64Type, UInt64Type>(array, cast_options),
1613        (Float64, Int8) => cast_numeric_arrays::<Float64Type, Int8Type>(array, cast_options),
1614        (Float64, Int16) => cast_numeric_arrays::<Float64Type, Int16Type>(array, cast_options),
1615        (Float64, Int32) => cast_numeric_arrays::<Float64Type, Int32Type>(array, cast_options),
1616        (Float64, Int64) => cast_numeric_arrays::<Float64Type, Int64Type>(array, cast_options),
1617        (Float64, Float16) => cast_numeric_arrays::<Float64Type, Float16Type>(array, cast_options),
1618        (Float64, Float32) => cast_numeric_arrays::<Float64Type, Float32Type>(array, cast_options),
1619        // end numeric casts
1620
1621        // temporal casts
1622        (Int32, Date32) => cast_reinterpret_arrays::<Int32Type, Date32Type>(array),
1623        (Int32, Date64) => cast_with_options(
1624            &cast_with_options(array, &Date32, cast_options)?,
1625            &Date64,
1626            cast_options,
1627        ),
1628        (Int32, Time32(TimeUnit::Second)) => {
1629            cast_reinterpret_arrays::<Int32Type, Time32SecondType>(array)
1630        }
1631        (Int32, Time32(TimeUnit::Millisecond)) => {
1632            cast_reinterpret_arrays::<Int32Type, Time32MillisecondType>(array)
1633        }
1634        // No support for microsecond/nanosecond with i32
1635        (Date32, Int32) => cast_reinterpret_arrays::<Date32Type, Int32Type>(array),
1636        (Date32, Int64) => cast_with_options(
1637            &cast_with_options(array, &Int32, cast_options)?,
1638            &Int64,
1639            cast_options,
1640        ),
1641        (Time32(TimeUnit::Second), Int32) => {
1642            cast_reinterpret_arrays::<Time32SecondType, Int32Type>(array)
1643        }
1644        (Time32(TimeUnit::Millisecond), Int32) => {
1645            cast_reinterpret_arrays::<Time32MillisecondType, Int32Type>(array)
1646        }
1647        (Int64, Date64) => cast_reinterpret_arrays::<Int64Type, Date64Type>(array),
1648        (Int64, Date32) => cast_with_options(
1649            &cast_with_options(array, &Int32, cast_options)?,
1650            &Date32,
1651            cast_options,
1652        ),
1653        // No support for second/milliseconds with i64
1654        (Int64, Time64(TimeUnit::Microsecond)) => {
1655            cast_reinterpret_arrays::<Int64Type, Time64MicrosecondType>(array)
1656        }
1657        (Int64, Time64(TimeUnit::Nanosecond)) => {
1658            cast_reinterpret_arrays::<Int64Type, Time64NanosecondType>(array)
1659        }
1660
1661        (Date64, Int64) => cast_reinterpret_arrays::<Date64Type, Int64Type>(array),
1662        (Date64, Int32) => cast_with_options(
1663            &cast_with_options(array, &Int64, cast_options)?,
1664            &Int32,
1665            cast_options,
1666        ),
1667        (Time64(TimeUnit::Microsecond), Int64) => {
1668            cast_reinterpret_arrays::<Time64MicrosecondType, Int64Type>(array)
1669        }
1670        (Time64(TimeUnit::Nanosecond), Int64) => {
1671            cast_reinterpret_arrays::<Time64NanosecondType, Int64Type>(array)
1672        }
1673        (Date32, Date64) => Ok(Arc::new(
1674            array
1675                .as_primitive::<Date32Type>()
1676                .unary::<_, Date64Type>(|x| x as i64 * MILLISECONDS_IN_DAY),
1677        )),
1678        (Date64, Date32) => Ok(Arc::new(
1679            array
1680                .as_primitive::<Date64Type>()
1681                .unary::<_, Date32Type>(|x| (x / MILLISECONDS_IN_DAY) as i32),
1682        )),
1683
1684        (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1685            array
1686                .as_primitive::<Time32SecondType>()
1687                .unary::<_, Time32MillisecondType>(|x| x * MILLISECONDS as i32),
1688        )),
1689        (Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1690            array
1691                .as_primitive::<Time32SecondType>()
1692                .unary::<_, Time64MicrosecondType>(|x| x as i64 * MICROSECONDS),
1693        )),
1694        (Time32(TimeUnit::Second), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1695            array
1696                .as_primitive::<Time32SecondType>()
1697                .unary::<_, Time64NanosecondType>(|x| x as i64 * NANOSECONDS),
1698        )),
1699
1700        (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1701            array
1702                .as_primitive::<Time32MillisecondType>()
1703                .unary::<_, Time32SecondType>(|x| x / MILLISECONDS as i32),
1704        )),
1705        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1706            array
1707                .as_primitive::<Time32MillisecondType>()
1708                .unary::<_, Time64MicrosecondType>(|x| x as i64 * (MICROSECONDS / MILLISECONDS)),
1709        )),
1710        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1711            array
1712                .as_primitive::<Time32MillisecondType>()
1713                .unary::<_, Time64NanosecondType>(|x| x as i64 * (MICROSECONDS / NANOSECONDS)),
1714        )),
1715
1716        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1717            array
1718                .as_primitive::<Time64MicrosecondType>()
1719                .unary::<_, Time32SecondType>(|x| (x / MICROSECONDS) as i32),
1720        )),
1721        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1722            array
1723                .as_primitive::<Time64MicrosecondType>()
1724                .unary::<_, Time32MillisecondType>(|x| (x / (MICROSECONDS / MILLISECONDS)) as i32),
1725        )),
1726        (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1727            array
1728                .as_primitive::<Time64MicrosecondType>()
1729                .unary::<_, Time64NanosecondType>(|x| x * (NANOSECONDS / MICROSECONDS)),
1730        )),
1731
1732        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1733            array
1734                .as_primitive::<Time64NanosecondType>()
1735                .unary::<_, Time32SecondType>(|x| (x / NANOSECONDS) as i32),
1736        )),
1737        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1738            array
1739                .as_primitive::<Time64NanosecondType>()
1740                .unary::<_, Time32MillisecondType>(|x| (x / (NANOSECONDS / MILLISECONDS)) as i32),
1741        )),
1742        (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1743            array
1744                .as_primitive::<Time64NanosecondType>()
1745                .unary::<_, Time64MicrosecondType>(|x| x / (NANOSECONDS / MICROSECONDS)),
1746        )),
1747
1748        // Timestamp to integer/floating/decimals
1749        (Timestamp(TimeUnit::Second, _), _) if to_type.is_numeric() => {
1750            let array = cast_reinterpret_arrays::<TimestampSecondType, Int64Type>(array)?;
1751            cast_with_options(&array, to_type, cast_options)
1752        }
1753        (Timestamp(TimeUnit::Millisecond, _), _) if to_type.is_numeric() => {
1754            let array = cast_reinterpret_arrays::<TimestampMillisecondType, Int64Type>(array)?;
1755            cast_with_options(&array, to_type, cast_options)
1756        }
1757        (Timestamp(TimeUnit::Microsecond, _), _) if to_type.is_numeric() => {
1758            let array = cast_reinterpret_arrays::<TimestampMicrosecondType, Int64Type>(array)?;
1759            cast_with_options(&array, to_type, cast_options)
1760        }
1761        (Timestamp(TimeUnit::Nanosecond, _), _) if to_type.is_numeric() => {
1762            let array = cast_reinterpret_arrays::<TimestampNanosecondType, Int64Type>(array)?;
1763            cast_with_options(&array, to_type, cast_options)
1764        }
1765
1766        (_, Timestamp(unit, tz)) if from_type.is_numeric() => {
1767            let array = cast_with_options(array, &Int64, cast_options)?;
1768            Ok(make_timestamp_array(
1769                array.as_primitive(),
1770                *unit,
1771                tz.clone(),
1772            ))
1773        }
1774
1775        (Timestamp(from_unit, from_tz), Timestamp(to_unit, to_tz)) => {
1776            let array = cast_with_options(array, &Int64, cast_options)?;
1777            let time_array = array.as_primitive::<Int64Type>();
1778            let from_size = time_unit_multiple(from_unit);
1779            let to_size = time_unit_multiple(to_unit);
1780            // we either divide or multiply, depending on size of each unit
1781            // units are never the same when the types are the same
1782            let converted = match from_size.cmp(&to_size) {
1783                Ordering::Greater => {
1784                    let divisor = from_size / to_size;
1785                    time_array.unary::<_, Int64Type>(|o| o / divisor)
1786                }
1787                Ordering::Equal => time_array.clone(),
1788                Ordering::Less => {
1789                    let mul = to_size / from_size;
1790                    if cast_options.safe {
1791                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1792                    } else {
1793                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1794                    }
1795                }
1796            };
1797            // Normalize timezone
1798            let adjusted = match (from_tz, to_tz) {
1799                // Only this case needs to be adjusted because we're casting from
1800                // unknown time offset to some time offset, we want the time to be
1801                // unchanged.
1802                //
1803                // i.e. Timestamp('2001-01-01T00:00', None) -> Timestamp('2001-01-01T00:00', '+0700')
1804                (None, Some(to_tz)) => {
1805                    let to_tz: Tz = to_tz.parse()?;
1806                    match to_unit {
1807                        TimeUnit::Second => adjust_timestamp_to_timezone::<TimestampSecondType>(
1808                            converted,
1809                            &to_tz,
1810                            cast_options,
1811                        )?,
1812                        TimeUnit::Millisecond => adjust_timestamp_to_timezone::<
1813                            TimestampMillisecondType,
1814                        >(
1815                            converted, &to_tz, cast_options
1816                        )?,
1817                        TimeUnit::Microsecond => adjust_timestamp_to_timezone::<
1818                            TimestampMicrosecondType,
1819                        >(
1820                            converted, &to_tz, cast_options
1821                        )?,
1822                        TimeUnit::Nanosecond => adjust_timestamp_to_timezone::<
1823                            TimestampNanosecondType,
1824                        >(
1825                            converted, &to_tz, cast_options
1826                        )?,
1827                    }
1828                }
1829                _ => converted,
1830            };
1831            Ok(make_timestamp_array(&adjusted, *to_unit, to_tz.clone()))
1832        }
1833        (Timestamp(TimeUnit::Microsecond, _), Date32) => {
1834            timestamp_to_date32(array.as_primitive::<TimestampMicrosecondType>())
1835        }
1836        (Timestamp(TimeUnit::Millisecond, _), Date32) => {
1837            timestamp_to_date32(array.as_primitive::<TimestampMillisecondType>())
1838        }
1839        (Timestamp(TimeUnit::Second, _), Date32) => {
1840            timestamp_to_date32(array.as_primitive::<TimestampSecondType>())
1841        }
1842        (Timestamp(TimeUnit::Nanosecond, _), Date32) => {
1843            timestamp_to_date32(array.as_primitive::<TimestampNanosecondType>())
1844        }
1845        (Timestamp(TimeUnit::Second, _), Date64) => Ok(Arc::new(match cast_options.safe {
1846            true => {
1847                // change error to None
1848                array
1849                    .as_primitive::<TimestampSecondType>()
1850                    .unary_opt::<_, Date64Type>(|x| x.checked_mul(MILLISECONDS))
1851            }
1852            false => array
1853                .as_primitive::<TimestampSecondType>()
1854                .try_unary::<_, Date64Type, _>(|x| x.mul_checked(MILLISECONDS))?,
1855        })),
1856        (Timestamp(TimeUnit::Millisecond, _), Date64) => {
1857            cast_reinterpret_arrays::<TimestampMillisecondType, Date64Type>(array)
1858        }
1859        (Timestamp(TimeUnit::Microsecond, _), Date64) => Ok(Arc::new(
1860            array
1861                .as_primitive::<TimestampMicrosecondType>()
1862                .unary::<_, Date64Type>(|x| x / (MICROSECONDS / MILLISECONDS)),
1863        )),
1864        (Timestamp(TimeUnit::Nanosecond, _), Date64) => Ok(Arc::new(
1865            array
1866                .as_primitive::<TimestampNanosecondType>()
1867                .unary::<_, Date64Type>(|x| x / (NANOSECONDS / MILLISECONDS)),
1868        )),
1869        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Microsecond)) => {
1870            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1871            Ok(Arc::new(
1872                array
1873                    .as_primitive::<TimestampSecondType>()
1874                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1875                        Ok(time_to_time64us(as_time_res_with_timezone::<
1876                            TimestampSecondType,
1877                        >(x, tz)?))
1878                    })?,
1879            ))
1880        }
1881        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Nanosecond)) => {
1882            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1883            Ok(Arc::new(
1884                array
1885                    .as_primitive::<TimestampSecondType>()
1886                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1887                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1888                            TimestampSecondType,
1889                        >(x, tz)?))
1890                    })?,
1891            ))
1892        }
1893        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Microsecond)) => {
1894            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1895            Ok(Arc::new(
1896                array
1897                    .as_primitive::<TimestampMillisecondType>()
1898                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1899                        Ok(time_to_time64us(as_time_res_with_timezone::<
1900                            TimestampMillisecondType,
1901                        >(x, tz)?))
1902                    })?,
1903            ))
1904        }
1905        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Nanosecond)) => {
1906            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1907            Ok(Arc::new(
1908                array
1909                    .as_primitive::<TimestampMillisecondType>()
1910                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1911                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1912                            TimestampMillisecondType,
1913                        >(x, tz)?))
1914                    })?,
1915            ))
1916        }
1917        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Microsecond)) => {
1918            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1919            Ok(Arc::new(
1920                array
1921                    .as_primitive::<TimestampMicrosecondType>()
1922                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1923                        Ok(time_to_time64us(as_time_res_with_timezone::<
1924                            TimestampMicrosecondType,
1925                        >(x, tz)?))
1926                    })?,
1927            ))
1928        }
1929        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Nanosecond)) => {
1930            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1931            Ok(Arc::new(
1932                array
1933                    .as_primitive::<TimestampMicrosecondType>()
1934                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1935                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1936                            TimestampMicrosecondType,
1937                        >(x, tz)?))
1938                    })?,
1939            ))
1940        }
1941        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Microsecond)) => {
1942            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1943            Ok(Arc::new(
1944                array
1945                    .as_primitive::<TimestampNanosecondType>()
1946                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1947                        Ok(time_to_time64us(as_time_res_with_timezone::<
1948                            TimestampNanosecondType,
1949                        >(x, tz)?))
1950                    })?,
1951            ))
1952        }
1953        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Nanosecond)) => {
1954            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1955            Ok(Arc::new(
1956                array
1957                    .as_primitive::<TimestampNanosecondType>()
1958                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1959                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1960                            TimestampNanosecondType,
1961                        >(x, tz)?))
1962                    })?,
1963            ))
1964        }
1965        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Second)) => {
1966            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1967            Ok(Arc::new(
1968                array
1969                    .as_primitive::<TimestampSecondType>()
1970                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1971                        Ok(time_to_time32s(as_time_res_with_timezone::<
1972                            TimestampSecondType,
1973                        >(x, tz)?))
1974                    })?,
1975            ))
1976        }
1977        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Millisecond)) => {
1978            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1979            Ok(Arc::new(
1980                array
1981                    .as_primitive::<TimestampSecondType>()
1982                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1983                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1984                            TimestampSecondType,
1985                        >(x, tz)?))
1986                    })?,
1987            ))
1988        }
1989        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Second)) => {
1990            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1991            Ok(Arc::new(
1992                array
1993                    .as_primitive::<TimestampMillisecondType>()
1994                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1995                        Ok(time_to_time32s(as_time_res_with_timezone::<
1996                            TimestampMillisecondType,
1997                        >(x, tz)?))
1998                    })?,
1999            ))
2000        }
2001        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Millisecond)) => {
2002            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2003            Ok(Arc::new(
2004                array
2005                    .as_primitive::<TimestampMillisecondType>()
2006                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2007                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2008                            TimestampMillisecondType,
2009                        >(x, tz)?))
2010                    })?,
2011            ))
2012        }
2013        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Second)) => {
2014            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2015            Ok(Arc::new(
2016                array
2017                    .as_primitive::<TimestampMicrosecondType>()
2018                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2019                        Ok(time_to_time32s(as_time_res_with_timezone::<
2020                            TimestampMicrosecondType,
2021                        >(x, tz)?))
2022                    })?,
2023            ))
2024        }
2025        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Millisecond)) => {
2026            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2027            Ok(Arc::new(
2028                array
2029                    .as_primitive::<TimestampMicrosecondType>()
2030                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2031                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2032                            TimestampMicrosecondType,
2033                        >(x, tz)?))
2034                    })?,
2035            ))
2036        }
2037        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Second)) => {
2038            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2039            Ok(Arc::new(
2040                array
2041                    .as_primitive::<TimestampNanosecondType>()
2042                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2043                        Ok(time_to_time32s(as_time_res_with_timezone::<
2044                            TimestampNanosecondType,
2045                        >(x, tz)?))
2046                    })?,
2047            ))
2048        }
2049        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Millisecond)) => {
2050            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2051            Ok(Arc::new(
2052                array
2053                    .as_primitive::<TimestampNanosecondType>()
2054                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2055                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2056                            TimestampNanosecondType,
2057                        >(x, tz)?))
2058                    })?,
2059            ))
2060        }
2061        (Date64, Timestamp(TimeUnit::Second, _)) => {
2062            let array = array
2063                .as_primitive::<Date64Type>()
2064                .unary::<_, TimestampSecondType>(|x| x / MILLISECONDS);
2065
2066            cast_with_options(&array, to_type, cast_options)
2067        }
2068        (Date64, Timestamp(TimeUnit::Millisecond, _)) => {
2069            let array = array
2070                .as_primitive::<Date64Type>()
2071                .reinterpret_cast::<TimestampMillisecondType>();
2072
2073            cast_with_options(&array, to_type, cast_options)
2074        }
2075
2076        (Date64, Timestamp(TimeUnit::Microsecond, _)) => {
2077            let array = array
2078                .as_primitive::<Date64Type>()
2079                .unary::<_, TimestampMicrosecondType>(|x| x * (MICROSECONDS / MILLISECONDS));
2080
2081            cast_with_options(&array, to_type, cast_options)
2082        }
2083        (Date64, Timestamp(TimeUnit::Nanosecond, _)) => {
2084            let array = array
2085                .as_primitive::<Date64Type>()
2086                .unary::<_, TimestampNanosecondType>(|x| x * (NANOSECONDS / MILLISECONDS));
2087
2088            cast_with_options(&array, to_type, cast_options)
2089        }
2090        (Date32, Timestamp(TimeUnit::Second, _)) => {
2091            let array = array
2092                .as_primitive::<Date32Type>()
2093                .unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY);
2094
2095            cast_with_options(&array, to_type, cast_options)
2096        }
2097        (Date32, Timestamp(TimeUnit::Millisecond, _)) => {
2098            let array = array
2099                .as_primitive::<Date32Type>()
2100                .unary::<_, TimestampMillisecondType>(|x| (x as i64) * MILLISECONDS_IN_DAY);
2101
2102            cast_with_options(&array, to_type, cast_options)
2103        }
2104        (Date32, Timestamp(TimeUnit::Microsecond, _)) => {
2105            let array = array
2106                .as_primitive::<Date32Type>()
2107                .unary::<_, TimestampMicrosecondType>(|x| (x as i64) * MICROSECONDS_IN_DAY);
2108
2109            cast_with_options(&array, to_type, cast_options)
2110        }
2111        (Date32, Timestamp(TimeUnit::Nanosecond, _)) => {
2112            let array = array
2113                .as_primitive::<Date32Type>()
2114                .unary::<_, TimestampNanosecondType>(|x| (x as i64) * NANOSECONDS_IN_DAY);
2115
2116            cast_with_options(&array, to_type, cast_options)
2117        }
2118
2119        (_, Duration(unit)) if from_type.is_numeric() => {
2120            let array = cast_with_options(array, &Int64, cast_options)?;
2121            Ok(make_duration_array(array.as_primitive(), *unit))
2122        }
2123        (Duration(TimeUnit::Second), _) if to_type.is_numeric() => {
2124            let array = cast_reinterpret_arrays::<DurationSecondType, Int64Type>(array)?;
2125            cast_with_options(&array, to_type, cast_options)
2126        }
2127        (Duration(TimeUnit::Millisecond), _) if to_type.is_numeric() => {
2128            let array = cast_reinterpret_arrays::<DurationMillisecondType, Int64Type>(array)?;
2129            cast_with_options(&array, to_type, cast_options)
2130        }
2131        (Duration(TimeUnit::Microsecond), _) if to_type.is_numeric() => {
2132            let array = cast_reinterpret_arrays::<DurationMicrosecondType, Int64Type>(array)?;
2133            cast_with_options(&array, to_type, cast_options)
2134        }
2135        (Duration(TimeUnit::Nanosecond), _) if to_type.is_numeric() => {
2136            let array = cast_reinterpret_arrays::<DurationNanosecondType, Int64Type>(array)?;
2137            cast_with_options(&array, to_type, cast_options)
2138        }
2139
2140        (Duration(from_unit), Duration(to_unit)) => {
2141            let array = cast_with_options(array, &Int64, cast_options)?;
2142            let time_array = array.as_primitive::<Int64Type>();
2143            let from_size = time_unit_multiple(from_unit);
2144            let to_size = time_unit_multiple(to_unit);
2145            // we either divide or multiply, depending on size of each unit
2146            // units are never the same when the types are the same
2147            let converted = match from_size.cmp(&to_size) {
2148                Ordering::Greater => {
2149                    let divisor = from_size / to_size;
2150                    time_array.unary::<_, Int64Type>(|o| o / divisor)
2151                }
2152                Ordering::Equal => time_array.clone(),
2153                Ordering::Less => {
2154                    let mul = to_size / from_size;
2155                    if cast_options.safe {
2156                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
2157                    } else {
2158                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
2159                    }
2160                }
2161            };
2162            Ok(make_duration_array(&converted, *to_unit))
2163        }
2164
2165        (Duration(TimeUnit::Second), Interval(IntervalUnit::MonthDayNano)) => {
2166            cast_duration_to_interval::<DurationSecondType>(array, cast_options)
2167        }
2168        (Duration(TimeUnit::Millisecond), Interval(IntervalUnit::MonthDayNano)) => {
2169            cast_duration_to_interval::<DurationMillisecondType>(array, cast_options)
2170        }
2171        (Duration(TimeUnit::Microsecond), Interval(IntervalUnit::MonthDayNano)) => {
2172            cast_duration_to_interval::<DurationMicrosecondType>(array, cast_options)
2173        }
2174        (Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano)) => {
2175            cast_duration_to_interval::<DurationNanosecondType>(array, cast_options)
2176        }
2177        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Second)) => {
2178            cast_month_day_nano_to_duration::<DurationSecondType>(array, cast_options)
2179        }
2180        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Millisecond)) => {
2181            cast_month_day_nano_to_duration::<DurationMillisecondType>(array, cast_options)
2182        }
2183        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Microsecond)) => {
2184            cast_month_day_nano_to_duration::<DurationMicrosecondType>(array, cast_options)
2185        }
2186        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Nanosecond)) => {
2187            cast_month_day_nano_to_duration::<DurationNanosecondType>(array, cast_options)
2188        }
2189        (Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
2190            cast_interval_year_month_to_interval_month_day_nano(array, cast_options)
2191        }
2192        (Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
2193            cast_interval_day_time_to_interval_month_day_nano(array, cast_options)
2194        }
2195        (Int32, Interval(IntervalUnit::YearMonth)) => {
2196            cast_reinterpret_arrays::<Int32Type, IntervalYearMonthType>(array)
2197        }
2198        (_, _) => Err(ArrowError::CastError(format!(
2199            "Casting from {from_type} to {to_type} not supported",
2200        ))),
2201    }
2202}
2203
2204fn cast_from_decimal<D, F>(
2205    array: &dyn Array,
2206    base: D::Native,
2207    scale: &i8,
2208    from_type: &DataType,
2209    to_type: &DataType,
2210    as_float: F,
2211    cast_options: &CastOptions,
2212) -> Result<ArrayRef, ArrowError>
2213where
2214    D: DecimalType + ArrowPrimitiveType,
2215    <D as ArrowPrimitiveType>::Native: ArrowNativeTypeOp + ToPrimitive,
2216    F: Fn(D::Native) -> f64,
2217{
2218    use DataType::*;
2219    // cast decimal to other type
2220    match to_type {
2221        UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, base, *scale, cast_options),
2222        UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, base, *scale, cast_options),
2223        UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, base, *scale, cast_options),
2224        UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, base, *scale, cast_options),
2225        Int8 => cast_decimal_to_integer::<D, Int8Type>(array, base, *scale, cast_options),
2226        Int16 => cast_decimal_to_integer::<D, Int16Type>(array, base, *scale, cast_options),
2227        Int32 => cast_decimal_to_integer::<D, Int32Type>(array, base, *scale, cast_options),
2228        Int64 => cast_decimal_to_integer::<D, Int64Type>(array, base, *scale, cast_options),
2229        Float32 => cast_decimal_to_float::<D, Float32Type, _>(array, |x| {
2230            (as_float(x) / 10_f64.powi(*scale as i32)) as f32
2231        }),
2232        Float64 => cast_decimal_to_float::<D, Float64Type, _>(array, |x| {
2233            as_float(x) / 10_f64.powi(*scale as i32)
2234        }),
2235        Utf8View => value_to_string_view(array, cast_options),
2236        Utf8 => value_to_string::<i32>(array, cast_options),
2237        LargeUtf8 => value_to_string::<i64>(array, cast_options),
2238        Null => Ok(new_null_array(to_type, array.len())),
2239        _ => Err(ArrowError::CastError(format!(
2240            "Casting from {from_type} to {to_type} not supported"
2241        ))),
2242    }
2243}
2244
2245fn cast_to_decimal<D, M>(
2246    array: &dyn Array,
2247    base: M,
2248    precision: &u8,
2249    scale: &i8,
2250    from_type: &DataType,
2251    to_type: &DataType,
2252    cast_options: &CastOptions,
2253) -> Result<ArrayRef, ArrowError>
2254where
2255    D: DecimalType + ArrowPrimitiveType<Native = M>,
2256    M: ArrowNativeTypeOp + DecimalCast,
2257    u8: num_traits::AsPrimitive<M>,
2258    u16: num_traits::AsPrimitive<M>,
2259    u32: num_traits::AsPrimitive<M>,
2260    u64: num_traits::AsPrimitive<M>,
2261    i8: num_traits::AsPrimitive<M>,
2262    i16: num_traits::AsPrimitive<M>,
2263    i32: num_traits::AsPrimitive<M>,
2264    i64: num_traits::AsPrimitive<M>,
2265{
2266    use DataType::*;
2267    // cast data to decimal
2268    match from_type {
2269        UInt8 => cast_integer_to_decimal::<_, D, M>(
2270            array.as_primitive::<UInt8Type>(),
2271            *precision,
2272            *scale,
2273            base,
2274            cast_options,
2275        ),
2276        UInt16 => cast_integer_to_decimal::<_, D, _>(
2277            array.as_primitive::<UInt16Type>(),
2278            *precision,
2279            *scale,
2280            base,
2281            cast_options,
2282        ),
2283        UInt32 => cast_integer_to_decimal::<_, D, _>(
2284            array.as_primitive::<UInt32Type>(),
2285            *precision,
2286            *scale,
2287            base,
2288            cast_options,
2289        ),
2290        UInt64 => cast_integer_to_decimal::<_, D, _>(
2291            array.as_primitive::<UInt64Type>(),
2292            *precision,
2293            *scale,
2294            base,
2295            cast_options,
2296        ),
2297        Int8 => cast_integer_to_decimal::<_, D, _>(
2298            array.as_primitive::<Int8Type>(),
2299            *precision,
2300            *scale,
2301            base,
2302            cast_options,
2303        ),
2304        Int16 => cast_integer_to_decimal::<_, D, _>(
2305            array.as_primitive::<Int16Type>(),
2306            *precision,
2307            *scale,
2308            base,
2309            cast_options,
2310        ),
2311        Int32 => cast_integer_to_decimal::<_, D, _>(
2312            array.as_primitive::<Int32Type>(),
2313            *precision,
2314            *scale,
2315            base,
2316            cast_options,
2317        ),
2318        Int64 => cast_integer_to_decimal::<_, D, _>(
2319            array.as_primitive::<Int64Type>(),
2320            *precision,
2321            *scale,
2322            base,
2323            cast_options,
2324        ),
2325        Float32 => cast_floating_point_to_decimal::<_, D>(
2326            array.as_primitive::<Float32Type>(),
2327            *precision,
2328            *scale,
2329            cast_options,
2330        ),
2331        Float64 => cast_floating_point_to_decimal::<_, D>(
2332            array.as_primitive::<Float64Type>(),
2333            *precision,
2334            *scale,
2335            cast_options,
2336        ),
2337        Utf8View | Utf8 => {
2338            cast_string_to_decimal::<D, i32>(array, *precision, *scale, cast_options)
2339        }
2340        LargeUtf8 => cast_string_to_decimal::<D, i64>(array, *precision, *scale, cast_options),
2341        Null => Ok(new_null_array(to_type, array.len())),
2342        _ => Err(ArrowError::CastError(format!(
2343            "Casting from {from_type} to {to_type} not supported"
2344        ))),
2345    }
2346}
2347
2348/// Get the time unit as a multiple of a second
2349const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
2350    match unit {
2351        TimeUnit::Second => 1,
2352        TimeUnit::Millisecond => MILLISECONDS,
2353        TimeUnit::Microsecond => MICROSECONDS,
2354        TimeUnit::Nanosecond => NANOSECONDS,
2355    }
2356}
2357
2358/// Convert Array into a PrimitiveArray of type, and apply numeric cast
2359fn cast_numeric_arrays<FROM, TO>(
2360    from: &dyn Array,
2361    cast_options: &CastOptions,
2362) -> Result<ArrayRef, ArrowError>
2363where
2364    FROM: ArrowPrimitiveType,
2365    TO: ArrowPrimitiveType,
2366    FROM::Native: NumCast,
2367    TO::Native: NumCast,
2368{
2369    if cast_options.safe {
2370        // If the value can't be casted to the `TO::Native`, return null
2371        Ok(Arc::new(numeric_cast::<FROM, TO>(
2372            from.as_primitive::<FROM>(),
2373        )))
2374    } else {
2375        // If the value can't be casted to the `TO::Native`, return error
2376        Ok(Arc::new(try_numeric_cast::<FROM, TO>(
2377            from.as_primitive::<FROM>(),
2378        )?))
2379    }
2380}
2381
2382// Natural cast between numeric types
2383// If the value of T can't be casted to R, will throw error
2384fn try_numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>, ArrowError>
2385where
2386    T: ArrowPrimitiveType,
2387    R: ArrowPrimitiveType,
2388    T::Native: NumCast,
2389    R::Native: NumCast,
2390{
2391    from.try_unary(|value| {
2392        num_traits::cast::cast::<T::Native, R::Native>(value).ok_or_else(|| {
2393            ArrowError::CastError(format!(
2394                "Can't cast value {:?} to type {}",
2395                value,
2396                R::DATA_TYPE
2397            ))
2398        })
2399    })
2400}
2401
2402// Natural cast between numeric types
2403// If the value of T can't be casted to R, it will be converted to null
2404fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> PrimitiveArray<R>
2405where
2406    T: ArrowPrimitiveType,
2407    R: ArrowPrimitiveType,
2408    T::Native: NumCast,
2409    R::Native: NumCast,
2410{
2411    from.unary_opt::<_, R>(num_traits::cast::cast::<T::Native, R::Native>)
2412}
2413
2414fn cast_numeric_to_binary<FROM: ArrowPrimitiveType, O: OffsetSizeTrait>(
2415    array: &dyn Array,
2416) -> Result<ArrayRef, ArrowError> {
2417    let array = array.as_primitive::<FROM>();
2418    let size = std::mem::size_of::<FROM::Native>();
2419    let offsets = OffsetBuffer::from_repeated_length(size, array.len());
2420    Ok(Arc::new(GenericBinaryArray::<O>::try_new(
2421        offsets,
2422        array.values().inner().clone(),
2423        array.nulls().cloned(),
2424    )?))
2425}
2426
2427fn adjust_timestamp_to_timezone<T: ArrowTimestampType>(
2428    array: PrimitiveArray<Int64Type>,
2429    to_tz: &Tz,
2430    cast_options: &CastOptions,
2431) -> Result<PrimitiveArray<Int64Type>, ArrowError> {
2432    let adjust = |o| {
2433        let local = as_datetime::<T>(o)?;
2434        let offset = to_tz.offset_from_local_datetime(&local).single()?;
2435        T::make_value(local - offset.fix())
2436    };
2437    let adjusted = if cast_options.safe {
2438        array.unary_opt::<_, Int64Type>(adjust)
2439    } else {
2440        array.try_unary::<_, Int64Type, _>(|o| {
2441            adjust(o).ok_or_else(|| {
2442                ArrowError::CastError("Cannot cast timezone to different timezone".to_string())
2443            })
2444        })?
2445    };
2446    Ok(adjusted)
2447}
2448
2449/// Cast numeric types to Boolean
2450///
2451/// Any zero value returns `false` while non-zero returns `true`
2452fn cast_numeric_to_bool<FROM>(from: &dyn Array) -> Result<ArrayRef, ArrowError>
2453where
2454    FROM: ArrowPrimitiveType,
2455{
2456    numeric_to_bool_cast::<FROM>(from.as_primitive::<FROM>()).map(|to| Arc::new(to) as ArrayRef)
2457}
2458
2459fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray, ArrowError>
2460where
2461    T: ArrowPrimitiveType + ArrowPrimitiveType,
2462{
2463    let mut b = BooleanBuilder::with_capacity(from.len());
2464
2465    for i in 0..from.len() {
2466        if from.is_null(i) {
2467            b.append_null();
2468        } else if from.value(i) != T::default_value() {
2469            b.append_value(true);
2470        } else {
2471            b.append_value(false);
2472        }
2473    }
2474
2475    Ok(b.finish())
2476}
2477
2478/// Cast Boolean types to numeric
2479///
2480/// `false` returns 0 while `true` returns 1
2481fn cast_bool_to_numeric<TO>(
2482    from: &dyn Array,
2483    cast_options: &CastOptions,
2484) -> Result<ArrayRef, ArrowError>
2485where
2486    TO: ArrowPrimitiveType,
2487    TO::Native: num_traits::cast::NumCast,
2488{
2489    Ok(Arc::new(bool_to_numeric_cast::<TO>(
2490        from.as_any().downcast_ref::<BooleanArray>().unwrap(),
2491        cast_options,
2492    )))
2493}
2494
2495fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions) -> PrimitiveArray<T>
2496where
2497    T: ArrowPrimitiveType,
2498    T::Native: num_traits::NumCast,
2499{
2500    let iter = (0..from.len()).map(|i| {
2501        if from.is_null(i) {
2502            None
2503        } else if from.value(i) {
2504            // a workaround to cast a primitive to T::Native, infallible
2505            num_traits::cast::cast(1)
2506        } else {
2507            Some(T::default_value())
2508        }
2509    });
2510    // Benefit:
2511    //     20% performance improvement
2512    // Soundness:
2513    //     The iterator is trustedLen because it comes from a Range
2514    unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
2515}
2516
2517/// Helper function to cast from one `BinaryArray` or 'LargeBinaryArray' to 'FixedSizeBinaryArray'.
2518fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
2519    array: &dyn Array,
2520    byte_width: i32,
2521    cast_options: &CastOptions,
2522) -> Result<ArrayRef, ArrowError> {
2523    let array = array.as_binary::<O>();
2524    let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(), byte_width);
2525
2526    for i in 0..array.len() {
2527        if array.is_null(i) {
2528            builder.append_null();
2529        } else {
2530            match builder.append_value(array.value(i)) {
2531                Ok(_) => {}
2532                Err(e) => match cast_options.safe {
2533                    true => builder.append_null(),
2534                    false => return Err(e),
2535                },
2536            }
2537        }
2538    }
2539
2540    Ok(Arc::new(builder.finish()))
2541}
2542
2543/// Helper function to cast from 'FixedSizeBinaryArray' to one `BinaryArray` or 'LargeBinaryArray'.
2544/// If the target one is too large for the source array it will return an Error.
2545fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
2546    array: &dyn Array,
2547    byte_width: i32,
2548) -> Result<ArrayRef, ArrowError> {
2549    let array = array
2550        .as_any()
2551        .downcast_ref::<FixedSizeBinaryArray>()
2552        .unwrap();
2553
2554    let offsets: i128 = byte_width as i128 * array.len() as i128;
2555
2556    let is_binary = matches!(GenericBinaryType::<O>::DATA_TYPE, DataType::Binary);
2557    if is_binary && offsets > i32::MAX as i128 {
2558        return Err(ArrowError::ComputeError(
2559            "FixedSizeBinary array too large to cast to Binary array".to_string(),
2560        ));
2561    } else if !is_binary && offsets > i64::MAX as i128 {
2562        return Err(ArrowError::ComputeError(
2563            "FixedSizeBinary array too large to cast to LargeBinary array".to_string(),
2564        ));
2565    }
2566
2567    let mut builder = GenericBinaryBuilder::<O>::with_capacity(array.len(), array.len());
2568
2569    for i in 0..array.len() {
2570        if array.is_null(i) {
2571            builder.append_null();
2572        } else {
2573            builder.append_value(array.value(i));
2574        }
2575    }
2576
2577    Ok(Arc::new(builder.finish()))
2578}
2579
2580fn cast_fixed_size_binary_to_binary_view(
2581    array: &dyn Array,
2582    _byte_width: i32,
2583) -> Result<ArrayRef, ArrowError> {
2584    let array = array
2585        .as_any()
2586        .downcast_ref::<FixedSizeBinaryArray>()
2587        .unwrap();
2588
2589    let mut builder = BinaryViewBuilder::with_capacity(array.len());
2590    for i in 0..array.len() {
2591        if array.is_null(i) {
2592            builder.append_null();
2593        } else {
2594            builder.append_value(array.value(i));
2595        }
2596    }
2597
2598    Ok(Arc::new(builder.finish()))
2599}
2600
2601/// Helper function to cast from one `ByteArrayType` to another and vice versa.
2602/// If the target one (e.g., `LargeUtf8`) is too large for the source array it will return an Error.
2603fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2604where
2605    FROM: ByteArrayType,
2606    TO: ByteArrayType<Native = FROM::Native>,
2607    FROM::Offset: OffsetSizeTrait + ToPrimitive,
2608    TO::Offset: OffsetSizeTrait + NumCast,
2609{
2610    let data = array.to_data();
2611    assert_eq!(data.data_type(), &FROM::DATA_TYPE);
2612    let str_values_buf = data.buffers()[1].clone();
2613    let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
2614
2615    let mut offset_builder = BufferBuilder::<TO::Offset>::new(offsets.len());
2616    offsets
2617        .iter()
2618        .try_for_each::<_, Result<_, ArrowError>>(|offset| {
2619            let offset =
2620                <<TO as ByteArrayType>::Offset as NumCast>::from(*offset).ok_or_else(|| {
2621                    ArrowError::ComputeError(format!(
2622                        "{}{} array too large to cast to {}{} array",
2623                        FROM::Offset::PREFIX,
2624                        FROM::PREFIX,
2625                        TO::Offset::PREFIX,
2626                        TO::PREFIX
2627                    ))
2628                })?;
2629            offset_builder.append(offset);
2630            Ok(())
2631        })?;
2632
2633    let offset_buffer = offset_builder.finish();
2634
2635    let dtype = TO::DATA_TYPE;
2636
2637    let builder = ArrayData::builder(dtype)
2638        .offset(array.offset())
2639        .len(array.len())
2640        .add_buffer(offset_buffer)
2641        .add_buffer(str_values_buf)
2642        .nulls(data.nulls().cloned());
2643
2644    let array_data = unsafe { builder.build_unchecked() };
2645
2646    Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
2647}
2648
2649/// Helper function to cast from one `ByteViewType` array to `ByteArrayType` array.
2650fn cast_view_to_byte<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2651where
2652    FROM: ByteViewType,
2653    TO: ByteArrayType,
2654    FROM::Native: AsRef<TO::Native>,
2655{
2656    let data = array.to_data();
2657    let view_array = GenericByteViewArray::<FROM>::from(data);
2658
2659    let len = view_array.len();
2660    let bytes = view_array
2661        .views()
2662        .iter()
2663        .map(|v| ByteView::from(*v).length as usize)
2664        .sum::<usize>();
2665
2666    let mut byte_array_builder = GenericByteBuilder::<TO>::with_capacity(len, bytes);
2667
2668    for val in view_array.iter() {
2669        byte_array_builder.append_option(val);
2670    }
2671
2672    Ok(Arc::new(byte_array_builder.finish()))
2673}
2674
2675#[cfg(test)]
2676mod tests {
2677    use super::*;
2678    use DataType::*;
2679    use arrow_array::{Int64Array, RunArray, StringArray};
2680    use arrow_buffer::i256;
2681    use arrow_buffer::{Buffer, IntervalDayTime, NullBuffer};
2682    use arrow_schema::{DataType, Field};
2683    use chrono::NaiveDate;
2684    use half::f16;
2685    use std::sync::Arc;
2686
2687    #[derive(Clone)]
2688    struct DecimalCastTestConfig {
2689        input_prec: u8,
2690        input_scale: i8,
2691        input_repr: i128,
2692        output_prec: u8,
2693        output_scale: i8,
2694        expected_output_repr: Result<i128, String>, // the error variant can contain a string
2695                                                    // template where the "{}" will be
2696                                                    // replaced with the decimal type name
2697                                                    // (e.g. Decimal128)
2698    }
2699
2700    macro_rules! generate_cast_test_case {
2701        ($INPUT_ARRAY: expr, $OUTPUT_TYPE_ARRAY: ident, $OUTPUT_TYPE: expr, $OUTPUT_VALUES: expr) => {
2702            let output =
2703                $OUTPUT_TYPE_ARRAY::from($OUTPUT_VALUES).with_data_type($OUTPUT_TYPE.clone());
2704
2705            // assert cast type
2706            let input_array_type = $INPUT_ARRAY.data_type();
2707            assert!(can_cast_types(input_array_type, $OUTPUT_TYPE));
2708            let result = cast($INPUT_ARRAY, $OUTPUT_TYPE).unwrap();
2709            assert_eq!($OUTPUT_TYPE, result.data_type());
2710            assert_eq!(result.as_ref(), &output);
2711
2712            let cast_option = CastOptions {
2713                safe: false,
2714                format_options: FormatOptions::default(),
2715            };
2716            let result = cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap();
2717            assert_eq!($OUTPUT_TYPE, result.data_type());
2718            assert_eq!(result.as_ref(), &output);
2719        };
2720    }
2721
2722    fn run_decimal_cast_test_case<I, O>(t: DecimalCastTestConfig)
2723    where
2724        I: DecimalType,
2725        O: DecimalType,
2726        I::Native: DecimalCast,
2727        O::Native: DecimalCast,
2728    {
2729        let array = vec![I::Native::from_decimal(t.input_repr)];
2730        let array = array
2731            .into_iter()
2732            .collect::<PrimitiveArray<I>>()
2733            .with_precision_and_scale(t.input_prec, t.input_scale)
2734            .unwrap();
2735        let input_type = array.data_type();
2736        let output_type = O::TYPE_CONSTRUCTOR(t.output_prec, t.output_scale);
2737        assert!(can_cast_types(input_type, &output_type));
2738
2739        let options = CastOptions {
2740            safe: false,
2741            ..Default::default()
2742        };
2743        let result = cast_with_options(&array, &output_type, &options);
2744
2745        match t.expected_output_repr {
2746            Ok(v) => {
2747                let expected_array = vec![O::Native::from_decimal(v)];
2748                let expected_array = expected_array
2749                    .into_iter()
2750                    .collect::<PrimitiveArray<O>>()
2751                    .with_precision_and_scale(t.output_prec, t.output_scale)
2752                    .unwrap();
2753                assert_eq!(*result.unwrap(), expected_array);
2754            }
2755            Err(expected_output_message_template) => {
2756                assert!(result.is_err());
2757                let expected_error_message =
2758                    expected_output_message_template.replace("{}", O::PREFIX);
2759                assert_eq!(result.unwrap_err().to_string(), expected_error_message);
2760            }
2761        }
2762    }
2763
2764    fn create_decimal32_array(
2765        array: Vec<Option<i32>>,
2766        precision: u8,
2767        scale: i8,
2768    ) -> Result<Decimal32Array, ArrowError> {
2769        array
2770            .into_iter()
2771            .collect::<Decimal32Array>()
2772            .with_precision_and_scale(precision, scale)
2773    }
2774
2775    fn create_decimal64_array(
2776        array: Vec<Option<i64>>,
2777        precision: u8,
2778        scale: i8,
2779    ) -> Result<Decimal64Array, ArrowError> {
2780        array
2781            .into_iter()
2782            .collect::<Decimal64Array>()
2783            .with_precision_and_scale(precision, scale)
2784    }
2785
2786    fn create_decimal128_array(
2787        array: Vec<Option<i128>>,
2788        precision: u8,
2789        scale: i8,
2790    ) -> Result<Decimal128Array, ArrowError> {
2791        array
2792            .into_iter()
2793            .collect::<Decimal128Array>()
2794            .with_precision_and_scale(precision, scale)
2795    }
2796
2797    fn create_decimal256_array(
2798        array: Vec<Option<i256>>,
2799        precision: u8,
2800        scale: i8,
2801    ) -> Result<Decimal256Array, ArrowError> {
2802        array
2803            .into_iter()
2804            .collect::<Decimal256Array>()
2805            .with_precision_and_scale(precision, scale)
2806    }
2807
2808    #[test]
2809    #[cfg(not(feature = "force_validate"))]
2810    #[should_panic(
2811        expected = "Cannot cast to Decimal128(20, 3). Overflowing on 57896044618658097711785492504343953926634992332820282019728792003956564819967"
2812    )]
2813    fn test_cast_decimal_to_decimal_round_with_error() {
2814        // decimal256 to decimal128 overflow
2815        let array = vec![
2816            Some(i256::from_i128(1123454)),
2817            Some(i256::from_i128(2123456)),
2818            Some(i256::from_i128(-3123453)),
2819            Some(i256::from_i128(-3123456)),
2820            None,
2821            Some(i256::MAX),
2822            Some(i256::MIN),
2823        ];
2824        let input_decimal_array = create_decimal256_array(array, 76, 4).unwrap();
2825        let array = Arc::new(input_decimal_array) as ArrayRef;
2826        let input_type = DataType::Decimal256(76, 4);
2827        let output_type = DataType::Decimal128(20, 3);
2828        assert!(can_cast_types(&input_type, &output_type));
2829        generate_cast_test_case!(
2830            &array,
2831            Decimal128Array,
2832            &output_type,
2833            vec![
2834                Some(112345_i128),
2835                Some(212346_i128),
2836                Some(-312345_i128),
2837                Some(-312346_i128),
2838                None,
2839                None,
2840                None,
2841            ]
2842        );
2843    }
2844
2845    #[test]
2846    #[cfg(not(feature = "force_validate"))]
2847    fn test_cast_decimal_to_decimal_round() {
2848        let array = vec![
2849            Some(1123454),
2850            Some(2123456),
2851            Some(-3123453),
2852            Some(-3123456),
2853            None,
2854        ];
2855        let array = create_decimal128_array(array, 20, 4).unwrap();
2856        // decimal128 to decimal128
2857        let input_type = DataType::Decimal128(20, 4);
2858        let output_type = DataType::Decimal128(20, 3);
2859        assert!(can_cast_types(&input_type, &output_type));
2860        generate_cast_test_case!(
2861            &array,
2862            Decimal128Array,
2863            &output_type,
2864            vec![
2865                Some(112345_i128),
2866                Some(212346_i128),
2867                Some(-312345_i128),
2868                Some(-312346_i128),
2869                None
2870            ]
2871        );
2872
2873        // decimal128 to decimal256
2874        let input_type = DataType::Decimal128(20, 4);
2875        let output_type = DataType::Decimal256(20, 3);
2876        assert!(can_cast_types(&input_type, &output_type));
2877        generate_cast_test_case!(
2878            &array,
2879            Decimal256Array,
2880            &output_type,
2881            vec![
2882                Some(i256::from_i128(112345_i128)),
2883                Some(i256::from_i128(212346_i128)),
2884                Some(i256::from_i128(-312345_i128)),
2885                Some(i256::from_i128(-312346_i128)),
2886                None
2887            ]
2888        );
2889
2890        // decimal256
2891        let array = vec![
2892            Some(i256::from_i128(1123454)),
2893            Some(i256::from_i128(2123456)),
2894            Some(i256::from_i128(-3123453)),
2895            Some(i256::from_i128(-3123456)),
2896            None,
2897        ];
2898        let array = create_decimal256_array(array, 20, 4).unwrap();
2899
2900        // decimal256 to decimal256
2901        let input_type = DataType::Decimal256(20, 4);
2902        let output_type = DataType::Decimal256(20, 3);
2903        assert!(can_cast_types(&input_type, &output_type));
2904        generate_cast_test_case!(
2905            &array,
2906            Decimal256Array,
2907            &output_type,
2908            vec![
2909                Some(i256::from_i128(112345_i128)),
2910                Some(i256::from_i128(212346_i128)),
2911                Some(i256::from_i128(-312345_i128)),
2912                Some(i256::from_i128(-312346_i128)),
2913                None
2914            ]
2915        );
2916        // decimal256 to decimal128
2917        let input_type = DataType::Decimal256(20, 4);
2918        let output_type = DataType::Decimal128(20, 3);
2919        assert!(can_cast_types(&input_type, &output_type));
2920        generate_cast_test_case!(
2921            &array,
2922            Decimal128Array,
2923            &output_type,
2924            vec![
2925                Some(112345_i128),
2926                Some(212346_i128),
2927                Some(-312345_i128),
2928                Some(-312346_i128),
2929                None
2930            ]
2931        );
2932    }
2933
2934    #[test]
2935    fn test_cast_decimal32_to_decimal32() {
2936        // test changing precision
2937        let input_type = DataType::Decimal32(9, 3);
2938        let output_type = DataType::Decimal32(9, 4);
2939        assert!(can_cast_types(&input_type, &output_type));
2940        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2941        let array = create_decimal32_array(array, 9, 3).unwrap();
2942        generate_cast_test_case!(
2943            &array,
2944            Decimal32Array,
2945            &output_type,
2946            vec![
2947                Some(11234560_i32),
2948                Some(21234560_i32),
2949                Some(31234560_i32),
2950                None
2951            ]
2952        );
2953        // negative test
2954        let array = vec![Some(123456), None];
2955        let array = create_decimal32_array(array, 9, 0).unwrap();
2956        let result_safe = cast(&array, &DataType::Decimal32(2, 2));
2957        assert!(result_safe.is_ok());
2958        let options = CastOptions {
2959            safe: false,
2960            ..Default::default()
2961        };
2962
2963        let result_unsafe = cast_with_options(&array, &DataType::Decimal32(2, 2), &options);
2964        assert_eq!(
2965            "Invalid argument error: 123456.00 is too large to store in a Decimal32 of precision 2. Max is 0.99",
2966            result_unsafe.unwrap_err().to_string()
2967        );
2968    }
2969
2970    #[test]
2971    fn test_cast_decimal64_to_decimal64() {
2972        // test changing precision
2973        let input_type = DataType::Decimal64(17, 3);
2974        let output_type = DataType::Decimal64(17, 4);
2975        assert!(can_cast_types(&input_type, &output_type));
2976        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
2977        let array = create_decimal64_array(array, 17, 3).unwrap();
2978        generate_cast_test_case!(
2979            &array,
2980            Decimal64Array,
2981            &output_type,
2982            vec![
2983                Some(11234560_i64),
2984                Some(21234560_i64),
2985                Some(31234560_i64),
2986                None
2987            ]
2988        );
2989        // negative test
2990        let array = vec![Some(123456), None];
2991        let array = create_decimal64_array(array, 9, 0).unwrap();
2992        let result_safe = cast(&array, &DataType::Decimal64(2, 2));
2993        assert!(result_safe.is_ok());
2994        let options = CastOptions {
2995            safe: false,
2996            ..Default::default()
2997        };
2998
2999        let result_unsafe = cast_with_options(&array, &DataType::Decimal64(2, 2), &options);
3000        assert_eq!(
3001            "Invalid argument error: 123456.00 is too large to store in a Decimal64 of precision 2. Max is 0.99",
3002            result_unsafe.unwrap_err().to_string()
3003        );
3004    }
3005
3006    #[test]
3007    fn test_cast_decimal128_to_decimal128() {
3008        // test changing precision
3009        let input_type = DataType::Decimal128(20, 3);
3010        let output_type = DataType::Decimal128(20, 4);
3011        assert!(can_cast_types(&input_type, &output_type));
3012        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3013        let array = create_decimal128_array(array, 20, 3).unwrap();
3014        generate_cast_test_case!(
3015            &array,
3016            Decimal128Array,
3017            &output_type,
3018            vec![
3019                Some(11234560_i128),
3020                Some(21234560_i128),
3021                Some(31234560_i128),
3022                None
3023            ]
3024        );
3025        // negative test
3026        let array = vec![Some(123456), None];
3027        let array = create_decimal128_array(array, 10, 0).unwrap();
3028        let result_safe = cast(&array, &DataType::Decimal128(2, 2));
3029        assert!(result_safe.is_ok());
3030        let options = CastOptions {
3031            safe: false,
3032            ..Default::default()
3033        };
3034
3035        let result_unsafe = cast_with_options(&array, &DataType::Decimal128(2, 2), &options);
3036        assert_eq!(
3037            "Invalid argument error: 123456.00 is too large to store in a Decimal128 of precision 2. Max is 0.99",
3038            result_unsafe.unwrap_err().to_string()
3039        );
3040    }
3041
3042    #[test]
3043    fn test_cast_decimal32_to_decimal32_dict() {
3044        let p = 9;
3045        let s = 3;
3046        let input_type = DataType::Decimal32(p, s);
3047        let output_type = DataType::Dictionary(
3048            Box::new(DataType::Int32),
3049            Box::new(DataType::Decimal32(p, s)),
3050        );
3051        assert!(can_cast_types(&input_type, &output_type));
3052        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3053        let array = create_decimal32_array(array, p, s).unwrap();
3054        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3055        assert_eq!(cast_array.data_type(), &output_type);
3056    }
3057
3058    #[test]
3059    fn test_cast_decimal64_to_decimal64_dict() {
3060        let p = 15;
3061        let s = 3;
3062        let input_type = DataType::Decimal64(p, s);
3063        let output_type = DataType::Dictionary(
3064            Box::new(DataType::Int32),
3065            Box::new(DataType::Decimal64(p, s)),
3066        );
3067        assert!(can_cast_types(&input_type, &output_type));
3068        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3069        let array = create_decimal64_array(array, p, s).unwrap();
3070        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3071        assert_eq!(cast_array.data_type(), &output_type);
3072    }
3073
3074    #[test]
3075    fn test_cast_decimal128_to_decimal128_dict() {
3076        let p = 20;
3077        let s = 3;
3078        let input_type = DataType::Decimal128(p, s);
3079        let output_type = DataType::Dictionary(
3080            Box::new(DataType::Int32),
3081            Box::new(DataType::Decimal128(p, s)),
3082        );
3083        assert!(can_cast_types(&input_type, &output_type));
3084        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3085        let array = create_decimal128_array(array, p, s).unwrap();
3086        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3087        assert_eq!(cast_array.data_type(), &output_type);
3088    }
3089
3090    #[test]
3091    fn test_cast_decimal256_to_decimal256_dict() {
3092        let p = 20;
3093        let s = 3;
3094        let input_type = DataType::Decimal256(p, s);
3095        let output_type = DataType::Dictionary(
3096            Box::new(DataType::Int32),
3097            Box::new(DataType::Decimal256(p, s)),
3098        );
3099        assert!(can_cast_types(&input_type, &output_type));
3100        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3101        let array = create_decimal128_array(array, p, s).unwrap();
3102        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3103        assert_eq!(cast_array.data_type(), &output_type);
3104    }
3105
3106    #[test]
3107    fn test_cast_decimal32_to_decimal32_overflow() {
3108        let input_type = DataType::Decimal32(9, 3);
3109        let output_type = DataType::Decimal32(9, 9);
3110        assert!(can_cast_types(&input_type, &output_type));
3111
3112        let array = vec![Some(i32::MAX)];
3113        let array = create_decimal32_array(array, 9, 3).unwrap();
3114        let result = cast_with_options(
3115            &array,
3116            &output_type,
3117            &CastOptions {
3118                safe: false,
3119                format_options: FormatOptions::default(),
3120            },
3121        );
3122        assert_eq!(
3123            "Cast error: Cannot cast to Decimal32(9, 9). Overflowing on 2147483647",
3124            result.unwrap_err().to_string()
3125        );
3126    }
3127
3128    #[test]
3129    fn test_cast_decimal32_to_decimal32_large_scale_reduction() {
3130        let array = vec![Some(-999999999), Some(0), Some(999999999), None];
3131        let array = create_decimal32_array(array, 9, 3).unwrap();
3132
3133        // Divide out all digits of precision -- rounding could still produce +/- 1
3134        let output_type = DataType::Decimal32(9, -6);
3135        assert!(can_cast_types(array.data_type(), &output_type));
3136        generate_cast_test_case!(
3137            &array,
3138            Decimal32Array,
3139            &output_type,
3140            vec![Some(-1), Some(0), Some(1), None]
3141        );
3142
3143        // Divide out more digits than we have precision -- all-zero result
3144        let output_type = DataType::Decimal32(9, -7);
3145        assert!(can_cast_types(array.data_type(), &output_type));
3146        generate_cast_test_case!(
3147            &array,
3148            Decimal32Array,
3149            &output_type,
3150            vec![Some(0), Some(0), Some(0), None]
3151        );
3152    }
3153
3154    #[test]
3155    fn test_cast_decimal64_to_decimal64_overflow() {
3156        let input_type = DataType::Decimal64(18, 3);
3157        let output_type = DataType::Decimal64(18, 18);
3158        assert!(can_cast_types(&input_type, &output_type));
3159
3160        let array = vec![Some(i64::MAX)];
3161        let array = create_decimal64_array(array, 18, 3).unwrap();
3162        let result = cast_with_options(
3163            &array,
3164            &output_type,
3165            &CastOptions {
3166                safe: false,
3167                format_options: FormatOptions::default(),
3168            },
3169        );
3170        assert_eq!(
3171            "Cast error: Cannot cast to Decimal64(18, 18). Overflowing on 9223372036854775807",
3172            result.unwrap_err().to_string()
3173        );
3174    }
3175
3176    #[test]
3177    fn test_cast_decimal64_to_decimal64_large_scale_reduction() {
3178        let array = vec![
3179            Some(-999999999999999999),
3180            Some(0),
3181            Some(999999999999999999),
3182            None,
3183        ];
3184        let array = create_decimal64_array(array, 18, 3).unwrap();
3185
3186        // Divide out all digits of precision -- rounding could still produce +/- 1
3187        let output_type = DataType::Decimal64(18, -15);
3188        assert!(can_cast_types(array.data_type(), &output_type));
3189        generate_cast_test_case!(
3190            &array,
3191            Decimal64Array,
3192            &output_type,
3193            vec![Some(-1), Some(0), Some(1), None]
3194        );
3195
3196        // Divide out more digits than we have precision -- all-zero result
3197        let output_type = DataType::Decimal64(18, -16);
3198        assert!(can_cast_types(array.data_type(), &output_type));
3199        generate_cast_test_case!(
3200            &array,
3201            Decimal64Array,
3202            &output_type,
3203            vec![Some(0), Some(0), Some(0), None]
3204        );
3205    }
3206
3207    #[test]
3208    fn test_cast_floating_to_decimals() {
3209        for output_type in [
3210            DataType::Decimal32(9, 3),
3211            DataType::Decimal64(9, 3),
3212            DataType::Decimal128(9, 3),
3213            DataType::Decimal256(9, 3),
3214        ] {
3215            let input_type = DataType::Float64;
3216            assert!(can_cast_types(&input_type, &output_type));
3217
3218            let array = vec![Some(1.1_f64)];
3219            let array = PrimitiveArray::<Float64Type>::from_iter(array);
3220            let result = cast_with_options(
3221                &array,
3222                &output_type,
3223                &CastOptions {
3224                    safe: false,
3225                    format_options: FormatOptions::default(),
3226                },
3227            );
3228            assert!(
3229                result.is_ok(),
3230                "Failed to cast to {output_type} with: {}",
3231                result.unwrap_err()
3232            );
3233        }
3234    }
3235
3236    #[test]
3237    fn test_cast_decimal128_to_decimal128_overflow() {
3238        let input_type = DataType::Decimal128(38, 3);
3239        let output_type = DataType::Decimal128(38, 38);
3240        assert!(can_cast_types(&input_type, &output_type));
3241
3242        let array = vec![Some(i128::MAX)];
3243        let array = create_decimal128_array(array, 38, 3).unwrap();
3244        let result = cast_with_options(
3245            &array,
3246            &output_type,
3247            &CastOptions {
3248                safe: false,
3249                format_options: FormatOptions::default(),
3250            },
3251        );
3252        assert_eq!(
3253            "Cast error: Cannot cast to Decimal128(38, 38). Overflowing on 170141183460469231731687303715884105727",
3254            result.unwrap_err().to_string()
3255        );
3256    }
3257
3258    #[test]
3259    fn test_cast_decimal128_to_decimal256_overflow() {
3260        let input_type = DataType::Decimal128(38, 3);
3261        let output_type = DataType::Decimal256(76, 76);
3262        assert!(can_cast_types(&input_type, &output_type));
3263
3264        let array = vec![Some(i128::MAX)];
3265        let array = create_decimal128_array(array, 38, 3).unwrap();
3266        let result = cast_with_options(
3267            &array,
3268            &output_type,
3269            &CastOptions {
3270                safe: false,
3271                format_options: FormatOptions::default(),
3272            },
3273        );
3274        assert_eq!(
3275            "Cast error: Cannot cast to Decimal256(76, 76). Overflowing on 170141183460469231731687303715884105727",
3276            result.unwrap_err().to_string()
3277        );
3278    }
3279
3280    #[test]
3281    fn test_cast_decimal32_to_decimal256() {
3282        let input_type = DataType::Decimal32(8, 3);
3283        let output_type = DataType::Decimal256(20, 4);
3284        assert!(can_cast_types(&input_type, &output_type));
3285        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3286        let array = create_decimal32_array(array, 8, 3).unwrap();
3287        generate_cast_test_case!(
3288            &array,
3289            Decimal256Array,
3290            &output_type,
3291            vec![
3292                Some(i256::from_i128(11234560_i128)),
3293                Some(i256::from_i128(21234560_i128)),
3294                Some(i256::from_i128(31234560_i128)),
3295                None
3296            ]
3297        );
3298    }
3299    #[test]
3300    fn test_cast_decimal64_to_decimal256() {
3301        let input_type = DataType::Decimal64(12, 3);
3302        let output_type = DataType::Decimal256(20, 4);
3303        assert!(can_cast_types(&input_type, &output_type));
3304        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3305        let array = create_decimal64_array(array, 12, 3).unwrap();
3306        generate_cast_test_case!(
3307            &array,
3308            Decimal256Array,
3309            &output_type,
3310            vec![
3311                Some(i256::from_i128(11234560_i128)),
3312                Some(i256::from_i128(21234560_i128)),
3313                Some(i256::from_i128(31234560_i128)),
3314                None
3315            ]
3316        );
3317    }
3318    #[test]
3319    fn test_cast_decimal128_to_decimal256() {
3320        let input_type = DataType::Decimal128(20, 3);
3321        let output_type = DataType::Decimal256(20, 4);
3322        assert!(can_cast_types(&input_type, &output_type));
3323        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3324        let array = create_decimal128_array(array, 20, 3).unwrap();
3325        generate_cast_test_case!(
3326            &array,
3327            Decimal256Array,
3328            &output_type,
3329            vec![
3330                Some(i256::from_i128(11234560_i128)),
3331                Some(i256::from_i128(21234560_i128)),
3332                Some(i256::from_i128(31234560_i128)),
3333                None
3334            ]
3335        );
3336    }
3337
3338    #[test]
3339    fn test_cast_decimal256_to_decimal128_overflow() {
3340        let input_type = DataType::Decimal256(76, 5);
3341        let output_type = DataType::Decimal128(38, 7);
3342        assert!(can_cast_types(&input_type, &output_type));
3343        let array = vec![Some(i256::from_i128(i128::MAX))];
3344        let array = create_decimal256_array(array, 76, 5).unwrap();
3345        let result = cast_with_options(
3346            &array,
3347            &output_type,
3348            &CastOptions {
3349                safe: false,
3350                format_options: FormatOptions::default(),
3351            },
3352        );
3353        assert_eq!(
3354            "Cast error: Cannot cast to Decimal128(38, 7). Overflowing on 170141183460469231731687303715884105727",
3355            result.unwrap_err().to_string()
3356        );
3357    }
3358
3359    #[test]
3360    fn test_cast_decimal256_to_decimal256_overflow() {
3361        let input_type = DataType::Decimal256(76, 5);
3362        let output_type = DataType::Decimal256(76, 55);
3363        assert!(can_cast_types(&input_type, &output_type));
3364        let array = vec![Some(i256::from_i128(i128::MAX))];
3365        let array = create_decimal256_array(array, 76, 5).unwrap();
3366        let result = cast_with_options(
3367            &array,
3368            &output_type,
3369            &CastOptions {
3370                safe: false,
3371                format_options: FormatOptions::default(),
3372            },
3373        );
3374        assert_eq!(
3375            "Cast error: Cannot cast to Decimal256(76, 55). Overflowing on 170141183460469231731687303715884105727",
3376            result.unwrap_err().to_string()
3377        );
3378    }
3379
3380    #[test]
3381    fn test_cast_decimal256_to_decimal128() {
3382        let input_type = DataType::Decimal256(20, 3);
3383        let output_type = DataType::Decimal128(20, 4);
3384        assert!(can_cast_types(&input_type, &output_type));
3385        let array = vec![
3386            Some(i256::from_i128(1123456)),
3387            Some(i256::from_i128(2123456)),
3388            Some(i256::from_i128(3123456)),
3389            None,
3390        ];
3391        let array = create_decimal256_array(array, 20, 3).unwrap();
3392        generate_cast_test_case!(
3393            &array,
3394            Decimal128Array,
3395            &output_type,
3396            vec![
3397                Some(11234560_i128),
3398                Some(21234560_i128),
3399                Some(31234560_i128),
3400                None
3401            ]
3402        );
3403    }
3404
3405    #[test]
3406    fn test_cast_decimal256_to_decimal256() {
3407        let input_type = DataType::Decimal256(20, 3);
3408        let output_type = DataType::Decimal256(20, 4);
3409        assert!(can_cast_types(&input_type, &output_type));
3410        let array = vec![
3411            Some(i256::from_i128(1123456)),
3412            Some(i256::from_i128(2123456)),
3413            Some(i256::from_i128(3123456)),
3414            None,
3415        ];
3416        let array = create_decimal256_array(array, 20, 3).unwrap();
3417        generate_cast_test_case!(
3418            &array,
3419            Decimal256Array,
3420            &output_type,
3421            vec![
3422                Some(i256::from_i128(11234560_i128)),
3423                Some(i256::from_i128(21234560_i128)),
3424                Some(i256::from_i128(31234560_i128)),
3425                None
3426            ]
3427        );
3428    }
3429
3430    fn generate_decimal_to_numeric_cast_test_case<T>(array: &PrimitiveArray<T>)
3431    where
3432        T: ArrowPrimitiveType + DecimalType,
3433    {
3434        // u8
3435        generate_cast_test_case!(
3436            array,
3437            UInt8Array,
3438            &DataType::UInt8,
3439            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3440        );
3441        // u16
3442        generate_cast_test_case!(
3443            array,
3444            UInt16Array,
3445            &DataType::UInt16,
3446            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3447        );
3448        // u32
3449        generate_cast_test_case!(
3450            array,
3451            UInt32Array,
3452            &DataType::UInt32,
3453            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3454        );
3455        // u64
3456        generate_cast_test_case!(
3457            array,
3458            UInt64Array,
3459            &DataType::UInt64,
3460            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3461        );
3462        // i8
3463        generate_cast_test_case!(
3464            array,
3465            Int8Array,
3466            &DataType::Int8,
3467            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3468        );
3469        // i16
3470        generate_cast_test_case!(
3471            array,
3472            Int16Array,
3473            &DataType::Int16,
3474            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3475        );
3476        // i32
3477        generate_cast_test_case!(
3478            array,
3479            Int32Array,
3480            &DataType::Int32,
3481            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3482        );
3483        // i64
3484        generate_cast_test_case!(
3485            array,
3486            Int64Array,
3487            &DataType::Int64,
3488            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3489        );
3490        // f32
3491        generate_cast_test_case!(
3492            array,
3493            Float32Array,
3494            &DataType::Float32,
3495            vec![
3496                Some(1.25_f32),
3497                Some(2.25_f32),
3498                Some(3.25_f32),
3499                None,
3500                Some(5.25_f32)
3501            ]
3502        );
3503        // f64
3504        generate_cast_test_case!(
3505            array,
3506            Float64Array,
3507            &DataType::Float64,
3508            vec![
3509                Some(1.25_f64),
3510                Some(2.25_f64),
3511                Some(3.25_f64),
3512                None,
3513                Some(5.25_f64)
3514            ]
3515        );
3516    }
3517
3518    #[test]
3519    fn test_cast_decimal32_to_numeric() {
3520        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3521        let array = create_decimal32_array(value_array, 8, 2).unwrap();
3522
3523        generate_decimal_to_numeric_cast_test_case(&array);
3524    }
3525
3526    #[test]
3527    fn test_cast_decimal64_to_numeric() {
3528        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3529        let array = create_decimal64_array(value_array, 8, 2).unwrap();
3530
3531        generate_decimal_to_numeric_cast_test_case(&array);
3532    }
3533
3534    #[test]
3535    fn test_cast_decimal128_to_numeric() {
3536        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3537        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3538
3539        generate_decimal_to_numeric_cast_test_case(&array);
3540
3541        // overflow test: out of range of max u8
3542        let value_array: Vec<Option<i128>> = vec![Some(51300)];
3543        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3544        let casted_array = cast_with_options(
3545            &array,
3546            &DataType::UInt8,
3547            &CastOptions {
3548                safe: false,
3549                format_options: FormatOptions::default(),
3550            },
3551        );
3552        assert_eq!(
3553            "Cast error: value of 513 is out of range UInt8".to_string(),
3554            casted_array.unwrap_err().to_string()
3555        );
3556
3557        let casted_array = cast_with_options(
3558            &array,
3559            &DataType::UInt8,
3560            &CastOptions {
3561                safe: true,
3562                format_options: FormatOptions::default(),
3563            },
3564        );
3565        assert!(casted_array.is_ok());
3566        assert!(casted_array.unwrap().is_null(0));
3567
3568        // overflow test: out of range of max i8
3569        let value_array: Vec<Option<i128>> = vec![Some(24400)];
3570        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3571        let casted_array = cast_with_options(
3572            &array,
3573            &DataType::Int8,
3574            &CastOptions {
3575                safe: false,
3576                format_options: FormatOptions::default(),
3577            },
3578        );
3579        assert_eq!(
3580            "Cast error: value of 244 is out of range Int8".to_string(),
3581            casted_array.unwrap_err().to_string()
3582        );
3583
3584        let casted_array = cast_with_options(
3585            &array,
3586            &DataType::Int8,
3587            &CastOptions {
3588                safe: true,
3589                format_options: FormatOptions::default(),
3590            },
3591        );
3592        assert!(casted_array.is_ok());
3593        assert!(casted_array.unwrap().is_null(0));
3594
3595        // loss the precision: convert decimal to f32、f64
3596        // f32
3597        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3598        let value_array: Vec<Option<i128>> = vec![
3599            Some(125),
3600            Some(225),
3601            Some(325),
3602            None,
3603            Some(525),
3604            Some(112345678),
3605            Some(112345679),
3606        ];
3607        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3608        generate_cast_test_case!(
3609            &array,
3610            Float32Array,
3611            &DataType::Float32,
3612            vec![
3613                Some(1.25_f32),
3614                Some(2.25_f32),
3615                Some(3.25_f32),
3616                None,
3617                Some(5.25_f32),
3618                Some(1_123_456.7_f32),
3619                Some(1_123_456.7_f32)
3620            ]
3621        );
3622
3623        // f64
3624        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3625        let value_array: Vec<Option<i128>> = vec![
3626            Some(125),
3627            Some(225),
3628            Some(325),
3629            None,
3630            Some(525),
3631            Some(112345678901234568),
3632            Some(112345678901234560),
3633        ];
3634        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3635        generate_cast_test_case!(
3636            &array,
3637            Float64Array,
3638            &DataType::Float64,
3639            vec![
3640                Some(1.25_f64),
3641                Some(2.25_f64),
3642                Some(3.25_f64),
3643                None,
3644                Some(5.25_f64),
3645                Some(1_123_456_789_012_345.6_f64),
3646                Some(1_123_456_789_012_345.6_f64),
3647            ]
3648        );
3649    }
3650
3651    #[test]
3652    fn test_cast_decimal256_to_numeric() {
3653        let value_array: Vec<Option<i256>> = vec![
3654            Some(i256::from_i128(125)),
3655            Some(i256::from_i128(225)),
3656            Some(i256::from_i128(325)),
3657            None,
3658            Some(i256::from_i128(525)),
3659        ];
3660        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3661        // u8
3662        generate_cast_test_case!(
3663            &array,
3664            UInt8Array,
3665            &DataType::UInt8,
3666            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3667        );
3668        // u16
3669        generate_cast_test_case!(
3670            &array,
3671            UInt16Array,
3672            &DataType::UInt16,
3673            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3674        );
3675        // u32
3676        generate_cast_test_case!(
3677            &array,
3678            UInt32Array,
3679            &DataType::UInt32,
3680            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3681        );
3682        // u64
3683        generate_cast_test_case!(
3684            &array,
3685            UInt64Array,
3686            &DataType::UInt64,
3687            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3688        );
3689        // i8
3690        generate_cast_test_case!(
3691            &array,
3692            Int8Array,
3693            &DataType::Int8,
3694            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3695        );
3696        // i16
3697        generate_cast_test_case!(
3698            &array,
3699            Int16Array,
3700            &DataType::Int16,
3701            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3702        );
3703        // i32
3704        generate_cast_test_case!(
3705            &array,
3706            Int32Array,
3707            &DataType::Int32,
3708            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3709        );
3710        // i64
3711        generate_cast_test_case!(
3712            &array,
3713            Int64Array,
3714            &DataType::Int64,
3715            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3716        );
3717        // f32
3718        generate_cast_test_case!(
3719            &array,
3720            Float32Array,
3721            &DataType::Float32,
3722            vec![
3723                Some(1.25_f32),
3724                Some(2.25_f32),
3725                Some(3.25_f32),
3726                None,
3727                Some(5.25_f32)
3728            ]
3729        );
3730        // f64
3731        generate_cast_test_case!(
3732            &array,
3733            Float64Array,
3734            &DataType::Float64,
3735            vec![
3736                Some(1.25_f64),
3737                Some(2.25_f64),
3738                Some(3.25_f64),
3739                None,
3740                Some(5.25_f64)
3741            ]
3742        );
3743
3744        // overflow test: out of range of max i8
3745        let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128(24400))];
3746        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3747        let casted_array = cast_with_options(
3748            &array,
3749            &DataType::Int8,
3750            &CastOptions {
3751                safe: false,
3752                format_options: FormatOptions::default(),
3753            },
3754        );
3755        assert_eq!(
3756            "Cast error: value of 244 is out of range Int8".to_string(),
3757            casted_array.unwrap_err().to_string()
3758        );
3759
3760        let casted_array = cast_with_options(
3761            &array,
3762            &DataType::Int8,
3763            &CastOptions {
3764                safe: true,
3765                format_options: FormatOptions::default(),
3766            },
3767        );
3768        assert!(casted_array.is_ok());
3769        assert!(casted_array.unwrap().is_null(0));
3770
3771        // loss the precision: convert decimal to f32、f64
3772        // f32
3773        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3774        let value_array: Vec<Option<i256>> = vec![
3775            Some(i256::from_i128(125)),
3776            Some(i256::from_i128(225)),
3777            Some(i256::from_i128(325)),
3778            None,
3779            Some(i256::from_i128(525)),
3780            Some(i256::from_i128(112345678)),
3781            Some(i256::from_i128(112345679)),
3782        ];
3783        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3784        generate_cast_test_case!(
3785            &array,
3786            Float32Array,
3787            &DataType::Float32,
3788            vec![
3789                Some(1.25_f32),
3790                Some(2.25_f32),
3791                Some(3.25_f32),
3792                None,
3793                Some(5.25_f32),
3794                Some(1_123_456.7_f32),
3795                Some(1_123_456.7_f32)
3796            ]
3797        );
3798
3799        // f64
3800        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3801        let value_array: Vec<Option<i256>> = vec![
3802            Some(i256::from_i128(125)),
3803            Some(i256::from_i128(225)),
3804            Some(i256::from_i128(325)),
3805            None,
3806            Some(i256::from_i128(525)),
3807            Some(i256::from_i128(112345678901234568)),
3808            Some(i256::from_i128(112345678901234560)),
3809        ];
3810        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3811        generate_cast_test_case!(
3812            &array,
3813            Float64Array,
3814            &DataType::Float64,
3815            vec![
3816                Some(1.25_f64),
3817                Some(2.25_f64),
3818                Some(3.25_f64),
3819                None,
3820                Some(5.25_f64),
3821                Some(1_123_456_789_012_345.6_f64),
3822                Some(1_123_456_789_012_345.6_f64),
3823            ]
3824        );
3825    }
3826
3827    #[test]
3828    fn test_cast_numeric_to_decimal128() {
3829        let decimal_type = DataType::Decimal128(38, 6);
3830        // u8, u16, u32, u64
3831        let input_datas = vec![
3832            Arc::new(UInt8Array::from(vec![
3833                Some(1),
3834                Some(2),
3835                Some(3),
3836                None,
3837                Some(5),
3838            ])) as ArrayRef, // u8
3839            Arc::new(UInt16Array::from(vec![
3840                Some(1),
3841                Some(2),
3842                Some(3),
3843                None,
3844                Some(5),
3845            ])) as ArrayRef, // u16
3846            Arc::new(UInt32Array::from(vec![
3847                Some(1),
3848                Some(2),
3849                Some(3),
3850                None,
3851                Some(5),
3852            ])) as ArrayRef, // u32
3853            Arc::new(UInt64Array::from(vec![
3854                Some(1),
3855                Some(2),
3856                Some(3),
3857                None,
3858                Some(5),
3859            ])) as ArrayRef, // u64
3860        ];
3861
3862        for array in input_datas {
3863            generate_cast_test_case!(
3864                &array,
3865                Decimal128Array,
3866                &decimal_type,
3867                vec![
3868                    Some(1000000_i128),
3869                    Some(2000000_i128),
3870                    Some(3000000_i128),
3871                    None,
3872                    Some(5000000_i128)
3873                ]
3874            );
3875        }
3876
3877        // i8, i16, i32, i64
3878        let input_datas = vec![
3879            Arc::new(Int8Array::from(vec![
3880                Some(1),
3881                Some(2),
3882                Some(3),
3883                None,
3884                Some(5),
3885            ])) as ArrayRef, // i8
3886            Arc::new(Int16Array::from(vec![
3887                Some(1),
3888                Some(2),
3889                Some(3),
3890                None,
3891                Some(5),
3892            ])) as ArrayRef, // i16
3893            Arc::new(Int32Array::from(vec![
3894                Some(1),
3895                Some(2),
3896                Some(3),
3897                None,
3898                Some(5),
3899            ])) as ArrayRef, // i32
3900            Arc::new(Int64Array::from(vec![
3901                Some(1),
3902                Some(2),
3903                Some(3),
3904                None,
3905                Some(5),
3906            ])) as ArrayRef, // i64
3907        ];
3908        for array in input_datas {
3909            generate_cast_test_case!(
3910                &array,
3911                Decimal128Array,
3912                &decimal_type,
3913                vec![
3914                    Some(1000000_i128),
3915                    Some(2000000_i128),
3916                    Some(3000000_i128),
3917                    None,
3918                    Some(5000000_i128)
3919                ]
3920            );
3921        }
3922
3923        // test u8 to decimal type with overflow the result type
3924        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
3925        let array = UInt8Array::from(vec![1, 2, 3, 4, 100]);
3926        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
3927        assert!(casted_array.is_ok());
3928        let array = casted_array.unwrap();
3929        let array: &Decimal128Array = array.as_primitive();
3930        assert!(array.is_null(4));
3931
3932        // test i8 to decimal type with overflow the result type
3933        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
3934        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
3935        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
3936        assert!(casted_array.is_ok());
3937        let array = casted_array.unwrap();
3938        let array: &Decimal128Array = array.as_primitive();
3939        assert!(array.is_null(4));
3940
3941        // test f32 to decimal type
3942        let array = Float32Array::from(vec![
3943            Some(1.1),
3944            Some(2.2),
3945            Some(4.4),
3946            None,
3947            Some(1.123_456_4), // round down
3948            Some(1.123_456_7), // round up
3949        ]);
3950        let array = Arc::new(array) as ArrayRef;
3951        generate_cast_test_case!(
3952            &array,
3953            Decimal128Array,
3954            &decimal_type,
3955            vec![
3956                Some(1100000_i128),
3957                Some(2200000_i128),
3958                Some(4400000_i128),
3959                None,
3960                Some(1123456_i128), // round down
3961                Some(1123457_i128), // round up
3962            ]
3963        );
3964
3965        // test f64 to decimal type
3966        let array = Float64Array::from(vec![
3967            Some(1.1),
3968            Some(2.2),
3969            Some(4.4),
3970            None,
3971            Some(1.123_456_489_123_4),     // round up
3972            Some(1.123_456_789_123_4),     // round up
3973            Some(1.123_456_489_012_345_6), // round down
3974            Some(1.123_456_789_012_345_6), // round up
3975        ]);
3976        generate_cast_test_case!(
3977            &array,
3978            Decimal128Array,
3979            &decimal_type,
3980            vec![
3981                Some(1100000_i128),
3982                Some(2200000_i128),
3983                Some(4400000_i128),
3984                None,
3985                Some(1123456_i128), // round down
3986                Some(1123457_i128), // round up
3987                Some(1123456_i128), // round down
3988                Some(1123457_i128), // round up
3989            ]
3990        );
3991    }
3992
3993    #[test]
3994    fn test_cast_numeric_to_decimal256() {
3995        let decimal_type = DataType::Decimal256(76, 6);
3996        // u8, u16, u32, u64
3997        let input_datas = vec![
3998            Arc::new(UInt8Array::from(vec![
3999                Some(1),
4000                Some(2),
4001                Some(3),
4002                None,
4003                Some(5),
4004            ])) as ArrayRef, // u8
4005            Arc::new(UInt16Array::from(vec![
4006                Some(1),
4007                Some(2),
4008                Some(3),
4009                None,
4010                Some(5),
4011            ])) as ArrayRef, // u16
4012            Arc::new(UInt32Array::from(vec![
4013                Some(1),
4014                Some(2),
4015                Some(3),
4016                None,
4017                Some(5),
4018            ])) as ArrayRef, // u32
4019            Arc::new(UInt64Array::from(vec![
4020                Some(1),
4021                Some(2),
4022                Some(3),
4023                None,
4024                Some(5),
4025            ])) as ArrayRef, // u64
4026        ];
4027
4028        for array in input_datas {
4029            generate_cast_test_case!(
4030                &array,
4031                Decimal256Array,
4032                &decimal_type,
4033                vec![
4034                    Some(i256::from_i128(1000000_i128)),
4035                    Some(i256::from_i128(2000000_i128)),
4036                    Some(i256::from_i128(3000000_i128)),
4037                    None,
4038                    Some(i256::from_i128(5000000_i128))
4039                ]
4040            );
4041        }
4042
4043        // i8, i16, i32, i64
4044        let input_datas = vec![
4045            Arc::new(Int8Array::from(vec![
4046                Some(1),
4047                Some(2),
4048                Some(3),
4049                None,
4050                Some(5),
4051            ])) as ArrayRef, // i8
4052            Arc::new(Int16Array::from(vec![
4053                Some(1),
4054                Some(2),
4055                Some(3),
4056                None,
4057                Some(5),
4058            ])) as ArrayRef, // i16
4059            Arc::new(Int32Array::from(vec![
4060                Some(1),
4061                Some(2),
4062                Some(3),
4063                None,
4064                Some(5),
4065            ])) as ArrayRef, // i32
4066            Arc::new(Int64Array::from(vec![
4067                Some(1),
4068                Some(2),
4069                Some(3),
4070                None,
4071                Some(5),
4072            ])) as ArrayRef, // i64
4073        ];
4074        for array in input_datas {
4075            generate_cast_test_case!(
4076                &array,
4077                Decimal256Array,
4078                &decimal_type,
4079                vec![
4080                    Some(i256::from_i128(1000000_i128)),
4081                    Some(i256::from_i128(2000000_i128)),
4082                    Some(i256::from_i128(3000000_i128)),
4083                    None,
4084                    Some(i256::from_i128(5000000_i128))
4085                ]
4086            );
4087        }
4088
4089        // test i8 to decimal type with overflow the result type
4090        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4091        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4092        let array = Arc::new(array) as ArrayRef;
4093        let casted_array = cast(&array, &DataType::Decimal256(3, 1));
4094        assert!(casted_array.is_ok());
4095        let array = casted_array.unwrap();
4096        let array: &Decimal256Array = array.as_primitive();
4097        assert!(array.is_null(4));
4098
4099        // test f32 to decimal type
4100        let array = Float32Array::from(vec![
4101            Some(1.1),
4102            Some(2.2),
4103            Some(4.4),
4104            None,
4105            Some(1.123_456_4), // round down
4106            Some(1.123_456_7), // round up
4107        ]);
4108        generate_cast_test_case!(
4109            &array,
4110            Decimal256Array,
4111            &decimal_type,
4112            vec![
4113                Some(i256::from_i128(1100000_i128)),
4114                Some(i256::from_i128(2200000_i128)),
4115                Some(i256::from_i128(4400000_i128)),
4116                None,
4117                Some(i256::from_i128(1123456_i128)), // round down
4118                Some(i256::from_i128(1123457_i128)), // round up
4119            ]
4120        );
4121
4122        // test f64 to decimal type
4123        let array = Float64Array::from(vec![
4124            Some(1.1),
4125            Some(2.2),
4126            Some(4.4),
4127            None,
4128            Some(1.123_456_489_123_4),     // round down
4129            Some(1.123_456_789_123_4),     // round up
4130            Some(1.123_456_489_012_345_6), // round down
4131            Some(1.123_456_789_012_345_6), // round up
4132        ]);
4133        generate_cast_test_case!(
4134            &array,
4135            Decimal256Array,
4136            &decimal_type,
4137            vec![
4138                Some(i256::from_i128(1100000_i128)),
4139                Some(i256::from_i128(2200000_i128)),
4140                Some(i256::from_i128(4400000_i128)),
4141                None,
4142                Some(i256::from_i128(1123456_i128)), // round down
4143                Some(i256::from_i128(1123457_i128)), // round up
4144                Some(i256::from_i128(1123456_i128)), // round down
4145                Some(i256::from_i128(1123457_i128)), // round up
4146            ]
4147        );
4148    }
4149
4150    #[test]
4151    fn test_cast_i32_to_f64() {
4152        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4153        let b = cast(&array, &DataType::Float64).unwrap();
4154        let c = b.as_primitive::<Float64Type>();
4155        assert_eq!(5.0, c.value(0));
4156        assert_eq!(6.0, c.value(1));
4157        assert_eq!(7.0, c.value(2));
4158        assert_eq!(8.0, c.value(3));
4159        assert_eq!(9.0, c.value(4));
4160    }
4161
4162    #[test]
4163    fn test_cast_i32_to_u8() {
4164        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4165        let b = cast(&array, &DataType::UInt8).unwrap();
4166        let c = b.as_primitive::<UInt8Type>();
4167        assert!(!c.is_valid(0));
4168        assert_eq!(6, c.value(1));
4169        assert!(!c.is_valid(2));
4170        assert_eq!(8, c.value(3));
4171        // overflows return None
4172        assert!(!c.is_valid(4));
4173    }
4174
4175    #[test]
4176    #[should_panic(expected = "Can't cast value -5 to type UInt8")]
4177    fn test_cast_int32_to_u8_with_error() {
4178        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4179        // overflow with the error
4180        let cast_option = CastOptions {
4181            safe: false,
4182            format_options: FormatOptions::default(),
4183        };
4184        let result = cast_with_options(&array, &DataType::UInt8, &cast_option);
4185        assert!(result.is_err());
4186        result.unwrap();
4187    }
4188
4189    #[test]
4190    fn test_cast_i32_to_u8_sliced() {
4191        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4192        assert_eq!(0, array.offset());
4193        let array = array.slice(2, 3);
4194        let b = cast(&array, &DataType::UInt8).unwrap();
4195        assert_eq!(3, b.len());
4196        let c = b.as_primitive::<UInt8Type>();
4197        assert!(!c.is_valid(0));
4198        assert_eq!(8, c.value(1));
4199        // overflows return None
4200        assert!(!c.is_valid(2));
4201    }
4202
4203    #[test]
4204    fn test_cast_i32_to_i32() {
4205        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4206        let b = cast(&array, &DataType::Int32).unwrap();
4207        let c = b.as_primitive::<Int32Type>();
4208        assert_eq!(5, c.value(0));
4209        assert_eq!(6, c.value(1));
4210        assert_eq!(7, c.value(2));
4211        assert_eq!(8, c.value(3));
4212        assert_eq!(9, c.value(4));
4213    }
4214
4215    #[test]
4216    fn test_cast_i32_to_list_i32() {
4217        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4218        let b = cast(
4219            &array,
4220            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4221        )
4222        .unwrap();
4223        assert_eq!(5, b.len());
4224        let arr = b.as_list::<i32>();
4225        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4226        assert_eq!(1, arr.value_length(0));
4227        assert_eq!(1, arr.value_length(1));
4228        assert_eq!(1, arr.value_length(2));
4229        assert_eq!(1, arr.value_length(3));
4230        assert_eq!(1, arr.value_length(4));
4231        let c = arr.values().as_primitive::<Int32Type>();
4232        assert_eq!(5, c.value(0));
4233        assert_eq!(6, c.value(1));
4234        assert_eq!(7, c.value(2));
4235        assert_eq!(8, c.value(3));
4236        assert_eq!(9, c.value(4));
4237    }
4238
4239    #[test]
4240    fn test_cast_i32_to_list_i32_nullable() {
4241        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), Some(9)]);
4242        let b = cast(
4243            &array,
4244            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4245        )
4246        .unwrap();
4247        assert_eq!(5, b.len());
4248        assert_eq!(0, b.null_count());
4249        let arr = b.as_list::<i32>();
4250        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4251        assert_eq!(1, arr.value_length(0));
4252        assert_eq!(1, arr.value_length(1));
4253        assert_eq!(1, arr.value_length(2));
4254        assert_eq!(1, arr.value_length(3));
4255        assert_eq!(1, arr.value_length(4));
4256
4257        let c = arr.values().as_primitive::<Int32Type>();
4258        assert_eq!(1, c.null_count());
4259        assert_eq!(5, c.value(0));
4260        assert!(!c.is_valid(1));
4261        assert_eq!(7, c.value(2));
4262        assert_eq!(8, c.value(3));
4263        assert_eq!(9, c.value(4));
4264    }
4265
4266    #[test]
4267    fn test_cast_i32_to_list_f64_nullable_sliced() {
4268        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), None, Some(10)]);
4269        let array = array.slice(2, 4);
4270        let b = cast(
4271            &array,
4272            &DataType::List(Arc::new(Field::new_list_field(DataType::Float64, true))),
4273        )
4274        .unwrap();
4275        assert_eq!(4, b.len());
4276        assert_eq!(0, b.null_count());
4277        let arr = b.as_list::<i32>();
4278        assert_eq!(&[0, 1, 2, 3, 4], arr.value_offsets());
4279        assert_eq!(1, arr.value_length(0));
4280        assert_eq!(1, arr.value_length(1));
4281        assert_eq!(1, arr.value_length(2));
4282        assert_eq!(1, arr.value_length(3));
4283        let c = arr.values().as_primitive::<Float64Type>();
4284        assert_eq!(1, c.null_count());
4285        assert_eq!(7.0, c.value(0));
4286        assert_eq!(8.0, c.value(1));
4287        assert!(!c.is_valid(2));
4288        assert_eq!(10.0, c.value(3));
4289    }
4290
4291    #[test]
4292    fn test_cast_int_to_utf8view() {
4293        let inputs = vec![
4294            Arc::new(Int8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4295            Arc::new(Int16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4296            Arc::new(Int32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4297            Arc::new(Int64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4298            Arc::new(UInt8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4299            Arc::new(UInt16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4300            Arc::new(UInt32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4301            Arc::new(UInt64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4302        ];
4303        let expected: ArrayRef = Arc::new(StringViewArray::from(vec![
4304            None,
4305            Some("8"),
4306            Some("9"),
4307            Some("10"),
4308        ]));
4309
4310        for array in inputs {
4311            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4312            let arr = cast(&array, &DataType::Utf8View).unwrap();
4313            assert_eq!(expected.as_ref(), arr.as_ref());
4314        }
4315    }
4316
4317    #[test]
4318    fn test_cast_float_to_utf8view() {
4319        let inputs = vec![
4320            Arc::new(Float16Array::from(vec![
4321                Some(f16::from_f64(1.5)),
4322                Some(f16::from_f64(2.5)),
4323                None,
4324            ])) as ArrayRef,
4325            Arc::new(Float32Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4326            Arc::new(Float64Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4327        ];
4328
4329        let expected: ArrayRef =
4330            Arc::new(StringViewArray::from(vec![Some("1.5"), Some("2.5"), None]));
4331
4332        for array in inputs {
4333            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4334            let arr = cast(&array, &DataType::Utf8View).unwrap();
4335            assert_eq!(expected.as_ref(), arr.as_ref());
4336        }
4337    }
4338
4339    #[test]
4340    fn test_cast_utf8_to_i32() {
4341        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4342        let b = cast(&array, &DataType::Int32).unwrap();
4343        let c = b.as_primitive::<Int32Type>();
4344        assert_eq!(5, c.value(0));
4345        assert_eq!(6, c.value(1));
4346        assert!(!c.is_valid(2));
4347        assert_eq!(8, c.value(3));
4348        assert!(!c.is_valid(4));
4349    }
4350
4351    #[test]
4352    fn test_cast_utf8view_to_i32() {
4353        let array = StringViewArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4354        let b = cast(&array, &DataType::Int32).unwrap();
4355        let c = b.as_primitive::<Int32Type>();
4356        assert_eq!(5, c.value(0));
4357        assert_eq!(6, c.value(1));
4358        assert!(!c.is_valid(2));
4359        assert_eq!(8, c.value(3));
4360        assert!(!c.is_valid(4));
4361    }
4362
4363    #[test]
4364    fn test_cast_utf8view_to_f32() {
4365        let array = StringViewArray::from(vec!["3", "4.56", "seven", "8.9"]);
4366        let b = cast(&array, &DataType::Float32).unwrap();
4367        let c = b.as_primitive::<Float32Type>();
4368        assert_eq!(3.0, c.value(0));
4369        assert_eq!(4.56, c.value(1));
4370        assert!(!c.is_valid(2));
4371        assert_eq!(8.9, c.value(3));
4372    }
4373
4374    #[test]
4375    fn test_cast_utf8view_to_decimal128() {
4376        let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
4377        let arr = Arc::new(array) as ArrayRef;
4378        generate_cast_test_case!(
4379            &arr,
4380            Decimal128Array,
4381            &DataType::Decimal128(4, 2),
4382            vec![None, Some(400_i128), Some(560_i128), Some(789_i128)]
4383        );
4384    }
4385
4386    #[test]
4387    fn test_cast_with_options_utf8_to_i32() {
4388        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4389        let result = cast_with_options(
4390            &array,
4391            &DataType::Int32,
4392            &CastOptions {
4393                safe: false,
4394                format_options: FormatOptions::default(),
4395            },
4396        );
4397        match result {
4398            Ok(_) => panic!("expected error"),
4399            Err(e) => {
4400                assert!(
4401                    e.to_string()
4402                        .contains("Cast error: Cannot cast string 'seven' to value of Int32 type",),
4403                    "Error: {e}"
4404                )
4405            }
4406        }
4407    }
4408
4409    #[test]
4410    fn test_cast_utf8_to_bool() {
4411        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4412        let casted = cast(&strings, &DataType::Boolean).unwrap();
4413        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4414        assert_eq!(*as_boolean_array(&casted), expected);
4415    }
4416
4417    #[test]
4418    fn test_cast_utf8view_to_bool() {
4419        let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4420        let casted = cast(&strings, &DataType::Boolean).unwrap();
4421        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4422        assert_eq!(*as_boolean_array(&casted), expected);
4423    }
4424
4425    #[test]
4426    fn test_cast_with_options_utf8_to_bool() {
4427        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4428        let casted = cast_with_options(
4429            &strings,
4430            &DataType::Boolean,
4431            &CastOptions {
4432                safe: false,
4433                format_options: FormatOptions::default(),
4434            },
4435        );
4436        match casted {
4437            Ok(_) => panic!("expected error"),
4438            Err(e) => {
4439                assert!(
4440                    e.to_string().contains(
4441                        "Cast error: Cannot cast value 'invalid' to value of Boolean type"
4442                    )
4443                )
4444            }
4445        }
4446    }
4447
4448    #[test]
4449    fn test_cast_bool_to_i32() {
4450        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4451        let b = cast(&array, &DataType::Int32).unwrap();
4452        let c = b.as_primitive::<Int32Type>();
4453        assert_eq!(1, c.value(0));
4454        assert_eq!(0, c.value(1));
4455        assert!(!c.is_valid(2));
4456    }
4457
4458    #[test]
4459    fn test_cast_bool_to_utf8view() {
4460        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4461        let b = cast(&array, &DataType::Utf8View).unwrap();
4462        let c = b.as_any().downcast_ref::<StringViewArray>().unwrap();
4463        assert_eq!("true", c.value(0));
4464        assert_eq!("false", c.value(1));
4465        assert!(!c.is_valid(2));
4466    }
4467
4468    #[test]
4469    fn test_cast_bool_to_utf8() {
4470        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4471        let b = cast(&array, &DataType::Utf8).unwrap();
4472        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
4473        assert_eq!("true", c.value(0));
4474        assert_eq!("false", c.value(1));
4475        assert!(!c.is_valid(2));
4476    }
4477
4478    #[test]
4479    fn test_cast_bool_to_large_utf8() {
4480        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4481        let b = cast(&array, &DataType::LargeUtf8).unwrap();
4482        let c = b.as_any().downcast_ref::<LargeStringArray>().unwrap();
4483        assert_eq!("true", c.value(0));
4484        assert_eq!("false", c.value(1));
4485        assert!(!c.is_valid(2));
4486    }
4487
4488    #[test]
4489    fn test_cast_bool_to_f64() {
4490        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4491        let b = cast(&array, &DataType::Float64).unwrap();
4492        let c = b.as_primitive::<Float64Type>();
4493        assert_eq!(1.0, c.value(0));
4494        assert_eq!(0.0, c.value(1));
4495        assert!(!c.is_valid(2));
4496    }
4497
4498    #[test]
4499    fn test_cast_integer_to_timestamp() {
4500        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4501        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4502
4503        let array = Int8Array::from(vec![Some(2), Some(10), None]);
4504        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4505
4506        assert_eq!(&actual, &expected);
4507
4508        let array = Int16Array::from(vec![Some(2), Some(10), None]);
4509        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4510
4511        assert_eq!(&actual, &expected);
4512
4513        let array = Int32Array::from(vec![Some(2), Some(10), None]);
4514        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4515
4516        assert_eq!(&actual, &expected);
4517
4518        let array = UInt8Array::from(vec![Some(2), Some(10), None]);
4519        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4520
4521        assert_eq!(&actual, &expected);
4522
4523        let array = UInt16Array::from(vec![Some(2), Some(10), None]);
4524        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4525
4526        assert_eq!(&actual, &expected);
4527
4528        let array = UInt32Array::from(vec![Some(2), Some(10), None]);
4529        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4530
4531        assert_eq!(&actual, &expected);
4532
4533        let array = UInt64Array::from(vec![Some(2), Some(10), None]);
4534        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4535
4536        assert_eq!(&actual, &expected);
4537    }
4538
4539    #[test]
4540    fn test_cast_timestamp_to_integer() {
4541        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4542            .with_timezone("UTC".to_string());
4543        let expected = cast(&array, &DataType::Int64).unwrap();
4544
4545        let actual = cast(&cast(&array, &DataType::Int8).unwrap(), &DataType::Int64).unwrap();
4546        assert_eq!(&actual, &expected);
4547
4548        let actual = cast(&cast(&array, &DataType::Int16).unwrap(), &DataType::Int64).unwrap();
4549        assert_eq!(&actual, &expected);
4550
4551        let actual = cast(&cast(&array, &DataType::Int32).unwrap(), &DataType::Int64).unwrap();
4552        assert_eq!(&actual, &expected);
4553
4554        let actual = cast(&cast(&array, &DataType::UInt8).unwrap(), &DataType::Int64).unwrap();
4555        assert_eq!(&actual, &expected);
4556
4557        let actual = cast(&cast(&array, &DataType::UInt16).unwrap(), &DataType::Int64).unwrap();
4558        assert_eq!(&actual, &expected);
4559
4560        let actual = cast(&cast(&array, &DataType::UInt32).unwrap(), &DataType::Int64).unwrap();
4561        assert_eq!(&actual, &expected);
4562
4563        let actual = cast(&cast(&array, &DataType::UInt64).unwrap(), &DataType::Int64).unwrap();
4564        assert_eq!(&actual, &expected);
4565    }
4566
4567    #[test]
4568    fn test_cast_floating_to_timestamp() {
4569        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4570        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4571
4572        let array = Float16Array::from(vec![
4573            Some(f16::from_f32(2.0)),
4574            Some(f16::from_f32(10.6)),
4575            None,
4576        ]);
4577        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4578
4579        assert_eq!(&actual, &expected);
4580
4581        let array = Float32Array::from(vec![Some(2.0), Some(10.6), None]);
4582        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4583
4584        assert_eq!(&actual, &expected);
4585
4586        let array = Float64Array::from(vec![Some(2.1), Some(10.2), None]);
4587        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4588
4589        assert_eq!(&actual, &expected);
4590    }
4591
4592    #[test]
4593    fn test_cast_timestamp_to_floating() {
4594        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4595            .with_timezone("UTC".to_string());
4596        let expected = cast(&array, &DataType::Int64).unwrap();
4597
4598        let actual = cast(&cast(&array, &DataType::Float16).unwrap(), &DataType::Int64).unwrap();
4599        assert_eq!(&actual, &expected);
4600
4601        let actual = cast(&cast(&array, &DataType::Float32).unwrap(), &DataType::Int64).unwrap();
4602        assert_eq!(&actual, &expected);
4603
4604        let actual = cast(&cast(&array, &DataType::Float64).unwrap(), &DataType::Int64).unwrap();
4605        assert_eq!(&actual, &expected);
4606    }
4607
4608    #[test]
4609    fn test_cast_decimal_to_timestamp() {
4610        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4611        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4612
4613        let array = Decimal128Array::from(vec![Some(200), Some(1000), None])
4614            .with_precision_and_scale(4, 2)
4615            .unwrap();
4616        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4617
4618        assert_eq!(&actual, &expected);
4619
4620        let array = Decimal256Array::from(vec![
4621            Some(i256::from_i128(2000)),
4622            Some(i256::from_i128(10000)),
4623            None,
4624        ])
4625        .with_precision_and_scale(5, 3)
4626        .unwrap();
4627        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4628
4629        assert_eq!(&actual, &expected);
4630    }
4631
4632    #[test]
4633    fn test_cast_timestamp_to_decimal() {
4634        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4635            .with_timezone("UTC".to_string());
4636        let expected = cast(&array, &DataType::Int64).unwrap();
4637
4638        let actual = cast(
4639            &cast(&array, &DataType::Decimal128(5, 2)).unwrap(),
4640            &DataType::Int64,
4641        )
4642        .unwrap();
4643        assert_eq!(&actual, &expected);
4644
4645        let actual = cast(
4646            &cast(&array, &DataType::Decimal256(10, 5)).unwrap(),
4647            &DataType::Int64,
4648        )
4649        .unwrap();
4650        assert_eq!(&actual, &expected);
4651    }
4652
4653    #[test]
4654    fn test_cast_list_i32_to_list_u16() {
4655        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 100000000]).into_data();
4656
4657        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
4658
4659        // Construct a list array from the above two
4660        // [[0,0,0], [-1, -2, -1], [2, 100000000]]
4661        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
4662        let list_data = ArrayData::builder(list_data_type)
4663            .len(3)
4664            .add_buffer(value_offsets)
4665            .add_child_data(value_data)
4666            .build()
4667            .unwrap();
4668        let list_array = ListArray::from(list_data);
4669
4670        let cast_array = cast(
4671            &list_array,
4672            &DataType::List(Arc::new(Field::new_list_field(DataType::UInt16, true))),
4673        )
4674        .unwrap();
4675
4676        // For the ListArray itself, there are no null values (as there were no nulls when they went in)
4677        //
4678        // 3 negative values should get lost when casting to unsigned,
4679        // 1 value should overflow
4680        assert_eq!(0, cast_array.null_count());
4681
4682        // offsets should be the same
4683        let array = cast_array.as_list::<i32>();
4684        assert_eq!(list_array.value_offsets(), array.value_offsets());
4685
4686        assert_eq!(DataType::UInt16, array.value_type());
4687        assert_eq!(3, array.value_length(0));
4688        assert_eq!(3, array.value_length(1));
4689        assert_eq!(2, array.value_length(2));
4690
4691        // expect 4 nulls: negative numbers and overflow
4692        let u16arr = array.values().as_primitive::<UInt16Type>();
4693        assert_eq!(4, u16arr.null_count());
4694
4695        // expect 4 nulls: negative numbers and overflow
4696        let expected: UInt16Array =
4697            vec![Some(0), Some(0), Some(0), None, None, None, Some(2), None]
4698                .into_iter()
4699                .collect();
4700
4701        assert_eq!(u16arr, &expected);
4702    }
4703
4704    #[test]
4705    fn test_cast_list_i32_to_list_timestamp() {
4706        // Construct a value array
4707        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 8, 100000000]).into_data();
4708
4709        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 9]);
4710
4711        // Construct a list array from the above two
4712        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
4713        let list_data = ArrayData::builder(list_data_type)
4714            .len(3)
4715            .add_buffer(value_offsets)
4716            .add_child_data(value_data)
4717            .build()
4718            .unwrap();
4719        let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
4720
4721        let actual = cast(
4722            &list_array,
4723            &DataType::List(Arc::new(Field::new_list_field(
4724                DataType::Timestamp(TimeUnit::Microsecond, None),
4725                true,
4726            ))),
4727        )
4728        .unwrap();
4729
4730        let expected = cast(
4731            &cast(
4732                &list_array,
4733                &DataType::List(Arc::new(Field::new_list_field(DataType::Int64, true))),
4734            )
4735            .unwrap(),
4736            &DataType::List(Arc::new(Field::new_list_field(
4737                DataType::Timestamp(TimeUnit::Microsecond, None),
4738                true,
4739            ))),
4740        )
4741        .unwrap();
4742
4743        assert_eq!(&actual, &expected);
4744    }
4745
4746    #[test]
4747    fn test_cast_date32_to_date64() {
4748        let a = Date32Array::from(vec![10000, 17890]);
4749        let array = Arc::new(a) as ArrayRef;
4750        let b = cast(&array, &DataType::Date64).unwrap();
4751        let c = b.as_primitive::<Date64Type>();
4752        assert_eq!(864000000000, c.value(0));
4753        assert_eq!(1545696000000, c.value(1));
4754    }
4755
4756    #[test]
4757    fn test_cast_date64_to_date32() {
4758        let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
4759        let array = Arc::new(a) as ArrayRef;
4760        let b = cast(&array, &DataType::Date32).unwrap();
4761        let c = b.as_primitive::<Date32Type>();
4762        assert_eq!(10000, c.value(0));
4763        assert_eq!(17890, c.value(1));
4764        assert!(c.is_null(2));
4765    }
4766
4767    #[test]
4768    fn test_cast_string_to_integral_overflow() {
4769        let str = Arc::new(StringArray::from(vec![
4770            Some("123"),
4771            Some("-123"),
4772            Some("86374"),
4773            None,
4774        ])) as ArrayRef;
4775
4776        let options = CastOptions {
4777            safe: true,
4778            format_options: FormatOptions::default(),
4779        };
4780        let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
4781        let expected =
4782            Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
4783        assert_eq!(&res, &expected);
4784    }
4785
4786    #[test]
4787    fn test_cast_string_to_timestamp() {
4788        let a0 = Arc::new(StringViewArray::from(vec![
4789            Some("2020-09-08T12:00:00.123456789+00:00"),
4790            Some("Not a valid date"),
4791            None,
4792        ])) as ArrayRef;
4793        let a1 = Arc::new(StringArray::from(vec![
4794            Some("2020-09-08T12:00:00.123456789+00:00"),
4795            Some("Not a valid date"),
4796            None,
4797        ])) as ArrayRef;
4798        let a2 = Arc::new(LargeStringArray::from(vec![
4799            Some("2020-09-08T12:00:00.123456789+00:00"),
4800            Some("Not a valid date"),
4801            None,
4802        ])) as ArrayRef;
4803        for array in &[a0, a1, a2] {
4804            for time_unit in &[
4805                TimeUnit::Second,
4806                TimeUnit::Millisecond,
4807                TimeUnit::Microsecond,
4808                TimeUnit::Nanosecond,
4809            ] {
4810                let to_type = DataType::Timestamp(*time_unit, None);
4811                let b = cast(array, &to_type).unwrap();
4812
4813                match time_unit {
4814                    TimeUnit::Second => {
4815                        let c = b.as_primitive::<TimestampSecondType>();
4816                        assert_eq!(1599566400, c.value(0));
4817                        assert!(c.is_null(1));
4818                        assert!(c.is_null(2));
4819                    }
4820                    TimeUnit::Millisecond => {
4821                        let c = b
4822                            .as_any()
4823                            .downcast_ref::<TimestampMillisecondArray>()
4824                            .unwrap();
4825                        assert_eq!(1599566400123, c.value(0));
4826                        assert!(c.is_null(1));
4827                        assert!(c.is_null(2));
4828                    }
4829                    TimeUnit::Microsecond => {
4830                        let c = b
4831                            .as_any()
4832                            .downcast_ref::<TimestampMicrosecondArray>()
4833                            .unwrap();
4834                        assert_eq!(1599566400123456, c.value(0));
4835                        assert!(c.is_null(1));
4836                        assert!(c.is_null(2));
4837                    }
4838                    TimeUnit::Nanosecond => {
4839                        let c = b
4840                            .as_any()
4841                            .downcast_ref::<TimestampNanosecondArray>()
4842                            .unwrap();
4843                        assert_eq!(1599566400123456789, c.value(0));
4844                        assert!(c.is_null(1));
4845                        assert!(c.is_null(2));
4846                    }
4847                }
4848
4849                let options = CastOptions {
4850                    safe: false,
4851                    format_options: FormatOptions::default(),
4852                };
4853                let err = cast_with_options(array, &to_type, &options).unwrap_err();
4854                assert_eq!(
4855                    err.to_string(),
4856                    "Parser error: Error parsing timestamp from 'Not a valid date': error parsing date"
4857                );
4858            }
4859        }
4860    }
4861
4862    #[test]
4863    fn test_cast_string_to_timestamp_overflow() {
4864        let array = StringArray::from(vec!["9800-09-08T12:00:00.123456789"]);
4865        let result = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
4866        let result = result.as_primitive::<TimestampSecondType>();
4867        assert_eq!(result.values(), &[247112596800]);
4868    }
4869
4870    #[test]
4871    fn test_cast_string_to_date32() {
4872        let a0 = Arc::new(StringViewArray::from(vec![
4873            Some("2018-12-25"),
4874            Some("Not a valid date"),
4875            None,
4876        ])) as ArrayRef;
4877        let a1 = Arc::new(StringArray::from(vec![
4878            Some("2018-12-25"),
4879            Some("Not a valid date"),
4880            None,
4881        ])) as ArrayRef;
4882        let a2 = Arc::new(LargeStringArray::from(vec![
4883            Some("2018-12-25"),
4884            Some("Not a valid date"),
4885            None,
4886        ])) as ArrayRef;
4887        for array in &[a0, a1, a2] {
4888            let to_type = DataType::Date32;
4889            let b = cast(array, &to_type).unwrap();
4890            let c = b.as_primitive::<Date32Type>();
4891            assert_eq!(17890, c.value(0));
4892            assert!(c.is_null(1));
4893            assert!(c.is_null(2));
4894
4895            let options = CastOptions {
4896                safe: false,
4897                format_options: FormatOptions::default(),
4898            };
4899            let err = cast_with_options(array, &to_type, &options).unwrap_err();
4900            assert_eq!(
4901                err.to_string(),
4902                "Cast error: Cannot cast string 'Not a valid date' to value of Date32 type"
4903            );
4904        }
4905    }
4906
4907    #[test]
4908    fn test_cast_string_with_large_date_to_date32() {
4909        let array = Arc::new(StringArray::from(vec![
4910            Some("+10999-12-31"),
4911            Some("-0010-02-28"),
4912            Some("0010-02-28"),
4913            Some("0000-01-01"),
4914            Some("-0000-01-01"),
4915            Some("-0001-01-01"),
4916        ])) as ArrayRef;
4917        let to_type = DataType::Date32;
4918        let options = CastOptions {
4919            safe: false,
4920            format_options: FormatOptions::default(),
4921        };
4922        let b = cast_with_options(&array, &to_type, &options).unwrap();
4923        let c = b.as_primitive::<Date32Type>();
4924        assert_eq!(3298139, c.value(0)); // 10999-12-31
4925        assert_eq!(-723122, c.value(1)); // -0010-02-28
4926        assert_eq!(-715817, c.value(2)); // 0010-02-28
4927        assert_eq!(c.value(3), c.value(4)); // Expect 0000-01-01 and -0000-01-01 to be parsed the same
4928        assert_eq!(-719528, c.value(3)); // 0000-01-01
4929        assert_eq!(-719528, c.value(4)); // -0000-01-01
4930        assert_eq!(-719893, c.value(5)); // -0001-01-01
4931    }
4932
4933    #[test]
4934    fn test_cast_invalid_string_with_large_date_to_date32() {
4935        // Large dates need to be prefixed with a + or - sign, otherwise they are not parsed correctly
4936        let array = Arc::new(StringArray::from(vec![Some("10999-12-31")])) as ArrayRef;
4937        let to_type = DataType::Date32;
4938        let options = CastOptions {
4939            safe: false,
4940            format_options: FormatOptions::default(),
4941        };
4942        let err = cast_with_options(&array, &to_type, &options).unwrap_err();
4943        assert_eq!(
4944            err.to_string(),
4945            "Cast error: Cannot cast string '10999-12-31' to value of Date32 type"
4946        );
4947    }
4948
4949    #[test]
4950    fn test_cast_string_format_yyyymmdd_to_date32() {
4951        let a0 = Arc::new(StringViewArray::from(vec![
4952            Some("2020-12-25"),
4953            Some("20201117"),
4954        ])) as ArrayRef;
4955        let a1 = Arc::new(StringArray::from(vec![
4956            Some("2020-12-25"),
4957            Some("20201117"),
4958        ])) as ArrayRef;
4959        let a2 = Arc::new(LargeStringArray::from(vec![
4960            Some("2020-12-25"),
4961            Some("20201117"),
4962        ])) as ArrayRef;
4963
4964        for array in &[a0, a1, a2] {
4965            let to_type = DataType::Date32;
4966            let options = CastOptions {
4967                safe: false,
4968                format_options: FormatOptions::default(),
4969            };
4970            let result = cast_with_options(&array, &to_type, &options).unwrap();
4971            let c = result.as_primitive::<Date32Type>();
4972            assert_eq!(
4973                chrono::NaiveDate::from_ymd_opt(2020, 12, 25),
4974                c.value_as_date(0)
4975            );
4976            assert_eq!(
4977                chrono::NaiveDate::from_ymd_opt(2020, 11, 17),
4978                c.value_as_date(1)
4979            );
4980        }
4981    }
4982
4983    #[test]
4984    fn test_cast_string_to_time32second() {
4985        let a0 = Arc::new(StringViewArray::from(vec![
4986            Some("08:08:35.091323414"),
4987            Some("08:08:60.091323414"), // leap second
4988            Some("08:08:61.091323414"), // not valid
4989            Some("Not a valid time"),
4990            None,
4991        ])) as ArrayRef;
4992        let a1 = Arc::new(StringArray::from(vec![
4993            Some("08:08:35.091323414"),
4994            Some("08:08:60.091323414"), // leap second
4995            Some("08:08:61.091323414"), // not valid
4996            Some("Not a valid time"),
4997            None,
4998        ])) as ArrayRef;
4999        let a2 = Arc::new(LargeStringArray::from(vec![
5000            Some("08:08:35.091323414"),
5001            Some("08:08:60.091323414"), // leap second
5002            Some("08:08:61.091323414"), // not valid
5003            Some("Not a valid time"),
5004            None,
5005        ])) as ArrayRef;
5006        for array in &[a0, a1, a2] {
5007            let to_type = DataType::Time32(TimeUnit::Second);
5008            let b = cast(array, &to_type).unwrap();
5009            let c = b.as_primitive::<Time32SecondType>();
5010            assert_eq!(29315, c.value(0));
5011            assert_eq!(29340, c.value(1));
5012            assert!(c.is_null(2));
5013            assert!(c.is_null(3));
5014            assert!(c.is_null(4));
5015
5016            let options = CastOptions {
5017                safe: false,
5018                format_options: FormatOptions::default(),
5019            };
5020            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5021            assert_eq!(
5022                err.to_string(),
5023                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(s) type"
5024            );
5025        }
5026    }
5027
5028    #[test]
5029    fn test_cast_string_to_time32millisecond() {
5030        let a0 = Arc::new(StringViewArray::from(vec![
5031            Some("08:08:35.091323414"),
5032            Some("08:08:60.091323414"), // leap second
5033            Some("08:08:61.091323414"), // not valid
5034            Some("Not a valid time"),
5035            None,
5036        ])) as ArrayRef;
5037        let a1 = Arc::new(StringArray::from(vec![
5038            Some("08:08:35.091323414"),
5039            Some("08:08:60.091323414"), // leap second
5040            Some("08:08:61.091323414"), // not valid
5041            Some("Not a valid time"),
5042            None,
5043        ])) as ArrayRef;
5044        let a2 = Arc::new(LargeStringArray::from(vec![
5045            Some("08:08:35.091323414"),
5046            Some("08:08:60.091323414"), // leap second
5047            Some("08:08:61.091323414"), // not valid
5048            Some("Not a valid time"),
5049            None,
5050        ])) as ArrayRef;
5051        for array in &[a0, a1, a2] {
5052            let to_type = DataType::Time32(TimeUnit::Millisecond);
5053            let b = cast(array, &to_type).unwrap();
5054            let c = b.as_primitive::<Time32MillisecondType>();
5055            assert_eq!(29315091, c.value(0));
5056            assert_eq!(29340091, c.value(1));
5057            assert!(c.is_null(2));
5058            assert!(c.is_null(3));
5059            assert!(c.is_null(4));
5060
5061            let options = CastOptions {
5062                safe: false,
5063                format_options: FormatOptions::default(),
5064            };
5065            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5066            assert_eq!(
5067                err.to_string(),
5068                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(ms) type"
5069            );
5070        }
5071    }
5072
5073    #[test]
5074    fn test_cast_string_to_time64microsecond() {
5075        let a0 = Arc::new(StringViewArray::from(vec![
5076            Some("08:08:35.091323414"),
5077            Some("Not a valid time"),
5078            None,
5079        ])) as ArrayRef;
5080        let a1 = Arc::new(StringArray::from(vec![
5081            Some("08:08:35.091323414"),
5082            Some("Not a valid time"),
5083            None,
5084        ])) as ArrayRef;
5085        let a2 = Arc::new(LargeStringArray::from(vec![
5086            Some("08:08:35.091323414"),
5087            Some("Not a valid time"),
5088            None,
5089        ])) as ArrayRef;
5090        for array in &[a0, a1, a2] {
5091            let to_type = DataType::Time64(TimeUnit::Microsecond);
5092            let b = cast(array, &to_type).unwrap();
5093            let c = b.as_primitive::<Time64MicrosecondType>();
5094            assert_eq!(29315091323, c.value(0));
5095            assert!(c.is_null(1));
5096            assert!(c.is_null(2));
5097
5098            let options = CastOptions {
5099                safe: false,
5100                format_options: FormatOptions::default(),
5101            };
5102            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5103            assert_eq!(
5104                err.to_string(),
5105                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(µs) type"
5106            );
5107        }
5108    }
5109
5110    #[test]
5111    fn test_cast_string_to_time64nanosecond() {
5112        let a0 = Arc::new(StringViewArray::from(vec![
5113            Some("08:08:35.091323414"),
5114            Some("Not a valid time"),
5115            None,
5116        ])) as ArrayRef;
5117        let a1 = Arc::new(StringArray::from(vec![
5118            Some("08:08:35.091323414"),
5119            Some("Not a valid time"),
5120            None,
5121        ])) as ArrayRef;
5122        let a2 = Arc::new(LargeStringArray::from(vec![
5123            Some("08:08:35.091323414"),
5124            Some("Not a valid time"),
5125            None,
5126        ])) as ArrayRef;
5127        for array in &[a0, a1, a2] {
5128            let to_type = DataType::Time64(TimeUnit::Nanosecond);
5129            let b = cast(array, &to_type).unwrap();
5130            let c = b.as_primitive::<Time64NanosecondType>();
5131            assert_eq!(29315091323414, c.value(0));
5132            assert!(c.is_null(1));
5133            assert!(c.is_null(2));
5134
5135            let options = CastOptions {
5136                safe: false,
5137                format_options: FormatOptions::default(),
5138            };
5139            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5140            assert_eq!(
5141                err.to_string(),
5142                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(ns) type"
5143            );
5144        }
5145    }
5146
5147    #[test]
5148    fn test_cast_string_to_date64() {
5149        let a0 = Arc::new(StringViewArray::from(vec![
5150            Some("2020-09-08T12:00:00"),
5151            Some("Not a valid date"),
5152            None,
5153        ])) as ArrayRef;
5154        let a1 = Arc::new(StringArray::from(vec![
5155            Some("2020-09-08T12:00:00"),
5156            Some("Not a valid date"),
5157            None,
5158        ])) as ArrayRef;
5159        let a2 = Arc::new(LargeStringArray::from(vec![
5160            Some("2020-09-08T12:00:00"),
5161            Some("Not a valid date"),
5162            None,
5163        ])) as ArrayRef;
5164        for array in &[a0, a1, a2] {
5165            let to_type = DataType::Date64;
5166            let b = cast(array, &to_type).unwrap();
5167            let c = b.as_primitive::<Date64Type>();
5168            assert_eq!(1599566400000, c.value(0));
5169            assert!(c.is_null(1));
5170            assert!(c.is_null(2));
5171
5172            let options = CastOptions {
5173                safe: false,
5174                format_options: FormatOptions::default(),
5175            };
5176            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5177            assert_eq!(
5178                err.to_string(),
5179                "Cast error: Cannot cast string 'Not a valid date' to value of Date64 type"
5180            );
5181        }
5182    }
5183
5184    macro_rules! test_safe_string_to_interval {
5185        ($data_vec:expr, $interval_unit:expr, $array_ty:ty, $expect_vec:expr) => {
5186            let source_string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5187
5188            let options = CastOptions {
5189                safe: true,
5190                format_options: FormatOptions::default(),
5191            };
5192
5193            let target_interval_array = cast_with_options(
5194                &source_string_array.clone(),
5195                &DataType::Interval($interval_unit),
5196                &options,
5197            )
5198            .unwrap()
5199            .as_any()
5200            .downcast_ref::<$array_ty>()
5201            .unwrap()
5202            .clone() as $array_ty;
5203
5204            let target_string_array =
5205                cast_with_options(&target_interval_array, &DataType::Utf8, &options)
5206                    .unwrap()
5207                    .as_any()
5208                    .downcast_ref::<StringArray>()
5209                    .unwrap()
5210                    .clone();
5211
5212            let expect_string_array = StringArray::from($expect_vec);
5213
5214            assert_eq!(target_string_array, expect_string_array);
5215
5216            let target_large_string_array =
5217                cast_with_options(&target_interval_array, &DataType::LargeUtf8, &options)
5218                    .unwrap()
5219                    .as_any()
5220                    .downcast_ref::<LargeStringArray>()
5221                    .unwrap()
5222                    .clone();
5223
5224            let expect_large_string_array = LargeStringArray::from($expect_vec);
5225
5226            assert_eq!(target_large_string_array, expect_large_string_array);
5227        };
5228    }
5229
5230    #[test]
5231    fn test_cast_string_to_interval_year_month() {
5232        test_safe_string_to_interval!(
5233            vec![
5234                Some("1 year 1 month"),
5235                Some("1.5 years 13 month"),
5236                Some("30 days"),
5237                Some("31 days"),
5238                Some("2 months 31 days"),
5239                Some("2 months 31 days 1 second"),
5240                Some("foobar"),
5241            ],
5242            IntervalUnit::YearMonth,
5243            IntervalYearMonthArray,
5244            vec![
5245                Some("1 years 1 mons"),
5246                Some("2 years 7 mons"),
5247                None,
5248                None,
5249                None,
5250                None,
5251                None,
5252            ]
5253        );
5254    }
5255
5256    #[test]
5257    fn test_cast_string_to_interval_day_time() {
5258        test_safe_string_to_interval!(
5259            vec![
5260                Some("1 year 1 month"),
5261                Some("1.5 years 13 month"),
5262                Some("30 days"),
5263                Some("1 day 2 second 3.5 milliseconds"),
5264                Some("foobar"),
5265            ],
5266            IntervalUnit::DayTime,
5267            IntervalDayTimeArray,
5268            vec![
5269                Some("390 days"),
5270                Some("930 days"),
5271                Some("30 days"),
5272                None,
5273                None,
5274            ]
5275        );
5276    }
5277
5278    #[test]
5279    fn test_cast_string_to_interval_month_day_nano() {
5280        test_safe_string_to_interval!(
5281            vec![
5282                Some("1 year 1 month 1 day"),
5283                None,
5284                Some("1.5 years 13 month 35 days 1.4 milliseconds"),
5285                Some("3 days"),
5286                Some("8 seconds"),
5287                None,
5288                Some("1 day 29800 milliseconds"),
5289                Some("3 months 1 second"),
5290                Some("6 minutes 120 second"),
5291                Some("2 years 39 months 9 days 19 hours 1 minute 83 seconds 399222 milliseconds"),
5292                Some("foobar"),
5293            ],
5294            IntervalUnit::MonthDayNano,
5295            IntervalMonthDayNanoArray,
5296            vec![
5297                Some("13 mons 1 days"),
5298                None,
5299                Some("31 mons 35 days 0.001400000 secs"),
5300                Some("3 days"),
5301                Some("8.000000000 secs"),
5302                None,
5303                Some("1 days 29.800000000 secs"),
5304                Some("3 mons 1.000000000 secs"),
5305                Some("8 mins"),
5306                Some("63 mons 9 days 19 hours 9 mins 2.222000000 secs"),
5307                None,
5308            ]
5309        );
5310    }
5311
5312    macro_rules! test_unsafe_string_to_interval_err {
5313        ($data_vec:expr, $interval_unit:expr, $error_msg:expr) => {
5314            let string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5315            let options = CastOptions {
5316                safe: false,
5317                format_options: FormatOptions::default(),
5318            };
5319            let arrow_err = cast_with_options(
5320                &string_array.clone(),
5321                &DataType::Interval($interval_unit),
5322                &options,
5323            )
5324            .unwrap_err();
5325            assert_eq!($error_msg, arrow_err.to_string());
5326        };
5327    }
5328
5329    #[test]
5330    fn test_cast_string_to_interval_err() {
5331        test_unsafe_string_to_interval_err!(
5332            vec![Some("foobar")],
5333            IntervalUnit::YearMonth,
5334            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5335        );
5336        test_unsafe_string_to_interval_err!(
5337            vec![Some("foobar")],
5338            IntervalUnit::DayTime,
5339            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5340        );
5341        test_unsafe_string_to_interval_err!(
5342            vec![Some("foobar")],
5343            IntervalUnit::MonthDayNano,
5344            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5345        );
5346        test_unsafe_string_to_interval_err!(
5347            vec![Some("2 months 31 days 1 second")],
5348            IntervalUnit::YearMonth,
5349            r#"Cast error: Cannot cast 2 months 31 days 1 second to IntervalYearMonth. Only year and month fields are allowed."#
5350        );
5351        test_unsafe_string_to_interval_err!(
5352            vec![Some("1 day 1.5 milliseconds")],
5353            IntervalUnit::DayTime,
5354            r#"Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"#
5355        );
5356
5357        // overflow
5358        test_unsafe_string_to_interval_err!(
5359            vec![Some(format!(
5360                "{} century {} year {} month",
5361                i64::MAX - 2,
5362                i64::MAX - 2,
5363                i64::MAX - 2
5364            ))],
5365            IntervalUnit::DayTime,
5366            format!(
5367                "Arithmetic overflow: Overflow happened on: {} * 100",
5368                i64::MAX - 2
5369            )
5370        );
5371        test_unsafe_string_to_interval_err!(
5372            vec![Some(format!(
5373                "{} year {} month {} day",
5374                i64::MAX - 2,
5375                i64::MAX - 2,
5376                i64::MAX - 2
5377            ))],
5378            IntervalUnit::MonthDayNano,
5379            format!(
5380                "Arithmetic overflow: Overflow happened on: {} * 12",
5381                i64::MAX - 2
5382            )
5383        );
5384    }
5385
5386    #[test]
5387    fn test_cast_binary_to_fixed_size_binary() {
5388        let bytes_1 = "Hiiii".as_bytes();
5389        let bytes_2 = "Hello".as_bytes();
5390
5391        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5392        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5393        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5394
5395        let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
5396        let down_cast = array_ref
5397            .as_any()
5398            .downcast_ref::<FixedSizeBinaryArray>()
5399            .unwrap();
5400        assert_eq!(bytes_1, down_cast.value(0));
5401        assert_eq!(bytes_2, down_cast.value(1));
5402        assert!(down_cast.is_null(2));
5403
5404        let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
5405        let down_cast = array_ref
5406            .as_any()
5407            .downcast_ref::<FixedSizeBinaryArray>()
5408            .unwrap();
5409        assert_eq!(bytes_1, down_cast.value(0));
5410        assert_eq!(bytes_2, down_cast.value(1));
5411        assert!(down_cast.is_null(2));
5412
5413        // test error cases when the length of binary are not same
5414        let bytes_1 = "Hi".as_bytes();
5415        let bytes_2 = "Hello".as_bytes();
5416
5417        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5418        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5419        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5420
5421        let array_ref = cast_with_options(
5422            &a1,
5423            &DataType::FixedSizeBinary(5),
5424            &CastOptions {
5425                safe: false,
5426                format_options: FormatOptions::default(),
5427            },
5428        );
5429        assert!(array_ref.is_err());
5430
5431        let array_ref = cast_with_options(
5432            &a2,
5433            &DataType::FixedSizeBinary(5),
5434            &CastOptions {
5435                safe: false,
5436                format_options: FormatOptions::default(),
5437            },
5438        );
5439        assert!(array_ref.is_err());
5440    }
5441
5442    #[test]
5443    fn test_fixed_size_binary_to_binary() {
5444        let bytes_1 = "Hiiii".as_bytes();
5445        let bytes_2 = "Hello".as_bytes();
5446
5447        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5448        let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
5449
5450        let array_ref = cast(&a1, &DataType::Binary).unwrap();
5451        let down_cast = array_ref.as_binary::<i32>();
5452        assert_eq!(bytes_1, down_cast.value(0));
5453        assert_eq!(bytes_2, down_cast.value(1));
5454        assert!(down_cast.is_null(2));
5455
5456        let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
5457        let down_cast = array_ref.as_binary::<i64>();
5458        assert_eq!(bytes_1, down_cast.value(0));
5459        assert_eq!(bytes_2, down_cast.value(1));
5460        assert!(down_cast.is_null(2));
5461
5462        let array_ref = cast(&a1, &DataType::BinaryView).unwrap();
5463        let down_cast = array_ref.as_binary_view();
5464        assert_eq!(bytes_1, down_cast.value(0));
5465        assert_eq!(bytes_2, down_cast.value(1));
5466        assert!(down_cast.is_null(2));
5467    }
5468
5469    #[test]
5470    fn test_fixed_size_binary_to_dictionary() {
5471        let bytes_1 = "Hiiii".as_bytes();
5472        let bytes_2 = "Hello".as_bytes();
5473
5474        let binary_data = vec![Some(bytes_1), Some(bytes_2), Some(bytes_1), None];
5475        let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
5476
5477        let cast_type = DataType::Dictionary(
5478            Box::new(DataType::Int8),
5479            Box::new(DataType::FixedSizeBinary(5)),
5480        );
5481        let cast_array = cast(&a1, &cast_type).unwrap();
5482        assert_eq!(cast_array.data_type(), &cast_type);
5483        assert_eq!(
5484            array_to_strings(&cast_array),
5485            vec!["4869696969", "48656c6c6f", "4869696969", "null"]
5486        );
5487        // dictionary should only have two distinct values
5488        let dict_array = cast_array
5489            .as_any()
5490            .downcast_ref::<DictionaryArray<Int8Type>>()
5491            .unwrap();
5492        assert_eq!(dict_array.values().len(), 2);
5493    }
5494
5495    #[test]
5496    fn test_binary_to_dictionary() {
5497        let mut builder = GenericBinaryBuilder::<i32>::new();
5498        builder.append_value(b"hello");
5499        builder.append_value(b"hiiii");
5500        builder.append_value(b"hiiii"); // duplicate
5501        builder.append_null();
5502        builder.append_value(b"rustt");
5503
5504        let a1 = builder.finish();
5505
5506        let cast_type = DataType::Dictionary(
5507            Box::new(DataType::Int8),
5508            Box::new(DataType::FixedSizeBinary(5)),
5509        );
5510        let cast_array = cast(&a1, &cast_type).unwrap();
5511        assert_eq!(cast_array.data_type(), &cast_type);
5512        assert_eq!(
5513            array_to_strings(&cast_array),
5514            vec![
5515                "68656c6c6f",
5516                "6869696969",
5517                "6869696969",
5518                "null",
5519                "7275737474"
5520            ]
5521        );
5522        // dictionary should only have three distinct values
5523        let dict_array = cast_array
5524            .as_any()
5525            .downcast_ref::<DictionaryArray<Int8Type>>()
5526            .unwrap();
5527        assert_eq!(dict_array.values().len(), 3);
5528    }
5529
5530    #[test]
5531    fn test_numeric_to_binary() {
5532        let a = Int16Array::from(vec![Some(1), Some(511), None]);
5533
5534        let array_ref = cast(&a, &DataType::Binary).unwrap();
5535        let down_cast = array_ref.as_binary::<i32>();
5536        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
5537        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
5538        assert!(down_cast.is_null(2));
5539
5540        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
5541
5542        let array_ref = cast(&a, &DataType::Binary).unwrap();
5543        let down_cast = array_ref.as_binary::<i32>();
5544        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
5545        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
5546        assert!(down_cast.is_null(2));
5547    }
5548
5549    #[test]
5550    fn test_numeric_to_large_binary() {
5551        let a = Int16Array::from(vec![Some(1), Some(511), None]);
5552
5553        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
5554        let down_cast = array_ref.as_binary::<i64>();
5555        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
5556        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
5557        assert!(down_cast.is_null(2));
5558
5559        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
5560
5561        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
5562        let down_cast = array_ref.as_binary::<i64>();
5563        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
5564        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
5565        assert!(down_cast.is_null(2));
5566    }
5567
5568    #[test]
5569    fn test_cast_date32_to_int32() {
5570        let array = Date32Array::from(vec![10000, 17890]);
5571        let b = cast(&array, &DataType::Int32).unwrap();
5572        let c = b.as_primitive::<Int32Type>();
5573        assert_eq!(10000, c.value(0));
5574        assert_eq!(17890, c.value(1));
5575    }
5576
5577    #[test]
5578    fn test_cast_int32_to_date32() {
5579        let array = Int32Array::from(vec![10000, 17890]);
5580        let b = cast(&array, &DataType::Date32).unwrap();
5581        let c = b.as_primitive::<Date32Type>();
5582        assert_eq!(10000, c.value(0));
5583        assert_eq!(17890, c.value(1));
5584    }
5585
5586    #[test]
5587    fn test_cast_timestamp_to_date32() {
5588        let array =
5589            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
5590                .with_timezone("+00:00".to_string());
5591        let b = cast(&array, &DataType::Date32).unwrap();
5592        let c = b.as_primitive::<Date32Type>();
5593        assert_eq!(10000, c.value(0));
5594        assert_eq!(17890, c.value(1));
5595        assert!(c.is_null(2));
5596    }
5597    #[test]
5598    fn test_cast_timestamp_to_date32_zone() {
5599        let strings = StringArray::from_iter([
5600            Some("1970-01-01T00:00:01"),
5601            Some("1970-01-01T23:59:59"),
5602            None,
5603            Some("2020-03-01T02:00:23+00:00"),
5604        ]);
5605        let dt = DataType::Timestamp(TimeUnit::Millisecond, Some("-07:00".into()));
5606        let timestamps = cast(&strings, &dt).unwrap();
5607        let dates = cast(timestamps.as_ref(), &DataType::Date32).unwrap();
5608
5609        let c = dates.as_primitive::<Date32Type>();
5610        let expected = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
5611        assert_eq!(c.value_as_date(0).unwrap(), expected);
5612        assert_eq!(c.value_as_date(1).unwrap(), expected);
5613        assert!(c.is_null(2));
5614        let expected = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
5615        assert_eq!(c.value_as_date(3).unwrap(), expected);
5616    }
5617    #[test]
5618    fn test_cast_timestamp_to_date64() {
5619        let array =
5620            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None]);
5621        let b = cast(&array, &DataType::Date64).unwrap();
5622        let c = b.as_primitive::<Date64Type>();
5623        assert_eq!(864000000005, c.value(0));
5624        assert_eq!(1545696000001, c.value(1));
5625        assert!(c.is_null(2));
5626
5627        let array = TimestampSecondArray::from(vec![Some(864000000005), Some(1545696000001)]);
5628        let b = cast(&array, &DataType::Date64).unwrap();
5629        let c = b.as_primitive::<Date64Type>();
5630        assert_eq!(864000000005000, c.value(0));
5631        assert_eq!(1545696000001000, c.value(1));
5632
5633        // test overflow, safe cast
5634        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
5635        let b = cast(&array, &DataType::Date64).unwrap();
5636        assert!(b.is_null(0));
5637        // test overflow, unsafe cast
5638        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
5639        let options = CastOptions {
5640            safe: false,
5641            format_options: FormatOptions::default(),
5642        };
5643        let b = cast_with_options(&array, &DataType::Date64, &options);
5644        assert!(b.is_err());
5645    }
5646
5647    #[test]
5648    fn test_cast_timestamp_to_time64() {
5649        // test timestamp secs
5650        let array = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
5651            .with_timezone("+01:00".to_string());
5652        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5653        let c = b.as_primitive::<Time64MicrosecondType>();
5654        assert_eq!(3605000000, c.value(0));
5655        assert_eq!(3601000000, c.value(1));
5656        assert!(c.is_null(2));
5657        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5658        let c = b.as_primitive::<Time64NanosecondType>();
5659        assert_eq!(3605000000000, c.value(0));
5660        assert_eq!(3601000000000, c.value(1));
5661        assert!(c.is_null(2));
5662
5663        // test timestamp milliseconds
5664        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
5665            .with_timezone("+01:00".to_string());
5666        let array = Arc::new(a) as ArrayRef;
5667        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5668        let c = b.as_primitive::<Time64MicrosecondType>();
5669        assert_eq!(3605000000, c.value(0));
5670        assert_eq!(3601000000, c.value(1));
5671        assert!(c.is_null(2));
5672        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5673        let c = b.as_primitive::<Time64NanosecondType>();
5674        assert_eq!(3605000000000, c.value(0));
5675        assert_eq!(3601000000000, c.value(1));
5676        assert!(c.is_null(2));
5677
5678        // test timestamp microseconds
5679        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
5680            .with_timezone("+01:00".to_string());
5681        let array = Arc::new(a) as ArrayRef;
5682        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5683        let c = b.as_primitive::<Time64MicrosecondType>();
5684        assert_eq!(3605000000, c.value(0));
5685        assert_eq!(3601000000, c.value(1));
5686        assert!(c.is_null(2));
5687        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5688        let c = b.as_primitive::<Time64NanosecondType>();
5689        assert_eq!(3605000000000, c.value(0));
5690        assert_eq!(3601000000000, c.value(1));
5691        assert!(c.is_null(2));
5692
5693        // test timestamp nanoseconds
5694        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
5695            .with_timezone("+01:00".to_string());
5696        let array = Arc::new(a) as ArrayRef;
5697        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5698        let c = b.as_primitive::<Time64MicrosecondType>();
5699        assert_eq!(3605000000, c.value(0));
5700        assert_eq!(3601000000, c.value(1));
5701        assert!(c.is_null(2));
5702        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5703        let c = b.as_primitive::<Time64NanosecondType>();
5704        assert_eq!(3605000000000, c.value(0));
5705        assert_eq!(3601000000000, c.value(1));
5706        assert!(c.is_null(2));
5707
5708        // test overflow
5709        let a =
5710            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
5711        let array = Arc::new(a) as ArrayRef;
5712        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond));
5713        assert!(b.is_err());
5714        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond));
5715        assert!(b.is_err());
5716        let b = cast(&array, &DataType::Time64(TimeUnit::Millisecond));
5717        assert!(b.is_err());
5718    }
5719
5720    #[test]
5721    fn test_cast_timestamp_to_time32() {
5722        // test timestamp secs
5723        let a = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
5724            .with_timezone("+01:00".to_string());
5725        let array = Arc::new(a) as ArrayRef;
5726        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5727        let c = b.as_primitive::<Time32SecondType>();
5728        assert_eq!(3605, c.value(0));
5729        assert_eq!(3601, c.value(1));
5730        assert!(c.is_null(2));
5731        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5732        let c = b.as_primitive::<Time32MillisecondType>();
5733        assert_eq!(3605000, c.value(0));
5734        assert_eq!(3601000, c.value(1));
5735        assert!(c.is_null(2));
5736
5737        // test timestamp milliseconds
5738        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
5739            .with_timezone("+01:00".to_string());
5740        let array = Arc::new(a) as ArrayRef;
5741        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5742        let c = b.as_primitive::<Time32SecondType>();
5743        assert_eq!(3605, c.value(0));
5744        assert_eq!(3601, c.value(1));
5745        assert!(c.is_null(2));
5746        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5747        let c = b.as_primitive::<Time32MillisecondType>();
5748        assert_eq!(3605000, c.value(0));
5749        assert_eq!(3601000, c.value(1));
5750        assert!(c.is_null(2));
5751
5752        // test timestamp microseconds
5753        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
5754            .with_timezone("+01:00".to_string());
5755        let array = Arc::new(a) as ArrayRef;
5756        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5757        let c = b.as_primitive::<Time32SecondType>();
5758        assert_eq!(3605, c.value(0));
5759        assert_eq!(3601, c.value(1));
5760        assert!(c.is_null(2));
5761        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5762        let c = b.as_primitive::<Time32MillisecondType>();
5763        assert_eq!(3605000, c.value(0));
5764        assert_eq!(3601000, c.value(1));
5765        assert!(c.is_null(2));
5766
5767        // test timestamp nanoseconds
5768        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
5769            .with_timezone("+01:00".to_string());
5770        let array = Arc::new(a) as ArrayRef;
5771        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5772        let c = b.as_primitive::<Time32SecondType>();
5773        assert_eq!(3605, c.value(0));
5774        assert_eq!(3601, c.value(1));
5775        assert!(c.is_null(2));
5776        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5777        let c = b.as_primitive::<Time32MillisecondType>();
5778        assert_eq!(3605000, c.value(0));
5779        assert_eq!(3601000, c.value(1));
5780        assert!(c.is_null(2));
5781
5782        // test overflow
5783        let a =
5784            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
5785        let array = Arc::new(a) as ArrayRef;
5786        let b = cast(&array, &DataType::Time32(TimeUnit::Second));
5787        assert!(b.is_err());
5788        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond));
5789        assert!(b.is_err());
5790    }
5791
5792    // Cast Timestamp(_, None) -> Timestamp(_, Some(timezone))
5793    #[test]
5794    fn test_cast_timestamp_with_timezone_1() {
5795        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5796            Some("2000-01-01T00:00:00.123456789"),
5797            Some("2010-01-01T00:00:00.123456789"),
5798            None,
5799        ]));
5800        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
5801        let timestamp_array = cast(&string_array, &to_type).unwrap();
5802
5803        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
5804        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
5805
5806        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5807        let result = string_array.as_string::<i32>();
5808        assert_eq!("2000-01-01T00:00:00.123456+07:00", result.value(0));
5809        assert_eq!("2010-01-01T00:00:00.123456+07:00", result.value(1));
5810        assert!(result.is_null(2));
5811    }
5812
5813    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, None)
5814    #[test]
5815    fn test_cast_timestamp_with_timezone_2() {
5816        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5817            Some("2000-01-01T07:00:00.123456789"),
5818            Some("2010-01-01T07:00:00.123456789"),
5819            None,
5820        ]));
5821        let to_type = DataType::Timestamp(TimeUnit::Millisecond, Some("+0700".into()));
5822        let timestamp_array = cast(&string_array, &to_type).unwrap();
5823
5824        // Check intermediate representation is correct
5825        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5826        let result = string_array.as_string::<i32>();
5827        assert_eq!("2000-01-01T07:00:00.123+07:00", result.value(0));
5828        assert_eq!("2010-01-01T07:00:00.123+07:00", result.value(1));
5829        assert!(result.is_null(2));
5830
5831        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
5832        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
5833
5834        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5835        let result = string_array.as_string::<i32>();
5836        assert_eq!("2000-01-01T00:00:00.123", result.value(0));
5837        assert_eq!("2010-01-01T00:00:00.123", result.value(1));
5838        assert!(result.is_null(2));
5839    }
5840
5841    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, Some(timezone))
5842    #[test]
5843    fn test_cast_timestamp_with_timezone_3() {
5844        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5845            Some("2000-01-01T07:00:00.123456789"),
5846            Some("2010-01-01T07:00:00.123456789"),
5847            None,
5848        ]));
5849        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
5850        let timestamp_array = cast(&string_array, &to_type).unwrap();
5851
5852        // Check intermediate representation is correct
5853        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5854        let result = string_array.as_string::<i32>();
5855        assert_eq!("2000-01-01T07:00:00.123456+07:00", result.value(0));
5856        assert_eq!("2010-01-01T07:00:00.123456+07:00", result.value(1));
5857        assert!(result.is_null(2));
5858
5859        let to_type = DataType::Timestamp(TimeUnit::Second, Some("-08:00".into()));
5860        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
5861
5862        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5863        let result = string_array.as_string::<i32>();
5864        assert_eq!("1999-12-31T16:00:00-08:00", result.value(0));
5865        assert_eq!("2009-12-31T16:00:00-08:00", result.value(1));
5866        assert!(result.is_null(2));
5867    }
5868
5869    #[test]
5870    fn test_cast_date64_to_timestamp() {
5871        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5872        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5873        let c = b.as_primitive::<TimestampSecondType>();
5874        assert_eq!(864000000, c.value(0));
5875        assert_eq!(1545696000, c.value(1));
5876        assert!(c.is_null(2));
5877    }
5878
5879    #[test]
5880    fn test_cast_date64_to_timestamp_ms() {
5881        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5882        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
5883        let c = b
5884            .as_any()
5885            .downcast_ref::<TimestampMillisecondArray>()
5886            .unwrap();
5887        assert_eq!(864000000005, c.value(0));
5888        assert_eq!(1545696000001, c.value(1));
5889        assert!(c.is_null(2));
5890    }
5891
5892    #[test]
5893    fn test_cast_date64_to_timestamp_us() {
5894        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5895        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5896        let c = b
5897            .as_any()
5898            .downcast_ref::<TimestampMicrosecondArray>()
5899            .unwrap();
5900        assert_eq!(864000000005000, c.value(0));
5901        assert_eq!(1545696000001000, c.value(1));
5902        assert!(c.is_null(2));
5903    }
5904
5905    #[test]
5906    fn test_cast_date64_to_timestamp_ns() {
5907        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5908        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
5909        let c = b
5910            .as_any()
5911            .downcast_ref::<TimestampNanosecondArray>()
5912            .unwrap();
5913        assert_eq!(864000000005000000, c.value(0));
5914        assert_eq!(1545696000001000000, c.value(1));
5915        assert!(c.is_null(2));
5916    }
5917
5918    #[test]
5919    fn test_cast_timestamp_to_i64() {
5920        let array =
5921            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
5922                .with_timezone("UTC".to_string());
5923        let b = cast(&array, &DataType::Int64).unwrap();
5924        let c = b.as_primitive::<Int64Type>();
5925        assert_eq!(&DataType::Int64, c.data_type());
5926        assert_eq!(864000000005, c.value(0));
5927        assert_eq!(1545696000001, c.value(1));
5928        assert!(c.is_null(2));
5929    }
5930
5931    macro_rules! assert_cast {
5932        ($array:expr, $datatype:expr, $output_array_type: ty, $expected:expr) => {{
5933            assert!(can_cast_types($array.data_type(), &$datatype));
5934            let out = cast(&$array, &$datatype).unwrap();
5935            let actual = out
5936                .as_any()
5937                .downcast_ref::<$output_array_type>()
5938                .unwrap()
5939                .into_iter()
5940                .collect::<Vec<_>>();
5941            assert_eq!(actual, $expected);
5942        }};
5943        ($array:expr, $datatype:expr, $output_array_type: ty, $options:expr, $expected:expr) => {{
5944            assert!(can_cast_types($array.data_type(), &$datatype));
5945            let out = cast_with_options(&$array, &$datatype, &$options).unwrap();
5946            let actual = out
5947                .as_any()
5948                .downcast_ref::<$output_array_type>()
5949                .unwrap()
5950                .into_iter()
5951                .collect::<Vec<_>>();
5952            assert_eq!(actual, $expected);
5953        }};
5954    }
5955
5956    #[test]
5957    fn test_cast_date32_to_string() {
5958        let array = Date32Array::from(vec![Some(0), Some(10000), Some(13036), Some(17890), None]);
5959        let expected = vec![
5960            Some("1970-01-01"),
5961            Some("1997-05-19"),
5962            Some("2005-09-10"),
5963            Some("2018-12-25"),
5964            None,
5965        ];
5966
5967        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
5968        assert_cast!(array, DataType::Utf8, StringArray, expected);
5969        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
5970    }
5971
5972    #[test]
5973    fn test_cast_date64_to_string() {
5974        let array = Date64Array::from(vec![
5975            Some(0),
5976            Some(10000 * 86400000),
5977            Some(13036 * 86400000),
5978            Some(17890 * 86400000),
5979            None,
5980        ]);
5981        let expected = vec![
5982            Some("1970-01-01T00:00:00"),
5983            Some("1997-05-19T00:00:00"),
5984            Some("2005-09-10T00:00:00"),
5985            Some("2018-12-25T00:00:00"),
5986            None,
5987        ];
5988
5989        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
5990        assert_cast!(array, DataType::Utf8, StringArray, expected);
5991        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
5992    }
5993
5994    #[test]
5995    fn test_cast_date32_to_timestamp_and_timestamp_with_timezone() {
5996        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
5997        let a = Date32Array::from(vec![Some(18628), None, None]); // 2021-1-1, 2022-1-1
5998        let array = Arc::new(a) as ArrayRef;
5999
6000        let b = cast(
6001            &array,
6002            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6003        )
6004        .unwrap();
6005        let c = b.as_primitive::<TimestampSecondType>();
6006        let string_array = cast(&c, &DataType::Utf8).unwrap();
6007        let result = string_array.as_string::<i32>();
6008        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6009
6010        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6011        let c = b.as_primitive::<TimestampSecondType>();
6012        let string_array = cast(&c, &DataType::Utf8).unwrap();
6013        let result = string_array.as_string::<i32>();
6014        assert_eq!("2021-01-01T00:00:00", result.value(0));
6015    }
6016
6017    #[test]
6018    fn test_cast_date32_to_timestamp_with_timezone() {
6019        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6020        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6021        let array = Arc::new(a) as ArrayRef;
6022        let b = cast(
6023            &array,
6024            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6025        )
6026        .unwrap();
6027        let c = b.as_primitive::<TimestampSecondType>();
6028        assert_eq!(1609438500, c.value(0));
6029        assert_eq!(1640974500, c.value(1));
6030        assert!(c.is_null(2));
6031
6032        let string_array = cast(&c, &DataType::Utf8).unwrap();
6033        let result = string_array.as_string::<i32>();
6034        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6035        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6036    }
6037
6038    #[test]
6039    fn test_cast_date32_to_timestamp_with_timezone_ms() {
6040        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6041        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6042        let array = Arc::new(a) as ArrayRef;
6043        let b = cast(
6044            &array,
6045            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
6046        )
6047        .unwrap();
6048        let c = b.as_primitive::<TimestampMillisecondType>();
6049        assert_eq!(1609438500000, c.value(0));
6050        assert_eq!(1640974500000, c.value(1));
6051        assert!(c.is_null(2));
6052
6053        let string_array = cast(&c, &DataType::Utf8).unwrap();
6054        let result = string_array.as_string::<i32>();
6055        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6056        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6057    }
6058
6059    #[test]
6060    fn test_cast_date32_to_timestamp_with_timezone_us() {
6061        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6062        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6063        let array = Arc::new(a) as ArrayRef;
6064        let b = cast(
6065            &array,
6066            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
6067        )
6068        .unwrap();
6069        let c = b.as_primitive::<TimestampMicrosecondType>();
6070        assert_eq!(1609438500000000, c.value(0));
6071        assert_eq!(1640974500000000, c.value(1));
6072        assert!(c.is_null(2));
6073
6074        let string_array = cast(&c, &DataType::Utf8).unwrap();
6075        let result = string_array.as_string::<i32>();
6076        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6077        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6078    }
6079
6080    #[test]
6081    fn test_cast_date32_to_timestamp_with_timezone_ns() {
6082        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6083        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6084        let array = Arc::new(a) as ArrayRef;
6085        let b = cast(
6086            &array,
6087            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
6088        )
6089        .unwrap();
6090        let c = b.as_primitive::<TimestampNanosecondType>();
6091        assert_eq!(1609438500000000000, c.value(0));
6092        assert_eq!(1640974500000000000, c.value(1));
6093        assert!(c.is_null(2));
6094
6095        let string_array = cast(&c, &DataType::Utf8).unwrap();
6096        let result = string_array.as_string::<i32>();
6097        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6098        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6099    }
6100
6101    #[test]
6102    fn test_cast_date64_to_timestamp_with_timezone() {
6103        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6104        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6105        let b = cast(
6106            &array,
6107            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6108        )
6109        .unwrap();
6110
6111        let c = b.as_primitive::<TimestampSecondType>();
6112        assert_eq!(863979300, c.value(0));
6113        assert_eq!(1545675300, c.value(1));
6114        assert!(c.is_null(2));
6115
6116        let string_array = cast(&c, &DataType::Utf8).unwrap();
6117        let result = string_array.as_string::<i32>();
6118        assert_eq!("1997-05-19T00:00:00+05:45", result.value(0));
6119        assert_eq!("2018-12-25T00:00:00+05:45", result.value(1));
6120    }
6121
6122    #[test]
6123    fn test_cast_date64_to_timestamp_with_timezone_ms() {
6124        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6125        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6126        let b = cast(
6127            &array,
6128            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
6129        )
6130        .unwrap();
6131
6132        let c = b.as_primitive::<TimestampMillisecondType>();
6133        assert_eq!(863979300005, c.value(0));
6134        assert_eq!(1545675300001, c.value(1));
6135        assert!(c.is_null(2));
6136
6137        let string_array = cast(&c, &DataType::Utf8).unwrap();
6138        let result = string_array.as_string::<i32>();
6139        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6140        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6141    }
6142
6143    #[test]
6144    fn test_cast_date64_to_timestamp_with_timezone_us() {
6145        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6146        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6147        let b = cast(
6148            &array,
6149            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
6150        )
6151        .unwrap();
6152
6153        let c = b.as_primitive::<TimestampMicrosecondType>();
6154        assert_eq!(863979300005000, c.value(0));
6155        assert_eq!(1545675300001000, c.value(1));
6156        assert!(c.is_null(2));
6157
6158        let string_array = cast(&c, &DataType::Utf8).unwrap();
6159        let result = string_array.as_string::<i32>();
6160        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6161        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6162    }
6163
6164    #[test]
6165    fn test_cast_date64_to_timestamp_with_timezone_ns() {
6166        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6167        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6168        let b = cast(
6169            &array,
6170            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
6171        )
6172        .unwrap();
6173
6174        let c = b.as_primitive::<TimestampNanosecondType>();
6175        assert_eq!(863979300005000000, c.value(0));
6176        assert_eq!(1545675300001000000, c.value(1));
6177        assert!(c.is_null(2));
6178
6179        let string_array = cast(&c, &DataType::Utf8).unwrap();
6180        let result = string_array.as_string::<i32>();
6181        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6182        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6183    }
6184
6185    #[test]
6186    fn test_cast_timestamp_to_strings() {
6187        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
6188        let array =
6189            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6190        let expected = vec![
6191            Some("1997-05-19T00:00:03.005"),
6192            Some("2018-12-25T00:00:02.001"),
6193            None,
6194        ];
6195
6196        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6197        assert_cast!(array, DataType::Utf8, StringArray, expected);
6198        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6199    }
6200
6201    #[test]
6202    fn test_cast_timestamp_to_strings_opt() {
6203        let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
6204        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6205        let cast_options = CastOptions {
6206            safe: true,
6207            format_options: FormatOptions::default()
6208                .with_timestamp_format(Some(ts_format))
6209                .with_timestamp_tz_format(Some(ts_format)),
6210        };
6211
6212        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
6213        let array_without_tz =
6214            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6215        let expected = vec![
6216            Some("1997-05-19 00:00:03.005000"),
6217            Some("2018-12-25 00:00:02.001000"),
6218            None,
6219        ];
6220        assert_cast!(
6221            array_without_tz,
6222            DataType::Utf8View,
6223            StringViewArray,
6224            cast_options,
6225            expected
6226        );
6227        assert_cast!(
6228            array_without_tz,
6229            DataType::Utf8,
6230            StringArray,
6231            cast_options,
6232            expected
6233        );
6234        assert_cast!(
6235            array_without_tz,
6236            DataType::LargeUtf8,
6237            LargeStringArray,
6238            cast_options,
6239            expected
6240        );
6241
6242        let array_with_tz =
6243            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None])
6244                .with_timezone(tz.to_string());
6245        let expected = vec![
6246            Some("1997-05-19 05:45:03.005000"),
6247            Some("2018-12-25 05:45:02.001000"),
6248            None,
6249        ];
6250        assert_cast!(
6251            array_with_tz,
6252            DataType::Utf8View,
6253            StringViewArray,
6254            cast_options,
6255            expected
6256        );
6257        assert_cast!(
6258            array_with_tz,
6259            DataType::Utf8,
6260            StringArray,
6261            cast_options,
6262            expected
6263        );
6264        assert_cast!(
6265            array_with_tz,
6266            DataType::LargeUtf8,
6267            LargeStringArray,
6268            cast_options,
6269            expected
6270        );
6271    }
6272
6273    #[test]
6274    fn test_cast_between_timestamps() {
6275        let array =
6276            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6277        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6278        let c = b.as_primitive::<TimestampSecondType>();
6279        assert_eq!(864000003, c.value(0));
6280        assert_eq!(1545696002, c.value(1));
6281        assert!(c.is_null(2));
6282    }
6283
6284    #[test]
6285    fn test_cast_duration_to_i64() {
6286        let base = vec![5, 6, 7, 8, 100000000];
6287
6288        let duration_arrays = vec![
6289            Arc::new(DurationNanosecondArray::from(base.clone())) as ArrayRef,
6290            Arc::new(DurationMicrosecondArray::from(base.clone())) as ArrayRef,
6291            Arc::new(DurationMillisecondArray::from(base.clone())) as ArrayRef,
6292            Arc::new(DurationSecondArray::from(base.clone())) as ArrayRef,
6293        ];
6294
6295        for arr in duration_arrays {
6296            assert!(can_cast_types(arr.data_type(), &DataType::Int64));
6297            let result = cast(&arr, &DataType::Int64).unwrap();
6298            let result = result.as_primitive::<Int64Type>();
6299            assert_eq!(base.as_slice(), result.values());
6300        }
6301    }
6302
6303    #[test]
6304    fn test_cast_between_durations_and_numerics() {
6305        fn test_cast_between_durations<FromType, ToType>()
6306        where
6307            FromType: ArrowPrimitiveType<Native = i64>,
6308            ToType: ArrowPrimitiveType<Native = i64>,
6309            PrimitiveArray<FromType>: From<Vec<Option<i64>>>,
6310        {
6311            let from_unit = match FromType::DATA_TYPE {
6312                DataType::Duration(unit) => unit,
6313                _ => panic!("Expected a duration type"),
6314            };
6315            let to_unit = match ToType::DATA_TYPE {
6316                DataType::Duration(unit) => unit,
6317                _ => panic!("Expected a duration type"),
6318            };
6319            let from_size = time_unit_multiple(&from_unit);
6320            let to_size = time_unit_multiple(&to_unit);
6321
6322            let (v1_before, v2_before) = (8640003005, 1696002001);
6323            let (v1_after, v2_after) = if from_size >= to_size {
6324                (
6325                    v1_before / (from_size / to_size),
6326                    v2_before / (from_size / to_size),
6327                )
6328            } else {
6329                (
6330                    v1_before * (to_size / from_size),
6331                    v2_before * (to_size / from_size),
6332                )
6333            };
6334
6335            let array =
6336                PrimitiveArray::<FromType>::from(vec![Some(v1_before), Some(v2_before), None]);
6337            let b = cast(&array, &ToType::DATA_TYPE).unwrap();
6338            let c = b.as_primitive::<ToType>();
6339            assert_eq!(v1_after, c.value(0));
6340            assert_eq!(v2_after, c.value(1));
6341            assert!(c.is_null(2));
6342        }
6343
6344        // between each individual duration type
6345        test_cast_between_durations::<DurationSecondType, DurationMillisecondType>();
6346        test_cast_between_durations::<DurationSecondType, DurationMicrosecondType>();
6347        test_cast_between_durations::<DurationSecondType, DurationNanosecondType>();
6348        test_cast_between_durations::<DurationMillisecondType, DurationSecondType>();
6349        test_cast_between_durations::<DurationMillisecondType, DurationMicrosecondType>();
6350        test_cast_between_durations::<DurationMillisecondType, DurationNanosecondType>();
6351        test_cast_between_durations::<DurationMicrosecondType, DurationSecondType>();
6352        test_cast_between_durations::<DurationMicrosecondType, DurationMillisecondType>();
6353        test_cast_between_durations::<DurationMicrosecondType, DurationNanosecondType>();
6354        test_cast_between_durations::<DurationNanosecondType, DurationSecondType>();
6355        test_cast_between_durations::<DurationNanosecondType, DurationMillisecondType>();
6356        test_cast_between_durations::<DurationNanosecondType, DurationMicrosecondType>();
6357
6358        // cast failed
6359        let array = DurationSecondArray::from(vec![
6360            Some(i64::MAX),
6361            Some(8640203410378005),
6362            Some(10241096),
6363            None,
6364        ]);
6365        let b = cast(&array, &DataType::Duration(TimeUnit::Nanosecond)).unwrap();
6366        let c = b.as_primitive::<DurationNanosecondType>();
6367        assert!(c.is_null(0));
6368        assert!(c.is_null(1));
6369        assert_eq!(10241096000000000, c.value(2));
6370        assert!(c.is_null(3));
6371
6372        // durations to numerics
6373        let array = DurationSecondArray::from(vec![
6374            Some(i64::MAX),
6375            Some(8640203410378005),
6376            Some(10241096),
6377            None,
6378        ]);
6379        let b = cast(&array, &DataType::Int64).unwrap();
6380        let c = b.as_primitive::<Int64Type>();
6381        assert_eq!(i64::MAX, c.value(0));
6382        assert_eq!(8640203410378005, c.value(1));
6383        assert_eq!(10241096, c.value(2));
6384        assert!(c.is_null(3));
6385
6386        let b = cast(&array, &DataType::Int32).unwrap();
6387        let c = b.as_primitive::<Int32Type>();
6388        assert_eq!(0, c.value(0));
6389        assert_eq!(0, c.value(1));
6390        assert_eq!(10241096, c.value(2));
6391        assert!(c.is_null(3));
6392
6393        // numerics to durations
6394        let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), Some(10241096), None]);
6395        let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
6396        let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
6397        assert_eq!(i32::MAX as i64, c.value(0));
6398        assert_eq!(802034103, c.value(1));
6399        assert_eq!(10241096, c.value(2));
6400        assert!(c.is_null(3));
6401    }
6402
6403    #[test]
6404    fn test_cast_to_strings() {
6405        let a = Int32Array::from(vec![1, 2, 3]);
6406        let out = cast(&a, &DataType::Utf8).unwrap();
6407        let out = out
6408            .as_any()
6409            .downcast_ref::<StringArray>()
6410            .unwrap()
6411            .into_iter()
6412            .collect::<Vec<_>>();
6413        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
6414        let out = cast(&a, &DataType::LargeUtf8).unwrap();
6415        let out = out
6416            .as_any()
6417            .downcast_ref::<LargeStringArray>()
6418            .unwrap()
6419            .into_iter()
6420            .collect::<Vec<_>>();
6421        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
6422    }
6423
6424    #[test]
6425    fn test_str_to_str_casts() {
6426        for data in [
6427            vec![Some("foo"), Some("bar"), Some("ham")],
6428            vec![Some("foo"), None, Some("bar")],
6429        ] {
6430            let a = LargeStringArray::from(data.clone());
6431            let to = cast(&a, &DataType::Utf8).unwrap();
6432            let expect = a
6433                .as_any()
6434                .downcast_ref::<LargeStringArray>()
6435                .unwrap()
6436                .into_iter()
6437                .collect::<Vec<_>>();
6438            let out = to
6439                .as_any()
6440                .downcast_ref::<StringArray>()
6441                .unwrap()
6442                .into_iter()
6443                .collect::<Vec<_>>();
6444            assert_eq!(expect, out);
6445
6446            let a = StringArray::from(data);
6447            let to = cast(&a, &DataType::LargeUtf8).unwrap();
6448            let expect = a
6449                .as_any()
6450                .downcast_ref::<StringArray>()
6451                .unwrap()
6452                .into_iter()
6453                .collect::<Vec<_>>();
6454            let out = to
6455                .as_any()
6456                .downcast_ref::<LargeStringArray>()
6457                .unwrap()
6458                .into_iter()
6459                .collect::<Vec<_>>();
6460            assert_eq!(expect, out);
6461        }
6462    }
6463
6464    const VIEW_TEST_DATA: [Option<&str>; 5] = [
6465        Some("hello"),
6466        Some("repeated"),
6467        None,
6468        Some("large payload over 12 bytes"),
6469        Some("repeated"),
6470    ];
6471
6472    #[test]
6473    fn test_string_view_to_binary_view() {
6474        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6475
6476        assert!(can_cast_types(
6477            string_view_array.data_type(),
6478            &DataType::BinaryView
6479        ));
6480
6481        let binary_view_array = cast(&string_view_array, &DataType::BinaryView).unwrap();
6482        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
6483
6484        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6485        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
6486    }
6487
6488    #[test]
6489    fn test_binary_view_to_string_view() {
6490        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6491
6492        assert!(can_cast_types(
6493            binary_view_array.data_type(),
6494            &DataType::Utf8View
6495        ));
6496
6497        let string_view_array = cast(&binary_view_array, &DataType::Utf8View).unwrap();
6498        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6499
6500        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6501        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
6502    }
6503
6504    #[test]
6505    fn test_binary_view_to_string_view_with_invalid_utf8() {
6506        let binary_view_array = BinaryViewArray::from_iter(vec![
6507            Some("valid".as_bytes()),
6508            Some(&[0xff]),
6509            Some("utf8".as_bytes()),
6510            None,
6511        ]);
6512
6513        let strict_options = CastOptions {
6514            safe: false,
6515            ..Default::default()
6516        };
6517
6518        assert!(
6519            cast_with_options(&binary_view_array, &DataType::Utf8View, &strict_options).is_err()
6520        );
6521
6522        let safe_options = CastOptions {
6523            safe: true,
6524            ..Default::default()
6525        };
6526
6527        let string_view_array =
6528            cast_with_options(&binary_view_array, &DataType::Utf8View, &safe_options).unwrap();
6529        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6530
6531        let values: Vec<_> = string_view_array.as_string_view().iter().collect();
6532
6533        assert_eq!(values, vec![Some("valid"), None, Some("utf8"), None]);
6534    }
6535
6536    #[test]
6537    fn test_string_to_view() {
6538        _test_string_to_view::<i32>();
6539        _test_string_to_view::<i64>();
6540    }
6541
6542    fn _test_string_to_view<O>()
6543    where
6544        O: OffsetSizeTrait,
6545    {
6546        let string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
6547
6548        assert!(can_cast_types(
6549            string_array.data_type(),
6550            &DataType::Utf8View
6551        ));
6552
6553        assert!(can_cast_types(
6554            string_array.data_type(),
6555            &DataType::BinaryView
6556        ));
6557
6558        let string_view_array = cast(&string_array, &DataType::Utf8View).unwrap();
6559        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6560
6561        let binary_view_array = cast(&string_array, &DataType::BinaryView).unwrap();
6562        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
6563
6564        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6565        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
6566
6567        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6568        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
6569    }
6570
6571    #[test]
6572    fn test_bianry_to_view() {
6573        _test_binary_to_view::<i32>();
6574        _test_binary_to_view::<i64>();
6575    }
6576
6577    fn _test_binary_to_view<O>()
6578    where
6579        O: OffsetSizeTrait,
6580    {
6581        let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
6582
6583        assert!(can_cast_types(
6584            binary_array.data_type(),
6585            &DataType::Utf8View
6586        ));
6587
6588        assert!(can_cast_types(
6589            binary_array.data_type(),
6590            &DataType::BinaryView
6591        ));
6592
6593        let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
6594        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6595
6596        let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
6597        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
6598
6599        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6600        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
6601
6602        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6603        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
6604    }
6605
6606    #[test]
6607    fn test_dict_to_view() {
6608        let values = StringArray::from_iter(VIEW_TEST_DATA);
6609        let keys = Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
6610        let string_dict_array =
6611            DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
6612        let typed_dict = string_dict_array.downcast_dict::<StringArray>().unwrap();
6613
6614        let string_view_array = {
6615            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
6616            for v in typed_dict.into_iter() {
6617                builder.append_option(v);
6618            }
6619            builder.finish()
6620        };
6621        let expected_string_array_type = string_view_array.data_type();
6622        let casted_string_array = cast(&string_dict_array, expected_string_array_type).unwrap();
6623        assert_eq!(casted_string_array.data_type(), expected_string_array_type);
6624        assert_eq!(casted_string_array.as_ref(), &string_view_array);
6625
6626        let binary_buffer = cast(&typed_dict.values(), &DataType::Binary).unwrap();
6627        let binary_dict_array =
6628            DictionaryArray::<Int8Type>::new(typed_dict.keys().clone(), binary_buffer);
6629        let typed_binary_dict = binary_dict_array.downcast_dict::<BinaryArray>().unwrap();
6630
6631        let binary_view_array = {
6632            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
6633            for v in typed_binary_dict.into_iter() {
6634                builder.append_option(v);
6635            }
6636            builder.finish()
6637        };
6638        let expected_binary_array_type = binary_view_array.data_type();
6639        let casted_binary_array = cast(&binary_dict_array, expected_binary_array_type).unwrap();
6640        assert_eq!(casted_binary_array.data_type(), expected_binary_array_type);
6641        assert_eq!(casted_binary_array.as_ref(), &binary_view_array);
6642    }
6643
6644    #[test]
6645    fn test_view_to_dict() {
6646        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6647        let string_dict_array: DictionaryArray<Int8Type> = VIEW_TEST_DATA.into_iter().collect();
6648        let casted_type = string_dict_array.data_type();
6649        let casted_dict_array = cast(&string_view_array, casted_type).unwrap();
6650        assert_eq!(casted_dict_array.data_type(), casted_type);
6651        assert_eq!(casted_dict_array.as_ref(), &string_dict_array);
6652
6653        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6654        let binary_dict_array = string_dict_array.downcast_dict::<StringArray>().unwrap();
6655        let binary_buffer = cast(&binary_dict_array.values(), &DataType::Binary).unwrap();
6656        let binary_dict_array =
6657            DictionaryArray::<Int8Type>::new(binary_dict_array.keys().clone(), binary_buffer);
6658        let casted_type = binary_dict_array.data_type();
6659        let casted_binary_array = cast(&binary_view_array, casted_type).unwrap();
6660        assert_eq!(casted_binary_array.data_type(), casted_type);
6661        assert_eq!(casted_binary_array.as_ref(), &binary_dict_array);
6662    }
6663
6664    #[test]
6665    fn test_view_to_string() {
6666        _test_view_to_string::<i32>();
6667        _test_view_to_string::<i64>();
6668    }
6669
6670    fn _test_view_to_string<O>()
6671    where
6672        O: OffsetSizeTrait,
6673    {
6674        let string_view_array = {
6675            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
6676            for s in VIEW_TEST_DATA.iter() {
6677                builder.append_option(*s);
6678            }
6679            builder.finish()
6680        };
6681
6682        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6683
6684        let expected_string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
6685        let expected_type = expected_string_array.data_type();
6686
6687        assert!(can_cast_types(string_view_array.data_type(), expected_type));
6688        assert!(can_cast_types(binary_view_array.data_type(), expected_type));
6689
6690        let string_view_casted_array = cast(&string_view_array, expected_type).unwrap();
6691        assert_eq!(string_view_casted_array.data_type(), expected_type);
6692        assert_eq!(string_view_casted_array.as_ref(), &expected_string_array);
6693
6694        let binary_view_casted_array = cast(&binary_view_array, expected_type).unwrap();
6695        assert_eq!(binary_view_casted_array.data_type(), expected_type);
6696        assert_eq!(binary_view_casted_array.as_ref(), &expected_string_array);
6697    }
6698
6699    #[test]
6700    fn test_view_to_binary() {
6701        _test_view_to_binary::<i32>();
6702        _test_view_to_binary::<i64>();
6703    }
6704
6705    fn _test_view_to_binary<O>()
6706    where
6707        O: OffsetSizeTrait,
6708    {
6709        let view_array = {
6710            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
6711            for s in VIEW_TEST_DATA.iter() {
6712                builder.append_option(*s);
6713            }
6714            builder.finish()
6715        };
6716
6717        let expected_binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
6718        let expected_type = expected_binary_array.data_type();
6719
6720        assert!(can_cast_types(view_array.data_type(), expected_type));
6721
6722        let binary_array = cast(&view_array, expected_type).unwrap();
6723        assert_eq!(binary_array.data_type(), expected_type);
6724
6725        assert_eq!(binary_array.as_ref(), &expected_binary_array);
6726    }
6727
6728    #[test]
6729    fn test_cast_from_f64() {
6730        let f64_values: Vec<f64> = vec![
6731            i64::MIN as f64,
6732            i32::MIN as f64,
6733            i16::MIN as f64,
6734            i8::MIN as f64,
6735            0_f64,
6736            u8::MAX as f64,
6737            u16::MAX as f64,
6738            u32::MAX as f64,
6739            u64::MAX as f64,
6740        ];
6741        let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values));
6742
6743        let f64_expected = vec![
6744            -9223372036854776000.0,
6745            -2147483648.0,
6746            -32768.0,
6747            -128.0,
6748            0.0,
6749            255.0,
6750            65535.0,
6751            4294967295.0,
6752            18446744073709552000.0,
6753        ];
6754        assert_eq!(
6755            f64_expected,
6756            get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
6757                .iter()
6758                .map(|i| i.parse::<f64>().unwrap())
6759                .collect::<Vec<f64>>()
6760        );
6761
6762        let f32_expected = vec![
6763            -9223372000000000000.0,
6764            -2147483600.0,
6765            -32768.0,
6766            -128.0,
6767            0.0,
6768            255.0,
6769            65535.0,
6770            4294967300.0,
6771            18446744000000000000.0,
6772        ];
6773        assert_eq!(
6774            f32_expected,
6775            get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
6776                .iter()
6777                .map(|i| i.parse::<f32>().unwrap())
6778                .collect::<Vec<f32>>()
6779        );
6780
6781        let f16_expected = vec![
6782            f16::from_f64(-9223372000000000000.0),
6783            f16::from_f64(-2147483600.0),
6784            f16::from_f64(-32768.0),
6785            f16::from_f64(-128.0),
6786            f16::from_f64(0.0),
6787            f16::from_f64(255.0),
6788            f16::from_f64(65535.0),
6789            f16::from_f64(4294967300.0),
6790            f16::from_f64(18446744000000000000.0),
6791        ];
6792        assert_eq!(
6793            f16_expected,
6794            get_cast_values::<Float16Type>(&f64_array, &DataType::Float16)
6795                .iter()
6796                .map(|i| i.parse::<f16>().unwrap())
6797                .collect::<Vec<f16>>()
6798        );
6799
6800        let i64_expected = vec![
6801            "-9223372036854775808",
6802            "-2147483648",
6803            "-32768",
6804            "-128",
6805            "0",
6806            "255",
6807            "65535",
6808            "4294967295",
6809            "null",
6810        ];
6811        assert_eq!(
6812            i64_expected,
6813            get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
6814        );
6815
6816        let i32_expected = vec![
6817            "null",
6818            "-2147483648",
6819            "-32768",
6820            "-128",
6821            "0",
6822            "255",
6823            "65535",
6824            "null",
6825            "null",
6826        ];
6827        assert_eq!(
6828            i32_expected,
6829            get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
6830        );
6831
6832        let i16_expected = vec![
6833            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
6834        ];
6835        assert_eq!(
6836            i16_expected,
6837            get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
6838        );
6839
6840        let i8_expected = vec![
6841            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
6842        ];
6843        assert_eq!(
6844            i8_expected,
6845            get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
6846        );
6847
6848        let u64_expected = vec![
6849            "null",
6850            "null",
6851            "null",
6852            "null",
6853            "0",
6854            "255",
6855            "65535",
6856            "4294967295",
6857            "null",
6858        ];
6859        assert_eq!(
6860            u64_expected,
6861            get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
6862        );
6863
6864        let u32_expected = vec![
6865            "null",
6866            "null",
6867            "null",
6868            "null",
6869            "0",
6870            "255",
6871            "65535",
6872            "4294967295",
6873            "null",
6874        ];
6875        assert_eq!(
6876            u32_expected,
6877            get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
6878        );
6879
6880        let u16_expected = vec![
6881            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
6882        ];
6883        assert_eq!(
6884            u16_expected,
6885            get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
6886        );
6887
6888        let u8_expected = vec![
6889            "null", "null", "null", "null", "0", "255", "null", "null", "null",
6890        ];
6891        assert_eq!(
6892            u8_expected,
6893            get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
6894        );
6895    }
6896
6897    #[test]
6898    fn test_cast_from_f32() {
6899        let f32_values: Vec<f32> = vec![
6900            i32::MIN as f32,
6901            i32::MIN as f32,
6902            i16::MIN as f32,
6903            i8::MIN as f32,
6904            0_f32,
6905            u8::MAX as f32,
6906            u16::MAX as f32,
6907            u32::MAX as f32,
6908            u32::MAX as f32,
6909        ];
6910        let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values));
6911
6912        let f64_expected = vec![
6913            "-2147483648.0",
6914            "-2147483648.0",
6915            "-32768.0",
6916            "-128.0",
6917            "0.0",
6918            "255.0",
6919            "65535.0",
6920            "4294967296.0",
6921            "4294967296.0",
6922        ];
6923        assert_eq!(
6924            f64_expected,
6925            get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
6926        );
6927
6928        let f32_expected = vec![
6929            "-2147483600.0",
6930            "-2147483600.0",
6931            "-32768.0",
6932            "-128.0",
6933            "0.0",
6934            "255.0",
6935            "65535.0",
6936            "4294967300.0",
6937            "4294967300.0",
6938        ];
6939        assert_eq!(
6940            f32_expected,
6941            get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
6942        );
6943
6944        let f16_expected = vec![
6945            "-inf", "-inf", "-32768.0", "-128.0", "0.0", "255.0", "inf", "inf", "inf",
6946        ];
6947        assert_eq!(
6948            f16_expected,
6949            get_cast_values::<Float16Type>(&f32_array, &DataType::Float16)
6950        );
6951
6952        let i64_expected = vec![
6953            "-2147483648",
6954            "-2147483648",
6955            "-32768",
6956            "-128",
6957            "0",
6958            "255",
6959            "65535",
6960            "4294967296",
6961            "4294967296",
6962        ];
6963        assert_eq!(
6964            i64_expected,
6965            get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
6966        );
6967
6968        let i32_expected = vec![
6969            "-2147483648",
6970            "-2147483648",
6971            "-32768",
6972            "-128",
6973            "0",
6974            "255",
6975            "65535",
6976            "null",
6977            "null",
6978        ];
6979        assert_eq!(
6980            i32_expected,
6981            get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
6982        );
6983
6984        let i16_expected = vec![
6985            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
6986        ];
6987        assert_eq!(
6988            i16_expected,
6989            get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
6990        );
6991
6992        let i8_expected = vec![
6993            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
6994        ];
6995        assert_eq!(
6996            i8_expected,
6997            get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
6998        );
6999
7000        let u64_expected = vec![
7001            "null",
7002            "null",
7003            "null",
7004            "null",
7005            "0",
7006            "255",
7007            "65535",
7008            "4294967296",
7009            "4294967296",
7010        ];
7011        assert_eq!(
7012            u64_expected,
7013            get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
7014        );
7015
7016        let u32_expected = vec![
7017            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7018        ];
7019        assert_eq!(
7020            u32_expected,
7021            get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
7022        );
7023
7024        let u16_expected = vec![
7025            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7026        ];
7027        assert_eq!(
7028            u16_expected,
7029            get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
7030        );
7031
7032        let u8_expected = vec![
7033            "null", "null", "null", "null", "0", "255", "null", "null", "null",
7034        ];
7035        assert_eq!(
7036            u8_expected,
7037            get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
7038        );
7039    }
7040
7041    #[test]
7042    fn test_cast_from_uint64() {
7043        let u64_values: Vec<u64> = vec![
7044            0,
7045            u8::MAX as u64,
7046            u16::MAX as u64,
7047            u32::MAX as u64,
7048            u64::MAX,
7049        ];
7050        let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values));
7051
7052        let f64_expected = vec![0.0, 255.0, 65535.0, 4294967295.0, 18446744073709552000.0];
7053        assert_eq!(
7054            f64_expected,
7055            get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
7056                .iter()
7057                .map(|i| i.parse::<f64>().unwrap())
7058                .collect::<Vec<f64>>()
7059        );
7060
7061        let f32_expected = vec![0.0, 255.0, 65535.0, 4294967300.0, 18446744000000000000.0];
7062        assert_eq!(
7063            f32_expected,
7064            get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
7065                .iter()
7066                .map(|i| i.parse::<f32>().unwrap())
7067                .collect::<Vec<f32>>()
7068        );
7069
7070        let f16_expected = vec![
7071            f16::from_f64(0.0),
7072            f16::from_f64(255.0),
7073            f16::from_f64(65535.0),
7074            f16::from_f64(4294967300.0),
7075            f16::from_f64(18446744000000000000.0),
7076        ];
7077        assert_eq!(
7078            f16_expected,
7079            get_cast_values::<Float16Type>(&u64_array, &DataType::Float16)
7080                .iter()
7081                .map(|i| i.parse::<f16>().unwrap())
7082                .collect::<Vec<f16>>()
7083        );
7084
7085        let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
7086        assert_eq!(
7087            i64_expected,
7088            get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
7089        );
7090
7091        let i32_expected = vec!["0", "255", "65535", "null", "null"];
7092        assert_eq!(
7093            i32_expected,
7094            get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
7095        );
7096
7097        let i16_expected = vec!["0", "255", "null", "null", "null"];
7098        assert_eq!(
7099            i16_expected,
7100            get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
7101        );
7102
7103        let i8_expected = vec!["0", "null", "null", "null", "null"];
7104        assert_eq!(
7105            i8_expected,
7106            get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
7107        );
7108
7109        let u64_expected = vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
7110        assert_eq!(
7111            u64_expected,
7112            get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
7113        );
7114
7115        let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
7116        assert_eq!(
7117            u32_expected,
7118            get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
7119        );
7120
7121        let u16_expected = vec!["0", "255", "65535", "null", "null"];
7122        assert_eq!(
7123            u16_expected,
7124            get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
7125        );
7126
7127        let u8_expected = vec!["0", "255", "null", "null", "null"];
7128        assert_eq!(
7129            u8_expected,
7130            get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
7131        );
7132    }
7133
7134    #[test]
7135    fn test_cast_from_uint32() {
7136        let u32_values: Vec<u32> = vec![0, u8::MAX as u32, u16::MAX as u32, u32::MAX];
7137        let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values));
7138
7139        let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
7140        assert_eq!(
7141            f64_expected,
7142            get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
7143        );
7144
7145        let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
7146        assert_eq!(
7147            f32_expected,
7148            get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
7149        );
7150
7151        let f16_expected = vec!["0.0", "255.0", "inf", "inf"];
7152        assert_eq!(
7153            f16_expected,
7154            get_cast_values::<Float16Type>(&u32_array, &DataType::Float16)
7155        );
7156
7157        let i64_expected = vec!["0", "255", "65535", "4294967295"];
7158        assert_eq!(
7159            i64_expected,
7160            get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
7161        );
7162
7163        let i32_expected = vec!["0", "255", "65535", "null"];
7164        assert_eq!(
7165            i32_expected,
7166            get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
7167        );
7168
7169        let i16_expected = vec!["0", "255", "null", "null"];
7170        assert_eq!(
7171            i16_expected,
7172            get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
7173        );
7174
7175        let i8_expected = vec!["0", "null", "null", "null"];
7176        assert_eq!(
7177            i8_expected,
7178            get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
7179        );
7180
7181        let u64_expected = vec!["0", "255", "65535", "4294967295"];
7182        assert_eq!(
7183            u64_expected,
7184            get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
7185        );
7186
7187        let u32_expected = vec!["0", "255", "65535", "4294967295"];
7188        assert_eq!(
7189            u32_expected,
7190            get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
7191        );
7192
7193        let u16_expected = vec!["0", "255", "65535", "null"];
7194        assert_eq!(
7195            u16_expected,
7196            get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
7197        );
7198
7199        let u8_expected = vec!["0", "255", "null", "null"];
7200        assert_eq!(
7201            u8_expected,
7202            get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
7203        );
7204    }
7205
7206    #[test]
7207    fn test_cast_from_uint16() {
7208        let u16_values: Vec<u16> = vec![0, u8::MAX as u16, u16::MAX];
7209        let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values));
7210
7211        let f64_expected = vec!["0.0", "255.0", "65535.0"];
7212        assert_eq!(
7213            f64_expected,
7214            get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
7215        );
7216
7217        let f32_expected = vec!["0.0", "255.0", "65535.0"];
7218        assert_eq!(
7219            f32_expected,
7220            get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
7221        );
7222
7223        let f16_expected = vec!["0.0", "255.0", "inf"];
7224        assert_eq!(
7225            f16_expected,
7226            get_cast_values::<Float16Type>(&u16_array, &DataType::Float16)
7227        );
7228
7229        let i64_expected = vec!["0", "255", "65535"];
7230        assert_eq!(
7231            i64_expected,
7232            get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
7233        );
7234
7235        let i32_expected = vec!["0", "255", "65535"];
7236        assert_eq!(
7237            i32_expected,
7238            get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
7239        );
7240
7241        let i16_expected = vec!["0", "255", "null"];
7242        assert_eq!(
7243            i16_expected,
7244            get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
7245        );
7246
7247        let i8_expected = vec!["0", "null", "null"];
7248        assert_eq!(
7249            i8_expected,
7250            get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
7251        );
7252
7253        let u64_expected = vec!["0", "255", "65535"];
7254        assert_eq!(
7255            u64_expected,
7256            get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
7257        );
7258
7259        let u32_expected = vec!["0", "255", "65535"];
7260        assert_eq!(
7261            u32_expected,
7262            get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
7263        );
7264
7265        let u16_expected = vec!["0", "255", "65535"];
7266        assert_eq!(
7267            u16_expected,
7268            get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
7269        );
7270
7271        let u8_expected = vec!["0", "255", "null"];
7272        assert_eq!(
7273            u8_expected,
7274            get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
7275        );
7276    }
7277
7278    #[test]
7279    fn test_cast_from_uint8() {
7280        let u8_values: Vec<u8> = vec![0, u8::MAX];
7281        let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values));
7282
7283        let f64_expected = vec!["0.0", "255.0"];
7284        assert_eq!(
7285            f64_expected,
7286            get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
7287        );
7288
7289        let f32_expected = vec!["0.0", "255.0"];
7290        assert_eq!(
7291            f32_expected,
7292            get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
7293        );
7294
7295        let f16_expected = vec!["0.0", "255.0"];
7296        assert_eq!(
7297            f16_expected,
7298            get_cast_values::<Float16Type>(&u8_array, &DataType::Float16)
7299        );
7300
7301        let i64_expected = vec!["0", "255"];
7302        assert_eq!(
7303            i64_expected,
7304            get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
7305        );
7306
7307        let i32_expected = vec!["0", "255"];
7308        assert_eq!(
7309            i32_expected,
7310            get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
7311        );
7312
7313        let i16_expected = vec!["0", "255"];
7314        assert_eq!(
7315            i16_expected,
7316            get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
7317        );
7318
7319        let i8_expected = vec!["0", "null"];
7320        assert_eq!(
7321            i8_expected,
7322            get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
7323        );
7324
7325        let u64_expected = vec!["0", "255"];
7326        assert_eq!(
7327            u64_expected,
7328            get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
7329        );
7330
7331        let u32_expected = vec!["0", "255"];
7332        assert_eq!(
7333            u32_expected,
7334            get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
7335        );
7336
7337        let u16_expected = vec!["0", "255"];
7338        assert_eq!(
7339            u16_expected,
7340            get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
7341        );
7342
7343        let u8_expected = vec!["0", "255"];
7344        assert_eq!(
7345            u8_expected,
7346            get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
7347        );
7348    }
7349
7350    #[test]
7351    fn test_cast_from_int64() {
7352        let i64_values: Vec<i64> = vec![
7353            i64::MIN,
7354            i32::MIN as i64,
7355            i16::MIN as i64,
7356            i8::MIN as i64,
7357            0,
7358            i8::MAX as i64,
7359            i16::MAX as i64,
7360            i32::MAX as i64,
7361            i64::MAX,
7362        ];
7363        let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values));
7364
7365        let f64_expected = vec![
7366            -9223372036854776000.0,
7367            -2147483648.0,
7368            -32768.0,
7369            -128.0,
7370            0.0,
7371            127.0,
7372            32767.0,
7373            2147483647.0,
7374            9223372036854776000.0,
7375        ];
7376        assert_eq!(
7377            f64_expected,
7378            get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
7379                .iter()
7380                .map(|i| i.parse::<f64>().unwrap())
7381                .collect::<Vec<f64>>()
7382        );
7383
7384        let f32_expected = vec![
7385            -9223372000000000000.0,
7386            -2147483600.0,
7387            -32768.0,
7388            -128.0,
7389            0.0,
7390            127.0,
7391            32767.0,
7392            2147483600.0,
7393            9223372000000000000.0,
7394        ];
7395        assert_eq!(
7396            f32_expected,
7397            get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
7398                .iter()
7399                .map(|i| i.parse::<f32>().unwrap())
7400                .collect::<Vec<f32>>()
7401        );
7402
7403        let f16_expected = vec![
7404            f16::from_f64(-9223372000000000000.0),
7405            f16::from_f64(-2147483600.0),
7406            f16::from_f64(-32768.0),
7407            f16::from_f64(-128.0),
7408            f16::from_f64(0.0),
7409            f16::from_f64(127.0),
7410            f16::from_f64(32767.0),
7411            f16::from_f64(2147483600.0),
7412            f16::from_f64(9223372000000000000.0),
7413        ];
7414        assert_eq!(
7415            f16_expected,
7416            get_cast_values::<Float16Type>(&i64_array, &DataType::Float16)
7417                .iter()
7418                .map(|i| i.parse::<f16>().unwrap())
7419                .collect::<Vec<f16>>()
7420        );
7421
7422        let i64_expected = vec![
7423            "-9223372036854775808",
7424            "-2147483648",
7425            "-32768",
7426            "-128",
7427            "0",
7428            "127",
7429            "32767",
7430            "2147483647",
7431            "9223372036854775807",
7432        ];
7433        assert_eq!(
7434            i64_expected,
7435            get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
7436        );
7437
7438        let i32_expected = vec![
7439            "null",
7440            "-2147483648",
7441            "-32768",
7442            "-128",
7443            "0",
7444            "127",
7445            "32767",
7446            "2147483647",
7447            "null",
7448        ];
7449        assert_eq!(
7450            i32_expected,
7451            get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
7452        );
7453
7454        assert_eq!(
7455            i32_expected,
7456            get_cast_values::<Date32Type>(&i64_array, &DataType::Date32)
7457        );
7458
7459        let i16_expected = vec![
7460            "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
7461        ];
7462        assert_eq!(
7463            i16_expected,
7464            get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
7465        );
7466
7467        let i8_expected = vec![
7468            "null", "null", "null", "-128", "0", "127", "null", "null", "null",
7469        ];
7470        assert_eq!(
7471            i8_expected,
7472            get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
7473        );
7474
7475        let u64_expected = vec![
7476            "null",
7477            "null",
7478            "null",
7479            "null",
7480            "0",
7481            "127",
7482            "32767",
7483            "2147483647",
7484            "9223372036854775807",
7485        ];
7486        assert_eq!(
7487            u64_expected,
7488            get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
7489        );
7490
7491        let u32_expected = vec![
7492            "null",
7493            "null",
7494            "null",
7495            "null",
7496            "0",
7497            "127",
7498            "32767",
7499            "2147483647",
7500            "null",
7501        ];
7502        assert_eq!(
7503            u32_expected,
7504            get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
7505        );
7506
7507        let u16_expected = vec![
7508            "null", "null", "null", "null", "0", "127", "32767", "null", "null",
7509        ];
7510        assert_eq!(
7511            u16_expected,
7512            get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
7513        );
7514
7515        let u8_expected = vec![
7516            "null", "null", "null", "null", "0", "127", "null", "null", "null",
7517        ];
7518        assert_eq!(
7519            u8_expected,
7520            get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
7521        );
7522    }
7523
7524    #[test]
7525    fn test_cast_from_int32() {
7526        let i32_values: Vec<i32> = vec![
7527            i32::MIN,
7528            i16::MIN as i32,
7529            i8::MIN as i32,
7530            0,
7531            i8::MAX as i32,
7532            i16::MAX as i32,
7533            i32::MAX,
7534        ];
7535        let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values));
7536
7537        let f64_expected = vec![
7538            "-2147483648.0",
7539            "-32768.0",
7540            "-128.0",
7541            "0.0",
7542            "127.0",
7543            "32767.0",
7544            "2147483647.0",
7545        ];
7546        assert_eq!(
7547            f64_expected,
7548            get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
7549        );
7550
7551        let f32_expected = vec![
7552            "-2147483600.0",
7553            "-32768.0",
7554            "-128.0",
7555            "0.0",
7556            "127.0",
7557            "32767.0",
7558            "2147483600.0",
7559        ];
7560        assert_eq!(
7561            f32_expected,
7562            get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
7563        );
7564
7565        let f16_expected = vec![
7566            f16::from_f64(-2147483600.0),
7567            f16::from_f64(-32768.0),
7568            f16::from_f64(-128.0),
7569            f16::from_f64(0.0),
7570            f16::from_f64(127.0),
7571            f16::from_f64(32767.0),
7572            f16::from_f64(2147483600.0),
7573        ];
7574        assert_eq!(
7575            f16_expected,
7576            get_cast_values::<Float16Type>(&i32_array, &DataType::Float16)
7577                .iter()
7578                .map(|i| i.parse::<f16>().unwrap())
7579                .collect::<Vec<f16>>()
7580        );
7581
7582        let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
7583        assert_eq!(
7584            i16_expected,
7585            get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
7586        );
7587
7588        let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
7589        assert_eq!(
7590            i8_expected,
7591            get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
7592        );
7593
7594        let u64_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
7595        assert_eq!(
7596            u64_expected,
7597            get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
7598        );
7599
7600        let u32_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
7601        assert_eq!(
7602            u32_expected,
7603            get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
7604        );
7605
7606        let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
7607        assert_eq!(
7608            u16_expected,
7609            get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
7610        );
7611
7612        let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
7613        assert_eq!(
7614            u8_expected,
7615            get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
7616        );
7617
7618        // The date32 to date64 cast increases the numerical values in order to keep the same dates.
7619        let i64_expected = vec![
7620            "-185542587187200000",
7621            "-2831155200000",
7622            "-11059200000",
7623            "0",
7624            "10972800000",
7625            "2831068800000",
7626            "185542587100800000",
7627        ];
7628        assert_eq!(
7629            i64_expected,
7630            get_cast_values::<Date64Type>(&i32_array, &DataType::Date64)
7631        );
7632    }
7633
7634    #[test]
7635    fn test_cast_from_int16() {
7636        let i16_values: Vec<i16> = vec![i16::MIN, i8::MIN as i16, 0, i8::MAX as i16, i16::MAX];
7637        let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values));
7638
7639        let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
7640        assert_eq!(
7641            f64_expected,
7642            get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
7643        );
7644
7645        let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
7646        assert_eq!(
7647            f32_expected,
7648            get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
7649        );
7650
7651        let f16_expected = vec![
7652            f16::from_f64(-32768.0),
7653            f16::from_f64(-128.0),
7654            f16::from_f64(0.0),
7655            f16::from_f64(127.0),
7656            f16::from_f64(32767.0),
7657        ];
7658        assert_eq!(
7659            f16_expected,
7660            get_cast_values::<Float16Type>(&i16_array, &DataType::Float16)
7661                .iter()
7662                .map(|i| i.parse::<f16>().unwrap())
7663                .collect::<Vec<f16>>()
7664        );
7665
7666        let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
7667        assert_eq!(
7668            i64_expected,
7669            get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
7670        );
7671
7672        let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
7673        assert_eq!(
7674            i32_expected,
7675            get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
7676        );
7677
7678        let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
7679        assert_eq!(
7680            i16_expected,
7681            get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
7682        );
7683
7684        let i8_expected = vec!["null", "-128", "0", "127", "null"];
7685        assert_eq!(
7686            i8_expected,
7687            get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
7688        );
7689
7690        let u64_expected = vec!["null", "null", "0", "127", "32767"];
7691        assert_eq!(
7692            u64_expected,
7693            get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
7694        );
7695
7696        let u32_expected = vec!["null", "null", "0", "127", "32767"];
7697        assert_eq!(
7698            u32_expected,
7699            get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
7700        );
7701
7702        let u16_expected = vec!["null", "null", "0", "127", "32767"];
7703        assert_eq!(
7704            u16_expected,
7705            get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
7706        );
7707
7708        let u8_expected = vec!["null", "null", "0", "127", "null"];
7709        assert_eq!(
7710            u8_expected,
7711            get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
7712        );
7713    }
7714
7715    #[test]
7716    fn test_cast_from_date32() {
7717        let i32_values: Vec<i32> = vec![
7718            i32::MIN,
7719            i16::MIN as i32,
7720            i8::MIN as i32,
7721            0,
7722            i8::MAX as i32,
7723            i16::MAX as i32,
7724            i32::MAX,
7725        ];
7726        let date32_array: ArrayRef = Arc::new(Date32Array::from(i32_values));
7727
7728        let i64_expected = vec![
7729            "-2147483648",
7730            "-32768",
7731            "-128",
7732            "0",
7733            "127",
7734            "32767",
7735            "2147483647",
7736        ];
7737        assert_eq!(
7738            i64_expected,
7739            get_cast_values::<Int64Type>(&date32_array, &DataType::Int64)
7740        );
7741    }
7742
7743    #[test]
7744    fn test_cast_from_int8() {
7745        let i8_values: Vec<i8> = vec![i8::MIN, 0, i8::MAX];
7746        let i8_array = Int8Array::from(i8_values);
7747
7748        let f64_expected = vec!["-128.0", "0.0", "127.0"];
7749        assert_eq!(
7750            f64_expected,
7751            get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
7752        );
7753
7754        let f32_expected = vec!["-128.0", "0.0", "127.0"];
7755        assert_eq!(
7756            f32_expected,
7757            get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
7758        );
7759
7760        let f16_expected = vec!["-128.0", "0.0", "127.0"];
7761        assert_eq!(
7762            f16_expected,
7763            get_cast_values::<Float16Type>(&i8_array, &DataType::Float16)
7764        );
7765
7766        let i64_expected = vec!["-128", "0", "127"];
7767        assert_eq!(
7768            i64_expected,
7769            get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
7770        );
7771
7772        let i32_expected = vec!["-128", "0", "127"];
7773        assert_eq!(
7774            i32_expected,
7775            get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
7776        );
7777
7778        let i16_expected = vec!["-128", "0", "127"];
7779        assert_eq!(
7780            i16_expected,
7781            get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
7782        );
7783
7784        let i8_expected = vec!["-128", "0", "127"];
7785        assert_eq!(
7786            i8_expected,
7787            get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
7788        );
7789
7790        let u64_expected = vec!["null", "0", "127"];
7791        assert_eq!(
7792            u64_expected,
7793            get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
7794        );
7795
7796        let u32_expected = vec!["null", "0", "127"];
7797        assert_eq!(
7798            u32_expected,
7799            get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
7800        );
7801
7802        let u16_expected = vec!["null", "0", "127"];
7803        assert_eq!(
7804            u16_expected,
7805            get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
7806        );
7807
7808        let u8_expected = vec!["null", "0", "127"];
7809        assert_eq!(
7810            u8_expected,
7811            get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
7812        );
7813    }
7814
7815    /// Convert `array` into a vector of strings by casting to data type dt
7816    fn get_cast_values<T>(array: &dyn Array, dt: &DataType) -> Vec<String>
7817    where
7818        T: ArrowPrimitiveType,
7819    {
7820        let c = cast(array, dt).unwrap();
7821        let a = c.as_primitive::<T>();
7822        let mut v: Vec<String> = vec![];
7823        for i in 0..array.len() {
7824            if a.is_null(i) {
7825                v.push("null".to_string())
7826            } else {
7827                v.push(format!("{:?}", a.value(i)));
7828            }
7829        }
7830        v
7831    }
7832
7833    #[test]
7834    fn test_cast_utf8_dict() {
7835        // FROM a dictionary with of Utf8 values
7836        let mut builder = StringDictionaryBuilder::<Int8Type>::new();
7837        builder.append("one").unwrap();
7838        builder.append_null();
7839        builder.append("three").unwrap();
7840        let array: ArrayRef = Arc::new(builder.finish());
7841
7842        let expected = vec!["one", "null", "three"];
7843
7844        // Test casting TO StringArray
7845        let cast_type = Utf8;
7846        let cast_array = cast(&array, &cast_type).expect("cast to UTF-8 failed");
7847        assert_eq!(cast_array.data_type(), &cast_type);
7848        assert_eq!(array_to_strings(&cast_array), expected);
7849
7850        // Test casting TO Dictionary (with different index sizes)
7851
7852        let cast_type = Dictionary(Box::new(Int16), Box::new(Utf8));
7853        let cast_array = cast(&array, &cast_type).expect("cast failed");
7854        assert_eq!(cast_array.data_type(), &cast_type);
7855        assert_eq!(array_to_strings(&cast_array), expected);
7856
7857        let cast_type = Dictionary(Box::new(Int32), Box::new(Utf8));
7858        let cast_array = cast(&array, &cast_type).expect("cast failed");
7859        assert_eq!(cast_array.data_type(), &cast_type);
7860        assert_eq!(array_to_strings(&cast_array), expected);
7861
7862        let cast_type = Dictionary(Box::new(Int64), Box::new(Utf8));
7863        let cast_array = cast(&array, &cast_type).expect("cast failed");
7864        assert_eq!(cast_array.data_type(), &cast_type);
7865        assert_eq!(array_to_strings(&cast_array), expected);
7866
7867        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
7868        let cast_array = cast(&array, &cast_type).expect("cast failed");
7869        assert_eq!(cast_array.data_type(), &cast_type);
7870        assert_eq!(array_to_strings(&cast_array), expected);
7871
7872        let cast_type = Dictionary(Box::new(UInt16), Box::new(Utf8));
7873        let cast_array = cast(&array, &cast_type).expect("cast failed");
7874        assert_eq!(cast_array.data_type(), &cast_type);
7875        assert_eq!(array_to_strings(&cast_array), expected);
7876
7877        let cast_type = Dictionary(Box::new(UInt32), Box::new(Utf8));
7878        let cast_array = cast(&array, &cast_type).expect("cast failed");
7879        assert_eq!(cast_array.data_type(), &cast_type);
7880        assert_eq!(array_to_strings(&cast_array), expected);
7881
7882        let cast_type = Dictionary(Box::new(UInt64), Box::new(Utf8));
7883        let cast_array = cast(&array, &cast_type).expect("cast failed");
7884        assert_eq!(cast_array.data_type(), &cast_type);
7885        assert_eq!(array_to_strings(&cast_array), expected);
7886    }
7887
7888    #[test]
7889    fn test_cast_dict_to_dict_bad_index_value_primitive() {
7890        // test converting from an array that has indexes of a type
7891        // that are out of bounds for a particular other kind of
7892        // index.
7893
7894        let mut builder = PrimitiveDictionaryBuilder::<Int32Type, Int64Type>::new();
7895
7896        // add 200 distinct values (which can be stored by a
7897        // dictionary indexed by int32, but not a dictionary indexed
7898        // with int8)
7899        for i in 0..200 {
7900            builder.append(i).unwrap();
7901        }
7902        let array: ArrayRef = Arc::new(builder.finish());
7903
7904        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
7905        let res = cast(&array, &cast_type);
7906        assert!(res.is_err());
7907        let actual_error = format!("{res:?}");
7908        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
7909        assert!(
7910            actual_error.contains(expected_error),
7911            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
7912        );
7913    }
7914
7915    #[test]
7916    fn test_cast_dict_to_dict_bad_index_value_utf8() {
7917        // Same test as test_cast_dict_to_dict_bad_index_value but use
7918        // string values (and encode the expected behavior here);
7919
7920        let mut builder = StringDictionaryBuilder::<Int32Type>::new();
7921
7922        // add 200 distinct values (which can be stored by a
7923        // dictionary indexed by int32, but not a dictionary indexed
7924        // with int8)
7925        for i in 0..200 {
7926            let val = format!("val{i}");
7927            builder.append(&val).unwrap();
7928        }
7929        let array = builder.finish();
7930
7931        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
7932        let res = cast(&array, &cast_type);
7933        assert!(res.is_err());
7934        let actual_error = format!("{res:?}");
7935        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
7936        assert!(
7937            actual_error.contains(expected_error),
7938            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
7939        );
7940    }
7941
7942    #[test]
7943    fn test_cast_primitive_dict() {
7944        // FROM a dictionary with of INT32 values
7945        let mut builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
7946        builder.append(1).unwrap();
7947        builder.append_null();
7948        builder.append(3).unwrap();
7949        let array: ArrayRef = Arc::new(builder.finish());
7950
7951        let expected = vec!["1", "null", "3"];
7952
7953        // Test casting TO PrimitiveArray, different dictionary type
7954        let cast_array = cast(&array, &Utf8).expect("cast to UTF-8 failed");
7955        assert_eq!(array_to_strings(&cast_array), expected);
7956        assert_eq!(cast_array.data_type(), &Utf8);
7957
7958        let cast_array = cast(&array, &Int64).expect("cast to int64 failed");
7959        assert_eq!(array_to_strings(&cast_array), expected);
7960        assert_eq!(cast_array.data_type(), &Int64);
7961    }
7962
7963    #[test]
7964    fn test_cast_primitive_array_to_dict() {
7965        let mut builder = PrimitiveBuilder::<Int32Type>::new();
7966        builder.append_value(1);
7967        builder.append_null();
7968        builder.append_value(3);
7969        let array: ArrayRef = Arc::new(builder.finish());
7970
7971        let expected = vec!["1", "null", "3"];
7972
7973        // Cast to a dictionary (same value type, Int32)
7974        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int32));
7975        let cast_array = cast(&array, &cast_type).expect("cast failed");
7976        assert_eq!(cast_array.data_type(), &cast_type);
7977        assert_eq!(array_to_strings(&cast_array), expected);
7978
7979        // Cast to a dictionary (different value type, Int8)
7980        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int8));
7981        let cast_array = cast(&array, &cast_type).expect("cast failed");
7982        assert_eq!(cast_array.data_type(), &cast_type);
7983        assert_eq!(array_to_strings(&cast_array), expected);
7984    }
7985
7986    #[test]
7987    fn test_cast_time_array_to_dict() {
7988        use DataType::*;
7989
7990        let array = Arc::new(Date32Array::from(vec![Some(1000), None, Some(2000)])) as ArrayRef;
7991
7992        let expected = vec!["1972-09-27", "null", "1975-06-24"];
7993
7994        let cast_type = Dictionary(Box::new(UInt8), Box::new(Date32));
7995        let cast_array = cast(&array, &cast_type).expect("cast failed");
7996        assert_eq!(cast_array.data_type(), &cast_type);
7997        assert_eq!(array_to_strings(&cast_array), expected);
7998    }
7999
8000    #[test]
8001    fn test_cast_timestamp_array_to_dict() {
8002        use DataType::*;
8003
8004        let array = Arc::new(
8005            TimestampSecondArray::from(vec![Some(1000), None, Some(2000)]).with_timezone_utc(),
8006        ) as ArrayRef;
8007
8008        let expected = vec!["1970-01-01T00:16:40", "null", "1970-01-01T00:33:20"];
8009
8010        let cast_type = Dictionary(Box::new(UInt8), Box::new(Timestamp(TimeUnit::Second, None)));
8011        let cast_array = cast(&array, &cast_type).expect("cast failed");
8012        assert_eq!(cast_array.data_type(), &cast_type);
8013        assert_eq!(array_to_strings(&cast_array), expected);
8014    }
8015
8016    #[test]
8017    fn test_cast_string_array_to_dict() {
8018        use DataType::*;
8019
8020        let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) as ArrayRef;
8021
8022        let expected = vec!["one", "null", "three"];
8023
8024        // Cast to a dictionary (same value type, Utf8)
8025        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8026        let cast_array = cast(&array, &cast_type).expect("cast failed");
8027        assert_eq!(cast_array.data_type(), &cast_type);
8028        assert_eq!(array_to_strings(&cast_array), expected);
8029    }
8030
8031    #[test]
8032    fn test_cast_null_array_to_from_decimal_array() {
8033        let data_type = DataType::Decimal128(12, 4);
8034        let array = new_null_array(&DataType::Null, 4);
8035        assert_eq!(array.data_type(), &DataType::Null);
8036        let cast_array = cast(&array, &data_type).expect("cast failed");
8037        assert_eq!(cast_array.data_type(), &data_type);
8038        for i in 0..4 {
8039            assert!(cast_array.is_null(i));
8040        }
8041
8042        let array = new_null_array(&data_type, 4);
8043        assert_eq!(array.data_type(), &data_type);
8044        let cast_array = cast(&array, &DataType::Null).expect("cast failed");
8045        assert_eq!(cast_array.data_type(), &DataType::Null);
8046        assert_eq!(cast_array.len(), 4);
8047        assert_eq!(cast_array.logical_nulls().unwrap().null_count(), 4);
8048    }
8049
8050    #[test]
8051    fn test_cast_null_array_from_and_to_primitive_array() {
8052        macro_rules! typed_test {
8053            ($ARR_TYPE:ident, $DATATYPE:ident, $TYPE:tt) => {{
8054                {
8055                    let array = Arc::new(NullArray::new(6)) as ArrayRef;
8056                    let expected = $ARR_TYPE::from(vec![None; 6]);
8057                    let cast_type = DataType::$DATATYPE;
8058                    let cast_array = cast(&array, &cast_type).expect("cast failed");
8059                    let cast_array = cast_array.as_primitive::<$TYPE>();
8060                    assert_eq!(cast_array.data_type(), &cast_type);
8061                    assert_eq!(cast_array, &expected);
8062                }
8063            }};
8064        }
8065
8066        typed_test!(Int16Array, Int16, Int16Type);
8067        typed_test!(Int32Array, Int32, Int32Type);
8068        typed_test!(Int64Array, Int64, Int64Type);
8069
8070        typed_test!(UInt16Array, UInt16, UInt16Type);
8071        typed_test!(UInt32Array, UInt32, UInt32Type);
8072        typed_test!(UInt64Array, UInt64, UInt64Type);
8073
8074        typed_test!(Float16Array, Float16, Float16Type);
8075        typed_test!(Float32Array, Float32, Float32Type);
8076        typed_test!(Float64Array, Float64, Float64Type);
8077
8078        typed_test!(Date32Array, Date32, Date32Type);
8079        typed_test!(Date64Array, Date64, Date64Type);
8080    }
8081
8082    fn cast_from_null_to_other(data_type: &DataType) {
8083        // Cast from null to data_type
8084        {
8085            let array = new_null_array(&DataType::Null, 4);
8086            assert_eq!(array.data_type(), &DataType::Null);
8087            let cast_array = cast(&array, data_type).expect("cast failed");
8088            assert_eq!(cast_array.data_type(), data_type);
8089            for i in 0..4 {
8090                assert!(cast_array.is_null(i));
8091            }
8092        }
8093    }
8094
8095    #[test]
8096    fn test_cast_null_from_and_to_variable_sized() {
8097        cast_from_null_to_other(&DataType::Utf8);
8098        cast_from_null_to_other(&DataType::LargeUtf8);
8099        cast_from_null_to_other(&DataType::Binary);
8100        cast_from_null_to_other(&DataType::LargeBinary);
8101    }
8102
8103    #[test]
8104    fn test_cast_null_from_and_to_nested_type() {
8105        // Cast null from and to map
8106        let data_type = DataType::Map(
8107            Arc::new(Field::new_struct(
8108                "entry",
8109                vec![
8110                    Field::new("key", DataType::Utf8, false),
8111                    Field::new("value", DataType::Int32, true),
8112                ],
8113                false,
8114            )),
8115            false,
8116        );
8117        cast_from_null_to_other(&data_type);
8118
8119        // Cast null from and to list
8120        let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
8121        cast_from_null_to_other(&data_type);
8122        let data_type = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8123        cast_from_null_to_other(&data_type);
8124        let data_type =
8125            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
8126        cast_from_null_to_other(&data_type);
8127
8128        // Cast null from and to dictionary
8129        let values = vec![None, None, None, None] as Vec<Option<&str>>;
8130        let array: DictionaryArray<Int8Type> = values.into_iter().collect();
8131        let array = Arc::new(array) as ArrayRef;
8132        let data_type = array.data_type().to_owned();
8133        cast_from_null_to_other(&data_type);
8134
8135        // Cast null from and to struct
8136        let data_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
8137        cast_from_null_to_other(&data_type);
8138    }
8139
8140    /// Print the `DictionaryArray` `array` as a vector of strings
8141    fn array_to_strings(array: &ArrayRef) -> Vec<String> {
8142        let options = FormatOptions::new().with_null("null");
8143        let formatter = ArrayFormatter::try_new(array.as_ref(), &options).unwrap();
8144        (0..array.len())
8145            .map(|i| formatter.value(i).to_string())
8146            .collect()
8147    }
8148
8149    #[test]
8150    fn test_cast_utf8_to_date32() {
8151        use chrono::NaiveDate;
8152        let from_ymd = chrono::NaiveDate::from_ymd_opt;
8153        let since = chrono::NaiveDate::signed_duration_since;
8154
8155        let a = StringArray::from(vec![
8156            "2000-01-01",          // valid date with leading 0s
8157            "2000-01-01T12:00:00", // valid datetime, will throw away the time part
8158            "2000-2-2",            // valid date without leading 0s
8159            "2000-00-00",          // invalid month and day
8160            "2000",                // just a year is invalid
8161        ]);
8162        let array = Arc::new(a) as ArrayRef;
8163        let b = cast(&array, &DataType::Date32).unwrap();
8164        let c = b.as_primitive::<Date32Type>();
8165
8166        // test valid inputs
8167        let date_value = since(
8168            NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
8169            from_ymd(1970, 1, 1).unwrap(),
8170        )
8171        .num_days() as i32;
8172        assert!(c.is_valid(0)); // "2000-01-01"
8173        assert_eq!(date_value, c.value(0));
8174
8175        assert!(c.is_valid(1)); // "2000-01-01T12:00:00"
8176        assert_eq!(date_value, c.value(1));
8177
8178        let date_value = since(
8179            NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
8180            from_ymd(1970, 1, 1).unwrap(),
8181        )
8182        .num_days() as i32;
8183        assert!(c.is_valid(2)); // "2000-2-2"
8184        assert_eq!(date_value, c.value(2));
8185
8186        // test invalid inputs
8187        assert!(!c.is_valid(3)); // "2000-00-00"
8188        assert!(!c.is_valid(4)); // "2000"
8189    }
8190
8191    #[test]
8192    fn test_cast_utf8_to_date64() {
8193        let a = StringArray::from(vec![
8194            "2000-01-01T12:00:00", // date + time valid
8195            "2020-12-15T12:34:56", // date + time valid
8196            "2020-2-2T12:34:56",   // valid date time without leading 0s
8197            "2000-00-00T12:00:00", // invalid month and day
8198            "2000-01-01 12:00:00", // missing the 'T'
8199            "2000-01-01",          // just a date is invalid
8200        ]);
8201        let array = Arc::new(a) as ArrayRef;
8202        let b = cast(&array, &DataType::Date64).unwrap();
8203        let c = b.as_primitive::<Date64Type>();
8204
8205        // test valid inputs
8206        assert!(c.is_valid(0)); // "2000-01-01T12:00:00"
8207        assert_eq!(946728000000, c.value(0));
8208        assert!(c.is_valid(1)); // "2020-12-15T12:34:56"
8209        assert_eq!(1608035696000, c.value(1));
8210        assert!(!c.is_valid(2)); // "2020-2-2T12:34:56"
8211
8212        assert!(!c.is_valid(3)); // "2000-00-00T12:00:00"
8213        assert!(c.is_valid(4)); // "2000-01-01 12:00:00"
8214        assert_eq!(946728000000, c.value(4));
8215        assert!(c.is_valid(5)); // "2000-01-01"
8216        assert_eq!(946684800000, c.value(5));
8217    }
8218
8219    #[test]
8220    fn test_can_cast_fsl_to_fsl() {
8221        let from_array = Arc::new(
8222            FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
8223                [Some([Some(1.0), Some(2.0)]), None],
8224                2,
8225            ),
8226        ) as ArrayRef;
8227        let to_array = Arc::new(
8228            FixedSizeListArray::from_iter_primitive::<Float16Type, _, _>(
8229                [
8230                    Some([Some(f16::from_f32(1.0)), Some(f16::from_f32(2.0))]),
8231                    None,
8232                ],
8233                2,
8234            ),
8235        ) as ArrayRef;
8236
8237        assert!(can_cast_types(from_array.data_type(), to_array.data_type()));
8238        let actual = cast(&from_array, to_array.data_type()).unwrap();
8239        assert_eq!(actual.data_type(), to_array.data_type());
8240
8241        let invalid_target =
8242            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Binary, true)), 2);
8243        assert!(!can_cast_types(from_array.data_type(), &invalid_target));
8244
8245        let invalid_size =
8246            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Float16, true)), 5);
8247        assert!(!can_cast_types(from_array.data_type(), &invalid_size));
8248    }
8249
8250    #[test]
8251    fn test_can_cast_types_fixed_size_list_to_list() {
8252        // DataType::List
8253        let array1 = Arc::new(make_fixed_size_list_array()) as ArrayRef;
8254        assert!(can_cast_types(
8255            array1.data_type(),
8256            &DataType::List(Arc::new(Field::new("", DataType::Int32, false)))
8257        ));
8258
8259        // DataType::LargeList
8260        let array2 = Arc::new(make_fixed_size_list_array_for_large_list()) as ArrayRef;
8261        assert!(can_cast_types(
8262            array2.data_type(),
8263            &DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false)))
8264        ));
8265    }
8266
8267    #[test]
8268    fn test_cast_fixed_size_list_to_list() {
8269        // Important cases:
8270        // 1. With/without nulls
8271        // 2. LargeList and List
8272        // 3. With and without inner casts
8273
8274        let cases = [
8275            // fixed_size_list<i32, 2> => list<i32>
8276            (
8277                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8278                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8279                    2,
8280                )) as ArrayRef,
8281                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
8282                    Some([Some(1), Some(1)]),
8283                    Some([Some(2), Some(2)]),
8284                ])) as ArrayRef,
8285            ),
8286            // fixed_size_list<i32, 2> => list<i32> (nullable)
8287            (
8288                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8289                    [None, Some([Some(2), Some(2)])],
8290                    2,
8291                )) as ArrayRef,
8292                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
8293                    None,
8294                    Some([Some(2), Some(2)]),
8295                ])) as ArrayRef,
8296            ),
8297            // fixed_size_list<i32, 2> => large_list<i64>
8298            (
8299                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8300                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8301                    2,
8302                )) as ArrayRef,
8303                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
8304                    Some([Some(1), Some(1)]),
8305                    Some([Some(2), Some(2)]),
8306                ])) as ArrayRef,
8307            ),
8308            // fixed_size_list<i32, 2> => large_list<i64> (nullable)
8309            (
8310                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8311                    [None, Some([Some(2), Some(2)])],
8312                    2,
8313                )) as ArrayRef,
8314                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
8315                    None,
8316                    Some([Some(2), Some(2)]),
8317                ])) as ArrayRef,
8318            ),
8319        ];
8320
8321        for (array, expected) in cases {
8322            let array = Arc::new(array) as ArrayRef;
8323
8324            assert!(
8325                can_cast_types(array.data_type(), expected.data_type()),
8326                "can_cast_types claims we cannot cast {:?} to {:?}",
8327                array.data_type(),
8328                expected.data_type()
8329            );
8330
8331            let list_array = cast(&array, expected.data_type())
8332                .unwrap_or_else(|_| panic!("Failed to cast {array:?} to {expected:?}"));
8333            assert_eq!(
8334                list_array.as_ref(),
8335                &expected,
8336                "Incorrect result from casting {array:?} to {expected:?}",
8337            );
8338        }
8339    }
8340
8341    #[test]
8342    fn test_cast_utf8_to_list() {
8343        // DataType::List
8344        let array = Arc::new(StringArray::from(vec!["5"])) as ArrayRef;
8345        let field = Arc::new(Field::new("", DataType::Int32, false));
8346        let list_array = cast(&array, &DataType::List(field.clone())).unwrap();
8347        let actual = list_array.as_list_opt::<i32>().unwrap();
8348        let expect = ListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
8349        assert_eq!(&expect.value(0), &actual.value(0));
8350
8351        // DataType::LargeList
8352        let list_array = cast(&array, &DataType::LargeList(field.clone())).unwrap();
8353        let actual = list_array.as_list_opt::<i64>().unwrap();
8354        let expect = LargeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
8355        assert_eq!(&expect.value(0), &actual.value(0));
8356
8357        // DataType::FixedSizeList
8358        let list_array = cast(&array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
8359        let actual = list_array.as_fixed_size_list_opt().unwrap();
8360        let expect =
8361            FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])], 1);
8362        assert_eq!(&expect.value(0), &actual.value(0));
8363    }
8364
8365    #[test]
8366    fn test_cast_single_element_fixed_size_list() {
8367        // FixedSizeList<T>[1] => T
8368        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
8369            [(Some([Some(5)]))],
8370            1,
8371        )) as ArrayRef;
8372        let casted_array = cast(&from_array, &DataType::Int32).unwrap();
8373        let actual: &Int32Array = casted_array.as_primitive();
8374        let expected = Int32Array::from(vec![Some(5)]);
8375        assert_eq!(&expected, actual);
8376
8377        // FixedSizeList<T>[1] => FixedSizeList<U>[1]
8378        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
8379            [(Some([Some(5)]))],
8380            1,
8381        )) as ArrayRef;
8382        let to_field = Arc::new(Field::new("dummy", DataType::Float32, false));
8383        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
8384        let expected = Arc::new(FixedSizeListArray::new(
8385            to_field.clone(),
8386            1,
8387            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
8388            None,
8389        )) as ArrayRef;
8390        assert_eq!(*expected, *actual);
8391
8392        // FixedSizeList<T>[1] => FixedSizeList<FixdSizedList<U>[1]>[1]
8393        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
8394            [(Some([Some(5)]))],
8395            1,
8396        )) as ArrayRef;
8397        let to_field_inner = Arc::new(Field::new_list_field(DataType::Float32, false));
8398        let to_field = Arc::new(Field::new(
8399            "dummy",
8400            DataType::FixedSizeList(to_field_inner.clone(), 1),
8401            false,
8402        ));
8403        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
8404        let expected = Arc::new(FixedSizeListArray::new(
8405            to_field.clone(),
8406            1,
8407            Arc::new(FixedSizeListArray::new(
8408                to_field_inner.clone(),
8409                1,
8410                Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
8411                None,
8412            )) as ArrayRef,
8413            None,
8414        )) as ArrayRef;
8415        assert_eq!(*expected, *actual);
8416
8417        // T => FixedSizeList<T>[1] (non-nullable)
8418        let field = Arc::new(Field::new("dummy", DataType::Float32, false));
8419        let from_array = Arc::new(Int8Array::from(vec![Some(5)])) as ArrayRef;
8420        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
8421        let actual = casted_array.as_fixed_size_list();
8422        let expected = Arc::new(FixedSizeListArray::new(
8423            field.clone(),
8424            1,
8425            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
8426            None,
8427        )) as ArrayRef;
8428        assert_eq!(expected.as_ref(), actual);
8429
8430        // T => FixedSizeList<T>[1] (nullable)
8431        let field = Arc::new(Field::new("nullable", DataType::Float32, true));
8432        let from_array = Arc::new(Int8Array::from(vec![None])) as ArrayRef;
8433        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
8434        let actual = casted_array.as_fixed_size_list();
8435        let expected = Arc::new(FixedSizeListArray::new(
8436            field.clone(),
8437            1,
8438            Arc::new(Float32Array::from(vec![None])) as ArrayRef,
8439            None,
8440        )) as ArrayRef;
8441        assert_eq!(expected.as_ref(), actual);
8442    }
8443
8444    #[test]
8445    fn test_cast_list_containers() {
8446        // large-list to list
8447        let array = Arc::new(make_large_list_array()) as ArrayRef;
8448        let list_array = cast(
8449            &array,
8450            &DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
8451        )
8452        .unwrap();
8453        let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
8454        let expected = array.as_any().downcast_ref::<LargeListArray>().unwrap();
8455
8456        assert_eq!(&expected.value(0), &actual.value(0));
8457        assert_eq!(&expected.value(1), &actual.value(1));
8458        assert_eq!(&expected.value(2), &actual.value(2));
8459
8460        // list to large-list
8461        let array = Arc::new(make_list_array()) as ArrayRef;
8462        let large_list_array = cast(
8463            &array,
8464            &DataType::LargeList(Arc::new(Field::new("", DataType::Int32, false))),
8465        )
8466        .unwrap();
8467        let actual = large_list_array
8468            .as_any()
8469            .downcast_ref::<LargeListArray>()
8470            .unwrap();
8471        let expected = array.as_any().downcast_ref::<ListArray>().unwrap();
8472
8473        assert_eq!(&expected.value(0), &actual.value(0));
8474        assert_eq!(&expected.value(1), &actual.value(1));
8475        assert_eq!(&expected.value(2), &actual.value(2));
8476    }
8477
8478    #[test]
8479    fn test_cast_list_to_fsl() {
8480        // There four noteworthy cases we should handle:
8481        // 1. No nulls
8482        // 2. Nulls that are always empty
8483        // 3. Nulls that have varying lengths
8484        // 4. Nulls that are correctly sized (same as target list size)
8485
8486        // Non-null case
8487        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
8488        let values = vec![
8489            Some(vec![Some(1), Some(2), Some(3)]),
8490            Some(vec![Some(4), Some(5), Some(6)]),
8491        ];
8492        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
8493            values.clone(),
8494        )) as ArrayRef;
8495        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8496            values, 3,
8497        )) as ArrayRef;
8498        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
8499        assert_eq!(expected.as_ref(), actual.as_ref());
8500
8501        // Null cases
8502        // Array is [[1, 2, 3], null, [4, 5, 6], null]
8503        let cases = [
8504            (
8505                // Zero-length nulls
8506                vec![1, 2, 3, 4, 5, 6],
8507                vec![3, 0, 3, 0],
8508            ),
8509            (
8510                // Varying-length nulls
8511                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
8512                vec![3, 2, 3, 1],
8513            ),
8514            (
8515                // Correctly-sized nulls
8516                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
8517                vec![3, 3, 3, 3],
8518            ),
8519            (
8520                // Mixed nulls
8521                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
8522                vec![3, 0, 3, 3],
8523            ),
8524        ];
8525        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
8526
8527        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8528            vec![
8529                Some(vec![Some(1), Some(2), Some(3)]),
8530                None,
8531                Some(vec![Some(4), Some(5), Some(6)]),
8532                None,
8533            ],
8534            3,
8535        )) as ArrayRef;
8536
8537        for (values, lengths) in cases.iter() {
8538            let array = Arc::new(ListArray::new(
8539                field.clone(),
8540                OffsetBuffer::from_lengths(lengths.clone()),
8541                Arc::new(Int32Array::from(values.clone())),
8542                Some(null_buffer.clone()),
8543            )) as ArrayRef;
8544            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
8545            assert_eq!(expected.as_ref(), actual.as_ref());
8546        }
8547    }
8548
8549    #[test]
8550    fn test_cast_list_to_fsl_safety() {
8551        let values = vec![
8552            Some(vec![Some(1), Some(2), Some(3)]),
8553            Some(vec![Some(4), Some(5)]),
8554            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
8555            Some(vec![Some(3), Some(4), Some(5)]),
8556        ];
8557        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
8558            values.clone(),
8559        )) as ArrayRef;
8560
8561        let res = cast_with_options(
8562            array.as_ref(),
8563            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
8564            &CastOptions {
8565                safe: false,
8566                ..Default::default()
8567            },
8568        );
8569        assert!(res.is_err());
8570        assert!(
8571            format!("{res:?}")
8572                .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
8573        );
8574
8575        // When safe=true (default), the cast will fill nulls for lists that are
8576        // too short and truncate lists that are too long.
8577        let res = cast(
8578            array.as_ref(),
8579            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
8580        )
8581        .unwrap();
8582        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8583            vec![
8584                Some(vec![Some(1), Some(2), Some(3)]),
8585                None, // Too short -> replaced with null
8586                None, // Too long -> replaced with null
8587                Some(vec![Some(3), Some(4), Some(5)]),
8588            ],
8589            3,
8590        )) as ArrayRef;
8591        assert_eq!(expected.as_ref(), res.as_ref());
8592
8593        // The safe option is false and the source array contains a null list.
8594        // issue: https://github.com/apache/arrow-rs/issues/5642
8595        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
8596            Some(vec![Some(1), Some(2), Some(3)]),
8597            None,
8598        ])) as ArrayRef;
8599        let res = cast_with_options(
8600            array.as_ref(),
8601            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
8602            &CastOptions {
8603                safe: false,
8604                ..Default::default()
8605            },
8606        )
8607        .unwrap();
8608        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8609            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
8610            3,
8611        )) as ArrayRef;
8612        assert_eq!(expected.as_ref(), res.as_ref());
8613    }
8614
8615    #[test]
8616    fn test_cast_large_list_to_fsl() {
8617        let values = vec![Some(vec![Some(1), Some(2)]), Some(vec![Some(3), Some(4)])];
8618        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
8619            values.clone(),
8620        )) as ArrayRef;
8621        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8622            values, 2,
8623        )) as ArrayRef;
8624        let actual = cast(
8625            array.as_ref(),
8626            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 2),
8627        )
8628        .unwrap();
8629        assert_eq!(expected.as_ref(), actual.as_ref());
8630    }
8631
8632    #[test]
8633    fn test_cast_list_to_fsl_subcast() {
8634        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
8635            vec![
8636                Some(vec![Some(1), Some(2)]),
8637                Some(vec![Some(3), Some(i32::MAX)]),
8638            ],
8639        )) as ArrayRef;
8640        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
8641            vec![
8642                Some(vec![Some(1), Some(2)]),
8643                Some(vec![Some(3), Some(i32::MAX as i64)]),
8644            ],
8645            2,
8646        )) as ArrayRef;
8647        let actual = cast(
8648            array.as_ref(),
8649            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 2),
8650        )
8651        .unwrap();
8652        assert_eq!(expected.as_ref(), actual.as_ref());
8653
8654        let res = cast_with_options(
8655            array.as_ref(),
8656            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int16, true)), 2),
8657            &CastOptions {
8658                safe: false,
8659                ..Default::default()
8660            },
8661        );
8662        assert!(res.is_err());
8663        assert!(format!("{res:?}").contains("Can't cast value 2147483647 to type Int16"));
8664    }
8665
8666    #[test]
8667    fn test_cast_list_to_fsl_empty() {
8668        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
8669        let array = new_empty_array(&DataType::List(field.clone()));
8670
8671        let target_type = DataType::FixedSizeList(field.clone(), 3);
8672        let expected = new_empty_array(&target_type);
8673
8674        let actual = cast(array.as_ref(), &target_type).unwrap();
8675        assert_eq!(expected.as_ref(), actual.as_ref());
8676    }
8677
8678    fn make_list_array() -> ListArray {
8679        // Construct a value array
8680        let value_data = ArrayData::builder(DataType::Int32)
8681            .len(8)
8682            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8683            .build()
8684            .unwrap();
8685
8686        // Construct a buffer for value offsets, for the nested array:
8687        //  [[0, 1, 2], [3, 4, 5], [6, 7]]
8688        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
8689
8690        // Construct a list array from the above two
8691        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
8692        let list_data = ArrayData::builder(list_data_type)
8693            .len(3)
8694            .add_buffer(value_offsets)
8695            .add_child_data(value_data)
8696            .build()
8697            .unwrap();
8698        ListArray::from(list_data)
8699    }
8700
8701    fn make_large_list_array() -> LargeListArray {
8702        // Construct a value array
8703        let value_data = ArrayData::builder(DataType::Int32)
8704            .len(8)
8705            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8706            .build()
8707            .unwrap();
8708
8709        // Construct a buffer for value offsets, for the nested array:
8710        //  [[0, 1, 2], [3, 4, 5], [6, 7]]
8711        let value_offsets = Buffer::from_slice_ref([0i64, 3, 6, 8]);
8712
8713        // Construct a list array from the above two
8714        let list_data_type =
8715            DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8716        let list_data = ArrayData::builder(list_data_type)
8717            .len(3)
8718            .add_buffer(value_offsets)
8719            .add_child_data(value_data)
8720            .build()
8721            .unwrap();
8722        LargeListArray::from(list_data)
8723    }
8724
8725    fn make_fixed_size_list_array() -> FixedSizeListArray {
8726        // Construct a value array
8727        let value_data = ArrayData::builder(DataType::Int32)
8728            .len(8)
8729            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8730            .build()
8731            .unwrap();
8732
8733        let list_data_type =
8734            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
8735        let list_data = ArrayData::builder(list_data_type)
8736            .len(2)
8737            .add_child_data(value_data)
8738            .build()
8739            .unwrap();
8740        FixedSizeListArray::from(list_data)
8741    }
8742
8743    fn make_fixed_size_list_array_for_large_list() -> FixedSizeListArray {
8744        // Construct a value array
8745        let value_data = ArrayData::builder(DataType::Int64)
8746            .len(8)
8747            .add_buffer(Buffer::from_slice_ref([0i64, 1, 2, 3, 4, 5, 6, 7]))
8748            .build()
8749            .unwrap();
8750
8751        let list_data_type =
8752            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 4);
8753        let list_data = ArrayData::builder(list_data_type)
8754            .len(2)
8755            .add_child_data(value_data)
8756            .build()
8757            .unwrap();
8758        FixedSizeListArray::from(list_data)
8759    }
8760
8761    #[test]
8762    fn test_cast_map_dont_allow_change_of_order() {
8763        let string_builder = StringBuilder::new();
8764        let value_builder = StringBuilder::new();
8765        let mut builder = MapBuilder::new(
8766            Some(MapFieldNames {
8767                entry: "entries".to_string(),
8768                key: "key".to_string(),
8769                value: "value".to_string(),
8770            }),
8771            string_builder,
8772            value_builder,
8773        );
8774
8775        builder.keys().append_value("0");
8776        builder.values().append_value("test_val_1");
8777        builder.append(true).unwrap();
8778        builder.keys().append_value("1");
8779        builder.values().append_value("test_val_2");
8780        builder.append(true).unwrap();
8781
8782        // map builder returns unsorted map by default
8783        let array = builder.finish();
8784
8785        let new_ordered = true;
8786        let new_type = DataType::Map(
8787            Arc::new(Field::new(
8788                "entries",
8789                DataType::Struct(
8790                    vec![
8791                        Field::new("key", DataType::Utf8, false),
8792                        Field::new("value", DataType::Utf8, false),
8793                    ]
8794                    .into(),
8795                ),
8796                false,
8797            )),
8798            new_ordered,
8799        );
8800
8801        let new_array_result = cast(&array, &new_type.clone());
8802        assert!(!can_cast_types(array.data_type(), &new_type));
8803        let Err(ArrowError::CastError(t)) = new_array_result else {
8804            panic!();
8805        };
8806        assert_eq!(
8807            t,
8808            r#"Casting from Map("entries": Struct("key": Utf8, "value": nullable Utf8), unsorted) to Map("entries": Struct("key": Utf8, "value": Utf8), sorted) not supported"#
8809        );
8810    }
8811
8812    #[test]
8813    fn test_cast_map_dont_allow_when_container_cant_cast() {
8814        let string_builder = StringBuilder::new();
8815        let value_builder = IntervalDayTimeArray::builder(2);
8816        let mut builder = MapBuilder::new(
8817            Some(MapFieldNames {
8818                entry: "entries".to_string(),
8819                key: "key".to_string(),
8820                value: "value".to_string(),
8821            }),
8822            string_builder,
8823            value_builder,
8824        );
8825
8826        builder.keys().append_value("0");
8827        builder.values().append_value(IntervalDayTime::new(1, 1));
8828        builder.append(true).unwrap();
8829        builder.keys().append_value("1");
8830        builder.values().append_value(IntervalDayTime::new(2, 2));
8831        builder.append(true).unwrap();
8832
8833        // map builder returns unsorted map by default
8834        let array = builder.finish();
8835
8836        let new_ordered = true;
8837        let new_type = DataType::Map(
8838            Arc::new(Field::new(
8839                "entries",
8840                DataType::Struct(
8841                    vec![
8842                        Field::new("key", DataType::Utf8, false),
8843                        Field::new("value", DataType::Duration(TimeUnit::Second), false),
8844                    ]
8845                    .into(),
8846                ),
8847                false,
8848            )),
8849            new_ordered,
8850        );
8851
8852        let new_array_result = cast(&array, &new_type.clone());
8853        assert!(!can_cast_types(array.data_type(), &new_type));
8854        let Err(ArrowError::CastError(t)) = new_array_result else {
8855            panic!();
8856        };
8857        assert_eq!(
8858            t,
8859            r#"Casting from Map("entries": Struct("key": Utf8, "value": nullable Interval(DayTime)), unsorted) to Map("entries": Struct("key": Utf8, "value": Duration(s)), sorted) not supported"#
8860        );
8861    }
8862
8863    #[test]
8864    fn test_cast_map_field_names() {
8865        let string_builder = StringBuilder::new();
8866        let value_builder = StringBuilder::new();
8867        let mut builder = MapBuilder::new(
8868            Some(MapFieldNames {
8869                entry: "entries".to_string(),
8870                key: "key".to_string(),
8871                value: "value".to_string(),
8872            }),
8873            string_builder,
8874            value_builder,
8875        );
8876
8877        builder.keys().append_value("0");
8878        builder.values().append_value("test_val_1");
8879        builder.append(true).unwrap();
8880        builder.keys().append_value("1");
8881        builder.values().append_value("test_val_2");
8882        builder.append(true).unwrap();
8883        builder.append(false).unwrap();
8884
8885        let array = builder.finish();
8886
8887        let new_type = DataType::Map(
8888            Arc::new(Field::new(
8889                "entries_new",
8890                DataType::Struct(
8891                    vec![
8892                        Field::new("key_new", DataType::Utf8, false),
8893                        Field::new("value_values", DataType::Utf8, false),
8894                    ]
8895                    .into(),
8896                ),
8897                false,
8898            )),
8899            false,
8900        );
8901
8902        assert_ne!(new_type, array.data_type().clone());
8903
8904        let new_array = cast(&array, &new_type.clone()).unwrap();
8905        assert_eq!(new_type, new_array.data_type().clone());
8906        let map_array = new_array.as_map();
8907
8908        assert_ne!(new_type, array.data_type().clone());
8909        assert_eq!(new_type, map_array.data_type().clone());
8910
8911        let key_string = map_array
8912            .keys()
8913            .as_any()
8914            .downcast_ref::<StringArray>()
8915            .unwrap()
8916            .into_iter()
8917            .flatten()
8918            .collect::<Vec<_>>();
8919        assert_eq!(&key_string, &vec!["0", "1"]);
8920
8921        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
8922        let values_string = values_string_array
8923            .as_any()
8924            .downcast_ref::<StringArray>()
8925            .unwrap()
8926            .into_iter()
8927            .flatten()
8928            .collect::<Vec<_>>();
8929        assert_eq!(&values_string, &vec!["test_val_1", "test_val_2"]);
8930
8931        assert_eq!(
8932            map_array.nulls(),
8933            Some(&NullBuffer::from(vec![true, true, false]))
8934        );
8935    }
8936
8937    #[test]
8938    fn test_cast_map_contained_values() {
8939        let string_builder = StringBuilder::new();
8940        let value_builder = Int8Builder::new();
8941        let mut builder = MapBuilder::new(
8942            Some(MapFieldNames {
8943                entry: "entries".to_string(),
8944                key: "key".to_string(),
8945                value: "value".to_string(),
8946            }),
8947            string_builder,
8948            value_builder,
8949        );
8950
8951        builder.keys().append_value("0");
8952        builder.values().append_value(44);
8953        builder.append(true).unwrap();
8954        builder.keys().append_value("1");
8955        builder.values().append_value(22);
8956        builder.append(true).unwrap();
8957
8958        let array = builder.finish();
8959
8960        let new_type = DataType::Map(
8961            Arc::new(Field::new(
8962                "entries",
8963                DataType::Struct(
8964                    vec![
8965                        Field::new("key", DataType::Utf8, false),
8966                        Field::new("value", DataType::Utf8, false),
8967                    ]
8968                    .into(),
8969                ),
8970                false,
8971            )),
8972            false,
8973        );
8974
8975        let new_array = cast(&array, &new_type.clone()).unwrap();
8976        assert_eq!(new_type, new_array.data_type().clone());
8977        let map_array = new_array.as_map();
8978
8979        assert_ne!(new_type, array.data_type().clone());
8980        assert_eq!(new_type, map_array.data_type().clone());
8981
8982        let key_string = map_array
8983            .keys()
8984            .as_any()
8985            .downcast_ref::<StringArray>()
8986            .unwrap()
8987            .into_iter()
8988            .flatten()
8989            .collect::<Vec<_>>();
8990        assert_eq!(&key_string, &vec!["0", "1"]);
8991
8992        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
8993        let values_string = values_string_array
8994            .as_any()
8995            .downcast_ref::<StringArray>()
8996            .unwrap()
8997            .into_iter()
8998            .flatten()
8999            .collect::<Vec<_>>();
9000        assert_eq!(&values_string, &vec!["44", "22"]);
9001    }
9002
9003    #[test]
9004    fn test_utf8_cast_offsets() {
9005        // test if offset of the array is taken into account during cast
9006        let str_array = StringArray::from(vec!["a", "b", "c"]);
9007        let str_array = str_array.slice(1, 2);
9008
9009        let out = cast(&str_array, &DataType::LargeUtf8).unwrap();
9010
9011        let large_str_array = out.as_any().downcast_ref::<LargeStringArray>().unwrap();
9012        let strs = large_str_array.into_iter().flatten().collect::<Vec<_>>();
9013        assert_eq!(strs, &["b", "c"])
9014    }
9015
9016    #[test]
9017    fn test_list_cast_offsets() {
9018        // test if offset of the array is taken into account during cast
9019        let array1 = make_list_array().slice(1, 2);
9020        let array2 = Arc::new(make_list_array()) as ArrayRef;
9021
9022        let dt = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
9023        let out1 = cast(&array1, &dt).unwrap();
9024        let out2 = cast(&array2, &dt).unwrap();
9025
9026        assert_eq!(&out1, &out2.slice(1, 2))
9027    }
9028
9029    #[test]
9030    fn test_list_to_string() {
9031        let str_array = StringArray::from(vec!["a", "b", "c", "d", "e", "f", "g", "h"]);
9032        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
9033        let value_data = str_array.into_data();
9034
9035        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Utf8, true)));
9036        let list_data = ArrayData::builder(list_data_type)
9037            .len(3)
9038            .add_buffer(value_offsets)
9039            .add_child_data(value_data)
9040            .build()
9041            .unwrap();
9042        let array = Arc::new(ListArray::from(list_data)) as ArrayRef;
9043
9044        let out = cast(&array, &DataType::Utf8).unwrap();
9045        let out = out
9046            .as_any()
9047            .downcast_ref::<StringArray>()
9048            .unwrap()
9049            .into_iter()
9050            .flatten()
9051            .collect::<Vec<_>>();
9052        assert_eq!(&out, &vec!["[a, b, c]", "[d, e, f]", "[g, h]"]);
9053
9054        let out = cast(&array, &DataType::LargeUtf8).unwrap();
9055        let out = out
9056            .as_any()
9057            .downcast_ref::<LargeStringArray>()
9058            .unwrap()
9059            .into_iter()
9060            .flatten()
9061            .collect::<Vec<_>>();
9062        assert_eq!(&out, &vec!["[a, b, c]", "[d, e, f]", "[g, h]"]);
9063
9064        let array = Arc::new(make_list_array()) as ArrayRef;
9065        let out = cast(&array, &DataType::Utf8).unwrap();
9066        let out = out
9067            .as_any()
9068            .downcast_ref::<StringArray>()
9069            .unwrap()
9070            .into_iter()
9071            .flatten()
9072            .collect::<Vec<_>>();
9073        assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
9074
9075        let array = Arc::new(make_large_list_array()) as ArrayRef;
9076        let out = cast(&array, &DataType::LargeUtf8).unwrap();
9077        let out = out
9078            .as_any()
9079            .downcast_ref::<LargeStringArray>()
9080            .unwrap()
9081            .into_iter()
9082            .flatten()
9083            .collect::<Vec<_>>();
9084        assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
9085    }
9086
9087    #[test]
9088    fn test_cast_f64_to_decimal128() {
9089        // to reproduce https://github.com/apache/arrow-rs/issues/2997
9090
9091        let decimal_type = DataType::Decimal128(18, 2);
9092        let array = Float64Array::from(vec![
9093            Some(0.0699999999),
9094            Some(0.0659999999),
9095            Some(0.0650000000),
9096            Some(0.0649999999),
9097        ]);
9098        let array = Arc::new(array) as ArrayRef;
9099        generate_cast_test_case!(
9100            &array,
9101            Decimal128Array,
9102            &decimal_type,
9103            vec![
9104                Some(7_i128), // round up
9105                Some(7_i128), // round up
9106                Some(7_i128), // round up
9107                Some(6_i128), // round down
9108            ]
9109        );
9110
9111        let decimal_type = DataType::Decimal128(18, 3);
9112        let array = Float64Array::from(vec![
9113            Some(0.0699999999),
9114            Some(0.0659999999),
9115            Some(0.0650000000),
9116            Some(0.0649999999),
9117        ]);
9118        let array = Arc::new(array) as ArrayRef;
9119        generate_cast_test_case!(
9120            &array,
9121            Decimal128Array,
9122            &decimal_type,
9123            vec![
9124                Some(70_i128), // round up
9125                Some(66_i128), // round up
9126                Some(65_i128), // round down
9127                Some(65_i128), // round up
9128            ]
9129        );
9130    }
9131
9132    #[test]
9133    fn test_cast_numeric_to_decimal128_overflow() {
9134        let array = Int64Array::from(vec![i64::MAX]);
9135        let array = Arc::new(array) as ArrayRef;
9136        let casted_array = cast_with_options(
9137            &array,
9138            &DataType::Decimal128(38, 30),
9139            &CastOptions {
9140                safe: true,
9141                format_options: FormatOptions::default(),
9142            },
9143        );
9144        assert!(casted_array.is_ok());
9145        assert!(casted_array.unwrap().is_null(0));
9146
9147        let casted_array = cast_with_options(
9148            &array,
9149            &DataType::Decimal128(38, 30),
9150            &CastOptions {
9151                safe: false,
9152                format_options: FormatOptions::default(),
9153            },
9154        );
9155        assert!(casted_array.is_err());
9156    }
9157
9158    #[test]
9159    fn test_cast_numeric_to_decimal256_overflow() {
9160        let array = Int64Array::from(vec![i64::MAX]);
9161        let array = Arc::new(array) as ArrayRef;
9162        let casted_array = cast_with_options(
9163            &array,
9164            &DataType::Decimal256(76, 76),
9165            &CastOptions {
9166                safe: true,
9167                format_options: FormatOptions::default(),
9168            },
9169        );
9170        assert!(casted_array.is_ok());
9171        assert!(casted_array.unwrap().is_null(0));
9172
9173        let casted_array = cast_with_options(
9174            &array,
9175            &DataType::Decimal256(76, 76),
9176            &CastOptions {
9177                safe: false,
9178                format_options: FormatOptions::default(),
9179            },
9180        );
9181        assert!(casted_array.is_err());
9182    }
9183
9184    #[test]
9185    fn test_cast_floating_point_to_decimal128_precision_overflow() {
9186        let array = Float64Array::from(vec![1.1]);
9187        let array = Arc::new(array) as ArrayRef;
9188        let casted_array = cast_with_options(
9189            &array,
9190            &DataType::Decimal128(2, 2),
9191            &CastOptions {
9192                safe: true,
9193                format_options: FormatOptions::default(),
9194            },
9195        );
9196        assert!(casted_array.is_ok());
9197        assert!(casted_array.unwrap().is_null(0));
9198
9199        let casted_array = cast_with_options(
9200            &array,
9201            &DataType::Decimal128(2, 2),
9202            &CastOptions {
9203                safe: false,
9204                format_options: FormatOptions::default(),
9205            },
9206        );
9207        let err = casted_array.unwrap_err().to_string();
9208        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
9209        assert!(
9210            err.contains(expected_error),
9211            "did not find expected error '{expected_error}' in actual error '{err}'"
9212        );
9213    }
9214
9215    #[test]
9216    fn test_cast_floating_point_to_decimal256_precision_overflow() {
9217        let array = Float64Array::from(vec![1.1]);
9218        let array = Arc::new(array) as ArrayRef;
9219        let casted_array = cast_with_options(
9220            &array,
9221            &DataType::Decimal256(2, 2),
9222            &CastOptions {
9223                safe: true,
9224                format_options: FormatOptions::default(),
9225            },
9226        );
9227        assert!(casted_array.is_ok());
9228        assert!(casted_array.unwrap().is_null(0));
9229
9230        let casted_array = cast_with_options(
9231            &array,
9232            &DataType::Decimal256(2, 2),
9233            &CastOptions {
9234                safe: false,
9235                format_options: FormatOptions::default(),
9236            },
9237        );
9238        let err = casted_array.unwrap_err().to_string();
9239        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
9240        assert_eq!(err, expected_error);
9241    }
9242
9243    #[test]
9244    fn test_cast_floating_point_to_decimal128_overflow() {
9245        let array = Float64Array::from(vec![f64::MAX]);
9246        let array = Arc::new(array) as ArrayRef;
9247        let casted_array = cast_with_options(
9248            &array,
9249            &DataType::Decimal128(38, 30),
9250            &CastOptions {
9251                safe: true,
9252                format_options: FormatOptions::default(),
9253            },
9254        );
9255        assert!(casted_array.is_ok());
9256        assert!(casted_array.unwrap().is_null(0));
9257
9258        let casted_array = cast_with_options(
9259            &array,
9260            &DataType::Decimal128(38, 30),
9261            &CastOptions {
9262                safe: false,
9263                format_options: FormatOptions::default(),
9264            },
9265        );
9266        let err = casted_array.unwrap_err().to_string();
9267        let expected_error = "Cast error: Cannot cast to Decimal128(38, 30)";
9268        assert!(
9269            err.contains(expected_error),
9270            "did not find expected error '{expected_error}' in actual error '{err}'"
9271        );
9272    }
9273
9274    #[test]
9275    fn test_cast_floating_point_to_decimal256_overflow() {
9276        let array = Float64Array::from(vec![f64::MAX]);
9277        let array = Arc::new(array) as ArrayRef;
9278        let casted_array = cast_with_options(
9279            &array,
9280            &DataType::Decimal256(76, 50),
9281            &CastOptions {
9282                safe: true,
9283                format_options: FormatOptions::default(),
9284            },
9285        );
9286        assert!(casted_array.is_ok());
9287        assert!(casted_array.unwrap().is_null(0));
9288
9289        let casted_array = cast_with_options(
9290            &array,
9291            &DataType::Decimal256(76, 50),
9292            &CastOptions {
9293                safe: false,
9294                format_options: FormatOptions::default(),
9295            },
9296        );
9297        let err = casted_array.unwrap_err().to_string();
9298        let expected_error = "Cast error: Cannot cast to Decimal256(76, 50)";
9299        assert!(
9300            err.contains(expected_error),
9301            "did not find expected error '{expected_error}' in actual error '{err}'"
9302        );
9303    }
9304    #[test]
9305    fn test_cast_decimal256_to_f64_no_overflow() {
9306        // Test casting i256::MAX: should produce a large finite positive value
9307        let array = vec![Some(i256::MAX)];
9308        let array = create_decimal256_array(array, 76, 2).unwrap();
9309        let array = Arc::new(array) as ArrayRef;
9310
9311        let result = cast(&array, &DataType::Float64).unwrap();
9312        let result = result.as_primitive::<Float64Type>();
9313        assert!(result.value(0).is_finite());
9314        assert!(result.value(0) > 0.0); // Positive result
9315
9316        // Test casting i256::MIN: should produce a large finite negative value
9317        let array = vec![Some(i256::MIN)];
9318        let array = create_decimal256_array(array, 76, 2).unwrap();
9319        let array = Arc::new(array) as ArrayRef;
9320
9321        let result = cast(&array, &DataType::Float64).unwrap();
9322        let result = result.as_primitive::<Float64Type>();
9323        assert!(result.value(0).is_finite());
9324        assert!(result.value(0) < 0.0); // Negative result
9325    }
9326
9327    #[test]
9328    fn test_cast_decimal128_to_decimal128_negative_scale() {
9329        let input_type = DataType::Decimal128(20, 0);
9330        let output_type = DataType::Decimal128(20, -1);
9331        assert!(can_cast_types(&input_type, &output_type));
9332        let array = vec![Some(1123450), Some(2123455), Some(3123456), None];
9333        let input_decimal_array = create_decimal128_array(array, 20, 0).unwrap();
9334        let array = Arc::new(input_decimal_array) as ArrayRef;
9335        generate_cast_test_case!(
9336            &array,
9337            Decimal128Array,
9338            &output_type,
9339            vec![
9340                Some(112345_i128),
9341                Some(212346_i128),
9342                Some(312346_i128),
9343                None
9344            ]
9345        );
9346
9347        let casted_array = cast(&array, &output_type).unwrap();
9348        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9349
9350        assert_eq!("1123450", decimal_arr.value_as_string(0));
9351        assert_eq!("2123460", decimal_arr.value_as_string(1));
9352        assert_eq!("3123460", decimal_arr.value_as_string(2));
9353    }
9354
9355    #[test]
9356    fn decimal128_min_max_to_f64() {
9357        // Ensure Decimal128 i128::MIN/MAX round-trip cast
9358        let min128 = i128::MIN;
9359        let max128 = i128::MAX;
9360        assert_eq!(min128 as f64, min128 as f64);
9361        assert_eq!(max128 as f64, max128 as f64);
9362    }
9363
9364    #[test]
9365    fn test_cast_numeric_to_decimal128_negative() {
9366        let decimal_type = DataType::Decimal128(38, -1);
9367        let array = Arc::new(Int32Array::from(vec![
9368            Some(1123456),
9369            Some(2123456),
9370            Some(3123456),
9371        ])) as ArrayRef;
9372
9373        let casted_array = cast(&array, &decimal_type).unwrap();
9374        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9375
9376        assert_eq!("1123450", decimal_arr.value_as_string(0));
9377        assert_eq!("2123450", decimal_arr.value_as_string(1));
9378        assert_eq!("3123450", decimal_arr.value_as_string(2));
9379
9380        let array = Arc::new(Float32Array::from(vec![
9381            Some(1123.456),
9382            Some(2123.456),
9383            Some(3123.456),
9384        ])) as ArrayRef;
9385
9386        let casted_array = cast(&array, &decimal_type).unwrap();
9387        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9388
9389        assert_eq!("1120", decimal_arr.value_as_string(0));
9390        assert_eq!("2120", decimal_arr.value_as_string(1));
9391        assert_eq!("3120", decimal_arr.value_as_string(2));
9392    }
9393
9394    #[test]
9395    fn test_cast_decimal128_to_decimal128_negative() {
9396        let input_type = DataType::Decimal128(10, -1);
9397        let output_type = DataType::Decimal128(10, -2);
9398        assert!(can_cast_types(&input_type, &output_type));
9399        let array = vec![Some(123)];
9400        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
9401        let array = Arc::new(input_decimal_array) as ArrayRef;
9402        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(12_i128),]);
9403
9404        let casted_array = cast(&array, &output_type).unwrap();
9405        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9406
9407        assert_eq!("1200", decimal_arr.value_as_string(0));
9408
9409        let array = vec![Some(125)];
9410        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
9411        let array = Arc::new(input_decimal_array) as ArrayRef;
9412        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(13_i128),]);
9413
9414        let casted_array = cast(&array, &output_type).unwrap();
9415        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9416
9417        assert_eq!("1300", decimal_arr.value_as_string(0));
9418    }
9419
9420    #[test]
9421    fn test_cast_decimal128_to_decimal256_negative() {
9422        let input_type = DataType::Decimal128(10, 3);
9423        let output_type = DataType::Decimal256(10, 5);
9424        assert!(can_cast_types(&input_type, &output_type));
9425        let array = vec![Some(123456), Some(-123456)];
9426        let input_decimal_array = create_decimal128_array(array, 10, 3).unwrap();
9427        let array = Arc::new(input_decimal_array) as ArrayRef;
9428
9429        let hundred = i256::from_i128(100);
9430        generate_cast_test_case!(
9431            &array,
9432            Decimal256Array,
9433            &output_type,
9434            vec![
9435                Some(i256::from_i128(123456).mul_wrapping(hundred)),
9436                Some(i256::from_i128(-123456).mul_wrapping(hundred))
9437            ]
9438        );
9439    }
9440
9441    #[test]
9442    fn test_parse_string_to_decimal() {
9443        assert_eq!(
9444            Decimal128Type::format_decimal(
9445                parse_string_to_decimal_native::<Decimal128Type>("123.45", 2).unwrap(),
9446                38,
9447                2,
9448            ),
9449            "123.45"
9450        );
9451        assert_eq!(
9452            Decimal128Type::format_decimal(
9453                parse_string_to_decimal_native::<Decimal128Type>("12345", 2).unwrap(),
9454                38,
9455                2,
9456            ),
9457            "12345.00"
9458        );
9459        assert_eq!(
9460            Decimal128Type::format_decimal(
9461                parse_string_to_decimal_native::<Decimal128Type>("0.12345", 2).unwrap(),
9462                38,
9463                2,
9464            ),
9465            "0.12"
9466        );
9467        assert_eq!(
9468            Decimal128Type::format_decimal(
9469                parse_string_to_decimal_native::<Decimal128Type>(".12345", 2).unwrap(),
9470                38,
9471                2,
9472            ),
9473            "0.12"
9474        );
9475        assert_eq!(
9476            Decimal128Type::format_decimal(
9477                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
9478                38,
9479                2,
9480            ),
9481            "0.13"
9482        );
9483        assert_eq!(
9484            Decimal128Type::format_decimal(
9485                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
9486                38,
9487                2,
9488            ),
9489            "0.13"
9490        );
9491
9492        assert_eq!(
9493            Decimal256Type::format_decimal(
9494                parse_string_to_decimal_native::<Decimal256Type>("123.45", 3).unwrap(),
9495                38,
9496                3,
9497            ),
9498            "123.450"
9499        );
9500        assert_eq!(
9501            Decimal256Type::format_decimal(
9502                parse_string_to_decimal_native::<Decimal256Type>("12345", 3).unwrap(),
9503                38,
9504                3,
9505            ),
9506            "12345.000"
9507        );
9508        assert_eq!(
9509            Decimal256Type::format_decimal(
9510                parse_string_to_decimal_native::<Decimal256Type>("0.12345", 3).unwrap(),
9511                38,
9512                3,
9513            ),
9514            "0.123"
9515        );
9516        assert_eq!(
9517            Decimal256Type::format_decimal(
9518                parse_string_to_decimal_native::<Decimal256Type>(".12345", 3).unwrap(),
9519                38,
9520                3,
9521            ),
9522            "0.123"
9523        );
9524        assert_eq!(
9525            Decimal256Type::format_decimal(
9526                parse_string_to_decimal_native::<Decimal256Type>(".1265", 3).unwrap(),
9527                38,
9528                3,
9529            ),
9530            "0.127"
9531        );
9532    }
9533
9534    fn test_cast_string_to_decimal(array: ArrayRef) {
9535        // Decimal128
9536        let output_type = DataType::Decimal128(38, 2);
9537        assert!(can_cast_types(array.data_type(), &output_type));
9538
9539        let casted_array = cast(&array, &output_type).unwrap();
9540        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9541
9542        assert_eq!("123.45", decimal_arr.value_as_string(0));
9543        assert_eq!("1.23", decimal_arr.value_as_string(1));
9544        assert_eq!("0.12", decimal_arr.value_as_string(2));
9545        assert_eq!("0.13", decimal_arr.value_as_string(3));
9546        assert_eq!("1.26", decimal_arr.value_as_string(4));
9547        assert_eq!("12345.00", decimal_arr.value_as_string(5));
9548        assert_eq!("12345.00", decimal_arr.value_as_string(6));
9549        assert_eq!("0.12", decimal_arr.value_as_string(7));
9550        assert_eq!("12.23", decimal_arr.value_as_string(8));
9551        assert!(decimal_arr.is_null(9));
9552        assert_eq!("0.00", decimal_arr.value_as_string(10));
9553        assert_eq!("0.00", decimal_arr.value_as_string(11));
9554        assert!(decimal_arr.is_null(12));
9555        assert_eq!("-1.23", decimal_arr.value_as_string(13));
9556        assert_eq!("-1.24", decimal_arr.value_as_string(14));
9557        assert_eq!("0.00", decimal_arr.value_as_string(15));
9558        assert_eq!("-123.00", decimal_arr.value_as_string(16));
9559        assert_eq!("-123.23", decimal_arr.value_as_string(17));
9560        assert_eq!("-0.12", decimal_arr.value_as_string(18));
9561        assert_eq!("1.23", decimal_arr.value_as_string(19));
9562        assert_eq!("1.24", decimal_arr.value_as_string(20));
9563        assert_eq!("0.00", decimal_arr.value_as_string(21));
9564        assert_eq!("123.00", decimal_arr.value_as_string(22));
9565        assert_eq!("123.23", decimal_arr.value_as_string(23));
9566        assert_eq!("0.12", decimal_arr.value_as_string(24));
9567        assert!(decimal_arr.is_null(25));
9568        assert!(decimal_arr.is_null(26));
9569        assert!(decimal_arr.is_null(27));
9570        assert_eq!("0.00", decimal_arr.value_as_string(28));
9571        assert_eq!("0.00", decimal_arr.value_as_string(29));
9572        assert_eq!("12345.00", decimal_arr.value_as_string(30));
9573        assert_eq!(decimal_arr.len(), 31);
9574
9575        // Decimal256
9576        let output_type = DataType::Decimal256(76, 3);
9577        assert!(can_cast_types(array.data_type(), &output_type));
9578
9579        let casted_array = cast(&array, &output_type).unwrap();
9580        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
9581
9582        assert_eq!("123.450", decimal_arr.value_as_string(0));
9583        assert_eq!("1.235", decimal_arr.value_as_string(1));
9584        assert_eq!("0.123", decimal_arr.value_as_string(2));
9585        assert_eq!("0.127", decimal_arr.value_as_string(3));
9586        assert_eq!("1.263", decimal_arr.value_as_string(4));
9587        assert_eq!("12345.000", decimal_arr.value_as_string(5));
9588        assert_eq!("12345.000", decimal_arr.value_as_string(6));
9589        assert_eq!("0.123", decimal_arr.value_as_string(7));
9590        assert_eq!("12.234", decimal_arr.value_as_string(8));
9591        assert!(decimal_arr.is_null(9));
9592        assert_eq!("0.000", decimal_arr.value_as_string(10));
9593        assert_eq!("0.000", decimal_arr.value_as_string(11));
9594        assert!(decimal_arr.is_null(12));
9595        assert_eq!("-1.235", decimal_arr.value_as_string(13));
9596        assert_eq!("-1.236", decimal_arr.value_as_string(14));
9597        assert_eq!("0.000", decimal_arr.value_as_string(15));
9598        assert_eq!("-123.000", decimal_arr.value_as_string(16));
9599        assert_eq!("-123.234", decimal_arr.value_as_string(17));
9600        assert_eq!("-0.123", decimal_arr.value_as_string(18));
9601        assert_eq!("1.235", decimal_arr.value_as_string(19));
9602        assert_eq!("1.236", decimal_arr.value_as_string(20));
9603        assert_eq!("0.000", decimal_arr.value_as_string(21));
9604        assert_eq!("123.000", decimal_arr.value_as_string(22));
9605        assert_eq!("123.234", decimal_arr.value_as_string(23));
9606        assert_eq!("0.123", decimal_arr.value_as_string(24));
9607        assert!(decimal_arr.is_null(25));
9608        assert!(decimal_arr.is_null(26));
9609        assert!(decimal_arr.is_null(27));
9610        assert_eq!("0.000", decimal_arr.value_as_string(28));
9611        assert_eq!("0.000", decimal_arr.value_as_string(29));
9612        assert_eq!("12345.000", decimal_arr.value_as_string(30));
9613        assert_eq!(decimal_arr.len(), 31);
9614    }
9615
9616    #[test]
9617    fn test_cast_utf8_to_decimal() {
9618        let str_array = StringArray::from(vec![
9619            Some("123.45"),
9620            Some("1.2345"),
9621            Some("0.12345"),
9622            Some("0.1267"),
9623            Some("1.263"),
9624            Some("12345.0"),
9625            Some("12345"),
9626            Some("000.123"),
9627            Some("12.234000"),
9628            None,
9629            Some(""),
9630            Some(" "),
9631            None,
9632            Some("-1.23499999"),
9633            Some("-1.23599999"),
9634            Some("-0.00001"),
9635            Some("-123"),
9636            Some("-123.234000"),
9637            Some("-000.123"),
9638            Some("+1.23499999"),
9639            Some("+1.23599999"),
9640            Some("+0.00001"),
9641            Some("+123"),
9642            Some("+123.234000"),
9643            Some("+000.123"),
9644            Some("1.-23499999"),
9645            Some("-1.-23499999"),
9646            Some("--1.23499999"),
9647            Some("0"),
9648            Some("000.000"),
9649            Some("0000000000000000012345.000"),
9650        ]);
9651        let array = Arc::new(str_array) as ArrayRef;
9652
9653        test_cast_string_to_decimal(array);
9654
9655        let test_cases = [
9656            (None, None),
9657            // (Some(""), None),
9658            // (Some("   "), None),
9659            (Some("0"), Some("0")),
9660            (Some("000.000"), Some("0")),
9661            (Some("12345"), Some("12345")),
9662            (Some("000000000000000000000000000012345"), Some("12345")),
9663            (Some("-123"), Some("-123")),
9664            (Some("+123"), Some("123")),
9665        ];
9666        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
9667        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
9668
9669        let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
9670        test_cast_string_to_decimal_scale_zero(array, &expected);
9671    }
9672
9673    #[test]
9674    fn test_cast_large_utf8_to_decimal() {
9675        let str_array = LargeStringArray::from(vec![
9676            Some("123.45"),
9677            Some("1.2345"),
9678            Some("0.12345"),
9679            Some("0.1267"),
9680            Some("1.263"),
9681            Some("12345.0"),
9682            Some("12345"),
9683            Some("000.123"),
9684            Some("12.234000"),
9685            None,
9686            Some(""),
9687            Some(" "),
9688            None,
9689            Some("-1.23499999"),
9690            Some("-1.23599999"),
9691            Some("-0.00001"),
9692            Some("-123"),
9693            Some("-123.234000"),
9694            Some("-000.123"),
9695            Some("+1.23499999"),
9696            Some("+1.23599999"),
9697            Some("+0.00001"),
9698            Some("+123"),
9699            Some("+123.234000"),
9700            Some("+000.123"),
9701            Some("1.-23499999"),
9702            Some("-1.-23499999"),
9703            Some("--1.23499999"),
9704            Some("0"),
9705            Some("000.000"),
9706            Some("0000000000000000012345.000"),
9707        ]);
9708        let array = Arc::new(str_array) as ArrayRef;
9709
9710        test_cast_string_to_decimal(array);
9711
9712        let test_cases = [
9713            (None, None),
9714            (Some(""), None),
9715            (Some("   "), None),
9716            (Some("0"), Some("0")),
9717            (Some("000.000"), Some("0")),
9718            (Some("12345"), Some("12345")),
9719            (Some("000000000000000000000000000012345"), Some("12345")),
9720            (Some("-123"), Some("-123")),
9721            (Some("+123"), Some("123")),
9722        ];
9723        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
9724        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
9725
9726        let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
9727        test_cast_string_to_decimal_scale_zero(array, &expected);
9728    }
9729
9730    fn test_cast_string_to_decimal_scale_zero(
9731        array: ArrayRef,
9732        expected_as_string: &[Option<&str>],
9733    ) {
9734        // Decimal128
9735        let output_type = DataType::Decimal128(38, 0);
9736        assert!(can_cast_types(array.data_type(), &output_type));
9737        let casted_array = cast(&array, &output_type).unwrap();
9738        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9739        assert_decimal_array_contents(decimal_arr, expected_as_string);
9740
9741        // Decimal256
9742        let output_type = DataType::Decimal256(76, 0);
9743        assert!(can_cast_types(array.data_type(), &output_type));
9744        let casted_array = cast(&array, &output_type).unwrap();
9745        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
9746        assert_decimal_array_contents(decimal_arr, expected_as_string);
9747    }
9748
9749    fn assert_decimal_array_contents<T>(
9750        array: &PrimitiveArray<T>,
9751        expected_as_string: &[Option<&str>],
9752    ) where
9753        T: DecimalType + ArrowPrimitiveType,
9754    {
9755        assert_eq!(array.len(), expected_as_string.len());
9756        for (i, expected) in expected_as_string.iter().enumerate() {
9757            let actual = if array.is_null(i) {
9758                None
9759            } else {
9760                Some(array.value_as_string(i))
9761            };
9762            let actual = actual.as_ref().map(|s| s.as_ref());
9763            assert_eq!(*expected, actual, "Expected at position {i}");
9764        }
9765    }
9766
9767    #[test]
9768    fn test_cast_invalid_utf8_to_decimal() {
9769        let str_array = StringArray::from(vec!["4.4.5", ". 0.123"]);
9770        let array = Arc::new(str_array) as ArrayRef;
9771
9772        // Safe cast
9773        let output_type = DataType::Decimal128(38, 2);
9774        let casted_array = cast(&array, &output_type).unwrap();
9775        assert!(casted_array.is_null(0));
9776        assert!(casted_array.is_null(1));
9777
9778        let output_type = DataType::Decimal256(76, 2);
9779        let casted_array = cast(&array, &output_type).unwrap();
9780        assert!(casted_array.is_null(0));
9781        assert!(casted_array.is_null(1));
9782
9783        // Non-safe cast
9784        let output_type = DataType::Decimal128(38, 2);
9785        let str_array = StringArray::from(vec!["4.4.5"]);
9786        let array = Arc::new(str_array) as ArrayRef;
9787        let option = CastOptions {
9788            safe: false,
9789            format_options: FormatOptions::default(),
9790        };
9791        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
9792        assert!(
9793            casted_err
9794                .to_string()
9795                .contains("Cannot cast string '4.4.5' to value of Decimal128(38, 10) type")
9796        );
9797
9798        let str_array = StringArray::from(vec![". 0.123"]);
9799        let array = Arc::new(str_array) as ArrayRef;
9800        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
9801        assert!(
9802            casted_err
9803                .to_string()
9804                .contains("Cannot cast string '. 0.123' to value of Decimal128(38, 10) type")
9805        );
9806    }
9807
9808    fn test_cast_string_to_decimal128_overflow(overflow_array: ArrayRef) {
9809        let output_type = DataType::Decimal128(38, 2);
9810        let casted_array = cast(&overflow_array, &output_type).unwrap();
9811        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9812
9813        assert!(decimal_arr.is_null(0));
9814        assert!(decimal_arr.is_null(1));
9815        assert!(decimal_arr.is_null(2));
9816        assert_eq!(
9817            "999999999999999999999999999999999999.99",
9818            decimal_arr.value_as_string(3)
9819        );
9820        assert_eq!(
9821            "100000000000000000000000000000000000.00",
9822            decimal_arr.value_as_string(4)
9823        );
9824    }
9825
9826    #[test]
9827    fn test_cast_string_to_decimal128_precision_overflow() {
9828        let array = StringArray::from(vec!["1000".to_string()]);
9829        let array = Arc::new(array) as ArrayRef;
9830        let casted_array = cast_with_options(
9831            &array,
9832            &DataType::Decimal128(10, 8),
9833            &CastOptions {
9834                safe: true,
9835                format_options: FormatOptions::default(),
9836            },
9837        );
9838        assert!(casted_array.is_ok());
9839        assert!(casted_array.unwrap().is_null(0));
9840
9841        let err = cast_with_options(
9842            &array,
9843            &DataType::Decimal128(10, 8),
9844            &CastOptions {
9845                safe: false,
9846                format_options: FormatOptions::default(),
9847            },
9848        );
9849        assert_eq!(
9850            "Invalid argument error: 1000.00000000 is too large to store in a Decimal128 of precision 10. Max is 99.99999999",
9851            err.unwrap_err().to_string()
9852        );
9853    }
9854
9855    #[test]
9856    fn test_cast_utf8_to_decimal128_overflow() {
9857        let overflow_str_array = StringArray::from(vec![
9858            i128::MAX.to_string(),
9859            i128::MIN.to_string(),
9860            "99999999999999999999999999999999999999".to_string(),
9861            "999999999999999999999999999999999999.99".to_string(),
9862            "99999999999999999999999999999999999.999".to_string(),
9863        ]);
9864        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9865
9866        test_cast_string_to_decimal128_overflow(overflow_array);
9867    }
9868
9869    #[test]
9870    fn test_cast_large_utf8_to_decimal128_overflow() {
9871        let overflow_str_array = LargeStringArray::from(vec![
9872            i128::MAX.to_string(),
9873            i128::MIN.to_string(),
9874            "99999999999999999999999999999999999999".to_string(),
9875            "999999999999999999999999999999999999.99".to_string(),
9876            "99999999999999999999999999999999999.999".to_string(),
9877        ]);
9878        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9879
9880        test_cast_string_to_decimal128_overflow(overflow_array);
9881    }
9882
9883    fn test_cast_string_to_decimal256_overflow(overflow_array: ArrayRef) {
9884        let output_type = DataType::Decimal256(76, 2);
9885        let casted_array = cast(&overflow_array, &output_type).unwrap();
9886        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
9887
9888        assert_eq!(
9889            "170141183460469231731687303715884105727.00",
9890            decimal_arr.value_as_string(0)
9891        );
9892        assert_eq!(
9893            "-170141183460469231731687303715884105728.00",
9894            decimal_arr.value_as_string(1)
9895        );
9896        assert_eq!(
9897            "99999999999999999999999999999999999999.00",
9898            decimal_arr.value_as_string(2)
9899        );
9900        assert_eq!(
9901            "999999999999999999999999999999999999.99",
9902            decimal_arr.value_as_string(3)
9903        );
9904        assert_eq!(
9905            "100000000000000000000000000000000000.00",
9906            decimal_arr.value_as_string(4)
9907        );
9908        assert!(decimal_arr.is_null(5));
9909        assert!(decimal_arr.is_null(6));
9910    }
9911
9912    #[test]
9913    fn test_cast_string_to_decimal256_precision_overflow() {
9914        let array = StringArray::from(vec!["1000".to_string()]);
9915        let array = Arc::new(array) as ArrayRef;
9916        let casted_array = cast_with_options(
9917            &array,
9918            &DataType::Decimal256(10, 8),
9919            &CastOptions {
9920                safe: true,
9921                format_options: FormatOptions::default(),
9922            },
9923        );
9924        assert!(casted_array.is_ok());
9925        assert!(casted_array.unwrap().is_null(0));
9926
9927        let err = cast_with_options(
9928            &array,
9929            &DataType::Decimal256(10, 8),
9930            &CastOptions {
9931                safe: false,
9932                format_options: FormatOptions::default(),
9933            },
9934        );
9935        assert_eq!(
9936            "Invalid argument error: 1000.00000000 is too large to store in a Decimal256 of precision 10. Max is 99.99999999",
9937            err.unwrap_err().to_string()
9938        );
9939    }
9940
9941    #[test]
9942    fn test_cast_utf8_to_decimal256_overflow() {
9943        let overflow_str_array = StringArray::from(vec![
9944            i128::MAX.to_string(),
9945            i128::MIN.to_string(),
9946            "99999999999999999999999999999999999999".to_string(),
9947            "999999999999999999999999999999999999.99".to_string(),
9948            "99999999999999999999999999999999999.999".to_string(),
9949            i256::MAX.to_string(),
9950            i256::MIN.to_string(),
9951        ]);
9952        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9953
9954        test_cast_string_to_decimal256_overflow(overflow_array);
9955    }
9956
9957    #[test]
9958    fn test_cast_large_utf8_to_decimal256_overflow() {
9959        let overflow_str_array = LargeStringArray::from(vec![
9960            i128::MAX.to_string(),
9961            i128::MIN.to_string(),
9962            "99999999999999999999999999999999999999".to_string(),
9963            "999999999999999999999999999999999999.99".to_string(),
9964            "99999999999999999999999999999999999.999".to_string(),
9965            i256::MAX.to_string(),
9966            i256::MIN.to_string(),
9967        ]);
9968        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
9969
9970        test_cast_string_to_decimal256_overflow(overflow_array);
9971    }
9972
9973    #[test]
9974    fn test_cast_outside_supported_range_for_nanoseconds() {
9975        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";
9976
9977        let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
9978
9979        let cast_options = CastOptions {
9980            safe: false,
9981            format_options: FormatOptions::default(),
9982        };
9983
9984        let result = cast_string_to_timestamp::<i32, TimestampNanosecondType>(
9985            &array,
9986            &None::<Arc<str>>,
9987            &cast_options,
9988        );
9989
9990        let err = result.unwrap_err();
9991        assert_eq!(
9992            err.to_string(),
9993            format!(
9994                "Cast error: Overflow converting {} to Nanosecond. {}",
9995                array.value(0),
9996                EXPECTED_ERROR_MESSAGE
9997            )
9998        );
9999    }
10000
10001    #[test]
10002    fn test_cast_date32_to_timestamp() {
10003        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10004        let array = Arc::new(a) as ArrayRef;
10005        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
10006        let c = b.as_primitive::<TimestampSecondType>();
10007        assert_eq!(1609459200, c.value(0));
10008        assert_eq!(1640995200, c.value(1));
10009        assert!(c.is_null(2));
10010    }
10011
10012    #[test]
10013    fn test_cast_date32_to_timestamp_ms() {
10014        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10015        let array = Arc::new(a) as ArrayRef;
10016        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
10017        let c = b
10018            .as_any()
10019            .downcast_ref::<TimestampMillisecondArray>()
10020            .unwrap();
10021        assert_eq!(1609459200000, c.value(0));
10022        assert_eq!(1640995200000, c.value(1));
10023        assert!(c.is_null(2));
10024    }
10025
10026    #[test]
10027    fn test_cast_date32_to_timestamp_us() {
10028        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10029        let array = Arc::new(a) as ArrayRef;
10030        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
10031        let c = b
10032            .as_any()
10033            .downcast_ref::<TimestampMicrosecondArray>()
10034            .unwrap();
10035        assert_eq!(1609459200000000, c.value(0));
10036        assert_eq!(1640995200000000, c.value(1));
10037        assert!(c.is_null(2));
10038    }
10039
10040    #[test]
10041    fn test_cast_date32_to_timestamp_ns() {
10042        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10043        let array = Arc::new(a) as ArrayRef;
10044        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
10045        let c = b
10046            .as_any()
10047            .downcast_ref::<TimestampNanosecondArray>()
10048            .unwrap();
10049        assert_eq!(1609459200000000000, c.value(0));
10050        assert_eq!(1640995200000000000, c.value(1));
10051        assert!(c.is_null(2));
10052    }
10053
10054    #[test]
10055    fn test_timezone_cast() {
10056        let a = StringArray::from(vec![
10057            "2000-01-01T12:00:00", // date + time valid
10058            "2020-12-15T12:34:56", // date + time valid
10059        ]);
10060        let array = Arc::new(a) as ArrayRef;
10061        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
10062        let v = b.as_primitive::<TimestampNanosecondType>();
10063
10064        assert_eq!(v.value(0), 946728000000000000);
10065        assert_eq!(v.value(1), 1608035696000000000);
10066
10067        let b = cast(
10068            &b,
10069            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10070        )
10071        .unwrap();
10072        let v = b.as_primitive::<TimestampNanosecondType>();
10073
10074        assert_eq!(v.value(0), 946728000000000000);
10075        assert_eq!(v.value(1), 1608035696000000000);
10076
10077        let b = cast(
10078            &b,
10079            &DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
10080        )
10081        .unwrap();
10082        let v = b.as_primitive::<TimestampMillisecondType>();
10083
10084        assert_eq!(v.value(0), 946728000000);
10085        assert_eq!(v.value(1), 1608035696000);
10086    }
10087
10088    #[test]
10089    fn test_cast_utf8_to_timestamp() {
10090        fn test_tz(tz: Arc<str>) {
10091            let valid = StringArray::from(vec![
10092                "2023-01-01 04:05:06.789000-08:00",
10093                "2023-01-01 04:05:06.789000-07:00",
10094                "2023-01-01 04:05:06.789 -0800",
10095                "2023-01-01 04:05:06.789 -08:00",
10096                "2023-01-01 040506 +0730",
10097                "2023-01-01 040506 +07:30",
10098                "2023-01-01 04:05:06.789",
10099                "2023-01-01 04:05:06",
10100                "2023-01-01",
10101            ]);
10102
10103            let array = Arc::new(valid) as ArrayRef;
10104            let b = cast_with_options(
10105                &array,
10106                &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
10107                &CastOptions {
10108                    safe: false,
10109                    format_options: FormatOptions::default(),
10110                },
10111            )
10112            .unwrap();
10113
10114            let tz = tz.as_ref().parse().unwrap();
10115
10116            let as_tz =
10117                |v: i64| as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap();
10118
10119            let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
10120            let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
10121
10122            let values = b.as_primitive::<TimestampNanosecondType>().values();
10123            let utc_results: Vec<_> = values.iter().map(as_utc).collect();
10124            let local_results: Vec<_> = values.iter().map(as_local).collect();
10125
10126            // Absolute timestamps should be parsed preserving the same UTC instant
10127            assert_eq!(
10128                &utc_results[..6],
10129                &[
10130                    "2023-01-01 12:05:06.789".to_string(),
10131                    "2023-01-01 11:05:06.789".to_string(),
10132                    "2023-01-01 12:05:06.789".to_string(),
10133                    "2023-01-01 12:05:06.789".to_string(),
10134                    "2022-12-31 20:35:06".to_string(),
10135                    "2022-12-31 20:35:06".to_string(),
10136                ]
10137            );
10138            // Non-absolute timestamps should be parsed preserving the same local instant
10139            assert_eq!(
10140                &local_results[6..],
10141                &[
10142                    "2023-01-01 04:05:06.789".to_string(),
10143                    "2023-01-01 04:05:06".to_string(),
10144                    "2023-01-01 00:00:00".to_string()
10145                ]
10146            )
10147        }
10148
10149        test_tz("+00:00".into());
10150        test_tz("+02:00".into());
10151    }
10152
10153    #[test]
10154    fn test_cast_invalid_utf8() {
10155        let v1: &[u8] = b"\xFF invalid";
10156        let v2: &[u8] = b"\x00 Foo";
10157        let s = BinaryArray::from(vec![v1, v2]);
10158        let options = CastOptions {
10159            safe: true,
10160            format_options: FormatOptions::default(),
10161        };
10162        let array = cast_with_options(&s, &DataType::Utf8, &options).unwrap();
10163        let a = array.as_string::<i32>();
10164        a.to_data().validate_full().unwrap();
10165
10166        assert_eq!(a.null_count(), 1);
10167        assert_eq!(a.len(), 2);
10168        assert!(a.is_null(0));
10169        assert_eq!(a.value(0), "");
10170        assert_eq!(a.value(1), "\x00 Foo");
10171    }
10172
10173    #[test]
10174    fn test_cast_utf8_to_timestamptz() {
10175        let valid = StringArray::from(vec!["2023-01-01"]);
10176
10177        let array = Arc::new(valid) as ArrayRef;
10178        let b = cast(
10179            &array,
10180            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10181        )
10182        .unwrap();
10183
10184        let expect = DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into()));
10185
10186        assert_eq!(b.data_type(), &expect);
10187        let c = b
10188            .as_any()
10189            .downcast_ref::<TimestampNanosecondArray>()
10190            .unwrap();
10191        assert_eq!(1672531200000000000, c.value(0));
10192    }
10193
10194    #[test]
10195    fn test_cast_decimal_to_string() {
10196        assert!(can_cast_types(
10197            &DataType::Decimal32(9, 4),
10198            &DataType::Utf8View
10199        ));
10200        assert!(can_cast_types(
10201            &DataType::Decimal64(16, 4),
10202            &DataType::Utf8View
10203        ));
10204        assert!(can_cast_types(
10205            &DataType::Decimal128(10, 4),
10206            &DataType::Utf8View
10207        ));
10208        assert!(can_cast_types(
10209            &DataType::Decimal256(38, 10),
10210            &DataType::Utf8View
10211        ));
10212
10213        macro_rules! assert_decimal_values {
10214            ($array:expr) => {
10215                let c = $array;
10216                assert_eq!("1123.454", c.value(0));
10217                assert_eq!("2123.456", c.value(1));
10218                assert_eq!("-3123.453", c.value(2));
10219                assert_eq!("-3123.456", c.value(3));
10220                assert_eq!("0.000", c.value(4));
10221                assert_eq!("0.123", c.value(5));
10222                assert_eq!("1234.567", c.value(6));
10223                assert_eq!("-1234.567", c.value(7));
10224                assert!(c.is_null(8));
10225            };
10226        }
10227
10228        fn test_decimal_to_string<IN: ArrowPrimitiveType, OffsetSize: OffsetSizeTrait>(
10229            output_type: DataType,
10230            array: PrimitiveArray<IN>,
10231        ) {
10232            let b = cast(&array, &output_type).unwrap();
10233
10234            assert_eq!(b.data_type(), &output_type);
10235            match b.data_type() {
10236                DataType::Utf8View => {
10237                    let c = b.as_string_view();
10238                    assert_decimal_values!(c);
10239                }
10240                DataType::Utf8 | DataType::LargeUtf8 => {
10241                    let c = b.as_string::<OffsetSize>();
10242                    assert_decimal_values!(c);
10243                }
10244                _ => (),
10245            }
10246        }
10247
10248        let array32: Vec<Option<i32>> = vec![
10249            Some(1123454),
10250            Some(2123456),
10251            Some(-3123453),
10252            Some(-3123456),
10253            Some(0),
10254            Some(123),
10255            Some(123456789),
10256            Some(-123456789),
10257            None,
10258        ];
10259        let array64: Vec<Option<i64>> = array32.iter().map(|num| num.map(|x| x as i64)).collect();
10260        let array128: Vec<Option<i128>> =
10261            array64.iter().map(|num| num.map(|x| x as i128)).collect();
10262        let array256: Vec<Option<i256>> = array128
10263            .iter()
10264            .map(|num| num.map(i256::from_i128))
10265            .collect();
10266
10267        test_decimal_to_string::<Decimal32Type, i32>(
10268            DataType::Utf8View,
10269            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
10270        );
10271        test_decimal_to_string::<Decimal32Type, i32>(
10272            DataType::Utf8,
10273            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
10274        );
10275        test_decimal_to_string::<Decimal32Type, i64>(
10276            DataType::LargeUtf8,
10277            create_decimal32_array(array32, 7, 3).unwrap(),
10278        );
10279
10280        test_decimal_to_string::<Decimal64Type, i32>(
10281            DataType::Utf8View,
10282            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
10283        );
10284        test_decimal_to_string::<Decimal64Type, i32>(
10285            DataType::Utf8,
10286            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
10287        );
10288        test_decimal_to_string::<Decimal64Type, i64>(
10289            DataType::LargeUtf8,
10290            create_decimal64_array(array64, 7, 3).unwrap(),
10291        );
10292
10293        test_decimal_to_string::<Decimal128Type, i32>(
10294            DataType::Utf8View,
10295            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
10296        );
10297        test_decimal_to_string::<Decimal128Type, i32>(
10298            DataType::Utf8,
10299            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
10300        );
10301        test_decimal_to_string::<Decimal128Type, i64>(
10302            DataType::LargeUtf8,
10303            create_decimal128_array(array128, 7, 3).unwrap(),
10304        );
10305
10306        test_decimal_to_string::<Decimal256Type, i32>(
10307            DataType::Utf8View,
10308            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
10309        );
10310        test_decimal_to_string::<Decimal256Type, i32>(
10311            DataType::Utf8,
10312            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
10313        );
10314        test_decimal_to_string::<Decimal256Type, i64>(
10315            DataType::LargeUtf8,
10316            create_decimal256_array(array256, 7, 3).unwrap(),
10317        );
10318    }
10319
10320    #[test]
10321    fn test_cast_numeric_to_decimal128_precision_overflow() {
10322        let array = Int64Array::from(vec![1234567]);
10323        let array = Arc::new(array) as ArrayRef;
10324        let casted_array = cast_with_options(
10325            &array,
10326            &DataType::Decimal128(7, 3),
10327            &CastOptions {
10328                safe: true,
10329                format_options: FormatOptions::default(),
10330            },
10331        );
10332        assert!(casted_array.is_ok());
10333        assert!(casted_array.unwrap().is_null(0));
10334
10335        let err = cast_with_options(
10336            &array,
10337            &DataType::Decimal128(7, 3),
10338            &CastOptions {
10339                safe: false,
10340                format_options: FormatOptions::default(),
10341            },
10342        );
10343        assert_eq!(
10344            "Invalid argument error: 1234567.000 is too large to store in a Decimal128 of precision 7. Max is 9999.999",
10345            err.unwrap_err().to_string()
10346        );
10347    }
10348
10349    #[test]
10350    fn test_cast_numeric_to_decimal256_precision_overflow() {
10351        let array = Int64Array::from(vec![1234567]);
10352        let array = Arc::new(array) as ArrayRef;
10353        let casted_array = cast_with_options(
10354            &array,
10355            &DataType::Decimal256(7, 3),
10356            &CastOptions {
10357                safe: true,
10358                format_options: FormatOptions::default(),
10359            },
10360        );
10361        assert!(casted_array.is_ok());
10362        assert!(casted_array.unwrap().is_null(0));
10363
10364        let err = cast_with_options(
10365            &array,
10366            &DataType::Decimal256(7, 3),
10367            &CastOptions {
10368                safe: false,
10369                format_options: FormatOptions::default(),
10370            },
10371        );
10372        assert_eq!(
10373            "Invalid argument error: 1234567.000 is too large to store in a Decimal256 of precision 7. Max is 9999.999",
10374            err.unwrap_err().to_string()
10375        );
10376    }
10377
10378    /// helper function to test casting from duration to interval
10379    fn cast_from_duration_to_interval<T: ArrowTemporalType<Native = i64>>(
10380        array: Vec<i64>,
10381        cast_options: &CastOptions,
10382    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
10383        let array = PrimitiveArray::<T>::new(array.into(), None);
10384        let array = Arc::new(array) as ArrayRef;
10385        let interval = DataType::Interval(IntervalUnit::MonthDayNano);
10386        let out = cast_with_options(&array, &interval, cast_options)?;
10387        let out = out.as_primitive::<IntervalMonthDayNanoType>().clone();
10388        Ok(out)
10389    }
10390
10391    #[test]
10392    fn test_cast_from_duration_to_interval() {
10393        // from duration second to interval month day nano
10394        let array = vec![1234567];
10395        let casted_array =
10396            cast_from_duration_to_interval::<DurationSecondType>(array, &CastOptions::default())
10397                .unwrap();
10398        assert_eq!(
10399            casted_array.data_type(),
10400            &DataType::Interval(IntervalUnit::MonthDayNano)
10401        );
10402        assert_eq!(
10403            casted_array.value(0),
10404            IntervalMonthDayNano::new(0, 0, 1234567000000000)
10405        );
10406
10407        let array = vec![i64::MAX];
10408        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
10409            array.clone(),
10410            &CastOptions::default(),
10411        )
10412        .unwrap();
10413        assert!(!casted_array.is_valid(0));
10414
10415        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
10416            array,
10417            &CastOptions {
10418                safe: false,
10419                format_options: FormatOptions::default(),
10420            },
10421        );
10422        assert!(casted_array.is_err());
10423
10424        // from duration millisecond to interval month day nano
10425        let array = vec![1234567];
10426        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
10427            array,
10428            &CastOptions::default(),
10429        )
10430        .unwrap();
10431        assert_eq!(
10432            casted_array.data_type(),
10433            &DataType::Interval(IntervalUnit::MonthDayNano)
10434        );
10435        assert_eq!(
10436            casted_array.value(0),
10437            IntervalMonthDayNano::new(0, 0, 1234567000000)
10438        );
10439
10440        let array = vec![i64::MAX];
10441        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
10442            array.clone(),
10443            &CastOptions::default(),
10444        )
10445        .unwrap();
10446        assert!(!casted_array.is_valid(0));
10447
10448        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
10449            array,
10450            &CastOptions {
10451                safe: false,
10452                format_options: FormatOptions::default(),
10453            },
10454        );
10455        assert!(casted_array.is_err());
10456
10457        // from duration microsecond to interval month day nano
10458        let array = vec![1234567];
10459        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
10460            array,
10461            &CastOptions::default(),
10462        )
10463        .unwrap();
10464        assert_eq!(
10465            casted_array.data_type(),
10466            &DataType::Interval(IntervalUnit::MonthDayNano)
10467        );
10468        assert_eq!(
10469            casted_array.value(0),
10470            IntervalMonthDayNano::new(0, 0, 1234567000)
10471        );
10472
10473        let array = vec![i64::MAX];
10474        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
10475            array.clone(),
10476            &CastOptions::default(),
10477        )
10478        .unwrap();
10479        assert!(!casted_array.is_valid(0));
10480
10481        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
10482            array,
10483            &CastOptions {
10484                safe: false,
10485                format_options: FormatOptions::default(),
10486            },
10487        );
10488        assert!(casted_array.is_err());
10489
10490        // from duration nanosecond to interval month day nano
10491        let array = vec![1234567];
10492        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
10493            array,
10494            &CastOptions::default(),
10495        )
10496        .unwrap();
10497        assert_eq!(
10498            casted_array.data_type(),
10499            &DataType::Interval(IntervalUnit::MonthDayNano)
10500        );
10501        assert_eq!(
10502            casted_array.value(0),
10503            IntervalMonthDayNano::new(0, 0, 1234567)
10504        );
10505
10506        let array = vec![i64::MAX];
10507        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
10508            array,
10509            &CastOptions {
10510                safe: false,
10511                format_options: FormatOptions::default(),
10512            },
10513        )
10514        .unwrap();
10515        assert_eq!(
10516            casted_array.value(0),
10517            IntervalMonthDayNano::new(0, 0, i64::MAX)
10518        );
10519    }
10520
10521    /// helper function to test casting from interval to duration
10522    fn cast_from_interval_to_duration<T: ArrowTemporalType>(
10523        array: &IntervalMonthDayNanoArray,
10524        cast_options: &CastOptions,
10525    ) -> Result<PrimitiveArray<T>, ArrowError> {
10526        let casted_array = cast_with_options(&array, &T::DATA_TYPE, cast_options)?;
10527        casted_array
10528            .as_any()
10529            .downcast_ref::<PrimitiveArray<T>>()
10530            .ok_or_else(|| {
10531                ArrowError::ComputeError(format!("Failed to downcast to {}", T::DATA_TYPE))
10532            })
10533            .cloned()
10534    }
10535
10536    #[test]
10537    fn test_cast_from_interval_to_duration() {
10538        let nullable = CastOptions::default();
10539        let fallible = CastOptions {
10540            safe: false,
10541            format_options: FormatOptions::default(),
10542        };
10543        let v = IntervalMonthDayNano::new(0, 0, 1234567);
10544
10545        // from interval month day nano to duration second
10546        let array = vec![v].into();
10547        let casted_array: DurationSecondArray =
10548            cast_from_interval_to_duration(&array, &nullable).unwrap();
10549        assert_eq!(casted_array.value(0), 0);
10550
10551        let array = vec![IntervalMonthDayNano::MAX].into();
10552        let casted_array: DurationSecondArray =
10553            cast_from_interval_to_duration(&array, &nullable).unwrap();
10554        assert!(!casted_array.is_valid(0));
10555
10556        let res = cast_from_interval_to_duration::<DurationSecondType>(&array, &fallible);
10557        assert!(res.is_err());
10558
10559        // from interval month day nano to duration millisecond
10560        let array = vec![v].into();
10561        let casted_array: DurationMillisecondArray =
10562            cast_from_interval_to_duration(&array, &nullable).unwrap();
10563        assert_eq!(casted_array.value(0), 1);
10564
10565        let array = vec![IntervalMonthDayNano::MAX].into();
10566        let casted_array: DurationMillisecondArray =
10567            cast_from_interval_to_duration(&array, &nullable).unwrap();
10568        assert!(!casted_array.is_valid(0));
10569
10570        let res = cast_from_interval_to_duration::<DurationMillisecondType>(&array, &fallible);
10571        assert!(res.is_err());
10572
10573        // from interval month day nano to duration microsecond
10574        let array = vec![v].into();
10575        let casted_array: DurationMicrosecondArray =
10576            cast_from_interval_to_duration(&array, &nullable).unwrap();
10577        assert_eq!(casted_array.value(0), 1234);
10578
10579        let array = vec![IntervalMonthDayNano::MAX].into();
10580        let casted_array =
10581            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &nullable).unwrap();
10582        assert!(!casted_array.is_valid(0));
10583
10584        let casted_array =
10585            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &fallible);
10586        assert!(casted_array.is_err());
10587
10588        // from interval month day nano to duration nanosecond
10589        let array = vec![v].into();
10590        let casted_array: DurationNanosecondArray =
10591            cast_from_interval_to_duration(&array, &nullable).unwrap();
10592        assert_eq!(casted_array.value(0), 1234567);
10593
10594        let array = vec![IntervalMonthDayNano::MAX].into();
10595        let casted_array: DurationNanosecondArray =
10596            cast_from_interval_to_duration(&array, &nullable).unwrap();
10597        assert!(!casted_array.is_valid(0));
10598
10599        let casted_array =
10600            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &fallible);
10601        assert!(casted_array.is_err());
10602
10603        let array = vec![
10604            IntervalMonthDayNanoType::make_value(0, 1, 0),
10605            IntervalMonthDayNanoType::make_value(-1, 0, 0),
10606            IntervalMonthDayNanoType::make_value(1, 1, 0),
10607            IntervalMonthDayNanoType::make_value(1, 0, 1),
10608            IntervalMonthDayNanoType::make_value(0, 0, -1),
10609        ]
10610        .into();
10611        let casted_array =
10612            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable).unwrap();
10613        assert!(!casted_array.is_valid(0));
10614        assert!(!casted_array.is_valid(1));
10615        assert!(!casted_array.is_valid(2));
10616        assert!(!casted_array.is_valid(3));
10617        assert!(casted_array.is_valid(4));
10618        assert_eq!(casted_array.value(4), -1);
10619    }
10620
10621    /// helper function to test casting from interval year month to interval month day nano
10622    fn cast_from_interval_year_month_to_interval_month_day_nano(
10623        array: Vec<i32>,
10624        cast_options: &CastOptions,
10625    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
10626        let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
10627        let array = Arc::new(array) as ArrayRef;
10628        let casted_array = cast_with_options(
10629            &array,
10630            &DataType::Interval(IntervalUnit::MonthDayNano),
10631            cast_options,
10632        )?;
10633        casted_array
10634            .as_any()
10635            .downcast_ref::<IntervalMonthDayNanoArray>()
10636            .ok_or_else(|| {
10637                ArrowError::ComputeError(
10638                    "Failed to downcast to IntervalMonthDayNanoArray".to_string(),
10639                )
10640            })
10641            .cloned()
10642    }
10643
10644    #[test]
10645    fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
10646        // from interval year month to interval month day nano
10647        let array = vec![1234567];
10648        let casted_array = cast_from_interval_year_month_to_interval_month_day_nano(
10649            array,
10650            &CastOptions::default(),
10651        )
10652        .unwrap();
10653        assert_eq!(
10654            casted_array.data_type(),
10655            &DataType::Interval(IntervalUnit::MonthDayNano)
10656        );
10657        assert_eq!(
10658            casted_array.value(0),
10659            IntervalMonthDayNano::new(1234567, 0, 0)
10660        );
10661    }
10662
10663    /// helper function to test casting from interval day time to interval month day nano
10664    fn cast_from_interval_day_time_to_interval_month_day_nano(
10665        array: Vec<IntervalDayTime>,
10666        cast_options: &CastOptions,
10667    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
10668        let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
10669        let array = Arc::new(array) as ArrayRef;
10670        let casted_array = cast_with_options(
10671            &array,
10672            &DataType::Interval(IntervalUnit::MonthDayNano),
10673            cast_options,
10674        )?;
10675        Ok(casted_array
10676            .as_primitive::<IntervalMonthDayNanoType>()
10677            .clone())
10678    }
10679
10680    #[test]
10681    fn test_cast_from_interval_day_time_to_interval_month_day_nano() {
10682        // from interval day time to interval month day nano
10683        let array = vec![IntervalDayTime::new(123, 0)];
10684        let casted_array =
10685            cast_from_interval_day_time_to_interval_month_day_nano(array, &CastOptions::default())
10686                .unwrap();
10687        assert_eq!(
10688            casted_array.data_type(),
10689            &DataType::Interval(IntervalUnit::MonthDayNano)
10690        );
10691        assert_eq!(casted_array.value(0), IntervalMonthDayNano::new(0, 123, 0));
10692    }
10693
10694    #[test]
10695    fn test_cast_below_unixtimestamp() {
10696        let valid = StringArray::from(vec![
10697            "1900-01-03 23:59:59",
10698            "1969-12-31 00:00:01",
10699            "1989-12-31 00:00:01",
10700        ]);
10701
10702        let array = Arc::new(valid) as ArrayRef;
10703        let casted_array = cast_with_options(
10704            &array,
10705            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10706            &CastOptions {
10707                safe: false,
10708                format_options: FormatOptions::default(),
10709            },
10710        )
10711        .unwrap();
10712
10713        let ts_array = casted_array
10714            .as_primitive::<TimestampNanosecondType>()
10715            .values()
10716            .iter()
10717            .map(|ts| ts / 1_000_000)
10718            .collect::<Vec<_>>();
10719
10720        let array = TimestampMillisecondArray::from(ts_array).with_timezone("+00:00".to_string());
10721        let casted_array = cast(&array, &DataType::Date32).unwrap();
10722        let date_array = casted_array.as_primitive::<Date32Type>();
10723        let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
10724        let string_array = casted_array.as_string::<i32>();
10725        assert_eq!("1900-01-03", string_array.value(0));
10726        assert_eq!("1969-12-31", string_array.value(1));
10727        assert_eq!("1989-12-31", string_array.value(2));
10728    }
10729
10730    #[test]
10731    fn test_nested_list() {
10732        let mut list = ListBuilder::new(Int32Builder::new());
10733        list.append_value([Some(1), Some(2), Some(3)]);
10734        list.append_value([Some(4), None, Some(6)]);
10735        let list = list.finish();
10736
10737        let to_field = Field::new("nested", list.data_type().clone(), false);
10738        let to = DataType::List(Arc::new(to_field));
10739        let out = cast(&list, &to).unwrap();
10740        let opts = FormatOptions::default().with_null("null");
10741        let formatted = ArrayFormatter::try_new(out.as_ref(), &opts).unwrap();
10742
10743        assert_eq!(formatted.value(0).to_string(), "[[1], [2], [3]]");
10744        assert_eq!(formatted.value(1).to_string(), "[[4], [null], [6]]");
10745    }
10746
10747    #[test]
10748    fn test_nested_list_cast() {
10749        let mut builder = ListBuilder::new(ListBuilder::new(Int32Builder::new()));
10750        builder.append_value([Some([Some(1), Some(2), None]), None]);
10751        builder.append_value([None, Some([]), None]);
10752        builder.append_null();
10753        builder.append_value([Some([Some(2), Some(3)])]);
10754        let start = builder.finish();
10755
10756        let mut builder = LargeListBuilder::new(LargeListBuilder::new(Int8Builder::new()));
10757        builder.append_value([Some([Some(1), Some(2), None]), None]);
10758        builder.append_value([None, Some([]), None]);
10759        builder.append_null();
10760        builder.append_value([Some([Some(2), Some(3)])]);
10761        let expected = builder.finish();
10762
10763        let actual = cast(&start, expected.data_type()).unwrap();
10764        assert_eq!(actual.as_ref(), &expected);
10765    }
10766
10767    const CAST_OPTIONS: CastOptions<'static> = CastOptions {
10768        safe: true,
10769        format_options: FormatOptions::new(),
10770    };
10771
10772    #[test]
10773    #[allow(clippy::assertions_on_constants)]
10774    fn test_const_options() {
10775        assert!(CAST_OPTIONS.safe)
10776    }
10777
10778    #[test]
10779    fn test_list_format_options() {
10780        let options = CastOptions {
10781            safe: false,
10782            format_options: FormatOptions::default().with_null("null"),
10783        };
10784        let array = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
10785            Some(vec![Some(0), Some(1), Some(2)]),
10786            Some(vec![Some(0), None, Some(2)]),
10787        ]);
10788        let a = cast_with_options(&array, &DataType::Utf8, &options).unwrap();
10789        let r: Vec<_> = a.as_string::<i32>().iter().flatten().collect();
10790        assert_eq!(r, &["[0, 1, 2]", "[0, null, 2]"]);
10791    }
10792    #[test]
10793    fn test_cast_string_to_timestamp_invalid_tz() {
10794        // content after Z should be ignored
10795        let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
10796        let array = StringArray::from(vec![Some(bad_timestamp)]);
10797
10798        let data_types = [
10799            DataType::Timestamp(TimeUnit::Second, None),
10800            DataType::Timestamp(TimeUnit::Millisecond, None),
10801            DataType::Timestamp(TimeUnit::Microsecond, None),
10802            DataType::Timestamp(TimeUnit::Nanosecond, None),
10803        ];
10804
10805        let cast_options = CastOptions {
10806            safe: false,
10807            ..Default::default()
10808        };
10809
10810        for dt in data_types {
10811            assert_eq!(
10812                cast_with_options(&array, &dt, &cast_options)
10813                    .unwrap_err()
10814                    .to_string(),
10815                "Parser error: Invalid timezone \"ZZTOP\": only offset based timezones supported without chrono-tz feature"
10816            );
10817        }
10818    }
10819    #[test]
10820    fn test_cast_struct_to_struct() {
10821        let struct_type = DataType::Struct(
10822            vec![
10823                Field::new("a", DataType::Boolean, false),
10824                Field::new("b", DataType::Int32, false),
10825            ]
10826            .into(),
10827        );
10828        let to_type = DataType::Struct(
10829            vec![
10830                Field::new("a", DataType::Utf8, false),
10831                Field::new("b", DataType::Utf8, false),
10832            ]
10833            .into(),
10834        );
10835        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10836        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
10837        let struct_array = StructArray::from(vec![
10838            (
10839                Arc::new(Field::new("b", DataType::Boolean, false)),
10840                boolean.clone() as ArrayRef,
10841            ),
10842            (
10843                Arc::new(Field::new("c", DataType::Int32, false)),
10844                int.clone() as ArrayRef,
10845            ),
10846        ]);
10847        let casted_array = cast(&struct_array, &to_type).unwrap();
10848        let casted_array = casted_array.as_struct();
10849        assert_eq!(casted_array.data_type(), &to_type);
10850        let casted_boolean_array = casted_array
10851            .column(0)
10852            .as_string::<i32>()
10853            .into_iter()
10854            .flatten()
10855            .collect::<Vec<_>>();
10856        let casted_int_array = casted_array
10857            .column(1)
10858            .as_string::<i32>()
10859            .into_iter()
10860            .flatten()
10861            .collect::<Vec<_>>();
10862        assert_eq!(casted_boolean_array, vec!["false", "false", "true", "true"]);
10863        assert_eq!(casted_int_array, vec!["42", "28", "19", "31"]);
10864
10865        // test for can't cast
10866        let to_type = DataType::Struct(
10867            vec![
10868                Field::new("a", DataType::Date32, false),
10869                Field::new("b", DataType::Utf8, false),
10870            ]
10871            .into(),
10872        );
10873        assert!(!can_cast_types(&struct_type, &to_type));
10874        let result = cast(&struct_array, &to_type);
10875        assert_eq!(
10876            "Cast error: Casting from Boolean to Date32 not supported",
10877            result.unwrap_err().to_string()
10878        );
10879    }
10880
10881    #[test]
10882    fn test_cast_struct_to_struct_nullability() {
10883        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10884        let int = Arc::new(Int32Array::from(vec![Some(42), None, Some(19), None]));
10885        let struct_array = StructArray::from(vec![
10886            (
10887                Arc::new(Field::new("b", DataType::Boolean, false)),
10888                boolean.clone() as ArrayRef,
10889            ),
10890            (
10891                Arc::new(Field::new("c", DataType::Int32, true)),
10892                int.clone() as ArrayRef,
10893            ),
10894        ]);
10895
10896        // okay: nullable to nullable
10897        let to_type = DataType::Struct(
10898            vec![
10899                Field::new("a", DataType::Utf8, false),
10900                Field::new("b", DataType::Utf8, true),
10901            ]
10902            .into(),
10903        );
10904        cast(&struct_array, &to_type).expect("Cast nullable to nullable struct field should work");
10905
10906        // error: nullable to non-nullable
10907        let to_type = DataType::Struct(
10908            vec![
10909                Field::new("a", DataType::Utf8, false),
10910                Field::new("b", DataType::Utf8, false),
10911            ]
10912            .into(),
10913        );
10914        cast(&struct_array, &to_type)
10915            .expect_err("Cast nullable to non-nullable struct field should fail");
10916
10917        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
10918        let int = Arc::new(Int32Array::from(vec![i32::MAX, 25, 1, 100]));
10919        let struct_array = StructArray::from(vec![
10920            (
10921                Arc::new(Field::new("b", DataType::Boolean, false)),
10922                boolean.clone() as ArrayRef,
10923            ),
10924            (
10925                Arc::new(Field::new("c", DataType::Int32, false)),
10926                int.clone() as ArrayRef,
10927            ),
10928        ]);
10929
10930        // okay: non-nullable to non-nullable
10931        let to_type = DataType::Struct(
10932            vec![
10933                Field::new("a", DataType::Utf8, false),
10934                Field::new("b", DataType::Utf8, false),
10935            ]
10936            .into(),
10937        );
10938        cast(&struct_array, &to_type)
10939            .expect("Cast non-nullable to non-nullable struct field should work");
10940
10941        // err: non-nullable to non-nullable but overflowing return null during casting
10942        let to_type = DataType::Struct(
10943            vec![
10944                Field::new("a", DataType::Utf8, false),
10945                Field::new("b", DataType::Int8, false),
10946            ]
10947            .into(),
10948        );
10949        cast(&struct_array, &to_type).expect_err(
10950            "Cast non-nullable to non-nullable struct field returning null should fail",
10951        );
10952    }
10953
10954    #[test]
10955    fn test_cast_struct_to_non_struct() {
10956        let boolean = Arc::new(BooleanArray::from(vec![true, false]));
10957        let struct_array = StructArray::from(vec![(
10958            Arc::new(Field::new("a", DataType::Boolean, false)),
10959            boolean.clone() as ArrayRef,
10960        )]);
10961        let to_type = DataType::Utf8;
10962        let result = cast(&struct_array, &to_type);
10963        assert_eq!(
10964            r#"Cast error: Casting from Struct("a": Boolean) to Utf8 not supported"#,
10965            result.unwrap_err().to_string()
10966        );
10967    }
10968
10969    #[test]
10970    fn test_cast_non_struct_to_struct() {
10971        let array = StringArray::from(vec!["a", "b"]);
10972        let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
10973        let result = cast(&array, &to_type);
10974        assert_eq!(
10975            r#"Cast error: Casting from Utf8 to Struct("a": Boolean) not supported"#,
10976            result.unwrap_err().to_string()
10977        );
10978    }
10979
10980    fn run_decimal_cast_test_case_between_multiple_types(t: DecimalCastTestConfig) {
10981        run_decimal_cast_test_case::<Decimal128Type, Decimal128Type>(t.clone());
10982        run_decimal_cast_test_case::<Decimal128Type, Decimal256Type>(t.clone());
10983        run_decimal_cast_test_case::<Decimal256Type, Decimal128Type>(t.clone());
10984        run_decimal_cast_test_case::<Decimal256Type, Decimal256Type>(t.clone());
10985    }
10986
10987    #[test]
10988    fn test_decimal_to_decimal_coverage() {
10989        let test_cases = [
10990            // increase precision, increase scale, infallible
10991            DecimalCastTestConfig {
10992                input_prec: 5,
10993                input_scale: 1,
10994                input_repr: 99999, // 9999.9
10995                output_prec: 10,
10996                output_scale: 6,
10997                expected_output_repr: Ok(9999900000), // 9999.900000
10998            },
10999            // increase precision, increase scale, fallible, safe
11000            DecimalCastTestConfig {
11001                input_prec: 5,
11002                input_scale: 1,
11003                input_repr: 99, // 9999.9
11004                output_prec: 7,
11005                output_scale: 6,
11006                expected_output_repr: Ok(9900000), // 9.900000
11007            },
11008            // increase precision, increase scale, fallible, unsafe
11009            DecimalCastTestConfig {
11010                input_prec: 5,
11011                input_scale: 1,
11012                input_repr: 99999, // 9999.9
11013                output_prec: 7,
11014                output_scale: 6,
11015                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
11016            },
11017            // increase precision, decrease scale, always infallible
11018            DecimalCastTestConfig {
11019                input_prec: 5,
11020                input_scale: 3,
11021                input_repr: 99999, // 99.999
11022                output_prec: 10,
11023                output_scale: 2,
11024                expected_output_repr: Ok(10000), // 100.00
11025            },
11026            // increase precision, decrease scale, no rouding
11027            DecimalCastTestConfig {
11028                input_prec: 5,
11029                input_scale: 3,
11030                input_repr: 99994, // 99.994
11031                output_prec: 10,
11032                output_scale: 2,
11033                expected_output_repr: Ok(9999), // 99.99
11034            },
11035            // increase precision, don't change scale, always infallible
11036            DecimalCastTestConfig {
11037                input_prec: 5,
11038                input_scale: 3,
11039                input_repr: 99999, // 99.999
11040                output_prec: 10,
11041                output_scale: 3,
11042                expected_output_repr: Ok(99999), // 99.999
11043            },
11044            // decrease precision, increase scale, safe
11045            DecimalCastTestConfig {
11046                input_prec: 10,
11047                input_scale: 5,
11048                input_repr: 999999, // 9.99999
11049                output_prec: 8,
11050                output_scale: 7,
11051                expected_output_repr: Ok(99999900), // 9.9999900
11052            },
11053            // decrease precision, increase scale, unsafe
11054            DecimalCastTestConfig {
11055                input_prec: 10,
11056                input_scale: 5,
11057                input_repr: 9999999, // 99.99999
11058                output_prec: 8,
11059                output_scale: 7,
11060                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
11061            },
11062            // decrease precision, decrease scale, safe, infallible
11063            DecimalCastTestConfig {
11064                input_prec: 7,
11065                input_scale: 4,
11066                input_repr: 9999999, // 999.9999
11067                output_prec: 6,
11068                output_scale: 2,
11069                expected_output_repr: Ok(100000),
11070            },
11071            // decrease precision, decrease scale, safe, fallible
11072            DecimalCastTestConfig {
11073                input_prec: 10,
11074                input_scale: 5,
11075                input_repr: 12345678, // 123.45678
11076                output_prec: 8,
11077                output_scale: 3,
11078                expected_output_repr: Ok(123457), // 123.457
11079            },
11080            // decrease precision, decrease scale, unsafe
11081            DecimalCastTestConfig {
11082                input_prec: 10,
11083                input_scale: 5,
11084                input_repr: 9999999, // 99.99999
11085                output_prec: 4,
11086                output_scale: 3,
11087                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
11088            },
11089            // decrease precision, same scale, safe
11090            DecimalCastTestConfig {
11091                input_prec: 10,
11092                input_scale: 5,
11093                input_repr: 999999, // 9.99999
11094                output_prec: 6,
11095                output_scale: 5,
11096                expected_output_repr: Ok(999999), // 9.99999
11097            },
11098            // decrease precision, same scale, unsafe
11099            DecimalCastTestConfig {
11100                input_prec: 10,
11101                input_scale: 5,
11102                input_repr: 9999999, // 99.99999
11103                output_prec: 6,
11104                output_scale: 5,
11105                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
11106            },
11107            // same precision, increase scale, safe
11108            DecimalCastTestConfig {
11109                input_prec: 7,
11110                input_scale: 4,
11111                input_repr: 12345, // 1.2345
11112                output_prec: 7,
11113                output_scale: 6,
11114                expected_output_repr: Ok(1234500), // 1.234500
11115            },
11116            // same precision, increase scale, unsafe
11117            DecimalCastTestConfig {
11118                input_prec: 7,
11119                input_scale: 4,
11120                input_repr: 123456, // 12.3456
11121                output_prec: 7,
11122                output_scale: 6,
11123                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
11124            },
11125            // same precision, decrease scale, infallible
11126            DecimalCastTestConfig {
11127                input_prec: 7,
11128                input_scale: 5,
11129                input_repr: 1234567, // 12.34567
11130                output_prec: 7,
11131                output_scale: 4,
11132                expected_output_repr: Ok(123457), // 12.3457
11133            },
11134            // same precision, same scale, infallible
11135            DecimalCastTestConfig {
11136                input_prec: 7,
11137                input_scale: 5,
11138                input_repr: 9999999, // 99.99999
11139                output_prec: 7,
11140                output_scale: 5,
11141                expected_output_repr: Ok(9999999), // 99.99999
11142            },
11143            // precision increase, input scale & output scale = 0, infallible
11144            DecimalCastTestConfig {
11145                input_prec: 7,
11146                input_scale: 0,
11147                input_repr: 1234567, // 1234567
11148                output_prec: 8,
11149                output_scale: 0,
11150                expected_output_repr: Ok(1234567), // 1234567
11151            },
11152            // precision decrease, input scale & output scale = 0, failure
11153            DecimalCastTestConfig {
11154                input_prec: 7,
11155                input_scale: 0,
11156                input_repr: 1234567, // 1234567
11157                output_prec: 6,
11158                output_scale: 0,
11159                expected_output_repr: Err("Invalid argument error: 1234567 is too large to store in a {} of precision 6. Max is 999999".to_string())
11160            },
11161            // precision decrease, input scale & output scale = 0, success
11162            DecimalCastTestConfig {
11163                input_prec: 7,
11164                input_scale: 0,
11165                input_repr: 123456, // 123456
11166                output_prec: 6,
11167                output_scale: 0,
11168                expected_output_repr: Ok(123456), // 123456
11169            },
11170        ];
11171
11172        for t in test_cases {
11173            run_decimal_cast_test_case_between_multiple_types(t);
11174        }
11175    }
11176
11177    #[test]
11178    fn test_decimal_to_decimal_increase_scale_and_precision_unchecked() {
11179        let test_cases = [
11180            DecimalCastTestConfig {
11181                input_prec: 5,
11182                input_scale: 0,
11183                input_repr: 99999,
11184                output_prec: 10,
11185                output_scale: 5,
11186                expected_output_repr: Ok(9999900000),
11187            },
11188            DecimalCastTestConfig {
11189                input_prec: 5,
11190                input_scale: 0,
11191                input_repr: -99999,
11192                output_prec: 10,
11193                output_scale: 5,
11194                expected_output_repr: Ok(-9999900000),
11195            },
11196            DecimalCastTestConfig {
11197                input_prec: 5,
11198                input_scale: 2,
11199                input_repr: 99999,
11200                output_prec: 10,
11201                output_scale: 5,
11202                expected_output_repr: Ok(99999000),
11203            },
11204            DecimalCastTestConfig {
11205                input_prec: 5,
11206                input_scale: -2,
11207                input_repr: -99999,
11208                output_prec: 10,
11209                output_scale: 3,
11210                expected_output_repr: Ok(-9999900000),
11211            },
11212            DecimalCastTestConfig {
11213                input_prec: 5,
11214                input_scale: 3,
11215                input_repr: -12345,
11216                output_prec: 6,
11217                output_scale: 5,
11218                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())
11219            },
11220        ];
11221
11222        for t in test_cases {
11223            run_decimal_cast_test_case_between_multiple_types(t);
11224        }
11225    }
11226
11227    #[test]
11228    fn test_decimal_to_decimal_decrease_scale_and_precision_unchecked() {
11229        let test_cases = [
11230            DecimalCastTestConfig {
11231                input_prec: 5,
11232                input_scale: 0,
11233                input_repr: 99999,
11234                output_scale: -3,
11235                output_prec: 3,
11236                expected_output_repr: Ok(100),
11237            },
11238            DecimalCastTestConfig {
11239                input_prec: 5,
11240                input_scale: 0,
11241                input_repr: -99999,
11242                output_prec: 1,
11243                output_scale: -5,
11244                expected_output_repr: Ok(-1),
11245            },
11246            DecimalCastTestConfig {
11247                input_prec: 10,
11248                input_scale: 2,
11249                input_repr: 123456789,
11250                output_prec: 5,
11251                output_scale: -2,
11252                expected_output_repr: Ok(12346),
11253            },
11254            DecimalCastTestConfig {
11255                input_prec: 10,
11256                input_scale: 4,
11257                input_repr: -9876543210,
11258                output_prec: 7,
11259                output_scale: 0,
11260                expected_output_repr: Ok(-987654),
11261            },
11262            DecimalCastTestConfig {
11263                input_prec: 7,
11264                input_scale: 4,
11265                input_repr: 9999999,
11266                output_prec: 6,
11267                output_scale: 3,
11268                expected_output_repr:
11269                    Err("Invalid argument error: 1000.000 is too large to store in a {} of precision 6. Max is 999.999".to_string()),
11270            },
11271        ];
11272        for t in test_cases {
11273            run_decimal_cast_test_case_between_multiple_types(t);
11274        }
11275    }
11276
11277    #[test]
11278    fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
11279        let array = vec![Some(123456789)];
11280        let array = create_decimal128_array(array, 24, 2).unwrap();
11281        let input_type = DataType::Decimal128(24, 2);
11282        let output_type = DataType::Decimal128(6, 2);
11283        assert!(can_cast_types(&input_type, &output_type));
11284
11285        let options = CastOptions {
11286            safe: false,
11287            ..Default::default()
11288        };
11289        let result = cast_with_options(&array, &output_type, &options);
11290        assert_eq!(
11291            result.unwrap_err().to_string(),
11292            "Invalid argument error: 1234567.89 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
11293        );
11294    }
11295
11296    #[test]
11297    fn test_decimal_to_decimal_same_scale() {
11298        let array = vec![Some(520)];
11299        let array = create_decimal128_array(array, 4, 2).unwrap();
11300        let input_type = DataType::Decimal128(4, 2);
11301        let output_type = DataType::Decimal128(3, 2);
11302        assert!(can_cast_types(&input_type, &output_type));
11303
11304        let options = CastOptions {
11305            safe: false,
11306            ..Default::default()
11307        };
11308        let result = cast_with_options(&array, &output_type, &options);
11309        assert_eq!(
11310            result.unwrap().as_primitive::<Decimal128Type>().value(0),
11311            520
11312        );
11313
11314        // Cast 0 of decimal(3, 0) type to decimal(2, 0)
11315        assert_eq!(
11316            &cast(
11317                &create_decimal128_array(vec![Some(0)], 3, 0).unwrap(),
11318                &DataType::Decimal128(2, 0)
11319            )
11320            .unwrap(),
11321            &(Arc::new(create_decimal128_array(vec![Some(0)], 2, 0).unwrap()) as ArrayRef)
11322        );
11323    }
11324
11325    #[test]
11326    fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale() {
11327        let array = vec![Some(123456789)];
11328        let array = create_decimal128_array(array, 24, 4).unwrap();
11329        let input_type = DataType::Decimal128(24, 4);
11330        let output_type = DataType::Decimal128(6, 2);
11331        assert!(can_cast_types(&input_type, &output_type));
11332
11333        let options = CastOptions {
11334            safe: false,
11335            ..Default::default()
11336        };
11337        let result = cast_with_options(&array, &output_type, &options);
11338        assert_eq!(
11339            result.unwrap_err().to_string(),
11340            "Invalid argument error: 12345.68 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
11341        );
11342    }
11343
11344    #[test]
11345    fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale() {
11346        let array = vec![Some(123456789)];
11347        let array = create_decimal128_array(array, 24, 2).unwrap();
11348        let input_type = DataType::Decimal128(24, 2);
11349        let output_type = DataType::Decimal128(6, 3);
11350        assert!(can_cast_types(&input_type, &output_type));
11351
11352        let options = CastOptions {
11353            safe: false,
11354            ..Default::default()
11355        };
11356        let result = cast_with_options(&array, &output_type, &options);
11357        assert_eq!(
11358            result.unwrap_err().to_string(),
11359            "Invalid argument error: 1234567.890 is too large to store in a Decimal128 of precision 6. Max is 999.999"
11360        );
11361    }
11362
11363    #[test]
11364    fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type() {
11365        let array = vec![Some(123456789)];
11366        let array = create_decimal128_array(array, 24, 2).unwrap();
11367        let input_type = DataType::Decimal128(24, 2);
11368        let output_type = DataType::Decimal256(6, 2);
11369        assert!(can_cast_types(&input_type, &output_type));
11370
11371        let options = CastOptions {
11372            safe: false,
11373            ..Default::default()
11374        };
11375        let result = cast_with_options(&array, &output_type, &options).unwrap_err();
11376        assert_eq!(
11377            result.to_string(),
11378            "Invalid argument error: 1234567.89 is too large to store in a Decimal256 of precision 6. Max is 9999.99"
11379        );
11380    }
11381
11382    #[test]
11383    fn test_first_none() {
11384        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
11385            None,
11386            Some(vec![Some(1), Some(2)]),
11387        ])) as ArrayRef;
11388        let data_type =
11389            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
11390        let opt = CastOptions::default();
11391        let r = cast_with_options(&array, &data_type, &opt).unwrap();
11392
11393        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
11394            vec![None, Some(vec![Some(1), Some(2)])],
11395            2,
11396        )) as ArrayRef;
11397        assert_eq!(*fixed_array, *r);
11398    }
11399
11400    #[test]
11401    fn test_first_last_none() {
11402        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
11403            None,
11404            Some(vec![Some(1), Some(2)]),
11405            None,
11406        ])) as ArrayRef;
11407        let data_type =
11408            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
11409        let opt = CastOptions::default();
11410        let r = cast_with_options(&array, &data_type, &opt).unwrap();
11411
11412        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
11413            vec![None, Some(vec![Some(1), Some(2)]), None],
11414            2,
11415        )) as ArrayRef;
11416        assert_eq!(*fixed_array, *r);
11417    }
11418
11419    #[test]
11420    fn test_cast_decimal_error_output() {
11421        let array = Int64Array::from(vec![1]);
11422        let error = cast_with_options(
11423            &array,
11424            &DataType::Decimal32(1, 1),
11425            &CastOptions {
11426                safe: false,
11427                format_options: FormatOptions::default(),
11428            },
11429        )
11430        .unwrap_err();
11431        assert_eq!(
11432            error.to_string(),
11433            "Invalid argument error: 1.0 is too large to store in a Decimal32 of precision 1. Max is 0.9"
11434        );
11435
11436        let array = Int64Array::from(vec![-1]);
11437        let error = cast_with_options(
11438            &array,
11439            &DataType::Decimal32(1, 1),
11440            &CastOptions {
11441                safe: false,
11442                format_options: FormatOptions::default(),
11443            },
11444        )
11445        .unwrap_err();
11446        assert_eq!(
11447            error.to_string(),
11448            "Invalid argument error: -1.0 is too small to store in a Decimal32 of precision 1. Min is -0.9"
11449        );
11450    }
11451
11452    #[test]
11453    fn test_run_end_encoded_to_primitive() {
11454        // Create a RunEndEncoded array: [1, 1, 2, 2, 2, 3]
11455        let run_ends = Int32Array::from(vec![2, 5, 6]);
11456        let values = Int32Array::from(vec![1, 2, 3]);
11457        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
11458        let array_ref = Arc::new(run_array) as ArrayRef;
11459        // Cast to Int64
11460        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
11461        // Verify the result is a RunArray with Int64 values
11462        let result_run_array = cast_result.as_any().downcast_ref::<Int64Array>().unwrap();
11463        assert_eq!(
11464            result_run_array.values(),
11465            &[1i64, 1i64, 2i64, 2i64, 2i64, 3i64]
11466        );
11467    }
11468
11469    #[test]
11470    fn test_run_end_encoded_to_string() {
11471        let run_ends = Int32Array::from(vec![2, 3, 5]);
11472        let values = Int32Array::from(vec![10, 20, 30]);
11473        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
11474        let array_ref = Arc::new(run_array) as ArrayRef;
11475
11476        // Cast to String
11477        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
11478
11479        // Verify the result is a RunArray with String values
11480        let result_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
11481        // Check that values are correct
11482        assert_eq!(result_array.value(0), "10");
11483        assert_eq!(result_array.value(1), "10");
11484        assert_eq!(result_array.value(2), "20");
11485    }
11486
11487    #[test]
11488    fn test_primitive_to_run_end_encoded() {
11489        // Create an Int32 array with repeated values: [1, 1, 2, 2, 2, 3]
11490        let source_array = Int32Array::from(vec![1, 1, 2, 2, 2, 3]);
11491        let array_ref = Arc::new(source_array) as ArrayRef;
11492
11493        // Cast to RunEndEncoded<Int32, Int32>
11494        let target_type = DataType::RunEndEncoded(
11495            Arc::new(Field::new("run_ends", DataType::Int32, false)),
11496            Arc::new(Field::new("values", DataType::Int32, true)),
11497        );
11498        let cast_result = cast(&array_ref, &target_type).unwrap();
11499
11500        // Verify the result is a RunArray
11501        let result_run_array = cast_result
11502            .as_any()
11503            .downcast_ref::<RunArray<Int32Type>>()
11504            .unwrap();
11505
11506        // Check run structure: runs should end at positions [2, 5, 6]
11507        assert_eq!(result_run_array.run_ends().values(), &[2, 5, 6]);
11508
11509        // Check values: should be [1, 2, 3]
11510        let values_array = result_run_array.values().as_primitive::<Int32Type>();
11511        assert_eq!(values_array.values(), &[1, 2, 3]);
11512    }
11513
11514    #[test]
11515    fn test_primitive_to_run_end_encoded_with_nulls() {
11516        let source_array = Int32Array::from(vec![
11517            Some(1),
11518            Some(1),
11519            None,
11520            None,
11521            Some(2),
11522            Some(2),
11523            Some(3),
11524            Some(3),
11525            None,
11526            None,
11527            Some(4),
11528            Some(4),
11529            Some(5),
11530            Some(5),
11531            None,
11532            None,
11533        ]);
11534        let array_ref = Arc::new(source_array) as ArrayRef;
11535        let target_type = DataType::RunEndEncoded(
11536            Arc::new(Field::new("run_ends", DataType::Int32, false)),
11537            Arc::new(Field::new("values", DataType::Int32, true)),
11538        );
11539        let cast_result = cast(&array_ref, &target_type).unwrap();
11540        let result_run_array = cast_result
11541            .as_any()
11542            .downcast_ref::<RunArray<Int32Type>>()
11543            .unwrap();
11544        assert_eq!(
11545            result_run_array.run_ends().values(),
11546            &[2, 4, 6, 8, 10, 12, 14, 16]
11547        );
11548        assert_eq!(
11549            result_run_array
11550                .values()
11551                .as_primitive::<Int32Type>()
11552                .values(),
11553            &[1, 0, 2, 3, 0, 4, 5, 0]
11554        );
11555        assert_eq!(result_run_array.values().null_count(), 3);
11556    }
11557
11558    #[test]
11559    fn test_primitive_to_run_end_encoded_with_nulls_consecutive() {
11560        let source_array = Int64Array::from(vec![
11561            Some(1),
11562            Some(1),
11563            None,
11564            None,
11565            None,
11566            None,
11567            None,
11568            None,
11569            None,
11570            None,
11571            Some(4),
11572            Some(20),
11573            Some(500),
11574            Some(500),
11575            None,
11576            None,
11577        ]);
11578        let array_ref = Arc::new(source_array) as ArrayRef;
11579        let target_type = DataType::RunEndEncoded(
11580            Arc::new(Field::new("run_ends", DataType::Int16, false)),
11581            Arc::new(Field::new("values", DataType::Int64, true)),
11582        );
11583        let cast_result = cast(&array_ref, &target_type).unwrap();
11584        let result_run_array = cast_result
11585            .as_any()
11586            .downcast_ref::<RunArray<Int16Type>>()
11587            .unwrap();
11588        assert_eq!(
11589            result_run_array.run_ends().values(),
11590            &[2, 10, 11, 12, 14, 16]
11591        );
11592        assert_eq!(
11593            result_run_array
11594                .values()
11595                .as_primitive::<Int64Type>()
11596                .values(),
11597            &[1, 0, 4, 20, 500, 0]
11598        );
11599        assert_eq!(result_run_array.values().null_count(), 2);
11600    }
11601
11602    #[test]
11603    fn test_string_to_run_end_encoded() {
11604        // Create a String array with repeated values: ["a", "a", "b", "c", "c"]
11605        let source_array = StringArray::from(vec!["a", "a", "b", "c", "c"]);
11606        let array_ref = Arc::new(source_array) as ArrayRef;
11607
11608        // Cast to RunEndEncoded<Int32, String>
11609        let target_type = DataType::RunEndEncoded(
11610            Arc::new(Field::new("run_ends", DataType::Int32, false)),
11611            Arc::new(Field::new("values", DataType::Utf8, true)),
11612        );
11613        let cast_result = cast(&array_ref, &target_type).unwrap();
11614
11615        // Verify the result is a RunArray
11616        let result_run_array = cast_result
11617            .as_any()
11618            .downcast_ref::<RunArray<Int32Type>>()
11619            .unwrap();
11620
11621        // Check run structure: runs should end at positions [2, 3, 5]
11622        assert_eq!(result_run_array.run_ends().values(), &[2, 3, 5]);
11623
11624        // Check values: should be ["a", "b", "c"]
11625        let values_array = result_run_array.values().as_string::<i32>();
11626        assert_eq!(values_array.value(0), "a");
11627        assert_eq!(values_array.value(1), "b");
11628        assert_eq!(values_array.value(2), "c");
11629    }
11630
11631    #[test]
11632    fn test_empty_array_to_run_end_encoded() {
11633        // Create an empty Int32 array
11634        let source_array = Int32Array::from(Vec::<i32>::new());
11635        let array_ref = Arc::new(source_array) as ArrayRef;
11636
11637        // Cast to RunEndEncoded<Int32, Int32>
11638        let target_type = DataType::RunEndEncoded(
11639            Arc::new(Field::new("run_ends", DataType::Int32, false)),
11640            Arc::new(Field::new("values", DataType::Int32, true)),
11641        );
11642        let cast_result = cast(&array_ref, &target_type).unwrap();
11643
11644        // Verify the result is an empty RunArray
11645        let result_run_array = cast_result
11646            .as_any()
11647            .downcast_ref::<RunArray<Int32Type>>()
11648            .unwrap();
11649
11650        // Check that both run_ends and values are empty
11651        assert_eq!(result_run_array.run_ends().len(), 0);
11652        assert_eq!(result_run_array.values().len(), 0);
11653    }
11654
11655    #[test]
11656    fn test_run_end_encoded_with_nulls() {
11657        // Create a RunEndEncoded array with nulls: [1, 1, null, 2, 2]
11658        let run_ends = Int32Array::from(vec![2, 3, 5]);
11659        let values = Int32Array::from(vec![Some(1), None, Some(2)]);
11660        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
11661        let array_ref = Arc::new(run_array) as ArrayRef;
11662
11663        // Cast to String
11664        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
11665
11666        // Verify the result preserves nulls
11667        let result_run_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
11668        assert_eq!(result_run_array.value(0), "1");
11669        assert!(result_run_array.is_null(2));
11670        assert_eq!(result_run_array.value(4), "2");
11671    }
11672
11673    #[test]
11674    fn test_different_index_types() {
11675        // Test with Int16 index type
11676        let source_array = Int32Array::from(vec![1, 1, 2, 3, 3]);
11677        let array_ref = Arc::new(source_array) as ArrayRef;
11678
11679        let target_type = DataType::RunEndEncoded(
11680            Arc::new(Field::new("run_ends", DataType::Int16, false)),
11681            Arc::new(Field::new("values", DataType::Int32, true)),
11682        );
11683        let cast_result = cast(&array_ref, &target_type).unwrap();
11684        assert_eq!(cast_result.data_type(), &target_type);
11685
11686        // Verify the cast worked correctly: values are [1, 2, 3]
11687        // and run-ends are [2, 3, 5]
11688        let run_array = cast_result
11689            .as_any()
11690            .downcast_ref::<RunArray<Int16Type>>()
11691            .unwrap();
11692        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
11693        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
11694        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
11695        assert_eq!(run_array.run_ends().values(), &[2i16, 3i16, 5i16]);
11696
11697        // Test again with Int64 index type
11698        let target_type = DataType::RunEndEncoded(
11699            Arc::new(Field::new("run_ends", DataType::Int64, false)),
11700            Arc::new(Field::new("values", DataType::Int32, true)),
11701        );
11702        let cast_result = cast(&array_ref, &target_type).unwrap();
11703        assert_eq!(cast_result.data_type(), &target_type);
11704
11705        // Verify the cast worked correctly: values are [1, 2, 3]
11706        // and run-ends are [2, 3, 5]
11707        let run_array = cast_result
11708            .as_any()
11709            .downcast_ref::<RunArray<Int64Type>>()
11710            .unwrap();
11711        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
11712        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
11713        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
11714        assert_eq!(run_array.run_ends().values(), &[2i64, 3i64, 5i64]);
11715    }
11716
11717    #[test]
11718    fn test_unsupported_cast_to_run_end_encoded() {
11719        // Create a Struct array - complex nested type that might not be supported
11720        let field = Field::new("item", DataType::Int32, false);
11721        let struct_array = StructArray::from(vec![(
11722            Arc::new(field),
11723            Arc::new(Int32Array::from(vec![1, 2, 3])) as ArrayRef,
11724        )]);
11725        let array_ref = Arc::new(struct_array) as ArrayRef;
11726
11727        // This should fail because:
11728        // 1. The target type is not RunEndEncoded
11729        // 2. The target type is not supported for casting from StructArray
11730        let cast_result = cast(&array_ref, &DataType::FixedSizeBinary(10));
11731
11732        // Expect this to fail
11733        assert!(cast_result.is_err());
11734    }
11735
11736    /// Test casting RunEndEncoded<Int64, String> to RunEndEncoded<Int16, String> should fail
11737    #[test]
11738    fn test_cast_run_end_encoded_int64_to_int16_should_fail() {
11739        // Construct a valid REE array with Int64 run-ends
11740        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
11741        let values = StringArray::from(vec!["a", "b", "c"]);
11742
11743        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
11744        let array_ref = Arc::new(ree_array) as ArrayRef;
11745
11746        // Attempt to cast to RunEndEncoded<Int16, Utf8>
11747        let target_type = DataType::RunEndEncoded(
11748            Arc::new(Field::new("run_ends", DataType::Int16, false)),
11749            Arc::new(Field::new("values", DataType::Utf8, true)),
11750        );
11751        let cast_options = CastOptions {
11752            safe: false, // This should make it fail instead of returning nulls
11753            format_options: FormatOptions::default(),
11754        };
11755
11756        // This should fail due to run-end overflow
11757        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
11758            cast_with_options(&array_ref, &target_type, &cast_options);
11759
11760        let e = result.expect_err("Cast should have failed but succeeded");
11761        assert!(
11762            e.to_string()
11763                .contains("Cast error: Can't cast value 100000 to type Int16")
11764        );
11765    }
11766
11767    #[test]
11768    fn test_cast_run_end_encoded_int64_to_int16_with_safe_should_fail_with_null_invalid_error() {
11769        // Construct a valid REE array with Int64 run-ends
11770        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
11771        let values = StringArray::from(vec!["a", "b", "c"]);
11772
11773        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
11774        let array_ref = Arc::new(ree_array) as ArrayRef;
11775
11776        // Attempt to cast to RunEndEncoded<Int16, Utf8>
11777        let target_type = DataType::RunEndEncoded(
11778            Arc::new(Field::new("run_ends", DataType::Int16, false)),
11779            Arc::new(Field::new("values", DataType::Utf8, true)),
11780        );
11781        let cast_options = CastOptions {
11782            safe: true,
11783            format_options: FormatOptions::default(),
11784        };
11785
11786        // This fails even though safe is true because the run_ends array has null values
11787        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
11788            cast_with_options(&array_ref, &target_type, &cast_options);
11789        let e = result.expect_err("Cast should have failed but succeeded");
11790        assert!(
11791            e.to_string()
11792                .contains("Invalid argument error: Found null values in run_ends array. The run_ends array should not have null values.")
11793        );
11794    }
11795
11796    /// Test casting RunEndEncoded<Int16, String> to RunEndEncoded<Int64, String> should succeed
11797    #[test]
11798    fn test_cast_run_end_encoded_int16_to_int64_should_succeed() {
11799        // Construct a valid REE array with Int16 run-ends
11800        let run_ends = Int16Array::from(vec![2, 5, 8]); // values that fit in Int16
11801        let values = StringArray::from(vec!["a", "b", "c"]);
11802
11803        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
11804        let array_ref = Arc::new(ree_array) as ArrayRef;
11805
11806        // Attempt to cast to RunEndEncoded<Int64, Utf8> (upcast should succeed)
11807        let target_type = DataType::RunEndEncoded(
11808            Arc::new(Field::new("run_ends", DataType::Int64, false)),
11809            Arc::new(Field::new("values", DataType::Utf8, true)),
11810        );
11811        let cast_options = CastOptions {
11812            safe: false,
11813            format_options: FormatOptions::default(),
11814        };
11815
11816        // This should succeed due to valid upcast
11817        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
11818            cast_with_options(&array_ref, &target_type, &cast_options);
11819
11820        let array_ref = result.expect("Cast should have succeeded but failed");
11821        // Downcast to RunArray<Int64Type>
11822        let run_array = array_ref
11823            .as_any()
11824            .downcast_ref::<RunArray<Int64Type>>()
11825            .unwrap();
11826
11827        // Verify the cast worked correctly
11828        // Assert the values were cast correctly
11829        assert_eq!(run_array.run_ends().values(), &[2i64, 5i64, 8i64]);
11830        assert_eq!(run_array.values().as_string::<i32>().value(0), "a");
11831        assert_eq!(run_array.values().as_string::<i32>().value(1), "b");
11832        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
11833    }
11834
11835    #[test]
11836    fn test_cast_run_end_encoded_dictionary_to_run_end_encoded() {
11837        // Construct a valid dictionary encoded array
11838        let values = StringArray::from_iter([Some("a"), Some("b"), Some("c")]);
11839        let keys = UInt64Array::from_iter(vec![1, 1, 1, 0, 0, 0, 2, 2, 2]);
11840        let array_ref = Arc::new(DictionaryArray::new(keys, Arc::new(values))) as ArrayRef;
11841
11842        // Attempt to cast to RunEndEncoded<Int64, Utf8>
11843        let target_type = DataType::RunEndEncoded(
11844            Arc::new(Field::new("run_ends", DataType::Int64, false)),
11845            Arc::new(Field::new("values", DataType::Utf8, true)),
11846        );
11847        let cast_options = CastOptions {
11848            safe: false,
11849            format_options: FormatOptions::default(),
11850        };
11851
11852        // This should succeed
11853        let result = cast_with_options(&array_ref, &target_type, &cast_options)
11854            .expect("Cast should have succeeded but failed");
11855
11856        // Verify the cast worked correctly
11857        // Assert the values were cast correctly
11858        let run_array = result
11859            .as_any()
11860            .downcast_ref::<RunArray<Int64Type>>()
11861            .unwrap();
11862        assert_eq!(run_array.values().as_string::<i32>().value(0), "b");
11863        assert_eq!(run_array.values().as_string::<i32>().value(1), "a");
11864        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
11865
11866        // Verify the run-ends were cast correctly (run ends at 3, 6, 9)
11867        assert_eq!(run_array.run_ends().values(), &[3i64, 6i64, 9i64]);
11868    }
11869}