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, 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
351fn cast_integer_to_decimal<
352    T: ArrowPrimitiveType,
353    D: DecimalType + ArrowPrimitiveType<Native = M>,
354    M,
355>(
356    array: &PrimitiveArray<T>,
357    precision: u8,
358    scale: i8,
359    base: M,
360    cast_options: &CastOptions,
361) -> Result<ArrayRef, ArrowError>
362where
363    <T as ArrowPrimitiveType>::Native: AsPrimitive<M>,
364    M: ArrowNativeTypeOp,
365{
366    let scale_factor = base.pow_checked(scale.unsigned_abs() as u32).map_err(|_| {
367        ArrowError::CastError(format!(
368            "Cannot cast to {:?}({}, {}). The scale causes overflow.",
369            D::PREFIX,
370            precision,
371            scale,
372        ))
373    })?;
374
375    let array = if scale < 0 {
376        match cast_options.safe {
377            true => array.unary_opt::<_, D>(|v| {
378                v.as_()
379                    .div_checked(scale_factor)
380                    .ok()
381                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
382            }),
383            false => array.try_unary::<_, D, _>(|v| {
384                v.as_()
385                    .div_checked(scale_factor)
386                    .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|()| v))
387            })?,
388        }
389    } else {
390        match cast_options.safe {
391            true => array.unary_opt::<_, D>(|v| {
392                v.as_()
393                    .mul_checked(scale_factor)
394                    .ok()
395                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
396            }),
397            false => array.try_unary::<_, D, _>(|v| {
398                v.as_()
399                    .mul_checked(scale_factor)
400                    .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|()| v))
401            })?,
402        }
403    };
404
405    Ok(Arc::new(array.with_precision_and_scale(precision, scale)?))
406}
407
408/// Cast the array from interval year month to month day nano
409fn cast_interval_year_month_to_interval_month_day_nano(
410    array: &dyn Array,
411    _cast_options: &CastOptions,
412) -> Result<ArrayRef, ArrowError> {
413    let array = array.as_primitive::<IntervalYearMonthType>();
414
415    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
416        let months = IntervalYearMonthType::to_months(v);
417        IntervalMonthDayNanoType::make_value(months, 0, 0)
418    })))
419}
420
421/// Cast the array from interval day time to month day nano
422fn cast_interval_day_time_to_interval_month_day_nano(
423    array: &dyn Array,
424    _cast_options: &CastOptions,
425) -> Result<ArrayRef, ArrowError> {
426    let array = array.as_primitive::<IntervalDayTimeType>();
427    let mul = 1_000_000;
428
429    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
430        let (days, ms) = IntervalDayTimeType::to_parts(v);
431        IntervalMonthDayNanoType::make_value(0, days, ms as i64 * mul)
432    })))
433}
434
435/// Cast the array from interval to duration
436fn cast_month_day_nano_to_duration<D: ArrowTemporalType<Native = i64>>(
437    array: &dyn Array,
438    cast_options: &CastOptions,
439) -> Result<ArrayRef, ArrowError> {
440    let array = array.as_primitive::<IntervalMonthDayNanoType>();
441    let scale = match D::DATA_TYPE {
442        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
443        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
444        DataType::Duration(TimeUnit::Microsecond) => 1_000,
445        DataType::Duration(TimeUnit::Nanosecond) => 1,
446        _ => unreachable!(),
447    };
448
449    if cast_options.safe {
450        let iter = array.iter().map(|v| {
451            v.and_then(|v| (v.days == 0 && v.months == 0).then_some(v.nanoseconds / scale))
452        });
453        Ok(Arc::new(unsafe {
454            PrimitiveArray::<D>::from_trusted_len_iter(iter)
455        }))
456    } else {
457        let vec = array
458            .iter()
459            .map(|v| {
460                v.map(|v| match v.days == 0 && v.months == 0 {
461                    true => Ok((v.nanoseconds) / scale),
462                    _ => Err(ArrowError::ComputeError(
463                        "Cannot convert interval containing non-zero months or days to duration"
464                            .to_string(),
465                    )),
466                })
467                .transpose()
468            })
469            .collect::<Result<Vec<_>, _>>()?;
470        Ok(Arc::new(unsafe {
471            PrimitiveArray::<D>::from_trusted_len_iter(vec.iter())
472        }))
473    }
474}
475
476/// Cast the array from duration and interval
477fn cast_duration_to_interval<D: ArrowTemporalType<Native = i64>>(
478    array: &dyn Array,
479    cast_options: &CastOptions,
480) -> Result<ArrayRef, ArrowError> {
481    let array = array
482        .as_any()
483        .downcast_ref::<PrimitiveArray<D>>()
484        .ok_or_else(|| {
485            ArrowError::ComputeError(
486                "Internal Error: Cannot cast duration to DurationArray of expected type"
487                    .to_string(),
488            )
489        })?;
490
491    let scale = match array.data_type() {
492        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
493        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
494        DataType::Duration(TimeUnit::Microsecond) => 1_000,
495        DataType::Duration(TimeUnit::Nanosecond) => 1,
496        _ => unreachable!(),
497    };
498
499    if cast_options.safe {
500        let iter = array.iter().map(|v| {
501            v.and_then(|v| {
502                v.checked_mul(scale)
503                    .map(|v| IntervalMonthDayNano::new(0, 0, v))
504            })
505        });
506        Ok(Arc::new(unsafe {
507            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(iter)
508        }))
509    } else {
510        let vec = array
511            .iter()
512            .map(|v| {
513                v.map(|v| {
514                    if let Ok(v) = v.mul_checked(scale) {
515                        Ok(IntervalMonthDayNano::new(0, 0, v))
516                    } else {
517                        Err(ArrowError::ComputeError(format!(
518                            "Cannot cast to {:?}. Overflowing on {:?}",
519                            IntervalMonthDayNanoType::DATA_TYPE,
520                            v
521                        )))
522                    }
523                })
524                .transpose()
525            })
526            .collect::<Result<Vec<_>, _>>()?;
527        Ok(Arc::new(unsafe {
528            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(vec.iter())
529        }))
530    }
531}
532
533/// Cast the primitive array using [`PrimitiveArray::reinterpret_cast`]
534fn cast_reinterpret_arrays<I: ArrowPrimitiveType, O: ArrowPrimitiveType<Native = I::Native>>(
535    array: &dyn Array,
536) -> Result<ArrayRef, ArrowError> {
537    Ok(Arc::new(array.as_primitive::<I>().reinterpret_cast::<O>()))
538}
539
540fn make_timestamp_array(
541    array: &PrimitiveArray<Int64Type>,
542    unit: TimeUnit,
543    tz: Option<Arc<str>>,
544) -> ArrayRef {
545    match unit {
546        TimeUnit::Second => Arc::new(
547            array
548                .reinterpret_cast::<TimestampSecondType>()
549                .with_timezone_opt(tz),
550        ),
551        TimeUnit::Millisecond => Arc::new(
552            array
553                .reinterpret_cast::<TimestampMillisecondType>()
554                .with_timezone_opt(tz),
555        ),
556        TimeUnit::Microsecond => Arc::new(
557            array
558                .reinterpret_cast::<TimestampMicrosecondType>()
559                .with_timezone_opt(tz),
560        ),
561        TimeUnit::Nanosecond => Arc::new(
562            array
563                .reinterpret_cast::<TimestampNanosecondType>()
564                .with_timezone_opt(tz),
565        ),
566    }
567}
568
569fn make_duration_array(array: &PrimitiveArray<Int64Type>, unit: TimeUnit) -> ArrayRef {
570    match unit {
571        TimeUnit::Second => Arc::new(array.reinterpret_cast::<DurationSecondType>()),
572        TimeUnit::Millisecond => Arc::new(array.reinterpret_cast::<DurationMillisecondType>()),
573        TimeUnit::Microsecond => Arc::new(array.reinterpret_cast::<DurationMicrosecondType>()),
574        TimeUnit::Nanosecond => Arc::new(array.reinterpret_cast::<DurationNanosecondType>()),
575    }
576}
577
578fn as_time_res_with_timezone<T: ArrowPrimitiveType>(
579    v: i64,
580    tz: Option<Tz>,
581) -> Result<NaiveTime, ArrowError> {
582    let time = match tz {
583        Some(tz) => as_datetime_with_timezone::<T>(v, tz).map(|d| d.time()),
584        None => as_datetime::<T>(v).map(|d| d.time()),
585    };
586
587    time.ok_or_else(|| {
588        ArrowError::CastError(format!(
589            "Failed to create naive time with {} {}",
590            std::any::type_name::<T>(),
591            v
592        ))
593    })
594}
595
596fn timestamp_to_date32<T: ArrowTimestampType>(
597    array: &PrimitiveArray<T>,
598) -> Result<ArrayRef, ArrowError> {
599    let err = |x: i64| {
600        ArrowError::CastError(format!(
601            "Cannot convert {} {x} to datetime",
602            std::any::type_name::<T>()
603        ))
604    };
605
606    let array: Date32Array = match array.timezone() {
607        Some(tz) => {
608            let tz: Tz = tz.parse()?;
609            array.try_unary(|x| {
610                as_datetime_with_timezone::<T>(x, tz)
611                    .ok_or_else(|| err(x))
612                    .map(|d| Date32Type::from_naive_date(d.date_naive()))
613            })?
614        }
615        None => array.try_unary(|x| {
616            as_datetime::<T>(x)
617                .ok_or_else(|| err(x))
618                .map(|d| Date32Type::from_naive_date(d.date()))
619        })?,
620    };
621    Ok(Arc::new(array))
622}
623
624/// Try to cast `array` to `to_type` if possible.
625///
626/// Returns a new Array with type `to_type` if possible.
627///
628/// Accepts [`CastOptions`] to specify cast behavior. See also [`cast()`].
629///
630/// # Behavior
631/// * `Boolean` to `Utf8`: `true` => '1', `false` => `0`
632/// * `Utf8` to `Boolean`: `true`, `yes`, `on`, `1` => `true`, `false`, `no`, `off`, `0` => `false`,
633///   short variants are accepted, other strings return null or error
634/// * `Utf8` to Numeric: strings that can't be parsed to numbers return null, float strings
635///   in integer casts return null
636/// * Numeric to `Boolean`: 0 returns `false`, any other value returns `true`
637/// * `List` to `List`: the underlying data type is cast
638/// * `List` to `FixedSizeList`: the underlying data type is cast. If safe is true and a list element
639///   has the wrong length it will be replaced with NULL, otherwise an error will be returned
640/// * Primitive to `List`: a list array with 1 value per slot is created
641/// * `Date32` and `Date64`: precision lost when going to higher interval
642/// * `Time32` and `Time64`: precision lost when going to higher interval
643/// * `Timestamp` and `Date{32|64}`: precision lost when going to higher interval
644/// * Temporal to/from backing Primitive: zero-copy with data type change
645/// * `Float16/Float32/Float64` to `Decimal(precision, scale)` rounds to the `scale` decimals
646///   (i.e. casting `6.4999` to `Decimal(10, 1)` becomes `6.5`).
647/// * `Decimal` to `Float16/Float32/Float64` is lossy and values outside the representable
648///   range become `INFINITY` or `-INFINITY` without error.
649///
650/// Unsupported Casts (check with `can_cast_types` before calling):
651/// * To or from `StructArray`
652/// * `List` to `Primitive`
653/// * `Interval` and `Duration`
654///
655/// # Durations and Intervals
656///
657/// Casting integer types directly to interval types such as
658/// [`IntervalMonthDayNano`] is not supported because the meaning of the integer
659/// is ambiguous. For example, the integer  could represent either nanoseconds
660/// or months.
661///
662/// To cast an integer type to an interval type, first convert to a Duration
663/// type, and then cast that to the desired interval type.
664///
665/// For example, to convert an `Int64` representing nanoseconds to an
666/// `IntervalMonthDayNano` you would first convert the `Int64` to a
667/// `DurationNanoseconds`, and then cast that to `IntervalMonthDayNano`.
668///
669/// # Timestamps and Timezones
670///
671/// Timestamps are stored with an optional timezone in Arrow.
672///
673/// ## Casting timestamps to a timestamp without timezone / UTC
674/// ```
675/// # use arrow_array::Int64Array;
676/// # use arrow_array::types::TimestampSecondType;
677/// # use arrow_cast::{cast, display};
678/// # use arrow_array::cast::AsArray;
679/// # use arrow_schema::{DataType, TimeUnit};
680/// // can use "UTC" if chrono-tz feature is enabled, here use offset based timezone
681/// let data_type = DataType::Timestamp(TimeUnit::Second, None);
682/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
683/// let b = cast(&a, &data_type).unwrap();
684/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
685/// assert_eq!(2_000_000_000, b.value(1)); // values are the same as the type has no timezone
686/// // use display to show them (note has no trailing Z)
687/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
688/// ```
689///
690/// ## Casting timestamps to a timestamp with timezone
691///
692/// Similarly to the previous example, if you cast numeric values to a timestamp
693/// with timezone, the cast kernel will not change the underlying values
694/// but display and other functions will interpret them as being in the provided timezone.
695///
696/// ```
697/// # use arrow_array::Int64Array;
698/// # use arrow_array::types::TimestampSecondType;
699/// # use arrow_cast::{cast, display};
700/// # use arrow_array::cast::AsArray;
701/// # use arrow_schema::{DataType, TimeUnit};
702/// // can use "Americas/New_York" if chrono-tz feature is enabled, here use offset based timezone
703/// let data_type = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
704/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
705/// let b = cast(&a, &data_type).unwrap();
706/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
707/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
708/// // displayed in the target timezone (note the offset -05:00)
709/// assert_eq!("2033-05-17T22:33:20-05:00", display::array_value_to_string(&b, 1).unwrap());
710/// ```
711/// # Casting timestamps without timezone to timestamps with timezone
712///
713/// When casting from a timestamp without timezone to a timestamp with
714/// timezone, the cast kernel interprets the timestamp values as being in
715/// the destination timezone and then adjusts the underlying value to UTC as required
716///
717/// However, note that when casting from a timestamp with timezone BACK to a
718/// timestamp without timezone the cast kernel does not adjust the values.
719///
720/// Thus round trip casting a timestamp without timezone to a timestamp with
721/// timezone and back to a timestamp without timezone results in different
722/// values than the starting values.
723///
724/// ```
725/// # use arrow_array::Int64Array;
726/// # use arrow_array::types::{TimestampSecondType};
727/// # use arrow_cast::{cast, display};
728/// # use arrow_array::cast::AsArray;
729/// # use arrow_schema::{DataType, TimeUnit};
730/// let data_type  = DataType::Timestamp(TimeUnit::Second, None);
731/// let data_type_tz = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
732/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
733/// let b = cast(&a, &data_type).unwrap(); // cast to timestamp without timezone
734/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
735/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
736/// // displayed without a timezone (note lack of offset or Z)
737/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
738///
739/// // Convert timestamps without a timezone to timestamps with a timezone
740/// let c = cast(&b, &data_type_tz).unwrap();
741/// let c = c.as_primitive::<TimestampSecondType>(); // downcast to result type
742/// assert_eq!(2_000_018_000, c.value(1)); // value has been adjusted by offset
743/// // displayed with the target timezone offset (-05:00)
744/// assert_eq!("2033-05-18T03:33:20-05:00", display::array_value_to_string(&c, 1).unwrap());
745///
746/// // Convert from timestamp with timezone back to timestamp without timezone
747/// let d = cast(&c, &data_type).unwrap();
748/// let d = d.as_primitive::<TimestampSecondType>(); // downcast to result type
749/// assert_eq!(2_000_018_000, d.value(1)); // value has not been adjusted
750/// // NOTE: the timestamp is adjusted (08:33:20 instead of 03:33:20 as in previous example)
751/// assert_eq!("2033-05-18T08:33:20", display::array_value_to_string(&d, 1).unwrap());
752/// ```
753pub fn cast_with_options(
754    array: &dyn Array,
755    to_type: &DataType,
756    cast_options: &CastOptions,
757) -> Result<ArrayRef, ArrowError> {
758    use DataType::*;
759    let from_type = array.data_type();
760    // clone array if types are the same
761    if from_type == to_type {
762        return Ok(make_array(array.to_data()));
763    }
764    match (from_type, to_type) {
765        (Null, _) => Ok(new_null_array(to_type, array.len())),
766        (RunEndEncoded(index_type, _), _) => match index_type.data_type() {
767            Int16 => run_end_encoded_cast::<Int16Type>(array, to_type, cast_options),
768            Int32 => run_end_encoded_cast::<Int32Type>(array, to_type, cast_options),
769            Int64 => run_end_encoded_cast::<Int64Type>(array, to_type, cast_options),
770            _ => Err(ArrowError::CastError(format!(
771                "Casting from run end encoded type {from_type:?} to {to_type:?} not supported",
772            ))),
773        },
774        (_, RunEndEncoded(index_type, value_type)) => {
775            let array_ref = make_array(array.to_data());
776            match index_type.data_type() {
777                Int16 => cast_to_run_end_encoded::<Int16Type>(
778                    &array_ref,
779                    value_type.data_type(),
780                    cast_options,
781                ),
782                Int32 => cast_to_run_end_encoded::<Int32Type>(
783                    &array_ref,
784                    value_type.data_type(),
785                    cast_options,
786                ),
787                Int64 => cast_to_run_end_encoded::<Int64Type>(
788                    &array_ref,
789                    value_type.data_type(),
790                    cast_options,
791                ),
792                _ => Err(ArrowError::CastError(format!(
793                    "Casting from type {from_type:?} to run end encoded type {to_type:?} not supported",
794                ))),
795            }
796        }
797        (Union(_, _), _) => union_extract_by_type(
798            array.as_any().downcast_ref::<UnionArray>().unwrap(),
799            to_type,
800            cast_options,
801        ),
802        (_, Union(_, _)) => Err(ArrowError::CastError(format!(
803            "Casting from {from_type} to {to_type} not supported"
804        ))),
805        (Dictionary(index_type, _), _) => match **index_type {
806            Int8 => dictionary_cast::<Int8Type>(array, to_type, cast_options),
807            Int16 => dictionary_cast::<Int16Type>(array, to_type, cast_options),
808            Int32 => dictionary_cast::<Int32Type>(array, to_type, cast_options),
809            Int64 => dictionary_cast::<Int64Type>(array, to_type, cast_options),
810            UInt8 => dictionary_cast::<UInt8Type>(array, to_type, cast_options),
811            UInt16 => dictionary_cast::<UInt16Type>(array, to_type, cast_options),
812            UInt32 => dictionary_cast::<UInt32Type>(array, to_type, cast_options),
813            UInt64 => dictionary_cast::<UInt64Type>(array, to_type, cast_options),
814            _ => Err(ArrowError::CastError(format!(
815                "Casting from dictionary type {from_type} to {to_type} not supported",
816            ))),
817        },
818        (_, Dictionary(index_type, value_type)) => match **index_type {
819            Int8 => cast_to_dictionary::<Int8Type>(array, value_type, cast_options),
820            Int16 => cast_to_dictionary::<Int16Type>(array, value_type, cast_options),
821            Int32 => cast_to_dictionary::<Int32Type>(array, value_type, cast_options),
822            Int64 => cast_to_dictionary::<Int64Type>(array, value_type, cast_options),
823            UInt8 => cast_to_dictionary::<UInt8Type>(array, value_type, cast_options),
824            UInt16 => cast_to_dictionary::<UInt16Type>(array, value_type, cast_options),
825            UInt32 => cast_to_dictionary::<UInt32Type>(array, value_type, cast_options),
826            UInt64 => cast_to_dictionary::<UInt64Type>(array, value_type, cast_options),
827            _ => Err(ArrowError::CastError(format!(
828                "Casting from type {from_type} to dictionary type {to_type} not supported",
829            ))),
830        },
831        // Casting between lists of same types (cast inner values)
832        (List(_), List(to)) => cast_list_values::<i32>(array, to, cast_options),
833        (LargeList(_), LargeList(to)) => cast_list_values::<i64>(array, to, cast_options),
834        (FixedSizeList(_, size_from), FixedSizeList(list_to, size_to)) => {
835            if size_from != size_to {
836                return Err(ArrowError::CastError(
837                    "cannot cast fixed-size-list to fixed-size-list with different size".into(),
838                ));
839            }
840            let array = array.as_fixed_size_list();
841            let values = cast_with_options(array.values(), list_to.data_type(), cast_options)?;
842            Ok(Arc::new(FixedSizeListArray::try_new(
843                list_to.clone(),
844                *size_from,
845                values,
846                array.nulls().cloned(),
847            )?))
848        }
849        (ListView(_), ListView(to)) => cast_list_view_values::<i32>(array, to, cast_options),
850        (LargeListView(_), LargeListView(to)) => {
851            cast_list_view_values::<i64>(array, to, cast_options)
852        }
853        // Casting between different types of lists
854        // List
855        (List(_), LargeList(list_to)) => cast_list::<i32, i64>(array, list_to, cast_options),
856        (List(_), FixedSizeList(field, size)) => {
857            cast_list_to_fixed_size_list::<i32>(array, field, *size, cast_options)
858        }
859        (List(_), ListView(list_to)) => {
860            cast_list_to_list_view::<i32, i32>(array, list_to, cast_options)
861        }
862        (List(_), LargeListView(list_to)) => {
863            cast_list_to_list_view::<i32, i64>(array, list_to, cast_options)
864        }
865        // LargeList
866        (LargeList(_), List(list_to)) => cast_list::<i64, i32>(array, list_to, cast_options),
867        (LargeList(_), FixedSizeList(field, size)) => {
868            cast_list_to_fixed_size_list::<i64>(array, field, *size, cast_options)
869        }
870        (LargeList(_), ListView(list_to)) => {
871            cast_list_to_list_view::<i64, i32>(array, list_to, cast_options)
872        }
873        (LargeList(_), LargeListView(list_to)) => {
874            cast_list_to_list_view::<i64, i64>(array, list_to, cast_options)
875        }
876        // ListView
877        (ListView(_), List(list_to)) => {
878            cast_list_view_to_list::<i32, Int32Type>(array, list_to, cast_options)
879        }
880        (ListView(_), LargeList(list_to)) => {
881            cast_list_view_to_list::<i32, Int64Type>(array, list_to, cast_options)
882        }
883        (ListView(_), LargeListView(list_to)) => {
884            cast_list_view::<i32, i64>(array, list_to, cast_options)
885        }
886        (ListView(_), FixedSizeList(field, size)) => {
887            cast_list_view_to_fixed_size_list::<i32>(array, field, *size, cast_options)
888        }
889        // LargeListView
890        (LargeListView(_), LargeList(list_to)) => {
891            cast_list_view_to_list::<i64, Int64Type>(array, list_to, cast_options)
892        }
893        (LargeListView(_), List(list_to)) => {
894            cast_list_view_to_list::<i64, Int32Type>(array, list_to, cast_options)
895        }
896        (LargeListView(_), ListView(list_to)) => {
897            cast_list_view::<i64, i32>(array, list_to, cast_options)
898        }
899        (LargeListView(_), FixedSizeList(field, size)) => {
900            cast_list_view_to_fixed_size_list::<i64>(array, field, *size, cast_options)
901        }
902        // FixedSizeList
903        (FixedSizeList(_, _), List(list_to)) => {
904            cast_fixed_size_list_to_list::<i32>(array, list_to, cast_options)
905        }
906        (FixedSizeList(_, _), LargeList(list_to)) => {
907            cast_fixed_size_list_to_list::<i64>(array, list_to, cast_options)
908        }
909        (FixedSizeList(_, _), ListView(list_to)) => {
910            cast_fixed_size_list_to_list_view::<i32>(array, list_to, cast_options)
911        }
912        (FixedSizeList(_, _), LargeListView(list_to)) => {
913            cast_fixed_size_list_to_list_view::<i64>(array, list_to, cast_options)
914        }
915        // List to/from other types
916        (FixedSizeList(_, size), _) if *size == 1 => {
917            cast_single_element_fixed_size_list_to_values(array, to_type, cast_options)
918        }
919        // NOTE: we could support FSL to string here too but might be confusing
920        //       since behaviour for size 1 would be different (see arm above)
921        (List(_) | LargeList(_) | ListView(_) | LargeListView(_), _) => match to_type {
922            Utf8 => value_to_string::<i32>(array, cast_options),
923            LargeUtf8 => value_to_string::<i64>(array, cast_options),
924            Utf8View => value_to_string_view(array, cast_options),
925            dt => Err(ArrowError::CastError(format!(
926                "Cannot cast LIST to non-list data type {dt}"
927            ))),
928        },
929        (_, List(to)) => cast_values_to_list::<i32>(array, to, cast_options),
930        (_, LargeList(to)) => cast_values_to_list::<i64>(array, to, cast_options),
931        (_, ListView(to)) => cast_values_to_list_view::<i32>(array, to, cast_options),
932        (_, LargeListView(to)) => cast_values_to_list_view::<i64>(array, to, cast_options),
933        (_, FixedSizeList(to, size)) if *size == 1 => {
934            let values = cast_with_options(array, to.data_type(), cast_options)?;
935            let list = FixedSizeListArray::try_new(to.clone(), 1, values, None)?;
936            Ok(Arc::new(list))
937        }
938        // Map
939        (Map(_, ordered1), Map(_, ordered2)) if ordered1 == ordered2 => {
940            cast_map_values(array.as_map(), to_type, cast_options, ordered1.to_owned())
941        }
942        // Decimal to decimal, same width
943        (Decimal32(p1, s1), Decimal32(p2, s2)) => {
944            cast_decimal_to_decimal_same_type::<Decimal32Type>(
945                array.as_primitive(),
946                *p1,
947                *s1,
948                *p2,
949                *s2,
950                cast_options,
951            )
952        }
953        (Decimal64(p1, s1), Decimal64(p2, s2)) => {
954            cast_decimal_to_decimal_same_type::<Decimal64Type>(
955                array.as_primitive(),
956                *p1,
957                *s1,
958                *p2,
959                *s2,
960                cast_options,
961            )
962        }
963        (Decimal128(p1, s1), Decimal128(p2, s2)) => {
964            cast_decimal_to_decimal_same_type::<Decimal128Type>(
965                array.as_primitive(),
966                *p1,
967                *s1,
968                *p2,
969                *s2,
970                cast_options,
971            )
972        }
973        (Decimal256(p1, s1), Decimal256(p2, s2)) => {
974            cast_decimal_to_decimal_same_type::<Decimal256Type>(
975                array.as_primitive(),
976                *p1,
977                *s1,
978                *p2,
979                *s2,
980                cast_options,
981            )
982        }
983        // Decimal to decimal, different width
984        (Decimal32(p1, s1), Decimal64(p2, s2)) => {
985            cast_decimal_to_decimal::<Decimal32Type, Decimal64Type>(
986                array.as_primitive(),
987                *p1,
988                *s1,
989                *p2,
990                *s2,
991                cast_options,
992            )
993        }
994        (Decimal32(p1, s1), Decimal128(p2, s2)) => {
995            cast_decimal_to_decimal::<Decimal32Type, Decimal128Type>(
996                array.as_primitive(),
997                *p1,
998                *s1,
999                *p2,
1000                *s2,
1001                cast_options,
1002            )
1003        }
1004        (Decimal32(p1, s1), Decimal256(p2, s2)) => {
1005            cast_decimal_to_decimal::<Decimal32Type, Decimal256Type>(
1006                array.as_primitive(),
1007                *p1,
1008                *s1,
1009                *p2,
1010                *s2,
1011                cast_options,
1012            )
1013        }
1014        (Decimal64(p1, s1), Decimal32(p2, s2)) => {
1015            cast_decimal_to_decimal::<Decimal64Type, Decimal32Type>(
1016                array.as_primitive(),
1017                *p1,
1018                *s1,
1019                *p2,
1020                *s2,
1021                cast_options,
1022            )
1023        }
1024        (Decimal64(p1, s1), Decimal128(p2, s2)) => {
1025            cast_decimal_to_decimal::<Decimal64Type, Decimal128Type>(
1026                array.as_primitive(),
1027                *p1,
1028                *s1,
1029                *p2,
1030                *s2,
1031                cast_options,
1032            )
1033        }
1034        (Decimal64(p1, s1), Decimal256(p2, s2)) => {
1035            cast_decimal_to_decimal::<Decimal64Type, Decimal256Type>(
1036                array.as_primitive(),
1037                *p1,
1038                *s1,
1039                *p2,
1040                *s2,
1041                cast_options,
1042            )
1043        }
1044        (Decimal128(p1, s1), Decimal32(p2, s2)) => {
1045            cast_decimal_to_decimal::<Decimal128Type, Decimal32Type>(
1046                array.as_primitive(),
1047                *p1,
1048                *s1,
1049                *p2,
1050                *s2,
1051                cast_options,
1052            )
1053        }
1054        (Decimal128(p1, s1), Decimal64(p2, s2)) => {
1055            cast_decimal_to_decimal::<Decimal128Type, Decimal64Type>(
1056                array.as_primitive(),
1057                *p1,
1058                *s1,
1059                *p2,
1060                *s2,
1061                cast_options,
1062            )
1063        }
1064        (Decimal128(p1, s1), Decimal256(p2, s2)) => {
1065            cast_decimal_to_decimal::<Decimal128Type, Decimal256Type>(
1066                array.as_primitive(),
1067                *p1,
1068                *s1,
1069                *p2,
1070                *s2,
1071                cast_options,
1072            )
1073        }
1074        (Decimal256(p1, s1), Decimal32(p2, s2)) => {
1075            cast_decimal_to_decimal::<Decimal256Type, Decimal32Type>(
1076                array.as_primitive(),
1077                *p1,
1078                *s1,
1079                *p2,
1080                *s2,
1081                cast_options,
1082            )
1083        }
1084        (Decimal256(p1, s1), Decimal64(p2, s2)) => {
1085            cast_decimal_to_decimal::<Decimal256Type, Decimal64Type>(
1086                array.as_primitive(),
1087                *p1,
1088                *s1,
1089                *p2,
1090                *s2,
1091                cast_options,
1092            )
1093        }
1094        (Decimal256(p1, s1), Decimal128(p2, s2)) => {
1095            cast_decimal_to_decimal::<Decimal256Type, Decimal128Type>(
1096                array.as_primitive(),
1097                *p1,
1098                *s1,
1099                *p2,
1100                *s2,
1101                cast_options,
1102            )
1103        }
1104        // Decimal to non-decimal
1105        (Decimal32(_, scale), _) if !to_type.is_temporal() => {
1106            cast_from_decimal::<Decimal32Type, _>(
1107                array,
1108                10_i32,
1109                scale,
1110                from_type,
1111                to_type,
1112                |x: i32| x as f64,
1113                cast_options,
1114            )
1115        }
1116        (Decimal64(_, scale), _) if !to_type.is_temporal() => {
1117            cast_from_decimal::<Decimal64Type, _>(
1118                array,
1119                10_i64,
1120                scale,
1121                from_type,
1122                to_type,
1123                |x: i64| x as f64,
1124                cast_options,
1125            )
1126        }
1127        (Decimal128(_, scale), _) if !to_type.is_temporal() => {
1128            cast_from_decimal::<Decimal128Type, _>(
1129                array,
1130                10_i128,
1131                scale,
1132                from_type,
1133                to_type,
1134                |x: i128| x as f64,
1135                cast_options,
1136            )
1137        }
1138        (Decimal256(_, scale), _) if !to_type.is_temporal() => {
1139            cast_from_decimal::<Decimal256Type, _>(
1140                array,
1141                i256::from_i128(10_i128),
1142                scale,
1143                from_type,
1144                to_type,
1145                |x: i256| x.to_f64().expect("All i256 values fit in f64"),
1146                cast_options,
1147            )
1148        }
1149        // Non-decimal to decimal
1150        (_, Decimal32(precision, scale)) if !from_type.is_temporal() => {
1151            cast_to_decimal::<Decimal32Type, _>(
1152                array,
1153                10_i32,
1154                precision,
1155                scale,
1156                from_type,
1157                to_type,
1158                cast_options,
1159            )
1160        }
1161        (_, Decimal64(precision, scale)) if !from_type.is_temporal() => {
1162            cast_to_decimal::<Decimal64Type, _>(
1163                array,
1164                10_i64,
1165                precision,
1166                scale,
1167                from_type,
1168                to_type,
1169                cast_options,
1170            )
1171        }
1172        (_, Decimal128(precision, scale)) if !from_type.is_temporal() => {
1173            cast_to_decimal::<Decimal128Type, _>(
1174                array,
1175                10_i128,
1176                precision,
1177                scale,
1178                from_type,
1179                to_type,
1180                cast_options,
1181            )
1182        }
1183        (_, Decimal256(precision, scale)) if !from_type.is_temporal() => {
1184            cast_to_decimal::<Decimal256Type, _>(
1185                array,
1186                i256::from_i128(10_i128),
1187                precision,
1188                scale,
1189                from_type,
1190                to_type,
1191                cast_options,
1192            )
1193        }
1194        (Struct(from_fields), Struct(to_fields)) => cast_struct_to_struct(
1195            array.as_struct(),
1196            from_fields.clone(),
1197            to_fields.clone(),
1198            cast_options,
1199        ),
1200        (Struct(_), _) => Err(ArrowError::CastError(format!(
1201            "Casting from {from_type} to {to_type} not supported"
1202        ))),
1203        (_, Struct(_)) => Err(ArrowError::CastError(format!(
1204            "Casting from {from_type} to {to_type} not supported"
1205        ))),
1206        (_, Boolean) => match from_type {
1207            UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
1208            UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
1209            UInt32 => cast_numeric_to_bool::<UInt32Type>(array),
1210            UInt64 => cast_numeric_to_bool::<UInt64Type>(array),
1211            Int8 => cast_numeric_to_bool::<Int8Type>(array),
1212            Int16 => cast_numeric_to_bool::<Int16Type>(array),
1213            Int32 => cast_numeric_to_bool::<Int32Type>(array),
1214            Int64 => cast_numeric_to_bool::<Int64Type>(array),
1215            Float16 => cast_numeric_to_bool::<Float16Type>(array),
1216            Float32 => cast_numeric_to_bool::<Float32Type>(array),
1217            Float64 => cast_numeric_to_bool::<Float64Type>(array),
1218            Utf8View => cast_utf8view_to_boolean(array, cast_options),
1219            Utf8 => cast_utf8_to_boolean::<i32>(array, cast_options),
1220            LargeUtf8 => cast_utf8_to_boolean::<i64>(array, cast_options),
1221            _ => Err(ArrowError::CastError(format!(
1222                "Casting from {from_type} to {to_type} not supported",
1223            ))),
1224        },
1225        (Boolean, _) => match to_type {
1226            UInt8 => cast_bool_to_numeric::<UInt8Type>(array, cast_options),
1227            UInt16 => cast_bool_to_numeric::<UInt16Type>(array, cast_options),
1228            UInt32 => cast_bool_to_numeric::<UInt32Type>(array, cast_options),
1229            UInt64 => cast_bool_to_numeric::<UInt64Type>(array, cast_options),
1230            Int8 => cast_bool_to_numeric::<Int8Type>(array, cast_options),
1231            Int16 => cast_bool_to_numeric::<Int16Type>(array, cast_options),
1232            Int32 => cast_bool_to_numeric::<Int32Type>(array, cast_options),
1233            Int64 => cast_bool_to_numeric::<Int64Type>(array, cast_options),
1234            Float16 => cast_bool_to_numeric::<Float16Type>(array, cast_options),
1235            Float32 => cast_bool_to_numeric::<Float32Type>(array, cast_options),
1236            Float64 => cast_bool_to_numeric::<Float64Type>(array, cast_options),
1237            Utf8View => value_to_string_view(array, cast_options),
1238            Utf8 => value_to_string::<i32>(array, cast_options),
1239            LargeUtf8 => value_to_string::<i64>(array, cast_options),
1240            _ => Err(ArrowError::CastError(format!(
1241                "Casting from {from_type} to {to_type} not supported",
1242            ))),
1243        },
1244        (Utf8, _) => match to_type {
1245            UInt8 => parse_string::<UInt8Type, i32>(array, cast_options),
1246            UInt16 => parse_string::<UInt16Type, i32>(array, cast_options),
1247            UInt32 => parse_string::<UInt32Type, i32>(array, cast_options),
1248            UInt64 => parse_string::<UInt64Type, i32>(array, cast_options),
1249            Int8 => parse_string::<Int8Type, i32>(array, cast_options),
1250            Int16 => parse_string::<Int16Type, i32>(array, cast_options),
1251            Int32 => parse_string::<Int32Type, i32>(array, cast_options),
1252            Int64 => parse_string::<Int64Type, i32>(array, cast_options),
1253            Float16 => parse_string::<Float16Type, i32>(array, cast_options),
1254            Float32 => parse_string::<Float32Type, i32>(array, cast_options),
1255            Float64 => parse_string::<Float64Type, i32>(array, cast_options),
1256            Date32 => parse_string::<Date32Type, i32>(array, cast_options),
1257            Date64 => parse_string::<Date64Type, i32>(array, cast_options),
1258            Binary => Ok(Arc::new(BinaryArray::from(
1259                array.as_string::<i32>().clone(),
1260            ))),
1261            LargeBinary => {
1262                let binary = BinaryArray::from(array.as_string::<i32>().clone());
1263                cast_byte_container::<BinaryType, LargeBinaryType>(&binary)
1264            }
1265            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i32>()))),
1266            BinaryView => Ok(Arc::new(
1267                StringViewArray::from(array.as_string::<i32>()).to_binary_view(),
1268            )),
1269            LargeUtf8 => cast_byte_container::<Utf8Type, LargeUtf8Type>(array),
1270            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i32>(array, cast_options),
1271            Time32(TimeUnit::Millisecond) => {
1272                parse_string::<Time32MillisecondType, i32>(array, cast_options)
1273            }
1274            Time64(TimeUnit::Microsecond) => {
1275                parse_string::<Time64MicrosecondType, i32>(array, cast_options)
1276            }
1277            Time64(TimeUnit::Nanosecond) => {
1278                parse_string::<Time64NanosecondType, i32>(array, cast_options)
1279            }
1280            Timestamp(TimeUnit::Second, to_tz) => {
1281                cast_string_to_timestamp::<i32, TimestampSecondType>(array, to_tz, cast_options)
1282            }
1283            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1284                i32,
1285                TimestampMillisecondType,
1286            >(array, to_tz, cast_options),
1287            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1288                i32,
1289                TimestampMicrosecondType,
1290            >(array, to_tz, cast_options),
1291            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1292                cast_string_to_timestamp::<i32, TimestampNanosecondType>(array, to_tz, cast_options)
1293            }
1294            Interval(IntervalUnit::YearMonth) => {
1295                cast_string_to_year_month_interval::<i32>(array, cast_options)
1296            }
1297            Interval(IntervalUnit::DayTime) => {
1298                cast_string_to_day_time_interval::<i32>(array, cast_options)
1299            }
1300            Interval(IntervalUnit::MonthDayNano) => {
1301                cast_string_to_month_day_nano_interval::<i32>(array, cast_options)
1302            }
1303            _ => Err(ArrowError::CastError(format!(
1304                "Casting from {from_type} to {to_type} not supported",
1305            ))),
1306        },
1307        (Utf8View, _) => match to_type {
1308            UInt8 => parse_string_view::<UInt8Type>(array, cast_options),
1309            UInt16 => parse_string_view::<UInt16Type>(array, cast_options),
1310            UInt32 => parse_string_view::<UInt32Type>(array, cast_options),
1311            UInt64 => parse_string_view::<UInt64Type>(array, cast_options),
1312            Int8 => parse_string_view::<Int8Type>(array, cast_options),
1313            Int16 => parse_string_view::<Int16Type>(array, cast_options),
1314            Int32 => parse_string_view::<Int32Type>(array, cast_options),
1315            Int64 => parse_string_view::<Int64Type>(array, cast_options),
1316            Float16 => parse_string_view::<Float16Type>(array, cast_options),
1317            Float32 => parse_string_view::<Float32Type>(array, cast_options),
1318            Float64 => parse_string_view::<Float64Type>(array, cast_options),
1319            Date32 => parse_string_view::<Date32Type>(array, cast_options),
1320            Date64 => parse_string_view::<Date64Type>(array, cast_options),
1321            Binary => cast_view_to_byte::<StringViewType, GenericBinaryType<i32>>(array),
1322            LargeBinary => cast_view_to_byte::<StringViewType, GenericBinaryType<i64>>(array),
1323            BinaryView => Ok(Arc::new(array.as_string_view().clone().to_binary_view())),
1324            Utf8 => cast_view_to_byte::<StringViewType, GenericStringType<i32>>(array),
1325            LargeUtf8 => cast_view_to_byte::<StringViewType, GenericStringType<i64>>(array),
1326            Time32(TimeUnit::Second) => parse_string_view::<Time32SecondType>(array, cast_options),
1327            Time32(TimeUnit::Millisecond) => {
1328                parse_string_view::<Time32MillisecondType>(array, cast_options)
1329            }
1330            Time64(TimeUnit::Microsecond) => {
1331                parse_string_view::<Time64MicrosecondType>(array, cast_options)
1332            }
1333            Time64(TimeUnit::Nanosecond) => {
1334                parse_string_view::<Time64NanosecondType>(array, cast_options)
1335            }
1336            Timestamp(TimeUnit::Second, to_tz) => {
1337                cast_view_to_timestamp::<TimestampSecondType>(array, to_tz, cast_options)
1338            }
1339            Timestamp(TimeUnit::Millisecond, to_tz) => {
1340                cast_view_to_timestamp::<TimestampMillisecondType>(array, to_tz, cast_options)
1341            }
1342            Timestamp(TimeUnit::Microsecond, to_tz) => {
1343                cast_view_to_timestamp::<TimestampMicrosecondType>(array, to_tz, cast_options)
1344            }
1345            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1346                cast_view_to_timestamp::<TimestampNanosecondType>(array, to_tz, cast_options)
1347            }
1348            Interval(IntervalUnit::YearMonth) => {
1349                cast_view_to_year_month_interval(array, cast_options)
1350            }
1351            Interval(IntervalUnit::DayTime) => cast_view_to_day_time_interval(array, cast_options),
1352            Interval(IntervalUnit::MonthDayNano) => {
1353                cast_view_to_month_day_nano_interval(array, cast_options)
1354            }
1355            _ => Err(ArrowError::CastError(format!(
1356                "Casting from {from_type} to {to_type} not supported",
1357            ))),
1358        },
1359        (LargeUtf8, _) => match to_type {
1360            UInt8 => parse_string::<UInt8Type, i64>(array, cast_options),
1361            UInt16 => parse_string::<UInt16Type, i64>(array, cast_options),
1362            UInt32 => parse_string::<UInt32Type, i64>(array, cast_options),
1363            UInt64 => parse_string::<UInt64Type, i64>(array, cast_options),
1364            Int8 => parse_string::<Int8Type, i64>(array, cast_options),
1365            Int16 => parse_string::<Int16Type, i64>(array, cast_options),
1366            Int32 => parse_string::<Int32Type, i64>(array, cast_options),
1367            Int64 => parse_string::<Int64Type, i64>(array, cast_options),
1368            Float16 => parse_string::<Float16Type, i64>(array, cast_options),
1369            Float32 => parse_string::<Float32Type, i64>(array, cast_options),
1370            Float64 => parse_string::<Float64Type, i64>(array, cast_options),
1371            Date32 => parse_string::<Date32Type, i64>(array, cast_options),
1372            Date64 => parse_string::<Date64Type, i64>(array, cast_options),
1373            Utf8 => cast_byte_container::<LargeUtf8Type, Utf8Type>(array),
1374            Binary => {
1375                let large_binary = LargeBinaryArray::from(array.as_string::<i64>().clone());
1376                cast_byte_container::<LargeBinaryType, BinaryType>(&large_binary)
1377            }
1378            LargeBinary => Ok(Arc::new(LargeBinaryArray::from(
1379                array.as_string::<i64>().clone(),
1380            ))),
1381            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i64>()))),
1382            BinaryView => Ok(Arc::new(BinaryViewArray::from(
1383                array
1384                    .as_string::<i64>()
1385                    .into_iter()
1386                    .map(|x| x.map(|x| x.as_bytes()))
1387                    .collect::<Vec<_>>(),
1388            ))),
1389            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i64>(array, cast_options),
1390            Time32(TimeUnit::Millisecond) => {
1391                parse_string::<Time32MillisecondType, i64>(array, cast_options)
1392            }
1393            Time64(TimeUnit::Microsecond) => {
1394                parse_string::<Time64MicrosecondType, i64>(array, cast_options)
1395            }
1396            Time64(TimeUnit::Nanosecond) => {
1397                parse_string::<Time64NanosecondType, i64>(array, cast_options)
1398            }
1399            Timestamp(TimeUnit::Second, to_tz) => {
1400                cast_string_to_timestamp::<i64, TimestampSecondType>(array, to_tz, cast_options)
1401            }
1402            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1403                i64,
1404                TimestampMillisecondType,
1405            >(array, to_tz, cast_options),
1406            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1407                i64,
1408                TimestampMicrosecondType,
1409            >(array, to_tz, cast_options),
1410            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1411                cast_string_to_timestamp::<i64, TimestampNanosecondType>(array, to_tz, cast_options)
1412            }
1413            Interval(IntervalUnit::YearMonth) => {
1414                cast_string_to_year_month_interval::<i64>(array, cast_options)
1415            }
1416            Interval(IntervalUnit::DayTime) => {
1417                cast_string_to_day_time_interval::<i64>(array, cast_options)
1418            }
1419            Interval(IntervalUnit::MonthDayNano) => {
1420                cast_string_to_month_day_nano_interval::<i64>(array, cast_options)
1421            }
1422            _ => Err(ArrowError::CastError(format!(
1423                "Casting from {from_type} to {to_type} not supported",
1424            ))),
1425        },
1426        (Binary, _) => match to_type {
1427            Utf8 => cast_binary_to_string::<i32>(array, cast_options),
1428            LargeUtf8 => {
1429                let array = cast_binary_to_string::<i32>(array, cast_options)?;
1430                cast_byte_container::<Utf8Type, LargeUtf8Type>(array.as_ref())
1431            }
1432            LargeBinary => cast_byte_container::<BinaryType, LargeBinaryType>(array),
1433            FixedSizeBinary(size) => {
1434                cast_binary_to_fixed_size_binary::<i32>(array, *size, cast_options)
1435            }
1436            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i32>()))),
1437            Utf8View => Ok(Arc::new(StringViewArray::from(
1438                cast_binary_to_string::<i32>(array, cast_options)?.as_string::<i32>(),
1439            ))),
1440            _ => Err(ArrowError::CastError(format!(
1441                "Casting from {from_type} to {to_type} not supported",
1442            ))),
1443        },
1444        (LargeBinary, _) => match to_type {
1445            Utf8 => {
1446                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1447                cast_byte_container::<LargeUtf8Type, Utf8Type>(array.as_ref())
1448            }
1449            LargeUtf8 => cast_binary_to_string::<i64>(array, cast_options),
1450            Binary => cast_byte_container::<LargeBinaryType, BinaryType>(array),
1451            FixedSizeBinary(size) => {
1452                cast_binary_to_fixed_size_binary::<i64>(array, *size, cast_options)
1453            }
1454            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i64>()))),
1455            Utf8View => {
1456                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1457                Ok(Arc::new(StringViewArray::from(array.as_string::<i64>())))
1458            }
1459            _ => Err(ArrowError::CastError(format!(
1460                "Casting from {from_type} to {to_type} not supported",
1461            ))),
1462        },
1463        (FixedSizeBinary(size), _) => match to_type {
1464            Binary => cast_fixed_size_binary_to_binary::<i32>(array, *size),
1465            LargeBinary => cast_fixed_size_binary_to_binary::<i64>(array, *size),
1466            BinaryView => cast_fixed_size_binary_to_binary_view(array, *size),
1467            _ => Err(ArrowError::CastError(format!(
1468                "Casting from {from_type} to {to_type} not supported",
1469            ))),
1470        },
1471        (BinaryView, Binary) => cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array),
1472        (BinaryView, LargeBinary) => {
1473            cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)
1474        }
1475        (BinaryView, Utf8) => {
1476            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array)?;
1477            cast_binary_to_string::<i32>(&binary_arr, cast_options)
1478        }
1479        (BinaryView, LargeUtf8) => {
1480            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)?;
1481            cast_binary_to_string::<i64>(&binary_arr, cast_options)
1482        }
1483        (BinaryView, Utf8View) => cast_binary_view_to_string_view(array, cast_options),
1484        (BinaryView, _) => Err(ArrowError::CastError(format!(
1485            "Casting from {from_type} to {to_type} not supported",
1486        ))),
1487        (from_type, Utf8View) if from_type.is_primitive() => {
1488            value_to_string_view(array, cast_options)
1489        }
1490        (from_type, LargeUtf8) if from_type.is_primitive() => {
1491            value_to_string::<i64>(array, cast_options)
1492        }
1493        (from_type, Utf8) if from_type.is_primitive() => {
1494            value_to_string::<i32>(array, cast_options)
1495        }
1496        (from_type, Binary) if from_type.is_integer() => match from_type {
1497            UInt8 => cast_numeric_to_binary::<UInt8Type, i32>(array),
1498            UInt16 => cast_numeric_to_binary::<UInt16Type, i32>(array),
1499            UInt32 => cast_numeric_to_binary::<UInt32Type, i32>(array),
1500            UInt64 => cast_numeric_to_binary::<UInt64Type, i32>(array),
1501            Int8 => cast_numeric_to_binary::<Int8Type, i32>(array),
1502            Int16 => cast_numeric_to_binary::<Int16Type, i32>(array),
1503            Int32 => cast_numeric_to_binary::<Int32Type, i32>(array),
1504            Int64 => cast_numeric_to_binary::<Int64Type, i32>(array),
1505            _ => unreachable!(),
1506        },
1507        (from_type, LargeBinary) if from_type.is_integer() => match from_type {
1508            UInt8 => cast_numeric_to_binary::<UInt8Type, i64>(array),
1509            UInt16 => cast_numeric_to_binary::<UInt16Type, i64>(array),
1510            UInt32 => cast_numeric_to_binary::<UInt32Type, i64>(array),
1511            UInt64 => cast_numeric_to_binary::<UInt64Type, i64>(array),
1512            Int8 => cast_numeric_to_binary::<Int8Type, i64>(array),
1513            Int16 => cast_numeric_to_binary::<Int16Type, i64>(array),
1514            Int32 => cast_numeric_to_binary::<Int32Type, i64>(array),
1515            Int64 => cast_numeric_to_binary::<Int64Type, i64>(array),
1516            _ => unreachable!(),
1517        },
1518        // start numeric casts
1519        (UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array, cast_options),
1520        (UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array, cast_options),
1521        (UInt8, UInt64) => cast_numeric_arrays::<UInt8Type, UInt64Type>(array, cast_options),
1522        (UInt8, Int8) => cast_numeric_arrays::<UInt8Type, Int8Type>(array, cast_options),
1523        (UInt8, Int16) => cast_numeric_arrays::<UInt8Type, Int16Type>(array, cast_options),
1524        (UInt8, Int32) => cast_numeric_arrays::<UInt8Type, Int32Type>(array, cast_options),
1525        (UInt8, Int64) => cast_numeric_arrays::<UInt8Type, Int64Type>(array, cast_options),
1526        (UInt8, Float16) => cast_numeric_arrays::<UInt8Type, Float16Type>(array, cast_options),
1527        (UInt8, Float32) => cast_numeric_arrays::<UInt8Type, Float32Type>(array, cast_options),
1528        (UInt8, Float64) => cast_numeric_arrays::<UInt8Type, Float64Type>(array, cast_options),
1529
1530        (UInt16, UInt8) => cast_numeric_arrays::<UInt16Type, UInt8Type>(array, cast_options),
1531        (UInt16, UInt32) => cast_numeric_arrays::<UInt16Type, UInt32Type>(array, cast_options),
1532        (UInt16, UInt64) => cast_numeric_arrays::<UInt16Type, UInt64Type>(array, cast_options),
1533        (UInt16, Int8) => cast_numeric_arrays::<UInt16Type, Int8Type>(array, cast_options),
1534        (UInt16, Int16) => cast_numeric_arrays::<UInt16Type, Int16Type>(array, cast_options),
1535        (UInt16, Int32) => cast_numeric_arrays::<UInt16Type, Int32Type>(array, cast_options),
1536        (UInt16, Int64) => cast_numeric_arrays::<UInt16Type, Int64Type>(array, cast_options),
1537        (UInt16, Float16) => cast_numeric_arrays::<UInt16Type, Float16Type>(array, cast_options),
1538        (UInt16, Float32) => cast_numeric_arrays::<UInt16Type, Float32Type>(array, cast_options),
1539        (UInt16, Float64) => cast_numeric_arrays::<UInt16Type, Float64Type>(array, cast_options),
1540
1541        (UInt32, UInt8) => cast_numeric_arrays::<UInt32Type, UInt8Type>(array, cast_options),
1542        (UInt32, UInt16) => cast_numeric_arrays::<UInt32Type, UInt16Type>(array, cast_options),
1543        (UInt32, UInt64) => cast_numeric_arrays::<UInt32Type, UInt64Type>(array, cast_options),
1544        (UInt32, Int8) => cast_numeric_arrays::<UInt32Type, Int8Type>(array, cast_options),
1545        (UInt32, Int16) => cast_numeric_arrays::<UInt32Type, Int16Type>(array, cast_options),
1546        (UInt32, Int32) => cast_numeric_arrays::<UInt32Type, Int32Type>(array, cast_options),
1547        (UInt32, Int64) => cast_numeric_arrays::<UInt32Type, Int64Type>(array, cast_options),
1548        (UInt32, Float16) => cast_numeric_arrays::<UInt32Type, Float16Type>(array, cast_options),
1549        (UInt32, Float32) => cast_numeric_arrays::<UInt32Type, Float32Type>(array, cast_options),
1550        (UInt32, Float64) => cast_numeric_arrays::<UInt32Type, Float64Type>(array, cast_options),
1551
1552        (UInt64, UInt8) => cast_numeric_arrays::<UInt64Type, UInt8Type>(array, cast_options),
1553        (UInt64, UInt16) => cast_numeric_arrays::<UInt64Type, UInt16Type>(array, cast_options),
1554        (UInt64, UInt32) => cast_numeric_arrays::<UInt64Type, UInt32Type>(array, cast_options),
1555        (UInt64, Int8) => cast_numeric_arrays::<UInt64Type, Int8Type>(array, cast_options),
1556        (UInt64, Int16) => cast_numeric_arrays::<UInt64Type, Int16Type>(array, cast_options),
1557        (UInt64, Int32) => cast_numeric_arrays::<UInt64Type, Int32Type>(array, cast_options),
1558        (UInt64, Int64) => cast_numeric_arrays::<UInt64Type, Int64Type>(array, cast_options),
1559        (UInt64, Float16) => cast_numeric_arrays::<UInt64Type, Float16Type>(array, cast_options),
1560        (UInt64, Float32) => cast_numeric_arrays::<UInt64Type, Float32Type>(array, cast_options),
1561        (UInt64, Float64) => cast_numeric_arrays::<UInt64Type, Float64Type>(array, cast_options),
1562
1563        (Int8, UInt8) => cast_numeric_arrays::<Int8Type, UInt8Type>(array, cast_options),
1564        (Int8, UInt16) => cast_numeric_arrays::<Int8Type, UInt16Type>(array, cast_options),
1565        (Int8, UInt32) => cast_numeric_arrays::<Int8Type, UInt32Type>(array, cast_options),
1566        (Int8, UInt64) => cast_numeric_arrays::<Int8Type, UInt64Type>(array, cast_options),
1567        (Int8, Int16) => cast_numeric_arrays::<Int8Type, Int16Type>(array, cast_options),
1568        (Int8, Int32) => cast_numeric_arrays::<Int8Type, Int32Type>(array, cast_options),
1569        (Int8, Int64) => cast_numeric_arrays::<Int8Type, Int64Type>(array, cast_options),
1570        (Int8, Float16) => cast_numeric_arrays::<Int8Type, Float16Type>(array, cast_options),
1571        (Int8, Float32) => cast_numeric_arrays::<Int8Type, Float32Type>(array, cast_options),
1572        (Int8, Float64) => cast_numeric_arrays::<Int8Type, Float64Type>(array, cast_options),
1573
1574        (Int16, UInt8) => cast_numeric_arrays::<Int16Type, UInt8Type>(array, cast_options),
1575        (Int16, UInt16) => cast_numeric_arrays::<Int16Type, UInt16Type>(array, cast_options),
1576        (Int16, UInt32) => cast_numeric_arrays::<Int16Type, UInt32Type>(array, cast_options),
1577        (Int16, UInt64) => cast_numeric_arrays::<Int16Type, UInt64Type>(array, cast_options),
1578        (Int16, Int8) => cast_numeric_arrays::<Int16Type, Int8Type>(array, cast_options),
1579        (Int16, Int32) => cast_numeric_arrays::<Int16Type, Int32Type>(array, cast_options),
1580        (Int16, Int64) => cast_numeric_arrays::<Int16Type, Int64Type>(array, cast_options),
1581        (Int16, Float16) => cast_numeric_arrays::<Int16Type, Float16Type>(array, cast_options),
1582        (Int16, Float32) => cast_numeric_arrays::<Int16Type, Float32Type>(array, cast_options),
1583        (Int16, Float64) => cast_numeric_arrays::<Int16Type, Float64Type>(array, cast_options),
1584
1585        (Int32, UInt8) => cast_numeric_arrays::<Int32Type, UInt8Type>(array, cast_options),
1586        (Int32, UInt16) => cast_numeric_arrays::<Int32Type, UInt16Type>(array, cast_options),
1587        (Int32, UInt32) => cast_numeric_arrays::<Int32Type, UInt32Type>(array, cast_options),
1588        (Int32, UInt64) => cast_numeric_arrays::<Int32Type, UInt64Type>(array, cast_options),
1589        (Int32, Int8) => cast_numeric_arrays::<Int32Type, Int8Type>(array, cast_options),
1590        (Int32, Int16) => cast_numeric_arrays::<Int32Type, Int16Type>(array, cast_options),
1591        (Int32, Int64) => cast_numeric_arrays::<Int32Type, Int64Type>(array, cast_options),
1592        (Int32, Float16) => cast_numeric_arrays::<Int32Type, Float16Type>(array, cast_options),
1593        (Int32, Float32) => cast_numeric_arrays::<Int32Type, Float32Type>(array, cast_options),
1594        (Int32, Float64) => cast_numeric_arrays::<Int32Type, Float64Type>(array, cast_options),
1595
1596        (Int64, UInt8) => cast_numeric_arrays::<Int64Type, UInt8Type>(array, cast_options),
1597        (Int64, UInt16) => cast_numeric_arrays::<Int64Type, UInt16Type>(array, cast_options),
1598        (Int64, UInt32) => cast_numeric_arrays::<Int64Type, UInt32Type>(array, cast_options),
1599        (Int64, UInt64) => cast_numeric_arrays::<Int64Type, UInt64Type>(array, cast_options),
1600        (Int64, Int8) => cast_numeric_arrays::<Int64Type, Int8Type>(array, cast_options),
1601        (Int64, Int16) => cast_numeric_arrays::<Int64Type, Int16Type>(array, cast_options),
1602        (Int64, Int32) => cast_numeric_arrays::<Int64Type, Int32Type>(array, cast_options),
1603        (Int64, Float16) => cast_numeric_arrays::<Int64Type, Float16Type>(array, cast_options),
1604        (Int64, Float32) => cast_numeric_arrays::<Int64Type, Float32Type>(array, cast_options),
1605        (Int64, Float64) => cast_numeric_arrays::<Int64Type, Float64Type>(array, cast_options),
1606
1607        (Float16, UInt8) => cast_numeric_arrays::<Float16Type, UInt8Type>(array, cast_options),
1608        (Float16, UInt16) => cast_numeric_arrays::<Float16Type, UInt16Type>(array, cast_options),
1609        (Float16, UInt32) => cast_numeric_arrays::<Float16Type, UInt32Type>(array, cast_options),
1610        (Float16, UInt64) => cast_numeric_arrays::<Float16Type, UInt64Type>(array, cast_options),
1611        (Float16, Int8) => cast_numeric_arrays::<Float16Type, Int8Type>(array, cast_options),
1612        (Float16, Int16) => cast_numeric_arrays::<Float16Type, Int16Type>(array, cast_options),
1613        (Float16, Int32) => cast_numeric_arrays::<Float16Type, Int32Type>(array, cast_options),
1614        (Float16, Int64) => cast_numeric_arrays::<Float16Type, Int64Type>(array, cast_options),
1615        (Float16, Float32) => cast_numeric_arrays::<Float16Type, Float32Type>(array, cast_options),
1616        (Float16, Float64) => cast_numeric_arrays::<Float16Type, Float64Type>(array, cast_options),
1617
1618        (Float32, UInt8) => cast_numeric_arrays::<Float32Type, UInt8Type>(array, cast_options),
1619        (Float32, UInt16) => cast_numeric_arrays::<Float32Type, UInt16Type>(array, cast_options),
1620        (Float32, UInt32) => cast_numeric_arrays::<Float32Type, UInt32Type>(array, cast_options),
1621        (Float32, UInt64) => cast_numeric_arrays::<Float32Type, UInt64Type>(array, cast_options),
1622        (Float32, Int8) => cast_numeric_arrays::<Float32Type, Int8Type>(array, cast_options),
1623        (Float32, Int16) => cast_numeric_arrays::<Float32Type, Int16Type>(array, cast_options),
1624        (Float32, Int32) => cast_numeric_arrays::<Float32Type, Int32Type>(array, cast_options),
1625        (Float32, Int64) => cast_numeric_arrays::<Float32Type, Int64Type>(array, cast_options),
1626        (Float32, Float16) => cast_numeric_arrays::<Float32Type, Float16Type>(array, cast_options),
1627        (Float32, Float64) => cast_numeric_arrays::<Float32Type, Float64Type>(array, cast_options),
1628
1629        (Float64, UInt8) => cast_numeric_arrays::<Float64Type, UInt8Type>(array, cast_options),
1630        (Float64, UInt16) => cast_numeric_arrays::<Float64Type, UInt16Type>(array, cast_options),
1631        (Float64, UInt32) => cast_numeric_arrays::<Float64Type, UInt32Type>(array, cast_options),
1632        (Float64, UInt64) => cast_numeric_arrays::<Float64Type, UInt64Type>(array, cast_options),
1633        (Float64, Int8) => cast_numeric_arrays::<Float64Type, Int8Type>(array, cast_options),
1634        (Float64, Int16) => cast_numeric_arrays::<Float64Type, Int16Type>(array, cast_options),
1635        (Float64, Int32) => cast_numeric_arrays::<Float64Type, Int32Type>(array, cast_options),
1636        (Float64, Int64) => cast_numeric_arrays::<Float64Type, Int64Type>(array, cast_options),
1637        (Float64, Float16) => cast_numeric_arrays::<Float64Type, Float16Type>(array, cast_options),
1638        (Float64, Float32) => cast_numeric_arrays::<Float64Type, Float32Type>(array, cast_options),
1639        // end numeric casts
1640
1641        // temporal casts
1642        (Int32, Date32) => cast_reinterpret_arrays::<Int32Type, Date32Type>(array),
1643        (Int32, Date64) => cast_with_options(
1644            &cast_with_options(array, &Date32, cast_options)?,
1645            &Date64,
1646            cast_options,
1647        ),
1648        (Int32, Time32(TimeUnit::Second)) => {
1649            cast_reinterpret_arrays::<Int32Type, Time32SecondType>(array)
1650        }
1651        (Int32, Time32(TimeUnit::Millisecond)) => {
1652            cast_reinterpret_arrays::<Int32Type, Time32MillisecondType>(array)
1653        }
1654        // No support for microsecond/nanosecond with i32
1655        (Date32, Int32) => cast_reinterpret_arrays::<Date32Type, Int32Type>(array),
1656        (Date32, Int64) => cast_with_options(
1657            &cast_with_options(array, &Int32, cast_options)?,
1658            &Int64,
1659            cast_options,
1660        ),
1661        (Time32(TimeUnit::Second), Int32) => {
1662            cast_reinterpret_arrays::<Time32SecondType, Int32Type>(array)
1663        }
1664        (Time32(TimeUnit::Millisecond), Int32) => {
1665            cast_reinterpret_arrays::<Time32MillisecondType, Int32Type>(array)
1666        }
1667        (Time32(TimeUnit::Second), Int64) => cast_with_options(
1668            &cast_with_options(array, &Int32, cast_options)?,
1669            &Int64,
1670            cast_options,
1671        ),
1672        (Time32(TimeUnit::Millisecond), Int64) => cast_with_options(
1673            &cast_with_options(array, &Int32, cast_options)?,
1674            &Int64,
1675            cast_options,
1676        ),
1677        (Int64, Date64) => cast_reinterpret_arrays::<Int64Type, Date64Type>(array),
1678        (Int64, Date32) => cast_with_options(
1679            &cast_with_options(array, &Int32, cast_options)?,
1680            &Date32,
1681            cast_options,
1682        ),
1683        // No support for second/milliseconds with i64
1684        (Int64, Time64(TimeUnit::Microsecond)) => {
1685            cast_reinterpret_arrays::<Int64Type, Time64MicrosecondType>(array)
1686        }
1687        (Int64, Time64(TimeUnit::Nanosecond)) => {
1688            cast_reinterpret_arrays::<Int64Type, Time64NanosecondType>(array)
1689        }
1690
1691        (Date64, Int64) => cast_reinterpret_arrays::<Date64Type, Int64Type>(array),
1692        (Date64, Int32) => cast_with_options(
1693            &cast_with_options(array, &Int64, cast_options)?,
1694            &Int32,
1695            cast_options,
1696        ),
1697        (Time64(TimeUnit::Microsecond), Int64) => {
1698            cast_reinterpret_arrays::<Time64MicrosecondType, Int64Type>(array)
1699        }
1700        (Time64(TimeUnit::Nanosecond), Int64) => {
1701            cast_reinterpret_arrays::<Time64NanosecondType, Int64Type>(array)
1702        }
1703        (Date32, Date64) => Ok(Arc::new(
1704            array
1705                .as_primitive::<Date32Type>()
1706                .unary::<_, Date64Type>(|x| x as i64 * MILLISECONDS_IN_DAY),
1707        )),
1708        (Date64, Date32) => {
1709            let array = array.as_primitive::<Date64Type>();
1710            let result = if cast_options.safe {
1711                array.unary_opt::<_, Date32Type>(|x| i32::try_from(x / MILLISECONDS_IN_DAY).ok())
1712            } else {
1713                array.try_unary::<_, Date32Type, _>(|x| {
1714                    i32::try_from(x / MILLISECONDS_IN_DAY).map_err(|_| {
1715                        ArrowError::CastError(format!(
1716                            "Cannot cast Date64 value {x} to Date32 without overflow"
1717                        ))
1718                    })
1719                })?
1720            };
1721            Ok(Arc::new(result))
1722        }
1723
1724        (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => {
1725            let array = array.as_primitive::<Time32SecondType>();
1726            let result = if cast_options.safe {
1727                array.unary_opt::<_, Time32MillisecondType>(|x| x.checked_mul(MILLISECONDS as i32))
1728            } else {
1729                array.try_unary::<_, Time32MillisecondType, _>(|x| {
1730                    x.mul_checked(MILLISECONDS as i32)
1731                })?
1732            };
1733            Ok(Arc::new(result))
1734        }
1735        (Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1736            array
1737                .as_primitive::<Time32SecondType>()
1738                .unary::<_, Time64MicrosecondType>(|x| x as i64 * MICROSECONDS),
1739        )),
1740        (Time32(TimeUnit::Second), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1741            array
1742                .as_primitive::<Time32SecondType>()
1743                .unary::<_, Time64NanosecondType>(|x| x as i64 * NANOSECONDS),
1744        )),
1745
1746        (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1747            array
1748                .as_primitive::<Time32MillisecondType>()
1749                .unary::<_, Time32SecondType>(|x| x / MILLISECONDS as i32),
1750        )),
1751        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1752            array
1753                .as_primitive::<Time32MillisecondType>()
1754                .unary::<_, Time64MicrosecondType>(|x| x as i64 * (MICROSECONDS / MILLISECONDS)),
1755        )),
1756        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1757            array
1758                .as_primitive::<Time32MillisecondType>()
1759                .unary::<_, Time64NanosecondType>(|x| x as i64 * (NANOSECONDS / MILLISECONDS)),
1760        )),
1761
1762        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1763            array
1764                .as_primitive::<Time64MicrosecondType>()
1765                .unary::<_, Time32SecondType>(|x| (x / MICROSECONDS) as i32),
1766        )),
1767        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1768            array
1769                .as_primitive::<Time64MicrosecondType>()
1770                .unary::<_, Time32MillisecondType>(|x| (x / (MICROSECONDS / MILLISECONDS)) as i32),
1771        )),
1772        (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1773            array
1774                .as_primitive::<Time64MicrosecondType>()
1775                .unary::<_, Time64NanosecondType>(|x| x * (NANOSECONDS / MICROSECONDS)),
1776        )),
1777
1778        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1779            array
1780                .as_primitive::<Time64NanosecondType>()
1781                .unary::<_, Time32SecondType>(|x| (x / NANOSECONDS) as i32),
1782        )),
1783        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1784            array
1785                .as_primitive::<Time64NanosecondType>()
1786                .unary::<_, Time32MillisecondType>(|x| (x / (NANOSECONDS / MILLISECONDS)) as i32),
1787        )),
1788        (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1789            array
1790                .as_primitive::<Time64NanosecondType>()
1791                .unary::<_, Time64MicrosecondType>(|x| x / (NANOSECONDS / MICROSECONDS)),
1792        )),
1793
1794        // Timestamp to integer/floating/decimals
1795        (Timestamp(TimeUnit::Second, _), _) if to_type.is_numeric() => {
1796            let array = cast_reinterpret_arrays::<TimestampSecondType, Int64Type>(array)?;
1797            cast_with_options(&array, to_type, cast_options)
1798        }
1799        (Timestamp(TimeUnit::Millisecond, _), _) if to_type.is_numeric() => {
1800            let array = cast_reinterpret_arrays::<TimestampMillisecondType, Int64Type>(array)?;
1801            cast_with_options(&array, to_type, cast_options)
1802        }
1803        (Timestamp(TimeUnit::Microsecond, _), _) if to_type.is_numeric() => {
1804            let array = cast_reinterpret_arrays::<TimestampMicrosecondType, Int64Type>(array)?;
1805            cast_with_options(&array, to_type, cast_options)
1806        }
1807        (Timestamp(TimeUnit::Nanosecond, _), _) if to_type.is_numeric() => {
1808            let array = cast_reinterpret_arrays::<TimestampNanosecondType, Int64Type>(array)?;
1809            cast_with_options(&array, to_type, cast_options)
1810        }
1811
1812        (_, Timestamp(unit, tz)) if from_type.is_numeric() => {
1813            let array = cast_with_options(array, &Int64, cast_options)?;
1814            Ok(make_timestamp_array(
1815                array.as_primitive(),
1816                *unit,
1817                tz.clone(),
1818            ))
1819        }
1820
1821        (Timestamp(from_unit, from_tz), Timestamp(to_unit, to_tz)) => {
1822            let array = cast_with_options(array, &Int64, cast_options)?;
1823            let time_array = array.as_primitive::<Int64Type>();
1824            let from_size = time_unit_multiple(from_unit);
1825            let to_size = time_unit_multiple(to_unit);
1826            // we either divide or multiply, depending on size of each unit
1827            // units are never the same when the types are the same
1828            let converted = match from_size.cmp(&to_size) {
1829                Ordering::Greater => {
1830                    let divisor = from_size / to_size;
1831                    time_array.unary::<_, Int64Type>(|o| o / divisor)
1832                }
1833                Ordering::Equal => time_array.clone(),
1834                Ordering::Less => {
1835                    let mul = to_size / from_size;
1836                    if cast_options.safe {
1837                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1838                    } else {
1839                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1840                    }
1841                }
1842            };
1843            // Normalize timezone
1844            let adjusted = match (from_tz, to_tz) {
1845                // Only this case needs to be adjusted because we're casting from
1846                // unknown time offset to some time offset, we want the time to be
1847                // unchanged.
1848                //
1849                // i.e. Timestamp('2001-01-01T00:00', None) -> Timestamp('2001-01-01T00:00', '+0700')
1850                (None, Some(to_tz)) => {
1851                    let to_tz: Tz = to_tz.parse()?;
1852                    match to_unit {
1853                        TimeUnit::Second => adjust_timestamp_to_timezone::<TimestampSecondType>(
1854                            converted,
1855                            &to_tz,
1856                            cast_options,
1857                        )?,
1858                        TimeUnit::Millisecond => adjust_timestamp_to_timezone::<
1859                            TimestampMillisecondType,
1860                        >(
1861                            converted, &to_tz, cast_options
1862                        )?,
1863                        TimeUnit::Microsecond => adjust_timestamp_to_timezone::<
1864                            TimestampMicrosecondType,
1865                        >(
1866                            converted, &to_tz, cast_options
1867                        )?,
1868                        TimeUnit::Nanosecond => adjust_timestamp_to_timezone::<
1869                            TimestampNanosecondType,
1870                        >(
1871                            converted, &to_tz, cast_options
1872                        )?,
1873                    }
1874                }
1875                _ => converted,
1876            };
1877            Ok(make_timestamp_array(&adjusted, *to_unit, to_tz.clone()))
1878        }
1879        (Timestamp(TimeUnit::Microsecond, _), Date32) => {
1880            timestamp_to_date32(array.as_primitive::<TimestampMicrosecondType>())
1881        }
1882        (Timestamp(TimeUnit::Millisecond, _), Date32) => {
1883            timestamp_to_date32(array.as_primitive::<TimestampMillisecondType>())
1884        }
1885        (Timestamp(TimeUnit::Second, _), Date32) => {
1886            timestamp_to_date32(array.as_primitive::<TimestampSecondType>())
1887        }
1888        (Timestamp(TimeUnit::Nanosecond, _), Date32) => {
1889            timestamp_to_date32(array.as_primitive::<TimestampNanosecondType>())
1890        }
1891        (Timestamp(TimeUnit::Second, _), Date64) => Ok(Arc::new(match cast_options.safe {
1892            true => {
1893                // change error to None
1894                array
1895                    .as_primitive::<TimestampSecondType>()
1896                    .unary_opt::<_, Date64Type>(|x| x.checked_mul(MILLISECONDS))
1897            }
1898            false => array
1899                .as_primitive::<TimestampSecondType>()
1900                .try_unary::<_, Date64Type, _>(|x| x.mul_checked(MILLISECONDS))?,
1901        })),
1902        (Timestamp(TimeUnit::Millisecond, _), Date64) => {
1903            cast_reinterpret_arrays::<TimestampMillisecondType, Date64Type>(array)
1904        }
1905        (Timestamp(TimeUnit::Microsecond, _), Date64) => Ok(Arc::new(
1906            array
1907                .as_primitive::<TimestampMicrosecondType>()
1908                .unary::<_, Date64Type>(|x| x / (MICROSECONDS / MILLISECONDS)),
1909        )),
1910        (Timestamp(TimeUnit::Nanosecond, _), Date64) => Ok(Arc::new(
1911            array
1912                .as_primitive::<TimestampNanosecondType>()
1913                .unary::<_, Date64Type>(|x| x / (NANOSECONDS / MILLISECONDS)),
1914        )),
1915        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Microsecond)) => {
1916            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1917            Ok(Arc::new(
1918                array
1919                    .as_primitive::<TimestampSecondType>()
1920                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1921                        Ok(time_to_time64us(as_time_res_with_timezone::<
1922                            TimestampSecondType,
1923                        >(x, tz)?))
1924                    })?,
1925            ))
1926        }
1927        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Nanosecond)) => {
1928            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1929            Ok(Arc::new(
1930                array
1931                    .as_primitive::<TimestampSecondType>()
1932                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1933                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1934                            TimestampSecondType,
1935                        >(x, tz)?))
1936                    })?,
1937            ))
1938        }
1939        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Microsecond)) => {
1940            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1941            Ok(Arc::new(
1942                array
1943                    .as_primitive::<TimestampMillisecondType>()
1944                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1945                        Ok(time_to_time64us(as_time_res_with_timezone::<
1946                            TimestampMillisecondType,
1947                        >(x, tz)?))
1948                    })?,
1949            ))
1950        }
1951        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Nanosecond)) => {
1952            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1953            Ok(Arc::new(
1954                array
1955                    .as_primitive::<TimestampMillisecondType>()
1956                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1957                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1958                            TimestampMillisecondType,
1959                        >(x, tz)?))
1960                    })?,
1961            ))
1962        }
1963        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Microsecond)) => {
1964            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1965            Ok(Arc::new(
1966                array
1967                    .as_primitive::<TimestampMicrosecondType>()
1968                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1969                        Ok(time_to_time64us(as_time_res_with_timezone::<
1970                            TimestampMicrosecondType,
1971                        >(x, tz)?))
1972                    })?,
1973            ))
1974        }
1975        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Nanosecond)) => {
1976            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1977            Ok(Arc::new(
1978                array
1979                    .as_primitive::<TimestampMicrosecondType>()
1980                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1981                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1982                            TimestampMicrosecondType,
1983                        >(x, tz)?))
1984                    })?,
1985            ))
1986        }
1987        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Microsecond)) => {
1988            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1989            Ok(Arc::new(
1990                array
1991                    .as_primitive::<TimestampNanosecondType>()
1992                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1993                        Ok(time_to_time64us(as_time_res_with_timezone::<
1994                            TimestampNanosecondType,
1995                        >(x, tz)?))
1996                    })?,
1997            ))
1998        }
1999        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Nanosecond)) => {
2000            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2001            Ok(Arc::new(
2002                array
2003                    .as_primitive::<TimestampNanosecondType>()
2004                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
2005                        Ok(time_to_time64ns(as_time_res_with_timezone::<
2006                            TimestampNanosecondType,
2007                        >(x, tz)?))
2008                    })?,
2009            ))
2010        }
2011        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Second)) => {
2012            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2013            Ok(Arc::new(
2014                array
2015                    .as_primitive::<TimestampSecondType>()
2016                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2017                        Ok(time_to_time32s(as_time_res_with_timezone::<
2018                            TimestampSecondType,
2019                        >(x, tz)?))
2020                    })?,
2021            ))
2022        }
2023        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Millisecond)) => {
2024            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2025            Ok(Arc::new(
2026                array
2027                    .as_primitive::<TimestampSecondType>()
2028                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2029                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2030                            TimestampSecondType,
2031                        >(x, tz)?))
2032                    })?,
2033            ))
2034        }
2035        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Second)) => {
2036            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2037            Ok(Arc::new(
2038                array
2039                    .as_primitive::<TimestampMillisecondType>()
2040                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2041                        Ok(time_to_time32s(as_time_res_with_timezone::<
2042                            TimestampMillisecondType,
2043                        >(x, tz)?))
2044                    })?,
2045            ))
2046        }
2047        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Millisecond)) => {
2048            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2049            Ok(Arc::new(
2050                array
2051                    .as_primitive::<TimestampMillisecondType>()
2052                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2053                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2054                            TimestampMillisecondType,
2055                        >(x, tz)?))
2056                    })?,
2057            ))
2058        }
2059        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Second)) => {
2060            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2061            Ok(Arc::new(
2062                array
2063                    .as_primitive::<TimestampMicrosecondType>()
2064                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2065                        Ok(time_to_time32s(as_time_res_with_timezone::<
2066                            TimestampMicrosecondType,
2067                        >(x, tz)?))
2068                    })?,
2069            ))
2070        }
2071        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Millisecond)) => {
2072            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2073            Ok(Arc::new(
2074                array
2075                    .as_primitive::<TimestampMicrosecondType>()
2076                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2077                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2078                            TimestampMicrosecondType,
2079                        >(x, tz)?))
2080                    })?,
2081            ))
2082        }
2083        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Second)) => {
2084            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2085            Ok(Arc::new(
2086                array
2087                    .as_primitive::<TimestampNanosecondType>()
2088                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2089                        Ok(time_to_time32s(as_time_res_with_timezone::<
2090                            TimestampNanosecondType,
2091                        >(x, tz)?))
2092                    })?,
2093            ))
2094        }
2095        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Millisecond)) => {
2096            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2097            Ok(Arc::new(
2098                array
2099                    .as_primitive::<TimestampNanosecondType>()
2100                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2101                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2102                            TimestampNanosecondType,
2103                        >(x, tz)?))
2104                    })?,
2105            ))
2106        }
2107        (Date64, Timestamp(TimeUnit::Second, _)) => {
2108            let array = array
2109                .as_primitive::<Date64Type>()
2110                .unary::<_, TimestampSecondType>(|x| x / MILLISECONDS);
2111
2112            cast_with_options(&array, to_type, cast_options)
2113        }
2114        (Date64, Timestamp(TimeUnit::Millisecond, _)) => {
2115            let array = array
2116                .as_primitive::<Date64Type>()
2117                .reinterpret_cast::<TimestampMillisecondType>();
2118
2119            cast_with_options(&array, to_type, cast_options)
2120        }
2121
2122        (Date64, Timestamp(TimeUnit::Microsecond, _)) => {
2123            let array = array
2124                .as_primitive::<Date64Type>()
2125                .unary::<_, TimestampMicrosecondType>(|x| x * (MICROSECONDS / MILLISECONDS));
2126
2127            cast_with_options(&array, to_type, cast_options)
2128        }
2129        (Date64, Timestamp(TimeUnit::Nanosecond, _)) => {
2130            let array = array
2131                .as_primitive::<Date64Type>()
2132                .unary::<_, TimestampNanosecondType>(|x| x * (NANOSECONDS / MILLISECONDS));
2133
2134            cast_with_options(&array, to_type, cast_options)
2135        }
2136        (Date32, Timestamp(TimeUnit::Second, _)) => {
2137            let array = array
2138                .as_primitive::<Date32Type>()
2139                .unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY);
2140
2141            cast_with_options(&array, to_type, cast_options)
2142        }
2143        (Date32, Timestamp(TimeUnit::Millisecond, _)) => {
2144            let array = array
2145                .as_primitive::<Date32Type>()
2146                .unary::<_, TimestampMillisecondType>(|x| (x as i64) * MILLISECONDS_IN_DAY);
2147
2148            cast_with_options(&array, to_type, cast_options)
2149        }
2150        (Date32, Timestamp(TimeUnit::Microsecond, _)) => {
2151            let date_array = array.as_primitive::<Date32Type>();
2152            let converted = if cast_options.safe {
2153                date_array.unary_opt::<_, TimestampMicrosecondType>(|x| {
2154                    (x as i64).checked_mul(MICROSECONDS_IN_DAY)
2155                })
2156            } else {
2157                date_array.try_unary::<_, TimestampMicrosecondType, _>(|x| {
2158                    (x as i64).mul_checked(MICROSECONDS_IN_DAY)
2159                })?
2160            };
2161            cast_with_options(&converted, to_type, cast_options)
2162        }
2163        (Date32, Timestamp(TimeUnit::Nanosecond, _)) => {
2164            let date_array = array.as_primitive::<Date32Type>();
2165            let converted = if cast_options.safe {
2166                date_array.unary_opt::<_, TimestampNanosecondType>(|x| {
2167                    (x as i64).checked_mul(NANOSECONDS_IN_DAY)
2168                })
2169            } else {
2170                date_array.try_unary::<_, TimestampNanosecondType, _>(|x| {
2171                    (x as i64).mul_checked(NANOSECONDS_IN_DAY)
2172                })?
2173            };
2174            cast_with_options(&converted, to_type, cast_options)
2175        }
2176
2177        (_, Duration(unit)) if from_type.is_numeric() => {
2178            let array = cast_with_options(array, &Int64, cast_options)?;
2179            Ok(make_duration_array(array.as_primitive(), *unit))
2180        }
2181        (Duration(TimeUnit::Second), _) if to_type.is_numeric() => {
2182            let array = cast_reinterpret_arrays::<DurationSecondType, Int64Type>(array)?;
2183            cast_with_options(&array, to_type, cast_options)
2184        }
2185        (Duration(TimeUnit::Millisecond), _) if to_type.is_numeric() => {
2186            let array = cast_reinterpret_arrays::<DurationMillisecondType, Int64Type>(array)?;
2187            cast_with_options(&array, to_type, cast_options)
2188        }
2189        (Duration(TimeUnit::Microsecond), _) if to_type.is_numeric() => {
2190            let array = cast_reinterpret_arrays::<DurationMicrosecondType, Int64Type>(array)?;
2191            cast_with_options(&array, to_type, cast_options)
2192        }
2193        (Duration(TimeUnit::Nanosecond), _) if to_type.is_numeric() => {
2194            let array = cast_reinterpret_arrays::<DurationNanosecondType, Int64Type>(array)?;
2195            cast_with_options(&array, to_type, cast_options)
2196        }
2197
2198        (Duration(from_unit), Duration(to_unit)) => {
2199            let array = cast_with_options(array, &Int64, cast_options)?;
2200            let time_array = array.as_primitive::<Int64Type>();
2201            let from_size = time_unit_multiple(from_unit);
2202            let to_size = time_unit_multiple(to_unit);
2203            // we either divide or multiply, depending on size of each unit
2204            // units are never the same when the types are the same
2205            let converted = match from_size.cmp(&to_size) {
2206                Ordering::Greater => {
2207                    let divisor = from_size / to_size;
2208                    time_array.unary::<_, Int64Type>(|o| o / divisor)
2209                }
2210                Ordering::Equal => time_array.clone(),
2211                Ordering::Less => {
2212                    let mul = to_size / from_size;
2213                    if cast_options.safe {
2214                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
2215                    } else {
2216                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
2217                    }
2218                }
2219            };
2220            Ok(make_duration_array(&converted, *to_unit))
2221        }
2222
2223        (Duration(TimeUnit::Second), Interval(IntervalUnit::MonthDayNano)) => {
2224            cast_duration_to_interval::<DurationSecondType>(array, cast_options)
2225        }
2226        (Duration(TimeUnit::Millisecond), Interval(IntervalUnit::MonthDayNano)) => {
2227            cast_duration_to_interval::<DurationMillisecondType>(array, cast_options)
2228        }
2229        (Duration(TimeUnit::Microsecond), Interval(IntervalUnit::MonthDayNano)) => {
2230            cast_duration_to_interval::<DurationMicrosecondType>(array, cast_options)
2231        }
2232        (Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano)) => {
2233            cast_duration_to_interval::<DurationNanosecondType>(array, cast_options)
2234        }
2235        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Second)) => {
2236            cast_month_day_nano_to_duration::<DurationSecondType>(array, cast_options)
2237        }
2238        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Millisecond)) => {
2239            cast_month_day_nano_to_duration::<DurationMillisecondType>(array, cast_options)
2240        }
2241        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Microsecond)) => {
2242            cast_month_day_nano_to_duration::<DurationMicrosecondType>(array, cast_options)
2243        }
2244        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Nanosecond)) => {
2245            cast_month_day_nano_to_duration::<DurationNanosecondType>(array, cast_options)
2246        }
2247        (Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
2248            cast_interval_year_month_to_interval_month_day_nano(array, cast_options)
2249        }
2250        (Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
2251            cast_interval_day_time_to_interval_month_day_nano(array, cast_options)
2252        }
2253        (Int32, Interval(IntervalUnit::YearMonth)) => {
2254            cast_reinterpret_arrays::<Int32Type, IntervalYearMonthType>(array)
2255        }
2256        (_, _) => Err(ArrowError::CastError(format!(
2257            "Casting from {from_type} to {to_type} not supported",
2258        ))),
2259    }
2260}
2261
2262fn cast_struct_to_struct(
2263    array: &StructArray,
2264    from_fields: Fields,
2265    to_fields: Fields,
2266    cast_options: &CastOptions,
2267) -> Result<ArrayRef, ArrowError> {
2268    // Fast path: if field names are in the same order, we can just zip and cast
2269    let fields_match_order = from_fields.len() == to_fields.len()
2270        && from_fields
2271            .iter()
2272            .zip(to_fields.iter())
2273            .all(|(f1, f2)| f1.name() == f2.name());
2274
2275    let fields = if fields_match_order {
2276        // Fast path: cast columns in order if their names match
2277        cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2278    } else {
2279        let all_fields_match_by_name = to_fields.iter().all(|to_field| {
2280            from_fields
2281                .iter()
2282                .any(|from_field| from_field.name() == to_field.name())
2283        });
2284
2285        if all_fields_match_by_name {
2286            // Slow path: match fields by name and reorder
2287            cast_struct_fields_by_name(array, from_fields.clone(), to_fields.clone(), cast_options)?
2288        } else {
2289            // Fallback: cast field by field in order
2290            cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2291        }
2292    };
2293
2294    let array = StructArray::try_new(to_fields.clone(), fields, array.nulls().cloned())?;
2295    Ok(Arc::new(array) as ArrayRef)
2296}
2297
2298fn cast_struct_fields_by_name(
2299    array: &StructArray,
2300    from_fields: Fields,
2301    to_fields: Fields,
2302    cast_options: &CastOptions,
2303) -> Result<Vec<ArrayRef>, ArrowError> {
2304    to_fields
2305        .iter()
2306        .map(|to_field| {
2307            let from_field_idx = from_fields
2308                .iter()
2309                .position(|from_field| from_field.name() == to_field.name())
2310                .unwrap(); // safe because we checked above
2311            let column = array.column(from_field_idx);
2312            cast_with_options(column, to_field.data_type(), cast_options)
2313        })
2314        .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2315}
2316
2317fn cast_struct_fields_in_order(
2318    array: &StructArray,
2319    to_fields: Fields,
2320    cast_options: &CastOptions,
2321) -> Result<Vec<ArrayRef>, ArrowError> {
2322    array
2323        .columns()
2324        .iter()
2325        .zip(to_fields.iter())
2326        .map(|(l, field)| cast_with_options(l, field.data_type(), cast_options))
2327        .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2328}
2329
2330fn cast_from_decimal<D, F>(
2331    array: &dyn Array,
2332    base: D::Native,
2333    scale: &i8,
2334    from_type: &DataType,
2335    to_type: &DataType,
2336    as_float: F,
2337    cast_options: &CastOptions,
2338) -> Result<ArrayRef, ArrowError>
2339where
2340    D: DecimalType + ArrowPrimitiveType,
2341    <D as ArrowPrimitiveType>::Native: ToPrimitive,
2342    F: Fn(D::Native) -> f64,
2343{
2344    use DataType::*;
2345    // cast decimal to other type
2346    match to_type {
2347        UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, base, *scale, cast_options),
2348        UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, base, *scale, cast_options),
2349        UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, base, *scale, cast_options),
2350        UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, base, *scale, cast_options),
2351        Int8 => cast_decimal_to_integer::<D, Int8Type>(array, base, *scale, cast_options),
2352        Int16 => cast_decimal_to_integer::<D, Int16Type>(array, base, *scale, cast_options),
2353        Int32 => cast_decimal_to_integer::<D, Int32Type>(array, base, *scale, cast_options),
2354        Int64 => cast_decimal_to_integer::<D, Int64Type>(array, base, *scale, cast_options),
2355        Float16 => cast_decimal_to_float::<D, Float16Type, _>(array, |x| {
2356            half::f16::from_f64(single_decimal_to_float_lossy::<D, F>(
2357                &as_float,
2358                x,
2359                <i32 as From<i8>>::from(*scale),
2360            ))
2361        }),
2362        Float32 => cast_decimal_to_float::<D, Float32Type, _>(array, |x| {
2363            single_decimal_to_float_lossy::<D, F>(&as_float, x, <i32 as From<i8>>::from(*scale))
2364                as f32
2365        }),
2366        Float64 => cast_decimal_to_float::<D, Float64Type, _>(array, |x| {
2367            single_decimal_to_float_lossy::<D, F>(&as_float, x, <i32 as From<i8>>::from(*scale))
2368        }),
2369        Utf8View => value_to_string_view(array, cast_options),
2370        Utf8 => value_to_string::<i32>(array, cast_options),
2371        LargeUtf8 => value_to_string::<i64>(array, cast_options),
2372        Null => Ok(new_null_array(to_type, array.len())),
2373        _ => Err(ArrowError::CastError(format!(
2374            "Casting from {from_type} to {to_type} not supported"
2375        ))),
2376    }
2377}
2378
2379fn cast_to_decimal<D, M>(
2380    array: &dyn Array,
2381    base: M,
2382    precision: &u8,
2383    scale: &i8,
2384    from_type: &DataType,
2385    to_type: &DataType,
2386    cast_options: &CastOptions,
2387) -> Result<ArrayRef, ArrowError>
2388where
2389    D: DecimalType + ArrowPrimitiveType<Native = M>,
2390    M: ArrowNativeTypeOp + DecimalCast,
2391    u8: num_traits::AsPrimitive<M>,
2392    u16: num_traits::AsPrimitive<M>,
2393    u32: num_traits::AsPrimitive<M>,
2394    u64: num_traits::AsPrimitive<M>,
2395    i8: num_traits::AsPrimitive<M>,
2396    i16: num_traits::AsPrimitive<M>,
2397    i32: num_traits::AsPrimitive<M>,
2398    i64: num_traits::AsPrimitive<M>,
2399{
2400    use DataType::*;
2401    // cast data to decimal
2402    match from_type {
2403        UInt8 => cast_integer_to_decimal::<_, D, M>(
2404            array.as_primitive::<UInt8Type>(),
2405            *precision,
2406            *scale,
2407            base,
2408            cast_options,
2409        ),
2410        UInt16 => cast_integer_to_decimal::<_, D, _>(
2411            array.as_primitive::<UInt16Type>(),
2412            *precision,
2413            *scale,
2414            base,
2415            cast_options,
2416        ),
2417        UInt32 => cast_integer_to_decimal::<_, D, _>(
2418            array.as_primitive::<UInt32Type>(),
2419            *precision,
2420            *scale,
2421            base,
2422            cast_options,
2423        ),
2424        UInt64 => cast_integer_to_decimal::<_, D, _>(
2425            array.as_primitive::<UInt64Type>(),
2426            *precision,
2427            *scale,
2428            base,
2429            cast_options,
2430        ),
2431        Int8 => cast_integer_to_decimal::<_, D, _>(
2432            array.as_primitive::<Int8Type>(),
2433            *precision,
2434            *scale,
2435            base,
2436            cast_options,
2437        ),
2438        Int16 => cast_integer_to_decimal::<_, D, _>(
2439            array.as_primitive::<Int16Type>(),
2440            *precision,
2441            *scale,
2442            base,
2443            cast_options,
2444        ),
2445        Int32 => cast_integer_to_decimal::<_, D, _>(
2446            array.as_primitive::<Int32Type>(),
2447            *precision,
2448            *scale,
2449            base,
2450            cast_options,
2451        ),
2452        Int64 => cast_integer_to_decimal::<_, D, _>(
2453            array.as_primitive::<Int64Type>(),
2454            *precision,
2455            *scale,
2456            base,
2457            cast_options,
2458        ),
2459        Float16 => cast_floating_point_to_decimal::<_, D>(
2460            array.as_primitive::<Float16Type>(),
2461            *precision,
2462            *scale,
2463            cast_options,
2464        ),
2465        Float32 => cast_floating_point_to_decimal::<_, D>(
2466            array.as_primitive::<Float32Type>(),
2467            *precision,
2468            *scale,
2469            cast_options,
2470        ),
2471        Float64 => cast_floating_point_to_decimal::<_, D>(
2472            array.as_primitive::<Float64Type>(),
2473            *precision,
2474            *scale,
2475            cast_options,
2476        ),
2477        Utf8View | Utf8 => {
2478            cast_string_to_decimal::<D, i32>(array, *precision, *scale, cast_options)
2479        }
2480        LargeUtf8 => cast_string_to_decimal::<D, i64>(array, *precision, *scale, cast_options),
2481        Null => Ok(new_null_array(to_type, array.len())),
2482        _ => Err(ArrowError::CastError(format!(
2483            "Casting from {from_type} to {to_type} not supported"
2484        ))),
2485    }
2486}
2487
2488/// Get the time unit as a multiple of a second
2489const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
2490    match unit {
2491        TimeUnit::Second => 1,
2492        TimeUnit::Millisecond => MILLISECONDS,
2493        TimeUnit::Microsecond => MICROSECONDS,
2494        TimeUnit::Nanosecond => NANOSECONDS,
2495    }
2496}
2497
2498/// Convert Array into a PrimitiveArray of type, and apply numeric cast
2499fn cast_numeric_arrays<FROM, TO>(
2500    from: &dyn Array,
2501    cast_options: &CastOptions,
2502) -> Result<ArrayRef, ArrowError>
2503where
2504    FROM: ArrowPrimitiveType,
2505    TO: ArrowPrimitiveType,
2506    FROM::Native: NumCast,
2507    TO::Native: NumCast,
2508{
2509    if cast_options.safe {
2510        // If the value can't be casted to the `TO::Native`, return null
2511        Ok(Arc::new(numeric_cast::<FROM, TO>(
2512            from.as_primitive::<FROM>(),
2513        )))
2514    } else {
2515        // If the value can't be casted to the `TO::Native`, return error
2516        Ok(Arc::new(try_numeric_cast::<FROM, TO>(
2517            from.as_primitive::<FROM>(),
2518        )?))
2519    }
2520}
2521
2522// Natural cast between numeric types
2523// If the value of T can't be casted to R, will throw error
2524fn try_numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>, ArrowError>
2525where
2526    T: ArrowPrimitiveType,
2527    R: ArrowPrimitiveType,
2528    T::Native: NumCast,
2529    R::Native: NumCast,
2530{
2531    from.try_unary(|value| {
2532        num_cast::<T::Native, R::Native>(value).ok_or_else(|| {
2533            ArrowError::CastError(format!(
2534                "Can't cast value {:?} to type {}",
2535                value,
2536                R::DATA_TYPE
2537            ))
2538        })
2539    })
2540}
2541
2542/// Natural cast between numeric types
2543/// Return None if the input `value` can't be casted to type `O`.
2544#[inline]
2545pub fn num_cast<I, O>(value: I) -> Option<O>
2546where
2547    I: NumCast,
2548    O: NumCast,
2549{
2550    num_traits::cast::cast::<I, O>(value)
2551}
2552
2553// Natural cast between numeric types
2554// If the value of T can't be casted to R, it will be converted to null
2555fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> PrimitiveArray<R>
2556where
2557    T: ArrowPrimitiveType,
2558    R: ArrowPrimitiveType,
2559    T::Native: NumCast,
2560    R::Native: NumCast,
2561{
2562    from.unary_opt::<_, R>(num_cast::<T::Native, R::Native>)
2563}
2564
2565fn cast_numeric_to_binary<FROM: ArrowPrimitiveType, O: OffsetSizeTrait>(
2566    array: &dyn Array,
2567) -> Result<ArrayRef, ArrowError> {
2568    let array = array.as_primitive::<FROM>();
2569    let size = std::mem::size_of::<FROM::Native>();
2570    let offsets = OffsetBuffer::from_repeated_length(size, array.len());
2571    Ok(Arc::new(GenericBinaryArray::<O>::try_new(
2572        offsets,
2573        array.values().inner().clone(),
2574        array.nulls().cloned(),
2575    )?))
2576}
2577
2578fn adjust_timestamp_to_timezone<T: ArrowTimestampType>(
2579    array: PrimitiveArray<Int64Type>,
2580    to_tz: &Tz,
2581    cast_options: &CastOptions,
2582) -> Result<PrimitiveArray<Int64Type>, ArrowError> {
2583    let adjust = |o| {
2584        let local = as_datetime::<T>(o)?;
2585        let offset = to_tz.offset_from_local_datetime(&local).single()?;
2586        T::from_naive_datetime(local - offset.fix(), None)
2587    };
2588    let adjusted = if cast_options.safe {
2589        array.unary_opt::<_, Int64Type>(adjust)
2590    } else {
2591        array.try_unary::<_, Int64Type, _>(|o| {
2592            adjust(o).ok_or_else(|| {
2593                ArrowError::CastError("Cannot cast timezone to different timezone".to_string())
2594            })
2595        })?
2596    };
2597    Ok(adjusted)
2598}
2599
2600/// Cast numeric types to Boolean
2601///
2602/// Any zero value returns `false` while non-zero returns `true`
2603fn cast_numeric_to_bool<FROM>(from: &dyn Array) -> Result<ArrayRef, ArrowError>
2604where
2605    FROM: ArrowPrimitiveType,
2606{
2607    numeric_to_bool_cast::<FROM>(from.as_primitive::<FROM>()).map(|to| Arc::new(to) as ArrayRef)
2608}
2609
2610fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray, ArrowError>
2611where
2612    T: ArrowPrimitiveType,
2613{
2614    let mut b = BooleanBuilder::with_capacity(from.len());
2615
2616    for i in 0..from.len() {
2617        if from.is_null(i) {
2618            b.append_null();
2619        } else {
2620            b.append_value(cast_num_to_bool::<T::Native>(from.value(i)));
2621        }
2622    }
2623
2624    Ok(b.finish())
2625}
2626
2627/// Cast numeric types to boolean
2628#[inline]
2629pub fn cast_num_to_bool<I>(value: I) -> bool
2630where
2631    I: Default + PartialEq,
2632{
2633    value != I::default()
2634}
2635
2636/// Cast Boolean types to numeric
2637///
2638/// `false` returns 0 while `true` returns 1
2639fn cast_bool_to_numeric<TO>(
2640    from: &dyn Array,
2641    cast_options: &CastOptions,
2642) -> Result<ArrayRef, ArrowError>
2643where
2644    TO: ArrowPrimitiveType,
2645    TO::Native: num_traits::cast::NumCast,
2646{
2647    Ok(Arc::new(bool_to_numeric_cast::<TO>(
2648        from.as_any().downcast_ref::<BooleanArray>().unwrap(),
2649        cast_options,
2650    )))
2651}
2652
2653fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions) -> PrimitiveArray<T>
2654where
2655    T: ArrowPrimitiveType,
2656    T::Native: num_traits::NumCast,
2657{
2658    let iter = (0..from.len()).map(|i| {
2659        if from.is_null(i) {
2660            None
2661        } else {
2662            single_bool_to_numeric::<T::Native>(from.value(i))
2663        }
2664    });
2665    // Benefit:
2666    //     20% performance improvement
2667    // Soundness:
2668    //     The iterator is trustedLen because it comes from a Range
2669    unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
2670}
2671
2672/// Cast single bool value to numeric value.
2673#[inline]
2674pub fn single_bool_to_numeric<O>(value: bool) -> Option<O>
2675where
2676    O: num_traits::NumCast + Default,
2677{
2678    if value {
2679        // a workaround to cast a primitive to type O, infallible
2680        num_traits::cast::cast(1)
2681    } else {
2682        Some(O::default())
2683    }
2684}
2685
2686/// Helper function to cast from one `BinaryArray` or 'LargeBinaryArray' to 'FixedSizeBinaryArray'.
2687fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
2688    array: &dyn Array,
2689    byte_width: i32,
2690    cast_options: &CastOptions,
2691) -> Result<ArrayRef, ArrowError> {
2692    let array = array.as_binary::<O>();
2693    let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(), byte_width);
2694
2695    for i in 0..array.len() {
2696        if array.is_null(i) {
2697            builder.append_null();
2698        } else {
2699            match builder.append_value(array.value(i)) {
2700                Ok(()) => {}
2701                Err(e) => match cast_options.safe {
2702                    true => builder.append_null(),
2703                    false => return Err(e),
2704                },
2705            }
2706        }
2707    }
2708
2709    Ok(Arc::new(builder.finish()))
2710}
2711
2712/// Helper function to cast from 'FixedSizeBinaryArray' to one `BinaryArray` or 'LargeBinaryArray'.
2713/// If the target one is too large for the source array it will return an Error.
2714fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
2715    array: &dyn Array,
2716    byte_width: i32,
2717) -> Result<ArrayRef, ArrowError> {
2718    let array = array
2719        .as_any()
2720        .downcast_ref::<FixedSizeBinaryArray>()
2721        .unwrap();
2722
2723    let offsets: i128 = byte_width as i128 * array.len() as i128;
2724
2725    let is_binary = matches!(GenericBinaryType::<O>::DATA_TYPE, DataType::Binary);
2726    if is_binary && offsets > i32::MAX as i128 {
2727        return Err(ArrowError::ComputeError(
2728            "FixedSizeBinary array too large to cast to Binary array".to_string(),
2729        ));
2730    } else if !is_binary && offsets > i64::MAX as i128 {
2731        return Err(ArrowError::ComputeError(
2732            "FixedSizeBinary array too large to cast to LargeBinary array".to_string(),
2733        ));
2734    }
2735
2736    let mut builder = GenericBinaryBuilder::<O>::with_capacity(array.len(), array.len());
2737
2738    for i in 0..array.len() {
2739        if array.is_null(i) {
2740            builder.append_null();
2741        } else {
2742            builder.append_value(array.value(i));
2743        }
2744    }
2745
2746    Ok(Arc::new(builder.finish()))
2747}
2748
2749fn cast_fixed_size_binary_to_binary_view(
2750    array: &dyn Array,
2751    _byte_width: i32,
2752) -> Result<ArrayRef, ArrowError> {
2753    let array = array
2754        .as_any()
2755        .downcast_ref::<FixedSizeBinaryArray>()
2756        .unwrap();
2757
2758    let mut builder = BinaryViewBuilder::with_capacity(array.len());
2759    for i in 0..array.len() {
2760        if array.is_null(i) {
2761            builder.append_null();
2762        } else {
2763            builder.append_value(array.value(i));
2764        }
2765    }
2766
2767    Ok(Arc::new(builder.finish()))
2768}
2769
2770/// Helper function to cast from one `ByteArrayType` to another and vice versa.
2771/// If the target one (e.g., `LargeUtf8`) is too large for the source array it will return an Error.
2772fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2773where
2774    FROM: ByteArrayType,
2775    TO: ByteArrayType<Native = FROM::Native>,
2776    FROM::Offset: OffsetSizeTrait + ToPrimitive,
2777    TO::Offset: OffsetSizeTrait + NumCast,
2778{
2779    let data = array.to_data();
2780    assert_eq!(data.data_type(), &FROM::DATA_TYPE);
2781    let str_values_buf = data.buffers()[1].clone();
2782    let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
2783
2784    let mut offset_builder = BufferBuilder::<TO::Offset>::new(offsets.len());
2785    offsets
2786        .iter()
2787        .try_for_each::<_, Result<_, ArrowError>>(|offset| {
2788            let offset =
2789                <<TO as ByteArrayType>::Offset as NumCast>::from(*offset).ok_or_else(|| {
2790                    ArrowError::ComputeError(format!(
2791                        "{}{} array too large to cast to {}{} array",
2792                        FROM::Offset::PREFIX,
2793                        FROM::PREFIX,
2794                        TO::Offset::PREFIX,
2795                        TO::PREFIX
2796                    ))
2797                })?;
2798            offset_builder.append(offset);
2799            Ok(())
2800        })?;
2801
2802    let offset_buffer = offset_builder.finish();
2803
2804    let dtype = TO::DATA_TYPE;
2805
2806    let builder = ArrayData::builder(dtype)
2807        .offset(array.offset())
2808        .len(array.len())
2809        .add_buffer(offset_buffer)
2810        .add_buffer(str_values_buf)
2811        .nulls(data.nulls().cloned());
2812
2813    let array_data = unsafe { builder.build_unchecked() };
2814
2815    Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
2816}
2817
2818/// Helper function to cast from one `ByteViewType` array to `ByteArrayType` array.
2819fn cast_view_to_byte<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2820where
2821    FROM: ByteViewType,
2822    TO: ByteArrayType,
2823    FROM::Native: AsRef<TO::Native>,
2824{
2825    let data = array.to_data();
2826    let view_array = GenericByteViewArray::<FROM>::from(data);
2827
2828    let len = view_array.len();
2829    let bytes = view_array
2830        .views()
2831        .iter()
2832        .map(|v| ByteView::from(*v).length as usize)
2833        .sum::<usize>();
2834
2835    let mut byte_array_builder = GenericByteBuilder::<TO>::with_capacity(len, bytes);
2836
2837    for val in view_array.iter() {
2838        byte_array_builder.append_option(val);
2839    }
2840
2841    Ok(Arc::new(byte_array_builder.finish()))
2842}
2843
2844#[cfg(test)]
2845mod tests {
2846    use super::*;
2847    use DataType::*;
2848    use arrow_array::{Int64Array, RunArray, StringArray};
2849    use arrow_buffer::{Buffer, IntervalDayTime, NullBuffer};
2850    use arrow_buffer::{ScalarBuffer, i256};
2851    use arrow_schema::{DataType, Field};
2852    use chrono::NaiveDate;
2853    use half::f16;
2854    use std::sync::Arc;
2855
2856    #[derive(Clone)]
2857    struct DecimalCastTestConfig {
2858        input_prec: u8,
2859        input_scale: i8,
2860        input_repr: i128,
2861        output_prec: u8,
2862        output_scale: i8,
2863        expected_output_repr: Result<i128, String>, // the error variant can contain a string
2864                                                    // template where the "{}" will be
2865                                                    // replaced with the decimal type name
2866                                                    // (e.g. Decimal128)
2867    }
2868
2869    macro_rules! generate_cast_test_case {
2870        ($INPUT_ARRAY: expr, $OUTPUT_TYPE_ARRAY: ident, $OUTPUT_TYPE: expr, $OUTPUT_VALUES: expr) => {
2871            let output =
2872                $OUTPUT_TYPE_ARRAY::from($OUTPUT_VALUES).with_data_type($OUTPUT_TYPE.clone());
2873
2874            // assert cast type
2875            let input_array_type = $INPUT_ARRAY.data_type();
2876            assert!(can_cast_types(input_array_type, $OUTPUT_TYPE));
2877            let result = cast($INPUT_ARRAY, $OUTPUT_TYPE).unwrap();
2878            assert_eq!($OUTPUT_TYPE, result.data_type());
2879            assert_eq!(result.as_ref(), &output);
2880
2881            let cast_option = CastOptions {
2882                safe: false,
2883                format_options: FormatOptions::default(),
2884            };
2885            let result = cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap();
2886            assert_eq!($OUTPUT_TYPE, result.data_type());
2887            assert_eq!(result.as_ref(), &output);
2888        };
2889    }
2890
2891    fn run_decimal_cast_test_case<I, O>(t: DecimalCastTestConfig)
2892    where
2893        I: DecimalType,
2894        O: DecimalType,
2895        I::Native: DecimalCast,
2896        O::Native: DecimalCast,
2897    {
2898        let array = vec![I::Native::from_decimal(t.input_repr)];
2899        let array = array
2900            .into_iter()
2901            .collect::<PrimitiveArray<I>>()
2902            .with_precision_and_scale(t.input_prec, t.input_scale)
2903            .unwrap();
2904        let input_type = array.data_type();
2905        let output_type = O::TYPE_CONSTRUCTOR(t.output_prec, t.output_scale);
2906        assert!(can_cast_types(input_type, &output_type));
2907
2908        let options = CastOptions {
2909            safe: false,
2910            ..Default::default()
2911        };
2912        let result = cast_with_options(&array, &output_type, &options);
2913
2914        match t.expected_output_repr {
2915            Ok(v) => {
2916                let expected_array = vec![O::Native::from_decimal(v)];
2917                let expected_array = expected_array
2918                    .into_iter()
2919                    .collect::<PrimitiveArray<O>>()
2920                    .with_precision_and_scale(t.output_prec, t.output_scale)
2921                    .unwrap();
2922                assert_eq!(*result.unwrap(), expected_array);
2923            }
2924            Err(expected_output_message_template) => {
2925                assert!(result.is_err());
2926                let expected_error_message =
2927                    expected_output_message_template.replace("{}", O::PREFIX);
2928                assert_eq!(result.unwrap_err().to_string(), expected_error_message);
2929            }
2930        }
2931    }
2932
2933    fn create_decimal32_array(
2934        array: Vec<Option<i32>>,
2935        precision: u8,
2936        scale: i8,
2937    ) -> Result<Decimal32Array, ArrowError> {
2938        array
2939            .into_iter()
2940            .collect::<Decimal32Array>()
2941            .with_precision_and_scale(precision, scale)
2942    }
2943
2944    fn create_decimal64_array(
2945        array: Vec<Option<i64>>,
2946        precision: u8,
2947        scale: i8,
2948    ) -> Result<Decimal64Array, ArrowError> {
2949        array
2950            .into_iter()
2951            .collect::<Decimal64Array>()
2952            .with_precision_and_scale(precision, scale)
2953    }
2954
2955    fn create_decimal128_array(
2956        array: Vec<Option<i128>>,
2957        precision: u8,
2958        scale: i8,
2959    ) -> Result<Decimal128Array, ArrowError> {
2960        array
2961            .into_iter()
2962            .collect::<Decimal128Array>()
2963            .with_precision_and_scale(precision, scale)
2964    }
2965
2966    fn create_decimal256_array(
2967        array: Vec<Option<i256>>,
2968        precision: u8,
2969        scale: i8,
2970    ) -> Result<Decimal256Array, ArrowError> {
2971        array
2972            .into_iter()
2973            .collect::<Decimal256Array>()
2974            .with_precision_and_scale(precision, scale)
2975    }
2976
2977    #[test]
2978    #[cfg(not(feature = "force_validate"))]
2979    #[should_panic(
2980        expected = "Cannot cast to Decimal128(20, 3). Overflowing on 57896044618658097711785492504343953926634992332820282019728792003956564819967"
2981    )]
2982    fn test_cast_decimal_to_decimal_round_with_error() {
2983        // decimal256 to decimal128 overflow
2984        let array = vec![
2985            Some(i256::from_i128(1123454)),
2986            Some(i256::from_i128(2123456)),
2987            Some(i256::from_i128(-3123453)),
2988            Some(i256::from_i128(-3123456)),
2989            None,
2990            Some(i256::MAX),
2991            Some(i256::MIN),
2992        ];
2993        let input_decimal_array = create_decimal256_array(array, 76, 4).unwrap();
2994        let array = Arc::new(input_decimal_array) as ArrayRef;
2995        let input_type = DataType::Decimal256(76, 4);
2996        let output_type = DataType::Decimal128(20, 3);
2997        assert!(can_cast_types(&input_type, &output_type));
2998        generate_cast_test_case!(
2999            &array,
3000            Decimal128Array,
3001            &output_type,
3002            vec![
3003                Some(112345_i128),
3004                Some(212346_i128),
3005                Some(-312345_i128),
3006                Some(-312346_i128),
3007                None,
3008                None,
3009                None,
3010            ]
3011        );
3012    }
3013
3014    #[test]
3015    #[cfg(not(feature = "force_validate"))]
3016    fn test_cast_decimal_to_decimal_round() {
3017        let array = vec![
3018            Some(1123454),
3019            Some(2123456),
3020            Some(-3123453),
3021            Some(-3123456),
3022            None,
3023        ];
3024        let array = create_decimal128_array(array, 20, 4).unwrap();
3025        // decimal128 to decimal128
3026        let input_type = DataType::Decimal128(20, 4);
3027        let output_type = DataType::Decimal128(20, 3);
3028        assert!(can_cast_types(&input_type, &output_type));
3029        generate_cast_test_case!(
3030            &array,
3031            Decimal128Array,
3032            &output_type,
3033            vec![
3034                Some(112345_i128),
3035                Some(212346_i128),
3036                Some(-312345_i128),
3037                Some(-312346_i128),
3038                None
3039            ]
3040        );
3041
3042        // decimal128 to decimal256
3043        let input_type = DataType::Decimal128(20, 4);
3044        let output_type = DataType::Decimal256(20, 3);
3045        assert!(can_cast_types(&input_type, &output_type));
3046        generate_cast_test_case!(
3047            &array,
3048            Decimal256Array,
3049            &output_type,
3050            vec![
3051                Some(i256::from_i128(112345_i128)),
3052                Some(i256::from_i128(212346_i128)),
3053                Some(i256::from_i128(-312345_i128)),
3054                Some(i256::from_i128(-312346_i128)),
3055                None
3056            ]
3057        );
3058
3059        // decimal256
3060        let array = vec![
3061            Some(i256::from_i128(1123454)),
3062            Some(i256::from_i128(2123456)),
3063            Some(i256::from_i128(-3123453)),
3064            Some(i256::from_i128(-3123456)),
3065            None,
3066        ];
3067        let array = create_decimal256_array(array, 20, 4).unwrap();
3068
3069        // decimal256 to decimal256
3070        let input_type = DataType::Decimal256(20, 4);
3071        let output_type = DataType::Decimal256(20, 3);
3072        assert!(can_cast_types(&input_type, &output_type));
3073        generate_cast_test_case!(
3074            &array,
3075            Decimal256Array,
3076            &output_type,
3077            vec![
3078                Some(i256::from_i128(112345_i128)),
3079                Some(i256::from_i128(212346_i128)),
3080                Some(i256::from_i128(-312345_i128)),
3081                Some(i256::from_i128(-312346_i128)),
3082                None
3083            ]
3084        );
3085        // decimal256 to decimal128
3086        let input_type = DataType::Decimal256(20, 4);
3087        let output_type = DataType::Decimal128(20, 3);
3088        assert!(can_cast_types(&input_type, &output_type));
3089        generate_cast_test_case!(
3090            &array,
3091            Decimal128Array,
3092            &output_type,
3093            vec![
3094                Some(112345_i128),
3095                Some(212346_i128),
3096                Some(-312345_i128),
3097                Some(-312346_i128),
3098                None
3099            ]
3100        );
3101    }
3102
3103    #[test]
3104    fn test_cast_decimal32_to_decimal32() {
3105        // test changing precision
3106        let input_type = DataType::Decimal32(9, 3);
3107        let output_type = DataType::Decimal32(9, 4);
3108        assert!(can_cast_types(&input_type, &output_type));
3109        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3110        let array = create_decimal32_array(array, 9, 3).unwrap();
3111        generate_cast_test_case!(
3112            &array,
3113            Decimal32Array,
3114            &output_type,
3115            vec![
3116                Some(11234560_i32),
3117                Some(21234560_i32),
3118                Some(31234560_i32),
3119                None
3120            ]
3121        );
3122        // negative test
3123        let array = vec![Some(123456), None];
3124        let array = create_decimal32_array(array, 9, 0).unwrap();
3125        let result_safe = cast(&array, &DataType::Decimal32(2, 2));
3126        assert!(result_safe.is_ok());
3127        let options = CastOptions {
3128            safe: false,
3129            ..Default::default()
3130        };
3131
3132        let result_unsafe = cast_with_options(&array, &DataType::Decimal32(2, 2), &options);
3133        assert_eq!(
3134            "Invalid argument error: 123456.00 is too large to store in a Decimal32 of precision 2. Max is 0.99",
3135            result_unsafe.unwrap_err().to_string()
3136        );
3137    }
3138
3139    #[test]
3140    fn test_cast_decimal64_to_decimal64() {
3141        // test changing precision
3142        let input_type = DataType::Decimal64(17, 3);
3143        let output_type = DataType::Decimal64(17, 4);
3144        assert!(can_cast_types(&input_type, &output_type));
3145        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3146        let array = create_decimal64_array(array, 17, 3).unwrap();
3147        generate_cast_test_case!(
3148            &array,
3149            Decimal64Array,
3150            &output_type,
3151            vec![
3152                Some(11234560_i64),
3153                Some(21234560_i64),
3154                Some(31234560_i64),
3155                None
3156            ]
3157        );
3158        // negative test
3159        let array = vec![Some(123456), None];
3160        let array = create_decimal64_array(array, 9, 0).unwrap();
3161        let result_safe = cast(&array, &DataType::Decimal64(2, 2));
3162        assert!(result_safe.is_ok());
3163        let options = CastOptions {
3164            safe: false,
3165            ..Default::default()
3166        };
3167
3168        let result_unsafe = cast_with_options(&array, &DataType::Decimal64(2, 2), &options);
3169        assert_eq!(
3170            "Invalid argument error: 123456.00 is too large to store in a Decimal64 of precision 2. Max is 0.99",
3171            result_unsafe.unwrap_err().to_string()
3172        );
3173    }
3174
3175    #[test]
3176    fn test_cast_decimal128_to_decimal128() {
3177        // test changing precision
3178        let input_type = DataType::Decimal128(20, 3);
3179        let output_type = DataType::Decimal128(20, 4);
3180        assert!(can_cast_types(&input_type, &output_type));
3181        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3182        let array = create_decimal128_array(array, 20, 3).unwrap();
3183        generate_cast_test_case!(
3184            &array,
3185            Decimal128Array,
3186            &output_type,
3187            vec![
3188                Some(11234560_i128),
3189                Some(21234560_i128),
3190                Some(31234560_i128),
3191                None
3192            ]
3193        );
3194        // negative test
3195        let array = vec![Some(123456), None];
3196        let array = create_decimal128_array(array, 10, 0).unwrap();
3197        let result_safe = cast(&array, &DataType::Decimal128(2, 2));
3198        assert!(result_safe.is_ok());
3199        let options = CastOptions {
3200            safe: false,
3201            ..Default::default()
3202        };
3203
3204        let result_unsafe = cast_with_options(&array, &DataType::Decimal128(2, 2), &options);
3205        assert_eq!(
3206            "Invalid argument error: 123456.00 is too large to store in a Decimal128 of precision 2. Max is 0.99",
3207            result_unsafe.unwrap_err().to_string()
3208        );
3209    }
3210
3211    #[test]
3212    fn test_cast_decimal32_to_decimal32_dict() {
3213        let p = 9;
3214        let s = 3;
3215        let input_type = DataType::Decimal32(p, s);
3216        let output_type = DataType::Dictionary(
3217            Box::new(DataType::Int32),
3218            Box::new(DataType::Decimal32(p, s)),
3219        );
3220        assert!(can_cast_types(&input_type, &output_type));
3221        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3222        let array = create_decimal32_array(array, p, s).unwrap();
3223        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3224        assert_eq!(cast_array.data_type(), &output_type);
3225    }
3226
3227    #[test]
3228    fn test_cast_decimal64_to_decimal64_dict() {
3229        let p = 15;
3230        let s = 3;
3231        let input_type = DataType::Decimal64(p, s);
3232        let output_type = DataType::Dictionary(
3233            Box::new(DataType::Int32),
3234            Box::new(DataType::Decimal64(p, s)),
3235        );
3236        assert!(can_cast_types(&input_type, &output_type));
3237        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3238        let array = create_decimal64_array(array, p, s).unwrap();
3239        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3240        assert_eq!(cast_array.data_type(), &output_type);
3241    }
3242
3243    #[test]
3244    fn test_cast_decimal128_to_decimal128_dict() {
3245        let p = 20;
3246        let s = 3;
3247        let input_type = DataType::Decimal128(p, s);
3248        let output_type = DataType::Dictionary(
3249            Box::new(DataType::Int32),
3250            Box::new(DataType::Decimal128(p, s)),
3251        );
3252        assert!(can_cast_types(&input_type, &output_type));
3253        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3254        let array = create_decimal128_array(array, p, s).unwrap();
3255        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3256        assert_eq!(cast_array.data_type(), &output_type);
3257    }
3258
3259    #[test]
3260    fn test_cast_decimal256_to_decimal256_dict() {
3261        let p = 20;
3262        let s = 3;
3263        let input_type = DataType::Decimal256(p, s);
3264        let output_type = DataType::Dictionary(
3265            Box::new(DataType::Int32),
3266            Box::new(DataType::Decimal256(p, s)),
3267        );
3268        assert!(can_cast_types(&input_type, &output_type));
3269        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3270        let array = create_decimal128_array(array, p, s).unwrap();
3271        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3272        assert_eq!(cast_array.data_type(), &output_type);
3273    }
3274
3275    #[test]
3276    fn test_cast_decimal32_to_decimal32_overflow() {
3277        let input_type = DataType::Decimal32(9, 3);
3278        let output_type = DataType::Decimal32(9, 9);
3279        assert!(can_cast_types(&input_type, &output_type));
3280
3281        let array = vec![Some(i32::MAX)];
3282        let array = create_decimal32_array(array, 9, 3).unwrap();
3283        let result = cast_with_options(
3284            &array,
3285            &output_type,
3286            &CastOptions {
3287                safe: false,
3288                format_options: FormatOptions::default(),
3289            },
3290        );
3291        assert_eq!(
3292            "Cast error: Cannot cast to Decimal32(9, 9). Overflowing on 2147483647",
3293            result.unwrap_err().to_string()
3294        );
3295    }
3296
3297    #[test]
3298    fn test_cast_decimal32_to_decimal32_large_scale_reduction() {
3299        let array = vec![Some(-999999999), Some(0), Some(999999999), None];
3300        let array = create_decimal32_array(array, 9, 3).unwrap();
3301
3302        // Divide out all digits of precision -- rounding could still produce +/- 1
3303        let output_type = DataType::Decimal32(9, -6);
3304        assert!(can_cast_types(array.data_type(), &output_type));
3305        generate_cast_test_case!(
3306            &array,
3307            Decimal32Array,
3308            &output_type,
3309            vec![Some(-1), Some(0), Some(1), None]
3310        );
3311
3312        // Divide out more digits than we have precision -- all-zero result
3313        let output_type = DataType::Decimal32(9, -7);
3314        assert!(can_cast_types(array.data_type(), &output_type));
3315        generate_cast_test_case!(
3316            &array,
3317            Decimal32Array,
3318            &output_type,
3319            vec![Some(0), Some(0), Some(0), None]
3320        );
3321    }
3322
3323    #[test]
3324    fn test_cast_decimal64_to_decimal64_overflow() {
3325        let input_type = DataType::Decimal64(18, 3);
3326        let output_type = DataType::Decimal64(18, 18);
3327        assert!(can_cast_types(&input_type, &output_type));
3328
3329        let array = vec![Some(i64::MAX)];
3330        let array = create_decimal64_array(array, 18, 3).unwrap();
3331        let result = cast_with_options(
3332            &array,
3333            &output_type,
3334            &CastOptions {
3335                safe: false,
3336                format_options: FormatOptions::default(),
3337            },
3338        );
3339        assert_eq!(
3340            "Cast error: Cannot cast to Decimal64(18, 18). Overflowing on 9223372036854775807",
3341            result.unwrap_err().to_string()
3342        );
3343    }
3344
3345    #[test]
3346    fn test_cast_decimal64_to_decimal64_large_scale_reduction() {
3347        let array = vec![
3348            Some(-999999999999999999),
3349            Some(0),
3350            Some(999999999999999999),
3351            None,
3352        ];
3353        let array = create_decimal64_array(array, 18, 3).unwrap();
3354
3355        // Divide out all digits of precision -- rounding could still produce +/- 1
3356        let output_type = DataType::Decimal64(18, -15);
3357        assert!(can_cast_types(array.data_type(), &output_type));
3358        generate_cast_test_case!(
3359            &array,
3360            Decimal64Array,
3361            &output_type,
3362            vec![Some(-1), Some(0), Some(1), None]
3363        );
3364
3365        // Divide out more digits than we have precision -- all-zero result
3366        let output_type = DataType::Decimal64(18, -16);
3367        assert!(can_cast_types(array.data_type(), &output_type));
3368        generate_cast_test_case!(
3369            &array,
3370            Decimal64Array,
3371            &output_type,
3372            vec![Some(0), Some(0), Some(0), None]
3373        );
3374    }
3375
3376    #[test]
3377    fn test_cast_floating_to_decimals() {
3378        for output_type in [
3379            DataType::Decimal32(9, 3),
3380            DataType::Decimal64(9, 3),
3381            DataType::Decimal128(9, 3),
3382            DataType::Decimal256(9, 3),
3383        ] {
3384            let input_type = DataType::Float64;
3385            assert!(can_cast_types(&input_type, &output_type));
3386
3387            let array = vec![Some(1.1_f64)];
3388            let array = PrimitiveArray::<Float64Type>::from_iter(array);
3389            let result = cast_with_options(
3390                &array,
3391                &output_type,
3392                &CastOptions {
3393                    safe: false,
3394                    format_options: FormatOptions::default(),
3395                },
3396            );
3397            assert!(
3398                result.is_ok(),
3399                "Failed to cast to {output_type} with: {}",
3400                result.unwrap_err()
3401            );
3402        }
3403    }
3404
3405    #[test]
3406    #[cfg_attr(miri, ignore)] // Takes too long
3407    fn test_cast_float16_to_decimals() {
3408        let array = Float16Array::from(vec![
3409            Some(f16::from_f32(1.25)),
3410            Some(f16::from_f32(-2.5)),
3411            Some(f16::from_f32(1.125)),
3412            Some(f16::from_f32(-1.125)),
3413            Some(f16::from_f32(0.0)),
3414            None,
3415        ]);
3416
3417        generate_cast_test_case!(
3418            &array,
3419            Decimal32Array,
3420            &DataType::Decimal32(9, 2),
3421            vec![
3422                Some(125_i32),
3423                Some(-250_i32),
3424                Some(113_i32),
3425                Some(-113_i32),
3426                Some(0_i32),
3427                None
3428            ]
3429        );
3430        generate_cast_test_case!(
3431            &array,
3432            Decimal64Array,
3433            &DataType::Decimal64(18, 2),
3434            vec![
3435                Some(125_i64),
3436                Some(-250_i64),
3437                Some(113_i64),
3438                Some(-113_i64),
3439                Some(0_i64),
3440                None
3441            ]
3442        );
3443        generate_cast_test_case!(
3444            &array,
3445            Decimal128Array,
3446            &DataType::Decimal128(38, 2),
3447            vec![
3448                Some(125_i128),
3449                Some(-250_i128),
3450                Some(113_i128),
3451                Some(-113_i128),
3452                Some(0_i128),
3453                None
3454            ]
3455        );
3456        generate_cast_test_case!(
3457            &array,
3458            Decimal256Array,
3459            &DataType::Decimal256(76, 2),
3460            vec![
3461                Some(i256::from_i128(125_i128)),
3462                Some(i256::from_i128(-250_i128)),
3463                Some(i256::from_i128(113_i128)),
3464                Some(i256::from_i128(-113_i128)),
3465                Some(i256::from_i128(0_i128)),
3466                None
3467            ]
3468        );
3469
3470        let array = Float16Array::from(vec![
3471            Some(f16::from_f32(1250.0)),
3472            Some(f16::from_f32(-1250.0)),
3473            Some(f16::from_f32(1249.0)),
3474            None,
3475        ]);
3476        generate_cast_test_case!(
3477            &array,
3478            Decimal128Array,
3479            &DataType::Decimal128(5, -2),
3480            vec![Some(13_i128), Some(-13_i128), Some(12_i128), None]
3481        );
3482    }
3483
3484    #[test]
3485    fn test_cast_decimal128_to_decimal128_overflow() {
3486        let input_type = DataType::Decimal128(38, 3);
3487        let output_type = DataType::Decimal128(38, 38);
3488        assert!(can_cast_types(&input_type, &output_type));
3489
3490        let array = vec![Some(i128::MAX)];
3491        let array = create_decimal128_array(array, 38, 3).unwrap();
3492        let result = cast_with_options(
3493            &array,
3494            &output_type,
3495            &CastOptions {
3496                safe: false,
3497                format_options: FormatOptions::default(),
3498            },
3499        );
3500        assert_eq!(
3501            "Cast error: Cannot cast to Decimal128(38, 38). Overflowing on 170141183460469231731687303715884105727",
3502            result.unwrap_err().to_string()
3503        );
3504    }
3505
3506    #[test]
3507    fn test_cast_decimal128_to_decimal256_overflow() {
3508        let input_type = DataType::Decimal128(38, 3);
3509        let output_type = DataType::Decimal256(76, 76);
3510        assert!(can_cast_types(&input_type, &output_type));
3511
3512        let array = vec![Some(i128::MAX)];
3513        let array = create_decimal128_array(array, 38, 3).unwrap();
3514        let result = cast_with_options(
3515            &array,
3516            &output_type,
3517            &CastOptions {
3518                safe: false,
3519                format_options: FormatOptions::default(),
3520            },
3521        );
3522        assert_eq!(
3523            "Cast error: Cannot cast to Decimal256(76, 76). Overflowing on 170141183460469231731687303715884105727",
3524            result.unwrap_err().to_string()
3525        );
3526    }
3527
3528    #[test]
3529    fn test_cast_decimal32_to_decimal256() {
3530        let input_type = DataType::Decimal32(8, 3);
3531        let output_type = DataType::Decimal256(20, 4);
3532        assert!(can_cast_types(&input_type, &output_type));
3533        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3534        let array = create_decimal32_array(array, 8, 3).unwrap();
3535        generate_cast_test_case!(
3536            &array,
3537            Decimal256Array,
3538            &output_type,
3539            vec![
3540                Some(i256::from_i128(11234560_i128)),
3541                Some(i256::from_i128(21234560_i128)),
3542                Some(i256::from_i128(31234560_i128)),
3543                None
3544            ]
3545        );
3546    }
3547    #[test]
3548    fn test_cast_decimal64_to_decimal256() {
3549        let input_type = DataType::Decimal64(12, 3);
3550        let output_type = DataType::Decimal256(20, 4);
3551        assert!(can_cast_types(&input_type, &output_type));
3552        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3553        let array = create_decimal64_array(array, 12, 3).unwrap();
3554        generate_cast_test_case!(
3555            &array,
3556            Decimal256Array,
3557            &output_type,
3558            vec![
3559                Some(i256::from_i128(11234560_i128)),
3560                Some(i256::from_i128(21234560_i128)),
3561                Some(i256::from_i128(31234560_i128)),
3562                None
3563            ]
3564        );
3565    }
3566    #[test]
3567    fn test_cast_decimal128_to_decimal256() {
3568        let input_type = DataType::Decimal128(20, 3);
3569        let output_type = DataType::Decimal256(20, 4);
3570        assert!(can_cast_types(&input_type, &output_type));
3571        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3572        let array = create_decimal128_array(array, 20, 3).unwrap();
3573        generate_cast_test_case!(
3574            &array,
3575            Decimal256Array,
3576            &output_type,
3577            vec![
3578                Some(i256::from_i128(11234560_i128)),
3579                Some(i256::from_i128(21234560_i128)),
3580                Some(i256::from_i128(31234560_i128)),
3581                None
3582            ]
3583        );
3584    }
3585
3586    #[test]
3587    fn test_cast_decimal256_to_decimal128_overflow() {
3588        let input_type = DataType::Decimal256(76, 5);
3589        let output_type = DataType::Decimal128(38, 7);
3590        assert!(can_cast_types(&input_type, &output_type));
3591        let array = vec![Some(i256::from_i128(i128::MAX))];
3592        let array = create_decimal256_array(array, 76, 5).unwrap();
3593        let result = cast_with_options(
3594            &array,
3595            &output_type,
3596            &CastOptions {
3597                safe: false,
3598                format_options: FormatOptions::default(),
3599            },
3600        );
3601        assert_eq!(
3602            "Cast error: Cannot cast to Decimal128(38, 7). Overflowing on 170141183460469231731687303715884105727",
3603            result.unwrap_err().to_string()
3604        );
3605    }
3606
3607    #[test]
3608    fn test_cast_decimal256_to_decimal256_overflow() {
3609        let input_type = DataType::Decimal256(76, 5);
3610        let output_type = DataType::Decimal256(76, 55);
3611        assert!(can_cast_types(&input_type, &output_type));
3612        let array = vec![Some(i256::from_i128(i128::MAX))];
3613        let array = create_decimal256_array(array, 76, 5).unwrap();
3614        let result = cast_with_options(
3615            &array,
3616            &output_type,
3617            &CastOptions {
3618                safe: false,
3619                format_options: FormatOptions::default(),
3620            },
3621        );
3622        assert_eq!(
3623            "Cast error: Cannot cast to Decimal256(76, 55). Overflowing on 170141183460469231731687303715884105727",
3624            result.unwrap_err().to_string()
3625        );
3626    }
3627
3628    #[test]
3629    fn test_cast_decimal256_to_decimal128() {
3630        let input_type = DataType::Decimal256(20, 3);
3631        let output_type = DataType::Decimal128(20, 4);
3632        assert!(can_cast_types(&input_type, &output_type));
3633        let array = vec![
3634            Some(i256::from_i128(1123456)),
3635            Some(i256::from_i128(2123456)),
3636            Some(i256::from_i128(3123456)),
3637            None,
3638        ];
3639        let array = create_decimal256_array(array, 20, 3).unwrap();
3640        generate_cast_test_case!(
3641            &array,
3642            Decimal128Array,
3643            &output_type,
3644            vec![
3645                Some(11234560_i128),
3646                Some(21234560_i128),
3647                Some(31234560_i128),
3648                None
3649            ]
3650        );
3651    }
3652
3653    #[test]
3654    fn test_cast_decimal256_to_decimal256() {
3655        let input_type = DataType::Decimal256(20, 3);
3656        let output_type = DataType::Decimal256(20, 4);
3657        assert!(can_cast_types(&input_type, &output_type));
3658        let array = vec![
3659            Some(i256::from_i128(1123456)),
3660            Some(i256::from_i128(2123456)),
3661            Some(i256::from_i128(3123456)),
3662            None,
3663        ];
3664        let array = create_decimal256_array(array, 20, 3).unwrap();
3665        generate_cast_test_case!(
3666            &array,
3667            Decimal256Array,
3668            &output_type,
3669            vec![
3670                Some(i256::from_i128(11234560_i128)),
3671                Some(i256::from_i128(21234560_i128)),
3672                Some(i256::from_i128(31234560_i128)),
3673                None
3674            ]
3675        );
3676    }
3677
3678    fn generate_decimal_to_numeric_cast_test_case<T>(array: &PrimitiveArray<T>)
3679    where
3680        T: ArrowPrimitiveType + DecimalType,
3681    {
3682        // u8
3683        generate_cast_test_case!(
3684            array,
3685            UInt8Array,
3686            &DataType::UInt8,
3687            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3688        );
3689        // u16
3690        generate_cast_test_case!(
3691            array,
3692            UInt16Array,
3693            &DataType::UInt16,
3694            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3695        );
3696        // u32
3697        generate_cast_test_case!(
3698            array,
3699            UInt32Array,
3700            &DataType::UInt32,
3701            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3702        );
3703        // u64
3704        generate_cast_test_case!(
3705            array,
3706            UInt64Array,
3707            &DataType::UInt64,
3708            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3709        );
3710        // i8
3711        generate_cast_test_case!(
3712            array,
3713            Int8Array,
3714            &DataType::Int8,
3715            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3716        );
3717        // i16
3718        generate_cast_test_case!(
3719            array,
3720            Int16Array,
3721            &DataType::Int16,
3722            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3723        );
3724        // i32
3725        generate_cast_test_case!(
3726            array,
3727            Int32Array,
3728            &DataType::Int32,
3729            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3730        );
3731        // i64
3732        generate_cast_test_case!(
3733            array,
3734            Int64Array,
3735            &DataType::Int64,
3736            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3737        );
3738        // f16
3739        generate_cast_test_case!(
3740            array,
3741            Float16Array,
3742            &DataType::Float16,
3743            vec![
3744                Some(f16::from_f32(1.25)),
3745                Some(f16::from_f32(2.25)),
3746                Some(f16::from_f32(3.25)),
3747                None,
3748                Some(f16::from_f32(5.25))
3749            ]
3750        );
3751        // f32
3752        generate_cast_test_case!(
3753            array,
3754            Float32Array,
3755            &DataType::Float32,
3756            vec![
3757                Some(1.25_f32),
3758                Some(2.25_f32),
3759                Some(3.25_f32),
3760                None,
3761                Some(5.25_f32)
3762            ]
3763        );
3764        // f64
3765        generate_cast_test_case!(
3766            array,
3767            Float64Array,
3768            &DataType::Float64,
3769            vec![
3770                Some(1.25_f64),
3771                Some(2.25_f64),
3772                Some(3.25_f64),
3773                None,
3774                Some(5.25_f64)
3775            ]
3776        );
3777    }
3778
3779    #[test]
3780    #[cfg_attr(miri, ignore)] // Takes too long
3781    fn test_cast_decimal32_to_numeric() {
3782        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3783        let array = create_decimal32_array(value_array, 8, 2).unwrap();
3784
3785        generate_decimal_to_numeric_cast_test_case(&array);
3786    }
3787
3788    #[test]
3789    #[cfg_attr(miri, ignore)] // Takes too long
3790    fn test_cast_decimal64_to_numeric() {
3791        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3792        let array = create_decimal64_array(value_array, 8, 2).unwrap();
3793
3794        generate_decimal_to_numeric_cast_test_case(&array);
3795    }
3796
3797    #[test]
3798    #[cfg_attr(miri, ignore)] // Takes too long
3799    fn test_cast_decimal128_to_numeric() {
3800        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3801        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3802
3803        generate_decimal_to_numeric_cast_test_case(&array);
3804
3805        // overflow test: out of range of max u8
3806        let value_array: Vec<Option<i128>> = vec![Some(51300)];
3807        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3808        let casted_array = cast_with_options(
3809            &array,
3810            &DataType::UInt8,
3811            &CastOptions {
3812                safe: false,
3813                format_options: FormatOptions::default(),
3814            },
3815        );
3816        assert_eq!(
3817            "Cast error: value of 513 is out of range UInt8".to_string(),
3818            casted_array.unwrap_err().to_string()
3819        );
3820
3821        let casted_array = cast_with_options(
3822            &array,
3823            &DataType::UInt8,
3824            &CastOptions {
3825                safe: true,
3826                format_options: FormatOptions::default(),
3827            },
3828        );
3829        assert!(casted_array.is_ok());
3830        assert!(casted_array.unwrap().is_null(0));
3831
3832        // overflow test: out of range of max i8
3833        let value_array: Vec<Option<i128>> = vec![Some(24400)];
3834        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3835        let casted_array = cast_with_options(
3836            &array,
3837            &DataType::Int8,
3838            &CastOptions {
3839                safe: false,
3840                format_options: FormatOptions::default(),
3841            },
3842        );
3843        assert_eq!(
3844            "Cast error: value of 244 is out of range Int8".to_string(),
3845            casted_array.unwrap_err().to_string()
3846        );
3847
3848        let casted_array = cast_with_options(
3849            &array,
3850            &DataType::Int8,
3851            &CastOptions {
3852                safe: true,
3853                format_options: FormatOptions::default(),
3854            },
3855        );
3856        assert!(casted_array.is_ok());
3857        assert!(casted_array.unwrap().is_null(0));
3858
3859        // loss the precision: convert decimal to f32、f64
3860        // f32
3861        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3862        let value_array: Vec<Option<i128>> = vec![
3863            Some(125),
3864            Some(225),
3865            Some(325),
3866            None,
3867            Some(525),
3868            Some(112345678),
3869            Some(112345679),
3870        ];
3871        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3872        generate_cast_test_case!(
3873            &array,
3874            Float32Array,
3875            &DataType::Float32,
3876            vec![
3877                Some(1.25_f32),
3878                Some(2.25_f32),
3879                Some(3.25_f32),
3880                None,
3881                Some(5.25_f32),
3882                Some(1_123_456.7_f32),
3883                Some(1_123_456.7_f32)
3884            ]
3885        );
3886
3887        // f64
3888        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3889        let value_array: Vec<Option<i128>> = vec![
3890            Some(125),
3891            Some(225),
3892            Some(325),
3893            None,
3894            Some(525),
3895            Some(112345678901234568),
3896            Some(112345678901234560),
3897        ];
3898        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3899        generate_cast_test_case!(
3900            &array,
3901            Float64Array,
3902            &DataType::Float64,
3903            vec![
3904                Some(1.25_f64),
3905                Some(2.25_f64),
3906                Some(3.25_f64),
3907                None,
3908                Some(5.25_f64),
3909                Some(1_123_456_789_012_345.6_f64),
3910                Some(1_123_456_789_012_345.6_f64),
3911            ]
3912        );
3913    }
3914
3915    #[test]
3916    #[cfg_attr(miri, ignore)] // Takes too long
3917    fn test_cast_decimal256_to_numeric() {
3918        let value_array: Vec<Option<i256>> = vec![
3919            Some(i256::from_i128(125)),
3920            Some(i256::from_i128(225)),
3921            Some(i256::from_i128(325)),
3922            None,
3923            Some(i256::from_i128(525)),
3924        ];
3925        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3926        // u8
3927        generate_cast_test_case!(
3928            &array,
3929            UInt8Array,
3930            &DataType::UInt8,
3931            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3932        );
3933        // u16
3934        generate_cast_test_case!(
3935            &array,
3936            UInt16Array,
3937            &DataType::UInt16,
3938            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3939        );
3940        // u32
3941        generate_cast_test_case!(
3942            &array,
3943            UInt32Array,
3944            &DataType::UInt32,
3945            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3946        );
3947        // u64
3948        generate_cast_test_case!(
3949            &array,
3950            UInt64Array,
3951            &DataType::UInt64,
3952            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3953        );
3954        // i8
3955        generate_cast_test_case!(
3956            &array,
3957            Int8Array,
3958            &DataType::Int8,
3959            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3960        );
3961        // i16
3962        generate_cast_test_case!(
3963            &array,
3964            Int16Array,
3965            &DataType::Int16,
3966            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3967        );
3968        // i32
3969        generate_cast_test_case!(
3970            &array,
3971            Int32Array,
3972            &DataType::Int32,
3973            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3974        );
3975        // i64
3976        generate_cast_test_case!(
3977            &array,
3978            Int64Array,
3979            &DataType::Int64,
3980            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3981        );
3982        // f16
3983        generate_cast_test_case!(
3984            &array,
3985            Float16Array,
3986            &DataType::Float16,
3987            vec![
3988                Some(f16::from_f32(1.25)),
3989                Some(f16::from_f32(2.25)),
3990                Some(f16::from_f32(3.25)),
3991                None,
3992                Some(f16::from_f32(5.25))
3993            ]
3994        );
3995        // f32
3996        generate_cast_test_case!(
3997            &array,
3998            Float32Array,
3999            &DataType::Float32,
4000            vec![
4001                Some(1.25_f32),
4002                Some(2.25_f32),
4003                Some(3.25_f32),
4004                None,
4005                Some(5.25_f32)
4006            ]
4007        );
4008        // f64
4009        generate_cast_test_case!(
4010            &array,
4011            Float64Array,
4012            &DataType::Float64,
4013            vec![
4014                Some(1.25_f64),
4015                Some(2.25_f64),
4016                Some(3.25_f64),
4017                None,
4018                Some(5.25_f64)
4019            ]
4020        );
4021
4022        // overflow test: out of range of max i8
4023        let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128(24400))];
4024        let array = create_decimal256_array(value_array, 38, 2).unwrap();
4025        let casted_array = cast_with_options(
4026            &array,
4027            &DataType::Int8,
4028            &CastOptions {
4029                safe: false,
4030                format_options: FormatOptions::default(),
4031            },
4032        );
4033        assert_eq!(
4034            "Cast error: value of 244 is out of range Int8".to_string(),
4035            casted_array.unwrap_err().to_string()
4036        );
4037
4038        let casted_array = cast_with_options(
4039            &array,
4040            &DataType::Int8,
4041            &CastOptions {
4042                safe: true,
4043                format_options: FormatOptions::default(),
4044            },
4045        );
4046        assert!(casted_array.is_ok());
4047        assert!(casted_array.unwrap().is_null(0));
4048
4049        // loss the precision: convert decimal to f32、f64
4050        // f32
4051        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
4052        let value_array: Vec<Option<i256>> = vec![
4053            Some(i256::from_i128(125)),
4054            Some(i256::from_i128(225)),
4055            Some(i256::from_i128(325)),
4056            None,
4057            Some(i256::from_i128(525)),
4058            Some(i256::from_i128(112345678)),
4059            Some(i256::from_i128(112345679)),
4060        ];
4061        let array = create_decimal256_array(value_array, 76, 2).unwrap();
4062        generate_cast_test_case!(
4063            &array,
4064            Float32Array,
4065            &DataType::Float32,
4066            vec![
4067                Some(1.25_f32),
4068                Some(2.25_f32),
4069                Some(3.25_f32),
4070                None,
4071                Some(5.25_f32),
4072                Some(1_123_456.7_f32),
4073                Some(1_123_456.7_f32)
4074            ]
4075        );
4076
4077        // f64
4078        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
4079        let value_array: Vec<Option<i256>> = vec![
4080            Some(i256::from_i128(125)),
4081            Some(i256::from_i128(225)),
4082            Some(i256::from_i128(325)),
4083            None,
4084            Some(i256::from_i128(525)),
4085            Some(i256::from_i128(112345678901234568)),
4086            Some(i256::from_i128(112345678901234560)),
4087        ];
4088        let array = create_decimal256_array(value_array, 76, 2).unwrap();
4089        generate_cast_test_case!(
4090            &array,
4091            Float64Array,
4092            &DataType::Float64,
4093            vec![
4094                Some(1.25_f64),
4095                Some(2.25_f64),
4096                Some(3.25_f64),
4097                None,
4098                Some(5.25_f64),
4099                Some(1_123_456_789_012_345.6_f64),
4100                Some(1_123_456_789_012_345.6_f64),
4101            ]
4102        );
4103    }
4104
4105    #[test]
4106    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
4107    fn test_cast_decimal128_to_float16_overflow() {
4108        let array = create_decimal128_array(
4109            vec![
4110                Some(6_550_400_i128),
4111                Some(100_000_000_i128),
4112                Some(-100_000_000_i128),
4113                None,
4114            ],
4115            10,
4116            2,
4117        )
4118        .unwrap();
4119
4120        generate_cast_test_case!(
4121            &array,
4122            Float16Array,
4123            &DataType::Float16,
4124            vec![
4125                Some(f16::from_f64(65504.0)),
4126                Some(f16::INFINITY),
4127                Some(f16::NEG_INFINITY),
4128                None
4129            ]
4130        );
4131    }
4132
4133    #[test]
4134    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
4135    fn test_cast_decimal256_to_float16_overflow() {
4136        let array = create_decimal256_array(
4137            vec![
4138                Some(i256::from_i128(6_550_400_i128)),
4139                Some(i256::from_i128(100_000_000_i128)),
4140                Some(i256::from_i128(-100_000_000_i128)),
4141                None,
4142            ],
4143            10,
4144            2,
4145        )
4146        .unwrap();
4147
4148        generate_cast_test_case!(
4149            &array,
4150            Float16Array,
4151            &DataType::Float16,
4152            vec![
4153                Some(f16::from_f64(65504.0)),
4154                Some(f16::INFINITY),
4155                Some(f16::NEG_INFINITY),
4156                None
4157            ]
4158        );
4159    }
4160
4161    #[test]
4162    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
4163    fn test_cast_decimal_to_numeric_negative_scale() {
4164        let value_array: Vec<Option<i256>> = vec![
4165            Some(i256::from_i128(125)),
4166            Some(i256::from_i128(225)),
4167            Some(i256::from_i128(325)),
4168            None,
4169            Some(i256::from_i128(525)),
4170        ];
4171        let array = create_decimal256_array(value_array, 38, -1).unwrap();
4172
4173        generate_cast_test_case!(
4174            &array,
4175            Int64Array,
4176            &DataType::Int64,
4177            vec![Some(1_250), Some(2_250), Some(3_250), None, Some(5_250)]
4178        );
4179
4180        let value_array: Vec<Option<i128>> = vec![Some(12), Some(-12), None];
4181        let array = create_decimal128_array(value_array, 10, -2).unwrap();
4182        generate_cast_test_case!(
4183            &array,
4184            Float16Array,
4185            &DataType::Float16,
4186            vec![
4187                Some(f16::from_f32(1200.0)),
4188                Some(f16::from_f32(-1200.0)),
4189                None
4190            ]
4191        );
4192
4193        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4194        let array = create_decimal32_array(value_array, 8, -2).unwrap();
4195        generate_cast_test_case!(
4196            &array,
4197            Int64Array,
4198            &DataType::Int64,
4199            vec![Some(12_500), Some(22_500), Some(32_500), None, Some(52_500)]
4200        );
4201
4202        let value_array: Vec<Option<i32>> = vec![Some(2), Some(1), None];
4203        let array = create_decimal32_array(value_array, 9, -9).unwrap();
4204        generate_cast_test_case!(
4205            &array,
4206            Int64Array,
4207            &DataType::Int64,
4208            vec![Some(2_000_000_000), Some(1_000_000_000), None]
4209        );
4210
4211        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4212        let array = create_decimal64_array(value_array, 18, -3).unwrap();
4213        generate_cast_test_case!(
4214            &array,
4215            Int64Array,
4216            &DataType::Int64,
4217            vec![
4218                Some(125_000),
4219                Some(225_000),
4220                Some(325_000),
4221                None,
4222                Some(525_000)
4223            ]
4224        );
4225
4226        let value_array: Vec<Option<i64>> = vec![Some(12), Some(34), None];
4227        let array = create_decimal64_array(value_array, 18, -10).unwrap();
4228        generate_cast_test_case!(
4229            &array,
4230            Int64Array,
4231            &DataType::Int64,
4232            vec![Some(120_000_000_000), Some(340_000_000_000), None]
4233        );
4234
4235        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4236        let array = create_decimal128_array(value_array, 38, -4).unwrap();
4237        generate_cast_test_case!(
4238            &array,
4239            Int64Array,
4240            &DataType::Int64,
4241            vec![
4242                Some(1_250_000),
4243                Some(2_250_000),
4244                Some(3_250_000),
4245                None,
4246                Some(5_250_000)
4247            ]
4248        );
4249
4250        let value_array: Vec<Option<i128>> = vec![Some(9), Some(1), None];
4251        let array = create_decimal128_array(value_array, 38, -18).unwrap();
4252        generate_cast_test_case!(
4253            &array,
4254            Int64Array,
4255            &DataType::Int64,
4256            vec![
4257                Some(9_000_000_000_000_000_000),
4258                Some(1_000_000_000_000_000_000),
4259                None
4260            ]
4261        );
4262
4263        let array = create_decimal32_array(vec![Some(999_999_999)], 9, -1).unwrap();
4264        let casted_array = cast_with_options(
4265            &array,
4266            &DataType::Int64,
4267            &CastOptions {
4268                safe: false,
4269                format_options: FormatOptions::default(),
4270            },
4271        );
4272        assert_eq!(
4273            "Arithmetic overflow: Overflow happened on: 999999999 * 10".to_string(),
4274            casted_array.unwrap_err().to_string()
4275        );
4276
4277        let casted_array = cast_with_options(
4278            &array,
4279            &DataType::Int64,
4280            &CastOptions {
4281                safe: true,
4282                format_options: FormatOptions::default(),
4283            },
4284        );
4285        assert!(casted_array.is_ok());
4286        assert!(casted_array.unwrap().is_null(0));
4287
4288        let array = create_decimal64_array(vec![Some(13)], 18, -1).unwrap();
4289        let casted_array = cast_with_options(
4290            &array,
4291            &DataType::Int8,
4292            &CastOptions {
4293                safe: false,
4294                format_options: FormatOptions::default(),
4295            },
4296        );
4297        assert_eq!(
4298            "Cast error: value of 130 is out of range Int8".to_string(),
4299            casted_array.unwrap_err().to_string()
4300        );
4301
4302        let casted_array = cast_with_options(
4303            &array,
4304            &DataType::Int8,
4305            &CastOptions {
4306                safe: true,
4307                format_options: FormatOptions::default(),
4308            },
4309        );
4310        assert!(casted_array.is_ok());
4311        assert!(casted_array.unwrap().is_null(0));
4312    }
4313
4314    #[test]
4315    fn test_cast_numeric_to_decimal128() {
4316        let decimal_type = DataType::Decimal128(38, 6);
4317        // u8, u16, u32, u64
4318        let input_datas = vec![
4319            Arc::new(UInt8Array::from(vec![
4320                Some(1),
4321                Some(2),
4322                Some(3),
4323                None,
4324                Some(5),
4325            ])) as ArrayRef, // u8
4326            Arc::new(UInt16Array::from(vec![
4327                Some(1),
4328                Some(2),
4329                Some(3),
4330                None,
4331                Some(5),
4332            ])) as ArrayRef, // u16
4333            Arc::new(UInt32Array::from(vec![
4334                Some(1),
4335                Some(2),
4336                Some(3),
4337                None,
4338                Some(5),
4339            ])) as ArrayRef, // u32
4340            Arc::new(UInt64Array::from(vec![
4341                Some(1),
4342                Some(2),
4343                Some(3),
4344                None,
4345                Some(5),
4346            ])) as ArrayRef, // u64
4347        ];
4348
4349        for array in input_datas {
4350            generate_cast_test_case!(
4351                &array,
4352                Decimal128Array,
4353                &decimal_type,
4354                vec![
4355                    Some(1000000_i128),
4356                    Some(2000000_i128),
4357                    Some(3000000_i128),
4358                    None,
4359                    Some(5000000_i128)
4360                ]
4361            );
4362        }
4363
4364        // i8, i16, i32, i64
4365        let input_datas = vec![
4366            Arc::new(Int8Array::from(vec![
4367                Some(1),
4368                Some(2),
4369                Some(3),
4370                None,
4371                Some(5),
4372            ])) as ArrayRef, // i8
4373            Arc::new(Int16Array::from(vec![
4374                Some(1),
4375                Some(2),
4376                Some(3),
4377                None,
4378                Some(5),
4379            ])) as ArrayRef, // i16
4380            Arc::new(Int32Array::from(vec![
4381                Some(1),
4382                Some(2),
4383                Some(3),
4384                None,
4385                Some(5),
4386            ])) as ArrayRef, // i32
4387            Arc::new(Int64Array::from(vec![
4388                Some(1),
4389                Some(2),
4390                Some(3),
4391                None,
4392                Some(5),
4393            ])) as ArrayRef, // i64
4394        ];
4395        for array in input_datas {
4396            generate_cast_test_case!(
4397                &array,
4398                Decimal128Array,
4399                &decimal_type,
4400                vec![
4401                    Some(1000000_i128),
4402                    Some(2000000_i128),
4403                    Some(3000000_i128),
4404                    None,
4405                    Some(5000000_i128)
4406                ]
4407            );
4408        }
4409
4410        // test u8 to decimal type with overflow the result type
4411        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4412        let array = UInt8Array::from(vec![1, 2, 3, 4, 100]);
4413        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4414        assert!(casted_array.is_ok());
4415        let array = casted_array.unwrap();
4416        let array: &Decimal128Array = array.as_primitive();
4417        assert!(array.is_null(4));
4418
4419        // test i8 to decimal type with overflow the result type
4420        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4421        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4422        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4423        assert!(casted_array.is_ok());
4424        let array = casted_array.unwrap();
4425        let array: &Decimal128Array = array.as_primitive();
4426        assert!(array.is_null(4));
4427
4428        // test f32 to decimal type
4429        let array = Float32Array::from(vec![
4430            Some(1.1),
4431            Some(2.2),
4432            Some(4.4),
4433            None,
4434            Some(1.123_456_4), // round down
4435            Some(1.123_456_7), // round up
4436        ]);
4437        let array = Arc::new(array) as ArrayRef;
4438        generate_cast_test_case!(
4439            &array,
4440            Decimal128Array,
4441            &decimal_type,
4442            vec![
4443                Some(1100000_i128),
4444                Some(2200000_i128),
4445                Some(4400000_i128),
4446                None,
4447                Some(1123456_i128), // round down
4448                Some(1123457_i128), // round up
4449            ]
4450        );
4451
4452        // test f64 to decimal type
4453        let array = Float64Array::from(vec![
4454            Some(1.1),
4455            Some(2.2),
4456            Some(4.4),
4457            None,
4458            Some(1.123_456_489_123_4),     // round up
4459            Some(1.123_456_789_123_4),     // round up
4460            Some(1.123_456_489_012_345_6), // round down
4461            Some(1.123_456_789_012_345_6), // round up
4462        ]);
4463        generate_cast_test_case!(
4464            &array,
4465            Decimal128Array,
4466            &decimal_type,
4467            vec![
4468                Some(1100000_i128),
4469                Some(2200000_i128),
4470                Some(4400000_i128),
4471                None,
4472                Some(1123456_i128), // round down
4473                Some(1123457_i128), // round up
4474                Some(1123456_i128), // round down
4475                Some(1123457_i128), // round up
4476            ]
4477        );
4478    }
4479
4480    #[test]
4481    fn test_cast_numeric_to_decimal256() {
4482        let decimal_type = DataType::Decimal256(76, 6);
4483        // u8, u16, u32, u64
4484        let input_datas = vec![
4485            Arc::new(UInt8Array::from(vec![
4486                Some(1),
4487                Some(2),
4488                Some(3),
4489                None,
4490                Some(5),
4491            ])) as ArrayRef, // u8
4492            Arc::new(UInt16Array::from(vec![
4493                Some(1),
4494                Some(2),
4495                Some(3),
4496                None,
4497                Some(5),
4498            ])) as ArrayRef, // u16
4499            Arc::new(UInt32Array::from(vec![
4500                Some(1),
4501                Some(2),
4502                Some(3),
4503                None,
4504                Some(5),
4505            ])) as ArrayRef, // u32
4506            Arc::new(UInt64Array::from(vec![
4507                Some(1),
4508                Some(2),
4509                Some(3),
4510                None,
4511                Some(5),
4512            ])) as ArrayRef, // u64
4513        ];
4514
4515        for array in input_datas {
4516            generate_cast_test_case!(
4517                &array,
4518                Decimal256Array,
4519                &decimal_type,
4520                vec![
4521                    Some(i256::from_i128(1000000_i128)),
4522                    Some(i256::from_i128(2000000_i128)),
4523                    Some(i256::from_i128(3000000_i128)),
4524                    None,
4525                    Some(i256::from_i128(5000000_i128))
4526                ]
4527            );
4528        }
4529
4530        // i8, i16, i32, i64
4531        let input_datas = vec![
4532            Arc::new(Int8Array::from(vec![
4533                Some(1),
4534                Some(2),
4535                Some(3),
4536                None,
4537                Some(5),
4538            ])) as ArrayRef, // i8
4539            Arc::new(Int16Array::from(vec![
4540                Some(1),
4541                Some(2),
4542                Some(3),
4543                None,
4544                Some(5),
4545            ])) as ArrayRef, // i16
4546            Arc::new(Int32Array::from(vec![
4547                Some(1),
4548                Some(2),
4549                Some(3),
4550                None,
4551                Some(5),
4552            ])) as ArrayRef, // i32
4553            Arc::new(Int64Array::from(vec![
4554                Some(1),
4555                Some(2),
4556                Some(3),
4557                None,
4558                Some(5),
4559            ])) as ArrayRef, // i64
4560        ];
4561        for array in input_datas {
4562            generate_cast_test_case!(
4563                &array,
4564                Decimal256Array,
4565                &decimal_type,
4566                vec![
4567                    Some(i256::from_i128(1000000_i128)),
4568                    Some(i256::from_i128(2000000_i128)),
4569                    Some(i256::from_i128(3000000_i128)),
4570                    None,
4571                    Some(i256::from_i128(5000000_i128))
4572                ]
4573            );
4574        }
4575
4576        // test i8 to decimal type with overflow the result type
4577        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4578        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4579        let array = Arc::new(array) as ArrayRef;
4580        let casted_array = cast(&array, &DataType::Decimal256(3, 1));
4581        assert!(casted_array.is_ok());
4582        let array = casted_array.unwrap();
4583        let array: &Decimal256Array = array.as_primitive();
4584        assert!(array.is_null(4));
4585
4586        // test f32 to decimal type
4587        let array = Float32Array::from(vec![
4588            Some(1.1),
4589            Some(2.2),
4590            Some(4.4),
4591            None,
4592            Some(1.123_456_4), // round down
4593            Some(1.123_456_7), // round up
4594        ]);
4595        generate_cast_test_case!(
4596            &array,
4597            Decimal256Array,
4598            &decimal_type,
4599            vec![
4600                Some(i256::from_i128(1100000_i128)),
4601                Some(i256::from_i128(2200000_i128)),
4602                Some(i256::from_i128(4400000_i128)),
4603                None,
4604                Some(i256::from_i128(1123456_i128)), // round down
4605                Some(i256::from_i128(1123457_i128)), // round up
4606            ]
4607        );
4608
4609        // test f64 to decimal type
4610        let array = Float64Array::from(vec![
4611            Some(1.1),
4612            Some(2.2),
4613            Some(4.4),
4614            None,
4615            Some(1.123_456_489_123_4),     // round down
4616            Some(1.123_456_789_123_4),     // round up
4617            Some(1.123_456_489_012_345_6), // round down
4618            Some(1.123_456_789_012_345_6), // round up
4619        ]);
4620        generate_cast_test_case!(
4621            &array,
4622            Decimal256Array,
4623            &decimal_type,
4624            vec![
4625                Some(i256::from_i128(1100000_i128)),
4626                Some(i256::from_i128(2200000_i128)),
4627                Some(i256::from_i128(4400000_i128)),
4628                None,
4629                Some(i256::from_i128(1123456_i128)), // round down
4630                Some(i256::from_i128(1123457_i128)), // round up
4631                Some(i256::from_i128(1123456_i128)), // round down
4632                Some(i256::from_i128(1123457_i128)), // round up
4633            ]
4634        );
4635    }
4636
4637    #[test]
4638    fn test_cast_i32_to_f64() {
4639        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4640        let b = cast(&array, &DataType::Float64).unwrap();
4641        let c = b.as_primitive::<Float64Type>();
4642        assert_eq!(5.0, c.value(0));
4643        assert_eq!(6.0, c.value(1));
4644        assert_eq!(7.0, c.value(2));
4645        assert_eq!(8.0, c.value(3));
4646        assert_eq!(9.0, c.value(4));
4647    }
4648
4649    #[test]
4650    fn test_cast_i32_to_u8() {
4651        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4652        let b = cast(&array, &DataType::UInt8).unwrap();
4653        let c = b.as_primitive::<UInt8Type>();
4654        assert!(!c.is_valid(0));
4655        assert_eq!(6, c.value(1));
4656        assert!(!c.is_valid(2));
4657        assert_eq!(8, c.value(3));
4658        // overflows return None
4659        assert!(!c.is_valid(4));
4660    }
4661
4662    #[test]
4663    #[should_panic(expected = "Can't cast value -5 to type UInt8")]
4664    fn test_cast_int32_to_u8_with_error() {
4665        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4666        // overflow with the error
4667        let cast_option = CastOptions {
4668            safe: false,
4669            format_options: FormatOptions::default(),
4670        };
4671        let result = cast_with_options(&array, &DataType::UInt8, &cast_option);
4672        assert!(result.is_err());
4673        result.unwrap();
4674    }
4675
4676    #[test]
4677    fn test_cast_i32_to_u8_sliced() {
4678        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4679        assert_eq!(0, array.offset());
4680        let array = array.slice(2, 3);
4681        let b = cast(&array, &DataType::UInt8).unwrap();
4682        assert_eq!(3, b.len());
4683        let c = b.as_primitive::<UInt8Type>();
4684        assert!(!c.is_valid(0));
4685        assert_eq!(8, c.value(1));
4686        // overflows return None
4687        assert!(!c.is_valid(2));
4688    }
4689
4690    #[test]
4691    fn test_cast_i32_to_i32() {
4692        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4693        let b = cast(&array, &DataType::Int32).unwrap();
4694        let c = b.as_primitive::<Int32Type>();
4695        assert_eq!(5, c.value(0));
4696        assert_eq!(6, c.value(1));
4697        assert_eq!(7, c.value(2));
4698        assert_eq!(8, c.value(3));
4699        assert_eq!(9, c.value(4));
4700    }
4701
4702    #[test]
4703    fn test_cast_i32_to_list_i32() {
4704        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4705        let b = cast(
4706            &array,
4707            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4708        )
4709        .unwrap();
4710        assert_eq!(5, b.len());
4711        let arr = b.as_list::<i32>();
4712        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4713        assert_eq!(1, arr.value_length(0));
4714        assert_eq!(1, arr.value_length(1));
4715        assert_eq!(1, arr.value_length(2));
4716        assert_eq!(1, arr.value_length(3));
4717        assert_eq!(1, arr.value_length(4));
4718        let c = arr.values().as_primitive::<Int32Type>();
4719        assert_eq!(5, c.value(0));
4720        assert_eq!(6, c.value(1));
4721        assert_eq!(7, c.value(2));
4722        assert_eq!(8, c.value(3));
4723        assert_eq!(9, c.value(4));
4724    }
4725
4726    #[test]
4727    fn test_cast_i32_to_list_i32_nullable() {
4728        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), Some(9)]);
4729        let b = cast(
4730            &array,
4731            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4732        )
4733        .unwrap();
4734        assert_eq!(5, b.len());
4735        assert_eq!(0, b.null_count());
4736        let arr = b.as_list::<i32>();
4737        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4738        assert_eq!(1, arr.value_length(0));
4739        assert_eq!(1, arr.value_length(1));
4740        assert_eq!(1, arr.value_length(2));
4741        assert_eq!(1, arr.value_length(3));
4742        assert_eq!(1, arr.value_length(4));
4743
4744        let c = arr.values().as_primitive::<Int32Type>();
4745        assert_eq!(1, c.null_count());
4746        assert_eq!(5, c.value(0));
4747        assert!(!c.is_valid(1));
4748        assert_eq!(7, c.value(2));
4749        assert_eq!(8, c.value(3));
4750        assert_eq!(9, c.value(4));
4751    }
4752
4753    #[test]
4754    fn test_cast_i32_to_list_f64_nullable_sliced() {
4755        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), None, Some(10)]);
4756        let array = array.slice(2, 4);
4757        let b = cast(
4758            &array,
4759            &DataType::List(Arc::new(Field::new_list_field(DataType::Float64, true))),
4760        )
4761        .unwrap();
4762        assert_eq!(4, b.len());
4763        assert_eq!(0, b.null_count());
4764        let arr = b.as_list::<i32>();
4765        assert_eq!(&[0, 1, 2, 3, 4], arr.value_offsets());
4766        assert_eq!(1, arr.value_length(0));
4767        assert_eq!(1, arr.value_length(1));
4768        assert_eq!(1, arr.value_length(2));
4769        assert_eq!(1, arr.value_length(3));
4770        let c = arr.values().as_primitive::<Float64Type>();
4771        assert_eq!(1, c.null_count());
4772        assert_eq!(7.0, c.value(0));
4773        assert_eq!(8.0, c.value(1));
4774        assert!(!c.is_valid(2));
4775        assert_eq!(10.0, c.value(3));
4776    }
4777
4778    #[test]
4779    fn test_cast_int_to_utf8view() {
4780        let inputs = vec![
4781            Arc::new(Int8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4782            Arc::new(Int16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4783            Arc::new(Int32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4784            Arc::new(Int64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4785            Arc::new(UInt8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4786            Arc::new(UInt16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4787            Arc::new(UInt32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4788            Arc::new(UInt64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4789        ];
4790        let expected: ArrayRef = Arc::new(StringViewArray::from(vec![
4791            None,
4792            Some("8"),
4793            Some("9"),
4794            Some("10"),
4795        ]));
4796
4797        for array in inputs {
4798            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4799            let arr = cast(&array, &DataType::Utf8View).unwrap();
4800            assert_eq!(expected.as_ref(), arr.as_ref());
4801        }
4802    }
4803
4804    #[test]
4805    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
4806    fn test_cast_float_to_utf8view() {
4807        let inputs = vec![
4808            Arc::new(Float16Array::from(vec![
4809                Some(f16::from_f64(1.5)),
4810                Some(f16::from_f64(2.5)),
4811                None,
4812            ])) as ArrayRef,
4813            Arc::new(Float32Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4814            Arc::new(Float64Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4815        ];
4816
4817        let expected: ArrayRef =
4818            Arc::new(StringViewArray::from(vec![Some("1.5"), Some("2.5"), None]));
4819
4820        for array in inputs {
4821            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4822            let arr = cast(&array, &DataType::Utf8View).unwrap();
4823            assert_eq!(expected.as_ref(), arr.as_ref());
4824        }
4825    }
4826
4827    #[test]
4828    fn test_cast_utf8_to_i32() {
4829        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4830        let b = cast(&array, &DataType::Int32).unwrap();
4831        let c = b.as_primitive::<Int32Type>();
4832        assert_eq!(5, c.value(0));
4833        assert_eq!(6, c.value(1));
4834        assert!(!c.is_valid(2));
4835        assert_eq!(8, c.value(3));
4836        assert!(!c.is_valid(4));
4837    }
4838
4839    #[test]
4840    fn test_cast_utf8view_to_i32() {
4841        let array = StringViewArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4842        let b = cast(&array, &DataType::Int32).unwrap();
4843        let c = b.as_primitive::<Int32Type>();
4844        assert_eq!(5, c.value(0));
4845        assert_eq!(6, c.value(1));
4846        assert!(!c.is_valid(2));
4847        assert_eq!(8, c.value(3));
4848        assert!(!c.is_valid(4));
4849    }
4850
4851    #[test]
4852    fn test_cast_utf8view_to_f32() {
4853        let array = StringViewArray::from(vec!["3", "4.56", "seven", "8.9"]);
4854        let b = cast(&array, &DataType::Float32).unwrap();
4855        let c = b.as_primitive::<Float32Type>();
4856        assert_eq!(3.0, c.value(0));
4857        assert_eq!(4.56, c.value(1));
4858        assert!(!c.is_valid(2));
4859        assert_eq!(8.9, c.value(3));
4860    }
4861
4862    #[test]
4863    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
4864    fn test_cast_string_to_f16() {
4865        let arrays = [
4866            Arc::new(StringViewArray::from(vec!["3", "4.56", "seven", "8.9"])) as ArrayRef,
4867            Arc::new(StringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4868            Arc::new(LargeStringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4869        ];
4870        for array in arrays {
4871            let b = cast(&array, &DataType::Float16).unwrap();
4872            let c = b.as_primitive::<Float16Type>();
4873            assert_eq!(half::f16::from_f32(3.0), c.value(0));
4874            assert_eq!(half::f16::from_f32(4.56), c.value(1));
4875            assert!(!c.is_valid(2));
4876            assert_eq!(half::f16::from_f32(8.9), c.value(3));
4877        }
4878    }
4879
4880    #[test]
4881    fn test_cast_utf8view_to_decimal128() {
4882        let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
4883        let arr = Arc::new(array) as ArrayRef;
4884        generate_cast_test_case!(
4885            &arr,
4886            Decimal128Array,
4887            &DataType::Decimal128(4, 2),
4888            vec![None, Some(400_i128), Some(560_i128), Some(789_i128)]
4889        );
4890    }
4891
4892    #[test]
4893    fn test_cast_with_options_utf8_to_i32() {
4894        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4895        let result = cast_with_options(
4896            &array,
4897            &DataType::Int32,
4898            &CastOptions {
4899                safe: false,
4900                format_options: FormatOptions::default(),
4901            },
4902        );
4903        match result {
4904            Ok(_) => panic!("expected error"),
4905            Err(e) => {
4906                assert!(
4907                    e.to_string()
4908                        .contains("Cast error: Cannot cast string 'seven' to value of Int32 type",),
4909                    "Error: {e}"
4910                )
4911            }
4912        }
4913    }
4914
4915    #[test]
4916    fn test_cast_utf8_to_bool() {
4917        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4918        let casted = cast(&strings, &DataType::Boolean).unwrap();
4919        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4920        assert_eq!(*as_boolean_array(&casted), expected);
4921    }
4922
4923    #[test]
4924    fn test_cast_utf8view_to_bool() {
4925        let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4926        let casted = cast(&strings, &DataType::Boolean).unwrap();
4927        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4928        assert_eq!(*as_boolean_array(&casted), expected);
4929    }
4930
4931    #[test]
4932    fn test_cast_with_options_utf8_to_bool() {
4933        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4934        let casted = cast_with_options(
4935            &strings,
4936            &DataType::Boolean,
4937            &CastOptions {
4938                safe: false,
4939                format_options: FormatOptions::default(),
4940            },
4941        );
4942        match casted {
4943            Ok(_) => panic!("expected error"),
4944            Err(e) => {
4945                assert!(
4946                    e.to_string().contains(
4947                        "Cast error: Cannot cast value 'invalid' to value of Boolean type"
4948                    )
4949                )
4950            }
4951        }
4952    }
4953
4954    #[test]
4955    fn test_cast_bool_to_i32() {
4956        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4957        let b = cast(&array, &DataType::Int32).unwrap();
4958        let c = b.as_primitive::<Int32Type>();
4959        assert_eq!(1, c.value(0));
4960        assert_eq!(0, c.value(1));
4961        assert!(!c.is_valid(2));
4962    }
4963
4964    #[test]
4965    fn test_cast_bool_to_utf8view() {
4966        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4967        let b = cast(&array, &DataType::Utf8View).unwrap();
4968        let c = b.as_any().downcast_ref::<StringViewArray>().unwrap();
4969        assert_eq!("true", c.value(0));
4970        assert_eq!("false", c.value(1));
4971        assert!(!c.is_valid(2));
4972    }
4973
4974    #[test]
4975    fn test_cast_bool_to_utf8() {
4976        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4977        let b = cast(&array, &DataType::Utf8).unwrap();
4978        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
4979        assert_eq!("true", c.value(0));
4980        assert_eq!("false", c.value(1));
4981        assert!(!c.is_valid(2));
4982    }
4983
4984    #[test]
4985    fn test_cast_bool_to_large_utf8() {
4986        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4987        let b = cast(&array, &DataType::LargeUtf8).unwrap();
4988        let c = b.as_any().downcast_ref::<LargeStringArray>().unwrap();
4989        assert_eq!("true", c.value(0));
4990        assert_eq!("false", c.value(1));
4991        assert!(!c.is_valid(2));
4992    }
4993
4994    #[test]
4995    fn test_cast_bool_to_f64() {
4996        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4997        let b = cast(&array, &DataType::Float64).unwrap();
4998        let c = b.as_primitive::<Float64Type>();
4999        assert_eq!(1.0, c.value(0));
5000        assert_eq!(0.0, c.value(1));
5001        assert!(!c.is_valid(2));
5002    }
5003
5004    #[test]
5005    fn test_cast_integer_to_timestamp() {
5006        let array = Int64Array::from(vec![Some(2), Some(10), None]);
5007        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5008
5009        let array = Int8Array::from(vec![Some(2), Some(10), None]);
5010        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5011
5012        assert_eq!(&actual, &expected);
5013
5014        let array = Int16Array::from(vec![Some(2), Some(10), None]);
5015        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5016
5017        assert_eq!(&actual, &expected);
5018
5019        let array = Int32Array::from(vec![Some(2), Some(10), None]);
5020        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5021
5022        assert_eq!(&actual, &expected);
5023
5024        let array = UInt8Array::from(vec![Some(2), Some(10), None]);
5025        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5026
5027        assert_eq!(&actual, &expected);
5028
5029        let array = UInt16Array::from(vec![Some(2), Some(10), None]);
5030        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5031
5032        assert_eq!(&actual, &expected);
5033
5034        let array = UInt32Array::from(vec![Some(2), Some(10), None]);
5035        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5036
5037        assert_eq!(&actual, &expected);
5038
5039        let array = UInt64Array::from(vec![Some(2), Some(10), None]);
5040        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5041
5042        assert_eq!(&actual, &expected);
5043    }
5044
5045    #[test]
5046    fn test_cast_timestamp_to_integer() {
5047        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
5048            .with_timezone("UTC".to_string());
5049        let expected = cast(&array, &DataType::Int64).unwrap();
5050
5051        let actual = cast(&cast(&array, &DataType::Int8).unwrap(), &DataType::Int64).unwrap();
5052        assert_eq!(&actual, &expected);
5053
5054        let actual = cast(&cast(&array, &DataType::Int16).unwrap(), &DataType::Int64).unwrap();
5055        assert_eq!(&actual, &expected);
5056
5057        let actual = cast(&cast(&array, &DataType::Int32).unwrap(), &DataType::Int64).unwrap();
5058        assert_eq!(&actual, &expected);
5059
5060        let actual = cast(&cast(&array, &DataType::UInt8).unwrap(), &DataType::Int64).unwrap();
5061        assert_eq!(&actual, &expected);
5062
5063        let actual = cast(&cast(&array, &DataType::UInt16).unwrap(), &DataType::Int64).unwrap();
5064        assert_eq!(&actual, &expected);
5065
5066        let actual = cast(&cast(&array, &DataType::UInt32).unwrap(), &DataType::Int64).unwrap();
5067        assert_eq!(&actual, &expected);
5068
5069        let actual = cast(&cast(&array, &DataType::UInt64).unwrap(), &DataType::Int64).unwrap();
5070        assert_eq!(&actual, &expected);
5071    }
5072
5073    #[test]
5074    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
5075    fn test_cast_floating_to_timestamp() {
5076        let array = Int64Array::from(vec![Some(2), Some(10), None]);
5077        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5078
5079        let array = Float16Array::from(vec![
5080            Some(f16::from_f32(2.0)),
5081            Some(f16::from_f32(10.6)),
5082            None,
5083        ]);
5084        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5085
5086        assert_eq!(&actual, &expected);
5087
5088        let array = Float32Array::from(vec![Some(2.0), Some(10.6), None]);
5089        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5090
5091        assert_eq!(&actual, &expected);
5092
5093        let array = Float64Array::from(vec![Some(2.1), Some(10.2), None]);
5094        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5095
5096        assert_eq!(&actual, &expected);
5097    }
5098
5099    #[test]
5100    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
5101    fn test_cast_timestamp_to_floating() {
5102        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
5103            .with_timezone("UTC".to_string());
5104        let expected = cast(&array, &DataType::Int64).unwrap();
5105
5106        let actual = cast(&cast(&array, &DataType::Float16).unwrap(), &DataType::Int64).unwrap();
5107        assert_eq!(&actual, &expected);
5108
5109        let actual = cast(&cast(&array, &DataType::Float32).unwrap(), &DataType::Int64).unwrap();
5110        assert_eq!(&actual, &expected);
5111
5112        let actual = cast(&cast(&array, &DataType::Float64).unwrap(), &DataType::Int64).unwrap();
5113        assert_eq!(&actual, &expected);
5114    }
5115
5116    #[test]
5117    fn test_cast_decimal_to_timestamp() {
5118        let array = Int64Array::from(vec![Some(2), Some(10), None]);
5119        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5120
5121        let array = Decimal128Array::from(vec![Some(200), Some(1000), None])
5122            .with_precision_and_scale(4, 2)
5123            .unwrap();
5124        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5125
5126        assert_eq!(&actual, &expected);
5127
5128        let array = Decimal256Array::from(vec![
5129            Some(i256::from_i128(2000)),
5130            Some(i256::from_i128(10000)),
5131            None,
5132        ])
5133        .with_precision_and_scale(5, 3)
5134        .unwrap();
5135        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5136
5137        assert_eq!(&actual, &expected);
5138    }
5139
5140    #[test]
5141    fn test_cast_timestamp_to_decimal() {
5142        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
5143            .with_timezone("UTC".to_string());
5144        let expected = cast(&array, &DataType::Int64).unwrap();
5145
5146        let actual = cast(
5147            &cast(&array, &DataType::Decimal128(5, 2)).unwrap(),
5148            &DataType::Int64,
5149        )
5150        .unwrap();
5151        assert_eq!(&actual, &expected);
5152
5153        let actual = cast(
5154            &cast(&array, &DataType::Decimal256(10, 5)).unwrap(),
5155            &DataType::Int64,
5156        )
5157        .unwrap();
5158        assert_eq!(&actual, &expected);
5159    }
5160
5161    #[test]
5162    fn test_cast_list_i32_to_list_u16() {
5163        let values = vec![
5164            Some(vec![Some(0), Some(0), Some(0)]),
5165            Some(vec![Some(-1), Some(-2), Some(-1)]),
5166            Some(vec![Some(2), Some(100000000)]),
5167        ];
5168        let list_array = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
5169
5170        let target_type = DataType::List(Arc::new(Field::new("item", DataType::UInt16, true)));
5171        assert!(can_cast_types(list_array.data_type(), &target_type));
5172        let cast_array = cast(&list_array, &target_type).unwrap();
5173
5174        // For the ListArray itself, there are no null values (as there were no nulls when they went in)
5175        //
5176        // 3 negative values should get lost when casting to unsigned,
5177        // 1 value should overflow
5178        assert_eq!(0, cast_array.null_count());
5179
5180        // offsets should be the same
5181        let array = cast_array.as_list::<i32>();
5182        assert_eq!(list_array.value_offsets(), array.value_offsets());
5183
5184        assert_eq!(DataType::UInt16, array.value_type());
5185        assert_eq!(3, array.value_length(0));
5186        assert_eq!(3, array.value_length(1));
5187        assert_eq!(2, array.value_length(2));
5188
5189        // expect 4 nulls: negative numbers and overflow
5190        let u16arr = array.values().as_primitive::<UInt16Type>();
5191        assert_eq!(4, u16arr.null_count());
5192
5193        // expect 4 nulls: negative numbers and overflow
5194        let expected: UInt16Array =
5195            vec![Some(0), Some(0), Some(0), None, None, None, Some(2), None]
5196                .into_iter()
5197                .collect();
5198
5199        assert_eq!(u16arr, &expected);
5200    }
5201
5202    #[test]
5203    fn test_cast_list_i32_to_list_timestamp() {
5204        // Construct a value array
5205        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 8, 100000000]).into_data();
5206
5207        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 9]);
5208
5209        // Construct a list array from the above two
5210        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
5211        let list_data = ArrayData::builder(list_data_type)
5212            .len(3)
5213            .add_buffer(value_offsets)
5214            .add_child_data(value_data)
5215            .build()
5216            .unwrap();
5217        let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
5218
5219        let actual = cast(
5220            &list_array,
5221            &DataType::List(Arc::new(Field::new_list_field(
5222                DataType::Timestamp(TimeUnit::Microsecond, None),
5223                true,
5224            ))),
5225        )
5226        .unwrap();
5227
5228        let expected = cast(
5229            &cast(
5230                &list_array,
5231                &DataType::List(Arc::new(Field::new_list_field(DataType::Int64, true))),
5232            )
5233            .unwrap(),
5234            &DataType::List(Arc::new(Field::new_list_field(
5235                DataType::Timestamp(TimeUnit::Microsecond, None),
5236                true,
5237            ))),
5238        )
5239        .unwrap();
5240
5241        assert_eq!(&actual, &expected);
5242    }
5243
5244    #[test]
5245    fn test_cast_date32_to_date64() {
5246        let a = Date32Array::from(vec![10000, 17890]);
5247        let array = Arc::new(a) as ArrayRef;
5248        let b = cast(&array, &DataType::Date64).unwrap();
5249        let c = b.as_primitive::<Date64Type>();
5250        assert_eq!(864000000000, c.value(0));
5251        assert_eq!(1545696000000, c.value(1));
5252    }
5253
5254    #[test]
5255    fn test_cast_date64_to_date32() {
5256        let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5257        let array = Arc::new(a) as ArrayRef;
5258        let b = cast(&array, &DataType::Date32).unwrap();
5259        let c = b.as_primitive::<Date32Type>();
5260        assert_eq!(10000, c.value(0));
5261        assert_eq!(17890, c.value(1));
5262        assert!(c.is_null(2));
5263    }
5264
5265    #[test]
5266    fn test_cast_date64_to_date32_overflow() {
5267        let a = Date64Array::from(vec![i64::MAX]);
5268        let array = Arc::new(a) as ArrayRef;
5269
5270        let b = cast(&array, &DataType::Date32).unwrap();
5271        let c = b.as_primitive::<Date32Type>();
5272        assert!(c.is_null(0));
5273
5274        let options = CastOptions {
5275            safe: false,
5276            ..Default::default()
5277        };
5278        let err = cast_with_options(&array, &DataType::Date32, &options).unwrap_err();
5279        assert!(
5280            err.to_string().contains("Cannot cast Date64 value"),
5281            "{err}"
5282        );
5283    }
5284
5285    #[test]
5286    fn test_cast_string_to_integral_overflow() {
5287        let str = Arc::new(StringArray::from(vec![
5288            Some("123"),
5289            Some("-123"),
5290            Some("86374"),
5291            None,
5292        ])) as ArrayRef;
5293
5294        let options = CastOptions {
5295            safe: true,
5296            format_options: FormatOptions::default(),
5297        };
5298        let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
5299        let expected =
5300            Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
5301        assert_eq!(&res, &expected);
5302    }
5303
5304    #[test]
5305    fn test_cast_string_to_timestamp() {
5306        let a0 = Arc::new(StringViewArray::from(vec![
5307            Some("2020-09-08T12:00:00.123456789+00:00"),
5308            Some("Not a valid date"),
5309            None,
5310        ])) as ArrayRef;
5311        let a1 = Arc::new(StringArray::from(vec![
5312            Some("2020-09-08T12:00:00.123456789+00:00"),
5313            Some("Not a valid date"),
5314            None,
5315        ])) as ArrayRef;
5316        let a2 = Arc::new(LargeStringArray::from(vec![
5317            Some("2020-09-08T12:00:00.123456789+00:00"),
5318            Some("Not a valid date"),
5319            None,
5320        ])) as ArrayRef;
5321        for array in &[a0, a1, a2] {
5322            for time_unit in &[
5323                TimeUnit::Second,
5324                TimeUnit::Millisecond,
5325                TimeUnit::Microsecond,
5326                TimeUnit::Nanosecond,
5327            ] {
5328                let to_type = DataType::Timestamp(*time_unit, None);
5329                let b = cast(array, &to_type).unwrap();
5330
5331                match time_unit {
5332                    TimeUnit::Second => {
5333                        let c = b.as_primitive::<TimestampSecondType>();
5334                        assert_eq!(1599566400, c.value(0));
5335                        assert!(c.is_null(1));
5336                        assert!(c.is_null(2));
5337                    }
5338                    TimeUnit::Millisecond => {
5339                        let c = b
5340                            .as_any()
5341                            .downcast_ref::<TimestampMillisecondArray>()
5342                            .unwrap();
5343                        assert_eq!(1599566400123, c.value(0));
5344                        assert!(c.is_null(1));
5345                        assert!(c.is_null(2));
5346                    }
5347                    TimeUnit::Microsecond => {
5348                        let c = b
5349                            .as_any()
5350                            .downcast_ref::<TimestampMicrosecondArray>()
5351                            .unwrap();
5352                        assert_eq!(1599566400123456, c.value(0));
5353                        assert!(c.is_null(1));
5354                        assert!(c.is_null(2));
5355                    }
5356                    TimeUnit::Nanosecond => {
5357                        let c = b
5358                            .as_any()
5359                            .downcast_ref::<TimestampNanosecondArray>()
5360                            .unwrap();
5361                        assert_eq!(1599566400123456789, c.value(0));
5362                        assert!(c.is_null(1));
5363                        assert!(c.is_null(2));
5364                    }
5365                }
5366
5367                let options = CastOptions {
5368                    safe: false,
5369                    format_options: FormatOptions::default(),
5370                };
5371                let err = cast_with_options(array, &to_type, &options).unwrap_err();
5372                assert_eq!(
5373                    err.to_string(),
5374                    "Parser error: Error parsing timestamp from 'Not a valid date': error parsing date"
5375                );
5376            }
5377        }
5378    }
5379
5380    #[test]
5381    fn test_cast_string_to_timestamp_overflow() {
5382        let array = StringArray::from(vec!["9800-09-08T12:00:00.123456789"]);
5383        let result = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5384        let result = result.as_primitive::<TimestampSecondType>();
5385        assert_eq!(result.values(), &[247112596800]);
5386    }
5387
5388    #[test]
5389    fn test_cast_string_to_date32() {
5390        let a0 = Arc::new(StringViewArray::from(vec![
5391            Some("2018-12-25"),
5392            Some("Not a valid date"),
5393            None,
5394        ])) as ArrayRef;
5395        let a1 = Arc::new(StringArray::from(vec![
5396            Some("2018-12-25"),
5397            Some("Not a valid date"),
5398            None,
5399        ])) as ArrayRef;
5400        let a2 = Arc::new(LargeStringArray::from(vec![
5401            Some("2018-12-25"),
5402            Some("Not a valid date"),
5403            None,
5404        ])) as ArrayRef;
5405        for array in &[a0, a1, a2] {
5406            let to_type = DataType::Date32;
5407            let b = cast(array, &to_type).unwrap();
5408            let c = b.as_primitive::<Date32Type>();
5409            assert_eq!(17890, c.value(0));
5410            assert!(c.is_null(1));
5411            assert!(c.is_null(2));
5412
5413            let options = CastOptions {
5414                safe: false,
5415                format_options: FormatOptions::default(),
5416            };
5417            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5418            assert_eq!(
5419                err.to_string(),
5420                "Cast error: Cannot cast string 'Not a valid date' to value of Date32 type"
5421            );
5422        }
5423    }
5424
5425    #[test]
5426    fn test_cast_string_with_large_date_to_date32() {
5427        let array = Arc::new(StringArray::from(vec![
5428            Some("+10999-12-31"),
5429            Some("-0010-02-28"),
5430            Some("0010-02-28"),
5431            Some("0000-01-01"),
5432            Some("-0000-01-01"),
5433            Some("-0001-01-01"),
5434        ])) as ArrayRef;
5435        let to_type = DataType::Date32;
5436        let options = CastOptions {
5437            safe: false,
5438            format_options: FormatOptions::default(),
5439        };
5440        let b = cast_with_options(&array, &to_type, &options).unwrap();
5441        let c = b.as_primitive::<Date32Type>();
5442        assert_eq!(3298139, c.value(0)); // 10999-12-31
5443        assert_eq!(-723122, c.value(1)); // -0010-02-28
5444        assert_eq!(-715817, c.value(2)); // 0010-02-28
5445        assert_eq!(c.value(3), c.value(4)); // Expect 0000-01-01 and -0000-01-01 to be parsed the same
5446        assert_eq!(-719528, c.value(3)); // 0000-01-01
5447        assert_eq!(-719528, c.value(4)); // -0000-01-01
5448        assert_eq!(-719893, c.value(5)); // -0001-01-01
5449    }
5450
5451    #[test]
5452    fn test_cast_invalid_string_with_large_date_to_date32() {
5453        // Large dates need to be prefixed with a + or - sign, otherwise they are not parsed correctly
5454        let array = Arc::new(StringArray::from(vec![Some("10999-12-31")])) as ArrayRef;
5455        let to_type = DataType::Date32;
5456        let options = CastOptions {
5457            safe: false,
5458            format_options: FormatOptions::default(),
5459        };
5460        let err = cast_with_options(&array, &to_type, &options).unwrap_err();
5461        assert_eq!(
5462            err.to_string(),
5463            "Cast error: Cannot cast string '10999-12-31' to value of Date32 type"
5464        );
5465    }
5466
5467    #[test]
5468    fn test_cast_string_format_yyyymmdd_to_date32() {
5469        let a0 = Arc::new(StringViewArray::from(vec![
5470            Some("2020-12-25"),
5471            Some("20201117"),
5472        ])) as ArrayRef;
5473        let a1 = Arc::new(StringArray::from(vec![
5474            Some("2020-12-25"),
5475            Some("20201117"),
5476        ])) as ArrayRef;
5477        let a2 = Arc::new(LargeStringArray::from(vec![
5478            Some("2020-12-25"),
5479            Some("20201117"),
5480        ])) as ArrayRef;
5481
5482        for array in &[a0, a1, a2] {
5483            let to_type = DataType::Date32;
5484            let options = CastOptions {
5485                safe: false,
5486                format_options: FormatOptions::default(),
5487            };
5488            let result = cast_with_options(&array, &to_type, &options).unwrap();
5489            let c = result.as_primitive::<Date32Type>();
5490            assert_eq!(
5491                chrono::NaiveDate::from_ymd_opt(2020, 12, 25),
5492                c.value_as_date(0)
5493            );
5494            assert_eq!(
5495                chrono::NaiveDate::from_ymd_opt(2020, 11, 17),
5496                c.value_as_date(1)
5497            );
5498        }
5499    }
5500
5501    #[test]
5502    fn test_cast_string_to_time32second() {
5503        let a0 = Arc::new(StringViewArray::from(vec![
5504            Some("08:08:35.091323414"),
5505            Some("08:08:60.091323414"), // leap second
5506            Some("08:08:61.091323414"), // not valid
5507            Some("Not a valid time"),
5508            None,
5509        ])) as ArrayRef;
5510        let a1 = Arc::new(StringArray::from(vec![
5511            Some("08:08:35.091323414"),
5512            Some("08:08:60.091323414"), // leap second
5513            Some("08:08:61.091323414"), // not valid
5514            Some("Not a valid time"),
5515            None,
5516        ])) as ArrayRef;
5517        let a2 = Arc::new(LargeStringArray::from(vec![
5518            Some("08:08:35.091323414"),
5519            Some("08:08:60.091323414"), // leap second
5520            Some("08:08:61.091323414"), // not valid
5521            Some("Not a valid time"),
5522            None,
5523        ])) as ArrayRef;
5524        for array in &[a0, a1, a2] {
5525            let to_type = DataType::Time32(TimeUnit::Second);
5526            let b = cast(array, &to_type).unwrap();
5527            let c = b.as_primitive::<Time32SecondType>();
5528            assert_eq!(29315, c.value(0));
5529            assert_eq!(29340, c.value(1));
5530            assert!(c.is_null(2));
5531            assert!(c.is_null(3));
5532            assert!(c.is_null(4));
5533
5534            let options = CastOptions {
5535                safe: false,
5536                format_options: FormatOptions::default(),
5537            };
5538            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5539            assert_eq!(
5540                err.to_string(),
5541                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(s) type"
5542            );
5543        }
5544    }
5545
5546    #[test]
5547    fn test_cast_string_to_time32millisecond() {
5548        let a0 = Arc::new(StringViewArray::from(vec![
5549            Some("08:08:35.091323414"),
5550            Some("08:08:60.091323414"), // leap second
5551            Some("08:08:61.091323414"), // not valid
5552            Some("Not a valid time"),
5553            None,
5554        ])) as ArrayRef;
5555        let a1 = Arc::new(StringArray::from(vec![
5556            Some("08:08:35.091323414"),
5557            Some("08:08:60.091323414"), // leap second
5558            Some("08:08:61.091323414"), // not valid
5559            Some("Not a valid time"),
5560            None,
5561        ])) as ArrayRef;
5562        let a2 = Arc::new(LargeStringArray::from(vec![
5563            Some("08:08:35.091323414"),
5564            Some("08:08:60.091323414"), // leap second
5565            Some("08:08:61.091323414"), // not valid
5566            Some("Not a valid time"),
5567            None,
5568        ])) as ArrayRef;
5569        for array in &[a0, a1, a2] {
5570            let to_type = DataType::Time32(TimeUnit::Millisecond);
5571            let b = cast(array, &to_type).unwrap();
5572            let c = b.as_primitive::<Time32MillisecondType>();
5573            assert_eq!(29315091, c.value(0));
5574            assert_eq!(29340091, c.value(1));
5575            assert!(c.is_null(2));
5576            assert!(c.is_null(3));
5577            assert!(c.is_null(4));
5578
5579            let options = CastOptions {
5580                safe: false,
5581                format_options: FormatOptions::default(),
5582            };
5583            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5584            assert_eq!(
5585                err.to_string(),
5586                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(ms) type"
5587            );
5588        }
5589    }
5590
5591    #[test]
5592    fn test_cast_string_to_time64microsecond() {
5593        let a0 = Arc::new(StringViewArray::from(vec![
5594            Some("08:08:35.091323414"),
5595            Some("Not a valid time"),
5596            None,
5597        ])) as ArrayRef;
5598        let a1 = Arc::new(StringArray::from(vec![
5599            Some("08:08:35.091323414"),
5600            Some("Not a valid time"),
5601            None,
5602        ])) as ArrayRef;
5603        let a2 = Arc::new(LargeStringArray::from(vec![
5604            Some("08:08:35.091323414"),
5605            Some("Not a valid time"),
5606            None,
5607        ])) as ArrayRef;
5608        for array in &[a0, a1, a2] {
5609            let to_type = DataType::Time64(TimeUnit::Microsecond);
5610            let b = cast(array, &to_type).unwrap();
5611            let c = b.as_primitive::<Time64MicrosecondType>();
5612            assert_eq!(29315091323, c.value(0));
5613            assert!(c.is_null(1));
5614            assert!(c.is_null(2));
5615
5616            let options = CastOptions {
5617                safe: false,
5618                format_options: FormatOptions::default(),
5619            };
5620            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5621            assert_eq!(
5622                err.to_string(),
5623                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(µs) type"
5624            );
5625        }
5626    }
5627
5628    #[test]
5629    fn test_cast_string_to_time64nanosecond() {
5630        let a0 = Arc::new(StringViewArray::from(vec![
5631            Some("08:08:35.091323414"),
5632            Some("Not a valid time"),
5633            None,
5634        ])) as ArrayRef;
5635        let a1 = Arc::new(StringArray::from(vec![
5636            Some("08:08:35.091323414"),
5637            Some("Not a valid time"),
5638            None,
5639        ])) as ArrayRef;
5640        let a2 = Arc::new(LargeStringArray::from(vec![
5641            Some("08:08:35.091323414"),
5642            Some("Not a valid time"),
5643            None,
5644        ])) as ArrayRef;
5645        for array in &[a0, a1, a2] {
5646            let to_type = DataType::Time64(TimeUnit::Nanosecond);
5647            let b = cast(array, &to_type).unwrap();
5648            let c = b.as_primitive::<Time64NanosecondType>();
5649            assert_eq!(29315091323414, c.value(0));
5650            assert!(c.is_null(1));
5651            assert!(c.is_null(2));
5652
5653            let options = CastOptions {
5654                safe: false,
5655                format_options: FormatOptions::default(),
5656            };
5657            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5658            assert_eq!(
5659                err.to_string(),
5660                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(ns) type"
5661            );
5662        }
5663    }
5664
5665    #[test]
5666    fn test_cast_string_to_date64() {
5667        let a0 = Arc::new(StringViewArray::from(vec![
5668            Some("2020-09-08T12:00:00"),
5669            Some("Not a valid date"),
5670            None,
5671        ])) as ArrayRef;
5672        let a1 = Arc::new(StringArray::from(vec![
5673            Some("2020-09-08T12:00:00"),
5674            Some("Not a valid date"),
5675            None,
5676        ])) as ArrayRef;
5677        let a2 = Arc::new(LargeStringArray::from(vec![
5678            Some("2020-09-08T12:00:00"),
5679            Some("Not a valid date"),
5680            None,
5681        ])) as ArrayRef;
5682        for array in &[a0, a1, a2] {
5683            let to_type = DataType::Date64;
5684            let b = cast(array, &to_type).unwrap();
5685            let c = b.as_primitive::<Date64Type>();
5686            assert_eq!(1599566400000, c.value(0));
5687            assert!(c.is_null(1));
5688            assert!(c.is_null(2));
5689
5690            let options = CastOptions {
5691                safe: false,
5692                format_options: FormatOptions::default(),
5693            };
5694            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5695            assert_eq!(
5696                err.to_string(),
5697                "Cast error: Cannot cast string 'Not a valid date' to value of Date64 type"
5698            );
5699        }
5700    }
5701
5702    macro_rules! test_safe_string_to_interval {
5703        ($data_vec:expr, $interval_unit:expr, $array_ty:ty, $expect_vec:expr) => {
5704            let source_string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5705
5706            let options = CastOptions {
5707                safe: true,
5708                format_options: FormatOptions::default(),
5709            };
5710
5711            let target_interval_array = cast_with_options(
5712                &source_string_array.clone(),
5713                &DataType::Interval($interval_unit),
5714                &options,
5715            )
5716            .unwrap()
5717            .as_any()
5718            .downcast_ref::<$array_ty>()
5719            .unwrap()
5720            .clone() as $array_ty;
5721
5722            let target_string_array =
5723                cast_with_options(&target_interval_array, &DataType::Utf8, &options)
5724                    .unwrap()
5725                    .as_any()
5726                    .downcast_ref::<StringArray>()
5727                    .unwrap()
5728                    .clone();
5729
5730            let expect_string_array = StringArray::from($expect_vec);
5731
5732            assert_eq!(target_string_array, expect_string_array);
5733
5734            let target_large_string_array =
5735                cast_with_options(&target_interval_array, &DataType::LargeUtf8, &options)
5736                    .unwrap()
5737                    .as_any()
5738                    .downcast_ref::<LargeStringArray>()
5739                    .unwrap()
5740                    .clone();
5741
5742            let expect_large_string_array = LargeStringArray::from($expect_vec);
5743
5744            assert_eq!(target_large_string_array, expect_large_string_array);
5745        };
5746    }
5747
5748    #[test]
5749    fn test_cast_string_to_interval_year_month() {
5750        test_safe_string_to_interval!(
5751            vec![
5752                Some("1 year 1 month"),
5753                Some("1.5 years 13 month"),
5754                Some("30 days"),
5755                Some("31 days"),
5756                Some("2 months 31 days"),
5757                Some("2 months 31 days 1 second"),
5758                Some("foobar"),
5759            ],
5760            IntervalUnit::YearMonth,
5761            IntervalYearMonthArray,
5762            vec![
5763                Some("1 years 1 mons"),
5764                Some("2 years 7 mons"),
5765                None,
5766                None,
5767                None,
5768                None,
5769                None,
5770            ]
5771        );
5772    }
5773
5774    #[test]
5775    fn test_cast_string_to_interval_day_time() {
5776        test_safe_string_to_interval!(
5777            vec![
5778                Some("1 year 1 month"),
5779                Some("1.5 years 13 month"),
5780                Some("30 days"),
5781                Some("1 day 2 second 3.5 milliseconds"),
5782                Some("foobar"),
5783            ],
5784            IntervalUnit::DayTime,
5785            IntervalDayTimeArray,
5786            vec![
5787                Some("390 days"),
5788                Some("930 days"),
5789                Some("30 days"),
5790                None,
5791                None,
5792            ]
5793        );
5794    }
5795
5796    #[test]
5797    fn test_cast_string_to_interval_month_day_nano() {
5798        test_safe_string_to_interval!(
5799            vec![
5800                Some("1 year 1 month 1 day"),
5801                None,
5802                Some("1.5 years 13 month 35 days 1.4 milliseconds"),
5803                Some("3 days"),
5804                Some("8 seconds"),
5805                None,
5806                Some("1 day 29800 milliseconds"),
5807                Some("3 months 1 second"),
5808                Some("6 minutes 120 second"),
5809                Some("2 years 39 months 9 days 19 hours 1 minute 83 seconds 399222 milliseconds"),
5810                Some("foobar"),
5811            ],
5812            IntervalUnit::MonthDayNano,
5813            IntervalMonthDayNanoArray,
5814            vec![
5815                Some("13 mons 1 days"),
5816                None,
5817                Some("31 mons 35 days 0.001400000 secs"),
5818                Some("3 days"),
5819                Some("8.000000000 secs"),
5820                None,
5821                Some("1 days 29.800000000 secs"),
5822                Some("3 mons 1.000000000 secs"),
5823                Some("8 mins"),
5824                Some("63 mons 9 days 19 hours 9 mins 2.222000000 secs"),
5825                None,
5826            ]
5827        );
5828    }
5829
5830    macro_rules! test_unsafe_string_to_interval_err {
5831        ($data_vec:expr, $interval_unit:expr, $error_msg:expr) => {
5832            let string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5833            let options = CastOptions {
5834                safe: false,
5835                format_options: FormatOptions::default(),
5836            };
5837            let arrow_err = cast_with_options(
5838                &string_array.clone(),
5839                &DataType::Interval($interval_unit),
5840                &options,
5841            )
5842            .unwrap_err();
5843            assert_eq!($error_msg, arrow_err.to_string());
5844        };
5845    }
5846
5847    #[test]
5848    fn test_cast_string_to_interval_err() {
5849        test_unsafe_string_to_interval_err!(
5850            vec![Some("foobar")],
5851            IntervalUnit::YearMonth,
5852            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5853        );
5854        test_unsafe_string_to_interval_err!(
5855            vec![Some("foobar")],
5856            IntervalUnit::DayTime,
5857            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5858        );
5859        test_unsafe_string_to_interval_err!(
5860            vec![Some("foobar")],
5861            IntervalUnit::MonthDayNano,
5862            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5863        );
5864        test_unsafe_string_to_interval_err!(
5865            vec![Some("2 months 31 days 1 second")],
5866            IntervalUnit::YearMonth,
5867            "Cast error: Cannot cast 2 months 31 days 1 second to IntervalYearMonth. Only year and month fields are allowed."
5868        );
5869        test_unsafe_string_to_interval_err!(
5870            vec![Some("1 day 1.5 milliseconds")],
5871            IntervalUnit::DayTime,
5872            "Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"
5873        );
5874
5875        // overflow
5876        test_unsafe_string_to_interval_err!(
5877            vec![Some(format!(
5878                "{} century {} year {} month",
5879                i64::MAX - 2,
5880                i64::MAX - 2,
5881                i64::MAX - 2
5882            ))],
5883            IntervalUnit::DayTime,
5884            format!(
5885                "Arithmetic overflow: Overflow happened on: {} * 100",
5886                i64::MAX - 2
5887            )
5888        );
5889        test_unsafe_string_to_interval_err!(
5890            vec![Some(format!(
5891                "{} year {} month {} day",
5892                i64::MAX - 2,
5893                i64::MAX - 2,
5894                i64::MAX - 2
5895            ))],
5896            IntervalUnit::MonthDayNano,
5897            format!(
5898                "Arithmetic overflow: Overflow happened on: {} * 12",
5899                i64::MAX - 2
5900            )
5901        );
5902    }
5903
5904    #[test]
5905    fn test_cast_binary_to_fixed_size_binary() {
5906        let bytes_1 = "Hiiii".as_bytes();
5907        let bytes_2 = "Hello".as_bytes();
5908
5909        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5910        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5911        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5912
5913        let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
5914        let down_cast = array_ref
5915            .as_any()
5916            .downcast_ref::<FixedSizeBinaryArray>()
5917            .unwrap();
5918        assert_eq!(bytes_1, down_cast.value(0));
5919        assert_eq!(bytes_2, down_cast.value(1));
5920        assert!(down_cast.is_null(2));
5921
5922        let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
5923        let down_cast = array_ref
5924            .as_any()
5925            .downcast_ref::<FixedSizeBinaryArray>()
5926            .unwrap();
5927        assert_eq!(bytes_1, down_cast.value(0));
5928        assert_eq!(bytes_2, down_cast.value(1));
5929        assert!(down_cast.is_null(2));
5930
5931        // test error cases when the length of binary are not same
5932        let bytes_1 = "Hi".as_bytes();
5933        let bytes_2 = "Hello".as_bytes();
5934
5935        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5936        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5937        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5938
5939        let array_ref = cast_with_options(
5940            &a1,
5941            &DataType::FixedSizeBinary(5),
5942            &CastOptions {
5943                safe: false,
5944                format_options: FormatOptions::default(),
5945            },
5946        );
5947        assert!(array_ref.is_err());
5948
5949        let array_ref = cast_with_options(
5950            &a2,
5951            &DataType::FixedSizeBinary(5),
5952            &CastOptions {
5953                safe: false,
5954                format_options: FormatOptions::default(),
5955            },
5956        );
5957        assert!(array_ref.is_err());
5958    }
5959
5960    #[test]
5961    fn test_fixed_size_binary_to_binary() {
5962        let bytes_1 = "Hiiii".as_bytes();
5963        let bytes_2 = "Hello".as_bytes();
5964
5965        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5966        let a1 = Arc::new(FixedSizeBinaryArray::try_from(binary_data.clone()).unwrap()) as ArrayRef;
5967
5968        let array_ref = cast(&a1, &DataType::Binary).unwrap();
5969        let down_cast = array_ref.as_binary::<i32>();
5970        assert_eq!(bytes_1, down_cast.value(0));
5971        assert_eq!(bytes_2, down_cast.value(1));
5972        assert!(down_cast.is_null(2));
5973
5974        let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
5975        let down_cast = array_ref.as_binary::<i64>();
5976        assert_eq!(bytes_1, down_cast.value(0));
5977        assert_eq!(bytes_2, down_cast.value(1));
5978        assert!(down_cast.is_null(2));
5979
5980        let array_ref = cast(&a1, &DataType::BinaryView).unwrap();
5981        let down_cast = array_ref.as_binary_view();
5982        assert_eq!(bytes_1, down_cast.value(0));
5983        assert_eq!(bytes_2, down_cast.value(1));
5984        assert!(down_cast.is_null(2));
5985    }
5986
5987    #[test]
5988    fn test_fixed_size_binary_to_dictionary() {
5989        let bytes_1 = "Hiiii".as_bytes();
5990        let bytes_2 = "Hello".as_bytes();
5991
5992        let binary_data = vec![Some(bytes_1), Some(bytes_2), Some(bytes_1), None];
5993        let a1 = Arc::new(FixedSizeBinaryArray::try_from(binary_data.clone()).unwrap()) as ArrayRef;
5994
5995        let cast_type = DataType::Dictionary(
5996            Box::new(DataType::Int8),
5997            Box::new(DataType::FixedSizeBinary(5)),
5998        );
5999        let cast_array = cast(&a1, &cast_type).unwrap();
6000        assert_eq!(cast_array.data_type(), &cast_type);
6001        assert_eq!(
6002            array_to_strings(&cast_array),
6003            vec!["4869696969", "48656c6c6f", "4869696969", "null"]
6004        );
6005        // dictionary should only have two distinct values
6006        let dict_array = cast_array.as_dictionary::<Int8Type>();
6007        assert_eq!(dict_array.values().len(), 2);
6008    }
6009
6010    #[test]
6011    fn test_binary_to_dictionary() {
6012        let mut builder = GenericBinaryBuilder::<i32>::new();
6013        builder.append_value(b"hello");
6014        builder.append_value(b"hiiii");
6015        builder.append_value(b"hiiii"); // duplicate
6016        builder.append_null();
6017        builder.append_value(b"rustt");
6018
6019        let a1 = builder.finish();
6020
6021        let cast_type = DataType::Dictionary(
6022            Box::new(DataType::Int8),
6023            Box::new(DataType::FixedSizeBinary(5)),
6024        );
6025        let cast_array = cast(&a1, &cast_type).unwrap();
6026        assert_eq!(cast_array.data_type(), &cast_type);
6027        assert_eq!(
6028            array_to_strings(&cast_array),
6029            vec![
6030                "68656c6c6f",
6031                "6869696969",
6032                "6869696969",
6033                "null",
6034                "7275737474"
6035            ]
6036        );
6037        // dictionary should only have three distinct values
6038        let dict_array = cast_array.as_dictionary::<Int8Type>();
6039        assert_eq!(dict_array.values().len(), 3);
6040    }
6041
6042    #[test]
6043    fn test_cast_string_array_to_dict_utf8_view() {
6044        let array = StringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
6045
6046        let cast_type =
6047            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6048        assert!(can_cast_types(array.data_type(), &cast_type));
6049        let cast_array = cast(&array, &cast_type).unwrap();
6050        assert_eq!(cast_array.data_type(), &cast_type);
6051
6052        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6053        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6054        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
6055
6056        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6057        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6058        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6059
6060        let keys = dict_array.keys();
6061        assert!(keys.is_null(1));
6062        assert_eq!(keys.value(0), keys.value(3));
6063        assert_ne!(keys.value(0), keys.value(2));
6064    }
6065
6066    #[test]
6067    fn test_cast_string_array_to_dict_utf8_view_null_vs_literal_null() {
6068        let array = StringArray::from(vec![Some("one"), None, Some("null"), Some("one")]);
6069
6070        let cast_type =
6071            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6072        assert!(can_cast_types(array.data_type(), &cast_type));
6073        let cast_array = cast(&array, &cast_type).unwrap();
6074        assert_eq!(cast_array.data_type(), &cast_type);
6075
6076        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6077        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6078        assert_eq!(dict_array.values().len(), 2);
6079
6080        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6081        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6082        assert_eq!(actual, vec![Some("one"), None, Some("null"), Some("one")]);
6083
6084        let keys = dict_array.keys();
6085        assert!(keys.is_null(1));
6086        assert_eq!(keys.value(0), keys.value(3));
6087        assert_ne!(keys.value(0), keys.value(2));
6088    }
6089
6090    #[test]
6091    fn test_cast_string_view_array_to_dict_utf8_view() {
6092        let array = StringViewArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
6093
6094        let cast_type =
6095            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6096        assert!(can_cast_types(array.data_type(), &cast_type));
6097        let cast_array = cast(&array, &cast_type).unwrap();
6098        assert_eq!(cast_array.data_type(), &cast_type);
6099
6100        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6101        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6102        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
6103
6104        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6105        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6106        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6107
6108        let keys = dict_array.keys();
6109        assert!(keys.is_null(1));
6110        assert_eq!(keys.value(0), keys.value(3));
6111        assert_ne!(keys.value(0), keys.value(2));
6112    }
6113
6114    #[test]
6115    fn test_cast_string_view_slice_to_dict_utf8_view() {
6116        let array = StringViewArray::from(vec![
6117            Some("zero"),
6118            Some("one"),
6119            None,
6120            Some("three"),
6121            Some("one"),
6122        ]);
6123        let view = array.slice(1, 4);
6124
6125        let cast_type =
6126            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6127        assert!(can_cast_types(view.data_type(), &cast_type));
6128        let cast_array = cast(&view, &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);
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_binary_array_to_dict_binary_view() {
6147        let mut builder = GenericBinaryBuilder::<i32>::new();
6148        builder.append_value(b"hello");
6149        builder.append_value(b"hiiii");
6150        builder.append_value(b"hiiii"); // duplicate
6151        builder.append_null();
6152        builder.append_value(b"rustt");
6153
6154        let array = builder.finish();
6155
6156        let cast_type =
6157            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6158        assert!(can_cast_types(array.data_type(), &cast_type));
6159        let cast_array = cast(&array, &cast_type).unwrap();
6160        assert_eq!(cast_array.data_type(), &cast_type);
6161
6162        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6163        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6164        assert_eq!(dict_array.values().len(), 3);
6165
6166        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6167        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6168        assert_eq!(
6169            actual,
6170            vec![
6171                Some(b"hello".as_slice()),
6172                Some(b"hiiii".as_slice()),
6173                Some(b"hiiii".as_slice()),
6174                None,
6175                Some(b"rustt".as_slice())
6176            ]
6177        );
6178
6179        let keys = dict_array.keys();
6180        assert!(keys.is_null(3));
6181        assert_eq!(keys.value(1), keys.value(2));
6182        assert_ne!(keys.value(0), keys.value(1));
6183    }
6184
6185    #[test]
6186    fn test_cast_binary_view_array_to_dict_binary_view() {
6187        let view = BinaryViewArray::from_iter([
6188            Some(b"hello".as_slice()),
6189            Some(b"hiiii".as_slice()),
6190            Some(b"hiiii".as_slice()), // duplicate
6191            None,
6192            Some(b"rustt".as_slice()),
6193        ]);
6194
6195        let cast_type =
6196            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6197        assert!(can_cast_types(view.data_type(), &cast_type));
6198        let cast_array = cast(&view, &cast_type).unwrap();
6199        assert_eq!(cast_array.data_type(), &cast_type);
6200
6201        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6202        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6203        assert_eq!(dict_array.values().len(), 3);
6204
6205        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6206        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6207        assert_eq!(
6208            actual,
6209            vec![
6210                Some(b"hello".as_slice()),
6211                Some(b"hiiii".as_slice()),
6212                Some(b"hiiii".as_slice()),
6213                None,
6214                Some(b"rustt".as_slice())
6215            ]
6216        );
6217
6218        let keys = dict_array.keys();
6219        assert!(keys.is_null(3));
6220        assert_eq!(keys.value(1), keys.value(2));
6221        assert_ne!(keys.value(0), keys.value(1));
6222    }
6223
6224    #[test]
6225    fn test_cast_binary_view_slice_to_dict_binary_view() {
6226        let view = BinaryViewArray::from_iter([
6227            Some(b"hello".as_slice()),
6228            Some(b"hiiii".as_slice()),
6229            Some(b"hiiii".as_slice()), // duplicate
6230            None,
6231            Some(b"rustt".as_slice()),
6232        ]);
6233        let sliced = view.slice(1, 4);
6234
6235        let cast_type =
6236            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6237        assert!(can_cast_types(sliced.data_type(), &cast_type));
6238        let cast_array = cast(&sliced, &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(), 2);
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"hiiii".as_slice()),
6251                Some(b"hiiii".as_slice()),
6252                None,
6253                Some(b"rustt".as_slice())
6254            ]
6255        );
6256
6257        let keys = dict_array.keys();
6258        assert!(keys.is_null(2));
6259        assert_eq!(keys.value(0), keys.value(1));
6260        assert_ne!(keys.value(0), keys.value(3));
6261    }
6262
6263    #[test]
6264    fn test_cast_string_array_to_dict_utf8_view_key_overflow_u8() {
6265        let array = StringArray::from_iter_values((0..257).map(|i| format!("v{i}")));
6266
6267        let cast_type =
6268            DataType::Dictionary(Box::new(DataType::UInt8), Box::new(DataType::Utf8View));
6269        assert!(can_cast_types(array.data_type(), &cast_type));
6270        let err = cast(&array, &cast_type).unwrap_err();
6271        assert!(matches!(err, ArrowError::DictionaryKeyOverflowError));
6272    }
6273
6274    #[test]
6275    fn test_cast_large_string_array_to_dict_utf8_view() {
6276        let array = LargeStringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
6277
6278        let cast_type =
6279            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6280        assert!(can_cast_types(array.data_type(), &cast_type));
6281        let cast_array = cast(&array, &cast_type).unwrap();
6282        assert_eq!(cast_array.data_type(), &cast_type);
6283
6284        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6285        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6286        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
6287
6288        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6289        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6290        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6291
6292        let keys = dict_array.keys();
6293        assert!(keys.is_null(1));
6294        assert_eq!(keys.value(0), keys.value(3));
6295        assert_ne!(keys.value(0), keys.value(2));
6296    }
6297
6298    #[test]
6299    fn test_cast_large_binary_array_to_dict_binary_view() {
6300        let mut builder = GenericBinaryBuilder::<i64>::new();
6301        builder.append_value(b"hello");
6302        builder.append_value(b"world");
6303        builder.append_value(b"hello"); // duplicate
6304        builder.append_null();
6305
6306        let array = builder.finish();
6307
6308        let cast_type =
6309            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6310        assert!(can_cast_types(array.data_type(), &cast_type));
6311        let cast_array = cast(&array, &cast_type).unwrap();
6312        assert_eq!(cast_array.data_type(), &cast_type);
6313
6314        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6315        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6316        assert_eq!(dict_array.values().len(), 2); // "hello" and "world" deduplicated
6317
6318        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6319        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6320        assert_eq!(
6321            actual,
6322            vec![
6323                Some(b"hello".as_slice()),
6324                Some(b"world".as_slice()),
6325                Some(b"hello".as_slice()),
6326                None
6327            ]
6328        );
6329
6330        let keys = dict_array.keys();
6331        assert!(keys.is_null(3));
6332        assert_eq!(keys.value(0), keys.value(2));
6333        assert_ne!(keys.value(0), keys.value(1));
6334    }
6335
6336    #[test]
6337    fn test_cast_struct_array_to_dict_struct() {
6338        // Cast a StructArray into Dictionary<UInt32, Struct{…}>. The dictionary
6339        // value type's child fields may differ from the source's (here:
6340        // Utf8 source → Utf8View child for `name`), so the per-field cast
6341        // must run before identity keys are emitted. This is the "as long as
6342        // the struct can be cast to the dict value" contract.
6343        let names = StringArray::from(vec![Some("alpha"), None, Some("gamma")]);
6344        let ids = Int32Array::from(vec![Some(1), Some(2), Some(3)]);
6345        let source = StructArray::from(vec![
6346            (
6347                Arc::new(Field::new("name", DataType::Utf8, true)),
6348                Arc::new(names) as ArrayRef,
6349            ),
6350            (
6351                Arc::new(Field::new("id", DataType::Int32, false)),
6352                Arc::new(ids) as ArrayRef,
6353            ),
6354        ]);
6355
6356        let target_value_type = DataType::Struct(
6357            vec![
6358                Field::new("name", DataType::Utf8View, true),
6359                Field::new("id", DataType::Int64, false),
6360            ]
6361            .into(),
6362        );
6363        let cast_type = DataType::Dictionary(
6364            Box::new(DataType::UInt32),
6365            Box::new(target_value_type.clone()),
6366        );
6367        assert!(can_cast_types(source.data_type(), &cast_type));
6368
6369        let cast_array = cast(&source, &cast_type).unwrap();
6370        assert_eq!(cast_array.data_type(), &cast_type);
6371        assert_eq!(cast_array.len(), 3);
6372
6373        let dict = cast_array.as_dictionary::<UInt32Type>();
6374        assert_eq!(dict.values().data_type(), &target_value_type);
6375        // No dedup is performed for struct values — one row, one key.
6376        assert_eq!(dict.values().len(), 3);
6377
6378        // Source row 1 was a `Utf8`-null in the `name` field but the whole
6379        // struct row was valid (StructArray::from above takes per-field
6380        // nulls only). The dictionary's logical null mask therefore mirrors
6381        // the source struct's row-level null mask — all rows valid here.
6382        let keys = dict.keys();
6383        assert_eq!(keys.values(), &[0u32, 1, 2]);
6384        assert_eq!(keys.null_count(), 0);
6385
6386        let struct_values = dict.values().as_struct();
6387        let names_out = struct_values
6388            .column_by_name("name")
6389            .unwrap()
6390            .as_string_view();
6391        assert_eq!(names_out.value(0), "alpha");
6392        assert!(names_out.is_null(1));
6393        assert_eq!(names_out.value(2), "gamma");
6394        let ids_out = struct_values
6395            .column_by_name("id")
6396            .unwrap()
6397            .as_primitive::<Int64Type>();
6398        assert_eq!(ids_out.values(), &[1i64, 2, 3]);
6399    }
6400
6401    #[test]
6402    fn test_cast_struct_array_to_dict_struct_row_nulls() {
6403        // Row-level nulls on the source struct must surface as null keys on
6404        // the dictionary, since the dictionary's logical null mask is
6405        // determined by the keys.
6406        let names = StringArray::from(vec![Some("alpha"), Some("beta"), Some("gamma")]);
6407        let ids = Int32Array::from(vec![Some(1), Some(2), Some(3)]);
6408        let source = StructArray::try_new(
6409            vec![
6410                Field::new("name", DataType::Utf8, true),
6411                Field::new("id", DataType::Int32, false),
6412            ]
6413            .into(),
6414            vec![Arc::new(names) as ArrayRef, Arc::new(ids) as ArrayRef],
6415            Some(NullBuffer::from(vec![true, false, true])),
6416        )
6417        .unwrap();
6418
6419        let target_value_type = DataType::Struct(
6420            vec![
6421                Field::new("name", DataType::Utf8, true),
6422                Field::new("id", DataType::Int32, false),
6423            ]
6424            .into(),
6425        );
6426        let cast_type =
6427            DataType::Dictionary(Box::new(DataType::UInt32), Box::new(target_value_type));
6428
6429        let cast_array = cast(&source, &cast_type).unwrap();
6430        let dict = cast_array.as_dictionary::<UInt32Type>();
6431        assert_eq!(dict.len(), 3);
6432        let keys = dict.keys();
6433        assert!(!keys.is_null(0));
6434        assert!(keys.is_null(1));
6435        assert!(!keys.is_null(2));
6436    }
6437
6438    #[test]
6439    fn test_cast_struct_array_to_dict_struct_key_overflow() {
6440        // Source has 300 rows but the dictionary key type is UInt8 (max 255).
6441        // We must return a CastError instead of silently truncating.
6442        let n = 300;
6443        let names = StringArray::from((0..n).map(|i| Some(format!("v{i}"))).collect::<Vec<_>>());
6444        let source = StructArray::from(vec![(
6445            Arc::new(Field::new("name", DataType::Utf8, true)),
6446            Arc::new(names) as ArrayRef,
6447        )]);
6448
6449        let cast_type = DataType::Dictionary(
6450            Box::new(DataType::UInt8),
6451            Box::new(DataType::Struct(
6452                vec![Field::new("name", DataType::Utf8, true)].into(),
6453            )),
6454        );
6455        let err = cast(&source, &cast_type).unwrap_err().to_string();
6456        assert!(
6457            err.contains("Cannot fit") && err.contains("dictionary keys"),
6458            "expected key-overflow error, got: {err}"
6459        );
6460    }
6461
6462    #[test]
6463    fn test_cast_empty_string_array_to_dict_utf8_view() {
6464        let array = StringArray::from(Vec::<Option<&str>>::new());
6465
6466        let cast_type =
6467            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6468        assert!(can_cast_types(array.data_type(), &cast_type));
6469        let cast_array = cast(&array, &cast_type).unwrap();
6470        assert_eq!(cast_array.data_type(), &cast_type);
6471        assert_eq!(cast_array.len(), 0);
6472    }
6473
6474    #[test]
6475    fn test_cast_empty_binary_array_to_dict_binary_view() {
6476        let array = BinaryArray::from(Vec::<Option<&[u8]>>::new());
6477
6478        let cast_type =
6479            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6480        assert!(can_cast_types(array.data_type(), &cast_type));
6481        let cast_array = cast(&array, &cast_type).unwrap();
6482        assert_eq!(cast_array.data_type(), &cast_type);
6483        assert_eq!(cast_array.len(), 0);
6484    }
6485
6486    #[test]
6487    fn test_cast_all_null_string_array_to_dict_utf8_view() {
6488        let array = StringArray::from(vec![None::<&str>, None, None]);
6489
6490        let cast_type =
6491            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6492        assert!(can_cast_types(array.data_type(), &cast_type));
6493        let cast_array = cast(&array, &cast_type).unwrap();
6494        assert_eq!(cast_array.data_type(), &cast_type);
6495        assert_eq!(cast_array.null_count(), 3);
6496
6497        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6498        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6499        assert_eq!(dict_array.values().len(), 0);
6500        assert_eq!(dict_array.keys().null_count(), 3);
6501
6502        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6503        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6504        assert_eq!(actual, vec![None, None, None]);
6505    }
6506
6507    #[test]
6508    fn test_cast_all_null_binary_array_to_dict_binary_view() {
6509        let array = BinaryArray::from(vec![None::<&[u8]>, None, None]);
6510
6511        let cast_type =
6512            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6513        assert!(can_cast_types(array.data_type(), &cast_type));
6514        let cast_array = cast(&array, &cast_type).unwrap();
6515        assert_eq!(cast_array.data_type(), &cast_type);
6516        assert_eq!(cast_array.null_count(), 3);
6517
6518        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6519        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6520        assert_eq!(dict_array.values().len(), 0);
6521        assert_eq!(dict_array.keys().null_count(), 3);
6522
6523        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6524        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6525        assert_eq!(actual, vec![None, None, None]);
6526    }
6527
6528    #[test]
6529    fn test_numeric_to_binary() {
6530        let a = Int16Array::from(vec![Some(1), Some(511), None]);
6531
6532        let array_ref = cast(&a, &DataType::Binary).unwrap();
6533        let down_cast = array_ref.as_binary::<i32>();
6534        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6535        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6536        assert!(down_cast.is_null(2));
6537
6538        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6539
6540        let array_ref = cast(&a, &DataType::Binary).unwrap();
6541        let down_cast = array_ref.as_binary::<i32>();
6542        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6543        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6544        assert!(down_cast.is_null(2));
6545    }
6546
6547    #[test]
6548    fn test_numeric_to_large_binary() {
6549        let a = Int16Array::from(vec![Some(1), Some(511), None]);
6550
6551        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6552        let down_cast = array_ref.as_binary::<i64>();
6553        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6554        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6555        assert!(down_cast.is_null(2));
6556
6557        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6558
6559        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6560        let down_cast = array_ref.as_binary::<i64>();
6561        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6562        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6563        assert!(down_cast.is_null(2));
6564    }
6565
6566    #[test]
6567    fn test_cast_date32_to_int32() {
6568        let array = Date32Array::from(vec![10000, 17890]);
6569        let b = cast(&array, &DataType::Int32).unwrap();
6570        let c = b.as_primitive::<Int32Type>();
6571        assert_eq!(10000, c.value(0));
6572        assert_eq!(17890, c.value(1));
6573    }
6574
6575    #[test]
6576    fn test_cast_int32_to_date32() {
6577        let array = Int32Array::from(vec![10000, 17890]);
6578        let b = cast(&array, &DataType::Date32).unwrap();
6579        let c = b.as_primitive::<Date32Type>();
6580        assert_eq!(10000, c.value(0));
6581        assert_eq!(17890, c.value(1));
6582    }
6583
6584    #[test]
6585    fn test_cast_timestamp_to_date32() {
6586        let array =
6587            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6588                .with_timezone("+00:00".to_string());
6589        let b = cast(&array, &DataType::Date32).unwrap();
6590        let c = b.as_primitive::<Date32Type>();
6591        assert_eq!(10000, c.value(0));
6592        assert_eq!(17890, c.value(1));
6593        assert!(c.is_null(2));
6594    }
6595    #[test]
6596    fn test_cast_timestamp_to_date32_zone() {
6597        let strings = StringArray::from_iter([
6598            Some("1970-01-01T00:00:01"),
6599            Some("1970-01-01T23:59:59"),
6600            None,
6601            Some("2020-03-01T02:00:23+00:00"),
6602        ]);
6603        let dt = DataType::Timestamp(TimeUnit::Millisecond, Some("-07:00".into()));
6604        let timestamps = cast(&strings, &dt).unwrap();
6605        let dates = cast(timestamps.as_ref(), &DataType::Date32).unwrap();
6606
6607        let c = dates.as_primitive::<Date32Type>();
6608        let expected = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
6609        assert_eq!(c.value_as_date(0).unwrap(), expected);
6610        assert_eq!(c.value_as_date(1).unwrap(), expected);
6611        assert!(c.is_null(2));
6612        let expected = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
6613        assert_eq!(c.value_as_date(3).unwrap(), expected);
6614    }
6615    #[test]
6616    fn test_cast_timestamp_to_date64() {
6617        let array =
6618            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None]);
6619        let b = cast(&array, &DataType::Date64).unwrap();
6620        let c = b.as_primitive::<Date64Type>();
6621        assert_eq!(864000000005, c.value(0));
6622        assert_eq!(1545696000001, c.value(1));
6623        assert!(c.is_null(2));
6624
6625        let array = TimestampSecondArray::from(vec![Some(864000000005), Some(1545696000001)]);
6626        let b = cast(&array, &DataType::Date64).unwrap();
6627        let c = b.as_primitive::<Date64Type>();
6628        assert_eq!(864000000005000, c.value(0));
6629        assert_eq!(1545696000001000, c.value(1));
6630
6631        // test overflow, safe cast
6632        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6633        let b = cast(&array, &DataType::Date64).unwrap();
6634        assert!(b.is_null(0));
6635        // test overflow, unsafe cast
6636        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6637        let options = CastOptions {
6638            safe: false,
6639            format_options: FormatOptions::default(),
6640        };
6641        let b = cast_with_options(&array, &DataType::Date64, &options);
6642        assert!(b.is_err());
6643    }
6644
6645    #[test]
6646    fn test_cast_timestamp_to_time64() {
6647        // test timestamp secs
6648        let array = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6649            .with_timezone("+01:00".to_string());
6650        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6651        let c = b.as_primitive::<Time64MicrosecondType>();
6652        assert_eq!(3605000000, c.value(0));
6653        assert_eq!(3601000000, c.value(1));
6654        assert!(c.is_null(2));
6655        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6656        let c = b.as_primitive::<Time64NanosecondType>();
6657        assert_eq!(3605000000000, c.value(0));
6658        assert_eq!(3601000000000, c.value(1));
6659        assert!(c.is_null(2));
6660
6661        // test timestamp milliseconds
6662        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6663            .with_timezone("+01:00".to_string());
6664        let array = Arc::new(a) as ArrayRef;
6665        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6666        let c = b.as_primitive::<Time64MicrosecondType>();
6667        assert_eq!(3605000000, c.value(0));
6668        assert_eq!(3601000000, c.value(1));
6669        assert!(c.is_null(2));
6670        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6671        let c = b.as_primitive::<Time64NanosecondType>();
6672        assert_eq!(3605000000000, c.value(0));
6673        assert_eq!(3601000000000, c.value(1));
6674        assert!(c.is_null(2));
6675
6676        // test timestamp microseconds
6677        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6678            .with_timezone("+01:00".to_string());
6679        let array = Arc::new(a) as ArrayRef;
6680        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6681        let c = b.as_primitive::<Time64MicrosecondType>();
6682        assert_eq!(3605000000, c.value(0));
6683        assert_eq!(3601000000, c.value(1));
6684        assert!(c.is_null(2));
6685        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6686        let c = b.as_primitive::<Time64NanosecondType>();
6687        assert_eq!(3605000000000, c.value(0));
6688        assert_eq!(3601000000000, c.value(1));
6689        assert!(c.is_null(2));
6690
6691        // test timestamp nanoseconds
6692        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
6693            .with_timezone("+01:00".to_string());
6694        let array = Arc::new(a) as ArrayRef;
6695        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6696        let c = b.as_primitive::<Time64MicrosecondType>();
6697        assert_eq!(3605000000, c.value(0));
6698        assert_eq!(3601000000, c.value(1));
6699        assert!(c.is_null(2));
6700        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6701        let c = b.as_primitive::<Time64NanosecondType>();
6702        assert_eq!(3605000000000, c.value(0));
6703        assert_eq!(3601000000000, c.value(1));
6704        assert!(c.is_null(2));
6705
6706        // test overflow
6707        let a =
6708            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6709        let array = Arc::new(a) as ArrayRef;
6710        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond));
6711        assert!(b.is_err());
6712        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond));
6713        assert!(b.is_err());
6714        let b = cast(&array, &DataType::Time64(TimeUnit::Millisecond));
6715        assert!(b.is_err());
6716    }
6717
6718    #[test]
6719    fn test_cast_timestamp_to_time32() {
6720        // test timestamp secs
6721        let a = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6722            .with_timezone("+01:00".to_string());
6723        let array = Arc::new(a) as ArrayRef;
6724        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6725        let c = b.as_primitive::<Time32SecondType>();
6726        assert_eq!(3605, c.value(0));
6727        assert_eq!(3601, c.value(1));
6728        assert!(c.is_null(2));
6729        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6730        let c = b.as_primitive::<Time32MillisecondType>();
6731        assert_eq!(3605000, c.value(0));
6732        assert_eq!(3601000, c.value(1));
6733        assert!(c.is_null(2));
6734
6735        // test timestamp milliseconds
6736        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6737            .with_timezone("+01:00".to_string());
6738        let array = Arc::new(a) as ArrayRef;
6739        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6740        let c = b.as_primitive::<Time32SecondType>();
6741        assert_eq!(3605, c.value(0));
6742        assert_eq!(3601, c.value(1));
6743        assert!(c.is_null(2));
6744        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6745        let c = b.as_primitive::<Time32MillisecondType>();
6746        assert_eq!(3605000, c.value(0));
6747        assert_eq!(3601000, c.value(1));
6748        assert!(c.is_null(2));
6749
6750        // test timestamp microseconds
6751        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6752            .with_timezone("+01:00".to_string());
6753        let array = Arc::new(a) as ArrayRef;
6754        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6755        let c = b.as_primitive::<Time32SecondType>();
6756        assert_eq!(3605, c.value(0));
6757        assert_eq!(3601, c.value(1));
6758        assert!(c.is_null(2));
6759        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6760        let c = b.as_primitive::<Time32MillisecondType>();
6761        assert_eq!(3605000, c.value(0));
6762        assert_eq!(3601000, c.value(1));
6763        assert!(c.is_null(2));
6764
6765        // test timestamp nanoseconds
6766        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
6767            .with_timezone("+01:00".to_string());
6768        let array = Arc::new(a) as ArrayRef;
6769        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6770        let c = b.as_primitive::<Time32SecondType>();
6771        assert_eq!(3605, c.value(0));
6772        assert_eq!(3601, c.value(1));
6773        assert!(c.is_null(2));
6774        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6775        let c = b.as_primitive::<Time32MillisecondType>();
6776        assert_eq!(3605000, c.value(0));
6777        assert_eq!(3601000, c.value(1));
6778        assert!(c.is_null(2));
6779
6780        // test overflow
6781        let a =
6782            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6783        let array = Arc::new(a) as ArrayRef;
6784        let b = cast(&array, &DataType::Time32(TimeUnit::Second));
6785        assert!(b.is_err());
6786        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond));
6787        assert!(b.is_err());
6788    }
6789
6790    // Cast Timestamp(_, None) -> Timestamp(_, Some(timezone))
6791    #[test]
6792    fn test_cast_timestamp_with_timezone_1() {
6793        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6794            Some("2000-01-01T00:00:00.123456789"),
6795            Some("2010-01-01T00:00:00.123456789"),
6796            None,
6797        ]));
6798        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6799        let timestamp_array = cast(&string_array, &to_type).unwrap();
6800
6801        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6802        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6803
6804        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6805        let result = string_array.as_string::<i32>();
6806        assert_eq!("2000-01-01T00:00:00.123456+07:00", result.value(0));
6807        assert_eq!("2010-01-01T00:00:00.123456+07:00", result.value(1));
6808        assert!(result.is_null(2));
6809    }
6810
6811    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, None)
6812    #[test]
6813    fn test_cast_timestamp_with_timezone_2() {
6814        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6815            Some("2000-01-01T07:00:00.123456789"),
6816            Some("2010-01-01T07:00:00.123456789"),
6817            None,
6818        ]));
6819        let to_type = DataType::Timestamp(TimeUnit::Millisecond, Some("+0700".into()));
6820        let timestamp_array = cast(&string_array, &to_type).unwrap();
6821
6822        // Check intermediate representation is correct
6823        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6824        let result = string_array.as_string::<i32>();
6825        assert_eq!("2000-01-01T07:00:00.123+07:00", result.value(0));
6826        assert_eq!("2010-01-01T07:00:00.123+07:00", result.value(1));
6827        assert!(result.is_null(2));
6828
6829        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6830        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6831
6832        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6833        let result = string_array.as_string::<i32>();
6834        assert_eq!("2000-01-01T00:00:00.123", result.value(0));
6835        assert_eq!("2010-01-01T00:00:00.123", result.value(1));
6836        assert!(result.is_null(2));
6837    }
6838
6839    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, Some(timezone))
6840    #[test]
6841    fn test_cast_timestamp_with_timezone_3() {
6842        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6843            Some("2000-01-01T07:00:00.123456789"),
6844            Some("2010-01-01T07:00:00.123456789"),
6845            None,
6846        ]));
6847        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6848        let timestamp_array = cast(&string_array, &to_type).unwrap();
6849
6850        // Check intermediate representation is correct
6851        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6852        let result = string_array.as_string::<i32>();
6853        assert_eq!("2000-01-01T07:00:00.123456+07:00", result.value(0));
6854        assert_eq!("2010-01-01T07:00:00.123456+07:00", result.value(1));
6855        assert!(result.is_null(2));
6856
6857        let to_type = DataType::Timestamp(TimeUnit::Second, Some("-08:00".into()));
6858        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6859
6860        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6861        let result = string_array.as_string::<i32>();
6862        assert_eq!("1999-12-31T16:00:00-08:00", result.value(0));
6863        assert_eq!("2009-12-31T16:00:00-08:00", result.value(1));
6864        assert!(result.is_null(2));
6865    }
6866
6867    #[test]
6868    fn test_cast_date64_to_timestamp() {
6869        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6870        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6871        let c = b.as_primitive::<TimestampSecondType>();
6872        assert_eq!(864000000, c.value(0));
6873        assert_eq!(1545696000, c.value(1));
6874        assert!(c.is_null(2));
6875    }
6876
6877    #[test]
6878    fn test_cast_date64_to_timestamp_ms() {
6879        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6880        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
6881        let c = b
6882            .as_any()
6883            .downcast_ref::<TimestampMillisecondArray>()
6884            .unwrap();
6885        assert_eq!(864000000005, c.value(0));
6886        assert_eq!(1545696000001, c.value(1));
6887        assert!(c.is_null(2));
6888    }
6889
6890    #[test]
6891    fn test_cast_date64_to_timestamp_us() {
6892        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6893        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
6894        let c = b
6895            .as_any()
6896            .downcast_ref::<TimestampMicrosecondArray>()
6897            .unwrap();
6898        assert_eq!(864000000005000, c.value(0));
6899        assert_eq!(1545696000001000, c.value(1));
6900        assert!(c.is_null(2));
6901    }
6902
6903    #[test]
6904    fn test_cast_date64_to_timestamp_ns() {
6905        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6906        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
6907        let c = b
6908            .as_any()
6909            .downcast_ref::<TimestampNanosecondArray>()
6910            .unwrap();
6911        assert_eq!(864000000005000000, c.value(0));
6912        assert_eq!(1545696000001000000, c.value(1));
6913        assert!(c.is_null(2));
6914    }
6915
6916    #[test]
6917    fn test_cast_timestamp_to_i64() {
6918        let array =
6919            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6920                .with_timezone("UTC".to_string());
6921        let b = cast(&array, &DataType::Int64).unwrap();
6922        let c = b.as_primitive::<Int64Type>();
6923        assert_eq!(&DataType::Int64, c.data_type());
6924        assert_eq!(864000000005, c.value(0));
6925        assert_eq!(1545696000001, c.value(1));
6926        assert!(c.is_null(2));
6927    }
6928
6929    macro_rules! assert_cast {
6930        ($array:expr, $datatype:expr, $output_array_type: ty, $expected:expr) => {{
6931            assert!(can_cast_types($array.data_type(), &$datatype));
6932            let out = cast(&$array, &$datatype).unwrap();
6933            let actual = out
6934                .as_any()
6935                .downcast_ref::<$output_array_type>()
6936                .unwrap()
6937                .into_iter()
6938                .collect::<Vec<_>>();
6939            assert_eq!(actual, $expected);
6940        }};
6941        ($array:expr, $datatype:expr, $output_array_type: ty, $options:expr, $expected:expr) => {{
6942            assert!(can_cast_types($array.data_type(), &$datatype));
6943            let out = cast_with_options(&$array, &$datatype, &$options).unwrap();
6944            let actual = out
6945                .as_any()
6946                .downcast_ref::<$output_array_type>()
6947                .unwrap()
6948                .into_iter()
6949                .collect::<Vec<_>>();
6950            assert_eq!(actual, $expected);
6951        }};
6952    }
6953
6954    #[test]
6955    fn test_cast_date32_to_string() {
6956        let array = Date32Array::from(vec![Some(0), Some(10000), Some(13036), Some(17890), None]);
6957        let expected = vec![
6958            Some("1970-01-01"),
6959            Some("1997-05-19"),
6960            Some("2005-09-10"),
6961            Some("2018-12-25"),
6962            None,
6963        ];
6964
6965        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6966        assert_cast!(array, DataType::Utf8, StringArray, expected);
6967        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6968    }
6969
6970    #[test]
6971    fn test_cast_date64_to_string() {
6972        let array = Date64Array::from(vec![
6973            Some(0),
6974            Some(10000 * 86400000),
6975            Some(13036 * 86400000),
6976            Some(17890 * 86400000),
6977            None,
6978        ]);
6979        let expected = vec![
6980            Some("1970-01-01T00:00:00"),
6981            Some("1997-05-19T00:00:00"),
6982            Some("2005-09-10T00:00:00"),
6983            Some("2018-12-25T00:00:00"),
6984            None,
6985        ];
6986
6987        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6988        assert_cast!(array, DataType::Utf8, StringArray, expected);
6989        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6990    }
6991
6992    #[test]
6993    fn test_cast_date32_to_timestamp_and_timestamp_with_timezone() {
6994        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6995        let a = Date32Array::from(vec![Some(18628), None, None]); // 2021-1-1, 2022-1-1
6996        let array = Arc::new(a) as ArrayRef;
6997
6998        let b = cast(
6999            &array,
7000            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
7001        )
7002        .unwrap();
7003        let c = b.as_primitive::<TimestampSecondType>();
7004        let string_array = cast(&c, &DataType::Utf8).unwrap();
7005        let result = string_array.as_string::<i32>();
7006        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7007
7008        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
7009        let c = b.as_primitive::<TimestampSecondType>();
7010        let string_array = cast(&c, &DataType::Utf8).unwrap();
7011        let result = string_array.as_string::<i32>();
7012        assert_eq!("2021-01-01T00:00:00", result.value(0));
7013    }
7014
7015    #[test]
7016    fn test_cast_date32_to_timestamp_with_timezone() {
7017        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7018        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7019        let array = Arc::new(a) as ArrayRef;
7020        let b = cast(
7021            &array,
7022            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
7023        )
7024        .unwrap();
7025        let c = b.as_primitive::<TimestampSecondType>();
7026        assert_eq!(1609438500, c.value(0));
7027        assert_eq!(1640974500, c.value(1));
7028        assert!(c.is_null(2));
7029
7030        let string_array = cast(&c, &DataType::Utf8).unwrap();
7031        let result = string_array.as_string::<i32>();
7032        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7033        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7034    }
7035
7036    #[test]
7037    fn test_cast_date32_to_timestamp_with_timezone_ms() {
7038        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7039        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7040        let array = Arc::new(a) as ArrayRef;
7041        let b = cast(
7042            &array,
7043            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
7044        )
7045        .unwrap();
7046        let c = b.as_primitive::<TimestampMillisecondType>();
7047        assert_eq!(1609438500000, c.value(0));
7048        assert_eq!(1640974500000, c.value(1));
7049        assert!(c.is_null(2));
7050
7051        let string_array = cast(&c, &DataType::Utf8).unwrap();
7052        let result = string_array.as_string::<i32>();
7053        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7054        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7055    }
7056
7057    #[test]
7058    fn test_cast_date32_to_timestamp_with_timezone_us() {
7059        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7060        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7061        let array = Arc::new(a) as ArrayRef;
7062        let b = cast(
7063            &array,
7064            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
7065        )
7066        .unwrap();
7067        let c = b.as_primitive::<TimestampMicrosecondType>();
7068        assert_eq!(1609438500000000, c.value(0));
7069        assert_eq!(1640974500000000, c.value(1));
7070        assert!(c.is_null(2));
7071
7072        let string_array = cast(&c, &DataType::Utf8).unwrap();
7073        let result = string_array.as_string::<i32>();
7074        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7075        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7076    }
7077
7078    #[test]
7079    fn test_cast_date32_to_timestamp_with_timezone_ns() {
7080        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7081        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7082        let array = Arc::new(a) as ArrayRef;
7083        let b = cast(
7084            &array,
7085            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
7086        )
7087        .unwrap();
7088        let c = b.as_primitive::<TimestampNanosecondType>();
7089        assert_eq!(1609438500000000000, c.value(0));
7090        assert_eq!(1640974500000000000, c.value(1));
7091        assert!(c.is_null(2));
7092
7093        let string_array = cast(&c, &DataType::Utf8).unwrap();
7094        let result = string_array.as_string::<i32>();
7095        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7096        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7097    }
7098
7099    #[test]
7100    fn test_cast_date64_to_timestamp_with_timezone() {
7101        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7102        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7103        let b = cast(
7104            &array,
7105            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
7106        )
7107        .unwrap();
7108
7109        let c = b.as_primitive::<TimestampSecondType>();
7110        assert_eq!(863979300, c.value(0));
7111        assert_eq!(1545675300, c.value(1));
7112        assert!(c.is_null(2));
7113
7114        let string_array = cast(&c, &DataType::Utf8).unwrap();
7115        let result = string_array.as_string::<i32>();
7116        assert_eq!("1997-05-19T00:00:00+05:45", result.value(0));
7117        assert_eq!("2018-12-25T00:00:00+05:45", result.value(1));
7118    }
7119
7120    #[test]
7121    fn test_cast_date64_to_timestamp_with_timezone_ms() {
7122        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7123        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7124        let b = cast(
7125            &array,
7126            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
7127        )
7128        .unwrap();
7129
7130        let c = b.as_primitive::<TimestampMillisecondType>();
7131        assert_eq!(863979300005, c.value(0));
7132        assert_eq!(1545675300001, c.value(1));
7133        assert!(c.is_null(2));
7134
7135        let string_array = cast(&c, &DataType::Utf8).unwrap();
7136        let result = string_array.as_string::<i32>();
7137        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
7138        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7139    }
7140
7141    #[test]
7142    fn test_cast_date64_to_timestamp_with_timezone_us() {
7143        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7144        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7145        let b = cast(
7146            &array,
7147            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
7148        )
7149        .unwrap();
7150
7151        let c = b.as_primitive::<TimestampMicrosecondType>();
7152        assert_eq!(863979300005000, c.value(0));
7153        assert_eq!(1545675300001000, c.value(1));
7154        assert!(c.is_null(2));
7155
7156        let string_array = cast(&c, &DataType::Utf8).unwrap();
7157        let result = string_array.as_string::<i32>();
7158        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
7159        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7160    }
7161
7162    #[test]
7163    fn test_cast_date64_to_timestamp_with_timezone_ns() {
7164        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7165        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7166        let b = cast(
7167            &array,
7168            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
7169        )
7170        .unwrap();
7171
7172        let c = b.as_primitive::<TimestampNanosecondType>();
7173        assert_eq!(863979300005000000, c.value(0));
7174        assert_eq!(1545675300001000000, c.value(1));
7175        assert!(c.is_null(2));
7176
7177        let string_array = cast(&c, &DataType::Utf8).unwrap();
7178        let result = string_array.as_string::<i32>();
7179        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
7180        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7181    }
7182
7183    #[test]
7184    fn test_cast_timestamp_to_strings() {
7185        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
7186        let array =
7187            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7188        let expected = vec![
7189            Some("1997-05-19T00:00:03.005"),
7190            Some("2018-12-25T00:00:02.001"),
7191            None,
7192        ];
7193
7194        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
7195        assert_cast!(array, DataType::Utf8, StringArray, expected);
7196        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
7197    }
7198
7199    #[test]
7200    fn test_cast_timestamp_to_strings_opt() {
7201        let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
7202        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7203        let cast_options = CastOptions {
7204            safe: true,
7205            format_options: FormatOptions::default()
7206                .with_timestamp_format(Some(ts_format))
7207                .with_timestamp_tz_format(Some(ts_format)),
7208        };
7209
7210        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
7211        let array_without_tz =
7212            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7213        let expected = vec![
7214            Some("1997-05-19 00:00:03.005000"),
7215            Some("2018-12-25 00:00:02.001000"),
7216            None,
7217        ];
7218        assert_cast!(
7219            array_without_tz,
7220            DataType::Utf8View,
7221            StringViewArray,
7222            cast_options,
7223            expected
7224        );
7225        assert_cast!(
7226            array_without_tz,
7227            DataType::Utf8,
7228            StringArray,
7229            cast_options,
7230            expected
7231        );
7232        assert_cast!(
7233            array_without_tz,
7234            DataType::LargeUtf8,
7235            LargeStringArray,
7236            cast_options,
7237            expected
7238        );
7239
7240        let array_with_tz =
7241            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None])
7242                .with_timezone(tz.to_string());
7243        let expected = vec![
7244            Some("1997-05-19 05:45:03.005000"),
7245            Some("2018-12-25 05:45:02.001000"),
7246            None,
7247        ];
7248        assert_cast!(
7249            array_with_tz,
7250            DataType::Utf8View,
7251            StringViewArray,
7252            cast_options,
7253            expected
7254        );
7255        assert_cast!(
7256            array_with_tz,
7257            DataType::Utf8,
7258            StringArray,
7259            cast_options,
7260            expected
7261        );
7262        assert_cast!(
7263            array_with_tz,
7264            DataType::LargeUtf8,
7265            LargeStringArray,
7266            cast_options,
7267            expected
7268        );
7269    }
7270
7271    #[test]
7272    fn test_cast_between_timestamps() {
7273        let array =
7274            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7275        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
7276        let c = b.as_primitive::<TimestampSecondType>();
7277        assert_eq!(864000003, c.value(0));
7278        assert_eq!(1545696002, c.value(1));
7279        assert!(c.is_null(2));
7280    }
7281
7282    #[test]
7283    fn test_cast_duration_to_i64() {
7284        let base = vec![5, 6, 7, 8, 100000000];
7285
7286        let duration_arrays = vec![
7287            Arc::new(DurationNanosecondArray::from(base.clone())) as ArrayRef,
7288            Arc::new(DurationMicrosecondArray::from(base.clone())) as ArrayRef,
7289            Arc::new(DurationMillisecondArray::from(base.clone())) as ArrayRef,
7290            Arc::new(DurationSecondArray::from(base.clone())) as ArrayRef,
7291        ];
7292
7293        for arr in duration_arrays {
7294            assert!(can_cast_types(arr.data_type(), &DataType::Int64));
7295            let result = cast(&arr, &DataType::Int64).unwrap();
7296            let result = result.as_primitive::<Int64Type>();
7297            assert_eq!(base.as_slice(), result.values());
7298        }
7299    }
7300
7301    #[test]
7302    fn test_cast_between_durations_and_numerics() {
7303        fn test_cast_between_durations<FromType, ToType>()
7304        where
7305            FromType: ArrowPrimitiveType<Native = i64>,
7306            ToType: ArrowPrimitiveType<Native = i64>,
7307            PrimitiveArray<FromType>: From<Vec<Option<i64>>>,
7308        {
7309            let from_unit = match FromType::DATA_TYPE {
7310                DataType::Duration(unit) => unit,
7311                _ => panic!("Expected a duration type"),
7312            };
7313            let to_unit = match ToType::DATA_TYPE {
7314                DataType::Duration(unit) => unit,
7315                _ => panic!("Expected a duration type"),
7316            };
7317            let from_size = time_unit_multiple(&from_unit);
7318            let to_size = time_unit_multiple(&to_unit);
7319
7320            let (v1_before, v2_before) = (8640003005, 1696002001);
7321            let (v1_after, v2_after) = if from_size >= to_size {
7322                (
7323                    v1_before / (from_size / to_size),
7324                    v2_before / (from_size / to_size),
7325                )
7326            } else {
7327                (
7328                    v1_before * (to_size / from_size),
7329                    v2_before * (to_size / from_size),
7330                )
7331            };
7332
7333            let array =
7334                PrimitiveArray::<FromType>::from(vec![Some(v1_before), Some(v2_before), None]);
7335            let b = cast(&array, &ToType::DATA_TYPE).unwrap();
7336            let c = b.as_primitive::<ToType>();
7337            assert_eq!(v1_after, c.value(0));
7338            assert_eq!(v2_after, c.value(1));
7339            assert!(c.is_null(2));
7340        }
7341
7342        // between each individual duration type
7343        test_cast_between_durations::<DurationSecondType, DurationMillisecondType>();
7344        test_cast_between_durations::<DurationSecondType, DurationMicrosecondType>();
7345        test_cast_between_durations::<DurationSecondType, DurationNanosecondType>();
7346        test_cast_between_durations::<DurationMillisecondType, DurationSecondType>();
7347        test_cast_between_durations::<DurationMillisecondType, DurationMicrosecondType>();
7348        test_cast_between_durations::<DurationMillisecondType, DurationNanosecondType>();
7349        test_cast_between_durations::<DurationMicrosecondType, DurationSecondType>();
7350        test_cast_between_durations::<DurationMicrosecondType, DurationMillisecondType>();
7351        test_cast_between_durations::<DurationMicrosecondType, DurationNanosecondType>();
7352        test_cast_between_durations::<DurationNanosecondType, DurationSecondType>();
7353        test_cast_between_durations::<DurationNanosecondType, DurationMillisecondType>();
7354        test_cast_between_durations::<DurationNanosecondType, DurationMicrosecondType>();
7355
7356        // cast failed
7357        let array = DurationSecondArray::from(vec![
7358            Some(i64::MAX),
7359            Some(8640203410378005),
7360            Some(10241096),
7361            None,
7362        ]);
7363        let b = cast(&array, &DataType::Duration(TimeUnit::Nanosecond)).unwrap();
7364        let c = b.as_primitive::<DurationNanosecondType>();
7365        assert!(c.is_null(0));
7366        assert!(c.is_null(1));
7367        assert_eq!(10241096000000000, c.value(2));
7368        assert!(c.is_null(3));
7369
7370        // durations to numerics
7371        let array = DurationSecondArray::from(vec![
7372            Some(i64::MAX),
7373            Some(8640203410378005),
7374            Some(10241096),
7375            None,
7376        ]);
7377        let b = cast(&array, &DataType::Int64).unwrap();
7378        let c = b.as_primitive::<Int64Type>();
7379        assert_eq!(i64::MAX, c.value(0));
7380        assert_eq!(8640203410378005, c.value(1));
7381        assert_eq!(10241096, c.value(2));
7382        assert!(c.is_null(3));
7383
7384        let b = cast(&array, &DataType::Int32).unwrap();
7385        let c = b.as_primitive::<Int32Type>();
7386        assert_eq!(0, c.value(0));
7387        assert_eq!(0, c.value(1));
7388        assert_eq!(10241096, c.value(2));
7389        assert!(c.is_null(3));
7390
7391        // numerics to durations
7392        let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), Some(10241096), None]);
7393        let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
7394        let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
7395        assert_eq!(i32::MAX as i64, c.value(0));
7396        assert_eq!(802034103, c.value(1));
7397        assert_eq!(10241096, c.value(2));
7398        assert!(c.is_null(3));
7399    }
7400
7401    #[test]
7402    fn test_cast_to_strings() {
7403        let a = Int32Array::from(vec![1, 2, 3]);
7404        let out = cast(&a, &DataType::Utf8).unwrap();
7405        let out = out
7406            .as_any()
7407            .downcast_ref::<StringArray>()
7408            .unwrap()
7409            .into_iter()
7410            .collect::<Vec<_>>();
7411        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
7412        let out = cast(&a, &DataType::LargeUtf8).unwrap();
7413        let out = out
7414            .as_any()
7415            .downcast_ref::<LargeStringArray>()
7416            .unwrap()
7417            .into_iter()
7418            .collect::<Vec<_>>();
7419        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
7420    }
7421
7422    #[test]
7423    fn test_str_to_str_casts() {
7424        for data in [
7425            vec![Some("foo"), Some("bar"), Some("ham")],
7426            vec![Some("foo"), None, Some("bar")],
7427        ] {
7428            let a = LargeStringArray::from(data.clone());
7429            let to = cast(&a, &DataType::Utf8).unwrap();
7430            let expect = a
7431                .as_any()
7432                .downcast_ref::<LargeStringArray>()
7433                .unwrap()
7434                .into_iter()
7435                .collect::<Vec<_>>();
7436            let out = to
7437                .as_any()
7438                .downcast_ref::<StringArray>()
7439                .unwrap()
7440                .into_iter()
7441                .collect::<Vec<_>>();
7442            assert_eq!(expect, out);
7443
7444            let a = StringArray::from(data);
7445            let to = cast(&a, &DataType::LargeUtf8).unwrap();
7446            let expect = a
7447                .as_any()
7448                .downcast_ref::<StringArray>()
7449                .unwrap()
7450                .into_iter()
7451                .collect::<Vec<_>>();
7452            let out = to
7453                .as_any()
7454                .downcast_ref::<LargeStringArray>()
7455                .unwrap()
7456                .into_iter()
7457                .collect::<Vec<_>>();
7458            assert_eq!(expect, out);
7459        }
7460    }
7461
7462    const VIEW_TEST_DATA: [Option<&str>; 5] = [
7463        Some("hello"),
7464        Some("repeated"),
7465        None,
7466        Some("large payload over 12 bytes"),
7467        Some("repeated"),
7468    ];
7469
7470    #[test]
7471    fn test_string_view_to_binary_view() {
7472        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7473
7474        assert!(can_cast_types(
7475            string_view_array.data_type(),
7476            &DataType::BinaryView
7477        ));
7478
7479        let binary_view_array = cast(&string_view_array, &DataType::BinaryView).unwrap();
7480        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7481
7482        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7483        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7484    }
7485
7486    #[test]
7487    fn test_binary_view_to_string_view() {
7488        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7489
7490        assert!(can_cast_types(
7491            binary_view_array.data_type(),
7492            &DataType::Utf8View
7493        ));
7494
7495        let string_view_array = cast(&binary_view_array, &DataType::Utf8View).unwrap();
7496        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7497
7498        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7499        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7500    }
7501
7502    #[test]
7503    fn test_binary_view_to_string_view_with_invalid_utf8() {
7504        let binary_view_array = BinaryViewArray::from_iter(vec![
7505            Some("valid".as_bytes()),
7506            Some(&[0xff]),
7507            Some("utf8".as_bytes()),
7508            None,
7509        ]);
7510
7511        let strict_options = CastOptions {
7512            safe: false,
7513            ..Default::default()
7514        };
7515
7516        assert!(
7517            cast_with_options(&binary_view_array, &DataType::Utf8View, &strict_options).is_err()
7518        );
7519
7520        let safe_options = CastOptions {
7521            safe: true,
7522            ..Default::default()
7523        };
7524
7525        let string_view_array =
7526            cast_with_options(&binary_view_array, &DataType::Utf8View, &safe_options).unwrap();
7527        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7528
7529        let values: Vec<_> = string_view_array.as_string_view().iter().collect();
7530
7531        assert_eq!(values, vec![Some("valid"), None, Some("utf8"), None]);
7532    }
7533
7534    #[test]
7535    fn test_string_to_view() {
7536        _test_string_to_view::<i32>();
7537        _test_string_to_view::<i64>();
7538    }
7539
7540    fn _test_string_to_view<O>()
7541    where
7542        O: OffsetSizeTrait,
7543    {
7544        let string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7545
7546        assert!(can_cast_types(
7547            string_array.data_type(),
7548            &DataType::Utf8View
7549        ));
7550
7551        assert!(can_cast_types(
7552            string_array.data_type(),
7553            &DataType::BinaryView
7554        ));
7555
7556        let string_view_array = cast(&string_array, &DataType::Utf8View).unwrap();
7557        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7558
7559        let binary_view_array = cast(&string_array, &DataType::BinaryView).unwrap();
7560        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7561
7562        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7563        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7564
7565        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7566        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7567    }
7568
7569    #[test]
7570    fn test_bianry_to_view() {
7571        _test_binary_to_view::<i32>();
7572        _test_binary_to_view::<i64>();
7573    }
7574
7575    fn _test_binary_to_view<O>()
7576    where
7577        O: OffsetSizeTrait,
7578    {
7579        let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7580
7581        assert!(can_cast_types(
7582            binary_array.data_type(),
7583            &DataType::Utf8View
7584        ));
7585
7586        assert!(can_cast_types(
7587            binary_array.data_type(),
7588            &DataType::BinaryView
7589        ));
7590
7591        let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
7592        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7593
7594        let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
7595        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7596
7597        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7598        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7599
7600        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7601        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7602    }
7603
7604    #[test]
7605    fn test_dict_to_view() {
7606        let values = StringArray::from_iter(VIEW_TEST_DATA);
7607        let keys = Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
7608        let string_dict_array =
7609            DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
7610        let typed_dict = string_dict_array.downcast_dict::<StringArray>().unwrap();
7611
7612        let string_view_array = {
7613            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7614            for v in typed_dict {
7615                builder.append_option(v);
7616            }
7617            builder.finish()
7618        };
7619        let expected_string_array_type = string_view_array.data_type();
7620        let casted_string_array = cast(&string_dict_array, expected_string_array_type).unwrap();
7621        assert_eq!(casted_string_array.data_type(), expected_string_array_type);
7622        assert_eq!(casted_string_array.as_ref(), &string_view_array);
7623
7624        let binary_buffer = cast(&typed_dict.values(), &DataType::Binary).unwrap();
7625        let binary_dict_array =
7626            DictionaryArray::<Int8Type>::new(typed_dict.keys().clone(), binary_buffer);
7627        let typed_binary_dict = binary_dict_array.downcast_dict::<BinaryArray>().unwrap();
7628
7629        let binary_view_array = {
7630            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7631            for v in typed_binary_dict {
7632                builder.append_option(v);
7633            }
7634            builder.finish()
7635        };
7636        let expected_binary_array_type = binary_view_array.data_type();
7637        let casted_binary_array = cast(&binary_dict_array, expected_binary_array_type).unwrap();
7638        assert_eq!(casted_binary_array.data_type(), expected_binary_array_type);
7639        assert_eq!(casted_binary_array.as_ref(), &binary_view_array);
7640    }
7641
7642    #[test]
7643    fn test_dict_to_view_null_dictionary_value_is_null() {
7644        // Ensure we preserve nulls in the values
7645        let keys = Int32Array::from_iter([Some(0), Some(1), Some(2), None, Some(1)]);
7646
7647        let values = StringArray::from(vec![Some("aa"), None, Some("a value over twelve bytes")]);
7648        let dict = DictionaryArray::<Int32Type>::try_new(keys.clone(), Arc::new(values)).unwrap();
7649        let casted = cast(&dict, &DataType::Utf8View).unwrap();
7650        assert_eq!(
7651            casted.as_string_view().iter().collect::<Vec<_>>(),
7652            vec![
7653                Some("aa"),
7654                None,
7655                Some("a value over twelve bytes"),
7656                None,
7657                None
7658            ]
7659        );
7660        // the same input cast to Utf8 goes through `unpack_dictionary` and always agreed
7661        let reference = cast(&dict, &DataType::Utf8).unwrap();
7662        assert_eq!(
7663            casted.as_string_view().iter().collect::<Vec<_>>(),
7664            reference.as_string::<i32>().iter().collect::<Vec<_>>()
7665        );
7666
7667        let values = BinaryArray::from_opt_vec(vec![
7668            Some(b"aa".as_slice()),
7669            None,
7670            Some(b"a value over twelve bytes"),
7671        ]);
7672        let dict = DictionaryArray::<Int32Type>::try_new(keys, Arc::new(values)).unwrap();
7673        let casted = cast(&dict, &DataType::BinaryView).unwrap();
7674        assert_eq!(
7675            casted.as_binary_view().iter().collect::<Vec<_>>(),
7676            vec![
7677                Some(b"aa".as_slice()),
7678                None,
7679                Some(b"a value over twelve bytes"),
7680                None,
7681                None
7682            ]
7683        );
7684    }
7685
7686    #[test]
7687    fn test_view_to_dict() {
7688        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7689        let string_dict_array: DictionaryArray<Int8Type> = VIEW_TEST_DATA.into_iter().collect();
7690        let casted_type = string_dict_array.data_type();
7691        let casted_dict_array = cast(&string_view_array, casted_type).unwrap();
7692        assert_eq!(casted_dict_array.data_type(), casted_type);
7693        assert_eq!(casted_dict_array.as_ref(), &string_dict_array);
7694
7695        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7696        let binary_dict_array = string_dict_array.downcast_dict::<StringArray>().unwrap();
7697        let binary_buffer = cast(&binary_dict_array.values(), &DataType::Binary).unwrap();
7698        let binary_dict_array =
7699            DictionaryArray::<Int8Type>::new(binary_dict_array.keys().clone(), binary_buffer);
7700        let casted_type = binary_dict_array.data_type();
7701        let casted_binary_array = cast(&binary_view_array, casted_type).unwrap();
7702        assert_eq!(casted_binary_array.data_type(), casted_type);
7703        assert_eq!(casted_binary_array.as_ref(), &binary_dict_array);
7704    }
7705
7706    #[test]
7707    fn test_view_to_string() {
7708        _test_view_to_string::<i32>();
7709        _test_view_to_string::<i64>();
7710    }
7711
7712    fn _test_view_to_string<O>()
7713    where
7714        O: OffsetSizeTrait,
7715    {
7716        let string_view_array = {
7717            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7718            for s in VIEW_TEST_DATA.iter() {
7719                builder.append_option(*s);
7720            }
7721            builder.finish()
7722        };
7723
7724        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7725
7726        let expected_string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7727        let expected_type = expected_string_array.data_type();
7728
7729        assert!(can_cast_types(string_view_array.data_type(), expected_type));
7730        assert!(can_cast_types(binary_view_array.data_type(), expected_type));
7731
7732        let string_view_casted_array = cast(&string_view_array, expected_type).unwrap();
7733        assert_eq!(string_view_casted_array.data_type(), expected_type);
7734        assert_eq!(string_view_casted_array.as_ref(), &expected_string_array);
7735
7736        let binary_view_casted_array = cast(&binary_view_array, expected_type).unwrap();
7737        assert_eq!(binary_view_casted_array.data_type(), expected_type);
7738        assert_eq!(binary_view_casted_array.as_ref(), &expected_string_array);
7739    }
7740
7741    #[test]
7742    fn test_view_to_binary() {
7743        _test_view_to_binary::<i32>();
7744        _test_view_to_binary::<i64>();
7745    }
7746
7747    fn _test_view_to_binary<O>()
7748    where
7749        O: OffsetSizeTrait,
7750    {
7751        let view_array = {
7752            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7753            for s in VIEW_TEST_DATA.iter() {
7754                builder.append_option(*s);
7755            }
7756            builder.finish()
7757        };
7758
7759        let expected_binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7760        let expected_type = expected_binary_array.data_type();
7761
7762        assert!(can_cast_types(view_array.data_type(), expected_type));
7763
7764        let binary_array = cast(&view_array, expected_type).unwrap();
7765        assert_eq!(binary_array.data_type(), expected_type);
7766
7767        assert_eq!(binary_array.as_ref(), &expected_binary_array);
7768    }
7769
7770    #[test]
7771    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
7772    fn test_cast_from_f64() {
7773        let f64_values: Vec<f64> = vec![
7774            i64::MIN as f64,
7775            i32::MIN as f64,
7776            i16::MIN as f64,
7777            i8::MIN as f64,
7778            0_f64,
7779            u8::MAX as f64,
7780            u16::MAX as f64,
7781            u32::MAX as f64,
7782            u64::MAX as f64,
7783        ];
7784        let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values));
7785
7786        let f64_expected = vec![
7787            -9223372036854776000.0,
7788            -2147483648.0,
7789            -32768.0,
7790            -128.0,
7791            0.0,
7792            255.0,
7793            65535.0,
7794            4294967295.0,
7795            18446744073709552000.0,
7796        ];
7797        assert_eq!(
7798            f64_expected,
7799            get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
7800                .iter()
7801                .map(|i| i.parse::<f64>().unwrap())
7802                .collect::<Vec<f64>>()
7803        );
7804
7805        let f32_expected = vec![
7806            -9223372000000000000.0,
7807            -2147483600.0,
7808            -32768.0,
7809            -128.0,
7810            0.0,
7811            255.0,
7812            65535.0,
7813            4294967300.0,
7814            18446744000000000000.0,
7815        ];
7816        assert_eq!(
7817            f32_expected,
7818            get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
7819                .iter()
7820                .map(|i| i.parse::<f32>().unwrap())
7821                .collect::<Vec<f32>>()
7822        );
7823
7824        let f16_expected = vec![
7825            f16::from_f64(-9223372000000000000.0),
7826            f16::from_f64(-2147483600.0),
7827            f16::from_f64(-32768.0),
7828            f16::from_f64(-128.0),
7829            f16::from_f64(0.0),
7830            f16::from_f64(255.0),
7831            f16::from_f64(65535.0),
7832            f16::from_f64(4294967300.0),
7833            f16::from_f64(18446744000000000000.0),
7834        ];
7835        assert_eq!(
7836            f16_expected,
7837            get_cast_values::<Float16Type>(&f64_array, &DataType::Float16)
7838                .iter()
7839                .map(|i| i.parse::<f16>().unwrap())
7840                .collect::<Vec<f16>>()
7841        );
7842
7843        let i64_expected = vec![
7844            "-9223372036854775808",
7845            "-2147483648",
7846            "-32768",
7847            "-128",
7848            "0",
7849            "255",
7850            "65535",
7851            "4294967295",
7852            "null",
7853        ];
7854        assert_eq!(
7855            i64_expected,
7856            get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
7857        );
7858
7859        let i32_expected = vec![
7860            "null",
7861            "-2147483648",
7862            "-32768",
7863            "-128",
7864            "0",
7865            "255",
7866            "65535",
7867            "null",
7868            "null",
7869        ];
7870        assert_eq!(
7871            i32_expected,
7872            get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
7873        );
7874
7875        let i16_expected = vec![
7876            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7877        ];
7878        assert_eq!(
7879            i16_expected,
7880            get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
7881        );
7882
7883        let i8_expected = vec![
7884            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7885        ];
7886        assert_eq!(
7887            i8_expected,
7888            get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
7889        );
7890
7891        let u64_expected = vec![
7892            "null",
7893            "null",
7894            "null",
7895            "null",
7896            "0",
7897            "255",
7898            "65535",
7899            "4294967295",
7900            "null",
7901        ];
7902        assert_eq!(
7903            u64_expected,
7904            get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
7905        );
7906
7907        let u32_expected = vec![
7908            "null",
7909            "null",
7910            "null",
7911            "null",
7912            "0",
7913            "255",
7914            "65535",
7915            "4294967295",
7916            "null",
7917        ];
7918        assert_eq!(
7919            u32_expected,
7920            get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
7921        );
7922
7923        let u16_expected = vec![
7924            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7925        ];
7926        assert_eq!(
7927            u16_expected,
7928            get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
7929        );
7930
7931        let u8_expected = vec![
7932            "null", "null", "null", "null", "0", "255", "null", "null", "null",
7933        ];
7934        assert_eq!(
7935            u8_expected,
7936            get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
7937        );
7938    }
7939
7940    #[test]
7941    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
7942    fn test_cast_from_f32() {
7943        let f32_values: Vec<f32> = vec![
7944            i32::MIN as f32,
7945            i32::MIN as f32,
7946            i16::MIN as f32,
7947            i8::MIN as f32,
7948            0_f32,
7949            u8::MAX as f32,
7950            u16::MAX as f32,
7951            u32::MAX as f32,
7952            u32::MAX as f32,
7953        ];
7954        let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values));
7955
7956        let f64_expected = vec![
7957            "-2147483648.0",
7958            "-2147483648.0",
7959            "-32768.0",
7960            "-128.0",
7961            "0.0",
7962            "255.0",
7963            "65535.0",
7964            "4294967296.0",
7965            "4294967296.0",
7966        ];
7967        assert_eq!(
7968            f64_expected,
7969            get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
7970        );
7971
7972        let f32_expected = vec![
7973            "-2147483600.0",
7974            "-2147483600.0",
7975            "-32768.0",
7976            "-128.0",
7977            "0.0",
7978            "255.0",
7979            "65535.0",
7980            "4294967300.0",
7981            "4294967300.0",
7982        ];
7983        assert_eq!(
7984            f32_expected,
7985            get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
7986        );
7987
7988        let f16_expected = vec![
7989            "-inf", "-inf", "-32768.0", "-128.0", "0.0", "255.0", "inf", "inf", "inf",
7990        ];
7991        assert_eq!(
7992            f16_expected,
7993            get_cast_values::<Float16Type>(&f32_array, &DataType::Float16)
7994        );
7995
7996        let i64_expected = vec![
7997            "-2147483648",
7998            "-2147483648",
7999            "-32768",
8000            "-128",
8001            "0",
8002            "255",
8003            "65535",
8004            "4294967296",
8005            "4294967296",
8006        ];
8007        assert_eq!(
8008            i64_expected,
8009            get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
8010        );
8011
8012        let i32_expected = vec![
8013            "-2147483648",
8014            "-2147483648",
8015            "-32768",
8016            "-128",
8017            "0",
8018            "255",
8019            "65535",
8020            "null",
8021            "null",
8022        ];
8023        assert_eq!(
8024            i32_expected,
8025            get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
8026        );
8027
8028        let i16_expected = vec![
8029            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
8030        ];
8031        assert_eq!(
8032            i16_expected,
8033            get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
8034        );
8035
8036        let i8_expected = vec![
8037            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
8038        ];
8039        assert_eq!(
8040            i8_expected,
8041            get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
8042        );
8043
8044        let u64_expected = vec![
8045            "null",
8046            "null",
8047            "null",
8048            "null",
8049            "0",
8050            "255",
8051            "65535",
8052            "4294967296",
8053            "4294967296",
8054        ];
8055        assert_eq!(
8056            u64_expected,
8057            get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
8058        );
8059
8060        let u32_expected = vec![
8061            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
8062        ];
8063        assert_eq!(
8064            u32_expected,
8065            get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
8066        );
8067
8068        let u16_expected = vec![
8069            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
8070        ];
8071        assert_eq!(
8072            u16_expected,
8073            get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
8074        );
8075
8076        let u8_expected = vec![
8077            "null", "null", "null", "null", "0", "255", "null", "null", "null",
8078        ];
8079        assert_eq!(
8080            u8_expected,
8081            get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
8082        );
8083    }
8084
8085    #[test]
8086    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8087    fn test_cast_from_uint64() {
8088        let u64_values: Vec<u64> = vec![
8089            0,
8090            u8::MAX as u64,
8091            u16::MAX as u64,
8092            u32::MAX as u64,
8093            u64::MAX,
8094        ];
8095        let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values));
8096
8097        let f64_expected = vec![0.0, 255.0, 65535.0, 4294967295.0, 18446744073709552000.0];
8098        assert_eq!(
8099            f64_expected,
8100            get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
8101                .iter()
8102                .map(|i| i.parse::<f64>().unwrap())
8103                .collect::<Vec<f64>>()
8104        );
8105
8106        let f32_expected = vec![0.0, 255.0, 65535.0, 4294967300.0, 18446744000000000000.0];
8107        assert_eq!(
8108            f32_expected,
8109            get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
8110                .iter()
8111                .map(|i| i.parse::<f32>().unwrap())
8112                .collect::<Vec<f32>>()
8113        );
8114
8115        let f16_expected = vec![
8116            f16::from_f64(0.0),
8117            f16::from_f64(255.0),
8118            f16::from_f64(65535.0),
8119            f16::from_f64(4294967300.0),
8120            f16::from_f64(18446744000000000000.0),
8121        ];
8122        assert_eq!(
8123            f16_expected,
8124            get_cast_values::<Float16Type>(&u64_array, &DataType::Float16)
8125                .iter()
8126                .map(|i| i.parse::<f16>().unwrap())
8127                .collect::<Vec<f16>>()
8128        );
8129
8130        let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
8131        assert_eq!(
8132            i64_expected,
8133            get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
8134        );
8135
8136        let i32_expected = vec!["0", "255", "65535", "null", "null"];
8137        assert_eq!(
8138            i32_expected,
8139            get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
8140        );
8141
8142        let i16_expected = vec!["0", "255", "null", "null", "null"];
8143        assert_eq!(
8144            i16_expected,
8145            get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
8146        );
8147
8148        let i8_expected = vec!["0", "null", "null", "null", "null"];
8149        assert_eq!(
8150            i8_expected,
8151            get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
8152        );
8153
8154        let u64_expected = vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
8155        assert_eq!(
8156            u64_expected,
8157            get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
8158        );
8159
8160        let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
8161        assert_eq!(
8162            u32_expected,
8163            get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
8164        );
8165
8166        let u16_expected = vec!["0", "255", "65535", "null", "null"];
8167        assert_eq!(
8168            u16_expected,
8169            get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
8170        );
8171
8172        let u8_expected = vec!["0", "255", "null", "null", "null"];
8173        assert_eq!(
8174            u8_expected,
8175            get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
8176        );
8177    }
8178
8179    #[test]
8180    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8181    fn test_cast_from_uint32() {
8182        let u32_values: Vec<u32> = vec![0, u8::MAX as u32, u16::MAX as u32, u32::MAX];
8183        let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values));
8184
8185        let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
8186        assert_eq!(
8187            f64_expected,
8188            get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
8189        );
8190
8191        let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
8192        assert_eq!(
8193            f32_expected,
8194            get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
8195        );
8196
8197        let f16_expected = vec!["0.0", "255.0", "inf", "inf"];
8198        assert_eq!(
8199            f16_expected,
8200            get_cast_values::<Float16Type>(&u32_array, &DataType::Float16)
8201        );
8202
8203        let i64_expected = vec!["0", "255", "65535", "4294967295"];
8204        assert_eq!(
8205            i64_expected,
8206            get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
8207        );
8208
8209        let i32_expected = vec!["0", "255", "65535", "null"];
8210        assert_eq!(
8211            i32_expected,
8212            get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
8213        );
8214
8215        let i16_expected = vec!["0", "255", "null", "null"];
8216        assert_eq!(
8217            i16_expected,
8218            get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
8219        );
8220
8221        let i8_expected = vec!["0", "null", "null", "null"];
8222        assert_eq!(
8223            i8_expected,
8224            get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
8225        );
8226
8227        let u64_expected = vec!["0", "255", "65535", "4294967295"];
8228        assert_eq!(
8229            u64_expected,
8230            get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
8231        );
8232
8233        let u32_expected = vec!["0", "255", "65535", "4294967295"];
8234        assert_eq!(
8235            u32_expected,
8236            get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
8237        );
8238
8239        let u16_expected = vec!["0", "255", "65535", "null"];
8240        assert_eq!(
8241            u16_expected,
8242            get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
8243        );
8244
8245        let u8_expected = vec!["0", "255", "null", "null"];
8246        assert_eq!(
8247            u8_expected,
8248            get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
8249        );
8250    }
8251
8252    #[test]
8253    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8254    fn test_cast_from_uint16() {
8255        let u16_values: Vec<u16> = vec![0, u8::MAX as u16, u16::MAX];
8256        let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values));
8257
8258        let f64_expected = vec!["0.0", "255.0", "65535.0"];
8259        assert_eq!(
8260            f64_expected,
8261            get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
8262        );
8263
8264        let f32_expected = vec!["0.0", "255.0", "65535.0"];
8265        assert_eq!(
8266            f32_expected,
8267            get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
8268        );
8269
8270        let f16_expected = vec!["0.0", "255.0", "inf"];
8271        assert_eq!(
8272            f16_expected,
8273            get_cast_values::<Float16Type>(&u16_array, &DataType::Float16)
8274        );
8275
8276        let i64_expected = vec!["0", "255", "65535"];
8277        assert_eq!(
8278            i64_expected,
8279            get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
8280        );
8281
8282        let i32_expected = vec!["0", "255", "65535"];
8283        assert_eq!(
8284            i32_expected,
8285            get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
8286        );
8287
8288        let i16_expected = vec!["0", "255", "null"];
8289        assert_eq!(
8290            i16_expected,
8291            get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
8292        );
8293
8294        let i8_expected = vec!["0", "null", "null"];
8295        assert_eq!(
8296            i8_expected,
8297            get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
8298        );
8299
8300        let u64_expected = vec!["0", "255", "65535"];
8301        assert_eq!(
8302            u64_expected,
8303            get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
8304        );
8305
8306        let u32_expected = vec!["0", "255", "65535"];
8307        assert_eq!(
8308            u32_expected,
8309            get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
8310        );
8311
8312        let u16_expected = vec!["0", "255", "65535"];
8313        assert_eq!(
8314            u16_expected,
8315            get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
8316        );
8317
8318        let u8_expected = vec!["0", "255", "null"];
8319        assert_eq!(
8320            u8_expected,
8321            get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
8322        );
8323    }
8324
8325    #[test]
8326    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8327    fn test_cast_from_uint8() {
8328        let u8_values: Vec<u8> = vec![0, u8::MAX];
8329        let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values));
8330
8331        let f64_expected = vec!["0.0", "255.0"];
8332        assert_eq!(
8333            f64_expected,
8334            get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
8335        );
8336
8337        let f32_expected = vec!["0.0", "255.0"];
8338        assert_eq!(
8339            f32_expected,
8340            get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
8341        );
8342
8343        let f16_expected = vec!["0.0", "255.0"];
8344        assert_eq!(
8345            f16_expected,
8346            get_cast_values::<Float16Type>(&u8_array, &DataType::Float16)
8347        );
8348
8349        let i64_expected = vec!["0", "255"];
8350        assert_eq!(
8351            i64_expected,
8352            get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
8353        );
8354
8355        let i32_expected = vec!["0", "255"];
8356        assert_eq!(
8357            i32_expected,
8358            get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
8359        );
8360
8361        let i16_expected = vec!["0", "255"];
8362        assert_eq!(
8363            i16_expected,
8364            get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
8365        );
8366
8367        let i8_expected = vec!["0", "null"];
8368        assert_eq!(
8369            i8_expected,
8370            get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
8371        );
8372
8373        let u64_expected = vec!["0", "255"];
8374        assert_eq!(
8375            u64_expected,
8376            get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
8377        );
8378
8379        let u32_expected = vec!["0", "255"];
8380        assert_eq!(
8381            u32_expected,
8382            get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
8383        );
8384
8385        let u16_expected = vec!["0", "255"];
8386        assert_eq!(
8387            u16_expected,
8388            get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
8389        );
8390
8391        let u8_expected = vec!["0", "255"];
8392        assert_eq!(
8393            u8_expected,
8394            get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
8395        );
8396    }
8397
8398    #[test]
8399    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8400    fn test_cast_from_int64() {
8401        let i64_values: Vec<i64> = vec![
8402            i64::MIN,
8403            i32::MIN as i64,
8404            i16::MIN as i64,
8405            i8::MIN as i64,
8406            0,
8407            i8::MAX as i64,
8408            i16::MAX as i64,
8409            i32::MAX as i64,
8410            i64::MAX,
8411        ];
8412        let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values));
8413
8414        let f64_expected = vec![
8415            -9223372036854776000.0,
8416            -2147483648.0,
8417            -32768.0,
8418            -128.0,
8419            0.0,
8420            127.0,
8421            32767.0,
8422            2147483647.0,
8423            9223372036854776000.0,
8424        ];
8425        assert_eq!(
8426            f64_expected,
8427            get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
8428                .iter()
8429                .map(|i| i.parse::<f64>().unwrap())
8430                .collect::<Vec<f64>>()
8431        );
8432
8433        let f32_expected = vec![
8434            -9223372000000000000.0,
8435            -2147483600.0,
8436            -32768.0,
8437            -128.0,
8438            0.0,
8439            127.0,
8440            32767.0,
8441            2147483600.0,
8442            9223372000000000000.0,
8443        ];
8444        assert_eq!(
8445            f32_expected,
8446            get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
8447                .iter()
8448                .map(|i| i.parse::<f32>().unwrap())
8449                .collect::<Vec<f32>>()
8450        );
8451
8452        let f16_expected = vec![
8453            f16::from_f64(-9223372000000000000.0),
8454            f16::from_f64(-2147483600.0),
8455            f16::from_f64(-32768.0),
8456            f16::from_f64(-128.0),
8457            f16::from_f64(0.0),
8458            f16::from_f64(127.0),
8459            f16::from_f64(32767.0),
8460            f16::from_f64(2147483600.0),
8461            f16::from_f64(9223372000000000000.0),
8462        ];
8463        assert_eq!(
8464            f16_expected,
8465            get_cast_values::<Float16Type>(&i64_array, &DataType::Float16)
8466                .iter()
8467                .map(|i| i.parse::<f16>().unwrap())
8468                .collect::<Vec<f16>>()
8469        );
8470
8471        let i64_expected = vec![
8472            "-9223372036854775808",
8473            "-2147483648",
8474            "-32768",
8475            "-128",
8476            "0",
8477            "127",
8478            "32767",
8479            "2147483647",
8480            "9223372036854775807",
8481        ];
8482        assert_eq!(
8483            i64_expected,
8484            get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
8485        );
8486
8487        let i32_expected = vec![
8488            "null",
8489            "-2147483648",
8490            "-32768",
8491            "-128",
8492            "0",
8493            "127",
8494            "32767",
8495            "2147483647",
8496            "null",
8497        ];
8498        assert_eq!(
8499            i32_expected,
8500            get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
8501        );
8502
8503        assert_eq!(
8504            i32_expected,
8505            get_cast_values::<Date32Type>(&i64_array, &DataType::Date32)
8506        );
8507
8508        let i16_expected = vec![
8509            "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
8510        ];
8511        assert_eq!(
8512            i16_expected,
8513            get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
8514        );
8515
8516        let i8_expected = vec![
8517            "null", "null", "null", "-128", "0", "127", "null", "null", "null",
8518        ];
8519        assert_eq!(
8520            i8_expected,
8521            get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
8522        );
8523
8524        let u64_expected = vec![
8525            "null",
8526            "null",
8527            "null",
8528            "null",
8529            "0",
8530            "127",
8531            "32767",
8532            "2147483647",
8533            "9223372036854775807",
8534        ];
8535        assert_eq!(
8536            u64_expected,
8537            get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
8538        );
8539
8540        let u32_expected = vec![
8541            "null",
8542            "null",
8543            "null",
8544            "null",
8545            "0",
8546            "127",
8547            "32767",
8548            "2147483647",
8549            "null",
8550        ];
8551        assert_eq!(
8552            u32_expected,
8553            get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
8554        );
8555
8556        let u16_expected = vec![
8557            "null", "null", "null", "null", "0", "127", "32767", "null", "null",
8558        ];
8559        assert_eq!(
8560            u16_expected,
8561            get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
8562        );
8563
8564        let u8_expected = vec![
8565            "null", "null", "null", "null", "0", "127", "null", "null", "null",
8566        ];
8567        assert_eq!(
8568            u8_expected,
8569            get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
8570        );
8571    }
8572
8573    #[test]
8574    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8575    fn test_cast_from_int32() {
8576        let i32_values: Vec<i32> = vec![
8577            i32::MIN,
8578            i16::MIN as i32,
8579            i8::MIN as i32,
8580            0,
8581            i8::MAX as i32,
8582            i16::MAX as i32,
8583            i32::MAX,
8584        ];
8585        let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values));
8586
8587        let f64_expected = vec![
8588            "-2147483648.0",
8589            "-32768.0",
8590            "-128.0",
8591            "0.0",
8592            "127.0",
8593            "32767.0",
8594            "2147483647.0",
8595        ];
8596        assert_eq!(
8597            f64_expected,
8598            get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
8599        );
8600
8601        let f32_expected = vec![
8602            "-2147483600.0",
8603            "-32768.0",
8604            "-128.0",
8605            "0.0",
8606            "127.0",
8607            "32767.0",
8608            "2147483600.0",
8609        ];
8610        assert_eq!(
8611            f32_expected,
8612            get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
8613        );
8614
8615        let f16_expected = vec![
8616            f16::from_f64(-2147483600.0),
8617            f16::from_f64(-32768.0),
8618            f16::from_f64(-128.0),
8619            f16::from_f64(0.0),
8620            f16::from_f64(127.0),
8621            f16::from_f64(32767.0),
8622            f16::from_f64(2147483600.0),
8623        ];
8624        assert_eq!(
8625            f16_expected,
8626            get_cast_values::<Float16Type>(&i32_array, &DataType::Float16)
8627                .iter()
8628                .map(|i| i.parse::<f16>().unwrap())
8629                .collect::<Vec<f16>>()
8630        );
8631
8632        let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
8633        assert_eq!(
8634            i16_expected,
8635            get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
8636        );
8637
8638        let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
8639        assert_eq!(
8640            i8_expected,
8641            get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
8642        );
8643
8644        let u64_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8645        assert_eq!(
8646            u64_expected,
8647            get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
8648        );
8649
8650        let u32_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8651        assert_eq!(
8652            u32_expected,
8653            get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
8654        );
8655
8656        let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
8657        assert_eq!(
8658            u16_expected,
8659            get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
8660        );
8661
8662        let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
8663        assert_eq!(
8664            u8_expected,
8665            get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
8666        );
8667
8668        // The date32 to date64 cast increases the numerical values in order to keep the same dates.
8669        let i64_expected = vec![
8670            "-185542587187200000",
8671            "-2831155200000",
8672            "-11059200000",
8673            "0",
8674            "10972800000",
8675            "2831068800000",
8676            "185542587100800000",
8677        ];
8678        assert_eq!(
8679            i64_expected,
8680            get_cast_values::<Date64Type>(&i32_array, &DataType::Date64)
8681        );
8682    }
8683
8684    #[test]
8685    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8686    fn test_cast_from_int16() {
8687        let i16_values: Vec<i16> = vec![i16::MIN, i8::MIN as i16, 0, i8::MAX as i16, i16::MAX];
8688        let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values));
8689
8690        let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8691        assert_eq!(
8692            f64_expected,
8693            get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
8694        );
8695
8696        let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8697        assert_eq!(
8698            f32_expected,
8699            get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
8700        );
8701
8702        let f16_expected = vec![
8703            f16::from_f64(-32768.0),
8704            f16::from_f64(-128.0),
8705            f16::from_f64(0.0),
8706            f16::from_f64(127.0),
8707            f16::from_f64(32767.0),
8708        ];
8709        assert_eq!(
8710            f16_expected,
8711            get_cast_values::<Float16Type>(&i16_array, &DataType::Float16)
8712                .iter()
8713                .map(|i| i.parse::<f16>().unwrap())
8714                .collect::<Vec<f16>>()
8715        );
8716
8717        let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
8718        assert_eq!(
8719            i64_expected,
8720            get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
8721        );
8722
8723        let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
8724        assert_eq!(
8725            i32_expected,
8726            get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
8727        );
8728
8729        let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
8730        assert_eq!(
8731            i16_expected,
8732            get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
8733        );
8734
8735        let i8_expected = vec!["null", "-128", "0", "127", "null"];
8736        assert_eq!(
8737            i8_expected,
8738            get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
8739        );
8740
8741        let u64_expected = vec!["null", "null", "0", "127", "32767"];
8742        assert_eq!(
8743            u64_expected,
8744            get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
8745        );
8746
8747        let u32_expected = vec!["null", "null", "0", "127", "32767"];
8748        assert_eq!(
8749            u32_expected,
8750            get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
8751        );
8752
8753        let u16_expected = vec!["null", "null", "0", "127", "32767"];
8754        assert_eq!(
8755            u16_expected,
8756            get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
8757        );
8758
8759        let u8_expected = vec!["null", "null", "0", "127", "null"];
8760        assert_eq!(
8761            u8_expected,
8762            get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
8763        );
8764    }
8765
8766    #[test]
8767    fn test_cast_from_date32() {
8768        let i32_values: Vec<i32> = vec![
8769            i32::MIN,
8770            i16::MIN as i32,
8771            i8::MIN as i32,
8772            0,
8773            i8::MAX as i32,
8774            i16::MAX as i32,
8775            i32::MAX,
8776        ];
8777        let date32_array: ArrayRef = Arc::new(Date32Array::from(i32_values));
8778
8779        let i64_expected = vec![
8780            "-2147483648",
8781            "-32768",
8782            "-128",
8783            "0",
8784            "127",
8785            "32767",
8786            "2147483647",
8787        ];
8788        assert_eq!(
8789            i64_expected,
8790            get_cast_values::<Int64Type>(&date32_array, &DataType::Int64)
8791        );
8792    }
8793
8794    #[test]
8795    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
8796    fn test_cast_from_int8() {
8797        let i8_values: Vec<i8> = vec![i8::MIN, 0, i8::MAX];
8798        let i8_array = Int8Array::from(i8_values);
8799
8800        let f64_expected = vec!["-128.0", "0.0", "127.0"];
8801        assert_eq!(
8802            f64_expected,
8803            get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
8804        );
8805
8806        let f32_expected = vec!["-128.0", "0.0", "127.0"];
8807        assert_eq!(
8808            f32_expected,
8809            get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
8810        );
8811
8812        let f16_expected = vec!["-128.0", "0.0", "127.0"];
8813        assert_eq!(
8814            f16_expected,
8815            get_cast_values::<Float16Type>(&i8_array, &DataType::Float16)
8816        );
8817
8818        let i64_expected = vec!["-128", "0", "127"];
8819        assert_eq!(
8820            i64_expected,
8821            get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
8822        );
8823
8824        let i32_expected = vec!["-128", "0", "127"];
8825        assert_eq!(
8826            i32_expected,
8827            get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
8828        );
8829
8830        let i16_expected = vec!["-128", "0", "127"];
8831        assert_eq!(
8832            i16_expected,
8833            get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
8834        );
8835
8836        let i8_expected = vec!["-128", "0", "127"];
8837        assert_eq!(
8838            i8_expected,
8839            get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
8840        );
8841
8842        let u64_expected = vec!["null", "0", "127"];
8843        assert_eq!(
8844            u64_expected,
8845            get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
8846        );
8847
8848        let u32_expected = vec!["null", "0", "127"];
8849        assert_eq!(
8850            u32_expected,
8851            get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
8852        );
8853
8854        let u16_expected = vec!["null", "0", "127"];
8855        assert_eq!(
8856            u16_expected,
8857            get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
8858        );
8859
8860        let u8_expected = vec!["null", "0", "127"];
8861        assert_eq!(
8862            u8_expected,
8863            get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
8864        );
8865    }
8866
8867    /// Convert `array` into a vector of strings by casting to data type dt
8868    fn get_cast_values<T>(array: &dyn Array, dt: &DataType) -> Vec<String>
8869    where
8870        T: ArrowPrimitiveType,
8871    {
8872        let c = cast(array, dt).unwrap();
8873        let a = c.as_primitive::<T>();
8874        let mut v: Vec<String> = vec![];
8875        for i in 0..array.len() {
8876            if a.is_null(i) {
8877                v.push("null".to_string())
8878            } else {
8879                v.push(format!("{:?}", a.value(i)));
8880            }
8881        }
8882        v
8883    }
8884
8885    #[test]
8886    fn test_cast_utf8_dict() {
8887        // FROM a dictionary with of Utf8 values
8888        let mut builder = StringDictionaryBuilder::<Int8Type>::new();
8889        builder.append("one").unwrap();
8890        builder.append_null();
8891        builder.append("three").unwrap();
8892        let array: ArrayRef = Arc::new(builder.finish());
8893
8894        let expected = vec!["one", "null", "three"];
8895
8896        // Test casting TO StringArray
8897        let cast_type = Utf8;
8898        let cast_array = cast(&array, &cast_type).expect("cast to UTF-8 failed");
8899        assert_eq!(cast_array.data_type(), &cast_type);
8900        assert_eq!(array_to_strings(&cast_array), expected);
8901
8902        // Test casting TO Dictionary (with different index sizes)
8903
8904        let cast_type = Dictionary(Box::new(Int16), Box::new(Utf8));
8905        let cast_array = cast(&array, &cast_type).expect("cast failed");
8906        assert_eq!(cast_array.data_type(), &cast_type);
8907        assert_eq!(array_to_strings(&cast_array), expected);
8908
8909        let cast_type = Dictionary(Box::new(Int32), Box::new(Utf8));
8910        let cast_array = cast(&array, &cast_type).expect("cast failed");
8911        assert_eq!(cast_array.data_type(), &cast_type);
8912        assert_eq!(array_to_strings(&cast_array), expected);
8913
8914        let cast_type = Dictionary(Box::new(Int64), Box::new(Utf8));
8915        let cast_array = cast(&array, &cast_type).expect("cast failed");
8916        assert_eq!(cast_array.data_type(), &cast_type);
8917        assert_eq!(array_to_strings(&cast_array), expected);
8918
8919        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8920        let cast_array = cast(&array, &cast_type).expect("cast failed");
8921        assert_eq!(cast_array.data_type(), &cast_type);
8922        assert_eq!(array_to_strings(&cast_array), expected);
8923
8924        let cast_type = Dictionary(Box::new(UInt16), Box::new(Utf8));
8925        let cast_array = cast(&array, &cast_type).expect("cast failed");
8926        assert_eq!(cast_array.data_type(), &cast_type);
8927        assert_eq!(array_to_strings(&cast_array), expected);
8928
8929        let cast_type = Dictionary(Box::new(UInt32), Box::new(Utf8));
8930        let cast_array = cast(&array, &cast_type).expect("cast failed");
8931        assert_eq!(cast_array.data_type(), &cast_type);
8932        assert_eq!(array_to_strings(&cast_array), expected);
8933
8934        let cast_type = Dictionary(Box::new(UInt64), Box::new(Utf8));
8935        let cast_array = cast(&array, &cast_type).expect("cast failed");
8936        assert_eq!(cast_array.data_type(), &cast_type);
8937        assert_eq!(array_to_strings(&cast_array), expected);
8938    }
8939
8940    #[test]
8941    fn test_cast_dict_to_dict_bad_index_value_primitive() {
8942        // test converting from an array that has indexes of a type
8943        // that are out of bounds for a particular other kind of
8944        // index.
8945
8946        let mut builder = PrimitiveDictionaryBuilder::<Int32Type, Int64Type>::new();
8947
8948        // add 200 distinct values (which can be stored by a
8949        // dictionary indexed by int32, but not a dictionary indexed
8950        // with int8)
8951        for i in 0..200 {
8952            builder.append(i).unwrap();
8953        }
8954        let array: ArrayRef = Arc::new(builder.finish());
8955
8956        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8957        let res = cast(&array, &cast_type);
8958        assert!(res.is_err());
8959        let actual_error = format!("{res:?}");
8960        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8961        assert!(
8962            actual_error.contains(expected_error),
8963            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8964        );
8965    }
8966
8967    #[test]
8968    fn test_cast_dict_to_dict_bad_index_value_utf8() {
8969        // Same test as test_cast_dict_to_dict_bad_index_value but use
8970        // string values (and encode the expected behavior here);
8971
8972        let mut builder = StringDictionaryBuilder::<Int32Type>::new();
8973
8974        // add 200 distinct values (which can be stored by a
8975        // dictionary indexed by int32, but not a dictionary indexed
8976        // with int8)
8977        for i in 0..200 {
8978            let val = format!("val{i}");
8979            builder.append(&val).unwrap();
8980        }
8981        let array = builder.finish();
8982
8983        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8984        let res = cast(&array, &cast_type);
8985        assert!(res.is_err());
8986        let actual_error = format!("{res:?}");
8987        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8988        assert!(
8989            actual_error.contains(expected_error),
8990            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8991        );
8992    }
8993
8994    #[test]
8995    fn test_cast_nested_dictionary_to_dictionary_reuses_values() {
8996        let inner = DictionaryArray::<Int32Type>::new(
8997            Int32Array::from(vec![Some(0), None, Some(1)]),
8998            Arc::new(StringArray::from(vec!["x", "y"])),
8999        );
9000        let nested = DictionaryArray::<Int32Type>::new(
9001            Int32Array::from(vec![Some(0), Some(1), Some(2), None, Some(0)]),
9002            Arc::new(inner),
9003        );
9004
9005        let result = cast(&nested, &Dictionary(Box::new(Int32), Box::new(Utf8))).unwrap();
9006        let result = result.as_dictionary::<Int32Type>();
9007
9008        assert_eq!(
9009            result.keys(),
9010            &Int32Array::from(vec![Some(0), None, Some(1), None, Some(0)])
9011        );
9012        assert_eq!(
9013            result.values().as_string::<i32>(),
9014            &StringArray::from(vec!["x", "y"])
9015        );
9016        let logical: Vec<Option<&str>> = result
9017            .downcast_dict::<StringArray>()
9018            .unwrap()
9019            .into_iter()
9020            .collect();
9021        assert_eq!(logical, vec![Some("x"), None, Some("y"), None, Some("x")]);
9022    }
9023
9024    #[test]
9025    fn test_cast_primitive_dict() {
9026        // FROM a dictionary with of INT32 values
9027        let mut builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
9028        builder.append(1).unwrap();
9029        builder.append_null();
9030        builder.append(3).unwrap();
9031        let array: ArrayRef = Arc::new(builder.finish());
9032
9033        let expected = vec!["1", "null", "3"];
9034
9035        // Test casting TO PrimitiveArray, different dictionary type
9036        let cast_array = cast(&array, &Utf8).expect("cast to UTF-8 failed");
9037        assert_eq!(array_to_strings(&cast_array), expected);
9038        assert_eq!(cast_array.data_type(), &Utf8);
9039
9040        let cast_array = cast(&array, &Int64).expect("cast to int64 failed");
9041        assert_eq!(array_to_strings(&cast_array), expected);
9042        assert_eq!(cast_array.data_type(), &Int64);
9043    }
9044
9045    #[test]
9046    fn test_cast_primitive_array_to_dict() {
9047        let mut builder = PrimitiveBuilder::<Int32Type>::new();
9048        builder.append_value(1);
9049        builder.append_null();
9050        builder.append_value(3);
9051        let array: ArrayRef = Arc::new(builder.finish());
9052
9053        let expected = vec!["1", "null", "3"];
9054
9055        // Cast to a dictionary (same value type, Int32)
9056        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int32));
9057        let cast_array = cast(&array, &cast_type).expect("cast failed");
9058        assert_eq!(cast_array.data_type(), &cast_type);
9059        assert_eq!(array_to_strings(&cast_array), expected);
9060
9061        // Cast to a dictionary (different value type, Int8)
9062        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int8));
9063        let cast_array = cast(&array, &cast_type).expect("cast failed");
9064        assert_eq!(cast_array.data_type(), &cast_type);
9065        assert_eq!(array_to_strings(&cast_array), expected);
9066    }
9067
9068    #[test]
9069    fn test_cast_time_array_to_dict() {
9070        use DataType::*;
9071
9072        let array = Arc::new(Date32Array::from(vec![Some(1000), None, Some(2000)])) as ArrayRef;
9073
9074        let expected = vec!["1972-09-27", "null", "1975-06-24"];
9075
9076        let cast_type = Dictionary(Box::new(UInt8), Box::new(Date32));
9077        let cast_array = cast(&array, &cast_type).expect("cast failed");
9078        assert_eq!(cast_array.data_type(), &cast_type);
9079        assert_eq!(array_to_strings(&cast_array), expected);
9080    }
9081
9082    #[test]
9083    fn test_cast_timestamp_array_to_dict() {
9084        use DataType::*;
9085
9086        let array = Arc::new(
9087            TimestampSecondArray::from(vec![Some(1000), None, Some(2000)]).with_timezone_utc(),
9088        ) as ArrayRef;
9089
9090        let expected = vec!["1970-01-01T00:16:40", "null", "1970-01-01T00:33:20"];
9091
9092        let cast_type = Dictionary(Box::new(UInt8), Box::new(Timestamp(TimeUnit::Second, None)));
9093        let cast_array = cast(&array, &cast_type).expect("cast failed");
9094        assert_eq!(cast_array.data_type(), &cast_type);
9095        assert_eq!(array_to_strings(&cast_array), expected);
9096    }
9097
9098    #[test]
9099    fn test_cast_string_array_to_dict() {
9100        use DataType::*;
9101
9102        let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) as ArrayRef;
9103
9104        let expected = vec!["one", "null", "three"];
9105
9106        // Cast to a dictionary (same value type, Utf8)
9107        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
9108        let cast_array = cast(&array, &cast_type).expect("cast failed");
9109        assert_eq!(cast_array.data_type(), &cast_type);
9110        assert_eq!(array_to_strings(&cast_array), expected);
9111    }
9112
9113    #[test]
9114    fn test_cast_null_array_to_from_decimal_array() {
9115        let data_type = DataType::Decimal128(12, 4);
9116        let array = new_null_array(&DataType::Null, 4);
9117        assert_eq!(array.data_type(), &DataType::Null);
9118        let cast_array = cast(&array, &data_type).expect("cast failed");
9119        assert_eq!(cast_array.data_type(), &data_type);
9120        for i in 0..4 {
9121            assert!(cast_array.is_null(i));
9122        }
9123
9124        let array = new_null_array(&data_type, 4);
9125        assert_eq!(array.data_type(), &data_type);
9126        let cast_array = cast(&array, &DataType::Null).expect("cast failed");
9127        assert_eq!(cast_array.data_type(), &DataType::Null);
9128        assert_eq!(cast_array.len(), 4);
9129        assert_eq!(cast_array.logical_nulls().unwrap().null_count(), 4);
9130    }
9131
9132    #[test]
9133    fn test_cast_null_array_from_and_to_primitive_array() {
9134        macro_rules! typed_test {
9135            ($ARR_TYPE:ident, $DATATYPE:ident, $TYPE:tt) => {{
9136                {
9137                    let array = Arc::new(NullArray::new(6)) as ArrayRef;
9138                    let expected = $ARR_TYPE::from(vec![None; 6]);
9139                    let cast_type = DataType::$DATATYPE;
9140                    let cast_array = cast(&array, &cast_type).expect("cast failed");
9141                    let cast_array = cast_array.as_primitive::<$TYPE>();
9142                    assert_eq!(cast_array.data_type(), &cast_type);
9143                    assert_eq!(cast_array, &expected);
9144                }
9145            }};
9146        }
9147
9148        typed_test!(Int16Array, Int16, Int16Type);
9149        typed_test!(Int32Array, Int32, Int32Type);
9150        typed_test!(Int64Array, Int64, Int64Type);
9151
9152        typed_test!(UInt16Array, UInt16, UInt16Type);
9153        typed_test!(UInt32Array, UInt32, UInt32Type);
9154        typed_test!(UInt64Array, UInt64, UInt64Type);
9155
9156        typed_test!(Float16Array, Float16, Float16Type);
9157        typed_test!(Float32Array, Float32, Float32Type);
9158        typed_test!(Float64Array, Float64, Float64Type);
9159
9160        typed_test!(Date32Array, Date32, Date32Type);
9161        typed_test!(Date64Array, Date64, Date64Type);
9162    }
9163
9164    fn cast_from_null_to_other_base(data_type: &DataType, is_complex: bool) {
9165        // Cast from null to data_type
9166        let array = new_null_array(&DataType::Null, 4);
9167        assert_eq!(array.data_type(), &DataType::Null);
9168        let cast_array = cast(&array, data_type).expect("cast failed");
9169        assert_eq!(cast_array.data_type(), data_type);
9170        for i in 0..4 {
9171            if is_complex {
9172                assert!(cast_array.logical_nulls().unwrap().is_null(i));
9173            } else {
9174                assert!(cast_array.is_null(i));
9175            }
9176        }
9177    }
9178
9179    fn cast_from_null_to_other(data_type: &DataType) {
9180        cast_from_null_to_other_base(data_type, false);
9181    }
9182
9183    fn cast_from_null_to_other_complex(data_type: &DataType) {
9184        cast_from_null_to_other_base(data_type, true);
9185    }
9186
9187    #[test]
9188    fn test_cast_null_from_and_to_variable_sized() {
9189        cast_from_null_to_other(&DataType::Utf8);
9190        cast_from_null_to_other(&DataType::LargeUtf8);
9191        cast_from_null_to_other(&DataType::Binary);
9192        cast_from_null_to_other(&DataType::LargeBinary);
9193    }
9194
9195    #[test]
9196    fn test_cast_null_from_and_to_nested_type() {
9197        // Cast null from and to map
9198        let data_type = DataType::Map(
9199            Arc::new(Field::new_struct(
9200                Field::MAP_ENTRIES_FIELD_DEFAULT_NAME,
9201                vec![
9202                    Field::new(Field::MAP_KEY_FIELD_DEFAULT_NAME, DataType::Utf8, false),
9203                    Field::new(Field::MAP_VALUE_FIELD_DEFAULT_NAME, DataType::Int32, true),
9204                ],
9205                false,
9206            )),
9207            false,
9208        );
9209        cast_from_null_to_other(&data_type);
9210
9211        // Cast null from and to list
9212        let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
9213        cast_from_null_to_other(&data_type);
9214        let data_type = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
9215        cast_from_null_to_other(&data_type);
9216        let data_type =
9217            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
9218        cast_from_null_to_other(&data_type);
9219
9220        // Cast null from and to dictionary
9221        let values = vec![None, None, None, None] as Vec<Option<&str>>;
9222        let array: DictionaryArray<Int8Type> = values.into_iter().collect();
9223        let array = Arc::new(array) as ArrayRef;
9224        let data_type = array.data_type().to_owned();
9225        cast_from_null_to_other(&data_type);
9226
9227        // Cast null from and to struct
9228        let data_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
9229        cast_from_null_to_other(&data_type);
9230
9231        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
9232        cast_from_null_to_other(&target_type);
9233
9234        let target_type =
9235            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
9236        cast_from_null_to_other(&target_type);
9237
9238        let fields = UnionFields::from_fields(vec![Field::new("a", DataType::Int64, false)]);
9239        let target_type = DataType::Union(fields, UnionMode::Sparse);
9240        cast_from_null_to_other_complex(&target_type);
9241
9242        let target_type = DataType::RunEndEncoded(
9243            Arc::new(Field::new("item", DataType::Int32, true)),
9244            Arc::new(Field::new("item", DataType::Int32, true)),
9245        );
9246        cast_from_null_to_other_complex(&target_type);
9247    }
9248
9249    /// Print the `DictionaryArray` `array` as a vector of strings
9250    fn array_to_strings(array: &ArrayRef) -> Vec<String> {
9251        let options = FormatOptions::new().with_null("null");
9252        let formatter = ArrayFormatter::try_new(array.as_ref(), &options).unwrap();
9253        (0..array.len())
9254            .map(|i| formatter.value(i).to_string())
9255            .collect()
9256    }
9257
9258    #[test]
9259    fn test_cast_utf8_to_date32() {
9260        use chrono::NaiveDate;
9261        let from_ymd = chrono::NaiveDate::from_ymd_opt;
9262        let since = chrono::NaiveDate::signed_duration_since;
9263
9264        let a = StringArray::from(vec![
9265            "2000-01-01",          // valid date with leading 0s
9266            "2000-01-01T12:00:00", // valid datetime, will throw away the time part
9267            "2000-2-2",            // valid date without leading 0s
9268            "2000-00-00",          // invalid month and day
9269            "2000",                // just a year is invalid
9270        ]);
9271        let array = Arc::new(a) as ArrayRef;
9272        let b = cast(&array, &DataType::Date32).unwrap();
9273        let c = b.as_primitive::<Date32Type>();
9274
9275        // test valid inputs
9276        let date_value = since(
9277            NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
9278            from_ymd(1970, 1, 1).unwrap(),
9279        )
9280        .num_days() as i32;
9281        assert!(c.is_valid(0)); // "2000-01-01"
9282        assert_eq!(date_value, c.value(0));
9283
9284        assert!(c.is_valid(1)); // "2000-01-01T12:00:00"
9285        assert_eq!(date_value, c.value(1));
9286
9287        let date_value = since(
9288            NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
9289            from_ymd(1970, 1, 1).unwrap(),
9290        )
9291        .num_days() as i32;
9292        assert!(c.is_valid(2)); // "2000-2-2"
9293        assert_eq!(date_value, c.value(2));
9294
9295        // test invalid inputs
9296        assert!(!c.is_valid(3)); // "2000-00-00"
9297        assert!(!c.is_valid(4)); // "2000"
9298    }
9299
9300    #[test]
9301    fn test_cast_utf8_to_date64() {
9302        let a = StringArray::from(vec![
9303            "2000-01-01T12:00:00", // date + time valid
9304            "2020-12-15T12:34:56", // date + time valid
9305            "2020-2-2T12:34:56",   // valid date time without leading 0s
9306            "2000-00-00T12:00:00", // invalid month and day
9307            "2000-01-01 12:00:00", // missing the 'T'
9308            "2000-01-01",          // just a date is invalid
9309        ]);
9310        let array = Arc::new(a) as ArrayRef;
9311        let b = cast(&array, &DataType::Date64).unwrap();
9312        let c = b.as_primitive::<Date64Type>();
9313
9314        // test valid inputs
9315        assert!(c.is_valid(0)); // "2000-01-01T12:00:00"
9316        assert_eq!(946728000000, c.value(0));
9317        assert!(c.is_valid(1)); // "2020-12-15T12:34:56"
9318        assert_eq!(1608035696000, c.value(1));
9319        assert!(!c.is_valid(2)); // "2020-2-2T12:34:56"
9320
9321        assert!(!c.is_valid(3)); // "2000-00-00T12:00:00"
9322        assert!(c.is_valid(4)); // "2000-01-01 12:00:00"
9323        assert_eq!(946728000000, c.value(4));
9324        assert!(c.is_valid(5)); // "2000-01-01"
9325        assert_eq!(946684800000, c.value(5));
9326    }
9327
9328    #[test]
9329    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
9330    fn test_can_cast_fsl_to_fsl() {
9331        let from_array = Arc::new(
9332            FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
9333                [Some([Some(1.0), Some(2.0)]), None],
9334                2,
9335            ),
9336        ) as ArrayRef;
9337        let to_array = Arc::new(
9338            FixedSizeListArray::from_iter_primitive::<Float16Type, _, _>(
9339                [
9340                    Some([Some(f16::from_f32(1.0)), Some(f16::from_f32(2.0))]),
9341                    None,
9342                ],
9343                2,
9344            ),
9345        ) as ArrayRef;
9346
9347        assert!(can_cast_types(from_array.data_type(), to_array.data_type()));
9348        let actual = cast(&from_array, to_array.data_type()).unwrap();
9349        assert_eq!(actual.data_type(), to_array.data_type());
9350
9351        let invalid_target =
9352            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Binary, true)), 2);
9353        assert!(!can_cast_types(from_array.data_type(), &invalid_target));
9354
9355        let invalid_size =
9356            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Float16, true)), 5);
9357        assert!(!can_cast_types(from_array.data_type(), &invalid_size));
9358    }
9359
9360    #[test]
9361    fn test_can_cast_types_fixed_size_list_to_list() {
9362        // DataType::List
9363        let array1 = make_fixed_size_list_array();
9364        assert!(can_cast_types(
9365            array1.data_type(),
9366            &DataType::List(Arc::new(Field::new("", DataType::Int32, false)))
9367        ));
9368
9369        // DataType::LargeList
9370        let array2 = make_fixed_size_list_array_for_large_list();
9371        assert!(can_cast_types(
9372            array2.data_type(),
9373            &DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false)))
9374        ));
9375    }
9376
9377    #[test]
9378    fn test_cast_fixed_size_list_to_list() {
9379        // Important cases:
9380        // 1. With/without nulls
9381        // 2. List/LargeList/ListView/LargeListView
9382        // 3. With and without inner casts
9383
9384        let cases = [
9385            // fixed_size_list<i32, 2> => list<i32>
9386            (
9387                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9388                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9389                    2,
9390                )) as ArrayRef,
9391                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
9392                    Some([Some(1), Some(1)]),
9393                    Some([Some(2), Some(2)]),
9394                ])) as ArrayRef,
9395            ),
9396            // fixed_size_list<i32, 2> => list<i32> (nullable)
9397            (
9398                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9399                    [None, Some([Some(2), Some(2)])],
9400                    2,
9401                )) as ArrayRef,
9402                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
9403                    None,
9404                    Some([Some(2), Some(2)]),
9405                ])) as ArrayRef,
9406            ),
9407            // fixed_size_list<i32, 2> => large_list<i64>
9408            (
9409                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9410                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9411                    2,
9412                )) as ArrayRef,
9413                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
9414                    Some([Some(1), Some(1)]),
9415                    Some([Some(2), Some(2)]),
9416                ])) as ArrayRef,
9417            ),
9418            // fixed_size_list<i32, 2> => large_list<i64> (nullable)
9419            (
9420                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9421                    [None, Some([Some(2), Some(2)])],
9422                    2,
9423                )) as ArrayRef,
9424                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
9425                    None,
9426                    Some([Some(2), Some(2)]),
9427                ])) as ArrayRef,
9428            ),
9429            // fixed_size_list<i32, 2> => list_view<i32>
9430            (
9431                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9432                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9433                    2,
9434                )) as ArrayRef,
9435                Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>([
9436                    Some([Some(1), Some(1)]),
9437                    Some([Some(2), Some(2)]),
9438                ])) as ArrayRef,
9439            ),
9440            // fixed_size_list<i32, 2> => list_view<i32> (nullable)
9441            (
9442                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9443                    [None, Some([Some(2), Some(2)])],
9444                    2,
9445                )) as ArrayRef,
9446                Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>([
9447                    None,
9448                    Some([Some(2), Some(2)]),
9449                ])) as ArrayRef,
9450            ),
9451            // fixed_size_list<i32, 2> => large_list_view<i64>
9452            (
9453                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9454                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9455                    2,
9456                )) as ArrayRef,
9457                Arc::new(LargeListViewArray::from_iter_primitive::<Int64Type, _, _>(
9458                    [Some([Some(1), Some(1)]), Some([Some(2), Some(2)])],
9459                )) as ArrayRef,
9460            ),
9461            // fixed_size_list<i32, 2> => large_list_view<i64> (nullable)
9462            (
9463                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9464                    [None, Some([Some(2), Some(2)])],
9465                    2,
9466                )) as ArrayRef,
9467                Arc::new(LargeListViewArray::from_iter_primitive::<Int64Type, _, _>(
9468                    [None, Some([Some(2), Some(2)])],
9469                )) as ArrayRef,
9470            ),
9471        ];
9472
9473        for (array, expected) in cases {
9474            assert!(
9475                can_cast_types(array.data_type(), expected.data_type()),
9476                "can_cast_types claims we cannot cast {:?} to {:?}",
9477                array.data_type(),
9478                expected.data_type()
9479            );
9480
9481            let list_array = cast(&array, expected.data_type())
9482                .unwrap_or_else(|_| panic!("Failed to cast {array:?} to {expected:?}"));
9483            assert_eq!(
9484                list_array.as_ref(),
9485                &expected,
9486                "Incorrect result from casting {array:?} to {expected:?}",
9487            );
9488        }
9489    }
9490
9491    #[test]
9492    fn test_cast_fixed_size_list_to_list_preserves_field_metadata() {
9493        use std::collections::HashMap;
9494
9495        let metadata: HashMap<String, String> =
9496            HashMap::from([("PARQUET:field_id".to_string(), "89".to_string())]);
9497
9498        let src = Arc::new(
9499            FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
9500                [[1.0_f32, 2.0].map(Some), [3.0, 4.0].map(Some)].map(Some),
9501                2,
9502            ),
9503        ) as ArrayRef;
9504
9505        let target_field = Arc::new(
9506            Field::new("element", DataType::Float32, true).with_metadata(metadata.clone()),
9507        );
9508
9509        let target_types = [
9510            DataType::List(target_field.clone()),
9511            DataType::LargeList(target_field.clone()),
9512            DataType::ListView(target_field.clone()),
9513            DataType::LargeListView(target_field.clone()),
9514        ];
9515
9516        for target_type in &target_types {
9517            let result = cast(&src, target_type).unwrap();
9518            assert_eq!(
9519                result.data_type(),
9520                target_type,
9521                "Cast to {target_type:?} should preserve field metadata"
9522            );
9523        }
9524    }
9525
9526    #[test]
9527    fn test_cast_utf8_to_list() {
9528        // DataType::List
9529        let array = Arc::new(StringArray::from(vec!["5"])) as ArrayRef;
9530        let field = Arc::new(Field::new("", DataType::Int32, false));
9531        let list_array = cast(&array, &DataType::List(field.clone())).unwrap();
9532        let actual = list_array.as_list_opt::<i32>().unwrap();
9533        let expect = ListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
9534        assert_eq!(&expect.value(0), &actual.value(0));
9535
9536        // DataType::LargeList
9537        let list_array = cast(&array, &DataType::LargeList(field.clone())).unwrap();
9538        let actual = list_array.as_list_opt::<i64>().unwrap();
9539        let expect = LargeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
9540        assert_eq!(&expect.value(0), &actual.value(0));
9541
9542        // DataType::FixedSizeList
9543        let list_array = cast(&array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9544        let actual = list_array.as_fixed_size_list_opt().unwrap();
9545        let expect =
9546            FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])], 1);
9547        assert_eq!(&expect.value(0), &actual.value(0));
9548    }
9549
9550    #[test]
9551    fn test_cast_single_element_fixed_size_list() {
9552        // FixedSizeList<T>[1] => T
9553        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9554            [(Some([Some(5)]))],
9555            1,
9556        )) as ArrayRef;
9557        let casted_array = cast(&from_array, &DataType::Int32).unwrap();
9558        let actual: &Int32Array = casted_array.as_primitive();
9559        let expected = Int32Array::from(vec![Some(5)]);
9560        assert_eq!(&expected, actual);
9561
9562        // FixedSizeList<T>[1] => FixedSizeList<U>[1]
9563        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9564            [(Some([Some(5)]))],
9565            1,
9566        )) as ArrayRef;
9567        let to_field = Arc::new(Field::new("dummy", DataType::Float32, false));
9568        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
9569        let expected = Arc::new(FixedSizeListArray::new(
9570            to_field.clone(),
9571            1,
9572            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9573            None,
9574        )) as ArrayRef;
9575        assert_eq!(*expected, *actual);
9576
9577        // FixedSizeList<T>[1] => FixedSizeList<FixdSizedList<U>[1]>[1]
9578        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9579            [(Some([Some(5)]))],
9580            1,
9581        )) as ArrayRef;
9582        let to_field_inner = Arc::new(Field::new_list_field(DataType::Float32, false));
9583        let to_field = Arc::new(Field::new(
9584            "dummy",
9585            DataType::FixedSizeList(to_field_inner.clone(), 1),
9586            false,
9587        ));
9588        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
9589        let expected = Arc::new(FixedSizeListArray::new(
9590            to_field.clone(),
9591            1,
9592            Arc::new(FixedSizeListArray::new(
9593                to_field_inner.clone(),
9594                1,
9595                Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9596                None,
9597            )) as ArrayRef,
9598            None,
9599        )) as ArrayRef;
9600        assert_eq!(*expected, *actual);
9601
9602        // T => FixedSizeList<T>[1] (non-nullable)
9603        let field = Arc::new(Field::new("dummy", DataType::Float32, false));
9604        let from_array = Arc::new(Int8Array::from(vec![Some(5)])) as ArrayRef;
9605        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9606        let actual = casted_array.as_fixed_size_list();
9607        let expected = Arc::new(FixedSizeListArray::new(
9608            field.clone(),
9609            1,
9610            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9611            None,
9612        )) as ArrayRef;
9613        assert_eq!(expected.as_ref(), actual);
9614
9615        // T => FixedSizeList<T>[1] (nullable)
9616        let field = Arc::new(Field::new("nullable", DataType::Float32, true));
9617        let from_array = Arc::new(Int8Array::from(vec![None])) as ArrayRef;
9618        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9619        let actual = casted_array.as_fixed_size_list();
9620        let expected = Arc::new(FixedSizeListArray::new(
9621            field.clone(),
9622            1,
9623            Arc::new(Float32Array::from(vec![None])) as ArrayRef,
9624            None,
9625        )) as ArrayRef;
9626        assert_eq!(expected.as_ref(), actual);
9627    }
9628
9629    #[test]
9630    fn test_cast_list_containers() {
9631        // large-list to list
9632        let array = make_large_list_array();
9633        let list_array = cast(
9634            &array,
9635            &DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
9636        )
9637        .unwrap();
9638        let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
9639        let expected = array.as_any().downcast_ref::<LargeListArray>().unwrap();
9640
9641        assert_eq!(&expected.value(0), &actual.value(0));
9642        assert_eq!(&expected.value(1), &actual.value(1));
9643        assert_eq!(&expected.value(2), &actual.value(2));
9644
9645        // list to large-list
9646        let array = make_list_array();
9647        let large_list_array = cast(
9648            &array,
9649            &DataType::LargeList(Arc::new(Field::new("", DataType::Int32, false))),
9650        )
9651        .unwrap();
9652        let actual = large_list_array
9653            .as_any()
9654            .downcast_ref::<LargeListArray>()
9655            .unwrap();
9656        let expected = array.as_any().downcast_ref::<ListArray>().unwrap();
9657
9658        assert_eq!(&expected.value(0), &actual.value(0));
9659        assert_eq!(&expected.value(1), &actual.value(1));
9660        assert_eq!(&expected.value(2), &actual.value(2));
9661    }
9662
9663    #[test]
9664    fn test_cast_list_view() {
9665        // cast between list view and list view
9666        let array = make_list_view_array();
9667        let to = DataType::ListView(Field::new_list_field(DataType::Float32, true).into());
9668        assert!(can_cast_types(array.data_type(), &to));
9669        let actual = cast(&array, &to).unwrap();
9670        let actual = actual.as_list_view::<i32>();
9671
9672        assert_eq!(
9673            &Float32Array::from(vec![0.0, 1.0, 2.0]) as &dyn Array,
9674            actual.value(0).as_ref()
9675        );
9676        assert_eq!(
9677            &Float32Array::from(vec![3.0, 4.0, 5.0]) as &dyn Array,
9678            actual.value(1).as_ref()
9679        );
9680        assert_eq!(
9681            &Float32Array::from(vec![6.0, 7.0]) as &dyn Array,
9682            actual.value(2).as_ref()
9683        );
9684
9685        // cast between large list view and large list view
9686        let array = make_large_list_view_array();
9687        let to = DataType::LargeListView(Field::new_list_field(DataType::Float32, true).into());
9688        assert!(can_cast_types(array.data_type(), &to));
9689        let actual = cast(&array, &to).unwrap();
9690        let actual = actual.as_list_view::<i64>();
9691
9692        assert_eq!(
9693            &Float32Array::from(vec![0.0, 1.0, 2.0]) as &dyn Array,
9694            actual.value(0).as_ref()
9695        );
9696        assert_eq!(
9697            &Float32Array::from(vec![3.0, 4.0, 5.0]) as &dyn Array,
9698            actual.value(1).as_ref()
9699        );
9700        assert_eq!(
9701            &Float32Array::from(vec![6.0, 7.0]) as &dyn Array,
9702            actual.value(2).as_ref()
9703        );
9704    }
9705
9706    #[test]
9707    fn test_non_list_to_list_view() {
9708        let input = Arc::new(Int32Array::from(vec![Some(0), None, Some(2)])) as ArrayRef;
9709        let expected_primitive =
9710            Arc::new(Float32Array::from(vec![Some(0.0), None, Some(2.0)])) as ArrayRef;
9711
9712        // [[0], [NULL], [2]]
9713        let expected = ListViewArray::new(
9714            Field::new_list_field(DataType::Float32, true).into(),
9715            vec![0, 1, 2].into(),
9716            vec![1, 1, 1].into(),
9717            expected_primitive.clone(),
9718            None,
9719        );
9720        assert!(can_cast_types(input.data_type(), expected.data_type()));
9721        let actual = cast(&input, expected.data_type()).unwrap();
9722        assert_eq!(actual.as_ref(), &expected);
9723
9724        // [[0], [NULL], [2]]
9725        let expected = LargeListViewArray::new(
9726            Field::new_list_field(DataType::Float32, true).into(),
9727            vec![0, 1, 2].into(),
9728            vec![1, 1, 1].into(),
9729            expected_primitive.clone(),
9730            None,
9731        );
9732        assert!(can_cast_types(input.data_type(), expected.data_type()));
9733        let actual = cast(&input, expected.data_type()).unwrap();
9734        assert_eq!(actual.as_ref(), &expected);
9735    }
9736
9737    #[test]
9738    fn test_cast_list_to_zero_size_fsl() {
9739        let field = Arc::new(Field::new("a", DataType::Null, true));
9740        let length = 2;
9741        let expected = Arc::new(
9742            FixedSizeListArray::try_new_with_length(
9743                field.clone(),
9744                0,
9745                new_empty_array(&DataType::Null),
9746                None,
9747                2,
9748            )
9749            .unwrap(),
9750        ) as ArrayRef;
9751
9752        let list = Arc::new(ListArray::new(
9753            field.clone(),
9754            OffsetBuffer::from_repeated_length(0, length),
9755            new_empty_array(&DataType::Null),
9756            None,
9757        ));
9758        let fsl = cast(list.as_ref(), expected.data_type()).unwrap();
9759        assert_eq!(&expected, &fsl);
9760
9761        let list = Arc::new(ListViewArray::new(
9762            field.clone(),
9763            vec![0; length].into(),
9764            vec![0; length].into(),
9765            new_empty_array(&DataType::Null),
9766            None,
9767        ));
9768        let fsl = cast(list.as_ref(), expected.data_type()).unwrap();
9769        assert_eq!(&expected, &fsl);
9770    }
9771
9772    #[test]
9773    fn test_cast_list_to_fsl() {
9774        // There four noteworthy cases we should handle:
9775        // 1. No nulls
9776        // 2. Nulls that are always empty
9777        // 3. Nulls that have varying lengths
9778        // 4. Nulls that are correctly sized (same as target list size)
9779
9780        // Non-null case
9781        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9782        let values = vec![
9783            Some(vec![Some(1), Some(2), Some(3)]),
9784            Some(vec![Some(4), Some(5), Some(6)]),
9785        ];
9786        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
9787            values.clone(),
9788        )) as ArrayRef;
9789        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9790            values, 3,
9791        )) as ArrayRef;
9792        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9793        assert_eq!(expected.as_ref(), actual.as_ref());
9794
9795        // Null cases
9796        // Array is [[1, 2, 3], null, [4, 5, 6], null]
9797        let cases = [
9798            (
9799                // Zero-length nulls
9800                vec![1, 2, 3, 4, 5, 6],
9801                vec![3, 0, 3, 0],
9802            ),
9803            (
9804                // Varying-length nulls
9805                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
9806                vec![3, 2, 3, 1],
9807            ),
9808            (
9809                // Correctly-sized nulls
9810                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
9811                vec![3, 3, 3, 3],
9812            ),
9813            (
9814                // Mixed nulls
9815                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
9816                vec![3, 0, 3, 3],
9817            ),
9818        ];
9819        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
9820
9821        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9822            vec![
9823                Some(vec![Some(1), Some(2), Some(3)]),
9824                None,
9825                Some(vec![Some(4), Some(5), Some(6)]),
9826                None,
9827            ],
9828            3,
9829        )) as ArrayRef;
9830
9831        for (values, lengths) in cases.iter() {
9832            let array = Arc::new(ListArray::new(
9833                field.clone(),
9834                OffsetBuffer::from_lengths(lengths.clone()),
9835                Arc::new(Int32Array::from(values.clone())),
9836                Some(null_buffer.clone()),
9837            )) as ArrayRef;
9838            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9839            assert_eq!(expected.as_ref(), actual.as_ref());
9840        }
9841    }
9842
9843    #[test]
9844    fn test_cast_list_view_to_fsl() {
9845        // There four noteworthy cases we should handle:
9846        // 1. No nulls
9847        // 2. Nulls that are always empty
9848        // 3. Nulls that have varying lengths
9849        // 4. Nulls that are correctly sized (same as target list size)
9850
9851        // Non-null case
9852        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9853        let values = vec![
9854            Some(vec![Some(1), Some(2), Some(3)]),
9855            Some(vec![Some(4), Some(5), Some(6)]),
9856        ];
9857        let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(
9858            values.clone(),
9859        )) as ArrayRef;
9860        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9861            values, 3,
9862        )) as ArrayRef;
9863        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9864        assert_eq!(expected.as_ref(), actual.as_ref());
9865
9866        // Null cases
9867        // Array is [[1, 2, 3], null, [4, 5, 6], null]
9868        let cases = [
9869            (
9870                // Zero-length nulls
9871                vec![1, 2, 3, 4, 5, 6],
9872                vec![0, 0, 3, 0],
9873                vec![3, 0, 3, 0],
9874            ),
9875            (
9876                // Varying-length nulls
9877                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
9878                vec![0, 1, 5, 0],
9879                vec![3, 2, 3, 1],
9880            ),
9881            (
9882                // Correctly-sized nulls
9883                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
9884                vec![0, 3, 6, 9],
9885                vec![3, 3, 3, 3],
9886            ),
9887            (
9888                // Mixed nulls
9889                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
9890                vec![0, 0, 3, 6],
9891                vec![3, 0, 3, 3],
9892            ),
9893        ];
9894        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
9895
9896        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9897            vec![
9898                Some(vec![Some(1), Some(2), Some(3)]),
9899                None,
9900                Some(vec![Some(4), Some(5), Some(6)]),
9901                None,
9902            ],
9903            3,
9904        )) as ArrayRef;
9905
9906        for (values, offsets, lengths) in cases.iter() {
9907            let array = Arc::new(ListViewArray::new(
9908                field.clone(),
9909                offsets.clone().into(),
9910                lengths.clone().into(),
9911                Arc::new(Int32Array::from(values.clone())),
9912                Some(null_buffer.clone()),
9913            )) as ArrayRef;
9914            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9915            assert_eq!(expected.as_ref(), actual.as_ref());
9916        }
9917    }
9918
9919    #[test]
9920    fn test_cast_list_to_fsl_safety() {
9921        let values = vec![
9922            Some(vec![Some(1), Some(2), Some(3)]),
9923            Some(vec![Some(4), Some(5)]),
9924            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
9925            Some(vec![Some(3), Some(4), Some(5)]),
9926        ];
9927        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
9928            values.clone(),
9929        )) as ArrayRef;
9930
9931        let res = cast_with_options(
9932            array.as_ref(),
9933            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9934            &CastOptions {
9935                safe: false,
9936                ..Default::default()
9937            },
9938        );
9939        assert!(res.is_err());
9940        assert!(
9941            format!("{res:?}")
9942                .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
9943        );
9944
9945        // When safe=true (default), the cast will fill nulls for lists that are
9946        // too short and truncate lists that are too long.
9947        let res = cast(
9948            array.as_ref(),
9949            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9950        )
9951        .unwrap();
9952        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9953            vec![
9954                Some(vec![Some(1), Some(2), Some(3)]),
9955                None, // Too short -> replaced with null
9956                None, // Too long -> replaced with null
9957                Some(vec![Some(3), Some(4), Some(5)]),
9958            ],
9959            3,
9960        )) as ArrayRef;
9961        assert_eq!(expected.as_ref(), res.as_ref());
9962
9963        // The safe option is false and the source array contains a null list.
9964        // issue: https://github.com/apache/arrow-rs/issues/5642
9965        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
9966            Some(vec![Some(1), Some(2), Some(3)]),
9967            None,
9968        ])) as ArrayRef;
9969        let res = cast_with_options(
9970            array.as_ref(),
9971            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9972            &CastOptions {
9973                safe: false,
9974                ..Default::default()
9975            },
9976        )
9977        .unwrap();
9978        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9979            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
9980            3,
9981        )) as ArrayRef;
9982        assert_eq!(expected.as_ref(), res.as_ref());
9983    }
9984
9985    #[test]
9986    fn test_cast_list_view_to_fsl_safety() {
9987        let values = vec![
9988            Some(vec![Some(1), Some(2), Some(3)]),
9989            Some(vec![Some(4), Some(5)]),
9990            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
9991            Some(vec![Some(3), Some(4), Some(5)]),
9992        ];
9993        let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(
9994            values.clone(),
9995        )) as ArrayRef;
9996
9997        let res = cast_with_options(
9998            array.as_ref(),
9999            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
10000            &CastOptions {
10001                safe: false,
10002                ..Default::default()
10003            },
10004        );
10005        assert!(res.is_err());
10006        assert!(
10007            format!("{res:?}")
10008                .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
10009        );
10010
10011        // When safe=true (default), the cast will fill nulls for lists that are
10012        // too short and truncate lists that are too long.
10013        let res = cast(
10014            array.as_ref(),
10015            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
10016        )
10017        .unwrap();
10018        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
10019            vec![
10020                Some(vec![Some(1), Some(2), Some(3)]),
10021                None, // Too short -> replaced with null
10022                None, // Too long -> replaced with null
10023                Some(vec![Some(3), Some(4), Some(5)]),
10024            ],
10025            3,
10026        )) as ArrayRef;
10027        assert_eq!(expected.as_ref(), res.as_ref());
10028
10029        // The safe option is false and the source array contains a null list.
10030        // issue: https://github.com/apache/arrow-rs/issues/5642
10031        let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(vec![
10032            Some(vec![Some(1), Some(2), Some(3)]),
10033            None,
10034        ])) as ArrayRef;
10035        let res = cast_with_options(
10036            array.as_ref(),
10037            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
10038            &CastOptions {
10039                safe: false,
10040                ..Default::default()
10041            },
10042        )
10043        .unwrap();
10044        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
10045            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
10046            3,
10047        )) as ArrayRef;
10048        assert_eq!(expected.as_ref(), res.as_ref());
10049    }
10050
10051    #[test]
10052    fn test_cast_large_list_to_fsl() {
10053        let values = vec![Some(vec![Some(1), Some(2)]), Some(vec![Some(3), Some(4)])];
10054        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
10055            values.clone(),
10056            2,
10057        )) as ArrayRef;
10058        let target_type =
10059            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 2);
10060
10061        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
10062            values.clone(),
10063        )) as ArrayRef;
10064        let actual = cast(array.as_ref(), &target_type).unwrap();
10065        assert_eq!(expected.as_ref(), actual.as_ref());
10066
10067        let array = Arc::new(LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(
10068            values.clone(),
10069        )) as ArrayRef;
10070        let actual = cast(array.as_ref(), &target_type).unwrap();
10071        assert_eq!(expected.as_ref(), actual.as_ref());
10072    }
10073
10074    #[test]
10075    fn test_cast_list_to_fsl_subcast() {
10076        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
10077            vec![
10078                Some(vec![Some(1), Some(2)]),
10079                Some(vec![Some(3), Some(i32::MAX)]),
10080            ],
10081        )) as ArrayRef;
10082        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
10083            vec![
10084                Some(vec![Some(1), Some(2)]),
10085                Some(vec![Some(3), Some(i32::MAX as i64)]),
10086            ],
10087            2,
10088        )) as ArrayRef;
10089        let actual = cast(
10090            array.as_ref(),
10091            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 2),
10092        )
10093        .unwrap();
10094        assert_eq!(expected.as_ref(), actual.as_ref());
10095
10096        let res = cast_with_options(
10097            array.as_ref(),
10098            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int16, true)), 2),
10099            &CastOptions {
10100                safe: false,
10101                ..Default::default()
10102            },
10103        );
10104        assert!(res.is_err());
10105        assert!(format!("{res:?}").contains("Can't cast value 2147483647 to type Int16"));
10106    }
10107
10108    #[test]
10109    fn test_cast_list_to_fsl_empty() {
10110        let inner_field = Arc::new(Field::new_list_field(DataType::Int32, true));
10111        let target_type = DataType::FixedSizeList(inner_field.clone(), 3);
10112        let expected = new_empty_array(&target_type);
10113
10114        // list
10115        let array = new_empty_array(&DataType::List(inner_field.clone()));
10116        assert!(can_cast_types(array.data_type(), &target_type));
10117        let actual = cast(array.as_ref(), &target_type).unwrap();
10118        assert_eq!(expected.as_ref(), actual.as_ref());
10119
10120        // largelist
10121        let array = new_empty_array(&DataType::LargeList(inner_field.clone()));
10122        assert!(can_cast_types(array.data_type(), &target_type));
10123        let actual = cast(array.as_ref(), &target_type).unwrap();
10124        assert_eq!(expected.as_ref(), actual.as_ref());
10125
10126        // listview
10127        let array = new_empty_array(&DataType::ListView(inner_field.clone()));
10128        assert!(can_cast_types(array.data_type(), &target_type));
10129        let actual = cast(array.as_ref(), &target_type).unwrap();
10130        assert_eq!(expected.as_ref(), actual.as_ref());
10131
10132        // largelistview
10133        let array = new_empty_array(&DataType::LargeListView(inner_field.clone()));
10134        assert!(can_cast_types(array.data_type(), &target_type));
10135        let actual = cast(array.as_ref(), &target_type).unwrap();
10136        assert_eq!(expected.as_ref(), actual.as_ref());
10137    }
10138
10139    fn make_list_array() -> ArrayRef {
10140        // [[0, 1, 2], [3, 4, 5], [6, 7]]
10141        Arc::new(ListArray::new(
10142            Field::new_list_field(DataType::Int32, true).into(),
10143            OffsetBuffer::from_lengths(vec![3, 3, 2]),
10144            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10145            None,
10146        ))
10147    }
10148
10149    fn make_large_list_array() -> ArrayRef {
10150        // [[0, 1, 2], [3, 4, 5], [6, 7]]
10151        Arc::new(LargeListArray::new(
10152            Field::new_list_field(DataType::Int32, true).into(),
10153            OffsetBuffer::from_lengths(vec![3, 3, 2]),
10154            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10155            None,
10156        ))
10157    }
10158
10159    fn make_list_view_array() -> ArrayRef {
10160        // [[0, 1, 2], [3, 4, 5], [6, 7]]
10161        Arc::new(ListViewArray::new(
10162            Field::new_list_field(DataType::Int32, true).into(),
10163            vec![0, 3, 6].into(),
10164            vec![3, 3, 2].into(),
10165            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10166            None,
10167        ))
10168    }
10169
10170    fn make_large_list_view_array() -> ArrayRef {
10171        // [[0, 1, 2], [3, 4, 5], [6, 7]]
10172        Arc::new(LargeListViewArray::new(
10173            Field::new_list_field(DataType::Int32, true).into(),
10174            vec![0, 3, 6].into(),
10175            vec![3, 3, 2].into(),
10176            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10177            None,
10178        ))
10179    }
10180
10181    fn make_fixed_size_list_array() -> ArrayRef {
10182        // [[0, 1, 2, 3], [4, 5, 6, 7]]
10183        Arc::new(FixedSizeListArray::new(
10184            Field::new_list_field(DataType::Int32, true).into(),
10185            4,
10186            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10187            None,
10188        ))
10189    }
10190
10191    fn make_fixed_size_list_array_for_large_list() -> ArrayRef {
10192        // [[0, 1, 2, 3], [4, 5, 6, 7]]
10193        Arc::new(FixedSizeListArray::new(
10194            Field::new_list_field(DataType::Int64, true).into(),
10195            4,
10196            Arc::new(Int64Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10197            None,
10198        ))
10199    }
10200
10201    #[test]
10202    fn test_cast_map_dont_allow_change_of_order() {
10203        let string_builder = StringBuilder::new();
10204        let value_builder = StringBuilder::new();
10205        let mut builder = MapBuilder::new(None, string_builder, value_builder);
10206
10207        builder.keys().append_value("0");
10208        builder.values().append_value("test_val_1");
10209        builder.append(true).unwrap();
10210        builder.keys().append_value("1");
10211        builder.values().append_value("test_val_2");
10212        builder.append(true).unwrap();
10213
10214        // map builder returns unsorted map by default
10215        let array = builder.finish();
10216
10217        let new_ordered = true;
10218        let new_type = DataType::Map(
10219            Arc::new(Field::new(
10220                Field::MAP_ENTRIES_FIELD_DEFAULT_NAME,
10221                DataType::Struct(
10222                    vec![
10223                        Field::new(Field::MAP_KEY_FIELD_DEFAULT_NAME, DataType::Utf8, false),
10224                        Field::new(Field::MAP_VALUE_FIELD_DEFAULT_NAME, DataType::Utf8, false),
10225                    ]
10226                    .into(),
10227                ),
10228                false,
10229            )),
10230            new_ordered,
10231        );
10232
10233        let new_array_result = cast(&array, &new_type.clone());
10234        assert!(!can_cast_types(array.data_type(), &new_type));
10235        let Err(ArrowError::CastError(t)) = new_array_result else {
10236            panic!();
10237        };
10238        assert_eq!(
10239            t,
10240            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"#
10241        );
10242    }
10243
10244    #[test]
10245    fn test_cast_map_dont_allow_when_container_cant_cast() {
10246        let string_builder = StringBuilder::new();
10247        let value_builder = IntervalDayTimeArray::builder(2);
10248        let mut builder = MapBuilder::new(None, string_builder, value_builder);
10249
10250        builder.keys().append_value("0");
10251        builder.values().append_value(IntervalDayTime::new(1, 1));
10252        builder.append(true).unwrap();
10253        builder.keys().append_value("1");
10254        builder.values().append_value(IntervalDayTime::new(2, 2));
10255        builder.append(true).unwrap();
10256
10257        // map builder returns unsorted map by default
10258        let array = builder.finish();
10259
10260        let new_ordered = true;
10261        let new_type = DataType::Map(
10262            Arc::new(Field::new(
10263                Field::MAP_ENTRIES_FIELD_DEFAULT_NAME,
10264                DataType::Struct(
10265                    vec![
10266                        Field::new(Field::MAP_KEY_FIELD_DEFAULT_NAME, DataType::Utf8, false),
10267                        Field::new(
10268                            Field::MAP_VALUE_FIELD_DEFAULT_NAME,
10269                            DataType::Duration(TimeUnit::Second),
10270                            false,
10271                        ),
10272                    ]
10273                    .into(),
10274                ),
10275                false,
10276            )),
10277            new_ordered,
10278        );
10279
10280        let new_array_result = cast(&array, &new_type.clone());
10281        assert!(!can_cast_types(array.data_type(), &new_type));
10282        let Err(ArrowError::CastError(t)) = new_array_result else {
10283            panic!();
10284        };
10285        assert_eq!(
10286            t,
10287            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"#
10288        );
10289    }
10290
10291    #[test]
10292    fn test_cast_map_field_names() {
10293        let string_builder = StringBuilder::new();
10294        let value_builder = StringBuilder::new();
10295        let mut builder = MapBuilder::new(
10296            Some(MapFieldNames {
10297                // Explicitly writing the name so it will be apparent from what names to what names are we converting to
10298                entry: Field::MAP_ENTRIES_FIELD_DEFAULT_NAME.to_string(),
10299                key: Field::MAP_KEY_FIELD_DEFAULT_NAME.to_string(),
10300                value: Field::MAP_VALUE_FIELD_DEFAULT_NAME.to_string(),
10301            }),
10302            string_builder,
10303            value_builder,
10304        );
10305
10306        builder.keys().append_value("0");
10307        builder.values().append_value("test_val_1");
10308        builder.append(true).unwrap();
10309        builder.keys().append_value("1");
10310        builder.values().append_value("test_val_2");
10311        builder.append(true).unwrap();
10312        builder.append(false).unwrap();
10313
10314        let array = builder.finish();
10315
10316        let new_type = DataType::Map(
10317            Arc::new(Field::new(
10318                "entries_new",
10319                DataType::Struct(
10320                    vec![
10321                        Field::new("key_new", DataType::Utf8, false),
10322                        Field::new("value_values", DataType::Utf8, false),
10323                    ]
10324                    .into(),
10325                ),
10326                false,
10327            )),
10328            false,
10329        );
10330
10331        assert_ne!(new_type, array.data_type().clone());
10332
10333        let new_array = cast(&array, &new_type.clone()).unwrap();
10334        assert_eq!(new_type, new_array.data_type().clone());
10335        let map_array = new_array.as_map();
10336
10337        assert_ne!(new_type, array.data_type().clone());
10338        assert_eq!(new_type, map_array.data_type().clone());
10339
10340        let key_string = map_array
10341            .keys()
10342            .as_any()
10343            .downcast_ref::<StringArray>()
10344            .unwrap()
10345            .into_iter()
10346            .flatten()
10347            .collect::<Vec<_>>();
10348        assert_eq!(&key_string, &vec!["0", "1"]);
10349
10350        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
10351        let values_string = values_string_array
10352            .as_any()
10353            .downcast_ref::<StringArray>()
10354            .unwrap()
10355            .into_iter()
10356            .flatten()
10357            .collect::<Vec<_>>();
10358        assert_eq!(&values_string, &vec!["test_val_1", "test_val_2"]);
10359
10360        assert_eq!(
10361            map_array.nulls(),
10362            Some(&NullBuffer::from(vec![true, true, false]))
10363        );
10364    }
10365
10366    #[test]
10367    fn test_cast_map_contained_values() {
10368        let string_builder = StringBuilder::new();
10369        let value_builder = Int8Builder::new();
10370        let mut builder = MapBuilder::new(None, string_builder, value_builder);
10371
10372        builder.keys().append_value("0");
10373        builder.values().append_value(44);
10374        builder.append(true).unwrap();
10375        builder.keys().append_value("1");
10376        builder.values().append_value(22);
10377        builder.append(true).unwrap();
10378
10379        let array = builder.finish();
10380
10381        let new_type = DataType::Map(
10382            Arc::new(Field::new(
10383                Field::MAP_ENTRIES_FIELD_DEFAULT_NAME,
10384                DataType::Struct(
10385                    vec![
10386                        Field::new(Field::MAP_KEY_FIELD_DEFAULT_NAME, DataType::Utf8, false),
10387                        Field::new(Field::MAP_VALUE_FIELD_DEFAULT_NAME, DataType::Utf8, false),
10388                    ]
10389                    .into(),
10390                ),
10391                false,
10392            )),
10393            false,
10394        );
10395
10396        let new_array = cast(&array, &new_type.clone()).unwrap();
10397        assert_eq!(new_type, new_array.data_type().clone());
10398        let map_array = new_array.as_map();
10399
10400        assert_ne!(new_type, array.data_type().clone());
10401        assert_eq!(new_type, map_array.data_type().clone());
10402
10403        let key_string = map_array
10404            .keys()
10405            .as_any()
10406            .downcast_ref::<StringArray>()
10407            .unwrap()
10408            .into_iter()
10409            .flatten()
10410            .collect::<Vec<_>>();
10411        assert_eq!(&key_string, &vec!["0", "1"]);
10412
10413        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
10414        let values_string = values_string_array
10415            .as_any()
10416            .downcast_ref::<StringArray>()
10417            .unwrap()
10418            .into_iter()
10419            .flatten()
10420            .collect::<Vec<_>>();
10421        assert_eq!(&values_string, &vec!["44", "22"]);
10422    }
10423
10424    #[test]
10425    fn test_utf8_cast_offsets() {
10426        // test if offset of the array is taken into account during cast
10427        let str_array = StringArray::from(vec!["a", "b", "c"]);
10428        let str_array = str_array.slice(1, 2);
10429
10430        let out = cast(&str_array, &DataType::LargeUtf8).unwrap();
10431
10432        let large_str_array = out.as_any().downcast_ref::<LargeStringArray>().unwrap();
10433        let strs = large_str_array.into_iter().flatten().collect::<Vec<_>>();
10434        assert_eq!(strs, &["b", "c"])
10435    }
10436
10437    #[test]
10438    fn test_list_cast_offsets() {
10439        // test if offset of the array is taken into account during cast
10440        let array1 = make_list_array().slice(1, 2);
10441        let array2 = make_list_array();
10442
10443        let dt = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
10444        let out1 = cast(&array1, &dt).unwrap();
10445        let out2 = cast(&array2, &dt).unwrap();
10446
10447        assert_eq!(&out1, &out2.slice(1, 2))
10448    }
10449
10450    #[test]
10451    fn test_list_to_string() {
10452        fn assert_cast(array: &ArrayRef, expected: &[&str]) {
10453            assert!(can_cast_types(array.data_type(), &DataType::Utf8));
10454            let out = cast(array, &DataType::Utf8).unwrap();
10455            let out = out
10456                .as_string::<i32>()
10457                .into_iter()
10458                .flatten()
10459                .collect::<Vec<_>>();
10460            assert_eq!(out, expected);
10461
10462            assert!(can_cast_types(array.data_type(), &DataType::LargeUtf8));
10463            let out = cast(array, &DataType::LargeUtf8).unwrap();
10464            let out = out
10465                .as_string::<i64>()
10466                .into_iter()
10467                .flatten()
10468                .collect::<Vec<_>>();
10469            assert_eq!(out, expected);
10470
10471            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
10472            let out = cast(array, &DataType::Utf8View).unwrap();
10473            let out = out
10474                .as_string_view()
10475                .into_iter()
10476                .flatten()
10477                .collect::<Vec<_>>();
10478            assert_eq!(out, expected);
10479        }
10480
10481        let array = Arc::new(ListArray::new(
10482            Field::new_list_field(DataType::Utf8, true).into(),
10483            OffsetBuffer::from_lengths(vec![3, 3, 2]),
10484            Arc::new(StringArray::from(vec![
10485                "a", "b", "c", "d", "e", "f", "g", "h",
10486            ])),
10487            None,
10488        )) as ArrayRef;
10489
10490        assert_cast(&array, &["[a, b, c]", "[d, e, f]", "[g, h]"]);
10491
10492        let array = make_list_array();
10493        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10494
10495        let array = make_large_list_array();
10496        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10497
10498        let array = make_list_view_array();
10499        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10500
10501        let array = make_large_list_view_array();
10502        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10503    }
10504
10505    #[test]
10506    #[cfg_attr(miri, ignore)] // Takes too long
10507    fn test_cast_f64_to_decimal128() {
10508        // to reproduce https://github.com/apache/arrow-rs/issues/2997
10509
10510        let decimal_type = DataType::Decimal128(18, 2);
10511        let array = Float64Array::from(vec![
10512            Some(0.0699999999),
10513            Some(0.0659999999),
10514            Some(0.0650000000),
10515            Some(0.0649999999),
10516        ]);
10517        let array = Arc::new(array) as ArrayRef;
10518        generate_cast_test_case!(
10519            &array,
10520            Decimal128Array,
10521            &decimal_type,
10522            vec![
10523                Some(7_i128), // round up
10524                Some(7_i128), // round up
10525                Some(7_i128), // round up
10526                Some(6_i128), // round down
10527            ]
10528        );
10529
10530        let decimal_type = DataType::Decimal128(18, 3);
10531        let array = Float64Array::from(vec![
10532            Some(0.0699999999),
10533            Some(0.0659999999),
10534            Some(0.0650000000),
10535            Some(0.0649999999),
10536        ]);
10537        let array = Arc::new(array) as ArrayRef;
10538        generate_cast_test_case!(
10539            &array,
10540            Decimal128Array,
10541            &decimal_type,
10542            vec![
10543                Some(70_i128), // round up
10544                Some(66_i128), // round up
10545                Some(65_i128), // round down
10546                Some(65_i128), // round up
10547            ]
10548        );
10549    }
10550
10551    #[test]
10552    fn test_cast_numeric_to_decimal128_overflow() {
10553        let array = Int64Array::from(vec![i64::MAX]);
10554        let array = Arc::new(array) as ArrayRef;
10555        let casted_array = cast_with_options(
10556            &array,
10557            &DataType::Decimal128(38, 30),
10558            &CastOptions {
10559                safe: true,
10560                format_options: FormatOptions::default(),
10561            },
10562        );
10563        assert!(casted_array.is_ok());
10564        assert!(casted_array.unwrap().is_null(0));
10565
10566        let casted_array = cast_with_options(
10567            &array,
10568            &DataType::Decimal128(38, 30),
10569            &CastOptions {
10570                safe: false,
10571                format_options: FormatOptions::default(),
10572            },
10573        );
10574        assert!(casted_array.is_err());
10575    }
10576
10577    #[test]
10578    fn test_cast_numeric_to_decimal256_overflow() {
10579        let array = Int64Array::from(vec![i64::MAX]);
10580        let array = Arc::new(array) as ArrayRef;
10581        let casted_array = cast_with_options(
10582            &array,
10583            &DataType::Decimal256(76, 76),
10584            &CastOptions {
10585                safe: true,
10586                format_options: FormatOptions::default(),
10587            },
10588        );
10589        assert!(casted_array.is_ok());
10590        assert!(casted_array.unwrap().is_null(0));
10591
10592        let casted_array = cast_with_options(
10593            &array,
10594            &DataType::Decimal256(76, 76),
10595            &CastOptions {
10596                safe: false,
10597                format_options: FormatOptions::default(),
10598            },
10599        );
10600        assert!(casted_array.is_err());
10601    }
10602
10603    #[test]
10604    fn test_cast_floating_point_to_decimal128_precision_overflow() {
10605        let array = Float64Array::from(vec![1.1]);
10606        let array = Arc::new(array) as ArrayRef;
10607        let casted_array = cast_with_options(
10608            &array,
10609            &DataType::Decimal128(2, 2),
10610            &CastOptions {
10611                safe: true,
10612                format_options: FormatOptions::default(),
10613            },
10614        );
10615        assert!(casted_array.is_ok());
10616        assert!(casted_array.unwrap().is_null(0));
10617
10618        let casted_array = cast_with_options(
10619            &array,
10620            &DataType::Decimal128(2, 2),
10621            &CastOptions {
10622                safe: false,
10623                format_options: FormatOptions::default(),
10624            },
10625        );
10626        let err = casted_array.unwrap_err().to_string();
10627        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
10628        assert!(
10629            err.contains(expected_error),
10630            "did not find expected error '{expected_error}' in actual error '{err}'"
10631        );
10632    }
10633
10634    #[test]
10635    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
10636    fn test_cast_float16_to_decimal128_precision_overflow() {
10637        let array = Float16Array::from(vec![f16::from_f32(1.1)]);
10638        let array = Arc::new(array) as ArrayRef;
10639        let casted_array = cast_with_options(
10640            &array,
10641            &DataType::Decimal128(2, 2),
10642            &CastOptions {
10643                safe: true,
10644                format_options: FormatOptions::default(),
10645            },
10646        );
10647        assert!(casted_array.is_ok());
10648        assert!(casted_array.unwrap().is_null(0));
10649
10650        let casted_array = cast_with_options(
10651            &array,
10652            &DataType::Decimal128(2, 2),
10653            &CastOptions {
10654                safe: false,
10655                format_options: FormatOptions::default(),
10656            },
10657        );
10658        let err = casted_array.unwrap_err().to_string();
10659        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
10660        assert_eq!(err, expected_error);
10661    }
10662
10663    #[test]
10664    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
10665    fn test_cast_float16_to_decimal256_precision_overflow() {
10666        let array = Float16Array::from(vec![f16::from_f32(1.1)]);
10667        let array = Arc::new(array) as ArrayRef;
10668        let casted_array = cast_with_options(
10669            &array,
10670            &DataType::Decimal256(2, 2),
10671            &CastOptions {
10672                safe: true,
10673                format_options: FormatOptions::default(),
10674            },
10675        );
10676        assert!(casted_array.is_ok());
10677        assert!(casted_array.unwrap().is_null(0));
10678
10679        let casted_array = cast_with_options(
10680            &array,
10681            &DataType::Decimal256(2, 2),
10682            &CastOptions {
10683                safe: false,
10684                format_options: FormatOptions::default(),
10685            },
10686        );
10687        let err = casted_array.unwrap_err().to_string();
10688        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
10689        assert_eq!(err, expected_error);
10690    }
10691
10692    #[test]
10693    #[cfg_attr(miri, ignore)] // Unsupported inline assembly
10694    fn test_cast_float16_to_decimal128_non_finite() {
10695        let array = Float16Array::from(vec![f16::NAN, f16::INFINITY, f16::NEG_INFINITY]);
10696        let array = Arc::new(array) as ArrayRef;
10697        let casted_array = cast_with_options(
10698            &array,
10699            &DataType::Decimal128(38, 2),
10700            &CastOptions {
10701                safe: true,
10702                format_options: FormatOptions::default(),
10703            },
10704        )
10705        .unwrap();
10706
10707        assert!(casted_array.is_null(0));
10708        assert!(casted_array.is_null(1));
10709        assert!(casted_array.is_null(2));
10710
10711        let casted_array = cast_with_options(
10712            &array,
10713            &DataType::Decimal128(38, 2),
10714            &CastOptions {
10715                safe: false,
10716                format_options: FormatOptions::default(),
10717            },
10718        );
10719        let err = casted_array.unwrap_err().to_string();
10720        let expected_error = "Cannot cast to Decimal128(38, 2)";
10721        assert!(
10722            err.contains(expected_error),
10723            "did not find expected error '{expected_error}' in actual error '{err}'"
10724        );
10725    }
10726
10727    #[test]
10728    fn test_cast_floating_point_to_decimal256_precision_overflow() {
10729        let array = Float64Array::from(vec![1.1]);
10730        let array = Arc::new(array) as ArrayRef;
10731        let casted_array = cast_with_options(
10732            &array,
10733            &DataType::Decimal256(2, 2),
10734            &CastOptions {
10735                safe: true,
10736                format_options: FormatOptions::default(),
10737            },
10738        );
10739        assert!(casted_array.is_ok());
10740        assert!(casted_array.unwrap().is_null(0));
10741
10742        let casted_array = cast_with_options(
10743            &array,
10744            &DataType::Decimal256(2, 2),
10745            &CastOptions {
10746                safe: false,
10747                format_options: FormatOptions::default(),
10748            },
10749        );
10750        let err = casted_array.unwrap_err().to_string();
10751        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
10752        assert_eq!(err, expected_error);
10753    }
10754
10755    #[test]
10756    fn test_cast_floating_point_to_decimal128_overflow() {
10757        let array = Float64Array::from(vec![f64::MAX]);
10758        let array = Arc::new(array) as ArrayRef;
10759        let casted_array = cast_with_options(
10760            &array,
10761            &DataType::Decimal128(38, 30),
10762            &CastOptions {
10763                safe: true,
10764                format_options: FormatOptions::default(),
10765            },
10766        );
10767        assert!(casted_array.is_ok());
10768        assert!(casted_array.unwrap().is_null(0));
10769
10770        let casted_array = cast_with_options(
10771            &array,
10772            &DataType::Decimal128(38, 30),
10773            &CastOptions {
10774                safe: false,
10775                format_options: FormatOptions::default(),
10776            },
10777        );
10778        let err = casted_array.unwrap_err().to_string();
10779        let expected_error = "Cast error: Cannot cast to Decimal128(38, 30)";
10780        assert!(
10781            err.contains(expected_error),
10782            "did not find expected error '{expected_error}' in actual error '{err}'"
10783        );
10784    }
10785
10786    #[test]
10787    fn test_cast_floating_point_to_decimal256_overflow() {
10788        let array = Float64Array::from(vec![f64::MAX]);
10789        let array = Arc::new(array) as ArrayRef;
10790        let casted_array = cast_with_options(
10791            &array,
10792            &DataType::Decimal256(76, 50),
10793            &CastOptions {
10794                safe: true,
10795                format_options: FormatOptions::default(),
10796            },
10797        );
10798        assert!(casted_array.is_ok());
10799        assert!(casted_array.unwrap().is_null(0));
10800
10801        let casted_array = cast_with_options(
10802            &array,
10803            &DataType::Decimal256(76, 50),
10804            &CastOptions {
10805                safe: false,
10806                format_options: FormatOptions::default(),
10807            },
10808        );
10809        let err = casted_array.unwrap_err().to_string();
10810        let expected_error = "Cast error: Cannot cast to Decimal256(76, 50)";
10811        assert!(
10812            err.contains(expected_error),
10813            "did not find expected error '{expected_error}' in actual error '{err}'"
10814        );
10815    }
10816    #[test]
10817    fn test_cast_decimal256_to_f64_no_overflow() {
10818        // Test casting i256::MAX: should produce a large finite positive value
10819        let array = vec![Some(i256::MAX)];
10820        let array = create_decimal256_array(array, 76, 2).unwrap();
10821        let array = Arc::new(array) as ArrayRef;
10822
10823        let result = cast(&array, &DataType::Float64).unwrap();
10824        let result = result.as_primitive::<Float64Type>();
10825        assert!(result.value(0).is_finite());
10826        assert!(result.value(0) > 0.0); // Positive result
10827
10828        // Test casting i256::MIN: should produce a large finite negative value
10829        let array = vec![Some(i256::MIN)];
10830        let array = create_decimal256_array(array, 76, 2).unwrap();
10831        let array = Arc::new(array) as ArrayRef;
10832
10833        let result = cast(&array, &DataType::Float64).unwrap();
10834        let result = result.as_primitive::<Float64Type>();
10835        assert!(result.value(0).is_finite());
10836        assert!(result.value(0) < 0.0); // Negative result
10837    }
10838
10839    #[test]
10840    fn test_cast_decimal128_to_decimal128_negative_scale() {
10841        let input_type = DataType::Decimal128(20, 0);
10842        let output_type = DataType::Decimal128(20, -1);
10843        assert!(can_cast_types(&input_type, &output_type));
10844        let array = vec![Some(1123450), Some(2123455), Some(3123456), None];
10845        let input_decimal_array = create_decimal128_array(array, 20, 0).unwrap();
10846        let array = Arc::new(input_decimal_array) as ArrayRef;
10847        generate_cast_test_case!(
10848            &array,
10849            Decimal128Array,
10850            &output_type,
10851            vec![
10852                Some(112345_i128),
10853                Some(212346_i128),
10854                Some(312346_i128),
10855                None
10856            ]
10857        );
10858
10859        let casted_array = cast(&array, &output_type).unwrap();
10860        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10861
10862        assert_eq!("1123450", decimal_arr.value_as_string(0));
10863        assert_eq!("2123460", decimal_arr.value_as_string(1));
10864        assert_eq!("3123460", decimal_arr.value_as_string(2));
10865    }
10866
10867    #[test]
10868    fn decimal128_min_max_to_f64() {
10869        // Ensure Decimal128 i128::MIN/MAX round-trip cast
10870        let min128 = i128::MIN;
10871        let max128 = i128::MAX;
10872        assert_eq!(min128 as f64, min128 as f64);
10873        assert_eq!(max128 as f64, max128 as f64);
10874    }
10875
10876    #[test]
10877    fn test_cast_numeric_to_decimal128_negative() {
10878        let decimal_type = DataType::Decimal128(38, -1);
10879        let array = Arc::new(Int32Array::from(vec![
10880            Some(1123456),
10881            Some(2123456),
10882            Some(3123456),
10883        ])) as ArrayRef;
10884
10885        let casted_array = cast(&array, &decimal_type).unwrap();
10886        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10887
10888        assert_eq!("1123450", decimal_arr.value_as_string(0));
10889        assert_eq!("2123450", decimal_arr.value_as_string(1));
10890        assert_eq!("3123450", decimal_arr.value_as_string(2));
10891
10892        let array = Arc::new(Float32Array::from(vec![
10893            Some(1123.456),
10894            Some(2123.456),
10895            Some(3123.456),
10896        ])) as ArrayRef;
10897
10898        let casted_array = cast(&array, &decimal_type).unwrap();
10899        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10900
10901        assert_eq!("1120", decimal_arr.value_as_string(0));
10902        assert_eq!("2120", decimal_arr.value_as_string(1));
10903        assert_eq!("3120", decimal_arr.value_as_string(2));
10904    }
10905
10906    #[test]
10907    fn test_cast_decimal128_to_decimal128_negative() {
10908        let input_type = DataType::Decimal128(10, -1);
10909        let output_type = DataType::Decimal128(10, -2);
10910        assert!(can_cast_types(&input_type, &output_type));
10911        let array = vec![Some(123)];
10912        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
10913        let array = Arc::new(input_decimal_array) as ArrayRef;
10914        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(12_i128),]);
10915
10916        let casted_array = cast(&array, &output_type).unwrap();
10917        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10918
10919        assert_eq!("1200", decimal_arr.value_as_string(0));
10920
10921        let array = vec![Some(125)];
10922        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
10923        let array = Arc::new(input_decimal_array) as ArrayRef;
10924        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(13_i128),]);
10925
10926        let casted_array = cast(&array, &output_type).unwrap();
10927        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10928
10929        assert_eq!("1300", decimal_arr.value_as_string(0));
10930    }
10931
10932    #[test]
10933    fn test_cast_decimal128_to_decimal256_negative() {
10934        let input_type = DataType::Decimal128(10, 3);
10935        let output_type = DataType::Decimal256(10, 5);
10936        assert!(can_cast_types(&input_type, &output_type));
10937        let array = vec![Some(123456), Some(-123456)];
10938        let input_decimal_array = create_decimal128_array(array, 10, 3).unwrap();
10939        let array = Arc::new(input_decimal_array) as ArrayRef;
10940
10941        let hundred = i256::from_i128(100);
10942        generate_cast_test_case!(
10943            &array,
10944            Decimal256Array,
10945            &output_type,
10946            vec![
10947                Some(i256::from_i128(123456).mul_wrapping(hundred)),
10948                Some(i256::from_i128(-123456).mul_wrapping(hundred))
10949            ]
10950        );
10951    }
10952
10953    #[test]
10954    fn test_parse_string_to_decimal() {
10955        assert_eq!(
10956            Decimal128Type::format_decimal(
10957                parse_string_to_decimal_native::<Decimal128Type>("123.45", 2).unwrap(),
10958                38,
10959                2,
10960            ),
10961            "123.45"
10962        );
10963        assert_eq!(
10964            Decimal128Type::format_decimal(
10965                parse_string_to_decimal_native::<Decimal128Type>("12345", 2).unwrap(),
10966                38,
10967                2,
10968            ),
10969            "12345.00"
10970        );
10971        assert_eq!(
10972            Decimal128Type::format_decimal(
10973                parse_string_to_decimal_native::<Decimal128Type>("0.12345", 2).unwrap(),
10974                38,
10975                2,
10976            ),
10977            "0.12"
10978        );
10979        assert_eq!(
10980            Decimal128Type::format_decimal(
10981                parse_string_to_decimal_native::<Decimal128Type>(".12345", 2).unwrap(),
10982                38,
10983                2,
10984            ),
10985            "0.12"
10986        );
10987        assert_eq!(
10988            Decimal128Type::format_decimal(
10989                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
10990                38,
10991                2,
10992            ),
10993            "0.13"
10994        );
10995        assert_eq!(
10996            Decimal128Type::format_decimal(
10997                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
10998                38,
10999                2,
11000            ),
11001            "0.13"
11002        );
11003
11004        assert_eq!(
11005            Decimal256Type::format_decimal(
11006                parse_string_to_decimal_native::<Decimal256Type>("123.45", 3).unwrap(),
11007                38,
11008                3,
11009            ),
11010            "123.450"
11011        );
11012        assert_eq!(
11013            Decimal256Type::format_decimal(
11014                parse_string_to_decimal_native::<Decimal256Type>("12345", 3).unwrap(),
11015                38,
11016                3,
11017            ),
11018            "12345.000"
11019        );
11020        assert_eq!(
11021            Decimal256Type::format_decimal(
11022                parse_string_to_decimal_native::<Decimal256Type>("0.12345", 3).unwrap(),
11023                38,
11024                3,
11025            ),
11026            "0.123"
11027        );
11028        assert_eq!(
11029            Decimal256Type::format_decimal(
11030                parse_string_to_decimal_native::<Decimal256Type>(".12345", 3).unwrap(),
11031                38,
11032                3,
11033            ),
11034            "0.123"
11035        );
11036        assert_eq!(
11037            Decimal256Type::format_decimal(
11038                parse_string_to_decimal_native::<Decimal256Type>(".1265", 3).unwrap(),
11039                38,
11040                3,
11041            ),
11042            "0.127"
11043        );
11044    }
11045
11046    fn test_cast_string_to_decimal(array: ArrayRef) {
11047        // Decimal128
11048        let output_type = DataType::Decimal128(38, 2);
11049        assert!(can_cast_types(array.data_type(), &output_type));
11050
11051        let casted_array = cast(&array, &output_type).unwrap();
11052        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11053
11054        assert_eq!("123.45", decimal_arr.value_as_string(0));
11055        assert_eq!("1.23", decimal_arr.value_as_string(1));
11056        assert_eq!("0.12", decimal_arr.value_as_string(2));
11057        assert_eq!("0.13", decimal_arr.value_as_string(3));
11058        assert_eq!("1.26", decimal_arr.value_as_string(4));
11059        assert_eq!("12345.00", decimal_arr.value_as_string(5));
11060        assert_eq!("12345.00", decimal_arr.value_as_string(6));
11061        assert_eq!("0.12", decimal_arr.value_as_string(7));
11062        assert_eq!("12.23", decimal_arr.value_as_string(8));
11063        assert!(decimal_arr.is_null(9));
11064        assert!(decimal_arr.is_null(10));
11065        assert!(decimal_arr.is_null(11));
11066        assert!(decimal_arr.is_null(12));
11067        assert_eq!("-1.23", decimal_arr.value_as_string(13));
11068        assert_eq!("-1.24", decimal_arr.value_as_string(14));
11069        assert_eq!("0.00", decimal_arr.value_as_string(15));
11070        assert_eq!("-123.00", decimal_arr.value_as_string(16));
11071        assert_eq!("-123.23", decimal_arr.value_as_string(17));
11072        assert_eq!("-0.12", decimal_arr.value_as_string(18));
11073        assert_eq!("1.23", decimal_arr.value_as_string(19));
11074        assert_eq!("1.24", decimal_arr.value_as_string(20));
11075        assert_eq!("0.00", decimal_arr.value_as_string(21));
11076        assert_eq!("123.00", decimal_arr.value_as_string(22));
11077        assert_eq!("123.23", decimal_arr.value_as_string(23));
11078        assert_eq!("0.12", decimal_arr.value_as_string(24));
11079        assert!(decimal_arr.is_null(25));
11080        assert!(decimal_arr.is_null(26));
11081        assert!(decimal_arr.is_null(27));
11082        assert_eq!("0.00", decimal_arr.value_as_string(28));
11083        assert_eq!("0.00", decimal_arr.value_as_string(29));
11084        assert_eq!("12345.00", decimal_arr.value_as_string(30));
11085        assert_eq!(decimal_arr.len(), 31);
11086
11087        // Decimal256
11088        let output_type = DataType::Decimal256(76, 3);
11089        assert!(can_cast_types(array.data_type(), &output_type));
11090
11091        let casted_array = cast(&array, &output_type).unwrap();
11092        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
11093
11094        assert_eq!("123.450", decimal_arr.value_as_string(0));
11095        assert_eq!("1.235", decimal_arr.value_as_string(1));
11096        assert_eq!("0.123", decimal_arr.value_as_string(2));
11097        assert_eq!("0.127", decimal_arr.value_as_string(3));
11098        assert_eq!("1.263", decimal_arr.value_as_string(4));
11099        assert_eq!("12345.000", decimal_arr.value_as_string(5));
11100        assert_eq!("12345.000", decimal_arr.value_as_string(6));
11101        assert_eq!("0.123", decimal_arr.value_as_string(7));
11102        assert_eq!("12.234", decimal_arr.value_as_string(8));
11103        assert!(decimal_arr.is_null(9));
11104        assert!(decimal_arr.is_null(10));
11105        assert!(decimal_arr.is_null(11));
11106        assert!(decimal_arr.is_null(12));
11107        assert_eq!("-1.235", decimal_arr.value_as_string(13));
11108        assert_eq!("-1.236", decimal_arr.value_as_string(14));
11109        assert_eq!("0.000", decimal_arr.value_as_string(15));
11110        assert_eq!("-123.000", decimal_arr.value_as_string(16));
11111        assert_eq!("-123.234", decimal_arr.value_as_string(17));
11112        assert_eq!("-0.123", decimal_arr.value_as_string(18));
11113        assert_eq!("1.235", decimal_arr.value_as_string(19));
11114        assert_eq!("1.236", decimal_arr.value_as_string(20));
11115        assert_eq!("0.000", decimal_arr.value_as_string(21));
11116        assert_eq!("123.000", decimal_arr.value_as_string(22));
11117        assert_eq!("123.234", decimal_arr.value_as_string(23));
11118        assert_eq!("0.123", decimal_arr.value_as_string(24));
11119        assert!(decimal_arr.is_null(25));
11120        assert!(decimal_arr.is_null(26));
11121        assert!(decimal_arr.is_null(27));
11122        assert_eq!("0.000", decimal_arr.value_as_string(28));
11123        assert_eq!("0.000", decimal_arr.value_as_string(29));
11124        assert_eq!("12345.000", decimal_arr.value_as_string(30));
11125        assert_eq!(decimal_arr.len(), 31);
11126    }
11127
11128    #[test]
11129    fn test_cast_utf8_to_decimal() {
11130        let str_array = StringArray::from(vec![
11131            Some("123.45"),
11132            Some("1.2345"),
11133            Some("0.12345"),
11134            Some("0.1267"),
11135            Some("1.263"),
11136            Some("12345.0"),
11137            Some("12345"),
11138            Some("000.123"),
11139            Some("12.234000"),
11140            None,
11141            Some(""),
11142            Some(" "),
11143            None,
11144            Some("-1.23499999"),
11145            Some("-1.23599999"),
11146            Some("-0.00001"),
11147            Some("-123"),
11148            Some("-123.234000"),
11149            Some("-000.123"),
11150            Some("+1.23499999"),
11151            Some("+1.23599999"),
11152            Some("+0.00001"),
11153            Some("+123"),
11154            Some("+123.234000"),
11155            Some("+000.123"),
11156            Some("1.-23499999"),
11157            Some("-1.-23499999"),
11158            Some("--1.23499999"),
11159            Some("0"),
11160            Some("000.000"),
11161            Some("0000000000000000012345.000"),
11162        ]);
11163        let array = Arc::new(str_array) as ArrayRef;
11164
11165        test_cast_string_to_decimal(array);
11166
11167        let test_cases = [
11168            (None, None),
11169            (Some(""), None),
11170            (Some("   "), None),
11171            (Some("0"), Some("0")),
11172            (Some("000.000"), Some("0")),
11173            (Some("12345"), Some("12345")),
11174            (Some("000000000000000000000000000012345"), Some("12345")),
11175            (Some("-123"), Some("-123")),
11176            (Some("+123"), Some("123")),
11177        ];
11178        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
11179        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
11180
11181        let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
11182        test_cast_string_to_decimal_scale_zero(array, &expected);
11183    }
11184
11185    #[test]
11186    fn test_cast_large_utf8_to_decimal() {
11187        let str_array = LargeStringArray::from(vec![
11188            Some("123.45"),
11189            Some("1.2345"),
11190            Some("0.12345"),
11191            Some("0.1267"),
11192            Some("1.263"),
11193            Some("12345.0"),
11194            Some("12345"),
11195            Some("000.123"),
11196            Some("12.234000"),
11197            None,
11198            Some(""),
11199            Some(" "),
11200            None,
11201            Some("-1.23499999"),
11202            Some("-1.23599999"),
11203            Some("-0.00001"),
11204            Some("-123"),
11205            Some("-123.234000"),
11206            Some("-000.123"),
11207            Some("+1.23499999"),
11208            Some("+1.23599999"),
11209            Some("+0.00001"),
11210            Some("+123"),
11211            Some("+123.234000"),
11212            Some("+000.123"),
11213            Some("1.-23499999"),
11214            Some("-1.-23499999"),
11215            Some("--1.23499999"),
11216            Some("0"),
11217            Some("000.000"),
11218            Some("0000000000000000012345.000"),
11219        ]);
11220        let array = Arc::new(str_array) as ArrayRef;
11221
11222        test_cast_string_to_decimal(array);
11223
11224        let test_cases = [
11225            (None, None),
11226            (Some(""), None),
11227            (Some("   "), None),
11228            (Some("0"), Some("0")),
11229            (Some("000.000"), Some("0")),
11230            (Some("12345"), Some("12345")),
11231            (Some("000000000000000000000000000012345"), Some("12345")),
11232            (Some("-123"), Some("-123")),
11233            (Some("+123"), Some("123")),
11234        ];
11235        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
11236        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
11237
11238        let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
11239        test_cast_string_to_decimal_scale_zero(array, &expected);
11240    }
11241
11242    fn test_cast_string_to_decimal_scale_zero(
11243        array: ArrayRef,
11244        expected_as_string: &[Option<&str>],
11245    ) {
11246        // Decimal128
11247        let output_type = DataType::Decimal128(38, 0);
11248        assert!(can_cast_types(array.data_type(), &output_type));
11249        let casted_array = cast(&array, &output_type).unwrap();
11250        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11251        assert_decimal_array_contents(decimal_arr, expected_as_string);
11252
11253        // Decimal256
11254        let output_type = DataType::Decimal256(76, 0);
11255        assert!(can_cast_types(array.data_type(), &output_type));
11256        let casted_array = cast(&array, &output_type).unwrap();
11257        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
11258        assert_decimal_array_contents(decimal_arr, expected_as_string);
11259    }
11260
11261    fn assert_decimal_array_contents<T>(
11262        array: &PrimitiveArray<T>,
11263        expected_as_string: &[Option<&str>],
11264    ) where
11265        T: DecimalType + ArrowPrimitiveType,
11266    {
11267        assert_eq!(array.len(), expected_as_string.len());
11268        for (i, expected) in expected_as_string.iter().enumerate() {
11269            let actual = if array.is_null(i) {
11270                None
11271            } else {
11272                Some(array.value_as_string(i))
11273            };
11274            let actual = actual.as_ref().map(|s| s.as_ref());
11275            assert_eq!(*expected, actual, "Expected at position {i}");
11276        }
11277    }
11278
11279    #[test]
11280    fn test_cast_invalid_utf8_to_decimal() {
11281        let str_array = StringArray::from(vec!["4.4.5", ". 0.123"]);
11282        let array = Arc::new(str_array) as ArrayRef;
11283
11284        // Safe cast
11285        let output_type = DataType::Decimal128(38, 2);
11286        let casted_array = cast(&array, &output_type).unwrap();
11287        assert!(casted_array.is_null(0));
11288        assert!(casted_array.is_null(1));
11289
11290        let output_type = DataType::Decimal256(76, 2);
11291        let casted_array = cast(&array, &output_type).unwrap();
11292        assert!(casted_array.is_null(0));
11293        assert!(casted_array.is_null(1));
11294
11295        // Non-safe cast
11296        let output_type = DataType::Decimal128(38, 2);
11297        let str_array = StringArray::from(vec!["4.4.5"]);
11298        let array = Arc::new(str_array) as ArrayRef;
11299        let option = CastOptions {
11300            safe: false,
11301            format_options: FormatOptions::default(),
11302        };
11303        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
11304        assert!(
11305            casted_err
11306                .to_string()
11307                .contains("Cannot cast string '4.4.5' to value of Decimal128(38, 10) type")
11308        );
11309
11310        let str_array = StringArray::from(vec![". 0.123"]);
11311        let array = Arc::new(str_array) as ArrayRef;
11312        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
11313        assert!(
11314            casted_err
11315                .to_string()
11316                .contains("Cannot cast string '. 0.123' to value of Decimal128(38, 10) type")
11317        );
11318
11319        let str_array = StringArray::from(vec![""]);
11320        let array = Arc::new(str_array) as ArrayRef;
11321        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
11322        assert!(
11323            casted_err
11324                .to_string()
11325                .contains("Cannot cast string '' to value of Decimal128(38, 10) type")
11326        );
11327    }
11328
11329    fn test_cast_string_to_decimal128_overflow(overflow_array: ArrayRef) {
11330        let output_type = DataType::Decimal128(38, 2);
11331        let casted_array = cast(&overflow_array, &output_type).unwrap();
11332        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11333
11334        assert!(decimal_arr.is_null(0));
11335        assert!(decimal_arr.is_null(1));
11336        assert!(decimal_arr.is_null(2));
11337        assert_eq!(
11338            "999999999999999999999999999999999999.99",
11339            decimal_arr.value_as_string(3)
11340        );
11341        assert_eq!(
11342            "100000000000000000000000000000000000.00",
11343            decimal_arr.value_as_string(4)
11344        );
11345    }
11346
11347    #[test]
11348    fn test_cast_string_to_decimal128_precision_overflow() {
11349        let array = StringArray::from(vec!["1000".to_string()]);
11350        let array = Arc::new(array) as ArrayRef;
11351        let casted_array = cast_with_options(
11352            &array,
11353            &DataType::Decimal128(10, 8),
11354            &CastOptions {
11355                safe: true,
11356                format_options: FormatOptions::default(),
11357            },
11358        );
11359        assert!(casted_array.is_ok());
11360        assert!(casted_array.unwrap().is_null(0));
11361
11362        let err = cast_with_options(
11363            &array,
11364            &DataType::Decimal128(10, 8),
11365            &CastOptions {
11366                safe: false,
11367                format_options: FormatOptions::default(),
11368            },
11369        );
11370        assert_eq!(
11371            "Invalid argument error: 1000.00000000 is too large to store in a Decimal128 of precision 10. Max is 99.99999999",
11372            err.unwrap_err().to_string()
11373        );
11374    }
11375
11376    #[test]
11377    fn test_cast_utf8_to_decimal128_overflow() {
11378        let overflow_str_array = StringArray::from(vec![
11379            i128::MAX.to_string(),
11380            i128::MIN.to_string(),
11381            "99999999999999999999999999999999999999".to_string(),
11382            "999999999999999999999999999999999999.99".to_string(),
11383            "99999999999999999999999999999999999.999".to_string(),
11384        ]);
11385        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11386
11387        test_cast_string_to_decimal128_overflow(overflow_array);
11388    }
11389
11390    #[test]
11391    fn test_cast_large_utf8_to_decimal128_overflow() {
11392        let overflow_str_array = LargeStringArray::from(vec![
11393            i128::MAX.to_string(),
11394            i128::MIN.to_string(),
11395            "99999999999999999999999999999999999999".to_string(),
11396            "999999999999999999999999999999999999.99".to_string(),
11397            "99999999999999999999999999999999999.999".to_string(),
11398        ]);
11399        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11400
11401        test_cast_string_to_decimal128_overflow(overflow_array);
11402    }
11403
11404    fn test_cast_string_to_decimal256_overflow(overflow_array: ArrayRef) {
11405        let output_type = DataType::Decimal256(76, 2);
11406        let casted_array = cast(&overflow_array, &output_type).unwrap();
11407        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
11408
11409        assert_eq!(
11410            "170141183460469231731687303715884105727.00",
11411            decimal_arr.value_as_string(0)
11412        );
11413        assert_eq!(
11414            "-170141183460469231731687303715884105728.00",
11415            decimal_arr.value_as_string(1)
11416        );
11417        assert_eq!(
11418            "99999999999999999999999999999999999999.00",
11419            decimal_arr.value_as_string(2)
11420        );
11421        assert_eq!(
11422            "999999999999999999999999999999999999.99",
11423            decimal_arr.value_as_string(3)
11424        );
11425        assert_eq!(
11426            "100000000000000000000000000000000000.00",
11427            decimal_arr.value_as_string(4)
11428        );
11429        assert!(decimal_arr.is_null(5));
11430        assert!(decimal_arr.is_null(6));
11431    }
11432
11433    #[test]
11434    fn test_cast_string_to_decimal256_precision_overflow() {
11435        let array = StringArray::from(vec!["1000".to_string()]);
11436        let array = Arc::new(array) as ArrayRef;
11437        let casted_array = cast_with_options(
11438            &array,
11439            &DataType::Decimal256(10, 8),
11440            &CastOptions {
11441                safe: true,
11442                format_options: FormatOptions::default(),
11443            },
11444        );
11445        assert!(casted_array.is_ok());
11446        assert!(casted_array.unwrap().is_null(0));
11447
11448        let err = cast_with_options(
11449            &array,
11450            &DataType::Decimal256(10, 8),
11451            &CastOptions {
11452                safe: false,
11453                format_options: FormatOptions::default(),
11454            },
11455        );
11456        assert_eq!(
11457            "Invalid argument error: 1000.00000000 is too large to store in a Decimal256 of precision 10. Max is 99.99999999",
11458            err.unwrap_err().to_string()
11459        );
11460    }
11461
11462    #[test]
11463    fn test_cast_utf8_to_decimal256_overflow() {
11464        let overflow_str_array = StringArray::from(vec![
11465            i128::MAX.to_string(),
11466            i128::MIN.to_string(),
11467            "99999999999999999999999999999999999999".to_string(),
11468            "999999999999999999999999999999999999.99".to_string(),
11469            "99999999999999999999999999999999999.999".to_string(),
11470            i256::MAX.to_string(),
11471            i256::MIN.to_string(),
11472        ]);
11473        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11474
11475        test_cast_string_to_decimal256_overflow(overflow_array);
11476    }
11477
11478    #[test]
11479    fn test_cast_large_utf8_to_decimal256_overflow() {
11480        let overflow_str_array = LargeStringArray::from(vec![
11481            i128::MAX.to_string(),
11482            i128::MIN.to_string(),
11483            "99999999999999999999999999999999999999".to_string(),
11484            "999999999999999999999999999999999999.99".to_string(),
11485            "99999999999999999999999999999999999.999".to_string(),
11486            i256::MAX.to_string(),
11487            i256::MIN.to_string(),
11488        ]);
11489        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11490
11491        test_cast_string_to_decimal256_overflow(overflow_array);
11492    }
11493
11494    #[test]
11495    fn test_cast_outside_supported_range_for_nanoseconds() {
11496        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";
11497
11498        let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
11499
11500        let cast_options = CastOptions {
11501            safe: false,
11502            format_options: FormatOptions::default(),
11503        };
11504
11505        let result = cast_string_to_timestamp::<i32, TimestampNanosecondType>(
11506            &array,
11507            &None::<Arc<str>>,
11508            &cast_options,
11509        );
11510
11511        let err = result.unwrap_err();
11512        assert_eq!(
11513            err.to_string(),
11514            format!(
11515                "Cast error: Overflow converting {} to Nanosecond. {}",
11516                array.value(0),
11517                EXPECTED_ERROR_MESSAGE
11518            )
11519        );
11520    }
11521
11522    #[test]
11523    fn test_cast_date32_to_timestamp() {
11524        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
11525        let array = Arc::new(a) as ArrayRef;
11526        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
11527        let c = b.as_primitive::<TimestampSecondType>();
11528        assert_eq!(1609459200, c.value(0));
11529        assert_eq!(1640995200, c.value(1));
11530        assert!(c.is_null(2));
11531    }
11532
11533    #[test]
11534    fn test_cast_date32_to_timestamp_ms() {
11535        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
11536        let array = Arc::new(a) as ArrayRef;
11537        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
11538        let c = b
11539            .as_any()
11540            .downcast_ref::<TimestampMillisecondArray>()
11541            .unwrap();
11542        assert_eq!(1609459200000, c.value(0));
11543        assert_eq!(1640995200000, c.value(1));
11544        assert!(c.is_null(2));
11545    }
11546
11547    #[test]
11548    fn test_cast_date32_to_timestamp_us() {
11549        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
11550        let array = Arc::new(a) as ArrayRef;
11551        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
11552        let c = b
11553            .as_any()
11554            .downcast_ref::<TimestampMicrosecondArray>()
11555            .unwrap();
11556        assert_eq!(1609459200000000, c.value(0));
11557        assert_eq!(1640995200000000, c.value(1));
11558        assert!(c.is_null(2));
11559    }
11560
11561    #[test]
11562    fn test_cast_date32_to_timestamp_ns() {
11563        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
11564        let array = Arc::new(a) as ArrayRef;
11565        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
11566        let c = b
11567            .as_any()
11568            .downcast_ref::<TimestampNanosecondArray>()
11569            .unwrap();
11570        assert_eq!(1609459200000000000, c.value(0));
11571        assert_eq!(1640995200000000000, c.value(1));
11572        assert!(c.is_null(2));
11573    }
11574
11575    #[test]
11576    fn test_cast_date32_to_timestamp_us_overflow() {
11577        const MAX_DAYS_MICROS: i32 = (i64::MAX / MICROSECONDS_IN_DAY) as i32;
11578        let a = Date32Array::from(vec![Some(MAX_DAYS_MICROS), Some(MAX_DAYS_MICROS + 1), None]);
11579        let array = Arc::new(a) as ArrayRef;
11580        let err = cast_with_options(
11581            &array,
11582            &DataType::Timestamp(TimeUnit::Microsecond, None),
11583            &CastOptions {
11584                safe: false,
11585                format_options: FormatOptions::default(),
11586            },
11587        );
11588        assert!(err.is_err());
11589
11590        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
11591        let c = b.as_primitive::<TimestampMicrosecondType>();
11592        assert_eq!(MAX_DAYS_MICROS as i64 * MICROSECONDS_IN_DAY, c.value(0));
11593        assert!(c.is_null(1));
11594        assert!(c.is_null(2));
11595    }
11596
11597    #[test]
11598    fn test_cast_date32_to_timestamp_ns_overflow() {
11599        // 2262-04-11, 2062-04-12
11600        let upper_limit = 106_751;
11601        let a = Date32Array::from(vec![Some(upper_limit), Some(upper_limit + 1), None]);
11602        let array = Arc::new(a) as ArrayRef;
11603        let err = cast_with_options(
11604            &array,
11605            &DataType::Timestamp(TimeUnit::Nanosecond, None),
11606            &CastOptions {
11607                safe: false,
11608                format_options: FormatOptions::default(),
11609            },
11610        );
11611        assert!(err.is_err());
11612
11613        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
11614        let c = b.as_primitive::<TimestampNanosecondType>();
11615        assert_eq!(upper_limit as i64 * NANOSECONDS_IN_DAY, c.value(0));
11616        assert!(c.is_null(1));
11617        assert!(c.is_null(2));
11618    }
11619
11620    #[test]
11621    fn test_timezone_cast() {
11622        let a = StringArray::from(vec![
11623            "2000-01-01T12:00:00", // date + time valid
11624            "2020-12-15T12:34:56", // date + time valid
11625        ]);
11626        let array = Arc::new(a) as ArrayRef;
11627        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
11628        let v = b.as_primitive::<TimestampNanosecondType>();
11629
11630        assert_eq!(v.value(0), 946728000000000000);
11631        assert_eq!(v.value(1), 1608035696000000000);
11632
11633        let b = cast(
11634            &b,
11635            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11636        )
11637        .unwrap();
11638        let v = b.as_primitive::<TimestampNanosecondType>();
11639
11640        assert_eq!(v.value(0), 946728000000000000);
11641        assert_eq!(v.value(1), 1608035696000000000);
11642
11643        let b = cast(
11644            &b,
11645            &DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
11646        )
11647        .unwrap();
11648        let v = b.as_primitive::<TimestampMillisecondType>();
11649
11650        assert_eq!(v.value(0), 946728000000);
11651        assert_eq!(v.value(1), 1608035696000);
11652    }
11653
11654    #[test]
11655    fn test_cast_utf8_to_timestamp() {
11656        fn test_tz(tz: Arc<str>) {
11657            let valid = StringArray::from(vec![
11658                "2023-01-01 04:05:06.789000-08:00",
11659                "2023-01-01 04:05:06.789000-07:00",
11660                "2023-01-01 04:05:06.789 -0800",
11661                "2023-01-01 04:05:06.789 -08:00",
11662                "2023-01-01 040506 +0730",
11663                "2023-01-01 040506 +07:30",
11664                "2023-01-01 04:05:06.789",
11665                "2023-01-01 04:05:06",
11666                "2023-01-01",
11667            ]);
11668
11669            let array = Arc::new(valid) as ArrayRef;
11670            let b = cast_with_options(
11671                &array,
11672                &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
11673                &CastOptions {
11674                    safe: false,
11675                    format_options: FormatOptions::default(),
11676                },
11677            )
11678            .unwrap();
11679
11680            let tz = tz.as_ref().parse().unwrap();
11681
11682            let as_tz =
11683                |v: i64| as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap();
11684
11685            let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
11686            let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
11687
11688            let values = b.as_primitive::<TimestampNanosecondType>().values();
11689            let utc_results: Vec<_> = values.iter().map(as_utc).collect();
11690            let local_results: Vec<_> = values.iter().map(as_local).collect();
11691
11692            // Absolute timestamps should be parsed preserving the same UTC instant
11693            assert_eq!(
11694                &utc_results[..6],
11695                &[
11696                    "2023-01-01 12:05:06.789".to_string(),
11697                    "2023-01-01 11:05:06.789".to_string(),
11698                    "2023-01-01 12:05:06.789".to_string(),
11699                    "2023-01-01 12:05:06.789".to_string(),
11700                    "2022-12-31 20:35:06".to_string(),
11701                    "2022-12-31 20:35:06".to_string(),
11702                ]
11703            );
11704            // Non-absolute timestamps should be parsed preserving the same local instant
11705            assert_eq!(
11706                &local_results[6..],
11707                &[
11708                    "2023-01-01 04:05:06.789".to_string(),
11709                    "2023-01-01 04:05:06".to_string(),
11710                    "2023-01-01 00:00:00".to_string()
11711                ]
11712            )
11713        }
11714
11715        test_tz("+00:00".into());
11716        test_tz("+02:00".into());
11717    }
11718
11719    #[test]
11720    fn test_cast_invalid_utf8() {
11721        let v1: &[u8] = b"\xFF invalid";
11722        let v2: &[u8] = b"\x00 Foo";
11723        let s = BinaryArray::from(vec![v1, v2]);
11724        let options = CastOptions {
11725            safe: true,
11726            format_options: FormatOptions::default(),
11727        };
11728        let array = cast_with_options(&s, &DataType::Utf8, &options).unwrap();
11729        let a = array.as_string::<i32>();
11730        a.to_data().validate_full().unwrap();
11731
11732        assert_eq!(a.null_count(), 1);
11733        assert_eq!(a.len(), 2);
11734        assert!(a.is_null(0));
11735        assert_eq!(a.value(0), "");
11736        assert_eq!(a.value(1), "\x00 Foo");
11737    }
11738
11739    #[test]
11740    fn test_cast_utf8_to_timestamptz() {
11741        let valid = StringArray::from(vec!["2023-01-01"]);
11742
11743        let array = Arc::new(valid) as ArrayRef;
11744        let b = cast(
11745            &array,
11746            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11747        )
11748        .unwrap();
11749
11750        let expect = DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into()));
11751
11752        assert_eq!(b.data_type(), &expect);
11753        let c = b
11754            .as_any()
11755            .downcast_ref::<TimestampNanosecondArray>()
11756            .unwrap();
11757        assert_eq!(1672531200000000000, c.value(0));
11758    }
11759
11760    #[test]
11761    fn test_cast_decimal_to_string() {
11762        assert!(can_cast_types(
11763            &DataType::Decimal32(9, 4),
11764            &DataType::Utf8View
11765        ));
11766        assert!(can_cast_types(
11767            &DataType::Decimal64(16, 4),
11768            &DataType::Utf8View
11769        ));
11770        assert!(can_cast_types(
11771            &DataType::Decimal128(10, 4),
11772            &DataType::Utf8View
11773        ));
11774        assert!(can_cast_types(
11775            &DataType::Decimal256(38, 10),
11776            &DataType::Utf8View
11777        ));
11778
11779        macro_rules! assert_decimal_values {
11780            ($array:expr) => {
11781                let c = $array;
11782                assert_eq!("1123.454", c.value(0));
11783                assert_eq!("2123.456", c.value(1));
11784                assert_eq!("-3123.453", c.value(2));
11785                assert_eq!("-3123.456", c.value(3));
11786                assert_eq!("0.000", c.value(4));
11787                assert_eq!("0.123", c.value(5));
11788                assert_eq!("1234.567", c.value(6));
11789                assert_eq!("-1234.567", c.value(7));
11790                assert!(c.is_null(8));
11791            };
11792        }
11793
11794        fn test_decimal_to_string<IN: ArrowPrimitiveType, OffsetSize: OffsetSizeTrait>(
11795            output_type: DataType,
11796            array: PrimitiveArray<IN>,
11797        ) {
11798            let b = cast(&array, &output_type).unwrap();
11799
11800            assert_eq!(b.data_type(), &output_type);
11801            match b.data_type() {
11802                DataType::Utf8View => {
11803                    let c = b.as_string_view();
11804                    assert_decimal_values!(c);
11805                }
11806                DataType::Utf8 | DataType::LargeUtf8 => {
11807                    let c = b.as_string::<OffsetSize>();
11808                    assert_decimal_values!(c);
11809                }
11810                _ => (),
11811            }
11812        }
11813
11814        let array32: Vec<Option<i32>> = vec![
11815            Some(1123454),
11816            Some(2123456),
11817            Some(-3123453),
11818            Some(-3123456),
11819            Some(0),
11820            Some(123),
11821            Some(123456789),
11822            Some(-123456789),
11823            None,
11824        ];
11825        let array64: Vec<Option<i64>> = array32.iter().map(|num| num.map(|x| x as i64)).collect();
11826        let array128: Vec<Option<i128>> =
11827            array64.iter().map(|num| num.map(|x| x as i128)).collect();
11828        let array256: Vec<Option<i256>> = array128
11829            .iter()
11830            .map(|num| num.map(i256::from_i128))
11831            .collect();
11832
11833        test_decimal_to_string::<Decimal32Type, i32>(
11834            DataType::Utf8View,
11835            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
11836        );
11837        test_decimal_to_string::<Decimal32Type, i32>(
11838            DataType::Utf8,
11839            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
11840        );
11841        test_decimal_to_string::<Decimal32Type, i64>(
11842            DataType::LargeUtf8,
11843            create_decimal32_array(array32, 7, 3).unwrap(),
11844        );
11845
11846        test_decimal_to_string::<Decimal64Type, i32>(
11847            DataType::Utf8View,
11848            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
11849        );
11850        test_decimal_to_string::<Decimal64Type, i32>(
11851            DataType::Utf8,
11852            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
11853        );
11854        test_decimal_to_string::<Decimal64Type, i64>(
11855            DataType::LargeUtf8,
11856            create_decimal64_array(array64, 7, 3).unwrap(),
11857        );
11858
11859        test_decimal_to_string::<Decimal128Type, i32>(
11860            DataType::Utf8View,
11861            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
11862        );
11863        test_decimal_to_string::<Decimal128Type, i32>(
11864            DataType::Utf8,
11865            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
11866        );
11867        test_decimal_to_string::<Decimal128Type, i64>(
11868            DataType::LargeUtf8,
11869            create_decimal128_array(array128, 7, 3).unwrap(),
11870        );
11871
11872        test_decimal_to_string::<Decimal256Type, i32>(
11873            DataType::Utf8View,
11874            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
11875        );
11876        test_decimal_to_string::<Decimal256Type, i32>(
11877            DataType::Utf8,
11878            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
11879        );
11880        test_decimal_to_string::<Decimal256Type, i64>(
11881            DataType::LargeUtf8,
11882            create_decimal256_array(array256, 7, 3).unwrap(),
11883        );
11884    }
11885
11886    #[test]
11887    fn test_cast_numeric_to_decimal128_precision_overflow() {
11888        let array = Int64Array::from(vec![1234567]);
11889        let array = Arc::new(array) as ArrayRef;
11890        let casted_array = cast_with_options(
11891            &array,
11892            &DataType::Decimal128(7, 3),
11893            &CastOptions {
11894                safe: true,
11895                format_options: FormatOptions::default(),
11896            },
11897        );
11898        assert!(casted_array.is_ok());
11899        assert!(casted_array.unwrap().is_null(0));
11900
11901        let err = cast_with_options(
11902            &array,
11903            &DataType::Decimal128(7, 3),
11904            &CastOptions {
11905                safe: false,
11906                format_options: FormatOptions::default(),
11907            },
11908        );
11909        assert_eq!(
11910            "Invalid argument error: 1234567.000 is too large to store in a Decimal128 of precision 7. Max is 9999.999",
11911            err.unwrap_err().to_string()
11912        );
11913    }
11914
11915    #[test]
11916    fn test_cast_numeric_to_decimal256_precision_overflow() {
11917        let array = Int64Array::from(vec![1234567]);
11918        let array = Arc::new(array) as ArrayRef;
11919        let casted_array = cast_with_options(
11920            &array,
11921            &DataType::Decimal256(7, 3),
11922            &CastOptions {
11923                safe: true,
11924                format_options: FormatOptions::default(),
11925            },
11926        );
11927        assert!(casted_array.is_ok());
11928        assert!(casted_array.unwrap().is_null(0));
11929
11930        let err = cast_with_options(
11931            &array,
11932            &DataType::Decimal256(7, 3),
11933            &CastOptions {
11934                safe: false,
11935                format_options: FormatOptions::default(),
11936            },
11937        );
11938        assert_eq!(
11939            "Invalid argument error: 1234567.000 is too large to store in a Decimal256 of precision 7. Max is 9999.999",
11940            err.unwrap_err().to_string()
11941        );
11942    }
11943
11944    /// helper function to test casting from duration to interval
11945    fn cast_from_duration_to_interval<T: ArrowTemporalType<Native = i64>>(
11946        array: Vec<i64>,
11947        cast_options: &CastOptions,
11948    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
11949        let array = PrimitiveArray::<T>::new(array.into(), None);
11950        let array = Arc::new(array) as ArrayRef;
11951        let interval = DataType::Interval(IntervalUnit::MonthDayNano);
11952        let out = cast_with_options(&array, &interval, cast_options)?;
11953        let out = out.as_primitive::<IntervalMonthDayNanoType>().clone();
11954        Ok(out)
11955    }
11956
11957    #[test]
11958    fn test_cast_from_duration_to_interval() {
11959        // from duration second to interval month day nano
11960        let array = vec![1234567];
11961        let casted_array =
11962            cast_from_duration_to_interval::<DurationSecondType>(array, &CastOptions::default())
11963                .unwrap();
11964        assert_eq!(
11965            casted_array.data_type(),
11966            &DataType::Interval(IntervalUnit::MonthDayNano)
11967        );
11968        assert_eq!(
11969            casted_array.value(0),
11970            IntervalMonthDayNano::new(0, 0, 1234567000000000)
11971        );
11972
11973        let array = vec![i64::MAX];
11974        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
11975            array.clone(),
11976            &CastOptions::default(),
11977        )
11978        .unwrap();
11979        assert!(!casted_array.is_valid(0));
11980
11981        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
11982            array,
11983            &CastOptions {
11984                safe: false,
11985                format_options: FormatOptions::default(),
11986            },
11987        );
11988        assert!(casted_array.is_err());
11989
11990        // from duration millisecond to interval month day nano
11991        let array = vec![1234567];
11992        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11993            array,
11994            &CastOptions::default(),
11995        )
11996        .unwrap();
11997        assert_eq!(
11998            casted_array.data_type(),
11999            &DataType::Interval(IntervalUnit::MonthDayNano)
12000        );
12001        assert_eq!(
12002            casted_array.value(0),
12003            IntervalMonthDayNano::new(0, 0, 1234567000000)
12004        );
12005
12006        let array = vec![i64::MAX];
12007        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
12008            array.clone(),
12009            &CastOptions::default(),
12010        )
12011        .unwrap();
12012        assert!(!casted_array.is_valid(0));
12013
12014        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
12015            array,
12016            &CastOptions {
12017                safe: false,
12018                format_options: FormatOptions::default(),
12019            },
12020        );
12021        assert!(casted_array.is_err());
12022
12023        // from duration microsecond to interval month day nano
12024        let array = vec![1234567];
12025        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
12026            array,
12027            &CastOptions::default(),
12028        )
12029        .unwrap();
12030        assert_eq!(
12031            casted_array.data_type(),
12032            &DataType::Interval(IntervalUnit::MonthDayNano)
12033        );
12034        assert_eq!(
12035            casted_array.value(0),
12036            IntervalMonthDayNano::new(0, 0, 1234567000)
12037        );
12038
12039        let array = vec![i64::MAX];
12040        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
12041            array.clone(),
12042            &CastOptions::default(),
12043        )
12044        .unwrap();
12045        assert!(!casted_array.is_valid(0));
12046
12047        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
12048            array,
12049            &CastOptions {
12050                safe: false,
12051                format_options: FormatOptions::default(),
12052            },
12053        );
12054        assert!(casted_array.is_err());
12055
12056        // from duration nanosecond to interval month day nano
12057        let array = vec![1234567];
12058        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
12059            array,
12060            &CastOptions::default(),
12061        )
12062        .unwrap();
12063        assert_eq!(
12064            casted_array.data_type(),
12065            &DataType::Interval(IntervalUnit::MonthDayNano)
12066        );
12067        assert_eq!(
12068            casted_array.value(0),
12069            IntervalMonthDayNano::new(0, 0, 1234567)
12070        );
12071
12072        let array = vec![i64::MAX];
12073        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
12074            array,
12075            &CastOptions {
12076                safe: false,
12077                format_options: FormatOptions::default(),
12078            },
12079        )
12080        .unwrap();
12081        assert_eq!(
12082            casted_array.value(0),
12083            IntervalMonthDayNano::new(0, 0, i64::MAX)
12084        );
12085    }
12086
12087    /// helper function to test casting from interval to duration
12088    fn cast_from_interval_to_duration<T: ArrowTemporalType>(
12089        array: &IntervalMonthDayNanoArray,
12090        cast_options: &CastOptions,
12091    ) -> Result<PrimitiveArray<T>, ArrowError> {
12092        let casted_array = cast_with_options(&array, &T::DATA_TYPE, cast_options)?;
12093        casted_array
12094            .as_any()
12095            .downcast_ref::<PrimitiveArray<T>>()
12096            .ok_or_else(|| {
12097                ArrowError::ComputeError(format!("Failed to downcast to {}", T::DATA_TYPE))
12098            })
12099            .cloned()
12100    }
12101
12102    #[test]
12103    fn test_cast_from_interval_to_duration() {
12104        let nullable = CastOptions::default();
12105        let fallible = CastOptions {
12106            safe: false,
12107            format_options: FormatOptions::default(),
12108        };
12109        let v = IntervalMonthDayNano::new(0, 0, 1234567);
12110
12111        // from interval month day nano to duration second
12112        let array = vec![v].into();
12113        let casted_array: DurationSecondArray =
12114            cast_from_interval_to_duration(&array, &nullable).unwrap();
12115        assert_eq!(casted_array.value(0), 0);
12116
12117        let array = vec![IntervalMonthDayNano::MAX].into();
12118        let casted_array: DurationSecondArray =
12119            cast_from_interval_to_duration(&array, &nullable).unwrap();
12120        assert!(!casted_array.is_valid(0));
12121
12122        let res = cast_from_interval_to_duration::<DurationSecondType>(&array, &fallible);
12123        assert!(res.is_err());
12124
12125        // from interval month day nano to duration millisecond
12126        let array = vec![v].into();
12127        let casted_array: DurationMillisecondArray =
12128            cast_from_interval_to_duration(&array, &nullable).unwrap();
12129        assert_eq!(casted_array.value(0), 1);
12130
12131        let array = vec![IntervalMonthDayNano::MAX].into();
12132        let casted_array: DurationMillisecondArray =
12133            cast_from_interval_to_duration(&array, &nullable).unwrap();
12134        assert!(!casted_array.is_valid(0));
12135
12136        let res = cast_from_interval_to_duration::<DurationMillisecondType>(&array, &fallible);
12137        assert!(res.is_err());
12138
12139        // from interval month day nano to duration microsecond
12140        let array = vec![v].into();
12141        let casted_array: DurationMicrosecondArray =
12142            cast_from_interval_to_duration(&array, &nullable).unwrap();
12143        assert_eq!(casted_array.value(0), 1234);
12144
12145        let array = vec![IntervalMonthDayNano::MAX].into();
12146        let casted_array =
12147            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &nullable).unwrap();
12148        assert!(!casted_array.is_valid(0));
12149
12150        let casted_array =
12151            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &fallible);
12152        assert!(casted_array.is_err());
12153
12154        // from interval month day nano to duration nanosecond
12155        let array = vec![v].into();
12156        let casted_array: DurationNanosecondArray =
12157            cast_from_interval_to_duration(&array, &nullable).unwrap();
12158        assert_eq!(casted_array.value(0), 1234567);
12159
12160        let array = vec![IntervalMonthDayNano::MAX].into();
12161        let casted_array: DurationNanosecondArray =
12162            cast_from_interval_to_duration(&array, &nullable).unwrap();
12163        assert!(!casted_array.is_valid(0));
12164
12165        let casted_array =
12166            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &fallible);
12167        assert!(casted_array.is_err());
12168
12169        let array = vec![
12170            IntervalMonthDayNanoType::make_value(0, 1, 0),
12171            IntervalMonthDayNanoType::make_value(-1, 0, 0),
12172            IntervalMonthDayNanoType::make_value(1, 1, 0),
12173            IntervalMonthDayNanoType::make_value(1, 0, 1),
12174            IntervalMonthDayNanoType::make_value(0, 0, -1),
12175        ]
12176        .into();
12177        let casted_array =
12178            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable).unwrap();
12179        assert!(!casted_array.is_valid(0));
12180        assert!(!casted_array.is_valid(1));
12181        assert!(!casted_array.is_valid(2));
12182        assert!(!casted_array.is_valid(3));
12183        assert!(casted_array.is_valid(4));
12184        assert_eq!(casted_array.value(4), -1);
12185    }
12186
12187    /// helper function to test casting from interval year month to interval month day nano
12188    fn cast_from_interval_year_month_to_interval_month_day_nano(
12189        array: Vec<i32>,
12190        cast_options: &CastOptions,
12191    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
12192        let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
12193        let array = Arc::new(array) as ArrayRef;
12194        let casted_array = cast_with_options(
12195            &array,
12196            &DataType::Interval(IntervalUnit::MonthDayNano),
12197            cast_options,
12198        )?;
12199        casted_array
12200            .as_any()
12201            .downcast_ref::<IntervalMonthDayNanoArray>()
12202            .ok_or_else(|| {
12203                ArrowError::ComputeError(
12204                    "Failed to downcast to IntervalMonthDayNanoArray".to_string(),
12205                )
12206            })
12207            .cloned()
12208    }
12209
12210    #[test]
12211    fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
12212        // from interval year month to interval month day nano
12213        let array = vec![1234567];
12214        let casted_array = cast_from_interval_year_month_to_interval_month_day_nano(
12215            array,
12216            &CastOptions::default(),
12217        )
12218        .unwrap();
12219        assert_eq!(
12220            casted_array.data_type(),
12221            &DataType::Interval(IntervalUnit::MonthDayNano)
12222        );
12223        assert_eq!(
12224            casted_array.value(0),
12225            IntervalMonthDayNano::new(1234567, 0, 0)
12226        );
12227    }
12228
12229    /// helper function to test casting from interval day time to interval month day nano
12230    fn cast_from_interval_day_time_to_interval_month_day_nano(
12231        array: Vec<IntervalDayTime>,
12232        cast_options: &CastOptions,
12233    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
12234        let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
12235        let array = Arc::new(array) as ArrayRef;
12236        let casted_array = cast_with_options(
12237            &array,
12238            &DataType::Interval(IntervalUnit::MonthDayNano),
12239            cast_options,
12240        )?;
12241        Ok(casted_array
12242            .as_primitive::<IntervalMonthDayNanoType>()
12243            .clone())
12244    }
12245
12246    #[test]
12247    fn test_cast_from_interval_day_time_to_interval_month_day_nano() {
12248        // from interval day time to interval month day nano
12249        let array = vec![IntervalDayTime::new(123, 0)];
12250        let casted_array =
12251            cast_from_interval_day_time_to_interval_month_day_nano(array, &CastOptions::default())
12252                .unwrap();
12253        assert_eq!(
12254            casted_array.data_type(),
12255            &DataType::Interval(IntervalUnit::MonthDayNano)
12256        );
12257        assert_eq!(casted_array.value(0), IntervalMonthDayNano::new(0, 123, 0));
12258    }
12259
12260    #[test]
12261    fn test_cast_below_unixtimestamp() {
12262        let valid = StringArray::from(vec![
12263            "1900-01-03 23:59:59",
12264            "1969-12-31 00:00:01",
12265            "1989-12-31 00:00:01",
12266        ]);
12267
12268        let array = Arc::new(valid) as ArrayRef;
12269        let casted_array = cast_with_options(
12270            &array,
12271            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
12272            &CastOptions {
12273                safe: false,
12274                format_options: FormatOptions::default(),
12275            },
12276        )
12277        .unwrap();
12278
12279        let ts_array = casted_array
12280            .as_primitive::<TimestampNanosecondType>()
12281            .values()
12282            .iter()
12283            .map(|ts| ts / 1_000_000)
12284            .collect::<Vec<_>>();
12285
12286        let array = TimestampMillisecondArray::from(ts_array).with_timezone("+00:00".to_string());
12287        let casted_array = cast(&array, &DataType::Date32).unwrap();
12288        let date_array = casted_array.as_primitive::<Date32Type>();
12289        let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
12290        let string_array = casted_array.as_string::<i32>();
12291        assert_eq!("1900-01-03", string_array.value(0));
12292        assert_eq!("1969-12-31", string_array.value(1));
12293        assert_eq!("1989-12-31", string_array.value(2));
12294    }
12295
12296    #[test]
12297    fn test_nested_list() {
12298        let mut list = ListBuilder::new(Int32Builder::new());
12299        list.append_value([Some(1), Some(2), Some(3)]);
12300        list.append_value([Some(4), None, Some(6)]);
12301        let list = list.finish();
12302
12303        let to_field = Field::new("nested", list.data_type().clone(), false);
12304        let to = DataType::List(Arc::new(to_field));
12305        let out = cast(&list, &to).unwrap();
12306        let opts = FormatOptions::default().with_null("null");
12307        let formatted = ArrayFormatter::try_new(out.as_ref(), &opts).unwrap();
12308
12309        assert_eq!(formatted.value(0).to_string(), "[[1], [2], [3]]");
12310        assert_eq!(formatted.value(1).to_string(), "[[4], [null], [6]]");
12311    }
12312
12313    #[test]
12314    fn test_nested_list_cast() {
12315        let mut builder = ListBuilder::new(ListBuilder::new(Int32Builder::new()));
12316        builder.append_value([Some([Some(1), Some(2), None]), None]);
12317        builder.append_value([None, Some([]), None]);
12318        builder.append_null();
12319        builder.append_value([Some([Some(2), Some(3)])]);
12320        let start = builder.finish();
12321
12322        let mut builder = LargeListBuilder::new(LargeListBuilder::new(Int8Builder::new()));
12323        builder.append_value([Some([Some(1), Some(2), None]), None]);
12324        builder.append_value([None, Some([]), None]);
12325        builder.append_null();
12326        builder.append_value([Some([Some(2), Some(3)])]);
12327        let expected = builder.finish();
12328
12329        let actual = cast(&start, expected.data_type()).unwrap();
12330        assert_eq!(actual.as_ref(), &expected);
12331    }
12332
12333    const CAST_OPTIONS: CastOptions<'static> = CastOptions {
12334        safe: true,
12335        format_options: FormatOptions::new(),
12336    };
12337
12338    #[test]
12339    #[expect(clippy::assertions_on_constants)]
12340    fn test_const_options() {
12341        assert!(CAST_OPTIONS.safe)
12342    }
12343
12344    #[test]
12345    fn test_list_format_options() {
12346        let options = CastOptions {
12347            safe: false,
12348            format_options: FormatOptions::default().with_null("null"),
12349        };
12350        let array = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
12351            Some(vec![Some(0), Some(1), Some(2)]),
12352            Some(vec![Some(0), None, Some(2)]),
12353        ]);
12354        let a = cast_with_options(&array, &DataType::Utf8, &options).unwrap();
12355        let r: Vec<_> = a.as_string::<i32>().iter().flatten().collect();
12356        assert_eq!(r, &["[0, 1, 2]", "[0, null, 2]"]);
12357    }
12358    #[test]
12359    fn test_cast_string_to_timestamp_invalid_tz() {
12360        // content after Z should be ignored
12361        let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
12362        let array = StringArray::from(vec![Some(bad_timestamp)]);
12363
12364        let data_types = [
12365            DataType::Timestamp(TimeUnit::Second, None),
12366            DataType::Timestamp(TimeUnit::Millisecond, None),
12367            DataType::Timestamp(TimeUnit::Microsecond, None),
12368            DataType::Timestamp(TimeUnit::Nanosecond, None),
12369        ];
12370
12371        let cast_options = CastOptions {
12372            safe: false,
12373            ..Default::default()
12374        };
12375
12376        for dt in data_types {
12377            assert_eq!(
12378                cast_with_options(&array, &dt, &cast_options)
12379                    .unwrap_err()
12380                    .to_string(),
12381                "Parser error: Invalid timezone \"ZZTOP\": only offset based timezones supported without chrono-tz feature"
12382            );
12383        }
12384    }
12385    #[test]
12386    fn test_cast_struct_to_struct() {
12387        let struct_type = DataType::Struct(
12388            vec![
12389                Field::new("a", DataType::Boolean, false),
12390                Field::new("b", DataType::Int32, false),
12391            ]
12392            .into(),
12393        );
12394        let to_type = DataType::Struct(
12395            vec![
12396                Field::new("a", DataType::Utf8, false),
12397                Field::new("b", DataType::Utf8, false),
12398            ]
12399            .into(),
12400        );
12401        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12402        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
12403        let struct_array = StructArray::from(vec![
12404            (
12405                Arc::new(Field::new("b", DataType::Boolean, false)),
12406                boolean.clone() as ArrayRef,
12407            ),
12408            (
12409                Arc::new(Field::new("c", DataType::Int32, false)),
12410                int.clone() as ArrayRef,
12411            ),
12412        ]);
12413        let casted_array = cast(&struct_array, &to_type).unwrap();
12414        let casted_array = casted_array.as_struct();
12415        assert_eq!(casted_array.data_type(), &to_type);
12416        let casted_boolean_array = casted_array
12417            .column(0)
12418            .as_string::<i32>()
12419            .into_iter()
12420            .flatten()
12421            .collect::<Vec<_>>();
12422        let casted_int_array = casted_array
12423            .column(1)
12424            .as_string::<i32>()
12425            .into_iter()
12426            .flatten()
12427            .collect::<Vec<_>>();
12428        assert_eq!(casted_boolean_array, vec!["false", "false", "true", "true"]);
12429        assert_eq!(casted_int_array, vec!["42", "28", "19", "31"]);
12430
12431        // test for can't cast
12432        let to_type = DataType::Struct(
12433            vec![
12434                Field::new("a", DataType::Date32, false),
12435                Field::new("b", DataType::Utf8, false),
12436            ]
12437            .into(),
12438        );
12439        assert!(!can_cast_types(&struct_type, &to_type));
12440        let result = cast(&struct_array, &to_type);
12441        assert_eq!(
12442            "Cast error: Casting from Boolean to Date32 not supported",
12443            result.unwrap_err().to_string()
12444        );
12445    }
12446
12447    #[test]
12448    fn test_cast_struct_to_struct_nullability() {
12449        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12450        let int = Arc::new(Int32Array::from(vec![Some(42), None, Some(19), None]));
12451        let struct_array = StructArray::from(vec![
12452            (
12453                Arc::new(Field::new("b", DataType::Boolean, false)),
12454                boolean.clone() as ArrayRef,
12455            ),
12456            (
12457                Arc::new(Field::new("c", DataType::Int32, true)),
12458                int.clone() as ArrayRef,
12459            ),
12460        ]);
12461
12462        // okay: nullable to nullable
12463        let to_type = DataType::Struct(
12464            vec![
12465                Field::new("a", DataType::Utf8, false),
12466                Field::new("b", DataType::Utf8, true),
12467            ]
12468            .into(),
12469        );
12470        cast(&struct_array, &to_type).expect("Cast nullable to nullable struct field should work");
12471
12472        // error: nullable to non-nullable
12473        let to_type = DataType::Struct(
12474            vec![
12475                Field::new("a", DataType::Utf8, false),
12476                Field::new("b", DataType::Utf8, false),
12477            ]
12478            .into(),
12479        );
12480        cast(&struct_array, &to_type)
12481            .expect_err("Cast nullable to non-nullable struct field should fail");
12482
12483        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12484        let int = Arc::new(Int32Array::from(vec![i32::MAX, 25, 1, 100]));
12485        let struct_array = StructArray::from(vec![
12486            (
12487                Arc::new(Field::new("b", DataType::Boolean, false)),
12488                boolean.clone() as ArrayRef,
12489            ),
12490            (
12491                Arc::new(Field::new("c", DataType::Int32, false)),
12492                int.clone() as ArrayRef,
12493            ),
12494        ]);
12495
12496        // okay: non-nullable to non-nullable
12497        let to_type = DataType::Struct(
12498            vec![
12499                Field::new("a", DataType::Utf8, false),
12500                Field::new("b", DataType::Utf8, false),
12501            ]
12502            .into(),
12503        );
12504        cast(&struct_array, &to_type)
12505            .expect("Cast non-nullable to non-nullable struct field should work");
12506
12507        // err: non-nullable to non-nullable but overflowing return null during casting
12508        let to_type = DataType::Struct(
12509            vec![
12510                Field::new("a", DataType::Utf8, false),
12511                Field::new("b", DataType::Int8, false),
12512            ]
12513            .into(),
12514        );
12515        cast(&struct_array, &to_type).expect_err(
12516            "Cast non-nullable to non-nullable struct field returning null should fail",
12517        );
12518    }
12519
12520    #[test]
12521    fn test_cast_struct_to_non_struct() {
12522        let boolean = Arc::new(BooleanArray::from(vec![true, false]));
12523        let struct_array = StructArray::from(vec![(
12524            Arc::new(Field::new("a", DataType::Boolean, false)),
12525            boolean.clone() as ArrayRef,
12526        )]);
12527        let to_type = DataType::Utf8;
12528        let result = cast(&struct_array, &to_type);
12529        assert_eq!(
12530            r#"Cast error: Casting from Struct("a": non-null Boolean) to Utf8 not supported"#,
12531            result.unwrap_err().to_string()
12532        );
12533    }
12534
12535    #[test]
12536    fn test_cast_non_struct_to_struct() {
12537        let array = StringArray::from(vec!["a", "b"]);
12538        let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
12539        let result = cast(&array, &to_type);
12540        assert_eq!(
12541            r#"Cast error: Casting from Utf8 to Struct("a": non-null Boolean) not supported"#,
12542            result.unwrap_err().to_string()
12543        );
12544    }
12545
12546    #[test]
12547    fn test_cast_struct_with_different_field_order() {
12548        // Test slow path: fields are in different order
12549        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12550        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
12551        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
12552
12553        let struct_array = StructArray::from(vec![
12554            (
12555                Arc::new(Field::new("a", DataType::Boolean, false)),
12556                boolean.clone() as ArrayRef,
12557            ),
12558            (
12559                Arc::new(Field::new("b", DataType::Int32, false)),
12560                int.clone() as ArrayRef,
12561            ),
12562            (
12563                Arc::new(Field::new("c", DataType::Utf8, false)),
12564                string.clone() as ArrayRef,
12565            ),
12566        ]);
12567
12568        // Target has fields in different order: c, a, b instead of a, b, c
12569        let to_type = DataType::Struct(
12570            vec![
12571                Field::new("c", DataType::Utf8, false),
12572                Field::new("a", DataType::Utf8, false), // Boolean to Utf8
12573                Field::new("b", DataType::Utf8, false), // Int32 to Utf8
12574            ]
12575            .into(),
12576        );
12577
12578        let result = cast(&struct_array, &to_type).unwrap();
12579        let result_struct = result.as_struct();
12580
12581        assert_eq!(result_struct.data_type(), &to_type);
12582        assert_eq!(result_struct.num_columns(), 3);
12583
12584        // Verify field "c" (originally position 2, now position 0) remains Utf8
12585        let c_column = result_struct.column(0).as_string::<i32>();
12586        assert_eq!(
12587            c_column.into_iter().flatten().collect::<Vec<_>>(),
12588            vec!["foo", "bar", "baz", "qux"]
12589        );
12590
12591        // Verify field "a" (originally position 0, now position 1) was cast from Boolean to Utf8
12592        let a_column = result_struct.column(1).as_string::<i32>();
12593        assert_eq!(
12594            a_column.into_iter().flatten().collect::<Vec<_>>(),
12595            vec!["false", "false", "true", "true"]
12596        );
12597
12598        // Verify field "b" (originally position 1, now position 2) was cast from Int32 to Utf8
12599        let b_column = result_struct.column(2).as_string::<i32>();
12600        assert_eq!(
12601            b_column.into_iter().flatten().collect::<Vec<_>>(),
12602            vec!["42", "28", "19", "31"]
12603        );
12604    }
12605
12606    #[test]
12607    fn test_cast_struct_with_missing_field() {
12608        // Test that casting fails when target has a field not present in source
12609        let boolean = Arc::new(BooleanArray::from(vec![false, true]));
12610        let struct_array = StructArray::from(vec![(
12611            Arc::new(Field::new("a", DataType::Boolean, false)),
12612            boolean.clone() as ArrayRef,
12613        )]);
12614
12615        let to_type = DataType::Struct(
12616            vec![
12617                Field::new("a", DataType::Utf8, false),
12618                Field::new("b", DataType::Int32, false), // Field "b" doesn't exist in source
12619            ]
12620            .into(),
12621        );
12622
12623        let result = cast(&struct_array, &to_type);
12624        assert!(result.is_err());
12625        assert_eq!(
12626            result.unwrap_err().to_string(),
12627            "Invalid argument error: Incorrect number of arrays for StructArray fields, expected 2 got 1"
12628        );
12629    }
12630
12631    #[test]
12632    fn test_cast_struct_with_subset_of_fields() {
12633        // Test casting to a struct with fewer fields (selecting a subset)
12634        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12635        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
12636        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
12637
12638        let struct_array = StructArray::from(vec![
12639            (
12640                Arc::new(Field::new("a", DataType::Boolean, false)),
12641                boolean.clone() as ArrayRef,
12642            ),
12643            (
12644                Arc::new(Field::new("b", DataType::Int32, false)),
12645                int.clone() as ArrayRef,
12646            ),
12647            (
12648                Arc::new(Field::new("c", DataType::Utf8, false)),
12649                string.clone() as ArrayRef,
12650            ),
12651        ]);
12652
12653        // Target has only fields "c" and "a", omitting "b"
12654        let to_type = DataType::Struct(
12655            vec![
12656                Field::new("c", DataType::Utf8, false),
12657                Field::new("a", DataType::Utf8, false),
12658            ]
12659            .into(),
12660        );
12661
12662        let result = cast(&struct_array, &to_type).unwrap();
12663        let result_struct = result.as_struct();
12664
12665        assert_eq!(result_struct.data_type(), &to_type);
12666        assert_eq!(result_struct.num_columns(), 2);
12667
12668        // Verify field "c" remains Utf8
12669        let c_column = result_struct.column(0).as_string::<i32>();
12670        assert_eq!(
12671            c_column.into_iter().flatten().collect::<Vec<_>>(),
12672            vec!["foo", "bar", "baz", "qux"]
12673        );
12674
12675        // Verify field "a" was cast from Boolean to Utf8
12676        let a_column = result_struct.column(1).as_string::<i32>();
12677        assert_eq!(
12678            a_column.into_iter().flatten().collect::<Vec<_>>(),
12679            vec!["false", "false", "true", "true"]
12680        );
12681    }
12682
12683    #[test]
12684    fn test_can_cast_struct_rename_field() {
12685        // Test that can_cast_types returns false when target has a field not in source
12686        let from_type = DataType::Struct(
12687            vec![
12688                Field::new("a", DataType::Int32, false),
12689                Field::new("b", DataType::Utf8, false),
12690            ]
12691            .into(),
12692        );
12693
12694        let to_type = DataType::Struct(
12695            vec![
12696                Field::new("a", DataType::Int64, false),
12697                Field::new("c", DataType::Boolean, false), // Field "c" not in source
12698            ]
12699            .into(),
12700        );
12701
12702        assert!(can_cast_types(&from_type, &to_type));
12703    }
12704
12705    fn run_decimal_cast_test_case_between_multiple_types(t: DecimalCastTestConfig) {
12706        run_decimal_cast_test_case::<Decimal128Type, Decimal128Type>(t.clone());
12707        run_decimal_cast_test_case::<Decimal128Type, Decimal256Type>(t.clone());
12708        run_decimal_cast_test_case::<Decimal256Type, Decimal128Type>(t.clone());
12709        run_decimal_cast_test_case::<Decimal256Type, Decimal256Type>(t.clone());
12710    }
12711
12712    #[test]
12713    fn test_decimal_to_decimal_coverage() {
12714        let test_cases = [
12715            // increase precision, increase scale, infallible
12716            DecimalCastTestConfig {
12717                input_prec: 5,
12718                input_scale: 1,
12719                input_repr: 99999, // 9999.9
12720                output_prec: 10,
12721                output_scale: 6,
12722                expected_output_repr: Ok(9999900000), // 9999.900000
12723            },
12724            // increase precision, increase scale, fallible, safe
12725            DecimalCastTestConfig {
12726                input_prec: 5,
12727                input_scale: 1,
12728                input_repr: 99, // 9999.9
12729                output_prec: 7,
12730                output_scale: 6,
12731                expected_output_repr: Ok(9900000), // 9.900000
12732            },
12733            // increase precision, increase scale, fallible, unsafe
12734            DecimalCastTestConfig {
12735                input_prec: 5,
12736                input_scale: 1,
12737                input_repr: 99999, // 9999.9
12738                output_prec: 7,
12739                output_scale: 6,
12740                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
12741            },
12742            // increase precision, decrease scale, always infallible
12743            DecimalCastTestConfig {
12744                input_prec: 5,
12745                input_scale: 3,
12746                input_repr: 99999, // 99.999
12747                output_prec: 10,
12748                output_scale: 2,
12749                expected_output_repr: Ok(10000), // 100.00
12750            },
12751            // increase precision, decrease scale, no rouding
12752            DecimalCastTestConfig {
12753                input_prec: 5,
12754                input_scale: 3,
12755                input_repr: 99994, // 99.994
12756                output_prec: 10,
12757                output_scale: 2,
12758                expected_output_repr: Ok(9999), // 99.99
12759            },
12760            // increase precision, don't change scale, always infallible
12761            DecimalCastTestConfig {
12762                input_prec: 5,
12763                input_scale: 3,
12764                input_repr: 99999, // 99.999
12765                output_prec: 10,
12766                output_scale: 3,
12767                expected_output_repr: Ok(99999), // 99.999
12768            },
12769            // decrease precision, increase scale, safe
12770            DecimalCastTestConfig {
12771                input_prec: 10,
12772                input_scale: 5,
12773                input_repr: 999999, // 9.99999
12774                output_prec: 8,
12775                output_scale: 7,
12776                expected_output_repr: Ok(99999900), // 9.9999900
12777            },
12778            // decrease precision, increase scale, unsafe
12779            DecimalCastTestConfig {
12780                input_prec: 10,
12781                input_scale: 5,
12782                input_repr: 9999999, // 99.99999
12783                output_prec: 8,
12784                output_scale: 7,
12785                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
12786            },
12787            // decrease precision, decrease scale, safe, infallible
12788            DecimalCastTestConfig {
12789                input_prec: 7,
12790                input_scale: 4,
12791                input_repr: 9999999, // 999.9999
12792                output_prec: 6,
12793                output_scale: 2,
12794                expected_output_repr: Ok(100000),
12795            },
12796            // decrease precision, decrease scale, safe, fallible
12797            DecimalCastTestConfig {
12798                input_prec: 10,
12799                input_scale: 5,
12800                input_repr: 12345678, // 123.45678
12801                output_prec: 8,
12802                output_scale: 3,
12803                expected_output_repr: Ok(123457), // 123.457
12804            },
12805            // decrease precision, decrease scale, unsafe
12806            DecimalCastTestConfig {
12807                input_prec: 10,
12808                input_scale: 5,
12809                input_repr: 9999999, // 99.99999
12810                output_prec: 4,
12811                output_scale: 3,
12812                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
12813            },
12814            // decrease precision, same scale, safe
12815            DecimalCastTestConfig {
12816                input_prec: 10,
12817                input_scale: 5,
12818                input_repr: 999999, // 9.99999
12819                output_prec: 6,
12820                output_scale: 5,
12821                expected_output_repr: Ok(999999), // 9.99999
12822            },
12823            // decrease precision, same scale, unsafe
12824            DecimalCastTestConfig {
12825                input_prec: 10,
12826                input_scale: 5,
12827                input_repr: 9999999, // 99.99999
12828                output_prec: 6,
12829                output_scale: 5,
12830                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
12831            },
12832            // same precision, increase scale, safe
12833            DecimalCastTestConfig {
12834                input_prec: 7,
12835                input_scale: 4,
12836                input_repr: 12345, // 1.2345
12837                output_prec: 7,
12838                output_scale: 6,
12839                expected_output_repr: Ok(1234500), // 1.234500
12840            },
12841            // same precision, increase scale, unsafe
12842            DecimalCastTestConfig {
12843                input_prec: 7,
12844                input_scale: 4,
12845                input_repr: 123456, // 12.3456
12846                output_prec: 7,
12847                output_scale: 6,
12848                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
12849            },
12850            // same precision, decrease scale, infallible
12851            DecimalCastTestConfig {
12852                input_prec: 7,
12853                input_scale: 5,
12854                input_repr: 1234567, // 12.34567
12855                output_prec: 7,
12856                output_scale: 4,
12857                expected_output_repr: Ok(123457), // 12.3457
12858            },
12859            // same precision, same scale, infallible
12860            DecimalCastTestConfig {
12861                input_prec: 7,
12862                input_scale: 5,
12863                input_repr: 9999999, // 99.99999
12864                output_prec: 7,
12865                output_scale: 5,
12866                expected_output_repr: Ok(9999999), // 99.99999
12867            },
12868            // precision increase, input scale & output scale = 0, infallible
12869            DecimalCastTestConfig {
12870                input_prec: 7,
12871                input_scale: 0,
12872                input_repr: 1234567, // 1234567
12873                output_prec: 8,
12874                output_scale: 0,
12875                expected_output_repr: Ok(1234567), // 1234567
12876            },
12877            // precision decrease, input scale & output scale = 0, failure
12878            DecimalCastTestConfig {
12879                input_prec: 7,
12880                input_scale: 0,
12881                input_repr: 1234567, // 1234567
12882                output_prec: 6,
12883                output_scale: 0,
12884                expected_output_repr: Err("Invalid argument error: 1234567 is too large to store in a {} of precision 6. Max is 999999".to_string())
12885            },
12886            // precision decrease, input scale & output scale = 0, success
12887            DecimalCastTestConfig {
12888                input_prec: 7,
12889                input_scale: 0,
12890                input_repr: 123456, // 123456
12891                output_prec: 6,
12892                output_scale: 0,
12893                expected_output_repr: Ok(123456), // 123456
12894            },
12895        ];
12896
12897        for t in test_cases {
12898            run_decimal_cast_test_case_between_multiple_types(t);
12899        }
12900    }
12901
12902    #[test]
12903    fn test_decimal_to_decimal_increase_scale_and_precision_unchecked() {
12904        let test_cases = [
12905            DecimalCastTestConfig {
12906                input_prec: 5,
12907                input_scale: 0,
12908                input_repr: 99999,
12909                output_prec: 10,
12910                output_scale: 5,
12911                expected_output_repr: Ok(9999900000),
12912            },
12913            DecimalCastTestConfig {
12914                input_prec: 5,
12915                input_scale: 0,
12916                input_repr: -99999,
12917                output_prec: 10,
12918                output_scale: 5,
12919                expected_output_repr: Ok(-9999900000),
12920            },
12921            DecimalCastTestConfig {
12922                input_prec: 5,
12923                input_scale: 2,
12924                input_repr: 99999,
12925                output_prec: 10,
12926                output_scale: 5,
12927                expected_output_repr: Ok(99999000),
12928            },
12929            DecimalCastTestConfig {
12930                input_prec: 5,
12931                input_scale: -2,
12932                input_repr: -99999,
12933                output_prec: 10,
12934                output_scale: 3,
12935                expected_output_repr: Ok(-9999900000),
12936            },
12937            DecimalCastTestConfig {
12938                input_prec: 5,
12939                input_scale: 3,
12940                input_repr: -12345,
12941                output_prec: 6,
12942                output_scale: 5,
12943                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())
12944            },
12945        ];
12946
12947        for t in test_cases {
12948            run_decimal_cast_test_case_between_multiple_types(t);
12949        }
12950    }
12951
12952    #[test]
12953    fn test_decimal_to_decimal_decrease_scale_and_precision_unchecked() {
12954        let test_cases = [
12955            DecimalCastTestConfig {
12956                input_prec: 5,
12957                input_scale: 0,
12958                input_repr: 99999,
12959                output_scale: -3,
12960                output_prec: 3,
12961                expected_output_repr: Ok(100),
12962            },
12963            DecimalCastTestConfig {
12964                input_prec: 5,
12965                input_scale: 0,
12966                input_repr: -99999,
12967                output_prec: 1,
12968                output_scale: -5,
12969                expected_output_repr: Ok(-1),
12970            },
12971            DecimalCastTestConfig {
12972                input_prec: 10,
12973                input_scale: 2,
12974                input_repr: 123456789,
12975                output_prec: 5,
12976                output_scale: -2,
12977                expected_output_repr: Ok(12346),
12978            },
12979            DecimalCastTestConfig {
12980                input_prec: 10,
12981                input_scale: 4,
12982                input_repr: -9876543210,
12983                output_prec: 7,
12984                output_scale: 0,
12985                expected_output_repr: Ok(-987654),
12986            },
12987            DecimalCastTestConfig {
12988                input_prec: 7,
12989                input_scale: 4,
12990                input_repr: 9999999,
12991                output_prec: 6,
12992                output_scale: 3,
12993                expected_output_repr:
12994                    Err("Invalid argument error: 1000.000 is too large to store in a {} of precision 6. Max is 999.999".to_string()),
12995            },
12996        ];
12997        for t in test_cases {
12998            run_decimal_cast_test_case_between_multiple_types(t);
12999        }
13000    }
13001
13002    #[test]
13003    fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
13004        let array = vec![Some(123456789)];
13005        let array = create_decimal128_array(array, 24, 2).unwrap();
13006        let input_type = DataType::Decimal128(24, 2);
13007        let output_type = DataType::Decimal128(6, 2);
13008        assert!(can_cast_types(&input_type, &output_type));
13009
13010        let options = CastOptions {
13011            safe: false,
13012            ..Default::default()
13013        };
13014        let result = cast_with_options(&array, &output_type, &options);
13015        assert_eq!(
13016            result.unwrap_err().to_string(),
13017            "Invalid argument error: 1234567.89 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
13018        );
13019    }
13020
13021    #[test]
13022    fn test_decimal_to_decimal_same_scale() {
13023        let array = vec![Some(520)];
13024        let array = create_decimal128_array(array, 4, 2).unwrap();
13025        let input_type = DataType::Decimal128(4, 2);
13026        let output_type = DataType::Decimal128(3, 2);
13027        assert!(can_cast_types(&input_type, &output_type));
13028
13029        let options = CastOptions {
13030            safe: false,
13031            ..Default::default()
13032        };
13033        let result = cast_with_options(&array, &output_type, &options);
13034        assert_eq!(
13035            result.unwrap().as_primitive::<Decimal128Type>().value(0),
13036            520
13037        );
13038
13039        // Cast 0 of decimal(3, 0) type to decimal(2, 0)
13040        assert_eq!(
13041            &cast(
13042                &create_decimal128_array(vec![Some(0)], 3, 0).unwrap(),
13043                &DataType::Decimal128(2, 0)
13044            )
13045            .unwrap(),
13046            &(Arc::new(create_decimal128_array(vec![Some(0)], 2, 0).unwrap()) as ArrayRef)
13047        );
13048    }
13049
13050    #[test]
13051    fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale() {
13052        let array = vec![Some(123456789)];
13053        let array = create_decimal128_array(array, 24, 4).unwrap();
13054        let input_type = DataType::Decimal128(24, 4);
13055        let output_type = DataType::Decimal128(6, 2);
13056        assert!(can_cast_types(&input_type, &output_type));
13057
13058        let options = CastOptions {
13059            safe: false,
13060            ..Default::default()
13061        };
13062        let result = cast_with_options(&array, &output_type, &options);
13063        assert_eq!(
13064            result.unwrap_err().to_string(),
13065            "Invalid argument error: 12345.68 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
13066        );
13067    }
13068
13069    #[test]
13070    fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale() {
13071        let array = vec![Some(123456789)];
13072        let array = create_decimal128_array(array, 24, 2).unwrap();
13073        let input_type = DataType::Decimal128(24, 2);
13074        let output_type = DataType::Decimal128(6, 3);
13075        assert!(can_cast_types(&input_type, &output_type));
13076
13077        let options = CastOptions {
13078            safe: false,
13079            ..Default::default()
13080        };
13081        let result = cast_with_options(&array, &output_type, &options);
13082        assert_eq!(
13083            result.unwrap_err().to_string(),
13084            "Invalid argument error: 1234567.890 is too large to store in a Decimal128 of precision 6. Max is 999.999"
13085        );
13086    }
13087
13088    #[test]
13089    fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type() {
13090        let array = vec![Some(123456789)];
13091        let array = create_decimal128_array(array, 24, 2).unwrap();
13092        let input_type = DataType::Decimal128(24, 2);
13093        let output_type = DataType::Decimal256(6, 2);
13094        assert!(can_cast_types(&input_type, &output_type));
13095
13096        let options = CastOptions {
13097            safe: false,
13098            ..Default::default()
13099        };
13100        let result = cast_with_options(&array, &output_type, &options).unwrap_err();
13101        assert_eq!(
13102            result.to_string(),
13103            "Invalid argument error: 1234567.89 is too large to store in a Decimal256 of precision 6. Max is 9999.99"
13104        );
13105    }
13106
13107    #[test]
13108    fn test_first_none() {
13109        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
13110            None,
13111            Some(vec![Some(1), Some(2)]),
13112        ])) as ArrayRef;
13113        let data_type =
13114            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
13115        let opt = CastOptions::default();
13116        let r = cast_with_options(&array, &data_type, &opt).unwrap();
13117
13118        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
13119            vec![None, Some(vec![Some(1), Some(2)])],
13120            2,
13121        )) as ArrayRef;
13122        assert_eq!(*fixed_array, *r);
13123    }
13124
13125    #[test]
13126    fn test_first_last_none() {
13127        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
13128            None,
13129            Some(vec![Some(1), Some(2)]),
13130            None,
13131        ])) as ArrayRef;
13132        let data_type =
13133            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
13134        let opt = CastOptions::default();
13135        let r = cast_with_options(&array, &data_type, &opt).unwrap();
13136
13137        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
13138            vec![None, Some(vec![Some(1), Some(2)]), None],
13139            2,
13140        )) as ArrayRef;
13141        assert_eq!(*fixed_array, *r);
13142    }
13143
13144    #[test]
13145    fn test_cast_decimal_error_output() {
13146        let array = Int64Array::from(vec![1]);
13147        let error = cast_with_options(
13148            &array,
13149            &DataType::Decimal32(1, 1),
13150            &CastOptions {
13151                safe: false,
13152                format_options: FormatOptions::default(),
13153            },
13154        )
13155        .unwrap_err();
13156        assert_eq!(
13157            error.to_string(),
13158            "Invalid argument error: 1.0 is too large to store in a Decimal32 of precision 1. Max is 0.9"
13159        );
13160
13161        let array = Int64Array::from(vec![-1]);
13162        let error = cast_with_options(
13163            &array,
13164            &DataType::Decimal32(1, 1),
13165            &CastOptions {
13166                safe: false,
13167                format_options: FormatOptions::default(),
13168            },
13169        )
13170        .unwrap_err();
13171        assert_eq!(
13172            error.to_string(),
13173            "Invalid argument error: -1.0 is too small to store in a Decimal32 of precision 1. Min is -0.9"
13174        );
13175    }
13176
13177    #[test]
13178    fn test_run_end_encoded_to_primitive() {
13179        // Create a RunEndEncoded array: [1, 1, 2, 2, 2, 3]
13180        let run_ends = Int32Array::from(vec![2, 5, 6]);
13181        let values = Int32Array::from(vec![1, 2, 3]);
13182        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13183        let array_ref = Arc::new(run_array) as ArrayRef;
13184        // Cast to Int64
13185        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
13186        // Verify the result is a RunArray with Int64 values
13187        let result_run_array = cast_result.as_any().downcast_ref::<Int64Array>().unwrap();
13188        assert_eq!(
13189            result_run_array.values(),
13190            &[1i64, 1i64, 2i64, 2i64, 2i64, 3i64]
13191        );
13192    }
13193
13194    #[test]
13195    fn test_sliced_run_end_encoded_to_primitive() {
13196        let run_ends = Int32Array::from(vec![2, 5, 6]);
13197        let values = Int32Array::from(vec![1, 2, 3]);
13198        // [1, 1, 2, 2, 2, 3]
13199        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13200        let run_array = run_array.slice(3, 3); // [2, 2, 3]
13201        let array_ref = Arc::new(run_array) as ArrayRef;
13202
13203        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
13204        let result_run_array = cast_result.as_primitive::<Int64Type>();
13205        assert_eq!(result_run_array.values(), &[2, 2, 3]);
13206    }
13207
13208    #[test]
13209    fn test_run_end_encoded_to_string() {
13210        let run_ends = Int32Array::from(vec![2, 3, 5]);
13211        let values = Int32Array::from(vec![10, 20, 30]);
13212        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13213        let array_ref = Arc::new(run_array) as ArrayRef;
13214
13215        // Cast to String
13216        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
13217
13218        // Verify the result is a RunArray with String values
13219        let result_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
13220        // Check that values are correct
13221        assert_eq!(result_array.value(0), "10");
13222        assert_eq!(result_array.value(1), "10");
13223        assert_eq!(result_array.value(2), "20");
13224    }
13225
13226    #[test]
13227    fn test_primitive_to_run_end_encoded() {
13228        // Create an Int32 array with repeated values: [1, 1, 2, 2, 2, 3]
13229        let source_array = Int32Array::from(vec![1, 1, 2, 2, 2, 3]);
13230        let array_ref = Arc::new(source_array) as ArrayRef;
13231
13232        // Cast to RunEndEncoded<Int32, Int32>
13233        let target_type = DataType::RunEndEncoded(
13234            Arc::new(Field::new("run_ends", DataType::Int32, false)),
13235            Arc::new(Field::new("values", DataType::Int32, true)),
13236        );
13237        let cast_result = cast(&array_ref, &target_type).unwrap();
13238
13239        // Verify the result is a RunArray
13240        let result_run_array = cast_result
13241            .as_any()
13242            .downcast_ref::<RunArray<Int32Type>>()
13243            .unwrap();
13244
13245        // Check run structure: runs should end at positions [2, 5, 6]
13246        assert_eq!(result_run_array.run_ends().values(), &[2, 5, 6]);
13247
13248        // Check values: should be [1, 2, 3]
13249        let values_array = result_run_array.values().as_primitive::<Int32Type>();
13250        assert_eq!(values_array.values(), &[1, 2, 3]);
13251    }
13252
13253    #[test]
13254    fn test_primitive_to_run_end_encoded_with_nulls() {
13255        let source_array = Int32Array::from(vec![
13256            Some(1),
13257            Some(1),
13258            None,
13259            None,
13260            Some(2),
13261            Some(2),
13262            Some(3),
13263            Some(3),
13264            None,
13265            None,
13266            Some(4),
13267            Some(4),
13268            Some(5),
13269            Some(5),
13270            None,
13271            None,
13272        ]);
13273        let array_ref = Arc::new(source_array) as ArrayRef;
13274        let target_type = DataType::RunEndEncoded(
13275            Arc::new(Field::new("run_ends", DataType::Int32, false)),
13276            Arc::new(Field::new("values", DataType::Int32, true)),
13277        );
13278        let cast_result = cast(&array_ref, &target_type).unwrap();
13279        let result_run_array = cast_result
13280            .as_any()
13281            .downcast_ref::<RunArray<Int32Type>>()
13282            .unwrap();
13283        assert_eq!(
13284            result_run_array.run_ends().values(),
13285            &[2, 4, 6, 8, 10, 12, 14, 16]
13286        );
13287        assert_eq!(
13288            result_run_array
13289                .values()
13290                .as_primitive::<Int32Type>()
13291                .values(),
13292            &[1, 0, 2, 3, 0, 4, 5, 0]
13293        );
13294        assert_eq!(result_run_array.values().null_count(), 3);
13295    }
13296
13297    #[test]
13298    fn test_primitive_to_run_end_encoded_with_nulls_consecutive() {
13299        let source_array = Int64Array::from(vec![
13300            Some(1),
13301            Some(1),
13302            None,
13303            None,
13304            None,
13305            None,
13306            None,
13307            None,
13308            None,
13309            None,
13310            Some(4),
13311            Some(20),
13312            Some(500),
13313            Some(500),
13314            None,
13315            None,
13316        ]);
13317        let array_ref = Arc::new(source_array) as ArrayRef;
13318        let target_type = DataType::RunEndEncoded(
13319            Arc::new(Field::new("run_ends", DataType::Int16, false)),
13320            Arc::new(Field::new("values", DataType::Int64, true)),
13321        );
13322        let cast_result = cast(&array_ref, &target_type).unwrap();
13323        let result_run_array = cast_result
13324            .as_any()
13325            .downcast_ref::<RunArray<Int16Type>>()
13326            .unwrap();
13327        assert_eq!(
13328            result_run_array.run_ends().values(),
13329            &[2, 10, 11, 12, 14, 16]
13330        );
13331        assert_eq!(
13332            result_run_array
13333                .values()
13334                .as_primitive::<Int64Type>()
13335                .values(),
13336            &[1, 0, 4, 20, 500, 0]
13337        );
13338        assert_eq!(result_run_array.values().null_count(), 2);
13339    }
13340
13341    #[test]
13342    fn test_string_to_run_end_encoded() {
13343        // Create a String array with repeated values: ["a", "a", "b", "c", "c"]
13344        let source_array = StringArray::from(vec!["a", "a", "b", "c", "c"]);
13345        let array_ref = Arc::new(source_array) as ArrayRef;
13346
13347        // Cast to RunEndEncoded<Int32, String>
13348        let target_type = DataType::RunEndEncoded(
13349            Arc::new(Field::new("run_ends", DataType::Int32, false)),
13350            Arc::new(Field::new("values", DataType::Utf8, true)),
13351        );
13352        let cast_result = cast(&array_ref, &target_type).unwrap();
13353
13354        // Verify the result is a RunArray
13355        let result_run_array = cast_result
13356            .as_any()
13357            .downcast_ref::<RunArray<Int32Type>>()
13358            .unwrap();
13359
13360        // Check run structure: runs should end at positions [2, 3, 5]
13361        assert_eq!(result_run_array.run_ends().values(), &[2, 3, 5]);
13362
13363        // Check values: should be ["a", "b", "c"]
13364        let values_array = result_run_array.values().as_string::<i32>();
13365        assert_eq!(values_array.value(0), "a");
13366        assert_eq!(values_array.value(1), "b");
13367        assert_eq!(values_array.value(2), "c");
13368    }
13369
13370    #[test]
13371    fn test_empty_array_to_run_end_encoded() {
13372        // Create an empty Int32 array
13373        let source_array = Int32Array::from(Vec::<i32>::new());
13374        let array_ref = Arc::new(source_array) as ArrayRef;
13375
13376        // Cast to RunEndEncoded<Int32, Int32>
13377        let target_type = DataType::RunEndEncoded(
13378            Arc::new(Field::new("run_ends", DataType::Int32, false)),
13379            Arc::new(Field::new("values", DataType::Int32, true)),
13380        );
13381        let cast_result = cast(&array_ref, &target_type).unwrap();
13382
13383        // Verify the result is an empty RunArray
13384        let result_run_array = cast_result
13385            .as_any()
13386            .downcast_ref::<RunArray<Int32Type>>()
13387            .unwrap();
13388
13389        // Check that both run_ends and values are empty
13390        assert_eq!(result_run_array.run_ends().len(), 0);
13391        assert_eq!(result_run_array.values().len(), 0);
13392    }
13393
13394    #[test]
13395    fn test_run_end_encoded_with_nulls() {
13396        // Create a RunEndEncoded array with nulls: [1, 1, null, 2, 2]
13397        let run_ends = Int32Array::from(vec![2, 3, 5]);
13398        let values = Int32Array::from(vec![Some(1), None, Some(2)]);
13399        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13400        let array_ref = Arc::new(run_array) as ArrayRef;
13401
13402        // Cast to String
13403        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
13404
13405        // Verify the result preserves nulls
13406        let result_run_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
13407        assert_eq!(result_run_array.value(0), "1");
13408        assert!(result_run_array.is_null(2));
13409        assert_eq!(result_run_array.value(4), "2");
13410    }
13411
13412    #[test]
13413    fn test_different_index_types() {
13414        // Test with Int16 index type
13415        let source_array = Int32Array::from(vec![1, 1, 2, 3, 3]);
13416        let array_ref = Arc::new(source_array) as ArrayRef;
13417
13418        let target_type = DataType::RunEndEncoded(
13419            Arc::new(Field::new("run_ends", DataType::Int16, false)),
13420            Arc::new(Field::new("values", DataType::Int32, true)),
13421        );
13422        let cast_result = cast(&array_ref, &target_type).unwrap();
13423        assert_eq!(cast_result.data_type(), &target_type);
13424
13425        // Verify the cast worked correctly: values are [1, 2, 3]
13426        // and run-ends are [2, 3, 5]
13427        let run_array = cast_result
13428            .as_any()
13429            .downcast_ref::<RunArray<Int16Type>>()
13430            .unwrap();
13431        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
13432        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
13433        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
13434        assert_eq!(run_array.run_ends().values(), &[2i16, 3i16, 5i16]);
13435
13436        // Test again with Int64 index type
13437        let target_type = DataType::RunEndEncoded(
13438            Arc::new(Field::new("run_ends", DataType::Int64, false)),
13439            Arc::new(Field::new("values", DataType::Int32, true)),
13440        );
13441        let cast_result = cast(&array_ref, &target_type).unwrap();
13442        assert_eq!(cast_result.data_type(), &target_type);
13443
13444        // Verify the cast worked correctly: values are [1, 2, 3]
13445        // and run-ends are [2, 3, 5]
13446        let run_array = cast_result
13447            .as_any()
13448            .downcast_ref::<RunArray<Int64Type>>()
13449            .unwrap();
13450        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
13451        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
13452        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
13453        assert_eq!(run_array.run_ends().values(), &[2i64, 3i64, 5i64]);
13454    }
13455
13456    #[test]
13457    fn test_unsupported_cast_to_run_end_encoded() {
13458        // Create a Struct array - complex nested type that might not be supported
13459        let field = Field::new("item", DataType::Int32, false);
13460        let struct_array = StructArray::from(vec![(
13461            Arc::new(field),
13462            Arc::new(Int32Array::from(vec![1, 2, 3])) as ArrayRef,
13463        )]);
13464        let array_ref = Arc::new(struct_array) as ArrayRef;
13465
13466        // This should fail because:
13467        // 1. The target type is not RunEndEncoded
13468        // 2. The target type is not supported for casting from StructArray
13469        let cast_result = cast(&array_ref, &DataType::FixedSizeBinary(10));
13470
13471        // Expect this to fail
13472        assert!(cast_result.is_err());
13473    }
13474
13475    /// Test casting RunEndEncoded<Int64, String> to RunEndEncoded<Int16, String> should fail
13476    #[test]
13477    fn test_cast_run_end_encoded_int64_to_int16_should_fail() {
13478        // Construct a valid REE array with Int64 run-ends
13479        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
13480        let values = StringArray::from(vec!["a", "b", "c"]);
13481
13482        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
13483        let array_ref = Arc::new(ree_array) as ArrayRef;
13484
13485        // Attempt to cast to RunEndEncoded<Int16, Utf8>
13486        let target_type = DataType::RunEndEncoded(
13487            Arc::new(Field::new("run_ends", DataType::Int16, false)),
13488            Arc::new(Field::new("values", DataType::Utf8, true)),
13489        );
13490        let cast_options = CastOptions {
13491            safe: false, // This should make it fail instead of returning nulls
13492            format_options: FormatOptions::default(),
13493        };
13494
13495        // This should fail due to run-end overflow
13496        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
13497            cast_with_options(&array_ref, &target_type, &cast_options);
13498
13499        let e = result.expect_err("Cast should have failed but succeeded");
13500        assert!(
13501            e.to_string()
13502                .contains("Cast error: Can't cast value 100000 to type Int16")
13503        );
13504    }
13505
13506    #[test]
13507    fn test_cast_run_end_encoded_int64_to_int16_with_safe_should_fail_with_null_invalid_error() {
13508        // Construct a valid REE array with Int64 run-ends
13509        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
13510        let values = StringArray::from(vec!["a", "b", "c"]);
13511
13512        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
13513        let array_ref = Arc::new(ree_array) as ArrayRef;
13514
13515        // Attempt to cast to RunEndEncoded<Int16, Utf8>
13516        let target_type = DataType::RunEndEncoded(
13517            Arc::new(Field::new("run_ends", DataType::Int16, false)),
13518            Arc::new(Field::new("values", DataType::Utf8, true)),
13519        );
13520        let cast_options = CastOptions {
13521            safe: true,
13522            format_options: FormatOptions::default(),
13523        };
13524
13525        // This fails even though safe is true because the run_ends array has null values
13526        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
13527            cast_with_options(&array_ref, &target_type, &cast_options);
13528        let e = result.expect_err("Cast should have failed but succeeded");
13529        assert!(
13530            e.to_string()
13531                .contains("Invalid argument error: Found null values in run_ends array. The run_ends array should not have null values.")
13532        );
13533    }
13534
13535    /// Test casting RunEndEncoded<Int16, String> to RunEndEncoded<Int64, String> should succeed
13536    #[test]
13537    fn test_cast_run_end_encoded_int16_to_int64_should_succeed() {
13538        // Construct a valid REE array with Int16 run-ends
13539        let run_ends = Int16Array::from(vec![2, 5, 8]); // values that fit in Int16
13540        let values = StringArray::from(vec!["a", "b", "c"]);
13541
13542        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
13543        let array_ref = Arc::new(ree_array) as ArrayRef;
13544
13545        // Attempt to cast to RunEndEncoded<Int64, Utf8> (upcast should succeed)
13546        let target_type = DataType::RunEndEncoded(
13547            Arc::new(Field::new("run_ends", DataType::Int64, false)),
13548            Arc::new(Field::new("values", DataType::Utf8, true)),
13549        );
13550        let cast_options = CastOptions {
13551            safe: false,
13552            format_options: FormatOptions::default(),
13553        };
13554
13555        // This should succeed due to valid upcast
13556        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
13557            cast_with_options(&array_ref, &target_type, &cast_options);
13558
13559        let array_ref = result.expect("Cast should have succeeded but failed");
13560        // Downcast to RunArray<Int64Type>
13561        let run_array = array_ref
13562            .as_any()
13563            .downcast_ref::<RunArray<Int64Type>>()
13564            .unwrap();
13565
13566        // Verify the cast worked correctly
13567        // Assert the values were cast correctly
13568        assert_eq!(run_array.run_ends().values(), &[2i64, 5i64, 8i64]);
13569        assert_eq!(run_array.values().as_string::<i32>().value(0), "a");
13570        assert_eq!(run_array.values().as_string::<i32>().value(1), "b");
13571        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
13572    }
13573
13574    #[test]
13575    fn test_cast_run_end_encoded_dictionary_to_run_end_encoded() {
13576        // Construct a valid dictionary encoded array
13577        let values = StringArray::from_iter([Some("a"), Some("b"), Some("c")]);
13578        let keys = UInt64Array::from_iter(vec![1, 1, 1, 0, 0, 0, 2, 2, 2]);
13579        let array_ref = Arc::new(DictionaryArray::new(keys, Arc::new(values))) as ArrayRef;
13580
13581        // Attempt to cast to RunEndEncoded<Int64, Utf8>
13582        let target_type = DataType::RunEndEncoded(
13583            Arc::new(Field::new("run_ends", DataType::Int64, false)),
13584            Arc::new(Field::new("values", DataType::Utf8, true)),
13585        );
13586        let cast_options = CastOptions {
13587            safe: false,
13588            format_options: FormatOptions::default(),
13589        };
13590
13591        // This should succeed
13592        let result = cast_with_options(&array_ref, &target_type, &cast_options)
13593            .expect("Cast should have succeeded but failed");
13594
13595        // Verify the cast worked correctly
13596        // Assert the values were cast correctly
13597        let run_array = result
13598            .as_any()
13599            .downcast_ref::<RunArray<Int64Type>>()
13600            .unwrap();
13601        assert_eq!(run_array.values().as_string::<i32>().value(0), "b");
13602        assert_eq!(run_array.values().as_string::<i32>().value(1), "a");
13603        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
13604
13605        // Verify the run-ends were cast correctly (run ends at 3, 6, 9)
13606        assert_eq!(run_array.run_ends().values(), &[3i64, 6i64, 9i64]);
13607    }
13608
13609    fn int32_list_values() -> Vec<Option<Vec<Option<i32>>>> {
13610        vec![
13611            Some(vec![Some(1), Some(2), Some(3)]),
13612            Some(vec![Some(4), Some(5), Some(6)]),
13613            None,
13614            Some(vec![Some(7), Some(8), Some(9)]),
13615            Some(vec![None, Some(10)]),
13616        ]
13617    }
13618
13619    #[test]
13620    fn test_cast_list_view_to_list() {
13621        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13622        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13623        assert!(can_cast_types(list_view.data_type(), &target_type));
13624        let cast_result = cast(&list_view, &target_type).unwrap();
13625        let got_list = cast_result.as_list::<i32>();
13626        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13627        assert_eq!(got_list, &expected_list);
13628    }
13629
13630    #[test]
13631    fn test_cast_list_view_to_large_list() {
13632        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13633        let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
13634        assert!(can_cast_types(list_view.data_type(), &target_type));
13635        let cast_result = cast(&list_view, &target_type).unwrap();
13636        let got_list = cast_result.as_list::<i64>();
13637        let expected_list =
13638            LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13639        assert_eq!(got_list, &expected_list);
13640    }
13641
13642    #[test]
13643    fn test_cast_list_to_list_view() {
13644        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13645        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
13646        assert!(can_cast_types(list.data_type(), &target_type));
13647        let cast_result = cast(&list, &target_type).unwrap();
13648
13649        let got_list_view = cast_result.as_list_view::<i32>();
13650        let expected_list_view =
13651            ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13652        assert_eq!(got_list_view, &expected_list_view);
13653
13654        // inner types get cast
13655        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13656            Some(vec![Some(1), Some(2)]),
13657            None,
13658            Some(vec![None, Some(3)]),
13659        ]);
13660        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Float32, true)));
13661        assert!(can_cast_types(list.data_type(), &target_type));
13662        let cast_result = cast(&list, &target_type).unwrap();
13663
13664        let got_list_view = cast_result.as_list_view::<i32>();
13665        let expected_list_view = ListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13666            Some(vec![Some(1.0), Some(2.0)]),
13667            None,
13668            Some(vec![None, Some(3.0)]),
13669        ]);
13670        assert_eq!(got_list_view, &expected_list_view);
13671    }
13672
13673    #[test]
13674    fn test_cast_list_to_large_list_view() {
13675        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13676            Some(vec![Some(1), Some(2)]),
13677            None,
13678            Some(vec![None, Some(3)]),
13679        ]);
13680        let target_type =
13681            DataType::LargeListView(Arc::new(Field::new("item", DataType::Float32, true)));
13682        assert!(can_cast_types(list.data_type(), &target_type));
13683        let cast_result = cast(&list, &target_type).unwrap();
13684
13685        let got_list_view = cast_result.as_list_view::<i64>();
13686        let expected_list_view =
13687            LargeListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13688                Some(vec![Some(1.0), Some(2.0)]),
13689                None,
13690                Some(vec![None, Some(3.0)]),
13691            ]);
13692        assert_eq!(got_list_view, &expected_list_view);
13693    }
13694
13695    #[test]
13696    fn test_cast_large_list_view_to_large_list() {
13697        let list_view =
13698            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13699        let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
13700        assert!(can_cast_types(list_view.data_type(), &target_type));
13701        let cast_result = cast(&list_view, &target_type).unwrap();
13702        let got_list = cast_result.as_list::<i64>();
13703
13704        let expected_list =
13705            LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13706        assert_eq!(got_list, &expected_list);
13707    }
13708
13709    #[test]
13710    fn test_cast_large_list_view_to_list() {
13711        let list_view =
13712            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13713        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13714        assert!(can_cast_types(list_view.data_type(), &target_type));
13715        let cast_result = cast(&list_view, &target_type).unwrap();
13716        let got_list = cast_result.as_list::<i32>();
13717
13718        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13719        assert_eq!(got_list, &expected_list);
13720    }
13721
13722    #[test]
13723    fn test_cast_large_list_to_large_list_view() {
13724        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13725        let target_type =
13726            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
13727        assert!(can_cast_types(list.data_type(), &target_type));
13728        let cast_result = cast(&list, &target_type).unwrap();
13729
13730        let got_list_view = cast_result.as_list_view::<i64>();
13731        let expected_list_view =
13732            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13733        assert_eq!(got_list_view, &expected_list_view);
13734
13735        // inner types get cast
13736        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13737            Some(vec![Some(1), Some(2)]),
13738            None,
13739            Some(vec![None, Some(3)]),
13740        ]);
13741        let target_type =
13742            DataType::LargeListView(Arc::new(Field::new("item", DataType::Float32, true)));
13743        assert!(can_cast_types(list.data_type(), &target_type));
13744        let cast_result = cast(&list, &target_type).unwrap();
13745
13746        let got_list_view = cast_result.as_list_view::<i64>();
13747        let expected_list_view =
13748            LargeListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13749                Some(vec![Some(1.0), Some(2.0)]),
13750                None,
13751                Some(vec![None, Some(3.0)]),
13752            ]);
13753        assert_eq!(got_list_view, &expected_list_view);
13754    }
13755
13756    #[test]
13757    fn test_cast_large_list_to_list_view() {
13758        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13759            Some(vec![Some(1), Some(2)]),
13760            None,
13761            Some(vec![None, Some(3)]),
13762        ]);
13763        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Float32, true)));
13764        assert!(can_cast_types(list.data_type(), &target_type));
13765        let cast_result = cast(&list, &target_type).unwrap();
13766
13767        let got_list_view = cast_result.as_list_view::<i32>();
13768        let expected_list_view = ListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13769            Some(vec![Some(1.0), Some(2.0)]),
13770            None,
13771            Some(vec![None, Some(3.0)]),
13772        ]);
13773        assert_eq!(got_list_view, &expected_list_view);
13774    }
13775
13776    #[test]
13777    fn test_cast_list_view_to_list_out_of_order() {
13778        let list_view = ListViewArray::new(
13779            Arc::new(Field::new("item", DataType::Int32, true)),
13780            ScalarBuffer::from(vec![0, 6, 3]),
13781            ScalarBuffer::from(vec![3, 3, 3]),
13782            Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
13783            None,
13784        );
13785        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13786        assert!(can_cast_types(list_view.data_type(), &target_type));
13787        let cast_result = cast(&list_view, &target_type).unwrap();
13788        let got_list = cast_result.as_list::<i32>();
13789        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13790            Some(vec![Some(1), Some(2), Some(3)]),
13791            Some(vec![Some(7), Some(8), Some(9)]),
13792            Some(vec![Some(4), Some(5), Some(6)]),
13793        ]);
13794        assert_eq!(got_list, &expected_list);
13795    }
13796
13797    #[test]
13798    fn test_cast_list_view_to_list_overlapping() {
13799        let list_view = ListViewArray::new(
13800            Arc::new(Field::new("item", DataType::Int32, true)),
13801            ScalarBuffer::from(vec![0, 0]),
13802            ScalarBuffer::from(vec![1, 2]),
13803            Arc::new(Int32Array::from(vec![1, 2])),
13804            None,
13805        );
13806        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13807        assert!(can_cast_types(list_view.data_type(), &target_type));
13808        let cast_result = cast(&list_view, &target_type).unwrap();
13809        let got_list = cast_result.as_list::<i32>();
13810        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13811            Some(vec![Some(1)]),
13812            Some(vec![Some(1), Some(2)]),
13813        ]);
13814        assert_eq!(got_list, &expected_list);
13815    }
13816
13817    #[test]
13818    fn test_cast_list_view_to_list_empty() {
13819        let values: Vec<Option<Vec<Option<i32>>>> = vec![];
13820        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
13821        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13822        assert!(can_cast_types(list_view.data_type(), &target_type));
13823        let cast_result = cast(&list_view, &target_type).unwrap();
13824        let got_list = cast_result.as_list::<i32>();
13825        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
13826        assert_eq!(got_list, &expected_list);
13827    }
13828
13829    #[test]
13830    fn test_cast_list_view_to_list_different_inner_type() {
13831        let values = int32_list_values();
13832        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
13833        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int64, true)));
13834        assert!(can_cast_types(list_view.data_type(), &target_type));
13835        let cast_result = cast(&list_view, &target_type).unwrap();
13836        let got_list = cast_result.as_list::<i32>();
13837
13838        let expected_list =
13839            ListArray::from_iter_primitive::<Int64Type, _, _>(values.into_iter().map(|list| {
13840                list.map(|list| {
13841                    list.into_iter()
13842                        .map(|v| v.map(|v| v as i64))
13843                        .collect::<Vec<_>>()
13844                })
13845            }));
13846        assert_eq!(got_list, &expected_list);
13847    }
13848
13849    #[test]
13850    fn test_cast_list_view_to_list_out_of_order_with_nulls() {
13851        let list_view = ListViewArray::new(
13852            Arc::new(Field::new("item", DataType::Int32, true)),
13853            ScalarBuffer::from(vec![0, 6, 3]),
13854            ScalarBuffer::from(vec![3, 3, 3]),
13855            Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
13856            Some(NullBuffer::from(vec![false, true, false])),
13857        );
13858        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13859        assert!(can_cast_types(list_view.data_type(), &target_type));
13860        let cast_result = cast(&list_view, &target_type).unwrap();
13861        let got_list = cast_result.as_list::<i32>();
13862        let expected_list = ListArray::new(
13863            Arc::new(Field::new("item", DataType::Int32, true)),
13864            OffsetBuffer::from_lengths([3, 3, 3]),
13865            Arc::new(Int32Array::from(vec![1, 2, 3, 7, 8, 9, 4, 5, 6])),
13866            Some(NullBuffer::from(vec![false, true, false])),
13867        );
13868        assert_eq!(got_list, &expected_list);
13869    }
13870
13871    #[test]
13872    fn test_cast_list_view_to_large_list_view() {
13873        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13874        let target_type =
13875            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
13876        assert!(can_cast_types(list_view.data_type(), &target_type));
13877        let cast_result = cast(&list_view, &target_type).unwrap();
13878        let got = cast_result.as_list_view::<i64>();
13879
13880        let expected =
13881            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13882        assert_eq!(got, &expected);
13883    }
13884
13885    #[test]
13886    fn test_cast_large_list_view_to_list_view() {
13887        let list_view =
13888            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13889        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
13890        assert!(can_cast_types(list_view.data_type(), &target_type));
13891        let cast_result = cast(&list_view, &target_type).unwrap();
13892        let got = cast_result.as_list_view::<i32>();
13893
13894        let expected = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13895        assert_eq!(got, &expected);
13896    }
13897
13898    #[test]
13899    fn test_cast_time32_second_to_int64() {
13900        let array = Time32SecondArray::from(vec![1000, 2000, 3000]);
13901        let array = Arc::new(array) as Arc<dyn Array>;
13902        let to_type = DataType::Int64;
13903        let cast_options = CastOptions::default();
13904
13905        assert!(can_cast_types(array.data_type(), &to_type));
13906
13907        let result = cast_with_options(&array, &to_type, &cast_options);
13908        assert!(
13909            result.is_ok(),
13910            "Failed to cast Time32(Second) to Int64: {:?}",
13911            result.err()
13912        );
13913
13914        let cast_array = result.unwrap();
13915        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13916
13917        assert_eq!(cast_array.value(0), 1000);
13918        assert_eq!(cast_array.value(1), 2000);
13919        assert_eq!(cast_array.value(2), 3000);
13920    }
13921
13922    #[test]
13923    fn test_cast_time32_millisecond_to_int64() {
13924        let array = Time32MillisecondArray::from(vec![1000, 2000, 3000]);
13925        let array = Arc::new(array) as Arc<dyn Array>;
13926        let to_type = DataType::Int64;
13927        let cast_options = CastOptions::default();
13928
13929        assert!(can_cast_types(array.data_type(), &to_type));
13930
13931        let result = cast_with_options(&array, &to_type, &cast_options);
13932        assert!(
13933            result.is_ok(),
13934            "Failed to cast Time32(Millisecond) to Int64: {:?}",
13935            result.err()
13936        );
13937
13938        let cast_array = result.unwrap();
13939        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13940
13941        assert_eq!(cast_array.value(0), 1000);
13942        assert_eq!(cast_array.value(1), 2000);
13943        assert_eq!(cast_array.value(2), 3000);
13944    }
13945
13946    #[test]
13947    fn test_cast_time32_millisecond_to_time64_nanosecond() {
13948        let array =
13949            Time32MillisecondArray::from(vec![Some(1_000), Some(2_000), None, Some(43_200_000)]);
13950        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
13951        let c = b.as_primitive::<Time64NanosecondType>();
13952        assert_eq!(c.value(0), 1_000_000_000);
13953        assert_eq!(c.value(1), 2_000_000_000);
13954        assert!(c.is_null(2));
13955        assert_eq!(c.value(3), 43_200_000_000_000);
13956    }
13957
13958    #[test]
13959    fn test_cast_time32_millisecond_to_time64_microsecond() {
13960        let array =
13961            Time32MillisecondArray::from(vec![Some(1_000), Some(2_000), None, Some(43_200_000)]);
13962        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
13963        let c = b.as_primitive::<Time64MicrosecondType>();
13964        assert_eq!(c.value(0), 1_000_000);
13965        assert_eq!(c.value(1), 2_000_000);
13966        assert!(c.is_null(2));
13967        assert_eq!(c.value(3), 43_200_000_000);
13968    }
13969
13970    #[test]
13971    fn test_cast_time32_second_to_time64_nanosecond() {
13972        let array = Time32SecondArray::from(vec![Some(1), Some(60), None, Some(43_200)]);
13973        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
13974        let c = b.as_primitive::<Time64NanosecondType>();
13975        assert_eq!(c.value(0), 1_000_000_000);
13976        assert_eq!(c.value(1), 60_000_000_000);
13977        assert!(c.is_null(2));
13978        assert_eq!(c.value(3), 43_200_000_000_000);
13979    }
13980
13981    #[test]
13982    fn test_cast_time32_second_to_time64_microsecond() {
13983        let array = Time32SecondArray::from(vec![Some(1), Some(60), None, Some(43_200)]);
13984        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
13985        let c = b.as_primitive::<Time64MicrosecondType>();
13986        assert_eq!(c.value(0), 1_000_000);
13987        assert_eq!(c.value(1), 60_000_000);
13988        assert!(c.is_null(2));
13989        assert_eq!(c.value(3), 43_200_000_000);
13990    }
13991
13992    #[test]
13993    fn test_cast_time32_second_to_time32_millisecond_overflow() {
13994        let array = Time32SecondArray::from(vec![i32::MAX]);
13995
13996        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
13997        let c = b.as_primitive::<Time32MillisecondType>();
13998        assert!(c.is_null(0));
13999
14000        let options = CastOptions {
14001            safe: false,
14002            ..Default::default()
14003        };
14004        let err = cast_with_options(&array, &DataType::Time32(TimeUnit::Millisecond), &options)
14005            .unwrap_err();
14006        assert!(err.to_string().contains("Overflow"), "{err}");
14007    }
14008
14009    #[test]
14010    fn test_cast_string_to_time32_second_to_int64() {
14011        // Mimic: select arrow_cast('03:12:44'::time, 'Time32(Second)')::bigint;
14012        // raised in https://github.com/apache/datafusion/issues/19036
14013        let array = StringArray::from(vec!["03:12:44"]);
14014        let array = Arc::new(array) as Arc<dyn Array>;
14015        let cast_options = CastOptions::default();
14016
14017        // 1. Cast String to Time32(Second)
14018        let time32_type = DataType::Time32(TimeUnit::Second);
14019        let time32_array = cast_with_options(&array, &time32_type, &cast_options).unwrap();
14020
14021        // 2. Cast Time32(Second) to Int64
14022        let int64_type = DataType::Int64;
14023        assert!(can_cast_types(time32_array.data_type(), &int64_type));
14024
14025        let result = cast_with_options(&time32_array, &int64_type, &cast_options);
14026
14027        assert!(
14028            result.is_ok(),
14029            "Failed to cast Time32(Second) to Int64: {:?}",
14030            result.err()
14031        );
14032
14033        let cast_array = result.unwrap();
14034        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
14035
14036        // 03:12:44 = 3*3600 + 12*60 + 44 = 10800 + 720 + 44 = 11564
14037        assert_eq!(cast_array.value(0), 11564);
14038    }
14039    #[test]
14040    fn test_string_dicts_to_binary_view() {
14041        let expected = BinaryViewArray::from_iter(vec![
14042            VIEW_TEST_DATA[1],
14043            VIEW_TEST_DATA[0],
14044            None,
14045            VIEW_TEST_DATA[3],
14046            None,
14047            VIEW_TEST_DATA[1],
14048            VIEW_TEST_DATA[4],
14049        ]);
14050
14051        let values_arrays: [ArrayRef; _] = [
14052            Arc::new(StringArray::from_iter(VIEW_TEST_DATA)),
14053            Arc::new(StringViewArray::from_iter(VIEW_TEST_DATA)),
14054            Arc::new(LargeStringArray::from_iter(VIEW_TEST_DATA)),
14055        ];
14056        for values in values_arrays {
14057            let keys =
14058                Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
14059            let string_dict_array = DictionaryArray::<Int8Type>::try_new(keys, values).unwrap();
14060
14061            let casted = cast(&string_dict_array, &DataType::BinaryView).unwrap();
14062            assert_eq!(casted.as_ref(), &expected);
14063        }
14064    }
14065
14066    #[test]
14067    fn test_binary_dicts_to_string_view() {
14068        let expected = StringViewArray::from_iter(vec![
14069            VIEW_TEST_DATA[1],
14070            VIEW_TEST_DATA[0],
14071            None,
14072            VIEW_TEST_DATA[3],
14073            None,
14074            VIEW_TEST_DATA[1],
14075            VIEW_TEST_DATA[4],
14076        ]);
14077
14078        let values_arrays: [ArrayRef; _] = [
14079            Arc::new(BinaryArray::from_iter(VIEW_TEST_DATA)),
14080            Arc::new(BinaryViewArray::from_iter(VIEW_TEST_DATA)),
14081            Arc::new(LargeBinaryArray::from_iter(VIEW_TEST_DATA)),
14082        ];
14083        for values in values_arrays {
14084            let keys =
14085                Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
14086            let string_dict_array = DictionaryArray::<Int8Type>::try_new(keys, values).unwrap();
14087
14088            let casted = cast(&string_dict_array, &DataType::Utf8View).unwrap();
14089            assert_eq!(casted.as_ref(), &expected);
14090        }
14091    }
14092
14093    #[test]
14094    fn test_cast_between_sliced_run_end_encoded() {
14095        let run_ends = Int16Array::from(vec![2, 5, 8]);
14096        let values = StringArray::from(vec!["a", "b", "c"]);
14097
14098        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
14099        let ree_array = ree_array.slice(1, 2);
14100        let array_ref = Arc::new(ree_array) as ArrayRef;
14101
14102        let target_type = DataType::RunEndEncoded(
14103            Arc::new(Field::new("run_ends", DataType::Int64, false)),
14104            Arc::new(Field::new("values", DataType::Utf8, true)),
14105        );
14106        let cast_options = CastOptions {
14107            safe: false,
14108            format_options: FormatOptions::default(),
14109        };
14110
14111        let result = cast_with_options(&array_ref, &target_type, &cast_options).unwrap();
14112        let run_array = result.as_run::<Int64Type>();
14113        let run_array = run_array.downcast::<StringArray>().unwrap();
14114
14115        let expected = vec!["a", "b"];
14116        let actual = run_array.into_iter().flatten().collect::<Vec<_>>();
14117
14118        assert_eq!(expected, actual);
14119    }
14120}