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)
273            | Time32(Millisecond)
274            | Time64(Microsecond)
275            | Time64(Nanosecond)
276            | Timestamp(Second, _)
277            | Timestamp(Millisecond, _)
278            | Timestamp(Microsecond, _)
279            | Timestamp(Nanosecond, _)
280            | Interval(_)
281            | BinaryView,
282        ) => true,
283        (Utf8 | LargeUtf8, Utf8View) => true,
284        (BinaryView, Binary | LargeBinary | Utf8 | LargeUtf8 | Utf8View) => true,
285        (Utf8View | Utf8 | LargeUtf8, _) => to_type.is_numeric(),
286        (_, Utf8 | Utf8View | LargeUtf8) => from_type.is_primitive(),
287
288        (_, Binary | LargeBinary) => from_type.is_integer(),
289
290        // start numeric casts
291        (
292            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
293            | Float64,
294            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
295            | Float64,
296        ) => true,
297        // end numeric casts
298
299        // temporal casts
300        (Int32, Date32 | Date64 | Time32(_)) => true,
301        (Date32, Int32 | Int64) => true,
302        (Time32(_), Int32 | Int64) => true,
303        (Int64, Date64 | Date32 | Time64(_)) => true,
304        (Date64, Int64 | Int32) => true,
305        (Time64(_), Int64) => true,
306        (Date32 | Date64, Date32 | Date64) => true,
307        // time casts
308        (Time32(_), Time32(_)) => true,
309        (Time32(_), Time64(_)) => true,
310        (Time64(_), Time64(_)) => true,
311        (Time64(_), Time32(to_unit)) => {
312            matches!(to_unit, Second | Millisecond)
313        }
314        (Timestamp(_, _), _) if to_type.is_numeric() => true,
315        (_, Timestamp(_, _)) if from_type.is_numeric() => true,
316        (Date64, Timestamp(_, _)) => true,
317        (Date32, Timestamp(_, _)) => true,
318        (
319            Timestamp(_, _),
320            Timestamp(_, _)
321            | Date32
322            | Date64
323            | Time32(Second)
324            | Time32(Millisecond)
325            | Time64(Microsecond)
326            | Time64(Nanosecond),
327        ) => true,
328        (_, Duration(_)) if from_type.is_numeric() => true,
329        (Duration(_), _) if to_type.is_numeric() => true,
330        (Duration(_), Duration(_)) => true,
331        (Interval(from_type), Int64) => {
332            match from_type {
333                YearMonth => true,
334                DayTime => true,
335                MonthDayNano => false, // Native type is i128
336            }
337        }
338        (Int32, Interval(to_type)) => match to_type {
339            YearMonth => true,
340            DayTime => false,
341            MonthDayNano => false,
342        },
343        (Duration(_), Interval(MonthDayNano)) => true,
344        (Interval(MonthDayNano), Duration(_)) => true,
345        (Interval(YearMonth), Interval(MonthDayNano)) => true,
346        (Interval(DayTime), Interval(MonthDayNano)) => true,
347        (_, _) => false,
348    }
349}
350
351/// Cast `array` to the provided data type and return a new Array with type `to_type`, if possible.
352///
353/// See [`cast_with_options`] for more information
354pub fn cast(array: &dyn Array, to_type: &DataType) -> Result<ArrayRef, ArrowError> {
355    cast_with_options(array, to_type, &CastOptions::default())
356}
357
358fn cast_integer_to_decimal<
359    T: ArrowPrimitiveType,
360    D: DecimalType + ArrowPrimitiveType<Native = M>,
361    M,
362>(
363    array: &PrimitiveArray<T>,
364    precision: u8,
365    scale: i8,
366    base: M,
367    cast_options: &CastOptions,
368) -> Result<ArrayRef, ArrowError>
369where
370    <T as ArrowPrimitiveType>::Native: AsPrimitive<M>,
371    M: ArrowNativeTypeOp,
372{
373    let scale_factor = base.pow_checked(scale.unsigned_abs() as u32).map_err(|_| {
374        ArrowError::CastError(format!(
375            "Cannot cast to {:?}({}, {}). The scale causes overflow.",
376            D::PREFIX,
377            precision,
378            scale,
379        ))
380    })?;
381
382    let array = if scale < 0 {
383        match cast_options.safe {
384            true => array.unary_opt::<_, D>(|v| {
385                v.as_()
386                    .div_checked(scale_factor)
387                    .ok()
388                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
389            }),
390            false => array.try_unary::<_, D, _>(|v| {
391                v.as_()
392                    .div_checked(scale_factor)
393                    .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|_| v))
394            })?,
395        }
396    } else {
397        match cast_options.safe {
398            true => array.unary_opt::<_, D>(|v| {
399                v.as_()
400                    .mul_checked(scale_factor)
401                    .ok()
402                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
403            }),
404            false => array.try_unary::<_, D, _>(|v| {
405                v.as_()
406                    .mul_checked(scale_factor)
407                    .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|_| v))
408            })?,
409        }
410    };
411
412    Ok(Arc::new(array.with_precision_and_scale(precision, scale)?))
413}
414
415/// Cast the array from interval year month to month day nano
416fn cast_interval_year_month_to_interval_month_day_nano(
417    array: &dyn Array,
418    _cast_options: &CastOptions,
419) -> Result<ArrayRef, ArrowError> {
420    let array = array.as_primitive::<IntervalYearMonthType>();
421
422    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
423        let months = IntervalYearMonthType::to_months(v);
424        IntervalMonthDayNanoType::make_value(months, 0, 0)
425    })))
426}
427
428/// Cast the array from interval day time to month day nano
429fn cast_interval_day_time_to_interval_month_day_nano(
430    array: &dyn Array,
431    _cast_options: &CastOptions,
432) -> Result<ArrayRef, ArrowError> {
433    let array = array.as_primitive::<IntervalDayTimeType>();
434    let mul = 1_000_000;
435
436    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
437        let (days, ms) = IntervalDayTimeType::to_parts(v);
438        IntervalMonthDayNanoType::make_value(0, days, ms as i64 * mul)
439    })))
440}
441
442/// Cast the array from interval to duration
443fn cast_month_day_nano_to_duration<D: ArrowTemporalType<Native = i64>>(
444    array: &dyn Array,
445    cast_options: &CastOptions,
446) -> Result<ArrayRef, ArrowError> {
447    let array = array.as_primitive::<IntervalMonthDayNanoType>();
448    let scale = match D::DATA_TYPE {
449        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
450        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
451        DataType::Duration(TimeUnit::Microsecond) => 1_000,
452        DataType::Duration(TimeUnit::Nanosecond) => 1,
453        _ => unreachable!(),
454    };
455
456    if cast_options.safe {
457        let iter = array.iter().map(|v| {
458            v.and_then(|v| (v.days == 0 && v.months == 0).then_some(v.nanoseconds / scale))
459        });
460        Ok(Arc::new(unsafe {
461            PrimitiveArray::<D>::from_trusted_len_iter(iter)
462        }))
463    } else {
464        let vec = array
465            .iter()
466            .map(|v| {
467                v.map(|v| match v.days == 0 && v.months == 0 {
468                    true => Ok((v.nanoseconds) / scale),
469                    _ => Err(ArrowError::ComputeError(
470                        "Cannot convert interval containing non-zero months or days to duration"
471                            .to_string(),
472                    )),
473                })
474                .transpose()
475            })
476            .collect::<Result<Vec<_>, _>>()?;
477        Ok(Arc::new(unsafe {
478            PrimitiveArray::<D>::from_trusted_len_iter(vec.iter())
479        }))
480    }
481}
482
483/// Cast the array from duration and interval
484fn cast_duration_to_interval<D: ArrowTemporalType<Native = i64>>(
485    array: &dyn Array,
486    cast_options: &CastOptions,
487) -> Result<ArrayRef, ArrowError> {
488    let array = array
489        .as_any()
490        .downcast_ref::<PrimitiveArray<D>>()
491        .ok_or_else(|| {
492            ArrowError::ComputeError(
493                "Internal Error: Cannot cast duration to DurationArray of expected type"
494                    .to_string(),
495            )
496        })?;
497
498    let scale = match array.data_type() {
499        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
500        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
501        DataType::Duration(TimeUnit::Microsecond) => 1_000,
502        DataType::Duration(TimeUnit::Nanosecond) => 1,
503        _ => unreachable!(),
504    };
505
506    if cast_options.safe {
507        let iter = array.iter().map(|v| {
508            v.and_then(|v| {
509                v.checked_mul(scale)
510                    .map(|v| IntervalMonthDayNano::new(0, 0, v))
511            })
512        });
513        Ok(Arc::new(unsafe {
514            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(iter)
515        }))
516    } else {
517        let vec = array
518            .iter()
519            .map(|v| {
520                v.map(|v| {
521                    if let Ok(v) = v.mul_checked(scale) {
522                        Ok(IntervalMonthDayNano::new(0, 0, v))
523                    } else {
524                        Err(ArrowError::ComputeError(format!(
525                            "Cannot cast to {:?}. Overflowing on {:?}",
526                            IntervalMonthDayNanoType::DATA_TYPE,
527                            v
528                        )))
529                    }
530                })
531                .transpose()
532            })
533            .collect::<Result<Vec<_>, _>>()?;
534        Ok(Arc::new(unsafe {
535            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(vec.iter())
536        }))
537    }
538}
539
540/// Cast the primitive array using [`PrimitiveArray::reinterpret_cast`]
541fn cast_reinterpret_arrays<I: ArrowPrimitiveType, O: ArrowPrimitiveType<Native = I::Native>>(
542    array: &dyn Array,
543) -> Result<ArrayRef, ArrowError> {
544    Ok(Arc::new(array.as_primitive::<I>().reinterpret_cast::<O>()))
545}
546
547fn make_timestamp_array(
548    array: &PrimitiveArray<Int64Type>,
549    unit: TimeUnit,
550    tz: Option<Arc<str>>,
551) -> ArrayRef {
552    match unit {
553        TimeUnit::Second => Arc::new(
554            array
555                .reinterpret_cast::<TimestampSecondType>()
556                .with_timezone_opt(tz),
557        ),
558        TimeUnit::Millisecond => Arc::new(
559            array
560                .reinterpret_cast::<TimestampMillisecondType>()
561                .with_timezone_opt(tz),
562        ),
563        TimeUnit::Microsecond => Arc::new(
564            array
565                .reinterpret_cast::<TimestampMicrosecondType>()
566                .with_timezone_opt(tz),
567        ),
568        TimeUnit::Nanosecond => Arc::new(
569            array
570                .reinterpret_cast::<TimestampNanosecondType>()
571                .with_timezone_opt(tz),
572        ),
573    }
574}
575
576fn make_duration_array(array: &PrimitiveArray<Int64Type>, unit: TimeUnit) -> ArrayRef {
577    match unit {
578        TimeUnit::Second => Arc::new(array.reinterpret_cast::<DurationSecondType>()),
579        TimeUnit::Millisecond => Arc::new(array.reinterpret_cast::<DurationMillisecondType>()),
580        TimeUnit::Microsecond => Arc::new(array.reinterpret_cast::<DurationMicrosecondType>()),
581        TimeUnit::Nanosecond => Arc::new(array.reinterpret_cast::<DurationNanosecondType>()),
582    }
583}
584
585fn as_time_res_with_timezone<T: ArrowPrimitiveType>(
586    v: i64,
587    tz: Option<Tz>,
588) -> Result<NaiveTime, ArrowError> {
589    let time = match tz {
590        Some(tz) => as_datetime_with_timezone::<T>(v, tz).map(|d| d.time()),
591        None => as_datetime::<T>(v).map(|d| d.time()),
592    };
593
594    time.ok_or_else(|| {
595        ArrowError::CastError(format!(
596            "Failed to create naive time with {} {}",
597            std::any::type_name::<T>(),
598            v
599        ))
600    })
601}
602
603fn timestamp_to_date32<T: ArrowTimestampType>(
604    array: &PrimitiveArray<T>,
605) -> Result<ArrayRef, ArrowError> {
606    let err = |x: i64| {
607        ArrowError::CastError(format!(
608            "Cannot convert {} {x} to datetime",
609            std::any::type_name::<T>()
610        ))
611    };
612
613    let array: Date32Array = match array.timezone() {
614        Some(tz) => {
615            let tz: Tz = tz.parse()?;
616            array.try_unary(|x| {
617                as_datetime_with_timezone::<T>(x, tz)
618                    .ok_or_else(|| err(x))
619                    .map(|d| Date32Type::from_naive_date(d.date_naive()))
620            })?
621        }
622        None => array.try_unary(|x| {
623            as_datetime::<T>(x)
624                .ok_or_else(|| err(x))
625                .map(|d| Date32Type::from_naive_date(d.date()))
626        })?,
627    };
628    Ok(Arc::new(array))
629}
630
631/// Try to cast `array` to `to_type` if possible.
632///
633/// Returns a new Array with type `to_type` if possible.
634///
635/// Accepts [`CastOptions`] to specify cast behavior. See also [`cast()`].
636///
637/// # Behavior
638/// * `Boolean` to `Utf8`: `true` => '1', `false` => `0`
639/// * `Utf8` to `Boolean`: `true`, `yes`, `on`, `1` => `true`, `false`, `no`, `off`, `0` => `false`,
640///   short variants are accepted, other strings return null or error
641/// * `Utf8` to Numeric: strings that can't be parsed to numbers return null, float strings
642///   in integer casts return null
643/// * Numeric to `Boolean`: 0 returns `false`, any other value returns `true`
644/// * `List` to `List`: the underlying data type is cast
645/// * `List` to `FixedSizeList`: the underlying data type is cast. If safe is true and a list element
646///   has the wrong length it will be replaced with NULL, otherwise an error will be returned
647/// * Primitive to `List`: a list array with 1 value per slot is created
648/// * `Date32` and `Date64`: precision lost when going to higher interval
649/// * `Time32 and `Time64`: precision lost when going to higher interval
650/// * `Timestamp` and `Date{32|64}`: precision lost when going to higher interval
651/// * Temporal to/from backing Primitive: zero-copy with data type change
652/// * `Float16/Float32/Float64` to `Decimal(precision, scale)` rounds to the `scale` decimals
653///   (i.e. casting `6.4999` to `Decimal(10, 1)` becomes `6.5`).
654/// * `Decimal` to `Float16/Float32/Float64` is lossy and values outside the representable
655///   range become `INFINITY` or `-INFINITY` without error.
656///
657/// Unsupported Casts (check with `can_cast_types` before calling):
658/// * To or from `StructArray`
659/// * `List` to `Primitive`
660/// * `Interval` and `Duration`
661///
662/// # Durations and Intervals
663///
664/// Casting integer types directly to interval types such as
665/// [`IntervalMonthDayNano`] is not supported because the meaning of the integer
666/// is ambiguous. For example, the integer  could represent either nanoseconds
667/// or months.
668///
669/// To cast an integer type to an interval type, first convert to a Duration
670/// type, and then cast that to the desired interval type.
671///
672/// For example, to convert an `Int64` representing nanoseconds to an
673/// `IntervalMonthDayNano` you would first convert the `Int64` to a
674/// `DurationNanoseconds`, and then cast that to `IntervalMonthDayNano`.
675///
676/// # Timestamps and Timezones
677///
678/// Timestamps are stored with an optional timezone in Arrow.
679///
680/// ## Casting timestamps to a timestamp without timezone / UTC
681/// ```
682/// # use arrow_array::Int64Array;
683/// # use arrow_array::types::TimestampSecondType;
684/// # use arrow_cast::{cast, display};
685/// # use arrow_array::cast::AsArray;
686/// # use arrow_schema::{DataType, TimeUnit};
687/// // can use "UTC" if chrono-tz feature is enabled, here use offset based timezone
688/// let data_type = DataType::Timestamp(TimeUnit::Second, None);
689/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
690/// let b = cast(&a, &data_type).unwrap();
691/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
692/// assert_eq!(2_000_000_000, b.value(1)); // values are the same as the type has no timezone
693/// // use display to show them (note has no trailing Z)
694/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
695/// ```
696///
697/// ## Casting timestamps to a timestamp with timezone
698///
699/// Similarly to the previous example, if you cast numeric values to a timestamp
700/// with timezone, the cast kernel will not change the underlying values
701/// but display and other functions will interpret them as being in the provided timezone.
702///
703/// ```
704/// # use arrow_array::Int64Array;
705/// # use arrow_array::types::TimestampSecondType;
706/// # use arrow_cast::{cast, display};
707/// # use arrow_array::cast::AsArray;
708/// # use arrow_schema::{DataType, TimeUnit};
709/// // can use "Americas/New_York" if chrono-tz feature is enabled, here use offset based timezone
710/// let data_type = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
711/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
712/// let b = cast(&a, &data_type).unwrap();
713/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
714/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
715/// // displayed in the target timezone (note the offset -05:00)
716/// assert_eq!("2033-05-17T22:33:20-05:00", display::array_value_to_string(&b, 1).unwrap());
717/// ```
718/// # Casting timestamps without timezone to timestamps with timezone
719///
720/// When casting from a timestamp without timezone to a timestamp with
721/// timezone, the cast kernel interprets the timestamp values as being in
722/// the destination timezone and then adjusts the underlying value to UTC as required
723///
724/// However, note that when casting from a timestamp with timezone BACK to a
725/// timestamp without timezone the cast kernel does not adjust the values.
726///
727/// Thus round trip casting a timestamp without timezone to a timestamp with
728/// timezone and back to a timestamp without timezone results in different
729/// values than the starting values.
730///
731/// ```
732/// # use arrow_array::Int64Array;
733/// # use arrow_array::types::{TimestampSecondType};
734/// # use arrow_cast::{cast, display};
735/// # use arrow_array::cast::AsArray;
736/// # use arrow_schema::{DataType, TimeUnit};
737/// let data_type  = DataType::Timestamp(TimeUnit::Second, None);
738/// let data_type_tz = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
739/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
740/// let b = cast(&a, &data_type).unwrap(); // cast to timestamp without timezone
741/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
742/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
743/// // displayed without a timezone (note lack of offset or Z)
744/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
745///
746/// // Convert timestamps without a timezone to timestamps with a timezone
747/// let c = cast(&b, &data_type_tz).unwrap();
748/// let c = c.as_primitive::<TimestampSecondType>(); // downcast to result type
749/// assert_eq!(2_000_018_000, c.value(1)); // value has been adjusted by offset
750/// // displayed with the target timezone offset (-05:00)
751/// assert_eq!("2033-05-18T03:33:20-05:00", display::array_value_to_string(&c, 1).unwrap());
752///
753/// // Convert from timestamp with timezone back to timestamp without timezone
754/// let d = cast(&c, &data_type).unwrap();
755/// let d = d.as_primitive::<TimestampSecondType>(); // downcast to result type
756/// assert_eq!(2_000_018_000, d.value(1)); // value has not been adjusted
757/// // NOTE: the timestamp is adjusted (08:33:20 instead of 03:33:20 as in previous example)
758/// assert_eq!("2033-05-18T08:33:20", display::array_value_to_string(&d, 1).unwrap());
759/// ```
760pub fn cast_with_options(
761    array: &dyn Array,
762    to_type: &DataType,
763    cast_options: &CastOptions,
764) -> Result<ArrayRef, ArrowError> {
765    use DataType::*;
766    let from_type = array.data_type();
767    // clone array if types are the same
768    if from_type == to_type {
769        return Ok(make_array(array.to_data()));
770    }
771    match (from_type, to_type) {
772        (Null, _) => Ok(new_null_array(to_type, array.len())),
773        (RunEndEncoded(index_type, _), _) => match index_type.data_type() {
774            Int16 => run_end_encoded_cast::<Int16Type>(array, to_type, cast_options),
775            Int32 => run_end_encoded_cast::<Int32Type>(array, to_type, cast_options),
776            Int64 => run_end_encoded_cast::<Int64Type>(array, to_type, cast_options),
777            _ => Err(ArrowError::CastError(format!(
778                "Casting from run end encoded type {from_type:?} to {to_type:?} not supported",
779            ))),
780        },
781        (_, RunEndEncoded(index_type, value_type)) => {
782            let array_ref = make_array(array.to_data());
783            match index_type.data_type() {
784                Int16 => cast_to_run_end_encoded::<Int16Type>(
785                    &array_ref,
786                    value_type.data_type(),
787                    cast_options,
788                ),
789                Int32 => cast_to_run_end_encoded::<Int32Type>(
790                    &array_ref,
791                    value_type.data_type(),
792                    cast_options,
793                ),
794                Int64 => cast_to_run_end_encoded::<Int64Type>(
795                    &array_ref,
796                    value_type.data_type(),
797                    cast_options,
798                ),
799                _ => Err(ArrowError::CastError(format!(
800                    "Casting from type {from_type:?} to run end encoded type {to_type:?} not supported",
801                ))),
802            }
803        }
804        (Union(_, _), _) => union_extract_by_type(
805            array.as_any().downcast_ref::<UnionArray>().unwrap(),
806            to_type,
807            cast_options,
808        ),
809        (_, Union(_, _)) => Err(ArrowError::CastError(format!(
810            "Casting from {from_type} to {to_type} not supported"
811        ))),
812        (Dictionary(index_type, _), _) => match **index_type {
813            Int8 => dictionary_cast::<Int8Type>(array, to_type, cast_options),
814            Int16 => dictionary_cast::<Int16Type>(array, to_type, cast_options),
815            Int32 => dictionary_cast::<Int32Type>(array, to_type, cast_options),
816            Int64 => dictionary_cast::<Int64Type>(array, to_type, cast_options),
817            UInt8 => dictionary_cast::<UInt8Type>(array, to_type, cast_options),
818            UInt16 => dictionary_cast::<UInt16Type>(array, to_type, cast_options),
819            UInt32 => dictionary_cast::<UInt32Type>(array, to_type, cast_options),
820            UInt64 => dictionary_cast::<UInt64Type>(array, to_type, cast_options),
821            _ => Err(ArrowError::CastError(format!(
822                "Casting from dictionary type {from_type} to {to_type} not supported",
823            ))),
824        },
825        (_, Dictionary(index_type, value_type)) => match **index_type {
826            Int8 => cast_to_dictionary::<Int8Type>(array, value_type, cast_options),
827            Int16 => cast_to_dictionary::<Int16Type>(array, value_type, cast_options),
828            Int32 => cast_to_dictionary::<Int32Type>(array, value_type, cast_options),
829            Int64 => cast_to_dictionary::<Int64Type>(array, value_type, cast_options),
830            UInt8 => cast_to_dictionary::<UInt8Type>(array, value_type, cast_options),
831            UInt16 => cast_to_dictionary::<UInt16Type>(array, value_type, cast_options),
832            UInt32 => cast_to_dictionary::<UInt32Type>(array, value_type, cast_options),
833            UInt64 => cast_to_dictionary::<UInt64Type>(array, value_type, cast_options),
834            _ => Err(ArrowError::CastError(format!(
835                "Casting from type {from_type} to dictionary type {to_type} not supported",
836            ))),
837        },
838        // Casting between lists of same types (cast inner values)
839        (List(_), List(to)) => cast_list_values::<i32>(array, to, cast_options),
840        (LargeList(_), LargeList(to)) => cast_list_values::<i64>(array, to, cast_options),
841        (FixedSizeList(_, size_from), FixedSizeList(list_to, size_to)) => {
842            if size_from != size_to {
843                return Err(ArrowError::CastError(
844                    "cannot cast fixed-size-list to fixed-size-list with different size".into(),
845                ));
846            }
847            let array = array.as_fixed_size_list();
848            let values = cast_with_options(array.values(), list_to.data_type(), cast_options)?;
849            Ok(Arc::new(FixedSizeListArray::try_new(
850                list_to.clone(),
851                *size_from,
852                values,
853                array.nulls().cloned(),
854            )?))
855        }
856        (ListView(_), ListView(to)) => cast_list_view_values::<i32>(array, to, cast_options),
857        (LargeListView(_), LargeListView(to)) => {
858            cast_list_view_values::<i64>(array, to, cast_options)
859        }
860        // Casting between different types of lists
861        // List
862        (List(_), LargeList(list_to)) => cast_list::<i32, i64>(array, list_to, cast_options),
863        (List(_), FixedSizeList(field, size)) => {
864            cast_list_to_fixed_size_list::<i32>(array, field, *size, cast_options)
865        }
866        (List(_), ListView(list_to)) => {
867            cast_list_to_list_view::<i32, i32>(array, list_to, cast_options)
868        }
869        (List(_), LargeListView(list_to)) => {
870            cast_list_to_list_view::<i32, i64>(array, list_to, cast_options)
871        }
872        // LargeList
873        (LargeList(_), List(list_to)) => cast_list::<i64, i32>(array, list_to, cast_options),
874        (LargeList(_), FixedSizeList(field, size)) => {
875            cast_list_to_fixed_size_list::<i64>(array, field, *size, cast_options)
876        }
877        (LargeList(_), ListView(list_to)) => {
878            cast_list_to_list_view::<i64, i32>(array, list_to, cast_options)
879        }
880        (LargeList(_), LargeListView(list_to)) => {
881            cast_list_to_list_view::<i64, i64>(array, list_to, cast_options)
882        }
883        // ListView
884        (ListView(_), List(list_to)) => {
885            cast_list_view_to_list::<i32, Int32Type>(array, list_to, cast_options)
886        }
887        (ListView(_), LargeList(list_to)) => {
888            cast_list_view_to_list::<i32, Int64Type>(array, list_to, cast_options)
889        }
890        (ListView(_), LargeListView(list_to)) => {
891            cast_list_view::<i32, i64>(array, list_to, cast_options)
892        }
893        (ListView(_), FixedSizeList(field, size)) => {
894            cast_list_view_to_fixed_size_list::<i32>(array, field, *size, cast_options)
895        }
896        // LargeListView
897        (LargeListView(_), LargeList(list_to)) => {
898            cast_list_view_to_list::<i64, Int64Type>(array, list_to, cast_options)
899        }
900        (LargeListView(_), List(list_to)) => {
901            cast_list_view_to_list::<i64, Int32Type>(array, list_to, cast_options)
902        }
903        (LargeListView(_), ListView(list_to)) => {
904            cast_list_view::<i64, i32>(array, list_to, cast_options)
905        }
906        (LargeListView(_), FixedSizeList(field, size)) => {
907            cast_list_view_to_fixed_size_list::<i64>(array, field, *size, cast_options)
908        }
909        // FixedSizeList
910        (FixedSizeList(_, _), List(list_to)) => {
911            cast_fixed_size_list_to_list::<i32>(array, list_to, cast_options)
912        }
913        (FixedSizeList(_, _), LargeList(list_to)) => {
914            cast_fixed_size_list_to_list::<i64>(array, list_to, cast_options)
915        }
916        (FixedSizeList(_, _), ListView(list_to)) => {
917            cast_fixed_size_list_to_list_view::<i32>(array, list_to, cast_options)
918        }
919        (FixedSizeList(_, _), LargeListView(list_to)) => {
920            cast_fixed_size_list_to_list_view::<i64>(array, list_to, cast_options)
921        }
922        // List to/from other types
923        (FixedSizeList(_, size), _) if *size == 1 => {
924            cast_single_element_fixed_size_list_to_values(array, to_type, cast_options)
925        }
926        // NOTE: we could support FSL to string here too but might be confusing
927        //       since behaviour for size 1 would be different (see arm above)
928        (List(_) | LargeList(_) | ListView(_) | LargeListView(_), _) => match to_type {
929            Utf8 => value_to_string::<i32>(array, cast_options),
930            LargeUtf8 => value_to_string::<i64>(array, cast_options),
931            Utf8View => value_to_string_view(array, cast_options),
932            dt => Err(ArrowError::CastError(format!(
933                "Cannot cast LIST to non-list data type {dt}"
934            ))),
935        },
936        (_, List(to)) => cast_values_to_list::<i32>(array, to, cast_options),
937        (_, LargeList(to)) => cast_values_to_list::<i64>(array, to, cast_options),
938        (_, ListView(to)) => cast_values_to_list_view::<i32>(array, to, cast_options),
939        (_, LargeListView(to)) => cast_values_to_list_view::<i64>(array, to, cast_options),
940        (_, FixedSizeList(to, size)) if *size == 1 => {
941            let values = cast_with_options(array, to.data_type(), cast_options)?;
942            let list = FixedSizeListArray::try_new(to.clone(), 1, values, None)?;
943            Ok(Arc::new(list))
944        }
945        // Map
946        (Map(_, ordered1), Map(_, ordered2)) if ordered1 == ordered2 => {
947            cast_map_values(array.as_map(), to_type, cast_options, ordered1.to_owned())
948        }
949        // Decimal to decimal, same width
950        (Decimal32(p1, s1), Decimal32(p2, s2)) => {
951            cast_decimal_to_decimal_same_type::<Decimal32Type>(
952                array.as_primitive(),
953                *p1,
954                *s1,
955                *p2,
956                *s2,
957                cast_options,
958            )
959        }
960        (Decimal64(p1, s1), Decimal64(p2, s2)) => {
961            cast_decimal_to_decimal_same_type::<Decimal64Type>(
962                array.as_primitive(),
963                *p1,
964                *s1,
965                *p2,
966                *s2,
967                cast_options,
968            )
969        }
970        (Decimal128(p1, s1), Decimal128(p2, s2)) => {
971            cast_decimal_to_decimal_same_type::<Decimal128Type>(
972                array.as_primitive(),
973                *p1,
974                *s1,
975                *p2,
976                *s2,
977                cast_options,
978            )
979        }
980        (Decimal256(p1, s1), Decimal256(p2, s2)) => {
981            cast_decimal_to_decimal_same_type::<Decimal256Type>(
982                array.as_primitive(),
983                *p1,
984                *s1,
985                *p2,
986                *s2,
987                cast_options,
988            )
989        }
990        // Decimal to decimal, different width
991        (Decimal32(p1, s1), Decimal64(p2, s2)) => {
992            cast_decimal_to_decimal::<Decimal32Type, Decimal64Type>(
993                array.as_primitive(),
994                *p1,
995                *s1,
996                *p2,
997                *s2,
998                cast_options,
999            )
1000        }
1001        (Decimal32(p1, s1), Decimal128(p2, s2)) => {
1002            cast_decimal_to_decimal::<Decimal32Type, Decimal128Type>(
1003                array.as_primitive(),
1004                *p1,
1005                *s1,
1006                *p2,
1007                *s2,
1008                cast_options,
1009            )
1010        }
1011        (Decimal32(p1, s1), Decimal256(p2, s2)) => {
1012            cast_decimal_to_decimal::<Decimal32Type, Decimal256Type>(
1013                array.as_primitive(),
1014                *p1,
1015                *s1,
1016                *p2,
1017                *s2,
1018                cast_options,
1019            )
1020        }
1021        (Decimal64(p1, s1), Decimal32(p2, s2)) => {
1022            cast_decimal_to_decimal::<Decimal64Type, Decimal32Type>(
1023                array.as_primitive(),
1024                *p1,
1025                *s1,
1026                *p2,
1027                *s2,
1028                cast_options,
1029            )
1030        }
1031        (Decimal64(p1, s1), Decimal128(p2, s2)) => {
1032            cast_decimal_to_decimal::<Decimal64Type, Decimal128Type>(
1033                array.as_primitive(),
1034                *p1,
1035                *s1,
1036                *p2,
1037                *s2,
1038                cast_options,
1039            )
1040        }
1041        (Decimal64(p1, s1), Decimal256(p2, s2)) => {
1042            cast_decimal_to_decimal::<Decimal64Type, Decimal256Type>(
1043                array.as_primitive(),
1044                *p1,
1045                *s1,
1046                *p2,
1047                *s2,
1048                cast_options,
1049            )
1050        }
1051        (Decimal128(p1, s1), Decimal32(p2, s2)) => {
1052            cast_decimal_to_decimal::<Decimal128Type, Decimal32Type>(
1053                array.as_primitive(),
1054                *p1,
1055                *s1,
1056                *p2,
1057                *s2,
1058                cast_options,
1059            )
1060        }
1061        (Decimal128(p1, s1), Decimal64(p2, s2)) => {
1062            cast_decimal_to_decimal::<Decimal128Type, Decimal64Type>(
1063                array.as_primitive(),
1064                *p1,
1065                *s1,
1066                *p2,
1067                *s2,
1068                cast_options,
1069            )
1070        }
1071        (Decimal128(p1, s1), Decimal256(p2, s2)) => {
1072            cast_decimal_to_decimal::<Decimal128Type, Decimal256Type>(
1073                array.as_primitive(),
1074                *p1,
1075                *s1,
1076                *p2,
1077                *s2,
1078                cast_options,
1079            )
1080        }
1081        (Decimal256(p1, s1), Decimal32(p2, s2)) => {
1082            cast_decimal_to_decimal::<Decimal256Type, Decimal32Type>(
1083                array.as_primitive(),
1084                *p1,
1085                *s1,
1086                *p2,
1087                *s2,
1088                cast_options,
1089            )
1090        }
1091        (Decimal256(p1, s1), Decimal64(p2, s2)) => {
1092            cast_decimal_to_decimal::<Decimal256Type, Decimal64Type>(
1093                array.as_primitive(),
1094                *p1,
1095                *s1,
1096                *p2,
1097                *s2,
1098                cast_options,
1099            )
1100        }
1101        (Decimal256(p1, s1), Decimal128(p2, s2)) => {
1102            cast_decimal_to_decimal::<Decimal256Type, Decimal128Type>(
1103                array.as_primitive(),
1104                *p1,
1105                *s1,
1106                *p2,
1107                *s2,
1108                cast_options,
1109            )
1110        }
1111        // Decimal to non-decimal
1112        (Decimal32(_, scale), _) if !to_type.is_temporal() => {
1113            cast_from_decimal::<Decimal32Type, _>(
1114                array,
1115                10_i32,
1116                scale,
1117                from_type,
1118                to_type,
1119                |x: i32| x as f64,
1120                cast_options,
1121            )
1122        }
1123        (Decimal64(_, scale), _) if !to_type.is_temporal() => {
1124            cast_from_decimal::<Decimal64Type, _>(
1125                array,
1126                10_i64,
1127                scale,
1128                from_type,
1129                to_type,
1130                |x: i64| x as f64,
1131                cast_options,
1132            )
1133        }
1134        (Decimal128(_, scale), _) if !to_type.is_temporal() => {
1135            cast_from_decimal::<Decimal128Type, _>(
1136                array,
1137                10_i128,
1138                scale,
1139                from_type,
1140                to_type,
1141                |x: i128| x as f64,
1142                cast_options,
1143            )
1144        }
1145        (Decimal256(_, scale), _) if !to_type.is_temporal() => {
1146            cast_from_decimal::<Decimal256Type, _>(
1147                array,
1148                i256::from_i128(10_i128),
1149                scale,
1150                from_type,
1151                to_type,
1152                |x: i256| x.to_f64().expect("All i256 values fit in f64"),
1153                cast_options,
1154            )
1155        }
1156        // Non-decimal to decimal
1157        (_, Decimal32(precision, scale)) if !from_type.is_temporal() => {
1158            cast_to_decimal::<Decimal32Type, _>(
1159                array,
1160                10_i32,
1161                precision,
1162                scale,
1163                from_type,
1164                to_type,
1165                cast_options,
1166            )
1167        }
1168        (_, Decimal64(precision, scale)) if !from_type.is_temporal() => {
1169            cast_to_decimal::<Decimal64Type, _>(
1170                array,
1171                10_i64,
1172                precision,
1173                scale,
1174                from_type,
1175                to_type,
1176                cast_options,
1177            )
1178        }
1179        (_, Decimal128(precision, scale)) if !from_type.is_temporal() => {
1180            cast_to_decimal::<Decimal128Type, _>(
1181                array,
1182                10_i128,
1183                precision,
1184                scale,
1185                from_type,
1186                to_type,
1187                cast_options,
1188            )
1189        }
1190        (_, Decimal256(precision, scale)) if !from_type.is_temporal() => {
1191            cast_to_decimal::<Decimal256Type, _>(
1192                array,
1193                i256::from_i128(10_i128),
1194                precision,
1195                scale,
1196                from_type,
1197                to_type,
1198                cast_options,
1199            )
1200        }
1201        (Struct(from_fields), Struct(to_fields)) => cast_struct_to_struct(
1202            array.as_struct(),
1203            from_fields.clone(),
1204            to_fields.clone(),
1205            cast_options,
1206        ),
1207        (Struct(_), _) => Err(ArrowError::CastError(format!(
1208            "Casting from {from_type} to {to_type} not supported"
1209        ))),
1210        (_, Struct(_)) => Err(ArrowError::CastError(format!(
1211            "Casting from {from_type} to {to_type} not supported"
1212        ))),
1213        (_, Boolean) => match from_type {
1214            UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
1215            UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
1216            UInt32 => cast_numeric_to_bool::<UInt32Type>(array),
1217            UInt64 => cast_numeric_to_bool::<UInt64Type>(array),
1218            Int8 => cast_numeric_to_bool::<Int8Type>(array),
1219            Int16 => cast_numeric_to_bool::<Int16Type>(array),
1220            Int32 => cast_numeric_to_bool::<Int32Type>(array),
1221            Int64 => cast_numeric_to_bool::<Int64Type>(array),
1222            Float16 => cast_numeric_to_bool::<Float16Type>(array),
1223            Float32 => cast_numeric_to_bool::<Float32Type>(array),
1224            Float64 => cast_numeric_to_bool::<Float64Type>(array),
1225            Utf8View => cast_utf8view_to_boolean(array, cast_options),
1226            Utf8 => cast_utf8_to_boolean::<i32>(array, cast_options),
1227            LargeUtf8 => cast_utf8_to_boolean::<i64>(array, cast_options),
1228            _ => Err(ArrowError::CastError(format!(
1229                "Casting from {from_type} to {to_type} not supported",
1230            ))),
1231        },
1232        (Boolean, _) => match to_type {
1233            UInt8 => cast_bool_to_numeric::<UInt8Type>(array, cast_options),
1234            UInt16 => cast_bool_to_numeric::<UInt16Type>(array, cast_options),
1235            UInt32 => cast_bool_to_numeric::<UInt32Type>(array, cast_options),
1236            UInt64 => cast_bool_to_numeric::<UInt64Type>(array, cast_options),
1237            Int8 => cast_bool_to_numeric::<Int8Type>(array, cast_options),
1238            Int16 => cast_bool_to_numeric::<Int16Type>(array, cast_options),
1239            Int32 => cast_bool_to_numeric::<Int32Type>(array, cast_options),
1240            Int64 => cast_bool_to_numeric::<Int64Type>(array, cast_options),
1241            Float16 => cast_bool_to_numeric::<Float16Type>(array, cast_options),
1242            Float32 => cast_bool_to_numeric::<Float32Type>(array, cast_options),
1243            Float64 => cast_bool_to_numeric::<Float64Type>(array, cast_options),
1244            Utf8View => value_to_string_view(array, cast_options),
1245            Utf8 => value_to_string::<i32>(array, cast_options),
1246            LargeUtf8 => value_to_string::<i64>(array, cast_options),
1247            _ => Err(ArrowError::CastError(format!(
1248                "Casting from {from_type} to {to_type} not supported",
1249            ))),
1250        },
1251        (Utf8, _) => match to_type {
1252            UInt8 => parse_string::<UInt8Type, i32>(array, cast_options),
1253            UInt16 => parse_string::<UInt16Type, i32>(array, cast_options),
1254            UInt32 => parse_string::<UInt32Type, i32>(array, cast_options),
1255            UInt64 => parse_string::<UInt64Type, i32>(array, cast_options),
1256            Int8 => parse_string::<Int8Type, i32>(array, cast_options),
1257            Int16 => parse_string::<Int16Type, i32>(array, cast_options),
1258            Int32 => parse_string::<Int32Type, i32>(array, cast_options),
1259            Int64 => parse_string::<Int64Type, i32>(array, cast_options),
1260            Float16 => parse_string::<Float16Type, i32>(array, cast_options),
1261            Float32 => parse_string::<Float32Type, i32>(array, cast_options),
1262            Float64 => parse_string::<Float64Type, i32>(array, cast_options),
1263            Date32 => parse_string::<Date32Type, i32>(array, cast_options),
1264            Date64 => parse_string::<Date64Type, i32>(array, cast_options),
1265            Binary => Ok(Arc::new(BinaryArray::from(
1266                array.as_string::<i32>().clone(),
1267            ))),
1268            LargeBinary => {
1269                let binary = BinaryArray::from(array.as_string::<i32>().clone());
1270                cast_byte_container::<BinaryType, LargeBinaryType>(&binary)
1271            }
1272            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i32>()))),
1273            BinaryView => Ok(Arc::new(
1274                StringViewArray::from(array.as_string::<i32>()).to_binary_view(),
1275            )),
1276            LargeUtf8 => cast_byte_container::<Utf8Type, LargeUtf8Type>(array),
1277            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i32>(array, cast_options),
1278            Time32(TimeUnit::Millisecond) => {
1279                parse_string::<Time32MillisecondType, i32>(array, cast_options)
1280            }
1281            Time64(TimeUnit::Microsecond) => {
1282                parse_string::<Time64MicrosecondType, i32>(array, cast_options)
1283            }
1284            Time64(TimeUnit::Nanosecond) => {
1285                parse_string::<Time64NanosecondType, i32>(array, cast_options)
1286            }
1287            Timestamp(TimeUnit::Second, to_tz) => {
1288                cast_string_to_timestamp::<i32, TimestampSecondType>(array, to_tz, cast_options)
1289            }
1290            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1291                i32,
1292                TimestampMillisecondType,
1293            >(array, to_tz, cast_options),
1294            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1295                i32,
1296                TimestampMicrosecondType,
1297            >(array, to_tz, cast_options),
1298            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1299                cast_string_to_timestamp::<i32, TimestampNanosecondType>(array, to_tz, cast_options)
1300            }
1301            Interval(IntervalUnit::YearMonth) => {
1302                cast_string_to_year_month_interval::<i32>(array, cast_options)
1303            }
1304            Interval(IntervalUnit::DayTime) => {
1305                cast_string_to_day_time_interval::<i32>(array, cast_options)
1306            }
1307            Interval(IntervalUnit::MonthDayNano) => {
1308                cast_string_to_month_day_nano_interval::<i32>(array, cast_options)
1309            }
1310            _ => Err(ArrowError::CastError(format!(
1311                "Casting from {from_type} to {to_type} not supported",
1312            ))),
1313        },
1314        (Utf8View, _) => match to_type {
1315            UInt8 => parse_string_view::<UInt8Type>(array, cast_options),
1316            UInt16 => parse_string_view::<UInt16Type>(array, cast_options),
1317            UInt32 => parse_string_view::<UInt32Type>(array, cast_options),
1318            UInt64 => parse_string_view::<UInt64Type>(array, cast_options),
1319            Int8 => parse_string_view::<Int8Type>(array, cast_options),
1320            Int16 => parse_string_view::<Int16Type>(array, cast_options),
1321            Int32 => parse_string_view::<Int32Type>(array, cast_options),
1322            Int64 => parse_string_view::<Int64Type>(array, cast_options),
1323            Float16 => parse_string_view::<Float16Type>(array, cast_options),
1324            Float32 => parse_string_view::<Float32Type>(array, cast_options),
1325            Float64 => parse_string_view::<Float64Type>(array, cast_options),
1326            Date32 => parse_string_view::<Date32Type>(array, cast_options),
1327            Date64 => parse_string_view::<Date64Type>(array, cast_options),
1328            Binary => cast_view_to_byte::<StringViewType, GenericBinaryType<i32>>(array),
1329            LargeBinary => cast_view_to_byte::<StringViewType, GenericBinaryType<i64>>(array),
1330            BinaryView => Ok(Arc::new(array.as_string_view().clone().to_binary_view())),
1331            Utf8 => cast_view_to_byte::<StringViewType, GenericStringType<i32>>(array),
1332            LargeUtf8 => cast_view_to_byte::<StringViewType, GenericStringType<i64>>(array),
1333            Time32(TimeUnit::Second) => parse_string_view::<Time32SecondType>(array, cast_options),
1334            Time32(TimeUnit::Millisecond) => {
1335                parse_string_view::<Time32MillisecondType>(array, cast_options)
1336            }
1337            Time64(TimeUnit::Microsecond) => {
1338                parse_string_view::<Time64MicrosecondType>(array, cast_options)
1339            }
1340            Time64(TimeUnit::Nanosecond) => {
1341                parse_string_view::<Time64NanosecondType>(array, cast_options)
1342            }
1343            Timestamp(TimeUnit::Second, to_tz) => {
1344                cast_view_to_timestamp::<TimestampSecondType>(array, to_tz, cast_options)
1345            }
1346            Timestamp(TimeUnit::Millisecond, to_tz) => {
1347                cast_view_to_timestamp::<TimestampMillisecondType>(array, to_tz, cast_options)
1348            }
1349            Timestamp(TimeUnit::Microsecond, to_tz) => {
1350                cast_view_to_timestamp::<TimestampMicrosecondType>(array, to_tz, cast_options)
1351            }
1352            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1353                cast_view_to_timestamp::<TimestampNanosecondType>(array, to_tz, cast_options)
1354            }
1355            Interval(IntervalUnit::YearMonth) => {
1356                cast_view_to_year_month_interval(array, cast_options)
1357            }
1358            Interval(IntervalUnit::DayTime) => cast_view_to_day_time_interval(array, cast_options),
1359            Interval(IntervalUnit::MonthDayNano) => {
1360                cast_view_to_month_day_nano_interval(array, cast_options)
1361            }
1362            _ => Err(ArrowError::CastError(format!(
1363                "Casting from {from_type} to {to_type} not supported",
1364            ))),
1365        },
1366        (LargeUtf8, _) => match to_type {
1367            UInt8 => parse_string::<UInt8Type, i64>(array, cast_options),
1368            UInt16 => parse_string::<UInt16Type, i64>(array, cast_options),
1369            UInt32 => parse_string::<UInt32Type, i64>(array, cast_options),
1370            UInt64 => parse_string::<UInt64Type, i64>(array, cast_options),
1371            Int8 => parse_string::<Int8Type, i64>(array, cast_options),
1372            Int16 => parse_string::<Int16Type, i64>(array, cast_options),
1373            Int32 => parse_string::<Int32Type, i64>(array, cast_options),
1374            Int64 => parse_string::<Int64Type, i64>(array, cast_options),
1375            Float16 => parse_string::<Float16Type, i64>(array, cast_options),
1376            Float32 => parse_string::<Float32Type, i64>(array, cast_options),
1377            Float64 => parse_string::<Float64Type, i64>(array, cast_options),
1378            Date32 => parse_string::<Date32Type, i64>(array, cast_options),
1379            Date64 => parse_string::<Date64Type, i64>(array, cast_options),
1380            Utf8 => cast_byte_container::<LargeUtf8Type, Utf8Type>(array),
1381            Binary => {
1382                let large_binary = LargeBinaryArray::from(array.as_string::<i64>().clone());
1383                cast_byte_container::<LargeBinaryType, BinaryType>(&large_binary)
1384            }
1385            LargeBinary => Ok(Arc::new(LargeBinaryArray::from(
1386                array.as_string::<i64>().clone(),
1387            ))),
1388            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i64>()))),
1389            BinaryView => Ok(Arc::new(BinaryViewArray::from(
1390                array
1391                    .as_string::<i64>()
1392                    .into_iter()
1393                    .map(|x| x.map(|x| x.as_bytes()))
1394                    .collect::<Vec<_>>(),
1395            ))),
1396            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i64>(array, cast_options),
1397            Time32(TimeUnit::Millisecond) => {
1398                parse_string::<Time32MillisecondType, i64>(array, cast_options)
1399            }
1400            Time64(TimeUnit::Microsecond) => {
1401                parse_string::<Time64MicrosecondType, i64>(array, cast_options)
1402            }
1403            Time64(TimeUnit::Nanosecond) => {
1404                parse_string::<Time64NanosecondType, i64>(array, cast_options)
1405            }
1406            Timestamp(TimeUnit::Second, to_tz) => {
1407                cast_string_to_timestamp::<i64, TimestampSecondType>(array, to_tz, cast_options)
1408            }
1409            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1410                i64,
1411                TimestampMillisecondType,
1412            >(array, to_tz, cast_options),
1413            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1414                i64,
1415                TimestampMicrosecondType,
1416            >(array, to_tz, cast_options),
1417            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1418                cast_string_to_timestamp::<i64, TimestampNanosecondType>(array, to_tz, cast_options)
1419            }
1420            Interval(IntervalUnit::YearMonth) => {
1421                cast_string_to_year_month_interval::<i64>(array, cast_options)
1422            }
1423            Interval(IntervalUnit::DayTime) => {
1424                cast_string_to_day_time_interval::<i64>(array, cast_options)
1425            }
1426            Interval(IntervalUnit::MonthDayNano) => {
1427                cast_string_to_month_day_nano_interval::<i64>(array, cast_options)
1428            }
1429            _ => Err(ArrowError::CastError(format!(
1430                "Casting from {from_type} to {to_type} not supported",
1431            ))),
1432        },
1433        (Binary, _) => match to_type {
1434            Utf8 => cast_binary_to_string::<i32>(array, cast_options),
1435            LargeUtf8 => {
1436                let array = cast_binary_to_string::<i32>(array, cast_options)?;
1437                cast_byte_container::<Utf8Type, LargeUtf8Type>(array.as_ref())
1438            }
1439            LargeBinary => cast_byte_container::<BinaryType, LargeBinaryType>(array),
1440            FixedSizeBinary(size) => {
1441                cast_binary_to_fixed_size_binary::<i32>(array, *size, cast_options)
1442            }
1443            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i32>()))),
1444            Utf8View => Ok(Arc::new(StringViewArray::from(
1445                cast_binary_to_string::<i32>(array, cast_options)?.as_string::<i32>(),
1446            ))),
1447            _ => Err(ArrowError::CastError(format!(
1448                "Casting from {from_type} to {to_type} not supported",
1449            ))),
1450        },
1451        (LargeBinary, _) => match to_type {
1452            Utf8 => {
1453                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1454                cast_byte_container::<LargeUtf8Type, Utf8Type>(array.as_ref())
1455            }
1456            LargeUtf8 => cast_binary_to_string::<i64>(array, cast_options),
1457            Binary => cast_byte_container::<LargeBinaryType, BinaryType>(array),
1458            FixedSizeBinary(size) => {
1459                cast_binary_to_fixed_size_binary::<i64>(array, *size, cast_options)
1460            }
1461            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i64>()))),
1462            Utf8View => {
1463                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1464                Ok(Arc::new(StringViewArray::from(array.as_string::<i64>())))
1465            }
1466            _ => Err(ArrowError::CastError(format!(
1467                "Casting from {from_type} to {to_type} not supported",
1468            ))),
1469        },
1470        (FixedSizeBinary(size), _) => match to_type {
1471            Binary => cast_fixed_size_binary_to_binary::<i32>(array, *size),
1472            LargeBinary => cast_fixed_size_binary_to_binary::<i64>(array, *size),
1473            BinaryView => cast_fixed_size_binary_to_binary_view(array, *size),
1474            _ => Err(ArrowError::CastError(format!(
1475                "Casting from {from_type} to {to_type} not supported",
1476            ))),
1477        },
1478        (BinaryView, Binary) => cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array),
1479        (BinaryView, LargeBinary) => {
1480            cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)
1481        }
1482        (BinaryView, Utf8) => {
1483            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array)?;
1484            cast_binary_to_string::<i32>(&binary_arr, cast_options)
1485        }
1486        (BinaryView, LargeUtf8) => {
1487            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)?;
1488            cast_binary_to_string::<i64>(&binary_arr, cast_options)
1489        }
1490        (BinaryView, Utf8View) => cast_binary_view_to_string_view(array, cast_options),
1491        (BinaryView, _) => Err(ArrowError::CastError(format!(
1492            "Casting from {from_type} to {to_type} not supported",
1493        ))),
1494        (from_type, Utf8View) if from_type.is_primitive() => {
1495            value_to_string_view(array, cast_options)
1496        }
1497        (from_type, LargeUtf8) if from_type.is_primitive() => {
1498            value_to_string::<i64>(array, cast_options)
1499        }
1500        (from_type, Utf8) if from_type.is_primitive() => {
1501            value_to_string::<i32>(array, cast_options)
1502        }
1503        (from_type, Binary) if from_type.is_integer() => match from_type {
1504            UInt8 => cast_numeric_to_binary::<UInt8Type, i32>(array),
1505            UInt16 => cast_numeric_to_binary::<UInt16Type, i32>(array),
1506            UInt32 => cast_numeric_to_binary::<UInt32Type, i32>(array),
1507            UInt64 => cast_numeric_to_binary::<UInt64Type, i32>(array),
1508            Int8 => cast_numeric_to_binary::<Int8Type, i32>(array),
1509            Int16 => cast_numeric_to_binary::<Int16Type, i32>(array),
1510            Int32 => cast_numeric_to_binary::<Int32Type, i32>(array),
1511            Int64 => cast_numeric_to_binary::<Int64Type, i32>(array),
1512            _ => unreachable!(),
1513        },
1514        (from_type, LargeBinary) if from_type.is_integer() => match from_type {
1515            UInt8 => cast_numeric_to_binary::<UInt8Type, i64>(array),
1516            UInt16 => cast_numeric_to_binary::<UInt16Type, i64>(array),
1517            UInt32 => cast_numeric_to_binary::<UInt32Type, i64>(array),
1518            UInt64 => cast_numeric_to_binary::<UInt64Type, i64>(array),
1519            Int8 => cast_numeric_to_binary::<Int8Type, i64>(array),
1520            Int16 => cast_numeric_to_binary::<Int16Type, i64>(array),
1521            Int32 => cast_numeric_to_binary::<Int32Type, i64>(array),
1522            Int64 => cast_numeric_to_binary::<Int64Type, i64>(array),
1523            _ => unreachable!(),
1524        },
1525        // start numeric casts
1526        (UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array, cast_options),
1527        (UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array, cast_options),
1528        (UInt8, UInt64) => cast_numeric_arrays::<UInt8Type, UInt64Type>(array, cast_options),
1529        (UInt8, Int8) => cast_numeric_arrays::<UInt8Type, Int8Type>(array, cast_options),
1530        (UInt8, Int16) => cast_numeric_arrays::<UInt8Type, Int16Type>(array, cast_options),
1531        (UInt8, Int32) => cast_numeric_arrays::<UInt8Type, Int32Type>(array, cast_options),
1532        (UInt8, Int64) => cast_numeric_arrays::<UInt8Type, Int64Type>(array, cast_options),
1533        (UInt8, Float16) => cast_numeric_arrays::<UInt8Type, Float16Type>(array, cast_options),
1534        (UInt8, Float32) => cast_numeric_arrays::<UInt8Type, Float32Type>(array, cast_options),
1535        (UInt8, Float64) => cast_numeric_arrays::<UInt8Type, Float64Type>(array, cast_options),
1536
1537        (UInt16, UInt8) => cast_numeric_arrays::<UInt16Type, UInt8Type>(array, cast_options),
1538        (UInt16, UInt32) => cast_numeric_arrays::<UInt16Type, UInt32Type>(array, cast_options),
1539        (UInt16, UInt64) => cast_numeric_arrays::<UInt16Type, UInt64Type>(array, cast_options),
1540        (UInt16, Int8) => cast_numeric_arrays::<UInt16Type, Int8Type>(array, cast_options),
1541        (UInt16, Int16) => cast_numeric_arrays::<UInt16Type, Int16Type>(array, cast_options),
1542        (UInt16, Int32) => cast_numeric_arrays::<UInt16Type, Int32Type>(array, cast_options),
1543        (UInt16, Int64) => cast_numeric_arrays::<UInt16Type, Int64Type>(array, cast_options),
1544        (UInt16, Float16) => cast_numeric_arrays::<UInt16Type, Float16Type>(array, cast_options),
1545        (UInt16, Float32) => cast_numeric_arrays::<UInt16Type, Float32Type>(array, cast_options),
1546        (UInt16, Float64) => cast_numeric_arrays::<UInt16Type, Float64Type>(array, cast_options),
1547
1548        (UInt32, UInt8) => cast_numeric_arrays::<UInt32Type, UInt8Type>(array, cast_options),
1549        (UInt32, UInt16) => cast_numeric_arrays::<UInt32Type, UInt16Type>(array, cast_options),
1550        (UInt32, UInt64) => cast_numeric_arrays::<UInt32Type, UInt64Type>(array, cast_options),
1551        (UInt32, Int8) => cast_numeric_arrays::<UInt32Type, Int8Type>(array, cast_options),
1552        (UInt32, Int16) => cast_numeric_arrays::<UInt32Type, Int16Type>(array, cast_options),
1553        (UInt32, Int32) => cast_numeric_arrays::<UInt32Type, Int32Type>(array, cast_options),
1554        (UInt32, Int64) => cast_numeric_arrays::<UInt32Type, Int64Type>(array, cast_options),
1555        (UInt32, Float16) => cast_numeric_arrays::<UInt32Type, Float16Type>(array, cast_options),
1556        (UInt32, Float32) => cast_numeric_arrays::<UInt32Type, Float32Type>(array, cast_options),
1557        (UInt32, Float64) => cast_numeric_arrays::<UInt32Type, Float64Type>(array, cast_options),
1558
1559        (UInt64, UInt8) => cast_numeric_arrays::<UInt64Type, UInt8Type>(array, cast_options),
1560        (UInt64, UInt16) => cast_numeric_arrays::<UInt64Type, UInt16Type>(array, cast_options),
1561        (UInt64, UInt32) => cast_numeric_arrays::<UInt64Type, UInt32Type>(array, cast_options),
1562        (UInt64, Int8) => cast_numeric_arrays::<UInt64Type, Int8Type>(array, cast_options),
1563        (UInt64, Int16) => cast_numeric_arrays::<UInt64Type, Int16Type>(array, cast_options),
1564        (UInt64, Int32) => cast_numeric_arrays::<UInt64Type, Int32Type>(array, cast_options),
1565        (UInt64, Int64) => cast_numeric_arrays::<UInt64Type, Int64Type>(array, cast_options),
1566        (UInt64, Float16) => cast_numeric_arrays::<UInt64Type, Float16Type>(array, cast_options),
1567        (UInt64, Float32) => cast_numeric_arrays::<UInt64Type, Float32Type>(array, cast_options),
1568        (UInt64, Float64) => cast_numeric_arrays::<UInt64Type, Float64Type>(array, cast_options),
1569
1570        (Int8, UInt8) => cast_numeric_arrays::<Int8Type, UInt8Type>(array, cast_options),
1571        (Int8, UInt16) => cast_numeric_arrays::<Int8Type, UInt16Type>(array, cast_options),
1572        (Int8, UInt32) => cast_numeric_arrays::<Int8Type, UInt32Type>(array, cast_options),
1573        (Int8, UInt64) => cast_numeric_arrays::<Int8Type, UInt64Type>(array, cast_options),
1574        (Int8, Int16) => cast_numeric_arrays::<Int8Type, Int16Type>(array, cast_options),
1575        (Int8, Int32) => cast_numeric_arrays::<Int8Type, Int32Type>(array, cast_options),
1576        (Int8, Int64) => cast_numeric_arrays::<Int8Type, Int64Type>(array, cast_options),
1577        (Int8, Float16) => cast_numeric_arrays::<Int8Type, Float16Type>(array, cast_options),
1578        (Int8, Float32) => cast_numeric_arrays::<Int8Type, Float32Type>(array, cast_options),
1579        (Int8, Float64) => cast_numeric_arrays::<Int8Type, Float64Type>(array, cast_options),
1580
1581        (Int16, UInt8) => cast_numeric_arrays::<Int16Type, UInt8Type>(array, cast_options),
1582        (Int16, UInt16) => cast_numeric_arrays::<Int16Type, UInt16Type>(array, cast_options),
1583        (Int16, UInt32) => cast_numeric_arrays::<Int16Type, UInt32Type>(array, cast_options),
1584        (Int16, UInt64) => cast_numeric_arrays::<Int16Type, UInt64Type>(array, cast_options),
1585        (Int16, Int8) => cast_numeric_arrays::<Int16Type, Int8Type>(array, cast_options),
1586        (Int16, Int32) => cast_numeric_arrays::<Int16Type, Int32Type>(array, cast_options),
1587        (Int16, Int64) => cast_numeric_arrays::<Int16Type, Int64Type>(array, cast_options),
1588        (Int16, Float16) => cast_numeric_arrays::<Int16Type, Float16Type>(array, cast_options),
1589        (Int16, Float32) => cast_numeric_arrays::<Int16Type, Float32Type>(array, cast_options),
1590        (Int16, Float64) => cast_numeric_arrays::<Int16Type, Float64Type>(array, cast_options),
1591
1592        (Int32, UInt8) => cast_numeric_arrays::<Int32Type, UInt8Type>(array, cast_options),
1593        (Int32, UInt16) => cast_numeric_arrays::<Int32Type, UInt16Type>(array, cast_options),
1594        (Int32, UInt32) => cast_numeric_arrays::<Int32Type, UInt32Type>(array, cast_options),
1595        (Int32, UInt64) => cast_numeric_arrays::<Int32Type, UInt64Type>(array, cast_options),
1596        (Int32, Int8) => cast_numeric_arrays::<Int32Type, Int8Type>(array, cast_options),
1597        (Int32, Int16) => cast_numeric_arrays::<Int32Type, Int16Type>(array, cast_options),
1598        (Int32, Int64) => cast_numeric_arrays::<Int32Type, Int64Type>(array, cast_options),
1599        (Int32, Float16) => cast_numeric_arrays::<Int32Type, Float16Type>(array, cast_options),
1600        (Int32, Float32) => cast_numeric_arrays::<Int32Type, Float32Type>(array, cast_options),
1601        (Int32, Float64) => cast_numeric_arrays::<Int32Type, Float64Type>(array, cast_options),
1602
1603        (Int64, UInt8) => cast_numeric_arrays::<Int64Type, UInt8Type>(array, cast_options),
1604        (Int64, UInt16) => cast_numeric_arrays::<Int64Type, UInt16Type>(array, cast_options),
1605        (Int64, UInt32) => cast_numeric_arrays::<Int64Type, UInt32Type>(array, cast_options),
1606        (Int64, UInt64) => cast_numeric_arrays::<Int64Type, UInt64Type>(array, cast_options),
1607        (Int64, Int8) => cast_numeric_arrays::<Int64Type, Int8Type>(array, cast_options),
1608        (Int64, Int16) => cast_numeric_arrays::<Int64Type, Int16Type>(array, cast_options),
1609        (Int64, Int32) => cast_numeric_arrays::<Int64Type, Int32Type>(array, cast_options),
1610        (Int64, Float16) => cast_numeric_arrays::<Int64Type, Float16Type>(array, cast_options),
1611        (Int64, Float32) => cast_numeric_arrays::<Int64Type, Float32Type>(array, cast_options),
1612        (Int64, Float64) => cast_numeric_arrays::<Int64Type, Float64Type>(array, cast_options),
1613
1614        (Float16, UInt8) => cast_numeric_arrays::<Float16Type, UInt8Type>(array, cast_options),
1615        (Float16, UInt16) => cast_numeric_arrays::<Float16Type, UInt16Type>(array, cast_options),
1616        (Float16, UInt32) => cast_numeric_arrays::<Float16Type, UInt32Type>(array, cast_options),
1617        (Float16, UInt64) => cast_numeric_arrays::<Float16Type, UInt64Type>(array, cast_options),
1618        (Float16, Int8) => cast_numeric_arrays::<Float16Type, Int8Type>(array, cast_options),
1619        (Float16, Int16) => cast_numeric_arrays::<Float16Type, Int16Type>(array, cast_options),
1620        (Float16, Int32) => cast_numeric_arrays::<Float16Type, Int32Type>(array, cast_options),
1621        (Float16, Int64) => cast_numeric_arrays::<Float16Type, Int64Type>(array, cast_options),
1622        (Float16, Float32) => cast_numeric_arrays::<Float16Type, Float32Type>(array, cast_options),
1623        (Float16, Float64) => cast_numeric_arrays::<Float16Type, Float64Type>(array, cast_options),
1624
1625        (Float32, UInt8) => cast_numeric_arrays::<Float32Type, UInt8Type>(array, cast_options),
1626        (Float32, UInt16) => cast_numeric_arrays::<Float32Type, UInt16Type>(array, cast_options),
1627        (Float32, UInt32) => cast_numeric_arrays::<Float32Type, UInt32Type>(array, cast_options),
1628        (Float32, UInt64) => cast_numeric_arrays::<Float32Type, UInt64Type>(array, cast_options),
1629        (Float32, Int8) => cast_numeric_arrays::<Float32Type, Int8Type>(array, cast_options),
1630        (Float32, Int16) => cast_numeric_arrays::<Float32Type, Int16Type>(array, cast_options),
1631        (Float32, Int32) => cast_numeric_arrays::<Float32Type, Int32Type>(array, cast_options),
1632        (Float32, Int64) => cast_numeric_arrays::<Float32Type, Int64Type>(array, cast_options),
1633        (Float32, Float16) => cast_numeric_arrays::<Float32Type, Float16Type>(array, cast_options),
1634        (Float32, Float64) => cast_numeric_arrays::<Float32Type, Float64Type>(array, cast_options),
1635
1636        (Float64, UInt8) => cast_numeric_arrays::<Float64Type, UInt8Type>(array, cast_options),
1637        (Float64, UInt16) => cast_numeric_arrays::<Float64Type, UInt16Type>(array, cast_options),
1638        (Float64, UInt32) => cast_numeric_arrays::<Float64Type, UInt32Type>(array, cast_options),
1639        (Float64, UInt64) => cast_numeric_arrays::<Float64Type, UInt64Type>(array, cast_options),
1640        (Float64, Int8) => cast_numeric_arrays::<Float64Type, Int8Type>(array, cast_options),
1641        (Float64, Int16) => cast_numeric_arrays::<Float64Type, Int16Type>(array, cast_options),
1642        (Float64, Int32) => cast_numeric_arrays::<Float64Type, Int32Type>(array, cast_options),
1643        (Float64, Int64) => cast_numeric_arrays::<Float64Type, Int64Type>(array, cast_options),
1644        (Float64, Float16) => cast_numeric_arrays::<Float64Type, Float16Type>(array, cast_options),
1645        (Float64, Float32) => cast_numeric_arrays::<Float64Type, Float32Type>(array, cast_options),
1646        // end numeric casts
1647
1648        // temporal casts
1649        (Int32, Date32) => cast_reinterpret_arrays::<Int32Type, Date32Type>(array),
1650        (Int32, Date64) => cast_with_options(
1651            &cast_with_options(array, &Date32, cast_options)?,
1652            &Date64,
1653            cast_options,
1654        ),
1655        (Int32, Time32(TimeUnit::Second)) => {
1656            cast_reinterpret_arrays::<Int32Type, Time32SecondType>(array)
1657        }
1658        (Int32, Time32(TimeUnit::Millisecond)) => {
1659            cast_reinterpret_arrays::<Int32Type, Time32MillisecondType>(array)
1660        }
1661        // No support for microsecond/nanosecond with i32
1662        (Date32, Int32) => cast_reinterpret_arrays::<Date32Type, Int32Type>(array),
1663        (Date32, Int64) => cast_with_options(
1664            &cast_with_options(array, &Int32, cast_options)?,
1665            &Int64,
1666            cast_options,
1667        ),
1668        (Time32(TimeUnit::Second), Int32) => {
1669            cast_reinterpret_arrays::<Time32SecondType, Int32Type>(array)
1670        }
1671        (Time32(TimeUnit::Millisecond), Int32) => {
1672            cast_reinterpret_arrays::<Time32MillisecondType, Int32Type>(array)
1673        }
1674        (Time32(TimeUnit::Second), Int64) => cast_with_options(
1675            &cast_with_options(array, &Int32, cast_options)?,
1676            &Int64,
1677            cast_options,
1678        ),
1679        (Time32(TimeUnit::Millisecond), Int64) => cast_with_options(
1680            &cast_with_options(array, &Int32, cast_options)?,
1681            &Int64,
1682            cast_options,
1683        ),
1684        (Int64, Date64) => cast_reinterpret_arrays::<Int64Type, Date64Type>(array),
1685        (Int64, Date32) => cast_with_options(
1686            &cast_with_options(array, &Int32, cast_options)?,
1687            &Date32,
1688            cast_options,
1689        ),
1690        // No support for second/milliseconds with i64
1691        (Int64, Time64(TimeUnit::Microsecond)) => {
1692            cast_reinterpret_arrays::<Int64Type, Time64MicrosecondType>(array)
1693        }
1694        (Int64, Time64(TimeUnit::Nanosecond)) => {
1695            cast_reinterpret_arrays::<Int64Type, Time64NanosecondType>(array)
1696        }
1697
1698        (Date64, Int64) => cast_reinterpret_arrays::<Date64Type, Int64Type>(array),
1699        (Date64, Int32) => cast_with_options(
1700            &cast_with_options(array, &Int64, cast_options)?,
1701            &Int32,
1702            cast_options,
1703        ),
1704        (Time64(TimeUnit::Microsecond), Int64) => {
1705            cast_reinterpret_arrays::<Time64MicrosecondType, Int64Type>(array)
1706        }
1707        (Time64(TimeUnit::Nanosecond), Int64) => {
1708            cast_reinterpret_arrays::<Time64NanosecondType, Int64Type>(array)
1709        }
1710        (Date32, Date64) => Ok(Arc::new(
1711            array
1712                .as_primitive::<Date32Type>()
1713                .unary::<_, Date64Type>(|x| x as i64 * MILLISECONDS_IN_DAY),
1714        )),
1715        (Date64, Date32) => {
1716            let array = array.as_primitive::<Date64Type>();
1717            let result = if cast_options.safe {
1718                array.unary_opt::<_, Date32Type>(|x| i32::try_from(x / MILLISECONDS_IN_DAY).ok())
1719            } else {
1720                array.try_unary::<_, Date32Type, _>(|x| {
1721                    i32::try_from(x / MILLISECONDS_IN_DAY).map_err(|_| {
1722                        ArrowError::CastError(format!(
1723                            "Cannot cast Date64 value {x} to Date32 without overflow"
1724                        ))
1725                    })
1726                })?
1727            };
1728            Ok(Arc::new(result))
1729        }
1730
1731        (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => {
1732            let array = array.as_primitive::<Time32SecondType>();
1733            let result = if cast_options.safe {
1734                array.unary_opt::<_, Time32MillisecondType>(|x| x.checked_mul(MILLISECONDS as i32))
1735            } else {
1736                array.try_unary::<_, Time32MillisecondType, _>(|x| {
1737                    x.mul_checked(MILLISECONDS as i32)
1738                })?
1739            };
1740            Ok(Arc::new(result))
1741        }
1742        (Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1743            array
1744                .as_primitive::<Time32SecondType>()
1745                .unary::<_, Time64MicrosecondType>(|x| x as i64 * MICROSECONDS),
1746        )),
1747        (Time32(TimeUnit::Second), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1748            array
1749                .as_primitive::<Time32SecondType>()
1750                .unary::<_, Time64NanosecondType>(|x| x as i64 * NANOSECONDS),
1751        )),
1752
1753        (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1754            array
1755                .as_primitive::<Time32MillisecondType>()
1756                .unary::<_, Time32SecondType>(|x| x / MILLISECONDS as i32),
1757        )),
1758        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1759            array
1760                .as_primitive::<Time32MillisecondType>()
1761                .unary::<_, Time64MicrosecondType>(|x| x as i64 * (MICROSECONDS / MILLISECONDS)),
1762        )),
1763        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1764            array
1765                .as_primitive::<Time32MillisecondType>()
1766                .unary::<_, Time64NanosecondType>(|x| x as i64 * (NANOSECONDS / MILLISECONDS)),
1767        )),
1768
1769        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1770            array
1771                .as_primitive::<Time64MicrosecondType>()
1772                .unary::<_, Time32SecondType>(|x| (x / MICROSECONDS) as i32),
1773        )),
1774        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1775            array
1776                .as_primitive::<Time64MicrosecondType>()
1777                .unary::<_, Time32MillisecondType>(|x| (x / (MICROSECONDS / MILLISECONDS)) as i32),
1778        )),
1779        (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1780            array
1781                .as_primitive::<Time64MicrosecondType>()
1782                .unary::<_, Time64NanosecondType>(|x| x * (NANOSECONDS / MICROSECONDS)),
1783        )),
1784
1785        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1786            array
1787                .as_primitive::<Time64NanosecondType>()
1788                .unary::<_, Time32SecondType>(|x| (x / NANOSECONDS) as i32),
1789        )),
1790        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1791            array
1792                .as_primitive::<Time64NanosecondType>()
1793                .unary::<_, Time32MillisecondType>(|x| (x / (NANOSECONDS / MILLISECONDS)) as i32),
1794        )),
1795        (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1796            array
1797                .as_primitive::<Time64NanosecondType>()
1798                .unary::<_, Time64MicrosecondType>(|x| x / (NANOSECONDS / MICROSECONDS)),
1799        )),
1800
1801        // Timestamp to integer/floating/decimals
1802        (Timestamp(TimeUnit::Second, _), _) if to_type.is_numeric() => {
1803            let array = cast_reinterpret_arrays::<TimestampSecondType, Int64Type>(array)?;
1804            cast_with_options(&array, to_type, cast_options)
1805        }
1806        (Timestamp(TimeUnit::Millisecond, _), _) if to_type.is_numeric() => {
1807            let array = cast_reinterpret_arrays::<TimestampMillisecondType, Int64Type>(array)?;
1808            cast_with_options(&array, to_type, cast_options)
1809        }
1810        (Timestamp(TimeUnit::Microsecond, _), _) if to_type.is_numeric() => {
1811            let array = cast_reinterpret_arrays::<TimestampMicrosecondType, Int64Type>(array)?;
1812            cast_with_options(&array, to_type, cast_options)
1813        }
1814        (Timestamp(TimeUnit::Nanosecond, _), _) if to_type.is_numeric() => {
1815            let array = cast_reinterpret_arrays::<TimestampNanosecondType, Int64Type>(array)?;
1816            cast_with_options(&array, to_type, cast_options)
1817        }
1818
1819        (_, Timestamp(unit, tz)) if from_type.is_numeric() => {
1820            let array = cast_with_options(array, &Int64, cast_options)?;
1821            Ok(make_timestamp_array(
1822                array.as_primitive(),
1823                *unit,
1824                tz.clone(),
1825            ))
1826        }
1827
1828        (Timestamp(from_unit, from_tz), Timestamp(to_unit, to_tz)) => {
1829            let array = cast_with_options(array, &Int64, cast_options)?;
1830            let time_array = array.as_primitive::<Int64Type>();
1831            let from_size = time_unit_multiple(from_unit);
1832            let to_size = time_unit_multiple(to_unit);
1833            // we either divide or multiply, depending on size of each unit
1834            // units are never the same when the types are the same
1835            let converted = match from_size.cmp(&to_size) {
1836                Ordering::Greater => {
1837                    let divisor = from_size / to_size;
1838                    time_array.unary::<_, Int64Type>(|o| o / divisor)
1839                }
1840                Ordering::Equal => time_array.clone(),
1841                Ordering::Less => {
1842                    let mul = to_size / from_size;
1843                    if cast_options.safe {
1844                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1845                    } else {
1846                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1847                    }
1848                }
1849            };
1850            // Normalize timezone
1851            let adjusted = match (from_tz, to_tz) {
1852                // Only this case needs to be adjusted because we're casting from
1853                // unknown time offset to some time offset, we want the time to be
1854                // unchanged.
1855                //
1856                // i.e. Timestamp('2001-01-01T00:00', None) -> Timestamp('2001-01-01T00:00', '+0700')
1857                (None, Some(to_tz)) => {
1858                    let to_tz: Tz = to_tz.parse()?;
1859                    match to_unit {
1860                        TimeUnit::Second => adjust_timestamp_to_timezone::<TimestampSecondType>(
1861                            converted,
1862                            &to_tz,
1863                            cast_options,
1864                        )?,
1865                        TimeUnit::Millisecond => adjust_timestamp_to_timezone::<
1866                            TimestampMillisecondType,
1867                        >(
1868                            converted, &to_tz, cast_options
1869                        )?,
1870                        TimeUnit::Microsecond => adjust_timestamp_to_timezone::<
1871                            TimestampMicrosecondType,
1872                        >(
1873                            converted, &to_tz, cast_options
1874                        )?,
1875                        TimeUnit::Nanosecond => adjust_timestamp_to_timezone::<
1876                            TimestampNanosecondType,
1877                        >(
1878                            converted, &to_tz, cast_options
1879                        )?,
1880                    }
1881                }
1882                _ => converted,
1883            };
1884            Ok(make_timestamp_array(&adjusted, *to_unit, to_tz.clone()))
1885        }
1886        (Timestamp(TimeUnit::Microsecond, _), Date32) => {
1887            timestamp_to_date32(array.as_primitive::<TimestampMicrosecondType>())
1888        }
1889        (Timestamp(TimeUnit::Millisecond, _), Date32) => {
1890            timestamp_to_date32(array.as_primitive::<TimestampMillisecondType>())
1891        }
1892        (Timestamp(TimeUnit::Second, _), Date32) => {
1893            timestamp_to_date32(array.as_primitive::<TimestampSecondType>())
1894        }
1895        (Timestamp(TimeUnit::Nanosecond, _), Date32) => {
1896            timestamp_to_date32(array.as_primitive::<TimestampNanosecondType>())
1897        }
1898        (Timestamp(TimeUnit::Second, _), Date64) => Ok(Arc::new(match cast_options.safe {
1899            true => {
1900                // change error to None
1901                array
1902                    .as_primitive::<TimestampSecondType>()
1903                    .unary_opt::<_, Date64Type>(|x| x.checked_mul(MILLISECONDS))
1904            }
1905            false => array
1906                .as_primitive::<TimestampSecondType>()
1907                .try_unary::<_, Date64Type, _>(|x| x.mul_checked(MILLISECONDS))?,
1908        })),
1909        (Timestamp(TimeUnit::Millisecond, _), Date64) => {
1910            cast_reinterpret_arrays::<TimestampMillisecondType, Date64Type>(array)
1911        }
1912        (Timestamp(TimeUnit::Microsecond, _), Date64) => Ok(Arc::new(
1913            array
1914                .as_primitive::<TimestampMicrosecondType>()
1915                .unary::<_, Date64Type>(|x| x / (MICROSECONDS / MILLISECONDS)),
1916        )),
1917        (Timestamp(TimeUnit::Nanosecond, _), Date64) => Ok(Arc::new(
1918            array
1919                .as_primitive::<TimestampNanosecondType>()
1920                .unary::<_, Date64Type>(|x| x / (NANOSECONDS / MILLISECONDS)),
1921        )),
1922        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Microsecond)) => {
1923            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1924            Ok(Arc::new(
1925                array
1926                    .as_primitive::<TimestampSecondType>()
1927                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1928                        Ok(time_to_time64us(as_time_res_with_timezone::<
1929                            TimestampSecondType,
1930                        >(x, tz)?))
1931                    })?,
1932            ))
1933        }
1934        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Nanosecond)) => {
1935            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1936            Ok(Arc::new(
1937                array
1938                    .as_primitive::<TimestampSecondType>()
1939                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1940                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1941                            TimestampSecondType,
1942                        >(x, tz)?))
1943                    })?,
1944            ))
1945        }
1946        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Microsecond)) => {
1947            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1948            Ok(Arc::new(
1949                array
1950                    .as_primitive::<TimestampMillisecondType>()
1951                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1952                        Ok(time_to_time64us(as_time_res_with_timezone::<
1953                            TimestampMillisecondType,
1954                        >(x, tz)?))
1955                    })?,
1956            ))
1957        }
1958        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Nanosecond)) => {
1959            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1960            Ok(Arc::new(
1961                array
1962                    .as_primitive::<TimestampMillisecondType>()
1963                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1964                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1965                            TimestampMillisecondType,
1966                        >(x, tz)?))
1967                    })?,
1968            ))
1969        }
1970        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Microsecond)) => {
1971            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1972            Ok(Arc::new(
1973                array
1974                    .as_primitive::<TimestampMicrosecondType>()
1975                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1976                        Ok(time_to_time64us(as_time_res_with_timezone::<
1977                            TimestampMicrosecondType,
1978                        >(x, tz)?))
1979                    })?,
1980            ))
1981        }
1982        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Nanosecond)) => {
1983            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1984            Ok(Arc::new(
1985                array
1986                    .as_primitive::<TimestampMicrosecondType>()
1987                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1988                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1989                            TimestampMicrosecondType,
1990                        >(x, tz)?))
1991                    })?,
1992            ))
1993        }
1994        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Microsecond)) => {
1995            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1996            Ok(Arc::new(
1997                array
1998                    .as_primitive::<TimestampNanosecondType>()
1999                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
2000                        Ok(time_to_time64us(as_time_res_with_timezone::<
2001                            TimestampNanosecondType,
2002                        >(x, tz)?))
2003                    })?,
2004            ))
2005        }
2006        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Nanosecond)) => {
2007            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2008            Ok(Arc::new(
2009                array
2010                    .as_primitive::<TimestampNanosecondType>()
2011                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
2012                        Ok(time_to_time64ns(as_time_res_with_timezone::<
2013                            TimestampNanosecondType,
2014                        >(x, tz)?))
2015                    })?,
2016            ))
2017        }
2018        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Second)) => {
2019            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2020            Ok(Arc::new(
2021                array
2022                    .as_primitive::<TimestampSecondType>()
2023                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2024                        Ok(time_to_time32s(as_time_res_with_timezone::<
2025                            TimestampSecondType,
2026                        >(x, tz)?))
2027                    })?,
2028            ))
2029        }
2030        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Millisecond)) => {
2031            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2032            Ok(Arc::new(
2033                array
2034                    .as_primitive::<TimestampSecondType>()
2035                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2036                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2037                            TimestampSecondType,
2038                        >(x, tz)?))
2039                    })?,
2040            ))
2041        }
2042        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Second)) => {
2043            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2044            Ok(Arc::new(
2045                array
2046                    .as_primitive::<TimestampMillisecondType>()
2047                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2048                        Ok(time_to_time32s(as_time_res_with_timezone::<
2049                            TimestampMillisecondType,
2050                        >(x, tz)?))
2051                    })?,
2052            ))
2053        }
2054        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Millisecond)) => {
2055            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2056            Ok(Arc::new(
2057                array
2058                    .as_primitive::<TimestampMillisecondType>()
2059                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2060                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2061                            TimestampMillisecondType,
2062                        >(x, tz)?))
2063                    })?,
2064            ))
2065        }
2066        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Second)) => {
2067            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2068            Ok(Arc::new(
2069                array
2070                    .as_primitive::<TimestampMicrosecondType>()
2071                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2072                        Ok(time_to_time32s(as_time_res_with_timezone::<
2073                            TimestampMicrosecondType,
2074                        >(x, tz)?))
2075                    })?,
2076            ))
2077        }
2078        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Millisecond)) => {
2079            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2080            Ok(Arc::new(
2081                array
2082                    .as_primitive::<TimestampMicrosecondType>()
2083                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2084                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2085                            TimestampMicrosecondType,
2086                        >(x, tz)?))
2087                    })?,
2088            ))
2089        }
2090        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Second)) => {
2091            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2092            Ok(Arc::new(
2093                array
2094                    .as_primitive::<TimestampNanosecondType>()
2095                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2096                        Ok(time_to_time32s(as_time_res_with_timezone::<
2097                            TimestampNanosecondType,
2098                        >(x, tz)?))
2099                    })?,
2100            ))
2101        }
2102        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Millisecond)) => {
2103            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2104            Ok(Arc::new(
2105                array
2106                    .as_primitive::<TimestampNanosecondType>()
2107                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2108                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2109                            TimestampNanosecondType,
2110                        >(x, tz)?))
2111                    })?,
2112            ))
2113        }
2114        (Date64, Timestamp(TimeUnit::Second, _)) => {
2115            let array = array
2116                .as_primitive::<Date64Type>()
2117                .unary::<_, TimestampSecondType>(|x| x / MILLISECONDS);
2118
2119            cast_with_options(&array, to_type, cast_options)
2120        }
2121        (Date64, Timestamp(TimeUnit::Millisecond, _)) => {
2122            let array = array
2123                .as_primitive::<Date64Type>()
2124                .reinterpret_cast::<TimestampMillisecondType>();
2125
2126            cast_with_options(&array, to_type, cast_options)
2127        }
2128
2129        (Date64, Timestamp(TimeUnit::Microsecond, _)) => {
2130            let array = array
2131                .as_primitive::<Date64Type>()
2132                .unary::<_, TimestampMicrosecondType>(|x| x * (MICROSECONDS / MILLISECONDS));
2133
2134            cast_with_options(&array, to_type, cast_options)
2135        }
2136        (Date64, Timestamp(TimeUnit::Nanosecond, _)) => {
2137            let array = array
2138                .as_primitive::<Date64Type>()
2139                .unary::<_, TimestampNanosecondType>(|x| x * (NANOSECONDS / MILLISECONDS));
2140
2141            cast_with_options(&array, to_type, cast_options)
2142        }
2143        (Date32, Timestamp(TimeUnit::Second, _)) => {
2144            let array = array
2145                .as_primitive::<Date32Type>()
2146                .unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY);
2147
2148            cast_with_options(&array, to_type, cast_options)
2149        }
2150        (Date32, Timestamp(TimeUnit::Millisecond, _)) => {
2151            let array = array
2152                .as_primitive::<Date32Type>()
2153                .unary::<_, TimestampMillisecondType>(|x| (x as i64) * MILLISECONDS_IN_DAY);
2154
2155            cast_with_options(&array, to_type, cast_options)
2156        }
2157        (Date32, Timestamp(TimeUnit::Microsecond, _)) => {
2158            let date_array = array.as_primitive::<Date32Type>();
2159            let converted = if cast_options.safe {
2160                date_array.unary_opt::<_, TimestampMicrosecondType>(|x| {
2161                    (x as i64).checked_mul(MICROSECONDS_IN_DAY)
2162                })
2163            } else {
2164                date_array.try_unary::<_, TimestampMicrosecondType, _>(|x| {
2165                    (x as i64).mul_checked(MICROSECONDS_IN_DAY)
2166                })?
2167            };
2168            cast_with_options(&converted, to_type, cast_options)
2169        }
2170        (Date32, Timestamp(TimeUnit::Nanosecond, _)) => {
2171            let date_array = array.as_primitive::<Date32Type>();
2172            let converted = if cast_options.safe {
2173                date_array.unary_opt::<_, TimestampNanosecondType>(|x| {
2174                    (x as i64).checked_mul(NANOSECONDS_IN_DAY)
2175                })
2176            } else {
2177                date_array.try_unary::<_, TimestampNanosecondType, _>(|x| {
2178                    (x as i64).mul_checked(NANOSECONDS_IN_DAY)
2179                })?
2180            };
2181            cast_with_options(&converted, to_type, cast_options)
2182        }
2183
2184        (_, Duration(unit)) if from_type.is_numeric() => {
2185            let array = cast_with_options(array, &Int64, cast_options)?;
2186            Ok(make_duration_array(array.as_primitive(), *unit))
2187        }
2188        (Duration(TimeUnit::Second), _) if to_type.is_numeric() => {
2189            let array = cast_reinterpret_arrays::<DurationSecondType, Int64Type>(array)?;
2190            cast_with_options(&array, to_type, cast_options)
2191        }
2192        (Duration(TimeUnit::Millisecond), _) if to_type.is_numeric() => {
2193            let array = cast_reinterpret_arrays::<DurationMillisecondType, Int64Type>(array)?;
2194            cast_with_options(&array, to_type, cast_options)
2195        }
2196        (Duration(TimeUnit::Microsecond), _) if to_type.is_numeric() => {
2197            let array = cast_reinterpret_arrays::<DurationMicrosecondType, Int64Type>(array)?;
2198            cast_with_options(&array, to_type, cast_options)
2199        }
2200        (Duration(TimeUnit::Nanosecond), _) if to_type.is_numeric() => {
2201            let array = cast_reinterpret_arrays::<DurationNanosecondType, Int64Type>(array)?;
2202            cast_with_options(&array, to_type, cast_options)
2203        }
2204
2205        (Duration(from_unit), Duration(to_unit)) => {
2206            let array = cast_with_options(array, &Int64, cast_options)?;
2207            let time_array = array.as_primitive::<Int64Type>();
2208            let from_size = time_unit_multiple(from_unit);
2209            let to_size = time_unit_multiple(to_unit);
2210            // we either divide or multiply, depending on size of each unit
2211            // units are never the same when the types are the same
2212            let converted = match from_size.cmp(&to_size) {
2213                Ordering::Greater => {
2214                    let divisor = from_size / to_size;
2215                    time_array.unary::<_, Int64Type>(|o| o / divisor)
2216                }
2217                Ordering::Equal => time_array.clone(),
2218                Ordering::Less => {
2219                    let mul = to_size / from_size;
2220                    if cast_options.safe {
2221                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
2222                    } else {
2223                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
2224                    }
2225                }
2226            };
2227            Ok(make_duration_array(&converted, *to_unit))
2228        }
2229
2230        (Duration(TimeUnit::Second), Interval(IntervalUnit::MonthDayNano)) => {
2231            cast_duration_to_interval::<DurationSecondType>(array, cast_options)
2232        }
2233        (Duration(TimeUnit::Millisecond), Interval(IntervalUnit::MonthDayNano)) => {
2234            cast_duration_to_interval::<DurationMillisecondType>(array, cast_options)
2235        }
2236        (Duration(TimeUnit::Microsecond), Interval(IntervalUnit::MonthDayNano)) => {
2237            cast_duration_to_interval::<DurationMicrosecondType>(array, cast_options)
2238        }
2239        (Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano)) => {
2240            cast_duration_to_interval::<DurationNanosecondType>(array, cast_options)
2241        }
2242        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Second)) => {
2243            cast_month_day_nano_to_duration::<DurationSecondType>(array, cast_options)
2244        }
2245        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Millisecond)) => {
2246            cast_month_day_nano_to_duration::<DurationMillisecondType>(array, cast_options)
2247        }
2248        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Microsecond)) => {
2249            cast_month_day_nano_to_duration::<DurationMicrosecondType>(array, cast_options)
2250        }
2251        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Nanosecond)) => {
2252            cast_month_day_nano_to_duration::<DurationNanosecondType>(array, cast_options)
2253        }
2254        (Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
2255            cast_interval_year_month_to_interval_month_day_nano(array, cast_options)
2256        }
2257        (Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
2258            cast_interval_day_time_to_interval_month_day_nano(array, cast_options)
2259        }
2260        (Int32, Interval(IntervalUnit::YearMonth)) => {
2261            cast_reinterpret_arrays::<Int32Type, IntervalYearMonthType>(array)
2262        }
2263        (_, _) => Err(ArrowError::CastError(format!(
2264            "Casting from {from_type} to {to_type} not supported",
2265        ))),
2266    }
2267}
2268
2269fn cast_struct_to_struct(
2270    array: &StructArray,
2271    from_fields: Fields,
2272    to_fields: Fields,
2273    cast_options: &CastOptions,
2274) -> Result<ArrayRef, ArrowError> {
2275    // Fast path: if field names are in the same order, we can just zip and cast
2276    let fields_match_order = from_fields.len() == to_fields.len()
2277        && from_fields
2278            .iter()
2279            .zip(to_fields.iter())
2280            .all(|(f1, f2)| f1.name() == f2.name());
2281
2282    let fields = if fields_match_order {
2283        // Fast path: cast columns in order if their names match
2284        cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2285    } else {
2286        let all_fields_match_by_name = to_fields.iter().all(|to_field| {
2287            from_fields
2288                .iter()
2289                .any(|from_field| from_field.name() == to_field.name())
2290        });
2291
2292        if all_fields_match_by_name {
2293            // Slow path: match fields by name and reorder
2294            cast_struct_fields_by_name(array, from_fields.clone(), to_fields.clone(), cast_options)?
2295        } else {
2296            // Fallback: cast field by field in order
2297            cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2298        }
2299    };
2300
2301    let array = StructArray::try_new(to_fields.clone(), fields, array.nulls().cloned())?;
2302    Ok(Arc::new(array) as ArrayRef)
2303}
2304
2305fn cast_struct_fields_by_name(
2306    array: &StructArray,
2307    from_fields: Fields,
2308    to_fields: Fields,
2309    cast_options: &CastOptions,
2310) -> Result<Vec<ArrayRef>, ArrowError> {
2311    to_fields
2312        .iter()
2313        .map(|to_field| {
2314            let from_field_idx = from_fields
2315                .iter()
2316                .position(|from_field| from_field.name() == to_field.name())
2317                .unwrap(); // safe because we checked above
2318            let column = array.column(from_field_idx);
2319            cast_with_options(column, to_field.data_type(), cast_options)
2320        })
2321        .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2322}
2323
2324fn cast_struct_fields_in_order(
2325    array: &StructArray,
2326    to_fields: Fields,
2327    cast_options: &CastOptions,
2328) -> Result<Vec<ArrayRef>, ArrowError> {
2329    array
2330        .columns()
2331        .iter()
2332        .zip(to_fields.iter())
2333        .map(|(l, field)| cast_with_options(l, field.data_type(), cast_options))
2334        .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2335}
2336
2337fn cast_from_decimal<D, F>(
2338    array: &dyn Array,
2339    base: D::Native,
2340    scale: &i8,
2341    from_type: &DataType,
2342    to_type: &DataType,
2343    as_float: F,
2344    cast_options: &CastOptions,
2345) -> Result<ArrayRef, ArrowError>
2346where
2347    D: DecimalType + ArrowPrimitiveType,
2348    <D as ArrowPrimitiveType>::Native: ToPrimitive,
2349    F: Fn(D::Native) -> f64,
2350{
2351    use DataType::*;
2352    // cast decimal to other type
2353    match to_type {
2354        UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, base, *scale, cast_options),
2355        UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, base, *scale, cast_options),
2356        UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, base, *scale, cast_options),
2357        UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, base, *scale, cast_options),
2358        Int8 => cast_decimal_to_integer::<D, Int8Type>(array, base, *scale, cast_options),
2359        Int16 => cast_decimal_to_integer::<D, Int16Type>(array, base, *scale, cast_options),
2360        Int32 => cast_decimal_to_integer::<D, Int32Type>(array, base, *scale, cast_options),
2361        Int64 => cast_decimal_to_integer::<D, Int64Type>(array, base, *scale, cast_options),
2362        Float16 => cast_decimal_to_float::<D, Float16Type, _>(array, |x| {
2363            half::f16::from_f64(single_decimal_to_float_lossy::<D, F>(
2364                &as_float,
2365                x,
2366                <i32 as From<i8>>::from(*scale),
2367            ))
2368        }),
2369        Float32 => cast_decimal_to_float::<D, Float32Type, _>(array, |x| {
2370            single_decimal_to_float_lossy::<D, F>(&as_float, x, <i32 as From<i8>>::from(*scale))
2371                as f32
2372        }),
2373        Float64 => cast_decimal_to_float::<D, Float64Type, _>(array, |x| {
2374            single_decimal_to_float_lossy::<D, F>(&as_float, x, <i32 as From<i8>>::from(*scale))
2375        }),
2376        Utf8View => value_to_string_view(array, cast_options),
2377        Utf8 => value_to_string::<i32>(array, cast_options),
2378        LargeUtf8 => value_to_string::<i64>(array, cast_options),
2379        Null => Ok(new_null_array(to_type, array.len())),
2380        _ => Err(ArrowError::CastError(format!(
2381            "Casting from {from_type} to {to_type} not supported"
2382        ))),
2383    }
2384}
2385
2386fn cast_to_decimal<D, M>(
2387    array: &dyn Array,
2388    base: M,
2389    precision: &u8,
2390    scale: &i8,
2391    from_type: &DataType,
2392    to_type: &DataType,
2393    cast_options: &CastOptions,
2394) -> Result<ArrayRef, ArrowError>
2395where
2396    D: DecimalType + ArrowPrimitiveType<Native = M>,
2397    M: ArrowNativeTypeOp + DecimalCast,
2398    u8: num_traits::AsPrimitive<M>,
2399    u16: num_traits::AsPrimitive<M>,
2400    u32: num_traits::AsPrimitive<M>,
2401    u64: num_traits::AsPrimitive<M>,
2402    i8: num_traits::AsPrimitive<M>,
2403    i16: num_traits::AsPrimitive<M>,
2404    i32: num_traits::AsPrimitive<M>,
2405    i64: num_traits::AsPrimitive<M>,
2406{
2407    use DataType::*;
2408    // cast data to decimal
2409    match from_type {
2410        UInt8 => cast_integer_to_decimal::<_, D, M>(
2411            array.as_primitive::<UInt8Type>(),
2412            *precision,
2413            *scale,
2414            base,
2415            cast_options,
2416        ),
2417        UInt16 => cast_integer_to_decimal::<_, D, _>(
2418            array.as_primitive::<UInt16Type>(),
2419            *precision,
2420            *scale,
2421            base,
2422            cast_options,
2423        ),
2424        UInt32 => cast_integer_to_decimal::<_, D, _>(
2425            array.as_primitive::<UInt32Type>(),
2426            *precision,
2427            *scale,
2428            base,
2429            cast_options,
2430        ),
2431        UInt64 => cast_integer_to_decimal::<_, D, _>(
2432            array.as_primitive::<UInt64Type>(),
2433            *precision,
2434            *scale,
2435            base,
2436            cast_options,
2437        ),
2438        Int8 => cast_integer_to_decimal::<_, D, _>(
2439            array.as_primitive::<Int8Type>(),
2440            *precision,
2441            *scale,
2442            base,
2443            cast_options,
2444        ),
2445        Int16 => cast_integer_to_decimal::<_, D, _>(
2446            array.as_primitive::<Int16Type>(),
2447            *precision,
2448            *scale,
2449            base,
2450            cast_options,
2451        ),
2452        Int32 => cast_integer_to_decimal::<_, D, _>(
2453            array.as_primitive::<Int32Type>(),
2454            *precision,
2455            *scale,
2456            base,
2457            cast_options,
2458        ),
2459        Int64 => cast_integer_to_decimal::<_, D, _>(
2460            array.as_primitive::<Int64Type>(),
2461            *precision,
2462            *scale,
2463            base,
2464            cast_options,
2465        ),
2466        Float16 => cast_floating_point_to_decimal::<_, D>(
2467            array.as_primitive::<Float16Type>(),
2468            *precision,
2469            *scale,
2470            cast_options,
2471        ),
2472        Float32 => cast_floating_point_to_decimal::<_, D>(
2473            array.as_primitive::<Float32Type>(),
2474            *precision,
2475            *scale,
2476            cast_options,
2477        ),
2478        Float64 => cast_floating_point_to_decimal::<_, D>(
2479            array.as_primitive::<Float64Type>(),
2480            *precision,
2481            *scale,
2482            cast_options,
2483        ),
2484        Utf8View | Utf8 => {
2485            cast_string_to_decimal::<D, i32>(array, *precision, *scale, cast_options)
2486        }
2487        LargeUtf8 => cast_string_to_decimal::<D, i64>(array, *precision, *scale, cast_options),
2488        Null => Ok(new_null_array(to_type, array.len())),
2489        _ => Err(ArrowError::CastError(format!(
2490            "Casting from {from_type} to {to_type} not supported"
2491        ))),
2492    }
2493}
2494
2495/// Get the time unit as a multiple of a second
2496const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
2497    match unit {
2498        TimeUnit::Second => 1,
2499        TimeUnit::Millisecond => MILLISECONDS,
2500        TimeUnit::Microsecond => MICROSECONDS,
2501        TimeUnit::Nanosecond => NANOSECONDS,
2502    }
2503}
2504
2505/// Convert Array into a PrimitiveArray of type, and apply numeric cast
2506fn cast_numeric_arrays<FROM, TO>(
2507    from: &dyn Array,
2508    cast_options: &CastOptions,
2509) -> Result<ArrayRef, ArrowError>
2510where
2511    FROM: ArrowPrimitiveType,
2512    TO: ArrowPrimitiveType,
2513    FROM::Native: NumCast,
2514    TO::Native: NumCast,
2515{
2516    if cast_options.safe {
2517        // If the value can't be casted to the `TO::Native`, return null
2518        Ok(Arc::new(numeric_cast::<FROM, TO>(
2519            from.as_primitive::<FROM>(),
2520        )))
2521    } else {
2522        // If the value can't be casted to the `TO::Native`, return error
2523        Ok(Arc::new(try_numeric_cast::<FROM, TO>(
2524            from.as_primitive::<FROM>(),
2525        )?))
2526    }
2527}
2528
2529// Natural cast between numeric types
2530// If the value of T can't be casted to R, will throw error
2531fn try_numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>, ArrowError>
2532where
2533    T: ArrowPrimitiveType,
2534    R: ArrowPrimitiveType,
2535    T::Native: NumCast,
2536    R::Native: NumCast,
2537{
2538    from.try_unary(|value| {
2539        num_cast::<T::Native, R::Native>(value).ok_or_else(|| {
2540            ArrowError::CastError(format!(
2541                "Can't cast value {:?} to type {}",
2542                value,
2543                R::DATA_TYPE
2544            ))
2545        })
2546    })
2547}
2548
2549/// Natural cast between numeric types
2550/// Return None if the input `value` can't be casted to type `O`.
2551#[inline]
2552pub fn num_cast<I, O>(value: I) -> Option<O>
2553where
2554    I: NumCast,
2555    O: NumCast,
2556{
2557    num_traits::cast::cast::<I, O>(value)
2558}
2559
2560// Natural cast between numeric types
2561// If the value of T can't be casted to R, it will be converted to null
2562fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> PrimitiveArray<R>
2563where
2564    T: ArrowPrimitiveType,
2565    R: ArrowPrimitiveType,
2566    T::Native: NumCast,
2567    R::Native: NumCast,
2568{
2569    from.unary_opt::<_, R>(num_cast::<T::Native, R::Native>)
2570}
2571
2572fn cast_numeric_to_binary<FROM: ArrowPrimitiveType, O: OffsetSizeTrait>(
2573    array: &dyn Array,
2574) -> Result<ArrayRef, ArrowError> {
2575    let array = array.as_primitive::<FROM>();
2576    let size = std::mem::size_of::<FROM::Native>();
2577    let offsets = OffsetBuffer::from_repeated_length(size, array.len());
2578    Ok(Arc::new(GenericBinaryArray::<O>::try_new(
2579        offsets,
2580        array.values().inner().clone(),
2581        array.nulls().cloned(),
2582    )?))
2583}
2584
2585fn adjust_timestamp_to_timezone<T: ArrowTimestampType>(
2586    array: PrimitiveArray<Int64Type>,
2587    to_tz: &Tz,
2588    cast_options: &CastOptions,
2589) -> Result<PrimitiveArray<Int64Type>, ArrowError> {
2590    let adjust = |o| {
2591        let local = as_datetime::<T>(o)?;
2592        let offset = to_tz.offset_from_local_datetime(&local).single()?;
2593        T::from_naive_datetime(local - offset.fix(), None)
2594    };
2595    let adjusted = if cast_options.safe {
2596        array.unary_opt::<_, Int64Type>(adjust)
2597    } else {
2598        array.try_unary::<_, Int64Type, _>(|o| {
2599            adjust(o).ok_or_else(|| {
2600                ArrowError::CastError("Cannot cast timezone to different timezone".to_string())
2601            })
2602        })?
2603    };
2604    Ok(adjusted)
2605}
2606
2607/// Cast numeric types to Boolean
2608///
2609/// Any zero value returns `false` while non-zero returns `true`
2610fn cast_numeric_to_bool<FROM>(from: &dyn Array) -> Result<ArrayRef, ArrowError>
2611where
2612    FROM: ArrowPrimitiveType,
2613{
2614    numeric_to_bool_cast::<FROM>(from.as_primitive::<FROM>()).map(|to| Arc::new(to) as ArrayRef)
2615}
2616
2617fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray, ArrowError>
2618where
2619    T: ArrowPrimitiveType + ArrowPrimitiveType,
2620{
2621    let mut b = BooleanBuilder::with_capacity(from.len());
2622
2623    for i in 0..from.len() {
2624        if from.is_null(i) {
2625            b.append_null();
2626        } else {
2627            b.append_value(cast_num_to_bool::<T::Native>(from.value(i)));
2628        }
2629    }
2630
2631    Ok(b.finish())
2632}
2633
2634/// Cast numeric types to boolean
2635#[inline]
2636pub fn cast_num_to_bool<I>(value: I) -> bool
2637where
2638    I: Default + PartialEq,
2639{
2640    value != I::default()
2641}
2642
2643/// Cast Boolean types to numeric
2644///
2645/// `false` returns 0 while `true` returns 1
2646fn cast_bool_to_numeric<TO>(
2647    from: &dyn Array,
2648    cast_options: &CastOptions,
2649) -> Result<ArrayRef, ArrowError>
2650where
2651    TO: ArrowPrimitiveType,
2652    TO::Native: num_traits::cast::NumCast,
2653{
2654    Ok(Arc::new(bool_to_numeric_cast::<TO>(
2655        from.as_any().downcast_ref::<BooleanArray>().unwrap(),
2656        cast_options,
2657    )))
2658}
2659
2660fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions) -> PrimitiveArray<T>
2661where
2662    T: ArrowPrimitiveType,
2663    T::Native: num_traits::NumCast,
2664{
2665    let iter = (0..from.len()).map(|i| {
2666        if from.is_null(i) {
2667            None
2668        } else {
2669            single_bool_to_numeric::<T::Native>(from.value(i))
2670        }
2671    });
2672    // Benefit:
2673    //     20% performance improvement
2674    // Soundness:
2675    //     The iterator is trustedLen because it comes from a Range
2676    unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
2677}
2678
2679/// Cast single bool value to numeric value.
2680#[inline]
2681pub fn single_bool_to_numeric<O>(value: bool) -> Option<O>
2682where
2683    O: num_traits::NumCast + Default,
2684{
2685    if value {
2686        // a workaround to cast a primitive to type O, infallible
2687        num_traits::cast::cast(1)
2688    } else {
2689        Some(O::default())
2690    }
2691}
2692
2693/// Helper function to cast from one `BinaryArray` or 'LargeBinaryArray' to 'FixedSizeBinaryArray'.
2694fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
2695    array: &dyn Array,
2696    byte_width: i32,
2697    cast_options: &CastOptions,
2698) -> Result<ArrayRef, ArrowError> {
2699    let array = array.as_binary::<O>();
2700    let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(), byte_width);
2701
2702    for i in 0..array.len() {
2703        if array.is_null(i) {
2704            builder.append_null();
2705        } else {
2706            match builder.append_value(array.value(i)) {
2707                Ok(_) => {}
2708                Err(e) => match cast_options.safe {
2709                    true => builder.append_null(),
2710                    false => return Err(e),
2711                },
2712            }
2713        }
2714    }
2715
2716    Ok(Arc::new(builder.finish()))
2717}
2718
2719/// Helper function to cast from 'FixedSizeBinaryArray' to one `BinaryArray` or 'LargeBinaryArray'.
2720/// If the target one is too large for the source array it will return an Error.
2721fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
2722    array: &dyn Array,
2723    byte_width: i32,
2724) -> Result<ArrayRef, ArrowError> {
2725    let array = array
2726        .as_any()
2727        .downcast_ref::<FixedSizeBinaryArray>()
2728        .unwrap();
2729
2730    let offsets: i128 = byte_width as i128 * array.len() as i128;
2731
2732    let is_binary = matches!(GenericBinaryType::<O>::DATA_TYPE, DataType::Binary);
2733    if is_binary && offsets > i32::MAX as i128 {
2734        return Err(ArrowError::ComputeError(
2735            "FixedSizeBinary array too large to cast to Binary array".to_string(),
2736        ));
2737    } else if !is_binary && offsets > i64::MAX as i128 {
2738        return Err(ArrowError::ComputeError(
2739            "FixedSizeBinary array too large to cast to LargeBinary array".to_string(),
2740        ));
2741    }
2742
2743    let mut builder = GenericBinaryBuilder::<O>::with_capacity(array.len(), array.len());
2744
2745    for i in 0..array.len() {
2746        if array.is_null(i) {
2747            builder.append_null();
2748        } else {
2749            builder.append_value(array.value(i));
2750        }
2751    }
2752
2753    Ok(Arc::new(builder.finish()))
2754}
2755
2756fn cast_fixed_size_binary_to_binary_view(
2757    array: &dyn Array,
2758    _byte_width: i32,
2759) -> Result<ArrayRef, ArrowError> {
2760    let array = array
2761        .as_any()
2762        .downcast_ref::<FixedSizeBinaryArray>()
2763        .unwrap();
2764
2765    let mut builder = BinaryViewBuilder::with_capacity(array.len());
2766    for i in 0..array.len() {
2767        if array.is_null(i) {
2768            builder.append_null();
2769        } else {
2770            builder.append_value(array.value(i));
2771        }
2772    }
2773
2774    Ok(Arc::new(builder.finish()))
2775}
2776
2777/// Helper function to cast from one `ByteArrayType` to another and vice versa.
2778/// If the target one (e.g., `LargeUtf8`) is too large for the source array it will return an Error.
2779fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2780where
2781    FROM: ByteArrayType,
2782    TO: ByteArrayType<Native = FROM::Native>,
2783    FROM::Offset: OffsetSizeTrait + ToPrimitive,
2784    TO::Offset: OffsetSizeTrait + NumCast,
2785{
2786    let data = array.to_data();
2787    assert_eq!(data.data_type(), &FROM::DATA_TYPE);
2788    let str_values_buf = data.buffers()[1].clone();
2789    let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
2790
2791    let mut offset_builder = BufferBuilder::<TO::Offset>::new(offsets.len());
2792    offsets
2793        .iter()
2794        .try_for_each::<_, Result<_, ArrowError>>(|offset| {
2795            let offset =
2796                <<TO as ByteArrayType>::Offset as NumCast>::from(*offset).ok_or_else(|| {
2797                    ArrowError::ComputeError(format!(
2798                        "{}{} array too large to cast to {}{} array",
2799                        FROM::Offset::PREFIX,
2800                        FROM::PREFIX,
2801                        TO::Offset::PREFIX,
2802                        TO::PREFIX
2803                    ))
2804                })?;
2805            offset_builder.append(offset);
2806            Ok(())
2807        })?;
2808
2809    let offset_buffer = offset_builder.finish();
2810
2811    let dtype = TO::DATA_TYPE;
2812
2813    let builder = ArrayData::builder(dtype)
2814        .offset(array.offset())
2815        .len(array.len())
2816        .add_buffer(offset_buffer)
2817        .add_buffer(str_values_buf)
2818        .nulls(data.nulls().cloned());
2819
2820    let array_data = unsafe { builder.build_unchecked() };
2821
2822    Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
2823}
2824
2825/// Helper function to cast from one `ByteViewType` array to `ByteArrayType` array.
2826fn cast_view_to_byte<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2827where
2828    FROM: ByteViewType,
2829    TO: ByteArrayType,
2830    FROM::Native: AsRef<TO::Native>,
2831{
2832    let data = array.to_data();
2833    let view_array = GenericByteViewArray::<FROM>::from(data);
2834
2835    let len = view_array.len();
2836    let bytes = view_array
2837        .views()
2838        .iter()
2839        .map(|v| ByteView::from(*v).length as usize)
2840        .sum::<usize>();
2841
2842    let mut byte_array_builder = GenericByteBuilder::<TO>::with_capacity(len, bytes);
2843
2844    for val in view_array.iter() {
2845        byte_array_builder.append_option(val);
2846    }
2847
2848    Ok(Arc::new(byte_array_builder.finish()))
2849}
2850
2851#[cfg(test)]
2852mod tests {
2853    use super::*;
2854    use DataType::*;
2855    use arrow_array::{Int64Array, RunArray, StringArray};
2856    use arrow_buffer::{Buffer, IntervalDayTime, NullBuffer};
2857    use arrow_buffer::{ScalarBuffer, i256};
2858    use arrow_schema::{DataType, Field};
2859    use chrono::NaiveDate;
2860    use half::f16;
2861    use std::sync::Arc;
2862
2863    #[derive(Clone)]
2864    struct DecimalCastTestConfig {
2865        input_prec: u8,
2866        input_scale: i8,
2867        input_repr: i128,
2868        output_prec: u8,
2869        output_scale: i8,
2870        expected_output_repr: Result<i128, String>, // the error variant can contain a string
2871                                                    // template where the "{}" will be
2872                                                    // replaced with the decimal type name
2873                                                    // (e.g. Decimal128)
2874    }
2875
2876    macro_rules! generate_cast_test_case {
2877        ($INPUT_ARRAY: expr, $OUTPUT_TYPE_ARRAY: ident, $OUTPUT_TYPE: expr, $OUTPUT_VALUES: expr) => {
2878            let output =
2879                $OUTPUT_TYPE_ARRAY::from($OUTPUT_VALUES).with_data_type($OUTPUT_TYPE.clone());
2880
2881            // assert cast type
2882            let input_array_type = $INPUT_ARRAY.data_type();
2883            assert!(can_cast_types(input_array_type, $OUTPUT_TYPE));
2884            let result = cast($INPUT_ARRAY, $OUTPUT_TYPE).unwrap();
2885            assert_eq!($OUTPUT_TYPE, result.data_type());
2886            assert_eq!(result.as_ref(), &output);
2887
2888            let cast_option = CastOptions {
2889                safe: false,
2890                format_options: FormatOptions::default(),
2891            };
2892            let result = cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap();
2893            assert_eq!($OUTPUT_TYPE, result.data_type());
2894            assert_eq!(result.as_ref(), &output);
2895        };
2896    }
2897
2898    fn run_decimal_cast_test_case<I, O>(t: DecimalCastTestConfig)
2899    where
2900        I: DecimalType,
2901        O: DecimalType,
2902        I::Native: DecimalCast,
2903        O::Native: DecimalCast,
2904    {
2905        let array = vec![I::Native::from_decimal(t.input_repr)];
2906        let array = array
2907            .into_iter()
2908            .collect::<PrimitiveArray<I>>()
2909            .with_precision_and_scale(t.input_prec, t.input_scale)
2910            .unwrap();
2911        let input_type = array.data_type();
2912        let output_type = O::TYPE_CONSTRUCTOR(t.output_prec, t.output_scale);
2913        assert!(can_cast_types(input_type, &output_type));
2914
2915        let options = CastOptions {
2916            safe: false,
2917            ..Default::default()
2918        };
2919        let result = cast_with_options(&array, &output_type, &options);
2920
2921        match t.expected_output_repr {
2922            Ok(v) => {
2923                let expected_array = vec![O::Native::from_decimal(v)];
2924                let expected_array = expected_array
2925                    .into_iter()
2926                    .collect::<PrimitiveArray<O>>()
2927                    .with_precision_and_scale(t.output_prec, t.output_scale)
2928                    .unwrap();
2929                assert_eq!(*result.unwrap(), expected_array);
2930            }
2931            Err(expected_output_message_template) => {
2932                assert!(result.is_err());
2933                let expected_error_message =
2934                    expected_output_message_template.replace("{}", O::PREFIX);
2935                assert_eq!(result.unwrap_err().to_string(), expected_error_message);
2936            }
2937        }
2938    }
2939
2940    fn create_decimal32_array(
2941        array: Vec<Option<i32>>,
2942        precision: u8,
2943        scale: i8,
2944    ) -> Result<Decimal32Array, ArrowError> {
2945        array
2946            .into_iter()
2947            .collect::<Decimal32Array>()
2948            .with_precision_and_scale(precision, scale)
2949    }
2950
2951    fn create_decimal64_array(
2952        array: Vec<Option<i64>>,
2953        precision: u8,
2954        scale: i8,
2955    ) -> Result<Decimal64Array, ArrowError> {
2956        array
2957            .into_iter()
2958            .collect::<Decimal64Array>()
2959            .with_precision_and_scale(precision, scale)
2960    }
2961
2962    fn create_decimal128_array(
2963        array: Vec<Option<i128>>,
2964        precision: u8,
2965        scale: i8,
2966    ) -> Result<Decimal128Array, ArrowError> {
2967        array
2968            .into_iter()
2969            .collect::<Decimal128Array>()
2970            .with_precision_and_scale(precision, scale)
2971    }
2972
2973    fn create_decimal256_array(
2974        array: Vec<Option<i256>>,
2975        precision: u8,
2976        scale: i8,
2977    ) -> Result<Decimal256Array, ArrowError> {
2978        array
2979            .into_iter()
2980            .collect::<Decimal256Array>()
2981            .with_precision_and_scale(precision, scale)
2982    }
2983
2984    #[test]
2985    #[cfg(not(feature = "force_validate"))]
2986    #[should_panic(
2987        expected = "Cannot cast to Decimal128(20, 3). Overflowing on 57896044618658097711785492504343953926634992332820282019728792003956564819967"
2988    )]
2989    fn test_cast_decimal_to_decimal_round_with_error() {
2990        // decimal256 to decimal128 overflow
2991        let array = vec![
2992            Some(i256::from_i128(1123454)),
2993            Some(i256::from_i128(2123456)),
2994            Some(i256::from_i128(-3123453)),
2995            Some(i256::from_i128(-3123456)),
2996            None,
2997            Some(i256::MAX),
2998            Some(i256::MIN),
2999        ];
3000        let input_decimal_array = create_decimal256_array(array, 76, 4).unwrap();
3001        let array = Arc::new(input_decimal_array) as ArrayRef;
3002        let input_type = DataType::Decimal256(76, 4);
3003        let output_type = DataType::Decimal128(20, 3);
3004        assert!(can_cast_types(&input_type, &output_type));
3005        generate_cast_test_case!(
3006            &array,
3007            Decimal128Array,
3008            &output_type,
3009            vec![
3010                Some(112345_i128),
3011                Some(212346_i128),
3012                Some(-312345_i128),
3013                Some(-312346_i128),
3014                None,
3015                None,
3016                None,
3017            ]
3018        );
3019    }
3020
3021    #[test]
3022    #[cfg(not(feature = "force_validate"))]
3023    fn test_cast_decimal_to_decimal_round() {
3024        let array = vec![
3025            Some(1123454),
3026            Some(2123456),
3027            Some(-3123453),
3028            Some(-3123456),
3029            None,
3030        ];
3031        let array = create_decimal128_array(array, 20, 4).unwrap();
3032        // decimal128 to decimal128
3033        let input_type = DataType::Decimal128(20, 4);
3034        let output_type = DataType::Decimal128(20, 3);
3035        assert!(can_cast_types(&input_type, &output_type));
3036        generate_cast_test_case!(
3037            &array,
3038            Decimal128Array,
3039            &output_type,
3040            vec![
3041                Some(112345_i128),
3042                Some(212346_i128),
3043                Some(-312345_i128),
3044                Some(-312346_i128),
3045                None
3046            ]
3047        );
3048
3049        // decimal128 to decimal256
3050        let input_type = DataType::Decimal128(20, 4);
3051        let output_type = DataType::Decimal256(20, 3);
3052        assert!(can_cast_types(&input_type, &output_type));
3053        generate_cast_test_case!(
3054            &array,
3055            Decimal256Array,
3056            &output_type,
3057            vec![
3058                Some(i256::from_i128(112345_i128)),
3059                Some(i256::from_i128(212346_i128)),
3060                Some(i256::from_i128(-312345_i128)),
3061                Some(i256::from_i128(-312346_i128)),
3062                None
3063            ]
3064        );
3065
3066        // decimal256
3067        let array = vec![
3068            Some(i256::from_i128(1123454)),
3069            Some(i256::from_i128(2123456)),
3070            Some(i256::from_i128(-3123453)),
3071            Some(i256::from_i128(-3123456)),
3072            None,
3073        ];
3074        let array = create_decimal256_array(array, 20, 4).unwrap();
3075
3076        // decimal256 to decimal256
3077        let input_type = DataType::Decimal256(20, 4);
3078        let output_type = DataType::Decimal256(20, 3);
3079        assert!(can_cast_types(&input_type, &output_type));
3080        generate_cast_test_case!(
3081            &array,
3082            Decimal256Array,
3083            &output_type,
3084            vec![
3085                Some(i256::from_i128(112345_i128)),
3086                Some(i256::from_i128(212346_i128)),
3087                Some(i256::from_i128(-312345_i128)),
3088                Some(i256::from_i128(-312346_i128)),
3089                None
3090            ]
3091        );
3092        // decimal256 to decimal128
3093        let input_type = DataType::Decimal256(20, 4);
3094        let output_type = DataType::Decimal128(20, 3);
3095        assert!(can_cast_types(&input_type, &output_type));
3096        generate_cast_test_case!(
3097            &array,
3098            Decimal128Array,
3099            &output_type,
3100            vec![
3101                Some(112345_i128),
3102                Some(212346_i128),
3103                Some(-312345_i128),
3104                Some(-312346_i128),
3105                None
3106            ]
3107        );
3108    }
3109
3110    #[test]
3111    fn test_cast_decimal32_to_decimal32() {
3112        // test changing precision
3113        let input_type = DataType::Decimal32(9, 3);
3114        let output_type = DataType::Decimal32(9, 4);
3115        assert!(can_cast_types(&input_type, &output_type));
3116        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3117        let array = create_decimal32_array(array, 9, 3).unwrap();
3118        generate_cast_test_case!(
3119            &array,
3120            Decimal32Array,
3121            &output_type,
3122            vec![
3123                Some(11234560_i32),
3124                Some(21234560_i32),
3125                Some(31234560_i32),
3126                None
3127            ]
3128        );
3129        // negative test
3130        let array = vec![Some(123456), None];
3131        let array = create_decimal32_array(array, 9, 0).unwrap();
3132        let result_safe = cast(&array, &DataType::Decimal32(2, 2));
3133        assert!(result_safe.is_ok());
3134        let options = CastOptions {
3135            safe: false,
3136            ..Default::default()
3137        };
3138
3139        let result_unsafe = cast_with_options(&array, &DataType::Decimal32(2, 2), &options);
3140        assert_eq!(
3141            "Invalid argument error: 123456.00 is too large to store in a Decimal32 of precision 2. Max is 0.99",
3142            result_unsafe.unwrap_err().to_string()
3143        );
3144    }
3145
3146    #[test]
3147    fn test_cast_decimal64_to_decimal64() {
3148        // test changing precision
3149        let input_type = DataType::Decimal64(17, 3);
3150        let output_type = DataType::Decimal64(17, 4);
3151        assert!(can_cast_types(&input_type, &output_type));
3152        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3153        let array = create_decimal64_array(array, 17, 3).unwrap();
3154        generate_cast_test_case!(
3155            &array,
3156            Decimal64Array,
3157            &output_type,
3158            vec![
3159                Some(11234560_i64),
3160                Some(21234560_i64),
3161                Some(31234560_i64),
3162                None
3163            ]
3164        );
3165        // negative test
3166        let array = vec![Some(123456), None];
3167        let array = create_decimal64_array(array, 9, 0).unwrap();
3168        let result_safe = cast(&array, &DataType::Decimal64(2, 2));
3169        assert!(result_safe.is_ok());
3170        let options = CastOptions {
3171            safe: false,
3172            ..Default::default()
3173        };
3174
3175        let result_unsafe = cast_with_options(&array, &DataType::Decimal64(2, 2), &options);
3176        assert_eq!(
3177            "Invalid argument error: 123456.00 is too large to store in a Decimal64 of precision 2. Max is 0.99",
3178            result_unsafe.unwrap_err().to_string()
3179        );
3180    }
3181
3182    #[test]
3183    fn test_cast_decimal128_to_decimal128() {
3184        // test changing precision
3185        let input_type = DataType::Decimal128(20, 3);
3186        let output_type = DataType::Decimal128(20, 4);
3187        assert!(can_cast_types(&input_type, &output_type));
3188        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3189        let array = create_decimal128_array(array, 20, 3).unwrap();
3190        generate_cast_test_case!(
3191            &array,
3192            Decimal128Array,
3193            &output_type,
3194            vec![
3195                Some(11234560_i128),
3196                Some(21234560_i128),
3197                Some(31234560_i128),
3198                None
3199            ]
3200        );
3201        // negative test
3202        let array = vec![Some(123456), None];
3203        let array = create_decimal128_array(array, 10, 0).unwrap();
3204        let result_safe = cast(&array, &DataType::Decimal128(2, 2));
3205        assert!(result_safe.is_ok());
3206        let options = CastOptions {
3207            safe: false,
3208            ..Default::default()
3209        };
3210
3211        let result_unsafe = cast_with_options(&array, &DataType::Decimal128(2, 2), &options);
3212        assert_eq!(
3213            "Invalid argument error: 123456.00 is too large to store in a Decimal128 of precision 2. Max is 0.99",
3214            result_unsafe.unwrap_err().to_string()
3215        );
3216    }
3217
3218    #[test]
3219    fn test_cast_decimal32_to_decimal32_dict() {
3220        let p = 9;
3221        let s = 3;
3222        let input_type = DataType::Decimal32(p, s);
3223        let output_type = DataType::Dictionary(
3224            Box::new(DataType::Int32),
3225            Box::new(DataType::Decimal32(p, s)),
3226        );
3227        assert!(can_cast_types(&input_type, &output_type));
3228        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3229        let array = create_decimal32_array(array, p, s).unwrap();
3230        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3231        assert_eq!(cast_array.data_type(), &output_type);
3232    }
3233
3234    #[test]
3235    fn test_cast_decimal64_to_decimal64_dict() {
3236        let p = 15;
3237        let s = 3;
3238        let input_type = DataType::Decimal64(p, s);
3239        let output_type = DataType::Dictionary(
3240            Box::new(DataType::Int32),
3241            Box::new(DataType::Decimal64(p, s)),
3242        );
3243        assert!(can_cast_types(&input_type, &output_type));
3244        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3245        let array = create_decimal64_array(array, p, s).unwrap();
3246        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3247        assert_eq!(cast_array.data_type(), &output_type);
3248    }
3249
3250    #[test]
3251    fn test_cast_decimal128_to_decimal128_dict() {
3252        let p = 20;
3253        let s = 3;
3254        let input_type = DataType::Decimal128(p, s);
3255        let output_type = DataType::Dictionary(
3256            Box::new(DataType::Int32),
3257            Box::new(DataType::Decimal128(p, s)),
3258        );
3259        assert!(can_cast_types(&input_type, &output_type));
3260        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3261        let array = create_decimal128_array(array, p, s).unwrap();
3262        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3263        assert_eq!(cast_array.data_type(), &output_type);
3264    }
3265
3266    #[test]
3267    fn test_cast_decimal256_to_decimal256_dict() {
3268        let p = 20;
3269        let s = 3;
3270        let input_type = DataType::Decimal256(p, s);
3271        let output_type = DataType::Dictionary(
3272            Box::new(DataType::Int32),
3273            Box::new(DataType::Decimal256(p, s)),
3274        );
3275        assert!(can_cast_types(&input_type, &output_type));
3276        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3277        let array = create_decimal128_array(array, p, s).unwrap();
3278        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3279        assert_eq!(cast_array.data_type(), &output_type);
3280    }
3281
3282    #[test]
3283    fn test_cast_decimal32_to_decimal32_overflow() {
3284        let input_type = DataType::Decimal32(9, 3);
3285        let output_type = DataType::Decimal32(9, 9);
3286        assert!(can_cast_types(&input_type, &output_type));
3287
3288        let array = vec![Some(i32::MAX)];
3289        let array = create_decimal32_array(array, 9, 3).unwrap();
3290        let result = cast_with_options(
3291            &array,
3292            &output_type,
3293            &CastOptions {
3294                safe: false,
3295                format_options: FormatOptions::default(),
3296            },
3297        );
3298        assert_eq!(
3299            "Cast error: Cannot cast to Decimal32(9, 9). Overflowing on 2147483647",
3300            result.unwrap_err().to_string()
3301        );
3302    }
3303
3304    #[test]
3305    fn test_cast_decimal32_to_decimal32_large_scale_reduction() {
3306        let array = vec![Some(-999999999), Some(0), Some(999999999), None];
3307        let array = create_decimal32_array(array, 9, 3).unwrap();
3308
3309        // Divide out all digits of precision -- rounding could still produce +/- 1
3310        let output_type = DataType::Decimal32(9, -6);
3311        assert!(can_cast_types(array.data_type(), &output_type));
3312        generate_cast_test_case!(
3313            &array,
3314            Decimal32Array,
3315            &output_type,
3316            vec![Some(-1), Some(0), Some(1), None]
3317        );
3318
3319        // Divide out more digits than we have precision -- all-zero result
3320        let output_type = DataType::Decimal32(9, -7);
3321        assert!(can_cast_types(array.data_type(), &output_type));
3322        generate_cast_test_case!(
3323            &array,
3324            Decimal32Array,
3325            &output_type,
3326            vec![Some(0), Some(0), Some(0), None]
3327        );
3328    }
3329
3330    #[test]
3331    fn test_cast_decimal64_to_decimal64_overflow() {
3332        let input_type = DataType::Decimal64(18, 3);
3333        let output_type = DataType::Decimal64(18, 18);
3334        assert!(can_cast_types(&input_type, &output_type));
3335
3336        let array = vec![Some(i64::MAX)];
3337        let array = create_decimal64_array(array, 18, 3).unwrap();
3338        let result = cast_with_options(
3339            &array,
3340            &output_type,
3341            &CastOptions {
3342                safe: false,
3343                format_options: FormatOptions::default(),
3344            },
3345        );
3346        assert_eq!(
3347            "Cast error: Cannot cast to Decimal64(18, 18). Overflowing on 9223372036854775807",
3348            result.unwrap_err().to_string()
3349        );
3350    }
3351
3352    #[test]
3353    fn test_cast_decimal64_to_decimal64_large_scale_reduction() {
3354        let array = vec![
3355            Some(-999999999999999999),
3356            Some(0),
3357            Some(999999999999999999),
3358            None,
3359        ];
3360        let array = create_decimal64_array(array, 18, 3).unwrap();
3361
3362        // Divide out all digits of precision -- rounding could still produce +/- 1
3363        let output_type = DataType::Decimal64(18, -15);
3364        assert!(can_cast_types(array.data_type(), &output_type));
3365        generate_cast_test_case!(
3366            &array,
3367            Decimal64Array,
3368            &output_type,
3369            vec![Some(-1), Some(0), Some(1), None]
3370        );
3371
3372        // Divide out more digits than we have precision -- all-zero result
3373        let output_type = DataType::Decimal64(18, -16);
3374        assert!(can_cast_types(array.data_type(), &output_type));
3375        generate_cast_test_case!(
3376            &array,
3377            Decimal64Array,
3378            &output_type,
3379            vec![Some(0), Some(0), Some(0), None]
3380        );
3381    }
3382
3383    #[test]
3384    fn test_cast_floating_to_decimals() {
3385        for output_type in [
3386            DataType::Decimal32(9, 3),
3387            DataType::Decimal64(9, 3),
3388            DataType::Decimal128(9, 3),
3389            DataType::Decimal256(9, 3),
3390        ] {
3391            let input_type = DataType::Float64;
3392            assert!(can_cast_types(&input_type, &output_type));
3393
3394            let array = vec![Some(1.1_f64)];
3395            let array = PrimitiveArray::<Float64Type>::from_iter(array);
3396            let result = cast_with_options(
3397                &array,
3398                &output_type,
3399                &CastOptions {
3400                    safe: false,
3401                    format_options: FormatOptions::default(),
3402                },
3403            );
3404            assert!(
3405                result.is_ok(),
3406                "Failed to cast to {output_type} with: {}",
3407                result.unwrap_err()
3408            );
3409        }
3410    }
3411
3412    #[test]
3413    fn test_cast_float16_to_decimals() {
3414        let array = Float16Array::from(vec![
3415            Some(f16::from_f32(1.25)),
3416            Some(f16::from_f32(-2.5)),
3417            Some(f16::from_f32(1.125)),
3418            Some(f16::from_f32(-1.125)),
3419            Some(f16::from_f32(0.0)),
3420            None,
3421        ]);
3422
3423        generate_cast_test_case!(
3424            &array,
3425            Decimal32Array,
3426            &DataType::Decimal32(9, 2),
3427            vec![
3428                Some(125_i32),
3429                Some(-250_i32),
3430                Some(113_i32),
3431                Some(-113_i32),
3432                Some(0_i32),
3433                None
3434            ]
3435        );
3436        generate_cast_test_case!(
3437            &array,
3438            Decimal64Array,
3439            &DataType::Decimal64(18, 2),
3440            vec![
3441                Some(125_i64),
3442                Some(-250_i64),
3443                Some(113_i64),
3444                Some(-113_i64),
3445                Some(0_i64),
3446                None
3447            ]
3448        );
3449        generate_cast_test_case!(
3450            &array,
3451            Decimal128Array,
3452            &DataType::Decimal128(38, 2),
3453            vec![
3454                Some(125_i128),
3455                Some(-250_i128),
3456                Some(113_i128),
3457                Some(-113_i128),
3458                Some(0_i128),
3459                None
3460            ]
3461        );
3462        generate_cast_test_case!(
3463            &array,
3464            Decimal256Array,
3465            &DataType::Decimal256(76, 2),
3466            vec![
3467                Some(i256::from_i128(125_i128)),
3468                Some(i256::from_i128(-250_i128)),
3469                Some(i256::from_i128(113_i128)),
3470                Some(i256::from_i128(-113_i128)),
3471                Some(i256::from_i128(0_i128)),
3472                None
3473            ]
3474        );
3475
3476        let array = Float16Array::from(vec![
3477            Some(f16::from_f32(1250.0)),
3478            Some(f16::from_f32(-1250.0)),
3479            Some(f16::from_f32(1249.0)),
3480            None,
3481        ]);
3482        generate_cast_test_case!(
3483            &array,
3484            Decimal128Array,
3485            &DataType::Decimal128(5, -2),
3486            vec![Some(13_i128), Some(-13_i128), Some(12_i128), None]
3487        );
3488    }
3489
3490    #[test]
3491    fn test_cast_decimal128_to_decimal128_overflow() {
3492        let input_type = DataType::Decimal128(38, 3);
3493        let output_type = DataType::Decimal128(38, 38);
3494        assert!(can_cast_types(&input_type, &output_type));
3495
3496        let array = vec![Some(i128::MAX)];
3497        let array = create_decimal128_array(array, 38, 3).unwrap();
3498        let result = cast_with_options(
3499            &array,
3500            &output_type,
3501            &CastOptions {
3502                safe: false,
3503                format_options: FormatOptions::default(),
3504            },
3505        );
3506        assert_eq!(
3507            "Cast error: Cannot cast to Decimal128(38, 38). Overflowing on 170141183460469231731687303715884105727",
3508            result.unwrap_err().to_string()
3509        );
3510    }
3511
3512    #[test]
3513    fn test_cast_decimal128_to_decimal256_overflow() {
3514        let input_type = DataType::Decimal128(38, 3);
3515        let output_type = DataType::Decimal256(76, 76);
3516        assert!(can_cast_types(&input_type, &output_type));
3517
3518        let array = vec![Some(i128::MAX)];
3519        let array = create_decimal128_array(array, 38, 3).unwrap();
3520        let result = cast_with_options(
3521            &array,
3522            &output_type,
3523            &CastOptions {
3524                safe: false,
3525                format_options: FormatOptions::default(),
3526            },
3527        );
3528        assert_eq!(
3529            "Cast error: Cannot cast to Decimal256(76, 76). Overflowing on 170141183460469231731687303715884105727",
3530            result.unwrap_err().to_string()
3531        );
3532    }
3533
3534    #[test]
3535    fn test_cast_decimal32_to_decimal256() {
3536        let input_type = DataType::Decimal32(8, 3);
3537        let output_type = DataType::Decimal256(20, 4);
3538        assert!(can_cast_types(&input_type, &output_type));
3539        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3540        let array = create_decimal32_array(array, 8, 3).unwrap();
3541        generate_cast_test_case!(
3542            &array,
3543            Decimal256Array,
3544            &output_type,
3545            vec![
3546                Some(i256::from_i128(11234560_i128)),
3547                Some(i256::from_i128(21234560_i128)),
3548                Some(i256::from_i128(31234560_i128)),
3549                None
3550            ]
3551        );
3552    }
3553    #[test]
3554    fn test_cast_decimal64_to_decimal256() {
3555        let input_type = DataType::Decimal64(12, 3);
3556        let output_type = DataType::Decimal256(20, 4);
3557        assert!(can_cast_types(&input_type, &output_type));
3558        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3559        let array = create_decimal64_array(array, 12, 3).unwrap();
3560        generate_cast_test_case!(
3561            &array,
3562            Decimal256Array,
3563            &output_type,
3564            vec![
3565                Some(i256::from_i128(11234560_i128)),
3566                Some(i256::from_i128(21234560_i128)),
3567                Some(i256::from_i128(31234560_i128)),
3568                None
3569            ]
3570        );
3571    }
3572    #[test]
3573    fn test_cast_decimal128_to_decimal256() {
3574        let input_type = DataType::Decimal128(20, 3);
3575        let output_type = DataType::Decimal256(20, 4);
3576        assert!(can_cast_types(&input_type, &output_type));
3577        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3578        let array = create_decimal128_array(array, 20, 3).unwrap();
3579        generate_cast_test_case!(
3580            &array,
3581            Decimal256Array,
3582            &output_type,
3583            vec![
3584                Some(i256::from_i128(11234560_i128)),
3585                Some(i256::from_i128(21234560_i128)),
3586                Some(i256::from_i128(31234560_i128)),
3587                None
3588            ]
3589        );
3590    }
3591
3592    #[test]
3593    fn test_cast_decimal256_to_decimal128_overflow() {
3594        let input_type = DataType::Decimal256(76, 5);
3595        let output_type = DataType::Decimal128(38, 7);
3596        assert!(can_cast_types(&input_type, &output_type));
3597        let array = vec![Some(i256::from_i128(i128::MAX))];
3598        let array = create_decimal256_array(array, 76, 5).unwrap();
3599        let result = cast_with_options(
3600            &array,
3601            &output_type,
3602            &CastOptions {
3603                safe: false,
3604                format_options: FormatOptions::default(),
3605            },
3606        );
3607        assert_eq!(
3608            "Cast error: Cannot cast to Decimal128(38, 7). Overflowing on 170141183460469231731687303715884105727",
3609            result.unwrap_err().to_string()
3610        );
3611    }
3612
3613    #[test]
3614    fn test_cast_decimal256_to_decimal256_overflow() {
3615        let input_type = DataType::Decimal256(76, 5);
3616        let output_type = DataType::Decimal256(76, 55);
3617        assert!(can_cast_types(&input_type, &output_type));
3618        let array = vec![Some(i256::from_i128(i128::MAX))];
3619        let array = create_decimal256_array(array, 76, 5).unwrap();
3620        let result = cast_with_options(
3621            &array,
3622            &output_type,
3623            &CastOptions {
3624                safe: false,
3625                format_options: FormatOptions::default(),
3626            },
3627        );
3628        assert_eq!(
3629            "Cast error: Cannot cast to Decimal256(76, 55). Overflowing on 170141183460469231731687303715884105727",
3630            result.unwrap_err().to_string()
3631        );
3632    }
3633
3634    #[test]
3635    fn test_cast_decimal256_to_decimal128() {
3636        let input_type = DataType::Decimal256(20, 3);
3637        let output_type = DataType::Decimal128(20, 4);
3638        assert!(can_cast_types(&input_type, &output_type));
3639        let array = vec![
3640            Some(i256::from_i128(1123456)),
3641            Some(i256::from_i128(2123456)),
3642            Some(i256::from_i128(3123456)),
3643            None,
3644        ];
3645        let array = create_decimal256_array(array, 20, 3).unwrap();
3646        generate_cast_test_case!(
3647            &array,
3648            Decimal128Array,
3649            &output_type,
3650            vec![
3651                Some(11234560_i128),
3652                Some(21234560_i128),
3653                Some(31234560_i128),
3654                None
3655            ]
3656        );
3657    }
3658
3659    #[test]
3660    fn test_cast_decimal256_to_decimal256() {
3661        let input_type = DataType::Decimal256(20, 3);
3662        let output_type = DataType::Decimal256(20, 4);
3663        assert!(can_cast_types(&input_type, &output_type));
3664        let array = vec![
3665            Some(i256::from_i128(1123456)),
3666            Some(i256::from_i128(2123456)),
3667            Some(i256::from_i128(3123456)),
3668            None,
3669        ];
3670        let array = create_decimal256_array(array, 20, 3).unwrap();
3671        generate_cast_test_case!(
3672            &array,
3673            Decimal256Array,
3674            &output_type,
3675            vec![
3676                Some(i256::from_i128(11234560_i128)),
3677                Some(i256::from_i128(21234560_i128)),
3678                Some(i256::from_i128(31234560_i128)),
3679                None
3680            ]
3681        );
3682    }
3683
3684    fn generate_decimal_to_numeric_cast_test_case<T>(array: &PrimitiveArray<T>)
3685    where
3686        T: ArrowPrimitiveType + DecimalType,
3687    {
3688        // u8
3689        generate_cast_test_case!(
3690            array,
3691            UInt8Array,
3692            &DataType::UInt8,
3693            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3694        );
3695        // u16
3696        generate_cast_test_case!(
3697            array,
3698            UInt16Array,
3699            &DataType::UInt16,
3700            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3701        );
3702        // u32
3703        generate_cast_test_case!(
3704            array,
3705            UInt32Array,
3706            &DataType::UInt32,
3707            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3708        );
3709        // u64
3710        generate_cast_test_case!(
3711            array,
3712            UInt64Array,
3713            &DataType::UInt64,
3714            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3715        );
3716        // i8
3717        generate_cast_test_case!(
3718            array,
3719            Int8Array,
3720            &DataType::Int8,
3721            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3722        );
3723        // i16
3724        generate_cast_test_case!(
3725            array,
3726            Int16Array,
3727            &DataType::Int16,
3728            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3729        );
3730        // i32
3731        generate_cast_test_case!(
3732            array,
3733            Int32Array,
3734            &DataType::Int32,
3735            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3736        );
3737        // i64
3738        generate_cast_test_case!(
3739            array,
3740            Int64Array,
3741            &DataType::Int64,
3742            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3743        );
3744        // f16
3745        generate_cast_test_case!(
3746            array,
3747            Float16Array,
3748            &DataType::Float16,
3749            vec![
3750                Some(f16::from_f32(1.25)),
3751                Some(f16::from_f32(2.25)),
3752                Some(f16::from_f32(3.25)),
3753                None,
3754                Some(f16::from_f32(5.25))
3755            ]
3756        );
3757        // f32
3758        generate_cast_test_case!(
3759            array,
3760            Float32Array,
3761            &DataType::Float32,
3762            vec![
3763                Some(1.25_f32),
3764                Some(2.25_f32),
3765                Some(3.25_f32),
3766                None,
3767                Some(5.25_f32)
3768            ]
3769        );
3770        // f64
3771        generate_cast_test_case!(
3772            array,
3773            Float64Array,
3774            &DataType::Float64,
3775            vec![
3776                Some(1.25_f64),
3777                Some(2.25_f64),
3778                Some(3.25_f64),
3779                None,
3780                Some(5.25_f64)
3781            ]
3782        );
3783    }
3784
3785    #[test]
3786    fn test_cast_decimal32_to_numeric() {
3787        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3788        let array = create_decimal32_array(value_array, 8, 2).unwrap();
3789
3790        generate_decimal_to_numeric_cast_test_case(&array);
3791    }
3792
3793    #[test]
3794    fn test_cast_decimal64_to_numeric() {
3795        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3796        let array = create_decimal64_array(value_array, 8, 2).unwrap();
3797
3798        generate_decimal_to_numeric_cast_test_case(&array);
3799    }
3800
3801    #[test]
3802    fn test_cast_decimal128_to_numeric() {
3803        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3804        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3805
3806        generate_decimal_to_numeric_cast_test_case(&array);
3807
3808        // overflow test: out of range of max u8
3809        let value_array: Vec<Option<i128>> = vec![Some(51300)];
3810        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3811        let casted_array = cast_with_options(
3812            &array,
3813            &DataType::UInt8,
3814            &CastOptions {
3815                safe: false,
3816                format_options: FormatOptions::default(),
3817            },
3818        );
3819        assert_eq!(
3820            "Cast error: value of 513 is out of range UInt8".to_string(),
3821            casted_array.unwrap_err().to_string()
3822        );
3823
3824        let casted_array = cast_with_options(
3825            &array,
3826            &DataType::UInt8,
3827            &CastOptions {
3828                safe: true,
3829                format_options: FormatOptions::default(),
3830            },
3831        );
3832        assert!(casted_array.is_ok());
3833        assert!(casted_array.unwrap().is_null(0));
3834
3835        // overflow test: out of range of max i8
3836        let value_array: Vec<Option<i128>> = vec![Some(24400)];
3837        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3838        let casted_array = cast_with_options(
3839            &array,
3840            &DataType::Int8,
3841            &CastOptions {
3842                safe: false,
3843                format_options: FormatOptions::default(),
3844            },
3845        );
3846        assert_eq!(
3847            "Cast error: value of 244 is out of range Int8".to_string(),
3848            casted_array.unwrap_err().to_string()
3849        );
3850
3851        let casted_array = cast_with_options(
3852            &array,
3853            &DataType::Int8,
3854            &CastOptions {
3855                safe: true,
3856                format_options: FormatOptions::default(),
3857            },
3858        );
3859        assert!(casted_array.is_ok());
3860        assert!(casted_array.unwrap().is_null(0));
3861
3862        // loss the precision: convert decimal to f32、f64
3863        // f32
3864        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3865        let value_array: Vec<Option<i128>> = vec![
3866            Some(125),
3867            Some(225),
3868            Some(325),
3869            None,
3870            Some(525),
3871            Some(112345678),
3872            Some(112345679),
3873        ];
3874        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3875        generate_cast_test_case!(
3876            &array,
3877            Float32Array,
3878            &DataType::Float32,
3879            vec![
3880                Some(1.25_f32),
3881                Some(2.25_f32),
3882                Some(3.25_f32),
3883                None,
3884                Some(5.25_f32),
3885                Some(1_123_456.7_f32),
3886                Some(1_123_456.7_f32)
3887            ]
3888        );
3889
3890        // f64
3891        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3892        let value_array: Vec<Option<i128>> = vec![
3893            Some(125),
3894            Some(225),
3895            Some(325),
3896            None,
3897            Some(525),
3898            Some(112345678901234568),
3899            Some(112345678901234560),
3900        ];
3901        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3902        generate_cast_test_case!(
3903            &array,
3904            Float64Array,
3905            &DataType::Float64,
3906            vec![
3907                Some(1.25_f64),
3908                Some(2.25_f64),
3909                Some(3.25_f64),
3910                None,
3911                Some(5.25_f64),
3912                Some(1_123_456_789_012_345.6_f64),
3913                Some(1_123_456_789_012_345.6_f64),
3914            ]
3915        );
3916    }
3917
3918    #[test]
3919    fn test_cast_decimal256_to_numeric() {
3920        let value_array: Vec<Option<i256>> = vec![
3921            Some(i256::from_i128(125)),
3922            Some(i256::from_i128(225)),
3923            Some(i256::from_i128(325)),
3924            None,
3925            Some(i256::from_i128(525)),
3926        ];
3927        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3928        // u8
3929        generate_cast_test_case!(
3930            &array,
3931            UInt8Array,
3932            &DataType::UInt8,
3933            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3934        );
3935        // u16
3936        generate_cast_test_case!(
3937            &array,
3938            UInt16Array,
3939            &DataType::UInt16,
3940            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3941        );
3942        // u32
3943        generate_cast_test_case!(
3944            &array,
3945            UInt32Array,
3946            &DataType::UInt32,
3947            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3948        );
3949        // u64
3950        generate_cast_test_case!(
3951            &array,
3952            UInt64Array,
3953            &DataType::UInt64,
3954            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3955        );
3956        // i8
3957        generate_cast_test_case!(
3958            &array,
3959            Int8Array,
3960            &DataType::Int8,
3961            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3962        );
3963        // i16
3964        generate_cast_test_case!(
3965            &array,
3966            Int16Array,
3967            &DataType::Int16,
3968            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3969        );
3970        // i32
3971        generate_cast_test_case!(
3972            &array,
3973            Int32Array,
3974            &DataType::Int32,
3975            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3976        );
3977        // i64
3978        generate_cast_test_case!(
3979            &array,
3980            Int64Array,
3981            &DataType::Int64,
3982            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3983        );
3984        // f16
3985        generate_cast_test_case!(
3986            &array,
3987            Float16Array,
3988            &DataType::Float16,
3989            vec![
3990                Some(f16::from_f32(1.25)),
3991                Some(f16::from_f32(2.25)),
3992                Some(f16::from_f32(3.25)),
3993                None,
3994                Some(f16::from_f32(5.25))
3995            ]
3996        );
3997        // f32
3998        generate_cast_test_case!(
3999            &array,
4000            Float32Array,
4001            &DataType::Float32,
4002            vec![
4003                Some(1.25_f32),
4004                Some(2.25_f32),
4005                Some(3.25_f32),
4006                None,
4007                Some(5.25_f32)
4008            ]
4009        );
4010        // f64
4011        generate_cast_test_case!(
4012            &array,
4013            Float64Array,
4014            &DataType::Float64,
4015            vec![
4016                Some(1.25_f64),
4017                Some(2.25_f64),
4018                Some(3.25_f64),
4019                None,
4020                Some(5.25_f64)
4021            ]
4022        );
4023
4024        // overflow test: out of range of max i8
4025        let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128(24400))];
4026        let array = create_decimal256_array(value_array, 38, 2).unwrap();
4027        let casted_array = cast_with_options(
4028            &array,
4029            &DataType::Int8,
4030            &CastOptions {
4031                safe: false,
4032                format_options: FormatOptions::default(),
4033            },
4034        );
4035        assert_eq!(
4036            "Cast error: value of 244 is out of range Int8".to_string(),
4037            casted_array.unwrap_err().to_string()
4038        );
4039
4040        let casted_array = cast_with_options(
4041            &array,
4042            &DataType::Int8,
4043            &CastOptions {
4044                safe: true,
4045                format_options: FormatOptions::default(),
4046            },
4047        );
4048        assert!(casted_array.is_ok());
4049        assert!(casted_array.unwrap().is_null(0));
4050
4051        // loss the precision: convert decimal to f32、f64
4052        // f32
4053        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
4054        let value_array: Vec<Option<i256>> = vec![
4055            Some(i256::from_i128(125)),
4056            Some(i256::from_i128(225)),
4057            Some(i256::from_i128(325)),
4058            None,
4059            Some(i256::from_i128(525)),
4060            Some(i256::from_i128(112345678)),
4061            Some(i256::from_i128(112345679)),
4062        ];
4063        let array = create_decimal256_array(value_array, 76, 2).unwrap();
4064        generate_cast_test_case!(
4065            &array,
4066            Float32Array,
4067            &DataType::Float32,
4068            vec![
4069                Some(1.25_f32),
4070                Some(2.25_f32),
4071                Some(3.25_f32),
4072                None,
4073                Some(5.25_f32),
4074                Some(1_123_456.7_f32),
4075                Some(1_123_456.7_f32)
4076            ]
4077        );
4078
4079        // f64
4080        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
4081        let value_array: Vec<Option<i256>> = vec![
4082            Some(i256::from_i128(125)),
4083            Some(i256::from_i128(225)),
4084            Some(i256::from_i128(325)),
4085            None,
4086            Some(i256::from_i128(525)),
4087            Some(i256::from_i128(112345678901234568)),
4088            Some(i256::from_i128(112345678901234560)),
4089        ];
4090        let array = create_decimal256_array(value_array, 76, 2).unwrap();
4091        generate_cast_test_case!(
4092            &array,
4093            Float64Array,
4094            &DataType::Float64,
4095            vec![
4096                Some(1.25_f64),
4097                Some(2.25_f64),
4098                Some(3.25_f64),
4099                None,
4100                Some(5.25_f64),
4101                Some(1_123_456_789_012_345.6_f64),
4102                Some(1_123_456_789_012_345.6_f64),
4103            ]
4104        );
4105    }
4106
4107    #[test]
4108    fn test_cast_decimal128_to_float16_overflow() {
4109        let array = create_decimal128_array(
4110            vec![
4111                Some(6_550_400_i128),
4112                Some(100_000_000_i128),
4113                Some(-100_000_000_i128),
4114                None,
4115            ],
4116            10,
4117            2,
4118        )
4119        .unwrap();
4120
4121        generate_cast_test_case!(
4122            &array,
4123            Float16Array,
4124            &DataType::Float16,
4125            vec![
4126                Some(f16::from_f64(65504.0)),
4127                Some(f16::INFINITY),
4128                Some(f16::NEG_INFINITY),
4129                None
4130            ]
4131        );
4132    }
4133
4134    #[test]
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    fn test_cast_decimal_to_numeric_negative_scale() {
4163        let value_array: Vec<Option<i256>> = vec![
4164            Some(i256::from_i128(125)),
4165            Some(i256::from_i128(225)),
4166            Some(i256::from_i128(325)),
4167            None,
4168            Some(i256::from_i128(525)),
4169        ];
4170        let array = create_decimal256_array(value_array, 38, -1).unwrap();
4171
4172        generate_cast_test_case!(
4173            &array,
4174            Int64Array,
4175            &DataType::Int64,
4176            vec![Some(1_250), Some(2_250), Some(3_250), None, Some(5_250)]
4177        );
4178
4179        let value_array: Vec<Option<i128>> = vec![Some(12), Some(-12), None];
4180        let array = create_decimal128_array(value_array, 10, -2).unwrap();
4181        generate_cast_test_case!(
4182            &array,
4183            Float16Array,
4184            &DataType::Float16,
4185            vec![
4186                Some(f16::from_f32(1200.0)),
4187                Some(f16::from_f32(-1200.0)),
4188                None
4189            ]
4190        );
4191
4192        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4193        let array = create_decimal32_array(value_array, 8, -2).unwrap();
4194        generate_cast_test_case!(
4195            &array,
4196            Int64Array,
4197            &DataType::Int64,
4198            vec![Some(12_500), Some(22_500), Some(32_500), None, Some(52_500)]
4199        );
4200
4201        let value_array: Vec<Option<i32>> = vec![Some(2), Some(1), None];
4202        let array = create_decimal32_array(value_array, 9, -9).unwrap();
4203        generate_cast_test_case!(
4204            &array,
4205            Int64Array,
4206            &DataType::Int64,
4207            vec![Some(2_000_000_000), Some(1_000_000_000), None]
4208        );
4209
4210        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4211        let array = create_decimal64_array(value_array, 18, -3).unwrap();
4212        generate_cast_test_case!(
4213            &array,
4214            Int64Array,
4215            &DataType::Int64,
4216            vec![
4217                Some(125_000),
4218                Some(225_000),
4219                Some(325_000),
4220                None,
4221                Some(525_000)
4222            ]
4223        );
4224
4225        let value_array: Vec<Option<i64>> = vec![Some(12), Some(34), None];
4226        let array = create_decimal64_array(value_array, 18, -10).unwrap();
4227        generate_cast_test_case!(
4228            &array,
4229            Int64Array,
4230            &DataType::Int64,
4231            vec![Some(120_000_000_000), Some(340_000_000_000), None]
4232        );
4233
4234        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4235        let array = create_decimal128_array(value_array, 38, -4).unwrap();
4236        generate_cast_test_case!(
4237            &array,
4238            Int64Array,
4239            &DataType::Int64,
4240            vec![
4241                Some(1_250_000),
4242                Some(2_250_000),
4243                Some(3_250_000),
4244                None,
4245                Some(5_250_000)
4246            ]
4247        );
4248
4249        let value_array: Vec<Option<i128>> = vec![Some(9), Some(1), None];
4250        let array = create_decimal128_array(value_array, 38, -18).unwrap();
4251        generate_cast_test_case!(
4252            &array,
4253            Int64Array,
4254            &DataType::Int64,
4255            vec![
4256                Some(9_000_000_000_000_000_000),
4257                Some(1_000_000_000_000_000_000),
4258                None
4259            ]
4260        );
4261
4262        let array = create_decimal32_array(vec![Some(999_999_999)], 9, -1).unwrap();
4263        let casted_array = cast_with_options(
4264            &array,
4265            &DataType::Int64,
4266            &CastOptions {
4267                safe: false,
4268                format_options: FormatOptions::default(),
4269            },
4270        );
4271        assert_eq!(
4272            "Arithmetic overflow: Overflow happened on: 999999999 * 10".to_string(),
4273            casted_array.unwrap_err().to_string()
4274        );
4275
4276        let casted_array = cast_with_options(
4277            &array,
4278            &DataType::Int64,
4279            &CastOptions {
4280                safe: true,
4281                format_options: FormatOptions::default(),
4282            },
4283        );
4284        assert!(casted_array.is_ok());
4285        assert!(casted_array.unwrap().is_null(0));
4286
4287        let array = create_decimal64_array(vec![Some(13)], 18, -1).unwrap();
4288        let casted_array = cast_with_options(
4289            &array,
4290            &DataType::Int8,
4291            &CastOptions {
4292                safe: false,
4293                format_options: FormatOptions::default(),
4294            },
4295        );
4296        assert_eq!(
4297            "Cast error: value of 130 is out of range Int8".to_string(),
4298            casted_array.unwrap_err().to_string()
4299        );
4300
4301        let casted_array = cast_with_options(
4302            &array,
4303            &DataType::Int8,
4304            &CastOptions {
4305                safe: true,
4306                format_options: FormatOptions::default(),
4307            },
4308        );
4309        assert!(casted_array.is_ok());
4310        assert!(casted_array.unwrap().is_null(0));
4311    }
4312
4313    #[test]
4314    fn test_cast_numeric_to_decimal128() {
4315        let decimal_type = DataType::Decimal128(38, 6);
4316        // u8, u16, u32, u64
4317        let input_datas = vec![
4318            Arc::new(UInt8Array::from(vec![
4319                Some(1),
4320                Some(2),
4321                Some(3),
4322                None,
4323                Some(5),
4324            ])) as ArrayRef, // u8
4325            Arc::new(UInt16Array::from(vec![
4326                Some(1),
4327                Some(2),
4328                Some(3),
4329                None,
4330                Some(5),
4331            ])) as ArrayRef, // u16
4332            Arc::new(UInt32Array::from(vec![
4333                Some(1),
4334                Some(2),
4335                Some(3),
4336                None,
4337                Some(5),
4338            ])) as ArrayRef, // u32
4339            Arc::new(UInt64Array::from(vec![
4340                Some(1),
4341                Some(2),
4342                Some(3),
4343                None,
4344                Some(5),
4345            ])) as ArrayRef, // u64
4346        ];
4347
4348        for array in input_datas {
4349            generate_cast_test_case!(
4350                &array,
4351                Decimal128Array,
4352                &decimal_type,
4353                vec![
4354                    Some(1000000_i128),
4355                    Some(2000000_i128),
4356                    Some(3000000_i128),
4357                    None,
4358                    Some(5000000_i128)
4359                ]
4360            );
4361        }
4362
4363        // i8, i16, i32, i64
4364        let input_datas = vec![
4365            Arc::new(Int8Array::from(vec![
4366                Some(1),
4367                Some(2),
4368                Some(3),
4369                None,
4370                Some(5),
4371            ])) as ArrayRef, // i8
4372            Arc::new(Int16Array::from(vec![
4373                Some(1),
4374                Some(2),
4375                Some(3),
4376                None,
4377                Some(5),
4378            ])) as ArrayRef, // i16
4379            Arc::new(Int32Array::from(vec![
4380                Some(1),
4381                Some(2),
4382                Some(3),
4383                None,
4384                Some(5),
4385            ])) as ArrayRef, // i32
4386            Arc::new(Int64Array::from(vec![
4387                Some(1),
4388                Some(2),
4389                Some(3),
4390                None,
4391                Some(5),
4392            ])) as ArrayRef, // i64
4393        ];
4394        for array in input_datas {
4395            generate_cast_test_case!(
4396                &array,
4397                Decimal128Array,
4398                &decimal_type,
4399                vec![
4400                    Some(1000000_i128),
4401                    Some(2000000_i128),
4402                    Some(3000000_i128),
4403                    None,
4404                    Some(5000000_i128)
4405                ]
4406            );
4407        }
4408
4409        // test u8 to decimal type with overflow the result type
4410        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4411        let array = UInt8Array::from(vec![1, 2, 3, 4, 100]);
4412        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4413        assert!(casted_array.is_ok());
4414        let array = casted_array.unwrap();
4415        let array: &Decimal128Array = array.as_primitive();
4416        assert!(array.is_null(4));
4417
4418        // test i8 to decimal type with overflow the result type
4419        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4420        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4421        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4422        assert!(casted_array.is_ok());
4423        let array = casted_array.unwrap();
4424        let array: &Decimal128Array = array.as_primitive();
4425        assert!(array.is_null(4));
4426
4427        // test f32 to decimal type
4428        let array = Float32Array::from(vec![
4429            Some(1.1),
4430            Some(2.2),
4431            Some(4.4),
4432            None,
4433            Some(1.123_456_4), // round down
4434            Some(1.123_456_7), // round up
4435        ]);
4436        let array = Arc::new(array) as ArrayRef;
4437        generate_cast_test_case!(
4438            &array,
4439            Decimal128Array,
4440            &decimal_type,
4441            vec![
4442                Some(1100000_i128),
4443                Some(2200000_i128),
4444                Some(4400000_i128),
4445                None,
4446                Some(1123456_i128), // round down
4447                Some(1123457_i128), // round up
4448            ]
4449        );
4450
4451        // test f64 to decimal type
4452        let array = Float64Array::from(vec![
4453            Some(1.1),
4454            Some(2.2),
4455            Some(4.4),
4456            None,
4457            Some(1.123_456_489_123_4),     // round up
4458            Some(1.123_456_789_123_4),     // round up
4459            Some(1.123_456_489_012_345_6), // round down
4460            Some(1.123_456_789_012_345_6), // round up
4461        ]);
4462        generate_cast_test_case!(
4463            &array,
4464            Decimal128Array,
4465            &decimal_type,
4466            vec![
4467                Some(1100000_i128),
4468                Some(2200000_i128),
4469                Some(4400000_i128),
4470                None,
4471                Some(1123456_i128), // round down
4472                Some(1123457_i128), // round up
4473                Some(1123456_i128), // round down
4474                Some(1123457_i128), // round up
4475            ]
4476        );
4477    }
4478
4479    #[test]
4480    fn test_cast_numeric_to_decimal256() {
4481        let decimal_type = DataType::Decimal256(76, 6);
4482        // u8, u16, u32, u64
4483        let input_datas = vec![
4484            Arc::new(UInt8Array::from(vec![
4485                Some(1),
4486                Some(2),
4487                Some(3),
4488                None,
4489                Some(5),
4490            ])) as ArrayRef, // u8
4491            Arc::new(UInt16Array::from(vec![
4492                Some(1),
4493                Some(2),
4494                Some(3),
4495                None,
4496                Some(5),
4497            ])) as ArrayRef, // u16
4498            Arc::new(UInt32Array::from(vec![
4499                Some(1),
4500                Some(2),
4501                Some(3),
4502                None,
4503                Some(5),
4504            ])) as ArrayRef, // u32
4505            Arc::new(UInt64Array::from(vec![
4506                Some(1),
4507                Some(2),
4508                Some(3),
4509                None,
4510                Some(5),
4511            ])) as ArrayRef, // u64
4512        ];
4513
4514        for array in input_datas {
4515            generate_cast_test_case!(
4516                &array,
4517                Decimal256Array,
4518                &decimal_type,
4519                vec![
4520                    Some(i256::from_i128(1000000_i128)),
4521                    Some(i256::from_i128(2000000_i128)),
4522                    Some(i256::from_i128(3000000_i128)),
4523                    None,
4524                    Some(i256::from_i128(5000000_i128))
4525                ]
4526            );
4527        }
4528
4529        // i8, i16, i32, i64
4530        let input_datas = vec![
4531            Arc::new(Int8Array::from(vec![
4532                Some(1),
4533                Some(2),
4534                Some(3),
4535                None,
4536                Some(5),
4537            ])) as ArrayRef, // i8
4538            Arc::new(Int16Array::from(vec![
4539                Some(1),
4540                Some(2),
4541                Some(3),
4542                None,
4543                Some(5),
4544            ])) as ArrayRef, // i16
4545            Arc::new(Int32Array::from(vec![
4546                Some(1),
4547                Some(2),
4548                Some(3),
4549                None,
4550                Some(5),
4551            ])) as ArrayRef, // i32
4552            Arc::new(Int64Array::from(vec![
4553                Some(1),
4554                Some(2),
4555                Some(3),
4556                None,
4557                Some(5),
4558            ])) as ArrayRef, // i64
4559        ];
4560        for array in input_datas {
4561            generate_cast_test_case!(
4562                &array,
4563                Decimal256Array,
4564                &decimal_type,
4565                vec![
4566                    Some(i256::from_i128(1000000_i128)),
4567                    Some(i256::from_i128(2000000_i128)),
4568                    Some(i256::from_i128(3000000_i128)),
4569                    None,
4570                    Some(i256::from_i128(5000000_i128))
4571                ]
4572            );
4573        }
4574
4575        // test i8 to decimal type with overflow the result type
4576        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4577        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4578        let array = Arc::new(array) as ArrayRef;
4579        let casted_array = cast(&array, &DataType::Decimal256(3, 1));
4580        assert!(casted_array.is_ok());
4581        let array = casted_array.unwrap();
4582        let array: &Decimal256Array = array.as_primitive();
4583        assert!(array.is_null(4));
4584
4585        // test f32 to decimal type
4586        let array = Float32Array::from(vec![
4587            Some(1.1),
4588            Some(2.2),
4589            Some(4.4),
4590            None,
4591            Some(1.123_456_4), // round down
4592            Some(1.123_456_7), // round up
4593        ]);
4594        generate_cast_test_case!(
4595            &array,
4596            Decimal256Array,
4597            &decimal_type,
4598            vec![
4599                Some(i256::from_i128(1100000_i128)),
4600                Some(i256::from_i128(2200000_i128)),
4601                Some(i256::from_i128(4400000_i128)),
4602                None,
4603                Some(i256::from_i128(1123456_i128)), // round down
4604                Some(i256::from_i128(1123457_i128)), // round up
4605            ]
4606        );
4607
4608        // test f64 to decimal type
4609        let array = Float64Array::from(vec![
4610            Some(1.1),
4611            Some(2.2),
4612            Some(4.4),
4613            None,
4614            Some(1.123_456_489_123_4),     // round down
4615            Some(1.123_456_789_123_4),     // round up
4616            Some(1.123_456_489_012_345_6), // round down
4617            Some(1.123_456_789_012_345_6), // round up
4618        ]);
4619        generate_cast_test_case!(
4620            &array,
4621            Decimal256Array,
4622            &decimal_type,
4623            vec![
4624                Some(i256::from_i128(1100000_i128)),
4625                Some(i256::from_i128(2200000_i128)),
4626                Some(i256::from_i128(4400000_i128)),
4627                None,
4628                Some(i256::from_i128(1123456_i128)), // round down
4629                Some(i256::from_i128(1123457_i128)), // round up
4630                Some(i256::from_i128(1123456_i128)), // round down
4631                Some(i256::from_i128(1123457_i128)), // round up
4632            ]
4633        );
4634    }
4635
4636    #[test]
4637    fn test_cast_i32_to_f64() {
4638        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4639        let b = cast(&array, &DataType::Float64).unwrap();
4640        let c = b.as_primitive::<Float64Type>();
4641        assert_eq!(5.0, c.value(0));
4642        assert_eq!(6.0, c.value(1));
4643        assert_eq!(7.0, c.value(2));
4644        assert_eq!(8.0, c.value(3));
4645        assert_eq!(9.0, c.value(4));
4646    }
4647
4648    #[test]
4649    fn test_cast_i32_to_u8() {
4650        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4651        let b = cast(&array, &DataType::UInt8).unwrap();
4652        let c = b.as_primitive::<UInt8Type>();
4653        assert!(!c.is_valid(0));
4654        assert_eq!(6, c.value(1));
4655        assert!(!c.is_valid(2));
4656        assert_eq!(8, c.value(3));
4657        // overflows return None
4658        assert!(!c.is_valid(4));
4659    }
4660
4661    #[test]
4662    #[should_panic(expected = "Can't cast value -5 to type UInt8")]
4663    fn test_cast_int32_to_u8_with_error() {
4664        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4665        // overflow with the error
4666        let cast_option = CastOptions {
4667            safe: false,
4668            format_options: FormatOptions::default(),
4669        };
4670        let result = cast_with_options(&array, &DataType::UInt8, &cast_option);
4671        assert!(result.is_err());
4672        result.unwrap();
4673    }
4674
4675    #[test]
4676    fn test_cast_i32_to_u8_sliced() {
4677        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4678        assert_eq!(0, array.offset());
4679        let array = array.slice(2, 3);
4680        let b = cast(&array, &DataType::UInt8).unwrap();
4681        assert_eq!(3, b.len());
4682        let c = b.as_primitive::<UInt8Type>();
4683        assert!(!c.is_valid(0));
4684        assert_eq!(8, c.value(1));
4685        // overflows return None
4686        assert!(!c.is_valid(2));
4687    }
4688
4689    #[test]
4690    fn test_cast_i32_to_i32() {
4691        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4692        let b = cast(&array, &DataType::Int32).unwrap();
4693        let c = b.as_primitive::<Int32Type>();
4694        assert_eq!(5, c.value(0));
4695        assert_eq!(6, c.value(1));
4696        assert_eq!(7, c.value(2));
4697        assert_eq!(8, c.value(3));
4698        assert_eq!(9, c.value(4));
4699    }
4700
4701    #[test]
4702    fn test_cast_i32_to_list_i32() {
4703        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4704        let b = cast(
4705            &array,
4706            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4707        )
4708        .unwrap();
4709        assert_eq!(5, b.len());
4710        let arr = b.as_list::<i32>();
4711        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4712        assert_eq!(1, arr.value_length(0));
4713        assert_eq!(1, arr.value_length(1));
4714        assert_eq!(1, arr.value_length(2));
4715        assert_eq!(1, arr.value_length(3));
4716        assert_eq!(1, arr.value_length(4));
4717        let c = arr.values().as_primitive::<Int32Type>();
4718        assert_eq!(5, c.value(0));
4719        assert_eq!(6, c.value(1));
4720        assert_eq!(7, c.value(2));
4721        assert_eq!(8, c.value(3));
4722        assert_eq!(9, c.value(4));
4723    }
4724
4725    #[test]
4726    fn test_cast_i32_to_list_i32_nullable() {
4727        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), Some(9)]);
4728        let b = cast(
4729            &array,
4730            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4731        )
4732        .unwrap();
4733        assert_eq!(5, b.len());
4734        assert_eq!(0, b.null_count());
4735        let arr = b.as_list::<i32>();
4736        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4737        assert_eq!(1, arr.value_length(0));
4738        assert_eq!(1, arr.value_length(1));
4739        assert_eq!(1, arr.value_length(2));
4740        assert_eq!(1, arr.value_length(3));
4741        assert_eq!(1, arr.value_length(4));
4742
4743        let c = arr.values().as_primitive::<Int32Type>();
4744        assert_eq!(1, c.null_count());
4745        assert_eq!(5, c.value(0));
4746        assert!(!c.is_valid(1));
4747        assert_eq!(7, c.value(2));
4748        assert_eq!(8, c.value(3));
4749        assert_eq!(9, c.value(4));
4750    }
4751
4752    #[test]
4753    fn test_cast_i32_to_list_f64_nullable_sliced() {
4754        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), None, Some(10)]);
4755        let array = array.slice(2, 4);
4756        let b = cast(
4757            &array,
4758            &DataType::List(Arc::new(Field::new_list_field(DataType::Float64, true))),
4759        )
4760        .unwrap();
4761        assert_eq!(4, b.len());
4762        assert_eq!(0, b.null_count());
4763        let arr = b.as_list::<i32>();
4764        assert_eq!(&[0, 1, 2, 3, 4], arr.value_offsets());
4765        assert_eq!(1, arr.value_length(0));
4766        assert_eq!(1, arr.value_length(1));
4767        assert_eq!(1, arr.value_length(2));
4768        assert_eq!(1, arr.value_length(3));
4769        let c = arr.values().as_primitive::<Float64Type>();
4770        assert_eq!(1, c.null_count());
4771        assert_eq!(7.0, c.value(0));
4772        assert_eq!(8.0, c.value(1));
4773        assert!(!c.is_valid(2));
4774        assert_eq!(10.0, c.value(3));
4775    }
4776
4777    #[test]
4778    fn test_cast_int_to_utf8view() {
4779        let inputs = vec![
4780            Arc::new(Int8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4781            Arc::new(Int16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4782            Arc::new(Int32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4783            Arc::new(Int64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4784            Arc::new(UInt8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4785            Arc::new(UInt16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4786            Arc::new(UInt32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4787            Arc::new(UInt64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4788        ];
4789        let expected: ArrayRef = Arc::new(StringViewArray::from(vec![
4790            None,
4791            Some("8"),
4792            Some("9"),
4793            Some("10"),
4794        ]));
4795
4796        for array in inputs {
4797            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4798            let arr = cast(&array, &DataType::Utf8View).unwrap();
4799            assert_eq!(expected.as_ref(), arr.as_ref());
4800        }
4801    }
4802
4803    #[test]
4804    fn test_cast_float_to_utf8view() {
4805        let inputs = vec![
4806            Arc::new(Float16Array::from(vec![
4807                Some(f16::from_f64(1.5)),
4808                Some(f16::from_f64(2.5)),
4809                None,
4810            ])) as ArrayRef,
4811            Arc::new(Float32Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4812            Arc::new(Float64Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4813        ];
4814
4815        let expected: ArrayRef =
4816            Arc::new(StringViewArray::from(vec![Some("1.5"), Some("2.5"), None]));
4817
4818        for array in inputs {
4819            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4820            let arr = cast(&array, &DataType::Utf8View).unwrap();
4821            assert_eq!(expected.as_ref(), arr.as_ref());
4822        }
4823    }
4824
4825    #[test]
4826    fn test_cast_utf8_to_i32() {
4827        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4828        let b = cast(&array, &DataType::Int32).unwrap();
4829        let c = b.as_primitive::<Int32Type>();
4830        assert_eq!(5, c.value(0));
4831        assert_eq!(6, c.value(1));
4832        assert!(!c.is_valid(2));
4833        assert_eq!(8, c.value(3));
4834        assert!(!c.is_valid(4));
4835    }
4836
4837    #[test]
4838    fn test_cast_utf8view_to_i32() {
4839        let array = StringViewArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4840        let b = cast(&array, &DataType::Int32).unwrap();
4841        let c = b.as_primitive::<Int32Type>();
4842        assert_eq!(5, c.value(0));
4843        assert_eq!(6, c.value(1));
4844        assert!(!c.is_valid(2));
4845        assert_eq!(8, c.value(3));
4846        assert!(!c.is_valid(4));
4847    }
4848
4849    #[test]
4850    fn test_cast_utf8view_to_f32() {
4851        let array = StringViewArray::from(vec!["3", "4.56", "seven", "8.9"]);
4852        let b = cast(&array, &DataType::Float32).unwrap();
4853        let c = b.as_primitive::<Float32Type>();
4854        assert_eq!(3.0, c.value(0));
4855        assert_eq!(4.56, c.value(1));
4856        assert!(!c.is_valid(2));
4857        assert_eq!(8.9, c.value(3));
4858    }
4859
4860    #[test]
4861    fn test_cast_string_to_f16() {
4862        let arrays = [
4863            Arc::new(StringViewArray::from(vec!["3", "4.56", "seven", "8.9"])) as ArrayRef,
4864            Arc::new(StringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4865            Arc::new(LargeStringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4866        ];
4867        for array in arrays {
4868            let b = cast(&array, &DataType::Float16).unwrap();
4869            let c = b.as_primitive::<Float16Type>();
4870            assert_eq!(half::f16::from_f32(3.0), c.value(0));
4871            assert_eq!(half::f16::from_f32(4.56), c.value(1));
4872            assert!(!c.is_valid(2));
4873            assert_eq!(half::f16::from_f32(8.9), c.value(3));
4874        }
4875    }
4876
4877    #[test]
4878    fn test_cast_utf8view_to_decimal128() {
4879        let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
4880        let arr = Arc::new(array) as ArrayRef;
4881        generate_cast_test_case!(
4882            &arr,
4883            Decimal128Array,
4884            &DataType::Decimal128(4, 2),
4885            vec![None, Some(400_i128), Some(560_i128), Some(789_i128)]
4886        );
4887    }
4888
4889    #[test]
4890    fn test_cast_with_options_utf8_to_i32() {
4891        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4892        let result = cast_with_options(
4893            &array,
4894            &DataType::Int32,
4895            &CastOptions {
4896                safe: false,
4897                format_options: FormatOptions::default(),
4898            },
4899        );
4900        match result {
4901            Ok(_) => panic!("expected error"),
4902            Err(e) => {
4903                assert!(
4904                    e.to_string()
4905                        .contains("Cast error: Cannot cast string 'seven' to value of Int32 type",),
4906                    "Error: {e}"
4907                )
4908            }
4909        }
4910    }
4911
4912    #[test]
4913    fn test_cast_utf8_to_bool() {
4914        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4915        let casted = cast(&strings, &DataType::Boolean).unwrap();
4916        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4917        assert_eq!(*as_boolean_array(&casted), expected);
4918    }
4919
4920    #[test]
4921    fn test_cast_utf8view_to_bool() {
4922        let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4923        let casted = cast(&strings, &DataType::Boolean).unwrap();
4924        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4925        assert_eq!(*as_boolean_array(&casted), expected);
4926    }
4927
4928    #[test]
4929    fn test_cast_with_options_utf8_to_bool() {
4930        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4931        let casted = cast_with_options(
4932            &strings,
4933            &DataType::Boolean,
4934            &CastOptions {
4935                safe: false,
4936                format_options: FormatOptions::default(),
4937            },
4938        );
4939        match casted {
4940            Ok(_) => panic!("expected error"),
4941            Err(e) => {
4942                assert!(
4943                    e.to_string().contains(
4944                        "Cast error: Cannot cast value 'invalid' to value of Boolean type"
4945                    )
4946                )
4947            }
4948        }
4949    }
4950
4951    #[test]
4952    fn test_cast_bool_to_i32() {
4953        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4954        let b = cast(&array, &DataType::Int32).unwrap();
4955        let c = b.as_primitive::<Int32Type>();
4956        assert_eq!(1, c.value(0));
4957        assert_eq!(0, c.value(1));
4958        assert!(!c.is_valid(2));
4959    }
4960
4961    #[test]
4962    fn test_cast_bool_to_utf8view() {
4963        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4964        let b = cast(&array, &DataType::Utf8View).unwrap();
4965        let c = b.as_any().downcast_ref::<StringViewArray>().unwrap();
4966        assert_eq!("true", c.value(0));
4967        assert_eq!("false", c.value(1));
4968        assert!(!c.is_valid(2));
4969    }
4970
4971    #[test]
4972    fn test_cast_bool_to_utf8() {
4973        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4974        let b = cast(&array, &DataType::Utf8).unwrap();
4975        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
4976        assert_eq!("true", c.value(0));
4977        assert_eq!("false", c.value(1));
4978        assert!(!c.is_valid(2));
4979    }
4980
4981    #[test]
4982    fn test_cast_bool_to_large_utf8() {
4983        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4984        let b = cast(&array, &DataType::LargeUtf8).unwrap();
4985        let c = b.as_any().downcast_ref::<LargeStringArray>().unwrap();
4986        assert_eq!("true", c.value(0));
4987        assert_eq!("false", c.value(1));
4988        assert!(!c.is_valid(2));
4989    }
4990
4991    #[test]
4992    fn test_cast_bool_to_f64() {
4993        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4994        let b = cast(&array, &DataType::Float64).unwrap();
4995        let c = b.as_primitive::<Float64Type>();
4996        assert_eq!(1.0, c.value(0));
4997        assert_eq!(0.0, c.value(1));
4998        assert!(!c.is_valid(2));
4999    }
5000
5001    #[test]
5002    fn test_cast_integer_to_timestamp() {
5003        let array = Int64Array::from(vec![Some(2), Some(10), None]);
5004        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5005
5006        let array = Int8Array::from(vec![Some(2), Some(10), None]);
5007        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5008
5009        assert_eq!(&actual, &expected);
5010
5011        let array = Int16Array::from(vec![Some(2), Some(10), None]);
5012        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5013
5014        assert_eq!(&actual, &expected);
5015
5016        let array = Int32Array::from(vec![Some(2), Some(10), None]);
5017        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5018
5019        assert_eq!(&actual, &expected);
5020
5021        let array = UInt8Array::from(vec![Some(2), Some(10), None]);
5022        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5023
5024        assert_eq!(&actual, &expected);
5025
5026        let array = UInt16Array::from(vec![Some(2), Some(10), None]);
5027        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5028
5029        assert_eq!(&actual, &expected);
5030
5031        let array = UInt32Array::from(vec![Some(2), Some(10), None]);
5032        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5033
5034        assert_eq!(&actual, &expected);
5035
5036        let array = UInt64Array::from(vec![Some(2), Some(10), None]);
5037        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5038
5039        assert_eq!(&actual, &expected);
5040    }
5041
5042    #[test]
5043    fn test_cast_timestamp_to_integer() {
5044        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
5045            .with_timezone("UTC".to_string());
5046        let expected = cast(&array, &DataType::Int64).unwrap();
5047
5048        let actual = cast(&cast(&array, &DataType::Int8).unwrap(), &DataType::Int64).unwrap();
5049        assert_eq!(&actual, &expected);
5050
5051        let actual = cast(&cast(&array, &DataType::Int16).unwrap(), &DataType::Int64).unwrap();
5052        assert_eq!(&actual, &expected);
5053
5054        let actual = cast(&cast(&array, &DataType::Int32).unwrap(), &DataType::Int64).unwrap();
5055        assert_eq!(&actual, &expected);
5056
5057        let actual = cast(&cast(&array, &DataType::UInt8).unwrap(), &DataType::Int64).unwrap();
5058        assert_eq!(&actual, &expected);
5059
5060        let actual = cast(&cast(&array, &DataType::UInt16).unwrap(), &DataType::Int64).unwrap();
5061        assert_eq!(&actual, &expected);
5062
5063        let actual = cast(&cast(&array, &DataType::UInt32).unwrap(), &DataType::Int64).unwrap();
5064        assert_eq!(&actual, &expected);
5065
5066        let actual = cast(&cast(&array, &DataType::UInt64).unwrap(), &DataType::Int64).unwrap();
5067        assert_eq!(&actual, &expected);
5068    }
5069
5070    #[test]
5071    fn test_cast_floating_to_timestamp() {
5072        let array = Int64Array::from(vec![Some(2), Some(10), None]);
5073        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5074
5075        let array = Float16Array::from(vec![
5076            Some(f16::from_f32(2.0)),
5077            Some(f16::from_f32(10.6)),
5078            None,
5079        ]);
5080        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5081
5082        assert_eq!(&actual, &expected);
5083
5084        let array = Float32Array::from(vec![Some(2.0), Some(10.6), None]);
5085        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5086
5087        assert_eq!(&actual, &expected);
5088
5089        let array = Float64Array::from(vec![Some(2.1), Some(10.2), None]);
5090        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5091
5092        assert_eq!(&actual, &expected);
5093    }
5094
5095    #[test]
5096    fn test_cast_timestamp_to_floating() {
5097        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
5098            .with_timezone("UTC".to_string());
5099        let expected = cast(&array, &DataType::Int64).unwrap();
5100
5101        let actual = cast(&cast(&array, &DataType::Float16).unwrap(), &DataType::Int64).unwrap();
5102        assert_eq!(&actual, &expected);
5103
5104        let actual = cast(&cast(&array, &DataType::Float32).unwrap(), &DataType::Int64).unwrap();
5105        assert_eq!(&actual, &expected);
5106
5107        let actual = cast(&cast(&array, &DataType::Float64).unwrap(), &DataType::Int64).unwrap();
5108        assert_eq!(&actual, &expected);
5109    }
5110
5111    #[test]
5112    fn test_cast_decimal_to_timestamp() {
5113        let array = Int64Array::from(vec![Some(2), Some(10), None]);
5114        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5115
5116        let array = Decimal128Array::from(vec![Some(200), Some(1000), None])
5117            .with_precision_and_scale(4, 2)
5118            .unwrap();
5119        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5120
5121        assert_eq!(&actual, &expected);
5122
5123        let array = Decimal256Array::from(vec![
5124            Some(i256::from_i128(2000)),
5125            Some(i256::from_i128(10000)),
5126            None,
5127        ])
5128        .with_precision_and_scale(5, 3)
5129        .unwrap();
5130        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5131
5132        assert_eq!(&actual, &expected);
5133    }
5134
5135    #[test]
5136    fn test_cast_timestamp_to_decimal() {
5137        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
5138            .with_timezone("UTC".to_string());
5139        let expected = cast(&array, &DataType::Int64).unwrap();
5140
5141        let actual = cast(
5142            &cast(&array, &DataType::Decimal128(5, 2)).unwrap(),
5143            &DataType::Int64,
5144        )
5145        .unwrap();
5146        assert_eq!(&actual, &expected);
5147
5148        let actual = cast(
5149            &cast(&array, &DataType::Decimal256(10, 5)).unwrap(),
5150            &DataType::Int64,
5151        )
5152        .unwrap();
5153        assert_eq!(&actual, &expected);
5154    }
5155
5156    #[test]
5157    fn test_cast_list_i32_to_list_u16() {
5158        let values = vec![
5159            Some(vec![Some(0), Some(0), Some(0)]),
5160            Some(vec![Some(-1), Some(-2), Some(-1)]),
5161            Some(vec![Some(2), Some(100000000)]),
5162        ];
5163        let list_array = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
5164
5165        let target_type = DataType::List(Arc::new(Field::new("item", DataType::UInt16, true)));
5166        assert!(can_cast_types(list_array.data_type(), &target_type));
5167        let cast_array = cast(&list_array, &target_type).unwrap();
5168
5169        // For the ListArray itself, there are no null values (as there were no nulls when they went in)
5170        //
5171        // 3 negative values should get lost when casting to unsigned,
5172        // 1 value should overflow
5173        assert_eq!(0, cast_array.null_count());
5174
5175        // offsets should be the same
5176        let array = cast_array.as_list::<i32>();
5177        assert_eq!(list_array.value_offsets(), array.value_offsets());
5178
5179        assert_eq!(DataType::UInt16, array.value_type());
5180        assert_eq!(3, array.value_length(0));
5181        assert_eq!(3, array.value_length(1));
5182        assert_eq!(2, array.value_length(2));
5183
5184        // expect 4 nulls: negative numbers and overflow
5185        let u16arr = array.values().as_primitive::<UInt16Type>();
5186        assert_eq!(4, u16arr.null_count());
5187
5188        // expect 4 nulls: negative numbers and overflow
5189        let expected: UInt16Array =
5190            vec![Some(0), Some(0), Some(0), None, None, None, Some(2), None]
5191                .into_iter()
5192                .collect();
5193
5194        assert_eq!(u16arr, &expected);
5195    }
5196
5197    #[test]
5198    fn test_cast_list_i32_to_list_timestamp() {
5199        // Construct a value array
5200        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 8, 100000000]).into_data();
5201
5202        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 9]);
5203
5204        // Construct a list array from the above two
5205        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
5206        let list_data = ArrayData::builder(list_data_type)
5207            .len(3)
5208            .add_buffer(value_offsets)
5209            .add_child_data(value_data)
5210            .build()
5211            .unwrap();
5212        let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
5213
5214        let actual = cast(
5215            &list_array,
5216            &DataType::List(Arc::new(Field::new_list_field(
5217                DataType::Timestamp(TimeUnit::Microsecond, None),
5218                true,
5219            ))),
5220        )
5221        .unwrap();
5222
5223        let expected = cast(
5224            &cast(
5225                &list_array,
5226                &DataType::List(Arc::new(Field::new_list_field(DataType::Int64, true))),
5227            )
5228            .unwrap(),
5229            &DataType::List(Arc::new(Field::new_list_field(
5230                DataType::Timestamp(TimeUnit::Microsecond, None),
5231                true,
5232            ))),
5233        )
5234        .unwrap();
5235
5236        assert_eq!(&actual, &expected);
5237    }
5238
5239    #[test]
5240    fn test_cast_date32_to_date64() {
5241        let a = Date32Array::from(vec![10000, 17890]);
5242        let array = Arc::new(a) as ArrayRef;
5243        let b = cast(&array, &DataType::Date64).unwrap();
5244        let c = b.as_primitive::<Date64Type>();
5245        assert_eq!(864000000000, c.value(0));
5246        assert_eq!(1545696000000, c.value(1));
5247    }
5248
5249    #[test]
5250    fn test_cast_date64_to_date32() {
5251        let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5252        let array = Arc::new(a) as ArrayRef;
5253        let b = cast(&array, &DataType::Date32).unwrap();
5254        let c = b.as_primitive::<Date32Type>();
5255        assert_eq!(10000, c.value(0));
5256        assert_eq!(17890, c.value(1));
5257        assert!(c.is_null(2));
5258    }
5259
5260    #[test]
5261    fn test_cast_date64_to_date32_overflow() {
5262        let a = Date64Array::from(vec![i64::MAX]);
5263        let array = Arc::new(a) as ArrayRef;
5264
5265        let b = cast(&array, &DataType::Date32).unwrap();
5266        let c = b.as_primitive::<Date32Type>();
5267        assert!(c.is_null(0));
5268
5269        let options = CastOptions {
5270            safe: false,
5271            ..Default::default()
5272        };
5273        let err = cast_with_options(&array, &DataType::Date32, &options).unwrap_err();
5274        assert!(
5275            err.to_string().contains("Cannot cast Date64 value"),
5276            "{err}"
5277        );
5278    }
5279
5280    #[test]
5281    fn test_cast_string_to_integral_overflow() {
5282        let str = Arc::new(StringArray::from(vec![
5283            Some("123"),
5284            Some("-123"),
5285            Some("86374"),
5286            None,
5287        ])) as ArrayRef;
5288
5289        let options = CastOptions {
5290            safe: true,
5291            format_options: FormatOptions::default(),
5292        };
5293        let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
5294        let expected =
5295            Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
5296        assert_eq!(&res, &expected);
5297    }
5298
5299    #[test]
5300    fn test_cast_string_to_timestamp() {
5301        let a0 = Arc::new(StringViewArray::from(vec![
5302            Some("2020-09-08T12:00:00.123456789+00:00"),
5303            Some("Not a valid date"),
5304            None,
5305        ])) as ArrayRef;
5306        let a1 = Arc::new(StringArray::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 a2 = Arc::new(LargeStringArray::from(vec![
5312            Some("2020-09-08T12:00:00.123456789+00:00"),
5313            Some("Not a valid date"),
5314            None,
5315        ])) as ArrayRef;
5316        for array in &[a0, a1, a2] {
5317            for time_unit in &[
5318                TimeUnit::Second,
5319                TimeUnit::Millisecond,
5320                TimeUnit::Microsecond,
5321                TimeUnit::Nanosecond,
5322            ] {
5323                let to_type = DataType::Timestamp(*time_unit, None);
5324                let b = cast(array, &to_type).unwrap();
5325
5326                match time_unit {
5327                    TimeUnit::Second => {
5328                        let c = b.as_primitive::<TimestampSecondType>();
5329                        assert_eq!(1599566400, c.value(0));
5330                        assert!(c.is_null(1));
5331                        assert!(c.is_null(2));
5332                    }
5333                    TimeUnit::Millisecond => {
5334                        let c = b
5335                            .as_any()
5336                            .downcast_ref::<TimestampMillisecondArray>()
5337                            .unwrap();
5338                        assert_eq!(1599566400123, c.value(0));
5339                        assert!(c.is_null(1));
5340                        assert!(c.is_null(2));
5341                    }
5342                    TimeUnit::Microsecond => {
5343                        let c = b
5344                            .as_any()
5345                            .downcast_ref::<TimestampMicrosecondArray>()
5346                            .unwrap();
5347                        assert_eq!(1599566400123456, c.value(0));
5348                        assert!(c.is_null(1));
5349                        assert!(c.is_null(2));
5350                    }
5351                    TimeUnit::Nanosecond => {
5352                        let c = b
5353                            .as_any()
5354                            .downcast_ref::<TimestampNanosecondArray>()
5355                            .unwrap();
5356                        assert_eq!(1599566400123456789, c.value(0));
5357                        assert!(c.is_null(1));
5358                        assert!(c.is_null(2));
5359                    }
5360                }
5361
5362                let options = CastOptions {
5363                    safe: false,
5364                    format_options: FormatOptions::default(),
5365                };
5366                let err = cast_with_options(array, &to_type, &options).unwrap_err();
5367                assert_eq!(
5368                    err.to_string(),
5369                    "Parser error: Error parsing timestamp from 'Not a valid date': error parsing date"
5370                );
5371            }
5372        }
5373    }
5374
5375    #[test]
5376    fn test_cast_string_to_timestamp_overflow() {
5377        let array = StringArray::from(vec!["9800-09-08T12:00:00.123456789"]);
5378        let result = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5379        let result = result.as_primitive::<TimestampSecondType>();
5380        assert_eq!(result.values(), &[247112596800]);
5381    }
5382
5383    #[test]
5384    fn test_cast_string_to_date32() {
5385        let a0 = Arc::new(StringViewArray::from(vec![
5386            Some("2018-12-25"),
5387            Some("Not a valid date"),
5388            None,
5389        ])) as ArrayRef;
5390        let a1 = Arc::new(StringArray::from(vec![
5391            Some("2018-12-25"),
5392            Some("Not a valid date"),
5393            None,
5394        ])) as ArrayRef;
5395        let a2 = Arc::new(LargeStringArray::from(vec![
5396            Some("2018-12-25"),
5397            Some("Not a valid date"),
5398            None,
5399        ])) as ArrayRef;
5400        for array in &[a0, a1, a2] {
5401            let to_type = DataType::Date32;
5402            let b = cast(array, &to_type).unwrap();
5403            let c = b.as_primitive::<Date32Type>();
5404            assert_eq!(17890, c.value(0));
5405            assert!(c.is_null(1));
5406            assert!(c.is_null(2));
5407
5408            let options = CastOptions {
5409                safe: false,
5410                format_options: FormatOptions::default(),
5411            };
5412            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5413            assert_eq!(
5414                err.to_string(),
5415                "Cast error: Cannot cast string 'Not a valid date' to value of Date32 type"
5416            );
5417        }
5418    }
5419
5420    #[test]
5421    fn test_cast_string_with_large_date_to_date32() {
5422        let array = Arc::new(StringArray::from(vec![
5423            Some("+10999-12-31"),
5424            Some("-0010-02-28"),
5425            Some("0010-02-28"),
5426            Some("0000-01-01"),
5427            Some("-0000-01-01"),
5428            Some("-0001-01-01"),
5429        ])) as ArrayRef;
5430        let to_type = DataType::Date32;
5431        let options = CastOptions {
5432            safe: false,
5433            format_options: FormatOptions::default(),
5434        };
5435        let b = cast_with_options(&array, &to_type, &options).unwrap();
5436        let c = b.as_primitive::<Date32Type>();
5437        assert_eq!(3298139, c.value(0)); // 10999-12-31
5438        assert_eq!(-723122, c.value(1)); // -0010-02-28
5439        assert_eq!(-715817, c.value(2)); // 0010-02-28
5440        assert_eq!(c.value(3), c.value(4)); // Expect 0000-01-01 and -0000-01-01 to be parsed the same
5441        assert_eq!(-719528, c.value(3)); // 0000-01-01
5442        assert_eq!(-719528, c.value(4)); // -0000-01-01
5443        assert_eq!(-719893, c.value(5)); // -0001-01-01
5444    }
5445
5446    #[test]
5447    fn test_cast_invalid_string_with_large_date_to_date32() {
5448        // Large dates need to be prefixed with a + or - sign, otherwise they are not parsed correctly
5449        let array = Arc::new(StringArray::from(vec![Some("10999-12-31")])) as ArrayRef;
5450        let to_type = DataType::Date32;
5451        let options = CastOptions {
5452            safe: false,
5453            format_options: FormatOptions::default(),
5454        };
5455        let err = cast_with_options(&array, &to_type, &options).unwrap_err();
5456        assert_eq!(
5457            err.to_string(),
5458            "Cast error: Cannot cast string '10999-12-31' to value of Date32 type"
5459        );
5460    }
5461
5462    #[test]
5463    fn test_cast_string_format_yyyymmdd_to_date32() {
5464        let a0 = Arc::new(StringViewArray::from(vec![
5465            Some("2020-12-25"),
5466            Some("20201117"),
5467        ])) as ArrayRef;
5468        let a1 = Arc::new(StringArray::from(vec![
5469            Some("2020-12-25"),
5470            Some("20201117"),
5471        ])) as ArrayRef;
5472        let a2 = Arc::new(LargeStringArray::from(vec![
5473            Some("2020-12-25"),
5474            Some("20201117"),
5475        ])) as ArrayRef;
5476
5477        for array in &[a0, a1, a2] {
5478            let to_type = DataType::Date32;
5479            let options = CastOptions {
5480                safe: false,
5481                format_options: FormatOptions::default(),
5482            };
5483            let result = cast_with_options(&array, &to_type, &options).unwrap();
5484            let c = result.as_primitive::<Date32Type>();
5485            assert_eq!(
5486                chrono::NaiveDate::from_ymd_opt(2020, 12, 25),
5487                c.value_as_date(0)
5488            );
5489            assert_eq!(
5490                chrono::NaiveDate::from_ymd_opt(2020, 11, 17),
5491                c.value_as_date(1)
5492            );
5493        }
5494    }
5495
5496    #[test]
5497    fn test_cast_string_to_time32second() {
5498        let a0 = Arc::new(StringViewArray::from(vec![
5499            Some("08:08:35.091323414"),
5500            Some("08:08:60.091323414"), // leap second
5501            Some("08:08:61.091323414"), // not valid
5502            Some("Not a valid time"),
5503            None,
5504        ])) as ArrayRef;
5505        let a1 = Arc::new(StringArray::from(vec![
5506            Some("08:08:35.091323414"),
5507            Some("08:08:60.091323414"), // leap second
5508            Some("08:08:61.091323414"), // not valid
5509            Some("Not a valid time"),
5510            None,
5511        ])) as ArrayRef;
5512        let a2 = Arc::new(LargeStringArray::from(vec![
5513            Some("08:08:35.091323414"),
5514            Some("08:08:60.091323414"), // leap second
5515            Some("08:08:61.091323414"), // not valid
5516            Some("Not a valid time"),
5517            None,
5518        ])) as ArrayRef;
5519        for array in &[a0, a1, a2] {
5520            let to_type = DataType::Time32(TimeUnit::Second);
5521            let b = cast(array, &to_type).unwrap();
5522            let c = b.as_primitive::<Time32SecondType>();
5523            assert_eq!(29315, c.value(0));
5524            assert_eq!(29340, c.value(1));
5525            assert!(c.is_null(2));
5526            assert!(c.is_null(3));
5527            assert!(c.is_null(4));
5528
5529            let options = CastOptions {
5530                safe: false,
5531                format_options: FormatOptions::default(),
5532            };
5533            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5534            assert_eq!(
5535                err.to_string(),
5536                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(s) type"
5537            );
5538        }
5539    }
5540
5541    #[test]
5542    fn test_cast_string_to_time32millisecond() {
5543        let a0 = Arc::new(StringViewArray::from(vec![
5544            Some("08:08:35.091323414"),
5545            Some("08:08:60.091323414"), // leap second
5546            Some("08:08:61.091323414"), // not valid
5547            Some("Not a valid time"),
5548            None,
5549        ])) as ArrayRef;
5550        let a1 = Arc::new(StringArray::from(vec![
5551            Some("08:08:35.091323414"),
5552            Some("08:08:60.091323414"), // leap second
5553            Some("08:08:61.091323414"), // not valid
5554            Some("Not a valid time"),
5555            None,
5556        ])) as ArrayRef;
5557        let a2 = Arc::new(LargeStringArray::from(vec![
5558            Some("08:08:35.091323414"),
5559            Some("08:08:60.091323414"), // leap second
5560            Some("08:08:61.091323414"), // not valid
5561            Some("Not a valid time"),
5562            None,
5563        ])) as ArrayRef;
5564        for array in &[a0, a1, a2] {
5565            let to_type = DataType::Time32(TimeUnit::Millisecond);
5566            let b = cast(array, &to_type).unwrap();
5567            let c = b.as_primitive::<Time32MillisecondType>();
5568            assert_eq!(29315091, c.value(0));
5569            assert_eq!(29340091, c.value(1));
5570            assert!(c.is_null(2));
5571            assert!(c.is_null(3));
5572            assert!(c.is_null(4));
5573
5574            let options = CastOptions {
5575                safe: false,
5576                format_options: FormatOptions::default(),
5577            };
5578            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5579            assert_eq!(
5580                err.to_string(),
5581                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(ms) type"
5582            );
5583        }
5584    }
5585
5586    #[test]
5587    fn test_cast_string_to_time64microsecond() {
5588        let a0 = Arc::new(StringViewArray::from(vec![
5589            Some("08:08:35.091323414"),
5590            Some("Not a valid time"),
5591            None,
5592        ])) as ArrayRef;
5593        let a1 = Arc::new(StringArray::from(vec![
5594            Some("08:08:35.091323414"),
5595            Some("Not a valid time"),
5596            None,
5597        ])) as ArrayRef;
5598        let a2 = Arc::new(LargeStringArray::from(vec![
5599            Some("08:08:35.091323414"),
5600            Some("Not a valid time"),
5601            None,
5602        ])) as ArrayRef;
5603        for array in &[a0, a1, a2] {
5604            let to_type = DataType::Time64(TimeUnit::Microsecond);
5605            let b = cast(array, &to_type).unwrap();
5606            let c = b.as_primitive::<Time64MicrosecondType>();
5607            assert_eq!(29315091323, c.value(0));
5608            assert!(c.is_null(1));
5609            assert!(c.is_null(2));
5610
5611            let options = CastOptions {
5612                safe: false,
5613                format_options: FormatOptions::default(),
5614            };
5615            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5616            assert_eq!(
5617                err.to_string(),
5618                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(µs) type"
5619            );
5620        }
5621    }
5622
5623    #[test]
5624    fn test_cast_string_to_time64nanosecond() {
5625        let a0 = Arc::new(StringViewArray::from(vec![
5626            Some("08:08:35.091323414"),
5627            Some("Not a valid time"),
5628            None,
5629        ])) as ArrayRef;
5630        let a1 = Arc::new(StringArray::from(vec![
5631            Some("08:08:35.091323414"),
5632            Some("Not a valid time"),
5633            None,
5634        ])) as ArrayRef;
5635        let a2 = Arc::new(LargeStringArray::from(vec![
5636            Some("08:08:35.091323414"),
5637            Some("Not a valid time"),
5638            None,
5639        ])) as ArrayRef;
5640        for array in &[a0, a1, a2] {
5641            let to_type = DataType::Time64(TimeUnit::Nanosecond);
5642            let b = cast(array, &to_type).unwrap();
5643            let c = b.as_primitive::<Time64NanosecondType>();
5644            assert_eq!(29315091323414, c.value(0));
5645            assert!(c.is_null(1));
5646            assert!(c.is_null(2));
5647
5648            let options = CastOptions {
5649                safe: false,
5650                format_options: FormatOptions::default(),
5651            };
5652            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5653            assert_eq!(
5654                err.to_string(),
5655                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(ns) type"
5656            );
5657        }
5658    }
5659
5660    #[test]
5661    fn test_cast_string_to_date64() {
5662        let a0 = Arc::new(StringViewArray::from(vec![
5663            Some("2020-09-08T12:00:00"),
5664            Some("Not a valid date"),
5665            None,
5666        ])) as ArrayRef;
5667        let a1 = Arc::new(StringArray::from(vec![
5668            Some("2020-09-08T12:00:00"),
5669            Some("Not a valid date"),
5670            None,
5671        ])) as ArrayRef;
5672        let a2 = Arc::new(LargeStringArray::from(vec![
5673            Some("2020-09-08T12:00:00"),
5674            Some("Not a valid date"),
5675            None,
5676        ])) as ArrayRef;
5677        for array in &[a0, a1, a2] {
5678            let to_type = DataType::Date64;
5679            let b = cast(array, &to_type).unwrap();
5680            let c = b.as_primitive::<Date64Type>();
5681            assert_eq!(1599566400000, c.value(0));
5682            assert!(c.is_null(1));
5683            assert!(c.is_null(2));
5684
5685            let options = CastOptions {
5686                safe: false,
5687                format_options: FormatOptions::default(),
5688            };
5689            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5690            assert_eq!(
5691                err.to_string(),
5692                "Cast error: Cannot cast string 'Not a valid date' to value of Date64 type"
5693            );
5694        }
5695    }
5696
5697    macro_rules! test_safe_string_to_interval {
5698        ($data_vec:expr, $interval_unit:expr, $array_ty:ty, $expect_vec:expr) => {
5699            let source_string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5700
5701            let options = CastOptions {
5702                safe: true,
5703                format_options: FormatOptions::default(),
5704            };
5705
5706            let target_interval_array = cast_with_options(
5707                &source_string_array.clone(),
5708                &DataType::Interval($interval_unit),
5709                &options,
5710            )
5711            .unwrap()
5712            .as_any()
5713            .downcast_ref::<$array_ty>()
5714            .unwrap()
5715            .clone() as $array_ty;
5716
5717            let target_string_array =
5718                cast_with_options(&target_interval_array, &DataType::Utf8, &options)
5719                    .unwrap()
5720                    .as_any()
5721                    .downcast_ref::<StringArray>()
5722                    .unwrap()
5723                    .clone();
5724
5725            let expect_string_array = StringArray::from($expect_vec);
5726
5727            assert_eq!(target_string_array, expect_string_array);
5728
5729            let target_large_string_array =
5730                cast_with_options(&target_interval_array, &DataType::LargeUtf8, &options)
5731                    .unwrap()
5732                    .as_any()
5733                    .downcast_ref::<LargeStringArray>()
5734                    .unwrap()
5735                    .clone();
5736
5737            let expect_large_string_array = LargeStringArray::from($expect_vec);
5738
5739            assert_eq!(target_large_string_array, expect_large_string_array);
5740        };
5741    }
5742
5743    #[test]
5744    fn test_cast_string_to_interval_year_month() {
5745        test_safe_string_to_interval!(
5746            vec![
5747                Some("1 year 1 month"),
5748                Some("1.5 years 13 month"),
5749                Some("30 days"),
5750                Some("31 days"),
5751                Some("2 months 31 days"),
5752                Some("2 months 31 days 1 second"),
5753                Some("foobar"),
5754            ],
5755            IntervalUnit::YearMonth,
5756            IntervalYearMonthArray,
5757            vec![
5758                Some("1 years 1 mons"),
5759                Some("2 years 7 mons"),
5760                None,
5761                None,
5762                None,
5763                None,
5764                None,
5765            ]
5766        );
5767    }
5768
5769    #[test]
5770    fn test_cast_string_to_interval_day_time() {
5771        test_safe_string_to_interval!(
5772            vec![
5773                Some("1 year 1 month"),
5774                Some("1.5 years 13 month"),
5775                Some("30 days"),
5776                Some("1 day 2 second 3.5 milliseconds"),
5777                Some("foobar"),
5778            ],
5779            IntervalUnit::DayTime,
5780            IntervalDayTimeArray,
5781            vec![
5782                Some("390 days"),
5783                Some("930 days"),
5784                Some("30 days"),
5785                None,
5786                None,
5787            ]
5788        );
5789    }
5790
5791    #[test]
5792    fn test_cast_string_to_interval_month_day_nano() {
5793        test_safe_string_to_interval!(
5794            vec![
5795                Some("1 year 1 month 1 day"),
5796                None,
5797                Some("1.5 years 13 month 35 days 1.4 milliseconds"),
5798                Some("3 days"),
5799                Some("8 seconds"),
5800                None,
5801                Some("1 day 29800 milliseconds"),
5802                Some("3 months 1 second"),
5803                Some("6 minutes 120 second"),
5804                Some("2 years 39 months 9 days 19 hours 1 minute 83 seconds 399222 milliseconds"),
5805                Some("foobar"),
5806            ],
5807            IntervalUnit::MonthDayNano,
5808            IntervalMonthDayNanoArray,
5809            vec![
5810                Some("13 mons 1 days"),
5811                None,
5812                Some("31 mons 35 days 0.001400000 secs"),
5813                Some("3 days"),
5814                Some("8.000000000 secs"),
5815                None,
5816                Some("1 days 29.800000000 secs"),
5817                Some("3 mons 1.000000000 secs"),
5818                Some("8 mins"),
5819                Some("63 mons 9 days 19 hours 9 mins 2.222000000 secs"),
5820                None,
5821            ]
5822        );
5823    }
5824
5825    macro_rules! test_unsafe_string_to_interval_err {
5826        ($data_vec:expr, $interval_unit:expr, $error_msg:expr) => {
5827            let string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5828            let options = CastOptions {
5829                safe: false,
5830                format_options: FormatOptions::default(),
5831            };
5832            let arrow_err = cast_with_options(
5833                &string_array.clone(),
5834                &DataType::Interval($interval_unit),
5835                &options,
5836            )
5837            .unwrap_err();
5838            assert_eq!($error_msg, arrow_err.to_string());
5839        };
5840    }
5841
5842    #[test]
5843    fn test_cast_string_to_interval_err() {
5844        test_unsafe_string_to_interval_err!(
5845            vec![Some("foobar")],
5846            IntervalUnit::YearMonth,
5847            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5848        );
5849        test_unsafe_string_to_interval_err!(
5850            vec![Some("foobar")],
5851            IntervalUnit::DayTime,
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::MonthDayNano,
5857            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5858        );
5859        test_unsafe_string_to_interval_err!(
5860            vec![Some("2 months 31 days 1 second")],
5861            IntervalUnit::YearMonth,
5862            r#"Cast error: Cannot cast 2 months 31 days 1 second to IntervalYearMonth. Only year and month fields are allowed."#
5863        );
5864        test_unsafe_string_to_interval_err!(
5865            vec![Some("1 day 1.5 milliseconds")],
5866            IntervalUnit::DayTime,
5867            r#"Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"#
5868        );
5869
5870        // overflow
5871        test_unsafe_string_to_interval_err!(
5872            vec![Some(format!(
5873                "{} century {} year {} month",
5874                i64::MAX - 2,
5875                i64::MAX - 2,
5876                i64::MAX - 2
5877            ))],
5878            IntervalUnit::DayTime,
5879            format!(
5880                "Arithmetic overflow: Overflow happened on: {} * 100",
5881                i64::MAX - 2
5882            )
5883        );
5884        test_unsafe_string_to_interval_err!(
5885            vec![Some(format!(
5886                "{} year {} month {} day",
5887                i64::MAX - 2,
5888                i64::MAX - 2,
5889                i64::MAX - 2
5890            ))],
5891            IntervalUnit::MonthDayNano,
5892            format!(
5893                "Arithmetic overflow: Overflow happened on: {} * 12",
5894                i64::MAX - 2
5895            )
5896        );
5897    }
5898
5899    #[test]
5900    fn test_cast_binary_to_fixed_size_binary() {
5901        let bytes_1 = "Hiiii".as_bytes();
5902        let bytes_2 = "Hello".as_bytes();
5903
5904        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5905        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5906        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5907
5908        let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
5909        let down_cast = array_ref
5910            .as_any()
5911            .downcast_ref::<FixedSizeBinaryArray>()
5912            .unwrap();
5913        assert_eq!(bytes_1, down_cast.value(0));
5914        assert_eq!(bytes_2, down_cast.value(1));
5915        assert!(down_cast.is_null(2));
5916
5917        let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
5918        let down_cast = array_ref
5919            .as_any()
5920            .downcast_ref::<FixedSizeBinaryArray>()
5921            .unwrap();
5922        assert_eq!(bytes_1, down_cast.value(0));
5923        assert_eq!(bytes_2, down_cast.value(1));
5924        assert!(down_cast.is_null(2));
5925
5926        // test error cases when the length of binary are not same
5927        let bytes_1 = "Hi".as_bytes();
5928        let bytes_2 = "Hello".as_bytes();
5929
5930        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5931        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5932        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5933
5934        let array_ref = cast_with_options(
5935            &a1,
5936            &DataType::FixedSizeBinary(5),
5937            &CastOptions {
5938                safe: false,
5939                format_options: FormatOptions::default(),
5940            },
5941        );
5942        assert!(array_ref.is_err());
5943
5944        let array_ref = cast_with_options(
5945            &a2,
5946            &DataType::FixedSizeBinary(5),
5947            &CastOptions {
5948                safe: false,
5949                format_options: FormatOptions::default(),
5950            },
5951        );
5952        assert!(array_ref.is_err());
5953    }
5954
5955    #[test]
5956    fn test_fixed_size_binary_to_binary() {
5957        let bytes_1 = "Hiiii".as_bytes();
5958        let bytes_2 = "Hello".as_bytes();
5959
5960        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5961        let a1 = Arc::new(FixedSizeBinaryArray::try_from(binary_data.clone()).unwrap()) as ArrayRef;
5962
5963        let array_ref = cast(&a1, &DataType::Binary).unwrap();
5964        let down_cast = array_ref.as_binary::<i32>();
5965        assert_eq!(bytes_1, down_cast.value(0));
5966        assert_eq!(bytes_2, down_cast.value(1));
5967        assert!(down_cast.is_null(2));
5968
5969        let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
5970        let down_cast = array_ref.as_binary::<i64>();
5971        assert_eq!(bytes_1, down_cast.value(0));
5972        assert_eq!(bytes_2, down_cast.value(1));
5973        assert!(down_cast.is_null(2));
5974
5975        let array_ref = cast(&a1, &DataType::BinaryView).unwrap();
5976        let down_cast = array_ref.as_binary_view();
5977        assert_eq!(bytes_1, down_cast.value(0));
5978        assert_eq!(bytes_2, down_cast.value(1));
5979        assert!(down_cast.is_null(2));
5980    }
5981
5982    #[test]
5983    fn test_fixed_size_binary_to_dictionary() {
5984        let bytes_1 = "Hiiii".as_bytes();
5985        let bytes_2 = "Hello".as_bytes();
5986
5987        let binary_data = vec![Some(bytes_1), Some(bytes_2), Some(bytes_1), None];
5988        let a1 = Arc::new(FixedSizeBinaryArray::try_from(binary_data.clone()).unwrap()) as ArrayRef;
5989
5990        let cast_type = DataType::Dictionary(
5991            Box::new(DataType::Int8),
5992            Box::new(DataType::FixedSizeBinary(5)),
5993        );
5994        let cast_array = cast(&a1, &cast_type).unwrap();
5995        assert_eq!(cast_array.data_type(), &cast_type);
5996        assert_eq!(
5997            array_to_strings(&cast_array),
5998            vec!["4869696969", "48656c6c6f", "4869696969", "null"]
5999        );
6000        // dictionary should only have two distinct values
6001        let dict_array = cast_array.as_dictionary::<Int8Type>();
6002        assert_eq!(dict_array.values().len(), 2);
6003    }
6004
6005    #[test]
6006    fn test_binary_to_dictionary() {
6007        let mut builder = GenericBinaryBuilder::<i32>::new();
6008        builder.append_value(b"hello");
6009        builder.append_value(b"hiiii");
6010        builder.append_value(b"hiiii"); // duplicate
6011        builder.append_null();
6012        builder.append_value(b"rustt");
6013
6014        let a1 = builder.finish();
6015
6016        let cast_type = DataType::Dictionary(
6017            Box::new(DataType::Int8),
6018            Box::new(DataType::FixedSizeBinary(5)),
6019        );
6020        let cast_array = cast(&a1, &cast_type).unwrap();
6021        assert_eq!(cast_array.data_type(), &cast_type);
6022        assert_eq!(
6023            array_to_strings(&cast_array),
6024            vec![
6025                "68656c6c6f",
6026                "6869696969",
6027                "6869696969",
6028                "null",
6029                "7275737474"
6030            ]
6031        );
6032        // dictionary should only have three distinct values
6033        let dict_array = cast_array.as_dictionary::<Int8Type>();
6034        assert_eq!(dict_array.values().len(), 3);
6035    }
6036
6037    #[test]
6038    fn test_cast_string_array_to_dict_utf8_view() {
6039        let array = StringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
6040
6041        let cast_type =
6042            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6043        assert!(can_cast_types(array.data_type(), &cast_type));
6044        let cast_array = cast(&array, &cast_type).unwrap();
6045        assert_eq!(cast_array.data_type(), &cast_type);
6046
6047        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6048        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6049        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
6050
6051        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6052        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6053        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6054
6055        let keys = dict_array.keys();
6056        assert!(keys.is_null(1));
6057        assert_eq!(keys.value(0), keys.value(3));
6058        assert_ne!(keys.value(0), keys.value(2));
6059    }
6060
6061    #[test]
6062    fn test_cast_string_array_to_dict_utf8_view_null_vs_literal_null() {
6063        let array = StringArray::from(vec![Some("one"), None, Some("null"), Some("one")]);
6064
6065        let cast_type =
6066            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6067        assert!(can_cast_types(array.data_type(), &cast_type));
6068        let cast_array = cast(&array, &cast_type).unwrap();
6069        assert_eq!(cast_array.data_type(), &cast_type);
6070
6071        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6072        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6073        assert_eq!(dict_array.values().len(), 2);
6074
6075        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6076        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6077        assert_eq!(actual, vec![Some("one"), None, Some("null"), Some("one")]);
6078
6079        let keys = dict_array.keys();
6080        assert!(keys.is_null(1));
6081        assert_eq!(keys.value(0), keys.value(3));
6082        assert_ne!(keys.value(0), keys.value(2));
6083    }
6084
6085    #[test]
6086    fn test_cast_string_view_array_to_dict_utf8_view() {
6087        let array = StringViewArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
6088
6089        let cast_type =
6090            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6091        assert!(can_cast_types(array.data_type(), &cast_type));
6092        let cast_array = cast(&array, &cast_type).unwrap();
6093        assert_eq!(cast_array.data_type(), &cast_type);
6094
6095        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6096        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6097        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
6098
6099        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6100        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6101        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6102
6103        let keys = dict_array.keys();
6104        assert!(keys.is_null(1));
6105        assert_eq!(keys.value(0), keys.value(3));
6106        assert_ne!(keys.value(0), keys.value(2));
6107    }
6108
6109    #[test]
6110    fn test_cast_string_view_slice_to_dict_utf8_view() {
6111        let array = StringViewArray::from(vec![
6112            Some("zero"),
6113            Some("one"),
6114            None,
6115            Some("three"),
6116            Some("one"),
6117        ]);
6118        let view = array.slice(1, 4);
6119
6120        let cast_type =
6121            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6122        assert!(can_cast_types(view.data_type(), &cast_type));
6123        let cast_array = cast(&view, &cast_type).unwrap();
6124        assert_eq!(cast_array.data_type(), &cast_type);
6125
6126        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6127        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6128        assert_eq!(dict_array.values().len(), 2);
6129
6130        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6131        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6132        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6133
6134        let keys = dict_array.keys();
6135        assert!(keys.is_null(1));
6136        assert_eq!(keys.value(0), keys.value(3));
6137        assert_ne!(keys.value(0), keys.value(2));
6138    }
6139
6140    #[test]
6141    fn test_cast_binary_array_to_dict_binary_view() {
6142        let mut builder = GenericBinaryBuilder::<i32>::new();
6143        builder.append_value(b"hello");
6144        builder.append_value(b"hiiii");
6145        builder.append_value(b"hiiii"); // duplicate
6146        builder.append_null();
6147        builder.append_value(b"rustt");
6148
6149        let array = builder.finish();
6150
6151        let cast_type =
6152            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6153        assert!(can_cast_types(array.data_type(), &cast_type));
6154        let cast_array = cast(&array, &cast_type).unwrap();
6155        assert_eq!(cast_array.data_type(), &cast_type);
6156
6157        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6158        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6159        assert_eq!(dict_array.values().len(), 3);
6160
6161        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6162        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6163        assert_eq!(
6164            actual,
6165            vec![
6166                Some(b"hello".as_slice()),
6167                Some(b"hiiii".as_slice()),
6168                Some(b"hiiii".as_slice()),
6169                None,
6170                Some(b"rustt".as_slice())
6171            ]
6172        );
6173
6174        let keys = dict_array.keys();
6175        assert!(keys.is_null(3));
6176        assert_eq!(keys.value(1), keys.value(2));
6177        assert_ne!(keys.value(0), keys.value(1));
6178    }
6179
6180    #[test]
6181    fn test_cast_binary_view_array_to_dict_binary_view() {
6182        let view = BinaryViewArray::from_iter([
6183            Some(b"hello".as_slice()),
6184            Some(b"hiiii".as_slice()),
6185            Some(b"hiiii".as_slice()), // duplicate
6186            None,
6187            Some(b"rustt".as_slice()),
6188        ]);
6189
6190        let cast_type =
6191            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6192        assert!(can_cast_types(view.data_type(), &cast_type));
6193        let cast_array = cast(&view, &cast_type).unwrap();
6194        assert_eq!(cast_array.data_type(), &cast_type);
6195
6196        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6197        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6198        assert_eq!(dict_array.values().len(), 3);
6199
6200        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6201        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6202        assert_eq!(
6203            actual,
6204            vec![
6205                Some(b"hello".as_slice()),
6206                Some(b"hiiii".as_slice()),
6207                Some(b"hiiii".as_slice()),
6208                None,
6209                Some(b"rustt".as_slice())
6210            ]
6211        );
6212
6213        let keys = dict_array.keys();
6214        assert!(keys.is_null(3));
6215        assert_eq!(keys.value(1), keys.value(2));
6216        assert_ne!(keys.value(0), keys.value(1));
6217    }
6218
6219    #[test]
6220    fn test_cast_binary_view_slice_to_dict_binary_view() {
6221        let view = BinaryViewArray::from_iter([
6222            Some(b"hello".as_slice()),
6223            Some(b"hiiii".as_slice()),
6224            Some(b"hiiii".as_slice()), // duplicate
6225            None,
6226            Some(b"rustt".as_slice()),
6227        ]);
6228        let sliced = view.slice(1, 4);
6229
6230        let cast_type =
6231            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6232        assert!(can_cast_types(sliced.data_type(), &cast_type));
6233        let cast_array = cast(&sliced, &cast_type).unwrap();
6234        assert_eq!(cast_array.data_type(), &cast_type);
6235
6236        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6237        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6238        assert_eq!(dict_array.values().len(), 2);
6239
6240        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6241        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6242        assert_eq!(
6243            actual,
6244            vec![
6245                Some(b"hiiii".as_slice()),
6246                Some(b"hiiii".as_slice()),
6247                None,
6248                Some(b"rustt".as_slice())
6249            ]
6250        );
6251
6252        let keys = dict_array.keys();
6253        assert!(keys.is_null(2));
6254        assert_eq!(keys.value(0), keys.value(1));
6255        assert_ne!(keys.value(0), keys.value(3));
6256    }
6257
6258    #[test]
6259    fn test_cast_string_array_to_dict_utf8_view_key_overflow_u8() {
6260        let array = StringArray::from_iter_values((0..257).map(|i| format!("v{i}")));
6261
6262        let cast_type =
6263            DataType::Dictionary(Box::new(DataType::UInt8), Box::new(DataType::Utf8View));
6264        assert!(can_cast_types(array.data_type(), &cast_type));
6265        let err = cast(&array, &cast_type).unwrap_err();
6266        assert!(matches!(err, ArrowError::DictionaryKeyOverflowError));
6267    }
6268
6269    #[test]
6270    fn test_cast_large_string_array_to_dict_utf8_view() {
6271        let array = LargeStringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
6272
6273        let cast_type =
6274            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6275        assert!(can_cast_types(array.data_type(), &cast_type));
6276        let cast_array = cast(&array, &cast_type).unwrap();
6277        assert_eq!(cast_array.data_type(), &cast_type);
6278
6279        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6280        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6281        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
6282
6283        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6284        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6285        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6286
6287        let keys = dict_array.keys();
6288        assert!(keys.is_null(1));
6289        assert_eq!(keys.value(0), keys.value(3));
6290        assert_ne!(keys.value(0), keys.value(2));
6291    }
6292
6293    #[test]
6294    fn test_cast_large_binary_array_to_dict_binary_view() {
6295        let mut builder = GenericBinaryBuilder::<i64>::new();
6296        builder.append_value(b"hello");
6297        builder.append_value(b"world");
6298        builder.append_value(b"hello"); // duplicate
6299        builder.append_null();
6300
6301        let array = builder.finish();
6302
6303        let cast_type =
6304            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6305        assert!(can_cast_types(array.data_type(), &cast_type));
6306        let cast_array = cast(&array, &cast_type).unwrap();
6307        assert_eq!(cast_array.data_type(), &cast_type);
6308
6309        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6310        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6311        assert_eq!(dict_array.values().len(), 2); // "hello" and "world" deduplicated
6312
6313        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6314        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6315        assert_eq!(
6316            actual,
6317            vec![
6318                Some(b"hello".as_slice()),
6319                Some(b"world".as_slice()),
6320                Some(b"hello".as_slice()),
6321                None
6322            ]
6323        );
6324
6325        let keys = dict_array.keys();
6326        assert!(keys.is_null(3));
6327        assert_eq!(keys.value(0), keys.value(2));
6328        assert_ne!(keys.value(0), keys.value(1));
6329    }
6330
6331    #[test]
6332    fn test_cast_struct_array_to_dict_struct() {
6333        // Cast a StructArray into Dictionary<UInt32, Struct{…}>. The dictionary
6334        // value type's child fields may differ from the source's (here:
6335        // Utf8 source → Utf8View child for `name`), so the per-field cast
6336        // must run before identity keys are emitted. This is the "as long as
6337        // the struct can be cast to the dict value" contract.
6338        let names = StringArray::from(vec![Some("alpha"), None, Some("gamma")]);
6339        let ids = Int32Array::from(vec![Some(1), Some(2), Some(3)]);
6340        let source = StructArray::from(vec![
6341            (
6342                Arc::new(Field::new("name", DataType::Utf8, true)),
6343                Arc::new(names) as ArrayRef,
6344            ),
6345            (
6346                Arc::new(Field::new("id", DataType::Int32, false)),
6347                Arc::new(ids) as ArrayRef,
6348            ),
6349        ]);
6350
6351        let target_value_type = DataType::Struct(
6352            vec![
6353                Field::new("name", DataType::Utf8View, true),
6354                Field::new("id", DataType::Int64, false),
6355            ]
6356            .into(),
6357        );
6358        let cast_type = DataType::Dictionary(
6359            Box::new(DataType::UInt32),
6360            Box::new(target_value_type.clone()),
6361        );
6362        assert!(can_cast_types(source.data_type(), &cast_type));
6363
6364        let cast_array = cast(&source, &cast_type).unwrap();
6365        assert_eq!(cast_array.data_type(), &cast_type);
6366        assert_eq!(cast_array.len(), 3);
6367
6368        let dict = cast_array.as_dictionary::<UInt32Type>();
6369        assert_eq!(dict.values().data_type(), &target_value_type);
6370        // No dedup is performed for struct values — one row, one key.
6371        assert_eq!(dict.values().len(), 3);
6372
6373        // Source row 1 was a `Utf8`-null in the `name` field but the whole
6374        // struct row was valid (StructArray::from above takes per-field
6375        // nulls only). The dictionary's logical null mask therefore mirrors
6376        // the source struct's row-level null mask — all rows valid here.
6377        let keys = dict.keys();
6378        assert_eq!(keys.values(), &[0u32, 1, 2]);
6379        assert_eq!(keys.null_count(), 0);
6380
6381        let struct_values = dict.values().as_struct();
6382        let names_out = struct_values
6383            .column_by_name("name")
6384            .unwrap()
6385            .as_string_view();
6386        assert_eq!(names_out.value(0), "alpha");
6387        assert!(names_out.is_null(1));
6388        assert_eq!(names_out.value(2), "gamma");
6389        let ids_out = struct_values
6390            .column_by_name("id")
6391            .unwrap()
6392            .as_primitive::<Int64Type>();
6393        assert_eq!(ids_out.values(), &[1i64, 2, 3]);
6394    }
6395
6396    #[test]
6397    fn test_cast_struct_array_to_dict_struct_row_nulls() {
6398        // Row-level nulls on the source struct must surface as null keys on
6399        // the dictionary, since the dictionary's logical null mask is
6400        // determined by the keys.
6401        let names = StringArray::from(vec![Some("alpha"), Some("beta"), Some("gamma")]);
6402        let ids = Int32Array::from(vec![Some(1), Some(2), Some(3)]);
6403        let source = StructArray::try_new(
6404            vec![
6405                Field::new("name", DataType::Utf8, true),
6406                Field::new("id", DataType::Int32, false),
6407            ]
6408            .into(),
6409            vec![Arc::new(names) as ArrayRef, Arc::new(ids) as ArrayRef],
6410            Some(NullBuffer::from(vec![true, false, true])),
6411        )
6412        .unwrap();
6413
6414        let target_value_type = DataType::Struct(
6415            vec![
6416                Field::new("name", DataType::Utf8, true),
6417                Field::new("id", DataType::Int32, false),
6418            ]
6419            .into(),
6420        );
6421        let cast_type =
6422            DataType::Dictionary(Box::new(DataType::UInt32), Box::new(target_value_type));
6423
6424        let cast_array = cast(&source, &cast_type).unwrap();
6425        let dict = cast_array.as_dictionary::<UInt32Type>();
6426        assert_eq!(dict.len(), 3);
6427        let keys = dict.keys();
6428        assert!(!keys.is_null(0));
6429        assert!(keys.is_null(1));
6430        assert!(!keys.is_null(2));
6431    }
6432
6433    #[test]
6434    fn test_cast_struct_array_to_dict_struct_key_overflow() {
6435        // Source has 300 rows but the dictionary key type is UInt8 (max 255).
6436        // We must return a CastError instead of silently truncating.
6437        let n = 300;
6438        let names = StringArray::from((0..n).map(|i| Some(format!("v{i}"))).collect::<Vec<_>>());
6439        let source = StructArray::from(vec![(
6440            Arc::new(Field::new("name", DataType::Utf8, true)),
6441            Arc::new(names) as ArrayRef,
6442        )]);
6443
6444        let cast_type = DataType::Dictionary(
6445            Box::new(DataType::UInt8),
6446            Box::new(DataType::Struct(
6447                vec![Field::new("name", DataType::Utf8, true)].into(),
6448            )),
6449        );
6450        let err = cast(&source, &cast_type).unwrap_err().to_string();
6451        assert!(
6452            err.contains("Cannot fit") && err.contains("dictionary keys"),
6453            "expected key-overflow error, got: {err}"
6454        );
6455    }
6456
6457    #[test]
6458    fn test_cast_empty_string_array_to_dict_utf8_view() {
6459        let array = StringArray::from(Vec::<Option<&str>>::new());
6460
6461        let cast_type =
6462            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6463        assert!(can_cast_types(array.data_type(), &cast_type));
6464        let cast_array = cast(&array, &cast_type).unwrap();
6465        assert_eq!(cast_array.data_type(), &cast_type);
6466        assert_eq!(cast_array.len(), 0);
6467    }
6468
6469    #[test]
6470    fn test_cast_empty_binary_array_to_dict_binary_view() {
6471        let array = BinaryArray::from(Vec::<Option<&[u8]>>::new());
6472
6473        let cast_type =
6474            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6475        assert!(can_cast_types(array.data_type(), &cast_type));
6476        let cast_array = cast(&array, &cast_type).unwrap();
6477        assert_eq!(cast_array.data_type(), &cast_type);
6478        assert_eq!(cast_array.len(), 0);
6479    }
6480
6481    #[test]
6482    fn test_cast_all_null_string_array_to_dict_utf8_view() {
6483        let array = StringArray::from(vec![None::<&str>, None, None]);
6484
6485        let cast_type =
6486            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6487        assert!(can_cast_types(array.data_type(), &cast_type));
6488        let cast_array = cast(&array, &cast_type).unwrap();
6489        assert_eq!(cast_array.data_type(), &cast_type);
6490        assert_eq!(cast_array.null_count(), 3);
6491
6492        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6493        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6494        assert_eq!(dict_array.values().len(), 0);
6495        assert_eq!(dict_array.keys().null_count(), 3);
6496
6497        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6498        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6499        assert_eq!(actual, vec![None, None, None]);
6500    }
6501
6502    #[test]
6503    fn test_cast_all_null_binary_array_to_dict_binary_view() {
6504        let array = BinaryArray::from(vec![None::<&[u8]>, None, None]);
6505
6506        let cast_type =
6507            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6508        assert!(can_cast_types(array.data_type(), &cast_type));
6509        let cast_array = cast(&array, &cast_type).unwrap();
6510        assert_eq!(cast_array.data_type(), &cast_type);
6511        assert_eq!(cast_array.null_count(), 3);
6512
6513        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6514        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6515        assert_eq!(dict_array.values().len(), 0);
6516        assert_eq!(dict_array.keys().null_count(), 3);
6517
6518        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6519        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6520        assert_eq!(actual, vec![None, None, None]);
6521    }
6522
6523    #[test]
6524    fn test_numeric_to_binary() {
6525        let a = Int16Array::from(vec![Some(1), Some(511), None]);
6526
6527        let array_ref = cast(&a, &DataType::Binary).unwrap();
6528        let down_cast = array_ref.as_binary::<i32>();
6529        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6530        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6531        assert!(down_cast.is_null(2));
6532
6533        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6534
6535        let array_ref = cast(&a, &DataType::Binary).unwrap();
6536        let down_cast = array_ref.as_binary::<i32>();
6537        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6538        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6539        assert!(down_cast.is_null(2));
6540    }
6541
6542    #[test]
6543    fn test_numeric_to_large_binary() {
6544        let a = Int16Array::from(vec![Some(1), Some(511), None]);
6545
6546        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6547        let down_cast = array_ref.as_binary::<i64>();
6548        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6549        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6550        assert!(down_cast.is_null(2));
6551
6552        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6553
6554        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6555        let down_cast = array_ref.as_binary::<i64>();
6556        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6557        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6558        assert!(down_cast.is_null(2));
6559    }
6560
6561    #[test]
6562    fn test_cast_date32_to_int32() {
6563        let array = Date32Array::from(vec![10000, 17890]);
6564        let b = cast(&array, &DataType::Int32).unwrap();
6565        let c = b.as_primitive::<Int32Type>();
6566        assert_eq!(10000, c.value(0));
6567        assert_eq!(17890, c.value(1));
6568    }
6569
6570    #[test]
6571    fn test_cast_int32_to_date32() {
6572        let array = Int32Array::from(vec![10000, 17890]);
6573        let b = cast(&array, &DataType::Date32).unwrap();
6574        let c = b.as_primitive::<Date32Type>();
6575        assert_eq!(10000, c.value(0));
6576        assert_eq!(17890, c.value(1));
6577    }
6578
6579    #[test]
6580    fn test_cast_timestamp_to_date32() {
6581        let array =
6582            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6583                .with_timezone("+00:00".to_string());
6584        let b = cast(&array, &DataType::Date32).unwrap();
6585        let c = b.as_primitive::<Date32Type>();
6586        assert_eq!(10000, c.value(0));
6587        assert_eq!(17890, c.value(1));
6588        assert!(c.is_null(2));
6589    }
6590    #[test]
6591    fn test_cast_timestamp_to_date32_zone() {
6592        let strings = StringArray::from_iter([
6593            Some("1970-01-01T00:00:01"),
6594            Some("1970-01-01T23:59:59"),
6595            None,
6596            Some("2020-03-01T02:00:23+00:00"),
6597        ]);
6598        let dt = DataType::Timestamp(TimeUnit::Millisecond, Some("-07:00".into()));
6599        let timestamps = cast(&strings, &dt).unwrap();
6600        let dates = cast(timestamps.as_ref(), &DataType::Date32).unwrap();
6601
6602        let c = dates.as_primitive::<Date32Type>();
6603        let expected = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
6604        assert_eq!(c.value_as_date(0).unwrap(), expected);
6605        assert_eq!(c.value_as_date(1).unwrap(), expected);
6606        assert!(c.is_null(2));
6607        let expected = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
6608        assert_eq!(c.value_as_date(3).unwrap(), expected);
6609    }
6610    #[test]
6611    fn test_cast_timestamp_to_date64() {
6612        let array =
6613            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None]);
6614        let b = cast(&array, &DataType::Date64).unwrap();
6615        let c = b.as_primitive::<Date64Type>();
6616        assert_eq!(864000000005, c.value(0));
6617        assert_eq!(1545696000001, c.value(1));
6618        assert!(c.is_null(2));
6619
6620        let array = TimestampSecondArray::from(vec![Some(864000000005), Some(1545696000001)]);
6621        let b = cast(&array, &DataType::Date64).unwrap();
6622        let c = b.as_primitive::<Date64Type>();
6623        assert_eq!(864000000005000, c.value(0));
6624        assert_eq!(1545696000001000, c.value(1));
6625
6626        // test overflow, safe cast
6627        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6628        let b = cast(&array, &DataType::Date64).unwrap();
6629        assert!(b.is_null(0));
6630        // test overflow, unsafe cast
6631        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6632        let options = CastOptions {
6633            safe: false,
6634            format_options: FormatOptions::default(),
6635        };
6636        let b = cast_with_options(&array, &DataType::Date64, &options);
6637        assert!(b.is_err());
6638    }
6639
6640    #[test]
6641    fn test_cast_timestamp_to_time64() {
6642        // test timestamp secs
6643        let array = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6644            .with_timezone("+01:00".to_string());
6645        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6646        let c = b.as_primitive::<Time64MicrosecondType>();
6647        assert_eq!(3605000000, c.value(0));
6648        assert_eq!(3601000000, c.value(1));
6649        assert!(c.is_null(2));
6650        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6651        let c = b.as_primitive::<Time64NanosecondType>();
6652        assert_eq!(3605000000000, c.value(0));
6653        assert_eq!(3601000000000, c.value(1));
6654        assert!(c.is_null(2));
6655
6656        // test timestamp milliseconds
6657        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6658            .with_timezone("+01:00".to_string());
6659        let array = Arc::new(a) as ArrayRef;
6660        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6661        let c = b.as_primitive::<Time64MicrosecondType>();
6662        assert_eq!(3605000000, c.value(0));
6663        assert_eq!(3601000000, c.value(1));
6664        assert!(c.is_null(2));
6665        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6666        let c = b.as_primitive::<Time64NanosecondType>();
6667        assert_eq!(3605000000000, c.value(0));
6668        assert_eq!(3601000000000, c.value(1));
6669        assert!(c.is_null(2));
6670
6671        // test timestamp microseconds
6672        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6673            .with_timezone("+01:00".to_string());
6674        let array = Arc::new(a) as ArrayRef;
6675        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6676        let c = b.as_primitive::<Time64MicrosecondType>();
6677        assert_eq!(3605000000, c.value(0));
6678        assert_eq!(3601000000, c.value(1));
6679        assert!(c.is_null(2));
6680        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6681        let c = b.as_primitive::<Time64NanosecondType>();
6682        assert_eq!(3605000000000, c.value(0));
6683        assert_eq!(3601000000000, c.value(1));
6684        assert!(c.is_null(2));
6685
6686        // test timestamp nanoseconds
6687        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
6688            .with_timezone("+01:00".to_string());
6689        let array = Arc::new(a) as ArrayRef;
6690        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6691        let c = b.as_primitive::<Time64MicrosecondType>();
6692        assert_eq!(3605000000, c.value(0));
6693        assert_eq!(3601000000, c.value(1));
6694        assert!(c.is_null(2));
6695        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6696        let c = b.as_primitive::<Time64NanosecondType>();
6697        assert_eq!(3605000000000, c.value(0));
6698        assert_eq!(3601000000000, c.value(1));
6699        assert!(c.is_null(2));
6700
6701        // test overflow
6702        let a =
6703            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6704        let array = Arc::new(a) as ArrayRef;
6705        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond));
6706        assert!(b.is_err());
6707        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond));
6708        assert!(b.is_err());
6709        let b = cast(&array, &DataType::Time64(TimeUnit::Millisecond));
6710        assert!(b.is_err());
6711    }
6712
6713    #[test]
6714    fn test_cast_timestamp_to_time32() {
6715        // test timestamp secs
6716        let a = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6717            .with_timezone("+01:00".to_string());
6718        let array = Arc::new(a) as ArrayRef;
6719        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6720        let c = b.as_primitive::<Time32SecondType>();
6721        assert_eq!(3605, c.value(0));
6722        assert_eq!(3601, c.value(1));
6723        assert!(c.is_null(2));
6724        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6725        let c = b.as_primitive::<Time32MillisecondType>();
6726        assert_eq!(3605000, c.value(0));
6727        assert_eq!(3601000, c.value(1));
6728        assert!(c.is_null(2));
6729
6730        // test timestamp milliseconds
6731        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6732            .with_timezone("+01:00".to_string());
6733        let array = Arc::new(a) as ArrayRef;
6734        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6735        let c = b.as_primitive::<Time32SecondType>();
6736        assert_eq!(3605, c.value(0));
6737        assert_eq!(3601, c.value(1));
6738        assert!(c.is_null(2));
6739        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6740        let c = b.as_primitive::<Time32MillisecondType>();
6741        assert_eq!(3605000, c.value(0));
6742        assert_eq!(3601000, c.value(1));
6743        assert!(c.is_null(2));
6744
6745        // test timestamp microseconds
6746        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6747            .with_timezone("+01:00".to_string());
6748        let array = Arc::new(a) as ArrayRef;
6749        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6750        let c = b.as_primitive::<Time32SecondType>();
6751        assert_eq!(3605, c.value(0));
6752        assert_eq!(3601, c.value(1));
6753        assert!(c.is_null(2));
6754        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6755        let c = b.as_primitive::<Time32MillisecondType>();
6756        assert_eq!(3605000, c.value(0));
6757        assert_eq!(3601000, c.value(1));
6758        assert!(c.is_null(2));
6759
6760        // test timestamp nanoseconds
6761        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
6762            .with_timezone("+01:00".to_string());
6763        let array = Arc::new(a) as ArrayRef;
6764        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6765        let c = b.as_primitive::<Time32SecondType>();
6766        assert_eq!(3605, c.value(0));
6767        assert_eq!(3601, c.value(1));
6768        assert!(c.is_null(2));
6769        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6770        let c = b.as_primitive::<Time32MillisecondType>();
6771        assert_eq!(3605000, c.value(0));
6772        assert_eq!(3601000, c.value(1));
6773        assert!(c.is_null(2));
6774
6775        // test overflow
6776        let a =
6777            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6778        let array = Arc::new(a) as ArrayRef;
6779        let b = cast(&array, &DataType::Time32(TimeUnit::Second));
6780        assert!(b.is_err());
6781        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond));
6782        assert!(b.is_err());
6783    }
6784
6785    // Cast Timestamp(_, None) -> Timestamp(_, Some(timezone))
6786    #[test]
6787    fn test_cast_timestamp_with_timezone_1() {
6788        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6789            Some("2000-01-01T00:00:00.123456789"),
6790            Some("2010-01-01T00:00:00.123456789"),
6791            None,
6792        ]));
6793        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6794        let timestamp_array = cast(&string_array, &to_type).unwrap();
6795
6796        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6797        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6798
6799        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6800        let result = string_array.as_string::<i32>();
6801        assert_eq!("2000-01-01T00:00:00.123456+07:00", result.value(0));
6802        assert_eq!("2010-01-01T00:00:00.123456+07:00", result.value(1));
6803        assert!(result.is_null(2));
6804    }
6805
6806    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, None)
6807    #[test]
6808    fn test_cast_timestamp_with_timezone_2() {
6809        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6810            Some("2000-01-01T07:00:00.123456789"),
6811            Some("2010-01-01T07:00:00.123456789"),
6812            None,
6813        ]));
6814        let to_type = DataType::Timestamp(TimeUnit::Millisecond, Some("+0700".into()));
6815        let timestamp_array = cast(&string_array, &to_type).unwrap();
6816
6817        // Check intermediate representation is correct
6818        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6819        let result = string_array.as_string::<i32>();
6820        assert_eq!("2000-01-01T07:00:00.123+07:00", result.value(0));
6821        assert_eq!("2010-01-01T07:00:00.123+07:00", result.value(1));
6822        assert!(result.is_null(2));
6823
6824        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6825        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6826
6827        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6828        let result = string_array.as_string::<i32>();
6829        assert_eq!("2000-01-01T00:00:00.123", result.value(0));
6830        assert_eq!("2010-01-01T00:00:00.123", result.value(1));
6831        assert!(result.is_null(2));
6832    }
6833
6834    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, Some(timezone))
6835    #[test]
6836    fn test_cast_timestamp_with_timezone_3() {
6837        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6838            Some("2000-01-01T07:00:00.123456789"),
6839            Some("2010-01-01T07:00:00.123456789"),
6840            None,
6841        ]));
6842        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6843        let timestamp_array = cast(&string_array, &to_type).unwrap();
6844
6845        // Check intermediate representation is correct
6846        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6847        let result = string_array.as_string::<i32>();
6848        assert_eq!("2000-01-01T07:00:00.123456+07:00", result.value(0));
6849        assert_eq!("2010-01-01T07:00:00.123456+07:00", result.value(1));
6850        assert!(result.is_null(2));
6851
6852        let to_type = DataType::Timestamp(TimeUnit::Second, Some("-08:00".into()));
6853        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6854
6855        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6856        let result = string_array.as_string::<i32>();
6857        assert_eq!("1999-12-31T16:00:00-08:00", result.value(0));
6858        assert_eq!("2009-12-31T16:00:00-08:00", result.value(1));
6859        assert!(result.is_null(2));
6860    }
6861
6862    #[test]
6863    fn test_cast_date64_to_timestamp() {
6864        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6865        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6866        let c = b.as_primitive::<TimestampSecondType>();
6867        assert_eq!(864000000, c.value(0));
6868        assert_eq!(1545696000, c.value(1));
6869        assert!(c.is_null(2));
6870    }
6871
6872    #[test]
6873    fn test_cast_date64_to_timestamp_ms() {
6874        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6875        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
6876        let c = b
6877            .as_any()
6878            .downcast_ref::<TimestampMillisecondArray>()
6879            .unwrap();
6880        assert_eq!(864000000005, c.value(0));
6881        assert_eq!(1545696000001, c.value(1));
6882        assert!(c.is_null(2));
6883    }
6884
6885    #[test]
6886    fn test_cast_date64_to_timestamp_us() {
6887        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6888        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
6889        let c = b
6890            .as_any()
6891            .downcast_ref::<TimestampMicrosecondArray>()
6892            .unwrap();
6893        assert_eq!(864000000005000, c.value(0));
6894        assert_eq!(1545696000001000, c.value(1));
6895        assert!(c.is_null(2));
6896    }
6897
6898    #[test]
6899    fn test_cast_date64_to_timestamp_ns() {
6900        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6901        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
6902        let c = b
6903            .as_any()
6904            .downcast_ref::<TimestampNanosecondArray>()
6905            .unwrap();
6906        assert_eq!(864000000005000000, c.value(0));
6907        assert_eq!(1545696000001000000, c.value(1));
6908        assert!(c.is_null(2));
6909    }
6910
6911    #[test]
6912    fn test_cast_timestamp_to_i64() {
6913        let array =
6914            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6915                .with_timezone("UTC".to_string());
6916        let b = cast(&array, &DataType::Int64).unwrap();
6917        let c = b.as_primitive::<Int64Type>();
6918        assert_eq!(&DataType::Int64, c.data_type());
6919        assert_eq!(864000000005, c.value(0));
6920        assert_eq!(1545696000001, c.value(1));
6921        assert!(c.is_null(2));
6922    }
6923
6924    macro_rules! assert_cast {
6925        ($array:expr, $datatype:expr, $output_array_type: ty, $expected:expr) => {{
6926            assert!(can_cast_types($array.data_type(), &$datatype));
6927            let out = cast(&$array, &$datatype).unwrap();
6928            let actual = out
6929                .as_any()
6930                .downcast_ref::<$output_array_type>()
6931                .unwrap()
6932                .into_iter()
6933                .collect::<Vec<_>>();
6934            assert_eq!(actual, $expected);
6935        }};
6936        ($array:expr, $datatype:expr, $output_array_type: ty, $options:expr, $expected:expr) => {{
6937            assert!(can_cast_types($array.data_type(), &$datatype));
6938            let out = cast_with_options(&$array, &$datatype, &$options).unwrap();
6939            let actual = out
6940                .as_any()
6941                .downcast_ref::<$output_array_type>()
6942                .unwrap()
6943                .into_iter()
6944                .collect::<Vec<_>>();
6945            assert_eq!(actual, $expected);
6946        }};
6947    }
6948
6949    #[test]
6950    fn test_cast_date32_to_string() {
6951        let array = Date32Array::from(vec![Some(0), Some(10000), Some(13036), Some(17890), None]);
6952        let expected = vec![
6953            Some("1970-01-01"),
6954            Some("1997-05-19"),
6955            Some("2005-09-10"),
6956            Some("2018-12-25"),
6957            None,
6958        ];
6959
6960        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6961        assert_cast!(array, DataType::Utf8, StringArray, expected);
6962        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6963    }
6964
6965    #[test]
6966    fn test_cast_date64_to_string() {
6967        let array = Date64Array::from(vec![
6968            Some(0),
6969            Some(10000 * 86400000),
6970            Some(13036 * 86400000),
6971            Some(17890 * 86400000),
6972            None,
6973        ]);
6974        let expected = vec![
6975            Some("1970-01-01T00:00:00"),
6976            Some("1997-05-19T00:00:00"),
6977            Some("2005-09-10T00:00:00"),
6978            Some("2018-12-25T00:00:00"),
6979            None,
6980        ];
6981
6982        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6983        assert_cast!(array, DataType::Utf8, StringArray, expected);
6984        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6985    }
6986
6987    #[test]
6988    fn test_cast_date32_to_timestamp_and_timestamp_with_timezone() {
6989        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6990        let a = Date32Array::from(vec![Some(18628), None, None]); // 2021-1-1, 2022-1-1
6991        let array = Arc::new(a) as ArrayRef;
6992
6993        let b = cast(
6994            &array,
6995            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6996        )
6997        .unwrap();
6998        let c = b.as_primitive::<TimestampSecondType>();
6999        let string_array = cast(&c, &DataType::Utf8).unwrap();
7000        let result = string_array.as_string::<i32>();
7001        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7002
7003        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
7004        let c = b.as_primitive::<TimestampSecondType>();
7005        let string_array = cast(&c, &DataType::Utf8).unwrap();
7006        let result = string_array.as_string::<i32>();
7007        assert_eq!("2021-01-01T00:00:00", result.value(0));
7008    }
7009
7010    #[test]
7011    fn test_cast_date32_to_timestamp_with_timezone() {
7012        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7013        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7014        let array = Arc::new(a) as ArrayRef;
7015        let b = cast(
7016            &array,
7017            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
7018        )
7019        .unwrap();
7020        let c = b.as_primitive::<TimestampSecondType>();
7021        assert_eq!(1609438500, c.value(0));
7022        assert_eq!(1640974500, c.value(1));
7023        assert!(c.is_null(2));
7024
7025        let string_array = cast(&c, &DataType::Utf8).unwrap();
7026        let result = string_array.as_string::<i32>();
7027        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7028        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7029    }
7030
7031    #[test]
7032    fn test_cast_date32_to_timestamp_with_timezone_ms() {
7033        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7034        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7035        let array = Arc::new(a) as ArrayRef;
7036        let b = cast(
7037            &array,
7038            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
7039        )
7040        .unwrap();
7041        let c = b.as_primitive::<TimestampMillisecondType>();
7042        assert_eq!(1609438500000, c.value(0));
7043        assert_eq!(1640974500000, c.value(1));
7044        assert!(c.is_null(2));
7045
7046        let string_array = cast(&c, &DataType::Utf8).unwrap();
7047        let result = string_array.as_string::<i32>();
7048        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7049        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7050    }
7051
7052    #[test]
7053    fn test_cast_date32_to_timestamp_with_timezone_us() {
7054        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7055        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7056        let array = Arc::new(a) as ArrayRef;
7057        let b = cast(
7058            &array,
7059            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
7060        )
7061        .unwrap();
7062        let c = b.as_primitive::<TimestampMicrosecondType>();
7063        assert_eq!(1609438500000000, c.value(0));
7064        assert_eq!(1640974500000000, c.value(1));
7065        assert!(c.is_null(2));
7066
7067        let string_array = cast(&c, &DataType::Utf8).unwrap();
7068        let result = string_array.as_string::<i32>();
7069        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7070        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7071    }
7072
7073    #[test]
7074    fn test_cast_date32_to_timestamp_with_timezone_ns() {
7075        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7076        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7077        let array = Arc::new(a) as ArrayRef;
7078        let b = cast(
7079            &array,
7080            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
7081        )
7082        .unwrap();
7083        let c = b.as_primitive::<TimestampNanosecondType>();
7084        assert_eq!(1609438500000000000, c.value(0));
7085        assert_eq!(1640974500000000000, c.value(1));
7086        assert!(c.is_null(2));
7087
7088        let string_array = cast(&c, &DataType::Utf8).unwrap();
7089        let result = string_array.as_string::<i32>();
7090        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7091        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7092    }
7093
7094    #[test]
7095    fn test_cast_date64_to_timestamp_with_timezone() {
7096        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7097        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7098        let b = cast(
7099            &array,
7100            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
7101        )
7102        .unwrap();
7103
7104        let c = b.as_primitive::<TimestampSecondType>();
7105        assert_eq!(863979300, c.value(0));
7106        assert_eq!(1545675300, c.value(1));
7107        assert!(c.is_null(2));
7108
7109        let string_array = cast(&c, &DataType::Utf8).unwrap();
7110        let result = string_array.as_string::<i32>();
7111        assert_eq!("1997-05-19T00:00:00+05:45", result.value(0));
7112        assert_eq!("2018-12-25T00:00:00+05:45", result.value(1));
7113    }
7114
7115    #[test]
7116    fn test_cast_date64_to_timestamp_with_timezone_ms() {
7117        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7118        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7119        let b = cast(
7120            &array,
7121            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
7122        )
7123        .unwrap();
7124
7125        let c = b.as_primitive::<TimestampMillisecondType>();
7126        assert_eq!(863979300005, c.value(0));
7127        assert_eq!(1545675300001, c.value(1));
7128        assert!(c.is_null(2));
7129
7130        let string_array = cast(&c, &DataType::Utf8).unwrap();
7131        let result = string_array.as_string::<i32>();
7132        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
7133        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7134    }
7135
7136    #[test]
7137    fn test_cast_date64_to_timestamp_with_timezone_us() {
7138        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7139        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7140        let b = cast(
7141            &array,
7142            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
7143        )
7144        .unwrap();
7145
7146        let c = b.as_primitive::<TimestampMicrosecondType>();
7147        assert_eq!(863979300005000, c.value(0));
7148        assert_eq!(1545675300001000, c.value(1));
7149        assert!(c.is_null(2));
7150
7151        let string_array = cast(&c, &DataType::Utf8).unwrap();
7152        let result = string_array.as_string::<i32>();
7153        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
7154        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7155    }
7156
7157    #[test]
7158    fn test_cast_date64_to_timestamp_with_timezone_ns() {
7159        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7160        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7161        let b = cast(
7162            &array,
7163            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
7164        )
7165        .unwrap();
7166
7167        let c = b.as_primitive::<TimestampNanosecondType>();
7168        assert_eq!(863979300005000000, c.value(0));
7169        assert_eq!(1545675300001000000, c.value(1));
7170        assert!(c.is_null(2));
7171
7172        let string_array = cast(&c, &DataType::Utf8).unwrap();
7173        let result = string_array.as_string::<i32>();
7174        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
7175        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7176    }
7177
7178    #[test]
7179    fn test_cast_timestamp_to_strings() {
7180        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
7181        let array =
7182            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7183        let expected = vec![
7184            Some("1997-05-19T00:00:03.005"),
7185            Some("2018-12-25T00:00:02.001"),
7186            None,
7187        ];
7188
7189        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
7190        assert_cast!(array, DataType::Utf8, StringArray, expected);
7191        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
7192    }
7193
7194    #[test]
7195    fn test_cast_timestamp_to_strings_opt() {
7196        let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
7197        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7198        let cast_options = CastOptions {
7199            safe: true,
7200            format_options: FormatOptions::default()
7201                .with_timestamp_format(Some(ts_format))
7202                .with_timestamp_tz_format(Some(ts_format)),
7203        };
7204
7205        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
7206        let array_without_tz =
7207            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7208        let expected = vec![
7209            Some("1997-05-19 00:00:03.005000"),
7210            Some("2018-12-25 00:00:02.001000"),
7211            None,
7212        ];
7213        assert_cast!(
7214            array_without_tz,
7215            DataType::Utf8View,
7216            StringViewArray,
7217            cast_options,
7218            expected
7219        );
7220        assert_cast!(
7221            array_without_tz,
7222            DataType::Utf8,
7223            StringArray,
7224            cast_options,
7225            expected
7226        );
7227        assert_cast!(
7228            array_without_tz,
7229            DataType::LargeUtf8,
7230            LargeStringArray,
7231            cast_options,
7232            expected
7233        );
7234
7235        let array_with_tz =
7236            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None])
7237                .with_timezone(tz.to_string());
7238        let expected = vec![
7239            Some("1997-05-19 05:45:03.005000"),
7240            Some("2018-12-25 05:45:02.001000"),
7241            None,
7242        ];
7243        assert_cast!(
7244            array_with_tz,
7245            DataType::Utf8View,
7246            StringViewArray,
7247            cast_options,
7248            expected
7249        );
7250        assert_cast!(
7251            array_with_tz,
7252            DataType::Utf8,
7253            StringArray,
7254            cast_options,
7255            expected
7256        );
7257        assert_cast!(
7258            array_with_tz,
7259            DataType::LargeUtf8,
7260            LargeStringArray,
7261            cast_options,
7262            expected
7263        );
7264    }
7265
7266    #[test]
7267    fn test_cast_between_timestamps() {
7268        let array =
7269            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7270        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
7271        let c = b.as_primitive::<TimestampSecondType>();
7272        assert_eq!(864000003, c.value(0));
7273        assert_eq!(1545696002, c.value(1));
7274        assert!(c.is_null(2));
7275    }
7276
7277    #[test]
7278    fn test_cast_duration_to_i64() {
7279        let base = vec![5, 6, 7, 8, 100000000];
7280
7281        let duration_arrays = vec![
7282            Arc::new(DurationNanosecondArray::from(base.clone())) as ArrayRef,
7283            Arc::new(DurationMicrosecondArray::from(base.clone())) as ArrayRef,
7284            Arc::new(DurationMillisecondArray::from(base.clone())) as ArrayRef,
7285            Arc::new(DurationSecondArray::from(base.clone())) as ArrayRef,
7286        ];
7287
7288        for arr in duration_arrays {
7289            assert!(can_cast_types(arr.data_type(), &DataType::Int64));
7290            let result = cast(&arr, &DataType::Int64).unwrap();
7291            let result = result.as_primitive::<Int64Type>();
7292            assert_eq!(base.as_slice(), result.values());
7293        }
7294    }
7295
7296    #[test]
7297    fn test_cast_between_durations_and_numerics() {
7298        fn test_cast_between_durations<FromType, ToType>()
7299        where
7300            FromType: ArrowPrimitiveType<Native = i64>,
7301            ToType: ArrowPrimitiveType<Native = i64>,
7302            PrimitiveArray<FromType>: From<Vec<Option<i64>>>,
7303        {
7304            let from_unit = match FromType::DATA_TYPE {
7305                DataType::Duration(unit) => unit,
7306                _ => panic!("Expected a duration type"),
7307            };
7308            let to_unit = match ToType::DATA_TYPE {
7309                DataType::Duration(unit) => unit,
7310                _ => panic!("Expected a duration type"),
7311            };
7312            let from_size = time_unit_multiple(&from_unit);
7313            let to_size = time_unit_multiple(&to_unit);
7314
7315            let (v1_before, v2_before) = (8640003005, 1696002001);
7316            let (v1_after, v2_after) = if from_size >= to_size {
7317                (
7318                    v1_before / (from_size / to_size),
7319                    v2_before / (from_size / to_size),
7320                )
7321            } else {
7322                (
7323                    v1_before * (to_size / from_size),
7324                    v2_before * (to_size / from_size),
7325                )
7326            };
7327
7328            let array =
7329                PrimitiveArray::<FromType>::from(vec![Some(v1_before), Some(v2_before), None]);
7330            let b = cast(&array, &ToType::DATA_TYPE).unwrap();
7331            let c = b.as_primitive::<ToType>();
7332            assert_eq!(v1_after, c.value(0));
7333            assert_eq!(v2_after, c.value(1));
7334            assert!(c.is_null(2));
7335        }
7336
7337        // between each individual duration type
7338        test_cast_between_durations::<DurationSecondType, DurationMillisecondType>();
7339        test_cast_between_durations::<DurationSecondType, DurationMicrosecondType>();
7340        test_cast_between_durations::<DurationSecondType, DurationNanosecondType>();
7341        test_cast_between_durations::<DurationMillisecondType, DurationSecondType>();
7342        test_cast_between_durations::<DurationMillisecondType, DurationMicrosecondType>();
7343        test_cast_between_durations::<DurationMillisecondType, DurationNanosecondType>();
7344        test_cast_between_durations::<DurationMicrosecondType, DurationSecondType>();
7345        test_cast_between_durations::<DurationMicrosecondType, DurationMillisecondType>();
7346        test_cast_between_durations::<DurationMicrosecondType, DurationNanosecondType>();
7347        test_cast_between_durations::<DurationNanosecondType, DurationSecondType>();
7348        test_cast_between_durations::<DurationNanosecondType, DurationMillisecondType>();
7349        test_cast_between_durations::<DurationNanosecondType, DurationMicrosecondType>();
7350
7351        // cast failed
7352        let array = DurationSecondArray::from(vec![
7353            Some(i64::MAX),
7354            Some(8640203410378005),
7355            Some(10241096),
7356            None,
7357        ]);
7358        let b = cast(&array, &DataType::Duration(TimeUnit::Nanosecond)).unwrap();
7359        let c = b.as_primitive::<DurationNanosecondType>();
7360        assert!(c.is_null(0));
7361        assert!(c.is_null(1));
7362        assert_eq!(10241096000000000, c.value(2));
7363        assert!(c.is_null(3));
7364
7365        // durations to numerics
7366        let array = DurationSecondArray::from(vec![
7367            Some(i64::MAX),
7368            Some(8640203410378005),
7369            Some(10241096),
7370            None,
7371        ]);
7372        let b = cast(&array, &DataType::Int64).unwrap();
7373        let c = b.as_primitive::<Int64Type>();
7374        assert_eq!(i64::MAX, c.value(0));
7375        assert_eq!(8640203410378005, c.value(1));
7376        assert_eq!(10241096, c.value(2));
7377        assert!(c.is_null(3));
7378
7379        let b = cast(&array, &DataType::Int32).unwrap();
7380        let c = b.as_primitive::<Int32Type>();
7381        assert_eq!(0, c.value(0));
7382        assert_eq!(0, c.value(1));
7383        assert_eq!(10241096, c.value(2));
7384        assert!(c.is_null(3));
7385
7386        // numerics to durations
7387        let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), Some(10241096), None]);
7388        let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
7389        let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
7390        assert_eq!(i32::MAX as i64, c.value(0));
7391        assert_eq!(802034103, c.value(1));
7392        assert_eq!(10241096, c.value(2));
7393        assert!(c.is_null(3));
7394    }
7395
7396    #[test]
7397    fn test_cast_to_strings() {
7398        let a = Int32Array::from(vec![1, 2, 3]);
7399        let out = cast(&a, &DataType::Utf8).unwrap();
7400        let out = out
7401            .as_any()
7402            .downcast_ref::<StringArray>()
7403            .unwrap()
7404            .into_iter()
7405            .collect::<Vec<_>>();
7406        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
7407        let out = cast(&a, &DataType::LargeUtf8).unwrap();
7408        let out = out
7409            .as_any()
7410            .downcast_ref::<LargeStringArray>()
7411            .unwrap()
7412            .into_iter()
7413            .collect::<Vec<_>>();
7414        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
7415    }
7416
7417    #[test]
7418    fn test_str_to_str_casts() {
7419        for data in [
7420            vec![Some("foo"), Some("bar"), Some("ham")],
7421            vec![Some("foo"), None, Some("bar")],
7422        ] {
7423            let a = LargeStringArray::from(data.clone());
7424            let to = cast(&a, &DataType::Utf8).unwrap();
7425            let expect = a
7426                .as_any()
7427                .downcast_ref::<LargeStringArray>()
7428                .unwrap()
7429                .into_iter()
7430                .collect::<Vec<_>>();
7431            let out = to
7432                .as_any()
7433                .downcast_ref::<StringArray>()
7434                .unwrap()
7435                .into_iter()
7436                .collect::<Vec<_>>();
7437            assert_eq!(expect, out);
7438
7439            let a = StringArray::from(data);
7440            let to = cast(&a, &DataType::LargeUtf8).unwrap();
7441            let expect = a
7442                .as_any()
7443                .downcast_ref::<StringArray>()
7444                .unwrap()
7445                .into_iter()
7446                .collect::<Vec<_>>();
7447            let out = to
7448                .as_any()
7449                .downcast_ref::<LargeStringArray>()
7450                .unwrap()
7451                .into_iter()
7452                .collect::<Vec<_>>();
7453            assert_eq!(expect, out);
7454        }
7455    }
7456
7457    const VIEW_TEST_DATA: [Option<&str>; 5] = [
7458        Some("hello"),
7459        Some("repeated"),
7460        None,
7461        Some("large payload over 12 bytes"),
7462        Some("repeated"),
7463    ];
7464
7465    #[test]
7466    fn test_string_view_to_binary_view() {
7467        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7468
7469        assert!(can_cast_types(
7470            string_view_array.data_type(),
7471            &DataType::BinaryView
7472        ));
7473
7474        let binary_view_array = cast(&string_view_array, &DataType::BinaryView).unwrap();
7475        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7476
7477        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7478        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7479    }
7480
7481    #[test]
7482    fn test_binary_view_to_string_view() {
7483        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7484
7485        assert!(can_cast_types(
7486            binary_view_array.data_type(),
7487            &DataType::Utf8View
7488        ));
7489
7490        let string_view_array = cast(&binary_view_array, &DataType::Utf8View).unwrap();
7491        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7492
7493        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7494        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7495    }
7496
7497    #[test]
7498    fn test_binary_view_to_string_view_with_invalid_utf8() {
7499        let binary_view_array = BinaryViewArray::from_iter(vec![
7500            Some("valid".as_bytes()),
7501            Some(&[0xff]),
7502            Some("utf8".as_bytes()),
7503            None,
7504        ]);
7505
7506        let strict_options = CastOptions {
7507            safe: false,
7508            ..Default::default()
7509        };
7510
7511        assert!(
7512            cast_with_options(&binary_view_array, &DataType::Utf8View, &strict_options).is_err()
7513        );
7514
7515        let safe_options = CastOptions {
7516            safe: true,
7517            ..Default::default()
7518        };
7519
7520        let string_view_array =
7521            cast_with_options(&binary_view_array, &DataType::Utf8View, &safe_options).unwrap();
7522        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7523
7524        let values: Vec<_> = string_view_array.as_string_view().iter().collect();
7525
7526        assert_eq!(values, vec![Some("valid"), None, Some("utf8"), None]);
7527    }
7528
7529    #[test]
7530    fn test_string_to_view() {
7531        _test_string_to_view::<i32>();
7532        _test_string_to_view::<i64>();
7533    }
7534
7535    fn _test_string_to_view<O>()
7536    where
7537        O: OffsetSizeTrait,
7538    {
7539        let string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7540
7541        assert!(can_cast_types(
7542            string_array.data_type(),
7543            &DataType::Utf8View
7544        ));
7545
7546        assert!(can_cast_types(
7547            string_array.data_type(),
7548            &DataType::BinaryView
7549        ));
7550
7551        let string_view_array = cast(&string_array, &DataType::Utf8View).unwrap();
7552        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7553
7554        let binary_view_array = cast(&string_array, &DataType::BinaryView).unwrap();
7555        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7556
7557        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7558        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7559
7560        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7561        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7562    }
7563
7564    #[test]
7565    fn test_bianry_to_view() {
7566        _test_binary_to_view::<i32>();
7567        _test_binary_to_view::<i64>();
7568    }
7569
7570    fn _test_binary_to_view<O>()
7571    where
7572        O: OffsetSizeTrait,
7573    {
7574        let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7575
7576        assert!(can_cast_types(
7577            binary_array.data_type(),
7578            &DataType::Utf8View
7579        ));
7580
7581        assert!(can_cast_types(
7582            binary_array.data_type(),
7583            &DataType::BinaryView
7584        ));
7585
7586        let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
7587        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7588
7589        let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
7590        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7591
7592        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7593        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7594
7595        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7596        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7597    }
7598
7599    #[test]
7600    fn test_dict_to_view() {
7601        let values = StringArray::from_iter(VIEW_TEST_DATA);
7602        let keys = Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
7603        let string_dict_array =
7604            DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
7605        let typed_dict = string_dict_array.downcast_dict::<StringArray>().unwrap();
7606
7607        let string_view_array = {
7608            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7609            for v in typed_dict.into_iter() {
7610                builder.append_option(v);
7611            }
7612            builder.finish()
7613        };
7614        let expected_string_array_type = string_view_array.data_type();
7615        let casted_string_array = cast(&string_dict_array, expected_string_array_type).unwrap();
7616        assert_eq!(casted_string_array.data_type(), expected_string_array_type);
7617        assert_eq!(casted_string_array.as_ref(), &string_view_array);
7618
7619        let binary_buffer = cast(&typed_dict.values(), &DataType::Binary).unwrap();
7620        let binary_dict_array =
7621            DictionaryArray::<Int8Type>::new(typed_dict.keys().clone(), binary_buffer);
7622        let typed_binary_dict = binary_dict_array.downcast_dict::<BinaryArray>().unwrap();
7623
7624        let binary_view_array = {
7625            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7626            for v in typed_binary_dict.into_iter() {
7627                builder.append_option(v);
7628            }
7629            builder.finish()
7630        };
7631        let expected_binary_array_type = binary_view_array.data_type();
7632        let casted_binary_array = cast(&binary_dict_array, expected_binary_array_type).unwrap();
7633        assert_eq!(casted_binary_array.data_type(), expected_binary_array_type);
7634        assert_eq!(casted_binary_array.as_ref(), &binary_view_array);
7635    }
7636
7637    #[test]
7638    fn test_view_to_dict() {
7639        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7640        let string_dict_array: DictionaryArray<Int8Type> = VIEW_TEST_DATA.into_iter().collect();
7641        let casted_type = string_dict_array.data_type();
7642        let casted_dict_array = cast(&string_view_array, casted_type).unwrap();
7643        assert_eq!(casted_dict_array.data_type(), casted_type);
7644        assert_eq!(casted_dict_array.as_ref(), &string_dict_array);
7645
7646        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7647        let binary_dict_array = string_dict_array.downcast_dict::<StringArray>().unwrap();
7648        let binary_buffer = cast(&binary_dict_array.values(), &DataType::Binary).unwrap();
7649        let binary_dict_array =
7650            DictionaryArray::<Int8Type>::new(binary_dict_array.keys().clone(), binary_buffer);
7651        let casted_type = binary_dict_array.data_type();
7652        let casted_binary_array = cast(&binary_view_array, casted_type).unwrap();
7653        assert_eq!(casted_binary_array.data_type(), casted_type);
7654        assert_eq!(casted_binary_array.as_ref(), &binary_dict_array);
7655    }
7656
7657    #[test]
7658    fn test_view_to_string() {
7659        _test_view_to_string::<i32>();
7660        _test_view_to_string::<i64>();
7661    }
7662
7663    fn _test_view_to_string<O>()
7664    where
7665        O: OffsetSizeTrait,
7666    {
7667        let string_view_array = {
7668            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7669            for s in VIEW_TEST_DATA.iter() {
7670                builder.append_option(*s);
7671            }
7672            builder.finish()
7673        };
7674
7675        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7676
7677        let expected_string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7678        let expected_type = expected_string_array.data_type();
7679
7680        assert!(can_cast_types(string_view_array.data_type(), expected_type));
7681        assert!(can_cast_types(binary_view_array.data_type(), expected_type));
7682
7683        let string_view_casted_array = cast(&string_view_array, expected_type).unwrap();
7684        assert_eq!(string_view_casted_array.data_type(), expected_type);
7685        assert_eq!(string_view_casted_array.as_ref(), &expected_string_array);
7686
7687        let binary_view_casted_array = cast(&binary_view_array, expected_type).unwrap();
7688        assert_eq!(binary_view_casted_array.data_type(), expected_type);
7689        assert_eq!(binary_view_casted_array.as_ref(), &expected_string_array);
7690    }
7691
7692    #[test]
7693    fn test_view_to_binary() {
7694        _test_view_to_binary::<i32>();
7695        _test_view_to_binary::<i64>();
7696    }
7697
7698    fn _test_view_to_binary<O>()
7699    where
7700        O: OffsetSizeTrait,
7701    {
7702        let view_array = {
7703            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7704            for s in VIEW_TEST_DATA.iter() {
7705                builder.append_option(*s);
7706            }
7707            builder.finish()
7708        };
7709
7710        let expected_binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7711        let expected_type = expected_binary_array.data_type();
7712
7713        assert!(can_cast_types(view_array.data_type(), expected_type));
7714
7715        let binary_array = cast(&view_array, expected_type).unwrap();
7716        assert_eq!(binary_array.data_type(), expected_type);
7717
7718        assert_eq!(binary_array.as_ref(), &expected_binary_array);
7719    }
7720
7721    #[test]
7722    fn test_cast_from_f64() {
7723        let f64_values: Vec<f64> = vec![
7724            i64::MIN as f64,
7725            i32::MIN as f64,
7726            i16::MIN as f64,
7727            i8::MIN as f64,
7728            0_f64,
7729            u8::MAX as f64,
7730            u16::MAX as f64,
7731            u32::MAX as f64,
7732            u64::MAX as f64,
7733        ];
7734        let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values));
7735
7736        let f64_expected = vec![
7737            -9223372036854776000.0,
7738            -2147483648.0,
7739            -32768.0,
7740            -128.0,
7741            0.0,
7742            255.0,
7743            65535.0,
7744            4294967295.0,
7745            18446744073709552000.0,
7746        ];
7747        assert_eq!(
7748            f64_expected,
7749            get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
7750                .iter()
7751                .map(|i| i.parse::<f64>().unwrap())
7752                .collect::<Vec<f64>>()
7753        );
7754
7755        let f32_expected = vec![
7756            -9223372000000000000.0,
7757            -2147483600.0,
7758            -32768.0,
7759            -128.0,
7760            0.0,
7761            255.0,
7762            65535.0,
7763            4294967300.0,
7764            18446744000000000000.0,
7765        ];
7766        assert_eq!(
7767            f32_expected,
7768            get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
7769                .iter()
7770                .map(|i| i.parse::<f32>().unwrap())
7771                .collect::<Vec<f32>>()
7772        );
7773
7774        let f16_expected = vec![
7775            f16::from_f64(-9223372000000000000.0),
7776            f16::from_f64(-2147483600.0),
7777            f16::from_f64(-32768.0),
7778            f16::from_f64(-128.0),
7779            f16::from_f64(0.0),
7780            f16::from_f64(255.0),
7781            f16::from_f64(65535.0),
7782            f16::from_f64(4294967300.0),
7783            f16::from_f64(18446744000000000000.0),
7784        ];
7785        assert_eq!(
7786            f16_expected,
7787            get_cast_values::<Float16Type>(&f64_array, &DataType::Float16)
7788                .iter()
7789                .map(|i| i.parse::<f16>().unwrap())
7790                .collect::<Vec<f16>>()
7791        );
7792
7793        let i64_expected = vec![
7794            "-9223372036854775808",
7795            "-2147483648",
7796            "-32768",
7797            "-128",
7798            "0",
7799            "255",
7800            "65535",
7801            "4294967295",
7802            "null",
7803        ];
7804        assert_eq!(
7805            i64_expected,
7806            get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
7807        );
7808
7809        let i32_expected = vec![
7810            "null",
7811            "-2147483648",
7812            "-32768",
7813            "-128",
7814            "0",
7815            "255",
7816            "65535",
7817            "null",
7818            "null",
7819        ];
7820        assert_eq!(
7821            i32_expected,
7822            get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
7823        );
7824
7825        let i16_expected = vec![
7826            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7827        ];
7828        assert_eq!(
7829            i16_expected,
7830            get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
7831        );
7832
7833        let i8_expected = vec![
7834            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7835        ];
7836        assert_eq!(
7837            i8_expected,
7838            get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
7839        );
7840
7841        let u64_expected = vec![
7842            "null",
7843            "null",
7844            "null",
7845            "null",
7846            "0",
7847            "255",
7848            "65535",
7849            "4294967295",
7850            "null",
7851        ];
7852        assert_eq!(
7853            u64_expected,
7854            get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
7855        );
7856
7857        let u32_expected = vec![
7858            "null",
7859            "null",
7860            "null",
7861            "null",
7862            "0",
7863            "255",
7864            "65535",
7865            "4294967295",
7866            "null",
7867        ];
7868        assert_eq!(
7869            u32_expected,
7870            get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
7871        );
7872
7873        let u16_expected = vec![
7874            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7875        ];
7876        assert_eq!(
7877            u16_expected,
7878            get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
7879        );
7880
7881        let u8_expected = vec![
7882            "null", "null", "null", "null", "0", "255", "null", "null", "null",
7883        ];
7884        assert_eq!(
7885            u8_expected,
7886            get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
7887        );
7888    }
7889
7890    #[test]
7891    fn test_cast_from_f32() {
7892        let f32_values: Vec<f32> = vec![
7893            i32::MIN as f32,
7894            i32::MIN as f32,
7895            i16::MIN as f32,
7896            i8::MIN as f32,
7897            0_f32,
7898            u8::MAX as f32,
7899            u16::MAX as f32,
7900            u32::MAX as f32,
7901            u32::MAX as f32,
7902        ];
7903        let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values));
7904
7905        let f64_expected = vec![
7906            "-2147483648.0",
7907            "-2147483648.0",
7908            "-32768.0",
7909            "-128.0",
7910            "0.0",
7911            "255.0",
7912            "65535.0",
7913            "4294967296.0",
7914            "4294967296.0",
7915        ];
7916        assert_eq!(
7917            f64_expected,
7918            get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
7919        );
7920
7921        let f32_expected = vec![
7922            "-2147483600.0",
7923            "-2147483600.0",
7924            "-32768.0",
7925            "-128.0",
7926            "0.0",
7927            "255.0",
7928            "65535.0",
7929            "4294967300.0",
7930            "4294967300.0",
7931        ];
7932        assert_eq!(
7933            f32_expected,
7934            get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
7935        );
7936
7937        let f16_expected = vec![
7938            "-inf", "-inf", "-32768.0", "-128.0", "0.0", "255.0", "inf", "inf", "inf",
7939        ];
7940        assert_eq!(
7941            f16_expected,
7942            get_cast_values::<Float16Type>(&f32_array, &DataType::Float16)
7943        );
7944
7945        let i64_expected = vec![
7946            "-2147483648",
7947            "-2147483648",
7948            "-32768",
7949            "-128",
7950            "0",
7951            "255",
7952            "65535",
7953            "4294967296",
7954            "4294967296",
7955        ];
7956        assert_eq!(
7957            i64_expected,
7958            get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
7959        );
7960
7961        let i32_expected = vec![
7962            "-2147483648",
7963            "-2147483648",
7964            "-32768",
7965            "-128",
7966            "0",
7967            "255",
7968            "65535",
7969            "null",
7970            "null",
7971        ];
7972        assert_eq!(
7973            i32_expected,
7974            get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
7975        );
7976
7977        let i16_expected = vec![
7978            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7979        ];
7980        assert_eq!(
7981            i16_expected,
7982            get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
7983        );
7984
7985        let i8_expected = vec![
7986            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7987        ];
7988        assert_eq!(
7989            i8_expected,
7990            get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
7991        );
7992
7993        let u64_expected = vec![
7994            "null",
7995            "null",
7996            "null",
7997            "null",
7998            "0",
7999            "255",
8000            "65535",
8001            "4294967296",
8002            "4294967296",
8003        ];
8004        assert_eq!(
8005            u64_expected,
8006            get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
8007        );
8008
8009        let u32_expected = vec![
8010            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
8011        ];
8012        assert_eq!(
8013            u32_expected,
8014            get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
8015        );
8016
8017        let u16_expected = vec![
8018            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
8019        ];
8020        assert_eq!(
8021            u16_expected,
8022            get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
8023        );
8024
8025        let u8_expected = vec![
8026            "null", "null", "null", "null", "0", "255", "null", "null", "null",
8027        ];
8028        assert_eq!(
8029            u8_expected,
8030            get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
8031        );
8032    }
8033
8034    #[test]
8035    fn test_cast_from_uint64() {
8036        let u64_values: Vec<u64> = vec![
8037            0,
8038            u8::MAX as u64,
8039            u16::MAX as u64,
8040            u32::MAX as u64,
8041            u64::MAX,
8042        ];
8043        let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values));
8044
8045        let f64_expected = vec![0.0, 255.0, 65535.0, 4294967295.0, 18446744073709552000.0];
8046        assert_eq!(
8047            f64_expected,
8048            get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
8049                .iter()
8050                .map(|i| i.parse::<f64>().unwrap())
8051                .collect::<Vec<f64>>()
8052        );
8053
8054        let f32_expected = vec![0.0, 255.0, 65535.0, 4294967300.0, 18446744000000000000.0];
8055        assert_eq!(
8056            f32_expected,
8057            get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
8058                .iter()
8059                .map(|i| i.parse::<f32>().unwrap())
8060                .collect::<Vec<f32>>()
8061        );
8062
8063        let f16_expected = vec![
8064            f16::from_f64(0.0),
8065            f16::from_f64(255.0),
8066            f16::from_f64(65535.0),
8067            f16::from_f64(4294967300.0),
8068            f16::from_f64(18446744000000000000.0),
8069        ];
8070        assert_eq!(
8071            f16_expected,
8072            get_cast_values::<Float16Type>(&u64_array, &DataType::Float16)
8073                .iter()
8074                .map(|i| i.parse::<f16>().unwrap())
8075                .collect::<Vec<f16>>()
8076        );
8077
8078        let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
8079        assert_eq!(
8080            i64_expected,
8081            get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
8082        );
8083
8084        let i32_expected = vec!["0", "255", "65535", "null", "null"];
8085        assert_eq!(
8086            i32_expected,
8087            get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
8088        );
8089
8090        let i16_expected = vec!["0", "255", "null", "null", "null"];
8091        assert_eq!(
8092            i16_expected,
8093            get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
8094        );
8095
8096        let i8_expected = vec!["0", "null", "null", "null", "null"];
8097        assert_eq!(
8098            i8_expected,
8099            get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
8100        );
8101
8102        let u64_expected = vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
8103        assert_eq!(
8104            u64_expected,
8105            get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
8106        );
8107
8108        let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
8109        assert_eq!(
8110            u32_expected,
8111            get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
8112        );
8113
8114        let u16_expected = vec!["0", "255", "65535", "null", "null"];
8115        assert_eq!(
8116            u16_expected,
8117            get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
8118        );
8119
8120        let u8_expected = vec!["0", "255", "null", "null", "null"];
8121        assert_eq!(
8122            u8_expected,
8123            get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
8124        );
8125    }
8126
8127    #[test]
8128    fn test_cast_from_uint32() {
8129        let u32_values: Vec<u32> = vec![0, u8::MAX as u32, u16::MAX as u32, u32::MAX];
8130        let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values));
8131
8132        let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
8133        assert_eq!(
8134            f64_expected,
8135            get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
8136        );
8137
8138        let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
8139        assert_eq!(
8140            f32_expected,
8141            get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
8142        );
8143
8144        let f16_expected = vec!["0.0", "255.0", "inf", "inf"];
8145        assert_eq!(
8146            f16_expected,
8147            get_cast_values::<Float16Type>(&u32_array, &DataType::Float16)
8148        );
8149
8150        let i64_expected = vec!["0", "255", "65535", "4294967295"];
8151        assert_eq!(
8152            i64_expected,
8153            get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
8154        );
8155
8156        let i32_expected = vec!["0", "255", "65535", "null"];
8157        assert_eq!(
8158            i32_expected,
8159            get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
8160        );
8161
8162        let i16_expected = vec!["0", "255", "null", "null"];
8163        assert_eq!(
8164            i16_expected,
8165            get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
8166        );
8167
8168        let i8_expected = vec!["0", "null", "null", "null"];
8169        assert_eq!(
8170            i8_expected,
8171            get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
8172        );
8173
8174        let u64_expected = vec!["0", "255", "65535", "4294967295"];
8175        assert_eq!(
8176            u64_expected,
8177            get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
8178        );
8179
8180        let u32_expected = vec!["0", "255", "65535", "4294967295"];
8181        assert_eq!(
8182            u32_expected,
8183            get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
8184        );
8185
8186        let u16_expected = vec!["0", "255", "65535", "null"];
8187        assert_eq!(
8188            u16_expected,
8189            get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
8190        );
8191
8192        let u8_expected = vec!["0", "255", "null", "null"];
8193        assert_eq!(
8194            u8_expected,
8195            get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
8196        );
8197    }
8198
8199    #[test]
8200    fn test_cast_from_uint16() {
8201        let u16_values: Vec<u16> = vec![0, u8::MAX as u16, u16::MAX];
8202        let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values));
8203
8204        let f64_expected = vec!["0.0", "255.0", "65535.0"];
8205        assert_eq!(
8206            f64_expected,
8207            get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
8208        );
8209
8210        let f32_expected = vec!["0.0", "255.0", "65535.0"];
8211        assert_eq!(
8212            f32_expected,
8213            get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
8214        );
8215
8216        let f16_expected = vec!["0.0", "255.0", "inf"];
8217        assert_eq!(
8218            f16_expected,
8219            get_cast_values::<Float16Type>(&u16_array, &DataType::Float16)
8220        );
8221
8222        let i64_expected = vec!["0", "255", "65535"];
8223        assert_eq!(
8224            i64_expected,
8225            get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
8226        );
8227
8228        let i32_expected = vec!["0", "255", "65535"];
8229        assert_eq!(
8230            i32_expected,
8231            get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
8232        );
8233
8234        let i16_expected = vec!["0", "255", "null"];
8235        assert_eq!(
8236            i16_expected,
8237            get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
8238        );
8239
8240        let i8_expected = vec!["0", "null", "null"];
8241        assert_eq!(
8242            i8_expected,
8243            get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
8244        );
8245
8246        let u64_expected = vec!["0", "255", "65535"];
8247        assert_eq!(
8248            u64_expected,
8249            get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
8250        );
8251
8252        let u32_expected = vec!["0", "255", "65535"];
8253        assert_eq!(
8254            u32_expected,
8255            get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
8256        );
8257
8258        let u16_expected = vec!["0", "255", "65535"];
8259        assert_eq!(
8260            u16_expected,
8261            get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
8262        );
8263
8264        let u8_expected = vec!["0", "255", "null"];
8265        assert_eq!(
8266            u8_expected,
8267            get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
8268        );
8269    }
8270
8271    #[test]
8272    fn test_cast_from_uint8() {
8273        let u8_values: Vec<u8> = vec![0, u8::MAX];
8274        let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values));
8275
8276        let f64_expected = vec!["0.0", "255.0"];
8277        assert_eq!(
8278            f64_expected,
8279            get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
8280        );
8281
8282        let f32_expected = vec!["0.0", "255.0"];
8283        assert_eq!(
8284            f32_expected,
8285            get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
8286        );
8287
8288        let f16_expected = vec!["0.0", "255.0"];
8289        assert_eq!(
8290            f16_expected,
8291            get_cast_values::<Float16Type>(&u8_array, &DataType::Float16)
8292        );
8293
8294        let i64_expected = vec!["0", "255"];
8295        assert_eq!(
8296            i64_expected,
8297            get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
8298        );
8299
8300        let i32_expected = vec!["0", "255"];
8301        assert_eq!(
8302            i32_expected,
8303            get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
8304        );
8305
8306        let i16_expected = vec!["0", "255"];
8307        assert_eq!(
8308            i16_expected,
8309            get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
8310        );
8311
8312        let i8_expected = vec!["0", "null"];
8313        assert_eq!(
8314            i8_expected,
8315            get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
8316        );
8317
8318        let u64_expected = vec!["0", "255"];
8319        assert_eq!(
8320            u64_expected,
8321            get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
8322        );
8323
8324        let u32_expected = vec!["0", "255"];
8325        assert_eq!(
8326            u32_expected,
8327            get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
8328        );
8329
8330        let u16_expected = vec!["0", "255"];
8331        assert_eq!(
8332            u16_expected,
8333            get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
8334        );
8335
8336        let u8_expected = vec!["0", "255"];
8337        assert_eq!(
8338            u8_expected,
8339            get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
8340        );
8341    }
8342
8343    #[test]
8344    fn test_cast_from_int64() {
8345        let i64_values: Vec<i64> = vec![
8346            i64::MIN,
8347            i32::MIN as i64,
8348            i16::MIN as i64,
8349            i8::MIN as i64,
8350            0,
8351            i8::MAX as i64,
8352            i16::MAX as i64,
8353            i32::MAX as i64,
8354            i64::MAX,
8355        ];
8356        let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values));
8357
8358        let f64_expected = vec![
8359            -9223372036854776000.0,
8360            -2147483648.0,
8361            -32768.0,
8362            -128.0,
8363            0.0,
8364            127.0,
8365            32767.0,
8366            2147483647.0,
8367            9223372036854776000.0,
8368        ];
8369        assert_eq!(
8370            f64_expected,
8371            get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
8372                .iter()
8373                .map(|i| i.parse::<f64>().unwrap())
8374                .collect::<Vec<f64>>()
8375        );
8376
8377        let f32_expected = vec![
8378            -9223372000000000000.0,
8379            -2147483600.0,
8380            -32768.0,
8381            -128.0,
8382            0.0,
8383            127.0,
8384            32767.0,
8385            2147483600.0,
8386            9223372000000000000.0,
8387        ];
8388        assert_eq!(
8389            f32_expected,
8390            get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
8391                .iter()
8392                .map(|i| i.parse::<f32>().unwrap())
8393                .collect::<Vec<f32>>()
8394        );
8395
8396        let f16_expected = vec![
8397            f16::from_f64(-9223372000000000000.0),
8398            f16::from_f64(-2147483600.0),
8399            f16::from_f64(-32768.0),
8400            f16::from_f64(-128.0),
8401            f16::from_f64(0.0),
8402            f16::from_f64(127.0),
8403            f16::from_f64(32767.0),
8404            f16::from_f64(2147483600.0),
8405            f16::from_f64(9223372000000000000.0),
8406        ];
8407        assert_eq!(
8408            f16_expected,
8409            get_cast_values::<Float16Type>(&i64_array, &DataType::Float16)
8410                .iter()
8411                .map(|i| i.parse::<f16>().unwrap())
8412                .collect::<Vec<f16>>()
8413        );
8414
8415        let i64_expected = vec![
8416            "-9223372036854775808",
8417            "-2147483648",
8418            "-32768",
8419            "-128",
8420            "0",
8421            "127",
8422            "32767",
8423            "2147483647",
8424            "9223372036854775807",
8425        ];
8426        assert_eq!(
8427            i64_expected,
8428            get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
8429        );
8430
8431        let i32_expected = vec![
8432            "null",
8433            "-2147483648",
8434            "-32768",
8435            "-128",
8436            "0",
8437            "127",
8438            "32767",
8439            "2147483647",
8440            "null",
8441        ];
8442        assert_eq!(
8443            i32_expected,
8444            get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
8445        );
8446
8447        assert_eq!(
8448            i32_expected,
8449            get_cast_values::<Date32Type>(&i64_array, &DataType::Date32)
8450        );
8451
8452        let i16_expected = vec![
8453            "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
8454        ];
8455        assert_eq!(
8456            i16_expected,
8457            get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
8458        );
8459
8460        let i8_expected = vec![
8461            "null", "null", "null", "-128", "0", "127", "null", "null", "null",
8462        ];
8463        assert_eq!(
8464            i8_expected,
8465            get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
8466        );
8467
8468        let u64_expected = vec![
8469            "null",
8470            "null",
8471            "null",
8472            "null",
8473            "0",
8474            "127",
8475            "32767",
8476            "2147483647",
8477            "9223372036854775807",
8478        ];
8479        assert_eq!(
8480            u64_expected,
8481            get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
8482        );
8483
8484        let u32_expected = vec![
8485            "null",
8486            "null",
8487            "null",
8488            "null",
8489            "0",
8490            "127",
8491            "32767",
8492            "2147483647",
8493            "null",
8494        ];
8495        assert_eq!(
8496            u32_expected,
8497            get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
8498        );
8499
8500        let u16_expected = vec![
8501            "null", "null", "null", "null", "0", "127", "32767", "null", "null",
8502        ];
8503        assert_eq!(
8504            u16_expected,
8505            get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
8506        );
8507
8508        let u8_expected = vec![
8509            "null", "null", "null", "null", "0", "127", "null", "null", "null",
8510        ];
8511        assert_eq!(
8512            u8_expected,
8513            get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
8514        );
8515    }
8516
8517    #[test]
8518    fn test_cast_from_int32() {
8519        let i32_values: Vec<i32> = vec![
8520            i32::MIN,
8521            i16::MIN as i32,
8522            i8::MIN as i32,
8523            0,
8524            i8::MAX as i32,
8525            i16::MAX as i32,
8526            i32::MAX,
8527        ];
8528        let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values));
8529
8530        let f64_expected = vec![
8531            "-2147483648.0",
8532            "-32768.0",
8533            "-128.0",
8534            "0.0",
8535            "127.0",
8536            "32767.0",
8537            "2147483647.0",
8538        ];
8539        assert_eq!(
8540            f64_expected,
8541            get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
8542        );
8543
8544        let f32_expected = vec![
8545            "-2147483600.0",
8546            "-32768.0",
8547            "-128.0",
8548            "0.0",
8549            "127.0",
8550            "32767.0",
8551            "2147483600.0",
8552        ];
8553        assert_eq!(
8554            f32_expected,
8555            get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
8556        );
8557
8558        let f16_expected = vec![
8559            f16::from_f64(-2147483600.0),
8560            f16::from_f64(-32768.0),
8561            f16::from_f64(-128.0),
8562            f16::from_f64(0.0),
8563            f16::from_f64(127.0),
8564            f16::from_f64(32767.0),
8565            f16::from_f64(2147483600.0),
8566        ];
8567        assert_eq!(
8568            f16_expected,
8569            get_cast_values::<Float16Type>(&i32_array, &DataType::Float16)
8570                .iter()
8571                .map(|i| i.parse::<f16>().unwrap())
8572                .collect::<Vec<f16>>()
8573        );
8574
8575        let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
8576        assert_eq!(
8577            i16_expected,
8578            get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
8579        );
8580
8581        let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
8582        assert_eq!(
8583            i8_expected,
8584            get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
8585        );
8586
8587        let u64_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8588        assert_eq!(
8589            u64_expected,
8590            get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
8591        );
8592
8593        let u32_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8594        assert_eq!(
8595            u32_expected,
8596            get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
8597        );
8598
8599        let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
8600        assert_eq!(
8601            u16_expected,
8602            get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
8603        );
8604
8605        let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
8606        assert_eq!(
8607            u8_expected,
8608            get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
8609        );
8610
8611        // The date32 to date64 cast increases the numerical values in order to keep the same dates.
8612        let i64_expected = vec![
8613            "-185542587187200000",
8614            "-2831155200000",
8615            "-11059200000",
8616            "0",
8617            "10972800000",
8618            "2831068800000",
8619            "185542587100800000",
8620        ];
8621        assert_eq!(
8622            i64_expected,
8623            get_cast_values::<Date64Type>(&i32_array, &DataType::Date64)
8624        );
8625    }
8626
8627    #[test]
8628    fn test_cast_from_int16() {
8629        let i16_values: Vec<i16> = vec![i16::MIN, i8::MIN as i16, 0, i8::MAX as i16, i16::MAX];
8630        let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values));
8631
8632        let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8633        assert_eq!(
8634            f64_expected,
8635            get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
8636        );
8637
8638        let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8639        assert_eq!(
8640            f32_expected,
8641            get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
8642        );
8643
8644        let f16_expected = vec![
8645            f16::from_f64(-32768.0),
8646            f16::from_f64(-128.0),
8647            f16::from_f64(0.0),
8648            f16::from_f64(127.0),
8649            f16::from_f64(32767.0),
8650        ];
8651        assert_eq!(
8652            f16_expected,
8653            get_cast_values::<Float16Type>(&i16_array, &DataType::Float16)
8654                .iter()
8655                .map(|i| i.parse::<f16>().unwrap())
8656                .collect::<Vec<f16>>()
8657        );
8658
8659        let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
8660        assert_eq!(
8661            i64_expected,
8662            get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
8663        );
8664
8665        let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
8666        assert_eq!(
8667            i32_expected,
8668            get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
8669        );
8670
8671        let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
8672        assert_eq!(
8673            i16_expected,
8674            get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
8675        );
8676
8677        let i8_expected = vec!["null", "-128", "0", "127", "null"];
8678        assert_eq!(
8679            i8_expected,
8680            get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
8681        );
8682
8683        let u64_expected = vec!["null", "null", "0", "127", "32767"];
8684        assert_eq!(
8685            u64_expected,
8686            get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
8687        );
8688
8689        let u32_expected = vec!["null", "null", "0", "127", "32767"];
8690        assert_eq!(
8691            u32_expected,
8692            get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
8693        );
8694
8695        let u16_expected = vec!["null", "null", "0", "127", "32767"];
8696        assert_eq!(
8697            u16_expected,
8698            get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
8699        );
8700
8701        let u8_expected = vec!["null", "null", "0", "127", "null"];
8702        assert_eq!(
8703            u8_expected,
8704            get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
8705        );
8706    }
8707
8708    #[test]
8709    fn test_cast_from_date32() {
8710        let i32_values: Vec<i32> = vec![
8711            i32::MIN,
8712            i16::MIN as i32,
8713            i8::MIN as i32,
8714            0,
8715            i8::MAX as i32,
8716            i16::MAX as i32,
8717            i32::MAX,
8718        ];
8719        let date32_array: ArrayRef = Arc::new(Date32Array::from(i32_values));
8720
8721        let i64_expected = vec![
8722            "-2147483648",
8723            "-32768",
8724            "-128",
8725            "0",
8726            "127",
8727            "32767",
8728            "2147483647",
8729        ];
8730        assert_eq!(
8731            i64_expected,
8732            get_cast_values::<Int64Type>(&date32_array, &DataType::Int64)
8733        );
8734    }
8735
8736    #[test]
8737    fn test_cast_from_int8() {
8738        let i8_values: Vec<i8> = vec![i8::MIN, 0, i8::MAX];
8739        let i8_array = Int8Array::from(i8_values);
8740
8741        let f64_expected = vec!["-128.0", "0.0", "127.0"];
8742        assert_eq!(
8743            f64_expected,
8744            get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
8745        );
8746
8747        let f32_expected = vec!["-128.0", "0.0", "127.0"];
8748        assert_eq!(
8749            f32_expected,
8750            get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
8751        );
8752
8753        let f16_expected = vec!["-128.0", "0.0", "127.0"];
8754        assert_eq!(
8755            f16_expected,
8756            get_cast_values::<Float16Type>(&i8_array, &DataType::Float16)
8757        );
8758
8759        let i64_expected = vec!["-128", "0", "127"];
8760        assert_eq!(
8761            i64_expected,
8762            get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
8763        );
8764
8765        let i32_expected = vec!["-128", "0", "127"];
8766        assert_eq!(
8767            i32_expected,
8768            get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
8769        );
8770
8771        let i16_expected = vec!["-128", "0", "127"];
8772        assert_eq!(
8773            i16_expected,
8774            get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
8775        );
8776
8777        let i8_expected = vec!["-128", "0", "127"];
8778        assert_eq!(
8779            i8_expected,
8780            get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
8781        );
8782
8783        let u64_expected = vec!["null", "0", "127"];
8784        assert_eq!(
8785            u64_expected,
8786            get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
8787        );
8788
8789        let u32_expected = vec!["null", "0", "127"];
8790        assert_eq!(
8791            u32_expected,
8792            get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
8793        );
8794
8795        let u16_expected = vec!["null", "0", "127"];
8796        assert_eq!(
8797            u16_expected,
8798            get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
8799        );
8800
8801        let u8_expected = vec!["null", "0", "127"];
8802        assert_eq!(
8803            u8_expected,
8804            get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
8805        );
8806    }
8807
8808    /// Convert `array` into a vector of strings by casting to data type dt
8809    fn get_cast_values<T>(array: &dyn Array, dt: &DataType) -> Vec<String>
8810    where
8811        T: ArrowPrimitiveType,
8812    {
8813        let c = cast(array, dt).unwrap();
8814        let a = c.as_primitive::<T>();
8815        let mut v: Vec<String> = vec![];
8816        for i in 0..array.len() {
8817            if a.is_null(i) {
8818                v.push("null".to_string())
8819            } else {
8820                v.push(format!("{:?}", a.value(i)));
8821            }
8822        }
8823        v
8824    }
8825
8826    #[test]
8827    fn test_cast_utf8_dict() {
8828        // FROM a dictionary with of Utf8 values
8829        let mut builder = StringDictionaryBuilder::<Int8Type>::new();
8830        builder.append("one").unwrap();
8831        builder.append_null();
8832        builder.append("three").unwrap();
8833        let array: ArrayRef = Arc::new(builder.finish());
8834
8835        let expected = vec!["one", "null", "three"];
8836
8837        // Test casting TO StringArray
8838        let cast_type = Utf8;
8839        let cast_array = cast(&array, &cast_type).expect("cast to UTF-8 failed");
8840        assert_eq!(cast_array.data_type(), &cast_type);
8841        assert_eq!(array_to_strings(&cast_array), expected);
8842
8843        // Test casting TO Dictionary (with different index sizes)
8844
8845        let cast_type = Dictionary(Box::new(Int16), Box::new(Utf8));
8846        let cast_array = cast(&array, &cast_type).expect("cast failed");
8847        assert_eq!(cast_array.data_type(), &cast_type);
8848        assert_eq!(array_to_strings(&cast_array), expected);
8849
8850        let cast_type = Dictionary(Box::new(Int32), Box::new(Utf8));
8851        let cast_array = cast(&array, &cast_type).expect("cast failed");
8852        assert_eq!(cast_array.data_type(), &cast_type);
8853        assert_eq!(array_to_strings(&cast_array), expected);
8854
8855        let cast_type = Dictionary(Box::new(Int64), Box::new(Utf8));
8856        let cast_array = cast(&array, &cast_type).expect("cast failed");
8857        assert_eq!(cast_array.data_type(), &cast_type);
8858        assert_eq!(array_to_strings(&cast_array), expected);
8859
8860        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8861        let cast_array = cast(&array, &cast_type).expect("cast failed");
8862        assert_eq!(cast_array.data_type(), &cast_type);
8863        assert_eq!(array_to_strings(&cast_array), expected);
8864
8865        let cast_type = Dictionary(Box::new(UInt16), Box::new(Utf8));
8866        let cast_array = cast(&array, &cast_type).expect("cast failed");
8867        assert_eq!(cast_array.data_type(), &cast_type);
8868        assert_eq!(array_to_strings(&cast_array), expected);
8869
8870        let cast_type = Dictionary(Box::new(UInt32), Box::new(Utf8));
8871        let cast_array = cast(&array, &cast_type).expect("cast failed");
8872        assert_eq!(cast_array.data_type(), &cast_type);
8873        assert_eq!(array_to_strings(&cast_array), expected);
8874
8875        let cast_type = Dictionary(Box::new(UInt64), Box::new(Utf8));
8876        let cast_array = cast(&array, &cast_type).expect("cast failed");
8877        assert_eq!(cast_array.data_type(), &cast_type);
8878        assert_eq!(array_to_strings(&cast_array), expected);
8879    }
8880
8881    #[test]
8882    fn test_cast_dict_to_dict_bad_index_value_primitive() {
8883        // test converting from an array that has indexes of a type
8884        // that are out of bounds for a particular other kind of
8885        // index.
8886
8887        let mut builder = PrimitiveDictionaryBuilder::<Int32Type, Int64Type>::new();
8888
8889        // add 200 distinct values (which can be stored by a
8890        // dictionary indexed by int32, but not a dictionary indexed
8891        // with int8)
8892        for i in 0..200 {
8893            builder.append(i).unwrap();
8894        }
8895        let array: ArrayRef = Arc::new(builder.finish());
8896
8897        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8898        let res = cast(&array, &cast_type);
8899        assert!(res.is_err());
8900        let actual_error = format!("{res:?}");
8901        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8902        assert!(
8903            actual_error.contains(expected_error),
8904            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8905        );
8906    }
8907
8908    #[test]
8909    fn test_cast_dict_to_dict_bad_index_value_utf8() {
8910        // Same test as test_cast_dict_to_dict_bad_index_value but use
8911        // string values (and encode the expected behavior here);
8912
8913        let mut builder = StringDictionaryBuilder::<Int32Type>::new();
8914
8915        // add 200 distinct values (which can be stored by a
8916        // dictionary indexed by int32, but not a dictionary indexed
8917        // with int8)
8918        for i in 0..200 {
8919            let val = format!("val{i}");
8920            builder.append(&val).unwrap();
8921        }
8922        let array = builder.finish();
8923
8924        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8925        let res = cast(&array, &cast_type);
8926        assert!(res.is_err());
8927        let actual_error = format!("{res:?}");
8928        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8929        assert!(
8930            actual_error.contains(expected_error),
8931            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8932        );
8933    }
8934
8935    #[test]
8936    fn test_cast_nested_dictionary_to_dictionary_reuses_values() {
8937        let inner = DictionaryArray::<Int32Type>::new(
8938            Int32Array::from(vec![Some(0), None, Some(1)]),
8939            Arc::new(StringArray::from(vec!["x", "y"])),
8940        );
8941        let nested = DictionaryArray::<Int32Type>::new(
8942            Int32Array::from(vec![Some(0), Some(1), Some(2), None, Some(0)]),
8943            Arc::new(inner),
8944        );
8945
8946        let result = cast(&nested, &Dictionary(Box::new(Int32), Box::new(Utf8))).unwrap();
8947        let result = result.as_dictionary::<Int32Type>();
8948
8949        assert_eq!(
8950            result.keys(),
8951            &Int32Array::from(vec![Some(0), None, Some(1), None, Some(0)])
8952        );
8953        assert_eq!(
8954            result.values().as_string::<i32>(),
8955            &StringArray::from(vec!["x", "y"])
8956        );
8957        let logical: Vec<Option<&str>> = result
8958            .downcast_dict::<StringArray>()
8959            .unwrap()
8960            .into_iter()
8961            .collect();
8962        assert_eq!(logical, vec![Some("x"), None, Some("y"), None, Some("x")]);
8963    }
8964
8965    #[test]
8966    fn test_cast_primitive_dict() {
8967        // FROM a dictionary with of INT32 values
8968        let mut builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
8969        builder.append(1).unwrap();
8970        builder.append_null();
8971        builder.append(3).unwrap();
8972        let array: ArrayRef = Arc::new(builder.finish());
8973
8974        let expected = vec!["1", "null", "3"];
8975
8976        // Test casting TO PrimitiveArray, different dictionary type
8977        let cast_array = cast(&array, &Utf8).expect("cast to UTF-8 failed");
8978        assert_eq!(array_to_strings(&cast_array), expected);
8979        assert_eq!(cast_array.data_type(), &Utf8);
8980
8981        let cast_array = cast(&array, &Int64).expect("cast to int64 failed");
8982        assert_eq!(array_to_strings(&cast_array), expected);
8983        assert_eq!(cast_array.data_type(), &Int64);
8984    }
8985
8986    #[test]
8987    fn test_cast_primitive_array_to_dict() {
8988        let mut builder = PrimitiveBuilder::<Int32Type>::new();
8989        builder.append_value(1);
8990        builder.append_null();
8991        builder.append_value(3);
8992        let array: ArrayRef = Arc::new(builder.finish());
8993
8994        let expected = vec!["1", "null", "3"];
8995
8996        // Cast to a dictionary (same value type, Int32)
8997        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int32));
8998        let cast_array = cast(&array, &cast_type).expect("cast failed");
8999        assert_eq!(cast_array.data_type(), &cast_type);
9000        assert_eq!(array_to_strings(&cast_array), expected);
9001
9002        // Cast to a dictionary (different value type, Int8)
9003        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int8));
9004        let cast_array = cast(&array, &cast_type).expect("cast failed");
9005        assert_eq!(cast_array.data_type(), &cast_type);
9006        assert_eq!(array_to_strings(&cast_array), expected);
9007    }
9008
9009    #[test]
9010    fn test_cast_time_array_to_dict() {
9011        use DataType::*;
9012
9013        let array = Arc::new(Date32Array::from(vec![Some(1000), None, Some(2000)])) as ArrayRef;
9014
9015        let expected = vec!["1972-09-27", "null", "1975-06-24"];
9016
9017        let cast_type = Dictionary(Box::new(UInt8), Box::new(Date32));
9018        let cast_array = cast(&array, &cast_type).expect("cast failed");
9019        assert_eq!(cast_array.data_type(), &cast_type);
9020        assert_eq!(array_to_strings(&cast_array), expected);
9021    }
9022
9023    #[test]
9024    fn test_cast_timestamp_array_to_dict() {
9025        use DataType::*;
9026
9027        let array = Arc::new(
9028            TimestampSecondArray::from(vec![Some(1000), None, Some(2000)]).with_timezone_utc(),
9029        ) as ArrayRef;
9030
9031        let expected = vec!["1970-01-01T00:16:40", "null", "1970-01-01T00:33:20"];
9032
9033        let cast_type = Dictionary(Box::new(UInt8), Box::new(Timestamp(TimeUnit::Second, None)));
9034        let cast_array = cast(&array, &cast_type).expect("cast failed");
9035        assert_eq!(cast_array.data_type(), &cast_type);
9036        assert_eq!(array_to_strings(&cast_array), expected);
9037    }
9038
9039    #[test]
9040    fn test_cast_string_array_to_dict() {
9041        use DataType::*;
9042
9043        let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) as ArrayRef;
9044
9045        let expected = vec!["one", "null", "three"];
9046
9047        // Cast to a dictionary (same value type, Utf8)
9048        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
9049        let cast_array = cast(&array, &cast_type).expect("cast failed");
9050        assert_eq!(cast_array.data_type(), &cast_type);
9051        assert_eq!(array_to_strings(&cast_array), expected);
9052    }
9053
9054    #[test]
9055    fn test_cast_null_array_to_from_decimal_array() {
9056        let data_type = DataType::Decimal128(12, 4);
9057        let array = new_null_array(&DataType::Null, 4);
9058        assert_eq!(array.data_type(), &DataType::Null);
9059        let cast_array = cast(&array, &data_type).expect("cast failed");
9060        assert_eq!(cast_array.data_type(), &data_type);
9061        for i in 0..4 {
9062            assert!(cast_array.is_null(i));
9063        }
9064
9065        let array = new_null_array(&data_type, 4);
9066        assert_eq!(array.data_type(), &data_type);
9067        let cast_array = cast(&array, &DataType::Null).expect("cast failed");
9068        assert_eq!(cast_array.data_type(), &DataType::Null);
9069        assert_eq!(cast_array.len(), 4);
9070        assert_eq!(cast_array.logical_nulls().unwrap().null_count(), 4);
9071    }
9072
9073    #[test]
9074    fn test_cast_null_array_from_and_to_primitive_array() {
9075        macro_rules! typed_test {
9076            ($ARR_TYPE:ident, $DATATYPE:ident, $TYPE:tt) => {{
9077                {
9078                    let array = Arc::new(NullArray::new(6)) as ArrayRef;
9079                    let expected = $ARR_TYPE::from(vec![None; 6]);
9080                    let cast_type = DataType::$DATATYPE;
9081                    let cast_array = cast(&array, &cast_type).expect("cast failed");
9082                    let cast_array = cast_array.as_primitive::<$TYPE>();
9083                    assert_eq!(cast_array.data_type(), &cast_type);
9084                    assert_eq!(cast_array, &expected);
9085                }
9086            }};
9087        }
9088
9089        typed_test!(Int16Array, Int16, Int16Type);
9090        typed_test!(Int32Array, Int32, Int32Type);
9091        typed_test!(Int64Array, Int64, Int64Type);
9092
9093        typed_test!(UInt16Array, UInt16, UInt16Type);
9094        typed_test!(UInt32Array, UInt32, UInt32Type);
9095        typed_test!(UInt64Array, UInt64, UInt64Type);
9096
9097        typed_test!(Float16Array, Float16, Float16Type);
9098        typed_test!(Float32Array, Float32, Float32Type);
9099        typed_test!(Float64Array, Float64, Float64Type);
9100
9101        typed_test!(Date32Array, Date32, Date32Type);
9102        typed_test!(Date64Array, Date64, Date64Type);
9103    }
9104
9105    fn cast_from_null_to_other_base(data_type: &DataType, is_complex: bool) {
9106        // Cast from null to data_type
9107        let array = new_null_array(&DataType::Null, 4);
9108        assert_eq!(array.data_type(), &DataType::Null);
9109        let cast_array = cast(&array, data_type).expect("cast failed");
9110        assert_eq!(cast_array.data_type(), data_type);
9111        for i in 0..4 {
9112            if is_complex {
9113                assert!(cast_array.logical_nulls().unwrap().is_null(i));
9114            } else {
9115                assert!(cast_array.is_null(i));
9116            }
9117        }
9118    }
9119
9120    fn cast_from_null_to_other(data_type: &DataType) {
9121        cast_from_null_to_other_base(data_type, false);
9122    }
9123
9124    fn cast_from_null_to_other_complex(data_type: &DataType) {
9125        cast_from_null_to_other_base(data_type, true);
9126    }
9127
9128    #[test]
9129    fn test_cast_null_from_and_to_variable_sized() {
9130        cast_from_null_to_other(&DataType::Utf8);
9131        cast_from_null_to_other(&DataType::LargeUtf8);
9132        cast_from_null_to_other(&DataType::Binary);
9133        cast_from_null_to_other(&DataType::LargeBinary);
9134    }
9135
9136    #[test]
9137    fn test_cast_null_from_and_to_nested_type() {
9138        // Cast null from and to map
9139        let data_type = DataType::Map(
9140            Arc::new(Field::new_struct(
9141                "entry",
9142                vec![
9143                    Field::new("key", DataType::Utf8, false),
9144                    Field::new("value", DataType::Int32, true),
9145                ],
9146                false,
9147            )),
9148            false,
9149        );
9150        cast_from_null_to_other(&data_type);
9151
9152        // Cast null from and to list
9153        let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
9154        cast_from_null_to_other(&data_type);
9155        let data_type = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
9156        cast_from_null_to_other(&data_type);
9157        let data_type =
9158            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
9159        cast_from_null_to_other(&data_type);
9160
9161        // Cast null from and to dictionary
9162        let values = vec![None, None, None, None] as Vec<Option<&str>>;
9163        let array: DictionaryArray<Int8Type> = values.into_iter().collect();
9164        let array = Arc::new(array) as ArrayRef;
9165        let data_type = array.data_type().to_owned();
9166        cast_from_null_to_other(&data_type);
9167
9168        // Cast null from and to struct
9169        let data_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
9170        cast_from_null_to_other(&data_type);
9171
9172        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
9173        cast_from_null_to_other(&target_type);
9174
9175        let target_type =
9176            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
9177        cast_from_null_to_other(&target_type);
9178
9179        let fields = UnionFields::from_fields(vec![Field::new("a", DataType::Int64, false)]);
9180        let target_type = DataType::Union(fields, UnionMode::Sparse);
9181        cast_from_null_to_other_complex(&target_type);
9182
9183        let target_type = DataType::RunEndEncoded(
9184            Arc::new(Field::new("item", DataType::Int32, true)),
9185            Arc::new(Field::new("item", DataType::Int32, true)),
9186        );
9187        cast_from_null_to_other_complex(&target_type);
9188    }
9189
9190    /// Print the `DictionaryArray` `array` as a vector of strings
9191    fn array_to_strings(array: &ArrayRef) -> Vec<String> {
9192        let options = FormatOptions::new().with_null("null");
9193        let formatter = ArrayFormatter::try_new(array.as_ref(), &options).unwrap();
9194        (0..array.len())
9195            .map(|i| formatter.value(i).to_string())
9196            .collect()
9197    }
9198
9199    #[test]
9200    fn test_cast_utf8_to_date32() {
9201        use chrono::NaiveDate;
9202        let from_ymd = chrono::NaiveDate::from_ymd_opt;
9203        let since = chrono::NaiveDate::signed_duration_since;
9204
9205        let a = StringArray::from(vec![
9206            "2000-01-01",          // valid date with leading 0s
9207            "2000-01-01T12:00:00", // valid datetime, will throw away the time part
9208            "2000-2-2",            // valid date without leading 0s
9209            "2000-00-00",          // invalid month and day
9210            "2000",                // just a year is invalid
9211        ]);
9212        let array = Arc::new(a) as ArrayRef;
9213        let b = cast(&array, &DataType::Date32).unwrap();
9214        let c = b.as_primitive::<Date32Type>();
9215
9216        // test valid inputs
9217        let date_value = since(
9218            NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
9219            from_ymd(1970, 1, 1).unwrap(),
9220        )
9221        .num_days() as i32;
9222        assert!(c.is_valid(0)); // "2000-01-01"
9223        assert_eq!(date_value, c.value(0));
9224
9225        assert!(c.is_valid(1)); // "2000-01-01T12:00:00"
9226        assert_eq!(date_value, c.value(1));
9227
9228        let date_value = since(
9229            NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
9230            from_ymd(1970, 1, 1).unwrap(),
9231        )
9232        .num_days() as i32;
9233        assert!(c.is_valid(2)); // "2000-2-2"
9234        assert_eq!(date_value, c.value(2));
9235
9236        // test invalid inputs
9237        assert!(!c.is_valid(3)); // "2000-00-00"
9238        assert!(!c.is_valid(4)); // "2000"
9239    }
9240
9241    #[test]
9242    fn test_cast_utf8_to_date64() {
9243        let a = StringArray::from(vec![
9244            "2000-01-01T12:00:00", // date + time valid
9245            "2020-12-15T12:34:56", // date + time valid
9246            "2020-2-2T12:34:56",   // valid date time without leading 0s
9247            "2000-00-00T12:00:00", // invalid month and day
9248            "2000-01-01 12:00:00", // missing the 'T'
9249            "2000-01-01",          // just a date is invalid
9250        ]);
9251        let array = Arc::new(a) as ArrayRef;
9252        let b = cast(&array, &DataType::Date64).unwrap();
9253        let c = b.as_primitive::<Date64Type>();
9254
9255        // test valid inputs
9256        assert!(c.is_valid(0)); // "2000-01-01T12:00:00"
9257        assert_eq!(946728000000, c.value(0));
9258        assert!(c.is_valid(1)); // "2020-12-15T12:34:56"
9259        assert_eq!(1608035696000, c.value(1));
9260        assert!(!c.is_valid(2)); // "2020-2-2T12:34:56"
9261
9262        assert!(!c.is_valid(3)); // "2000-00-00T12:00:00"
9263        assert!(c.is_valid(4)); // "2000-01-01 12:00:00"
9264        assert_eq!(946728000000, c.value(4));
9265        assert!(c.is_valid(5)); // "2000-01-01"
9266        assert_eq!(946684800000, c.value(5));
9267    }
9268
9269    #[test]
9270    fn test_can_cast_fsl_to_fsl() {
9271        let from_array = Arc::new(
9272            FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
9273                [Some([Some(1.0), Some(2.0)]), None],
9274                2,
9275            ),
9276        ) as ArrayRef;
9277        let to_array = Arc::new(
9278            FixedSizeListArray::from_iter_primitive::<Float16Type, _, _>(
9279                [
9280                    Some([Some(f16::from_f32(1.0)), Some(f16::from_f32(2.0))]),
9281                    None,
9282                ],
9283                2,
9284            ),
9285        ) as ArrayRef;
9286
9287        assert!(can_cast_types(from_array.data_type(), to_array.data_type()));
9288        let actual = cast(&from_array, to_array.data_type()).unwrap();
9289        assert_eq!(actual.data_type(), to_array.data_type());
9290
9291        let invalid_target =
9292            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Binary, true)), 2);
9293        assert!(!can_cast_types(from_array.data_type(), &invalid_target));
9294
9295        let invalid_size =
9296            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Float16, true)), 5);
9297        assert!(!can_cast_types(from_array.data_type(), &invalid_size));
9298    }
9299
9300    #[test]
9301    fn test_can_cast_types_fixed_size_list_to_list() {
9302        // DataType::List
9303        let array1 = make_fixed_size_list_array();
9304        assert!(can_cast_types(
9305            array1.data_type(),
9306            &DataType::List(Arc::new(Field::new("", DataType::Int32, false)))
9307        ));
9308
9309        // DataType::LargeList
9310        let array2 = make_fixed_size_list_array_for_large_list();
9311        assert!(can_cast_types(
9312            array2.data_type(),
9313            &DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false)))
9314        ));
9315    }
9316
9317    #[test]
9318    fn test_cast_fixed_size_list_to_list() {
9319        // Important cases:
9320        // 1. With/without nulls
9321        // 2. List/LargeList/ListView/LargeListView
9322        // 3. With and without inner casts
9323
9324        let cases = [
9325            // fixed_size_list<i32, 2> => list<i32>
9326            (
9327                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9328                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9329                    2,
9330                )) as ArrayRef,
9331                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
9332                    Some([Some(1), Some(1)]),
9333                    Some([Some(2), Some(2)]),
9334                ])) as ArrayRef,
9335            ),
9336            // fixed_size_list<i32, 2> => list<i32> (nullable)
9337            (
9338                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9339                    [None, Some([Some(2), Some(2)])],
9340                    2,
9341                )) as ArrayRef,
9342                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
9343                    None,
9344                    Some([Some(2), Some(2)]),
9345                ])) as ArrayRef,
9346            ),
9347            // fixed_size_list<i32, 2> => large_list<i64>
9348            (
9349                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9350                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9351                    2,
9352                )) as ArrayRef,
9353                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
9354                    Some([Some(1), Some(1)]),
9355                    Some([Some(2), Some(2)]),
9356                ])) as ArrayRef,
9357            ),
9358            // fixed_size_list<i32, 2> => large_list<i64> (nullable)
9359            (
9360                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9361                    [None, Some([Some(2), Some(2)])],
9362                    2,
9363                )) as ArrayRef,
9364                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
9365                    None,
9366                    Some([Some(2), Some(2)]),
9367                ])) as ArrayRef,
9368            ),
9369            // fixed_size_list<i32, 2> => list_view<i32>
9370            (
9371                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9372                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9373                    2,
9374                )) as ArrayRef,
9375                Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>([
9376                    Some([Some(1), Some(1)]),
9377                    Some([Some(2), Some(2)]),
9378                ])) as ArrayRef,
9379            ),
9380            // fixed_size_list<i32, 2> => list_view<i32> (nullable)
9381            (
9382                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9383                    [None, Some([Some(2), Some(2)])],
9384                    2,
9385                )) as ArrayRef,
9386                Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>([
9387                    None,
9388                    Some([Some(2), Some(2)]),
9389                ])) as ArrayRef,
9390            ),
9391            // fixed_size_list<i32, 2> => large_list_view<i64>
9392            (
9393                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9394                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9395                    2,
9396                )) as ArrayRef,
9397                Arc::new(LargeListViewArray::from_iter_primitive::<Int64Type, _, _>(
9398                    [Some([Some(1), Some(1)]), Some([Some(2), Some(2)])],
9399                )) as ArrayRef,
9400            ),
9401            // fixed_size_list<i32, 2> => large_list_view<i64> (nullable)
9402            (
9403                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9404                    [None, Some([Some(2), Some(2)])],
9405                    2,
9406                )) as ArrayRef,
9407                Arc::new(LargeListViewArray::from_iter_primitive::<Int64Type, _, _>(
9408                    [None, Some([Some(2), Some(2)])],
9409                )) as ArrayRef,
9410            ),
9411        ];
9412
9413        for (array, expected) in cases {
9414            assert!(
9415                can_cast_types(array.data_type(), expected.data_type()),
9416                "can_cast_types claims we cannot cast {:?} to {:?}",
9417                array.data_type(),
9418                expected.data_type()
9419            );
9420
9421            let list_array = cast(&array, expected.data_type())
9422                .unwrap_or_else(|_| panic!("Failed to cast {array:?} to {expected:?}"));
9423            assert_eq!(
9424                list_array.as_ref(),
9425                &expected,
9426                "Incorrect result from casting {array:?} to {expected:?}",
9427            );
9428        }
9429    }
9430
9431    #[test]
9432    fn test_cast_fixed_size_list_to_list_preserves_field_metadata() {
9433        use std::collections::HashMap;
9434
9435        let metadata: HashMap<String, String> =
9436            HashMap::from([("PARQUET:field_id".to_string(), "89".to_string())]);
9437
9438        let src = Arc::new(
9439            FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
9440                [[1.0_f32, 2.0].map(Some), [3.0, 4.0].map(Some)].map(Some),
9441                2,
9442            ),
9443        ) as ArrayRef;
9444
9445        let target_field = Arc::new(
9446            Field::new("element", DataType::Float32, true).with_metadata(metadata.clone()),
9447        );
9448
9449        let target_types = [
9450            DataType::List(target_field.clone()),
9451            DataType::LargeList(target_field.clone()),
9452            DataType::ListView(target_field.clone()),
9453            DataType::LargeListView(target_field.clone()),
9454        ];
9455
9456        for target_type in &target_types {
9457            let result = cast(&src, target_type).unwrap();
9458            assert_eq!(
9459                result.data_type(),
9460                target_type,
9461                "Cast to {target_type:?} should preserve field metadata"
9462            );
9463        }
9464    }
9465
9466    #[test]
9467    fn test_cast_utf8_to_list() {
9468        // DataType::List
9469        let array = Arc::new(StringArray::from(vec!["5"])) as ArrayRef;
9470        let field = Arc::new(Field::new("", DataType::Int32, false));
9471        let list_array = cast(&array, &DataType::List(field.clone())).unwrap();
9472        let actual = list_array.as_list_opt::<i32>().unwrap();
9473        let expect = ListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
9474        assert_eq!(&expect.value(0), &actual.value(0));
9475
9476        // DataType::LargeList
9477        let list_array = cast(&array, &DataType::LargeList(field.clone())).unwrap();
9478        let actual = list_array.as_list_opt::<i64>().unwrap();
9479        let expect = LargeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
9480        assert_eq!(&expect.value(0), &actual.value(0));
9481
9482        // DataType::FixedSizeList
9483        let list_array = cast(&array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9484        let actual = list_array.as_fixed_size_list_opt().unwrap();
9485        let expect =
9486            FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])], 1);
9487        assert_eq!(&expect.value(0), &actual.value(0));
9488    }
9489
9490    #[test]
9491    fn test_cast_single_element_fixed_size_list() {
9492        // FixedSizeList<T>[1] => T
9493        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9494            [(Some([Some(5)]))],
9495            1,
9496        )) as ArrayRef;
9497        let casted_array = cast(&from_array, &DataType::Int32).unwrap();
9498        let actual: &Int32Array = casted_array.as_primitive();
9499        let expected = Int32Array::from(vec![Some(5)]);
9500        assert_eq!(&expected, actual);
9501
9502        // FixedSizeList<T>[1] => FixedSizeList<U>[1]
9503        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9504            [(Some([Some(5)]))],
9505            1,
9506        )) as ArrayRef;
9507        let to_field = Arc::new(Field::new("dummy", DataType::Float32, false));
9508        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
9509        let expected = Arc::new(FixedSizeListArray::new(
9510            to_field.clone(),
9511            1,
9512            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9513            None,
9514        )) as ArrayRef;
9515        assert_eq!(*expected, *actual);
9516
9517        // FixedSizeList<T>[1] => FixedSizeList<FixdSizedList<U>[1]>[1]
9518        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9519            [(Some([Some(5)]))],
9520            1,
9521        )) as ArrayRef;
9522        let to_field_inner = Arc::new(Field::new_list_field(DataType::Float32, false));
9523        let to_field = Arc::new(Field::new(
9524            "dummy",
9525            DataType::FixedSizeList(to_field_inner.clone(), 1),
9526            false,
9527        ));
9528        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
9529        let expected = Arc::new(FixedSizeListArray::new(
9530            to_field.clone(),
9531            1,
9532            Arc::new(FixedSizeListArray::new(
9533                to_field_inner.clone(),
9534                1,
9535                Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9536                None,
9537            )) as ArrayRef,
9538            None,
9539        )) as ArrayRef;
9540        assert_eq!(*expected, *actual);
9541
9542        // T => FixedSizeList<T>[1] (non-nullable)
9543        let field = Arc::new(Field::new("dummy", DataType::Float32, false));
9544        let from_array = Arc::new(Int8Array::from(vec![Some(5)])) as ArrayRef;
9545        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9546        let actual = casted_array.as_fixed_size_list();
9547        let expected = Arc::new(FixedSizeListArray::new(
9548            field.clone(),
9549            1,
9550            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9551            None,
9552        )) as ArrayRef;
9553        assert_eq!(expected.as_ref(), actual);
9554
9555        // T => FixedSizeList<T>[1] (nullable)
9556        let field = Arc::new(Field::new("nullable", DataType::Float32, true));
9557        let from_array = Arc::new(Int8Array::from(vec![None])) as ArrayRef;
9558        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9559        let actual = casted_array.as_fixed_size_list();
9560        let expected = Arc::new(FixedSizeListArray::new(
9561            field.clone(),
9562            1,
9563            Arc::new(Float32Array::from(vec![None])) as ArrayRef,
9564            None,
9565        )) as ArrayRef;
9566        assert_eq!(expected.as_ref(), actual);
9567    }
9568
9569    #[test]
9570    fn test_cast_list_containers() {
9571        // large-list to list
9572        let array = make_large_list_array();
9573        let list_array = cast(
9574            &array,
9575            &DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
9576        )
9577        .unwrap();
9578        let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
9579        let expected = array.as_any().downcast_ref::<LargeListArray>().unwrap();
9580
9581        assert_eq!(&expected.value(0), &actual.value(0));
9582        assert_eq!(&expected.value(1), &actual.value(1));
9583        assert_eq!(&expected.value(2), &actual.value(2));
9584
9585        // list to large-list
9586        let array = make_list_array();
9587        let large_list_array = cast(
9588            &array,
9589            &DataType::LargeList(Arc::new(Field::new("", DataType::Int32, false))),
9590        )
9591        .unwrap();
9592        let actual = large_list_array
9593            .as_any()
9594            .downcast_ref::<LargeListArray>()
9595            .unwrap();
9596        let expected = array.as_any().downcast_ref::<ListArray>().unwrap();
9597
9598        assert_eq!(&expected.value(0), &actual.value(0));
9599        assert_eq!(&expected.value(1), &actual.value(1));
9600        assert_eq!(&expected.value(2), &actual.value(2));
9601    }
9602
9603    #[test]
9604    fn test_cast_list_view() {
9605        // cast between list view and list view
9606        let array = make_list_view_array();
9607        let to = DataType::ListView(Field::new_list_field(DataType::Float32, true).into());
9608        assert!(can_cast_types(array.data_type(), &to));
9609        let actual = cast(&array, &to).unwrap();
9610        let actual = actual.as_list_view::<i32>();
9611
9612        assert_eq!(
9613            &Float32Array::from(vec![0.0, 1.0, 2.0]) as &dyn Array,
9614            actual.value(0).as_ref()
9615        );
9616        assert_eq!(
9617            &Float32Array::from(vec![3.0, 4.0, 5.0]) as &dyn Array,
9618            actual.value(1).as_ref()
9619        );
9620        assert_eq!(
9621            &Float32Array::from(vec![6.0, 7.0]) as &dyn Array,
9622            actual.value(2).as_ref()
9623        );
9624
9625        // cast between large list view and large list view
9626        let array = make_large_list_view_array();
9627        let to = DataType::LargeListView(Field::new_list_field(DataType::Float32, true).into());
9628        assert!(can_cast_types(array.data_type(), &to));
9629        let actual = cast(&array, &to).unwrap();
9630        let actual = actual.as_list_view::<i64>();
9631
9632        assert_eq!(
9633            &Float32Array::from(vec![0.0, 1.0, 2.0]) as &dyn Array,
9634            actual.value(0).as_ref()
9635        );
9636        assert_eq!(
9637            &Float32Array::from(vec![3.0, 4.0, 5.0]) as &dyn Array,
9638            actual.value(1).as_ref()
9639        );
9640        assert_eq!(
9641            &Float32Array::from(vec![6.0, 7.0]) as &dyn Array,
9642            actual.value(2).as_ref()
9643        );
9644    }
9645
9646    #[test]
9647    fn test_non_list_to_list_view() {
9648        let input = Arc::new(Int32Array::from(vec![Some(0), None, Some(2)])) as ArrayRef;
9649        let expected_primitive =
9650            Arc::new(Float32Array::from(vec![Some(0.0), None, Some(2.0)])) as ArrayRef;
9651
9652        // [[0], [NULL], [2]]
9653        let expected = ListViewArray::new(
9654            Field::new_list_field(DataType::Float32, true).into(),
9655            vec![0, 1, 2].into(),
9656            vec![1, 1, 1].into(),
9657            expected_primitive.clone(),
9658            None,
9659        );
9660        assert!(can_cast_types(input.data_type(), expected.data_type()));
9661        let actual = cast(&input, expected.data_type()).unwrap();
9662        assert_eq!(actual.as_ref(), &expected);
9663
9664        // [[0], [NULL], [2]]
9665        let expected = LargeListViewArray::new(
9666            Field::new_list_field(DataType::Float32, true).into(),
9667            vec![0, 1, 2].into(),
9668            vec![1, 1, 1].into(),
9669            expected_primitive.clone(),
9670            None,
9671        );
9672        assert!(can_cast_types(input.data_type(), expected.data_type()));
9673        let actual = cast(&input, expected.data_type()).unwrap();
9674        assert_eq!(actual.as_ref(), &expected);
9675    }
9676
9677    #[test]
9678    fn test_cast_list_to_zero_size_fsl() {
9679        let field = Arc::new(Field::new("a", DataType::Null, true));
9680        let length = 2;
9681        let expected = Arc::new(
9682            FixedSizeListArray::try_new_with_length(
9683                field.clone(),
9684                0,
9685                new_empty_array(&DataType::Null),
9686                None,
9687                2,
9688            )
9689            .unwrap(),
9690        ) as ArrayRef;
9691
9692        let list = Arc::new(ListArray::new(
9693            field.clone(),
9694            OffsetBuffer::from_repeated_length(0, length),
9695            new_empty_array(&DataType::Null),
9696            None,
9697        ));
9698        let fsl = cast(list.as_ref(), expected.data_type()).unwrap();
9699        assert_eq!(&expected, &fsl);
9700
9701        let list = Arc::new(ListViewArray::new(
9702            field.clone(),
9703            vec![0; length].into(),
9704            vec![0; length].into(),
9705            new_empty_array(&DataType::Null),
9706            None,
9707        ));
9708        let fsl = cast(list.as_ref(), expected.data_type()).unwrap();
9709        assert_eq!(&expected, &fsl);
9710    }
9711
9712    #[test]
9713    fn test_cast_list_to_fsl() {
9714        // There four noteworthy cases we should handle:
9715        // 1. No nulls
9716        // 2. Nulls that are always empty
9717        // 3. Nulls that have varying lengths
9718        // 4. Nulls that are correctly sized (same as target list size)
9719
9720        // Non-null case
9721        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9722        let values = vec![
9723            Some(vec![Some(1), Some(2), Some(3)]),
9724            Some(vec![Some(4), Some(5), Some(6)]),
9725        ];
9726        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
9727            values.clone(),
9728        )) as ArrayRef;
9729        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9730            values, 3,
9731        )) as ArrayRef;
9732        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9733        assert_eq!(expected.as_ref(), actual.as_ref());
9734
9735        // Null cases
9736        // Array is [[1, 2, 3], null, [4, 5, 6], null]
9737        let cases = [
9738            (
9739                // Zero-length nulls
9740                vec![1, 2, 3, 4, 5, 6],
9741                vec![3, 0, 3, 0],
9742            ),
9743            (
9744                // Varying-length nulls
9745                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
9746                vec![3, 2, 3, 1],
9747            ),
9748            (
9749                // Correctly-sized nulls
9750                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
9751                vec![3, 3, 3, 3],
9752            ),
9753            (
9754                // Mixed nulls
9755                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
9756                vec![3, 0, 3, 3],
9757            ),
9758        ];
9759        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
9760
9761        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9762            vec![
9763                Some(vec![Some(1), Some(2), Some(3)]),
9764                None,
9765                Some(vec![Some(4), Some(5), Some(6)]),
9766                None,
9767            ],
9768            3,
9769        )) as ArrayRef;
9770
9771        for (values, lengths) in cases.iter() {
9772            let array = Arc::new(ListArray::new(
9773                field.clone(),
9774                OffsetBuffer::from_lengths(lengths.clone()),
9775                Arc::new(Int32Array::from(values.clone())),
9776                Some(null_buffer.clone()),
9777            )) as ArrayRef;
9778            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9779            assert_eq!(expected.as_ref(), actual.as_ref());
9780        }
9781    }
9782
9783    #[test]
9784    fn test_cast_list_view_to_fsl() {
9785        // There four noteworthy cases we should handle:
9786        // 1. No nulls
9787        // 2. Nulls that are always empty
9788        // 3. Nulls that have varying lengths
9789        // 4. Nulls that are correctly sized (same as target list size)
9790
9791        // Non-null case
9792        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9793        let values = vec![
9794            Some(vec![Some(1), Some(2), Some(3)]),
9795            Some(vec![Some(4), Some(5), Some(6)]),
9796        ];
9797        let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(
9798            values.clone(),
9799        )) as ArrayRef;
9800        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9801            values, 3,
9802        )) as ArrayRef;
9803        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9804        assert_eq!(expected.as_ref(), actual.as_ref());
9805
9806        // Null cases
9807        // Array is [[1, 2, 3], null, [4, 5, 6], null]
9808        let cases = [
9809            (
9810                // Zero-length nulls
9811                vec![1, 2, 3, 4, 5, 6],
9812                vec![0, 0, 3, 0],
9813                vec![3, 0, 3, 0],
9814            ),
9815            (
9816                // Varying-length nulls
9817                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
9818                vec![0, 1, 5, 0],
9819                vec![3, 2, 3, 1],
9820            ),
9821            (
9822                // Correctly-sized nulls
9823                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
9824                vec![0, 3, 6, 9],
9825                vec![3, 3, 3, 3],
9826            ),
9827            (
9828                // Mixed nulls
9829                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
9830                vec![0, 0, 3, 6],
9831                vec![3, 0, 3, 3],
9832            ),
9833        ];
9834        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
9835
9836        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9837            vec![
9838                Some(vec![Some(1), Some(2), Some(3)]),
9839                None,
9840                Some(vec![Some(4), Some(5), Some(6)]),
9841                None,
9842            ],
9843            3,
9844        )) as ArrayRef;
9845
9846        for (values, offsets, lengths) in cases.iter() {
9847            let array = Arc::new(ListViewArray::new(
9848                field.clone(),
9849                offsets.clone().into(),
9850                lengths.clone().into(),
9851                Arc::new(Int32Array::from(values.clone())),
9852                Some(null_buffer.clone()),
9853            )) as ArrayRef;
9854            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9855            assert_eq!(expected.as_ref(), actual.as_ref());
9856        }
9857    }
9858
9859    #[test]
9860    fn test_cast_list_to_fsl_safety() {
9861        let values = vec![
9862            Some(vec![Some(1), Some(2), Some(3)]),
9863            Some(vec![Some(4), Some(5)]),
9864            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
9865            Some(vec![Some(3), Some(4), Some(5)]),
9866        ];
9867        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
9868            values.clone(),
9869        )) as ArrayRef;
9870
9871        let res = cast_with_options(
9872            array.as_ref(),
9873            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9874            &CastOptions {
9875                safe: false,
9876                ..Default::default()
9877            },
9878        );
9879        assert!(res.is_err());
9880        assert!(
9881            format!("{res:?}")
9882                .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
9883        );
9884
9885        // When safe=true (default), the cast will fill nulls for lists that are
9886        // too short and truncate lists that are too long.
9887        let res = cast(
9888            array.as_ref(),
9889            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9890        )
9891        .unwrap();
9892        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9893            vec![
9894                Some(vec![Some(1), Some(2), Some(3)]),
9895                None, // Too short -> replaced with null
9896                None, // Too long -> replaced with null
9897                Some(vec![Some(3), Some(4), Some(5)]),
9898            ],
9899            3,
9900        )) as ArrayRef;
9901        assert_eq!(expected.as_ref(), res.as_ref());
9902
9903        // The safe option is false and the source array contains a null list.
9904        // issue: https://github.com/apache/arrow-rs/issues/5642
9905        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
9906            Some(vec![Some(1), Some(2), Some(3)]),
9907            None,
9908        ])) as ArrayRef;
9909        let res = cast_with_options(
9910            array.as_ref(),
9911            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9912            &CastOptions {
9913                safe: false,
9914                ..Default::default()
9915            },
9916        )
9917        .unwrap();
9918        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9919            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
9920            3,
9921        )) as ArrayRef;
9922        assert_eq!(expected.as_ref(), res.as_ref());
9923    }
9924
9925    #[test]
9926    fn test_cast_list_view_to_fsl_safety() {
9927        let values = vec![
9928            Some(vec![Some(1), Some(2), Some(3)]),
9929            Some(vec![Some(4), Some(5)]),
9930            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
9931            Some(vec![Some(3), Some(4), Some(5)]),
9932        ];
9933        let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(
9934            values.clone(),
9935        )) as ArrayRef;
9936
9937        let res = cast_with_options(
9938            array.as_ref(),
9939            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9940            &CastOptions {
9941                safe: false,
9942                ..Default::default()
9943            },
9944        );
9945        assert!(res.is_err());
9946        assert!(
9947            format!("{res:?}")
9948                .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
9949        );
9950
9951        // When safe=true (default), the cast will fill nulls for lists that are
9952        // too short and truncate lists that are too long.
9953        let res = cast(
9954            array.as_ref(),
9955            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9956        )
9957        .unwrap();
9958        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9959            vec![
9960                Some(vec![Some(1), Some(2), Some(3)]),
9961                None, // Too short -> replaced with null
9962                None, // Too long -> replaced with null
9963                Some(vec![Some(3), Some(4), Some(5)]),
9964            ],
9965            3,
9966        )) as ArrayRef;
9967        assert_eq!(expected.as_ref(), res.as_ref());
9968
9969        // The safe option is false and the source array contains a null list.
9970        // issue: https://github.com/apache/arrow-rs/issues/5642
9971        let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(vec![
9972            Some(vec![Some(1), Some(2), Some(3)]),
9973            None,
9974        ])) as ArrayRef;
9975        let res = cast_with_options(
9976            array.as_ref(),
9977            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9978            &CastOptions {
9979                safe: false,
9980                ..Default::default()
9981            },
9982        )
9983        .unwrap();
9984        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9985            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
9986            3,
9987        )) as ArrayRef;
9988        assert_eq!(expected.as_ref(), res.as_ref());
9989    }
9990
9991    #[test]
9992    fn test_cast_large_list_to_fsl() {
9993        let values = vec![Some(vec![Some(1), Some(2)]), Some(vec![Some(3), Some(4)])];
9994        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9995            values.clone(),
9996            2,
9997        )) as ArrayRef;
9998        let target_type =
9999            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 2);
10000
10001        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
10002            values.clone(),
10003        )) as ArrayRef;
10004        let actual = cast(array.as_ref(), &target_type).unwrap();
10005        assert_eq!(expected.as_ref(), actual.as_ref());
10006
10007        let array = Arc::new(LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(
10008            values.clone(),
10009        )) as ArrayRef;
10010        let actual = cast(array.as_ref(), &target_type).unwrap();
10011        assert_eq!(expected.as_ref(), actual.as_ref());
10012    }
10013
10014    #[test]
10015    fn test_cast_list_to_fsl_subcast() {
10016        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
10017            vec![
10018                Some(vec![Some(1), Some(2)]),
10019                Some(vec![Some(3), Some(i32::MAX)]),
10020            ],
10021        )) as ArrayRef;
10022        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
10023            vec![
10024                Some(vec![Some(1), Some(2)]),
10025                Some(vec![Some(3), Some(i32::MAX as i64)]),
10026            ],
10027            2,
10028        )) as ArrayRef;
10029        let actual = cast(
10030            array.as_ref(),
10031            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 2),
10032        )
10033        .unwrap();
10034        assert_eq!(expected.as_ref(), actual.as_ref());
10035
10036        let res = cast_with_options(
10037            array.as_ref(),
10038            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int16, true)), 2),
10039            &CastOptions {
10040                safe: false,
10041                ..Default::default()
10042            },
10043        );
10044        assert!(res.is_err());
10045        assert!(format!("{res:?}").contains("Can't cast value 2147483647 to type Int16"));
10046    }
10047
10048    #[test]
10049    fn test_cast_list_to_fsl_empty() {
10050        let inner_field = Arc::new(Field::new_list_field(DataType::Int32, true));
10051        let target_type = DataType::FixedSizeList(inner_field.clone(), 3);
10052        let expected = new_empty_array(&target_type);
10053
10054        // list
10055        let array = new_empty_array(&DataType::List(inner_field.clone()));
10056        assert!(can_cast_types(array.data_type(), &target_type));
10057        let actual = cast(array.as_ref(), &target_type).unwrap();
10058        assert_eq!(expected.as_ref(), actual.as_ref());
10059
10060        // largelist
10061        let array = new_empty_array(&DataType::LargeList(inner_field.clone()));
10062        assert!(can_cast_types(array.data_type(), &target_type));
10063        let actual = cast(array.as_ref(), &target_type).unwrap();
10064        assert_eq!(expected.as_ref(), actual.as_ref());
10065
10066        // listview
10067        let array = new_empty_array(&DataType::ListView(inner_field.clone()));
10068        assert!(can_cast_types(array.data_type(), &target_type));
10069        let actual = cast(array.as_ref(), &target_type).unwrap();
10070        assert_eq!(expected.as_ref(), actual.as_ref());
10071
10072        // largelistview
10073        let array = new_empty_array(&DataType::LargeListView(inner_field.clone()));
10074        assert!(can_cast_types(array.data_type(), &target_type));
10075        let actual = cast(array.as_ref(), &target_type).unwrap();
10076        assert_eq!(expected.as_ref(), actual.as_ref());
10077    }
10078
10079    fn make_list_array() -> ArrayRef {
10080        // [[0, 1, 2], [3, 4, 5], [6, 7]]
10081        Arc::new(ListArray::new(
10082            Field::new_list_field(DataType::Int32, true).into(),
10083            OffsetBuffer::from_lengths(vec![3, 3, 2]),
10084            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10085            None,
10086        ))
10087    }
10088
10089    fn make_large_list_array() -> ArrayRef {
10090        // [[0, 1, 2], [3, 4, 5], [6, 7]]
10091        Arc::new(LargeListArray::new(
10092            Field::new_list_field(DataType::Int32, true).into(),
10093            OffsetBuffer::from_lengths(vec![3, 3, 2]),
10094            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10095            None,
10096        ))
10097    }
10098
10099    fn make_list_view_array() -> ArrayRef {
10100        // [[0, 1, 2], [3, 4, 5], [6, 7]]
10101        Arc::new(ListViewArray::new(
10102            Field::new_list_field(DataType::Int32, true).into(),
10103            vec![0, 3, 6].into(),
10104            vec![3, 3, 2].into(),
10105            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10106            None,
10107        ))
10108    }
10109
10110    fn make_large_list_view_array() -> ArrayRef {
10111        // [[0, 1, 2], [3, 4, 5], [6, 7]]
10112        Arc::new(LargeListViewArray::new(
10113            Field::new_list_field(DataType::Int32, true).into(),
10114            vec![0, 3, 6].into(),
10115            vec![3, 3, 2].into(),
10116            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10117            None,
10118        ))
10119    }
10120
10121    fn make_fixed_size_list_array() -> ArrayRef {
10122        // [[0, 1, 2, 3], [4, 5, 6, 7]]
10123        Arc::new(FixedSizeListArray::new(
10124            Field::new_list_field(DataType::Int32, true).into(),
10125            4,
10126            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10127            None,
10128        ))
10129    }
10130
10131    fn make_fixed_size_list_array_for_large_list() -> ArrayRef {
10132        // [[0, 1, 2, 3], [4, 5, 6, 7]]
10133        Arc::new(FixedSizeListArray::new(
10134            Field::new_list_field(DataType::Int64, true).into(),
10135            4,
10136            Arc::new(Int64Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10137            None,
10138        ))
10139    }
10140
10141    #[test]
10142    fn test_cast_map_dont_allow_change_of_order() {
10143        let string_builder = StringBuilder::new();
10144        let value_builder = StringBuilder::new();
10145        let mut builder = MapBuilder::new(
10146            Some(MapFieldNames {
10147                entry: "entries".to_string(),
10148                key: "key".to_string(),
10149                value: "value".to_string(),
10150            }),
10151            string_builder,
10152            value_builder,
10153        );
10154
10155        builder.keys().append_value("0");
10156        builder.values().append_value("test_val_1");
10157        builder.append(true).unwrap();
10158        builder.keys().append_value("1");
10159        builder.values().append_value("test_val_2");
10160        builder.append(true).unwrap();
10161
10162        // map builder returns unsorted map by default
10163        let array = builder.finish();
10164
10165        let new_ordered = true;
10166        let new_type = DataType::Map(
10167            Arc::new(Field::new(
10168                "entries",
10169                DataType::Struct(
10170                    vec![
10171                        Field::new("key", DataType::Utf8, false),
10172                        Field::new("value", DataType::Utf8, false),
10173                    ]
10174                    .into(),
10175                ),
10176                false,
10177            )),
10178            new_ordered,
10179        );
10180
10181        let new_array_result = cast(&array, &new_type.clone());
10182        assert!(!can_cast_types(array.data_type(), &new_type));
10183        let Err(ArrowError::CastError(t)) = new_array_result else {
10184            panic!();
10185        };
10186        assert_eq!(
10187            t,
10188            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"#
10189        );
10190    }
10191
10192    #[test]
10193    fn test_cast_map_dont_allow_when_container_cant_cast() {
10194        let string_builder = StringBuilder::new();
10195        let value_builder = IntervalDayTimeArray::builder(2);
10196        let mut builder = MapBuilder::new(
10197            Some(MapFieldNames {
10198                entry: "entries".to_string(),
10199                key: "key".to_string(),
10200                value: "value".to_string(),
10201            }),
10202            string_builder,
10203            value_builder,
10204        );
10205
10206        builder.keys().append_value("0");
10207        builder.values().append_value(IntervalDayTime::new(1, 1));
10208        builder.append(true).unwrap();
10209        builder.keys().append_value("1");
10210        builder.values().append_value(IntervalDayTime::new(2, 2));
10211        builder.append(true).unwrap();
10212
10213        // map builder returns unsorted map by default
10214        let array = builder.finish();
10215
10216        let new_ordered = true;
10217        let new_type = DataType::Map(
10218            Arc::new(Field::new(
10219                "entries",
10220                DataType::Struct(
10221                    vec![
10222                        Field::new("key", DataType::Utf8, false),
10223                        Field::new("value", DataType::Duration(TimeUnit::Second), false),
10224                    ]
10225                    .into(),
10226                ),
10227                false,
10228            )),
10229            new_ordered,
10230        );
10231
10232        let new_array_result = cast(&array, &new_type.clone());
10233        assert!(!can_cast_types(array.data_type(), &new_type));
10234        let Err(ArrowError::CastError(t)) = new_array_result else {
10235            panic!();
10236        };
10237        assert_eq!(
10238            t,
10239            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"#
10240        );
10241    }
10242
10243    #[test]
10244    fn test_cast_map_field_names() {
10245        let string_builder = StringBuilder::new();
10246        let value_builder = StringBuilder::new();
10247        let mut builder = MapBuilder::new(
10248            Some(MapFieldNames {
10249                entry: "entries".to_string(),
10250                key: "key".to_string(),
10251                value: "value".to_string(),
10252            }),
10253            string_builder,
10254            value_builder,
10255        );
10256
10257        builder.keys().append_value("0");
10258        builder.values().append_value("test_val_1");
10259        builder.append(true).unwrap();
10260        builder.keys().append_value("1");
10261        builder.values().append_value("test_val_2");
10262        builder.append(true).unwrap();
10263        builder.append(false).unwrap();
10264
10265        let array = builder.finish();
10266
10267        let new_type = DataType::Map(
10268            Arc::new(Field::new(
10269                "entries_new",
10270                DataType::Struct(
10271                    vec![
10272                        Field::new("key_new", DataType::Utf8, false),
10273                        Field::new("value_values", DataType::Utf8, false),
10274                    ]
10275                    .into(),
10276                ),
10277                false,
10278            )),
10279            false,
10280        );
10281
10282        assert_ne!(new_type, array.data_type().clone());
10283
10284        let new_array = cast(&array, &new_type.clone()).unwrap();
10285        assert_eq!(new_type, new_array.data_type().clone());
10286        let map_array = new_array.as_map();
10287
10288        assert_ne!(new_type, array.data_type().clone());
10289        assert_eq!(new_type, map_array.data_type().clone());
10290
10291        let key_string = map_array
10292            .keys()
10293            .as_any()
10294            .downcast_ref::<StringArray>()
10295            .unwrap()
10296            .into_iter()
10297            .flatten()
10298            .collect::<Vec<_>>();
10299        assert_eq!(&key_string, &vec!["0", "1"]);
10300
10301        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
10302        let values_string = values_string_array
10303            .as_any()
10304            .downcast_ref::<StringArray>()
10305            .unwrap()
10306            .into_iter()
10307            .flatten()
10308            .collect::<Vec<_>>();
10309        assert_eq!(&values_string, &vec!["test_val_1", "test_val_2"]);
10310
10311        assert_eq!(
10312            map_array.nulls(),
10313            Some(&NullBuffer::from(vec![true, true, false]))
10314        );
10315    }
10316
10317    #[test]
10318    fn test_cast_map_contained_values() {
10319        let string_builder = StringBuilder::new();
10320        let value_builder = Int8Builder::new();
10321        let mut builder = MapBuilder::new(
10322            Some(MapFieldNames {
10323                entry: "entries".to_string(),
10324                key: "key".to_string(),
10325                value: "value".to_string(),
10326            }),
10327            string_builder,
10328            value_builder,
10329        );
10330
10331        builder.keys().append_value("0");
10332        builder.values().append_value(44);
10333        builder.append(true).unwrap();
10334        builder.keys().append_value("1");
10335        builder.values().append_value(22);
10336        builder.append(true).unwrap();
10337
10338        let array = builder.finish();
10339
10340        let new_type = DataType::Map(
10341            Arc::new(Field::new(
10342                "entries",
10343                DataType::Struct(
10344                    vec![
10345                        Field::new("key", DataType::Utf8, false),
10346                        Field::new("value", DataType::Utf8, false),
10347                    ]
10348                    .into(),
10349                ),
10350                false,
10351            )),
10352            false,
10353        );
10354
10355        let new_array = cast(&array, &new_type.clone()).unwrap();
10356        assert_eq!(new_type, new_array.data_type().clone());
10357        let map_array = new_array.as_map();
10358
10359        assert_ne!(new_type, array.data_type().clone());
10360        assert_eq!(new_type, map_array.data_type().clone());
10361
10362        let key_string = map_array
10363            .keys()
10364            .as_any()
10365            .downcast_ref::<StringArray>()
10366            .unwrap()
10367            .into_iter()
10368            .flatten()
10369            .collect::<Vec<_>>();
10370        assert_eq!(&key_string, &vec!["0", "1"]);
10371
10372        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
10373        let values_string = values_string_array
10374            .as_any()
10375            .downcast_ref::<StringArray>()
10376            .unwrap()
10377            .into_iter()
10378            .flatten()
10379            .collect::<Vec<_>>();
10380        assert_eq!(&values_string, &vec!["44", "22"]);
10381    }
10382
10383    #[test]
10384    fn test_utf8_cast_offsets() {
10385        // test if offset of the array is taken into account during cast
10386        let str_array = StringArray::from(vec!["a", "b", "c"]);
10387        let str_array = str_array.slice(1, 2);
10388
10389        let out = cast(&str_array, &DataType::LargeUtf8).unwrap();
10390
10391        let large_str_array = out.as_any().downcast_ref::<LargeStringArray>().unwrap();
10392        let strs = large_str_array.into_iter().flatten().collect::<Vec<_>>();
10393        assert_eq!(strs, &["b", "c"])
10394    }
10395
10396    #[test]
10397    fn test_list_cast_offsets() {
10398        // test if offset of the array is taken into account during cast
10399        let array1 = make_list_array().slice(1, 2);
10400        let array2 = make_list_array();
10401
10402        let dt = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
10403        let out1 = cast(&array1, &dt).unwrap();
10404        let out2 = cast(&array2, &dt).unwrap();
10405
10406        assert_eq!(&out1, &out2.slice(1, 2))
10407    }
10408
10409    #[test]
10410    fn test_list_to_string() {
10411        fn assert_cast(array: &ArrayRef, expected: &[&str]) {
10412            assert!(can_cast_types(array.data_type(), &DataType::Utf8));
10413            let out = cast(array, &DataType::Utf8).unwrap();
10414            let out = out
10415                .as_string::<i32>()
10416                .into_iter()
10417                .flatten()
10418                .collect::<Vec<_>>();
10419            assert_eq!(out, expected);
10420
10421            assert!(can_cast_types(array.data_type(), &DataType::LargeUtf8));
10422            let out = cast(array, &DataType::LargeUtf8).unwrap();
10423            let out = out
10424                .as_string::<i64>()
10425                .into_iter()
10426                .flatten()
10427                .collect::<Vec<_>>();
10428            assert_eq!(out, expected);
10429
10430            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
10431            let out = cast(array, &DataType::Utf8View).unwrap();
10432            let out = out
10433                .as_string_view()
10434                .into_iter()
10435                .flatten()
10436                .collect::<Vec<_>>();
10437            assert_eq!(out, expected);
10438        }
10439
10440        let array = Arc::new(ListArray::new(
10441            Field::new_list_field(DataType::Utf8, true).into(),
10442            OffsetBuffer::from_lengths(vec![3, 3, 2]),
10443            Arc::new(StringArray::from(vec![
10444                "a", "b", "c", "d", "e", "f", "g", "h",
10445            ])),
10446            None,
10447        )) as ArrayRef;
10448
10449        assert_cast(&array, &["[a, b, c]", "[d, e, f]", "[g, h]"]);
10450
10451        let array = make_list_array();
10452        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10453
10454        let array = make_large_list_array();
10455        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10456
10457        let array = make_list_view_array();
10458        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10459
10460        let array = make_large_list_view_array();
10461        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10462    }
10463
10464    #[test]
10465    fn test_cast_f64_to_decimal128() {
10466        // to reproduce https://github.com/apache/arrow-rs/issues/2997
10467
10468        let decimal_type = DataType::Decimal128(18, 2);
10469        let array = Float64Array::from(vec![
10470            Some(0.0699999999),
10471            Some(0.0659999999),
10472            Some(0.0650000000),
10473            Some(0.0649999999),
10474        ]);
10475        let array = Arc::new(array) as ArrayRef;
10476        generate_cast_test_case!(
10477            &array,
10478            Decimal128Array,
10479            &decimal_type,
10480            vec![
10481                Some(7_i128), // round up
10482                Some(7_i128), // round up
10483                Some(7_i128), // round up
10484                Some(6_i128), // round down
10485            ]
10486        );
10487
10488        let decimal_type = DataType::Decimal128(18, 3);
10489        let array = Float64Array::from(vec![
10490            Some(0.0699999999),
10491            Some(0.0659999999),
10492            Some(0.0650000000),
10493            Some(0.0649999999),
10494        ]);
10495        let array = Arc::new(array) as ArrayRef;
10496        generate_cast_test_case!(
10497            &array,
10498            Decimal128Array,
10499            &decimal_type,
10500            vec![
10501                Some(70_i128), // round up
10502                Some(66_i128), // round up
10503                Some(65_i128), // round down
10504                Some(65_i128), // round up
10505            ]
10506        );
10507    }
10508
10509    #[test]
10510    fn test_cast_numeric_to_decimal128_overflow() {
10511        let array = Int64Array::from(vec![i64::MAX]);
10512        let array = Arc::new(array) as ArrayRef;
10513        let casted_array = cast_with_options(
10514            &array,
10515            &DataType::Decimal128(38, 30),
10516            &CastOptions {
10517                safe: true,
10518                format_options: FormatOptions::default(),
10519            },
10520        );
10521        assert!(casted_array.is_ok());
10522        assert!(casted_array.unwrap().is_null(0));
10523
10524        let casted_array = cast_with_options(
10525            &array,
10526            &DataType::Decimal128(38, 30),
10527            &CastOptions {
10528                safe: false,
10529                format_options: FormatOptions::default(),
10530            },
10531        );
10532        assert!(casted_array.is_err());
10533    }
10534
10535    #[test]
10536    fn test_cast_numeric_to_decimal256_overflow() {
10537        let array = Int64Array::from(vec![i64::MAX]);
10538        let array = Arc::new(array) as ArrayRef;
10539        let casted_array = cast_with_options(
10540            &array,
10541            &DataType::Decimal256(76, 76),
10542            &CastOptions {
10543                safe: true,
10544                format_options: FormatOptions::default(),
10545            },
10546        );
10547        assert!(casted_array.is_ok());
10548        assert!(casted_array.unwrap().is_null(0));
10549
10550        let casted_array = cast_with_options(
10551            &array,
10552            &DataType::Decimal256(76, 76),
10553            &CastOptions {
10554                safe: false,
10555                format_options: FormatOptions::default(),
10556            },
10557        );
10558        assert!(casted_array.is_err());
10559    }
10560
10561    #[test]
10562    fn test_cast_floating_point_to_decimal128_precision_overflow() {
10563        let array = Float64Array::from(vec![1.1]);
10564        let array = Arc::new(array) as ArrayRef;
10565        let casted_array = cast_with_options(
10566            &array,
10567            &DataType::Decimal128(2, 2),
10568            &CastOptions {
10569                safe: true,
10570                format_options: FormatOptions::default(),
10571            },
10572        );
10573        assert!(casted_array.is_ok());
10574        assert!(casted_array.unwrap().is_null(0));
10575
10576        let casted_array = cast_with_options(
10577            &array,
10578            &DataType::Decimal128(2, 2),
10579            &CastOptions {
10580                safe: false,
10581                format_options: FormatOptions::default(),
10582            },
10583        );
10584        let err = casted_array.unwrap_err().to_string();
10585        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
10586        assert!(
10587            err.contains(expected_error),
10588            "did not find expected error '{expected_error}' in actual error '{err}'"
10589        );
10590    }
10591
10592    #[test]
10593    fn test_cast_float16_to_decimal128_precision_overflow() {
10594        let array = Float16Array::from(vec![f16::from_f32(1.1)]);
10595        let array = Arc::new(array) as ArrayRef;
10596        let casted_array = cast_with_options(
10597            &array,
10598            &DataType::Decimal128(2, 2),
10599            &CastOptions {
10600                safe: true,
10601                format_options: FormatOptions::default(),
10602            },
10603        );
10604        assert!(casted_array.is_ok());
10605        assert!(casted_array.unwrap().is_null(0));
10606
10607        let casted_array = cast_with_options(
10608            &array,
10609            &DataType::Decimal128(2, 2),
10610            &CastOptions {
10611                safe: false,
10612                format_options: FormatOptions::default(),
10613            },
10614        );
10615        let err = casted_array.unwrap_err().to_string();
10616        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
10617        assert_eq!(err, expected_error);
10618    }
10619
10620    #[test]
10621    fn test_cast_float16_to_decimal256_precision_overflow() {
10622        let array = Float16Array::from(vec![f16::from_f32(1.1)]);
10623        let array = Arc::new(array) as ArrayRef;
10624        let casted_array = cast_with_options(
10625            &array,
10626            &DataType::Decimal256(2, 2),
10627            &CastOptions {
10628                safe: true,
10629                format_options: FormatOptions::default(),
10630            },
10631        );
10632        assert!(casted_array.is_ok());
10633        assert!(casted_array.unwrap().is_null(0));
10634
10635        let casted_array = cast_with_options(
10636            &array,
10637            &DataType::Decimal256(2, 2),
10638            &CastOptions {
10639                safe: false,
10640                format_options: FormatOptions::default(),
10641            },
10642        );
10643        let err = casted_array.unwrap_err().to_string();
10644        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
10645        assert_eq!(err, expected_error);
10646    }
10647
10648    #[test]
10649    fn test_cast_float16_to_decimal128_non_finite() {
10650        let array = Float16Array::from(vec![f16::NAN, f16::INFINITY, f16::NEG_INFINITY]);
10651        let array = Arc::new(array) as ArrayRef;
10652        let casted_array = cast_with_options(
10653            &array,
10654            &DataType::Decimal128(38, 2),
10655            &CastOptions {
10656                safe: true,
10657                format_options: FormatOptions::default(),
10658            },
10659        )
10660        .unwrap();
10661
10662        assert!(casted_array.is_null(0));
10663        assert!(casted_array.is_null(1));
10664        assert!(casted_array.is_null(2));
10665
10666        let casted_array = cast_with_options(
10667            &array,
10668            &DataType::Decimal128(38, 2),
10669            &CastOptions {
10670                safe: false,
10671                format_options: FormatOptions::default(),
10672            },
10673        );
10674        let err = casted_array.unwrap_err().to_string();
10675        let expected_error = "Cannot cast to Decimal128(38, 2)";
10676        assert!(
10677            err.contains(expected_error),
10678            "did not find expected error '{expected_error}' in actual error '{err}'"
10679        );
10680    }
10681
10682    #[test]
10683    fn test_cast_floating_point_to_decimal256_precision_overflow() {
10684        let array = Float64Array::from(vec![1.1]);
10685        let array = Arc::new(array) as ArrayRef;
10686        let casted_array = cast_with_options(
10687            &array,
10688            &DataType::Decimal256(2, 2),
10689            &CastOptions {
10690                safe: true,
10691                format_options: FormatOptions::default(),
10692            },
10693        );
10694        assert!(casted_array.is_ok());
10695        assert!(casted_array.unwrap().is_null(0));
10696
10697        let casted_array = cast_with_options(
10698            &array,
10699            &DataType::Decimal256(2, 2),
10700            &CastOptions {
10701                safe: false,
10702                format_options: FormatOptions::default(),
10703            },
10704        );
10705        let err = casted_array.unwrap_err().to_string();
10706        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
10707        assert_eq!(err, expected_error);
10708    }
10709
10710    #[test]
10711    fn test_cast_floating_point_to_decimal128_overflow() {
10712        let array = Float64Array::from(vec![f64::MAX]);
10713        let array = Arc::new(array) as ArrayRef;
10714        let casted_array = cast_with_options(
10715            &array,
10716            &DataType::Decimal128(38, 30),
10717            &CastOptions {
10718                safe: true,
10719                format_options: FormatOptions::default(),
10720            },
10721        );
10722        assert!(casted_array.is_ok());
10723        assert!(casted_array.unwrap().is_null(0));
10724
10725        let casted_array = cast_with_options(
10726            &array,
10727            &DataType::Decimal128(38, 30),
10728            &CastOptions {
10729                safe: false,
10730                format_options: FormatOptions::default(),
10731            },
10732        );
10733        let err = casted_array.unwrap_err().to_string();
10734        let expected_error = "Cast error: Cannot cast to Decimal128(38, 30)";
10735        assert!(
10736            err.contains(expected_error),
10737            "did not find expected error '{expected_error}' in actual error '{err}'"
10738        );
10739    }
10740
10741    #[test]
10742    fn test_cast_floating_point_to_decimal256_overflow() {
10743        let array = Float64Array::from(vec![f64::MAX]);
10744        let array = Arc::new(array) as ArrayRef;
10745        let casted_array = cast_with_options(
10746            &array,
10747            &DataType::Decimal256(76, 50),
10748            &CastOptions {
10749                safe: true,
10750                format_options: FormatOptions::default(),
10751            },
10752        );
10753        assert!(casted_array.is_ok());
10754        assert!(casted_array.unwrap().is_null(0));
10755
10756        let casted_array = cast_with_options(
10757            &array,
10758            &DataType::Decimal256(76, 50),
10759            &CastOptions {
10760                safe: false,
10761                format_options: FormatOptions::default(),
10762            },
10763        );
10764        let err = casted_array.unwrap_err().to_string();
10765        let expected_error = "Cast error: Cannot cast to Decimal256(76, 50)";
10766        assert!(
10767            err.contains(expected_error),
10768            "did not find expected error '{expected_error}' in actual error '{err}'"
10769        );
10770    }
10771    #[test]
10772    fn test_cast_decimal256_to_f64_no_overflow() {
10773        // Test casting i256::MAX: should produce a large finite positive value
10774        let array = vec![Some(i256::MAX)];
10775        let array = create_decimal256_array(array, 76, 2).unwrap();
10776        let array = Arc::new(array) as ArrayRef;
10777
10778        let result = cast(&array, &DataType::Float64).unwrap();
10779        let result = result.as_primitive::<Float64Type>();
10780        assert!(result.value(0).is_finite());
10781        assert!(result.value(0) > 0.0); // Positive result
10782
10783        // Test casting i256::MIN: should produce a large finite negative value
10784        let array = vec![Some(i256::MIN)];
10785        let array = create_decimal256_array(array, 76, 2).unwrap();
10786        let array = Arc::new(array) as ArrayRef;
10787
10788        let result = cast(&array, &DataType::Float64).unwrap();
10789        let result = result.as_primitive::<Float64Type>();
10790        assert!(result.value(0).is_finite());
10791        assert!(result.value(0) < 0.0); // Negative result
10792    }
10793
10794    #[test]
10795    fn test_cast_decimal128_to_decimal128_negative_scale() {
10796        let input_type = DataType::Decimal128(20, 0);
10797        let output_type = DataType::Decimal128(20, -1);
10798        assert!(can_cast_types(&input_type, &output_type));
10799        let array = vec![Some(1123450), Some(2123455), Some(3123456), None];
10800        let input_decimal_array = create_decimal128_array(array, 20, 0).unwrap();
10801        let array = Arc::new(input_decimal_array) as ArrayRef;
10802        generate_cast_test_case!(
10803            &array,
10804            Decimal128Array,
10805            &output_type,
10806            vec![
10807                Some(112345_i128),
10808                Some(212346_i128),
10809                Some(312346_i128),
10810                None
10811            ]
10812        );
10813
10814        let casted_array = cast(&array, &output_type).unwrap();
10815        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10816
10817        assert_eq!("1123450", decimal_arr.value_as_string(0));
10818        assert_eq!("2123460", decimal_arr.value_as_string(1));
10819        assert_eq!("3123460", decimal_arr.value_as_string(2));
10820    }
10821
10822    #[test]
10823    fn decimal128_min_max_to_f64() {
10824        // Ensure Decimal128 i128::MIN/MAX round-trip cast
10825        let min128 = i128::MIN;
10826        let max128 = i128::MAX;
10827        assert_eq!(min128 as f64, min128 as f64);
10828        assert_eq!(max128 as f64, max128 as f64);
10829    }
10830
10831    #[test]
10832    fn test_cast_numeric_to_decimal128_negative() {
10833        let decimal_type = DataType::Decimal128(38, -1);
10834        let array = Arc::new(Int32Array::from(vec![
10835            Some(1123456),
10836            Some(2123456),
10837            Some(3123456),
10838        ])) as ArrayRef;
10839
10840        let casted_array = cast(&array, &decimal_type).unwrap();
10841        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10842
10843        assert_eq!("1123450", decimal_arr.value_as_string(0));
10844        assert_eq!("2123450", decimal_arr.value_as_string(1));
10845        assert_eq!("3123450", decimal_arr.value_as_string(2));
10846
10847        let array = Arc::new(Float32Array::from(vec![
10848            Some(1123.456),
10849            Some(2123.456),
10850            Some(3123.456),
10851        ])) as ArrayRef;
10852
10853        let casted_array = cast(&array, &decimal_type).unwrap();
10854        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10855
10856        assert_eq!("1120", decimal_arr.value_as_string(0));
10857        assert_eq!("2120", decimal_arr.value_as_string(1));
10858        assert_eq!("3120", decimal_arr.value_as_string(2));
10859    }
10860
10861    #[test]
10862    fn test_cast_decimal128_to_decimal128_negative() {
10863        let input_type = DataType::Decimal128(10, -1);
10864        let output_type = DataType::Decimal128(10, -2);
10865        assert!(can_cast_types(&input_type, &output_type));
10866        let array = vec![Some(123)];
10867        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
10868        let array = Arc::new(input_decimal_array) as ArrayRef;
10869        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(12_i128),]);
10870
10871        let casted_array = cast(&array, &output_type).unwrap();
10872        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10873
10874        assert_eq!("1200", decimal_arr.value_as_string(0));
10875
10876        let array = vec![Some(125)];
10877        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
10878        let array = Arc::new(input_decimal_array) as ArrayRef;
10879        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(13_i128),]);
10880
10881        let casted_array = cast(&array, &output_type).unwrap();
10882        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10883
10884        assert_eq!("1300", decimal_arr.value_as_string(0));
10885    }
10886
10887    #[test]
10888    fn test_cast_decimal128_to_decimal256_negative() {
10889        let input_type = DataType::Decimal128(10, 3);
10890        let output_type = DataType::Decimal256(10, 5);
10891        assert!(can_cast_types(&input_type, &output_type));
10892        let array = vec![Some(123456), Some(-123456)];
10893        let input_decimal_array = create_decimal128_array(array, 10, 3).unwrap();
10894        let array = Arc::new(input_decimal_array) as ArrayRef;
10895
10896        let hundred = i256::from_i128(100);
10897        generate_cast_test_case!(
10898            &array,
10899            Decimal256Array,
10900            &output_type,
10901            vec![
10902                Some(i256::from_i128(123456).mul_wrapping(hundred)),
10903                Some(i256::from_i128(-123456).mul_wrapping(hundred))
10904            ]
10905        );
10906    }
10907
10908    #[test]
10909    fn test_parse_string_to_decimal() {
10910        assert_eq!(
10911            Decimal128Type::format_decimal(
10912                parse_string_to_decimal_native::<Decimal128Type>("123.45", 2).unwrap(),
10913                38,
10914                2,
10915            ),
10916            "123.45"
10917        );
10918        assert_eq!(
10919            Decimal128Type::format_decimal(
10920                parse_string_to_decimal_native::<Decimal128Type>("12345", 2).unwrap(),
10921                38,
10922                2,
10923            ),
10924            "12345.00"
10925        );
10926        assert_eq!(
10927            Decimal128Type::format_decimal(
10928                parse_string_to_decimal_native::<Decimal128Type>("0.12345", 2).unwrap(),
10929                38,
10930                2,
10931            ),
10932            "0.12"
10933        );
10934        assert_eq!(
10935            Decimal128Type::format_decimal(
10936                parse_string_to_decimal_native::<Decimal128Type>(".12345", 2).unwrap(),
10937                38,
10938                2,
10939            ),
10940            "0.12"
10941        );
10942        assert_eq!(
10943            Decimal128Type::format_decimal(
10944                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
10945                38,
10946                2,
10947            ),
10948            "0.13"
10949        );
10950        assert_eq!(
10951            Decimal128Type::format_decimal(
10952                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
10953                38,
10954                2,
10955            ),
10956            "0.13"
10957        );
10958
10959        assert_eq!(
10960            Decimal256Type::format_decimal(
10961                parse_string_to_decimal_native::<Decimal256Type>("123.45", 3).unwrap(),
10962                38,
10963                3,
10964            ),
10965            "123.450"
10966        );
10967        assert_eq!(
10968            Decimal256Type::format_decimal(
10969                parse_string_to_decimal_native::<Decimal256Type>("12345", 3).unwrap(),
10970                38,
10971                3,
10972            ),
10973            "12345.000"
10974        );
10975        assert_eq!(
10976            Decimal256Type::format_decimal(
10977                parse_string_to_decimal_native::<Decimal256Type>("0.12345", 3).unwrap(),
10978                38,
10979                3,
10980            ),
10981            "0.123"
10982        );
10983        assert_eq!(
10984            Decimal256Type::format_decimal(
10985                parse_string_to_decimal_native::<Decimal256Type>(".12345", 3).unwrap(),
10986                38,
10987                3,
10988            ),
10989            "0.123"
10990        );
10991        assert_eq!(
10992            Decimal256Type::format_decimal(
10993                parse_string_to_decimal_native::<Decimal256Type>(".1265", 3).unwrap(),
10994                38,
10995                3,
10996            ),
10997            "0.127"
10998        );
10999    }
11000
11001    fn test_cast_string_to_decimal(array: ArrayRef) {
11002        // Decimal128
11003        let output_type = DataType::Decimal128(38, 2);
11004        assert!(can_cast_types(array.data_type(), &output_type));
11005
11006        let casted_array = cast(&array, &output_type).unwrap();
11007        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11008
11009        assert_eq!("123.45", decimal_arr.value_as_string(0));
11010        assert_eq!("1.23", decimal_arr.value_as_string(1));
11011        assert_eq!("0.12", decimal_arr.value_as_string(2));
11012        assert_eq!("0.13", decimal_arr.value_as_string(3));
11013        assert_eq!("1.26", decimal_arr.value_as_string(4));
11014        assert_eq!("12345.00", decimal_arr.value_as_string(5));
11015        assert_eq!("12345.00", decimal_arr.value_as_string(6));
11016        assert_eq!("0.12", decimal_arr.value_as_string(7));
11017        assert_eq!("12.23", decimal_arr.value_as_string(8));
11018        assert!(decimal_arr.is_null(9));
11019        assert!(decimal_arr.is_null(10));
11020        assert!(decimal_arr.is_null(11));
11021        assert!(decimal_arr.is_null(12));
11022        assert_eq!("-1.23", decimal_arr.value_as_string(13));
11023        assert_eq!("-1.24", decimal_arr.value_as_string(14));
11024        assert_eq!("0.00", decimal_arr.value_as_string(15));
11025        assert_eq!("-123.00", decimal_arr.value_as_string(16));
11026        assert_eq!("-123.23", decimal_arr.value_as_string(17));
11027        assert_eq!("-0.12", decimal_arr.value_as_string(18));
11028        assert_eq!("1.23", decimal_arr.value_as_string(19));
11029        assert_eq!("1.24", decimal_arr.value_as_string(20));
11030        assert_eq!("0.00", decimal_arr.value_as_string(21));
11031        assert_eq!("123.00", decimal_arr.value_as_string(22));
11032        assert_eq!("123.23", decimal_arr.value_as_string(23));
11033        assert_eq!("0.12", decimal_arr.value_as_string(24));
11034        assert!(decimal_arr.is_null(25));
11035        assert!(decimal_arr.is_null(26));
11036        assert!(decimal_arr.is_null(27));
11037        assert_eq!("0.00", decimal_arr.value_as_string(28));
11038        assert_eq!("0.00", decimal_arr.value_as_string(29));
11039        assert_eq!("12345.00", decimal_arr.value_as_string(30));
11040        assert_eq!(decimal_arr.len(), 31);
11041
11042        // Decimal256
11043        let output_type = DataType::Decimal256(76, 3);
11044        assert!(can_cast_types(array.data_type(), &output_type));
11045
11046        let casted_array = cast(&array, &output_type).unwrap();
11047        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
11048
11049        assert_eq!("123.450", decimal_arr.value_as_string(0));
11050        assert_eq!("1.235", decimal_arr.value_as_string(1));
11051        assert_eq!("0.123", decimal_arr.value_as_string(2));
11052        assert_eq!("0.127", decimal_arr.value_as_string(3));
11053        assert_eq!("1.263", decimal_arr.value_as_string(4));
11054        assert_eq!("12345.000", decimal_arr.value_as_string(5));
11055        assert_eq!("12345.000", decimal_arr.value_as_string(6));
11056        assert_eq!("0.123", decimal_arr.value_as_string(7));
11057        assert_eq!("12.234", decimal_arr.value_as_string(8));
11058        assert!(decimal_arr.is_null(9));
11059        assert!(decimal_arr.is_null(10));
11060        assert!(decimal_arr.is_null(11));
11061        assert!(decimal_arr.is_null(12));
11062        assert_eq!("-1.235", decimal_arr.value_as_string(13));
11063        assert_eq!("-1.236", decimal_arr.value_as_string(14));
11064        assert_eq!("0.000", decimal_arr.value_as_string(15));
11065        assert_eq!("-123.000", decimal_arr.value_as_string(16));
11066        assert_eq!("-123.234", decimal_arr.value_as_string(17));
11067        assert_eq!("-0.123", decimal_arr.value_as_string(18));
11068        assert_eq!("1.235", decimal_arr.value_as_string(19));
11069        assert_eq!("1.236", decimal_arr.value_as_string(20));
11070        assert_eq!("0.000", decimal_arr.value_as_string(21));
11071        assert_eq!("123.000", decimal_arr.value_as_string(22));
11072        assert_eq!("123.234", decimal_arr.value_as_string(23));
11073        assert_eq!("0.123", decimal_arr.value_as_string(24));
11074        assert!(decimal_arr.is_null(25));
11075        assert!(decimal_arr.is_null(26));
11076        assert!(decimal_arr.is_null(27));
11077        assert_eq!("0.000", decimal_arr.value_as_string(28));
11078        assert_eq!("0.000", decimal_arr.value_as_string(29));
11079        assert_eq!("12345.000", decimal_arr.value_as_string(30));
11080        assert_eq!(decimal_arr.len(), 31);
11081    }
11082
11083    #[test]
11084    fn test_cast_utf8_to_decimal() {
11085        let str_array = StringArray::from(vec![
11086            Some("123.45"),
11087            Some("1.2345"),
11088            Some("0.12345"),
11089            Some("0.1267"),
11090            Some("1.263"),
11091            Some("12345.0"),
11092            Some("12345"),
11093            Some("000.123"),
11094            Some("12.234000"),
11095            None,
11096            Some(""),
11097            Some(" "),
11098            None,
11099            Some("-1.23499999"),
11100            Some("-1.23599999"),
11101            Some("-0.00001"),
11102            Some("-123"),
11103            Some("-123.234000"),
11104            Some("-000.123"),
11105            Some("+1.23499999"),
11106            Some("+1.23599999"),
11107            Some("+0.00001"),
11108            Some("+123"),
11109            Some("+123.234000"),
11110            Some("+000.123"),
11111            Some("1.-23499999"),
11112            Some("-1.-23499999"),
11113            Some("--1.23499999"),
11114            Some("0"),
11115            Some("000.000"),
11116            Some("0000000000000000012345.000"),
11117        ]);
11118        let array = Arc::new(str_array) as ArrayRef;
11119
11120        test_cast_string_to_decimal(array);
11121
11122        let test_cases = [
11123            (None, None),
11124            (Some(""), None),
11125            (Some("   "), None),
11126            (Some("0"), Some("0")),
11127            (Some("000.000"), Some("0")),
11128            (Some("12345"), Some("12345")),
11129            (Some("000000000000000000000000000012345"), Some("12345")),
11130            (Some("-123"), Some("-123")),
11131            (Some("+123"), Some("123")),
11132        ];
11133        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
11134        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
11135
11136        let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
11137        test_cast_string_to_decimal_scale_zero(array, &expected);
11138    }
11139
11140    #[test]
11141    fn test_cast_large_utf8_to_decimal() {
11142        let str_array = LargeStringArray::from(vec![
11143            Some("123.45"),
11144            Some("1.2345"),
11145            Some("0.12345"),
11146            Some("0.1267"),
11147            Some("1.263"),
11148            Some("12345.0"),
11149            Some("12345"),
11150            Some("000.123"),
11151            Some("12.234000"),
11152            None,
11153            Some(""),
11154            Some(" "),
11155            None,
11156            Some("-1.23499999"),
11157            Some("-1.23599999"),
11158            Some("-0.00001"),
11159            Some("-123"),
11160            Some("-123.234000"),
11161            Some("-000.123"),
11162            Some("+1.23499999"),
11163            Some("+1.23599999"),
11164            Some("+0.00001"),
11165            Some("+123"),
11166            Some("+123.234000"),
11167            Some("+000.123"),
11168            Some("1.-23499999"),
11169            Some("-1.-23499999"),
11170            Some("--1.23499999"),
11171            Some("0"),
11172            Some("000.000"),
11173            Some("0000000000000000012345.000"),
11174        ]);
11175        let array = Arc::new(str_array) as ArrayRef;
11176
11177        test_cast_string_to_decimal(array);
11178
11179        let test_cases = [
11180            (None, None),
11181            (Some(""), None),
11182            (Some("   "), None),
11183            (Some("0"), Some("0")),
11184            (Some("000.000"), Some("0")),
11185            (Some("12345"), Some("12345")),
11186            (Some("000000000000000000000000000012345"), Some("12345")),
11187            (Some("-123"), Some("-123")),
11188            (Some("+123"), Some("123")),
11189        ];
11190        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
11191        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
11192
11193        let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
11194        test_cast_string_to_decimal_scale_zero(array, &expected);
11195    }
11196
11197    fn test_cast_string_to_decimal_scale_zero(
11198        array: ArrayRef,
11199        expected_as_string: &[Option<&str>],
11200    ) {
11201        // Decimal128
11202        let output_type = DataType::Decimal128(38, 0);
11203        assert!(can_cast_types(array.data_type(), &output_type));
11204        let casted_array = cast(&array, &output_type).unwrap();
11205        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11206        assert_decimal_array_contents(decimal_arr, expected_as_string);
11207
11208        // Decimal256
11209        let output_type = DataType::Decimal256(76, 0);
11210        assert!(can_cast_types(array.data_type(), &output_type));
11211        let casted_array = cast(&array, &output_type).unwrap();
11212        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
11213        assert_decimal_array_contents(decimal_arr, expected_as_string);
11214    }
11215
11216    fn assert_decimal_array_contents<T>(
11217        array: &PrimitiveArray<T>,
11218        expected_as_string: &[Option<&str>],
11219    ) where
11220        T: DecimalType + ArrowPrimitiveType,
11221    {
11222        assert_eq!(array.len(), expected_as_string.len());
11223        for (i, expected) in expected_as_string.iter().enumerate() {
11224            let actual = if array.is_null(i) {
11225                None
11226            } else {
11227                Some(array.value_as_string(i))
11228            };
11229            let actual = actual.as_ref().map(|s| s.as_ref());
11230            assert_eq!(*expected, actual, "Expected at position {i}");
11231        }
11232    }
11233
11234    #[test]
11235    fn test_cast_invalid_utf8_to_decimal() {
11236        let str_array = StringArray::from(vec!["4.4.5", ". 0.123"]);
11237        let array = Arc::new(str_array) as ArrayRef;
11238
11239        // Safe cast
11240        let output_type = DataType::Decimal128(38, 2);
11241        let casted_array = cast(&array, &output_type).unwrap();
11242        assert!(casted_array.is_null(0));
11243        assert!(casted_array.is_null(1));
11244
11245        let output_type = DataType::Decimal256(76, 2);
11246        let casted_array = cast(&array, &output_type).unwrap();
11247        assert!(casted_array.is_null(0));
11248        assert!(casted_array.is_null(1));
11249
11250        // Non-safe cast
11251        let output_type = DataType::Decimal128(38, 2);
11252        let str_array = StringArray::from(vec!["4.4.5"]);
11253        let array = Arc::new(str_array) as ArrayRef;
11254        let option = CastOptions {
11255            safe: false,
11256            format_options: FormatOptions::default(),
11257        };
11258        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
11259        assert!(
11260            casted_err
11261                .to_string()
11262                .contains("Cannot cast string '4.4.5' to value of Decimal128(38, 10) type")
11263        );
11264
11265        let str_array = StringArray::from(vec![". 0.123"]);
11266        let array = Arc::new(str_array) as ArrayRef;
11267        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
11268        assert!(
11269            casted_err
11270                .to_string()
11271                .contains("Cannot cast string '. 0.123' to value of Decimal128(38, 10) type")
11272        );
11273
11274        let str_array = StringArray::from(vec![""]);
11275        let array = Arc::new(str_array) as ArrayRef;
11276        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
11277        assert!(
11278            casted_err
11279                .to_string()
11280                .contains("Cannot cast string '' to value of Decimal128(38, 10) type")
11281        );
11282    }
11283
11284    fn test_cast_string_to_decimal128_overflow(overflow_array: ArrayRef) {
11285        let output_type = DataType::Decimal128(38, 2);
11286        let casted_array = cast(&overflow_array, &output_type).unwrap();
11287        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11288
11289        assert!(decimal_arr.is_null(0));
11290        assert!(decimal_arr.is_null(1));
11291        assert!(decimal_arr.is_null(2));
11292        assert_eq!(
11293            "999999999999999999999999999999999999.99",
11294            decimal_arr.value_as_string(3)
11295        );
11296        assert_eq!(
11297            "100000000000000000000000000000000000.00",
11298            decimal_arr.value_as_string(4)
11299        );
11300    }
11301
11302    #[test]
11303    fn test_cast_string_to_decimal128_precision_overflow() {
11304        let array = StringArray::from(vec!["1000".to_string()]);
11305        let array = Arc::new(array) as ArrayRef;
11306        let casted_array = cast_with_options(
11307            &array,
11308            &DataType::Decimal128(10, 8),
11309            &CastOptions {
11310                safe: true,
11311                format_options: FormatOptions::default(),
11312            },
11313        );
11314        assert!(casted_array.is_ok());
11315        assert!(casted_array.unwrap().is_null(0));
11316
11317        let err = cast_with_options(
11318            &array,
11319            &DataType::Decimal128(10, 8),
11320            &CastOptions {
11321                safe: false,
11322                format_options: FormatOptions::default(),
11323            },
11324        );
11325        assert_eq!(
11326            "Invalid argument error: 1000.00000000 is too large to store in a Decimal128 of precision 10. Max is 99.99999999",
11327            err.unwrap_err().to_string()
11328        );
11329    }
11330
11331    #[test]
11332    fn test_cast_utf8_to_decimal128_overflow() {
11333        let overflow_str_array = StringArray::from(vec![
11334            i128::MAX.to_string(),
11335            i128::MIN.to_string(),
11336            "99999999999999999999999999999999999999".to_string(),
11337            "999999999999999999999999999999999999.99".to_string(),
11338            "99999999999999999999999999999999999.999".to_string(),
11339        ]);
11340        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11341
11342        test_cast_string_to_decimal128_overflow(overflow_array);
11343    }
11344
11345    #[test]
11346    fn test_cast_large_utf8_to_decimal128_overflow() {
11347        let overflow_str_array = LargeStringArray::from(vec![
11348            i128::MAX.to_string(),
11349            i128::MIN.to_string(),
11350            "99999999999999999999999999999999999999".to_string(),
11351            "999999999999999999999999999999999999.99".to_string(),
11352            "99999999999999999999999999999999999.999".to_string(),
11353        ]);
11354        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11355
11356        test_cast_string_to_decimal128_overflow(overflow_array);
11357    }
11358
11359    fn test_cast_string_to_decimal256_overflow(overflow_array: ArrayRef) {
11360        let output_type = DataType::Decimal256(76, 2);
11361        let casted_array = cast(&overflow_array, &output_type).unwrap();
11362        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
11363
11364        assert_eq!(
11365            "170141183460469231731687303715884105727.00",
11366            decimal_arr.value_as_string(0)
11367        );
11368        assert_eq!(
11369            "-170141183460469231731687303715884105728.00",
11370            decimal_arr.value_as_string(1)
11371        );
11372        assert_eq!(
11373            "99999999999999999999999999999999999999.00",
11374            decimal_arr.value_as_string(2)
11375        );
11376        assert_eq!(
11377            "999999999999999999999999999999999999.99",
11378            decimal_arr.value_as_string(3)
11379        );
11380        assert_eq!(
11381            "100000000000000000000000000000000000.00",
11382            decimal_arr.value_as_string(4)
11383        );
11384        assert!(decimal_arr.is_null(5));
11385        assert!(decimal_arr.is_null(6));
11386    }
11387
11388    #[test]
11389    fn test_cast_string_to_decimal256_precision_overflow() {
11390        let array = StringArray::from(vec!["1000".to_string()]);
11391        let array = Arc::new(array) as ArrayRef;
11392        let casted_array = cast_with_options(
11393            &array,
11394            &DataType::Decimal256(10, 8),
11395            &CastOptions {
11396                safe: true,
11397                format_options: FormatOptions::default(),
11398            },
11399        );
11400        assert!(casted_array.is_ok());
11401        assert!(casted_array.unwrap().is_null(0));
11402
11403        let err = cast_with_options(
11404            &array,
11405            &DataType::Decimal256(10, 8),
11406            &CastOptions {
11407                safe: false,
11408                format_options: FormatOptions::default(),
11409            },
11410        );
11411        assert_eq!(
11412            "Invalid argument error: 1000.00000000 is too large to store in a Decimal256 of precision 10. Max is 99.99999999",
11413            err.unwrap_err().to_string()
11414        );
11415    }
11416
11417    #[test]
11418    fn test_cast_utf8_to_decimal256_overflow() {
11419        let overflow_str_array = StringArray::from(vec![
11420            i128::MAX.to_string(),
11421            i128::MIN.to_string(),
11422            "99999999999999999999999999999999999999".to_string(),
11423            "999999999999999999999999999999999999.99".to_string(),
11424            "99999999999999999999999999999999999.999".to_string(),
11425            i256::MAX.to_string(),
11426            i256::MIN.to_string(),
11427        ]);
11428        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11429
11430        test_cast_string_to_decimal256_overflow(overflow_array);
11431    }
11432
11433    #[test]
11434    fn test_cast_large_utf8_to_decimal256_overflow() {
11435        let overflow_str_array = LargeStringArray::from(vec![
11436            i128::MAX.to_string(),
11437            i128::MIN.to_string(),
11438            "99999999999999999999999999999999999999".to_string(),
11439            "999999999999999999999999999999999999.99".to_string(),
11440            "99999999999999999999999999999999999.999".to_string(),
11441            i256::MAX.to_string(),
11442            i256::MIN.to_string(),
11443        ]);
11444        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11445
11446        test_cast_string_to_decimal256_overflow(overflow_array);
11447    }
11448
11449    #[test]
11450    fn test_cast_outside_supported_range_for_nanoseconds() {
11451        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";
11452
11453        let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
11454
11455        let cast_options = CastOptions {
11456            safe: false,
11457            format_options: FormatOptions::default(),
11458        };
11459
11460        let result = cast_string_to_timestamp::<i32, TimestampNanosecondType>(
11461            &array,
11462            &None::<Arc<str>>,
11463            &cast_options,
11464        );
11465
11466        let err = result.unwrap_err();
11467        assert_eq!(
11468            err.to_string(),
11469            format!(
11470                "Cast error: Overflow converting {} to Nanosecond. {}",
11471                array.value(0),
11472                EXPECTED_ERROR_MESSAGE
11473            )
11474        );
11475    }
11476
11477    #[test]
11478    fn test_cast_date32_to_timestamp() {
11479        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
11480        let array = Arc::new(a) as ArrayRef;
11481        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
11482        let c = b.as_primitive::<TimestampSecondType>();
11483        assert_eq!(1609459200, c.value(0));
11484        assert_eq!(1640995200, c.value(1));
11485        assert!(c.is_null(2));
11486    }
11487
11488    #[test]
11489    fn test_cast_date32_to_timestamp_ms() {
11490        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
11491        let array = Arc::new(a) as ArrayRef;
11492        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
11493        let c = b
11494            .as_any()
11495            .downcast_ref::<TimestampMillisecondArray>()
11496            .unwrap();
11497        assert_eq!(1609459200000, c.value(0));
11498        assert_eq!(1640995200000, c.value(1));
11499        assert!(c.is_null(2));
11500    }
11501
11502    #[test]
11503    fn test_cast_date32_to_timestamp_us() {
11504        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
11505        let array = Arc::new(a) as ArrayRef;
11506        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
11507        let c = b
11508            .as_any()
11509            .downcast_ref::<TimestampMicrosecondArray>()
11510            .unwrap();
11511        assert_eq!(1609459200000000, c.value(0));
11512        assert_eq!(1640995200000000, c.value(1));
11513        assert!(c.is_null(2));
11514    }
11515
11516    #[test]
11517    fn test_cast_date32_to_timestamp_ns() {
11518        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
11519        let array = Arc::new(a) as ArrayRef;
11520        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
11521        let c = b
11522            .as_any()
11523            .downcast_ref::<TimestampNanosecondArray>()
11524            .unwrap();
11525        assert_eq!(1609459200000000000, c.value(0));
11526        assert_eq!(1640995200000000000, c.value(1));
11527        assert!(c.is_null(2));
11528    }
11529
11530    #[test]
11531    fn test_cast_date32_to_timestamp_us_overflow() {
11532        const MAX_DAYS_MICROS: i32 = (i64::MAX / MICROSECONDS_IN_DAY) as i32;
11533        let a = Date32Array::from(vec![Some(MAX_DAYS_MICROS), Some(MAX_DAYS_MICROS + 1), None]);
11534        let array = Arc::new(a) as ArrayRef;
11535        let err = cast_with_options(
11536            &array,
11537            &DataType::Timestamp(TimeUnit::Microsecond, None),
11538            &CastOptions {
11539                safe: false,
11540                format_options: FormatOptions::default(),
11541            },
11542        );
11543        assert!(err.is_err());
11544
11545        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
11546        let c = b.as_primitive::<TimestampMicrosecondType>();
11547        assert_eq!(MAX_DAYS_MICROS as i64 * MICROSECONDS_IN_DAY, c.value(0));
11548        assert!(c.is_null(1));
11549        assert!(c.is_null(2));
11550    }
11551
11552    #[test]
11553    fn test_cast_date32_to_timestamp_ns_overflow() {
11554        // 2262-04-11, 2062-04-12
11555        let upper_limit = 106_751;
11556        let a = Date32Array::from(vec![Some(upper_limit), Some(upper_limit + 1), None]);
11557        let array = Arc::new(a) as ArrayRef;
11558        let err = cast_with_options(
11559            &array,
11560            &DataType::Timestamp(TimeUnit::Nanosecond, None),
11561            &CastOptions {
11562                safe: false,
11563                format_options: FormatOptions::default(),
11564            },
11565        );
11566        assert!(err.is_err());
11567
11568        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
11569        let c = b.as_primitive::<TimestampNanosecondType>();
11570        assert_eq!(upper_limit as i64 * NANOSECONDS_IN_DAY, c.value(0));
11571        assert!(c.is_null(1));
11572        assert!(c.is_null(2));
11573    }
11574
11575    #[test]
11576    fn test_timezone_cast() {
11577        let a = StringArray::from(vec![
11578            "2000-01-01T12:00:00", // date + time valid
11579            "2020-12-15T12:34:56", // date + time valid
11580        ]);
11581        let array = Arc::new(a) as ArrayRef;
11582        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
11583        let v = b.as_primitive::<TimestampNanosecondType>();
11584
11585        assert_eq!(v.value(0), 946728000000000000);
11586        assert_eq!(v.value(1), 1608035696000000000);
11587
11588        let b = cast(
11589            &b,
11590            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11591        )
11592        .unwrap();
11593        let v = b.as_primitive::<TimestampNanosecondType>();
11594
11595        assert_eq!(v.value(0), 946728000000000000);
11596        assert_eq!(v.value(1), 1608035696000000000);
11597
11598        let b = cast(
11599            &b,
11600            &DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
11601        )
11602        .unwrap();
11603        let v = b.as_primitive::<TimestampMillisecondType>();
11604
11605        assert_eq!(v.value(0), 946728000000);
11606        assert_eq!(v.value(1), 1608035696000);
11607    }
11608
11609    #[test]
11610    fn test_cast_utf8_to_timestamp() {
11611        fn test_tz(tz: Arc<str>) {
11612            let valid = StringArray::from(vec![
11613                "2023-01-01 04:05:06.789000-08:00",
11614                "2023-01-01 04:05:06.789000-07:00",
11615                "2023-01-01 04:05:06.789 -0800",
11616                "2023-01-01 04:05:06.789 -08:00",
11617                "2023-01-01 040506 +0730",
11618                "2023-01-01 040506 +07:30",
11619                "2023-01-01 04:05:06.789",
11620                "2023-01-01 04:05:06",
11621                "2023-01-01",
11622            ]);
11623
11624            let array = Arc::new(valid) as ArrayRef;
11625            let b = cast_with_options(
11626                &array,
11627                &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
11628                &CastOptions {
11629                    safe: false,
11630                    format_options: FormatOptions::default(),
11631                },
11632            )
11633            .unwrap();
11634
11635            let tz = tz.as_ref().parse().unwrap();
11636
11637            let as_tz =
11638                |v: i64| as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap();
11639
11640            let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
11641            let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
11642
11643            let values = b.as_primitive::<TimestampNanosecondType>().values();
11644            let utc_results: Vec<_> = values.iter().map(as_utc).collect();
11645            let local_results: Vec<_> = values.iter().map(as_local).collect();
11646
11647            // Absolute timestamps should be parsed preserving the same UTC instant
11648            assert_eq!(
11649                &utc_results[..6],
11650                &[
11651                    "2023-01-01 12:05:06.789".to_string(),
11652                    "2023-01-01 11:05:06.789".to_string(),
11653                    "2023-01-01 12:05:06.789".to_string(),
11654                    "2023-01-01 12:05:06.789".to_string(),
11655                    "2022-12-31 20:35:06".to_string(),
11656                    "2022-12-31 20:35:06".to_string(),
11657                ]
11658            );
11659            // Non-absolute timestamps should be parsed preserving the same local instant
11660            assert_eq!(
11661                &local_results[6..],
11662                &[
11663                    "2023-01-01 04:05:06.789".to_string(),
11664                    "2023-01-01 04:05:06".to_string(),
11665                    "2023-01-01 00:00:00".to_string()
11666                ]
11667            )
11668        }
11669
11670        test_tz("+00:00".into());
11671        test_tz("+02:00".into());
11672    }
11673
11674    #[test]
11675    fn test_cast_invalid_utf8() {
11676        let v1: &[u8] = b"\xFF invalid";
11677        let v2: &[u8] = b"\x00 Foo";
11678        let s = BinaryArray::from(vec![v1, v2]);
11679        let options = CastOptions {
11680            safe: true,
11681            format_options: FormatOptions::default(),
11682        };
11683        let array = cast_with_options(&s, &DataType::Utf8, &options).unwrap();
11684        let a = array.as_string::<i32>();
11685        a.to_data().validate_full().unwrap();
11686
11687        assert_eq!(a.null_count(), 1);
11688        assert_eq!(a.len(), 2);
11689        assert!(a.is_null(0));
11690        assert_eq!(a.value(0), "");
11691        assert_eq!(a.value(1), "\x00 Foo");
11692    }
11693
11694    #[test]
11695    fn test_cast_utf8_to_timestamptz() {
11696        let valid = StringArray::from(vec!["2023-01-01"]);
11697
11698        let array = Arc::new(valid) as ArrayRef;
11699        let b = cast(
11700            &array,
11701            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11702        )
11703        .unwrap();
11704
11705        let expect = DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into()));
11706
11707        assert_eq!(b.data_type(), &expect);
11708        let c = b
11709            .as_any()
11710            .downcast_ref::<TimestampNanosecondArray>()
11711            .unwrap();
11712        assert_eq!(1672531200000000000, c.value(0));
11713    }
11714
11715    #[test]
11716    fn test_cast_decimal_to_string() {
11717        assert!(can_cast_types(
11718            &DataType::Decimal32(9, 4),
11719            &DataType::Utf8View
11720        ));
11721        assert!(can_cast_types(
11722            &DataType::Decimal64(16, 4),
11723            &DataType::Utf8View
11724        ));
11725        assert!(can_cast_types(
11726            &DataType::Decimal128(10, 4),
11727            &DataType::Utf8View
11728        ));
11729        assert!(can_cast_types(
11730            &DataType::Decimal256(38, 10),
11731            &DataType::Utf8View
11732        ));
11733
11734        macro_rules! assert_decimal_values {
11735            ($array:expr) => {
11736                let c = $array;
11737                assert_eq!("1123.454", c.value(0));
11738                assert_eq!("2123.456", c.value(1));
11739                assert_eq!("-3123.453", c.value(2));
11740                assert_eq!("-3123.456", c.value(3));
11741                assert_eq!("0.000", c.value(4));
11742                assert_eq!("0.123", c.value(5));
11743                assert_eq!("1234.567", c.value(6));
11744                assert_eq!("-1234.567", c.value(7));
11745                assert!(c.is_null(8));
11746            };
11747        }
11748
11749        fn test_decimal_to_string<IN: ArrowPrimitiveType, OffsetSize: OffsetSizeTrait>(
11750            output_type: DataType,
11751            array: PrimitiveArray<IN>,
11752        ) {
11753            let b = cast(&array, &output_type).unwrap();
11754
11755            assert_eq!(b.data_type(), &output_type);
11756            match b.data_type() {
11757                DataType::Utf8View => {
11758                    let c = b.as_string_view();
11759                    assert_decimal_values!(c);
11760                }
11761                DataType::Utf8 | DataType::LargeUtf8 => {
11762                    let c = b.as_string::<OffsetSize>();
11763                    assert_decimal_values!(c);
11764                }
11765                _ => (),
11766            }
11767        }
11768
11769        let array32: Vec<Option<i32>> = vec![
11770            Some(1123454),
11771            Some(2123456),
11772            Some(-3123453),
11773            Some(-3123456),
11774            Some(0),
11775            Some(123),
11776            Some(123456789),
11777            Some(-123456789),
11778            None,
11779        ];
11780        let array64: Vec<Option<i64>> = array32.iter().map(|num| num.map(|x| x as i64)).collect();
11781        let array128: Vec<Option<i128>> =
11782            array64.iter().map(|num| num.map(|x| x as i128)).collect();
11783        let array256: Vec<Option<i256>> = array128
11784            .iter()
11785            .map(|num| num.map(i256::from_i128))
11786            .collect();
11787
11788        test_decimal_to_string::<Decimal32Type, i32>(
11789            DataType::Utf8View,
11790            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
11791        );
11792        test_decimal_to_string::<Decimal32Type, i32>(
11793            DataType::Utf8,
11794            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
11795        );
11796        test_decimal_to_string::<Decimal32Type, i64>(
11797            DataType::LargeUtf8,
11798            create_decimal32_array(array32, 7, 3).unwrap(),
11799        );
11800
11801        test_decimal_to_string::<Decimal64Type, i32>(
11802            DataType::Utf8View,
11803            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
11804        );
11805        test_decimal_to_string::<Decimal64Type, i32>(
11806            DataType::Utf8,
11807            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
11808        );
11809        test_decimal_to_string::<Decimal64Type, i64>(
11810            DataType::LargeUtf8,
11811            create_decimal64_array(array64, 7, 3).unwrap(),
11812        );
11813
11814        test_decimal_to_string::<Decimal128Type, i32>(
11815            DataType::Utf8View,
11816            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
11817        );
11818        test_decimal_to_string::<Decimal128Type, i32>(
11819            DataType::Utf8,
11820            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
11821        );
11822        test_decimal_to_string::<Decimal128Type, i64>(
11823            DataType::LargeUtf8,
11824            create_decimal128_array(array128, 7, 3).unwrap(),
11825        );
11826
11827        test_decimal_to_string::<Decimal256Type, i32>(
11828            DataType::Utf8View,
11829            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
11830        );
11831        test_decimal_to_string::<Decimal256Type, i32>(
11832            DataType::Utf8,
11833            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
11834        );
11835        test_decimal_to_string::<Decimal256Type, i64>(
11836            DataType::LargeUtf8,
11837            create_decimal256_array(array256, 7, 3).unwrap(),
11838        );
11839    }
11840
11841    #[test]
11842    fn test_cast_numeric_to_decimal128_precision_overflow() {
11843        let array = Int64Array::from(vec![1234567]);
11844        let array = Arc::new(array) as ArrayRef;
11845        let casted_array = cast_with_options(
11846            &array,
11847            &DataType::Decimal128(7, 3),
11848            &CastOptions {
11849                safe: true,
11850                format_options: FormatOptions::default(),
11851            },
11852        );
11853        assert!(casted_array.is_ok());
11854        assert!(casted_array.unwrap().is_null(0));
11855
11856        let err = cast_with_options(
11857            &array,
11858            &DataType::Decimal128(7, 3),
11859            &CastOptions {
11860                safe: false,
11861                format_options: FormatOptions::default(),
11862            },
11863        );
11864        assert_eq!(
11865            "Invalid argument error: 1234567.000 is too large to store in a Decimal128 of precision 7. Max is 9999.999",
11866            err.unwrap_err().to_string()
11867        );
11868    }
11869
11870    #[test]
11871    fn test_cast_numeric_to_decimal256_precision_overflow() {
11872        let array = Int64Array::from(vec![1234567]);
11873        let array = Arc::new(array) as ArrayRef;
11874        let casted_array = cast_with_options(
11875            &array,
11876            &DataType::Decimal256(7, 3),
11877            &CastOptions {
11878                safe: true,
11879                format_options: FormatOptions::default(),
11880            },
11881        );
11882        assert!(casted_array.is_ok());
11883        assert!(casted_array.unwrap().is_null(0));
11884
11885        let err = cast_with_options(
11886            &array,
11887            &DataType::Decimal256(7, 3),
11888            &CastOptions {
11889                safe: false,
11890                format_options: FormatOptions::default(),
11891            },
11892        );
11893        assert_eq!(
11894            "Invalid argument error: 1234567.000 is too large to store in a Decimal256 of precision 7. Max is 9999.999",
11895            err.unwrap_err().to_string()
11896        );
11897    }
11898
11899    /// helper function to test casting from duration to interval
11900    fn cast_from_duration_to_interval<T: ArrowTemporalType<Native = i64>>(
11901        array: Vec<i64>,
11902        cast_options: &CastOptions,
11903    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
11904        let array = PrimitiveArray::<T>::new(array.into(), None);
11905        let array = Arc::new(array) as ArrayRef;
11906        let interval = DataType::Interval(IntervalUnit::MonthDayNano);
11907        let out = cast_with_options(&array, &interval, cast_options)?;
11908        let out = out.as_primitive::<IntervalMonthDayNanoType>().clone();
11909        Ok(out)
11910    }
11911
11912    #[test]
11913    fn test_cast_from_duration_to_interval() {
11914        // from duration second to interval month day nano
11915        let array = vec![1234567];
11916        let casted_array =
11917            cast_from_duration_to_interval::<DurationSecondType>(array, &CastOptions::default())
11918                .unwrap();
11919        assert_eq!(
11920            casted_array.data_type(),
11921            &DataType::Interval(IntervalUnit::MonthDayNano)
11922        );
11923        assert_eq!(
11924            casted_array.value(0),
11925            IntervalMonthDayNano::new(0, 0, 1234567000000000)
11926        );
11927
11928        let array = vec![i64::MAX];
11929        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
11930            array.clone(),
11931            &CastOptions::default(),
11932        )
11933        .unwrap();
11934        assert!(!casted_array.is_valid(0));
11935
11936        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
11937            array,
11938            &CastOptions {
11939                safe: false,
11940                format_options: FormatOptions::default(),
11941            },
11942        );
11943        assert!(casted_array.is_err());
11944
11945        // from duration millisecond to interval month day nano
11946        let array = vec![1234567];
11947        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11948            array,
11949            &CastOptions::default(),
11950        )
11951        .unwrap();
11952        assert_eq!(
11953            casted_array.data_type(),
11954            &DataType::Interval(IntervalUnit::MonthDayNano)
11955        );
11956        assert_eq!(
11957            casted_array.value(0),
11958            IntervalMonthDayNano::new(0, 0, 1234567000000)
11959        );
11960
11961        let array = vec![i64::MAX];
11962        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11963            array.clone(),
11964            &CastOptions::default(),
11965        )
11966        .unwrap();
11967        assert!(!casted_array.is_valid(0));
11968
11969        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11970            array,
11971            &CastOptions {
11972                safe: false,
11973                format_options: FormatOptions::default(),
11974            },
11975        );
11976        assert!(casted_array.is_err());
11977
11978        // from duration microsecond to interval month day nano
11979        let array = vec![1234567];
11980        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11981            array,
11982            &CastOptions::default(),
11983        )
11984        .unwrap();
11985        assert_eq!(
11986            casted_array.data_type(),
11987            &DataType::Interval(IntervalUnit::MonthDayNano)
11988        );
11989        assert_eq!(
11990            casted_array.value(0),
11991            IntervalMonthDayNano::new(0, 0, 1234567000)
11992        );
11993
11994        let array = vec![i64::MAX];
11995        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11996            array.clone(),
11997            &CastOptions::default(),
11998        )
11999        .unwrap();
12000        assert!(!casted_array.is_valid(0));
12001
12002        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
12003            array,
12004            &CastOptions {
12005                safe: false,
12006                format_options: FormatOptions::default(),
12007            },
12008        );
12009        assert!(casted_array.is_err());
12010
12011        // from duration nanosecond to interval month day nano
12012        let array = vec![1234567];
12013        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
12014            array,
12015            &CastOptions::default(),
12016        )
12017        .unwrap();
12018        assert_eq!(
12019            casted_array.data_type(),
12020            &DataType::Interval(IntervalUnit::MonthDayNano)
12021        );
12022        assert_eq!(
12023            casted_array.value(0),
12024            IntervalMonthDayNano::new(0, 0, 1234567)
12025        );
12026
12027        let array = vec![i64::MAX];
12028        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
12029            array,
12030            &CastOptions {
12031                safe: false,
12032                format_options: FormatOptions::default(),
12033            },
12034        )
12035        .unwrap();
12036        assert_eq!(
12037            casted_array.value(0),
12038            IntervalMonthDayNano::new(0, 0, i64::MAX)
12039        );
12040    }
12041
12042    /// helper function to test casting from interval to duration
12043    fn cast_from_interval_to_duration<T: ArrowTemporalType>(
12044        array: &IntervalMonthDayNanoArray,
12045        cast_options: &CastOptions,
12046    ) -> Result<PrimitiveArray<T>, ArrowError> {
12047        let casted_array = cast_with_options(&array, &T::DATA_TYPE, cast_options)?;
12048        casted_array
12049            .as_any()
12050            .downcast_ref::<PrimitiveArray<T>>()
12051            .ok_or_else(|| {
12052                ArrowError::ComputeError(format!("Failed to downcast to {}", T::DATA_TYPE))
12053            })
12054            .cloned()
12055    }
12056
12057    #[test]
12058    fn test_cast_from_interval_to_duration() {
12059        let nullable = CastOptions::default();
12060        let fallible = CastOptions {
12061            safe: false,
12062            format_options: FormatOptions::default(),
12063        };
12064        let v = IntervalMonthDayNano::new(0, 0, 1234567);
12065
12066        // from interval month day nano to duration second
12067        let array = vec![v].into();
12068        let casted_array: DurationSecondArray =
12069            cast_from_interval_to_duration(&array, &nullable).unwrap();
12070        assert_eq!(casted_array.value(0), 0);
12071
12072        let array = vec![IntervalMonthDayNano::MAX].into();
12073        let casted_array: DurationSecondArray =
12074            cast_from_interval_to_duration(&array, &nullable).unwrap();
12075        assert!(!casted_array.is_valid(0));
12076
12077        let res = cast_from_interval_to_duration::<DurationSecondType>(&array, &fallible);
12078        assert!(res.is_err());
12079
12080        // from interval month day nano to duration millisecond
12081        let array = vec![v].into();
12082        let casted_array: DurationMillisecondArray =
12083            cast_from_interval_to_duration(&array, &nullable).unwrap();
12084        assert_eq!(casted_array.value(0), 1);
12085
12086        let array = vec![IntervalMonthDayNano::MAX].into();
12087        let casted_array: DurationMillisecondArray =
12088            cast_from_interval_to_duration(&array, &nullable).unwrap();
12089        assert!(!casted_array.is_valid(0));
12090
12091        let res = cast_from_interval_to_duration::<DurationMillisecondType>(&array, &fallible);
12092        assert!(res.is_err());
12093
12094        // from interval month day nano to duration microsecond
12095        let array = vec![v].into();
12096        let casted_array: DurationMicrosecondArray =
12097            cast_from_interval_to_duration(&array, &nullable).unwrap();
12098        assert_eq!(casted_array.value(0), 1234);
12099
12100        let array = vec![IntervalMonthDayNano::MAX].into();
12101        let casted_array =
12102            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &nullable).unwrap();
12103        assert!(!casted_array.is_valid(0));
12104
12105        let casted_array =
12106            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &fallible);
12107        assert!(casted_array.is_err());
12108
12109        // from interval month day nano to duration nanosecond
12110        let array = vec![v].into();
12111        let casted_array: DurationNanosecondArray =
12112            cast_from_interval_to_duration(&array, &nullable).unwrap();
12113        assert_eq!(casted_array.value(0), 1234567);
12114
12115        let array = vec![IntervalMonthDayNano::MAX].into();
12116        let casted_array: DurationNanosecondArray =
12117            cast_from_interval_to_duration(&array, &nullable).unwrap();
12118        assert!(!casted_array.is_valid(0));
12119
12120        let casted_array =
12121            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &fallible);
12122        assert!(casted_array.is_err());
12123
12124        let array = vec![
12125            IntervalMonthDayNanoType::make_value(0, 1, 0),
12126            IntervalMonthDayNanoType::make_value(-1, 0, 0),
12127            IntervalMonthDayNanoType::make_value(1, 1, 0),
12128            IntervalMonthDayNanoType::make_value(1, 0, 1),
12129            IntervalMonthDayNanoType::make_value(0, 0, -1),
12130        ]
12131        .into();
12132        let casted_array =
12133            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable).unwrap();
12134        assert!(!casted_array.is_valid(0));
12135        assert!(!casted_array.is_valid(1));
12136        assert!(!casted_array.is_valid(2));
12137        assert!(!casted_array.is_valid(3));
12138        assert!(casted_array.is_valid(4));
12139        assert_eq!(casted_array.value(4), -1);
12140    }
12141
12142    /// helper function to test casting from interval year month to interval month day nano
12143    fn cast_from_interval_year_month_to_interval_month_day_nano(
12144        array: Vec<i32>,
12145        cast_options: &CastOptions,
12146    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
12147        let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
12148        let array = Arc::new(array) as ArrayRef;
12149        let casted_array = cast_with_options(
12150            &array,
12151            &DataType::Interval(IntervalUnit::MonthDayNano),
12152            cast_options,
12153        )?;
12154        casted_array
12155            .as_any()
12156            .downcast_ref::<IntervalMonthDayNanoArray>()
12157            .ok_or_else(|| {
12158                ArrowError::ComputeError(
12159                    "Failed to downcast to IntervalMonthDayNanoArray".to_string(),
12160                )
12161            })
12162            .cloned()
12163    }
12164
12165    #[test]
12166    fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
12167        // from interval year month to interval month day nano
12168        let array = vec![1234567];
12169        let casted_array = cast_from_interval_year_month_to_interval_month_day_nano(
12170            array,
12171            &CastOptions::default(),
12172        )
12173        .unwrap();
12174        assert_eq!(
12175            casted_array.data_type(),
12176            &DataType::Interval(IntervalUnit::MonthDayNano)
12177        );
12178        assert_eq!(
12179            casted_array.value(0),
12180            IntervalMonthDayNano::new(1234567, 0, 0)
12181        );
12182    }
12183
12184    /// helper function to test casting from interval day time to interval month day nano
12185    fn cast_from_interval_day_time_to_interval_month_day_nano(
12186        array: Vec<IntervalDayTime>,
12187        cast_options: &CastOptions,
12188    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
12189        let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
12190        let array = Arc::new(array) as ArrayRef;
12191        let casted_array = cast_with_options(
12192            &array,
12193            &DataType::Interval(IntervalUnit::MonthDayNano),
12194            cast_options,
12195        )?;
12196        Ok(casted_array
12197            .as_primitive::<IntervalMonthDayNanoType>()
12198            .clone())
12199    }
12200
12201    #[test]
12202    fn test_cast_from_interval_day_time_to_interval_month_day_nano() {
12203        // from interval day time to interval month day nano
12204        let array = vec![IntervalDayTime::new(123, 0)];
12205        let casted_array =
12206            cast_from_interval_day_time_to_interval_month_day_nano(array, &CastOptions::default())
12207                .unwrap();
12208        assert_eq!(
12209            casted_array.data_type(),
12210            &DataType::Interval(IntervalUnit::MonthDayNano)
12211        );
12212        assert_eq!(casted_array.value(0), IntervalMonthDayNano::new(0, 123, 0));
12213    }
12214
12215    #[test]
12216    fn test_cast_below_unixtimestamp() {
12217        let valid = StringArray::from(vec![
12218            "1900-01-03 23:59:59",
12219            "1969-12-31 00:00:01",
12220            "1989-12-31 00:00:01",
12221        ]);
12222
12223        let array = Arc::new(valid) as ArrayRef;
12224        let casted_array = cast_with_options(
12225            &array,
12226            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
12227            &CastOptions {
12228                safe: false,
12229                format_options: FormatOptions::default(),
12230            },
12231        )
12232        .unwrap();
12233
12234        let ts_array = casted_array
12235            .as_primitive::<TimestampNanosecondType>()
12236            .values()
12237            .iter()
12238            .map(|ts| ts / 1_000_000)
12239            .collect::<Vec<_>>();
12240
12241        let array = TimestampMillisecondArray::from(ts_array).with_timezone("+00:00".to_string());
12242        let casted_array = cast(&array, &DataType::Date32).unwrap();
12243        let date_array = casted_array.as_primitive::<Date32Type>();
12244        let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
12245        let string_array = casted_array.as_string::<i32>();
12246        assert_eq!("1900-01-03", string_array.value(0));
12247        assert_eq!("1969-12-31", string_array.value(1));
12248        assert_eq!("1989-12-31", string_array.value(2));
12249    }
12250
12251    #[test]
12252    fn test_nested_list() {
12253        let mut list = ListBuilder::new(Int32Builder::new());
12254        list.append_value([Some(1), Some(2), Some(3)]);
12255        list.append_value([Some(4), None, Some(6)]);
12256        let list = list.finish();
12257
12258        let to_field = Field::new("nested", list.data_type().clone(), false);
12259        let to = DataType::List(Arc::new(to_field));
12260        let out = cast(&list, &to).unwrap();
12261        let opts = FormatOptions::default().with_null("null");
12262        let formatted = ArrayFormatter::try_new(out.as_ref(), &opts).unwrap();
12263
12264        assert_eq!(formatted.value(0).to_string(), "[[1], [2], [3]]");
12265        assert_eq!(formatted.value(1).to_string(), "[[4], [null], [6]]");
12266    }
12267
12268    #[test]
12269    fn test_nested_list_cast() {
12270        let mut builder = ListBuilder::new(ListBuilder::new(Int32Builder::new()));
12271        builder.append_value([Some([Some(1), Some(2), None]), None]);
12272        builder.append_value([None, Some([]), None]);
12273        builder.append_null();
12274        builder.append_value([Some([Some(2), Some(3)])]);
12275        let start = builder.finish();
12276
12277        let mut builder = LargeListBuilder::new(LargeListBuilder::new(Int8Builder::new()));
12278        builder.append_value([Some([Some(1), Some(2), None]), None]);
12279        builder.append_value([None, Some([]), None]);
12280        builder.append_null();
12281        builder.append_value([Some([Some(2), Some(3)])]);
12282        let expected = builder.finish();
12283
12284        let actual = cast(&start, expected.data_type()).unwrap();
12285        assert_eq!(actual.as_ref(), &expected);
12286    }
12287
12288    const CAST_OPTIONS: CastOptions<'static> = CastOptions {
12289        safe: true,
12290        format_options: FormatOptions::new(),
12291    };
12292
12293    #[test]
12294    #[allow(clippy::assertions_on_constants)]
12295    fn test_const_options() {
12296        assert!(CAST_OPTIONS.safe)
12297    }
12298
12299    #[test]
12300    fn test_list_format_options() {
12301        let options = CastOptions {
12302            safe: false,
12303            format_options: FormatOptions::default().with_null("null"),
12304        };
12305        let array = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
12306            Some(vec![Some(0), Some(1), Some(2)]),
12307            Some(vec![Some(0), None, Some(2)]),
12308        ]);
12309        let a = cast_with_options(&array, &DataType::Utf8, &options).unwrap();
12310        let r: Vec<_> = a.as_string::<i32>().iter().flatten().collect();
12311        assert_eq!(r, &["[0, 1, 2]", "[0, null, 2]"]);
12312    }
12313    #[test]
12314    fn test_cast_string_to_timestamp_invalid_tz() {
12315        // content after Z should be ignored
12316        let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
12317        let array = StringArray::from(vec![Some(bad_timestamp)]);
12318
12319        let data_types = [
12320            DataType::Timestamp(TimeUnit::Second, None),
12321            DataType::Timestamp(TimeUnit::Millisecond, None),
12322            DataType::Timestamp(TimeUnit::Microsecond, None),
12323            DataType::Timestamp(TimeUnit::Nanosecond, None),
12324        ];
12325
12326        let cast_options = CastOptions {
12327            safe: false,
12328            ..Default::default()
12329        };
12330
12331        for dt in data_types {
12332            assert_eq!(
12333                cast_with_options(&array, &dt, &cast_options)
12334                    .unwrap_err()
12335                    .to_string(),
12336                "Parser error: Invalid timezone \"ZZTOP\": only offset based timezones supported without chrono-tz feature"
12337            );
12338        }
12339    }
12340    #[test]
12341    fn test_cast_struct_to_struct() {
12342        let struct_type = DataType::Struct(
12343            vec![
12344                Field::new("a", DataType::Boolean, false),
12345                Field::new("b", DataType::Int32, false),
12346            ]
12347            .into(),
12348        );
12349        let to_type = DataType::Struct(
12350            vec![
12351                Field::new("a", DataType::Utf8, false),
12352                Field::new("b", DataType::Utf8, false),
12353            ]
12354            .into(),
12355        );
12356        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12357        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
12358        let struct_array = StructArray::from(vec![
12359            (
12360                Arc::new(Field::new("b", DataType::Boolean, false)),
12361                boolean.clone() as ArrayRef,
12362            ),
12363            (
12364                Arc::new(Field::new("c", DataType::Int32, false)),
12365                int.clone() as ArrayRef,
12366            ),
12367        ]);
12368        let casted_array = cast(&struct_array, &to_type).unwrap();
12369        let casted_array = casted_array.as_struct();
12370        assert_eq!(casted_array.data_type(), &to_type);
12371        let casted_boolean_array = casted_array
12372            .column(0)
12373            .as_string::<i32>()
12374            .into_iter()
12375            .flatten()
12376            .collect::<Vec<_>>();
12377        let casted_int_array = casted_array
12378            .column(1)
12379            .as_string::<i32>()
12380            .into_iter()
12381            .flatten()
12382            .collect::<Vec<_>>();
12383        assert_eq!(casted_boolean_array, vec!["false", "false", "true", "true"]);
12384        assert_eq!(casted_int_array, vec!["42", "28", "19", "31"]);
12385
12386        // test for can't cast
12387        let to_type = DataType::Struct(
12388            vec![
12389                Field::new("a", DataType::Date32, false),
12390                Field::new("b", DataType::Utf8, false),
12391            ]
12392            .into(),
12393        );
12394        assert!(!can_cast_types(&struct_type, &to_type));
12395        let result = cast(&struct_array, &to_type);
12396        assert_eq!(
12397            "Cast error: Casting from Boolean to Date32 not supported",
12398            result.unwrap_err().to_string()
12399        );
12400    }
12401
12402    #[test]
12403    fn test_cast_struct_to_struct_nullability() {
12404        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12405        let int = Arc::new(Int32Array::from(vec![Some(42), None, Some(19), None]));
12406        let struct_array = StructArray::from(vec![
12407            (
12408                Arc::new(Field::new("b", DataType::Boolean, false)),
12409                boolean.clone() as ArrayRef,
12410            ),
12411            (
12412                Arc::new(Field::new("c", DataType::Int32, true)),
12413                int.clone() as ArrayRef,
12414            ),
12415        ]);
12416
12417        // okay: nullable to nullable
12418        let to_type = DataType::Struct(
12419            vec![
12420                Field::new("a", DataType::Utf8, false),
12421                Field::new("b", DataType::Utf8, true),
12422            ]
12423            .into(),
12424        );
12425        cast(&struct_array, &to_type).expect("Cast nullable to nullable struct field should work");
12426
12427        // error: nullable to non-nullable
12428        let to_type = DataType::Struct(
12429            vec![
12430                Field::new("a", DataType::Utf8, false),
12431                Field::new("b", DataType::Utf8, false),
12432            ]
12433            .into(),
12434        );
12435        cast(&struct_array, &to_type)
12436            .expect_err("Cast nullable to non-nullable struct field should fail");
12437
12438        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12439        let int = Arc::new(Int32Array::from(vec![i32::MAX, 25, 1, 100]));
12440        let struct_array = StructArray::from(vec![
12441            (
12442                Arc::new(Field::new("b", DataType::Boolean, false)),
12443                boolean.clone() as ArrayRef,
12444            ),
12445            (
12446                Arc::new(Field::new("c", DataType::Int32, false)),
12447                int.clone() as ArrayRef,
12448            ),
12449        ]);
12450
12451        // okay: non-nullable to non-nullable
12452        let to_type = DataType::Struct(
12453            vec![
12454                Field::new("a", DataType::Utf8, false),
12455                Field::new("b", DataType::Utf8, false),
12456            ]
12457            .into(),
12458        );
12459        cast(&struct_array, &to_type)
12460            .expect("Cast non-nullable to non-nullable struct field should work");
12461
12462        // err: non-nullable to non-nullable but overflowing return null during casting
12463        let to_type = DataType::Struct(
12464            vec![
12465                Field::new("a", DataType::Utf8, false),
12466                Field::new("b", DataType::Int8, false),
12467            ]
12468            .into(),
12469        );
12470        cast(&struct_array, &to_type).expect_err(
12471            "Cast non-nullable to non-nullable struct field returning null should fail",
12472        );
12473    }
12474
12475    #[test]
12476    fn test_cast_struct_to_non_struct() {
12477        let boolean = Arc::new(BooleanArray::from(vec![true, false]));
12478        let struct_array = StructArray::from(vec![(
12479            Arc::new(Field::new("a", DataType::Boolean, false)),
12480            boolean.clone() as ArrayRef,
12481        )]);
12482        let to_type = DataType::Utf8;
12483        let result = cast(&struct_array, &to_type);
12484        assert_eq!(
12485            r#"Cast error: Casting from Struct("a": non-null Boolean) to Utf8 not supported"#,
12486            result.unwrap_err().to_string()
12487        );
12488    }
12489
12490    #[test]
12491    fn test_cast_non_struct_to_struct() {
12492        let array = StringArray::from(vec!["a", "b"]);
12493        let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
12494        let result = cast(&array, &to_type);
12495        assert_eq!(
12496            r#"Cast error: Casting from Utf8 to Struct("a": non-null Boolean) not supported"#,
12497            result.unwrap_err().to_string()
12498        );
12499    }
12500
12501    #[test]
12502    fn test_cast_struct_with_different_field_order() {
12503        // Test slow path: fields are in different order
12504        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12505        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
12506        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
12507
12508        let struct_array = StructArray::from(vec![
12509            (
12510                Arc::new(Field::new("a", DataType::Boolean, false)),
12511                boolean.clone() as ArrayRef,
12512            ),
12513            (
12514                Arc::new(Field::new("b", DataType::Int32, false)),
12515                int.clone() as ArrayRef,
12516            ),
12517            (
12518                Arc::new(Field::new("c", DataType::Utf8, false)),
12519                string.clone() as ArrayRef,
12520            ),
12521        ]);
12522
12523        // Target has fields in different order: c, a, b instead of a, b, c
12524        let to_type = DataType::Struct(
12525            vec![
12526                Field::new("c", DataType::Utf8, false),
12527                Field::new("a", DataType::Utf8, false), // Boolean to Utf8
12528                Field::new("b", DataType::Utf8, false), // Int32 to Utf8
12529            ]
12530            .into(),
12531        );
12532
12533        let result = cast(&struct_array, &to_type).unwrap();
12534        let result_struct = result.as_struct();
12535
12536        assert_eq!(result_struct.data_type(), &to_type);
12537        assert_eq!(result_struct.num_columns(), 3);
12538
12539        // Verify field "c" (originally position 2, now position 0) remains Utf8
12540        let c_column = result_struct.column(0).as_string::<i32>();
12541        assert_eq!(
12542            c_column.into_iter().flatten().collect::<Vec<_>>(),
12543            vec!["foo", "bar", "baz", "qux"]
12544        );
12545
12546        // Verify field "a" (originally position 0, now position 1) was cast from Boolean to Utf8
12547        let a_column = result_struct.column(1).as_string::<i32>();
12548        assert_eq!(
12549            a_column.into_iter().flatten().collect::<Vec<_>>(),
12550            vec!["false", "false", "true", "true"]
12551        );
12552
12553        // Verify field "b" (originally position 1, now position 2) was cast from Int32 to Utf8
12554        let b_column = result_struct.column(2).as_string::<i32>();
12555        assert_eq!(
12556            b_column.into_iter().flatten().collect::<Vec<_>>(),
12557            vec!["42", "28", "19", "31"]
12558        );
12559    }
12560
12561    #[test]
12562    fn test_cast_struct_with_missing_field() {
12563        // Test that casting fails when target has a field not present in source
12564        let boolean = Arc::new(BooleanArray::from(vec![false, true]));
12565        let struct_array = StructArray::from(vec![(
12566            Arc::new(Field::new("a", DataType::Boolean, false)),
12567            boolean.clone() as ArrayRef,
12568        )]);
12569
12570        let to_type = DataType::Struct(
12571            vec![
12572                Field::new("a", DataType::Utf8, false),
12573                Field::new("b", DataType::Int32, false), // Field "b" doesn't exist in source
12574            ]
12575            .into(),
12576        );
12577
12578        let result = cast(&struct_array, &to_type);
12579        assert!(result.is_err());
12580        assert_eq!(
12581            result.unwrap_err().to_string(),
12582            "Invalid argument error: Incorrect number of arrays for StructArray fields, expected 2 got 1"
12583        );
12584    }
12585
12586    #[test]
12587    fn test_cast_struct_with_subset_of_fields() {
12588        // Test casting to a struct with fewer fields (selecting a subset)
12589        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12590        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
12591        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
12592
12593        let struct_array = StructArray::from(vec![
12594            (
12595                Arc::new(Field::new("a", DataType::Boolean, false)),
12596                boolean.clone() as ArrayRef,
12597            ),
12598            (
12599                Arc::new(Field::new("b", DataType::Int32, false)),
12600                int.clone() as ArrayRef,
12601            ),
12602            (
12603                Arc::new(Field::new("c", DataType::Utf8, false)),
12604                string.clone() as ArrayRef,
12605            ),
12606        ]);
12607
12608        // Target has only fields "c" and "a", omitting "b"
12609        let to_type = DataType::Struct(
12610            vec![
12611                Field::new("c", DataType::Utf8, false),
12612                Field::new("a", DataType::Utf8, false),
12613            ]
12614            .into(),
12615        );
12616
12617        let result = cast(&struct_array, &to_type).unwrap();
12618        let result_struct = result.as_struct();
12619
12620        assert_eq!(result_struct.data_type(), &to_type);
12621        assert_eq!(result_struct.num_columns(), 2);
12622
12623        // Verify field "c" remains Utf8
12624        let c_column = result_struct.column(0).as_string::<i32>();
12625        assert_eq!(
12626            c_column.into_iter().flatten().collect::<Vec<_>>(),
12627            vec!["foo", "bar", "baz", "qux"]
12628        );
12629
12630        // Verify field "a" was cast from Boolean to Utf8
12631        let a_column = result_struct.column(1).as_string::<i32>();
12632        assert_eq!(
12633            a_column.into_iter().flatten().collect::<Vec<_>>(),
12634            vec!["false", "false", "true", "true"]
12635        );
12636    }
12637
12638    #[test]
12639    fn test_can_cast_struct_rename_field() {
12640        // Test that can_cast_types returns false when target has a field not in source
12641        let from_type = DataType::Struct(
12642            vec![
12643                Field::new("a", DataType::Int32, false),
12644                Field::new("b", DataType::Utf8, false),
12645            ]
12646            .into(),
12647        );
12648
12649        let to_type = DataType::Struct(
12650            vec![
12651                Field::new("a", DataType::Int64, false),
12652                Field::new("c", DataType::Boolean, false), // Field "c" not in source
12653            ]
12654            .into(),
12655        );
12656
12657        assert!(can_cast_types(&from_type, &to_type));
12658    }
12659
12660    fn run_decimal_cast_test_case_between_multiple_types(t: DecimalCastTestConfig) {
12661        run_decimal_cast_test_case::<Decimal128Type, Decimal128Type>(t.clone());
12662        run_decimal_cast_test_case::<Decimal128Type, Decimal256Type>(t.clone());
12663        run_decimal_cast_test_case::<Decimal256Type, Decimal128Type>(t.clone());
12664        run_decimal_cast_test_case::<Decimal256Type, Decimal256Type>(t.clone());
12665    }
12666
12667    #[test]
12668    fn test_decimal_to_decimal_coverage() {
12669        let test_cases = [
12670            // increase precision, increase scale, infallible
12671            DecimalCastTestConfig {
12672                input_prec: 5,
12673                input_scale: 1,
12674                input_repr: 99999, // 9999.9
12675                output_prec: 10,
12676                output_scale: 6,
12677                expected_output_repr: Ok(9999900000), // 9999.900000
12678            },
12679            // increase precision, increase scale, fallible, safe
12680            DecimalCastTestConfig {
12681                input_prec: 5,
12682                input_scale: 1,
12683                input_repr: 99, // 9999.9
12684                output_prec: 7,
12685                output_scale: 6,
12686                expected_output_repr: Ok(9900000), // 9.900000
12687            },
12688            // increase precision, increase scale, fallible, unsafe
12689            DecimalCastTestConfig {
12690                input_prec: 5,
12691                input_scale: 1,
12692                input_repr: 99999, // 9999.9
12693                output_prec: 7,
12694                output_scale: 6,
12695                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
12696            },
12697            // increase precision, decrease scale, always infallible
12698            DecimalCastTestConfig {
12699                input_prec: 5,
12700                input_scale: 3,
12701                input_repr: 99999, // 99.999
12702                output_prec: 10,
12703                output_scale: 2,
12704                expected_output_repr: Ok(10000), // 100.00
12705            },
12706            // increase precision, decrease scale, no rouding
12707            DecimalCastTestConfig {
12708                input_prec: 5,
12709                input_scale: 3,
12710                input_repr: 99994, // 99.994
12711                output_prec: 10,
12712                output_scale: 2,
12713                expected_output_repr: Ok(9999), // 99.99
12714            },
12715            // increase precision, don't change scale, always infallible
12716            DecimalCastTestConfig {
12717                input_prec: 5,
12718                input_scale: 3,
12719                input_repr: 99999, // 99.999
12720                output_prec: 10,
12721                output_scale: 3,
12722                expected_output_repr: Ok(99999), // 99.999
12723            },
12724            // decrease precision, increase scale, safe
12725            DecimalCastTestConfig {
12726                input_prec: 10,
12727                input_scale: 5,
12728                input_repr: 999999, // 9.99999
12729                output_prec: 8,
12730                output_scale: 7,
12731                expected_output_repr: Ok(99999900), // 9.9999900
12732            },
12733            // decrease precision, increase scale, unsafe
12734            DecimalCastTestConfig {
12735                input_prec: 10,
12736                input_scale: 5,
12737                input_repr: 9999999, // 99.99999
12738                output_prec: 8,
12739                output_scale: 7,
12740                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
12741            },
12742            // decrease precision, decrease scale, safe, infallible
12743            DecimalCastTestConfig {
12744                input_prec: 7,
12745                input_scale: 4,
12746                input_repr: 9999999, // 999.9999
12747                output_prec: 6,
12748                output_scale: 2,
12749                expected_output_repr: Ok(100000),
12750            },
12751            // decrease precision, decrease scale, safe, fallible
12752            DecimalCastTestConfig {
12753                input_prec: 10,
12754                input_scale: 5,
12755                input_repr: 12345678, // 123.45678
12756                output_prec: 8,
12757                output_scale: 3,
12758                expected_output_repr: Ok(123457), // 123.457
12759            },
12760            // decrease precision, decrease scale, unsafe
12761            DecimalCastTestConfig {
12762                input_prec: 10,
12763                input_scale: 5,
12764                input_repr: 9999999, // 99.99999
12765                output_prec: 4,
12766                output_scale: 3,
12767                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
12768            },
12769            // decrease precision, same scale, safe
12770            DecimalCastTestConfig {
12771                input_prec: 10,
12772                input_scale: 5,
12773                input_repr: 999999, // 9.99999
12774                output_prec: 6,
12775                output_scale: 5,
12776                expected_output_repr: Ok(999999), // 9.99999
12777            },
12778            // decrease precision, same scale, unsafe
12779            DecimalCastTestConfig {
12780                input_prec: 10,
12781                input_scale: 5,
12782                input_repr: 9999999, // 99.99999
12783                output_prec: 6,
12784                output_scale: 5,
12785                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
12786            },
12787            // same precision, increase scale, safe
12788            DecimalCastTestConfig {
12789                input_prec: 7,
12790                input_scale: 4,
12791                input_repr: 12345, // 1.2345
12792                output_prec: 7,
12793                output_scale: 6,
12794                expected_output_repr: Ok(1234500), // 1.234500
12795            },
12796            // same precision, increase scale, unsafe
12797            DecimalCastTestConfig {
12798                input_prec: 7,
12799                input_scale: 4,
12800                input_repr: 123456, // 12.3456
12801                output_prec: 7,
12802                output_scale: 6,
12803                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
12804            },
12805            // same precision, decrease scale, infallible
12806            DecimalCastTestConfig {
12807                input_prec: 7,
12808                input_scale: 5,
12809                input_repr: 1234567, // 12.34567
12810                output_prec: 7,
12811                output_scale: 4,
12812                expected_output_repr: Ok(123457), // 12.3457
12813            },
12814            // same precision, same scale, infallible
12815            DecimalCastTestConfig {
12816                input_prec: 7,
12817                input_scale: 5,
12818                input_repr: 9999999, // 99.99999
12819                output_prec: 7,
12820                output_scale: 5,
12821                expected_output_repr: Ok(9999999), // 99.99999
12822            },
12823            // precision increase, input scale & output scale = 0, infallible
12824            DecimalCastTestConfig {
12825                input_prec: 7,
12826                input_scale: 0,
12827                input_repr: 1234567, // 1234567
12828                output_prec: 8,
12829                output_scale: 0,
12830                expected_output_repr: Ok(1234567), // 1234567
12831            },
12832            // precision decrease, input scale & output scale = 0, failure
12833            DecimalCastTestConfig {
12834                input_prec: 7,
12835                input_scale: 0,
12836                input_repr: 1234567, // 1234567
12837                output_prec: 6,
12838                output_scale: 0,
12839                expected_output_repr: Err("Invalid argument error: 1234567 is too large to store in a {} of precision 6. Max is 999999".to_string())
12840            },
12841            // precision decrease, input scale & output scale = 0, success
12842            DecimalCastTestConfig {
12843                input_prec: 7,
12844                input_scale: 0,
12845                input_repr: 123456, // 123456
12846                output_prec: 6,
12847                output_scale: 0,
12848                expected_output_repr: Ok(123456), // 123456
12849            },
12850        ];
12851
12852        for t in test_cases {
12853            run_decimal_cast_test_case_between_multiple_types(t);
12854        }
12855    }
12856
12857    #[test]
12858    fn test_decimal_to_decimal_increase_scale_and_precision_unchecked() {
12859        let test_cases = [
12860            DecimalCastTestConfig {
12861                input_prec: 5,
12862                input_scale: 0,
12863                input_repr: 99999,
12864                output_prec: 10,
12865                output_scale: 5,
12866                expected_output_repr: Ok(9999900000),
12867            },
12868            DecimalCastTestConfig {
12869                input_prec: 5,
12870                input_scale: 0,
12871                input_repr: -99999,
12872                output_prec: 10,
12873                output_scale: 5,
12874                expected_output_repr: Ok(-9999900000),
12875            },
12876            DecimalCastTestConfig {
12877                input_prec: 5,
12878                input_scale: 2,
12879                input_repr: 99999,
12880                output_prec: 10,
12881                output_scale: 5,
12882                expected_output_repr: Ok(99999000),
12883            },
12884            DecimalCastTestConfig {
12885                input_prec: 5,
12886                input_scale: -2,
12887                input_repr: -99999,
12888                output_prec: 10,
12889                output_scale: 3,
12890                expected_output_repr: Ok(-9999900000),
12891            },
12892            DecimalCastTestConfig {
12893                input_prec: 5,
12894                input_scale: 3,
12895                input_repr: -12345,
12896                output_prec: 6,
12897                output_scale: 5,
12898                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())
12899            },
12900        ];
12901
12902        for t in test_cases {
12903            run_decimal_cast_test_case_between_multiple_types(t);
12904        }
12905    }
12906
12907    #[test]
12908    fn test_decimal_to_decimal_decrease_scale_and_precision_unchecked() {
12909        let test_cases = [
12910            DecimalCastTestConfig {
12911                input_prec: 5,
12912                input_scale: 0,
12913                input_repr: 99999,
12914                output_scale: -3,
12915                output_prec: 3,
12916                expected_output_repr: Ok(100),
12917            },
12918            DecimalCastTestConfig {
12919                input_prec: 5,
12920                input_scale: 0,
12921                input_repr: -99999,
12922                output_prec: 1,
12923                output_scale: -5,
12924                expected_output_repr: Ok(-1),
12925            },
12926            DecimalCastTestConfig {
12927                input_prec: 10,
12928                input_scale: 2,
12929                input_repr: 123456789,
12930                output_prec: 5,
12931                output_scale: -2,
12932                expected_output_repr: Ok(12346),
12933            },
12934            DecimalCastTestConfig {
12935                input_prec: 10,
12936                input_scale: 4,
12937                input_repr: -9876543210,
12938                output_prec: 7,
12939                output_scale: 0,
12940                expected_output_repr: Ok(-987654),
12941            },
12942            DecimalCastTestConfig {
12943                input_prec: 7,
12944                input_scale: 4,
12945                input_repr: 9999999,
12946                output_prec: 6,
12947                output_scale: 3,
12948                expected_output_repr:
12949                    Err("Invalid argument error: 1000.000 is too large to store in a {} of precision 6. Max is 999.999".to_string()),
12950            },
12951        ];
12952        for t in test_cases {
12953            run_decimal_cast_test_case_between_multiple_types(t);
12954        }
12955    }
12956
12957    #[test]
12958    fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
12959        let array = vec![Some(123456789)];
12960        let array = create_decimal128_array(array, 24, 2).unwrap();
12961        let input_type = DataType::Decimal128(24, 2);
12962        let output_type = DataType::Decimal128(6, 2);
12963        assert!(can_cast_types(&input_type, &output_type));
12964
12965        let options = CastOptions {
12966            safe: false,
12967            ..Default::default()
12968        };
12969        let result = cast_with_options(&array, &output_type, &options);
12970        assert_eq!(
12971            result.unwrap_err().to_string(),
12972            "Invalid argument error: 1234567.89 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
12973        );
12974    }
12975
12976    #[test]
12977    fn test_decimal_to_decimal_same_scale() {
12978        let array = vec![Some(520)];
12979        let array = create_decimal128_array(array, 4, 2).unwrap();
12980        let input_type = DataType::Decimal128(4, 2);
12981        let output_type = DataType::Decimal128(3, 2);
12982        assert!(can_cast_types(&input_type, &output_type));
12983
12984        let options = CastOptions {
12985            safe: false,
12986            ..Default::default()
12987        };
12988        let result = cast_with_options(&array, &output_type, &options);
12989        assert_eq!(
12990            result.unwrap().as_primitive::<Decimal128Type>().value(0),
12991            520
12992        );
12993
12994        // Cast 0 of decimal(3, 0) type to decimal(2, 0)
12995        assert_eq!(
12996            &cast(
12997                &create_decimal128_array(vec![Some(0)], 3, 0).unwrap(),
12998                &DataType::Decimal128(2, 0)
12999            )
13000            .unwrap(),
13001            &(Arc::new(create_decimal128_array(vec![Some(0)], 2, 0).unwrap()) as ArrayRef)
13002        );
13003    }
13004
13005    #[test]
13006    fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale() {
13007        let array = vec![Some(123456789)];
13008        let array = create_decimal128_array(array, 24, 4).unwrap();
13009        let input_type = DataType::Decimal128(24, 4);
13010        let output_type = DataType::Decimal128(6, 2);
13011        assert!(can_cast_types(&input_type, &output_type));
13012
13013        let options = CastOptions {
13014            safe: false,
13015            ..Default::default()
13016        };
13017        let result = cast_with_options(&array, &output_type, &options);
13018        assert_eq!(
13019            result.unwrap_err().to_string(),
13020            "Invalid argument error: 12345.68 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
13021        );
13022    }
13023
13024    #[test]
13025    fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale() {
13026        let array = vec![Some(123456789)];
13027        let array = create_decimal128_array(array, 24, 2).unwrap();
13028        let input_type = DataType::Decimal128(24, 2);
13029        let output_type = DataType::Decimal128(6, 3);
13030        assert!(can_cast_types(&input_type, &output_type));
13031
13032        let options = CastOptions {
13033            safe: false,
13034            ..Default::default()
13035        };
13036        let result = cast_with_options(&array, &output_type, &options);
13037        assert_eq!(
13038            result.unwrap_err().to_string(),
13039            "Invalid argument error: 1234567.890 is too large to store in a Decimal128 of precision 6. Max is 999.999"
13040        );
13041    }
13042
13043    #[test]
13044    fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type() {
13045        let array = vec![Some(123456789)];
13046        let array = create_decimal128_array(array, 24, 2).unwrap();
13047        let input_type = DataType::Decimal128(24, 2);
13048        let output_type = DataType::Decimal256(6, 2);
13049        assert!(can_cast_types(&input_type, &output_type));
13050
13051        let options = CastOptions {
13052            safe: false,
13053            ..Default::default()
13054        };
13055        let result = cast_with_options(&array, &output_type, &options).unwrap_err();
13056        assert_eq!(
13057            result.to_string(),
13058            "Invalid argument error: 1234567.89 is too large to store in a Decimal256 of precision 6. Max is 9999.99"
13059        );
13060    }
13061
13062    #[test]
13063    fn test_first_none() {
13064        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
13065            None,
13066            Some(vec![Some(1), Some(2)]),
13067        ])) as ArrayRef;
13068        let data_type =
13069            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
13070        let opt = CastOptions::default();
13071        let r = cast_with_options(&array, &data_type, &opt).unwrap();
13072
13073        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
13074            vec![None, Some(vec![Some(1), Some(2)])],
13075            2,
13076        )) as ArrayRef;
13077        assert_eq!(*fixed_array, *r);
13078    }
13079
13080    #[test]
13081    fn test_first_last_none() {
13082        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
13083            None,
13084            Some(vec![Some(1), Some(2)]),
13085            None,
13086        ])) as ArrayRef;
13087        let data_type =
13088            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
13089        let opt = CastOptions::default();
13090        let r = cast_with_options(&array, &data_type, &opt).unwrap();
13091
13092        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
13093            vec![None, Some(vec![Some(1), Some(2)]), None],
13094            2,
13095        )) as ArrayRef;
13096        assert_eq!(*fixed_array, *r);
13097    }
13098
13099    #[test]
13100    fn test_cast_decimal_error_output() {
13101        let array = Int64Array::from(vec![1]);
13102        let error = cast_with_options(
13103            &array,
13104            &DataType::Decimal32(1, 1),
13105            &CastOptions {
13106                safe: false,
13107                format_options: FormatOptions::default(),
13108            },
13109        )
13110        .unwrap_err();
13111        assert_eq!(
13112            error.to_string(),
13113            "Invalid argument error: 1.0 is too large to store in a Decimal32 of precision 1. Max is 0.9"
13114        );
13115
13116        let array = Int64Array::from(vec![-1]);
13117        let error = cast_with_options(
13118            &array,
13119            &DataType::Decimal32(1, 1),
13120            &CastOptions {
13121                safe: false,
13122                format_options: FormatOptions::default(),
13123            },
13124        )
13125        .unwrap_err();
13126        assert_eq!(
13127            error.to_string(),
13128            "Invalid argument error: -1.0 is too small to store in a Decimal32 of precision 1. Min is -0.9"
13129        );
13130    }
13131
13132    #[test]
13133    fn test_run_end_encoded_to_primitive() {
13134        // Create a RunEndEncoded array: [1, 1, 2, 2, 2, 3]
13135        let run_ends = Int32Array::from(vec![2, 5, 6]);
13136        let values = Int32Array::from(vec![1, 2, 3]);
13137        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13138        let array_ref = Arc::new(run_array) as ArrayRef;
13139        // Cast to Int64
13140        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
13141        // Verify the result is a RunArray with Int64 values
13142        let result_run_array = cast_result.as_any().downcast_ref::<Int64Array>().unwrap();
13143        assert_eq!(
13144            result_run_array.values(),
13145            &[1i64, 1i64, 2i64, 2i64, 2i64, 3i64]
13146        );
13147    }
13148
13149    #[test]
13150    fn test_sliced_run_end_encoded_to_primitive() {
13151        let run_ends = Int32Array::from(vec![2, 5, 6]);
13152        let values = Int32Array::from(vec![1, 2, 3]);
13153        // [1, 1, 2, 2, 2, 3]
13154        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13155        let run_array = run_array.slice(3, 3); // [2, 2, 3]
13156        let array_ref = Arc::new(run_array) as ArrayRef;
13157
13158        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
13159        let result_run_array = cast_result.as_primitive::<Int64Type>();
13160        assert_eq!(result_run_array.values(), &[2, 2, 3]);
13161    }
13162
13163    #[test]
13164    fn test_run_end_encoded_to_string() {
13165        let run_ends = Int32Array::from(vec![2, 3, 5]);
13166        let values = Int32Array::from(vec![10, 20, 30]);
13167        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13168        let array_ref = Arc::new(run_array) as ArrayRef;
13169
13170        // Cast to String
13171        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
13172
13173        // Verify the result is a RunArray with String values
13174        let result_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
13175        // Check that values are correct
13176        assert_eq!(result_array.value(0), "10");
13177        assert_eq!(result_array.value(1), "10");
13178        assert_eq!(result_array.value(2), "20");
13179    }
13180
13181    #[test]
13182    fn test_primitive_to_run_end_encoded() {
13183        // Create an Int32 array with repeated values: [1, 1, 2, 2, 2, 3]
13184        let source_array = Int32Array::from(vec![1, 1, 2, 2, 2, 3]);
13185        let array_ref = Arc::new(source_array) as ArrayRef;
13186
13187        // Cast to RunEndEncoded<Int32, Int32>
13188        let target_type = DataType::RunEndEncoded(
13189            Arc::new(Field::new("run_ends", DataType::Int32, false)),
13190            Arc::new(Field::new("values", DataType::Int32, true)),
13191        );
13192        let cast_result = cast(&array_ref, &target_type).unwrap();
13193
13194        // Verify the result is a RunArray
13195        let result_run_array = cast_result
13196            .as_any()
13197            .downcast_ref::<RunArray<Int32Type>>()
13198            .unwrap();
13199
13200        // Check run structure: runs should end at positions [2, 5, 6]
13201        assert_eq!(result_run_array.run_ends().values(), &[2, 5, 6]);
13202
13203        // Check values: should be [1, 2, 3]
13204        let values_array = result_run_array.values().as_primitive::<Int32Type>();
13205        assert_eq!(values_array.values(), &[1, 2, 3]);
13206    }
13207
13208    #[test]
13209    fn test_primitive_to_run_end_encoded_with_nulls() {
13210        let source_array = Int32Array::from(vec![
13211            Some(1),
13212            Some(1),
13213            None,
13214            None,
13215            Some(2),
13216            Some(2),
13217            Some(3),
13218            Some(3),
13219            None,
13220            None,
13221            Some(4),
13222            Some(4),
13223            Some(5),
13224            Some(5),
13225            None,
13226            None,
13227        ]);
13228        let array_ref = Arc::new(source_array) as ArrayRef;
13229        let target_type = DataType::RunEndEncoded(
13230            Arc::new(Field::new("run_ends", DataType::Int32, false)),
13231            Arc::new(Field::new("values", DataType::Int32, true)),
13232        );
13233        let cast_result = cast(&array_ref, &target_type).unwrap();
13234        let result_run_array = cast_result
13235            .as_any()
13236            .downcast_ref::<RunArray<Int32Type>>()
13237            .unwrap();
13238        assert_eq!(
13239            result_run_array.run_ends().values(),
13240            &[2, 4, 6, 8, 10, 12, 14, 16]
13241        );
13242        assert_eq!(
13243            result_run_array
13244                .values()
13245                .as_primitive::<Int32Type>()
13246                .values(),
13247            &[1, 0, 2, 3, 0, 4, 5, 0]
13248        );
13249        assert_eq!(result_run_array.values().null_count(), 3);
13250    }
13251
13252    #[test]
13253    fn test_primitive_to_run_end_encoded_with_nulls_consecutive() {
13254        let source_array = Int64Array::from(vec![
13255            Some(1),
13256            Some(1),
13257            None,
13258            None,
13259            None,
13260            None,
13261            None,
13262            None,
13263            None,
13264            None,
13265            Some(4),
13266            Some(20),
13267            Some(500),
13268            Some(500),
13269            None,
13270            None,
13271        ]);
13272        let array_ref = Arc::new(source_array) as ArrayRef;
13273        let target_type = DataType::RunEndEncoded(
13274            Arc::new(Field::new("run_ends", DataType::Int16, false)),
13275            Arc::new(Field::new("values", DataType::Int64, true)),
13276        );
13277        let cast_result = cast(&array_ref, &target_type).unwrap();
13278        let result_run_array = cast_result
13279            .as_any()
13280            .downcast_ref::<RunArray<Int16Type>>()
13281            .unwrap();
13282        assert_eq!(
13283            result_run_array.run_ends().values(),
13284            &[2, 10, 11, 12, 14, 16]
13285        );
13286        assert_eq!(
13287            result_run_array
13288                .values()
13289                .as_primitive::<Int64Type>()
13290                .values(),
13291            &[1, 0, 4, 20, 500, 0]
13292        );
13293        assert_eq!(result_run_array.values().null_count(), 2);
13294    }
13295
13296    #[test]
13297    fn test_string_to_run_end_encoded() {
13298        // Create a String array with repeated values: ["a", "a", "b", "c", "c"]
13299        let source_array = StringArray::from(vec!["a", "a", "b", "c", "c"]);
13300        let array_ref = Arc::new(source_array) as ArrayRef;
13301
13302        // Cast to RunEndEncoded<Int32, String>
13303        let target_type = DataType::RunEndEncoded(
13304            Arc::new(Field::new("run_ends", DataType::Int32, false)),
13305            Arc::new(Field::new("values", DataType::Utf8, true)),
13306        );
13307        let cast_result = cast(&array_ref, &target_type).unwrap();
13308
13309        // Verify the result is a RunArray
13310        let result_run_array = cast_result
13311            .as_any()
13312            .downcast_ref::<RunArray<Int32Type>>()
13313            .unwrap();
13314
13315        // Check run structure: runs should end at positions [2, 3, 5]
13316        assert_eq!(result_run_array.run_ends().values(), &[2, 3, 5]);
13317
13318        // Check values: should be ["a", "b", "c"]
13319        let values_array = result_run_array.values().as_string::<i32>();
13320        assert_eq!(values_array.value(0), "a");
13321        assert_eq!(values_array.value(1), "b");
13322        assert_eq!(values_array.value(2), "c");
13323    }
13324
13325    #[test]
13326    fn test_empty_array_to_run_end_encoded() {
13327        // Create an empty Int32 array
13328        let source_array = Int32Array::from(Vec::<i32>::new());
13329        let array_ref = Arc::new(source_array) as ArrayRef;
13330
13331        // Cast to RunEndEncoded<Int32, Int32>
13332        let target_type = DataType::RunEndEncoded(
13333            Arc::new(Field::new("run_ends", DataType::Int32, false)),
13334            Arc::new(Field::new("values", DataType::Int32, true)),
13335        );
13336        let cast_result = cast(&array_ref, &target_type).unwrap();
13337
13338        // Verify the result is an empty RunArray
13339        let result_run_array = cast_result
13340            .as_any()
13341            .downcast_ref::<RunArray<Int32Type>>()
13342            .unwrap();
13343
13344        // Check that both run_ends and values are empty
13345        assert_eq!(result_run_array.run_ends().len(), 0);
13346        assert_eq!(result_run_array.values().len(), 0);
13347    }
13348
13349    #[test]
13350    fn test_run_end_encoded_with_nulls() {
13351        // Create a RunEndEncoded array with nulls: [1, 1, null, 2, 2]
13352        let run_ends = Int32Array::from(vec![2, 3, 5]);
13353        let values = Int32Array::from(vec![Some(1), None, Some(2)]);
13354        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13355        let array_ref = Arc::new(run_array) as ArrayRef;
13356
13357        // Cast to String
13358        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
13359
13360        // Verify the result preserves nulls
13361        let result_run_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
13362        assert_eq!(result_run_array.value(0), "1");
13363        assert!(result_run_array.is_null(2));
13364        assert_eq!(result_run_array.value(4), "2");
13365    }
13366
13367    #[test]
13368    fn test_different_index_types() {
13369        // Test with Int16 index type
13370        let source_array = Int32Array::from(vec![1, 1, 2, 3, 3]);
13371        let array_ref = Arc::new(source_array) as ArrayRef;
13372
13373        let target_type = DataType::RunEndEncoded(
13374            Arc::new(Field::new("run_ends", DataType::Int16, false)),
13375            Arc::new(Field::new("values", DataType::Int32, true)),
13376        );
13377        let cast_result = cast(&array_ref, &target_type).unwrap();
13378        assert_eq!(cast_result.data_type(), &target_type);
13379
13380        // Verify the cast worked correctly: values are [1, 2, 3]
13381        // and run-ends are [2, 3, 5]
13382        let run_array = cast_result
13383            .as_any()
13384            .downcast_ref::<RunArray<Int16Type>>()
13385            .unwrap();
13386        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
13387        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
13388        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
13389        assert_eq!(run_array.run_ends().values(), &[2i16, 3i16, 5i16]);
13390
13391        // Test again with Int64 index type
13392        let target_type = DataType::RunEndEncoded(
13393            Arc::new(Field::new("run_ends", DataType::Int64, false)),
13394            Arc::new(Field::new("values", DataType::Int32, true)),
13395        );
13396        let cast_result = cast(&array_ref, &target_type).unwrap();
13397        assert_eq!(cast_result.data_type(), &target_type);
13398
13399        // Verify the cast worked correctly: values are [1, 2, 3]
13400        // and run-ends are [2, 3, 5]
13401        let run_array = cast_result
13402            .as_any()
13403            .downcast_ref::<RunArray<Int64Type>>()
13404            .unwrap();
13405        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
13406        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
13407        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
13408        assert_eq!(run_array.run_ends().values(), &[2i64, 3i64, 5i64]);
13409    }
13410
13411    #[test]
13412    fn test_unsupported_cast_to_run_end_encoded() {
13413        // Create a Struct array - complex nested type that might not be supported
13414        let field = Field::new("item", DataType::Int32, false);
13415        let struct_array = StructArray::from(vec![(
13416            Arc::new(field),
13417            Arc::new(Int32Array::from(vec![1, 2, 3])) as ArrayRef,
13418        )]);
13419        let array_ref = Arc::new(struct_array) as ArrayRef;
13420
13421        // This should fail because:
13422        // 1. The target type is not RunEndEncoded
13423        // 2. The target type is not supported for casting from StructArray
13424        let cast_result = cast(&array_ref, &DataType::FixedSizeBinary(10));
13425
13426        // Expect this to fail
13427        assert!(cast_result.is_err());
13428    }
13429
13430    /// Test casting RunEndEncoded<Int64, String> to RunEndEncoded<Int16, String> should fail
13431    #[test]
13432    fn test_cast_run_end_encoded_int64_to_int16_should_fail() {
13433        // Construct a valid REE array with Int64 run-ends
13434        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
13435        let values = StringArray::from(vec!["a", "b", "c"]);
13436
13437        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
13438        let array_ref = Arc::new(ree_array) as ArrayRef;
13439
13440        // Attempt to cast to RunEndEncoded<Int16, Utf8>
13441        let target_type = DataType::RunEndEncoded(
13442            Arc::new(Field::new("run_ends", DataType::Int16, false)),
13443            Arc::new(Field::new("values", DataType::Utf8, true)),
13444        );
13445        let cast_options = CastOptions {
13446            safe: false, // This should make it fail instead of returning nulls
13447            format_options: FormatOptions::default(),
13448        };
13449
13450        // This should fail due to run-end overflow
13451        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
13452            cast_with_options(&array_ref, &target_type, &cast_options);
13453
13454        let e = result.expect_err("Cast should have failed but succeeded");
13455        assert!(
13456            e.to_string()
13457                .contains("Cast error: Can't cast value 100000 to type Int16")
13458        );
13459    }
13460
13461    #[test]
13462    fn test_cast_run_end_encoded_int64_to_int16_with_safe_should_fail_with_null_invalid_error() {
13463        // Construct a valid REE array with Int64 run-ends
13464        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
13465        let values = StringArray::from(vec!["a", "b", "c"]);
13466
13467        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
13468        let array_ref = Arc::new(ree_array) as ArrayRef;
13469
13470        // Attempt to cast to RunEndEncoded<Int16, Utf8>
13471        let target_type = DataType::RunEndEncoded(
13472            Arc::new(Field::new("run_ends", DataType::Int16, false)),
13473            Arc::new(Field::new("values", DataType::Utf8, true)),
13474        );
13475        let cast_options = CastOptions {
13476            safe: true,
13477            format_options: FormatOptions::default(),
13478        };
13479
13480        // This fails even though safe is true because the run_ends array has null values
13481        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
13482            cast_with_options(&array_ref, &target_type, &cast_options);
13483        let e = result.expect_err("Cast should have failed but succeeded");
13484        assert!(
13485            e.to_string()
13486                .contains("Invalid argument error: Found null values in run_ends array. The run_ends array should not have null values.")
13487        );
13488    }
13489
13490    /// Test casting RunEndEncoded<Int16, String> to RunEndEncoded<Int64, String> should succeed
13491    #[test]
13492    fn test_cast_run_end_encoded_int16_to_int64_should_succeed() {
13493        // Construct a valid REE array with Int16 run-ends
13494        let run_ends = Int16Array::from(vec![2, 5, 8]); // values that fit in Int16
13495        let values = StringArray::from(vec!["a", "b", "c"]);
13496
13497        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
13498        let array_ref = Arc::new(ree_array) as ArrayRef;
13499
13500        // Attempt to cast to RunEndEncoded<Int64, Utf8> (upcast should succeed)
13501        let target_type = DataType::RunEndEncoded(
13502            Arc::new(Field::new("run_ends", DataType::Int64, false)),
13503            Arc::new(Field::new("values", DataType::Utf8, true)),
13504        );
13505        let cast_options = CastOptions {
13506            safe: false,
13507            format_options: FormatOptions::default(),
13508        };
13509
13510        // This should succeed due to valid upcast
13511        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
13512            cast_with_options(&array_ref, &target_type, &cast_options);
13513
13514        let array_ref = result.expect("Cast should have succeeded but failed");
13515        // Downcast to RunArray<Int64Type>
13516        let run_array = array_ref
13517            .as_any()
13518            .downcast_ref::<RunArray<Int64Type>>()
13519            .unwrap();
13520
13521        // Verify the cast worked correctly
13522        // Assert the values were cast correctly
13523        assert_eq!(run_array.run_ends().values(), &[2i64, 5i64, 8i64]);
13524        assert_eq!(run_array.values().as_string::<i32>().value(0), "a");
13525        assert_eq!(run_array.values().as_string::<i32>().value(1), "b");
13526        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
13527    }
13528
13529    #[test]
13530    fn test_cast_run_end_encoded_dictionary_to_run_end_encoded() {
13531        // Construct a valid dictionary encoded array
13532        let values = StringArray::from_iter([Some("a"), Some("b"), Some("c")]);
13533        let keys = UInt64Array::from_iter(vec![1, 1, 1, 0, 0, 0, 2, 2, 2]);
13534        let array_ref = Arc::new(DictionaryArray::new(keys, Arc::new(values))) as ArrayRef;
13535
13536        // Attempt to cast to RunEndEncoded<Int64, Utf8>
13537        let target_type = DataType::RunEndEncoded(
13538            Arc::new(Field::new("run_ends", DataType::Int64, false)),
13539            Arc::new(Field::new("values", DataType::Utf8, true)),
13540        );
13541        let cast_options = CastOptions {
13542            safe: false,
13543            format_options: FormatOptions::default(),
13544        };
13545
13546        // This should succeed
13547        let result = cast_with_options(&array_ref, &target_type, &cast_options)
13548            .expect("Cast should have succeeded but failed");
13549
13550        // Verify the cast worked correctly
13551        // Assert the values were cast correctly
13552        let run_array = result
13553            .as_any()
13554            .downcast_ref::<RunArray<Int64Type>>()
13555            .unwrap();
13556        assert_eq!(run_array.values().as_string::<i32>().value(0), "b");
13557        assert_eq!(run_array.values().as_string::<i32>().value(1), "a");
13558        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
13559
13560        // Verify the run-ends were cast correctly (run ends at 3, 6, 9)
13561        assert_eq!(run_array.run_ends().values(), &[3i64, 6i64, 9i64]);
13562    }
13563
13564    fn int32_list_values() -> Vec<Option<Vec<Option<i32>>>> {
13565        vec![
13566            Some(vec![Some(1), Some(2), Some(3)]),
13567            Some(vec![Some(4), Some(5), Some(6)]),
13568            None,
13569            Some(vec![Some(7), Some(8), Some(9)]),
13570            Some(vec![None, Some(10)]),
13571        ]
13572    }
13573
13574    #[test]
13575    fn test_cast_list_view_to_list() {
13576        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13577        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13578        assert!(can_cast_types(list_view.data_type(), &target_type));
13579        let cast_result = cast(&list_view, &target_type).unwrap();
13580        let got_list = cast_result.as_list::<i32>();
13581        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13582        assert_eq!(got_list, &expected_list);
13583    }
13584
13585    #[test]
13586    fn test_cast_list_view_to_large_list() {
13587        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13588        let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
13589        assert!(can_cast_types(list_view.data_type(), &target_type));
13590        let cast_result = cast(&list_view, &target_type).unwrap();
13591        let got_list = cast_result.as_list::<i64>();
13592        let expected_list =
13593            LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13594        assert_eq!(got_list, &expected_list);
13595    }
13596
13597    #[test]
13598    fn test_cast_list_to_list_view() {
13599        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13600        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
13601        assert!(can_cast_types(list.data_type(), &target_type));
13602        let cast_result = cast(&list, &target_type).unwrap();
13603
13604        let got_list_view = cast_result.as_list_view::<i32>();
13605        let expected_list_view =
13606            ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13607        assert_eq!(got_list_view, &expected_list_view);
13608
13609        // inner types get cast
13610        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13611            Some(vec![Some(1), Some(2)]),
13612            None,
13613            Some(vec![None, Some(3)]),
13614        ]);
13615        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Float32, true)));
13616        assert!(can_cast_types(list.data_type(), &target_type));
13617        let cast_result = cast(&list, &target_type).unwrap();
13618
13619        let got_list_view = cast_result.as_list_view::<i32>();
13620        let expected_list_view = ListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13621            Some(vec![Some(1.0), Some(2.0)]),
13622            None,
13623            Some(vec![None, Some(3.0)]),
13624        ]);
13625        assert_eq!(got_list_view, &expected_list_view);
13626    }
13627
13628    #[test]
13629    fn test_cast_list_to_large_list_view() {
13630        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13631            Some(vec![Some(1), Some(2)]),
13632            None,
13633            Some(vec![None, Some(3)]),
13634        ]);
13635        let target_type =
13636            DataType::LargeListView(Arc::new(Field::new("item", DataType::Float32, true)));
13637        assert!(can_cast_types(list.data_type(), &target_type));
13638        let cast_result = cast(&list, &target_type).unwrap();
13639
13640        let got_list_view = cast_result.as_list_view::<i64>();
13641        let expected_list_view =
13642            LargeListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13643                Some(vec![Some(1.0), Some(2.0)]),
13644                None,
13645                Some(vec![None, Some(3.0)]),
13646            ]);
13647        assert_eq!(got_list_view, &expected_list_view);
13648    }
13649
13650    #[test]
13651    fn test_cast_large_list_view_to_large_list() {
13652        let list_view =
13653            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13654        let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
13655        assert!(can_cast_types(list_view.data_type(), &target_type));
13656        let cast_result = cast(&list_view, &target_type).unwrap();
13657        let got_list = cast_result.as_list::<i64>();
13658
13659        let expected_list =
13660            LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13661        assert_eq!(got_list, &expected_list);
13662    }
13663
13664    #[test]
13665    fn test_cast_large_list_view_to_list() {
13666        let list_view =
13667            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13668        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13669        assert!(can_cast_types(list_view.data_type(), &target_type));
13670        let cast_result = cast(&list_view, &target_type).unwrap();
13671        let got_list = cast_result.as_list::<i32>();
13672
13673        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13674        assert_eq!(got_list, &expected_list);
13675    }
13676
13677    #[test]
13678    fn test_cast_large_list_to_large_list_view() {
13679        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13680        let target_type =
13681            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, 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::<Int32Type, _, _>(int32_list_values());
13688        assert_eq!(got_list_view, &expected_list_view);
13689
13690        // inner types get cast
13691        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13692            Some(vec![Some(1), Some(2)]),
13693            None,
13694            Some(vec![None, Some(3)]),
13695        ]);
13696        let target_type =
13697            DataType::LargeListView(Arc::new(Field::new("item", DataType::Float32, true)));
13698        assert!(can_cast_types(list.data_type(), &target_type));
13699        let cast_result = cast(&list, &target_type).unwrap();
13700
13701        let got_list_view = cast_result.as_list_view::<i64>();
13702        let expected_list_view =
13703            LargeListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13704                Some(vec![Some(1.0), Some(2.0)]),
13705                None,
13706                Some(vec![None, Some(3.0)]),
13707            ]);
13708        assert_eq!(got_list_view, &expected_list_view);
13709    }
13710
13711    #[test]
13712    fn test_cast_large_list_to_list_view() {
13713        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13714            Some(vec![Some(1), Some(2)]),
13715            None,
13716            Some(vec![None, Some(3)]),
13717        ]);
13718        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Float32, true)));
13719        assert!(can_cast_types(list.data_type(), &target_type));
13720        let cast_result = cast(&list, &target_type).unwrap();
13721
13722        let got_list_view = cast_result.as_list_view::<i32>();
13723        let expected_list_view = ListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13724            Some(vec![Some(1.0), Some(2.0)]),
13725            None,
13726            Some(vec![None, Some(3.0)]),
13727        ]);
13728        assert_eq!(got_list_view, &expected_list_view);
13729    }
13730
13731    #[test]
13732    fn test_cast_list_view_to_list_out_of_order() {
13733        let list_view = ListViewArray::new(
13734            Arc::new(Field::new("item", DataType::Int32, true)),
13735            ScalarBuffer::from(vec![0, 6, 3]),
13736            ScalarBuffer::from(vec![3, 3, 3]),
13737            Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
13738            None,
13739        );
13740        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13741        assert!(can_cast_types(list_view.data_type(), &target_type));
13742        let cast_result = cast(&list_view, &target_type).unwrap();
13743        let got_list = cast_result.as_list::<i32>();
13744        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13745            Some(vec![Some(1), Some(2), Some(3)]),
13746            Some(vec![Some(7), Some(8), Some(9)]),
13747            Some(vec![Some(4), Some(5), Some(6)]),
13748        ]);
13749        assert_eq!(got_list, &expected_list);
13750    }
13751
13752    #[test]
13753    fn test_cast_list_view_to_list_overlapping() {
13754        let list_view = ListViewArray::new(
13755            Arc::new(Field::new("item", DataType::Int32, true)),
13756            ScalarBuffer::from(vec![0, 0]),
13757            ScalarBuffer::from(vec![1, 2]),
13758            Arc::new(Int32Array::from(vec![1, 2])),
13759            None,
13760        );
13761        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13762        assert!(can_cast_types(list_view.data_type(), &target_type));
13763        let cast_result = cast(&list_view, &target_type).unwrap();
13764        let got_list = cast_result.as_list::<i32>();
13765        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13766            Some(vec![Some(1)]),
13767            Some(vec![Some(1), Some(2)]),
13768        ]);
13769        assert_eq!(got_list, &expected_list);
13770    }
13771
13772    #[test]
13773    fn test_cast_list_view_to_list_empty() {
13774        let values: Vec<Option<Vec<Option<i32>>>> = vec![];
13775        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
13776        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13777        assert!(can_cast_types(list_view.data_type(), &target_type));
13778        let cast_result = cast(&list_view, &target_type).unwrap();
13779        let got_list = cast_result.as_list::<i32>();
13780        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
13781        assert_eq!(got_list, &expected_list);
13782    }
13783
13784    #[test]
13785    fn test_cast_list_view_to_list_different_inner_type() {
13786        let values = int32_list_values();
13787        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
13788        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int64, true)));
13789        assert!(can_cast_types(list_view.data_type(), &target_type));
13790        let cast_result = cast(&list_view, &target_type).unwrap();
13791        let got_list = cast_result.as_list::<i32>();
13792
13793        let expected_list =
13794            ListArray::from_iter_primitive::<Int64Type, _, _>(values.into_iter().map(|list| {
13795                list.map(|list| {
13796                    list.into_iter()
13797                        .map(|v| v.map(|v| v as i64))
13798                        .collect::<Vec<_>>()
13799                })
13800            }));
13801        assert_eq!(got_list, &expected_list);
13802    }
13803
13804    #[test]
13805    fn test_cast_list_view_to_list_out_of_order_with_nulls() {
13806        let list_view = ListViewArray::new(
13807            Arc::new(Field::new("item", DataType::Int32, true)),
13808            ScalarBuffer::from(vec![0, 6, 3]),
13809            ScalarBuffer::from(vec![3, 3, 3]),
13810            Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
13811            Some(NullBuffer::from(vec![false, true, false])),
13812        );
13813        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13814        assert!(can_cast_types(list_view.data_type(), &target_type));
13815        let cast_result = cast(&list_view, &target_type).unwrap();
13816        let got_list = cast_result.as_list::<i32>();
13817        let expected_list = ListArray::new(
13818            Arc::new(Field::new("item", DataType::Int32, true)),
13819            OffsetBuffer::from_lengths([3, 3, 3]),
13820            Arc::new(Int32Array::from(vec![1, 2, 3, 7, 8, 9, 4, 5, 6])),
13821            Some(NullBuffer::from(vec![false, true, false])),
13822        );
13823        assert_eq!(got_list, &expected_list);
13824    }
13825
13826    #[test]
13827    fn test_cast_list_view_to_large_list_view() {
13828        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13829        let target_type =
13830            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
13831        assert!(can_cast_types(list_view.data_type(), &target_type));
13832        let cast_result = cast(&list_view, &target_type).unwrap();
13833        let got = cast_result.as_list_view::<i64>();
13834
13835        let expected =
13836            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13837        assert_eq!(got, &expected);
13838    }
13839
13840    #[test]
13841    fn test_cast_large_list_view_to_list_view() {
13842        let list_view =
13843            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13844        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
13845        assert!(can_cast_types(list_view.data_type(), &target_type));
13846        let cast_result = cast(&list_view, &target_type).unwrap();
13847        let got = cast_result.as_list_view::<i32>();
13848
13849        let expected = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13850        assert_eq!(got, &expected);
13851    }
13852
13853    #[test]
13854    fn test_cast_time32_second_to_int64() {
13855        let array = Time32SecondArray::from(vec![1000, 2000, 3000]);
13856        let array = Arc::new(array) as Arc<dyn Array>;
13857        let to_type = DataType::Int64;
13858        let cast_options = CastOptions::default();
13859
13860        assert!(can_cast_types(array.data_type(), &to_type));
13861
13862        let result = cast_with_options(&array, &to_type, &cast_options);
13863        assert!(
13864            result.is_ok(),
13865            "Failed to cast Time32(Second) to Int64: {:?}",
13866            result.err()
13867        );
13868
13869        let cast_array = result.unwrap();
13870        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13871
13872        assert_eq!(cast_array.value(0), 1000);
13873        assert_eq!(cast_array.value(1), 2000);
13874        assert_eq!(cast_array.value(2), 3000);
13875    }
13876
13877    #[test]
13878    fn test_cast_time32_millisecond_to_int64() {
13879        let array = Time32MillisecondArray::from(vec![1000, 2000, 3000]);
13880        let array = Arc::new(array) as Arc<dyn Array>;
13881        let to_type = DataType::Int64;
13882        let cast_options = CastOptions::default();
13883
13884        assert!(can_cast_types(array.data_type(), &to_type));
13885
13886        let result = cast_with_options(&array, &to_type, &cast_options);
13887        assert!(
13888            result.is_ok(),
13889            "Failed to cast Time32(Millisecond) to Int64: {:?}",
13890            result.err()
13891        );
13892
13893        let cast_array = result.unwrap();
13894        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13895
13896        assert_eq!(cast_array.value(0), 1000);
13897        assert_eq!(cast_array.value(1), 2000);
13898        assert_eq!(cast_array.value(2), 3000);
13899    }
13900
13901    #[test]
13902    fn test_cast_time32_millisecond_to_time64_nanosecond() {
13903        let array =
13904            Time32MillisecondArray::from(vec![Some(1_000), Some(2_000), None, Some(43_200_000)]);
13905        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
13906        let c = b.as_primitive::<Time64NanosecondType>();
13907        assert_eq!(c.value(0), 1_000_000_000);
13908        assert_eq!(c.value(1), 2_000_000_000);
13909        assert!(c.is_null(2));
13910        assert_eq!(c.value(3), 43_200_000_000_000);
13911    }
13912
13913    #[test]
13914    fn test_cast_time32_millisecond_to_time64_microsecond() {
13915        let array =
13916            Time32MillisecondArray::from(vec![Some(1_000), Some(2_000), None, Some(43_200_000)]);
13917        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
13918        let c = b.as_primitive::<Time64MicrosecondType>();
13919        assert_eq!(c.value(0), 1_000_000);
13920        assert_eq!(c.value(1), 2_000_000);
13921        assert!(c.is_null(2));
13922        assert_eq!(c.value(3), 43_200_000_000);
13923    }
13924
13925    #[test]
13926    fn test_cast_time32_second_to_time64_nanosecond() {
13927        let array = Time32SecondArray::from(vec![Some(1), Some(60), None, Some(43_200)]);
13928        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
13929        let c = b.as_primitive::<Time64NanosecondType>();
13930        assert_eq!(c.value(0), 1_000_000_000);
13931        assert_eq!(c.value(1), 60_000_000_000);
13932        assert!(c.is_null(2));
13933        assert_eq!(c.value(3), 43_200_000_000_000);
13934    }
13935
13936    #[test]
13937    fn test_cast_time32_second_to_time64_microsecond() {
13938        let array = Time32SecondArray::from(vec![Some(1), Some(60), None, Some(43_200)]);
13939        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
13940        let c = b.as_primitive::<Time64MicrosecondType>();
13941        assert_eq!(c.value(0), 1_000_000);
13942        assert_eq!(c.value(1), 60_000_000);
13943        assert!(c.is_null(2));
13944        assert_eq!(c.value(3), 43_200_000_000);
13945    }
13946
13947    #[test]
13948    fn test_cast_time32_second_to_time32_millisecond_overflow() {
13949        let array = Time32SecondArray::from(vec![i32::MAX]);
13950
13951        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
13952        let c = b.as_primitive::<Time32MillisecondType>();
13953        assert!(c.is_null(0));
13954
13955        let options = CastOptions {
13956            safe: false,
13957            ..Default::default()
13958        };
13959        let err = cast_with_options(&array, &DataType::Time32(TimeUnit::Millisecond), &options)
13960            .unwrap_err();
13961        assert!(err.to_string().contains("Overflow"), "{err}");
13962    }
13963
13964    #[test]
13965    fn test_cast_string_to_time32_second_to_int64() {
13966        // Mimic: select arrow_cast('03:12:44'::time, 'Time32(Second)')::bigint;
13967        // raised in https://github.com/apache/datafusion/issues/19036
13968        let array = StringArray::from(vec!["03:12:44"]);
13969        let array = Arc::new(array) as Arc<dyn Array>;
13970        let cast_options = CastOptions::default();
13971
13972        // 1. Cast String to Time32(Second)
13973        let time32_type = DataType::Time32(TimeUnit::Second);
13974        let time32_array = cast_with_options(&array, &time32_type, &cast_options).unwrap();
13975
13976        // 2. Cast Time32(Second) to Int64
13977        let int64_type = DataType::Int64;
13978        assert!(can_cast_types(time32_array.data_type(), &int64_type));
13979
13980        let result = cast_with_options(&time32_array, &int64_type, &cast_options);
13981
13982        assert!(
13983            result.is_ok(),
13984            "Failed to cast Time32(Second) to Int64: {:?}",
13985            result.err()
13986        );
13987
13988        let cast_array = result.unwrap();
13989        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13990
13991        // 03:12:44 = 3*3600 + 12*60 + 44 = 10800 + 720 + 44 = 11564
13992        assert_eq!(cast_array.value(0), 11564);
13993    }
13994    #[test]
13995    fn test_string_dicts_to_binary_view() {
13996        let expected = BinaryViewArray::from_iter(vec![
13997            VIEW_TEST_DATA[1],
13998            VIEW_TEST_DATA[0],
13999            None,
14000            VIEW_TEST_DATA[3],
14001            None,
14002            VIEW_TEST_DATA[1],
14003            VIEW_TEST_DATA[4],
14004        ]);
14005
14006        let values_arrays: [ArrayRef; _] = [
14007            Arc::new(StringArray::from_iter(VIEW_TEST_DATA)),
14008            Arc::new(StringViewArray::from_iter(VIEW_TEST_DATA)),
14009            Arc::new(LargeStringArray::from_iter(VIEW_TEST_DATA)),
14010        ];
14011        for values in values_arrays {
14012            let keys =
14013                Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
14014            let string_dict_array = DictionaryArray::<Int8Type>::try_new(keys, values).unwrap();
14015
14016            let casted = cast(&string_dict_array, &DataType::BinaryView).unwrap();
14017            assert_eq!(casted.as_ref(), &expected);
14018        }
14019    }
14020
14021    #[test]
14022    fn test_binary_dicts_to_string_view() {
14023        let expected = StringViewArray::from_iter(vec![
14024            VIEW_TEST_DATA[1],
14025            VIEW_TEST_DATA[0],
14026            None,
14027            VIEW_TEST_DATA[3],
14028            None,
14029            VIEW_TEST_DATA[1],
14030            VIEW_TEST_DATA[4],
14031        ]);
14032
14033        let values_arrays: [ArrayRef; _] = [
14034            Arc::new(BinaryArray::from_iter(VIEW_TEST_DATA)),
14035            Arc::new(BinaryViewArray::from_iter(VIEW_TEST_DATA)),
14036            Arc::new(LargeBinaryArray::from_iter(VIEW_TEST_DATA)),
14037        ];
14038        for values in values_arrays {
14039            let keys =
14040                Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
14041            let string_dict_array = DictionaryArray::<Int8Type>::try_new(keys, values).unwrap();
14042
14043            let casted = cast(&string_dict_array, &DataType::Utf8View).unwrap();
14044            assert_eq!(casted.as_ref(), &expected);
14045        }
14046    }
14047
14048    #[test]
14049    fn test_cast_between_sliced_run_end_encoded() {
14050        let run_ends = Int16Array::from(vec![2, 5, 8]);
14051        let values = StringArray::from(vec!["a", "b", "c"]);
14052
14053        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
14054        let ree_array = ree_array.slice(1, 2);
14055        let array_ref = Arc::new(ree_array) as ArrayRef;
14056
14057        let target_type = DataType::RunEndEncoded(
14058            Arc::new(Field::new("run_ends", DataType::Int64, false)),
14059            Arc::new(Field::new("values", DataType::Utf8, true)),
14060        );
14061        let cast_options = CastOptions {
14062            safe: false,
14063            format_options: FormatOptions::default(),
14064        };
14065
14066        let result = cast_with_options(&array_ref, &target_type, &cast_options).unwrap();
14067        let run_array = result.as_run::<Int64Type>();
14068        let run_array = run_array.downcast::<StringArray>().unwrap();
14069
14070        let expected = vec!["a", "b"];
14071        let actual = run_array.into_iter().flatten().collect::<Vec<_>>();
14072
14073        assert_eq!(expected, actual);
14074    }
14075}