Skip to main content

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;
46mod union;
47
48use crate::cast::decimal::*;
49use crate::cast::dictionary::*;
50use crate::cast::list::*;
51use crate::cast::map::*;
52use crate::cast::run_array::*;
53use crate::cast::string::*;
54pub use crate::cast::union::*;
55
56use arrow_buffer::IntervalMonthDayNano;
57use arrow_data::ByteView;
58use chrono::{NaiveTime, Offset, TimeZone, Utc};
59use std::cmp::Ordering;
60use std::sync::Arc;
61
62use crate::display::{ArrayFormatter, FormatOptions};
63use crate::parse::{
64    Parser, parse_interval_day_time, parse_interval_month_day_nano, parse_interval_year_month,
65    string_to_datetime,
66};
67use arrow_array::{builder::*, cast::*, temporal_conversions::*, timezone::Tz, types::*, *};
68use arrow_buffer::{ArrowNativeType, Buffer, OffsetBuffer, i256};
69use arrow_data::ArrayData;
70use arrow_data::transform::MutableArrayData;
71use arrow_schema::*;
72use arrow_select::take::take;
73use num_traits::{NumCast, ToPrimitive, cast::AsPrimitive};
74
75pub use decimal::{
76    DecimalCast, parse_string_to_decimal_native, rescale_decimal, single_float_to_decimal,
77};
78pub use string::cast_single_string_to_boolean_default;
79
80/// Lossy conversion from decimal to float.
81///
82/// Conversion is lossy and follows standard floating point semantics. Values
83/// that exceed the representable range become `INFINITY` or `-INFINITY` without
84/// returning an error.
85#[inline(always)]
86pub fn single_decimal_to_float_lossy<D, F>(f: &F, x: D::Native, scale: i32) -> f64
87where
88    D: DecimalType,
89    F: Fn(D::Native) -> f64,
90{
91    f(x) / 10_f64.powi(scale)
92}
93
94/// CastOptions provides a way to override the default cast behaviors
95#[derive(Debug, Clone, PartialEq, Eq, Hash)]
96pub struct CastOptions<'a> {
97    /// how to handle cast failures, either return NULL (safe=true) or return ERR (safe=false)
98    pub safe: bool,
99    /// Formatting options when casting from temporal types to string
100    pub format_options: FormatOptions<'a>,
101}
102
103impl Default for CastOptions<'_> {
104    fn default() -> Self {
105        Self {
106            safe: true,
107            format_options: FormatOptions::default(),
108        }
109    }
110}
111
112/// Return true if a value of type `from_type` can be cast into a value of `to_type`.
113///
114/// See [`cast_with_options`] for more information
115pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
116    use self::DataType::*;
117    use self::IntervalUnit::*;
118    use self::TimeUnit::*;
119    if from_type == to_type {
120        return true;
121    }
122
123    match (from_type, to_type) {
124        (Null, _) => true,
125        // Dictionary/List conditions should be put in front of others
126        (Dictionary(_, from_value_type), Dictionary(_, to_value_type)) => {
127            can_cast_types(from_value_type, to_value_type)
128        }
129        (Dictionary(_, value_type), _) => can_cast_types(value_type, to_type),
130        (Union(fields, _), _) => union::resolve_child_array(fields, to_type).is_some(),
131        (_, Union(_, _)) => false,
132        (RunEndEncoded(_, value_type), _) => can_cast_types(value_type.data_type(), to_type),
133        (_, RunEndEncoded(_, value_type)) => can_cast_types(from_type, value_type.data_type()),
134        (_, Dictionary(_, value_type)) => can_cast_types(from_type, value_type),
135        (
136            List(list_from) | LargeList(list_from) | ListView(list_from) | LargeListView(list_from),
137            List(list_to) | LargeList(list_to) | ListView(list_to) | LargeListView(list_to),
138        ) => can_cast_types(list_from.data_type(), list_to.data_type()),
139        (
140            List(list_from) | LargeList(list_from) | ListView(list_from) | LargeListView(list_from),
141            Utf8 | LargeUtf8 | Utf8View,
142        ) => can_cast_types(list_from.data_type(), to_type),
143        (
144            FixedSizeList(list_from, _),
145            List(list_to) | LargeList(list_to) | ListView(list_to) | LargeListView(list_to),
146        ) => can_cast_types(list_from.data_type(), list_to.data_type()),
147        (
148            List(list_from) | LargeList(list_from) | ListView(list_from) | LargeListView(list_from),
149            FixedSizeList(list_to, _),
150        ) => can_cast_types(list_from.data_type(), list_to.data_type()),
151        (FixedSizeList(inner, size), FixedSizeList(inner_to, size_to)) if size == size_to => {
152            can_cast_types(inner.data_type(), inner_to.data_type())
153        }
154        (_, List(list_to) | LargeList(list_to) | ListView(list_to) | LargeListView(list_to)) => {
155            can_cast_types(from_type, list_to.data_type())
156        }
157        (_, FixedSizeList(list_to, size)) if *size == 1 => {
158            can_cast_types(from_type, list_to.data_type())
159        }
160        (FixedSizeList(list_from, size), _) if *size == 1 => {
161            can_cast_types(list_from.data_type(), to_type)
162        }
163        (Map(from_entries, ordered_from), Map(to_entries, ordered_to))
164            if ordered_from == ordered_to =>
165        {
166            match (
167                key_field(from_entries),
168                key_field(to_entries),
169                value_field(from_entries),
170                value_field(to_entries),
171            ) {
172                (Some(from_key), Some(to_key), Some(from_value), Some(to_value)) => {
173                    can_cast_types(from_key.data_type(), to_key.data_type())
174                        && can_cast_types(from_value.data_type(), to_value.data_type())
175                }
176                _ => false,
177            }
178        }
179        // cast one decimal type to another decimal type
180        (
181            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
182            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
183        ) => true,
184        // unsigned integer to decimal
185        (
186            UInt8 | UInt16 | UInt32 | UInt64,
187            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
188        ) => true,
189        // signed numeric to decimal
190        (
191            Int8 | Int16 | Int32 | Int64 | Float16 | Float32 | Float64,
192            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
193        ) => true,
194        // decimal to unsigned numeric
195        (
196            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
197            UInt8 | UInt16 | UInt32 | UInt64,
198        ) => true,
199        // decimal to signed numeric
200        (
201            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
202            Null | Int8 | Int16 | Int32 | Int64 | Float16 | Float32 | Float64,
203        ) => true,
204        // decimal to string
205        (
206            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
207            Utf8View | Utf8 | LargeUtf8,
208        ) => true,
209        // string to decimal
210        (
211            Utf8View | Utf8 | LargeUtf8,
212            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
213        ) => true,
214        (Struct(from_fields), Struct(to_fields)) => {
215            if from_fields.len() != to_fields.len() {
216                return false;
217            }
218
219            // fast path, all field names are in the same order and same number of fields
220            if from_fields
221                .iter()
222                .zip(to_fields.iter())
223                .all(|(f1, f2)| f1.name() == f2.name())
224            {
225                return 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
232            // slow path, we match the fields by name
233            if to_fields.iter().all(|to_field| {
234                from_fields
235                    .iter()
236                    .find(|from_field| from_field.name() == to_field.name())
237                    .is_some_and(|from_field| {
238                        // Assume that nullability between two structs are compatible, if not,
239                        // cast kernel will return error.
240                        can_cast_types(from_field.data_type(), to_field.data_type())
241                    })
242            }) {
243                return true;
244            }
245
246            // if we couldn't match by name, we try to see if they can be matched by position
247            from_fields
248                .iter()
249                .zip(to_fields.iter())
250                .all(|(f1, f2)| can_cast_types(f1.data_type(), f2.data_type()))
251        }
252        (Struct(_), _) => false,
253        (_, Struct(_)) => false,
254        (_, Boolean) => from_type.is_integer() || from_type.is_floating() || from_type.is_string(),
255        (Boolean, _) => to_type.is_integer() || to_type.is_floating() || to_type.is_string(),
256
257        (Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
258            true
259        }
260        (LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
261            true
262        }
263        (FixedSizeBinary(_), Binary | LargeBinary | BinaryView) => true,
264        (
265            Utf8 | LargeUtf8 | Utf8View,
266            Binary
267            | LargeBinary
268            | Utf8
269            | LargeUtf8
270            | Date32
271            | Date64
272            | Time32(Second | Millisecond)
273            | Time64(Microsecond | Nanosecond)
274            | Timestamp(Second | Millisecond | Microsecond | Nanosecond, _)
275            | Interval(_)
276            | BinaryView,
277        ) => true,
278        (Utf8 | LargeUtf8, Utf8View) => true,
279        (BinaryView, Binary | LargeBinary | Utf8 | LargeUtf8 | Utf8View) => true,
280        (Utf8View | Utf8 | LargeUtf8, _) => to_type.is_numeric(),
281        (_, Utf8 | Utf8View | LargeUtf8) => from_type.is_primitive(),
282
283        (_, Binary | LargeBinary) => from_type.is_integer(),
284
285        // start numeric casts
286        (
287            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
288            | Float64,
289            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
290            | Float64,
291        ) => true,
292        // end numeric casts
293
294        // temporal casts
295        (Int32, Date32 | Date64 | Time32(_)) => true,
296        (Date32, Int32 | Int64) => true,
297        (Time32(_), Int32 | Int64) => true,
298        (Int64, Date64 | Date32 | Time64(_)) => true,
299        (Date64, Int64 | Int32) => true,
300        (Time64(_), Int64) => true,
301        (Date32 | Date64, Date32 | Date64) => true,
302        // time casts
303        (Time32(_), Time32(_)) => true,
304        (Time32(_), Time64(_)) => true,
305        (Time64(_), Time64(_)) => true,
306        (Time64(_), Time32(to_unit)) => {
307            matches!(to_unit, Second | Millisecond)
308        }
309        (Timestamp(_, _), _) if to_type.is_numeric() => true,
310        (_, Timestamp(_, _)) if from_type.is_numeric() => true,
311        (Date64, Timestamp(_, _)) => true,
312        (Date32, Timestamp(_, _)) => true,
313        (
314            Timestamp(_, _),
315            Timestamp(_, _)
316            | Date32
317            | Date64
318            | Time32(Second | Millisecond)
319            | Time64(Microsecond | Nanosecond),
320        ) => true,
321        (_, Duration(_)) if from_type.is_numeric() => true,
322        (Duration(_), _) if to_type.is_numeric() => true,
323        (Duration(_), Duration(_)) => true,
324        (Interval(from_type), Int64) => {
325            match from_type {
326                YearMonth => true,
327                DayTime => true,
328                MonthDayNano => false, // Native type is i128
329            }
330        }
331        (Int32, Interval(to_type)) => match to_type {
332            YearMonth => true,
333            DayTime => false,
334            MonthDayNano => false,
335        },
336        (Duration(_), Interval(MonthDayNano)) => true,
337        (Interval(MonthDayNano), Duration(_)) => true,
338        (Interval(YearMonth), Interval(MonthDayNano)) => true,
339        (Interval(DayTime), Interval(MonthDayNano)) => true,
340        (_, _) => false,
341    }
342}
343
344/// Cast `array` to the provided data type and return a new Array with type `to_type`, if possible.
345///
346/// See [`cast_with_options`] for more information
347pub fn cast(array: &dyn Array, to_type: &DataType) -> Result<ArrayRef, ArrowError> {
348    cast_with_options(array, to_type, &CastOptions::default())
349}
350
351/// Convert an integer to a decimal native value without wrapping.
352///
353/// `AsPrimitive` / `as` silently truncates when the source is wider than `M`
354/// (for example `5_000_000_000i64 as i32`). All integer sources fit in `i128`
355/// losslessly, which [`DecimalCast`] then converts to the decimal native type
356/// with a range check. For types that always fit (e.g. `i64` to `Decimal128`) this
357/// should get optimized to being equivalent to `i64 as i128`.
358fn integer_to_decimal_native<I, M>(value: I) -> Option<M>
359where
360    I: Into<i128>,
361    M: DecimalCast,
362{
363    M::from_decimal(value.into())
364}
365
366fn cast_integer_to_decimal<
367    T: ArrowPrimitiveType,
368    D: DecimalType + ArrowPrimitiveType<Native = M>,
369    M,
370>(
371    array: &PrimitiveArray<T>,
372    precision: u8,
373    scale: i8,
374    base: M,
375    cast_options: &CastOptions,
376) -> Result<ArrayRef, ArrowError>
377where
378    <T as ArrowPrimitiveType>::Native: ArrowNativeTypeOp + Into<i128>,
379    M: ArrowNativeTypeOp + DecimalCast,
380{
381    let overflow = |v: T::Native| {
382        ArrowError::CastError(format!(
383            "Cannot cast to {}({precision}, {scale}). Overflowing on {v:?}",
384            D::PREFIX,
385        ))
386    };
387
388    let array = if scale < 0 {
389        // Compute the scale factor once in the source type. Scaling before the
390        // checked conversion permits values that only fit the decimal native
391        // type after scaling.
392        let scale_factor = T::Native::usize_as(10)
393            .pow_checked(scale.unsigned_abs() as u32)
394            .ok();
395
396        match (scale_factor, cast_options.safe) {
397            (Some(scale_factor), true) => array.unary_opt::<_, D>(|v| {
398                let v = v
399                    .div_checked(scale_factor)
400                    .ok()
401                    .and_then(integer_to_decimal_native::<_, M>)?;
402                (D::is_valid_decimal_precision(v, precision)).then_some(v)
403            }),
404            (Some(scale_factor), false) => array.try_unary::<_, D, _>(|v| {
405                let v = v
406                    .div_checked(scale_factor)
407                    .ok()
408                    .and_then(integer_to_decimal_native::<_, M>)
409                    .ok_or_else(|| overflow(v))?;
410                D::validate_decimal_precision(v, precision, scale).map(|()| v)
411            })?,
412            // A scale factor that overflows the source type is larger than all
413            // source values, so integer division produces zero.
414            //
415            // For a well formed decimal scale, this path should never be reachable.
416            (None, _) => array.unary::<_, D>(|_| M::ZERO),
417        }
418    } else {
419        let scale_factor = base.pow_checked(scale.unsigned_abs() as u32).map_err(|_| {
420            ArrowError::CastError(format!(
421                "Cannot cast to {:?}({}, {}). The scale causes overflow.",
422                D::PREFIX,
423                precision,
424                scale,
425            ))
426        })?;
427
428        match cast_options.safe {
429            true => array.unary_opt::<_, D>(|v| {
430                let v = integer_to_decimal_native::<_, M>(v)
431                    .and_then(|v| v.mul_checked(scale_factor).ok())?;
432                (D::is_valid_decimal_precision(v, precision)).then_some(v)
433            }),
434            false => array.try_unary::<_, D, _>(|v| {
435                let v = integer_to_decimal_native::<_, M>(v)
436                    .ok_or_else(|| overflow(v))
437                    .and_then(|v| v.mul_checked(scale_factor))?;
438                D::validate_decimal_precision(v, precision, scale).map(|()| v)
439            })?,
440        }
441    };
442
443    Ok(Arc::new(array.with_precision_and_scale(precision, scale)?))
444}
445
446/// Cast the array from interval year month to month day nano
447fn cast_interval_year_month_to_interval_month_day_nano(
448    array: &dyn Array,
449    _cast_options: &CastOptions,
450) -> Result<ArrayRef, ArrowError> {
451    let array = array.as_primitive::<IntervalYearMonthType>();
452
453    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
454        let months = IntervalYearMonthType::to_months(v);
455        IntervalMonthDayNanoType::make_value(months, 0, 0)
456    })))
457}
458
459/// Cast the array from interval day time to month day nano
460fn cast_interval_day_time_to_interval_month_day_nano(
461    array: &dyn Array,
462    _cast_options: &CastOptions,
463) -> Result<ArrayRef, ArrowError> {
464    let array = array.as_primitive::<IntervalDayTimeType>();
465    let mul = 1_000_000;
466
467    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
468        let (days, ms) = IntervalDayTimeType::to_parts(v);
469        IntervalMonthDayNanoType::make_value(0, days, ms as i64 * mul)
470    })))
471}
472
473/// Cast the array from interval to duration
474fn cast_month_day_nano_to_duration<D: ArrowTemporalType<Native = i64>>(
475    array: &dyn Array,
476    cast_options: &CastOptions,
477) -> Result<ArrayRef, ArrowError> {
478    let array = array.as_primitive::<IntervalMonthDayNanoType>();
479    let scale = match D::DATA_TYPE {
480        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
481        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
482        DataType::Duration(TimeUnit::Microsecond) => 1_000,
483        DataType::Duration(TimeUnit::Nanosecond) => 1,
484        _ => unreachable!(),
485    };
486
487    if cast_options.safe {
488        let iter = array.iter().map(|v| {
489            let v = v?;
490            (v.days == 0 && v.months == 0).then_some(v.nanoseconds / scale)
491        });
492        Ok(Arc::new(unsafe {
493            PrimitiveArray::<D>::from_trusted_len_iter(iter)
494        }))
495    } else {
496        let vec = array
497            .iter()
498            .map(|v| {
499                v.map(|v| match v.days == 0 && v.months == 0 {
500                    true => Ok((v.nanoseconds) / scale),
501                    _ => Err(ArrowError::ComputeError(
502                        "Cannot convert interval containing non-zero months or days to duration"
503                            .to_string(),
504                    )),
505                })
506                .transpose()
507            })
508            .collect::<Result<Vec<_>, _>>()?;
509        Ok(Arc::new(unsafe {
510            PrimitiveArray::<D>::from_trusted_len_iter(vec.iter())
511        }))
512    }
513}
514
515/// Cast the array from duration and interval
516fn cast_duration_to_interval<D: ArrowTemporalType<Native = i64>>(
517    array: &dyn Array,
518    cast_options: &CastOptions,
519) -> Result<ArrayRef, ArrowError> {
520    let array = array
521        .as_any()
522        .downcast_ref::<PrimitiveArray<D>>()
523        .ok_or_else(|| {
524            ArrowError::ComputeError(
525                "Internal Error: Cannot cast duration to DurationArray of expected type"
526                    .to_string(),
527            )
528        })?;
529
530    let scale = match array.data_type() {
531        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
532        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
533        DataType::Duration(TimeUnit::Microsecond) => 1_000,
534        DataType::Duration(TimeUnit::Nanosecond) => 1,
535        _ => unreachable!(),
536    };
537
538    if cast_options.safe {
539        let iter = array.iter().map(|v| {
540            v?.checked_mul(scale)
541                .map(|v| IntervalMonthDayNano::new(0, 0, v))
542        });
543        Ok(Arc::new(unsafe {
544            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(iter)
545        }))
546    } else {
547        let vec = array
548            .iter()
549            .map(|v| {
550                v.map(|v| {
551                    if let Ok(v) = v.mul_checked(scale) {
552                        Ok(IntervalMonthDayNano::new(0, 0, v))
553                    } else {
554                        Err(ArrowError::ComputeError(format!(
555                            "Cannot cast to {:?}. Overflowing on {:?}",
556                            IntervalMonthDayNanoType::DATA_TYPE,
557                            v
558                        )))
559                    }
560                })
561                .transpose()
562            })
563            .collect::<Result<Vec<_>, _>>()?;
564        Ok(Arc::new(unsafe {
565            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(vec.iter())
566        }))
567    }
568}
569
570/// Cast the primitive array using [`PrimitiveArray::reinterpret_cast`]
571fn cast_reinterpret_arrays<I: ArrowPrimitiveType, O: ArrowPrimitiveType<Native = I::Native>>(
572    array: &dyn Array,
573) -> Result<ArrayRef, ArrowError> {
574    Ok(Arc::new(array.as_primitive::<I>().reinterpret_cast::<O>()))
575}
576
577fn make_timestamp_array(
578    array: &PrimitiveArray<Int64Type>,
579    unit: TimeUnit,
580    tz: Option<Arc<str>>,
581) -> ArrayRef {
582    match unit {
583        TimeUnit::Second => Arc::new(
584            array
585                .reinterpret_cast::<TimestampSecondType>()
586                .with_timezone_opt(tz),
587        ),
588        TimeUnit::Millisecond => Arc::new(
589            array
590                .reinterpret_cast::<TimestampMillisecondType>()
591                .with_timezone_opt(tz),
592        ),
593        TimeUnit::Microsecond => Arc::new(
594            array
595                .reinterpret_cast::<TimestampMicrosecondType>()
596                .with_timezone_opt(tz),
597        ),
598        TimeUnit::Nanosecond => Arc::new(
599            array
600                .reinterpret_cast::<TimestampNanosecondType>()
601                .with_timezone_opt(tz),
602        ),
603    }
604}
605
606fn make_duration_array(array: &PrimitiveArray<Int64Type>, unit: TimeUnit) -> ArrayRef {
607    match unit {
608        TimeUnit::Second => Arc::new(array.reinterpret_cast::<DurationSecondType>()),
609        TimeUnit::Millisecond => Arc::new(array.reinterpret_cast::<DurationMillisecondType>()),
610        TimeUnit::Microsecond => Arc::new(array.reinterpret_cast::<DurationMicrosecondType>()),
611        TimeUnit::Nanosecond => Arc::new(array.reinterpret_cast::<DurationNanosecondType>()),
612    }
613}
614
615fn as_time_res_with_timezone<T: ArrowPrimitiveType>(
616    v: i64,
617    tz: Option<Tz>,
618) -> Result<NaiveTime, ArrowError> {
619    let time = match tz {
620        Some(tz) => as_datetime_with_timezone::<T>(v, tz).map(|d| d.time()),
621        None => as_datetime::<T>(v).map(|d| d.time()),
622    };
623
624    time.ok_or_else(|| {
625        ArrowError::CastError(format!(
626            "Failed to create naive time with {} {}",
627            std::any::type_name::<T>(),
628            v
629        ))
630    })
631}
632
633fn timestamp_to_date32<T: ArrowTimestampType>(
634    array: &PrimitiveArray<T>,
635) -> Result<ArrayRef, ArrowError> {
636    let err = |x: i64| {
637        ArrowError::CastError(format!(
638            "Cannot convert {} {x} to datetime",
639            std::any::type_name::<T>()
640        ))
641    };
642
643    let array: Date32Array = match array.timezone() {
644        Some(tz) => {
645            let tz: Tz = tz.parse()?;
646            array.try_unary(|x| {
647                as_datetime_with_timezone::<T>(x, tz)
648                    .ok_or_else(|| err(x))
649                    .map(|d| Date32Type::from_naive_date(d.date_naive()))
650            })?
651        }
652        None => array.try_unary(|x| {
653            as_datetime::<T>(x)
654                .ok_or_else(|| err(x))
655                .map(|d| Date32Type::from_naive_date(d.date()))
656        })?,
657    };
658    Ok(Arc::new(array))
659}
660
661/// Try to cast `array` to `to_type` if possible.
662///
663/// Returns a new Array with type `to_type` if possible.
664///
665/// Accepts [`CastOptions`] to specify cast behavior. See also [`cast()`].
666///
667/// # Behavior
668/// * `Boolean` to `Utf8`: `true` => '1', `false` => `0`
669/// * `Utf8` to `Boolean`: `true`, `yes`, `on`, `1` => `true`, `false`, `no`, `off`, `0` => `false`,
670///   short variants are accepted, other strings return null or error
671/// * `Utf8` to Numeric: strings that can't be parsed to numbers return null, float strings
672///   in integer casts return null
673/// * Numeric to `Boolean`: 0 returns `false`, any other value returns `true`
674/// * `List` to `List`: the underlying data type is cast
675/// * `List` to `FixedSizeList`: the underlying data type is cast. If safe is true and a list element
676///   has the wrong length it will be replaced with NULL, otherwise an error will be returned
677/// * Primitive to `List`: a list array with 1 value per slot is created
678/// * `Date32` and `Date64`: precision lost when going to higher interval
679/// * `Time32` and `Time64`: precision lost when going to higher interval
680/// * `Timestamp` and `Date{32|64}`: precision lost when going to higher interval
681/// * Temporal to/from backing Primitive: zero-copy with data type change
682/// * `Float16/Float32/Float64` to `Decimal(precision, scale)` rounds to the `scale` decimals
683///   (i.e. casting `6.4999` to `Decimal(10, 1)` becomes `6.5`).
684/// * `Decimal` to `Float16/Float32/Float64` is lossy and values outside the representable
685///   range become `INFINITY` or `-INFINITY` without error.
686///
687/// Unsupported Casts (check with `can_cast_types` before calling):
688/// * To or from `StructArray`
689/// * `List` to `Primitive`
690/// * `Interval` and `Duration`
691///
692/// # Durations and Intervals
693///
694/// Casting integer types directly to interval types such as
695/// [`IntervalMonthDayNano`] is not supported because the meaning of the integer
696/// is ambiguous. For example, the integer  could represent either nanoseconds
697/// or months.
698///
699/// To cast an integer type to an interval type, first convert to a Duration
700/// type, and then cast that to the desired interval type.
701///
702/// For example, to convert an `Int64` representing nanoseconds to an
703/// `IntervalMonthDayNano` you would first convert the `Int64` to a
704/// `DurationNanoseconds`, and then cast that to `IntervalMonthDayNano`.
705///
706/// # Timestamps and Timezones
707///
708/// Timestamps are stored with an optional timezone in Arrow.
709///
710/// ## Casting timestamps to a timestamp without timezone / UTC
711/// ```
712/// # use arrow_array::Int64Array;
713/// # use arrow_array::types::TimestampSecondType;
714/// # use arrow_cast::{cast, display};
715/// # use arrow_array::cast::AsArray;
716/// # use arrow_schema::{DataType, TimeUnit};
717/// // can use "UTC" if chrono-tz feature is enabled, here use offset based timezone
718/// let data_type = DataType::Timestamp(TimeUnit::Second, None);
719/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
720/// let b = cast(&a, &data_type).unwrap();
721/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
722/// assert_eq!(2_000_000_000, b.value(1)); // values are the same as the type has no timezone
723/// // use display to show them (note has no trailing Z)
724/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
725/// ```
726///
727/// ## Casting timestamps to a timestamp with timezone
728///
729/// Similarly to the previous example, if you cast numeric values to a timestamp
730/// with timezone, the cast kernel will not change the underlying values
731/// but display and other functions will interpret them as being in the provided timezone.
732///
733/// ```
734/// # use arrow_array::Int64Array;
735/// # use arrow_array::types::TimestampSecondType;
736/// # use arrow_cast::{cast, display};
737/// # use arrow_array::cast::AsArray;
738/// # use arrow_schema::{DataType, TimeUnit};
739/// // can use "Americas/New_York" if chrono-tz feature is enabled, here use offset based timezone
740/// let data_type = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
741/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
742/// let b = cast(&a, &data_type).unwrap();
743/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
744/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
745/// // displayed in the target timezone (note the offset -05:00)
746/// assert_eq!("2033-05-17T22:33:20-05:00", display::array_value_to_string(&b, 1).unwrap());
747/// ```
748/// # Casting timestamps without timezone to timestamps with timezone
749///
750/// When casting from a timestamp without timezone to a timestamp with
751/// timezone, the cast kernel interprets the timestamp values as being in
752/// the destination timezone and then adjusts the underlying value to UTC as required
753///
754/// However, note that when casting from a timestamp with timezone BACK to a
755/// timestamp without timezone the cast kernel does not adjust the values.
756///
757/// Thus round trip casting a timestamp without timezone to a timestamp with
758/// timezone and back to a timestamp without timezone results in different
759/// values than the starting values.
760///
761/// ```
762/// # use arrow_array::Int64Array;
763/// # use arrow_array::types::{TimestampSecondType};
764/// # use arrow_cast::{cast, display};
765/// # use arrow_array::cast::AsArray;
766/// # use arrow_schema::{DataType, TimeUnit};
767/// let data_type  = DataType::Timestamp(TimeUnit::Second, None);
768/// let data_type_tz = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
769/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
770/// let b = cast(&a, &data_type).unwrap(); // cast to timestamp without timezone
771/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
772/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
773/// // displayed without a timezone (note lack of offset or Z)
774/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
775///
776/// // Convert timestamps without a timezone to timestamps with a timezone
777/// let c = cast(&b, &data_type_tz).unwrap();
778/// let c = c.as_primitive::<TimestampSecondType>(); // downcast to result type
779/// assert_eq!(2_000_018_000, c.value(1)); // value has been adjusted by offset
780/// // displayed with the target timezone offset (-05:00)
781/// assert_eq!("2033-05-18T03:33:20-05:00", display::array_value_to_string(&c, 1).unwrap());
782///
783/// // Convert from timestamp with timezone back to timestamp without timezone
784/// let d = cast(&c, &data_type).unwrap();
785/// let d = d.as_primitive::<TimestampSecondType>(); // downcast to result type
786/// assert_eq!(2_000_018_000, d.value(1)); // value has not been adjusted
787/// // NOTE: the timestamp is adjusted (08:33:20 instead of 03:33:20 as in previous example)
788/// assert_eq!("2033-05-18T08:33:20", display::array_value_to_string(&d, 1).unwrap());
789/// ```
790pub fn cast_with_options(
791    array: &dyn Array,
792    to_type: &DataType,
793    cast_options: &CastOptions,
794) -> Result<ArrayRef, ArrowError> {
795    use DataType::*;
796    let from_type = array.data_type();
797    // clone array if types are the same
798    if from_type == to_type {
799        return Ok(make_array(array.to_data()));
800    }
801    match (from_type, to_type) {
802        (Null, _) => Ok(new_null_array(to_type, array.len())),
803        (RunEndEncoded(index_type, _), _) => match index_type.data_type() {
804            Int16 => run_end_encoded_cast::<Int16Type>(array, to_type, cast_options),
805            Int32 => run_end_encoded_cast::<Int32Type>(array, to_type, cast_options),
806            Int64 => run_end_encoded_cast::<Int64Type>(array, to_type, cast_options),
807            _ => Err(ArrowError::CastError(format!(
808                "Casting from run end encoded type {from_type:?} to {to_type:?} not supported",
809            ))),
810        },
811        (_, RunEndEncoded(index_type, value_type)) => {
812            let array_ref = make_array(array.to_data());
813            match index_type.data_type() {
814                Int16 => cast_to_run_end_encoded::<Int16Type>(
815                    &array_ref,
816                    value_type.data_type(),
817                    cast_options,
818                ),
819                Int32 => cast_to_run_end_encoded::<Int32Type>(
820                    &array_ref,
821                    value_type.data_type(),
822                    cast_options,
823                ),
824                Int64 => cast_to_run_end_encoded::<Int64Type>(
825                    &array_ref,
826                    value_type.data_type(),
827                    cast_options,
828                ),
829                _ => Err(ArrowError::CastError(format!(
830                    "Casting from type {from_type:?} to run end encoded type {to_type:?} not supported",
831                ))),
832            }
833        }
834        (Union(_, _), _) => union_extract_by_type(
835            array.as_any().downcast_ref::<UnionArray>().unwrap(),
836            to_type,
837            cast_options,
838        ),
839        (_, Union(_, _)) => Err(ArrowError::CastError(format!(
840            "Casting from {from_type} to {to_type} not supported"
841        ))),
842        (Dictionary(index_type, _), _) => match **index_type {
843            Int8 => dictionary_cast::<Int8Type>(array, to_type, cast_options),
844            Int16 => dictionary_cast::<Int16Type>(array, to_type, cast_options),
845            Int32 => dictionary_cast::<Int32Type>(array, to_type, cast_options),
846            Int64 => dictionary_cast::<Int64Type>(array, to_type, cast_options),
847            UInt8 => dictionary_cast::<UInt8Type>(array, to_type, cast_options),
848            UInt16 => dictionary_cast::<UInt16Type>(array, to_type, cast_options),
849            UInt32 => dictionary_cast::<UInt32Type>(array, to_type, cast_options),
850            UInt64 => dictionary_cast::<UInt64Type>(array, to_type, cast_options),
851            _ => Err(ArrowError::CastError(format!(
852                "Casting from dictionary type {from_type} to {to_type} not supported",
853            ))),
854        },
855        (_, Dictionary(index_type, value_type)) => match **index_type {
856            Int8 => cast_to_dictionary::<Int8Type>(array, value_type, cast_options),
857            Int16 => cast_to_dictionary::<Int16Type>(array, value_type, cast_options),
858            Int32 => cast_to_dictionary::<Int32Type>(array, value_type, cast_options),
859            Int64 => cast_to_dictionary::<Int64Type>(array, value_type, cast_options),
860            UInt8 => cast_to_dictionary::<UInt8Type>(array, value_type, cast_options),
861            UInt16 => cast_to_dictionary::<UInt16Type>(array, value_type, cast_options),
862            UInt32 => cast_to_dictionary::<UInt32Type>(array, value_type, cast_options),
863            UInt64 => cast_to_dictionary::<UInt64Type>(array, value_type, cast_options),
864            _ => Err(ArrowError::CastError(format!(
865                "Casting from type {from_type} to dictionary type {to_type} not supported",
866            ))),
867        },
868        // Casting between lists of same types (cast inner values)
869        (List(_), List(to)) => cast_list_values::<i32>(array, to, cast_options),
870        (LargeList(_), LargeList(to)) => cast_list_values::<i64>(array, to, cast_options),
871        (FixedSizeList(_, size_from), FixedSizeList(list_to, size_to)) => {
872            if size_from != size_to {
873                return Err(ArrowError::CastError(
874                    "cannot cast fixed-size-list to fixed-size-list with different size".into(),
875                ));
876            }
877            let array = array.as_fixed_size_list();
878            let values = cast_with_options(array.values(), list_to.data_type(), cast_options)?;
879            Ok(Arc::new(FixedSizeListArray::try_new(
880                list_to.clone(),
881                *size_from,
882                values,
883                array.nulls().cloned(),
884            )?))
885        }
886        (ListView(_), ListView(to)) => cast_list_view_values::<i32>(array, to, cast_options),
887        (LargeListView(_), LargeListView(to)) => {
888            cast_list_view_values::<i64>(array, to, cast_options)
889        }
890        // Casting between different types of lists
891        // List
892        (List(_), LargeList(list_to)) => cast_list::<i32, i64>(array, list_to, cast_options),
893        (List(_), FixedSizeList(field, size)) => {
894            cast_list_to_fixed_size_list::<i32>(array, field, *size, cast_options)
895        }
896        (List(_), ListView(list_to)) => {
897            cast_list_to_list_view::<i32, i32>(array, list_to, cast_options)
898        }
899        (List(_), LargeListView(list_to)) => {
900            cast_list_to_list_view::<i32, i64>(array, list_to, cast_options)
901        }
902        // LargeList
903        (LargeList(_), List(list_to)) => cast_list::<i64, i32>(array, list_to, cast_options),
904        (LargeList(_), FixedSizeList(field, size)) => {
905            cast_list_to_fixed_size_list::<i64>(array, field, *size, cast_options)
906        }
907        (LargeList(_), ListView(list_to)) => {
908            cast_list_to_list_view::<i64, i32>(array, list_to, cast_options)
909        }
910        (LargeList(_), LargeListView(list_to)) => {
911            cast_list_to_list_view::<i64, i64>(array, list_to, cast_options)
912        }
913        // ListView
914        (ListView(_), List(list_to)) => {
915            cast_list_view_to_list::<i32, Int32Type>(array, list_to, cast_options)
916        }
917        (ListView(_), LargeList(list_to)) => {
918            cast_list_view_to_list::<i32, Int64Type>(array, list_to, cast_options)
919        }
920        (ListView(_), LargeListView(list_to)) => {
921            cast_list_view::<i32, i64>(array, list_to, cast_options)
922        }
923        (ListView(_), FixedSizeList(field, size)) => {
924            cast_list_view_to_fixed_size_list::<i32>(array, field, *size, cast_options)
925        }
926        // LargeListView
927        (LargeListView(_), LargeList(list_to)) => {
928            cast_list_view_to_list::<i64, Int64Type>(array, list_to, cast_options)
929        }
930        (LargeListView(_), List(list_to)) => {
931            cast_list_view_to_list::<i64, Int32Type>(array, list_to, cast_options)
932        }
933        (LargeListView(_), ListView(list_to)) => {
934            cast_list_view::<i64, i32>(array, list_to, cast_options)
935        }
936        (LargeListView(_), FixedSizeList(field, size)) => {
937            cast_list_view_to_fixed_size_list::<i64>(array, field, *size, cast_options)
938        }
939        // FixedSizeList
940        (FixedSizeList(_, _), List(list_to)) => {
941            cast_fixed_size_list_to_list::<i32>(array, list_to, cast_options)
942        }
943        (FixedSizeList(_, _), LargeList(list_to)) => {
944            cast_fixed_size_list_to_list::<i64>(array, list_to, cast_options)
945        }
946        (FixedSizeList(_, _), ListView(list_to)) => {
947            cast_fixed_size_list_to_list_view::<i32>(array, list_to, cast_options)
948        }
949        (FixedSizeList(_, _), LargeListView(list_to)) => {
950            cast_fixed_size_list_to_list_view::<i64>(array, list_to, cast_options)
951        }
952        // List to/from other types
953        (FixedSizeList(_, size), _) if *size == 1 => {
954            cast_single_element_fixed_size_list_to_values(array, to_type, cast_options)
955        }
956        // NOTE: we could support FSL to string here too but might be confusing
957        //       since behaviour for size 1 would be different (see arm above)
958        (List(_) | LargeList(_) | ListView(_) | LargeListView(_), _) => match to_type {
959            Utf8 => value_to_string::<i32>(array, cast_options),
960            LargeUtf8 => value_to_string::<i64>(array, cast_options),
961            Utf8View => value_to_string_view(array, cast_options),
962            dt => Err(ArrowError::CastError(format!(
963                "Cannot cast LIST to non-list data type {dt}"
964            ))),
965        },
966        (_, List(to)) => cast_values_to_list::<i32>(array, to, cast_options),
967        (_, LargeList(to)) => cast_values_to_list::<i64>(array, to, cast_options),
968        (_, ListView(to)) => cast_values_to_list_view::<i32>(array, to, cast_options),
969        (_, LargeListView(to)) => cast_values_to_list_view::<i64>(array, to, cast_options),
970        (_, FixedSizeList(to, size)) if *size == 1 => {
971            let values = cast_with_options(array, to.data_type(), cast_options)?;
972            let list = FixedSizeListArray::try_new(to.clone(), 1, values, None)?;
973            Ok(Arc::new(list))
974        }
975        // Map
976        (Map(_, ordered1), Map(_, ordered2)) if ordered1 == ordered2 => {
977            cast_map_values(array.as_map(), to_type, cast_options, ordered1.to_owned())
978        }
979        // Decimal to decimal, same width
980        (Decimal32(p1, s1), Decimal32(p2, s2)) => {
981            cast_decimal_to_decimal_same_type::<Decimal32Type>(
982                array.as_primitive(),
983                *p1,
984                *s1,
985                *p2,
986                *s2,
987                cast_options,
988            )
989        }
990        (Decimal64(p1, s1), Decimal64(p2, s2)) => {
991            cast_decimal_to_decimal_same_type::<Decimal64Type>(
992                array.as_primitive(),
993                *p1,
994                *s1,
995                *p2,
996                *s2,
997                cast_options,
998            )
999        }
1000        (Decimal128(p1, s1), Decimal128(p2, s2)) => {
1001            cast_decimal_to_decimal_same_type::<Decimal128Type>(
1002                array.as_primitive(),
1003                *p1,
1004                *s1,
1005                *p2,
1006                *s2,
1007                cast_options,
1008            )
1009        }
1010        (Decimal256(p1, s1), Decimal256(p2, s2)) => {
1011            cast_decimal_to_decimal_same_type::<Decimal256Type>(
1012                array.as_primitive(),
1013                *p1,
1014                *s1,
1015                *p2,
1016                *s2,
1017                cast_options,
1018            )
1019        }
1020        // Decimal to decimal, different width
1021        (Decimal32(p1, s1), Decimal64(p2, s2)) => {
1022            cast_decimal_to_decimal::<Decimal32Type, Decimal64Type>(
1023                array.as_primitive(),
1024                *p1,
1025                *s1,
1026                *p2,
1027                *s2,
1028                cast_options,
1029            )
1030        }
1031        (Decimal32(p1, s1), Decimal128(p2, s2)) => {
1032            cast_decimal_to_decimal::<Decimal32Type, Decimal128Type>(
1033                array.as_primitive(),
1034                *p1,
1035                *s1,
1036                *p2,
1037                *s2,
1038                cast_options,
1039            )
1040        }
1041        (Decimal32(p1, s1), Decimal256(p2, s2)) => {
1042            cast_decimal_to_decimal::<Decimal32Type, Decimal256Type>(
1043                array.as_primitive(),
1044                *p1,
1045                *s1,
1046                *p2,
1047                *s2,
1048                cast_options,
1049            )
1050        }
1051        (Decimal64(p1, s1), Decimal32(p2, s2)) => {
1052            cast_decimal_to_decimal::<Decimal64Type, Decimal32Type>(
1053                array.as_primitive(),
1054                *p1,
1055                *s1,
1056                *p2,
1057                *s2,
1058                cast_options,
1059            )
1060        }
1061        (Decimal64(p1, s1), Decimal128(p2, s2)) => {
1062            cast_decimal_to_decimal::<Decimal64Type, Decimal128Type>(
1063                array.as_primitive(),
1064                *p1,
1065                *s1,
1066                *p2,
1067                *s2,
1068                cast_options,
1069            )
1070        }
1071        (Decimal64(p1, s1), Decimal256(p2, s2)) => {
1072            cast_decimal_to_decimal::<Decimal64Type, Decimal256Type>(
1073                array.as_primitive(),
1074                *p1,
1075                *s1,
1076                *p2,
1077                *s2,
1078                cast_options,
1079            )
1080        }
1081        (Decimal128(p1, s1), Decimal32(p2, s2)) => {
1082            cast_decimal_to_decimal::<Decimal128Type, Decimal32Type>(
1083                array.as_primitive(),
1084                *p1,
1085                *s1,
1086                *p2,
1087                *s2,
1088                cast_options,
1089            )
1090        }
1091        (Decimal128(p1, s1), Decimal64(p2, s2)) => {
1092            cast_decimal_to_decimal::<Decimal128Type, Decimal64Type>(
1093                array.as_primitive(),
1094                *p1,
1095                *s1,
1096                *p2,
1097                *s2,
1098                cast_options,
1099            )
1100        }
1101        (Decimal128(p1, s1), Decimal256(p2, s2)) => {
1102            cast_decimal_to_decimal::<Decimal128Type, Decimal256Type>(
1103                array.as_primitive(),
1104                *p1,
1105                *s1,
1106                *p2,
1107                *s2,
1108                cast_options,
1109            )
1110        }
1111        (Decimal256(p1, s1), Decimal32(p2, s2)) => {
1112            cast_decimal_to_decimal::<Decimal256Type, Decimal32Type>(
1113                array.as_primitive(),
1114                *p1,
1115                *s1,
1116                *p2,
1117                *s2,
1118                cast_options,
1119            )
1120        }
1121        (Decimal256(p1, s1), Decimal64(p2, s2)) => {
1122            cast_decimal_to_decimal::<Decimal256Type, Decimal64Type>(
1123                array.as_primitive(),
1124                *p1,
1125                *s1,
1126                *p2,
1127                *s2,
1128                cast_options,
1129            )
1130        }
1131        (Decimal256(p1, s1), Decimal128(p2, s2)) => {
1132            cast_decimal_to_decimal::<Decimal256Type, Decimal128Type>(
1133                array.as_primitive(),
1134                *p1,
1135                *s1,
1136                *p2,
1137                *s2,
1138                cast_options,
1139            )
1140        }
1141        // Decimal to non-decimal
1142        (Decimal32(_, scale), _) if !to_type.is_temporal() => {
1143            cast_from_decimal::<Decimal32Type, _>(
1144                array,
1145                10_i32,
1146                scale,
1147                from_type,
1148                to_type,
1149                |x: i32| x as f64,
1150                cast_options,
1151            )
1152        }
1153        (Decimal64(_, scale), _) if !to_type.is_temporal() => {
1154            cast_from_decimal::<Decimal64Type, _>(
1155                array,
1156                10_i64,
1157                scale,
1158                from_type,
1159                to_type,
1160                |x: i64| x as f64,
1161                cast_options,
1162            )
1163        }
1164        (Decimal128(_, scale), _) if !to_type.is_temporal() => {
1165            cast_from_decimal::<Decimal128Type, _>(
1166                array,
1167                10_i128,
1168                scale,
1169                from_type,
1170                to_type,
1171                |x: i128| x as f64,
1172                cast_options,
1173            )
1174        }
1175        (Decimal256(_, scale), _) if !to_type.is_temporal() => {
1176            cast_from_decimal::<Decimal256Type, _>(
1177                array,
1178                i256::from_i128(10_i128),
1179                scale,
1180                from_type,
1181                to_type,
1182                |x: i256| x.to_f64().expect("All i256 values fit in f64"),
1183                cast_options,
1184            )
1185        }
1186        // Non-decimal to decimal
1187        (_, Decimal32(precision, scale)) if !from_type.is_temporal() => {
1188            cast_to_decimal::<Decimal32Type, _>(
1189                array,
1190                10_i32,
1191                precision,
1192                scale,
1193                from_type,
1194                to_type,
1195                cast_options,
1196            )
1197        }
1198        (_, Decimal64(precision, scale)) if !from_type.is_temporal() => {
1199            cast_to_decimal::<Decimal64Type, _>(
1200                array,
1201                10_i64,
1202                precision,
1203                scale,
1204                from_type,
1205                to_type,
1206                cast_options,
1207            )
1208        }
1209        (_, Decimal128(precision, scale)) if !from_type.is_temporal() => {
1210            cast_to_decimal::<Decimal128Type, _>(
1211                array,
1212                10_i128,
1213                precision,
1214                scale,
1215                from_type,
1216                to_type,
1217                cast_options,
1218            )
1219        }
1220        (_, Decimal256(precision, scale)) if !from_type.is_temporal() => {
1221            cast_to_decimal::<Decimal256Type, _>(
1222                array,
1223                i256::from_i128(10_i128),
1224                precision,
1225                scale,
1226                from_type,
1227                to_type,
1228                cast_options,
1229            )
1230        }
1231        (Struct(from_fields), Struct(to_fields)) => cast_struct_to_struct(
1232            array.as_struct(),
1233            from_fields.clone(),
1234            to_fields.clone(),
1235            cast_options,
1236        ),
1237        (Struct(_), _) => Err(ArrowError::CastError(format!(
1238            "Casting from {from_type} to {to_type} not supported"
1239        ))),
1240        (_, Struct(_)) => Err(ArrowError::CastError(format!(
1241            "Casting from {from_type} to {to_type} not supported"
1242        ))),
1243        (_, Boolean) => match from_type {
1244            UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
1245            UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
1246            UInt32 => cast_numeric_to_bool::<UInt32Type>(array),
1247            UInt64 => cast_numeric_to_bool::<UInt64Type>(array),
1248            Int8 => cast_numeric_to_bool::<Int8Type>(array),
1249            Int16 => cast_numeric_to_bool::<Int16Type>(array),
1250            Int32 => cast_numeric_to_bool::<Int32Type>(array),
1251            Int64 => cast_numeric_to_bool::<Int64Type>(array),
1252            Float16 => cast_numeric_to_bool::<Float16Type>(array),
1253            Float32 => cast_numeric_to_bool::<Float32Type>(array),
1254            Float64 => cast_numeric_to_bool::<Float64Type>(array),
1255            Utf8View => cast_utf8view_to_boolean(array, cast_options),
1256            Utf8 => cast_utf8_to_boolean::<i32>(array, cast_options),
1257            LargeUtf8 => cast_utf8_to_boolean::<i64>(array, cast_options),
1258            _ => Err(ArrowError::CastError(format!(
1259                "Casting from {from_type} to {to_type} not supported",
1260            ))),
1261        },
1262        (Boolean, _) => match to_type {
1263            UInt8 => cast_bool_to_numeric::<UInt8Type>(array, cast_options),
1264            UInt16 => cast_bool_to_numeric::<UInt16Type>(array, cast_options),
1265            UInt32 => cast_bool_to_numeric::<UInt32Type>(array, cast_options),
1266            UInt64 => cast_bool_to_numeric::<UInt64Type>(array, cast_options),
1267            Int8 => cast_bool_to_numeric::<Int8Type>(array, cast_options),
1268            Int16 => cast_bool_to_numeric::<Int16Type>(array, cast_options),
1269            Int32 => cast_bool_to_numeric::<Int32Type>(array, cast_options),
1270            Int64 => cast_bool_to_numeric::<Int64Type>(array, cast_options),
1271            Float16 => cast_bool_to_numeric::<Float16Type>(array, cast_options),
1272            Float32 => cast_bool_to_numeric::<Float32Type>(array, cast_options),
1273            Float64 => cast_bool_to_numeric::<Float64Type>(array, cast_options),
1274            Utf8View => value_to_string_view(array, cast_options),
1275            Utf8 => value_to_string::<i32>(array, cast_options),
1276            LargeUtf8 => value_to_string::<i64>(array, cast_options),
1277            _ => Err(ArrowError::CastError(format!(
1278                "Casting from {from_type} to {to_type} not supported",
1279            ))),
1280        },
1281        (Utf8, _) => match to_type {
1282            UInt8 => parse_string::<UInt8Type, i32>(array, cast_options),
1283            UInt16 => parse_string::<UInt16Type, i32>(array, cast_options),
1284            UInt32 => parse_string::<UInt32Type, i32>(array, cast_options),
1285            UInt64 => parse_string::<UInt64Type, i32>(array, cast_options),
1286            Int8 => parse_string::<Int8Type, i32>(array, cast_options),
1287            Int16 => parse_string::<Int16Type, i32>(array, cast_options),
1288            Int32 => parse_string::<Int32Type, i32>(array, cast_options),
1289            Int64 => parse_string::<Int64Type, i32>(array, cast_options),
1290            Float16 => parse_string::<Float16Type, i32>(array, cast_options),
1291            Float32 => parse_string::<Float32Type, i32>(array, cast_options),
1292            Float64 => parse_string::<Float64Type, i32>(array, cast_options),
1293            Date32 => parse_string::<Date32Type, i32>(array, cast_options),
1294            Date64 => parse_string::<Date64Type, i32>(array, cast_options),
1295            Binary => Ok(Arc::new(BinaryArray::from(
1296                array.as_string::<i32>().clone(),
1297            ))),
1298            LargeBinary => {
1299                let binary = BinaryArray::from(array.as_string::<i32>().clone());
1300                cast_byte_container::<BinaryType, LargeBinaryType>(&binary)
1301            }
1302            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i32>()))),
1303            BinaryView => Ok(Arc::new(
1304                StringViewArray::from(array.as_string::<i32>()).to_binary_view(),
1305            )),
1306            LargeUtf8 => cast_byte_container::<Utf8Type, LargeUtf8Type>(array),
1307            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i32>(array, cast_options),
1308            Time32(TimeUnit::Millisecond) => {
1309                parse_string::<Time32MillisecondType, i32>(array, cast_options)
1310            }
1311            Time64(TimeUnit::Microsecond) => {
1312                parse_string::<Time64MicrosecondType, i32>(array, cast_options)
1313            }
1314            Time64(TimeUnit::Nanosecond) => {
1315                parse_string::<Time64NanosecondType, i32>(array, cast_options)
1316            }
1317            Timestamp(TimeUnit::Second, to_tz) => cast_string_to_timestamp::<
1318                i32,
1319                TimestampSecondType,
1320            >(array, to_tz.as_ref(), cast_options),
1321            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1322                i32,
1323                TimestampMillisecondType,
1324            >(
1325                array, to_tz.as_ref(), cast_options
1326            ),
1327            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1328                i32,
1329                TimestampMicrosecondType,
1330            >(
1331                array, to_tz.as_ref(), cast_options
1332            ),
1333            Timestamp(TimeUnit::Nanosecond, to_tz) => cast_string_to_timestamp::<
1334                i32,
1335                TimestampNanosecondType,
1336            >(
1337                array, to_tz.as_ref(), cast_options
1338            ),
1339            Interval(IntervalUnit::YearMonth) => {
1340                cast_string_to_year_month_interval::<i32>(array, cast_options)
1341            }
1342            Interval(IntervalUnit::DayTime) => {
1343                cast_string_to_day_time_interval::<i32>(array, cast_options)
1344            }
1345            Interval(IntervalUnit::MonthDayNano) => {
1346                cast_string_to_month_day_nano_interval::<i32>(array, cast_options)
1347            }
1348            _ => Err(ArrowError::CastError(format!(
1349                "Casting from {from_type} to {to_type} not supported",
1350            ))),
1351        },
1352        (Utf8View, _) => match to_type {
1353            UInt8 => parse_string_view::<UInt8Type>(array, cast_options),
1354            UInt16 => parse_string_view::<UInt16Type>(array, cast_options),
1355            UInt32 => parse_string_view::<UInt32Type>(array, cast_options),
1356            UInt64 => parse_string_view::<UInt64Type>(array, cast_options),
1357            Int8 => parse_string_view::<Int8Type>(array, cast_options),
1358            Int16 => parse_string_view::<Int16Type>(array, cast_options),
1359            Int32 => parse_string_view::<Int32Type>(array, cast_options),
1360            Int64 => parse_string_view::<Int64Type>(array, cast_options),
1361            Float16 => parse_string_view::<Float16Type>(array, cast_options),
1362            Float32 => parse_string_view::<Float32Type>(array, cast_options),
1363            Float64 => parse_string_view::<Float64Type>(array, cast_options),
1364            Date32 => parse_string_view::<Date32Type>(array, cast_options),
1365            Date64 => parse_string_view::<Date64Type>(array, cast_options),
1366            Binary => cast_view_to_byte::<StringViewType, GenericBinaryType<i32>>(array),
1367            LargeBinary => cast_view_to_byte::<StringViewType, GenericBinaryType<i64>>(array),
1368            BinaryView => Ok(Arc::new(array.as_string_view().clone().to_binary_view())),
1369            Utf8 => cast_view_to_byte::<StringViewType, GenericStringType<i32>>(array),
1370            LargeUtf8 => cast_view_to_byte::<StringViewType, GenericStringType<i64>>(array),
1371            Time32(TimeUnit::Second) => parse_string_view::<Time32SecondType>(array, cast_options),
1372            Time32(TimeUnit::Millisecond) => {
1373                parse_string_view::<Time32MillisecondType>(array, cast_options)
1374            }
1375            Time64(TimeUnit::Microsecond) => {
1376                parse_string_view::<Time64MicrosecondType>(array, cast_options)
1377            }
1378            Time64(TimeUnit::Nanosecond) => {
1379                parse_string_view::<Time64NanosecondType>(array, cast_options)
1380            }
1381            Timestamp(TimeUnit::Second, to_tz) => {
1382                cast_view_to_timestamp::<TimestampSecondType>(array, to_tz.as_ref(), cast_options)
1383            }
1384            Timestamp(TimeUnit::Millisecond, to_tz) => cast_view_to_timestamp::<
1385                TimestampMillisecondType,
1386            >(
1387                array, to_tz.as_ref(), cast_options
1388            ),
1389            Timestamp(TimeUnit::Microsecond, to_tz) => cast_view_to_timestamp::<
1390                TimestampMicrosecondType,
1391            >(
1392                array, to_tz.as_ref(), cast_options
1393            ),
1394            Timestamp(TimeUnit::Nanosecond, to_tz) => cast_view_to_timestamp::<
1395                TimestampNanosecondType,
1396            >(
1397                array, to_tz.as_ref(), cast_options
1398            ),
1399            Interval(IntervalUnit::YearMonth) => {
1400                cast_view_to_year_month_interval(array, cast_options)
1401            }
1402            Interval(IntervalUnit::DayTime) => cast_view_to_day_time_interval(array, cast_options),
1403            Interval(IntervalUnit::MonthDayNano) => {
1404                cast_view_to_month_day_nano_interval(array, cast_options)
1405            }
1406            _ => Err(ArrowError::CastError(format!(
1407                "Casting from {from_type} to {to_type} not supported",
1408            ))),
1409        },
1410        (LargeUtf8, _) => match to_type {
1411            UInt8 => parse_string::<UInt8Type, i64>(array, cast_options),
1412            UInt16 => parse_string::<UInt16Type, i64>(array, cast_options),
1413            UInt32 => parse_string::<UInt32Type, i64>(array, cast_options),
1414            UInt64 => parse_string::<UInt64Type, i64>(array, cast_options),
1415            Int8 => parse_string::<Int8Type, i64>(array, cast_options),
1416            Int16 => parse_string::<Int16Type, i64>(array, cast_options),
1417            Int32 => parse_string::<Int32Type, i64>(array, cast_options),
1418            Int64 => parse_string::<Int64Type, i64>(array, cast_options),
1419            Float16 => parse_string::<Float16Type, i64>(array, cast_options),
1420            Float32 => parse_string::<Float32Type, i64>(array, cast_options),
1421            Float64 => parse_string::<Float64Type, i64>(array, cast_options),
1422            Date32 => parse_string::<Date32Type, i64>(array, cast_options),
1423            Date64 => parse_string::<Date64Type, i64>(array, cast_options),
1424            Utf8 => cast_byte_container::<LargeUtf8Type, Utf8Type>(array),
1425            Binary => {
1426                let large_binary = LargeBinaryArray::from(array.as_string::<i64>().clone());
1427                cast_byte_container::<LargeBinaryType, BinaryType>(&large_binary)
1428            }
1429            LargeBinary => Ok(Arc::new(LargeBinaryArray::from(
1430                array.as_string::<i64>().clone(),
1431            ))),
1432            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i64>()))),
1433            BinaryView => Ok(Arc::new(BinaryViewArray::from(
1434                array
1435                    .as_string::<i64>()
1436                    .into_iter()
1437                    .map(|x| x.map(|x| x.as_bytes()))
1438                    .collect::<Vec<_>>(),
1439            ))),
1440            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i64>(array, cast_options),
1441            Time32(TimeUnit::Millisecond) => {
1442                parse_string::<Time32MillisecondType, i64>(array, cast_options)
1443            }
1444            Time64(TimeUnit::Microsecond) => {
1445                parse_string::<Time64MicrosecondType, i64>(array, cast_options)
1446            }
1447            Time64(TimeUnit::Nanosecond) => {
1448                parse_string::<Time64NanosecondType, i64>(array, cast_options)
1449            }
1450            Timestamp(TimeUnit::Second, to_tz) => cast_string_to_timestamp::<
1451                i64,
1452                TimestampSecondType,
1453            >(array, to_tz.as_ref(), cast_options),
1454            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1455                i64,
1456                TimestampMillisecondType,
1457            >(
1458                array, to_tz.as_ref(), cast_options
1459            ),
1460            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1461                i64,
1462                TimestampMicrosecondType,
1463            >(
1464                array, to_tz.as_ref(), cast_options
1465            ),
1466            Timestamp(TimeUnit::Nanosecond, to_tz) => cast_string_to_timestamp::<
1467                i64,
1468                TimestampNanosecondType,
1469            >(
1470                array, to_tz.as_ref(), cast_options
1471            ),
1472            Interval(IntervalUnit::YearMonth) => {
1473                cast_string_to_year_month_interval::<i64>(array, cast_options)
1474            }
1475            Interval(IntervalUnit::DayTime) => {
1476                cast_string_to_day_time_interval::<i64>(array, cast_options)
1477            }
1478            Interval(IntervalUnit::MonthDayNano) => {
1479                cast_string_to_month_day_nano_interval::<i64>(array, cast_options)
1480            }
1481            _ => Err(ArrowError::CastError(format!(
1482                "Casting from {from_type} to {to_type} not supported",
1483            ))),
1484        },
1485        (Binary, _) => match to_type {
1486            Utf8 => cast_binary_to_string::<i32>(array, cast_options),
1487            LargeUtf8 => {
1488                let array = cast_binary_to_string::<i32>(array, cast_options)?;
1489                cast_byte_container::<Utf8Type, LargeUtf8Type>(array.as_ref())
1490            }
1491            LargeBinary => cast_byte_container::<BinaryType, LargeBinaryType>(array),
1492            FixedSizeBinary(size) => {
1493                cast_binary_to_fixed_size_binary::<i32>(array, *size, cast_options)
1494            }
1495            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i32>()))),
1496            Utf8View => Ok(Arc::new(StringViewArray::from(
1497                cast_binary_to_string::<i32>(array, cast_options)?.as_string::<i32>(),
1498            ))),
1499            _ => Err(ArrowError::CastError(format!(
1500                "Casting from {from_type} to {to_type} not supported",
1501            ))),
1502        },
1503        (LargeBinary, _) => match to_type {
1504            Utf8 => {
1505                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1506                cast_byte_container::<LargeUtf8Type, Utf8Type>(array.as_ref())
1507            }
1508            LargeUtf8 => cast_binary_to_string::<i64>(array, cast_options),
1509            Binary => cast_byte_container::<LargeBinaryType, BinaryType>(array),
1510            FixedSizeBinary(size) => {
1511                cast_binary_to_fixed_size_binary::<i64>(array, *size, cast_options)
1512            }
1513            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i64>()))),
1514            Utf8View => {
1515                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1516                Ok(Arc::new(StringViewArray::from(array.as_string::<i64>())))
1517            }
1518            _ => Err(ArrowError::CastError(format!(
1519                "Casting from {from_type} to {to_type} not supported",
1520            ))),
1521        },
1522        (FixedSizeBinary(size), _) => match to_type {
1523            Binary => cast_fixed_size_binary_to_binary::<i32>(array, *size),
1524            LargeBinary => cast_fixed_size_binary_to_binary::<i64>(array, *size),
1525            BinaryView => cast_fixed_size_binary_to_binary_view(array, *size),
1526            _ => Err(ArrowError::CastError(format!(
1527                "Casting from {from_type} to {to_type} not supported",
1528            ))),
1529        },
1530        (BinaryView, Binary) => cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array),
1531        (BinaryView, LargeBinary) => {
1532            cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)
1533        }
1534        (BinaryView, Utf8) => {
1535            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array)?;
1536            cast_binary_to_string::<i32>(&binary_arr, cast_options)
1537        }
1538        (BinaryView, LargeUtf8) => {
1539            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)?;
1540            cast_binary_to_string::<i64>(&binary_arr, cast_options)
1541        }
1542        (BinaryView, Utf8View) => cast_binary_view_to_string_view(array, cast_options),
1543        (BinaryView, _) => Err(ArrowError::CastError(format!(
1544            "Casting from {from_type} to {to_type} not supported",
1545        ))),
1546        (from_type, Utf8View) if from_type.is_primitive() => {
1547            value_to_string_view(array, cast_options)
1548        }
1549        (from_type, LargeUtf8) if from_type.is_primitive() => {
1550            value_to_string::<i64>(array, cast_options)
1551        }
1552        (from_type, Utf8) if from_type.is_primitive() => {
1553            value_to_string::<i32>(array, cast_options)
1554        }
1555        (from_type, Binary) if from_type.is_integer() => match from_type {
1556            UInt8 => cast_numeric_to_binary::<UInt8Type, i32>(array),
1557            UInt16 => cast_numeric_to_binary::<UInt16Type, i32>(array),
1558            UInt32 => cast_numeric_to_binary::<UInt32Type, i32>(array),
1559            UInt64 => cast_numeric_to_binary::<UInt64Type, i32>(array),
1560            Int8 => cast_numeric_to_binary::<Int8Type, i32>(array),
1561            Int16 => cast_numeric_to_binary::<Int16Type, i32>(array),
1562            Int32 => cast_numeric_to_binary::<Int32Type, i32>(array),
1563            Int64 => cast_numeric_to_binary::<Int64Type, i32>(array),
1564            _ => unreachable!(),
1565        },
1566        (from_type, LargeBinary) if from_type.is_integer() => match from_type {
1567            UInt8 => cast_numeric_to_binary::<UInt8Type, i64>(array),
1568            UInt16 => cast_numeric_to_binary::<UInt16Type, i64>(array),
1569            UInt32 => cast_numeric_to_binary::<UInt32Type, i64>(array),
1570            UInt64 => cast_numeric_to_binary::<UInt64Type, i64>(array),
1571            Int8 => cast_numeric_to_binary::<Int8Type, i64>(array),
1572            Int16 => cast_numeric_to_binary::<Int16Type, i64>(array),
1573            Int32 => cast_numeric_to_binary::<Int32Type, i64>(array),
1574            Int64 => cast_numeric_to_binary::<Int64Type, i64>(array),
1575            _ => unreachable!(),
1576        },
1577        // start numeric casts
1578        (UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array, cast_options),
1579        (UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array, cast_options),
1580        (UInt8, UInt64) => cast_numeric_arrays::<UInt8Type, UInt64Type>(array, cast_options),
1581        (UInt8, Int8) => cast_numeric_arrays::<UInt8Type, Int8Type>(array, cast_options),
1582        (UInt8, Int16) => cast_numeric_arrays::<UInt8Type, Int16Type>(array, cast_options),
1583        (UInt8, Int32) => cast_numeric_arrays::<UInt8Type, Int32Type>(array, cast_options),
1584        (UInt8, Int64) => cast_numeric_arrays::<UInt8Type, Int64Type>(array, cast_options),
1585        (UInt8, Float16) => cast_numeric_arrays::<UInt8Type, Float16Type>(array, cast_options),
1586        (UInt8, Float32) => cast_numeric_arrays::<UInt8Type, Float32Type>(array, cast_options),
1587        (UInt8, Float64) => cast_numeric_arrays::<UInt8Type, Float64Type>(array, cast_options),
1588
1589        (UInt16, UInt8) => cast_numeric_arrays::<UInt16Type, UInt8Type>(array, cast_options),
1590        (UInt16, UInt32) => cast_numeric_arrays::<UInt16Type, UInt32Type>(array, cast_options),
1591        (UInt16, UInt64) => cast_numeric_arrays::<UInt16Type, UInt64Type>(array, cast_options),
1592        (UInt16, Int8) => cast_numeric_arrays::<UInt16Type, Int8Type>(array, cast_options),
1593        (UInt16, Int16) => cast_numeric_arrays::<UInt16Type, Int16Type>(array, cast_options),
1594        (UInt16, Int32) => cast_numeric_arrays::<UInt16Type, Int32Type>(array, cast_options),
1595        (UInt16, Int64) => cast_numeric_arrays::<UInt16Type, Int64Type>(array, cast_options),
1596        (UInt16, Float16) => cast_numeric_arrays::<UInt16Type, Float16Type>(array, cast_options),
1597        (UInt16, Float32) => cast_numeric_arrays::<UInt16Type, Float32Type>(array, cast_options),
1598        (UInt16, Float64) => cast_numeric_arrays::<UInt16Type, Float64Type>(array, cast_options),
1599
1600        (UInt32, UInt8) => cast_numeric_arrays::<UInt32Type, UInt8Type>(array, cast_options),
1601        (UInt32, UInt16) => cast_numeric_arrays::<UInt32Type, UInt16Type>(array, cast_options),
1602        (UInt32, UInt64) => cast_numeric_arrays::<UInt32Type, UInt64Type>(array, cast_options),
1603        (UInt32, Int8) => cast_numeric_arrays::<UInt32Type, Int8Type>(array, cast_options),
1604        (UInt32, Int16) => cast_numeric_arrays::<UInt32Type, Int16Type>(array, cast_options),
1605        (UInt32, Int32) => cast_numeric_arrays::<UInt32Type, Int32Type>(array, cast_options),
1606        (UInt32, Int64) => cast_numeric_arrays::<UInt32Type, Int64Type>(array, cast_options),
1607        (UInt32, Float16) => cast_numeric_arrays::<UInt32Type, Float16Type>(array, cast_options),
1608        (UInt32, Float32) => cast_numeric_arrays::<UInt32Type, Float32Type>(array, cast_options),
1609        (UInt32, Float64) => cast_numeric_arrays::<UInt32Type, Float64Type>(array, cast_options),
1610
1611        (UInt64, UInt8) => cast_numeric_arrays::<UInt64Type, UInt8Type>(array, cast_options),
1612        (UInt64, UInt16) => cast_numeric_arrays::<UInt64Type, UInt16Type>(array, cast_options),
1613        (UInt64, UInt32) => cast_numeric_arrays::<UInt64Type, UInt32Type>(array, cast_options),
1614        (UInt64, Int8) => cast_numeric_arrays::<UInt64Type, Int8Type>(array, cast_options),
1615        (UInt64, Int16) => cast_numeric_arrays::<UInt64Type, Int16Type>(array, cast_options),
1616        (UInt64, Int32) => cast_numeric_arrays::<UInt64Type, Int32Type>(array, cast_options),
1617        (UInt64, Int64) => cast_numeric_arrays::<UInt64Type, Int64Type>(array, cast_options),
1618        (UInt64, Float16) => cast_numeric_arrays::<UInt64Type, Float16Type>(array, cast_options),
1619        (UInt64, Float32) => cast_numeric_arrays::<UInt64Type, Float32Type>(array, cast_options),
1620        (UInt64, Float64) => cast_numeric_arrays::<UInt64Type, Float64Type>(array, cast_options),
1621
1622        (Int8, UInt8) => cast_numeric_arrays::<Int8Type, UInt8Type>(array, cast_options),
1623        (Int8, UInt16) => cast_numeric_arrays::<Int8Type, UInt16Type>(array, cast_options),
1624        (Int8, UInt32) => cast_numeric_arrays::<Int8Type, UInt32Type>(array, cast_options),
1625        (Int8, UInt64) => cast_numeric_arrays::<Int8Type, UInt64Type>(array, cast_options),
1626        (Int8, Int16) => cast_numeric_arrays::<Int8Type, Int16Type>(array, cast_options),
1627        (Int8, Int32) => cast_numeric_arrays::<Int8Type, Int32Type>(array, cast_options),
1628        (Int8, Int64) => cast_numeric_arrays::<Int8Type, Int64Type>(array, cast_options),
1629        (Int8, Float16) => cast_numeric_arrays::<Int8Type, Float16Type>(array, cast_options),
1630        (Int8, Float32) => cast_numeric_arrays::<Int8Type, Float32Type>(array, cast_options),
1631        (Int8, Float64) => cast_numeric_arrays::<Int8Type, Float64Type>(array, cast_options),
1632
1633        (Int16, UInt8) => cast_numeric_arrays::<Int16Type, UInt8Type>(array, cast_options),
1634        (Int16, UInt16) => cast_numeric_arrays::<Int16Type, UInt16Type>(array, cast_options),
1635        (Int16, UInt32) => cast_numeric_arrays::<Int16Type, UInt32Type>(array, cast_options),
1636        (Int16, UInt64) => cast_numeric_arrays::<Int16Type, UInt64Type>(array, cast_options),
1637        (Int16, Int8) => cast_numeric_arrays::<Int16Type, Int8Type>(array, cast_options),
1638        (Int16, Int32) => cast_numeric_arrays::<Int16Type, Int32Type>(array, cast_options),
1639        (Int16, Int64) => cast_numeric_arrays::<Int16Type, Int64Type>(array, cast_options),
1640        (Int16, Float16) => cast_numeric_arrays::<Int16Type, Float16Type>(array, cast_options),
1641        (Int16, Float32) => cast_numeric_arrays::<Int16Type, Float32Type>(array, cast_options),
1642        (Int16, Float64) => cast_numeric_arrays::<Int16Type, Float64Type>(array, cast_options),
1643
1644        (Int32, UInt8) => cast_numeric_arrays::<Int32Type, UInt8Type>(array, cast_options),
1645        (Int32, UInt16) => cast_numeric_arrays::<Int32Type, UInt16Type>(array, cast_options),
1646        (Int32, UInt32) => cast_numeric_arrays::<Int32Type, UInt32Type>(array, cast_options),
1647        (Int32, UInt64) => cast_numeric_arrays::<Int32Type, UInt64Type>(array, cast_options),
1648        (Int32, Int8) => cast_numeric_arrays::<Int32Type, Int8Type>(array, cast_options),
1649        (Int32, Int16) => cast_numeric_arrays::<Int32Type, Int16Type>(array, cast_options),
1650        (Int32, Int64) => cast_numeric_arrays::<Int32Type, Int64Type>(array, cast_options),
1651        (Int32, Float16) => cast_numeric_arrays::<Int32Type, Float16Type>(array, cast_options),
1652        (Int32, Float32) => cast_numeric_arrays::<Int32Type, Float32Type>(array, cast_options),
1653        (Int32, Float64) => cast_numeric_arrays::<Int32Type, Float64Type>(array, cast_options),
1654
1655        (Int64, UInt8) => cast_numeric_arrays::<Int64Type, UInt8Type>(array, cast_options),
1656        (Int64, UInt16) => cast_numeric_arrays::<Int64Type, UInt16Type>(array, cast_options),
1657        (Int64, UInt32) => cast_numeric_arrays::<Int64Type, UInt32Type>(array, cast_options),
1658        (Int64, UInt64) => cast_numeric_arrays::<Int64Type, UInt64Type>(array, cast_options),
1659        (Int64, Int8) => cast_numeric_arrays::<Int64Type, Int8Type>(array, cast_options),
1660        (Int64, Int16) => cast_numeric_arrays::<Int64Type, Int16Type>(array, cast_options),
1661        (Int64, Int32) => cast_numeric_arrays::<Int64Type, Int32Type>(array, cast_options),
1662        (Int64, Float16) => cast_numeric_arrays::<Int64Type, Float16Type>(array, cast_options),
1663        (Int64, Float32) => cast_numeric_arrays::<Int64Type, Float32Type>(array, cast_options),
1664        (Int64, Float64) => cast_numeric_arrays::<Int64Type, Float64Type>(array, cast_options),
1665
1666        (Float16, UInt8) => cast_numeric_arrays::<Float16Type, UInt8Type>(array, cast_options),
1667        (Float16, UInt16) => cast_numeric_arrays::<Float16Type, UInt16Type>(array, cast_options),
1668        (Float16, UInt32) => cast_numeric_arrays::<Float16Type, UInt32Type>(array, cast_options),
1669        (Float16, UInt64) => cast_numeric_arrays::<Float16Type, UInt64Type>(array, cast_options),
1670        (Float16, Int8) => cast_numeric_arrays::<Float16Type, Int8Type>(array, cast_options),
1671        (Float16, Int16) => cast_numeric_arrays::<Float16Type, Int16Type>(array, cast_options),
1672        (Float16, Int32) => cast_numeric_arrays::<Float16Type, Int32Type>(array, cast_options),
1673        (Float16, Int64) => cast_numeric_arrays::<Float16Type, Int64Type>(array, cast_options),
1674        (Float16, Float32) => cast_numeric_arrays::<Float16Type, Float32Type>(array, cast_options),
1675        (Float16, Float64) => cast_numeric_arrays::<Float16Type, Float64Type>(array, cast_options),
1676
1677        (Float32, UInt8) => cast_numeric_arrays::<Float32Type, UInt8Type>(array, cast_options),
1678        (Float32, UInt16) => cast_numeric_arrays::<Float32Type, UInt16Type>(array, cast_options),
1679        (Float32, UInt32) => cast_numeric_arrays::<Float32Type, UInt32Type>(array, cast_options),
1680        (Float32, UInt64) => cast_numeric_arrays::<Float32Type, UInt64Type>(array, cast_options),
1681        (Float32, Int8) => cast_numeric_arrays::<Float32Type, Int8Type>(array, cast_options),
1682        (Float32, Int16) => cast_numeric_arrays::<Float32Type, Int16Type>(array, cast_options),
1683        (Float32, Int32) => cast_numeric_arrays::<Float32Type, Int32Type>(array, cast_options),
1684        (Float32, Int64) => cast_numeric_arrays::<Float32Type, Int64Type>(array, cast_options),
1685        (Float32, Float16) => cast_numeric_arrays::<Float32Type, Float16Type>(array, cast_options),
1686        (Float32, Float64) => cast_numeric_arrays::<Float32Type, Float64Type>(array, cast_options),
1687
1688        (Float64, UInt8) => cast_numeric_arrays::<Float64Type, UInt8Type>(array, cast_options),
1689        (Float64, UInt16) => cast_numeric_arrays::<Float64Type, UInt16Type>(array, cast_options),
1690        (Float64, UInt32) => cast_numeric_arrays::<Float64Type, UInt32Type>(array, cast_options),
1691        (Float64, UInt64) => cast_numeric_arrays::<Float64Type, UInt64Type>(array, cast_options),
1692        (Float64, Int8) => cast_numeric_arrays::<Float64Type, Int8Type>(array, cast_options),
1693        (Float64, Int16) => cast_numeric_arrays::<Float64Type, Int16Type>(array, cast_options),
1694        (Float64, Int32) => cast_numeric_arrays::<Float64Type, Int32Type>(array, cast_options),
1695        (Float64, Int64) => cast_numeric_arrays::<Float64Type, Int64Type>(array, cast_options),
1696        (Float64, Float16) => cast_numeric_arrays::<Float64Type, Float16Type>(array, cast_options),
1697        (Float64, Float32) => cast_numeric_arrays::<Float64Type, Float32Type>(array, cast_options),
1698        // end numeric casts
1699
1700        // temporal casts
1701        (Int32, Date32) => cast_reinterpret_arrays::<Int32Type, Date32Type>(array),
1702        (Int32, Date64) => cast_with_options(
1703            &cast_with_options(array, &Date32, cast_options)?,
1704            &Date64,
1705            cast_options,
1706        ),
1707        (Int32, Time32(TimeUnit::Second)) => {
1708            cast_reinterpret_arrays::<Int32Type, Time32SecondType>(array)
1709        }
1710        (Int32, Time32(TimeUnit::Millisecond)) => {
1711            cast_reinterpret_arrays::<Int32Type, Time32MillisecondType>(array)
1712        }
1713        // No support for microsecond/nanosecond with i32
1714        (Date32, Int32) => cast_reinterpret_arrays::<Date32Type, Int32Type>(array),
1715        (Date32, Int64) => cast_with_options(
1716            &cast_with_options(array, &Int32, cast_options)?,
1717            &Int64,
1718            cast_options,
1719        ),
1720        (Time32(TimeUnit::Second), Int32) => {
1721            cast_reinterpret_arrays::<Time32SecondType, Int32Type>(array)
1722        }
1723        (Time32(TimeUnit::Millisecond), Int32) => {
1724            cast_reinterpret_arrays::<Time32MillisecondType, Int32Type>(array)
1725        }
1726        (Time32(TimeUnit::Second), Int64) => cast_with_options(
1727            &cast_with_options(array, &Int32, cast_options)?,
1728            &Int64,
1729            cast_options,
1730        ),
1731        (Time32(TimeUnit::Millisecond), Int64) => cast_with_options(
1732            &cast_with_options(array, &Int32, cast_options)?,
1733            &Int64,
1734            cast_options,
1735        ),
1736        (Int64, Date64) => cast_reinterpret_arrays::<Int64Type, Date64Type>(array),
1737        (Int64, Date32) => cast_with_options(
1738            &cast_with_options(array, &Int32, cast_options)?,
1739            &Date32,
1740            cast_options,
1741        ),
1742        // No support for second/milliseconds with i64
1743        (Int64, Time64(TimeUnit::Microsecond)) => {
1744            cast_reinterpret_arrays::<Int64Type, Time64MicrosecondType>(array)
1745        }
1746        (Int64, Time64(TimeUnit::Nanosecond)) => {
1747            cast_reinterpret_arrays::<Int64Type, Time64NanosecondType>(array)
1748        }
1749
1750        (Date64, Int64) => cast_reinterpret_arrays::<Date64Type, Int64Type>(array),
1751        (Date64, Int32) => cast_with_options(
1752            &cast_with_options(array, &Int64, cast_options)?,
1753            &Int32,
1754            cast_options,
1755        ),
1756        (Time64(TimeUnit::Microsecond), Int64) => {
1757            cast_reinterpret_arrays::<Time64MicrosecondType, Int64Type>(array)
1758        }
1759        (Time64(TimeUnit::Nanosecond), Int64) => {
1760            cast_reinterpret_arrays::<Time64NanosecondType, Int64Type>(array)
1761        }
1762        (Date32, Date64) => Ok(Arc::new(
1763            array
1764                .as_primitive::<Date32Type>()
1765                .unary::<_, Date64Type>(|x| x as i64 * MILLISECONDS_IN_DAY),
1766        )),
1767        (Date64, Date32) => {
1768            let array = array.as_primitive::<Date64Type>();
1769            let result = if cast_options.safe {
1770                array.unary_opt::<_, Date32Type>(|x| i32::try_from(x / MILLISECONDS_IN_DAY).ok())
1771            } else {
1772                array.try_unary::<_, Date32Type, _>(|x| {
1773                    i32::try_from(x / MILLISECONDS_IN_DAY).map_err(|_| {
1774                        ArrowError::CastError(format!(
1775                            "Cannot cast Date64 value {x} to Date32 without overflow"
1776                        ))
1777                    })
1778                })?
1779            };
1780            Ok(Arc::new(result))
1781        }
1782
1783        (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => {
1784            let array = array.as_primitive::<Time32SecondType>();
1785            let result = if cast_options.safe {
1786                array.unary_opt::<_, Time32MillisecondType>(|x| x.checked_mul(MILLISECONDS as i32))
1787            } else {
1788                array.try_unary::<_, Time32MillisecondType, _>(|x| {
1789                    x.mul_checked(MILLISECONDS as i32)
1790                })?
1791            };
1792            Ok(Arc::new(result))
1793        }
1794        (Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1795            array
1796                .as_primitive::<Time32SecondType>()
1797                .unary::<_, Time64MicrosecondType>(|x| x as i64 * MICROSECONDS),
1798        )),
1799        (Time32(TimeUnit::Second), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1800            array
1801                .as_primitive::<Time32SecondType>()
1802                .unary::<_, Time64NanosecondType>(|x| x as i64 * NANOSECONDS),
1803        )),
1804
1805        (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1806            array
1807                .as_primitive::<Time32MillisecondType>()
1808                .unary::<_, Time32SecondType>(|x| x / MILLISECONDS as i32),
1809        )),
1810        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1811            array
1812                .as_primitive::<Time32MillisecondType>()
1813                .unary::<_, Time64MicrosecondType>(|x| x as i64 * (MICROSECONDS / MILLISECONDS)),
1814        )),
1815        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1816            array
1817                .as_primitive::<Time32MillisecondType>()
1818                .unary::<_, Time64NanosecondType>(|x| x as i64 * (NANOSECONDS / MILLISECONDS)),
1819        )),
1820
1821        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1822            array
1823                .as_primitive::<Time64MicrosecondType>()
1824                .unary::<_, Time32SecondType>(|x| (x / MICROSECONDS) as i32),
1825        )),
1826        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1827            array
1828                .as_primitive::<Time64MicrosecondType>()
1829                .unary::<_, Time32MillisecondType>(|x| (x / (MICROSECONDS / MILLISECONDS)) as i32),
1830        )),
1831        (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1832            array
1833                .as_primitive::<Time64MicrosecondType>()
1834                .unary::<_, Time64NanosecondType>(|x| x * (NANOSECONDS / MICROSECONDS)),
1835        )),
1836
1837        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1838            array
1839                .as_primitive::<Time64NanosecondType>()
1840                .unary::<_, Time32SecondType>(|x| (x / NANOSECONDS) as i32),
1841        )),
1842        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1843            array
1844                .as_primitive::<Time64NanosecondType>()
1845                .unary::<_, Time32MillisecondType>(|x| (x / (NANOSECONDS / MILLISECONDS)) as i32),
1846        )),
1847        (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1848            array
1849                .as_primitive::<Time64NanosecondType>()
1850                .unary::<_, Time64MicrosecondType>(|x| x / (NANOSECONDS / MICROSECONDS)),
1851        )),
1852
1853        // Timestamp to integer/floating/decimals
1854        (Timestamp(TimeUnit::Second, _), _) if to_type.is_numeric() => {
1855            let array = cast_reinterpret_arrays::<TimestampSecondType, Int64Type>(array)?;
1856            cast_with_options(&array, to_type, cast_options)
1857        }
1858        (Timestamp(TimeUnit::Millisecond, _), _) if to_type.is_numeric() => {
1859            let array = cast_reinterpret_arrays::<TimestampMillisecondType, Int64Type>(array)?;
1860            cast_with_options(&array, to_type, cast_options)
1861        }
1862        (Timestamp(TimeUnit::Microsecond, _), _) if to_type.is_numeric() => {
1863            let array = cast_reinterpret_arrays::<TimestampMicrosecondType, Int64Type>(array)?;
1864            cast_with_options(&array, to_type, cast_options)
1865        }
1866        (Timestamp(TimeUnit::Nanosecond, _), _) if to_type.is_numeric() => {
1867            let array = cast_reinterpret_arrays::<TimestampNanosecondType, Int64Type>(array)?;
1868            cast_with_options(&array, to_type, cast_options)
1869        }
1870
1871        (_, Timestamp(unit, tz)) if from_type.is_numeric() => {
1872            let array = cast_with_options(array, &Int64, cast_options)?;
1873            Ok(make_timestamp_array(
1874                array.as_primitive(),
1875                *unit,
1876                tz.clone(),
1877            ))
1878        }
1879
1880        (Timestamp(from_unit, from_tz), Timestamp(to_unit, to_tz)) => {
1881            let array = cast_with_options(array, &Int64, cast_options)?;
1882            let time_array = array.as_primitive::<Int64Type>();
1883            let from_size = time_unit_multiple(from_unit);
1884            let to_size = time_unit_multiple(to_unit);
1885            // we either divide or multiply, depending on size of each unit
1886            // units are never the same when the types are the same
1887            let converted = match from_size.cmp(&to_size) {
1888                Ordering::Greater => {
1889                    let divisor = from_size / to_size;
1890                    time_array.unary::<_, Int64Type>(|o| o / divisor)
1891                }
1892                Ordering::Equal => time_array.clone(),
1893                Ordering::Less => {
1894                    let mul = to_size / from_size;
1895                    if cast_options.safe {
1896                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1897                    } else {
1898                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1899                    }
1900                }
1901            };
1902            // Normalize timezone
1903            let adjusted = match (from_tz, to_tz) {
1904                // Only this case needs to be adjusted because we're casting from
1905                // unknown time offset to some time offset, we want the time to be
1906                // unchanged.
1907                //
1908                // i.e. Timestamp('2001-01-01T00:00', None) -> Timestamp('2001-01-01T00:00', '+0700')
1909                (None, Some(to_tz)) => {
1910                    let to_tz: Tz = to_tz.parse()?;
1911                    match to_unit {
1912                        TimeUnit::Second => adjust_timestamp_to_timezone::<TimestampSecondType>(
1913                            converted,
1914                            &to_tz,
1915                            cast_options,
1916                        )?,
1917                        TimeUnit::Millisecond => adjust_timestamp_to_timezone::<
1918                            TimestampMillisecondType,
1919                        >(
1920                            converted, &to_tz, cast_options
1921                        )?,
1922                        TimeUnit::Microsecond => adjust_timestamp_to_timezone::<
1923                            TimestampMicrosecondType,
1924                        >(
1925                            converted, &to_tz, cast_options
1926                        )?,
1927                        TimeUnit::Nanosecond => adjust_timestamp_to_timezone::<
1928                            TimestampNanosecondType,
1929                        >(
1930                            converted, &to_tz, cast_options
1931                        )?,
1932                    }
1933                }
1934                _ => converted,
1935            };
1936            Ok(make_timestamp_array(&adjusted, *to_unit, to_tz.clone()))
1937        }
1938        (Timestamp(TimeUnit::Microsecond, _), Date32) => {
1939            timestamp_to_date32(array.as_primitive::<TimestampMicrosecondType>())
1940        }
1941        (Timestamp(TimeUnit::Millisecond, _), Date32) => {
1942            timestamp_to_date32(array.as_primitive::<TimestampMillisecondType>())
1943        }
1944        (Timestamp(TimeUnit::Second, _), Date32) => {
1945            timestamp_to_date32(array.as_primitive::<TimestampSecondType>())
1946        }
1947        (Timestamp(TimeUnit::Nanosecond, _), Date32) => {
1948            timestamp_to_date32(array.as_primitive::<TimestampNanosecondType>())
1949        }
1950        (Timestamp(TimeUnit::Second, _), Date64) => Ok(Arc::new(match cast_options.safe {
1951            true => {
1952                // change error to None
1953                array
1954                    .as_primitive::<TimestampSecondType>()
1955                    .unary_opt::<_, Date64Type>(|x| x.checked_mul(MILLISECONDS))
1956            }
1957            false => array
1958                .as_primitive::<TimestampSecondType>()
1959                .try_unary::<_, Date64Type, _>(|x| x.mul_checked(MILLISECONDS))?,
1960        })),
1961        (Timestamp(TimeUnit::Millisecond, _), Date64) => {
1962            cast_reinterpret_arrays::<TimestampMillisecondType, Date64Type>(array)
1963        }
1964        (Timestamp(TimeUnit::Microsecond, _), Date64) => Ok(Arc::new(
1965            array
1966                .as_primitive::<TimestampMicrosecondType>()
1967                .unary::<_, Date64Type>(|x| x / (MICROSECONDS / MILLISECONDS)),
1968        )),
1969        (Timestamp(TimeUnit::Nanosecond, _), Date64) => Ok(Arc::new(
1970            array
1971                .as_primitive::<TimestampNanosecondType>()
1972                .unary::<_, Date64Type>(|x| x / (NANOSECONDS / MILLISECONDS)),
1973        )),
1974        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Microsecond)) => {
1975            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1976            Ok(Arc::new(
1977                array
1978                    .as_primitive::<TimestampSecondType>()
1979                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1980                        Ok(time_to_time64us(as_time_res_with_timezone::<
1981                            TimestampSecondType,
1982                        >(x, tz)?))
1983                    })?,
1984            ))
1985        }
1986        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Nanosecond)) => {
1987            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1988            Ok(Arc::new(
1989                array
1990                    .as_primitive::<TimestampSecondType>()
1991                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1992                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1993                            TimestampSecondType,
1994                        >(x, tz)?))
1995                    })?,
1996            ))
1997        }
1998        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Microsecond)) => {
1999            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2000            Ok(Arc::new(
2001                array
2002                    .as_primitive::<TimestampMillisecondType>()
2003                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
2004                        Ok(time_to_time64us(as_time_res_with_timezone::<
2005                            TimestampMillisecondType,
2006                        >(x, tz)?))
2007                    })?,
2008            ))
2009        }
2010        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Nanosecond)) => {
2011            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2012            Ok(Arc::new(
2013                array
2014                    .as_primitive::<TimestampMillisecondType>()
2015                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
2016                        Ok(time_to_time64ns(as_time_res_with_timezone::<
2017                            TimestampMillisecondType,
2018                        >(x, tz)?))
2019                    })?,
2020            ))
2021        }
2022        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Microsecond)) => {
2023            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2024            Ok(Arc::new(
2025                array
2026                    .as_primitive::<TimestampMicrosecondType>()
2027                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
2028                        Ok(time_to_time64us(as_time_res_with_timezone::<
2029                            TimestampMicrosecondType,
2030                        >(x, tz)?))
2031                    })?,
2032            ))
2033        }
2034        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Nanosecond)) => {
2035            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2036            Ok(Arc::new(
2037                array
2038                    .as_primitive::<TimestampMicrosecondType>()
2039                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
2040                        Ok(time_to_time64ns(as_time_res_with_timezone::<
2041                            TimestampMicrosecondType,
2042                        >(x, tz)?))
2043                    })?,
2044            ))
2045        }
2046        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Microsecond)) => {
2047            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2048            Ok(Arc::new(
2049                array
2050                    .as_primitive::<TimestampNanosecondType>()
2051                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
2052                        Ok(time_to_time64us(as_time_res_with_timezone::<
2053                            TimestampNanosecondType,
2054                        >(x, tz)?))
2055                    })?,
2056            ))
2057        }
2058        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Nanosecond)) => {
2059            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2060            Ok(Arc::new(
2061                array
2062                    .as_primitive::<TimestampNanosecondType>()
2063                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
2064                        Ok(time_to_time64ns(as_time_res_with_timezone::<
2065                            TimestampNanosecondType,
2066                        >(x, tz)?))
2067                    })?,
2068            ))
2069        }
2070        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Second)) => {
2071            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2072            Ok(Arc::new(
2073                array
2074                    .as_primitive::<TimestampSecondType>()
2075                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2076                        Ok(time_to_time32s(as_time_res_with_timezone::<
2077                            TimestampSecondType,
2078                        >(x, tz)?))
2079                    })?,
2080            ))
2081        }
2082        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Millisecond)) => {
2083            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2084            Ok(Arc::new(
2085                array
2086                    .as_primitive::<TimestampSecondType>()
2087                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2088                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2089                            TimestampSecondType,
2090                        >(x, tz)?))
2091                    })?,
2092            ))
2093        }
2094        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Second)) => {
2095            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2096            Ok(Arc::new(
2097                array
2098                    .as_primitive::<TimestampMillisecondType>()
2099                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2100                        Ok(time_to_time32s(as_time_res_with_timezone::<
2101                            TimestampMillisecondType,
2102                        >(x, tz)?))
2103                    })?,
2104            ))
2105        }
2106        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Millisecond)) => {
2107            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2108            Ok(Arc::new(
2109                array
2110                    .as_primitive::<TimestampMillisecondType>()
2111                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2112                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2113                            TimestampMillisecondType,
2114                        >(x, tz)?))
2115                    })?,
2116            ))
2117        }
2118        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Second)) => {
2119            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2120            Ok(Arc::new(
2121                array
2122                    .as_primitive::<TimestampMicrosecondType>()
2123                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2124                        Ok(time_to_time32s(as_time_res_with_timezone::<
2125                            TimestampMicrosecondType,
2126                        >(x, tz)?))
2127                    })?,
2128            ))
2129        }
2130        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Millisecond)) => {
2131            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2132            Ok(Arc::new(
2133                array
2134                    .as_primitive::<TimestampMicrosecondType>()
2135                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2136                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2137                            TimestampMicrosecondType,
2138                        >(x, tz)?))
2139                    })?,
2140            ))
2141        }
2142        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Second)) => {
2143            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2144            Ok(Arc::new(
2145                array
2146                    .as_primitive::<TimestampNanosecondType>()
2147                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2148                        Ok(time_to_time32s(as_time_res_with_timezone::<
2149                            TimestampNanosecondType,
2150                        >(x, tz)?))
2151                    })?,
2152            ))
2153        }
2154        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Millisecond)) => {
2155            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2156            Ok(Arc::new(
2157                array
2158                    .as_primitive::<TimestampNanosecondType>()
2159                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2160                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2161                            TimestampNanosecondType,
2162                        >(x, tz)?))
2163                    })?,
2164            ))
2165        }
2166        (Date64, Timestamp(TimeUnit::Second, _)) => {
2167            let array = array
2168                .as_primitive::<Date64Type>()
2169                .unary::<_, TimestampSecondType>(|x| x / MILLISECONDS);
2170
2171            cast_with_options(&array, to_type, cast_options)
2172        }
2173        (Date64, Timestamp(TimeUnit::Millisecond, _)) => {
2174            let array = array
2175                .as_primitive::<Date64Type>()
2176                .reinterpret_cast::<TimestampMillisecondType>();
2177
2178            cast_with_options(&array, to_type, cast_options)
2179        }
2180
2181        (Date64, Timestamp(TimeUnit::Microsecond, _)) => {
2182            let array = array
2183                .as_primitive::<Date64Type>()
2184                .unary::<_, TimestampMicrosecondType>(|x| x * (MICROSECONDS / MILLISECONDS));
2185
2186            cast_with_options(&array, to_type, cast_options)
2187        }
2188        (Date64, Timestamp(TimeUnit::Nanosecond, _)) => {
2189            let array = array
2190                .as_primitive::<Date64Type>()
2191                .unary::<_, TimestampNanosecondType>(|x| x * (NANOSECONDS / MILLISECONDS));
2192
2193            cast_with_options(&array, to_type, cast_options)
2194        }
2195        (Date32, Timestamp(TimeUnit::Second, _)) => {
2196            let array = array
2197                .as_primitive::<Date32Type>()
2198                .unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY);
2199
2200            cast_with_options(&array, to_type, cast_options)
2201        }
2202        (Date32, Timestamp(TimeUnit::Millisecond, _)) => {
2203            let array = array
2204                .as_primitive::<Date32Type>()
2205                .unary::<_, TimestampMillisecondType>(|x| (x as i64) * MILLISECONDS_IN_DAY);
2206
2207            cast_with_options(&array, to_type, cast_options)
2208        }
2209        (Date32, Timestamp(TimeUnit::Microsecond, _)) => {
2210            let date_array = array.as_primitive::<Date32Type>();
2211            let converted = if cast_options.safe {
2212                date_array.unary_opt::<_, TimestampMicrosecondType>(|x| {
2213                    (x as i64).checked_mul(MICROSECONDS_IN_DAY)
2214                })
2215            } else {
2216                date_array.try_unary::<_, TimestampMicrosecondType, _>(|x| {
2217                    (x as i64).mul_checked(MICROSECONDS_IN_DAY)
2218                })?
2219            };
2220            cast_with_options(&converted, to_type, cast_options)
2221        }
2222        (Date32, Timestamp(TimeUnit::Nanosecond, _)) => {
2223            let date_array = array.as_primitive::<Date32Type>();
2224            let converted = if cast_options.safe {
2225                date_array.unary_opt::<_, TimestampNanosecondType>(|x| {
2226                    (x as i64).checked_mul(NANOSECONDS_IN_DAY)
2227                })
2228            } else {
2229                date_array.try_unary::<_, TimestampNanosecondType, _>(|x| {
2230                    (x as i64).mul_checked(NANOSECONDS_IN_DAY)
2231                })?
2232            };
2233            cast_with_options(&converted, to_type, cast_options)
2234        }
2235
2236        (_, Duration(unit)) if from_type.is_numeric() => {
2237            let array = cast_with_options(array, &Int64, cast_options)?;
2238            Ok(make_duration_array(array.as_primitive(), *unit))
2239        }
2240        (Duration(TimeUnit::Second), _) if to_type.is_numeric() => {
2241            let array = cast_reinterpret_arrays::<DurationSecondType, Int64Type>(array)?;
2242            cast_with_options(&array, to_type, cast_options)
2243        }
2244        (Duration(TimeUnit::Millisecond), _) if to_type.is_numeric() => {
2245            let array = cast_reinterpret_arrays::<DurationMillisecondType, Int64Type>(array)?;
2246            cast_with_options(&array, to_type, cast_options)
2247        }
2248        (Duration(TimeUnit::Microsecond), _) if to_type.is_numeric() => {
2249            let array = cast_reinterpret_arrays::<DurationMicrosecondType, Int64Type>(array)?;
2250            cast_with_options(&array, to_type, cast_options)
2251        }
2252        (Duration(TimeUnit::Nanosecond), _) if to_type.is_numeric() => {
2253            let array = cast_reinterpret_arrays::<DurationNanosecondType, Int64Type>(array)?;
2254            cast_with_options(&array, to_type, cast_options)
2255        }
2256
2257        (Duration(from_unit), Duration(to_unit)) => {
2258            let array = cast_with_options(array, &Int64, cast_options)?;
2259            let time_array = array.as_primitive::<Int64Type>();
2260            let from_size = time_unit_multiple(from_unit);
2261            let to_size = time_unit_multiple(to_unit);
2262            // we either divide or multiply, depending on size of each unit
2263            // units are never the same when the types are the same
2264            let converted = match from_size.cmp(&to_size) {
2265                Ordering::Greater => {
2266                    let divisor = from_size / to_size;
2267                    time_array.unary::<_, Int64Type>(|o| o / divisor)
2268                }
2269                Ordering::Equal => time_array.clone(),
2270                Ordering::Less => {
2271                    let mul = to_size / from_size;
2272                    if cast_options.safe {
2273                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
2274                    } else {
2275                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
2276                    }
2277                }
2278            };
2279            Ok(make_duration_array(&converted, *to_unit))
2280        }
2281
2282        (Duration(TimeUnit::Second), Interval(IntervalUnit::MonthDayNano)) => {
2283            cast_duration_to_interval::<DurationSecondType>(array, cast_options)
2284        }
2285        (Duration(TimeUnit::Millisecond), Interval(IntervalUnit::MonthDayNano)) => {
2286            cast_duration_to_interval::<DurationMillisecondType>(array, cast_options)
2287        }
2288        (Duration(TimeUnit::Microsecond), Interval(IntervalUnit::MonthDayNano)) => {
2289            cast_duration_to_interval::<DurationMicrosecondType>(array, cast_options)
2290        }
2291        (Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano)) => {
2292            cast_duration_to_interval::<DurationNanosecondType>(array, cast_options)
2293        }
2294        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Second)) => {
2295            cast_month_day_nano_to_duration::<DurationSecondType>(array, cast_options)
2296        }
2297        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Millisecond)) => {
2298            cast_month_day_nano_to_duration::<DurationMillisecondType>(array, cast_options)
2299        }
2300        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Microsecond)) => {
2301            cast_month_day_nano_to_duration::<DurationMicrosecondType>(array, cast_options)
2302        }
2303        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Nanosecond)) => {
2304            cast_month_day_nano_to_duration::<DurationNanosecondType>(array, cast_options)
2305        }
2306        (Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
2307            cast_interval_year_month_to_interval_month_day_nano(array, cast_options)
2308        }
2309        (Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
2310            cast_interval_day_time_to_interval_month_day_nano(array, cast_options)
2311        }
2312        (Int32, Interval(IntervalUnit::YearMonth)) => {
2313            cast_reinterpret_arrays::<Int32Type, IntervalYearMonthType>(array)
2314        }
2315        (_, _) => Err(ArrowError::CastError(format!(
2316            "Casting from {from_type} to {to_type} not supported",
2317        ))),
2318    }
2319}
2320
2321fn cast_struct_to_struct(
2322    array: &StructArray,
2323    from_fields: Fields,
2324    to_fields: Fields,
2325    cast_options: &CastOptions,
2326) -> Result<ArrayRef, ArrowError> {
2327    // Fast path: if field names are in the same order, we can just zip and cast
2328    let fields_match_order = from_fields.len() == to_fields.len()
2329        && from_fields
2330            .iter()
2331            .zip(to_fields.iter())
2332            .all(|(f1, f2)| f1.name() == f2.name());
2333
2334    let fields = if fields_match_order {
2335        // Fast path: cast columns in order if their names match
2336        cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2337    } else {
2338        let all_fields_match_by_name = to_fields.iter().all(|to_field| {
2339            from_fields
2340                .iter()
2341                .any(|from_field| from_field.name() == to_field.name())
2342        });
2343
2344        if all_fields_match_by_name {
2345            // Slow path: match fields by name and reorder
2346            cast_struct_fields_by_name(array, from_fields.clone(), to_fields.clone(), cast_options)?
2347        } else {
2348            // Fallback: cast field by field in order
2349            cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2350        }
2351    };
2352
2353    let array = StructArray::try_new(to_fields.clone(), fields, array.nulls().cloned())?;
2354    Ok(Arc::new(array) as ArrayRef)
2355}
2356
2357fn cast_struct_fields_by_name(
2358    array: &StructArray,
2359    from_fields: Fields,
2360    to_fields: Fields,
2361    cast_options: &CastOptions,
2362) -> Result<Vec<ArrayRef>, ArrowError> {
2363    to_fields
2364        .iter()
2365        .map(|to_field| {
2366            let from_field_idx = from_fields
2367                .iter()
2368                .position(|from_field| from_field.name() == to_field.name())
2369                .unwrap(); // safe because we checked above
2370            let column = array.column(from_field_idx);
2371            cast_with_options(column, to_field.data_type(), cast_options)
2372        })
2373        .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2374}
2375
2376fn cast_struct_fields_in_order(
2377    array: &StructArray,
2378    to_fields: Fields,
2379    cast_options: &CastOptions,
2380) -> Result<Vec<ArrayRef>, ArrowError> {
2381    array
2382        .columns()
2383        .iter()
2384        .zip(to_fields.iter())
2385        .map(|(l, field)| cast_with_options(l, field.data_type(), cast_options))
2386        .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2387}
2388
2389fn cast_from_decimal<D, F>(
2390    array: &dyn Array,
2391    base: D::Native,
2392    scale: &i8,
2393    from_type: &DataType,
2394    to_type: &DataType,
2395    as_float: F,
2396    cast_options: &CastOptions,
2397) -> Result<ArrayRef, ArrowError>
2398where
2399    D: DecimalType + ArrowPrimitiveType,
2400    <D as ArrowPrimitiveType>::Native: ToPrimitive,
2401    F: Fn(D::Native) -> f64,
2402{
2403    use DataType::*;
2404    // cast decimal to other type
2405    match to_type {
2406        UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, base, *scale, cast_options),
2407        UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, base, *scale, cast_options),
2408        UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, base, *scale, cast_options),
2409        UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, base, *scale, cast_options),
2410        Int8 => cast_decimal_to_integer::<D, Int8Type>(array, base, *scale, cast_options),
2411        Int16 => cast_decimal_to_integer::<D, Int16Type>(array, base, *scale, cast_options),
2412        Int32 => cast_decimal_to_integer::<D, Int32Type>(array, base, *scale, cast_options),
2413        Int64 => cast_decimal_to_integer::<D, Int64Type>(array, base, *scale, cast_options),
2414        Float16 => cast_decimal_to_float::<D, Float16Type, _>(array, |x| {
2415            half::f16::from_f64(single_decimal_to_float_lossy::<D, F>(
2416                &as_float,
2417                x,
2418                <i32 as From<i8>>::from(*scale),
2419            ))
2420        }),
2421        Float32 => cast_decimal_to_float::<D, Float32Type, _>(array, |x| {
2422            single_decimal_to_float_lossy::<D, F>(&as_float, x, <i32 as From<i8>>::from(*scale))
2423                as f32
2424        }),
2425        Float64 => cast_decimal_to_float::<D, Float64Type, _>(array, |x| {
2426            single_decimal_to_float_lossy::<D, F>(&as_float, x, <i32 as From<i8>>::from(*scale))
2427        }),
2428        Utf8View => value_to_string_view(array, cast_options),
2429        Utf8 => value_to_string::<i32>(array, cast_options),
2430        LargeUtf8 => value_to_string::<i64>(array, cast_options),
2431        Null => Ok(new_null_array(to_type, array.len())),
2432        _ => Err(ArrowError::CastError(format!(
2433            "Casting from {from_type} to {to_type} not supported"
2434        ))),
2435    }
2436}
2437
2438fn cast_to_decimal<D, M>(
2439    array: &dyn Array,
2440    base: M,
2441    precision: &u8,
2442    scale: &i8,
2443    from_type: &DataType,
2444    to_type: &DataType,
2445    cast_options: &CastOptions,
2446) -> Result<ArrayRef, ArrowError>
2447where
2448    D: DecimalType + ArrowPrimitiveType<Native = M>,
2449    M: ArrowNativeTypeOp + DecimalCast,
2450{
2451    use DataType::*;
2452    // cast data to decimal
2453    match from_type {
2454        UInt8 => cast_integer_to_decimal::<_, D, M>(
2455            array.as_primitive::<UInt8Type>(),
2456            *precision,
2457            *scale,
2458            base,
2459            cast_options,
2460        ),
2461        UInt16 => cast_integer_to_decimal::<_, D, _>(
2462            array.as_primitive::<UInt16Type>(),
2463            *precision,
2464            *scale,
2465            base,
2466            cast_options,
2467        ),
2468        UInt32 => cast_integer_to_decimal::<_, D, _>(
2469            array.as_primitive::<UInt32Type>(),
2470            *precision,
2471            *scale,
2472            base,
2473            cast_options,
2474        ),
2475        UInt64 => cast_integer_to_decimal::<_, D, _>(
2476            array.as_primitive::<UInt64Type>(),
2477            *precision,
2478            *scale,
2479            base,
2480            cast_options,
2481        ),
2482        Int8 => cast_integer_to_decimal::<_, D, _>(
2483            array.as_primitive::<Int8Type>(),
2484            *precision,
2485            *scale,
2486            base,
2487            cast_options,
2488        ),
2489        Int16 => cast_integer_to_decimal::<_, D, _>(
2490            array.as_primitive::<Int16Type>(),
2491            *precision,
2492            *scale,
2493            base,
2494            cast_options,
2495        ),
2496        Int32 => cast_integer_to_decimal::<_, D, _>(
2497            array.as_primitive::<Int32Type>(),
2498            *precision,
2499            *scale,
2500            base,
2501            cast_options,
2502        ),
2503        Int64 => cast_integer_to_decimal::<_, D, _>(
2504            array.as_primitive::<Int64Type>(),
2505            *precision,
2506            *scale,
2507            base,
2508            cast_options,
2509        ),
2510        Float16 => cast_floating_point_to_decimal::<_, D>(
2511            array.as_primitive::<Float16Type>(),
2512            *precision,
2513            *scale,
2514            cast_options,
2515        ),
2516        Float32 => cast_floating_point_to_decimal::<_, D>(
2517            array.as_primitive::<Float32Type>(),
2518            *precision,
2519            *scale,
2520            cast_options,
2521        ),
2522        Float64 => cast_floating_point_to_decimal::<_, D>(
2523            array.as_primitive::<Float64Type>(),
2524            *precision,
2525            *scale,
2526            cast_options,
2527        ),
2528        Utf8View | Utf8 => {
2529            cast_string_to_decimal::<D, i32>(array, *precision, *scale, cast_options)
2530        }
2531        LargeUtf8 => cast_string_to_decimal::<D, i64>(array, *precision, *scale, cast_options),
2532        Null => Ok(new_null_array(to_type, array.len())),
2533        _ => Err(ArrowError::CastError(format!(
2534            "Casting from {from_type} to {to_type} not supported"
2535        ))),
2536    }
2537}
2538
2539/// Get the time unit as a multiple of a second
2540const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
2541    match unit {
2542        TimeUnit::Second => 1,
2543        TimeUnit::Millisecond => MILLISECONDS,
2544        TimeUnit::Microsecond => MICROSECONDS,
2545        TimeUnit::Nanosecond => NANOSECONDS,
2546    }
2547}
2548
2549/// Convert Array into a PrimitiveArray of type, and apply numeric cast
2550fn cast_numeric_arrays<FROM, TO>(
2551    from: &dyn Array,
2552    cast_options: &CastOptions,
2553) -> Result<ArrayRef, ArrowError>
2554where
2555    FROM: ArrowPrimitiveType,
2556    TO: ArrowPrimitiveType,
2557    FROM::Native: NumCast,
2558    TO::Native: NumCast,
2559{
2560    if cast_options.safe {
2561        // If the value can't be casted to the `TO::Native`, return null
2562        Ok(Arc::new(numeric_cast::<FROM, TO>(
2563            from.as_primitive::<FROM>(),
2564        )))
2565    } else {
2566        // If the value can't be casted to the `TO::Native`, return error
2567        Ok(Arc::new(try_numeric_cast::<FROM, TO>(
2568            from.as_primitive::<FROM>(),
2569        )?))
2570    }
2571}
2572
2573// Natural cast between numeric types
2574// If the value of T can't be casted to R, will throw error
2575fn try_numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>, ArrowError>
2576where
2577    T: ArrowPrimitiveType,
2578    R: ArrowPrimitiveType,
2579    T::Native: NumCast,
2580    R::Native: NumCast,
2581{
2582    from.try_unary(|value| {
2583        num_cast::<T::Native, R::Native>(value).ok_or_else(|| {
2584            ArrowError::CastError(format!(
2585                "Can't cast value {:?} to type {}",
2586                value,
2587                R::DATA_TYPE
2588            ))
2589        })
2590    })
2591}
2592
2593/// Natural cast between numeric types
2594/// Return None if the input `value` can't be casted to type `O`.
2595#[inline]
2596pub fn num_cast<I, O>(value: I) -> Option<O>
2597where
2598    I: NumCast,
2599    O: NumCast,
2600{
2601    num_traits::cast::cast::<I, O>(value)
2602}
2603
2604// Natural cast between numeric types
2605// If the value of T can't be casted to R, it will be converted to null
2606fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> PrimitiveArray<R>
2607where
2608    T: ArrowPrimitiveType,
2609    R: ArrowPrimitiveType,
2610    T::Native: NumCast,
2611    R::Native: NumCast,
2612{
2613    from.unary_opt::<_, R>(num_cast::<T::Native, R::Native>)
2614}
2615
2616fn cast_numeric_to_binary<FROM: ArrowPrimitiveType, O: OffsetSizeTrait>(
2617    array: &dyn Array,
2618) -> Result<ArrayRef, ArrowError> {
2619    let array = array.as_primitive::<FROM>();
2620    let size = std::mem::size_of::<FROM::Native>();
2621    let offsets = OffsetBuffer::from_repeated_length(size, array.len());
2622    Ok(Arc::new(GenericBinaryArray::<O>::try_new(
2623        offsets,
2624        array.values().inner().clone(),
2625        array.nulls().cloned(),
2626    )?))
2627}
2628
2629fn adjust_timestamp_to_timezone<T: ArrowTimestampType>(
2630    array: PrimitiveArray<Int64Type>,
2631    to_tz: &Tz,
2632    cast_options: &CastOptions,
2633) -> Result<PrimitiveArray<Int64Type>, ArrowError> {
2634    let adjust = |o| {
2635        let local = as_datetime::<T>(o)?;
2636        let offset = to_tz.offset_from_local_datetime(&local).single()?;
2637        T::from_naive_datetime(local - offset.fix(), None)
2638    };
2639    let adjusted = if cast_options.safe {
2640        array.unary_opt::<_, Int64Type>(adjust)
2641    } else {
2642        array.try_unary::<_, Int64Type, _>(|o| {
2643            adjust(o).ok_or_else(|| {
2644                ArrowError::CastError("Cannot cast timezone to different timezone".to_string())
2645            })
2646        })?
2647    };
2648    Ok(adjusted)
2649}
2650
2651/// Cast numeric types to Boolean
2652///
2653/// Any zero value returns `false` while non-zero returns `true`
2654fn cast_numeric_to_bool<FROM>(from: &dyn Array) -> Result<ArrayRef, ArrowError>
2655where
2656    FROM: ArrowPrimitiveType,
2657{
2658    numeric_to_bool_cast::<FROM>(from.as_primitive::<FROM>()).map(|to| Arc::new(to) as ArrayRef)
2659}
2660
2661fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray, ArrowError>
2662where
2663    T: ArrowPrimitiveType,
2664{
2665    let mut b = BooleanBuilder::with_capacity(from.len());
2666
2667    for i in 0..from.len() {
2668        if from.is_null(i) {
2669            b.append_null();
2670        } else {
2671            b.append_value(cast_num_to_bool::<T::Native>(from.value(i)));
2672        }
2673    }
2674
2675    Ok(b.finish())
2676}
2677
2678/// Cast numeric types to boolean
2679#[inline]
2680pub fn cast_num_to_bool<I>(value: I) -> bool
2681where
2682    I: Default + PartialEq,
2683{
2684    value != I::default()
2685}
2686
2687/// Cast Boolean types to numeric
2688///
2689/// `false` returns 0 while `true` returns 1
2690fn cast_bool_to_numeric<TO>(
2691    from: &dyn Array,
2692    cast_options: &CastOptions,
2693) -> Result<ArrayRef, ArrowError>
2694where
2695    TO: ArrowPrimitiveType,
2696    TO::Native: num_traits::cast::NumCast,
2697{
2698    Ok(Arc::new(bool_to_numeric_cast::<TO>(
2699        from.as_any().downcast_ref::<BooleanArray>().unwrap(),
2700        cast_options,
2701    )))
2702}
2703
2704fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions) -> PrimitiveArray<T>
2705where
2706    T: ArrowPrimitiveType,
2707    T::Native: num_traits::NumCast,
2708{
2709    let iter = (0..from.len()).map(|i| {
2710        if from.is_null(i) {
2711            None
2712        } else {
2713            single_bool_to_numeric::<T::Native>(from.value(i))
2714        }
2715    });
2716    // Benefit:
2717    //     20% performance improvement
2718    // Soundness:
2719    //     The iterator is trustedLen because it comes from a Range
2720    unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
2721}
2722
2723/// Cast single bool value to numeric value.
2724#[inline]
2725pub fn single_bool_to_numeric<O>(value: bool) -> Option<O>
2726where
2727    O: num_traits::NumCast + Default,
2728{
2729    if value {
2730        // a workaround to cast a primitive to type O, infallible
2731        num_traits::cast::cast(1)
2732    } else {
2733        Some(O::default())
2734    }
2735}
2736
2737/// Helper function to cast from one `BinaryArray` or 'LargeBinaryArray' to 'FixedSizeBinaryArray'.
2738fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
2739    array: &dyn Array,
2740    byte_width: i32,
2741    cast_options: &CastOptions,
2742) -> Result<ArrayRef, ArrowError> {
2743    let array = array.as_binary::<O>();
2744    let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(), byte_width);
2745
2746    for i in 0..array.len() {
2747        if array.is_null(i) {
2748            builder.append_null();
2749        } else {
2750            match builder.append_value(array.value(i)) {
2751                Ok(()) => {}
2752                Err(e) => match cast_options.safe {
2753                    true => builder.append_null(),
2754                    false => return Err(e),
2755                },
2756            }
2757        }
2758    }
2759
2760    Ok(Arc::new(builder.finish()))
2761}
2762
2763/// Helper function to cast from 'FixedSizeBinaryArray' to one `BinaryArray` or 'LargeBinaryArray'.
2764/// If the target one is too large for the source array it will return an Error.
2765fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
2766    array: &dyn Array,
2767    byte_width: i32,
2768) -> Result<ArrayRef, ArrowError> {
2769    let array = array
2770        .as_any()
2771        .downcast_ref::<FixedSizeBinaryArray>()
2772        .unwrap();
2773
2774    let offsets: i128 = byte_width as i128 * array.len() as i128;
2775
2776    let is_binary = matches!(GenericBinaryType::<O>::DATA_TYPE, DataType::Binary);
2777    if is_binary && offsets > i32::MAX as i128 {
2778        return Err(ArrowError::ComputeError(
2779            "FixedSizeBinary array too large to cast to Binary array".to_string(),
2780        ));
2781    } else if !is_binary && offsets > i64::MAX as i128 {
2782        return Err(ArrowError::ComputeError(
2783            "FixedSizeBinary array too large to cast to LargeBinary array".to_string(),
2784        ));
2785    }
2786
2787    let mut builder = GenericBinaryBuilder::<O>::with_capacity(array.len(), array.len());
2788
2789    for i in 0..array.len() {
2790        if array.is_null(i) {
2791            builder.append_null();
2792        } else {
2793            builder.append_value(array.value(i));
2794        }
2795    }
2796
2797    Ok(Arc::new(builder.finish()))
2798}
2799
2800fn cast_fixed_size_binary_to_binary_view(
2801    array: &dyn Array,
2802    _byte_width: i32,
2803) -> Result<ArrayRef, ArrowError> {
2804    let array = array
2805        .as_any()
2806        .downcast_ref::<FixedSizeBinaryArray>()
2807        .unwrap();
2808
2809    let mut builder = BinaryViewBuilder::with_capacity(array.len());
2810    for i in 0..array.len() {
2811        if array.is_null(i) {
2812            builder.append_null();
2813        } else {
2814            builder.append_value(array.value(i));
2815        }
2816    }
2817
2818    Ok(Arc::new(builder.finish()))
2819}
2820
2821/// Helper function to cast from one `ByteArrayType` to another and vice versa.
2822/// If the target one (e.g., `LargeUtf8`) is too large for the source array it will return an Error.
2823fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2824where
2825    FROM: ByteArrayType,
2826    TO: ByteArrayType<Native = FROM::Native>,
2827    FROM::Offset: OffsetSizeTrait + ToPrimitive,
2828    TO::Offset: OffsetSizeTrait + NumCast,
2829{
2830    let data = array.to_data();
2831    assert_eq!(data.data_type(), &FROM::DATA_TYPE);
2832    let str_values_buf = data.buffers()[1].clone();
2833    let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
2834
2835    let mut cast_offsets = Vec::<TO::Offset>::with_capacity(offsets.len());
2836    offsets
2837        .iter()
2838        .try_for_each::<_, Result<_, ArrowError>>(|offset| {
2839            let offset =
2840                <<TO as ByteArrayType>::Offset as NumCast>::from(*offset).ok_or_else(|| {
2841                    ArrowError::ComputeError(format!(
2842                        "{}{} array too large to cast to {}{} array",
2843                        FROM::Offset::PREFIX,
2844                        FROM::PREFIX,
2845                        TO::Offset::PREFIX,
2846                        TO::PREFIX
2847                    ))
2848                })?;
2849            cast_offsets.push(offset);
2850            Ok(())
2851        })?;
2852
2853    let offset_buffer = Buffer::from_vec(cast_offsets);
2854
2855    let dtype = TO::DATA_TYPE;
2856
2857    let builder = ArrayData::builder(dtype)
2858        .offset(array.offset())
2859        .len(array.len())
2860        .add_buffer(offset_buffer)
2861        .add_buffer(str_values_buf)
2862        .nulls(data.nulls().cloned());
2863
2864    let array_data = unsafe { builder.build_unchecked() };
2865
2866    Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
2867}
2868
2869/// Helper function to cast from one `ByteViewType` array to `ByteArrayType` array.
2870fn cast_view_to_byte<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2871where
2872    FROM: ByteViewType,
2873    TO: ByteArrayType,
2874    FROM::Native: AsRef<TO::Native>,
2875{
2876    let data = array.to_data();
2877    let view_array = GenericByteViewArray::<FROM>::from(data);
2878
2879    let len = view_array.len();
2880    let bytes = view_array
2881        .views()
2882        .iter()
2883        .map(|v| ByteView::from(*v).length as usize)
2884        .sum::<usize>();
2885
2886    let mut byte_array_builder = GenericByteBuilder::<TO>::with_capacity(len, bytes);
2887
2888    for val in &view_array {
2889        byte_array_builder.append_option(val);
2890    }
2891
2892    Ok(Arc::new(byte_array_builder.finish()))
2893}
2894
2895#[cfg(test)]
2896mod tests {
2897    use super::*;
2898    use DataType::*;
2899    use arrow_array::{Int64Array, RunArray, StringArray};
2900    use arrow_buffer::{Buffer, IntervalDayTime, NullBuffer};
2901    use arrow_buffer::{ScalarBuffer, i256};
2902    use arrow_schema::{DataType, Field};
2903    use chrono::NaiveDate;
2904    use half::f16;
2905    use std::sync::Arc;
2906
2907    #[derive(Clone)]
2908    struct DecimalCastTestConfig {
2909        input_prec: u8,
2910        input_scale: i8,
2911        input_repr: i128,
2912        output_prec: u8,
2913        output_scale: i8,
2914        expected_output_repr: Result<i128, String>, // the error variant can contain a string
2915                                                    // template where the "{}" will be
2916                                                    // replaced with the decimal type name
2917                                                    // (e.g. Decimal128)
2918    }
2919
2920    macro_rules! generate_cast_test_case {
2921        ($INPUT_ARRAY: expr, $OUTPUT_TYPE_ARRAY: ident, $OUTPUT_TYPE: expr, $OUTPUT_VALUES: expr) => {
2922            let output =
2923                $OUTPUT_TYPE_ARRAY::from($OUTPUT_VALUES).with_data_type($OUTPUT_TYPE.clone());
2924
2925            // assert cast type
2926            let input_array_type = $INPUT_ARRAY.data_type();
2927            assert!(can_cast_types(input_array_type, $OUTPUT_TYPE));
2928            let result = cast($INPUT_ARRAY, $OUTPUT_TYPE).unwrap();
2929            assert_eq!($OUTPUT_TYPE, result.data_type());
2930            assert_eq!(result.as_ref(), &output);
2931
2932            let cast_option = CastOptions {
2933                safe: false,
2934                format_options: FormatOptions::default(),
2935            };
2936            let result = cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap();
2937            assert_eq!($OUTPUT_TYPE, result.data_type());
2938            assert_eq!(result.as_ref(), &output);
2939        };
2940    }
2941
2942    fn run_decimal_cast_test_case<I, O>(t: DecimalCastTestConfig)
2943    where
2944        I: DecimalType,
2945        O: DecimalType,
2946        I::Native: DecimalCast,
2947        O::Native: DecimalCast,
2948    {
2949        let array = vec![I::Native::from_decimal(t.input_repr)];
2950        let array = array
2951            .into_iter()
2952            .collect::<PrimitiveArray<I>>()
2953            .with_precision_and_scale(t.input_prec, t.input_scale)
2954            .unwrap();
2955        let input_type = array.data_type();
2956        let output_type = O::TYPE_CONSTRUCTOR(t.output_prec, t.output_scale);
2957        assert!(can_cast_types(input_type, &output_type));
2958
2959        let options = CastOptions {
2960            safe: false,
2961            ..Default::default()
2962        };
2963        let result = cast_with_options(&array, &output_type, &options);
2964
2965        match t.expected_output_repr {
2966            Ok(v) => {
2967                let expected_array = vec![O::Native::from_decimal(v)];
2968                let expected_array = expected_array
2969                    .into_iter()
2970                    .collect::<PrimitiveArray<O>>()
2971                    .with_precision_and_scale(t.output_prec, t.output_scale)
2972                    .unwrap();
2973                assert_eq!(*result.unwrap(), expected_array);
2974            }
2975            Err(expected_output_message_template) => {
2976                assert!(result.is_err());
2977                let expected_error_message =
2978                    expected_output_message_template.replace("{}", O::PREFIX);
2979                assert_eq!(result.unwrap_err().to_string(), expected_error_message);
2980            }
2981        }
2982    }
2983
2984    fn create_decimal32_array(
2985        array: Vec<Option<i32>>,
2986        precision: u8,
2987        scale: i8,
2988    ) -> Result<Decimal32Array, ArrowError> {
2989        array
2990            .into_iter()
2991            .collect::<Decimal32Array>()
2992            .with_precision_and_scale(precision, scale)
2993    }
2994
2995    fn create_decimal64_array(
2996        array: Vec<Option<i64>>,
2997        precision: u8,
2998        scale: i8,
2999    ) -> Result<Decimal64Array, ArrowError> {
3000        array
3001            .into_iter()
3002            .collect::<Decimal64Array>()
3003            .with_precision_and_scale(precision, scale)
3004    }
3005
3006    fn create_decimal128_array(
3007        array: Vec<Option<i128>>,
3008        precision: u8,
3009        scale: i8,
3010    ) -> Result<Decimal128Array, ArrowError> {
3011        array
3012            .into_iter()
3013            .collect::<Decimal128Array>()
3014            .with_precision_and_scale(precision, scale)
3015    }
3016
3017    fn create_decimal256_array(
3018        array: Vec<Option<i256>>,
3019        precision: u8,
3020        scale: i8,
3021    ) -> Result<Decimal256Array, ArrowError> {
3022        array
3023            .into_iter()
3024            .collect::<Decimal256Array>()
3025            .with_precision_and_scale(precision, scale)
3026    }
3027
3028    #[test]
3029    #[cfg(not(feature = "force_validate"))]
3030    #[should_panic(
3031        expected = "Cannot cast to Decimal128(20, 3). Overflowing on 57896044618658097711785492504343953926634992332820282019728792003956564819967"
3032    )]
3033    fn test_cast_decimal_to_decimal_round_with_error() {
3034        // decimal256 to decimal128 overflow
3035        let array = vec![
3036            Some(i256::from_i128(1123454)),
3037            Some(i256::from_i128(2123456)),
3038            Some(i256::from_i128(-3123453)),
3039            Some(i256::from_i128(-3123456)),
3040            None,
3041            Some(i256::MAX),
3042            Some(i256::MIN),
3043        ];
3044        let input_decimal_array = create_decimal256_array(array, 76, 4).unwrap();
3045        let array = Arc::new(input_decimal_array) as ArrayRef;
3046        let input_type = DataType::Decimal256(76, 4);
3047        let output_type = DataType::Decimal128(20, 3);
3048        assert!(can_cast_types(&input_type, &output_type));
3049        generate_cast_test_case!(
3050            &array,
3051            Decimal128Array,
3052            &output_type,
3053            vec![
3054                Some(112345_i128),
3055                Some(212346_i128),
3056                Some(-312345_i128),
3057                Some(-312346_i128),
3058                None,
3059                None,
3060                None,
3061            ]
3062        );
3063    }
3064
3065    #[test]
3066    #[cfg(not(feature = "force_validate"))]
3067    fn test_cast_decimal_to_decimal_round() {
3068        let array = vec![
3069            Some(1123454),
3070            Some(2123456),
3071            Some(-3123453),
3072            Some(-3123456),
3073            None,
3074        ];
3075        let array = create_decimal128_array(array, 20, 4).unwrap();
3076        // decimal128 to decimal128
3077        let input_type = DataType::Decimal128(20, 4);
3078        let output_type = DataType::Decimal128(20, 3);
3079        assert!(can_cast_types(&input_type, &output_type));
3080        generate_cast_test_case!(
3081            &array,
3082            Decimal128Array,
3083            &output_type,
3084            vec![
3085                Some(112345_i128),
3086                Some(212346_i128),
3087                Some(-312345_i128),
3088                Some(-312346_i128),
3089                None
3090            ]
3091        );
3092
3093        // decimal128 to decimal256
3094        let input_type = DataType::Decimal128(20, 4);
3095        let output_type = DataType::Decimal256(20, 3);
3096        assert!(can_cast_types(&input_type, &output_type));
3097        generate_cast_test_case!(
3098            &array,
3099            Decimal256Array,
3100            &output_type,
3101            vec![
3102                Some(i256::from_i128(112345_i128)),
3103                Some(i256::from_i128(212346_i128)),
3104                Some(i256::from_i128(-312345_i128)),
3105                Some(i256::from_i128(-312346_i128)),
3106                None
3107            ]
3108        );
3109
3110        // decimal256
3111        let array = vec![
3112            Some(i256::from_i128(1123454)),
3113            Some(i256::from_i128(2123456)),
3114            Some(i256::from_i128(-3123453)),
3115            Some(i256::from_i128(-3123456)),
3116            None,
3117        ];
3118        let array = create_decimal256_array(array, 20, 4).unwrap();
3119
3120        // decimal256 to decimal256
3121        let input_type = DataType::Decimal256(20, 4);
3122        let output_type = DataType::Decimal256(20, 3);
3123        assert!(can_cast_types(&input_type, &output_type));
3124        generate_cast_test_case!(
3125            &array,
3126            Decimal256Array,
3127            &output_type,
3128            vec![
3129                Some(i256::from_i128(112345_i128)),
3130                Some(i256::from_i128(212346_i128)),
3131                Some(i256::from_i128(-312345_i128)),
3132                Some(i256::from_i128(-312346_i128)),
3133                None
3134            ]
3135        );
3136        // decimal256 to decimal128
3137        let input_type = DataType::Decimal256(20, 4);
3138        let output_type = DataType::Decimal128(20, 3);
3139        assert!(can_cast_types(&input_type, &output_type));
3140        generate_cast_test_case!(
3141            &array,
3142            Decimal128Array,
3143            &output_type,
3144            vec![
3145                Some(112345_i128),
3146                Some(212346_i128),
3147                Some(-312345_i128),
3148                Some(-312346_i128),
3149                None
3150            ]
3151        );
3152    }
3153
3154    #[test]
3155    fn test_cast_decimal32_to_decimal32() {
3156        // test changing precision
3157        let input_type = DataType::Decimal32(9, 3);
3158        let output_type = DataType::Decimal32(9, 4);
3159        assert!(can_cast_types(&input_type, &output_type));
3160        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3161        let array = create_decimal32_array(array, 9, 3).unwrap();
3162        generate_cast_test_case!(
3163            &array,
3164            Decimal32Array,
3165            &output_type,
3166            vec![
3167                Some(11234560_i32),
3168                Some(21234560_i32),
3169                Some(31234560_i32),
3170                None
3171            ]
3172        );
3173        // negative test
3174        let array = vec![Some(123456), None];
3175        let array = create_decimal32_array(array, 9, 0).unwrap();
3176        let result_safe = cast(&array, &DataType::Decimal32(2, 2));
3177        assert!(result_safe.is_ok());
3178        let options = CastOptions {
3179            safe: false,
3180            ..Default::default()
3181        };
3182
3183        let result_unsafe = cast_with_options(&array, &DataType::Decimal32(2, 2), &options);
3184        assert_eq!(
3185            "Invalid argument error: 123456.00 is too large to store in a Decimal32 of precision 2. Max is 0.99",
3186            result_unsafe.unwrap_err().to_string()
3187        );
3188    }
3189
3190    #[test]
3191    fn test_cast_decimal64_to_decimal64() {
3192        // test changing precision
3193        let input_type = DataType::Decimal64(17, 3);
3194        let output_type = DataType::Decimal64(17, 4);
3195        assert!(can_cast_types(&input_type, &output_type));
3196        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3197        let array = create_decimal64_array(array, 17, 3).unwrap();
3198        generate_cast_test_case!(
3199            &array,
3200            Decimal64Array,
3201            &output_type,
3202            vec![
3203                Some(11234560_i64),
3204                Some(21234560_i64),
3205                Some(31234560_i64),
3206                None
3207            ]
3208        );
3209        // negative test
3210        let array = vec![Some(123456), None];
3211        let array = create_decimal64_array(array, 9, 0).unwrap();
3212        let result_safe = cast(&array, &DataType::Decimal64(2, 2));
3213        assert!(result_safe.is_ok());
3214        let options = CastOptions {
3215            safe: false,
3216            ..Default::default()
3217        };
3218
3219        let result_unsafe = cast_with_options(&array, &DataType::Decimal64(2, 2), &options);
3220        assert_eq!(
3221            "Invalid argument error: 123456.00 is too large to store in a Decimal64 of precision 2. Max is 0.99",
3222            result_unsafe.unwrap_err().to_string()
3223        );
3224    }
3225
3226    #[test]
3227    fn test_cast_decimal128_to_decimal128() {
3228        // test changing precision
3229        let input_type = DataType::Decimal128(20, 3);
3230        let output_type = DataType::Decimal128(20, 4);
3231        assert!(can_cast_types(&input_type, &output_type));
3232        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3233        let array = create_decimal128_array(array, 20, 3).unwrap();
3234        generate_cast_test_case!(
3235            &array,
3236            Decimal128Array,
3237            &output_type,
3238            vec![
3239                Some(11234560_i128),
3240                Some(21234560_i128),
3241                Some(31234560_i128),
3242                None
3243            ]
3244        );
3245        // negative test
3246        let array = vec![Some(123456), None];
3247        let array = create_decimal128_array(array, 10, 0).unwrap();
3248        let result_safe = cast(&array, &DataType::Decimal128(2, 2));
3249        assert!(result_safe.is_ok());
3250        let options = CastOptions {
3251            safe: false,
3252            ..Default::default()
3253        };
3254
3255        let result_unsafe = cast_with_options(&array, &DataType::Decimal128(2, 2), &options);
3256        assert_eq!(
3257            "Invalid argument error: 123456.00 is too large to store in a Decimal128 of precision 2. Max is 0.99",
3258            result_unsafe.unwrap_err().to_string()
3259        );
3260    }
3261
3262    #[test]
3263    fn test_cast_decimal32_to_decimal32_dict() {
3264        let p = 9;
3265        let s = 3;
3266        let input_type = DataType::Decimal32(p, s);
3267        let output_type = DataType::Dictionary(
3268            Box::new(DataType::Int32),
3269            Box::new(DataType::Decimal32(p, s)),
3270        );
3271        assert!(can_cast_types(&input_type, &output_type));
3272        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3273        let array = create_decimal32_array(array, p, s).unwrap();
3274        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3275        assert_eq!(cast_array.data_type(), &output_type);
3276    }
3277
3278    #[test]
3279    fn test_cast_decimal64_to_decimal64_dict() {
3280        let p = 15;
3281        let s = 3;
3282        let input_type = DataType::Decimal64(p, s);
3283        let output_type = DataType::Dictionary(
3284            Box::new(DataType::Int32),
3285            Box::new(DataType::Decimal64(p, s)),
3286        );
3287        assert!(can_cast_types(&input_type, &output_type));
3288        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3289        let array = create_decimal64_array(array, p, s).unwrap();
3290        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3291        assert_eq!(cast_array.data_type(), &output_type);
3292    }
3293
3294    #[test]
3295    fn test_cast_decimal128_to_decimal128_dict() {
3296        let p = 20;
3297        let s = 3;
3298        let input_type = DataType::Decimal128(p, s);
3299        let output_type = DataType::Dictionary(
3300            Box::new(DataType::Int32),
3301            Box::new(DataType::Decimal128(p, s)),
3302        );
3303        assert!(can_cast_types(&input_type, &output_type));
3304        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3305        let array = create_decimal128_array(array, p, s).unwrap();
3306        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3307        assert_eq!(cast_array.data_type(), &output_type);
3308    }
3309
3310    #[test]
3311    fn test_cast_decimal256_to_decimal256_dict() {
3312        let p = 20;
3313        let s = 3;
3314        let input_type = DataType::Decimal256(p, s);
3315        let output_type = DataType::Dictionary(
3316            Box::new(DataType::Int32),
3317            Box::new(DataType::Decimal256(p, s)),
3318        );
3319        assert!(can_cast_types(&input_type, &output_type));
3320        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3321        let array = create_decimal128_array(array, p, s).unwrap();
3322        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3323        assert_eq!(cast_array.data_type(), &output_type);
3324    }
3325
3326    #[test]
3327    fn test_cast_decimal32_to_decimal32_overflow() {
3328        let input_type = DataType::Decimal32(9, 3);
3329        let output_type = DataType::Decimal32(9, 9);
3330        assert!(can_cast_types(&input_type, &output_type));
3331
3332        let array = vec![Some(i32::MAX)];
3333        let array = create_decimal32_array(array, 9, 3).unwrap();
3334        let result = cast_with_options(
3335            &array,
3336            &output_type,
3337            &CastOptions {
3338                safe: false,
3339                format_options: FormatOptions::default(),
3340            },
3341        );
3342        assert_eq!(
3343            "Cast error: Cannot cast to Decimal32(9, 9). Overflowing on 2147483647",
3344            result.unwrap_err().to_string()
3345        );
3346    }
3347
3348    #[test]
3349    fn test_cast_decimal32_to_decimal32_large_scale_reduction() {
3350        let array = vec![Some(-999999999), Some(0), Some(999999999), None];
3351        let array = create_decimal32_array(array, 9, 3).unwrap();
3352
3353        // Divide out all digits of precision -- rounding could still produce +/- 1
3354        let output_type = DataType::Decimal32(9, -6);
3355        assert!(can_cast_types(array.data_type(), &output_type));
3356        generate_cast_test_case!(
3357            &array,
3358            Decimal32Array,
3359            &output_type,
3360            vec![Some(-1), Some(0), Some(1), None]
3361        );
3362
3363        // Divide out more digits than we have precision -- all-zero result
3364        let output_type = DataType::Decimal32(9, -7);
3365        assert!(can_cast_types(array.data_type(), &output_type));
3366        generate_cast_test_case!(
3367            &array,
3368            Decimal32Array,
3369            &output_type,
3370            vec![Some(0), Some(0), Some(0), None]
3371        );
3372    }
3373
3374    #[test]
3375    fn test_cast_decimal64_to_decimal64_overflow() {
3376        let input_type = DataType::Decimal64(18, 3);
3377        let output_type = DataType::Decimal64(18, 18);
3378        assert!(can_cast_types(&input_type, &output_type));
3379
3380        let array = vec![Some(i64::MAX)];
3381        let array = create_decimal64_array(array, 18, 3).unwrap();
3382        let result = cast_with_options(
3383            &array,
3384            &output_type,
3385            &CastOptions {
3386                safe: false,
3387                format_options: FormatOptions::default(),
3388            },
3389        );
3390        assert_eq!(
3391            "Cast error: Cannot cast to Decimal64(18, 18). Overflowing on 9223372036854775807",
3392            result.unwrap_err().to_string()
3393        );
3394    }
3395
3396    #[test]
3397    fn test_cast_decimal64_to_decimal64_large_scale_reduction() {
3398        let array = vec![
3399            Some(-999999999999999999),
3400            Some(0),
3401            Some(999999999999999999),
3402            None,
3403        ];
3404        let array = create_decimal64_array(array, 18, 3).unwrap();
3405
3406        // Divide out all digits of precision -- rounding could still produce +/- 1
3407        let output_type = DataType::Decimal64(18, -15);
3408        assert!(can_cast_types(array.data_type(), &output_type));
3409        generate_cast_test_case!(
3410            &array,
3411            Decimal64Array,
3412            &output_type,
3413            vec![Some(-1), Some(0), Some(1), None]
3414        );
3415
3416        // Divide out more digits than we have precision -- all-zero result
3417        let output_type = DataType::Decimal64(18, -16);
3418        assert!(can_cast_types(array.data_type(), &output_type));
3419        generate_cast_test_case!(
3420            &array,
3421            Decimal64Array,
3422            &output_type,
3423            vec![Some(0), Some(0), Some(0), None]
3424        );
3425    }
3426
3427    #[test]
3428    fn test_cast_floating_to_decimals() {
3429        for output_type in [
3430            DataType::Decimal32(9, 3),
3431            DataType::Decimal64(9, 3),
3432            DataType::Decimal128(9, 3),
3433            DataType::Decimal256(9, 3),
3434        ] {
3435            let input_type = DataType::Float64;
3436            assert!(can_cast_types(&input_type, &output_type));
3437
3438            let array = vec![Some(1.1_f64)];
3439            let array = PrimitiveArray::<Float64Type>::from_iter(array);
3440            let result = cast_with_options(
3441                &array,
3442                &output_type,
3443                &CastOptions {
3444                    safe: false,
3445                    format_options: FormatOptions::default(),
3446                },
3447            );
3448            assert!(
3449                result.is_ok(),
3450                "Failed to cast to {output_type} with: {}",
3451                result.unwrap_err()
3452            );
3453        }
3454    }
3455
3456    #[test]
3457    #[cfg_attr(miri, ignore)] // Takes too long
3458    fn test_cast_float16_to_decimals() {
3459        let array = Float16Array::from(vec![
3460            Some(f16::from_f32(1.25)),
3461            Some(f16::from_f32(-2.5)),
3462            Some(f16::from_f32(1.125)),
3463            Some(f16::from_f32(-1.125)),
3464            Some(f16::from_f32(0.0)),
3465            None,
3466        ]);
3467
3468        generate_cast_test_case!(
3469            &array,
3470            Decimal32Array,
3471            &DataType::Decimal32(9, 2),
3472            vec![
3473                Some(125_i32),
3474                Some(-250_i32),
3475                Some(113_i32),
3476                Some(-113_i32),
3477                Some(0_i32),
3478                None
3479            ]
3480        );
3481        generate_cast_test_case!(
3482            &array,
3483            Decimal64Array,
3484            &DataType::Decimal64(18, 2),
3485            vec![
3486                Some(125_i64),
3487                Some(-250_i64),
3488                Some(113_i64),
3489                Some(-113_i64),
3490                Some(0_i64),
3491                None
3492            ]
3493        );
3494        generate_cast_test_case!(
3495            &array,
3496            Decimal128Array,
3497            &DataType::Decimal128(38, 2),
3498            vec![
3499                Some(125_i128),
3500                Some(-250_i128),
3501                Some(113_i128),
3502                Some(-113_i128),
3503                Some(0_i128),
3504                None
3505            ]
3506        );
3507        generate_cast_test_case!(
3508            &array,
3509            Decimal256Array,
3510            &DataType::Decimal256(76, 2),
3511            vec![
3512                Some(i256::from_i128(125_i128)),
3513                Some(i256::from_i128(-250_i128)),
3514                Some(i256::from_i128(113_i128)),
3515                Some(i256::from_i128(-113_i128)),
3516                Some(i256::from_i128(0_i128)),
3517                None
3518            ]
3519        );
3520
3521        let array = Float16Array::from(vec![
3522            Some(f16::from_f32(1250.0)),
3523            Some(f16::from_f32(-1250.0)),
3524            Some(f16::from_f32(1249.0)),
3525            None,
3526        ]);
3527        generate_cast_test_case!(
3528            &array,
3529            Decimal128Array,
3530            &DataType::Decimal128(5, -2),
3531            vec![Some(13_i128), Some(-13_i128), Some(12_i128), None]
3532        );
3533    }
3534
3535    #[test]
3536    fn test_cast_decimal128_to_decimal128_overflow() {
3537        let input_type = DataType::Decimal128(38, 3);
3538        let output_type = DataType::Decimal128(38, 38);
3539        assert!(can_cast_types(&input_type, &output_type));
3540
3541        let array = vec![Some(i128::MAX)];
3542        let array = create_decimal128_array(array, 38, 3).unwrap();
3543        let result = cast_with_options(
3544            &array,
3545            &output_type,
3546            &CastOptions {
3547                safe: false,
3548                format_options: FormatOptions::default(),
3549            },
3550        );
3551        assert_eq!(
3552            "Cast error: Cannot cast to Decimal128(38, 38). Overflowing on 170141183460469231731687303715884105727",
3553            result.unwrap_err().to_string()
3554        );
3555    }
3556
3557    #[test]
3558    fn test_cast_decimal128_to_decimal256_overflow() {
3559        let input_type = DataType::Decimal128(38, 3);
3560        let output_type = DataType::Decimal256(76, 76);
3561        assert!(can_cast_types(&input_type, &output_type));
3562
3563        let array = vec![Some(i128::MAX)];
3564        let array = create_decimal128_array(array, 38, 3).unwrap();
3565        let result = cast_with_options(
3566            &array,
3567            &output_type,
3568            &CastOptions {
3569                safe: false,
3570                format_options: FormatOptions::default(),
3571            },
3572        );
3573        assert_eq!(
3574            "Cast error: Cannot cast to Decimal256(76, 76). Overflowing on 170141183460469231731687303715884105727",
3575            result.unwrap_err().to_string()
3576        );
3577    }
3578
3579    #[test]
3580    fn test_cast_decimal32_to_decimal256() {
3581        let input_type = DataType::Decimal32(8, 3);
3582        let output_type = DataType::Decimal256(20, 4);
3583        assert!(can_cast_types(&input_type, &output_type));
3584        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3585        let array = create_decimal32_array(array, 8, 3).unwrap();
3586        generate_cast_test_case!(
3587            &array,
3588            Decimal256Array,
3589            &output_type,
3590            vec![
3591                Some(i256::from_i128(11234560_i128)),
3592                Some(i256::from_i128(21234560_i128)),
3593                Some(i256::from_i128(31234560_i128)),
3594                None
3595            ]
3596        );
3597    }
3598    #[test]
3599    fn test_cast_decimal64_to_decimal256() {
3600        let input_type = DataType::Decimal64(12, 3);
3601        let output_type = DataType::Decimal256(20, 4);
3602        assert!(can_cast_types(&input_type, &output_type));
3603        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3604        let array = create_decimal64_array(array, 12, 3).unwrap();
3605        generate_cast_test_case!(
3606            &array,
3607            Decimal256Array,
3608            &output_type,
3609            vec![
3610                Some(i256::from_i128(11234560_i128)),
3611                Some(i256::from_i128(21234560_i128)),
3612                Some(i256::from_i128(31234560_i128)),
3613                None
3614            ]
3615        );
3616    }
3617    #[test]
3618    fn test_cast_decimal128_to_decimal256() {
3619        let input_type = DataType::Decimal128(20, 3);
3620        let output_type = DataType::Decimal256(20, 4);
3621        assert!(can_cast_types(&input_type, &output_type));
3622        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3623        let array = create_decimal128_array(array, 20, 3).unwrap();
3624        generate_cast_test_case!(
3625            &array,
3626            Decimal256Array,
3627            &output_type,
3628            vec![
3629                Some(i256::from_i128(11234560_i128)),
3630                Some(i256::from_i128(21234560_i128)),
3631                Some(i256::from_i128(31234560_i128)),
3632                None
3633            ]
3634        );
3635    }
3636
3637    #[test]
3638    fn test_cast_decimal256_to_decimal128_overflow() {
3639        let input_type = DataType::Decimal256(76, 5);
3640        let output_type = DataType::Decimal128(38, 7);
3641        assert!(can_cast_types(&input_type, &output_type));
3642        let array = vec![Some(i256::from_i128(i128::MAX))];
3643        let array = create_decimal256_array(array, 76, 5).unwrap();
3644        let result = cast_with_options(
3645            &array,
3646            &output_type,
3647            &CastOptions {
3648                safe: false,
3649                format_options: FormatOptions::default(),
3650            },
3651        );
3652        assert_eq!(
3653            "Cast error: Cannot cast to Decimal128(38, 7). Overflowing on 170141183460469231731687303715884105727",
3654            result.unwrap_err().to_string()
3655        );
3656    }
3657
3658    #[test]
3659    fn test_cast_decimal256_to_decimal256_overflow() {
3660        let input_type = DataType::Decimal256(76, 5);
3661        let output_type = DataType::Decimal256(76, 55);
3662        assert!(can_cast_types(&input_type, &output_type));
3663        let array = vec![Some(i256::from_i128(i128::MAX))];
3664        let array = create_decimal256_array(array, 76, 5).unwrap();
3665        let result = cast_with_options(
3666            &array,
3667            &output_type,
3668            &CastOptions {
3669                safe: false,
3670                format_options: FormatOptions::default(),
3671            },
3672        );
3673        assert_eq!(
3674            "Cast error: Cannot cast to Decimal256(76, 55). Overflowing on 170141183460469231731687303715884105727",
3675            result.unwrap_err().to_string()
3676        );
3677    }
3678
3679    #[test]
3680    fn test_cast_decimal256_to_decimal128() {
3681        let input_type = DataType::Decimal256(20, 3);
3682        let output_type = DataType::Decimal128(20, 4);
3683        assert!(can_cast_types(&input_type, &output_type));
3684        let array = vec![
3685            Some(i256::from_i128(1123456)),
3686            Some(i256::from_i128(2123456)),
3687            Some(i256::from_i128(3123456)),
3688            None,
3689        ];
3690        let array = create_decimal256_array(array, 20, 3).unwrap();
3691        generate_cast_test_case!(
3692            &array,
3693            Decimal128Array,
3694            &output_type,
3695            vec![
3696                Some(11234560_i128),
3697                Some(21234560_i128),
3698                Some(31234560_i128),
3699                None
3700            ]
3701        );
3702    }
3703
3704    #[test]
3705    fn test_cast_decimal256_to_decimal256() {
3706        let input_type = DataType::Decimal256(20, 3);
3707        let output_type = DataType::Decimal256(20, 4);
3708        assert!(can_cast_types(&input_type, &output_type));
3709        let array = vec![
3710            Some(i256::from_i128(1123456)),
3711            Some(i256::from_i128(2123456)),
3712            Some(i256::from_i128(3123456)),
3713            None,
3714        ];
3715        let array = create_decimal256_array(array, 20, 3).unwrap();
3716        generate_cast_test_case!(
3717            &array,
3718            Decimal256Array,
3719            &output_type,
3720            vec![
3721                Some(i256::from_i128(11234560_i128)),
3722                Some(i256::from_i128(21234560_i128)),
3723                Some(i256::from_i128(31234560_i128)),
3724                None
3725            ]
3726        );
3727    }
3728
3729    fn generate_decimal_to_numeric_cast_test_case<T>(array: &PrimitiveArray<T>)
3730    where
3731        T: ArrowPrimitiveType + DecimalType,
3732    {
3733        // u8
3734        generate_cast_test_case!(
3735            array,
3736            UInt8Array,
3737            &DataType::UInt8,
3738            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3739        );
3740        // u16
3741        generate_cast_test_case!(
3742            array,
3743            UInt16Array,
3744            &DataType::UInt16,
3745            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3746        );
3747        // u32
3748        generate_cast_test_case!(
3749            array,
3750            UInt32Array,
3751            &DataType::UInt32,
3752            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3753        );
3754        // u64
3755        generate_cast_test_case!(
3756            array,
3757            UInt64Array,
3758            &DataType::UInt64,
3759            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3760        );
3761        // i8
3762        generate_cast_test_case!(
3763            array,
3764            Int8Array,
3765            &DataType::Int8,
3766            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3767        );
3768        // i16
3769        generate_cast_test_case!(
3770            array,
3771            Int16Array,
3772            &DataType::Int16,
3773            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3774        );
3775        // i32
3776        generate_cast_test_case!(
3777            array,
3778            Int32Array,
3779            &DataType::Int32,
3780            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3781        );
3782        // i64
3783        generate_cast_test_case!(
3784            array,
3785            Int64Array,
3786            &DataType::Int64,
3787            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3788        );
3789        // f16
3790        generate_cast_test_case!(
3791            array,
3792            Float16Array,
3793            &DataType::Float16,
3794            vec![
3795                Some(f16::from_f32(1.25)),
3796                Some(f16::from_f32(2.25)),
3797                Some(f16::from_f32(3.25)),
3798                None,
3799                Some(f16::from_f32(5.25))
3800            ]
3801        );
3802        // f32
3803        generate_cast_test_case!(
3804            array,
3805            Float32Array,
3806            &DataType::Float32,
3807            vec![
3808                Some(1.25_f32),
3809                Some(2.25_f32),
3810                Some(3.25_f32),
3811                None,
3812                Some(5.25_f32)
3813            ]
3814        );
3815        // f64
3816        generate_cast_test_case!(
3817            array,
3818            Float64Array,
3819            &DataType::Float64,
3820            vec![
3821                Some(1.25_f64),
3822                Some(2.25_f64),
3823                Some(3.25_f64),
3824                None,
3825                Some(5.25_f64)
3826            ]
3827        );
3828    }
3829
3830    #[test]
3831    #[cfg_attr(miri, ignore)] // Takes too long
3832    fn test_cast_decimal32_to_numeric() {
3833        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3834        let array = create_decimal32_array(value_array, 8, 2).unwrap();
3835
3836        generate_decimal_to_numeric_cast_test_case(&array);
3837    }
3838
3839    #[test]
3840    #[cfg_attr(miri, ignore)] // Takes too long
3841    fn test_cast_decimal64_to_numeric() {
3842        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3843        let array = create_decimal64_array(value_array, 8, 2).unwrap();
3844
3845        generate_decimal_to_numeric_cast_test_case(&array);
3846    }
3847
3848    #[test]
3849    #[cfg_attr(miri, ignore)] // Takes too long
3850    fn test_cast_decimal128_to_numeric() {
3851        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3852        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3853
3854        generate_decimal_to_numeric_cast_test_case(&array);
3855
3856        // overflow test: out of range of max u8
3857        let value_array: Vec<Option<i128>> = vec![Some(51300)];
3858        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3859        let casted_array = cast_with_options(
3860            &array,
3861            &DataType::UInt8,
3862            &CastOptions {
3863                safe: false,
3864                format_options: FormatOptions::default(),
3865            },
3866        );
3867        assert_eq!(
3868            "Cast error: value of 513 is out of range UInt8".to_string(),
3869            casted_array.unwrap_err().to_string()
3870        );
3871
3872        let casted_array = cast_with_options(
3873            &array,
3874            &DataType::UInt8,
3875            &CastOptions {
3876                safe: true,
3877                format_options: FormatOptions::default(),
3878            },
3879        );
3880        assert!(casted_array.is_ok());
3881        assert!(casted_array.unwrap().is_null(0));
3882
3883        // overflow test: out of range of max i8
3884        let value_array: Vec<Option<i128>> = vec![Some(24400)];
3885        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3886        let casted_array = cast_with_options(
3887            &array,
3888            &DataType::Int8,
3889            &CastOptions {
3890                safe: false,
3891                format_options: FormatOptions::default(),
3892            },
3893        );
3894        assert_eq!(
3895            "Cast error: value of 244 is out of range Int8".to_string(),
3896            casted_array.unwrap_err().to_string()
3897        );
3898
3899        let casted_array = cast_with_options(
3900            &array,
3901            &DataType::Int8,
3902            &CastOptions {
3903                safe: true,
3904                format_options: FormatOptions::default(),
3905            },
3906        );
3907        assert!(casted_array.is_ok());
3908        assert!(casted_array.unwrap().is_null(0));
3909
3910        // loss the precision: convert decimal to f32、f64
3911        // f32
3912        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3913        let value_array: Vec<Option<i128>> = vec![
3914            Some(125),
3915            Some(225),
3916            Some(325),
3917            None,
3918            Some(525),
3919            Some(112345678),
3920            Some(112345679),
3921        ];
3922        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3923        generate_cast_test_case!(
3924            &array,
3925            Float32Array,
3926            &DataType::Float32,
3927            vec![
3928                Some(1.25_f32),
3929                Some(2.25_f32),
3930                Some(3.25_f32),
3931                None,
3932                Some(5.25_f32),
3933                Some(1_123_456.7_f32),
3934                Some(1_123_456.7_f32)
3935            ]
3936        );
3937
3938        // f64
3939        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3940        let value_array: Vec<Option<i128>> = vec![
3941            Some(125),
3942            Some(225),
3943            Some(325),
3944            None,
3945            Some(525),
3946            Some(112345678901234568),
3947            Some(112345678901234560),
3948        ];
3949        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3950        generate_cast_test_case!(
3951            &array,
3952            Float64Array,
3953            &DataType::Float64,
3954            vec![
3955                Some(1.25_f64),
3956                Some(2.25_f64),
3957                Some(3.25_f64),
3958                None,
3959                Some(5.25_f64),
3960                Some(1_123_456_789_012_345.6_f64),
3961                Some(1_123_456_789_012_345.6_f64),
3962            ]
3963        );
3964    }
3965
3966    #[test]
3967    #[cfg_attr(miri, ignore)] // Takes too long
3968    fn test_cast_decimal256_to_numeric() {
3969        let value_array: Vec<Option<i256>> = vec![
3970            Some(i256::from_i128(125)),
3971            Some(i256::from_i128(225)),
3972            Some(i256::from_i128(325)),
3973            None,
3974            Some(i256::from_i128(525)),
3975        ];
3976        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3977        // u8
3978        generate_cast_test_case!(
3979            &array,
3980            UInt8Array,
3981            &DataType::UInt8,
3982            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3983        );
3984        // u16
3985        generate_cast_test_case!(
3986            &array,
3987            UInt16Array,
3988            &DataType::UInt16,
3989            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3990        );
3991        // u32
3992        generate_cast_test_case!(
3993            &array,
3994            UInt32Array,
3995            &DataType::UInt32,
3996            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3997        );
3998        // u64
3999        generate_cast_test_case!(
4000            &array,
4001            UInt64Array,
4002            &DataType::UInt64,
4003            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
4004        );
4005        // i8
4006        generate_cast_test_case!(
4007            &array,
4008            Int8Array,
4009            &DataType::Int8,
4010            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
4011        );
4012        // i16
4013        generate_cast_test_case!(
4014            &array,
4015            Int16Array,
4016            &DataType::Int16,
4017            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
4018        );
4019        // i32
4020        generate_cast_test_case!(
4021            &array,
4022            Int32Array,
4023            &DataType::Int32,
4024            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
4025        );
4026        // i64
4027        generate_cast_test_case!(
4028            &array,
4029            Int64Array,
4030            &DataType::Int64,
4031            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
4032        );
4033        // f16
4034        generate_cast_test_case!(
4035            &array,
4036            Float16Array,
4037            &DataType::Float16,
4038            vec![
4039                Some(f16::from_f32(1.25)),
4040                Some(f16::from_f32(2.25)),
4041                Some(f16::from_f32(3.25)),
4042                None,
4043                Some(f16::from_f32(5.25))
4044            ]
4045        );
4046        // f32
4047        generate_cast_test_case!(
4048            &array,
4049            Float32Array,
4050            &DataType::Float32,
4051            vec![
4052                Some(1.25_f32),
4053                Some(2.25_f32),
4054                Some(3.25_f32),
4055                None,
4056                Some(5.25_f32)
4057            ]
4058        );
4059        // f64
4060        generate_cast_test_case!(
4061            &array,
4062            Float64Array,
4063            &DataType::Float64,
4064            vec![
4065                Some(1.25_f64),
4066                Some(2.25_f64),
4067                Some(3.25_f64),
4068                None,
4069                Some(5.25_f64)
4070            ]
4071        );
4072
4073        // overflow test: out of range of max i8
4074        let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128(24400))];
4075        let array = create_decimal256_array(value_array, 38, 2).unwrap();
4076        let casted_array = cast_with_options(
4077            &array,
4078            &DataType::Int8,
4079            &CastOptions {
4080                safe: false,
4081                format_options: FormatOptions::default(),
4082            },
4083        );
4084        assert_eq!(
4085            "Cast error: value of 244 is out of range Int8".to_string(),
4086            casted_array.unwrap_err().to_string()
4087        );
4088
4089        let casted_array = cast_with_options(
4090            &array,
4091            &DataType::Int8,
4092            &CastOptions {
4093                safe: true,
4094                format_options: FormatOptions::default(),
4095            },
4096        );
4097        assert!(casted_array.is_ok());
4098        assert!(casted_array.unwrap().is_null(0));
4099
4100        // overflow test: values whose low 64 bits fit the target type
4101        // https://github.com/apache/arrow-rs/issues/10855
4102        let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128((1i128 << 64) + 5))];
4103        let array = create_decimal256_array(value_array, 76, 0).unwrap();
4104        let casted_array = cast_with_options(
4105            &array,
4106            &DataType::Int64,
4107            &CastOptions {
4108                safe: false,
4109                format_options: FormatOptions::default(),
4110            },
4111        );
4112        assert_eq!(
4113            "Cast error: value of 18446744073709551621 is out of range Int64".to_string(),
4114            casted_array.unwrap_err().to_string()
4115        );
4116
4117        let casted_array = cast_with_options(
4118            &array,
4119            &DataType::Int64,
4120            &CastOptions {
4121                safe: true,
4122                format_options: FormatOptions::default(),
4123            },
4124        );
4125        assert!(casted_array.is_ok());
4126        assert!(casted_array.unwrap().is_null(0));
4127
4128        // loss the precision: convert decimal to f32、f64
4129        // f32
4130        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
4131        let value_array: Vec<Option<i256>> = vec![
4132            Some(i256::from_i128(125)),
4133            Some(i256::from_i128(225)),
4134            Some(i256::from_i128(325)),
4135            None,
4136            Some(i256::from_i128(525)),
4137            Some(i256::from_i128(112345678)),
4138            Some(i256::from_i128(112345679)),
4139        ];
4140        let array = create_decimal256_array(value_array, 76, 2).unwrap();
4141        generate_cast_test_case!(
4142            &array,
4143            Float32Array,
4144            &DataType::Float32,
4145            vec![
4146                Some(1.25_f32),
4147                Some(2.25_f32),
4148                Some(3.25_f32),
4149                None,
4150                Some(5.25_f32),
4151                Some(1_123_456.7_f32),
4152                Some(1_123_456.7_f32)
4153            ]
4154        );
4155
4156        // f64
4157        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
4158        let value_array: Vec<Option<i256>> = vec![
4159            Some(i256::from_i128(125)),
4160            Some(i256::from_i128(225)),
4161            Some(i256::from_i128(325)),
4162            None,
4163            Some(i256::from_i128(525)),
4164            Some(i256::from_i128(112345678901234568)),
4165            Some(i256::from_i128(112345678901234560)),
4166        ];
4167        let array = create_decimal256_array(value_array, 76, 2).unwrap();
4168        generate_cast_test_case!(
4169            &array,
4170            Float64Array,
4171            &DataType::Float64,
4172            vec![
4173                Some(1.25_f64),
4174                Some(2.25_f64),
4175                Some(3.25_f64),
4176                None,
4177                Some(5.25_f64),
4178                Some(1_123_456_789_012_345.6_f64),
4179                Some(1_123_456_789_012_345.6_f64),
4180            ]
4181        );
4182    }
4183
4184    #[test]
4185    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
4186    fn test_cast_decimal128_to_float16_overflow() {
4187        let array = create_decimal128_array(
4188            vec![
4189                Some(6_550_400_i128),
4190                Some(100_000_000_i128),
4191                Some(-100_000_000_i128),
4192                None,
4193            ],
4194            10,
4195            2,
4196        )
4197        .unwrap();
4198
4199        generate_cast_test_case!(
4200            &array,
4201            Float16Array,
4202            &DataType::Float16,
4203            vec![
4204                Some(f16::from_f64(65504.0)),
4205                Some(f16::INFINITY),
4206                Some(f16::NEG_INFINITY),
4207                None
4208            ]
4209        );
4210    }
4211
4212    #[test]
4213    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
4214    fn test_cast_decimal256_to_float16_overflow() {
4215        let array = create_decimal256_array(
4216            vec![
4217                Some(i256::from_i128(6_550_400_i128)),
4218                Some(i256::from_i128(100_000_000_i128)),
4219                Some(i256::from_i128(-100_000_000_i128)),
4220                None,
4221            ],
4222            10,
4223            2,
4224        )
4225        .unwrap();
4226
4227        generate_cast_test_case!(
4228            &array,
4229            Float16Array,
4230            &DataType::Float16,
4231            vec![
4232                Some(f16::from_f64(65504.0)),
4233                Some(f16::INFINITY),
4234                Some(f16::NEG_INFINITY),
4235                None
4236            ]
4237        );
4238    }
4239
4240    #[test]
4241    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
4242    fn test_cast_decimal_to_numeric_negative_scale() {
4243        let value_array: Vec<Option<i256>> = vec![
4244            Some(i256::from_i128(125)),
4245            Some(i256::from_i128(225)),
4246            Some(i256::from_i128(325)),
4247            None,
4248            Some(i256::from_i128(525)),
4249        ];
4250        let array = create_decimal256_array(value_array, 38, -1).unwrap();
4251
4252        generate_cast_test_case!(
4253            &array,
4254            Int64Array,
4255            &DataType::Int64,
4256            vec![Some(1_250), Some(2_250), Some(3_250), None, Some(5_250)]
4257        );
4258
4259        let value_array: Vec<Option<i128>> = vec![Some(12), Some(-12), None];
4260        let array = create_decimal128_array(value_array, 10, -2).unwrap();
4261        generate_cast_test_case!(
4262            &array,
4263            Float16Array,
4264            &DataType::Float16,
4265            vec![
4266                Some(f16::from_f32(1200.0)),
4267                Some(f16::from_f32(-1200.0)),
4268                None
4269            ]
4270        );
4271
4272        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4273        let array = create_decimal32_array(value_array, 8, -2).unwrap();
4274        generate_cast_test_case!(
4275            &array,
4276            Int64Array,
4277            &DataType::Int64,
4278            vec![Some(12_500), Some(22_500), Some(32_500), None, Some(52_500)]
4279        );
4280
4281        let value_array: Vec<Option<i32>> = vec![Some(2), Some(1), None];
4282        let array = create_decimal32_array(value_array, 9, -9).unwrap();
4283        generate_cast_test_case!(
4284            &array,
4285            Int64Array,
4286            &DataType::Int64,
4287            vec![Some(2_000_000_000), Some(1_000_000_000), None]
4288        );
4289
4290        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4291        let array = create_decimal64_array(value_array, 18, -3).unwrap();
4292        generate_cast_test_case!(
4293            &array,
4294            Int64Array,
4295            &DataType::Int64,
4296            vec![
4297                Some(125_000),
4298                Some(225_000),
4299                Some(325_000),
4300                None,
4301                Some(525_000)
4302            ]
4303        );
4304
4305        let value_array: Vec<Option<i64>> = vec![Some(12), Some(34), None];
4306        let array = create_decimal64_array(value_array, 18, -10).unwrap();
4307        generate_cast_test_case!(
4308            &array,
4309            Int64Array,
4310            &DataType::Int64,
4311            vec![Some(120_000_000_000), Some(340_000_000_000), None]
4312        );
4313
4314        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4315        let array = create_decimal128_array(value_array, 38, -4).unwrap();
4316        generate_cast_test_case!(
4317            &array,
4318            Int64Array,
4319            &DataType::Int64,
4320            vec![
4321                Some(1_250_000),
4322                Some(2_250_000),
4323                Some(3_250_000),
4324                None,
4325                Some(5_250_000)
4326            ]
4327        );
4328
4329        let value_array: Vec<Option<i128>> = vec![Some(9), Some(1), None];
4330        let array = create_decimal128_array(value_array, 38, -18).unwrap();
4331        generate_cast_test_case!(
4332            &array,
4333            Int64Array,
4334            &DataType::Int64,
4335            vec![
4336                Some(9_000_000_000_000_000_000),
4337                Some(1_000_000_000_000_000_000),
4338                None
4339            ]
4340        );
4341
4342        let array = create_decimal32_array(vec![Some(999_999_999)], 9, -1).unwrap();
4343        let casted_array = cast_with_options(
4344            &array,
4345            &DataType::Int64,
4346            &CastOptions {
4347                safe: false,
4348                format_options: FormatOptions::default(),
4349            },
4350        );
4351        assert_eq!(
4352            "Arithmetic overflow: Overflow happened on: 999999999 * 10".to_string(),
4353            casted_array.unwrap_err().to_string()
4354        );
4355
4356        let casted_array = cast_with_options(
4357            &array,
4358            &DataType::Int64,
4359            &CastOptions {
4360                safe: true,
4361                format_options: FormatOptions::default(),
4362            },
4363        );
4364        assert!(casted_array.is_ok());
4365        assert!(casted_array.unwrap().is_null(0));
4366
4367        let array = create_decimal64_array(vec![Some(13)], 18, -1).unwrap();
4368        let casted_array = cast_with_options(
4369            &array,
4370            &DataType::Int8,
4371            &CastOptions {
4372                safe: false,
4373                format_options: FormatOptions::default(),
4374            },
4375        );
4376        assert_eq!(
4377            "Cast error: value of 130 is out of range Int8".to_string(),
4378            casted_array.unwrap_err().to_string()
4379        );
4380
4381        let casted_array = cast_with_options(
4382            &array,
4383            &DataType::Int8,
4384            &CastOptions {
4385                safe: true,
4386                format_options: FormatOptions::default(),
4387            },
4388        );
4389        assert!(casted_array.is_ok());
4390        assert!(casted_array.unwrap().is_null(0));
4391    }
4392
4393    #[test]
4394    fn test_cast_numeric_to_decimal128() {
4395        let decimal_type = DataType::Decimal128(38, 6);
4396        // u8, u16, u32, u64
4397        let input_datas = vec![
4398            Arc::new(UInt8Array::from(vec![
4399                Some(1),
4400                Some(2),
4401                Some(3),
4402                None,
4403                Some(5),
4404            ])) as ArrayRef, // u8
4405            Arc::new(UInt16Array::from(vec![
4406                Some(1),
4407                Some(2),
4408                Some(3),
4409                None,
4410                Some(5),
4411            ])) as ArrayRef, // u16
4412            Arc::new(UInt32Array::from(vec![
4413                Some(1),
4414                Some(2),
4415                Some(3),
4416                None,
4417                Some(5),
4418            ])) as ArrayRef, // u32
4419            Arc::new(UInt64Array::from(vec![
4420                Some(1),
4421                Some(2),
4422                Some(3),
4423                None,
4424                Some(5),
4425            ])) as ArrayRef, // u64
4426        ];
4427
4428        for array in input_datas {
4429            generate_cast_test_case!(
4430                &array,
4431                Decimal128Array,
4432                &decimal_type,
4433                vec![
4434                    Some(1000000_i128),
4435                    Some(2000000_i128),
4436                    Some(3000000_i128),
4437                    None,
4438                    Some(5000000_i128)
4439                ]
4440            );
4441        }
4442
4443        // i8, i16, i32, i64
4444        let input_datas = vec![
4445            Arc::new(Int8Array::from(vec![
4446                Some(1),
4447                Some(2),
4448                Some(3),
4449                None,
4450                Some(5),
4451            ])) as ArrayRef, // i8
4452            Arc::new(Int16Array::from(vec![
4453                Some(1),
4454                Some(2),
4455                Some(3),
4456                None,
4457                Some(5),
4458            ])) as ArrayRef, // i16
4459            Arc::new(Int32Array::from(vec![
4460                Some(1),
4461                Some(2),
4462                Some(3),
4463                None,
4464                Some(5),
4465            ])) as ArrayRef, // i32
4466            Arc::new(Int64Array::from(vec![
4467                Some(1),
4468                Some(2),
4469                Some(3),
4470                None,
4471                Some(5),
4472            ])) as ArrayRef, // i64
4473        ];
4474        for array in input_datas {
4475            generate_cast_test_case!(
4476                &array,
4477                Decimal128Array,
4478                &decimal_type,
4479                vec![
4480                    Some(1000000_i128),
4481                    Some(2000000_i128),
4482                    Some(3000000_i128),
4483                    None,
4484                    Some(5000000_i128)
4485                ]
4486            );
4487        }
4488
4489        // test u8 to decimal type with overflow the result type
4490        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4491        let array = UInt8Array::from(vec![1, 2, 3, 4, 100]);
4492        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4493        assert!(casted_array.is_ok());
4494        let array = casted_array.unwrap();
4495        let array: &Decimal128Array = array.as_primitive();
4496        assert!(array.is_null(4));
4497
4498        // test i8 to decimal type with overflow the result type
4499        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4500        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4501        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4502        assert!(casted_array.is_ok());
4503        let array = casted_array.unwrap();
4504        let array: &Decimal128Array = array.as_primitive();
4505        assert!(array.is_null(4));
4506
4507        // test f32 to decimal type
4508        let array = Float32Array::from(vec![
4509            Some(1.1),
4510            Some(2.2),
4511            Some(4.4),
4512            None,
4513            Some(1.123_456_4), // round down
4514            Some(1.123_456_7), // round up
4515        ]);
4516        let array = Arc::new(array) as ArrayRef;
4517        generate_cast_test_case!(
4518            &array,
4519            Decimal128Array,
4520            &decimal_type,
4521            vec![
4522                Some(1100000_i128),
4523                Some(2200000_i128),
4524                Some(4400000_i128),
4525                None,
4526                Some(1123456_i128), // round down
4527                Some(1123457_i128), // round up
4528            ]
4529        );
4530
4531        // test f64 to decimal type
4532        let array = Float64Array::from(vec![
4533            Some(1.1),
4534            Some(2.2),
4535            Some(4.4),
4536            None,
4537            Some(1.123_456_489_123_4),     // round up
4538            Some(1.123_456_789_123_4),     // round up
4539            Some(1.123_456_489_012_345_6), // round down
4540            Some(1.123_456_789_012_345_6), // round up
4541        ]);
4542        generate_cast_test_case!(
4543            &array,
4544            Decimal128Array,
4545            &decimal_type,
4546            vec![
4547                Some(1100000_i128),
4548                Some(2200000_i128),
4549                Some(4400000_i128),
4550                None,
4551                Some(1123456_i128), // round down
4552                Some(1123457_i128), // round up
4553                Some(1123456_i128), // round down
4554                Some(1123457_i128), // round up
4555            ]
4556        );
4557    }
4558
4559    #[test]
4560    fn test_cast_numeric_to_decimal256() {
4561        let decimal_type = DataType::Decimal256(76, 6);
4562        // u8, u16, u32, u64
4563        let input_datas = vec![
4564            Arc::new(UInt8Array::from(vec![
4565                Some(1),
4566                Some(2),
4567                Some(3),
4568                None,
4569                Some(5),
4570            ])) as ArrayRef, // u8
4571            Arc::new(UInt16Array::from(vec![
4572                Some(1),
4573                Some(2),
4574                Some(3),
4575                None,
4576                Some(5),
4577            ])) as ArrayRef, // u16
4578            Arc::new(UInt32Array::from(vec![
4579                Some(1),
4580                Some(2),
4581                Some(3),
4582                None,
4583                Some(5),
4584            ])) as ArrayRef, // u32
4585            Arc::new(UInt64Array::from(vec![
4586                Some(1),
4587                Some(2),
4588                Some(3),
4589                None,
4590                Some(5),
4591            ])) as ArrayRef, // u64
4592        ];
4593
4594        for array in input_datas {
4595            generate_cast_test_case!(
4596                &array,
4597                Decimal256Array,
4598                &decimal_type,
4599                vec![
4600                    Some(i256::from_i128(1000000_i128)),
4601                    Some(i256::from_i128(2000000_i128)),
4602                    Some(i256::from_i128(3000000_i128)),
4603                    None,
4604                    Some(i256::from_i128(5000000_i128))
4605                ]
4606            );
4607        }
4608
4609        // i8, i16, i32, i64
4610        let input_datas = vec![
4611            Arc::new(Int8Array::from(vec![
4612                Some(1),
4613                Some(2),
4614                Some(3),
4615                None,
4616                Some(5),
4617            ])) as ArrayRef, // i8
4618            Arc::new(Int16Array::from(vec![
4619                Some(1),
4620                Some(2),
4621                Some(3),
4622                None,
4623                Some(5),
4624            ])) as ArrayRef, // i16
4625            Arc::new(Int32Array::from(vec![
4626                Some(1),
4627                Some(2),
4628                Some(3),
4629                None,
4630                Some(5),
4631            ])) as ArrayRef, // i32
4632            Arc::new(Int64Array::from(vec![
4633                Some(1),
4634                Some(2),
4635                Some(3),
4636                None,
4637                Some(5),
4638            ])) as ArrayRef, // i64
4639        ];
4640        for array in input_datas {
4641            generate_cast_test_case!(
4642                &array,
4643                Decimal256Array,
4644                &decimal_type,
4645                vec![
4646                    Some(i256::from_i128(1000000_i128)),
4647                    Some(i256::from_i128(2000000_i128)),
4648                    Some(i256::from_i128(3000000_i128)),
4649                    None,
4650                    Some(i256::from_i128(5000000_i128))
4651                ]
4652            );
4653        }
4654
4655        // test i8 to decimal type with overflow the result type
4656        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4657        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4658        let array = Arc::new(array) as ArrayRef;
4659        let casted_array = cast(&array, &DataType::Decimal256(3, 1));
4660        assert!(casted_array.is_ok());
4661        let array = casted_array.unwrap();
4662        let array: &Decimal256Array = array.as_primitive();
4663        assert!(array.is_null(4));
4664
4665        // test f32 to decimal type
4666        let array = Float32Array::from(vec![
4667            Some(1.1),
4668            Some(2.2),
4669            Some(4.4),
4670            None,
4671            Some(1.123_456_4), // round down
4672            Some(1.123_456_7), // round up
4673        ]);
4674        generate_cast_test_case!(
4675            &array,
4676            Decimal256Array,
4677            &decimal_type,
4678            vec![
4679                Some(i256::from_i128(1100000_i128)),
4680                Some(i256::from_i128(2200000_i128)),
4681                Some(i256::from_i128(4400000_i128)),
4682                None,
4683                Some(i256::from_i128(1123456_i128)), // round down
4684                Some(i256::from_i128(1123457_i128)), // round up
4685            ]
4686        );
4687
4688        // test f64 to decimal type
4689        let array = Float64Array::from(vec![
4690            Some(1.1),
4691            Some(2.2),
4692            Some(4.4),
4693            None,
4694            Some(1.123_456_489_123_4),     // round down
4695            Some(1.123_456_789_123_4),     // round up
4696            Some(1.123_456_489_012_345_6), // round down
4697            Some(1.123_456_789_012_345_6), // round up
4698        ]);
4699        generate_cast_test_case!(
4700            &array,
4701            Decimal256Array,
4702            &decimal_type,
4703            vec![
4704                Some(i256::from_i128(1100000_i128)),
4705                Some(i256::from_i128(2200000_i128)),
4706                Some(i256::from_i128(4400000_i128)),
4707                None,
4708                Some(i256::from_i128(1123456_i128)), // round down
4709                Some(i256::from_i128(1123457_i128)), // round up
4710                Some(i256::from_i128(1123456_i128)), // round down
4711                Some(i256::from_i128(1123457_i128)), // round up
4712            ]
4713        );
4714    }
4715
4716    #[test]
4717    fn test_cast_i32_to_f64() {
4718        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4719        let b = cast(&array, &DataType::Float64).unwrap();
4720        let c = b.as_primitive::<Float64Type>();
4721        assert_eq!(5.0, c.value(0));
4722        assert_eq!(6.0, c.value(1));
4723        assert_eq!(7.0, c.value(2));
4724        assert_eq!(8.0, c.value(3));
4725        assert_eq!(9.0, c.value(4));
4726    }
4727
4728    #[test]
4729    fn test_cast_i32_to_u8() {
4730        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4731        let b = cast(&array, &DataType::UInt8).unwrap();
4732        let c = b.as_primitive::<UInt8Type>();
4733        assert!(!c.is_valid(0));
4734        assert_eq!(6, c.value(1));
4735        assert!(!c.is_valid(2));
4736        assert_eq!(8, c.value(3));
4737        // overflows return None
4738        assert!(!c.is_valid(4));
4739    }
4740
4741    #[test]
4742    #[should_panic(expected = "Can't cast value -5 to type UInt8")]
4743    fn test_cast_int32_to_u8_with_error() {
4744        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4745        // overflow with the error
4746        let cast_option = CastOptions {
4747            safe: false,
4748            format_options: FormatOptions::default(),
4749        };
4750        let result = cast_with_options(&array, &DataType::UInt8, &cast_option);
4751        assert!(result.is_err());
4752        result.unwrap();
4753    }
4754
4755    #[test]
4756    fn test_cast_i32_to_u8_sliced() {
4757        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4758        assert_eq!(0, array.offset());
4759        let array = array.slice(2, 3);
4760        let b = cast(&array, &DataType::UInt8).unwrap();
4761        assert_eq!(3, b.len());
4762        let c = b.as_primitive::<UInt8Type>();
4763        assert!(!c.is_valid(0));
4764        assert_eq!(8, c.value(1));
4765        // overflows return None
4766        assert!(!c.is_valid(2));
4767    }
4768
4769    #[test]
4770    fn test_cast_i32_to_i32() {
4771        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4772        let b = cast(&array, &DataType::Int32).unwrap();
4773        let c = b.as_primitive::<Int32Type>();
4774        assert_eq!(5, c.value(0));
4775        assert_eq!(6, c.value(1));
4776        assert_eq!(7, c.value(2));
4777        assert_eq!(8, c.value(3));
4778        assert_eq!(9, c.value(4));
4779    }
4780
4781    #[test]
4782    fn test_cast_i32_to_list_i32() {
4783        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4784        let b = cast(
4785            &array,
4786            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4787        )
4788        .unwrap();
4789        assert_eq!(5, b.len());
4790        let arr = b.as_list::<i32>();
4791        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4792        assert_eq!(1, arr.value_length(0));
4793        assert_eq!(1, arr.value_length(1));
4794        assert_eq!(1, arr.value_length(2));
4795        assert_eq!(1, arr.value_length(3));
4796        assert_eq!(1, arr.value_length(4));
4797        let c = arr.values().as_primitive::<Int32Type>();
4798        assert_eq!(5, c.value(0));
4799        assert_eq!(6, c.value(1));
4800        assert_eq!(7, c.value(2));
4801        assert_eq!(8, c.value(3));
4802        assert_eq!(9, c.value(4));
4803    }
4804
4805    #[test]
4806    fn test_cast_i32_to_list_i32_nullable() {
4807        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), Some(9)]);
4808        let b = cast(
4809            &array,
4810            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4811        )
4812        .unwrap();
4813        assert_eq!(5, b.len());
4814        assert_eq!(0, b.null_count());
4815        let arr = b.as_list::<i32>();
4816        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4817        assert_eq!(1, arr.value_length(0));
4818        assert_eq!(1, arr.value_length(1));
4819        assert_eq!(1, arr.value_length(2));
4820        assert_eq!(1, arr.value_length(3));
4821        assert_eq!(1, arr.value_length(4));
4822
4823        let c = arr.values().as_primitive::<Int32Type>();
4824        assert_eq!(1, c.null_count());
4825        assert_eq!(5, c.value(0));
4826        assert!(!c.is_valid(1));
4827        assert_eq!(7, c.value(2));
4828        assert_eq!(8, c.value(3));
4829        assert_eq!(9, c.value(4));
4830    }
4831
4832    #[test]
4833    fn test_cast_i32_to_list_f64_nullable_sliced() {
4834        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), None, Some(10)]);
4835        let array = array.slice(2, 4);
4836        let b = cast(
4837            &array,
4838            &DataType::List(Arc::new(Field::new_list_field(DataType::Float64, true))),
4839        )
4840        .unwrap();
4841        assert_eq!(4, b.len());
4842        assert_eq!(0, b.null_count());
4843        let arr = b.as_list::<i32>();
4844        assert_eq!(&[0, 1, 2, 3, 4], arr.value_offsets());
4845        assert_eq!(1, arr.value_length(0));
4846        assert_eq!(1, arr.value_length(1));
4847        assert_eq!(1, arr.value_length(2));
4848        assert_eq!(1, arr.value_length(3));
4849        let c = arr.values().as_primitive::<Float64Type>();
4850        assert_eq!(1, c.null_count());
4851        assert_eq!(7.0, c.value(0));
4852        assert_eq!(8.0, c.value(1));
4853        assert!(!c.is_valid(2));
4854        assert_eq!(10.0, c.value(3));
4855    }
4856
4857    #[test]
4858    fn test_cast_int_to_utf8view() {
4859        let inputs = vec![
4860            Arc::new(Int8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4861            Arc::new(Int16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4862            Arc::new(Int32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4863            Arc::new(Int64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4864            Arc::new(UInt8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4865            Arc::new(UInt16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4866            Arc::new(UInt32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4867            Arc::new(UInt64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4868        ];
4869        let expected: ArrayRef = Arc::new(StringViewArray::from(vec![
4870            None,
4871            Some("8"),
4872            Some("9"),
4873            Some("10"),
4874        ]));
4875
4876        for array in inputs {
4877            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4878            let arr = cast(&array, &DataType::Utf8View).unwrap();
4879            assert_eq!(expected.as_ref(), arr.as_ref());
4880        }
4881    }
4882
4883    #[test]
4884    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
4885    fn test_cast_float_to_utf8view() {
4886        let inputs = vec![
4887            Arc::new(Float16Array::from(vec![
4888                Some(f16::from_f64(1.5)),
4889                Some(f16::from_f64(2.5)),
4890                None,
4891            ])) as ArrayRef,
4892            Arc::new(Float32Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4893            Arc::new(Float64Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4894        ];
4895
4896        let expected: ArrayRef =
4897            Arc::new(StringViewArray::from(vec![Some("1.5"), Some("2.5"), None]));
4898
4899        for array in inputs {
4900            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4901            let arr = cast(&array, &DataType::Utf8View).unwrap();
4902            assert_eq!(expected.as_ref(), arr.as_ref());
4903        }
4904    }
4905
4906    #[test]
4907    fn test_cast_utf8_to_i32() {
4908        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4909        let b = cast(&array, &DataType::Int32).unwrap();
4910        let c = b.as_primitive::<Int32Type>();
4911        assert_eq!(5, c.value(0));
4912        assert_eq!(6, c.value(1));
4913        assert!(!c.is_valid(2));
4914        assert_eq!(8, c.value(3));
4915        assert!(!c.is_valid(4));
4916    }
4917
4918    #[test]
4919    fn test_cast_utf8view_to_i32() {
4920        let array = StringViewArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4921        let b = cast(&array, &DataType::Int32).unwrap();
4922        let c = b.as_primitive::<Int32Type>();
4923        assert_eq!(5, c.value(0));
4924        assert_eq!(6, c.value(1));
4925        assert!(!c.is_valid(2));
4926        assert_eq!(8, c.value(3));
4927        assert!(!c.is_valid(4));
4928    }
4929
4930    #[test]
4931    fn test_cast_utf8view_to_f32() {
4932        let array = StringViewArray::from(vec!["3", "4.56", "seven", "8.9"]);
4933        let b = cast(&array, &DataType::Float32).unwrap();
4934        let c = b.as_primitive::<Float32Type>();
4935        assert_eq!(3.0, c.value(0));
4936        assert_eq!(4.56, c.value(1));
4937        assert!(!c.is_valid(2));
4938        assert_eq!(8.9, c.value(3));
4939    }
4940
4941    #[test]
4942    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
4943    fn test_cast_string_to_f16() {
4944        let arrays = [
4945            Arc::new(StringViewArray::from(vec!["3", "4.56", "seven", "8.9"])) as ArrayRef,
4946            Arc::new(StringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4947            Arc::new(LargeStringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4948        ];
4949        for array in arrays {
4950            let b = cast(&array, &DataType::Float16).unwrap();
4951            let c = b.as_primitive::<Float16Type>();
4952            assert_eq!(half::f16::from_f32(3.0), c.value(0));
4953            assert_eq!(half::f16::from_f32(4.56), c.value(1));
4954            assert!(!c.is_valid(2));
4955            assert_eq!(half::f16::from_f32(8.9), c.value(3));
4956        }
4957    }
4958
4959    #[test]
4960    fn test_cast_utf8view_to_decimal128() {
4961        let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
4962        let arr = Arc::new(array) as ArrayRef;
4963        generate_cast_test_case!(
4964            &arr,
4965            Decimal128Array,
4966            &DataType::Decimal128(4, 2),
4967            vec![None, Some(400_i128), Some(560_i128), Some(789_i128)]
4968        );
4969    }
4970
4971    #[test]
4972    fn test_cast_with_options_utf8_to_i32() {
4973        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4974        let result = cast_with_options(
4975            &array,
4976            &DataType::Int32,
4977            &CastOptions {
4978                safe: false,
4979                format_options: FormatOptions::default(),
4980            },
4981        );
4982        match result {
4983            Ok(_) => panic!("expected error"),
4984            Err(e) => {
4985                assert!(
4986                    e.to_string()
4987                        .contains("Cast error: Cannot cast string 'seven' to value of Int32 type",),
4988                    "Error: {e}"
4989                )
4990            }
4991        }
4992    }
4993
4994    #[test]
4995    fn test_cast_utf8_to_bool() {
4996        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4997        let casted = cast(&strings, &DataType::Boolean).unwrap();
4998        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4999        assert_eq!(*as_boolean_array(&casted), expected);
5000    }
5001
5002    #[test]
5003    fn test_cast_utf8view_to_bool() {
5004        let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
5005        let casted = cast(&strings, &DataType::Boolean).unwrap();
5006        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
5007        assert_eq!(*as_boolean_array(&casted), expected);
5008    }
5009
5010    #[test]
5011    fn test_cast_with_options_utf8_to_bool() {
5012        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
5013        let casted = cast_with_options(
5014            &strings,
5015            &DataType::Boolean,
5016            &CastOptions {
5017                safe: false,
5018                format_options: FormatOptions::default(),
5019            },
5020        );
5021        match casted {
5022            Ok(_) => panic!("expected error"),
5023            Err(e) => {
5024                assert!(
5025                    e.to_string().contains(
5026                        "Cast error: Cannot cast value 'invalid' to value of Boolean type"
5027                    )
5028                )
5029            }
5030        }
5031    }
5032
5033    #[test]
5034    fn test_cast_bool_to_i32() {
5035        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
5036        let b = cast(&array, &DataType::Int32).unwrap();
5037        let c = b.as_primitive::<Int32Type>();
5038        assert_eq!(1, c.value(0));
5039        assert_eq!(0, c.value(1));
5040        assert!(!c.is_valid(2));
5041    }
5042
5043    #[test]
5044    fn test_cast_bool_to_utf8view() {
5045        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
5046        let b = cast(&array, &DataType::Utf8View).unwrap();
5047        let c = b.as_any().downcast_ref::<StringViewArray>().unwrap();
5048        assert_eq!("true", c.value(0));
5049        assert_eq!("false", c.value(1));
5050        assert!(!c.is_valid(2));
5051    }
5052
5053    #[test]
5054    fn test_cast_bool_to_utf8() {
5055        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
5056        let b = cast(&array, &DataType::Utf8).unwrap();
5057        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
5058        assert_eq!("true", c.value(0));
5059        assert_eq!("false", c.value(1));
5060        assert!(!c.is_valid(2));
5061    }
5062
5063    #[test]
5064    fn test_cast_bool_to_large_utf8() {
5065        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
5066        let b = cast(&array, &DataType::LargeUtf8).unwrap();
5067        let c = b.as_any().downcast_ref::<LargeStringArray>().unwrap();
5068        assert_eq!("true", c.value(0));
5069        assert_eq!("false", c.value(1));
5070        assert!(!c.is_valid(2));
5071    }
5072
5073    #[test]
5074    fn test_cast_bool_to_f64() {
5075        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
5076        let b = cast(&array, &DataType::Float64).unwrap();
5077        let c = b.as_primitive::<Float64Type>();
5078        assert_eq!(1.0, c.value(0));
5079        assert_eq!(0.0, c.value(1));
5080        assert!(!c.is_valid(2));
5081    }
5082
5083    #[test]
5084    fn test_cast_integer_to_timestamp() {
5085        let array = Int64Array::from(vec![Some(2), Some(10), None]);
5086        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5087
5088        let array = Int8Array::from(vec![Some(2), Some(10), None]);
5089        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5090
5091        assert_eq!(&actual, &expected);
5092
5093        let array = Int16Array::from(vec![Some(2), Some(10), None]);
5094        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5095
5096        assert_eq!(&actual, &expected);
5097
5098        let array = Int32Array::from(vec![Some(2), Some(10), None]);
5099        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5100
5101        assert_eq!(&actual, &expected);
5102
5103        let array = UInt8Array::from(vec![Some(2), Some(10), None]);
5104        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5105
5106        assert_eq!(&actual, &expected);
5107
5108        let array = UInt16Array::from(vec![Some(2), Some(10), None]);
5109        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5110
5111        assert_eq!(&actual, &expected);
5112
5113        let array = UInt32Array::from(vec![Some(2), Some(10), None]);
5114        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5115
5116        assert_eq!(&actual, &expected);
5117
5118        let array = UInt64Array::from(vec![Some(2), Some(10), None]);
5119        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5120
5121        assert_eq!(&actual, &expected);
5122    }
5123
5124    #[test]
5125    fn test_cast_timestamp_to_integer() {
5126        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
5127            .with_timezone("UTC".to_string());
5128        let expected = cast(&array, &DataType::Int64).unwrap();
5129
5130        let actual = cast(&cast(&array, &DataType::Int8).unwrap(), &DataType::Int64).unwrap();
5131        assert_eq!(&actual, &expected);
5132
5133        let actual = cast(&cast(&array, &DataType::Int16).unwrap(), &DataType::Int64).unwrap();
5134        assert_eq!(&actual, &expected);
5135
5136        let actual = cast(&cast(&array, &DataType::Int32).unwrap(), &DataType::Int64).unwrap();
5137        assert_eq!(&actual, &expected);
5138
5139        let actual = cast(&cast(&array, &DataType::UInt8).unwrap(), &DataType::Int64).unwrap();
5140        assert_eq!(&actual, &expected);
5141
5142        let actual = cast(&cast(&array, &DataType::UInt16).unwrap(), &DataType::Int64).unwrap();
5143        assert_eq!(&actual, &expected);
5144
5145        let actual = cast(&cast(&array, &DataType::UInt32).unwrap(), &DataType::Int64).unwrap();
5146        assert_eq!(&actual, &expected);
5147
5148        let actual = cast(&cast(&array, &DataType::UInt64).unwrap(), &DataType::Int64).unwrap();
5149        assert_eq!(&actual, &expected);
5150    }
5151
5152    #[test]
5153    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
5154    fn test_cast_floating_to_timestamp() {
5155        let array = Int64Array::from(vec![Some(2), Some(10), None]);
5156        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5157
5158        let array = Float16Array::from(vec![
5159            Some(f16::from_f32(2.0)),
5160            Some(f16::from_f32(10.6)),
5161            None,
5162        ]);
5163        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5164
5165        assert_eq!(&actual, &expected);
5166
5167        let array = Float32Array::from(vec![Some(2.0), Some(10.6), None]);
5168        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5169
5170        assert_eq!(&actual, &expected);
5171
5172        let array = Float64Array::from(vec![Some(2.1), Some(10.2), None]);
5173        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5174
5175        assert_eq!(&actual, &expected);
5176    }
5177
5178    #[test]
5179    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
5180    fn test_cast_timestamp_to_floating() {
5181        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
5182            .with_timezone("UTC".to_string());
5183        let expected = cast(&array, &DataType::Int64).unwrap();
5184
5185        let actual = cast(&cast(&array, &DataType::Float16).unwrap(), &DataType::Int64).unwrap();
5186        assert_eq!(&actual, &expected);
5187
5188        let actual = cast(&cast(&array, &DataType::Float32).unwrap(), &DataType::Int64).unwrap();
5189        assert_eq!(&actual, &expected);
5190
5191        let actual = cast(&cast(&array, &DataType::Float64).unwrap(), &DataType::Int64).unwrap();
5192        assert_eq!(&actual, &expected);
5193    }
5194
5195    #[test]
5196    fn test_cast_decimal_to_timestamp() {
5197        let array = Int64Array::from(vec![Some(2), Some(10), None]);
5198        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5199
5200        let array = Decimal128Array::from(vec![Some(200), Some(1000), None])
5201            .with_precision_and_scale(4, 2)
5202            .unwrap();
5203        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5204
5205        assert_eq!(&actual, &expected);
5206
5207        let array = Decimal256Array::from(vec![
5208            Some(i256::from_i128(2000)),
5209            Some(i256::from_i128(10000)),
5210            None,
5211        ])
5212        .with_precision_and_scale(5, 3)
5213        .unwrap();
5214        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5215
5216        assert_eq!(&actual, &expected);
5217    }
5218
5219    #[test]
5220    fn test_cast_timestamp_to_decimal() {
5221        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
5222            .with_timezone("UTC".to_string());
5223        let expected = cast(&array, &DataType::Int64).unwrap();
5224
5225        let actual = cast(
5226            &cast(&array, &DataType::Decimal128(5, 2)).unwrap(),
5227            &DataType::Int64,
5228        )
5229        .unwrap();
5230        assert_eq!(&actual, &expected);
5231
5232        let actual = cast(
5233            &cast(&array, &DataType::Decimal256(10, 5)).unwrap(),
5234            &DataType::Int64,
5235        )
5236        .unwrap();
5237        assert_eq!(&actual, &expected);
5238    }
5239
5240    #[test]
5241    fn test_cast_list_i32_to_list_u16() {
5242        let values = vec![
5243            Some(vec![Some(0), Some(0), Some(0)]),
5244            Some(vec![Some(-1), Some(-2), Some(-1)]),
5245            Some(vec![Some(2), Some(100000000)]),
5246        ];
5247        let list_array = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
5248
5249        let target_type = DataType::List(Arc::new(Field::new("item", DataType::UInt16, true)));
5250        assert!(can_cast_types(list_array.data_type(), &target_type));
5251        let cast_array = cast(&list_array, &target_type).unwrap();
5252
5253        // For the ListArray itself, there are no null values (as there were no nulls when they went in)
5254        //
5255        // 3 negative values should get lost when casting to unsigned,
5256        // 1 value should overflow
5257        assert_eq!(0, cast_array.null_count());
5258
5259        // offsets should be the same
5260        let array = cast_array.as_list::<i32>();
5261        assert_eq!(list_array.value_offsets(), array.value_offsets());
5262
5263        assert_eq!(DataType::UInt16, array.value_type());
5264        assert_eq!(3, array.value_length(0));
5265        assert_eq!(3, array.value_length(1));
5266        assert_eq!(2, array.value_length(2));
5267
5268        // expect 4 nulls: negative numbers and overflow
5269        let u16arr = array.values().as_primitive::<UInt16Type>();
5270        assert_eq!(4, u16arr.null_count());
5271
5272        // expect 4 nulls: negative numbers and overflow
5273        let expected: UInt16Array =
5274            vec![Some(0), Some(0), Some(0), None, None, None, Some(2), None]
5275                .into_iter()
5276                .collect();
5277
5278        assert_eq!(u16arr, &expected);
5279    }
5280
5281    #[test]
5282    fn test_cast_list_i32_to_list_timestamp() {
5283        // Construct a value array
5284        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 8, 100000000]).into_data();
5285
5286        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 9]);
5287
5288        // Construct a list array from the above two
5289        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
5290        let list_data = ArrayData::builder(list_data_type)
5291            .len(3)
5292            .add_buffer(value_offsets)
5293            .add_child_data(value_data)
5294            .build()
5295            .unwrap();
5296        let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
5297
5298        let actual = cast(
5299            &list_array,
5300            &DataType::List(Arc::new(Field::new_list_field(
5301                DataType::Timestamp(TimeUnit::Microsecond, None),
5302                true,
5303            ))),
5304        )
5305        .unwrap();
5306
5307        let expected = cast(
5308            &cast(
5309                &list_array,
5310                &DataType::List(Arc::new(Field::new_list_field(DataType::Int64, true))),
5311            )
5312            .unwrap(),
5313            &DataType::List(Arc::new(Field::new_list_field(
5314                DataType::Timestamp(TimeUnit::Microsecond, None),
5315                true,
5316            ))),
5317        )
5318        .unwrap();
5319
5320        assert_eq!(&actual, &expected);
5321    }
5322
5323    #[test]
5324    fn test_cast_date32_to_date64() {
5325        let a = Date32Array::from(vec![10000, 17890]);
5326        let array = Arc::new(a) as ArrayRef;
5327        let b = cast(&array, &DataType::Date64).unwrap();
5328        let c = b.as_primitive::<Date64Type>();
5329        assert_eq!(864000000000, c.value(0));
5330        assert_eq!(1545696000000, c.value(1));
5331    }
5332
5333    #[test]
5334    fn test_cast_date64_to_date32() {
5335        let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5336        let array = Arc::new(a) as ArrayRef;
5337        let b = cast(&array, &DataType::Date32).unwrap();
5338        let c = b.as_primitive::<Date32Type>();
5339        assert_eq!(10000, c.value(0));
5340        assert_eq!(17890, c.value(1));
5341        assert!(c.is_null(2));
5342    }
5343
5344    #[test]
5345    fn test_cast_date64_to_date32_overflow() {
5346        let a = Date64Array::from(vec![i64::MAX]);
5347        let array = Arc::new(a) as ArrayRef;
5348
5349        let b = cast(&array, &DataType::Date32).unwrap();
5350        let c = b.as_primitive::<Date32Type>();
5351        assert!(c.is_null(0));
5352
5353        let options = CastOptions {
5354            safe: false,
5355            ..Default::default()
5356        };
5357        let err = cast_with_options(&array, &DataType::Date32, &options).unwrap_err();
5358        assert!(
5359            err.to_string().contains("Cannot cast Date64 value"),
5360            "{err}"
5361        );
5362    }
5363
5364    #[test]
5365    fn test_cast_string_to_integral_overflow() {
5366        let str = Arc::new(StringArray::from(vec![
5367            Some("123"),
5368            Some("-123"),
5369            Some("86374"),
5370            None,
5371        ])) as ArrayRef;
5372
5373        let options = CastOptions {
5374            safe: true,
5375            format_options: FormatOptions::default(),
5376        };
5377        let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
5378        let expected =
5379            Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
5380        assert_eq!(&res, &expected);
5381    }
5382
5383    #[test]
5384    fn test_cast_string_to_timestamp() {
5385        let a0 = Arc::new(StringViewArray::from(vec![
5386            Some("2020-09-08T12:00:00.123456789+00:00"),
5387            Some("Not a valid date"),
5388            None,
5389        ])) as ArrayRef;
5390        let a1 = Arc::new(StringArray::from(vec![
5391            Some("2020-09-08T12:00:00.123456789+00:00"),
5392            Some("Not a valid date"),
5393            None,
5394        ])) as ArrayRef;
5395        let a2 = Arc::new(LargeStringArray::from(vec![
5396            Some("2020-09-08T12:00:00.123456789+00:00"),
5397            Some("Not a valid date"),
5398            None,
5399        ])) as ArrayRef;
5400        for array in &[a0, a1, a2] {
5401            for time_unit in &[
5402                TimeUnit::Second,
5403                TimeUnit::Millisecond,
5404                TimeUnit::Microsecond,
5405                TimeUnit::Nanosecond,
5406            ] {
5407                let to_type = DataType::Timestamp(*time_unit, None);
5408                let b = cast(array, &to_type).unwrap();
5409
5410                match time_unit {
5411                    TimeUnit::Second => {
5412                        let c = b.as_primitive::<TimestampSecondType>();
5413                        assert_eq!(1599566400, c.value(0));
5414                        assert!(c.is_null(1));
5415                        assert!(c.is_null(2));
5416                    }
5417                    TimeUnit::Millisecond => {
5418                        let c = b
5419                            .as_any()
5420                            .downcast_ref::<TimestampMillisecondArray>()
5421                            .unwrap();
5422                        assert_eq!(1599566400123, c.value(0));
5423                        assert!(c.is_null(1));
5424                        assert!(c.is_null(2));
5425                    }
5426                    TimeUnit::Microsecond => {
5427                        let c = b
5428                            .as_any()
5429                            .downcast_ref::<TimestampMicrosecondArray>()
5430                            .unwrap();
5431                        assert_eq!(1599566400123456, c.value(0));
5432                        assert!(c.is_null(1));
5433                        assert!(c.is_null(2));
5434                    }
5435                    TimeUnit::Nanosecond => {
5436                        let c = b
5437                            .as_any()
5438                            .downcast_ref::<TimestampNanosecondArray>()
5439                            .unwrap();
5440                        assert_eq!(1599566400123456789, c.value(0));
5441                        assert!(c.is_null(1));
5442                        assert!(c.is_null(2));
5443                    }
5444                }
5445
5446                let options = CastOptions {
5447                    safe: false,
5448                    format_options: FormatOptions::default(),
5449                };
5450                let err = cast_with_options(array, &to_type, &options).unwrap_err();
5451                assert_eq!(
5452                    err.to_string(),
5453                    "Parser error: Error parsing timestamp from 'Not a valid date': error parsing date"
5454                );
5455            }
5456        }
5457    }
5458
5459    #[test]
5460    fn test_cast_string_to_timestamp_overflow() {
5461        let array = StringArray::from(vec!["9800-09-08T12:00:00.123456789"]);
5462        let result = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5463        let result = result.as_primitive::<TimestampSecondType>();
5464        assert_eq!(result.values(), &[247112596800]);
5465    }
5466
5467    #[test]
5468    fn test_cast_string_to_date32() {
5469        let a0 = Arc::new(StringViewArray::from(vec![
5470            Some("2018-12-25"),
5471            Some("Not a valid date"),
5472            None,
5473        ])) as ArrayRef;
5474        let a1 = Arc::new(StringArray::from(vec![
5475            Some("2018-12-25"),
5476            Some("Not a valid date"),
5477            None,
5478        ])) as ArrayRef;
5479        let a2 = Arc::new(LargeStringArray::from(vec![
5480            Some("2018-12-25"),
5481            Some("Not a valid date"),
5482            None,
5483        ])) as ArrayRef;
5484        for array in &[a0, a1, a2] {
5485            let to_type = DataType::Date32;
5486            let b = cast(array, &to_type).unwrap();
5487            let c = b.as_primitive::<Date32Type>();
5488            assert_eq!(17890, c.value(0));
5489            assert!(c.is_null(1));
5490            assert!(c.is_null(2));
5491
5492            let options = CastOptions {
5493                safe: false,
5494                format_options: FormatOptions::default(),
5495            };
5496            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5497            assert_eq!(
5498                err.to_string(),
5499                "Cast error: Cannot cast string 'Not a valid date' to value of Date32 type"
5500            );
5501        }
5502    }
5503
5504    #[test]
5505    fn test_cast_string_with_large_date_to_date32() {
5506        let array = Arc::new(StringArray::from(vec![
5507            Some("+10999-12-31"),
5508            Some("-0010-02-28"),
5509            Some("0010-02-28"),
5510            Some("0000-01-01"),
5511            Some("-0000-01-01"),
5512            Some("-0001-01-01"),
5513        ])) as ArrayRef;
5514        let to_type = DataType::Date32;
5515        let options = CastOptions {
5516            safe: false,
5517            format_options: FormatOptions::default(),
5518        };
5519        let b = cast_with_options(&array, &to_type, &options).unwrap();
5520        let c = b.as_primitive::<Date32Type>();
5521        assert_eq!(3298139, c.value(0)); // 10999-12-31
5522        assert_eq!(-723122, c.value(1)); // -0010-02-28
5523        assert_eq!(-715817, c.value(2)); // 0010-02-28
5524        assert_eq!(c.value(3), c.value(4)); // Expect 0000-01-01 and -0000-01-01 to be parsed the same
5525        assert_eq!(-719528, c.value(3)); // 0000-01-01
5526        assert_eq!(-719528, c.value(4)); // -0000-01-01
5527        assert_eq!(-719893, c.value(5)); // -0001-01-01
5528    }
5529
5530    #[test]
5531    fn test_cast_invalid_string_with_large_date_to_date32() {
5532        // Large dates need to be prefixed with a + or - sign, otherwise they are not parsed correctly
5533        let array = Arc::new(StringArray::from(vec![Some("10999-12-31")])) as ArrayRef;
5534        let to_type = DataType::Date32;
5535        let options = CastOptions {
5536            safe: false,
5537            format_options: FormatOptions::default(),
5538        };
5539        let err = cast_with_options(&array, &to_type, &options).unwrap_err();
5540        assert_eq!(
5541            err.to_string(),
5542            "Cast error: Cannot cast string '10999-12-31' to value of Date32 type"
5543        );
5544    }
5545
5546    #[test]
5547    fn test_cast_string_format_yyyymmdd_to_date32() {
5548        let a0 = Arc::new(StringViewArray::from(vec![
5549            Some("2020-12-25"),
5550            Some("20201117"),
5551        ])) as ArrayRef;
5552        let a1 = Arc::new(StringArray::from(vec![
5553            Some("2020-12-25"),
5554            Some("20201117"),
5555        ])) as ArrayRef;
5556        let a2 = Arc::new(LargeStringArray::from(vec![
5557            Some("2020-12-25"),
5558            Some("20201117"),
5559        ])) as ArrayRef;
5560
5561        for array in &[a0, a1, a2] {
5562            let to_type = DataType::Date32;
5563            let options = CastOptions {
5564                safe: false,
5565                format_options: FormatOptions::default(),
5566            };
5567            let result = cast_with_options(&array, &to_type, &options).unwrap();
5568            let c = result.as_primitive::<Date32Type>();
5569            assert_eq!(
5570                chrono::NaiveDate::from_ymd_opt(2020, 12, 25),
5571                c.value_as_date(0)
5572            );
5573            assert_eq!(
5574                chrono::NaiveDate::from_ymd_opt(2020, 11, 17),
5575                c.value_as_date(1)
5576            );
5577        }
5578    }
5579
5580    #[test]
5581    fn test_cast_string_to_time32second() {
5582        let a0 = Arc::new(StringViewArray::from(vec![
5583            Some("08:08:35.091323414"),
5584            Some("08:08:60.091323414"), // leap second
5585            Some("08:08:61.091323414"), // not valid
5586            Some("Not a valid time"),
5587            None,
5588        ])) as ArrayRef;
5589        let a1 = Arc::new(StringArray::from(vec![
5590            Some("08:08:35.091323414"),
5591            Some("08:08:60.091323414"), // leap second
5592            Some("08:08:61.091323414"), // not valid
5593            Some("Not a valid time"),
5594            None,
5595        ])) as ArrayRef;
5596        let a2 = Arc::new(LargeStringArray::from(vec![
5597            Some("08:08:35.091323414"),
5598            Some("08:08:60.091323414"), // leap second
5599            Some("08:08:61.091323414"), // not valid
5600            Some("Not a valid time"),
5601            None,
5602        ])) as ArrayRef;
5603        for array in &[a0, a1, a2] {
5604            let to_type = DataType::Time32(TimeUnit::Second);
5605            let b = cast(array, &to_type).unwrap();
5606            let c = b.as_primitive::<Time32SecondType>();
5607            assert_eq!(29315, c.value(0));
5608            assert_eq!(29340, c.value(1));
5609            assert!(c.is_null(2));
5610            assert!(c.is_null(3));
5611            assert!(c.is_null(4));
5612
5613            let options = CastOptions {
5614                safe: false,
5615                format_options: FormatOptions::default(),
5616            };
5617            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5618            assert_eq!(
5619                err.to_string(),
5620                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(s) type"
5621            );
5622        }
5623    }
5624
5625    #[test]
5626    fn test_cast_string_to_time32millisecond() {
5627        let a0 = Arc::new(StringViewArray::from(vec![
5628            Some("08:08:35.091323414"),
5629            Some("08:08:60.091323414"), // leap second
5630            Some("08:08:61.091323414"), // not valid
5631            Some("Not a valid time"),
5632            None,
5633        ])) as ArrayRef;
5634        let a1 = Arc::new(StringArray::from(vec![
5635            Some("08:08:35.091323414"),
5636            Some("08:08:60.091323414"), // leap second
5637            Some("08:08:61.091323414"), // not valid
5638            Some("Not a valid time"),
5639            None,
5640        ])) as ArrayRef;
5641        let a2 = Arc::new(LargeStringArray::from(vec![
5642            Some("08:08:35.091323414"),
5643            Some("08:08:60.091323414"), // leap second
5644            Some("08:08:61.091323414"), // not valid
5645            Some("Not a valid time"),
5646            None,
5647        ])) as ArrayRef;
5648        for array in &[a0, a1, a2] {
5649            let to_type = DataType::Time32(TimeUnit::Millisecond);
5650            let b = cast(array, &to_type).unwrap();
5651            let c = b.as_primitive::<Time32MillisecondType>();
5652            assert_eq!(29315091, c.value(0));
5653            assert_eq!(29340091, c.value(1));
5654            assert!(c.is_null(2));
5655            assert!(c.is_null(3));
5656            assert!(c.is_null(4));
5657
5658            let options = CastOptions {
5659                safe: false,
5660                format_options: FormatOptions::default(),
5661            };
5662            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5663            assert_eq!(
5664                err.to_string(),
5665                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(ms) type"
5666            );
5667        }
5668    }
5669
5670    #[test]
5671    fn test_cast_string_to_time64microsecond() {
5672        let a0 = Arc::new(StringViewArray::from(vec![
5673            Some("08:08:35.091323414"),
5674            Some("Not a valid time"),
5675            None,
5676        ])) as ArrayRef;
5677        let a1 = Arc::new(StringArray::from(vec![
5678            Some("08:08:35.091323414"),
5679            Some("Not a valid time"),
5680            None,
5681        ])) as ArrayRef;
5682        let a2 = Arc::new(LargeStringArray::from(vec![
5683            Some("08:08:35.091323414"),
5684            Some("Not a valid time"),
5685            None,
5686        ])) as ArrayRef;
5687        for array in &[a0, a1, a2] {
5688            let to_type = DataType::Time64(TimeUnit::Microsecond);
5689            let b = cast(array, &to_type).unwrap();
5690            let c = b.as_primitive::<Time64MicrosecondType>();
5691            assert_eq!(29315091323, c.value(0));
5692            assert!(c.is_null(1));
5693            assert!(c.is_null(2));
5694
5695            let options = CastOptions {
5696                safe: false,
5697                format_options: FormatOptions::default(),
5698            };
5699            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5700            assert_eq!(
5701                err.to_string(),
5702                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(µs) type"
5703            );
5704        }
5705    }
5706
5707    #[test]
5708    fn test_cast_string_to_time64nanosecond() {
5709        let a0 = Arc::new(StringViewArray::from(vec![
5710            Some("08:08:35.091323414"),
5711            Some("Not a valid time"),
5712            None,
5713        ])) as ArrayRef;
5714        let a1 = Arc::new(StringArray::from(vec![
5715            Some("08:08:35.091323414"),
5716            Some("Not a valid time"),
5717            None,
5718        ])) as ArrayRef;
5719        let a2 = Arc::new(LargeStringArray::from(vec![
5720            Some("08:08:35.091323414"),
5721            Some("Not a valid time"),
5722            None,
5723        ])) as ArrayRef;
5724        for array in &[a0, a1, a2] {
5725            let to_type = DataType::Time64(TimeUnit::Nanosecond);
5726            let b = cast(array, &to_type).unwrap();
5727            let c = b.as_primitive::<Time64NanosecondType>();
5728            assert_eq!(29315091323414, c.value(0));
5729            assert!(c.is_null(1));
5730            assert!(c.is_null(2));
5731
5732            let options = CastOptions {
5733                safe: false,
5734                format_options: FormatOptions::default(),
5735            };
5736            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5737            assert_eq!(
5738                err.to_string(),
5739                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(ns) type"
5740            );
5741        }
5742    }
5743
5744    #[test]
5745    fn test_cast_string_to_date64() {
5746        let a0 = Arc::new(StringViewArray::from(vec![
5747            Some("2020-09-08T12:00:00"),
5748            Some("Not a valid date"),
5749            None,
5750        ])) as ArrayRef;
5751        let a1 = Arc::new(StringArray::from(vec![
5752            Some("2020-09-08T12:00:00"),
5753            Some("Not a valid date"),
5754            None,
5755        ])) as ArrayRef;
5756        let a2 = Arc::new(LargeStringArray::from(vec![
5757            Some("2020-09-08T12:00:00"),
5758            Some("Not a valid date"),
5759            None,
5760        ])) as ArrayRef;
5761        for array in &[a0, a1, a2] {
5762            let to_type = DataType::Date64;
5763            let b = cast(array, &to_type).unwrap();
5764            let c = b.as_primitive::<Date64Type>();
5765            assert_eq!(1599566400000, c.value(0));
5766            assert!(c.is_null(1));
5767            assert!(c.is_null(2));
5768
5769            let options = CastOptions {
5770                safe: false,
5771                format_options: FormatOptions::default(),
5772            };
5773            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5774            assert_eq!(
5775                err.to_string(),
5776                "Cast error: Cannot cast string 'Not a valid date' to value of Date64 type"
5777            );
5778        }
5779    }
5780
5781    macro_rules! test_safe_string_to_interval {
5782        ($data_vec:expr, $interval_unit:expr, $array_ty:ty, $expect_vec:expr) => {
5783            let source_string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5784
5785            let options = CastOptions {
5786                safe: true,
5787                format_options: FormatOptions::default(),
5788            };
5789
5790            let target_interval_array = cast_with_options(
5791                &source_string_array.clone(),
5792                &DataType::Interval($interval_unit),
5793                &options,
5794            )
5795            .unwrap()
5796            .as_any()
5797            .downcast_ref::<$array_ty>()
5798            .unwrap()
5799            .clone() as $array_ty;
5800
5801            let target_string_array =
5802                cast_with_options(&target_interval_array, &DataType::Utf8, &options)
5803                    .unwrap()
5804                    .as_any()
5805                    .downcast_ref::<StringArray>()
5806                    .unwrap()
5807                    .clone();
5808
5809            let expect_string_array = StringArray::from($expect_vec);
5810
5811            assert_eq!(target_string_array, expect_string_array);
5812
5813            let target_large_string_array =
5814                cast_with_options(&target_interval_array, &DataType::LargeUtf8, &options)
5815                    .unwrap()
5816                    .as_any()
5817                    .downcast_ref::<LargeStringArray>()
5818                    .unwrap()
5819                    .clone();
5820
5821            let expect_large_string_array = LargeStringArray::from($expect_vec);
5822
5823            assert_eq!(target_large_string_array, expect_large_string_array);
5824        };
5825    }
5826
5827    #[test]
5828    fn test_cast_string_to_interval_year_month() {
5829        test_safe_string_to_interval!(
5830            vec![
5831                Some("1 year 1 month"),
5832                Some("1.5 years 13 month"),
5833                Some("30 days"),
5834                Some("31 days"),
5835                Some("2 months 31 days"),
5836                Some("2 months 31 days 1 second"),
5837                Some("foobar"),
5838            ],
5839            IntervalUnit::YearMonth,
5840            IntervalYearMonthArray,
5841            vec![
5842                Some("1 years 1 mons"),
5843                Some("2 years 7 mons"),
5844                None,
5845                None,
5846                None,
5847                None,
5848                None,
5849            ]
5850        );
5851    }
5852
5853    #[test]
5854    fn test_cast_string_to_interval_day_time() {
5855        test_safe_string_to_interval!(
5856            vec![
5857                Some("1 year 1 month"),
5858                Some("1.5 years 13 month"),
5859                Some("30 days"),
5860                Some("1 day 2 second 3.5 milliseconds"),
5861                Some("foobar"),
5862            ],
5863            IntervalUnit::DayTime,
5864            IntervalDayTimeArray,
5865            vec![
5866                Some("390 days"),
5867                Some("930 days"),
5868                Some("30 days"),
5869                None,
5870                None,
5871            ]
5872        );
5873    }
5874
5875    #[test]
5876    fn test_cast_string_to_interval_month_day_nano() {
5877        test_safe_string_to_interval!(
5878            vec![
5879                Some("1 year 1 month 1 day"),
5880                None,
5881                Some("1.5 years 13 month 35 days 1.4 milliseconds"),
5882                Some("3 days"),
5883                Some("8 seconds"),
5884                None,
5885                Some("1 day 29800 milliseconds"),
5886                Some("3 months 1 second"),
5887                Some("6 minutes 120 second"),
5888                Some("2 years 39 months 9 days 19 hours 1 minute 83 seconds 399222 milliseconds"),
5889                Some("foobar"),
5890            ],
5891            IntervalUnit::MonthDayNano,
5892            IntervalMonthDayNanoArray,
5893            vec![
5894                Some("13 mons 1 days"),
5895                None,
5896                Some("31 mons 35 days 0.001400000 secs"),
5897                Some("3 days"),
5898                Some("8.000000000 secs"),
5899                None,
5900                Some("1 days 29.800000000 secs"),
5901                Some("3 mons 1.000000000 secs"),
5902                Some("8 mins"),
5903                Some("63 mons 9 days 19 hours 9 mins 2.222000000 secs"),
5904                None,
5905            ]
5906        );
5907    }
5908
5909    macro_rules! test_unsafe_string_to_interval_err {
5910        ($data_vec:expr, $interval_unit:expr, $error_msg:expr) => {
5911            let string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5912            let options = CastOptions {
5913                safe: false,
5914                format_options: FormatOptions::default(),
5915            };
5916            let arrow_err = cast_with_options(
5917                &string_array.clone(),
5918                &DataType::Interval($interval_unit),
5919                &options,
5920            )
5921            .unwrap_err();
5922            assert_eq!($error_msg, arrow_err.to_string());
5923        };
5924    }
5925
5926    #[test]
5927    fn test_cast_string_to_interval_err() {
5928        test_unsafe_string_to_interval_err!(
5929            vec![Some("foobar")],
5930            IntervalUnit::YearMonth,
5931            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5932        );
5933        test_unsafe_string_to_interval_err!(
5934            vec![Some("foobar")],
5935            IntervalUnit::DayTime,
5936            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5937        );
5938        test_unsafe_string_to_interval_err!(
5939            vec![Some("foobar")],
5940            IntervalUnit::MonthDayNano,
5941            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5942        );
5943        test_unsafe_string_to_interval_err!(
5944            vec![Some("2 months 31 days 1 second")],
5945            IntervalUnit::YearMonth,
5946            "Cast error: Cannot cast 2 months 31 days 1 second to IntervalYearMonth. Only year and month fields are allowed."
5947        );
5948        test_unsafe_string_to_interval_err!(
5949            vec![Some("1 day 1.5 milliseconds")],
5950            IntervalUnit::DayTime,
5951            "Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"
5952        );
5953
5954        // overflow
5955        test_unsafe_string_to_interval_err!(
5956            vec![Some(format!(
5957                "{} century {} year {} month",
5958                i64::MAX - 2,
5959                i64::MAX - 2,
5960                i64::MAX - 2
5961            ))],
5962            IntervalUnit::DayTime,
5963            format!(
5964                "Arithmetic overflow: Overflow happened on: {} * 100",
5965                i64::MAX - 2
5966            )
5967        );
5968        test_unsafe_string_to_interval_err!(
5969            vec![Some(format!(
5970                "{} year {} month {} day",
5971                i64::MAX - 2,
5972                i64::MAX - 2,
5973                i64::MAX - 2
5974            ))],
5975            IntervalUnit::MonthDayNano,
5976            format!(
5977                "Arithmetic overflow: Overflow happened on: {} * 12",
5978                i64::MAX - 2
5979            )
5980        );
5981    }
5982
5983    #[test]
5984    fn test_cast_binary_to_fixed_size_binary() {
5985        let bytes_1 = b"Hiiii".as_slice();
5986        let bytes_2 = b"Hello".as_slice();
5987
5988        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5989        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5990        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5991
5992        let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
5993        let down_cast = array_ref
5994            .as_any()
5995            .downcast_ref::<FixedSizeBinaryArray>()
5996            .unwrap();
5997        assert_eq!(bytes_1, down_cast.value(0));
5998        assert_eq!(bytes_2, down_cast.value(1));
5999        assert!(down_cast.is_null(2));
6000
6001        let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
6002        let down_cast = array_ref
6003            .as_any()
6004            .downcast_ref::<FixedSizeBinaryArray>()
6005            .unwrap();
6006        assert_eq!(bytes_1, down_cast.value(0));
6007        assert_eq!(bytes_2, down_cast.value(1));
6008        assert!(down_cast.is_null(2));
6009
6010        // test error cases when the length of binary are not same
6011        let bytes_1 = b"Hi".as_slice();
6012        let bytes_2 = b"Hello".as_slice();
6013
6014        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
6015        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
6016        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
6017
6018        let array_ref = cast_with_options(
6019            &a1,
6020            &DataType::FixedSizeBinary(5),
6021            &CastOptions {
6022                safe: false,
6023                format_options: FormatOptions::default(),
6024            },
6025        );
6026        assert!(array_ref.is_err());
6027
6028        let array_ref = cast_with_options(
6029            &a2,
6030            &DataType::FixedSizeBinary(5),
6031            &CastOptions {
6032                safe: false,
6033                format_options: FormatOptions::default(),
6034            },
6035        );
6036        assert!(array_ref.is_err());
6037    }
6038
6039    #[test]
6040    fn test_fixed_size_binary_to_binary() {
6041        let bytes_1 = b"Hiiii".as_slice();
6042        let bytes_2 = b"Hello".as_slice();
6043
6044        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
6045        let a1 = Arc::new(FixedSizeBinaryArray::try_from(binary_data.clone()).unwrap()) as ArrayRef;
6046
6047        let array_ref = cast(&a1, &DataType::Binary).unwrap();
6048        let down_cast = array_ref.as_binary::<i32>();
6049        assert_eq!(bytes_1, down_cast.value(0));
6050        assert_eq!(bytes_2, down_cast.value(1));
6051        assert!(down_cast.is_null(2));
6052
6053        let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
6054        let down_cast = array_ref.as_binary::<i64>();
6055        assert_eq!(bytes_1, down_cast.value(0));
6056        assert_eq!(bytes_2, down_cast.value(1));
6057        assert!(down_cast.is_null(2));
6058
6059        let array_ref = cast(&a1, &DataType::BinaryView).unwrap();
6060        let down_cast = array_ref.as_binary_view();
6061        assert_eq!(bytes_1, down_cast.value(0));
6062        assert_eq!(bytes_2, down_cast.value(1));
6063        assert!(down_cast.is_null(2));
6064    }
6065
6066    #[test]
6067    fn test_fixed_size_binary_to_dictionary() {
6068        let bytes_1 = b"Hiiii".as_slice();
6069        let bytes_2 = b"Hello".as_slice();
6070
6071        let binary_data = vec![Some(bytes_1), Some(bytes_2), Some(bytes_1), None];
6072        let a1 = Arc::new(FixedSizeBinaryArray::try_from(binary_data.clone()).unwrap()) as ArrayRef;
6073
6074        let cast_type = DataType::Dictionary(
6075            Box::new(DataType::Int8),
6076            Box::new(DataType::FixedSizeBinary(5)),
6077        );
6078        let cast_array = cast(&a1, &cast_type).unwrap();
6079        assert_eq!(cast_array.data_type(), &cast_type);
6080        assert_eq!(
6081            array_to_strings(&cast_array),
6082            vec!["4869696969", "48656c6c6f", "4869696969", "null"]
6083        );
6084        // dictionary should only have two distinct values
6085        let dict_array = cast_array.as_dictionary::<Int8Type>();
6086        assert_eq!(dict_array.values().len(), 2);
6087    }
6088
6089    #[test]
6090    fn test_binary_to_dictionary() {
6091        let mut builder = GenericBinaryBuilder::<i32>::new();
6092        builder.append_value(b"hello");
6093        builder.append_value(b"hiiii");
6094        builder.append_value(b"hiiii"); // duplicate
6095        builder.append_null();
6096        builder.append_value(b"rustt");
6097
6098        let a1 = builder.finish();
6099
6100        let cast_type = DataType::Dictionary(
6101            Box::new(DataType::Int8),
6102            Box::new(DataType::FixedSizeBinary(5)),
6103        );
6104        let cast_array = cast(&a1, &cast_type).unwrap();
6105        assert_eq!(cast_array.data_type(), &cast_type);
6106        assert_eq!(
6107            array_to_strings(&cast_array),
6108            vec![
6109                "68656c6c6f",
6110                "6869696969",
6111                "6869696969",
6112                "null",
6113                "7275737474"
6114            ]
6115        );
6116        // dictionary should only have three distinct values
6117        let dict_array = cast_array.as_dictionary::<Int8Type>();
6118        assert_eq!(dict_array.values().len(), 3);
6119    }
6120
6121    #[test]
6122    fn test_cast_string_array_to_dict_utf8_view() {
6123        let array = StringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
6124
6125        let cast_type =
6126            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6127        assert!(can_cast_types(array.data_type(), &cast_type));
6128        let cast_array = cast(&array, &cast_type).unwrap();
6129        assert_eq!(cast_array.data_type(), &cast_type);
6130
6131        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6132        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6133        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
6134
6135        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6136        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6137        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6138
6139        let keys = dict_array.keys();
6140        assert!(keys.is_null(1));
6141        assert_eq!(keys.value(0), keys.value(3));
6142        assert_ne!(keys.value(0), keys.value(2));
6143    }
6144
6145    #[test]
6146    fn test_cast_string_array_to_dict_utf8_view_null_vs_literal_null() {
6147        let array = StringArray::from(vec![Some("one"), None, Some("null"), Some("one")]);
6148
6149        let cast_type =
6150            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6151        assert!(can_cast_types(array.data_type(), &cast_type));
6152        let cast_array = cast(&array, &cast_type).unwrap();
6153        assert_eq!(cast_array.data_type(), &cast_type);
6154
6155        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6156        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6157        assert_eq!(dict_array.values().len(), 2);
6158
6159        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6160        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6161        assert_eq!(actual, vec![Some("one"), None, Some("null"), Some("one")]);
6162
6163        let keys = dict_array.keys();
6164        assert!(keys.is_null(1));
6165        assert_eq!(keys.value(0), keys.value(3));
6166        assert_ne!(keys.value(0), keys.value(2));
6167    }
6168
6169    #[test]
6170    fn test_cast_string_view_array_to_dict_utf8_view() {
6171        let array = StringViewArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
6172
6173        let cast_type =
6174            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6175        assert!(can_cast_types(array.data_type(), &cast_type));
6176        let cast_array = cast(&array, &cast_type).unwrap();
6177        assert_eq!(cast_array.data_type(), &cast_type);
6178
6179        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6180        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6181        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
6182
6183        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6184        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6185        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6186
6187        let keys = dict_array.keys();
6188        assert!(keys.is_null(1));
6189        assert_eq!(keys.value(0), keys.value(3));
6190        assert_ne!(keys.value(0), keys.value(2));
6191    }
6192
6193    #[test]
6194    fn test_cast_string_view_slice_to_dict_utf8_view() {
6195        let array = StringViewArray::from(vec![
6196            Some("zero"),
6197            Some("one"),
6198            None,
6199            Some("three"),
6200            Some("one"),
6201        ]);
6202        let view = array.slice(1, 4);
6203
6204        let cast_type =
6205            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6206        assert!(can_cast_types(view.data_type(), &cast_type));
6207        let cast_array = cast(&view, &cast_type).unwrap();
6208        assert_eq!(cast_array.data_type(), &cast_type);
6209
6210        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6211        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6212        assert_eq!(dict_array.values().len(), 2);
6213
6214        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6215        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6216        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6217
6218        let keys = dict_array.keys();
6219        assert!(keys.is_null(1));
6220        assert_eq!(keys.value(0), keys.value(3));
6221        assert_ne!(keys.value(0), keys.value(2));
6222    }
6223
6224    #[test]
6225    fn test_cast_binary_array_to_dict_binary_view() {
6226        let mut builder = GenericBinaryBuilder::<i32>::new();
6227        builder.append_value(b"hello");
6228        builder.append_value(b"hiiii");
6229        builder.append_value(b"hiiii"); // duplicate
6230        builder.append_null();
6231        builder.append_value(b"rustt");
6232
6233        let array = builder.finish();
6234
6235        let cast_type =
6236            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6237        assert!(can_cast_types(array.data_type(), &cast_type));
6238        let cast_array = cast(&array, &cast_type).unwrap();
6239        assert_eq!(cast_array.data_type(), &cast_type);
6240
6241        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6242        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6243        assert_eq!(dict_array.values().len(), 3);
6244
6245        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6246        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6247        assert_eq!(
6248            actual,
6249            vec![
6250                Some(b"hello".as_slice()),
6251                Some(b"hiiii".as_slice()),
6252                Some(b"hiiii".as_slice()),
6253                None,
6254                Some(b"rustt".as_slice())
6255            ]
6256        );
6257
6258        let keys = dict_array.keys();
6259        assert!(keys.is_null(3));
6260        assert_eq!(keys.value(1), keys.value(2));
6261        assert_ne!(keys.value(0), keys.value(1));
6262    }
6263
6264    #[test]
6265    fn test_cast_binary_view_array_to_dict_binary_view() {
6266        let view = BinaryViewArray::from_iter([
6267            Some(b"hello".as_slice()),
6268            Some(b"hiiii".as_slice()),
6269            Some(b"hiiii".as_slice()), // duplicate
6270            None,
6271            Some(b"rustt".as_slice()),
6272        ]);
6273
6274        let cast_type =
6275            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6276        assert!(can_cast_types(view.data_type(), &cast_type));
6277        let cast_array = cast(&view, &cast_type).unwrap();
6278        assert_eq!(cast_array.data_type(), &cast_type);
6279
6280        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6281        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6282        assert_eq!(dict_array.values().len(), 3);
6283
6284        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6285        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6286        assert_eq!(
6287            actual,
6288            vec![
6289                Some(b"hello".as_slice()),
6290                Some(b"hiiii".as_slice()),
6291                Some(b"hiiii".as_slice()),
6292                None,
6293                Some(b"rustt".as_slice())
6294            ]
6295        );
6296
6297        let keys = dict_array.keys();
6298        assert!(keys.is_null(3));
6299        assert_eq!(keys.value(1), keys.value(2));
6300        assert_ne!(keys.value(0), keys.value(1));
6301    }
6302
6303    #[test]
6304    fn test_cast_binary_view_slice_to_dict_binary_view() {
6305        let view = BinaryViewArray::from_iter([
6306            Some(b"hello".as_slice()),
6307            Some(b"hiiii".as_slice()),
6308            Some(b"hiiii".as_slice()), // duplicate
6309            None,
6310            Some(b"rustt".as_slice()),
6311        ]);
6312        let sliced = view.slice(1, 4);
6313
6314        let cast_type =
6315            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6316        assert!(can_cast_types(sliced.data_type(), &cast_type));
6317        let cast_array = cast(&sliced, &cast_type).unwrap();
6318        assert_eq!(cast_array.data_type(), &cast_type);
6319
6320        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6321        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6322        assert_eq!(dict_array.values().len(), 2);
6323
6324        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6325        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6326        assert_eq!(
6327            actual,
6328            vec![
6329                Some(b"hiiii".as_slice()),
6330                Some(b"hiiii".as_slice()),
6331                None,
6332                Some(b"rustt".as_slice())
6333            ]
6334        );
6335
6336        let keys = dict_array.keys();
6337        assert!(keys.is_null(2));
6338        assert_eq!(keys.value(0), keys.value(1));
6339        assert_ne!(keys.value(0), keys.value(3));
6340    }
6341
6342    #[test]
6343    fn test_cast_string_array_to_dict_utf8_view_key_overflow_u8() {
6344        let array = StringArray::from_iter_values((0..257).map(|i| format!("v{i}")));
6345
6346        let cast_type =
6347            DataType::Dictionary(Box::new(DataType::UInt8), Box::new(DataType::Utf8View));
6348        assert!(can_cast_types(array.data_type(), &cast_type));
6349        let err = cast(&array, &cast_type).unwrap_err();
6350        assert!(matches!(err, ArrowError::DictionaryKeyOverflowError));
6351    }
6352
6353    #[test]
6354    fn test_cast_large_string_array_to_dict_utf8_view() {
6355        let array = LargeStringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
6356
6357        let cast_type =
6358            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6359        assert!(can_cast_types(array.data_type(), &cast_type));
6360        let cast_array = cast(&array, &cast_type).unwrap();
6361        assert_eq!(cast_array.data_type(), &cast_type);
6362
6363        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6364        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6365        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
6366
6367        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6368        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6369        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6370
6371        let keys = dict_array.keys();
6372        assert!(keys.is_null(1));
6373        assert_eq!(keys.value(0), keys.value(3));
6374        assert_ne!(keys.value(0), keys.value(2));
6375    }
6376
6377    #[test]
6378    fn test_cast_large_binary_array_to_dict_binary_view() {
6379        let mut builder = GenericBinaryBuilder::<i64>::new();
6380        builder.append_value(b"hello");
6381        builder.append_value(b"world");
6382        builder.append_value(b"hello"); // duplicate
6383        builder.append_null();
6384
6385        let array = builder.finish();
6386
6387        let cast_type =
6388            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6389        assert!(can_cast_types(array.data_type(), &cast_type));
6390        let cast_array = cast(&array, &cast_type).unwrap();
6391        assert_eq!(cast_array.data_type(), &cast_type);
6392
6393        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6394        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6395        assert_eq!(dict_array.values().len(), 2); // "hello" and "world" deduplicated
6396
6397        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6398        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6399        assert_eq!(
6400            actual,
6401            vec![
6402                Some(b"hello".as_slice()),
6403                Some(b"world".as_slice()),
6404                Some(b"hello".as_slice()),
6405                None
6406            ]
6407        );
6408
6409        let keys = dict_array.keys();
6410        assert!(keys.is_null(3));
6411        assert_eq!(keys.value(0), keys.value(2));
6412        assert_ne!(keys.value(0), keys.value(1));
6413    }
6414
6415    #[test]
6416    fn test_cast_struct_array_to_dict_struct() {
6417        // Cast a StructArray into Dictionary<UInt32, Struct{…}>. The dictionary
6418        // value type's child fields may differ from the source's (here:
6419        // Utf8 source → Utf8View child for `name`), so the per-field cast
6420        // must run before identity keys are emitted. This is the "as long as
6421        // the struct can be cast to the dict value" contract.
6422        let names = StringArray::from(vec![Some("alpha"), None, Some("gamma")]);
6423        let ids = Int32Array::from(vec![Some(1), Some(2), Some(3)]);
6424        let source = StructArray::from(vec![
6425            (
6426                Arc::new(Field::new("name", DataType::Utf8, true)),
6427                Arc::new(names) as ArrayRef,
6428            ),
6429            (
6430                Arc::new(Field::new("id", DataType::Int32, false)),
6431                Arc::new(ids) as ArrayRef,
6432            ),
6433        ]);
6434
6435        let target_value_type = DataType::Struct(
6436            vec![
6437                Field::new("name", DataType::Utf8View, true),
6438                Field::new("id", DataType::Int64, false),
6439            ]
6440            .into(),
6441        );
6442        let cast_type = DataType::Dictionary(
6443            Box::new(DataType::UInt32),
6444            Box::new(target_value_type.clone()),
6445        );
6446        assert!(can_cast_types(source.data_type(), &cast_type));
6447
6448        let cast_array = cast(&source, &cast_type).unwrap();
6449        assert_eq!(cast_array.data_type(), &cast_type);
6450        assert_eq!(cast_array.len(), 3);
6451
6452        let dict = cast_array.as_dictionary::<UInt32Type>();
6453        assert_eq!(dict.values().data_type(), &target_value_type);
6454        // No dedup is performed for struct values — one row, one key.
6455        assert_eq!(dict.values().len(), 3);
6456
6457        // Source row 1 was a `Utf8`-null in the `name` field but the whole
6458        // struct row was valid (StructArray::from above takes per-field
6459        // nulls only). The dictionary's logical null mask therefore mirrors
6460        // the source struct's row-level null mask — all rows valid here.
6461        let keys = dict.keys();
6462        assert_eq!(keys.values(), &[0u32, 1, 2]);
6463        assert_eq!(keys.null_count(), 0);
6464
6465        let struct_values = dict.values().as_struct();
6466        let names_out = struct_values
6467            .column_by_name("name")
6468            .unwrap()
6469            .as_string_view();
6470        assert_eq!(names_out.value(0), "alpha");
6471        assert!(names_out.is_null(1));
6472        assert_eq!(names_out.value(2), "gamma");
6473        let ids_out = struct_values
6474            .column_by_name("id")
6475            .unwrap()
6476            .as_primitive::<Int64Type>();
6477        assert_eq!(ids_out.values(), &[1i64, 2, 3]);
6478    }
6479
6480    #[test]
6481    fn test_cast_struct_array_to_dict_struct_row_nulls() {
6482        // Row-level nulls on the source struct must surface as null keys on
6483        // the dictionary, since the dictionary's logical null mask is
6484        // determined by the keys.
6485        let names = StringArray::from(vec![Some("alpha"), Some("beta"), Some("gamma")]);
6486        let ids = Int32Array::from(vec![Some(1), Some(2), Some(3)]);
6487        let source = StructArray::try_new(
6488            vec![
6489                Field::new("name", DataType::Utf8, true),
6490                Field::new("id", DataType::Int32, false),
6491            ]
6492            .into(),
6493            vec![Arc::new(names) as ArrayRef, Arc::new(ids) as ArrayRef],
6494            Some(NullBuffer::from(vec![true, false, true])),
6495        )
6496        .unwrap();
6497
6498        let target_value_type = DataType::Struct(
6499            vec![
6500                Field::new("name", DataType::Utf8, true),
6501                Field::new("id", DataType::Int32, false),
6502            ]
6503            .into(),
6504        );
6505        let cast_type =
6506            DataType::Dictionary(Box::new(DataType::UInt32), Box::new(target_value_type));
6507
6508        let cast_array = cast(&source, &cast_type).unwrap();
6509        let dict = cast_array.as_dictionary::<UInt32Type>();
6510        assert_eq!(dict.len(), 3);
6511        let keys = dict.keys();
6512        assert!(!keys.is_null(0));
6513        assert!(keys.is_null(1));
6514        assert!(!keys.is_null(2));
6515    }
6516
6517    #[test]
6518    fn test_cast_struct_array_to_dict_struct_key_overflow() {
6519        // Source has 300 rows but the dictionary key type is UInt8 (max 255).
6520        // We must return a CastError instead of silently truncating.
6521        let n = 300;
6522        let names = StringArray::from((0..n).map(|i| Some(format!("v{i}"))).collect::<Vec<_>>());
6523        let source = StructArray::from(vec![(
6524            Arc::new(Field::new("name", DataType::Utf8, true)),
6525            Arc::new(names) as ArrayRef,
6526        )]);
6527
6528        let cast_type = DataType::Dictionary(
6529            Box::new(DataType::UInt8),
6530            Box::new(DataType::Struct(
6531                vec![Field::new("name", DataType::Utf8, true)].into(),
6532            )),
6533        );
6534        let err = cast(&source, &cast_type).unwrap_err().to_string();
6535        assert!(
6536            err.contains("Cannot fit") && err.contains("dictionary keys"),
6537            "expected key-overflow error, got: {err}"
6538        );
6539    }
6540
6541    #[test]
6542    fn test_cast_empty_string_array_to_dict_utf8_view() {
6543        let array = StringArray::from(Vec::<Option<&str>>::new());
6544
6545        let cast_type =
6546            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6547        assert!(can_cast_types(array.data_type(), &cast_type));
6548        let cast_array = cast(&array, &cast_type).unwrap();
6549        assert_eq!(cast_array.data_type(), &cast_type);
6550        assert_eq!(cast_array.len(), 0);
6551    }
6552
6553    #[test]
6554    fn test_cast_empty_binary_array_to_dict_binary_view() {
6555        let array = BinaryArray::from(Vec::<Option<&[u8]>>::new());
6556
6557        let cast_type =
6558            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6559        assert!(can_cast_types(array.data_type(), &cast_type));
6560        let cast_array = cast(&array, &cast_type).unwrap();
6561        assert_eq!(cast_array.data_type(), &cast_type);
6562        assert_eq!(cast_array.len(), 0);
6563    }
6564
6565    #[test]
6566    fn test_cast_all_null_string_array_to_dict_utf8_view() {
6567        let array = StringArray::from(vec![None::<&str>, None, None]);
6568
6569        let cast_type =
6570            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6571        assert!(can_cast_types(array.data_type(), &cast_type));
6572        let cast_array = cast(&array, &cast_type).unwrap();
6573        assert_eq!(cast_array.data_type(), &cast_type);
6574        assert_eq!(cast_array.null_count(), 3);
6575
6576        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6577        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6578        assert_eq!(dict_array.values().len(), 0);
6579        assert_eq!(dict_array.keys().null_count(), 3);
6580
6581        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6582        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6583        assert_eq!(actual, vec![None, None, None]);
6584    }
6585
6586    #[test]
6587    fn test_cast_all_null_binary_array_to_dict_binary_view() {
6588        let array = BinaryArray::from(vec![None::<&[u8]>, None, None]);
6589
6590        let cast_type =
6591            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6592        assert!(can_cast_types(array.data_type(), &cast_type));
6593        let cast_array = cast(&array, &cast_type).unwrap();
6594        assert_eq!(cast_array.data_type(), &cast_type);
6595        assert_eq!(cast_array.null_count(), 3);
6596
6597        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6598        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6599        assert_eq!(dict_array.values().len(), 0);
6600        assert_eq!(dict_array.keys().null_count(), 3);
6601
6602        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6603        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6604        assert_eq!(actual, vec![None, None, None]);
6605    }
6606
6607    #[test]
6608    fn test_numeric_to_binary() {
6609        let a = Int16Array::from(vec![Some(1), Some(511), None]);
6610
6611        let array_ref = cast(&a, &DataType::Binary).unwrap();
6612        let down_cast = array_ref.as_binary::<i32>();
6613        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6614        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6615        assert!(down_cast.is_null(2));
6616
6617        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6618
6619        let array_ref = cast(&a, &DataType::Binary).unwrap();
6620        let down_cast = array_ref.as_binary::<i32>();
6621        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6622        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6623        assert!(down_cast.is_null(2));
6624    }
6625
6626    #[test]
6627    fn test_numeric_to_large_binary() {
6628        let a = Int16Array::from(vec![Some(1), Some(511), None]);
6629
6630        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6631        let down_cast = array_ref.as_binary::<i64>();
6632        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6633        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6634        assert!(down_cast.is_null(2));
6635
6636        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6637
6638        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6639        let down_cast = array_ref.as_binary::<i64>();
6640        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6641        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6642        assert!(down_cast.is_null(2));
6643    }
6644
6645    #[test]
6646    fn test_cast_date32_to_int32() {
6647        let array = Date32Array::from(vec![10000, 17890]);
6648        let b = cast(&array, &DataType::Int32).unwrap();
6649        let c = b.as_primitive::<Int32Type>();
6650        assert_eq!(10000, c.value(0));
6651        assert_eq!(17890, c.value(1));
6652    }
6653
6654    #[test]
6655    fn test_cast_int32_to_date32() {
6656        let array = Int32Array::from(vec![10000, 17890]);
6657        let b = cast(&array, &DataType::Date32).unwrap();
6658        let c = b.as_primitive::<Date32Type>();
6659        assert_eq!(10000, c.value(0));
6660        assert_eq!(17890, c.value(1));
6661    }
6662
6663    #[test]
6664    fn test_cast_timestamp_to_date32() {
6665        let array =
6666            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6667                .with_timezone("+00:00".to_string());
6668        let b = cast(&array, &DataType::Date32).unwrap();
6669        let c = b.as_primitive::<Date32Type>();
6670        assert_eq!(10000, c.value(0));
6671        assert_eq!(17890, c.value(1));
6672        assert!(c.is_null(2));
6673    }
6674    #[test]
6675    fn test_cast_timestamp_to_date32_zone() {
6676        let strings = StringArray::from_iter([
6677            Some("1970-01-01T00:00:01"),
6678            Some("1970-01-01T23:59:59"),
6679            None,
6680            Some("2020-03-01T02:00:23+00:00"),
6681        ]);
6682        let dt = DataType::Timestamp(TimeUnit::Millisecond, Some("-07:00".into()));
6683        let timestamps = cast(&strings, &dt).unwrap();
6684        let dates = cast(timestamps.as_ref(), &DataType::Date32).unwrap();
6685
6686        let c = dates.as_primitive::<Date32Type>();
6687        let expected = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
6688        assert_eq!(c.value_as_date(0).unwrap(), expected);
6689        assert_eq!(c.value_as_date(1).unwrap(), expected);
6690        assert!(c.is_null(2));
6691        let expected = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
6692        assert_eq!(c.value_as_date(3).unwrap(), expected);
6693    }
6694    #[test]
6695    fn test_cast_timestamp_to_date64() {
6696        let array =
6697            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None]);
6698        let b = cast(&array, &DataType::Date64).unwrap();
6699        let c = b.as_primitive::<Date64Type>();
6700        assert_eq!(864000000005, c.value(0));
6701        assert_eq!(1545696000001, c.value(1));
6702        assert!(c.is_null(2));
6703
6704        let array = TimestampSecondArray::from(vec![Some(864000000005), Some(1545696000001)]);
6705        let b = cast(&array, &DataType::Date64).unwrap();
6706        let c = b.as_primitive::<Date64Type>();
6707        assert_eq!(864000000005000, c.value(0));
6708        assert_eq!(1545696000001000, c.value(1));
6709
6710        // test overflow, safe cast
6711        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6712        let b = cast(&array, &DataType::Date64).unwrap();
6713        assert!(b.is_null(0));
6714        // test overflow, unsafe cast
6715        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6716        let options = CastOptions {
6717            safe: false,
6718            format_options: FormatOptions::default(),
6719        };
6720        let b = cast_with_options(&array, &DataType::Date64, &options);
6721        assert!(b.is_err());
6722    }
6723
6724    #[test]
6725    fn test_cast_timestamp_to_time64() {
6726        // test timestamp secs
6727        let array = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6728            .with_timezone("+01:00".to_string());
6729        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6730        let c = b.as_primitive::<Time64MicrosecondType>();
6731        assert_eq!(3605000000, c.value(0));
6732        assert_eq!(3601000000, c.value(1));
6733        assert!(c.is_null(2));
6734        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6735        let c = b.as_primitive::<Time64NanosecondType>();
6736        assert_eq!(3605000000000, c.value(0));
6737        assert_eq!(3601000000000, c.value(1));
6738        assert!(c.is_null(2));
6739
6740        // test timestamp milliseconds
6741        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6742            .with_timezone("+01:00".to_string());
6743        let array = Arc::new(a) as ArrayRef;
6744        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6745        let c = b.as_primitive::<Time64MicrosecondType>();
6746        assert_eq!(3605000000, c.value(0));
6747        assert_eq!(3601000000, c.value(1));
6748        assert!(c.is_null(2));
6749        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6750        let c = b.as_primitive::<Time64NanosecondType>();
6751        assert_eq!(3605000000000, c.value(0));
6752        assert_eq!(3601000000000, c.value(1));
6753        assert!(c.is_null(2));
6754
6755        // test timestamp microseconds
6756        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6757            .with_timezone("+01:00".to_string());
6758        let array = Arc::new(a) as ArrayRef;
6759        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6760        let c = b.as_primitive::<Time64MicrosecondType>();
6761        assert_eq!(3605000000, c.value(0));
6762        assert_eq!(3601000000, c.value(1));
6763        assert!(c.is_null(2));
6764        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6765        let c = b.as_primitive::<Time64NanosecondType>();
6766        assert_eq!(3605000000000, c.value(0));
6767        assert_eq!(3601000000000, c.value(1));
6768        assert!(c.is_null(2));
6769
6770        // test timestamp nanoseconds
6771        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
6772            .with_timezone("+01:00".to_string());
6773        let array = Arc::new(a) as ArrayRef;
6774        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6775        let c = b.as_primitive::<Time64MicrosecondType>();
6776        assert_eq!(3605000000, c.value(0));
6777        assert_eq!(3601000000, c.value(1));
6778        assert!(c.is_null(2));
6779        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6780        let c = b.as_primitive::<Time64NanosecondType>();
6781        assert_eq!(3605000000000, c.value(0));
6782        assert_eq!(3601000000000, c.value(1));
6783        assert!(c.is_null(2));
6784
6785        // test overflow
6786        let a =
6787            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6788        let array = Arc::new(a) as ArrayRef;
6789        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond));
6790        assert!(b.is_err());
6791        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond));
6792        assert!(b.is_err());
6793        let b = cast(&array, &DataType::Time64(TimeUnit::Millisecond));
6794        assert!(b.is_err());
6795    }
6796
6797    #[test]
6798    fn test_cast_timestamp_to_time32() {
6799        // test timestamp secs
6800        let a = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6801            .with_timezone("+01:00".to_string());
6802        let array = Arc::new(a) as ArrayRef;
6803        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6804        let c = b.as_primitive::<Time32SecondType>();
6805        assert_eq!(3605, c.value(0));
6806        assert_eq!(3601, c.value(1));
6807        assert!(c.is_null(2));
6808        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6809        let c = b.as_primitive::<Time32MillisecondType>();
6810        assert_eq!(3605000, c.value(0));
6811        assert_eq!(3601000, c.value(1));
6812        assert!(c.is_null(2));
6813
6814        // test timestamp milliseconds
6815        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6816            .with_timezone("+01:00".to_string());
6817        let array = Arc::new(a) as ArrayRef;
6818        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6819        let c = b.as_primitive::<Time32SecondType>();
6820        assert_eq!(3605, c.value(0));
6821        assert_eq!(3601, c.value(1));
6822        assert!(c.is_null(2));
6823        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6824        let c = b.as_primitive::<Time32MillisecondType>();
6825        assert_eq!(3605000, c.value(0));
6826        assert_eq!(3601000, c.value(1));
6827        assert!(c.is_null(2));
6828
6829        // test timestamp microseconds
6830        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6831            .with_timezone("+01:00".to_string());
6832        let array = Arc::new(a) as ArrayRef;
6833        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6834        let c = b.as_primitive::<Time32SecondType>();
6835        assert_eq!(3605, c.value(0));
6836        assert_eq!(3601, c.value(1));
6837        assert!(c.is_null(2));
6838        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6839        let c = b.as_primitive::<Time32MillisecondType>();
6840        assert_eq!(3605000, c.value(0));
6841        assert_eq!(3601000, c.value(1));
6842        assert!(c.is_null(2));
6843
6844        // test timestamp nanoseconds
6845        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
6846            .with_timezone("+01:00".to_string());
6847        let array = Arc::new(a) as ArrayRef;
6848        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6849        let c = b.as_primitive::<Time32SecondType>();
6850        assert_eq!(3605, c.value(0));
6851        assert_eq!(3601, c.value(1));
6852        assert!(c.is_null(2));
6853        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6854        let c = b.as_primitive::<Time32MillisecondType>();
6855        assert_eq!(3605000, c.value(0));
6856        assert_eq!(3601000, c.value(1));
6857        assert!(c.is_null(2));
6858
6859        // test overflow
6860        let a =
6861            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6862        let array = Arc::new(a) as ArrayRef;
6863        let b = cast(&array, &DataType::Time32(TimeUnit::Second));
6864        assert!(b.is_err());
6865        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond));
6866        assert!(b.is_err());
6867    }
6868
6869    // Cast Timestamp(_, None) -> Timestamp(_, Some(timezone))
6870    #[test]
6871    fn test_cast_timestamp_with_timezone_1() {
6872        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6873            Some("2000-01-01T00:00:00.123456789"),
6874            Some("2010-01-01T00:00:00.123456789"),
6875            None,
6876        ]));
6877        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6878        let timestamp_array = cast(&string_array, &to_type).unwrap();
6879
6880        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6881        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6882
6883        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6884        let result = string_array.as_string::<i32>();
6885        assert_eq!("2000-01-01T00:00:00.123456+07:00", result.value(0));
6886        assert_eq!("2010-01-01T00:00:00.123456+07:00", result.value(1));
6887        assert!(result.is_null(2));
6888    }
6889
6890    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, None)
6891    #[test]
6892    fn test_cast_timestamp_with_timezone_2() {
6893        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6894            Some("2000-01-01T07:00:00.123456789"),
6895            Some("2010-01-01T07:00:00.123456789"),
6896            None,
6897        ]));
6898        let to_type = DataType::Timestamp(TimeUnit::Millisecond, Some("+0700".into()));
6899        let timestamp_array = cast(&string_array, &to_type).unwrap();
6900
6901        // Check intermediate representation is correct
6902        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6903        let result = string_array.as_string::<i32>();
6904        assert_eq!("2000-01-01T07:00:00.123+07:00", result.value(0));
6905        assert_eq!("2010-01-01T07:00:00.123+07:00", result.value(1));
6906        assert!(result.is_null(2));
6907
6908        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6909        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6910
6911        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6912        let result = string_array.as_string::<i32>();
6913        assert_eq!("2000-01-01T00:00:00.123", result.value(0));
6914        assert_eq!("2010-01-01T00:00:00.123", result.value(1));
6915        assert!(result.is_null(2));
6916    }
6917
6918    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, Some(timezone))
6919    #[test]
6920    fn test_cast_timestamp_with_timezone_3() {
6921        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6922            Some("2000-01-01T07:00:00.123456789"),
6923            Some("2010-01-01T07:00:00.123456789"),
6924            None,
6925        ]));
6926        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6927        let timestamp_array = cast(&string_array, &to_type).unwrap();
6928
6929        // Check intermediate representation is correct
6930        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6931        let result = string_array.as_string::<i32>();
6932        assert_eq!("2000-01-01T07:00:00.123456+07:00", result.value(0));
6933        assert_eq!("2010-01-01T07:00:00.123456+07:00", result.value(1));
6934        assert!(result.is_null(2));
6935
6936        let to_type = DataType::Timestamp(TimeUnit::Second, Some("-08:00".into()));
6937        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6938
6939        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6940        let result = string_array.as_string::<i32>();
6941        assert_eq!("1999-12-31T16:00:00-08:00", result.value(0));
6942        assert_eq!("2009-12-31T16:00:00-08:00", result.value(1));
6943        assert!(result.is_null(2));
6944    }
6945
6946    #[test]
6947    fn test_cast_date64_to_timestamp() {
6948        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6949        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6950        let c = b.as_primitive::<TimestampSecondType>();
6951        assert_eq!(864000000, c.value(0));
6952        assert_eq!(1545696000, c.value(1));
6953        assert!(c.is_null(2));
6954    }
6955
6956    #[test]
6957    fn test_cast_date64_to_timestamp_ms() {
6958        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6959        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
6960        let c = b
6961            .as_any()
6962            .downcast_ref::<TimestampMillisecondArray>()
6963            .unwrap();
6964        assert_eq!(864000000005, c.value(0));
6965        assert_eq!(1545696000001, c.value(1));
6966        assert!(c.is_null(2));
6967    }
6968
6969    #[test]
6970    fn test_cast_date64_to_timestamp_us() {
6971        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6972        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
6973        let c = b
6974            .as_any()
6975            .downcast_ref::<TimestampMicrosecondArray>()
6976            .unwrap();
6977        assert_eq!(864000000005000, c.value(0));
6978        assert_eq!(1545696000001000, c.value(1));
6979        assert!(c.is_null(2));
6980    }
6981
6982    #[test]
6983    fn test_cast_date64_to_timestamp_ns() {
6984        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6985        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
6986        let c = b
6987            .as_any()
6988            .downcast_ref::<TimestampNanosecondArray>()
6989            .unwrap();
6990        assert_eq!(864000000005000000, c.value(0));
6991        assert_eq!(1545696000001000000, c.value(1));
6992        assert!(c.is_null(2));
6993    }
6994
6995    #[test]
6996    fn test_cast_timestamp_to_i64() {
6997        let array =
6998            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6999                .with_timezone("UTC".to_string());
7000        let b = cast(&array, &DataType::Int64).unwrap();
7001        let c = b.as_primitive::<Int64Type>();
7002        assert_eq!(&DataType::Int64, c.data_type());
7003        assert_eq!(864000000005, c.value(0));
7004        assert_eq!(1545696000001, c.value(1));
7005        assert!(c.is_null(2));
7006    }
7007
7008    macro_rules! assert_cast {
7009        ($array:expr, $datatype:expr, $output_array_type: ty, $expected:expr) => {{
7010            assert!(can_cast_types($array.data_type(), &$datatype));
7011            let out = cast(&$array, &$datatype).unwrap();
7012            let actual = out
7013                .as_any()
7014                .downcast_ref::<$output_array_type>()
7015                .unwrap()
7016                .into_iter()
7017                .collect::<Vec<_>>();
7018            assert_eq!(actual, $expected);
7019        }};
7020        ($array:expr, $datatype:expr, $output_array_type: ty, $options:expr, $expected:expr) => {{
7021            assert!(can_cast_types($array.data_type(), &$datatype));
7022            let out = cast_with_options(&$array, &$datatype, &$options).unwrap();
7023            let actual = out
7024                .as_any()
7025                .downcast_ref::<$output_array_type>()
7026                .unwrap()
7027                .into_iter()
7028                .collect::<Vec<_>>();
7029            assert_eq!(actual, $expected);
7030        }};
7031    }
7032
7033    #[test]
7034    fn test_cast_date32_to_string() {
7035        let array = Date32Array::from(vec![Some(0), Some(10000), Some(13036), Some(17890), None]);
7036        let expected = vec![
7037            Some("1970-01-01"),
7038            Some("1997-05-19"),
7039            Some("2005-09-10"),
7040            Some("2018-12-25"),
7041            None,
7042        ];
7043
7044        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
7045        assert_cast!(array, DataType::Utf8, StringArray, expected);
7046        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
7047    }
7048
7049    #[test]
7050    fn test_cast_date64_to_string() {
7051        let array = Date64Array::from(vec![
7052            Some(0),
7053            Some(10000 * 86400000),
7054            Some(13036 * 86400000),
7055            Some(17890 * 86400000),
7056            None,
7057        ]);
7058        let expected = vec![
7059            Some("1970-01-01T00:00:00"),
7060            Some("1997-05-19T00:00:00"),
7061            Some("2005-09-10T00:00:00"),
7062            Some("2018-12-25T00:00:00"),
7063            None,
7064        ];
7065
7066        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
7067        assert_cast!(array, DataType::Utf8, StringArray, expected);
7068        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
7069    }
7070
7071    #[test]
7072    fn test_cast_date32_to_timestamp_and_timestamp_with_timezone() {
7073        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7074        let a = Date32Array::from(vec![Some(18628), None, None]); // 2021-1-1, 2022-1-1
7075        let array = Arc::new(a) as ArrayRef;
7076
7077        let b = cast(
7078            &array,
7079            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
7080        )
7081        .unwrap();
7082        let c = b.as_primitive::<TimestampSecondType>();
7083        let string_array = cast(&c, &DataType::Utf8).unwrap();
7084        let result = string_array.as_string::<i32>();
7085        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7086
7087        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
7088        let c = b.as_primitive::<TimestampSecondType>();
7089        let string_array = cast(&c, &DataType::Utf8).unwrap();
7090        let result = string_array.as_string::<i32>();
7091        assert_eq!("2021-01-01T00:00:00", result.value(0));
7092    }
7093
7094    #[test]
7095    fn test_cast_date32_to_timestamp_with_timezone() {
7096        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7097        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7098        let array = Arc::new(a) as ArrayRef;
7099        let b = cast(
7100            &array,
7101            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
7102        )
7103        .unwrap();
7104        let c = b.as_primitive::<TimestampSecondType>();
7105        assert_eq!(1609438500, c.value(0));
7106        assert_eq!(1640974500, c.value(1));
7107        assert!(c.is_null(2));
7108
7109        let string_array = cast(&c, &DataType::Utf8).unwrap();
7110        let result = string_array.as_string::<i32>();
7111        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7112        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7113    }
7114
7115    #[test]
7116    fn test_cast_date32_to_timestamp_with_timezone_ms() {
7117        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7118        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7119        let array = Arc::new(a) as ArrayRef;
7120        let b = cast(
7121            &array,
7122            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
7123        )
7124        .unwrap();
7125        let c = b.as_primitive::<TimestampMillisecondType>();
7126        assert_eq!(1609438500000, c.value(0));
7127        assert_eq!(1640974500000, c.value(1));
7128        assert!(c.is_null(2));
7129
7130        let string_array = cast(&c, &DataType::Utf8).unwrap();
7131        let result = string_array.as_string::<i32>();
7132        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7133        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7134    }
7135
7136    #[test]
7137    fn test_cast_date32_to_timestamp_with_timezone_us() {
7138        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7139        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7140        let array = Arc::new(a) as ArrayRef;
7141        let b = cast(
7142            &array,
7143            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
7144        )
7145        .unwrap();
7146        let c = b.as_primitive::<TimestampMicrosecondType>();
7147        assert_eq!(1609438500000000, c.value(0));
7148        assert_eq!(1640974500000000, c.value(1));
7149        assert!(c.is_null(2));
7150
7151        let string_array = cast(&c, &DataType::Utf8).unwrap();
7152        let result = string_array.as_string::<i32>();
7153        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7154        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7155    }
7156
7157    #[test]
7158    fn test_cast_date32_to_timestamp_with_timezone_ns() {
7159        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7160        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7161        let array = Arc::new(a) as ArrayRef;
7162        let b = cast(
7163            &array,
7164            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
7165        )
7166        .unwrap();
7167        let c = b.as_primitive::<TimestampNanosecondType>();
7168        assert_eq!(1609438500000000000, c.value(0));
7169        assert_eq!(1640974500000000000, c.value(1));
7170        assert!(c.is_null(2));
7171
7172        let string_array = cast(&c, &DataType::Utf8).unwrap();
7173        let result = string_array.as_string::<i32>();
7174        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7175        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7176    }
7177
7178    #[test]
7179    fn test_cast_date64_to_timestamp_with_timezone() {
7180        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7181        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7182        let b = cast(
7183            &array,
7184            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
7185        )
7186        .unwrap();
7187
7188        let c = b.as_primitive::<TimestampSecondType>();
7189        assert_eq!(863979300, c.value(0));
7190        assert_eq!(1545675300, c.value(1));
7191        assert!(c.is_null(2));
7192
7193        let string_array = cast(&c, &DataType::Utf8).unwrap();
7194        let result = string_array.as_string::<i32>();
7195        assert_eq!("1997-05-19T00:00:00+05:45", result.value(0));
7196        assert_eq!("2018-12-25T00:00:00+05:45", result.value(1));
7197    }
7198
7199    #[test]
7200    fn test_cast_date64_to_timestamp_with_timezone_ms() {
7201        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7202        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7203        let b = cast(
7204            &array,
7205            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
7206        )
7207        .unwrap();
7208
7209        let c = b.as_primitive::<TimestampMillisecondType>();
7210        assert_eq!(863979300005, c.value(0));
7211        assert_eq!(1545675300001, c.value(1));
7212        assert!(c.is_null(2));
7213
7214        let string_array = cast(&c, &DataType::Utf8).unwrap();
7215        let result = string_array.as_string::<i32>();
7216        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
7217        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7218    }
7219
7220    #[test]
7221    fn test_cast_date64_to_timestamp_with_timezone_us() {
7222        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7223        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7224        let b = cast(
7225            &array,
7226            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
7227        )
7228        .unwrap();
7229
7230        let c = b.as_primitive::<TimestampMicrosecondType>();
7231        assert_eq!(863979300005000, c.value(0));
7232        assert_eq!(1545675300001000, c.value(1));
7233        assert!(c.is_null(2));
7234
7235        let string_array = cast(&c, &DataType::Utf8).unwrap();
7236        let result = string_array.as_string::<i32>();
7237        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
7238        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7239    }
7240
7241    #[test]
7242    fn test_cast_date64_to_timestamp_with_timezone_ns() {
7243        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7244        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7245        let b = cast(
7246            &array,
7247            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
7248        )
7249        .unwrap();
7250
7251        let c = b.as_primitive::<TimestampNanosecondType>();
7252        assert_eq!(863979300005000000, c.value(0));
7253        assert_eq!(1545675300001000000, c.value(1));
7254        assert!(c.is_null(2));
7255
7256        let string_array = cast(&c, &DataType::Utf8).unwrap();
7257        let result = string_array.as_string::<i32>();
7258        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
7259        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7260    }
7261
7262    #[test]
7263    fn test_cast_timestamp_to_strings() {
7264        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
7265        let array =
7266            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7267        let expected = vec![
7268            Some("1997-05-19T00:00:03.005"),
7269            Some("2018-12-25T00:00:02.001"),
7270            None,
7271        ];
7272
7273        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
7274        assert_cast!(array, DataType::Utf8, StringArray, expected);
7275        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
7276    }
7277
7278    #[test]
7279    fn test_cast_timestamp_to_strings_opt() {
7280        let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
7281        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7282        let cast_options = CastOptions {
7283            safe: true,
7284            format_options: FormatOptions::default()
7285                .with_timestamp_format(Some(ts_format))
7286                .with_timestamp_tz_format(Some(ts_format)),
7287        };
7288
7289        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
7290        let array_without_tz =
7291            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7292        let expected = vec![
7293            Some("1997-05-19 00:00:03.005000"),
7294            Some("2018-12-25 00:00:02.001000"),
7295            None,
7296        ];
7297        assert_cast!(
7298            array_without_tz,
7299            DataType::Utf8View,
7300            StringViewArray,
7301            cast_options,
7302            expected
7303        );
7304        assert_cast!(
7305            array_without_tz,
7306            DataType::Utf8,
7307            StringArray,
7308            cast_options,
7309            expected
7310        );
7311        assert_cast!(
7312            array_without_tz,
7313            DataType::LargeUtf8,
7314            LargeStringArray,
7315            cast_options,
7316            expected
7317        );
7318
7319        let array_with_tz =
7320            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None])
7321                .with_timezone(tz.to_string());
7322        let expected = vec![
7323            Some("1997-05-19 05:45:03.005000"),
7324            Some("2018-12-25 05:45:02.001000"),
7325            None,
7326        ];
7327        assert_cast!(
7328            array_with_tz,
7329            DataType::Utf8View,
7330            StringViewArray,
7331            cast_options,
7332            expected
7333        );
7334        assert_cast!(
7335            array_with_tz,
7336            DataType::Utf8,
7337            StringArray,
7338            cast_options,
7339            expected
7340        );
7341        assert_cast!(
7342            array_with_tz,
7343            DataType::LargeUtf8,
7344            LargeStringArray,
7345            cast_options,
7346            expected
7347        );
7348    }
7349
7350    #[test]
7351    fn test_cast_between_timestamps() {
7352        let array =
7353            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7354        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
7355        let c = b.as_primitive::<TimestampSecondType>();
7356        assert_eq!(864000003, c.value(0));
7357        assert_eq!(1545696002, c.value(1));
7358        assert!(c.is_null(2));
7359    }
7360
7361    #[test]
7362    fn test_cast_duration_to_i64() {
7363        let base = vec![5, 6, 7, 8, 100000000];
7364
7365        let duration_arrays = vec![
7366            Arc::new(DurationNanosecondArray::from(base.clone())) as ArrayRef,
7367            Arc::new(DurationMicrosecondArray::from(base.clone())) as ArrayRef,
7368            Arc::new(DurationMillisecondArray::from(base.clone())) as ArrayRef,
7369            Arc::new(DurationSecondArray::from(base.clone())) as ArrayRef,
7370        ];
7371
7372        for arr in duration_arrays {
7373            assert!(can_cast_types(arr.data_type(), &DataType::Int64));
7374            let result = cast(&arr, &DataType::Int64).unwrap();
7375            let result = result.as_primitive::<Int64Type>();
7376            assert_eq!(base.as_slice(), result.values());
7377        }
7378    }
7379
7380    #[test]
7381    fn test_cast_between_durations_and_numerics() {
7382        fn test_cast_between_durations<FromType, ToType>()
7383        where
7384            FromType: ArrowPrimitiveType<Native = i64>,
7385            ToType: ArrowPrimitiveType<Native = i64>,
7386            PrimitiveArray<FromType>: From<Vec<Option<i64>>>,
7387        {
7388            let DataType::Duration(from_unit) = FromType::DATA_TYPE else {
7389                panic!("Expected a duration type")
7390            };
7391            let DataType::Duration(to_unit) = ToType::DATA_TYPE else {
7392                panic!("Expected a duration type")
7393            };
7394            let from_size = time_unit_multiple(&from_unit);
7395            let to_size = time_unit_multiple(&to_unit);
7396
7397            let (v1_before, v2_before) = (8640003005, 1696002001);
7398            let (v1_after, v2_after) = if from_size >= to_size {
7399                (
7400                    v1_before / (from_size / to_size),
7401                    v2_before / (from_size / to_size),
7402                )
7403            } else {
7404                (
7405                    v1_before * (to_size / from_size),
7406                    v2_before * (to_size / from_size),
7407                )
7408            };
7409
7410            let array =
7411                PrimitiveArray::<FromType>::from(vec![Some(v1_before), Some(v2_before), None]);
7412            let b = cast(&array, &ToType::DATA_TYPE).unwrap();
7413            let c = b.as_primitive::<ToType>();
7414            assert_eq!(v1_after, c.value(0));
7415            assert_eq!(v2_after, c.value(1));
7416            assert!(c.is_null(2));
7417        }
7418
7419        // between each individual duration type
7420        test_cast_between_durations::<DurationSecondType, DurationMillisecondType>();
7421        test_cast_between_durations::<DurationSecondType, DurationMicrosecondType>();
7422        test_cast_between_durations::<DurationSecondType, DurationNanosecondType>();
7423        test_cast_between_durations::<DurationMillisecondType, DurationSecondType>();
7424        test_cast_between_durations::<DurationMillisecondType, DurationMicrosecondType>();
7425        test_cast_between_durations::<DurationMillisecondType, DurationNanosecondType>();
7426        test_cast_between_durations::<DurationMicrosecondType, DurationSecondType>();
7427        test_cast_between_durations::<DurationMicrosecondType, DurationMillisecondType>();
7428        test_cast_between_durations::<DurationMicrosecondType, DurationNanosecondType>();
7429        test_cast_between_durations::<DurationNanosecondType, DurationSecondType>();
7430        test_cast_between_durations::<DurationNanosecondType, DurationMillisecondType>();
7431        test_cast_between_durations::<DurationNanosecondType, DurationMicrosecondType>();
7432
7433        // cast failed
7434        let array = DurationSecondArray::from(vec![
7435            Some(i64::MAX),
7436            Some(8640203410378005),
7437            Some(10241096),
7438            None,
7439        ]);
7440        let b = cast(&array, &DataType::Duration(TimeUnit::Nanosecond)).unwrap();
7441        let c = b.as_primitive::<DurationNanosecondType>();
7442        assert!(c.is_null(0));
7443        assert!(c.is_null(1));
7444        assert_eq!(10241096000000000, c.value(2));
7445        assert!(c.is_null(3));
7446
7447        // durations to numerics
7448        let array = DurationSecondArray::from(vec![
7449            Some(i64::MAX),
7450            Some(8640203410378005),
7451            Some(10241096),
7452            None,
7453        ]);
7454        let b = cast(&array, &DataType::Int64).unwrap();
7455        let c = b.as_primitive::<Int64Type>();
7456        assert_eq!(i64::MAX, c.value(0));
7457        assert_eq!(8640203410378005, c.value(1));
7458        assert_eq!(10241096, c.value(2));
7459        assert!(c.is_null(3));
7460
7461        let b = cast(&array, &DataType::Int32).unwrap();
7462        let c = b.as_primitive::<Int32Type>();
7463        assert_eq!(0, c.value(0));
7464        assert_eq!(0, c.value(1));
7465        assert_eq!(10241096, c.value(2));
7466        assert!(c.is_null(3));
7467
7468        // numerics to durations
7469        let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), Some(10241096), None]);
7470        let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
7471        let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
7472        assert_eq!(i32::MAX as i64, c.value(0));
7473        assert_eq!(802034103, c.value(1));
7474        assert_eq!(10241096, c.value(2));
7475        assert!(c.is_null(3));
7476    }
7477
7478    #[test]
7479    fn test_cast_to_strings() {
7480        let a = Int32Array::from(vec![1, 2, 3]);
7481        let out = cast(&a, &DataType::Utf8).unwrap();
7482        let out = out
7483            .as_any()
7484            .downcast_ref::<StringArray>()
7485            .unwrap()
7486            .into_iter()
7487            .collect::<Vec<_>>();
7488        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
7489        let out = cast(&a, &DataType::LargeUtf8).unwrap();
7490        let out = out
7491            .as_any()
7492            .downcast_ref::<LargeStringArray>()
7493            .unwrap()
7494            .into_iter()
7495            .collect::<Vec<_>>();
7496        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
7497    }
7498
7499    #[test]
7500    fn test_str_to_str_casts() {
7501        for data in [
7502            vec![Some("foo"), Some("bar"), Some("ham")],
7503            vec![Some("foo"), None, Some("bar")],
7504        ] {
7505            let a = LargeStringArray::from(data.clone());
7506            let to = cast(&a, &DataType::Utf8).unwrap();
7507            let expect = a
7508                .as_any()
7509                .downcast_ref::<LargeStringArray>()
7510                .unwrap()
7511                .into_iter()
7512                .collect::<Vec<_>>();
7513            let out = to
7514                .as_any()
7515                .downcast_ref::<StringArray>()
7516                .unwrap()
7517                .into_iter()
7518                .collect::<Vec<_>>();
7519            assert_eq!(expect, out);
7520
7521            let a = StringArray::from(data);
7522            let to = cast(&a, &DataType::LargeUtf8).unwrap();
7523            let expect = a
7524                .as_any()
7525                .downcast_ref::<StringArray>()
7526                .unwrap()
7527                .into_iter()
7528                .collect::<Vec<_>>();
7529            let out = to
7530                .as_any()
7531                .downcast_ref::<LargeStringArray>()
7532                .unwrap()
7533                .into_iter()
7534                .collect::<Vec<_>>();
7535            assert_eq!(expect, out);
7536        }
7537    }
7538
7539    const VIEW_TEST_DATA: [Option<&str>; 5] = [
7540        Some("hello"),
7541        Some("repeated"),
7542        None,
7543        Some("large payload over 12 bytes"),
7544        Some("repeated"),
7545    ];
7546
7547    #[test]
7548    fn test_string_view_to_binary_view() {
7549        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7550
7551        assert!(can_cast_types(
7552            string_view_array.data_type(),
7553            &DataType::BinaryView
7554        ));
7555
7556        let binary_view_array = cast(&string_view_array, &DataType::BinaryView).unwrap();
7557        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7558
7559        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7560        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7561    }
7562
7563    #[test]
7564    fn test_binary_view_to_string_view() {
7565        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7566
7567        assert!(can_cast_types(
7568            binary_view_array.data_type(),
7569            &DataType::Utf8View
7570        ));
7571
7572        let string_view_array = cast(&binary_view_array, &DataType::Utf8View).unwrap();
7573        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7574
7575        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7576        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7577    }
7578
7579    #[test]
7580    fn test_binary_view_to_string_view_with_invalid_utf8() {
7581        let binary_view_array = BinaryViewArray::from_iter(vec![
7582            Some(b"valid".as_slice()),
7583            Some(&[0xff]),
7584            Some(b"utf8".as_slice()),
7585            None,
7586        ]);
7587
7588        let strict_options = CastOptions {
7589            safe: false,
7590            ..Default::default()
7591        };
7592
7593        assert!(
7594            cast_with_options(&binary_view_array, &DataType::Utf8View, &strict_options).is_err()
7595        );
7596
7597        let safe_options = CastOptions {
7598            safe: true,
7599            ..Default::default()
7600        };
7601
7602        let string_view_array =
7603            cast_with_options(&binary_view_array, &DataType::Utf8View, &safe_options).unwrap();
7604        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7605
7606        let values: Vec<_> = string_view_array.as_string_view().iter().collect();
7607
7608        assert_eq!(values, vec![Some("valid"), None, Some("utf8"), None]);
7609    }
7610
7611    #[test]
7612    fn test_string_to_view() {
7613        _test_string_to_view::<i32>();
7614        _test_string_to_view::<i64>();
7615    }
7616
7617    fn _test_string_to_view<O>()
7618    where
7619        O: OffsetSizeTrait,
7620    {
7621        let string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7622
7623        assert!(can_cast_types(
7624            string_array.data_type(),
7625            &DataType::Utf8View
7626        ));
7627
7628        assert!(can_cast_types(
7629            string_array.data_type(),
7630            &DataType::BinaryView
7631        ));
7632
7633        let string_view_array = cast(&string_array, &DataType::Utf8View).unwrap();
7634        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7635
7636        let binary_view_array = cast(&string_array, &DataType::BinaryView).unwrap();
7637        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7638
7639        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7640        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7641
7642        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7643        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7644    }
7645
7646    #[test]
7647    fn test_bianry_to_view() {
7648        _test_binary_to_view::<i32>();
7649        _test_binary_to_view::<i64>();
7650    }
7651
7652    fn _test_binary_to_view<O>()
7653    where
7654        O: OffsetSizeTrait,
7655    {
7656        let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7657
7658        assert!(can_cast_types(
7659            binary_array.data_type(),
7660            &DataType::Utf8View
7661        ));
7662
7663        assert!(can_cast_types(
7664            binary_array.data_type(),
7665            &DataType::BinaryView
7666        ));
7667
7668        let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
7669        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7670
7671        let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
7672        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7673
7674        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7675        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7676
7677        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7678        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7679    }
7680
7681    #[test]
7682    fn test_dict_to_view() {
7683        let values = StringArray::from_iter(VIEW_TEST_DATA);
7684        let keys = Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
7685        let string_dict_array =
7686            DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
7687        let typed_dict = string_dict_array.downcast_dict::<StringArray>().unwrap();
7688
7689        let string_view_array = {
7690            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7691            for v in typed_dict {
7692                builder.append_option(v);
7693            }
7694            builder.finish()
7695        };
7696        let expected_string_array_type = string_view_array.data_type();
7697        let casted_string_array = cast(&string_dict_array, expected_string_array_type).unwrap();
7698        assert_eq!(casted_string_array.data_type(), expected_string_array_type);
7699        assert_eq!(casted_string_array.as_ref(), &string_view_array);
7700
7701        let binary_buffer = cast(&typed_dict.values(), &DataType::Binary).unwrap();
7702        let binary_dict_array =
7703            DictionaryArray::<Int8Type>::new(typed_dict.keys().clone(), binary_buffer);
7704        let typed_binary_dict = binary_dict_array.downcast_dict::<BinaryArray>().unwrap();
7705
7706        let binary_view_array = {
7707            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7708            for v in typed_binary_dict {
7709                builder.append_option(v);
7710            }
7711            builder.finish()
7712        };
7713        let expected_binary_array_type = binary_view_array.data_type();
7714        let casted_binary_array = cast(&binary_dict_array, expected_binary_array_type).unwrap();
7715        assert_eq!(casted_binary_array.data_type(), expected_binary_array_type);
7716        assert_eq!(casted_binary_array.as_ref(), &binary_view_array);
7717    }
7718
7719    #[test]
7720    fn test_dict_to_view_null_dictionary_value_is_null() {
7721        // Ensure we preserve nulls in the values
7722        let keys = Int32Array::from_iter([Some(0), Some(1), Some(2), None, Some(1)]);
7723
7724        let values = StringArray::from(vec![Some("aa"), None, Some("a value over twelve bytes")]);
7725        let dict = DictionaryArray::<Int32Type>::try_new(keys.clone(), Arc::new(values)).unwrap();
7726        let casted = cast(&dict, &DataType::Utf8View).unwrap();
7727        assert_eq!(
7728            casted.as_string_view().iter().collect::<Vec<_>>(),
7729            vec![
7730                Some("aa"),
7731                None,
7732                Some("a value over twelve bytes"),
7733                None,
7734                None
7735            ]
7736        );
7737        // the same input cast to Utf8 goes through `unpack_dictionary` and always agreed
7738        let reference = cast(&dict, &DataType::Utf8).unwrap();
7739        assert_eq!(
7740            casted.as_string_view().iter().collect::<Vec<_>>(),
7741            reference.as_string::<i32>().iter().collect::<Vec<_>>()
7742        );
7743
7744        let values = BinaryArray::from_opt_vec(vec![
7745            Some(b"aa".as_slice()),
7746            None,
7747            Some(b"a value over twelve bytes"),
7748        ]);
7749        let dict = DictionaryArray::<Int32Type>::try_new(keys, Arc::new(values)).unwrap();
7750        let casted = cast(&dict, &DataType::BinaryView).unwrap();
7751        assert_eq!(
7752            casted.as_binary_view().iter().collect::<Vec<_>>(),
7753            vec![
7754                Some(b"aa".as_slice()),
7755                None,
7756                Some(b"a value over twelve bytes"),
7757                None,
7758                None
7759            ]
7760        );
7761    }
7762
7763    #[test]
7764    fn test_view_to_dict() {
7765        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7766        let string_dict_array: DictionaryArray<Int8Type> = VIEW_TEST_DATA.into_iter().collect();
7767        let casted_type = string_dict_array.data_type();
7768        let casted_dict_array = cast(&string_view_array, casted_type).unwrap();
7769        assert_eq!(casted_dict_array.data_type(), casted_type);
7770        assert_eq!(casted_dict_array.as_ref(), &string_dict_array);
7771
7772        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7773        let binary_dict_array = string_dict_array.downcast_dict::<StringArray>().unwrap();
7774        let binary_buffer = cast(&binary_dict_array.values(), &DataType::Binary).unwrap();
7775        let binary_dict_array =
7776            DictionaryArray::<Int8Type>::new(binary_dict_array.keys().clone(), binary_buffer);
7777        let casted_type = binary_dict_array.data_type();
7778        let casted_binary_array = cast(&binary_view_array, casted_type).unwrap();
7779        assert_eq!(casted_binary_array.data_type(), casted_type);
7780        assert_eq!(casted_binary_array.as_ref(), &binary_dict_array);
7781    }
7782
7783    #[test]
7784    fn test_view_to_string() {
7785        _test_view_to_string::<i32>();
7786        _test_view_to_string::<i64>();
7787    }
7788
7789    fn _test_view_to_string<O>()
7790    where
7791        O: OffsetSizeTrait,
7792    {
7793        let string_view_array = {
7794            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7795            for s in &VIEW_TEST_DATA {
7796                builder.append_option(*s);
7797            }
7798            builder.finish()
7799        };
7800
7801        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7802
7803        let expected_string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7804        let expected_type = expected_string_array.data_type();
7805
7806        assert!(can_cast_types(string_view_array.data_type(), expected_type));
7807        assert!(can_cast_types(binary_view_array.data_type(), expected_type));
7808
7809        let string_view_casted_array = cast(&string_view_array, expected_type).unwrap();
7810        assert_eq!(string_view_casted_array.data_type(), expected_type);
7811        assert_eq!(string_view_casted_array.as_ref(), &expected_string_array);
7812
7813        let binary_view_casted_array = cast(&binary_view_array, expected_type).unwrap();
7814        assert_eq!(binary_view_casted_array.data_type(), expected_type);
7815        assert_eq!(binary_view_casted_array.as_ref(), &expected_string_array);
7816    }
7817
7818    #[test]
7819    fn test_view_to_binary() {
7820        _test_view_to_binary::<i32>();
7821        _test_view_to_binary::<i64>();
7822    }
7823
7824    fn _test_view_to_binary<O>()
7825    where
7826        O: OffsetSizeTrait,
7827    {
7828        let view_array = {
7829            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7830            for s in &VIEW_TEST_DATA {
7831                builder.append_option(*s);
7832            }
7833            builder.finish()
7834        };
7835
7836        let expected_binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7837        let expected_type = expected_binary_array.data_type();
7838
7839        assert!(can_cast_types(view_array.data_type(), expected_type));
7840
7841        let binary_array = cast(&view_array, expected_type).unwrap();
7842        assert_eq!(binary_array.data_type(), expected_type);
7843
7844        assert_eq!(binary_array.as_ref(), &expected_binary_array);
7845    }
7846
7847    #[test]
7848    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
7849    fn test_cast_from_f64() {
7850        let f64_values: Vec<f64> = vec![
7851            i64::MIN as f64,
7852            i32::MIN as f64,
7853            i16::MIN as f64,
7854            i8::MIN as f64,
7855            0_f64,
7856            u8::MAX as f64,
7857            u16::MAX as f64,
7858            u32::MAX as f64,
7859            u64::MAX as f64,
7860        ];
7861        let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values));
7862
7863        let f64_expected = vec![
7864            -9223372036854776000.0,
7865            -2147483648.0,
7866            -32768.0,
7867            -128.0,
7868            0.0,
7869            255.0,
7870            65535.0,
7871            4294967295.0,
7872            18446744073709552000.0,
7873        ];
7874        assert_eq!(
7875            f64_expected,
7876            get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
7877                .iter()
7878                .map(|i| i.parse::<f64>().unwrap())
7879                .collect::<Vec<f64>>()
7880        );
7881
7882        let f32_expected = vec![
7883            -9223372000000000000.0,
7884            -2147483600.0,
7885            -32768.0,
7886            -128.0,
7887            0.0,
7888            255.0,
7889            65535.0,
7890            4294967300.0,
7891            18446744000000000000.0,
7892        ];
7893        assert_eq!(
7894            f32_expected,
7895            get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
7896                .iter()
7897                .map(|i| i.parse::<f32>().unwrap())
7898                .collect::<Vec<f32>>()
7899        );
7900
7901        let f16_expected = vec![
7902            f16::from_f64(-9223372000000000000.0),
7903            f16::from_f64(-2147483600.0),
7904            f16::from_f64(-32768.0),
7905            f16::from_f64(-128.0),
7906            f16::from_f64(0.0),
7907            f16::from_f64(255.0),
7908            f16::from_f64(65535.0),
7909            f16::from_f64(4294967300.0),
7910            f16::from_f64(18446744000000000000.0),
7911        ];
7912        assert_eq!(
7913            f16_expected,
7914            get_cast_values::<Float16Type>(&f64_array, &DataType::Float16)
7915                .iter()
7916                .map(|i| i.parse::<f16>().unwrap())
7917                .collect::<Vec<f16>>()
7918        );
7919
7920        let i64_expected = vec![
7921            "-9223372036854775808",
7922            "-2147483648",
7923            "-32768",
7924            "-128",
7925            "0",
7926            "255",
7927            "65535",
7928            "4294967295",
7929            "null",
7930        ];
7931        assert_eq!(
7932            i64_expected,
7933            get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
7934        );
7935
7936        let i32_expected = vec![
7937            "null",
7938            "-2147483648",
7939            "-32768",
7940            "-128",
7941            "0",
7942            "255",
7943            "65535",
7944            "null",
7945            "null",
7946        ];
7947        assert_eq!(
7948            i32_expected,
7949            get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
7950        );
7951
7952        let i16_expected = vec![
7953            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7954        ];
7955        assert_eq!(
7956            i16_expected,
7957            get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
7958        );
7959
7960        let i8_expected = vec![
7961            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7962        ];
7963        assert_eq!(
7964            i8_expected,
7965            get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
7966        );
7967
7968        let u64_expected = vec![
7969            "null",
7970            "null",
7971            "null",
7972            "null",
7973            "0",
7974            "255",
7975            "65535",
7976            "4294967295",
7977            "null",
7978        ];
7979        assert_eq!(
7980            u64_expected,
7981            get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
7982        );
7983
7984        let u32_expected = vec![
7985            "null",
7986            "null",
7987            "null",
7988            "null",
7989            "0",
7990            "255",
7991            "65535",
7992            "4294967295",
7993            "null",
7994        ];
7995        assert_eq!(
7996            u32_expected,
7997            get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
7998        );
7999
8000        let u16_expected = vec![
8001            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
8002        ];
8003        assert_eq!(
8004            u16_expected,
8005            get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
8006        );
8007
8008        let u8_expected = vec![
8009            "null", "null", "null", "null", "0", "255", "null", "null", "null",
8010        ];
8011        assert_eq!(
8012            u8_expected,
8013            get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
8014        );
8015    }
8016
8017    #[test]
8018    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8019    fn test_cast_from_f32() {
8020        let f32_values: Vec<f32> = vec![
8021            i32::MIN as f32,
8022            i32::MIN as f32,
8023            i16::MIN as f32,
8024            i8::MIN as f32,
8025            0_f32,
8026            u8::MAX as f32,
8027            u16::MAX as f32,
8028            u32::MAX as f32,
8029            u32::MAX as f32,
8030        ];
8031        let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values));
8032
8033        let f64_expected = vec![
8034            "-2147483648.0",
8035            "-2147483648.0",
8036            "-32768.0",
8037            "-128.0",
8038            "0.0",
8039            "255.0",
8040            "65535.0",
8041            "4294967296.0",
8042            "4294967296.0",
8043        ];
8044        assert_eq!(
8045            f64_expected,
8046            get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
8047        );
8048
8049        let f32_expected = vec![
8050            "-2147483600.0",
8051            "-2147483600.0",
8052            "-32768.0",
8053            "-128.0",
8054            "0.0",
8055            "255.0",
8056            "65535.0",
8057            "4294967300.0",
8058            "4294967300.0",
8059        ];
8060        assert_eq!(
8061            f32_expected,
8062            get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
8063        );
8064
8065        let f16_expected = vec![
8066            "-inf", "-inf", "-32768.0", "-128.0", "0.0", "255.0", "inf", "inf", "inf",
8067        ];
8068        assert_eq!(
8069            f16_expected,
8070            get_cast_values::<Float16Type>(&f32_array, &DataType::Float16)
8071        );
8072
8073        let i64_expected = vec![
8074            "-2147483648",
8075            "-2147483648",
8076            "-32768",
8077            "-128",
8078            "0",
8079            "255",
8080            "65535",
8081            "4294967296",
8082            "4294967296",
8083        ];
8084        assert_eq!(
8085            i64_expected,
8086            get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
8087        );
8088
8089        let i32_expected = vec![
8090            "-2147483648",
8091            "-2147483648",
8092            "-32768",
8093            "-128",
8094            "0",
8095            "255",
8096            "65535",
8097            "null",
8098            "null",
8099        ];
8100        assert_eq!(
8101            i32_expected,
8102            get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
8103        );
8104
8105        let i16_expected = vec![
8106            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
8107        ];
8108        assert_eq!(
8109            i16_expected,
8110            get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
8111        );
8112
8113        let i8_expected = vec![
8114            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
8115        ];
8116        assert_eq!(
8117            i8_expected,
8118            get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
8119        );
8120
8121        let u64_expected = vec![
8122            "null",
8123            "null",
8124            "null",
8125            "null",
8126            "0",
8127            "255",
8128            "65535",
8129            "4294967296",
8130            "4294967296",
8131        ];
8132        assert_eq!(
8133            u64_expected,
8134            get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
8135        );
8136
8137        let u32_expected = vec![
8138            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
8139        ];
8140        assert_eq!(
8141            u32_expected,
8142            get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
8143        );
8144
8145        let u16_expected = vec![
8146            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
8147        ];
8148        assert_eq!(
8149            u16_expected,
8150            get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
8151        );
8152
8153        let u8_expected = vec![
8154            "null", "null", "null", "null", "0", "255", "null", "null", "null",
8155        ];
8156        assert_eq!(
8157            u8_expected,
8158            get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
8159        );
8160    }
8161
8162    #[test]
8163    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8164    fn test_cast_from_uint64() {
8165        let u64_values: Vec<u64> = vec![
8166            0,
8167            u8::MAX as u64,
8168            u16::MAX as u64,
8169            u32::MAX as u64,
8170            u64::MAX,
8171        ];
8172        let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values));
8173
8174        let f64_expected = vec![0.0, 255.0, 65535.0, 4294967295.0, 18446744073709552000.0];
8175        assert_eq!(
8176            f64_expected,
8177            get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
8178                .iter()
8179                .map(|i| i.parse::<f64>().unwrap())
8180                .collect::<Vec<f64>>()
8181        );
8182
8183        let f32_expected = vec![0.0, 255.0, 65535.0, 4294967300.0, 18446744000000000000.0];
8184        assert_eq!(
8185            f32_expected,
8186            get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
8187                .iter()
8188                .map(|i| i.parse::<f32>().unwrap())
8189                .collect::<Vec<f32>>()
8190        );
8191
8192        let f16_expected = vec![
8193            f16::from_f64(0.0),
8194            f16::from_f64(255.0),
8195            f16::from_f64(65535.0),
8196            f16::from_f64(4294967300.0),
8197            f16::from_f64(18446744000000000000.0),
8198        ];
8199        assert_eq!(
8200            f16_expected,
8201            get_cast_values::<Float16Type>(&u64_array, &DataType::Float16)
8202                .iter()
8203                .map(|i| i.parse::<f16>().unwrap())
8204                .collect::<Vec<f16>>()
8205        );
8206
8207        let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
8208        assert_eq!(
8209            i64_expected,
8210            get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
8211        );
8212
8213        let i32_expected = vec!["0", "255", "65535", "null", "null"];
8214        assert_eq!(
8215            i32_expected,
8216            get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
8217        );
8218
8219        let i16_expected = vec!["0", "255", "null", "null", "null"];
8220        assert_eq!(
8221            i16_expected,
8222            get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
8223        );
8224
8225        let i8_expected = vec!["0", "null", "null", "null", "null"];
8226        assert_eq!(
8227            i8_expected,
8228            get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
8229        );
8230
8231        let u64_expected = vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
8232        assert_eq!(
8233            u64_expected,
8234            get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
8235        );
8236
8237        let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
8238        assert_eq!(
8239            u32_expected,
8240            get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
8241        );
8242
8243        let u16_expected = vec!["0", "255", "65535", "null", "null"];
8244        assert_eq!(
8245            u16_expected,
8246            get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
8247        );
8248
8249        let u8_expected = vec!["0", "255", "null", "null", "null"];
8250        assert_eq!(
8251            u8_expected,
8252            get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
8253        );
8254    }
8255
8256    #[test]
8257    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8258    fn test_cast_from_uint32() {
8259        let u32_values: Vec<u32> = vec![0, u8::MAX as u32, u16::MAX as u32, u32::MAX];
8260        let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values));
8261
8262        let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
8263        assert_eq!(
8264            f64_expected,
8265            get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
8266        );
8267
8268        let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
8269        assert_eq!(
8270            f32_expected,
8271            get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
8272        );
8273
8274        let f16_expected = vec!["0.0", "255.0", "inf", "inf"];
8275        assert_eq!(
8276            f16_expected,
8277            get_cast_values::<Float16Type>(&u32_array, &DataType::Float16)
8278        );
8279
8280        let i64_expected = vec!["0", "255", "65535", "4294967295"];
8281        assert_eq!(
8282            i64_expected,
8283            get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
8284        );
8285
8286        let i32_expected = vec!["0", "255", "65535", "null"];
8287        assert_eq!(
8288            i32_expected,
8289            get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
8290        );
8291
8292        let i16_expected = vec!["0", "255", "null", "null"];
8293        assert_eq!(
8294            i16_expected,
8295            get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
8296        );
8297
8298        let i8_expected = vec!["0", "null", "null", "null"];
8299        assert_eq!(
8300            i8_expected,
8301            get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
8302        );
8303
8304        let u64_expected = vec!["0", "255", "65535", "4294967295"];
8305        assert_eq!(
8306            u64_expected,
8307            get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
8308        );
8309
8310        let u32_expected = vec!["0", "255", "65535", "4294967295"];
8311        assert_eq!(
8312            u32_expected,
8313            get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
8314        );
8315
8316        let u16_expected = vec!["0", "255", "65535", "null"];
8317        assert_eq!(
8318            u16_expected,
8319            get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
8320        );
8321
8322        let u8_expected = vec!["0", "255", "null", "null"];
8323        assert_eq!(
8324            u8_expected,
8325            get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
8326        );
8327    }
8328
8329    #[test]
8330    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8331    fn test_cast_from_uint16() {
8332        let u16_values: Vec<u16> = vec![0, u8::MAX as u16, u16::MAX];
8333        let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values));
8334
8335        let f64_expected = vec!["0.0", "255.0", "65535.0"];
8336        assert_eq!(
8337            f64_expected,
8338            get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
8339        );
8340
8341        let f32_expected = vec!["0.0", "255.0", "65535.0"];
8342        assert_eq!(
8343            f32_expected,
8344            get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
8345        );
8346
8347        let f16_expected = vec!["0.0", "255.0", "inf"];
8348        assert_eq!(
8349            f16_expected,
8350            get_cast_values::<Float16Type>(&u16_array, &DataType::Float16)
8351        );
8352
8353        let i64_expected = vec!["0", "255", "65535"];
8354        assert_eq!(
8355            i64_expected,
8356            get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
8357        );
8358
8359        let i32_expected = vec!["0", "255", "65535"];
8360        assert_eq!(
8361            i32_expected,
8362            get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
8363        );
8364
8365        let i16_expected = vec!["0", "255", "null"];
8366        assert_eq!(
8367            i16_expected,
8368            get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
8369        );
8370
8371        let i8_expected = vec!["0", "null", "null"];
8372        assert_eq!(
8373            i8_expected,
8374            get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
8375        );
8376
8377        let u64_expected = vec!["0", "255", "65535"];
8378        assert_eq!(
8379            u64_expected,
8380            get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
8381        );
8382
8383        let u32_expected = vec!["0", "255", "65535"];
8384        assert_eq!(
8385            u32_expected,
8386            get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
8387        );
8388
8389        let u16_expected = vec!["0", "255", "65535"];
8390        assert_eq!(
8391            u16_expected,
8392            get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
8393        );
8394
8395        let u8_expected = vec!["0", "255", "null"];
8396        assert_eq!(
8397            u8_expected,
8398            get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
8399        );
8400    }
8401
8402    #[test]
8403    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8404    fn test_cast_from_uint8() {
8405        let u8_values: Vec<u8> = vec![0, u8::MAX];
8406        let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values));
8407
8408        let f64_expected = vec!["0.0", "255.0"];
8409        assert_eq!(
8410            f64_expected,
8411            get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
8412        );
8413
8414        let f32_expected = vec!["0.0", "255.0"];
8415        assert_eq!(
8416            f32_expected,
8417            get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
8418        );
8419
8420        let f16_expected = vec!["0.0", "255.0"];
8421        assert_eq!(
8422            f16_expected,
8423            get_cast_values::<Float16Type>(&u8_array, &DataType::Float16)
8424        );
8425
8426        let i64_expected = vec!["0", "255"];
8427        assert_eq!(
8428            i64_expected,
8429            get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
8430        );
8431
8432        let i32_expected = vec!["0", "255"];
8433        assert_eq!(
8434            i32_expected,
8435            get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
8436        );
8437
8438        let i16_expected = vec!["0", "255"];
8439        assert_eq!(
8440            i16_expected,
8441            get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
8442        );
8443
8444        let i8_expected = vec!["0", "null"];
8445        assert_eq!(
8446            i8_expected,
8447            get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
8448        );
8449
8450        let u64_expected = vec!["0", "255"];
8451        assert_eq!(
8452            u64_expected,
8453            get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
8454        );
8455
8456        let u32_expected = vec!["0", "255"];
8457        assert_eq!(
8458            u32_expected,
8459            get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
8460        );
8461
8462        let u16_expected = vec!["0", "255"];
8463        assert_eq!(
8464            u16_expected,
8465            get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
8466        );
8467
8468        let u8_expected = vec!["0", "255"];
8469        assert_eq!(
8470            u8_expected,
8471            get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
8472        );
8473    }
8474
8475    #[test]
8476    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8477    fn test_cast_from_int64() {
8478        let i64_values: Vec<i64> = vec![
8479            i64::MIN,
8480            i32::MIN as i64,
8481            i16::MIN as i64,
8482            i8::MIN as i64,
8483            0,
8484            i8::MAX as i64,
8485            i16::MAX as i64,
8486            i32::MAX as i64,
8487            i64::MAX,
8488        ];
8489        let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values));
8490
8491        let f64_expected = vec![
8492            -9223372036854776000.0,
8493            -2147483648.0,
8494            -32768.0,
8495            -128.0,
8496            0.0,
8497            127.0,
8498            32767.0,
8499            2147483647.0,
8500            9223372036854776000.0,
8501        ];
8502        assert_eq!(
8503            f64_expected,
8504            get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
8505                .iter()
8506                .map(|i| i.parse::<f64>().unwrap())
8507                .collect::<Vec<f64>>()
8508        );
8509
8510        let f32_expected = vec![
8511            -9223372000000000000.0,
8512            -2147483600.0,
8513            -32768.0,
8514            -128.0,
8515            0.0,
8516            127.0,
8517            32767.0,
8518            2147483600.0,
8519            9223372000000000000.0,
8520        ];
8521        assert_eq!(
8522            f32_expected,
8523            get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
8524                .iter()
8525                .map(|i| i.parse::<f32>().unwrap())
8526                .collect::<Vec<f32>>()
8527        );
8528
8529        let f16_expected = vec![
8530            f16::from_f64(-9223372000000000000.0),
8531            f16::from_f64(-2147483600.0),
8532            f16::from_f64(-32768.0),
8533            f16::from_f64(-128.0),
8534            f16::from_f64(0.0),
8535            f16::from_f64(127.0),
8536            f16::from_f64(32767.0),
8537            f16::from_f64(2147483600.0),
8538            f16::from_f64(9223372000000000000.0),
8539        ];
8540        assert_eq!(
8541            f16_expected,
8542            get_cast_values::<Float16Type>(&i64_array, &DataType::Float16)
8543                .iter()
8544                .map(|i| i.parse::<f16>().unwrap())
8545                .collect::<Vec<f16>>()
8546        );
8547
8548        let i64_expected = vec![
8549            "-9223372036854775808",
8550            "-2147483648",
8551            "-32768",
8552            "-128",
8553            "0",
8554            "127",
8555            "32767",
8556            "2147483647",
8557            "9223372036854775807",
8558        ];
8559        assert_eq!(
8560            i64_expected,
8561            get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
8562        );
8563
8564        let i32_expected = vec![
8565            "null",
8566            "-2147483648",
8567            "-32768",
8568            "-128",
8569            "0",
8570            "127",
8571            "32767",
8572            "2147483647",
8573            "null",
8574        ];
8575        assert_eq!(
8576            i32_expected,
8577            get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
8578        );
8579
8580        assert_eq!(
8581            i32_expected,
8582            get_cast_values::<Date32Type>(&i64_array, &DataType::Date32)
8583        );
8584
8585        let i16_expected = vec![
8586            "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
8587        ];
8588        assert_eq!(
8589            i16_expected,
8590            get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
8591        );
8592
8593        let i8_expected = vec![
8594            "null", "null", "null", "-128", "0", "127", "null", "null", "null",
8595        ];
8596        assert_eq!(
8597            i8_expected,
8598            get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
8599        );
8600
8601        let u64_expected = vec![
8602            "null",
8603            "null",
8604            "null",
8605            "null",
8606            "0",
8607            "127",
8608            "32767",
8609            "2147483647",
8610            "9223372036854775807",
8611        ];
8612        assert_eq!(
8613            u64_expected,
8614            get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
8615        );
8616
8617        let u32_expected = vec![
8618            "null",
8619            "null",
8620            "null",
8621            "null",
8622            "0",
8623            "127",
8624            "32767",
8625            "2147483647",
8626            "null",
8627        ];
8628        assert_eq!(
8629            u32_expected,
8630            get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
8631        );
8632
8633        let u16_expected = vec![
8634            "null", "null", "null", "null", "0", "127", "32767", "null", "null",
8635        ];
8636        assert_eq!(
8637            u16_expected,
8638            get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
8639        );
8640
8641        let u8_expected = vec![
8642            "null", "null", "null", "null", "0", "127", "null", "null", "null",
8643        ];
8644        assert_eq!(
8645            u8_expected,
8646            get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
8647        );
8648    }
8649
8650    #[test]
8651    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8652    fn test_cast_from_int32() {
8653        let i32_values: Vec<i32> = vec![
8654            i32::MIN,
8655            i16::MIN as i32,
8656            i8::MIN as i32,
8657            0,
8658            i8::MAX as i32,
8659            i16::MAX as i32,
8660            i32::MAX,
8661        ];
8662        let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values));
8663
8664        let f64_expected = vec![
8665            "-2147483648.0",
8666            "-32768.0",
8667            "-128.0",
8668            "0.0",
8669            "127.0",
8670            "32767.0",
8671            "2147483647.0",
8672        ];
8673        assert_eq!(
8674            f64_expected,
8675            get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
8676        );
8677
8678        let f32_expected = vec![
8679            "-2147483600.0",
8680            "-32768.0",
8681            "-128.0",
8682            "0.0",
8683            "127.0",
8684            "32767.0",
8685            "2147483600.0",
8686        ];
8687        assert_eq!(
8688            f32_expected,
8689            get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
8690        );
8691
8692        let f16_expected = vec![
8693            f16::from_f64(-2147483600.0),
8694            f16::from_f64(-32768.0),
8695            f16::from_f64(-128.0),
8696            f16::from_f64(0.0),
8697            f16::from_f64(127.0),
8698            f16::from_f64(32767.0),
8699            f16::from_f64(2147483600.0),
8700        ];
8701        assert_eq!(
8702            f16_expected,
8703            get_cast_values::<Float16Type>(&i32_array, &DataType::Float16)
8704                .iter()
8705                .map(|i| i.parse::<f16>().unwrap())
8706                .collect::<Vec<f16>>()
8707        );
8708
8709        let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
8710        assert_eq!(
8711            i16_expected,
8712            get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
8713        );
8714
8715        let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
8716        assert_eq!(
8717            i8_expected,
8718            get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
8719        );
8720
8721        let u64_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8722        assert_eq!(
8723            u64_expected,
8724            get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
8725        );
8726
8727        let u32_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8728        assert_eq!(
8729            u32_expected,
8730            get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
8731        );
8732
8733        let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
8734        assert_eq!(
8735            u16_expected,
8736            get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
8737        );
8738
8739        let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
8740        assert_eq!(
8741            u8_expected,
8742            get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
8743        );
8744
8745        // The date32 to date64 cast increases the numerical values in order to keep the same dates.
8746        let i64_expected = vec![
8747            "-185542587187200000",
8748            "-2831155200000",
8749            "-11059200000",
8750            "0",
8751            "10972800000",
8752            "2831068800000",
8753            "185542587100800000",
8754        ];
8755        assert_eq!(
8756            i64_expected,
8757            get_cast_values::<Date64Type>(&i32_array, &DataType::Date64)
8758        );
8759    }
8760
8761    #[test]
8762    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8763    fn test_cast_from_int16() {
8764        let i16_values: Vec<i16> = vec![i16::MIN, i8::MIN as i16, 0, i8::MAX as i16, i16::MAX];
8765        let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values));
8766
8767        let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8768        assert_eq!(
8769            f64_expected,
8770            get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
8771        );
8772
8773        let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8774        assert_eq!(
8775            f32_expected,
8776            get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
8777        );
8778
8779        let f16_expected = vec![
8780            f16::from_f64(-32768.0),
8781            f16::from_f64(-128.0),
8782            f16::from_f64(0.0),
8783            f16::from_f64(127.0),
8784            f16::from_f64(32767.0),
8785        ];
8786        assert_eq!(
8787            f16_expected,
8788            get_cast_values::<Float16Type>(&i16_array, &DataType::Float16)
8789                .iter()
8790                .map(|i| i.parse::<f16>().unwrap())
8791                .collect::<Vec<f16>>()
8792        );
8793
8794        let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
8795        assert_eq!(
8796            i64_expected,
8797            get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
8798        );
8799
8800        let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
8801        assert_eq!(
8802            i32_expected,
8803            get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
8804        );
8805
8806        let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
8807        assert_eq!(
8808            i16_expected,
8809            get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
8810        );
8811
8812        let i8_expected = vec!["null", "-128", "0", "127", "null"];
8813        assert_eq!(
8814            i8_expected,
8815            get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
8816        );
8817
8818        let u64_expected = vec!["null", "null", "0", "127", "32767"];
8819        assert_eq!(
8820            u64_expected,
8821            get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
8822        );
8823
8824        let u32_expected = vec!["null", "null", "0", "127", "32767"];
8825        assert_eq!(
8826            u32_expected,
8827            get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
8828        );
8829
8830        let u16_expected = vec!["null", "null", "0", "127", "32767"];
8831        assert_eq!(
8832            u16_expected,
8833            get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
8834        );
8835
8836        let u8_expected = vec!["null", "null", "0", "127", "null"];
8837        assert_eq!(
8838            u8_expected,
8839            get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
8840        );
8841    }
8842
8843    #[test]
8844    fn test_cast_from_date32() {
8845        let i32_values: Vec<i32> = vec![
8846            i32::MIN,
8847            i16::MIN as i32,
8848            i8::MIN as i32,
8849            0,
8850            i8::MAX as i32,
8851            i16::MAX as i32,
8852            i32::MAX,
8853        ];
8854        let date32_array: ArrayRef = Arc::new(Date32Array::from(i32_values));
8855
8856        let i64_expected = vec![
8857            "-2147483648",
8858            "-32768",
8859            "-128",
8860            "0",
8861            "127",
8862            "32767",
8863            "2147483647",
8864        ];
8865        assert_eq!(
8866            i64_expected,
8867            get_cast_values::<Int64Type>(&date32_array, &DataType::Int64)
8868        );
8869    }
8870
8871    #[test]
8872    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8873    fn test_cast_from_int8() {
8874        let i8_values: Vec<i8> = vec![i8::MIN, 0, i8::MAX];
8875        let i8_array = Int8Array::from(i8_values);
8876
8877        let f64_expected = vec!["-128.0", "0.0", "127.0"];
8878        assert_eq!(
8879            f64_expected,
8880            get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
8881        );
8882
8883        let f32_expected = vec!["-128.0", "0.0", "127.0"];
8884        assert_eq!(
8885            f32_expected,
8886            get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
8887        );
8888
8889        let f16_expected = vec!["-128.0", "0.0", "127.0"];
8890        assert_eq!(
8891            f16_expected,
8892            get_cast_values::<Float16Type>(&i8_array, &DataType::Float16)
8893        );
8894
8895        let i64_expected = vec!["-128", "0", "127"];
8896        assert_eq!(
8897            i64_expected,
8898            get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
8899        );
8900
8901        let i32_expected = vec!["-128", "0", "127"];
8902        assert_eq!(
8903            i32_expected,
8904            get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
8905        );
8906
8907        let i16_expected = vec!["-128", "0", "127"];
8908        assert_eq!(
8909            i16_expected,
8910            get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
8911        );
8912
8913        let i8_expected = vec!["-128", "0", "127"];
8914        assert_eq!(
8915            i8_expected,
8916            get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
8917        );
8918
8919        let u64_expected = vec!["null", "0", "127"];
8920        assert_eq!(
8921            u64_expected,
8922            get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
8923        );
8924
8925        let u32_expected = vec!["null", "0", "127"];
8926        assert_eq!(
8927            u32_expected,
8928            get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
8929        );
8930
8931        let u16_expected = vec!["null", "0", "127"];
8932        assert_eq!(
8933            u16_expected,
8934            get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
8935        );
8936
8937        let u8_expected = vec!["null", "0", "127"];
8938        assert_eq!(
8939            u8_expected,
8940            get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
8941        );
8942    }
8943
8944    /// Convert `array` into a vector of strings by casting to data type dt
8945    fn get_cast_values<T>(array: &dyn Array, dt: &DataType) -> Vec<String>
8946    where
8947        T: ArrowPrimitiveType,
8948    {
8949        let c = cast(array, dt).unwrap();
8950        let a = c.as_primitive::<T>();
8951        let mut v: Vec<String> = vec![];
8952        for i in 0..array.len() {
8953            if a.is_null(i) {
8954                v.push("null".to_string())
8955            } else {
8956                v.push(format!("{:?}", a.value(i)));
8957            }
8958        }
8959        v
8960    }
8961
8962    #[test]
8963    fn test_cast_utf8_dict() {
8964        // FROM a dictionary with of Utf8 values
8965        let mut builder = StringDictionaryBuilder::<Int8Type>::new();
8966        builder.append("one").unwrap();
8967        builder.append_null();
8968        builder.append("three").unwrap();
8969        let array: ArrayRef = Arc::new(builder.finish());
8970
8971        let expected = vec!["one", "null", "three"];
8972
8973        // Test casting TO StringArray
8974        let cast_type = Utf8;
8975        let cast_array = cast(&array, &cast_type).expect("cast to UTF-8 failed");
8976        assert_eq!(cast_array.data_type(), &cast_type);
8977        assert_eq!(array_to_strings(&cast_array), expected);
8978
8979        // Test casting TO Dictionary (with different index sizes)
8980
8981        let cast_type = Dictionary(Box::new(Int16), Box::new(Utf8));
8982        let cast_array = cast(&array, &cast_type).expect("cast failed");
8983        assert_eq!(cast_array.data_type(), &cast_type);
8984        assert_eq!(array_to_strings(&cast_array), expected);
8985
8986        let cast_type = Dictionary(Box::new(Int32), Box::new(Utf8));
8987        let cast_array = cast(&array, &cast_type).expect("cast failed");
8988        assert_eq!(cast_array.data_type(), &cast_type);
8989        assert_eq!(array_to_strings(&cast_array), expected);
8990
8991        let cast_type = Dictionary(Box::new(Int64), Box::new(Utf8));
8992        let cast_array = cast(&array, &cast_type).expect("cast failed");
8993        assert_eq!(cast_array.data_type(), &cast_type);
8994        assert_eq!(array_to_strings(&cast_array), expected);
8995
8996        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8997        let cast_array = cast(&array, &cast_type).expect("cast failed");
8998        assert_eq!(cast_array.data_type(), &cast_type);
8999        assert_eq!(array_to_strings(&cast_array), expected);
9000
9001        let cast_type = Dictionary(Box::new(UInt16), Box::new(Utf8));
9002        let cast_array = cast(&array, &cast_type).expect("cast failed");
9003        assert_eq!(cast_array.data_type(), &cast_type);
9004        assert_eq!(array_to_strings(&cast_array), expected);
9005
9006        let cast_type = Dictionary(Box::new(UInt32), Box::new(Utf8));
9007        let cast_array = cast(&array, &cast_type).expect("cast failed");
9008        assert_eq!(cast_array.data_type(), &cast_type);
9009        assert_eq!(array_to_strings(&cast_array), expected);
9010
9011        let cast_type = Dictionary(Box::new(UInt64), Box::new(Utf8));
9012        let cast_array = cast(&array, &cast_type).expect("cast failed");
9013        assert_eq!(cast_array.data_type(), &cast_type);
9014        assert_eq!(array_to_strings(&cast_array), expected);
9015    }
9016
9017    #[test]
9018    fn test_cast_dict_to_dict_bad_index_value_primitive() {
9019        // test converting from an array that has indexes of a type
9020        // that are out of bounds for a particular other kind of
9021        // index.
9022
9023        let mut builder = PrimitiveDictionaryBuilder::<Int32Type, Int64Type>::new();
9024
9025        // add 200 distinct values (which can be stored by a
9026        // dictionary indexed by int32, but not a dictionary indexed
9027        // with int8)
9028        for i in 0..200 {
9029            builder.append(i).unwrap();
9030        }
9031        let array: ArrayRef = Arc::new(builder.finish());
9032
9033        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
9034        let res = cast(&array, &cast_type);
9035        assert!(res.is_err());
9036        let actual_error = format!("{res:?}");
9037        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
9038        assert!(
9039            actual_error.contains(expected_error),
9040            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
9041        );
9042    }
9043
9044    #[test]
9045    fn test_cast_dict_to_dict_bad_index_value_utf8() {
9046        // Same test as test_cast_dict_to_dict_bad_index_value but use
9047        // string values (and encode the expected behavior here);
9048
9049        let mut builder = StringDictionaryBuilder::<Int32Type>::new();
9050
9051        // add 200 distinct values (which can be stored by a
9052        // dictionary indexed by int32, but not a dictionary indexed
9053        // with int8)
9054        for i in 0..200 {
9055            let val = format!("val{i}");
9056            builder.append(&val).unwrap();
9057        }
9058        let array = builder.finish();
9059
9060        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
9061        let res = cast(&array, &cast_type);
9062        assert!(res.is_err());
9063        let actual_error = format!("{res:?}");
9064        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
9065        assert!(
9066            actual_error.contains(expected_error),
9067            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
9068        );
9069    }
9070
9071    #[test]
9072    fn test_cast_nested_dictionary_to_dictionary_reuses_values() {
9073        let inner = DictionaryArray::<Int32Type>::new(
9074            Int32Array::from(vec![Some(0), None, Some(1)]),
9075            Arc::new(StringArray::from(vec!["x", "y"])),
9076        );
9077        let nested = DictionaryArray::<Int32Type>::new(
9078            Int32Array::from(vec![Some(0), Some(1), Some(2), None, Some(0)]),
9079            Arc::new(inner),
9080        );
9081
9082        let result = cast(&nested, &Dictionary(Box::new(Int32), Box::new(Utf8))).unwrap();
9083        let result = result.as_dictionary::<Int32Type>();
9084
9085        assert_eq!(
9086            result.keys(),
9087            &Int32Array::from(vec![Some(0), None, Some(1), None, Some(0)])
9088        );
9089        assert_eq!(
9090            result.values().as_string::<i32>(),
9091            &StringArray::from(vec!["x", "y"])
9092        );
9093        let logical: Vec<Option<&str>> = result
9094            .downcast_dict::<StringArray>()
9095            .unwrap()
9096            .into_iter()
9097            .collect();
9098        assert_eq!(logical, vec![Some("x"), None, Some("y"), None, Some("x")]);
9099    }
9100
9101    #[test]
9102    fn test_cast_primitive_dict() {
9103        // FROM a dictionary with of INT32 values
9104        let mut builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
9105        builder.append(1).unwrap();
9106        builder.append_null();
9107        builder.append(3).unwrap();
9108        let array: ArrayRef = Arc::new(builder.finish());
9109
9110        let expected = vec!["1", "null", "3"];
9111
9112        // Test casting TO PrimitiveArray, different dictionary type
9113        let cast_array = cast(&array, &Utf8).expect("cast to UTF-8 failed");
9114        assert_eq!(array_to_strings(&cast_array), expected);
9115        assert_eq!(cast_array.data_type(), &Utf8);
9116
9117        let cast_array = cast(&array, &Int64).expect("cast to int64 failed");
9118        assert_eq!(array_to_strings(&cast_array), expected);
9119        assert_eq!(cast_array.data_type(), &Int64);
9120    }
9121
9122    #[test]
9123    fn test_cast_primitive_array_to_dict() {
9124        let mut builder = PrimitiveBuilder::<Int32Type>::new();
9125        builder.append_value(1);
9126        builder.append_null();
9127        builder.append_value(3);
9128        let array: ArrayRef = Arc::new(builder.finish());
9129
9130        let expected = vec!["1", "null", "3"];
9131
9132        // Cast to a dictionary (same value type, Int32)
9133        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int32));
9134        let cast_array = cast(&array, &cast_type).expect("cast failed");
9135        assert_eq!(cast_array.data_type(), &cast_type);
9136        assert_eq!(array_to_strings(&cast_array), expected);
9137
9138        // Cast to a dictionary (different value type, Int8)
9139        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int8));
9140        let cast_array = cast(&array, &cast_type).expect("cast failed");
9141        assert_eq!(cast_array.data_type(), &cast_type);
9142        assert_eq!(array_to_strings(&cast_array), expected);
9143    }
9144
9145    #[test]
9146    fn test_cast_time_array_to_dict() {
9147        use DataType::*;
9148
9149        let array = Arc::new(Date32Array::from(vec![Some(1000), None, Some(2000)])) as ArrayRef;
9150
9151        let expected = vec!["1972-09-27", "null", "1975-06-24"];
9152
9153        let cast_type = Dictionary(Box::new(UInt8), Box::new(Date32));
9154        let cast_array = cast(&array, &cast_type).expect("cast failed");
9155        assert_eq!(cast_array.data_type(), &cast_type);
9156        assert_eq!(array_to_strings(&cast_array), expected);
9157    }
9158
9159    #[test]
9160    fn test_cast_timestamp_array_to_dict() {
9161        use DataType::*;
9162
9163        let array = Arc::new(
9164            TimestampSecondArray::from(vec![Some(1000), None, Some(2000)]).with_timezone_utc(),
9165        ) as ArrayRef;
9166
9167        let expected = vec!["1970-01-01T00:16:40", "null", "1970-01-01T00:33:20"];
9168
9169        let cast_type = Dictionary(Box::new(UInt8), Box::new(Timestamp(TimeUnit::Second, None)));
9170        let cast_array = cast(&array, &cast_type).expect("cast failed");
9171        assert_eq!(cast_array.data_type(), &cast_type);
9172        assert_eq!(array_to_strings(&cast_array), expected);
9173    }
9174
9175    #[test]
9176    fn test_cast_string_array_to_dict() {
9177        use DataType::*;
9178
9179        let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) as ArrayRef;
9180
9181        let expected = vec!["one", "null", "three"];
9182
9183        // Cast to a dictionary (same value type, Utf8)
9184        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
9185        let cast_array = cast(&array, &cast_type).expect("cast failed");
9186        assert_eq!(cast_array.data_type(), &cast_type);
9187        assert_eq!(array_to_strings(&cast_array), expected);
9188    }
9189
9190    #[test]
9191    fn test_cast_null_array_to_from_decimal_array() {
9192        let data_type = DataType::Decimal128(12, 4);
9193        let array = new_null_array(&DataType::Null, 4);
9194        assert_eq!(array.data_type(), &DataType::Null);
9195        let cast_array = cast(&array, &data_type).expect("cast failed");
9196        assert_eq!(cast_array.data_type(), &data_type);
9197        for i in 0..4 {
9198            assert!(cast_array.is_null(i));
9199        }
9200
9201        let array = new_null_array(&data_type, 4);
9202        assert_eq!(array.data_type(), &data_type);
9203        let cast_array = cast(&array, &DataType::Null).expect("cast failed");
9204        assert_eq!(cast_array.data_type(), &DataType::Null);
9205        assert_eq!(cast_array.len(), 4);
9206        assert_eq!(cast_array.logical_nulls().unwrap().null_count(), 4);
9207    }
9208
9209    #[test]
9210    fn test_cast_null_array_from_and_to_primitive_array() {
9211        macro_rules! typed_test {
9212            ($ARR_TYPE:ident, $DATATYPE:ident, $TYPE:tt) => {{
9213                {
9214                    let array = Arc::new(NullArray::new(6)) as ArrayRef;
9215                    let expected = $ARR_TYPE::from(vec![None; 6]);
9216                    let cast_type = DataType::$DATATYPE;
9217                    let cast_array = cast(&array, &cast_type).expect("cast failed");
9218                    let cast_array = cast_array.as_primitive::<$TYPE>();
9219                    assert_eq!(cast_array.data_type(), &cast_type);
9220                    assert_eq!(cast_array, &expected);
9221                }
9222            }};
9223        }
9224
9225        typed_test!(Int16Array, Int16, Int16Type);
9226        typed_test!(Int32Array, Int32, Int32Type);
9227        typed_test!(Int64Array, Int64, Int64Type);
9228
9229        typed_test!(UInt16Array, UInt16, UInt16Type);
9230        typed_test!(UInt32Array, UInt32, UInt32Type);
9231        typed_test!(UInt64Array, UInt64, UInt64Type);
9232
9233        typed_test!(Float16Array, Float16, Float16Type);
9234        typed_test!(Float32Array, Float32, Float32Type);
9235        typed_test!(Float64Array, Float64, Float64Type);
9236
9237        typed_test!(Date32Array, Date32, Date32Type);
9238        typed_test!(Date64Array, Date64, Date64Type);
9239    }
9240
9241    fn cast_from_null_to_other_base(data_type: &DataType, is_complex: bool) {
9242        // Cast from null to data_type
9243        let array = new_null_array(&DataType::Null, 4);
9244        assert_eq!(array.data_type(), &DataType::Null);
9245        let cast_array = cast(&array, data_type).expect("cast failed");
9246        assert_eq!(cast_array.data_type(), data_type);
9247        for i in 0..4 {
9248            if is_complex {
9249                assert!(cast_array.logical_nulls().unwrap().is_null(i));
9250            } else {
9251                assert!(cast_array.is_null(i));
9252            }
9253        }
9254    }
9255
9256    fn cast_from_null_to_other(data_type: &DataType) {
9257        cast_from_null_to_other_base(data_type, false);
9258    }
9259
9260    fn cast_from_null_to_other_complex(data_type: &DataType) {
9261        cast_from_null_to_other_base(data_type, true);
9262    }
9263
9264    #[test]
9265    fn test_cast_null_from_and_to_variable_sized() {
9266        cast_from_null_to_other(&DataType::Utf8);
9267        cast_from_null_to_other(&DataType::LargeUtf8);
9268        cast_from_null_to_other(&DataType::Binary);
9269        cast_from_null_to_other(&DataType::LargeBinary);
9270    }
9271
9272    #[test]
9273    fn test_cast_null_from_and_to_nested_type() {
9274        // Cast null from and to map
9275        let data_type = DataType::Map(
9276            Arc::new(Field::new_struct(
9277                Field::MAP_ENTRIES_FIELD_DEFAULT_NAME,
9278                vec![
9279                    Field::new(Field::MAP_KEY_FIELD_DEFAULT_NAME, DataType::Utf8, false),
9280                    Field::new(Field::MAP_VALUE_FIELD_DEFAULT_NAME, DataType::Int32, true),
9281                ],
9282                false,
9283            )),
9284            false,
9285        );
9286        cast_from_null_to_other(&data_type);
9287
9288        // Cast null from and to list
9289        let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
9290        cast_from_null_to_other(&data_type);
9291        let data_type = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
9292        cast_from_null_to_other(&data_type);
9293        let data_type =
9294            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
9295        cast_from_null_to_other(&data_type);
9296
9297        // Cast null from and to dictionary
9298        let values = vec![None, None, None, None] as Vec<Option<&str>>;
9299        let array: DictionaryArray<Int8Type> = values.into_iter().collect();
9300        let array = Arc::new(array) as ArrayRef;
9301        let data_type = array.data_type().to_owned();
9302        cast_from_null_to_other(&data_type);
9303
9304        // Cast null from and to struct
9305        let data_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
9306        cast_from_null_to_other(&data_type);
9307
9308        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
9309        cast_from_null_to_other(&target_type);
9310
9311        let target_type =
9312            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
9313        cast_from_null_to_other(&target_type);
9314
9315        let fields = UnionFields::from_fields(vec![Field::new("a", DataType::Int64, false)]);
9316        let target_type = DataType::Union(fields, UnionMode::Sparse);
9317        cast_from_null_to_other_complex(&target_type);
9318
9319        let target_type = DataType::RunEndEncoded(
9320            Arc::new(Field::new("item", DataType::Int32, true)),
9321            Arc::new(Field::new("item", DataType::Int32, true)),
9322        );
9323        cast_from_null_to_other_complex(&target_type);
9324    }
9325
9326    /// Print the `DictionaryArray` `array` as a vector of strings
9327    fn array_to_strings(array: &ArrayRef) -> Vec<String> {
9328        let options = FormatOptions::new().with_null("null");
9329        let formatter = ArrayFormatter::try_new(array.as_ref(), &options).unwrap();
9330        (0..array.len())
9331            .map(|i| formatter.value(i).to_string())
9332            .collect()
9333    }
9334
9335    #[test]
9336    fn test_cast_utf8_to_date32() {
9337        use chrono::NaiveDate;
9338        let from_ymd = chrono::NaiveDate::from_ymd_opt;
9339        let since = chrono::NaiveDate::signed_duration_since;
9340
9341        let a = StringArray::from(vec![
9342            "2000-01-01",          // valid date with leading 0s
9343            "2000-01-01T12:00:00", // valid datetime, will throw away the time part
9344            "2000-2-2",            // valid date without leading 0s
9345            "2000-00-00",          // invalid month and day
9346            "2000",                // just a year is invalid
9347        ]);
9348        let array = Arc::new(a) as ArrayRef;
9349        let b = cast(&array, &DataType::Date32).unwrap();
9350        let c = b.as_primitive::<Date32Type>();
9351
9352        // test valid inputs
9353        let date_value = since(
9354            NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
9355            from_ymd(1970, 1, 1).unwrap(),
9356        )
9357        .num_days() as i32;
9358        assert!(c.is_valid(0)); // "2000-01-01"
9359        assert_eq!(date_value, c.value(0));
9360
9361        assert!(c.is_valid(1)); // "2000-01-01T12:00:00"
9362        assert_eq!(date_value, c.value(1));
9363
9364        let date_value = since(
9365            NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
9366            from_ymd(1970, 1, 1).unwrap(),
9367        )
9368        .num_days() as i32;
9369        assert!(c.is_valid(2)); // "2000-2-2"
9370        assert_eq!(date_value, c.value(2));
9371
9372        // test invalid inputs
9373        assert!(!c.is_valid(3)); // "2000-00-00"
9374        assert!(!c.is_valid(4)); // "2000"
9375    }
9376
9377    #[test]
9378    fn test_cast_utf8_to_date64() {
9379        let a = StringArray::from(vec![
9380            "2000-01-01T12:00:00", // date + time valid
9381            "2020-12-15T12:34:56", // date + time valid
9382            "2020-2-2T12:34:56",   // valid date time without leading 0s
9383            "2000-00-00T12:00:00", // invalid month and day
9384            "2000-01-01 12:00:00", // missing the 'T'
9385            "2000-01-01",          // just a date is invalid
9386        ]);
9387        let array = Arc::new(a) as ArrayRef;
9388        let b = cast(&array, &DataType::Date64).unwrap();
9389        let c = b.as_primitive::<Date64Type>();
9390
9391        // test valid inputs
9392        assert!(c.is_valid(0)); // "2000-01-01T12:00:00"
9393        assert_eq!(946728000000, c.value(0));
9394        assert!(c.is_valid(1)); // "2020-12-15T12:34:56"
9395        assert_eq!(1608035696000, c.value(1));
9396        assert!(!c.is_valid(2)); // "2020-2-2T12:34:56"
9397
9398        assert!(!c.is_valid(3)); // "2000-00-00T12:00:00"
9399        assert!(c.is_valid(4)); // "2000-01-01 12:00:00"
9400        assert_eq!(946728000000, c.value(4));
9401        assert!(c.is_valid(5)); // "2000-01-01"
9402        assert_eq!(946684800000, c.value(5));
9403    }
9404
9405    #[test]
9406    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
9407    fn test_can_cast_fsl_to_fsl() {
9408        let from_array = Arc::new(
9409            FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
9410                [Some([Some(1.0), Some(2.0)]), None],
9411                2,
9412            ),
9413        ) as ArrayRef;
9414        let to_array = Arc::new(
9415            FixedSizeListArray::from_iter_primitive::<Float16Type, _, _>(
9416                [
9417                    Some([Some(f16::from_f32(1.0)), Some(f16::from_f32(2.0))]),
9418                    None,
9419                ],
9420                2,
9421            ),
9422        ) as ArrayRef;
9423
9424        assert!(can_cast_types(from_array.data_type(), to_array.data_type()));
9425        let actual = cast(&from_array, to_array.data_type()).unwrap();
9426        assert_eq!(actual.data_type(), to_array.data_type());
9427
9428        let invalid_target =
9429            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Binary, true)), 2);
9430        assert!(!can_cast_types(from_array.data_type(), &invalid_target));
9431
9432        let invalid_size =
9433            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Float16, true)), 5);
9434        assert!(!can_cast_types(from_array.data_type(), &invalid_size));
9435    }
9436
9437    #[test]
9438    fn test_can_cast_types_fixed_size_list_to_list() {
9439        // DataType::List
9440        let array1 = make_fixed_size_list_array();
9441        assert!(can_cast_types(
9442            array1.data_type(),
9443            &DataType::List(Arc::new(Field::new("", DataType::Int32, false)))
9444        ));
9445
9446        // DataType::LargeList
9447        let array2 = make_fixed_size_list_array_for_large_list();
9448        assert!(can_cast_types(
9449            array2.data_type(),
9450            &DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false)))
9451        ));
9452    }
9453
9454    #[test]
9455    fn test_cast_fixed_size_list_to_list() {
9456        // Important cases:
9457        // 1. With/without nulls
9458        // 2. List/LargeList/ListView/LargeListView
9459        // 3. With and without inner casts
9460
9461        let cases = [
9462            // fixed_size_list<i32, 2> => list<i32>
9463            (
9464                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9465                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9466                    2,
9467                )) as ArrayRef,
9468                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
9469                    Some([Some(1), Some(1)]),
9470                    Some([Some(2), Some(2)]),
9471                ])) as ArrayRef,
9472            ),
9473            // fixed_size_list<i32, 2> => list<i32> (nullable)
9474            (
9475                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9476                    [None, Some([Some(2), Some(2)])],
9477                    2,
9478                )) as ArrayRef,
9479                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
9480                    None,
9481                    Some([Some(2), Some(2)]),
9482                ])) as ArrayRef,
9483            ),
9484            // fixed_size_list<i32, 2> => large_list<i64>
9485            (
9486                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9487                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9488                    2,
9489                )) as ArrayRef,
9490                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
9491                    Some([Some(1), Some(1)]),
9492                    Some([Some(2), Some(2)]),
9493                ])) as ArrayRef,
9494            ),
9495            // fixed_size_list<i32, 2> => large_list<i64> (nullable)
9496            (
9497                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9498                    [None, Some([Some(2), Some(2)])],
9499                    2,
9500                )) as ArrayRef,
9501                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
9502                    None,
9503                    Some([Some(2), Some(2)]),
9504                ])) as ArrayRef,
9505            ),
9506            // fixed_size_list<i32, 2> => list_view<i32>
9507            (
9508                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9509                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9510                    2,
9511                )) as ArrayRef,
9512                Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>([
9513                    Some([Some(1), Some(1)]),
9514                    Some([Some(2), Some(2)]),
9515                ])) as ArrayRef,
9516            ),
9517            // fixed_size_list<i32, 2> => list_view<i32> (nullable)
9518            (
9519                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9520                    [None, Some([Some(2), Some(2)])],
9521                    2,
9522                )) as ArrayRef,
9523                Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>([
9524                    None,
9525                    Some([Some(2), Some(2)]),
9526                ])) as ArrayRef,
9527            ),
9528            // fixed_size_list<i32, 2> => large_list_view<i64>
9529            (
9530                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9531                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9532                    2,
9533                )) as ArrayRef,
9534                Arc::new(LargeListViewArray::from_iter_primitive::<Int64Type, _, _>(
9535                    [Some([Some(1), Some(1)]), Some([Some(2), Some(2)])],
9536                )) as ArrayRef,
9537            ),
9538            // fixed_size_list<i32, 2> => large_list_view<i64> (nullable)
9539            (
9540                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9541                    [None, Some([Some(2), Some(2)])],
9542                    2,
9543                )) as ArrayRef,
9544                Arc::new(LargeListViewArray::from_iter_primitive::<Int64Type, _, _>(
9545                    [None, Some([Some(2), Some(2)])],
9546                )) as ArrayRef,
9547            ),
9548        ];
9549
9550        for (array, expected) in cases {
9551            assert!(
9552                can_cast_types(array.data_type(), expected.data_type()),
9553                "can_cast_types claims we cannot cast {:?} to {:?}",
9554                array.data_type(),
9555                expected.data_type()
9556            );
9557
9558            let list_array = cast(&array, expected.data_type())
9559                .unwrap_or_else(|_| panic!("Failed to cast {array:?} to {expected:?}"));
9560            assert_eq!(
9561                list_array.as_ref(),
9562                &expected,
9563                "Incorrect result from casting {array:?} to {expected:?}",
9564            );
9565        }
9566    }
9567
9568    #[test]
9569    fn test_cast_fixed_size_list_to_list_preserves_field_metadata() {
9570        use std::collections::HashMap;
9571
9572        let metadata: HashMap<String, String> =
9573            HashMap::from([("PARQUET:field_id".to_string(), "89".to_string())]);
9574
9575        let src = Arc::new(
9576            FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
9577                [[1.0_f32, 2.0].map(Some), [3.0, 4.0].map(Some)].map(Some),
9578                2,
9579            ),
9580        ) as ArrayRef;
9581
9582        let target_field = Arc::new(
9583            Field::new("element", DataType::Float32, true).with_metadata(metadata.clone()),
9584        );
9585
9586        let target_types = [
9587            DataType::List(target_field.clone()),
9588            DataType::LargeList(target_field.clone()),
9589            DataType::ListView(target_field.clone()),
9590            DataType::LargeListView(target_field.clone()),
9591        ];
9592
9593        for target_type in &target_types {
9594            let result = cast(&src, target_type).unwrap();
9595            assert_eq!(
9596                result.data_type(),
9597                target_type,
9598                "Cast to {target_type:?} should preserve field metadata"
9599            );
9600        }
9601    }
9602
9603    #[test]
9604    fn test_cast_utf8_to_list() {
9605        // DataType::List
9606        let array = Arc::new(StringArray::from(vec!["5"])) as ArrayRef;
9607        let field = Arc::new(Field::new("", DataType::Int32, false));
9608        let list_array = cast(&array, &DataType::List(field.clone())).unwrap();
9609        let actual = list_array.as_list_opt::<i32>().unwrap();
9610        let expect = ListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
9611        assert_eq!(&expect.value(0), &actual.value(0));
9612
9613        // DataType::LargeList
9614        let list_array = cast(&array, &DataType::LargeList(field.clone())).unwrap();
9615        let actual = list_array.as_list_opt::<i64>().unwrap();
9616        let expect = LargeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
9617        assert_eq!(&expect.value(0), &actual.value(0));
9618
9619        // DataType::FixedSizeList
9620        let list_array = cast(&array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9621        let actual = list_array.as_fixed_size_list_opt().unwrap();
9622        let expect =
9623            FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])], 1);
9624        assert_eq!(&expect.value(0), &actual.value(0));
9625    }
9626
9627    #[test]
9628    fn test_cast_single_element_fixed_size_list() {
9629        // FixedSizeList<T>[1] => T
9630        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9631            [(Some([Some(5)]))],
9632            1,
9633        )) as ArrayRef;
9634        let casted_array = cast(&from_array, &DataType::Int32).unwrap();
9635        let actual: &Int32Array = casted_array.as_primitive();
9636        let expected = Int32Array::from(vec![Some(5)]);
9637        assert_eq!(&expected, actual);
9638
9639        // FixedSizeList<T>[1] => FixedSizeList<U>[1]
9640        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9641            [(Some([Some(5)]))],
9642            1,
9643        )) as ArrayRef;
9644        let to_field = Arc::new(Field::new("dummy", DataType::Float32, false));
9645        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
9646        let expected = Arc::new(FixedSizeListArray::new(
9647            to_field.clone(),
9648            1,
9649            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9650            None,
9651        )) as ArrayRef;
9652        assert_eq!(*expected, *actual);
9653
9654        // FixedSizeList<T>[1] => FixedSizeList<FixdSizedList<U>[1]>[1]
9655        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9656            [(Some([Some(5)]))],
9657            1,
9658        )) as ArrayRef;
9659        let to_field_inner = Arc::new(Field::new_list_field(DataType::Float32, false));
9660        let to_field = Arc::new(Field::new(
9661            "dummy",
9662            DataType::FixedSizeList(to_field_inner.clone(), 1),
9663            false,
9664        ));
9665        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
9666        let expected = Arc::new(FixedSizeListArray::new(
9667            to_field.clone(),
9668            1,
9669            Arc::new(FixedSizeListArray::new(
9670                to_field_inner.clone(),
9671                1,
9672                Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9673                None,
9674            )) as ArrayRef,
9675            None,
9676        )) as ArrayRef;
9677        assert_eq!(*expected, *actual);
9678
9679        // T => FixedSizeList<T>[1] (non-nullable)
9680        let field = Arc::new(Field::new("dummy", DataType::Float32, false));
9681        let from_array = Arc::new(Int8Array::from(vec![Some(5)])) as ArrayRef;
9682        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9683        let actual = casted_array.as_fixed_size_list();
9684        let expected = Arc::new(FixedSizeListArray::new(
9685            field.clone(),
9686            1,
9687            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9688            None,
9689        )) as ArrayRef;
9690        assert_eq!(expected.as_ref(), actual);
9691
9692        // T => FixedSizeList<T>[1] (nullable)
9693        let field = Arc::new(Field::new("nullable", DataType::Float32, true));
9694        let from_array = Arc::new(Int8Array::from(vec![None])) as ArrayRef;
9695        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9696        let actual = casted_array.as_fixed_size_list();
9697        let expected = Arc::new(FixedSizeListArray::new(
9698            field.clone(),
9699            1,
9700            Arc::new(Float32Array::from(vec![None])) as ArrayRef,
9701            None,
9702        )) as ArrayRef;
9703        assert_eq!(expected.as_ref(), actual);
9704    }
9705
9706    #[test]
9707    fn test_cast_list_containers() {
9708        // large-list to list
9709        let array = make_large_list_array();
9710        let list_array = cast(
9711            &array,
9712            &DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
9713        )
9714        .unwrap();
9715        let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
9716        let expected = array.as_any().downcast_ref::<LargeListArray>().unwrap();
9717
9718        assert_eq!(&expected.value(0), &actual.value(0));
9719        assert_eq!(&expected.value(1), &actual.value(1));
9720        assert_eq!(&expected.value(2), &actual.value(2));
9721
9722        // list to large-list
9723        let array = make_list_array();
9724        let large_list_array = cast(
9725            &array,
9726            &DataType::LargeList(Arc::new(Field::new("", DataType::Int32, false))),
9727        )
9728        .unwrap();
9729        let actual = large_list_array
9730            .as_any()
9731            .downcast_ref::<LargeListArray>()
9732            .unwrap();
9733        let expected = array.as_any().downcast_ref::<ListArray>().unwrap();
9734
9735        assert_eq!(&expected.value(0), &actual.value(0));
9736        assert_eq!(&expected.value(1), &actual.value(1));
9737        assert_eq!(&expected.value(2), &actual.value(2));
9738    }
9739
9740    #[test]
9741    fn test_cast_list_view() {
9742        // cast between list view and list view
9743        let array = make_list_view_array();
9744        let to = DataType::ListView(Field::new_list_field(DataType::Float32, true).into());
9745        assert!(can_cast_types(array.data_type(), &to));
9746        let actual = cast(&array, &to).unwrap();
9747        let actual = actual.as_list_view::<i32>();
9748
9749        assert_eq!(
9750            &Float32Array::from(vec![0.0, 1.0, 2.0]) as &dyn Array,
9751            actual.value(0).as_ref()
9752        );
9753        assert_eq!(
9754            &Float32Array::from(vec![3.0, 4.0, 5.0]) as &dyn Array,
9755            actual.value(1).as_ref()
9756        );
9757        assert_eq!(
9758            &Float32Array::from(vec![6.0, 7.0]) as &dyn Array,
9759            actual.value(2).as_ref()
9760        );
9761
9762        // cast between large list view and large list view
9763        let array = make_large_list_view_array();
9764        let to = DataType::LargeListView(Field::new_list_field(DataType::Float32, true).into());
9765        assert!(can_cast_types(array.data_type(), &to));
9766        let actual = cast(&array, &to).unwrap();
9767        let actual = actual.as_list_view::<i64>();
9768
9769        assert_eq!(
9770            &Float32Array::from(vec![0.0, 1.0, 2.0]) as &dyn Array,
9771            actual.value(0).as_ref()
9772        );
9773        assert_eq!(
9774            &Float32Array::from(vec![3.0, 4.0, 5.0]) as &dyn Array,
9775            actual.value(1).as_ref()
9776        );
9777        assert_eq!(
9778            &Float32Array::from(vec![6.0, 7.0]) as &dyn Array,
9779            actual.value(2).as_ref()
9780        );
9781    }
9782
9783    #[test]
9784    fn test_non_list_to_list_view() {
9785        let input = Arc::new(Int32Array::from(vec![Some(0), None, Some(2)])) as ArrayRef;
9786        let expected_primitive =
9787            Arc::new(Float32Array::from(vec![Some(0.0), None, Some(2.0)])) as ArrayRef;
9788
9789        // [[0], [NULL], [2]]
9790        let expected = ListViewArray::new(
9791            Field::new_list_field(DataType::Float32, true).into(),
9792            vec![0, 1, 2].into(),
9793            vec![1, 1, 1].into(),
9794            expected_primitive.clone(),
9795            None,
9796        );
9797        assert!(can_cast_types(input.data_type(), expected.data_type()));
9798        let actual = cast(&input, expected.data_type()).unwrap();
9799        assert_eq!(actual.as_ref(), &expected);
9800
9801        // [[0], [NULL], [2]]
9802        let expected = LargeListViewArray::new(
9803            Field::new_list_field(DataType::Float32, true).into(),
9804            vec![0, 1, 2].into(),
9805            vec![1, 1, 1].into(),
9806            expected_primitive.clone(),
9807            None,
9808        );
9809        assert!(can_cast_types(input.data_type(), expected.data_type()));
9810        let actual = cast(&input, expected.data_type()).unwrap();
9811        assert_eq!(actual.as_ref(), &expected);
9812    }
9813
9814    #[test]
9815    fn test_cast_list_to_zero_size_fsl() {
9816        let field = Arc::new(Field::new("a", DataType::Null, true));
9817        let length = 2;
9818        let expected = Arc::new(
9819            FixedSizeListArray::try_new_with_length(
9820                field.clone(),
9821                0,
9822                new_empty_array(&DataType::Null),
9823                None,
9824                2,
9825            )
9826            .unwrap(),
9827        ) as ArrayRef;
9828
9829        let list = Arc::new(ListArray::new(
9830            field.clone(),
9831            OffsetBuffer::from_repeated_length(0, length),
9832            new_empty_array(&DataType::Null),
9833            None,
9834        ));
9835        let fsl = cast(list.as_ref(), expected.data_type()).unwrap();
9836        assert_eq!(&expected, &fsl);
9837
9838        let list = Arc::new(ListViewArray::new(
9839            field.clone(),
9840            vec![0; length].into(),
9841            vec![0; length].into(),
9842            new_empty_array(&DataType::Null),
9843            None,
9844        ));
9845        let fsl = cast(list.as_ref(), expected.data_type()).unwrap();
9846        assert_eq!(&expected, &fsl);
9847    }
9848
9849    #[test]
9850    fn test_cast_list_to_fsl() {
9851        // There four noteworthy cases we should handle:
9852        // 1. No nulls
9853        // 2. Nulls that are always empty
9854        // 3. Nulls that have varying lengths
9855        // 4. Nulls that are correctly sized (same as target list size)
9856
9857        // Non-null case
9858        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9859        let values = vec![
9860            Some(vec![Some(1), Some(2), Some(3)]),
9861            Some(vec![Some(4), Some(5), Some(6)]),
9862        ];
9863        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
9864            values.clone(),
9865        )) as ArrayRef;
9866        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9867            values, 3,
9868        )) as ArrayRef;
9869        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9870        assert_eq!(expected.as_ref(), actual.as_ref());
9871
9872        // Null cases
9873        // Array is [[1, 2, 3], null, [4, 5, 6], null]
9874        let cases = [
9875            (
9876                // Zero-length nulls
9877                vec![1, 2, 3, 4, 5, 6],
9878                vec![3, 0, 3, 0],
9879            ),
9880            (
9881                // Varying-length nulls
9882                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
9883                vec![3, 2, 3, 1],
9884            ),
9885            (
9886                // Correctly-sized nulls
9887                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
9888                vec![3, 3, 3, 3],
9889            ),
9890            (
9891                // Mixed nulls
9892                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
9893                vec![3, 0, 3, 3],
9894            ),
9895        ];
9896        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
9897
9898        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9899            vec![
9900                Some(vec![Some(1), Some(2), Some(3)]),
9901                None,
9902                Some(vec![Some(4), Some(5), Some(6)]),
9903                None,
9904            ],
9905            3,
9906        )) as ArrayRef;
9907
9908        for (values, lengths) in &cases {
9909            let array = Arc::new(ListArray::new(
9910                field.clone(),
9911                OffsetBuffer::from_lengths(lengths.clone()),
9912                Arc::new(Int32Array::from(values.clone())),
9913                Some(null_buffer.clone()),
9914            )) as ArrayRef;
9915            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9916            assert_eq!(expected.as_ref(), actual.as_ref());
9917        }
9918    }
9919
9920    #[test]
9921    fn test_cast_list_view_to_fsl() {
9922        // There four noteworthy cases we should handle:
9923        // 1. No nulls
9924        // 2. Nulls that are always empty
9925        // 3. Nulls that have varying lengths
9926        // 4. Nulls that are correctly sized (same as target list size)
9927
9928        // Non-null case
9929        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9930        let values = vec![
9931            Some(vec![Some(1), Some(2), Some(3)]),
9932            Some(vec![Some(4), Some(5), Some(6)]),
9933        ];
9934        let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(
9935            values.clone(),
9936        )) as ArrayRef;
9937        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9938            values, 3,
9939        )) as ArrayRef;
9940        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9941        assert_eq!(expected.as_ref(), actual.as_ref());
9942
9943        // Null cases
9944        // Array is [[1, 2, 3], null, [4, 5, 6], null]
9945        let cases = [
9946            (
9947                // Zero-length nulls
9948                vec![1, 2, 3, 4, 5, 6],
9949                vec![0, 0, 3, 0],
9950                vec![3, 0, 3, 0],
9951            ),
9952            (
9953                // Varying-length nulls
9954                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
9955                vec![0, 1, 5, 0],
9956                vec![3, 2, 3, 1],
9957            ),
9958            (
9959                // Correctly-sized nulls
9960                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
9961                vec![0, 3, 6, 9],
9962                vec![3, 3, 3, 3],
9963            ),
9964            (
9965                // Mixed nulls
9966                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
9967                vec![0, 0, 3, 6],
9968                vec![3, 0, 3, 3],
9969            ),
9970        ];
9971        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
9972
9973        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9974            vec![
9975                Some(vec![Some(1), Some(2), Some(3)]),
9976                None,
9977                Some(vec![Some(4), Some(5), Some(6)]),
9978                None,
9979            ],
9980            3,
9981        )) as ArrayRef;
9982
9983        for (values, offsets, lengths) in &cases {
9984            let array = Arc::new(ListViewArray::new(
9985                field.clone(),
9986                offsets.clone().into(),
9987                lengths.clone().into(),
9988                Arc::new(Int32Array::from(values.clone())),
9989                Some(null_buffer.clone()),
9990            )) as ArrayRef;
9991            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9992            assert_eq!(expected.as_ref(), actual.as_ref());
9993        }
9994    }
9995
9996    #[test]
9997    fn test_cast_list_to_fsl_safety() {
9998        let values = vec![
9999            Some(vec![Some(1), Some(2), Some(3)]),
10000            Some(vec![Some(4), Some(5)]),
10001            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
10002            Some(vec![Some(3), Some(4), Some(5)]),
10003        ];
10004        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
10005            values.clone(),
10006        )) as ArrayRef;
10007
10008        let res = cast_with_options(
10009            array.as_ref(),
10010            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
10011            &CastOptions {
10012                safe: false,
10013                ..Default::default()
10014            },
10015        );
10016        assert!(res.is_err());
10017        assert!(
10018            format!("{res:?}")
10019                .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
10020        );
10021
10022        // When safe=true (default), the cast will fill nulls for lists that are
10023        // too short and truncate lists that are too long.
10024        let res = cast(
10025            array.as_ref(),
10026            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
10027        )
10028        .unwrap();
10029        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
10030            vec![
10031                Some(vec![Some(1), Some(2), Some(3)]),
10032                None, // Too short -> replaced with null
10033                None, // Too long -> replaced with null
10034                Some(vec![Some(3), Some(4), Some(5)]),
10035            ],
10036            3,
10037        )) as ArrayRef;
10038        assert_eq!(expected.as_ref(), res.as_ref());
10039
10040        // The safe option is false and the source array contains a null list.
10041        // issue: https://github.com/apache/arrow-rs/issues/5642
10042        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
10043            Some(vec![Some(1), Some(2), Some(3)]),
10044            None,
10045        ])) as ArrayRef;
10046        let res = cast_with_options(
10047            array.as_ref(),
10048            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
10049            &CastOptions {
10050                safe: false,
10051                ..Default::default()
10052            },
10053        )
10054        .unwrap();
10055        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
10056            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
10057            3,
10058        )) as ArrayRef;
10059        assert_eq!(expected.as_ref(), res.as_ref());
10060    }
10061
10062    #[test]
10063    fn test_cast_list_view_to_fsl_safety() {
10064        let values = vec![
10065            Some(vec![Some(1), Some(2), Some(3)]),
10066            Some(vec![Some(4), Some(5)]),
10067            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
10068            Some(vec![Some(3), Some(4), Some(5)]),
10069        ];
10070        let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(
10071            values.clone(),
10072        )) as ArrayRef;
10073
10074        let res = cast_with_options(
10075            array.as_ref(),
10076            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
10077            &CastOptions {
10078                safe: false,
10079                ..Default::default()
10080            },
10081        );
10082        assert!(res.is_err());
10083        assert!(
10084            format!("{res:?}")
10085                .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
10086        );
10087
10088        // When safe=true (default), the cast will fill nulls for lists that are
10089        // too short and truncate lists that are too long.
10090        let res = cast(
10091            array.as_ref(),
10092            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
10093        )
10094        .unwrap();
10095        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
10096            vec![
10097                Some(vec![Some(1), Some(2), Some(3)]),
10098                None, // Too short -> replaced with null
10099                None, // Too long -> replaced with null
10100                Some(vec![Some(3), Some(4), Some(5)]),
10101            ],
10102            3,
10103        )) as ArrayRef;
10104        assert_eq!(expected.as_ref(), res.as_ref());
10105
10106        // The safe option is false and the source array contains a null list.
10107        // issue: https://github.com/apache/arrow-rs/issues/5642
10108        let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(vec![
10109            Some(vec![Some(1), Some(2), Some(3)]),
10110            None,
10111        ])) as ArrayRef;
10112        let res = cast_with_options(
10113            array.as_ref(),
10114            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
10115            &CastOptions {
10116                safe: false,
10117                ..Default::default()
10118            },
10119        )
10120        .unwrap();
10121        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
10122            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
10123            3,
10124        )) as ArrayRef;
10125        assert_eq!(expected.as_ref(), res.as_ref());
10126    }
10127
10128    #[test]
10129    fn test_cast_large_list_to_fsl() {
10130        let values = vec![Some(vec![Some(1), Some(2)]), Some(vec![Some(3), Some(4)])];
10131        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
10132            values.clone(),
10133            2,
10134        )) as ArrayRef;
10135        let target_type =
10136            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 2);
10137
10138        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
10139            values.clone(),
10140        )) as ArrayRef;
10141        let actual = cast(array.as_ref(), &target_type).unwrap();
10142        assert_eq!(expected.as_ref(), actual.as_ref());
10143
10144        let array = Arc::new(LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(
10145            values.clone(),
10146        )) as ArrayRef;
10147        let actual = cast(array.as_ref(), &target_type).unwrap();
10148        assert_eq!(expected.as_ref(), actual.as_ref());
10149    }
10150
10151    #[test]
10152    fn test_cast_list_to_fsl_subcast() {
10153        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
10154            vec![
10155                Some(vec![Some(1), Some(2)]),
10156                Some(vec![Some(3), Some(i32::MAX)]),
10157            ],
10158        )) as ArrayRef;
10159        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
10160            vec![
10161                Some(vec![Some(1), Some(2)]),
10162                Some(vec![Some(3), Some(i32::MAX as i64)]),
10163            ],
10164            2,
10165        )) as ArrayRef;
10166        let actual = cast(
10167            array.as_ref(),
10168            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 2),
10169        )
10170        .unwrap();
10171        assert_eq!(expected.as_ref(), actual.as_ref());
10172
10173        let res = cast_with_options(
10174            array.as_ref(),
10175            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int16, true)), 2),
10176            &CastOptions {
10177                safe: false,
10178                ..Default::default()
10179            },
10180        );
10181        assert!(res.is_err());
10182        assert!(format!("{res:?}").contains("Can't cast value 2147483647 to type Int16"));
10183    }
10184
10185    #[test]
10186    fn test_cast_list_to_fsl_empty() {
10187        let inner_field = Arc::new(Field::new_list_field(DataType::Int32, true));
10188        let target_type = DataType::FixedSizeList(inner_field.clone(), 3);
10189        let expected = new_empty_array(&target_type);
10190
10191        // list
10192        let array = new_empty_array(&DataType::List(inner_field.clone()));
10193        assert!(can_cast_types(array.data_type(), &target_type));
10194        let actual = cast(array.as_ref(), &target_type).unwrap();
10195        assert_eq!(expected.as_ref(), actual.as_ref());
10196
10197        // largelist
10198        let array = new_empty_array(&DataType::LargeList(inner_field.clone()));
10199        assert!(can_cast_types(array.data_type(), &target_type));
10200        let actual = cast(array.as_ref(), &target_type).unwrap();
10201        assert_eq!(expected.as_ref(), actual.as_ref());
10202
10203        // listview
10204        let array = new_empty_array(&DataType::ListView(inner_field.clone()));
10205        assert!(can_cast_types(array.data_type(), &target_type));
10206        let actual = cast(array.as_ref(), &target_type).unwrap();
10207        assert_eq!(expected.as_ref(), actual.as_ref());
10208
10209        // largelistview
10210        let array = new_empty_array(&DataType::LargeListView(inner_field.clone()));
10211        assert!(can_cast_types(array.data_type(), &target_type));
10212        let actual = cast(array.as_ref(), &target_type).unwrap();
10213        assert_eq!(expected.as_ref(), actual.as_ref());
10214    }
10215
10216    fn make_list_array() -> ArrayRef {
10217        // [[0, 1, 2], [3, 4, 5], [6, 7]]
10218        Arc::new(ListArray::new(
10219            Field::new_list_field(DataType::Int32, true).into(),
10220            OffsetBuffer::from_lengths(vec![3, 3, 2]),
10221            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10222            None,
10223        ))
10224    }
10225
10226    fn make_large_list_array() -> ArrayRef {
10227        // [[0, 1, 2], [3, 4, 5], [6, 7]]
10228        Arc::new(LargeListArray::new(
10229            Field::new_list_field(DataType::Int32, true).into(),
10230            OffsetBuffer::from_lengths(vec![3, 3, 2]),
10231            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10232            None,
10233        ))
10234    }
10235
10236    fn make_list_view_array() -> ArrayRef {
10237        // [[0, 1, 2], [3, 4, 5], [6, 7]]
10238        Arc::new(ListViewArray::new(
10239            Field::new_list_field(DataType::Int32, true).into(),
10240            vec![0, 3, 6].into(),
10241            vec![3, 3, 2].into(),
10242            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10243            None,
10244        ))
10245    }
10246
10247    fn make_large_list_view_array() -> ArrayRef {
10248        // [[0, 1, 2], [3, 4, 5], [6, 7]]
10249        Arc::new(LargeListViewArray::new(
10250            Field::new_list_field(DataType::Int32, true).into(),
10251            vec![0, 3, 6].into(),
10252            vec![3, 3, 2].into(),
10253            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10254            None,
10255        ))
10256    }
10257
10258    fn make_fixed_size_list_array() -> ArrayRef {
10259        // [[0, 1, 2, 3], [4, 5, 6, 7]]
10260        Arc::new(FixedSizeListArray::new(
10261            Field::new_list_field(DataType::Int32, true).into(),
10262            4,
10263            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10264            None,
10265        ))
10266    }
10267
10268    fn make_fixed_size_list_array_for_large_list() -> ArrayRef {
10269        // [[0, 1, 2, 3], [4, 5, 6, 7]]
10270        Arc::new(FixedSizeListArray::new(
10271            Field::new_list_field(DataType::Int64, true).into(),
10272            4,
10273            Arc::new(Int64Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10274            None,
10275        ))
10276    }
10277
10278    #[test]
10279    fn test_cast_map_dont_allow_change_of_order() {
10280        let string_builder = StringBuilder::new();
10281        let value_builder = StringBuilder::new();
10282        let mut builder = MapBuilder::new(None, string_builder, value_builder);
10283
10284        builder.keys().append_value("0");
10285        builder.values().append_value("test_val_1");
10286        builder.append(true).unwrap();
10287        builder.keys().append_value("1");
10288        builder.values().append_value("test_val_2");
10289        builder.append(true).unwrap();
10290
10291        // map builder returns unsorted map by default
10292        let array = builder.finish();
10293
10294        let new_ordered = true;
10295        let new_type = DataType::Map(
10296            Arc::new(Field::new(
10297                Field::MAP_ENTRIES_FIELD_DEFAULT_NAME,
10298                DataType::Struct(
10299                    vec![
10300                        Field::new(Field::MAP_KEY_FIELD_DEFAULT_NAME, DataType::Utf8, false),
10301                        Field::new(Field::MAP_VALUE_FIELD_DEFAULT_NAME, DataType::Utf8, false),
10302                    ]
10303                    .into(),
10304                ),
10305                false,
10306            )),
10307            new_ordered,
10308        );
10309
10310        let new_array_result = cast(&array, &new_type.clone());
10311        assert!(!can_cast_types(array.data_type(), &new_type));
10312        let Err(ArrowError::CastError(t)) = new_array_result else {
10313            panic!();
10314        };
10315        assert_eq!(
10316            t,
10317            r#"Casting from Map("entries": non-null Struct("key": non-null Utf8, "value": Utf8), unsorted) to Map("entries": non-null Struct("key": non-null Utf8, "value": non-null Utf8), sorted) not supported"#
10318        );
10319    }
10320
10321    #[test]
10322    fn test_cast_map_dont_allow_when_container_cant_cast() {
10323        let string_builder = StringBuilder::new();
10324        let value_builder = IntervalDayTimeArray::builder(2);
10325        let mut builder = MapBuilder::new(None, string_builder, value_builder);
10326
10327        builder.keys().append_value("0");
10328        builder.values().append_value(IntervalDayTime::new(1, 1));
10329        builder.append(true).unwrap();
10330        builder.keys().append_value("1");
10331        builder.values().append_value(IntervalDayTime::new(2, 2));
10332        builder.append(true).unwrap();
10333
10334        // map builder returns unsorted map by default
10335        let array = builder.finish();
10336
10337        let new_ordered = true;
10338        let new_type = DataType::Map(
10339            Arc::new(Field::new(
10340                Field::MAP_ENTRIES_FIELD_DEFAULT_NAME,
10341                DataType::Struct(
10342                    vec![
10343                        Field::new(Field::MAP_KEY_FIELD_DEFAULT_NAME, DataType::Utf8, false),
10344                        Field::new(
10345                            Field::MAP_VALUE_FIELD_DEFAULT_NAME,
10346                            DataType::Duration(TimeUnit::Second),
10347                            false,
10348                        ),
10349                    ]
10350                    .into(),
10351                ),
10352                false,
10353            )),
10354            new_ordered,
10355        );
10356
10357        let new_array_result = cast(&array, &new_type.clone());
10358        assert!(!can_cast_types(array.data_type(), &new_type));
10359        let Err(ArrowError::CastError(t)) = new_array_result else {
10360            panic!();
10361        };
10362        assert_eq!(
10363            t,
10364            r#"Casting from Map("entries": non-null Struct("key": non-null Utf8, "value": Interval(DayTime)), unsorted) to Map("entries": non-null Struct("key": non-null Utf8, "value": non-null Duration(s)), sorted) not supported"#
10365        );
10366    }
10367
10368    #[test]
10369    fn test_cast_map_field_names() {
10370        let string_builder = StringBuilder::new();
10371        let value_builder = StringBuilder::new();
10372        let mut builder = MapBuilder::new(
10373            Some(MapFieldNames {
10374                // Explicitly writing the name so it will be apparent from what names to what names are we converting to
10375                entry: Field::MAP_ENTRIES_FIELD_DEFAULT_NAME.to_string(),
10376                key: Field::MAP_KEY_FIELD_DEFAULT_NAME.to_string(),
10377                value: Field::MAP_VALUE_FIELD_DEFAULT_NAME.to_string(),
10378            }),
10379            string_builder,
10380            value_builder,
10381        );
10382
10383        builder.keys().append_value("0");
10384        builder.values().append_value("test_val_1");
10385        builder.append(true).unwrap();
10386        builder.keys().append_value("1");
10387        builder.values().append_value("test_val_2");
10388        builder.append(true).unwrap();
10389        builder.append(false).unwrap();
10390
10391        let array = builder.finish();
10392
10393        let new_type = DataType::Map(
10394            Arc::new(Field::new(
10395                "entries_new",
10396                DataType::Struct(
10397                    vec![
10398                        Field::new("key_new", DataType::Utf8, false),
10399                        Field::new("value_values", DataType::Utf8, false),
10400                    ]
10401                    .into(),
10402                ),
10403                false,
10404            )),
10405            false,
10406        );
10407
10408        assert_ne!(new_type, array.data_type().clone());
10409
10410        let new_array = cast(&array, &new_type.clone()).unwrap();
10411        assert_eq!(new_type, new_array.data_type().clone());
10412        let map_array = new_array.as_map();
10413
10414        assert_ne!(new_type, array.data_type().clone());
10415        assert_eq!(new_type, map_array.data_type().clone());
10416
10417        let key_string = map_array
10418            .keys()
10419            .as_any()
10420            .downcast_ref::<StringArray>()
10421            .unwrap()
10422            .into_iter()
10423            .flatten()
10424            .collect::<Vec<_>>();
10425        assert_eq!(&key_string, &vec!["0", "1"]);
10426
10427        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
10428        let values_string = values_string_array
10429            .as_any()
10430            .downcast_ref::<StringArray>()
10431            .unwrap()
10432            .into_iter()
10433            .flatten()
10434            .collect::<Vec<_>>();
10435        assert_eq!(&values_string, &vec!["test_val_1", "test_val_2"]);
10436
10437        assert_eq!(
10438            map_array.nulls(),
10439            Some(&NullBuffer::from(vec![true, true, false]))
10440        );
10441    }
10442
10443    #[test]
10444    fn test_cast_map_contained_values() {
10445        let string_builder = StringBuilder::new();
10446        let value_builder = Int8Builder::new();
10447        let mut builder = MapBuilder::new(None, string_builder, value_builder);
10448
10449        builder.keys().append_value("0");
10450        builder.values().append_value(44);
10451        builder.append(true).unwrap();
10452        builder.keys().append_value("1");
10453        builder.values().append_value(22);
10454        builder.append(true).unwrap();
10455
10456        let array = builder.finish();
10457
10458        let new_type = DataType::Map(
10459            Arc::new(Field::new(
10460                Field::MAP_ENTRIES_FIELD_DEFAULT_NAME,
10461                DataType::Struct(
10462                    vec![
10463                        Field::new(Field::MAP_KEY_FIELD_DEFAULT_NAME, DataType::Utf8, false),
10464                        Field::new(Field::MAP_VALUE_FIELD_DEFAULT_NAME, DataType::Utf8, false),
10465                    ]
10466                    .into(),
10467                ),
10468                false,
10469            )),
10470            false,
10471        );
10472
10473        let new_array = cast(&array, &new_type.clone()).unwrap();
10474        assert_eq!(new_type, new_array.data_type().clone());
10475        let map_array = new_array.as_map();
10476
10477        assert_ne!(new_type, array.data_type().clone());
10478        assert_eq!(new_type, map_array.data_type().clone());
10479
10480        let key_string = map_array
10481            .keys()
10482            .as_any()
10483            .downcast_ref::<StringArray>()
10484            .unwrap()
10485            .into_iter()
10486            .flatten()
10487            .collect::<Vec<_>>();
10488        assert_eq!(&key_string, &vec!["0", "1"]);
10489
10490        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
10491        let values_string = values_string_array
10492            .as_any()
10493            .downcast_ref::<StringArray>()
10494            .unwrap()
10495            .into_iter()
10496            .flatten()
10497            .collect::<Vec<_>>();
10498        assert_eq!(&values_string, &vec!["44", "22"]);
10499    }
10500
10501    #[test]
10502    fn test_utf8_cast_offsets() {
10503        // test if offset of the array is taken into account during cast
10504        let str_array = StringArray::from(vec!["a", "b", "c"]);
10505        let str_array = str_array.slice(1, 2);
10506
10507        let out = cast(&str_array, &DataType::LargeUtf8).unwrap();
10508
10509        let large_str_array = out.as_any().downcast_ref::<LargeStringArray>().unwrap();
10510        let strs = large_str_array.into_iter().flatten().collect::<Vec<_>>();
10511        assert_eq!(strs, &["b", "c"])
10512    }
10513
10514    #[test]
10515    fn test_list_cast_offsets() {
10516        // test if offset of the array is taken into account during cast
10517        let array1 = make_list_array().slice(1, 2);
10518        let array2 = make_list_array();
10519
10520        let dt = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
10521        let out1 = cast(&array1, &dt).unwrap();
10522        let out2 = cast(&array2, &dt).unwrap();
10523
10524        assert_eq!(&out1, &out2.slice(1, 2))
10525    }
10526
10527    #[test]
10528    fn test_list_to_string() {
10529        fn assert_cast(array: &ArrayRef, expected: &[&str]) {
10530            assert!(can_cast_types(array.data_type(), &DataType::Utf8));
10531            let out = cast(array, &DataType::Utf8).unwrap();
10532            let out = out
10533                .as_string::<i32>()
10534                .into_iter()
10535                .flatten()
10536                .collect::<Vec<_>>();
10537            assert_eq!(out, expected);
10538
10539            assert!(can_cast_types(array.data_type(), &DataType::LargeUtf8));
10540            let out = cast(array, &DataType::LargeUtf8).unwrap();
10541            let out = out
10542                .as_string::<i64>()
10543                .into_iter()
10544                .flatten()
10545                .collect::<Vec<_>>();
10546            assert_eq!(out, expected);
10547
10548            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
10549            let out = cast(array, &DataType::Utf8View).unwrap();
10550            let out = out
10551                .as_string_view()
10552                .into_iter()
10553                .flatten()
10554                .collect::<Vec<_>>();
10555            assert_eq!(out, expected);
10556        }
10557
10558        let array = Arc::new(ListArray::new(
10559            Field::new_list_field(DataType::Utf8, true).into(),
10560            OffsetBuffer::from_lengths(vec![3, 3, 2]),
10561            Arc::new(StringArray::from(vec![
10562                "a", "b", "c", "d", "e", "f", "g", "h",
10563            ])),
10564            None,
10565        )) as ArrayRef;
10566
10567        assert_cast(&array, &["[a, b, c]", "[d, e, f]", "[g, h]"]);
10568
10569        let array = make_list_array();
10570        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10571
10572        let array = make_large_list_array();
10573        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10574
10575        let array = make_list_view_array();
10576        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10577
10578        let array = make_large_list_view_array();
10579        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10580    }
10581
10582    #[test]
10583    #[cfg_attr(miri, ignore)] // Takes too long
10584    fn test_cast_f64_to_decimal128() {
10585        // to reproduce https://github.com/apache/arrow-rs/issues/2997
10586
10587        let decimal_type = DataType::Decimal128(18, 2);
10588        let array = Float64Array::from(vec![
10589            Some(0.0699999999),
10590            Some(0.0659999999),
10591            Some(0.0650000000),
10592            Some(0.0649999999),
10593        ]);
10594        let array = Arc::new(array) as ArrayRef;
10595        generate_cast_test_case!(
10596            &array,
10597            Decimal128Array,
10598            &decimal_type,
10599            vec![
10600                Some(7_i128), // round up
10601                Some(7_i128), // round up
10602                Some(7_i128), // round up
10603                Some(6_i128), // round down
10604            ]
10605        );
10606
10607        let decimal_type = DataType::Decimal128(18, 3);
10608        let array = Float64Array::from(vec![
10609            Some(0.0699999999),
10610            Some(0.0659999999),
10611            Some(0.0650000000),
10612            Some(0.0649999999),
10613        ]);
10614        let array = Arc::new(array) as ArrayRef;
10615        generate_cast_test_case!(
10616            &array,
10617            Decimal128Array,
10618            &decimal_type,
10619            vec![
10620                Some(70_i128), // round up
10621                Some(66_i128), // round up
10622                Some(65_i128), // round down
10623                Some(65_i128), // round up
10624            ]
10625        );
10626    }
10627
10628    #[test]
10629    fn test_cast_numeric_to_decimal128_overflow() {
10630        let array = Int64Array::from(vec![i64::MAX]);
10631        let array = Arc::new(array) as ArrayRef;
10632        let casted_array = cast_with_options(
10633            &array,
10634            &DataType::Decimal128(38, 30),
10635            &CastOptions {
10636                safe: true,
10637                format_options: FormatOptions::default(),
10638            },
10639        );
10640        assert!(casted_array.is_ok());
10641        assert!(casted_array.unwrap().is_null(0));
10642
10643        let casted_array = cast_with_options(
10644            &array,
10645            &DataType::Decimal128(38, 30),
10646            &CastOptions {
10647                safe: false,
10648                format_options: FormatOptions::default(),
10649            },
10650        );
10651        assert!(casted_array.is_err());
10652    }
10653
10654    #[test]
10655    fn test_cast_numeric_to_decimal256_overflow() {
10656        let array = Int64Array::from(vec![i64::MAX]);
10657        let array = Arc::new(array) as ArrayRef;
10658        let casted_array = cast_with_options(
10659            &array,
10660            &DataType::Decimal256(76, 76),
10661            &CastOptions {
10662                safe: true,
10663                format_options: FormatOptions::default(),
10664            },
10665        );
10666        assert!(casted_array.is_ok());
10667        assert!(casted_array.unwrap().is_null(0));
10668
10669        let casted_array = cast_with_options(
10670            &array,
10671            &DataType::Decimal256(76, 76),
10672            &CastOptions {
10673                safe: false,
10674                format_options: FormatOptions::default(),
10675            },
10676        );
10677        assert!(casted_array.is_err());
10678    }
10679
10680    #[test]
10681    fn test_cast_integer_to_decimal32_does_not_truncate() {
10682        let array = Int64Array::from(vec![5_000_000_000i64, 10_000_000_000, 42]);
10683        let safe = CastOptions {
10684            safe: true,
10685            format_options: FormatOptions::default(),
10686        };
10687        let unsafe_opts = CastOptions {
10688            safe: false,
10689            format_options: FormatOptions::default(),
10690        };
10691
10692        let result = cast_with_options(&array, &DataType::Decimal32(9, 0), &safe).unwrap();
10693        let result = result.as_primitive::<Decimal32Type>();
10694        assert!(
10695            result.is_null(0),
10696            "5e9 must not wrap to {}",
10697            result.value(0)
10698        );
10699        assert!(result.is_null(1));
10700        assert_eq!(result.value(2), 42);
10701
10702        let err = cast_with_options(&array, &DataType::Decimal32(9, 0), &unsafe_opts)
10703            .unwrap_err()
10704            .to_string();
10705        assert_eq!(
10706            err,
10707            "Cast error: Cannot cast to Decimal32(9, 0). Overflowing on 5000000000"
10708        );
10709
10710        let result = cast_with_options(&array, &DataType::Decimal128(9, 0), &safe).unwrap();
10711        let result = result.as_primitive::<Decimal128Type>();
10712        assert!(result.is_null(0));
10713        assert!(result.is_null(1));
10714        assert_eq!(result.value(2), 42);
10715    }
10716
10717    #[test]
10718    fn test_cast_integer_to_decimal32_scales_before_narrowing() {
10719        let array = Int64Array::from(vec![5_000_000_000i64]);
10720        let safe = CastOptions {
10721            safe: true,
10722            format_options: FormatOptions::default(),
10723        };
10724        let unsafe_opts = CastOptions {
10725            safe: false,
10726            format_options: FormatOptions::default(),
10727        };
10728        let data_type = DataType::Decimal32(9, -1);
10729
10730        let result = cast_with_options(&array, &data_type, &safe).unwrap();
10731        let result = result.as_primitive::<Decimal32Type>();
10732        assert_eq!(result.value(0), 500_000_000);
10733
10734        let result = cast_with_options(&array, &data_type, &unsafe_opts).unwrap();
10735        let result = result.as_primitive::<Decimal32Type>();
10736        assert_eq!(result.value(0), 500_000_000);
10737    }
10738
10739    #[test]
10740    fn test_cast_uint_to_decimal32_does_not_wrap() {
10741        let array = UInt32Array::from(vec![4_000_000_000u32]);
10742        let safe = CastOptions {
10743            safe: true,
10744            format_options: FormatOptions::default(),
10745        };
10746        let unsafe_opts = CastOptions {
10747            safe: false,
10748            format_options: FormatOptions::default(),
10749        };
10750
10751        let result = cast_with_options(&array, &DataType::Decimal32(9, 0), &safe).unwrap();
10752        let result = result.as_primitive::<Decimal32Type>();
10753        assert!(
10754            result.is_null(0),
10755            "u32 4e9 must not wrap to {}",
10756            result.value(0)
10757        );
10758
10759        let err = cast_with_options(&array, &DataType::Decimal32(9, 0), &unsafe_opts)
10760            .unwrap_err()
10761            .to_string();
10762        assert_eq!(
10763            err,
10764            "Cast error: Cannot cast to Decimal32(9, 0). Overflowing on 4000000000"
10765        );
10766
10767        let result = cast_with_options(&array, &DataType::Decimal128(9, 0), &safe).unwrap();
10768        assert!(result.is_null(0));
10769        assert!(cast_with_options(&array, &DataType::Decimal128(9, 0), &unsafe_opts).is_err());
10770    }
10771
10772    #[test]
10773    fn test_cast_uint64_max_to_decimal64_does_not_wrap() {
10774        let array = UInt64Array::from(vec![u64::MAX]);
10775        let unsafe_opts = CastOptions {
10776            safe: false,
10777            format_options: FormatOptions::default(),
10778        };
10779
10780        let err = cast_with_options(&array, &DataType::Decimal64(18, 0), &unsafe_opts)
10781            .unwrap_err()
10782            .to_string();
10783        assert_eq!(
10784            err,
10785            "Cast error: Cannot cast to Decimal64(18, 0). Overflowing on 18446744073709551615"
10786        );
10787
10788        assert!(cast_with_options(&array, &DataType::Decimal128(18, 0), &unsafe_opts).is_err());
10789    }
10790
10791    #[test]
10792    fn test_cast_floating_point_to_decimal128_precision_overflow() {
10793        let array = Float64Array::from(vec![1.1]);
10794        let array = Arc::new(array) as ArrayRef;
10795        let casted_array = cast_with_options(
10796            &array,
10797            &DataType::Decimal128(2, 2),
10798            &CastOptions {
10799                safe: true,
10800                format_options: FormatOptions::default(),
10801            },
10802        );
10803        assert!(casted_array.is_ok());
10804        assert!(casted_array.unwrap().is_null(0));
10805
10806        let casted_array = cast_with_options(
10807            &array,
10808            &DataType::Decimal128(2, 2),
10809            &CastOptions {
10810                safe: false,
10811                format_options: FormatOptions::default(),
10812            },
10813        );
10814        let err = casted_array.unwrap_err().to_string();
10815        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
10816        assert!(
10817            err.contains(expected_error),
10818            "did not find expected error '{expected_error}' in actual error '{err}'"
10819        );
10820    }
10821
10822    #[test]
10823    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
10824    fn test_cast_float16_to_decimal128_precision_overflow() {
10825        let array = Float16Array::from(vec![f16::from_f32(1.1)]);
10826        let array = Arc::new(array) as ArrayRef;
10827        let casted_array = cast_with_options(
10828            &array,
10829            &DataType::Decimal128(2, 2),
10830            &CastOptions {
10831                safe: true,
10832                format_options: FormatOptions::default(),
10833            },
10834        );
10835        assert!(casted_array.is_ok());
10836        assert!(casted_array.unwrap().is_null(0));
10837
10838        let casted_array = cast_with_options(
10839            &array,
10840            &DataType::Decimal128(2, 2),
10841            &CastOptions {
10842                safe: false,
10843                format_options: FormatOptions::default(),
10844            },
10845        );
10846        let err = casted_array.unwrap_err().to_string();
10847        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
10848        assert_eq!(err, expected_error);
10849    }
10850
10851    #[test]
10852    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
10853    fn test_cast_float16_to_decimal256_precision_overflow() {
10854        let array = Float16Array::from(vec![f16::from_f32(1.1)]);
10855        let array = Arc::new(array) as ArrayRef;
10856        let casted_array = cast_with_options(
10857            &array,
10858            &DataType::Decimal256(2, 2),
10859            &CastOptions {
10860                safe: true,
10861                format_options: FormatOptions::default(),
10862            },
10863        );
10864        assert!(casted_array.is_ok());
10865        assert!(casted_array.unwrap().is_null(0));
10866
10867        let casted_array = cast_with_options(
10868            &array,
10869            &DataType::Decimal256(2, 2),
10870            &CastOptions {
10871                safe: false,
10872                format_options: FormatOptions::default(),
10873            },
10874        );
10875        let err = casted_array.unwrap_err().to_string();
10876        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
10877        assert_eq!(err, expected_error);
10878    }
10879
10880    #[test]
10881    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
10882    fn test_cast_float16_to_decimal128_non_finite() {
10883        let array = Float16Array::from(vec![f16::NAN, f16::INFINITY, f16::NEG_INFINITY]);
10884        let array = Arc::new(array) as ArrayRef;
10885        let casted_array = cast_with_options(
10886            &array,
10887            &DataType::Decimal128(38, 2),
10888            &CastOptions {
10889                safe: true,
10890                format_options: FormatOptions::default(),
10891            },
10892        )
10893        .unwrap();
10894
10895        assert!(casted_array.is_null(0));
10896        assert!(casted_array.is_null(1));
10897        assert!(casted_array.is_null(2));
10898
10899        let casted_array = cast_with_options(
10900            &array,
10901            &DataType::Decimal128(38, 2),
10902            &CastOptions {
10903                safe: false,
10904                format_options: FormatOptions::default(),
10905            },
10906        );
10907        let err = casted_array.unwrap_err().to_string();
10908        let expected_error = "Cannot cast to Decimal128(38, 2)";
10909        assert!(
10910            err.contains(expected_error),
10911            "did not find expected error '{expected_error}' in actual error '{err}'"
10912        );
10913    }
10914
10915    #[test]
10916    fn test_cast_floating_point_to_decimal256_precision_overflow() {
10917        let array = Float64Array::from(vec![1.1]);
10918        let array = Arc::new(array) as ArrayRef;
10919        let casted_array = cast_with_options(
10920            &array,
10921            &DataType::Decimal256(2, 2),
10922            &CastOptions {
10923                safe: true,
10924                format_options: FormatOptions::default(),
10925            },
10926        );
10927        assert!(casted_array.is_ok());
10928        assert!(casted_array.unwrap().is_null(0));
10929
10930        let casted_array = cast_with_options(
10931            &array,
10932            &DataType::Decimal256(2, 2),
10933            &CastOptions {
10934                safe: false,
10935                format_options: FormatOptions::default(),
10936            },
10937        );
10938        let err = casted_array.unwrap_err().to_string();
10939        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
10940        assert_eq!(err, expected_error);
10941    }
10942
10943    #[test]
10944    fn test_cast_floating_point_to_decimal128_overflow() {
10945        let array = Float64Array::from(vec![f64::MAX]);
10946        let array = Arc::new(array) as ArrayRef;
10947        let casted_array = cast_with_options(
10948            &array,
10949            &DataType::Decimal128(38, 30),
10950            &CastOptions {
10951                safe: true,
10952                format_options: FormatOptions::default(),
10953            },
10954        );
10955        assert!(casted_array.is_ok());
10956        assert!(casted_array.unwrap().is_null(0));
10957
10958        let casted_array = cast_with_options(
10959            &array,
10960            &DataType::Decimal128(38, 30),
10961            &CastOptions {
10962                safe: false,
10963                format_options: FormatOptions::default(),
10964            },
10965        );
10966        let err = casted_array.unwrap_err().to_string();
10967        let expected_error = "Cast error: Cannot cast to Decimal128(38, 30)";
10968        assert!(
10969            err.contains(expected_error),
10970            "did not find expected error '{expected_error}' in actual error '{err}'"
10971        );
10972    }
10973
10974    #[test]
10975    fn test_cast_floating_point_to_decimal256_overflow() {
10976        let array = Float64Array::from(vec![f64::MAX]);
10977        let array = Arc::new(array) as ArrayRef;
10978        let casted_array = cast_with_options(
10979            &array,
10980            &DataType::Decimal256(76, 50),
10981            &CastOptions {
10982                safe: true,
10983                format_options: FormatOptions::default(),
10984            },
10985        );
10986        assert!(casted_array.is_ok());
10987        assert!(casted_array.unwrap().is_null(0));
10988
10989        let casted_array = cast_with_options(
10990            &array,
10991            &DataType::Decimal256(76, 50),
10992            &CastOptions {
10993                safe: false,
10994                format_options: FormatOptions::default(),
10995            },
10996        );
10997        let err = casted_array.unwrap_err().to_string();
10998        let expected_error = "Cast error: Cannot cast to Decimal256(76, 50)";
10999        assert!(
11000            err.contains(expected_error),
11001            "did not find expected error '{expected_error}' in actual error '{err}'"
11002        );
11003    }
11004    #[test]
11005    fn test_cast_decimal256_to_f64_no_overflow() {
11006        // Test casting i256::MAX: should produce a large finite positive value
11007        let array = vec![Some(i256::MAX)];
11008        let array = create_decimal256_array(array, 76, 2).unwrap();
11009        let array = Arc::new(array) as ArrayRef;
11010
11011        let result = cast(&array, &DataType::Float64).unwrap();
11012        let result = result.as_primitive::<Float64Type>();
11013        assert!(result.value(0).is_finite());
11014        assert!(result.value(0) > 0.0); // Positive result
11015
11016        // Test casting i256::MIN: should produce a large finite negative value
11017        let array = vec![Some(i256::MIN)];
11018        let array = create_decimal256_array(array, 76, 2).unwrap();
11019        let array = Arc::new(array) as ArrayRef;
11020
11021        let result = cast(&array, &DataType::Float64).unwrap();
11022        let result = result.as_primitive::<Float64Type>();
11023        assert!(result.value(0).is_finite());
11024        assert!(result.value(0) < 0.0); // Negative result
11025    }
11026
11027    #[test]
11028    fn test_cast_decimal128_to_decimal128_negative_scale() {
11029        let input_type = DataType::Decimal128(20, 0);
11030        let output_type = DataType::Decimal128(20, -1);
11031        assert!(can_cast_types(&input_type, &output_type));
11032        let array = vec![Some(1123450), Some(2123455), Some(3123456), None];
11033        let input_decimal_array = create_decimal128_array(array, 20, 0).unwrap();
11034        let array = Arc::new(input_decimal_array) as ArrayRef;
11035        generate_cast_test_case!(
11036            &array,
11037            Decimal128Array,
11038            &output_type,
11039            vec![
11040                Some(112345_i128),
11041                Some(212346_i128),
11042                Some(312346_i128),
11043                None
11044            ]
11045        );
11046
11047        let casted_array = cast(&array, &output_type).unwrap();
11048        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11049
11050        assert_eq!("1123450", decimal_arr.value_as_string(0));
11051        assert_eq!("2123460", decimal_arr.value_as_string(1));
11052        assert_eq!("3123460", decimal_arr.value_as_string(2));
11053    }
11054
11055    #[test]
11056    fn decimal128_min_max_to_f64() {
11057        // Ensure Decimal128 i128::MIN/MAX round-trip cast
11058        let min128 = i128::MIN;
11059        let max128 = i128::MAX;
11060        assert_eq!(min128 as f64, min128 as f64);
11061        assert_eq!(max128 as f64, max128 as f64);
11062    }
11063
11064    #[test]
11065    fn test_cast_numeric_to_decimal128_negative() {
11066        let decimal_type = DataType::Decimal128(38, -1);
11067        let array = Arc::new(Int32Array::from(vec![
11068            Some(1123456),
11069            Some(2123456),
11070            Some(3123456),
11071        ])) as ArrayRef;
11072
11073        let casted_array = cast(&array, &decimal_type).unwrap();
11074        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11075
11076        assert_eq!("1123450", decimal_arr.value_as_string(0));
11077        assert_eq!("2123450", decimal_arr.value_as_string(1));
11078        assert_eq!("3123450", decimal_arr.value_as_string(2));
11079
11080        let array = Arc::new(Float32Array::from(vec![
11081            Some(1123.456),
11082            Some(2123.456),
11083            Some(3123.456),
11084        ])) as ArrayRef;
11085
11086        let casted_array = cast(&array, &decimal_type).unwrap();
11087        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11088
11089        assert_eq!("1120", decimal_arr.value_as_string(0));
11090        assert_eq!("2120", decimal_arr.value_as_string(1));
11091        assert_eq!("3120", decimal_arr.value_as_string(2));
11092    }
11093
11094    #[test]
11095    fn test_cast_decimal128_to_decimal128_negative() {
11096        let input_type = DataType::Decimal128(10, -1);
11097        let output_type = DataType::Decimal128(10, -2);
11098        assert!(can_cast_types(&input_type, &output_type));
11099        let array = vec![Some(123)];
11100        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
11101        let array = Arc::new(input_decimal_array) as ArrayRef;
11102        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(12_i128),]);
11103
11104        let casted_array = cast(&array, &output_type).unwrap();
11105        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11106
11107        assert_eq!("1200", decimal_arr.value_as_string(0));
11108
11109        let array = vec![Some(125)];
11110        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
11111        let array = Arc::new(input_decimal_array) as ArrayRef;
11112        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(13_i128),]);
11113
11114        let casted_array = cast(&array, &output_type).unwrap();
11115        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11116
11117        assert_eq!("1300", decimal_arr.value_as_string(0));
11118    }
11119
11120    #[test]
11121    fn test_cast_decimal128_to_decimal256_negative() {
11122        let input_type = DataType::Decimal128(10, 3);
11123        let output_type = DataType::Decimal256(10, 5);
11124        assert!(can_cast_types(&input_type, &output_type));
11125        let array = vec![Some(123456), Some(-123456)];
11126        let input_decimal_array = create_decimal128_array(array, 10, 3).unwrap();
11127        let array = Arc::new(input_decimal_array) as ArrayRef;
11128
11129        let hundred = i256::from_i128(100);
11130        generate_cast_test_case!(
11131            &array,
11132            Decimal256Array,
11133            &output_type,
11134            vec![
11135                Some(i256::from_i128(123456).mul_wrapping(hundred)),
11136                Some(i256::from_i128(-123456).mul_wrapping(hundred))
11137            ]
11138        );
11139    }
11140
11141    #[test]
11142    fn test_parse_string_to_decimal() {
11143        assert_eq!(
11144            Decimal128Type::format_decimal(
11145                parse_string_to_decimal_native::<Decimal128Type>("123.45", 2).unwrap(),
11146                38,
11147                2,
11148            ),
11149            "123.45"
11150        );
11151        assert_eq!(
11152            Decimal128Type::format_decimal(
11153                parse_string_to_decimal_native::<Decimal128Type>("12345", 2).unwrap(),
11154                38,
11155                2,
11156            ),
11157            "12345.00"
11158        );
11159        assert_eq!(
11160            Decimal128Type::format_decimal(
11161                parse_string_to_decimal_native::<Decimal128Type>("0.12345", 2).unwrap(),
11162                38,
11163                2,
11164            ),
11165            "0.12"
11166        );
11167        assert_eq!(
11168            Decimal128Type::format_decimal(
11169                parse_string_to_decimal_native::<Decimal128Type>(".12345", 2).unwrap(),
11170                38,
11171                2,
11172            ),
11173            "0.12"
11174        );
11175        assert_eq!(
11176            Decimal128Type::format_decimal(
11177                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
11178                38,
11179                2,
11180            ),
11181            "0.13"
11182        );
11183        assert_eq!(
11184            Decimal128Type::format_decimal(
11185                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
11186                38,
11187                2,
11188            ),
11189            "0.13"
11190        );
11191
11192        assert_eq!(
11193            Decimal256Type::format_decimal(
11194                parse_string_to_decimal_native::<Decimal256Type>("123.45", 3).unwrap(),
11195                38,
11196                3,
11197            ),
11198            "123.450"
11199        );
11200        assert_eq!(
11201            Decimal256Type::format_decimal(
11202                parse_string_to_decimal_native::<Decimal256Type>("12345", 3).unwrap(),
11203                38,
11204                3,
11205            ),
11206            "12345.000"
11207        );
11208        assert_eq!(
11209            Decimal256Type::format_decimal(
11210                parse_string_to_decimal_native::<Decimal256Type>("0.12345", 3).unwrap(),
11211                38,
11212                3,
11213            ),
11214            "0.123"
11215        );
11216        assert_eq!(
11217            Decimal256Type::format_decimal(
11218                parse_string_to_decimal_native::<Decimal256Type>(".12345", 3).unwrap(),
11219                38,
11220                3,
11221            ),
11222            "0.123"
11223        );
11224        assert_eq!(
11225            Decimal256Type::format_decimal(
11226                parse_string_to_decimal_native::<Decimal256Type>(".1265", 3).unwrap(),
11227                38,
11228                3,
11229            ),
11230            "0.127"
11231        );
11232    }
11233
11234    fn test_cast_string_to_decimal(array: ArrayRef) {
11235        // Decimal128
11236        let output_type = DataType::Decimal128(38, 2);
11237        assert!(can_cast_types(array.data_type(), &output_type));
11238
11239        let casted_array = cast(&array, &output_type).unwrap();
11240        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11241
11242        assert_eq!("123.45", decimal_arr.value_as_string(0));
11243        assert_eq!("1.23", decimal_arr.value_as_string(1));
11244        assert_eq!("0.12", decimal_arr.value_as_string(2));
11245        assert_eq!("0.13", decimal_arr.value_as_string(3));
11246        assert_eq!("1.26", decimal_arr.value_as_string(4));
11247        assert_eq!("12345.00", decimal_arr.value_as_string(5));
11248        assert_eq!("12345.00", decimal_arr.value_as_string(6));
11249        assert_eq!("0.12", decimal_arr.value_as_string(7));
11250        assert_eq!("12.23", decimal_arr.value_as_string(8));
11251        assert!(decimal_arr.is_null(9));
11252        assert!(decimal_arr.is_null(10));
11253        assert!(decimal_arr.is_null(11));
11254        assert!(decimal_arr.is_null(12));
11255        assert_eq!("-1.23", decimal_arr.value_as_string(13));
11256        assert_eq!("-1.24", decimal_arr.value_as_string(14));
11257        assert_eq!("0.00", decimal_arr.value_as_string(15));
11258        assert_eq!("-123.00", decimal_arr.value_as_string(16));
11259        assert_eq!("-123.23", decimal_arr.value_as_string(17));
11260        assert_eq!("-0.12", decimal_arr.value_as_string(18));
11261        assert_eq!("1.23", decimal_arr.value_as_string(19));
11262        assert_eq!("1.24", decimal_arr.value_as_string(20));
11263        assert_eq!("0.00", decimal_arr.value_as_string(21));
11264        assert_eq!("123.00", decimal_arr.value_as_string(22));
11265        assert_eq!("123.23", decimal_arr.value_as_string(23));
11266        assert_eq!("0.12", decimal_arr.value_as_string(24));
11267        assert!(decimal_arr.is_null(25));
11268        assert!(decimal_arr.is_null(26));
11269        assert!(decimal_arr.is_null(27));
11270        assert_eq!("0.00", decimal_arr.value_as_string(28));
11271        assert_eq!("0.00", decimal_arr.value_as_string(29));
11272        assert_eq!("12345.00", decimal_arr.value_as_string(30));
11273        assert_eq!(decimal_arr.len(), 31);
11274
11275        // Decimal256
11276        let output_type = DataType::Decimal256(76, 3);
11277        assert!(can_cast_types(array.data_type(), &output_type));
11278
11279        let casted_array = cast(&array, &output_type).unwrap();
11280        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
11281
11282        assert_eq!("123.450", decimal_arr.value_as_string(0));
11283        assert_eq!("1.235", decimal_arr.value_as_string(1));
11284        assert_eq!("0.123", decimal_arr.value_as_string(2));
11285        assert_eq!("0.127", decimal_arr.value_as_string(3));
11286        assert_eq!("1.263", decimal_arr.value_as_string(4));
11287        assert_eq!("12345.000", decimal_arr.value_as_string(5));
11288        assert_eq!("12345.000", decimal_arr.value_as_string(6));
11289        assert_eq!("0.123", decimal_arr.value_as_string(7));
11290        assert_eq!("12.234", decimal_arr.value_as_string(8));
11291        assert!(decimal_arr.is_null(9));
11292        assert!(decimal_arr.is_null(10));
11293        assert!(decimal_arr.is_null(11));
11294        assert!(decimal_arr.is_null(12));
11295        assert_eq!("-1.235", decimal_arr.value_as_string(13));
11296        assert_eq!("-1.236", decimal_arr.value_as_string(14));
11297        assert_eq!("0.000", decimal_arr.value_as_string(15));
11298        assert_eq!("-123.000", decimal_arr.value_as_string(16));
11299        assert_eq!("-123.234", decimal_arr.value_as_string(17));
11300        assert_eq!("-0.123", decimal_arr.value_as_string(18));
11301        assert_eq!("1.235", decimal_arr.value_as_string(19));
11302        assert_eq!("1.236", decimal_arr.value_as_string(20));
11303        assert_eq!("0.000", decimal_arr.value_as_string(21));
11304        assert_eq!("123.000", decimal_arr.value_as_string(22));
11305        assert_eq!("123.234", decimal_arr.value_as_string(23));
11306        assert_eq!("0.123", decimal_arr.value_as_string(24));
11307        assert!(decimal_arr.is_null(25));
11308        assert!(decimal_arr.is_null(26));
11309        assert!(decimal_arr.is_null(27));
11310        assert_eq!("0.000", decimal_arr.value_as_string(28));
11311        assert_eq!("0.000", decimal_arr.value_as_string(29));
11312        assert_eq!("12345.000", decimal_arr.value_as_string(30));
11313        assert_eq!(decimal_arr.len(), 31);
11314    }
11315
11316    #[test]
11317    fn test_cast_utf8_to_decimal() {
11318        let str_array = StringArray::from(vec![
11319            Some("123.45"),
11320            Some("1.2345"),
11321            Some("0.12345"),
11322            Some("0.1267"),
11323            Some("1.263"),
11324            Some("12345.0"),
11325            Some("12345"),
11326            Some("000.123"),
11327            Some("12.234000"),
11328            None,
11329            Some(""),
11330            Some(" "),
11331            None,
11332            Some("-1.23499999"),
11333            Some("-1.23599999"),
11334            Some("-0.00001"),
11335            Some("-123"),
11336            Some("-123.234000"),
11337            Some("-000.123"),
11338            Some("+1.23499999"),
11339            Some("+1.23599999"),
11340            Some("+0.00001"),
11341            Some("+123"),
11342            Some("+123.234000"),
11343            Some("+000.123"),
11344            Some("1.-23499999"),
11345            Some("-1.-23499999"),
11346            Some("--1.23499999"),
11347            Some("0"),
11348            Some("000.000"),
11349            Some("0000000000000000012345.000"),
11350        ]);
11351        let array = Arc::new(str_array) as ArrayRef;
11352
11353        test_cast_string_to_decimal(array);
11354
11355        let test_cases = [
11356            (None, None),
11357            (Some(""), None),
11358            (Some("   "), None),
11359            (Some("0"), Some("0")),
11360            (Some("000.000"), Some("0")),
11361            (Some("12345"), Some("12345")),
11362            (Some("000000000000000000000000000012345"), Some("12345")),
11363            (Some("-123"), Some("-123")),
11364            (Some("+123"), Some("123")),
11365        ];
11366        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
11367        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
11368
11369        let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
11370        test_cast_string_to_decimal_scale_zero(array, &expected);
11371    }
11372
11373    #[test]
11374    fn test_cast_large_utf8_to_decimal() {
11375        let str_array = LargeStringArray::from(vec![
11376            Some("123.45"),
11377            Some("1.2345"),
11378            Some("0.12345"),
11379            Some("0.1267"),
11380            Some("1.263"),
11381            Some("12345.0"),
11382            Some("12345"),
11383            Some("000.123"),
11384            Some("12.234000"),
11385            None,
11386            Some(""),
11387            Some(" "),
11388            None,
11389            Some("-1.23499999"),
11390            Some("-1.23599999"),
11391            Some("-0.00001"),
11392            Some("-123"),
11393            Some("-123.234000"),
11394            Some("-000.123"),
11395            Some("+1.23499999"),
11396            Some("+1.23599999"),
11397            Some("+0.00001"),
11398            Some("+123"),
11399            Some("+123.234000"),
11400            Some("+000.123"),
11401            Some("1.-23499999"),
11402            Some("-1.-23499999"),
11403            Some("--1.23499999"),
11404            Some("0"),
11405            Some("000.000"),
11406            Some("0000000000000000012345.000"),
11407        ]);
11408        let array = Arc::new(str_array) as ArrayRef;
11409
11410        test_cast_string_to_decimal(array);
11411
11412        let test_cases = [
11413            (None, None),
11414            (Some(""), None),
11415            (Some("   "), None),
11416            (Some("0"), Some("0")),
11417            (Some("000.000"), Some("0")),
11418            (Some("12345"), Some("12345")),
11419            (Some("000000000000000000000000000012345"), Some("12345")),
11420            (Some("-123"), Some("-123")),
11421            (Some("+123"), Some("123")),
11422        ];
11423        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
11424        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
11425
11426        let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
11427        test_cast_string_to_decimal_scale_zero(array, &expected);
11428    }
11429
11430    fn test_cast_string_to_decimal_scale_zero(
11431        array: ArrayRef,
11432        expected_as_string: &[Option<&str>],
11433    ) {
11434        // Decimal128
11435        let output_type = DataType::Decimal128(38, 0);
11436        assert!(can_cast_types(array.data_type(), &output_type));
11437        let casted_array = cast(&array, &output_type).unwrap();
11438        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11439        assert_decimal_array_contents(decimal_arr, expected_as_string);
11440
11441        // Decimal256
11442        let output_type = DataType::Decimal256(76, 0);
11443        assert!(can_cast_types(array.data_type(), &output_type));
11444        let casted_array = cast(&array, &output_type).unwrap();
11445        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
11446        assert_decimal_array_contents(decimal_arr, expected_as_string);
11447    }
11448
11449    fn assert_decimal_array_contents<T>(
11450        array: &PrimitiveArray<T>,
11451        expected_as_string: &[Option<&str>],
11452    ) where
11453        T: DecimalType + ArrowPrimitiveType,
11454    {
11455        assert_eq!(array.len(), expected_as_string.len());
11456        for (i, expected) in expected_as_string.iter().enumerate() {
11457            let actual = if array.is_null(i) {
11458                None
11459            } else {
11460                Some(array.value_as_string(i))
11461            };
11462            let actual = actual.as_ref().map(|s| s.as_ref());
11463            assert_eq!(*expected, actual, "Expected at position {i}");
11464        }
11465    }
11466
11467    #[test]
11468    fn test_cast_invalid_utf8_to_decimal() {
11469        let str_array = StringArray::from(vec!["4.4.5", ". 0.123"]);
11470        let array = Arc::new(str_array) as ArrayRef;
11471
11472        // Safe cast
11473        let output_type = DataType::Decimal128(38, 2);
11474        let casted_array = cast(&array, &output_type).unwrap();
11475        assert!(casted_array.is_null(0));
11476        assert!(casted_array.is_null(1));
11477
11478        let output_type = DataType::Decimal256(76, 2);
11479        let casted_array = cast(&array, &output_type).unwrap();
11480        assert!(casted_array.is_null(0));
11481        assert!(casted_array.is_null(1));
11482
11483        // Non-safe cast
11484        let output_type = DataType::Decimal128(38, 2);
11485        let str_array = StringArray::from(vec!["4.4.5"]);
11486        let array = Arc::new(str_array) as ArrayRef;
11487        let option = CastOptions {
11488            safe: false,
11489            format_options: FormatOptions::default(),
11490        };
11491        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
11492        assert!(
11493            casted_err
11494                .to_string()
11495                .contains("Cannot cast string '4.4.5' to value of Decimal128(38, 2) type")
11496        );
11497
11498        let str_array = StringArray::from(vec![". 0.123"]);
11499        let array = Arc::new(str_array) as ArrayRef;
11500        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
11501        assert!(
11502            casted_err
11503                .to_string()
11504                .contains("Cannot cast string '. 0.123' to value of Decimal128(38, 2) type")
11505        );
11506
11507        let str_array = StringArray::from(vec![""]);
11508        let array = Arc::new(str_array) as ArrayRef;
11509        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
11510        assert!(
11511            casted_err
11512                .to_string()
11513                .contains("Cannot cast string '' to value of Decimal128(38, 2) type")
11514        );
11515    }
11516
11517    fn test_cast_string_to_decimal128_overflow(overflow_array: ArrayRef) {
11518        let output_type = DataType::Decimal128(38, 2);
11519        let casted_array = cast(&overflow_array, &output_type).unwrap();
11520        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11521
11522        assert!(decimal_arr.is_null(0));
11523        assert!(decimal_arr.is_null(1));
11524        assert!(decimal_arr.is_null(2));
11525        assert_eq!(
11526            "999999999999999999999999999999999999.99",
11527            decimal_arr.value_as_string(3)
11528        );
11529        assert_eq!(
11530            "100000000000000000000000000000000000.00",
11531            decimal_arr.value_as_string(4)
11532        );
11533    }
11534
11535    #[test]
11536    fn test_cast_string_to_decimal128_precision_overflow() {
11537        let array = StringArray::from(vec!["1000".to_string()]);
11538        let array = Arc::new(array) as ArrayRef;
11539        let casted_array = cast_with_options(
11540            &array,
11541            &DataType::Decimal128(10, 8),
11542            &CastOptions {
11543                safe: true,
11544                format_options: FormatOptions::default(),
11545            },
11546        );
11547        assert!(casted_array.is_ok());
11548        assert!(casted_array.unwrap().is_null(0));
11549
11550        let err = cast_with_options(
11551            &array,
11552            &DataType::Decimal128(10, 8),
11553            &CastOptions {
11554                safe: false,
11555                format_options: FormatOptions::default(),
11556            },
11557        );
11558        assert_eq!(
11559            "Invalid argument error: 1000.00000000 is too large to store in a Decimal128 of precision 10. Max is 99.99999999",
11560            err.unwrap_err().to_string()
11561        );
11562    }
11563
11564    #[test]
11565    fn test_cast_utf8_to_decimal128_overflow() {
11566        let overflow_str_array = StringArray::from(vec![
11567            i128::MAX.to_string(),
11568            i128::MIN.to_string(),
11569            "99999999999999999999999999999999999999".to_string(),
11570            "999999999999999999999999999999999999.99".to_string(),
11571            "99999999999999999999999999999999999.999".to_string(),
11572        ]);
11573        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11574
11575        test_cast_string_to_decimal128_overflow(overflow_array);
11576    }
11577
11578    #[test]
11579    fn test_cast_large_utf8_to_decimal128_overflow() {
11580        let overflow_str_array = LargeStringArray::from(vec![
11581            i128::MAX.to_string(),
11582            i128::MIN.to_string(),
11583            "99999999999999999999999999999999999999".to_string(),
11584            "999999999999999999999999999999999999.99".to_string(),
11585            "99999999999999999999999999999999999.999".to_string(),
11586        ]);
11587        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11588
11589        test_cast_string_to_decimal128_overflow(overflow_array);
11590    }
11591
11592    fn test_cast_string_to_decimal256_overflow(overflow_array: ArrayRef) {
11593        let output_type = DataType::Decimal256(76, 2);
11594        let casted_array = cast(&overflow_array, &output_type).unwrap();
11595        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
11596
11597        assert_eq!(
11598            "170141183460469231731687303715884105727.00",
11599            decimal_arr.value_as_string(0)
11600        );
11601        assert_eq!(
11602            "-170141183460469231731687303715884105728.00",
11603            decimal_arr.value_as_string(1)
11604        );
11605        assert_eq!(
11606            "99999999999999999999999999999999999999.00",
11607            decimal_arr.value_as_string(2)
11608        );
11609        assert_eq!(
11610            "999999999999999999999999999999999999.99",
11611            decimal_arr.value_as_string(3)
11612        );
11613        assert_eq!(
11614            "100000000000000000000000000000000000.00",
11615            decimal_arr.value_as_string(4)
11616        );
11617        assert!(decimal_arr.is_null(5));
11618        assert!(decimal_arr.is_null(6));
11619    }
11620
11621    #[test]
11622    fn test_cast_string_to_decimal256_precision_overflow() {
11623        let array = StringArray::from(vec!["1000".to_string()]);
11624        let array = Arc::new(array) as ArrayRef;
11625        let casted_array = cast_with_options(
11626            &array,
11627            &DataType::Decimal256(10, 8),
11628            &CastOptions {
11629                safe: true,
11630                format_options: FormatOptions::default(),
11631            },
11632        );
11633        assert!(casted_array.is_ok());
11634        assert!(casted_array.unwrap().is_null(0));
11635
11636        let err = cast_with_options(
11637            &array,
11638            &DataType::Decimal256(10, 8),
11639            &CastOptions {
11640                safe: false,
11641                format_options: FormatOptions::default(),
11642            },
11643        );
11644        assert_eq!(
11645            "Invalid argument error: 1000.00000000 is too large to store in a Decimal256 of precision 10. Max is 99.99999999",
11646            err.unwrap_err().to_string()
11647        );
11648    }
11649
11650    #[test]
11651    fn test_cast_utf8_to_decimal256_overflow() {
11652        let overflow_str_array = StringArray::from(vec![
11653            i128::MAX.to_string(),
11654            i128::MIN.to_string(),
11655            "99999999999999999999999999999999999999".to_string(),
11656            "999999999999999999999999999999999999.99".to_string(),
11657            "99999999999999999999999999999999999.999".to_string(),
11658            i256::MAX.to_string(),
11659            i256::MIN.to_string(),
11660        ]);
11661        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11662
11663        test_cast_string_to_decimal256_overflow(overflow_array);
11664    }
11665
11666    #[test]
11667    fn test_cast_large_utf8_to_decimal256_overflow() {
11668        let overflow_str_array = LargeStringArray::from(vec![
11669            i128::MAX.to_string(),
11670            i128::MIN.to_string(),
11671            "99999999999999999999999999999999999999".to_string(),
11672            "999999999999999999999999999999999999.99".to_string(),
11673            "99999999999999999999999999999999999.999".to_string(),
11674            i256::MAX.to_string(),
11675            i256::MIN.to_string(),
11676        ]);
11677        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11678
11679        test_cast_string_to_decimal256_overflow(overflow_array);
11680    }
11681
11682    #[test]
11683    fn test_cast_outside_supported_range_for_nanoseconds() {
11684        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";
11685
11686        let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
11687
11688        let cast_options = CastOptions {
11689            safe: false,
11690            format_options: FormatOptions::default(),
11691        };
11692
11693        let result =
11694            cast_string_to_timestamp::<i32, TimestampNanosecondType>(&array, None, &cast_options);
11695
11696        let err = result.unwrap_err();
11697        assert_eq!(
11698            err.to_string(),
11699            format!(
11700                "Cast error: Overflow converting {} to Nanosecond. {}",
11701                array.value(0),
11702                EXPECTED_ERROR_MESSAGE
11703            )
11704        );
11705    }
11706
11707    #[test]
11708    fn test_cast_date32_to_timestamp() {
11709        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
11710        let array = Arc::new(a) as ArrayRef;
11711        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
11712        let c = b.as_primitive::<TimestampSecondType>();
11713        assert_eq!(1609459200, c.value(0));
11714        assert_eq!(1640995200, c.value(1));
11715        assert!(c.is_null(2));
11716    }
11717
11718    #[test]
11719    fn test_cast_date32_to_timestamp_ms() {
11720        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
11721        let array = Arc::new(a) as ArrayRef;
11722        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
11723        let c = b
11724            .as_any()
11725            .downcast_ref::<TimestampMillisecondArray>()
11726            .unwrap();
11727        assert_eq!(1609459200000, c.value(0));
11728        assert_eq!(1640995200000, c.value(1));
11729        assert!(c.is_null(2));
11730    }
11731
11732    #[test]
11733    fn test_cast_date32_to_timestamp_us() {
11734        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
11735        let array = Arc::new(a) as ArrayRef;
11736        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
11737        let c = b
11738            .as_any()
11739            .downcast_ref::<TimestampMicrosecondArray>()
11740            .unwrap();
11741        assert_eq!(1609459200000000, c.value(0));
11742        assert_eq!(1640995200000000, c.value(1));
11743        assert!(c.is_null(2));
11744    }
11745
11746    #[test]
11747    fn test_cast_date32_to_timestamp_ns() {
11748        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
11749        let array = Arc::new(a) as ArrayRef;
11750        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
11751        let c = b
11752            .as_any()
11753            .downcast_ref::<TimestampNanosecondArray>()
11754            .unwrap();
11755        assert_eq!(1609459200000000000, c.value(0));
11756        assert_eq!(1640995200000000000, c.value(1));
11757        assert!(c.is_null(2));
11758    }
11759
11760    #[test]
11761    fn test_cast_date32_to_timestamp_us_overflow() {
11762        const MAX_DAYS_MICROS: i32 = (i64::MAX / MICROSECONDS_IN_DAY) as i32;
11763        let a = Date32Array::from(vec![Some(MAX_DAYS_MICROS), Some(MAX_DAYS_MICROS + 1), None]);
11764        let array = Arc::new(a) as ArrayRef;
11765        let err = cast_with_options(
11766            &array,
11767            &DataType::Timestamp(TimeUnit::Microsecond, None),
11768            &CastOptions {
11769                safe: false,
11770                format_options: FormatOptions::default(),
11771            },
11772        );
11773        assert!(err.is_err());
11774
11775        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
11776        let c = b.as_primitive::<TimestampMicrosecondType>();
11777        assert_eq!(MAX_DAYS_MICROS as i64 * MICROSECONDS_IN_DAY, c.value(0));
11778        assert!(c.is_null(1));
11779        assert!(c.is_null(2));
11780    }
11781
11782    #[test]
11783    fn test_cast_date32_to_timestamp_ns_overflow() {
11784        // 2262-04-11, 2062-04-12
11785        let upper_limit = 106_751;
11786        let a = Date32Array::from(vec![Some(upper_limit), Some(upper_limit + 1), None]);
11787        let array = Arc::new(a) as ArrayRef;
11788        let err = cast_with_options(
11789            &array,
11790            &DataType::Timestamp(TimeUnit::Nanosecond, None),
11791            &CastOptions {
11792                safe: false,
11793                format_options: FormatOptions::default(),
11794            },
11795        );
11796        assert!(err.is_err());
11797
11798        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
11799        let c = b.as_primitive::<TimestampNanosecondType>();
11800        assert_eq!(upper_limit as i64 * NANOSECONDS_IN_DAY, c.value(0));
11801        assert!(c.is_null(1));
11802        assert!(c.is_null(2));
11803    }
11804
11805    #[test]
11806    fn test_timezone_cast() {
11807        let a = StringArray::from(vec![
11808            "2000-01-01T12:00:00", // date + time valid
11809            "2020-12-15T12:34:56", // date + time valid
11810        ]);
11811        let array = Arc::new(a) as ArrayRef;
11812        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
11813        let v = b.as_primitive::<TimestampNanosecondType>();
11814
11815        assert_eq!(v.value(0), 946728000000000000);
11816        assert_eq!(v.value(1), 1608035696000000000);
11817
11818        let b = cast(
11819            &b,
11820            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11821        )
11822        .unwrap();
11823        let v = b.as_primitive::<TimestampNanosecondType>();
11824
11825        assert_eq!(v.value(0), 946728000000000000);
11826        assert_eq!(v.value(1), 1608035696000000000);
11827
11828        let b = cast(
11829            &b,
11830            &DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
11831        )
11832        .unwrap();
11833        let v = b.as_primitive::<TimestampMillisecondType>();
11834
11835        assert_eq!(v.value(0), 946728000000);
11836        assert_eq!(v.value(1), 1608035696000);
11837    }
11838
11839    #[test]
11840    fn test_cast_utf8_to_timestamp() {
11841        fn test_tz(tz: Arc<str>) {
11842            let valid = StringArray::from(vec![
11843                "2023-01-01 04:05:06.789000-08:00",
11844                "2023-01-01 04:05:06.789000-07:00",
11845                "2023-01-01 04:05:06.789 -0800",
11846                "2023-01-01 04:05:06.789 -08:00",
11847                "2023-01-01 040506 +0730",
11848                "2023-01-01 040506 +07:30",
11849                "2023-01-01 04:05:06.789",
11850                "2023-01-01 04:05:06",
11851                "2023-01-01",
11852            ]);
11853
11854            let array = Arc::new(valid) as ArrayRef;
11855            let b = cast_with_options(
11856                &array,
11857                &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
11858                &CastOptions {
11859                    safe: false,
11860                    format_options: FormatOptions::default(),
11861                },
11862            )
11863            .unwrap();
11864
11865            let tz = tz.as_ref().parse().unwrap();
11866
11867            let as_tz =
11868                |v: i64| as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap();
11869
11870            let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
11871            let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
11872
11873            let values = b.as_primitive::<TimestampNanosecondType>().values();
11874            let utc_results: Vec<_> = values.iter().map(as_utc).collect();
11875            let local_results: Vec<_> = values.iter().map(as_local).collect();
11876
11877            // Absolute timestamps should be parsed preserving the same UTC instant
11878            assert_eq!(
11879                &utc_results[..6],
11880                &[
11881                    "2023-01-01 12:05:06.789".to_string(),
11882                    "2023-01-01 11:05:06.789".to_string(),
11883                    "2023-01-01 12:05:06.789".to_string(),
11884                    "2023-01-01 12:05:06.789".to_string(),
11885                    "2022-12-31 20:35:06".to_string(),
11886                    "2022-12-31 20:35:06".to_string(),
11887                ]
11888            );
11889            // Non-absolute timestamps should be parsed preserving the same local instant
11890            assert_eq!(
11891                &local_results[6..],
11892                &[
11893                    "2023-01-01 04:05:06.789".to_string(),
11894                    "2023-01-01 04:05:06".to_string(),
11895                    "2023-01-01 00:00:00".to_string()
11896                ]
11897            )
11898        }
11899
11900        test_tz("+00:00".into());
11901        test_tz("+02:00".into());
11902    }
11903
11904    #[test]
11905    fn test_cast_invalid_utf8() {
11906        let v1: &[u8] = b"\xFF invalid";
11907        let v2: &[u8] = b"\x00 Foo";
11908        let s = BinaryArray::from(vec![v1, v2]);
11909        let options = CastOptions {
11910            safe: true,
11911            format_options: FormatOptions::default(),
11912        };
11913        let array = cast_with_options(&s, &DataType::Utf8, &options).unwrap();
11914        let a = array.as_string::<i32>();
11915        a.to_data().validate_full().unwrap();
11916
11917        assert_eq!(a.null_count(), 1);
11918        assert_eq!(a.len(), 2);
11919        assert!(a.is_null(0));
11920        assert_eq!(a.value(0), "");
11921        assert_eq!(a.value(1), "\x00 Foo");
11922    }
11923
11924    #[test]
11925    fn test_cast_utf8_to_timestamptz() {
11926        let valid = StringArray::from(vec!["2023-01-01"]);
11927
11928        let array = Arc::new(valid) as ArrayRef;
11929        let b = cast(
11930            &array,
11931            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11932        )
11933        .unwrap();
11934
11935        let expect = DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into()));
11936
11937        assert_eq!(b.data_type(), &expect);
11938        let c = b
11939            .as_any()
11940            .downcast_ref::<TimestampNanosecondArray>()
11941            .unwrap();
11942        assert_eq!(1672531200000000000, c.value(0));
11943    }
11944
11945    #[test]
11946    fn test_cast_out_of_precision_decimal_to_string() {
11947        // Decimal values are not validated against their type's declared
11948        // precision by default. Check that out-of-precision values are rendered
11949        // in full when cast to strings, rather than truncated to the declared
11950        // precision (https://github.com/apache/arrow-rs/issues/10866)
11951        let array = create_decimal128_array(vec![Some(123456789), Some(-123456789)], 7, 3).unwrap();
11952        let b = cast(&array, &DataType::Utf8).unwrap();
11953        let c = b.as_string::<i32>();
11954        assert_eq!("123456.789", c.value(0));
11955        assert_eq!("-123456.789", c.value(1));
11956    }
11957
11958    #[test]
11959    fn test_cast_decimal_to_string() {
11960        assert!(can_cast_types(
11961            &DataType::Decimal32(9, 4),
11962            &DataType::Utf8View
11963        ));
11964        assert!(can_cast_types(
11965            &DataType::Decimal64(16, 4),
11966            &DataType::Utf8View
11967        ));
11968        assert!(can_cast_types(
11969            &DataType::Decimal128(10, 4),
11970            &DataType::Utf8View
11971        ));
11972        assert!(can_cast_types(
11973            &DataType::Decimal256(38, 10),
11974            &DataType::Utf8View
11975        ));
11976
11977        macro_rules! assert_decimal_values {
11978            ($array:expr) => {
11979                let c = $array;
11980                assert_eq!("1123.454", c.value(0));
11981                assert_eq!("2123.456", c.value(1));
11982                assert_eq!("-3123.453", c.value(2));
11983                assert_eq!("-3123.456", c.value(3));
11984                assert_eq!("0.000", c.value(4));
11985                assert_eq!("0.123", c.value(5));
11986                assert!(c.is_null(6));
11987            };
11988        }
11989
11990        fn test_decimal_to_string<IN: ArrowPrimitiveType, OffsetSize: OffsetSizeTrait>(
11991            output_type: DataType,
11992            array: PrimitiveArray<IN>,
11993        ) {
11994            let b = cast(&array, &output_type).unwrap();
11995
11996            assert_eq!(b.data_type(), &output_type);
11997            match b.data_type() {
11998                DataType::Utf8View => {
11999                    let c = b.as_string_view();
12000                    assert_decimal_values!(c);
12001                }
12002                DataType::Utf8 | DataType::LargeUtf8 => {
12003                    let c = b.as_string::<OffsetSize>();
12004                    assert_decimal_values!(c);
12005                }
12006                _ => (),
12007            }
12008        }
12009
12010        let array32: Vec<Option<i32>> = vec![
12011            Some(1123454),
12012            Some(2123456),
12013            Some(-3123453),
12014            Some(-3123456),
12015            Some(0),
12016            Some(123),
12017            None,
12018        ];
12019        let array64: Vec<Option<i64>> = array32.iter().map(|num| num.map(|x| x as i64)).collect();
12020        let array128: Vec<Option<i128>> =
12021            array64.iter().map(|num| num.map(|x| x as i128)).collect();
12022        let array256: Vec<Option<i256>> = array128
12023            .iter()
12024            .map(|num| num.map(i256::from_i128))
12025            .collect();
12026
12027        test_decimal_to_string::<Decimal32Type, i32>(
12028            DataType::Utf8View,
12029            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
12030        );
12031        test_decimal_to_string::<Decimal32Type, i32>(
12032            DataType::Utf8,
12033            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
12034        );
12035        test_decimal_to_string::<Decimal32Type, i64>(
12036            DataType::LargeUtf8,
12037            create_decimal32_array(array32, 7, 3).unwrap(),
12038        );
12039
12040        test_decimal_to_string::<Decimal64Type, i32>(
12041            DataType::Utf8View,
12042            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
12043        );
12044        test_decimal_to_string::<Decimal64Type, i32>(
12045            DataType::Utf8,
12046            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
12047        );
12048        test_decimal_to_string::<Decimal64Type, i64>(
12049            DataType::LargeUtf8,
12050            create_decimal64_array(array64, 7, 3).unwrap(),
12051        );
12052
12053        test_decimal_to_string::<Decimal128Type, i32>(
12054            DataType::Utf8View,
12055            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
12056        );
12057        test_decimal_to_string::<Decimal128Type, i32>(
12058            DataType::Utf8,
12059            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
12060        );
12061        test_decimal_to_string::<Decimal128Type, i64>(
12062            DataType::LargeUtf8,
12063            create_decimal128_array(array128, 7, 3).unwrap(),
12064        );
12065
12066        test_decimal_to_string::<Decimal256Type, i32>(
12067            DataType::Utf8View,
12068            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
12069        );
12070        test_decimal_to_string::<Decimal256Type, i32>(
12071            DataType::Utf8,
12072            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
12073        );
12074        test_decimal_to_string::<Decimal256Type, i64>(
12075            DataType::LargeUtf8,
12076            create_decimal256_array(array256, 7, 3).unwrap(),
12077        );
12078    }
12079
12080    #[test]
12081    fn test_cast_numeric_to_decimal128_precision_overflow() {
12082        let array = Int64Array::from(vec![1234567]);
12083        let array = Arc::new(array) as ArrayRef;
12084        let casted_array = cast_with_options(
12085            &array,
12086            &DataType::Decimal128(7, 3),
12087            &CastOptions {
12088                safe: true,
12089                format_options: FormatOptions::default(),
12090            },
12091        );
12092        assert!(casted_array.is_ok());
12093        assert!(casted_array.unwrap().is_null(0));
12094
12095        let err = cast_with_options(
12096            &array,
12097            &DataType::Decimal128(7, 3),
12098            &CastOptions {
12099                safe: false,
12100                format_options: FormatOptions::default(),
12101            },
12102        );
12103        assert_eq!(
12104            "Invalid argument error: 1234567.000 is too large to store in a Decimal128 of precision 7. Max is 9999.999",
12105            err.unwrap_err().to_string()
12106        );
12107    }
12108
12109    #[test]
12110    fn test_cast_numeric_to_decimal256_precision_overflow() {
12111        let array = Int64Array::from(vec![1234567]);
12112        let array = Arc::new(array) as ArrayRef;
12113        let casted_array = cast_with_options(
12114            &array,
12115            &DataType::Decimal256(7, 3),
12116            &CastOptions {
12117                safe: true,
12118                format_options: FormatOptions::default(),
12119            },
12120        );
12121        assert!(casted_array.is_ok());
12122        assert!(casted_array.unwrap().is_null(0));
12123
12124        let err = cast_with_options(
12125            &array,
12126            &DataType::Decimal256(7, 3),
12127            &CastOptions {
12128                safe: false,
12129                format_options: FormatOptions::default(),
12130            },
12131        );
12132        assert_eq!(
12133            "Invalid argument error: 1234567.000 is too large to store in a Decimal256 of precision 7. Max is 9999.999",
12134            err.unwrap_err().to_string()
12135        );
12136    }
12137
12138    /// helper function to test casting from duration to interval
12139    fn cast_from_duration_to_interval<T: ArrowTemporalType<Native = i64>>(
12140        array: Vec<i64>,
12141        cast_options: &CastOptions,
12142    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
12143        let array = PrimitiveArray::<T>::new(array.into(), None);
12144        let array = Arc::new(array) as ArrayRef;
12145        let interval = DataType::Interval(IntervalUnit::MonthDayNano);
12146        let out = cast_with_options(&array, &interval, cast_options)?;
12147        let out = out.as_primitive::<IntervalMonthDayNanoType>().clone();
12148        Ok(out)
12149    }
12150
12151    #[test]
12152    fn test_cast_from_duration_to_interval() {
12153        // from duration second to interval month day nano
12154        let array = vec![1234567];
12155        let casted_array =
12156            cast_from_duration_to_interval::<DurationSecondType>(array, &CastOptions::default())
12157                .unwrap();
12158        assert_eq!(
12159            casted_array.data_type(),
12160            &DataType::Interval(IntervalUnit::MonthDayNano)
12161        );
12162        assert_eq!(
12163            casted_array.value(0),
12164            IntervalMonthDayNano::new(0, 0, 1234567000000000)
12165        );
12166
12167        let array = vec![i64::MAX];
12168        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
12169            array.clone(),
12170            &CastOptions::default(),
12171        )
12172        .unwrap();
12173        assert!(!casted_array.is_valid(0));
12174
12175        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
12176            array,
12177            &CastOptions {
12178                safe: false,
12179                format_options: FormatOptions::default(),
12180            },
12181        );
12182        assert!(casted_array.is_err());
12183
12184        // from duration millisecond to interval month day nano
12185        let array = vec![1234567];
12186        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
12187            array,
12188            &CastOptions::default(),
12189        )
12190        .unwrap();
12191        assert_eq!(
12192            casted_array.data_type(),
12193            &DataType::Interval(IntervalUnit::MonthDayNano)
12194        );
12195        assert_eq!(
12196            casted_array.value(0),
12197            IntervalMonthDayNano::new(0, 0, 1234567000000)
12198        );
12199
12200        let array = vec![i64::MAX];
12201        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
12202            array.clone(),
12203            &CastOptions::default(),
12204        )
12205        .unwrap();
12206        assert!(!casted_array.is_valid(0));
12207
12208        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
12209            array,
12210            &CastOptions {
12211                safe: false,
12212                format_options: FormatOptions::default(),
12213            },
12214        );
12215        assert!(casted_array.is_err());
12216
12217        // from duration microsecond to interval month day nano
12218        let array = vec![1234567];
12219        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
12220            array,
12221            &CastOptions::default(),
12222        )
12223        .unwrap();
12224        assert_eq!(
12225            casted_array.data_type(),
12226            &DataType::Interval(IntervalUnit::MonthDayNano)
12227        );
12228        assert_eq!(
12229            casted_array.value(0),
12230            IntervalMonthDayNano::new(0, 0, 1234567000)
12231        );
12232
12233        let array = vec![i64::MAX];
12234        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
12235            array.clone(),
12236            &CastOptions::default(),
12237        )
12238        .unwrap();
12239        assert!(!casted_array.is_valid(0));
12240
12241        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
12242            array,
12243            &CastOptions {
12244                safe: false,
12245                format_options: FormatOptions::default(),
12246            },
12247        );
12248        assert!(casted_array.is_err());
12249
12250        // from duration nanosecond to interval month day nano
12251        let array = vec![1234567];
12252        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
12253            array,
12254            &CastOptions::default(),
12255        )
12256        .unwrap();
12257        assert_eq!(
12258            casted_array.data_type(),
12259            &DataType::Interval(IntervalUnit::MonthDayNano)
12260        );
12261        assert_eq!(
12262            casted_array.value(0),
12263            IntervalMonthDayNano::new(0, 0, 1234567)
12264        );
12265
12266        let array = vec![i64::MAX];
12267        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
12268            array,
12269            &CastOptions {
12270                safe: false,
12271                format_options: FormatOptions::default(),
12272            },
12273        )
12274        .unwrap();
12275        assert_eq!(
12276            casted_array.value(0),
12277            IntervalMonthDayNano::new(0, 0, i64::MAX)
12278        );
12279    }
12280
12281    /// helper function to test casting from interval to duration
12282    fn cast_from_interval_to_duration<T: ArrowTemporalType>(
12283        array: &IntervalMonthDayNanoArray,
12284        cast_options: &CastOptions,
12285    ) -> Result<PrimitiveArray<T>, ArrowError> {
12286        let casted_array = cast_with_options(&array, &T::DATA_TYPE, cast_options)?;
12287        casted_array
12288            .as_any()
12289            .downcast_ref::<PrimitiveArray<T>>()
12290            .ok_or_else(|| {
12291                ArrowError::ComputeError(format!("Failed to downcast to {}", T::DATA_TYPE))
12292            })
12293            .cloned()
12294    }
12295
12296    #[test]
12297    fn test_cast_from_interval_to_duration() {
12298        let nullable = CastOptions::default();
12299        let fallible = CastOptions {
12300            safe: false,
12301            format_options: FormatOptions::default(),
12302        };
12303        let v = IntervalMonthDayNano::new(0, 0, 1234567);
12304
12305        // from interval month day nano to duration second
12306        let array = vec![v].into();
12307        let casted_array: DurationSecondArray =
12308            cast_from_interval_to_duration(&array, &nullable).unwrap();
12309        assert_eq!(casted_array.value(0), 0);
12310
12311        let array = vec![IntervalMonthDayNano::MAX].into();
12312        let casted_array: DurationSecondArray =
12313            cast_from_interval_to_duration(&array, &nullable).unwrap();
12314        assert!(!casted_array.is_valid(0));
12315
12316        let res = cast_from_interval_to_duration::<DurationSecondType>(&array, &fallible);
12317        assert!(res.is_err());
12318
12319        // from interval month day nano to duration millisecond
12320        let array = vec![v].into();
12321        let casted_array: DurationMillisecondArray =
12322            cast_from_interval_to_duration(&array, &nullable).unwrap();
12323        assert_eq!(casted_array.value(0), 1);
12324
12325        let array = vec![IntervalMonthDayNano::MAX].into();
12326        let casted_array: DurationMillisecondArray =
12327            cast_from_interval_to_duration(&array, &nullable).unwrap();
12328        assert!(!casted_array.is_valid(0));
12329
12330        let res = cast_from_interval_to_duration::<DurationMillisecondType>(&array, &fallible);
12331        assert!(res.is_err());
12332
12333        // from interval month day nano to duration microsecond
12334        let array = vec![v].into();
12335        let casted_array: DurationMicrosecondArray =
12336            cast_from_interval_to_duration(&array, &nullable).unwrap();
12337        assert_eq!(casted_array.value(0), 1234);
12338
12339        let array = vec![IntervalMonthDayNano::MAX].into();
12340        let casted_array =
12341            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &nullable).unwrap();
12342        assert!(!casted_array.is_valid(0));
12343
12344        let casted_array =
12345            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &fallible);
12346        assert!(casted_array.is_err());
12347
12348        // from interval month day nano to duration nanosecond
12349        let array = vec![v].into();
12350        let casted_array: DurationNanosecondArray =
12351            cast_from_interval_to_duration(&array, &nullable).unwrap();
12352        assert_eq!(casted_array.value(0), 1234567);
12353
12354        let array = vec![IntervalMonthDayNano::MAX].into();
12355        let casted_array: DurationNanosecondArray =
12356            cast_from_interval_to_duration(&array, &nullable).unwrap();
12357        assert!(!casted_array.is_valid(0));
12358
12359        let casted_array =
12360            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &fallible);
12361        assert!(casted_array.is_err());
12362
12363        let array = vec![
12364            IntervalMonthDayNanoType::make_value(0, 1, 0),
12365            IntervalMonthDayNanoType::make_value(-1, 0, 0),
12366            IntervalMonthDayNanoType::make_value(1, 1, 0),
12367            IntervalMonthDayNanoType::make_value(1, 0, 1),
12368            IntervalMonthDayNanoType::make_value(0, 0, -1),
12369        ]
12370        .into();
12371        let casted_array =
12372            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable).unwrap();
12373        assert!(!casted_array.is_valid(0));
12374        assert!(!casted_array.is_valid(1));
12375        assert!(!casted_array.is_valid(2));
12376        assert!(!casted_array.is_valid(3));
12377        assert!(casted_array.is_valid(4));
12378        assert_eq!(casted_array.value(4), -1);
12379    }
12380
12381    /// helper function to test casting from interval year month to interval month day nano
12382    fn cast_from_interval_year_month_to_interval_month_day_nano(
12383        array: Vec<i32>,
12384        cast_options: &CastOptions,
12385    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
12386        let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
12387        let array = Arc::new(array) as ArrayRef;
12388        let casted_array = cast_with_options(
12389            &array,
12390            &DataType::Interval(IntervalUnit::MonthDayNano),
12391            cast_options,
12392        )?;
12393        casted_array
12394            .as_any()
12395            .downcast_ref::<IntervalMonthDayNanoArray>()
12396            .ok_or_else(|| {
12397                ArrowError::ComputeError(
12398                    "Failed to downcast to IntervalMonthDayNanoArray".to_string(),
12399                )
12400            })
12401            .cloned()
12402    }
12403
12404    #[test]
12405    fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
12406        // from interval year month to interval month day nano
12407        let array = vec![1234567];
12408        let casted_array = cast_from_interval_year_month_to_interval_month_day_nano(
12409            array,
12410            &CastOptions::default(),
12411        )
12412        .unwrap();
12413        assert_eq!(
12414            casted_array.data_type(),
12415            &DataType::Interval(IntervalUnit::MonthDayNano)
12416        );
12417        assert_eq!(
12418            casted_array.value(0),
12419            IntervalMonthDayNano::new(1234567, 0, 0)
12420        );
12421    }
12422
12423    /// helper function to test casting from interval day time to interval month day nano
12424    fn cast_from_interval_day_time_to_interval_month_day_nano(
12425        array: Vec<IntervalDayTime>,
12426        cast_options: &CastOptions,
12427    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
12428        let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
12429        let array = Arc::new(array) as ArrayRef;
12430        let casted_array = cast_with_options(
12431            &array,
12432            &DataType::Interval(IntervalUnit::MonthDayNano),
12433            cast_options,
12434        )?;
12435        Ok(casted_array
12436            .as_primitive::<IntervalMonthDayNanoType>()
12437            .clone())
12438    }
12439
12440    #[test]
12441    fn test_cast_from_interval_day_time_to_interval_month_day_nano() {
12442        // from interval day time to interval month day nano
12443        let array = vec![IntervalDayTime::new(123, 0)];
12444        let casted_array =
12445            cast_from_interval_day_time_to_interval_month_day_nano(array, &CastOptions::default())
12446                .unwrap();
12447        assert_eq!(
12448            casted_array.data_type(),
12449            &DataType::Interval(IntervalUnit::MonthDayNano)
12450        );
12451        assert_eq!(casted_array.value(0), IntervalMonthDayNano::new(0, 123, 0));
12452    }
12453
12454    #[test]
12455    fn test_cast_below_unixtimestamp() {
12456        let valid = StringArray::from(vec![
12457            "1900-01-03 23:59:59",
12458            "1969-12-31 00:00:01",
12459            "1989-12-31 00:00:01",
12460        ]);
12461
12462        let array = Arc::new(valid) as ArrayRef;
12463        let casted_array = cast_with_options(
12464            &array,
12465            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
12466            &CastOptions {
12467                safe: false,
12468                format_options: FormatOptions::default(),
12469            },
12470        )
12471        .unwrap();
12472
12473        let ts_array = casted_array
12474            .as_primitive::<TimestampNanosecondType>()
12475            .values()
12476            .iter()
12477            .map(|ts| ts / 1_000_000)
12478            .collect::<Vec<_>>();
12479
12480        let array = TimestampMillisecondArray::from(ts_array).with_timezone("+00:00".to_string());
12481        let casted_array = cast(&array, &DataType::Date32).unwrap();
12482        let date_array = casted_array.as_primitive::<Date32Type>();
12483        let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
12484        let string_array = casted_array.as_string::<i32>();
12485        assert_eq!("1900-01-03", string_array.value(0));
12486        assert_eq!("1969-12-31", string_array.value(1));
12487        assert_eq!("1989-12-31", string_array.value(2));
12488    }
12489
12490    #[test]
12491    fn test_nested_list() {
12492        let mut list = ListBuilder::new(Int32Builder::new());
12493        list.append_value([Some(1), Some(2), Some(3)]);
12494        list.append_value([Some(4), None, Some(6)]);
12495        let list = list.finish();
12496
12497        let to_field = Field::new("nested", list.data_type().clone(), false);
12498        let to = DataType::List(Arc::new(to_field));
12499        let out = cast(&list, &to).unwrap();
12500        let opts = FormatOptions::default().with_null("null");
12501        let formatted = ArrayFormatter::try_new(out.as_ref(), &opts).unwrap();
12502
12503        assert_eq!(formatted.value(0).to_string(), "[[1], [2], [3]]");
12504        assert_eq!(formatted.value(1).to_string(), "[[4], [null], [6]]");
12505    }
12506
12507    #[test]
12508    fn test_nested_list_cast() {
12509        let mut builder = ListBuilder::new(ListBuilder::new(Int32Builder::new()));
12510        builder.append_value([Some([Some(1), Some(2), None]), None]);
12511        builder.append_value([None, Some([]), None]);
12512        builder.append_null();
12513        builder.append_value([Some([Some(2), Some(3)])]);
12514        let start = builder.finish();
12515
12516        let mut builder = LargeListBuilder::new(LargeListBuilder::new(Int8Builder::new()));
12517        builder.append_value([Some([Some(1), Some(2), None]), None]);
12518        builder.append_value([None, Some([]), None]);
12519        builder.append_null();
12520        builder.append_value([Some([Some(2), Some(3)])]);
12521        let expected = builder.finish();
12522
12523        let actual = cast(&start, expected.data_type()).unwrap();
12524        assert_eq!(actual.as_ref(), &expected);
12525    }
12526
12527    const CAST_OPTIONS: CastOptions<'static> = CastOptions {
12528        safe: true,
12529        format_options: FormatOptions::new(),
12530    };
12531
12532    #[test]
12533    #[expect(clippy::assertions_on_constants)]
12534    fn test_const_options() {
12535        assert!(CAST_OPTIONS.safe)
12536    }
12537
12538    #[test]
12539    fn test_list_format_options() {
12540        let options = CastOptions {
12541            safe: false,
12542            format_options: FormatOptions::default().with_null("null"),
12543        };
12544        let array = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
12545            Some(vec![Some(0), Some(1), Some(2)]),
12546            Some(vec![Some(0), None, Some(2)]),
12547        ]);
12548        let a = cast_with_options(&array, &DataType::Utf8, &options).unwrap();
12549        let r: Vec<_> = a.as_string::<i32>().iter().flatten().collect();
12550        assert_eq!(r, &["[0, 1, 2]", "[0, null, 2]"]);
12551    }
12552    #[test]
12553    fn test_cast_string_to_timestamp_invalid_tz() {
12554        // content after Z should be ignored
12555        let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
12556        let array = StringArray::from(vec![Some(bad_timestamp)]);
12557
12558        let data_types = [
12559            DataType::Timestamp(TimeUnit::Second, None),
12560            DataType::Timestamp(TimeUnit::Millisecond, None),
12561            DataType::Timestamp(TimeUnit::Microsecond, None),
12562            DataType::Timestamp(TimeUnit::Nanosecond, None),
12563        ];
12564
12565        let cast_options = CastOptions {
12566            safe: false,
12567            ..Default::default()
12568        };
12569
12570        for dt in data_types {
12571            assert_eq!(
12572                cast_with_options(&array, &dt, &cast_options)
12573                    .unwrap_err()
12574                    .to_string(),
12575                "Parser error: Invalid timezone \"ZZTOP\": only offset based timezones supported without chrono-tz feature"
12576            );
12577        }
12578    }
12579    #[test]
12580    fn test_cast_struct_to_struct() {
12581        let struct_type = DataType::Struct(
12582            vec![
12583                Field::new("a", DataType::Boolean, false),
12584                Field::new("b", DataType::Int32, false),
12585            ]
12586            .into(),
12587        );
12588        let to_type = DataType::Struct(
12589            vec![
12590                Field::new("a", DataType::Utf8, false),
12591                Field::new("b", DataType::Utf8, false),
12592            ]
12593            .into(),
12594        );
12595        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12596        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
12597        let struct_array = StructArray::from(vec![
12598            (
12599                Arc::new(Field::new("b", DataType::Boolean, false)),
12600                boolean.clone() as ArrayRef,
12601            ),
12602            (
12603                Arc::new(Field::new("c", DataType::Int32, false)),
12604                int.clone() as ArrayRef,
12605            ),
12606        ]);
12607        let casted_array = cast(&struct_array, &to_type).unwrap();
12608        let casted_array = casted_array.as_struct();
12609        assert_eq!(casted_array.data_type(), &to_type);
12610        let casted_boolean_array = casted_array
12611            .column(0)
12612            .as_string::<i32>()
12613            .into_iter()
12614            .flatten()
12615            .collect::<Vec<_>>();
12616        let casted_int_array = casted_array
12617            .column(1)
12618            .as_string::<i32>()
12619            .into_iter()
12620            .flatten()
12621            .collect::<Vec<_>>();
12622        assert_eq!(casted_boolean_array, vec!["false", "false", "true", "true"]);
12623        assert_eq!(casted_int_array, vec!["42", "28", "19", "31"]);
12624
12625        // test for can't cast
12626        let to_type = DataType::Struct(
12627            vec![
12628                Field::new("a", DataType::Date32, false),
12629                Field::new("b", DataType::Utf8, false),
12630            ]
12631            .into(),
12632        );
12633        assert!(!can_cast_types(&struct_type, &to_type));
12634        let result = cast(&struct_array, &to_type);
12635        assert_eq!(
12636            "Cast error: Casting from Boolean to Date32 not supported",
12637            result.unwrap_err().to_string()
12638        );
12639    }
12640
12641    #[test]
12642    fn test_cast_struct_to_struct_nullability() {
12643        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12644        let int = Arc::new(Int32Array::from(vec![Some(42), None, Some(19), None]));
12645        let struct_array = StructArray::from(vec![
12646            (
12647                Arc::new(Field::new("b", DataType::Boolean, false)),
12648                boolean.clone() as ArrayRef,
12649            ),
12650            (
12651                Arc::new(Field::new("c", DataType::Int32, true)),
12652                int.clone() as ArrayRef,
12653            ),
12654        ]);
12655
12656        // okay: nullable to nullable
12657        let to_type = DataType::Struct(
12658            vec![
12659                Field::new("a", DataType::Utf8, false),
12660                Field::new("b", DataType::Utf8, true),
12661            ]
12662            .into(),
12663        );
12664        cast(&struct_array, &to_type).expect("Cast nullable to nullable struct field should work");
12665
12666        // error: nullable to non-nullable
12667        let to_type = DataType::Struct(
12668            vec![
12669                Field::new("a", DataType::Utf8, false),
12670                Field::new("b", DataType::Utf8, false),
12671            ]
12672            .into(),
12673        );
12674        cast(&struct_array, &to_type)
12675            .expect_err("Cast nullable to non-nullable struct field should fail");
12676
12677        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12678        let int = Arc::new(Int32Array::from(vec![i32::MAX, 25, 1, 100]));
12679        let struct_array = StructArray::from(vec![
12680            (
12681                Arc::new(Field::new("b", DataType::Boolean, false)),
12682                boolean.clone() as ArrayRef,
12683            ),
12684            (
12685                Arc::new(Field::new("c", DataType::Int32, false)),
12686                int.clone() as ArrayRef,
12687            ),
12688        ]);
12689
12690        // okay: non-nullable to non-nullable
12691        let to_type = DataType::Struct(
12692            vec![
12693                Field::new("a", DataType::Utf8, false),
12694                Field::new("b", DataType::Utf8, false),
12695            ]
12696            .into(),
12697        );
12698        cast(&struct_array, &to_type)
12699            .expect("Cast non-nullable to non-nullable struct field should work");
12700
12701        // err: non-nullable to non-nullable but overflowing return null during casting
12702        let to_type = DataType::Struct(
12703            vec![
12704                Field::new("a", DataType::Utf8, false),
12705                Field::new("b", DataType::Int8, false),
12706            ]
12707            .into(),
12708        );
12709        cast(&struct_array, &to_type).expect_err(
12710            "Cast non-nullable to non-nullable struct field returning null should fail",
12711        );
12712    }
12713
12714    #[test]
12715    fn test_cast_struct_to_non_struct() {
12716        let boolean = Arc::new(BooleanArray::from(vec![true, false]));
12717        let struct_array = StructArray::from(vec![(
12718            Arc::new(Field::new("a", DataType::Boolean, false)),
12719            boolean.clone() as ArrayRef,
12720        )]);
12721        let to_type = DataType::Utf8;
12722        let result = cast(&struct_array, &to_type);
12723        assert_eq!(
12724            r#"Cast error: Casting from Struct("a": non-null Boolean) to Utf8 not supported"#,
12725            result.unwrap_err().to_string()
12726        );
12727    }
12728
12729    #[test]
12730    fn test_cast_non_struct_to_struct() {
12731        let array = StringArray::from(vec!["a", "b"]);
12732        let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
12733        let result = cast(&array, &to_type);
12734        assert_eq!(
12735            r#"Cast error: Casting from Utf8 to Struct("a": non-null Boolean) not supported"#,
12736            result.unwrap_err().to_string()
12737        );
12738    }
12739
12740    #[test]
12741    fn test_cast_struct_with_different_field_order() {
12742        // Test slow path: fields are in different order
12743        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12744        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
12745        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
12746
12747        let struct_array = StructArray::from(vec![
12748            (
12749                Arc::new(Field::new("a", DataType::Boolean, false)),
12750                boolean.clone() as ArrayRef,
12751            ),
12752            (
12753                Arc::new(Field::new("b", DataType::Int32, false)),
12754                int.clone() as ArrayRef,
12755            ),
12756            (
12757                Arc::new(Field::new("c", DataType::Utf8, false)),
12758                string.clone() as ArrayRef,
12759            ),
12760        ]);
12761
12762        // Target has fields in different order: c, a, b instead of a, b, c
12763        let to_type = DataType::Struct(
12764            vec![
12765                Field::new("c", DataType::Utf8, false),
12766                Field::new("a", DataType::Utf8, false), // Boolean to Utf8
12767                Field::new("b", DataType::Utf8, false), // Int32 to Utf8
12768            ]
12769            .into(),
12770        );
12771
12772        let result = cast(&struct_array, &to_type).unwrap();
12773        let result_struct = result.as_struct();
12774
12775        assert_eq!(result_struct.data_type(), &to_type);
12776        assert_eq!(result_struct.num_columns(), 3);
12777
12778        // Verify field "c" (originally position 2, now position 0) remains Utf8
12779        let c_column = result_struct.column(0).as_string::<i32>();
12780        assert_eq!(
12781            c_column.into_iter().flatten().collect::<Vec<_>>(),
12782            vec!["foo", "bar", "baz", "qux"]
12783        );
12784
12785        // Verify field "a" (originally position 0, now position 1) was cast from Boolean to Utf8
12786        let a_column = result_struct.column(1).as_string::<i32>();
12787        assert_eq!(
12788            a_column.into_iter().flatten().collect::<Vec<_>>(),
12789            vec!["false", "false", "true", "true"]
12790        );
12791
12792        // Verify field "b" (originally position 1, now position 2) was cast from Int32 to Utf8
12793        let b_column = result_struct.column(2).as_string::<i32>();
12794        assert_eq!(
12795            b_column.into_iter().flatten().collect::<Vec<_>>(),
12796            vec!["42", "28", "19", "31"]
12797        );
12798    }
12799
12800    #[test]
12801    fn test_cast_struct_with_missing_field() {
12802        // Test that casting fails when target has a field not present in source
12803        let boolean = Arc::new(BooleanArray::from(vec![false, true]));
12804        let struct_array = StructArray::from(vec![(
12805            Arc::new(Field::new("a", DataType::Boolean, false)),
12806            boolean.clone() as ArrayRef,
12807        )]);
12808
12809        let to_type = DataType::Struct(
12810            vec![
12811                Field::new("a", DataType::Utf8, false),
12812                Field::new("b", DataType::Int32, false), // Field "b" doesn't exist in source
12813            ]
12814            .into(),
12815        );
12816
12817        let result = cast(&struct_array, &to_type);
12818        assert!(result.is_err());
12819        assert_eq!(
12820            result.unwrap_err().to_string(),
12821            "Invalid argument error: Incorrect number of arrays for StructArray fields, expected 2 got 1"
12822        );
12823    }
12824
12825    #[test]
12826    fn test_cast_struct_with_subset_of_fields() {
12827        // Test casting to a struct with fewer fields (selecting a subset)
12828        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12829        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
12830        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
12831
12832        let struct_array = StructArray::from(vec![
12833            (
12834                Arc::new(Field::new("a", DataType::Boolean, false)),
12835                boolean.clone() as ArrayRef,
12836            ),
12837            (
12838                Arc::new(Field::new("b", DataType::Int32, false)),
12839                int.clone() as ArrayRef,
12840            ),
12841            (
12842                Arc::new(Field::new("c", DataType::Utf8, false)),
12843                string.clone() as ArrayRef,
12844            ),
12845        ]);
12846
12847        // Target has only fields "c" and "a", omitting "b"
12848        let to_type = DataType::Struct(
12849            vec![
12850                Field::new("c", DataType::Utf8, false),
12851                Field::new("a", DataType::Utf8, false),
12852            ]
12853            .into(),
12854        );
12855
12856        let result = cast(&struct_array, &to_type).unwrap();
12857        let result_struct = result.as_struct();
12858
12859        assert_eq!(result_struct.data_type(), &to_type);
12860        assert_eq!(result_struct.num_columns(), 2);
12861
12862        // Verify field "c" remains Utf8
12863        let c_column = result_struct.column(0).as_string::<i32>();
12864        assert_eq!(
12865            c_column.into_iter().flatten().collect::<Vec<_>>(),
12866            vec!["foo", "bar", "baz", "qux"]
12867        );
12868
12869        // Verify field "a" was cast from Boolean to Utf8
12870        let a_column = result_struct.column(1).as_string::<i32>();
12871        assert_eq!(
12872            a_column.into_iter().flatten().collect::<Vec<_>>(),
12873            vec!["false", "false", "true", "true"]
12874        );
12875    }
12876
12877    #[test]
12878    fn test_can_cast_struct_rename_field() {
12879        // Test that can_cast_types returns false when target has a field not in source
12880        let from_type = DataType::Struct(
12881            vec![
12882                Field::new("a", DataType::Int32, false),
12883                Field::new("b", DataType::Utf8, false),
12884            ]
12885            .into(),
12886        );
12887
12888        let to_type = DataType::Struct(
12889            vec![
12890                Field::new("a", DataType::Int64, false),
12891                Field::new("c", DataType::Boolean, false), // Field "c" not in source
12892            ]
12893            .into(),
12894        );
12895
12896        assert!(can_cast_types(&from_type, &to_type));
12897    }
12898
12899    fn run_decimal_cast_test_case_between_multiple_types(t: DecimalCastTestConfig) {
12900        run_decimal_cast_test_case::<Decimal128Type, Decimal128Type>(t.clone());
12901        run_decimal_cast_test_case::<Decimal128Type, Decimal256Type>(t.clone());
12902        run_decimal_cast_test_case::<Decimal256Type, Decimal128Type>(t.clone());
12903        run_decimal_cast_test_case::<Decimal256Type, Decimal256Type>(t.clone());
12904    }
12905
12906    #[test]
12907    fn test_decimal_to_decimal_coverage() {
12908        let test_cases = [
12909            // increase precision, increase scale, infallible
12910            DecimalCastTestConfig {
12911                input_prec: 5,
12912                input_scale: 1,
12913                input_repr: 99999, // 9999.9
12914                output_prec: 10,
12915                output_scale: 6,
12916                expected_output_repr: Ok(9999900000), // 9999.900000
12917            },
12918            // increase precision, increase scale, fallible, safe
12919            DecimalCastTestConfig {
12920                input_prec: 5,
12921                input_scale: 1,
12922                input_repr: 99, // 9999.9
12923                output_prec: 7,
12924                output_scale: 6,
12925                expected_output_repr: Ok(9900000), // 9.900000
12926            },
12927            // increase precision, increase scale, fallible, unsafe
12928            DecimalCastTestConfig {
12929                input_prec: 5,
12930                input_scale: 1,
12931                input_repr: 99999, // 9999.9
12932                output_prec: 7,
12933                output_scale: 6,
12934                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
12935            },
12936            // increase precision, decrease scale, always infallible
12937            DecimalCastTestConfig {
12938                input_prec: 5,
12939                input_scale: 3,
12940                input_repr: 99999, // 99.999
12941                output_prec: 10,
12942                output_scale: 2,
12943                expected_output_repr: Ok(10000), // 100.00
12944            },
12945            // increase precision, decrease scale, no rouding
12946            DecimalCastTestConfig {
12947                input_prec: 5,
12948                input_scale: 3,
12949                input_repr: 99994, // 99.994
12950                output_prec: 10,
12951                output_scale: 2,
12952                expected_output_repr: Ok(9999), // 99.99
12953            },
12954            // increase precision, don't change scale, always infallible
12955            DecimalCastTestConfig {
12956                input_prec: 5,
12957                input_scale: 3,
12958                input_repr: 99999, // 99.999
12959                output_prec: 10,
12960                output_scale: 3,
12961                expected_output_repr: Ok(99999), // 99.999
12962            },
12963            // decrease precision, increase scale, safe
12964            DecimalCastTestConfig {
12965                input_prec: 10,
12966                input_scale: 5,
12967                input_repr: 999999, // 9.99999
12968                output_prec: 8,
12969                output_scale: 7,
12970                expected_output_repr: Ok(99999900), // 9.9999900
12971            },
12972            // decrease precision, increase scale, unsafe
12973            DecimalCastTestConfig {
12974                input_prec: 10,
12975                input_scale: 5,
12976                input_repr: 9999999, // 99.99999
12977                output_prec: 8,
12978                output_scale: 7,
12979                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
12980            },
12981            // decrease precision, decrease scale, safe, infallible
12982            DecimalCastTestConfig {
12983                input_prec: 7,
12984                input_scale: 4,
12985                input_repr: 9999999, // 999.9999
12986                output_prec: 6,
12987                output_scale: 2,
12988                expected_output_repr: Ok(100000),
12989            },
12990            // decrease precision, decrease scale, safe, fallible
12991            DecimalCastTestConfig {
12992                input_prec: 10,
12993                input_scale: 5,
12994                input_repr: 12345678, // 123.45678
12995                output_prec: 8,
12996                output_scale: 3,
12997                expected_output_repr: Ok(123457), // 123.457
12998            },
12999            // decrease precision, decrease scale, unsafe
13000            DecimalCastTestConfig {
13001                input_prec: 10,
13002                input_scale: 5,
13003                input_repr: 9999999, // 99.99999
13004                output_prec: 4,
13005                output_scale: 3,
13006                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
13007            },
13008            // decrease precision, same scale, safe
13009            DecimalCastTestConfig {
13010                input_prec: 10,
13011                input_scale: 5,
13012                input_repr: 999999, // 9.99999
13013                output_prec: 6,
13014                output_scale: 5,
13015                expected_output_repr: Ok(999999), // 9.99999
13016            },
13017            // decrease precision, same scale, unsafe
13018            DecimalCastTestConfig {
13019                input_prec: 10,
13020                input_scale: 5,
13021                input_repr: 9999999, // 99.99999
13022                output_prec: 6,
13023                output_scale: 5,
13024                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
13025            },
13026            // same precision, increase scale, safe
13027            DecimalCastTestConfig {
13028                input_prec: 7,
13029                input_scale: 4,
13030                input_repr: 12345, // 1.2345
13031                output_prec: 7,
13032                output_scale: 6,
13033                expected_output_repr: Ok(1234500), // 1.234500
13034            },
13035            // same precision, increase scale, unsafe
13036            DecimalCastTestConfig {
13037                input_prec: 7,
13038                input_scale: 4,
13039                input_repr: 123456, // 12.3456
13040                output_prec: 7,
13041                output_scale: 6,
13042                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
13043            },
13044            // same precision, decrease scale, infallible
13045            DecimalCastTestConfig {
13046                input_prec: 7,
13047                input_scale: 5,
13048                input_repr: 1234567, // 12.34567
13049                output_prec: 7,
13050                output_scale: 4,
13051                expected_output_repr: Ok(123457), // 12.3457
13052            },
13053            // same precision, same scale, infallible
13054            DecimalCastTestConfig {
13055                input_prec: 7,
13056                input_scale: 5,
13057                input_repr: 9999999, // 99.99999
13058                output_prec: 7,
13059                output_scale: 5,
13060                expected_output_repr: Ok(9999999), // 99.99999
13061            },
13062            // precision increase, input scale & output scale = 0, infallible
13063            DecimalCastTestConfig {
13064                input_prec: 7,
13065                input_scale: 0,
13066                input_repr: 1234567, // 1234567
13067                output_prec: 8,
13068                output_scale: 0,
13069                expected_output_repr: Ok(1234567), // 1234567
13070            },
13071            // precision decrease, input scale & output scale = 0, failure
13072            DecimalCastTestConfig {
13073                input_prec: 7,
13074                input_scale: 0,
13075                input_repr: 1234567, // 1234567
13076                output_prec: 6,
13077                output_scale: 0,
13078                expected_output_repr: Err("Invalid argument error: 1234567 is too large to store in a {} of precision 6. Max is 999999".to_string())
13079            },
13080            // precision decrease, input scale & output scale = 0, success
13081            DecimalCastTestConfig {
13082                input_prec: 7,
13083                input_scale: 0,
13084                input_repr: 123456, // 123456
13085                output_prec: 6,
13086                output_scale: 0,
13087                expected_output_repr: Ok(123456), // 123456
13088            },
13089        ];
13090
13091        for t in test_cases {
13092            run_decimal_cast_test_case_between_multiple_types(t);
13093        }
13094    }
13095
13096    #[test]
13097    fn test_decimal_to_decimal_increase_scale_and_precision_unchecked() {
13098        let test_cases = [
13099            DecimalCastTestConfig {
13100                input_prec: 5,
13101                input_scale: 0,
13102                input_repr: 99999,
13103                output_prec: 10,
13104                output_scale: 5,
13105                expected_output_repr: Ok(9999900000),
13106            },
13107            DecimalCastTestConfig {
13108                input_prec: 5,
13109                input_scale: 0,
13110                input_repr: -99999,
13111                output_prec: 10,
13112                output_scale: 5,
13113                expected_output_repr: Ok(-9999900000),
13114            },
13115            DecimalCastTestConfig {
13116                input_prec: 5,
13117                input_scale: 2,
13118                input_repr: 99999,
13119                output_prec: 10,
13120                output_scale: 5,
13121                expected_output_repr: Ok(99999000),
13122            },
13123            DecimalCastTestConfig {
13124                input_prec: 5,
13125                input_scale: -2,
13126                input_repr: -99999,
13127                output_prec: 10,
13128                output_scale: 3,
13129                expected_output_repr: Ok(-9999900000),
13130            },
13131            DecimalCastTestConfig {
13132                input_prec: 5,
13133                input_scale: 3,
13134                input_repr: -12345,
13135                output_prec: 6,
13136                output_scale: 5,
13137                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())
13138            },
13139        ];
13140
13141        for t in test_cases {
13142            run_decimal_cast_test_case_between_multiple_types(t);
13143        }
13144    }
13145
13146    #[test]
13147    fn test_decimal_to_decimal_decrease_scale_and_precision_unchecked() {
13148        let test_cases = [
13149            DecimalCastTestConfig {
13150                input_prec: 5,
13151                input_scale: 0,
13152                input_repr: 99999,
13153                output_scale: -3,
13154                output_prec: 3,
13155                expected_output_repr: Ok(100),
13156            },
13157            DecimalCastTestConfig {
13158                input_prec: 5,
13159                input_scale: 0,
13160                input_repr: -99999,
13161                output_prec: 1,
13162                output_scale: -5,
13163                expected_output_repr: Ok(-1),
13164            },
13165            DecimalCastTestConfig {
13166                input_prec: 10,
13167                input_scale: 2,
13168                input_repr: 123456789,
13169                output_prec: 5,
13170                output_scale: -2,
13171                expected_output_repr: Ok(12346),
13172            },
13173            DecimalCastTestConfig {
13174                input_prec: 10,
13175                input_scale: 4,
13176                input_repr: -9876543210,
13177                output_prec: 7,
13178                output_scale: 0,
13179                expected_output_repr: Ok(-987654),
13180            },
13181            DecimalCastTestConfig {
13182                input_prec: 7,
13183                input_scale: 4,
13184                input_repr: 9999999,
13185                output_prec: 6,
13186                output_scale: 3,
13187                expected_output_repr:
13188                    Err("Invalid argument error: 1000.000 is too large to store in a {} of precision 6. Max is 999.999".to_string()),
13189            },
13190        ];
13191        for t in test_cases {
13192            run_decimal_cast_test_case_between_multiple_types(t);
13193        }
13194    }
13195
13196    #[test]
13197    fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
13198        let array = vec![Some(123456789)];
13199        let array = create_decimal128_array(array, 24, 2).unwrap();
13200        let input_type = DataType::Decimal128(24, 2);
13201        let output_type = DataType::Decimal128(6, 2);
13202        assert!(can_cast_types(&input_type, &output_type));
13203
13204        let options = CastOptions {
13205            safe: false,
13206            ..Default::default()
13207        };
13208        let result = cast_with_options(&array, &output_type, &options);
13209        assert_eq!(
13210            result.unwrap_err().to_string(),
13211            "Invalid argument error: 1234567.89 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
13212        );
13213    }
13214
13215    #[test]
13216    fn test_decimal_to_decimal_same_scale() {
13217        let array = vec![Some(520)];
13218        let array = create_decimal128_array(array, 4, 2).unwrap();
13219        let input_type = DataType::Decimal128(4, 2);
13220        let output_type = DataType::Decimal128(3, 2);
13221        assert!(can_cast_types(&input_type, &output_type));
13222
13223        let options = CastOptions {
13224            safe: false,
13225            ..Default::default()
13226        };
13227        let result = cast_with_options(&array, &output_type, &options);
13228        assert_eq!(
13229            result.unwrap().as_primitive::<Decimal128Type>().value(0),
13230            520
13231        );
13232
13233        // Cast 0 of decimal(3, 0) type to decimal(2, 0)
13234        assert_eq!(
13235            &cast(
13236                &create_decimal128_array(vec![Some(0)], 3, 0).unwrap(),
13237                &DataType::Decimal128(2, 0)
13238            )
13239            .unwrap(),
13240            &(Arc::new(create_decimal128_array(vec![Some(0)], 2, 0).unwrap()) as ArrayRef)
13241        );
13242    }
13243
13244    #[test]
13245    fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale() {
13246        let array = vec![Some(123456789)];
13247        let array = create_decimal128_array(array, 24, 4).unwrap();
13248        let input_type = DataType::Decimal128(24, 4);
13249        let output_type = DataType::Decimal128(6, 2);
13250        assert!(can_cast_types(&input_type, &output_type));
13251
13252        let options = CastOptions {
13253            safe: false,
13254            ..Default::default()
13255        };
13256        let result = cast_with_options(&array, &output_type, &options);
13257        assert_eq!(
13258            result.unwrap_err().to_string(),
13259            "Invalid argument error: 12345.68 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
13260        );
13261    }
13262
13263    #[test]
13264    fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale() {
13265        let array = vec![Some(123456789)];
13266        let array = create_decimal128_array(array, 24, 2).unwrap();
13267        let input_type = DataType::Decimal128(24, 2);
13268        let output_type = DataType::Decimal128(6, 3);
13269        assert!(can_cast_types(&input_type, &output_type));
13270
13271        let options = CastOptions {
13272            safe: false,
13273            ..Default::default()
13274        };
13275        let result = cast_with_options(&array, &output_type, &options);
13276        assert_eq!(
13277            result.unwrap_err().to_string(),
13278            "Invalid argument error: 1234567.890 is too large to store in a Decimal128 of precision 6. Max is 999.999"
13279        );
13280    }
13281
13282    #[test]
13283    fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type() {
13284        let array = vec![Some(123456789)];
13285        let array = create_decimal128_array(array, 24, 2).unwrap();
13286        let input_type = DataType::Decimal128(24, 2);
13287        let output_type = DataType::Decimal256(6, 2);
13288        assert!(can_cast_types(&input_type, &output_type));
13289
13290        let options = CastOptions {
13291            safe: false,
13292            ..Default::default()
13293        };
13294        let result = cast_with_options(&array, &output_type, &options).unwrap_err();
13295        assert_eq!(
13296            result.to_string(),
13297            "Invalid argument error: 1234567.89 is too large to store in a Decimal256 of precision 6. Max is 9999.99"
13298        );
13299    }
13300
13301    #[test]
13302    fn test_first_none() {
13303        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
13304            None,
13305            Some(vec![Some(1), Some(2)]),
13306        ])) as ArrayRef;
13307        let data_type =
13308            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
13309        let opt = CastOptions::default();
13310        let r = cast_with_options(&array, &data_type, &opt).unwrap();
13311
13312        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
13313            vec![None, Some(vec![Some(1), Some(2)])],
13314            2,
13315        )) as ArrayRef;
13316        assert_eq!(*fixed_array, *r);
13317    }
13318
13319    #[test]
13320    fn test_first_last_none() {
13321        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
13322            None,
13323            Some(vec![Some(1), Some(2)]),
13324            None,
13325        ])) as ArrayRef;
13326        let data_type =
13327            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
13328        let opt = CastOptions::default();
13329        let r = cast_with_options(&array, &data_type, &opt).unwrap();
13330
13331        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
13332            vec![None, Some(vec![Some(1), Some(2)]), None],
13333            2,
13334        )) as ArrayRef;
13335        assert_eq!(*fixed_array, *r);
13336    }
13337
13338    #[test]
13339    fn test_cast_decimal_error_output() {
13340        let array = Int64Array::from(vec![1]);
13341        let error = cast_with_options(
13342            &array,
13343            &DataType::Decimal32(1, 1),
13344            &CastOptions {
13345                safe: false,
13346                format_options: FormatOptions::default(),
13347            },
13348        )
13349        .unwrap_err();
13350        assert_eq!(
13351            error.to_string(),
13352            "Invalid argument error: 1.0 is too large to store in a Decimal32 of precision 1. Max is 0.9"
13353        );
13354
13355        let array = Int64Array::from(vec![-1]);
13356        let error = cast_with_options(
13357            &array,
13358            &DataType::Decimal32(1, 1),
13359            &CastOptions {
13360                safe: false,
13361                format_options: FormatOptions::default(),
13362            },
13363        )
13364        .unwrap_err();
13365        assert_eq!(
13366            error.to_string(),
13367            "Invalid argument error: -1.0 is too small to store in a Decimal32 of precision 1. Min is -0.9"
13368        );
13369    }
13370
13371    #[test]
13372    fn test_run_end_encoded_to_primitive() {
13373        // Create a RunEndEncoded array: [1, 1, 2, 2, 2, 3]
13374        let run_ends = Int32Array::from(vec![2, 5, 6]);
13375        let values = Int32Array::from(vec![1, 2, 3]);
13376        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13377        let array_ref = Arc::new(run_array) as ArrayRef;
13378        // Cast to Int64
13379        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
13380        // Verify the result is a RunArray with Int64 values
13381        let result_run_array = cast_result.as_any().downcast_ref::<Int64Array>().unwrap();
13382        assert_eq!(
13383            result_run_array.values(),
13384            &[1i64, 1i64, 2i64, 2i64, 2i64, 3i64]
13385        );
13386    }
13387
13388    #[test]
13389    fn test_sliced_run_end_encoded_to_primitive() {
13390        let run_ends = Int32Array::from(vec![2, 5, 6]);
13391        let values = Int32Array::from(vec![1, 2, 3]);
13392        // [1, 1, 2, 2, 2, 3]
13393        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13394        let run_array = run_array.slice(3, 3); // [2, 2, 3]
13395        let array_ref = Arc::new(run_array) as ArrayRef;
13396
13397        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
13398        let result_run_array = cast_result.as_primitive::<Int64Type>();
13399        assert_eq!(result_run_array.values(), &[2, 2, 3]);
13400    }
13401
13402    #[test]
13403    fn test_run_end_encoded_to_string() {
13404        let run_ends = Int32Array::from(vec![2, 3, 5]);
13405        let values = Int32Array::from(vec![10, 20, 30]);
13406        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13407        let array_ref = Arc::new(run_array) as ArrayRef;
13408
13409        // Cast to String
13410        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
13411
13412        // Verify the result is a RunArray with String values
13413        let result_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
13414        // Check that values are correct
13415        assert_eq!(result_array.value(0), "10");
13416        assert_eq!(result_array.value(1), "10");
13417        assert_eq!(result_array.value(2), "20");
13418    }
13419
13420    #[test]
13421    fn test_primitive_to_run_end_encoded() {
13422        // Create an Int32 array with repeated values: [1, 1, 2, 2, 2, 3]
13423        let source_array = Int32Array::from(vec![1, 1, 2, 2, 2, 3]);
13424        let array_ref = Arc::new(source_array) as ArrayRef;
13425
13426        // Cast to RunEndEncoded<Int32, Int32>
13427        let target_type = DataType::RunEndEncoded(
13428            Arc::new(Field::new("run_ends", DataType::Int32, false)),
13429            Arc::new(Field::new("values", DataType::Int32, true)),
13430        );
13431        let cast_result = cast(&array_ref, &target_type).unwrap();
13432
13433        // Verify the result is a RunArray
13434        let result_run_array = cast_result
13435            .as_any()
13436            .downcast_ref::<RunArray<Int32Type>>()
13437            .unwrap();
13438
13439        // Check run structure: runs should end at positions [2, 5, 6]
13440        assert_eq!(result_run_array.run_ends().values(), &[2, 5, 6]);
13441
13442        // Check values: should be [1, 2, 3]
13443        let values_array = result_run_array.values().as_primitive::<Int32Type>();
13444        assert_eq!(values_array.values(), &[1, 2, 3]);
13445    }
13446
13447    #[test]
13448    fn test_primitive_to_run_end_encoded_with_nulls() {
13449        let source_array = Int32Array::from(vec![
13450            Some(1),
13451            Some(1),
13452            None,
13453            None,
13454            Some(2),
13455            Some(2),
13456            Some(3),
13457            Some(3),
13458            None,
13459            None,
13460            Some(4),
13461            Some(4),
13462            Some(5),
13463            Some(5),
13464            None,
13465            None,
13466        ]);
13467        let array_ref = Arc::new(source_array) as ArrayRef;
13468        let target_type = DataType::RunEndEncoded(
13469            Arc::new(Field::new("run_ends", DataType::Int32, false)),
13470            Arc::new(Field::new("values", DataType::Int32, true)),
13471        );
13472        let cast_result = cast(&array_ref, &target_type).unwrap();
13473        let result_run_array = cast_result
13474            .as_any()
13475            .downcast_ref::<RunArray<Int32Type>>()
13476            .unwrap();
13477        assert_eq!(
13478            result_run_array.run_ends().values(),
13479            &[2, 4, 6, 8, 10, 12, 14, 16]
13480        );
13481        assert_eq!(
13482            result_run_array
13483                .values()
13484                .as_primitive::<Int32Type>()
13485                .values(),
13486            &[1, 0, 2, 3, 0, 4, 5, 0]
13487        );
13488        assert_eq!(result_run_array.values().null_count(), 3);
13489    }
13490
13491    #[test]
13492    fn test_primitive_to_run_end_encoded_with_nulls_consecutive() {
13493        let source_array = Int64Array::from(vec![
13494            Some(1),
13495            Some(1),
13496            None,
13497            None,
13498            None,
13499            None,
13500            None,
13501            None,
13502            None,
13503            None,
13504            Some(4),
13505            Some(20),
13506            Some(500),
13507            Some(500),
13508            None,
13509            None,
13510        ]);
13511        let array_ref = Arc::new(source_array) as ArrayRef;
13512        let target_type = DataType::RunEndEncoded(
13513            Arc::new(Field::new("run_ends", DataType::Int16, false)),
13514            Arc::new(Field::new("values", DataType::Int64, true)),
13515        );
13516        let cast_result = cast(&array_ref, &target_type).unwrap();
13517        let result_run_array = cast_result
13518            .as_any()
13519            .downcast_ref::<RunArray<Int16Type>>()
13520            .unwrap();
13521        assert_eq!(
13522            result_run_array.run_ends().values(),
13523            &[2, 10, 11, 12, 14, 16]
13524        );
13525        assert_eq!(
13526            result_run_array
13527                .values()
13528                .as_primitive::<Int64Type>()
13529                .values(),
13530            &[1, 0, 4, 20, 500, 0]
13531        );
13532        assert_eq!(result_run_array.values().null_count(), 2);
13533    }
13534
13535    #[test]
13536    fn test_string_to_run_end_encoded() {
13537        // Create a String array with repeated values: ["a", "a", "b", "c", "c"]
13538        let source_array = StringArray::from(vec!["a", "a", "b", "c", "c"]);
13539        let array_ref = Arc::new(source_array) as ArrayRef;
13540
13541        // Cast to RunEndEncoded<Int32, String>
13542        let target_type = DataType::RunEndEncoded(
13543            Arc::new(Field::new("run_ends", DataType::Int32, false)),
13544            Arc::new(Field::new("values", DataType::Utf8, true)),
13545        );
13546        let cast_result = cast(&array_ref, &target_type).unwrap();
13547
13548        // Verify the result is a RunArray
13549        let result_run_array = cast_result
13550            .as_any()
13551            .downcast_ref::<RunArray<Int32Type>>()
13552            .unwrap();
13553
13554        // Check run structure: runs should end at positions [2, 3, 5]
13555        assert_eq!(result_run_array.run_ends().values(), &[2, 3, 5]);
13556
13557        // Check values: should be ["a", "b", "c"]
13558        let values_array = result_run_array.values().as_string::<i32>();
13559        assert_eq!(values_array.value(0), "a");
13560        assert_eq!(values_array.value(1), "b");
13561        assert_eq!(values_array.value(2), "c");
13562    }
13563
13564    #[test]
13565    fn test_empty_array_to_run_end_encoded() {
13566        // Create an empty Int32 array
13567        let source_array = Int32Array::from(Vec::<i32>::new());
13568        let array_ref = Arc::new(source_array) as ArrayRef;
13569
13570        // Cast to RunEndEncoded<Int32, Int32>
13571        let target_type = DataType::RunEndEncoded(
13572            Arc::new(Field::new("run_ends", DataType::Int32, false)),
13573            Arc::new(Field::new("values", DataType::Int32, true)),
13574        );
13575        let cast_result = cast(&array_ref, &target_type).unwrap();
13576
13577        // Verify the result is an empty RunArray
13578        let result_run_array = cast_result
13579            .as_any()
13580            .downcast_ref::<RunArray<Int32Type>>()
13581            .unwrap();
13582
13583        // Check that both run_ends and values are empty
13584        assert_eq!(result_run_array.run_ends().len(), 0);
13585        assert_eq!(result_run_array.values().len(), 0);
13586    }
13587
13588    #[test]
13589    fn test_run_end_encoded_with_nulls() {
13590        // Create a RunEndEncoded array with nulls: [1, 1, null, 2, 2]
13591        let run_ends = Int32Array::from(vec![2, 3, 5]);
13592        let values = Int32Array::from(vec![Some(1), None, Some(2)]);
13593        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13594        let array_ref = Arc::new(run_array) as ArrayRef;
13595
13596        // Cast to String
13597        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
13598
13599        // Verify the result preserves nulls
13600        let result_run_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
13601        assert_eq!(result_run_array.value(0), "1");
13602        assert!(result_run_array.is_null(2));
13603        assert_eq!(result_run_array.value(4), "2");
13604    }
13605
13606    #[test]
13607    fn test_different_index_types() {
13608        // Test with Int16 index type
13609        let source_array = Int32Array::from(vec![1, 1, 2, 3, 3]);
13610        let array_ref = Arc::new(source_array) as ArrayRef;
13611
13612        let target_type = DataType::RunEndEncoded(
13613            Arc::new(Field::new("run_ends", DataType::Int16, false)),
13614            Arc::new(Field::new("values", DataType::Int32, true)),
13615        );
13616        let cast_result = cast(&array_ref, &target_type).unwrap();
13617        assert_eq!(cast_result.data_type(), &target_type);
13618
13619        // Verify the cast worked correctly: values are [1, 2, 3]
13620        // and run-ends are [2, 3, 5]
13621        let run_array = cast_result
13622            .as_any()
13623            .downcast_ref::<RunArray<Int16Type>>()
13624            .unwrap();
13625        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
13626        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
13627        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
13628        assert_eq!(run_array.run_ends().values(), &[2i16, 3i16, 5i16]);
13629
13630        // Test again with Int64 index type
13631        let target_type = DataType::RunEndEncoded(
13632            Arc::new(Field::new("run_ends", DataType::Int64, false)),
13633            Arc::new(Field::new("values", DataType::Int32, true)),
13634        );
13635        let cast_result = cast(&array_ref, &target_type).unwrap();
13636        assert_eq!(cast_result.data_type(), &target_type);
13637
13638        // Verify the cast worked correctly: values are [1, 2, 3]
13639        // and run-ends are [2, 3, 5]
13640        let run_array = cast_result
13641            .as_any()
13642            .downcast_ref::<RunArray<Int64Type>>()
13643            .unwrap();
13644        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
13645        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
13646        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
13647        assert_eq!(run_array.run_ends().values(), &[2i64, 3i64, 5i64]);
13648    }
13649
13650    #[test]
13651    fn test_unsupported_cast_to_run_end_encoded() {
13652        // Create a Struct array - complex nested type that might not be supported
13653        let field = Field::new("item", DataType::Int32, false);
13654        let struct_array = StructArray::from(vec![(
13655            Arc::new(field),
13656            Arc::new(Int32Array::from(vec![1, 2, 3])) as ArrayRef,
13657        )]);
13658        let array_ref = Arc::new(struct_array) as ArrayRef;
13659
13660        // This should fail because:
13661        // 1. The target type is not RunEndEncoded
13662        // 2. The target type is not supported for casting from StructArray
13663        let cast_result = cast(&array_ref, &DataType::FixedSizeBinary(10));
13664
13665        // Expect this to fail
13666        assert!(cast_result.is_err());
13667    }
13668
13669    /// Test casting RunEndEncoded<Int64, String> to RunEndEncoded<Int16, String> should fail
13670    #[test]
13671    fn test_cast_run_end_encoded_int64_to_int16_should_fail() {
13672        // Construct a valid REE array with Int64 run-ends
13673        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
13674        let values = StringArray::from(vec!["a", "b", "c"]);
13675
13676        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
13677        let array_ref = Arc::new(ree_array) as ArrayRef;
13678
13679        // Attempt to cast to RunEndEncoded<Int16, Utf8>
13680        let target_type = DataType::RunEndEncoded(
13681            Arc::new(Field::new("run_ends", DataType::Int16, false)),
13682            Arc::new(Field::new("values", DataType::Utf8, true)),
13683        );
13684        let cast_options = CastOptions {
13685            safe: false, // This should make it fail instead of returning nulls
13686            format_options: FormatOptions::default(),
13687        };
13688
13689        // This should fail due to run-end overflow
13690        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
13691            cast_with_options(&array_ref, &target_type, &cast_options);
13692
13693        let e = result.expect_err("Cast should have failed but succeeded");
13694        assert!(
13695            e.to_string()
13696                .contains("Cast error: Can't cast value 100000 to type Int16")
13697        );
13698    }
13699
13700    #[test]
13701    fn test_cast_run_end_encoded_int64_to_int16_with_safe_should_fail_with_null_invalid_error() {
13702        // Construct a valid REE array with Int64 run-ends
13703        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
13704        let values = StringArray::from(vec!["a", "b", "c"]);
13705
13706        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
13707        let array_ref = Arc::new(ree_array) as ArrayRef;
13708
13709        // Attempt to cast to RunEndEncoded<Int16, Utf8>
13710        let target_type = DataType::RunEndEncoded(
13711            Arc::new(Field::new("run_ends", DataType::Int16, false)),
13712            Arc::new(Field::new("values", DataType::Utf8, true)),
13713        );
13714        let cast_options = CastOptions {
13715            safe: true,
13716            format_options: FormatOptions::default(),
13717        };
13718
13719        // This fails even though safe is true because the run_ends array has null values
13720        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
13721            cast_with_options(&array_ref, &target_type, &cast_options);
13722        let e = result.expect_err("Cast should have failed but succeeded");
13723        assert!(
13724            e.to_string()
13725                .contains("Invalid argument error: Found null values in run_ends array. The run_ends array should not have null values.")
13726        );
13727    }
13728
13729    /// Test casting RunEndEncoded<Int16, String> to RunEndEncoded<Int64, String> should succeed
13730    #[test]
13731    fn test_cast_run_end_encoded_int16_to_int64_should_succeed() {
13732        // Construct a valid REE array with Int16 run-ends
13733        let run_ends = Int16Array::from(vec![2, 5, 8]); // values that fit in Int16
13734        let values = StringArray::from(vec!["a", "b", "c"]);
13735
13736        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
13737        let array_ref = Arc::new(ree_array) as ArrayRef;
13738
13739        // Attempt to cast to RunEndEncoded<Int64, Utf8> (upcast should succeed)
13740        let target_type = DataType::RunEndEncoded(
13741            Arc::new(Field::new("run_ends", DataType::Int64, false)),
13742            Arc::new(Field::new("values", DataType::Utf8, true)),
13743        );
13744        let cast_options = CastOptions {
13745            safe: false,
13746            format_options: FormatOptions::default(),
13747        };
13748
13749        // This should succeed due to valid upcast
13750        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
13751            cast_with_options(&array_ref, &target_type, &cast_options);
13752
13753        let array_ref = result.expect("Cast should have succeeded but failed");
13754        // Downcast to RunArray<Int64Type>
13755        let run_array = array_ref
13756            .as_any()
13757            .downcast_ref::<RunArray<Int64Type>>()
13758            .unwrap();
13759
13760        // Verify the cast worked correctly
13761        // Assert the values were cast correctly
13762        assert_eq!(run_array.run_ends().values(), &[2i64, 5i64, 8i64]);
13763        assert_eq!(run_array.values().as_string::<i32>().value(0), "a");
13764        assert_eq!(run_array.values().as_string::<i32>().value(1), "b");
13765        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
13766    }
13767
13768    #[test]
13769    fn test_cast_run_end_encoded_dictionary_to_run_end_encoded() {
13770        // Construct a valid dictionary encoded array
13771        let values = StringArray::from_iter([Some("a"), Some("b"), Some("c")]);
13772        let keys = UInt64Array::from_iter(vec![1, 1, 1, 0, 0, 0, 2, 2, 2]);
13773        let array_ref = Arc::new(DictionaryArray::new(keys, Arc::new(values))) as ArrayRef;
13774
13775        // Attempt to cast to RunEndEncoded<Int64, Utf8>
13776        let target_type = DataType::RunEndEncoded(
13777            Arc::new(Field::new("run_ends", DataType::Int64, false)),
13778            Arc::new(Field::new("values", DataType::Utf8, true)),
13779        );
13780        let cast_options = CastOptions {
13781            safe: false,
13782            format_options: FormatOptions::default(),
13783        };
13784
13785        // This should succeed
13786        let result = cast_with_options(&array_ref, &target_type, &cast_options)
13787            .expect("Cast should have succeeded but failed");
13788
13789        // Verify the cast worked correctly
13790        // Assert the values were cast correctly
13791        let run_array = result
13792            .as_any()
13793            .downcast_ref::<RunArray<Int64Type>>()
13794            .unwrap();
13795        assert_eq!(run_array.values().as_string::<i32>().value(0), "b");
13796        assert_eq!(run_array.values().as_string::<i32>().value(1), "a");
13797        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
13798
13799        // Verify the run-ends were cast correctly (run ends at 3, 6, 9)
13800        assert_eq!(run_array.run_ends().values(), &[3i64, 6i64, 9i64]);
13801    }
13802
13803    fn int32_list_values() -> Vec<Option<Vec<Option<i32>>>> {
13804        vec![
13805            Some(vec![Some(1), Some(2), Some(3)]),
13806            Some(vec![Some(4), Some(5), Some(6)]),
13807            None,
13808            Some(vec![Some(7), Some(8), Some(9)]),
13809            Some(vec![None, Some(10)]),
13810        ]
13811    }
13812
13813    #[test]
13814    fn test_cast_list_view_to_list() {
13815        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13816        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13817        assert!(can_cast_types(list_view.data_type(), &target_type));
13818        let cast_result = cast(&list_view, &target_type).unwrap();
13819        let got_list = cast_result.as_list::<i32>();
13820        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13821        assert_eq!(got_list, &expected_list);
13822    }
13823
13824    #[test]
13825    fn test_cast_list_view_to_large_list() {
13826        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13827        let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
13828        assert!(can_cast_types(list_view.data_type(), &target_type));
13829        let cast_result = cast(&list_view, &target_type).unwrap();
13830        let got_list = cast_result.as_list::<i64>();
13831        let expected_list =
13832            LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13833        assert_eq!(got_list, &expected_list);
13834    }
13835
13836    #[test]
13837    fn test_cast_list_to_list_view() {
13838        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13839        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
13840        assert!(can_cast_types(list.data_type(), &target_type));
13841        let cast_result = cast(&list, &target_type).unwrap();
13842
13843        let got_list_view = cast_result.as_list_view::<i32>();
13844        let expected_list_view =
13845            ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13846        assert_eq!(got_list_view, &expected_list_view);
13847
13848        // inner types get cast
13849        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13850            Some(vec![Some(1), Some(2)]),
13851            None,
13852            Some(vec![None, Some(3)]),
13853        ]);
13854        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Float32, true)));
13855        assert!(can_cast_types(list.data_type(), &target_type));
13856        let cast_result = cast(&list, &target_type).unwrap();
13857
13858        let got_list_view = cast_result.as_list_view::<i32>();
13859        let expected_list_view = ListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13860            Some(vec![Some(1.0), Some(2.0)]),
13861            None,
13862            Some(vec![None, Some(3.0)]),
13863        ]);
13864        assert_eq!(got_list_view, &expected_list_view);
13865    }
13866
13867    #[test]
13868    fn test_cast_list_to_large_list_view() {
13869        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13870            Some(vec![Some(1), Some(2)]),
13871            None,
13872            Some(vec![None, Some(3)]),
13873        ]);
13874        let target_type =
13875            DataType::LargeListView(Arc::new(Field::new("item", DataType::Float32, true)));
13876        assert!(can_cast_types(list.data_type(), &target_type));
13877        let cast_result = cast(&list, &target_type).unwrap();
13878
13879        let got_list_view = cast_result.as_list_view::<i64>();
13880        let expected_list_view =
13881            LargeListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13882                Some(vec![Some(1.0), Some(2.0)]),
13883                None,
13884                Some(vec![None, Some(3.0)]),
13885            ]);
13886        assert_eq!(got_list_view, &expected_list_view);
13887    }
13888
13889    #[test]
13890    fn test_cast_large_list_view_to_large_list() {
13891        let list_view =
13892            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13893        let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
13894        assert!(can_cast_types(list_view.data_type(), &target_type));
13895        let cast_result = cast(&list_view, &target_type).unwrap();
13896        let got_list = cast_result.as_list::<i64>();
13897
13898        let expected_list =
13899            LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13900        assert_eq!(got_list, &expected_list);
13901    }
13902
13903    #[test]
13904    fn test_cast_large_list_view_to_list() {
13905        let list_view =
13906            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13907        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13908        assert!(can_cast_types(list_view.data_type(), &target_type));
13909        let cast_result = cast(&list_view, &target_type).unwrap();
13910        let got_list = cast_result.as_list::<i32>();
13911
13912        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13913        assert_eq!(got_list, &expected_list);
13914    }
13915
13916    #[test]
13917    fn test_cast_large_list_to_large_list_view() {
13918        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13919        let target_type =
13920            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
13921        assert!(can_cast_types(list.data_type(), &target_type));
13922        let cast_result = cast(&list, &target_type).unwrap();
13923
13924        let got_list_view = cast_result.as_list_view::<i64>();
13925        let expected_list_view =
13926            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13927        assert_eq!(got_list_view, &expected_list_view);
13928
13929        // inner types get cast
13930        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13931            Some(vec![Some(1), Some(2)]),
13932            None,
13933            Some(vec![None, Some(3)]),
13934        ]);
13935        let target_type =
13936            DataType::LargeListView(Arc::new(Field::new("item", DataType::Float32, true)));
13937        assert!(can_cast_types(list.data_type(), &target_type));
13938        let cast_result = cast(&list, &target_type).unwrap();
13939
13940        let got_list_view = cast_result.as_list_view::<i64>();
13941        let expected_list_view =
13942            LargeListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13943                Some(vec![Some(1.0), Some(2.0)]),
13944                None,
13945                Some(vec![None, Some(3.0)]),
13946            ]);
13947        assert_eq!(got_list_view, &expected_list_view);
13948    }
13949
13950    #[test]
13951    fn test_cast_large_list_to_list_view() {
13952        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13953            Some(vec![Some(1), Some(2)]),
13954            None,
13955            Some(vec![None, Some(3)]),
13956        ]);
13957        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Float32, true)));
13958        assert!(can_cast_types(list.data_type(), &target_type));
13959        let cast_result = cast(&list, &target_type).unwrap();
13960
13961        let got_list_view = cast_result.as_list_view::<i32>();
13962        let expected_list_view = ListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13963            Some(vec![Some(1.0), Some(2.0)]),
13964            None,
13965            Some(vec![None, Some(3.0)]),
13966        ]);
13967        assert_eq!(got_list_view, &expected_list_view);
13968    }
13969
13970    #[test]
13971    fn test_cast_list_view_to_list_out_of_order() {
13972        let list_view = ListViewArray::new(
13973            Arc::new(Field::new("item", DataType::Int32, true)),
13974            ScalarBuffer::from(vec![0, 6, 3]),
13975            ScalarBuffer::from(vec![3, 3, 3]),
13976            Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
13977            None,
13978        );
13979        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13980        assert!(can_cast_types(list_view.data_type(), &target_type));
13981        let cast_result = cast(&list_view, &target_type).unwrap();
13982        let got_list = cast_result.as_list::<i32>();
13983        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13984            Some(vec![Some(1), Some(2), Some(3)]),
13985            Some(vec![Some(7), Some(8), Some(9)]),
13986            Some(vec![Some(4), Some(5), Some(6)]),
13987        ]);
13988        assert_eq!(got_list, &expected_list);
13989    }
13990
13991    #[test]
13992    fn test_cast_list_view_to_list_overlapping() {
13993        let list_view = ListViewArray::new(
13994            Arc::new(Field::new("item", DataType::Int32, true)),
13995            ScalarBuffer::from(vec![0, 0]),
13996            ScalarBuffer::from(vec![1, 2]),
13997            Arc::new(Int32Array::from(vec![1, 2])),
13998            None,
13999        );
14000        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
14001        assert!(can_cast_types(list_view.data_type(), &target_type));
14002        let cast_result = cast(&list_view, &target_type).unwrap();
14003        let got_list = cast_result.as_list::<i32>();
14004        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
14005            Some(vec![Some(1)]),
14006            Some(vec![Some(1), Some(2)]),
14007        ]);
14008        assert_eq!(got_list, &expected_list);
14009    }
14010
14011    #[test]
14012    fn test_cast_list_view_to_list_empty() {
14013        let values: Vec<Option<Vec<Option<i32>>>> = vec![];
14014        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
14015        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
14016        assert!(can_cast_types(list_view.data_type(), &target_type));
14017        let cast_result = cast(&list_view, &target_type).unwrap();
14018        let got_list = cast_result.as_list::<i32>();
14019        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
14020        assert_eq!(got_list, &expected_list);
14021    }
14022
14023    #[test]
14024    fn test_cast_list_view_to_list_different_inner_type() {
14025        let values = int32_list_values();
14026        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
14027        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int64, true)));
14028        assert!(can_cast_types(list_view.data_type(), &target_type));
14029        let cast_result = cast(&list_view, &target_type).unwrap();
14030        let got_list = cast_result.as_list::<i32>();
14031
14032        let expected_list =
14033            ListArray::from_iter_primitive::<Int64Type, _, _>(values.into_iter().map(|list| {
14034                list.map(|list| {
14035                    list.into_iter()
14036                        .map(|v| v.map(|v| v as i64))
14037                        .collect::<Vec<_>>()
14038                })
14039            }));
14040        assert_eq!(got_list, &expected_list);
14041    }
14042
14043    #[test]
14044    fn test_cast_list_view_to_list_out_of_order_with_nulls() {
14045        let list_view = ListViewArray::new(
14046            Arc::new(Field::new("item", DataType::Int32, true)),
14047            ScalarBuffer::from(vec![0, 6, 3]),
14048            ScalarBuffer::from(vec![3, 3, 3]),
14049            Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
14050            Some(NullBuffer::from(vec![false, true, false])),
14051        );
14052        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
14053        assert!(can_cast_types(list_view.data_type(), &target_type));
14054        let cast_result = cast(&list_view, &target_type).unwrap();
14055        let got_list = cast_result.as_list::<i32>();
14056        let expected_list = ListArray::new(
14057            Arc::new(Field::new("item", DataType::Int32, true)),
14058            OffsetBuffer::from_lengths([3, 3, 3]),
14059            Arc::new(Int32Array::from(vec![1, 2, 3, 7, 8, 9, 4, 5, 6])),
14060            Some(NullBuffer::from(vec![false, true, false])),
14061        );
14062        assert_eq!(got_list, &expected_list);
14063    }
14064
14065    #[test]
14066    fn test_cast_list_view_to_large_list_view() {
14067        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
14068        let target_type =
14069            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
14070        assert!(can_cast_types(list_view.data_type(), &target_type));
14071        let cast_result = cast(&list_view, &target_type).unwrap();
14072        let got = cast_result.as_list_view::<i64>();
14073
14074        let expected =
14075            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
14076        assert_eq!(got, &expected);
14077    }
14078
14079    #[test]
14080    fn test_cast_large_list_view_to_list_view() {
14081        let list_view =
14082            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
14083        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
14084        assert!(can_cast_types(list_view.data_type(), &target_type));
14085        let cast_result = cast(&list_view, &target_type).unwrap();
14086        let got = cast_result.as_list_view::<i32>();
14087
14088        let expected = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
14089        assert_eq!(got, &expected);
14090    }
14091
14092    #[test]
14093    fn test_cast_time32_second_to_int64() {
14094        let array = Time32SecondArray::from(vec![1000, 2000, 3000]);
14095        let array = Arc::new(array) as Arc<dyn Array>;
14096        let to_type = DataType::Int64;
14097        let cast_options = CastOptions::default();
14098
14099        assert!(can_cast_types(array.data_type(), &to_type));
14100
14101        let result = cast_with_options(&array, &to_type, &cast_options);
14102        assert!(
14103            result.is_ok(),
14104            "Failed to cast Time32(Second) to Int64: {:?}",
14105            result.err()
14106        );
14107
14108        let cast_array = result.unwrap();
14109        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
14110
14111        assert_eq!(cast_array.value(0), 1000);
14112        assert_eq!(cast_array.value(1), 2000);
14113        assert_eq!(cast_array.value(2), 3000);
14114    }
14115
14116    #[test]
14117    fn test_cast_time32_millisecond_to_int64() {
14118        let array = Time32MillisecondArray::from(vec![1000, 2000, 3000]);
14119        let array = Arc::new(array) as Arc<dyn Array>;
14120        let to_type = DataType::Int64;
14121        let cast_options = CastOptions::default();
14122
14123        assert!(can_cast_types(array.data_type(), &to_type));
14124
14125        let result = cast_with_options(&array, &to_type, &cast_options);
14126        assert!(
14127            result.is_ok(),
14128            "Failed to cast Time32(Millisecond) to Int64: {:?}",
14129            result.err()
14130        );
14131
14132        let cast_array = result.unwrap();
14133        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
14134
14135        assert_eq!(cast_array.value(0), 1000);
14136        assert_eq!(cast_array.value(1), 2000);
14137        assert_eq!(cast_array.value(2), 3000);
14138    }
14139
14140    #[test]
14141    fn test_cast_time32_millisecond_to_time64_nanosecond() {
14142        let array =
14143            Time32MillisecondArray::from(vec![Some(1_000), Some(2_000), None, Some(43_200_000)]);
14144        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
14145        let c = b.as_primitive::<Time64NanosecondType>();
14146        assert_eq!(c.value(0), 1_000_000_000);
14147        assert_eq!(c.value(1), 2_000_000_000);
14148        assert!(c.is_null(2));
14149        assert_eq!(c.value(3), 43_200_000_000_000);
14150    }
14151
14152    #[test]
14153    fn test_cast_time32_millisecond_to_time64_microsecond() {
14154        let array =
14155            Time32MillisecondArray::from(vec![Some(1_000), Some(2_000), None, Some(43_200_000)]);
14156        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
14157        let c = b.as_primitive::<Time64MicrosecondType>();
14158        assert_eq!(c.value(0), 1_000_000);
14159        assert_eq!(c.value(1), 2_000_000);
14160        assert!(c.is_null(2));
14161        assert_eq!(c.value(3), 43_200_000_000);
14162    }
14163
14164    #[test]
14165    fn test_cast_time32_second_to_time64_nanosecond() {
14166        let array = Time32SecondArray::from(vec![Some(1), Some(60), None, Some(43_200)]);
14167        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
14168        let c = b.as_primitive::<Time64NanosecondType>();
14169        assert_eq!(c.value(0), 1_000_000_000);
14170        assert_eq!(c.value(1), 60_000_000_000);
14171        assert!(c.is_null(2));
14172        assert_eq!(c.value(3), 43_200_000_000_000);
14173    }
14174
14175    #[test]
14176    fn test_cast_time32_second_to_time64_microsecond() {
14177        let array = Time32SecondArray::from(vec![Some(1), Some(60), None, Some(43_200)]);
14178        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
14179        let c = b.as_primitive::<Time64MicrosecondType>();
14180        assert_eq!(c.value(0), 1_000_000);
14181        assert_eq!(c.value(1), 60_000_000);
14182        assert!(c.is_null(2));
14183        assert_eq!(c.value(3), 43_200_000_000);
14184    }
14185
14186    #[test]
14187    fn test_cast_time32_second_to_time32_millisecond_overflow() {
14188        let array = Time32SecondArray::from(vec![i32::MAX]);
14189
14190        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
14191        let c = b.as_primitive::<Time32MillisecondType>();
14192        assert!(c.is_null(0));
14193
14194        let options = CastOptions {
14195            safe: false,
14196            ..Default::default()
14197        };
14198        let err = cast_with_options(&array, &DataType::Time32(TimeUnit::Millisecond), &options)
14199            .unwrap_err();
14200        assert!(err.to_string().contains("Overflow"), "{err}");
14201    }
14202
14203    #[test]
14204    fn test_cast_string_to_time32_second_to_int64() {
14205        // Mimic: select arrow_cast('03:12:44'::time, 'Time32(Second)')::bigint;
14206        // raised in https://github.com/apache/datafusion/issues/19036
14207        let array = StringArray::from(vec!["03:12:44"]);
14208        let array = Arc::new(array) as Arc<dyn Array>;
14209        let cast_options = CastOptions::default();
14210
14211        // 1. Cast String to Time32(Second)
14212        let time32_type = DataType::Time32(TimeUnit::Second);
14213        let time32_array = cast_with_options(&array, &time32_type, &cast_options).unwrap();
14214
14215        // 2. Cast Time32(Second) to Int64
14216        let int64_type = DataType::Int64;
14217        assert!(can_cast_types(time32_array.data_type(), &int64_type));
14218
14219        let result = cast_with_options(&time32_array, &int64_type, &cast_options);
14220
14221        assert!(
14222            result.is_ok(),
14223            "Failed to cast Time32(Second) to Int64: {:?}",
14224            result.err()
14225        );
14226
14227        let cast_array = result.unwrap();
14228        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
14229
14230        // 03:12:44 = 3*3600 + 12*60 + 44 = 10800 + 720 + 44 = 11564
14231        assert_eq!(cast_array.value(0), 11564);
14232    }
14233    #[test]
14234    fn test_string_dicts_to_binary_view() {
14235        let expected = BinaryViewArray::from_iter(vec![
14236            VIEW_TEST_DATA[1],
14237            VIEW_TEST_DATA[0],
14238            None,
14239            VIEW_TEST_DATA[3],
14240            None,
14241            VIEW_TEST_DATA[1],
14242            VIEW_TEST_DATA[4],
14243        ]);
14244
14245        let values_arrays: [ArrayRef; _] = [
14246            Arc::new(StringArray::from_iter(VIEW_TEST_DATA)),
14247            Arc::new(StringViewArray::from_iter(VIEW_TEST_DATA)),
14248            Arc::new(LargeStringArray::from_iter(VIEW_TEST_DATA)),
14249        ];
14250        for values in values_arrays {
14251            let keys =
14252                Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
14253            let string_dict_array = DictionaryArray::<Int8Type>::try_new(keys, values).unwrap();
14254
14255            let casted = cast(&string_dict_array, &DataType::BinaryView).unwrap();
14256            assert_eq!(casted.as_ref(), &expected);
14257        }
14258    }
14259
14260    #[test]
14261    fn test_binary_dicts_to_string_view() {
14262        let expected = StringViewArray::from_iter(vec![
14263            VIEW_TEST_DATA[1],
14264            VIEW_TEST_DATA[0],
14265            None,
14266            VIEW_TEST_DATA[3],
14267            None,
14268            VIEW_TEST_DATA[1],
14269            VIEW_TEST_DATA[4],
14270        ]);
14271
14272        let values_arrays: [ArrayRef; _] = [
14273            Arc::new(BinaryArray::from_iter(VIEW_TEST_DATA)),
14274            Arc::new(BinaryViewArray::from_iter(VIEW_TEST_DATA)),
14275            Arc::new(LargeBinaryArray::from_iter(VIEW_TEST_DATA)),
14276        ];
14277        for values in values_arrays {
14278            let keys =
14279                Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
14280            let string_dict_array = DictionaryArray::<Int8Type>::try_new(keys, values).unwrap();
14281
14282            let casted = cast(&string_dict_array, &DataType::Utf8View).unwrap();
14283            assert_eq!(casted.as_ref(), &expected);
14284        }
14285    }
14286
14287    #[test]
14288    fn test_cast_between_sliced_run_end_encoded() {
14289        let run_ends = Int16Array::from(vec![2, 5, 8]);
14290        let values = StringArray::from(vec!["a", "b", "c"]);
14291
14292        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
14293        let ree_array = ree_array.slice(1, 2);
14294        let array_ref = Arc::new(ree_array) as ArrayRef;
14295
14296        let target_type = DataType::RunEndEncoded(
14297            Arc::new(Field::new("run_ends", DataType::Int64, false)),
14298            Arc::new(Field::new("values", DataType::Utf8, true)),
14299        );
14300        let cast_options = CastOptions {
14301            safe: false,
14302            format_options: FormatOptions::default(),
14303        };
14304
14305        let result = cast_with_options(&array_ref, &target_type, &cast_options).unwrap();
14306        let run_array = result.as_run::<Int64Type>();
14307        let run_array = run_array.downcast::<StringArray>().unwrap();
14308
14309        let expected = vec!["a", "b"];
14310        let actual = run_array.into_iter().flatten().collect::<Vec<_>>();
14311
14312        assert_eq!(expected, actual);
14313    }
14314}