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            cast_values_to_fixed_size_list(array, to, *size, cast_options)
942        }
943        // Map
944        (Map(_, ordered1), Map(_, ordered2)) if ordered1 == ordered2 => {
945            cast_map_values(array.as_map(), to_type, cast_options, ordered1.to_owned())
946        }
947        // Decimal to decimal, same width
948        (Decimal32(p1, s1), Decimal32(p2, s2)) => {
949            cast_decimal_to_decimal_same_type::<Decimal32Type>(
950                array.as_primitive(),
951                *p1,
952                *s1,
953                *p2,
954                *s2,
955                cast_options,
956            )
957        }
958        (Decimal64(p1, s1), Decimal64(p2, s2)) => {
959            cast_decimal_to_decimal_same_type::<Decimal64Type>(
960                array.as_primitive(),
961                *p1,
962                *s1,
963                *p2,
964                *s2,
965                cast_options,
966            )
967        }
968        (Decimal128(p1, s1), Decimal128(p2, s2)) => {
969            cast_decimal_to_decimal_same_type::<Decimal128Type>(
970                array.as_primitive(),
971                *p1,
972                *s1,
973                *p2,
974                *s2,
975                cast_options,
976            )
977        }
978        (Decimal256(p1, s1), Decimal256(p2, s2)) => {
979            cast_decimal_to_decimal_same_type::<Decimal256Type>(
980                array.as_primitive(),
981                *p1,
982                *s1,
983                *p2,
984                *s2,
985                cast_options,
986            )
987        }
988        // Decimal to decimal, different width
989        (Decimal32(p1, s1), Decimal64(p2, s2)) => {
990            cast_decimal_to_decimal::<Decimal32Type, Decimal64Type>(
991                array.as_primitive(),
992                *p1,
993                *s1,
994                *p2,
995                *s2,
996                cast_options,
997            )
998        }
999        (Decimal32(p1, s1), Decimal128(p2, s2)) => {
1000            cast_decimal_to_decimal::<Decimal32Type, Decimal128Type>(
1001                array.as_primitive(),
1002                *p1,
1003                *s1,
1004                *p2,
1005                *s2,
1006                cast_options,
1007            )
1008        }
1009        (Decimal32(p1, s1), Decimal256(p2, s2)) => {
1010            cast_decimal_to_decimal::<Decimal32Type, Decimal256Type>(
1011                array.as_primitive(),
1012                *p1,
1013                *s1,
1014                *p2,
1015                *s2,
1016                cast_options,
1017            )
1018        }
1019        (Decimal64(p1, s1), Decimal32(p2, s2)) => {
1020            cast_decimal_to_decimal::<Decimal64Type, Decimal32Type>(
1021                array.as_primitive(),
1022                *p1,
1023                *s1,
1024                *p2,
1025                *s2,
1026                cast_options,
1027            )
1028        }
1029        (Decimal64(p1, s1), Decimal128(p2, s2)) => {
1030            cast_decimal_to_decimal::<Decimal64Type, Decimal128Type>(
1031                array.as_primitive(),
1032                *p1,
1033                *s1,
1034                *p2,
1035                *s2,
1036                cast_options,
1037            )
1038        }
1039        (Decimal64(p1, s1), Decimal256(p2, s2)) => {
1040            cast_decimal_to_decimal::<Decimal64Type, Decimal256Type>(
1041                array.as_primitive(),
1042                *p1,
1043                *s1,
1044                *p2,
1045                *s2,
1046                cast_options,
1047            )
1048        }
1049        (Decimal128(p1, s1), Decimal32(p2, s2)) => {
1050            cast_decimal_to_decimal::<Decimal128Type, Decimal32Type>(
1051                array.as_primitive(),
1052                *p1,
1053                *s1,
1054                *p2,
1055                *s2,
1056                cast_options,
1057            )
1058        }
1059        (Decimal128(p1, s1), Decimal64(p2, s2)) => {
1060            cast_decimal_to_decimal::<Decimal128Type, Decimal64Type>(
1061                array.as_primitive(),
1062                *p1,
1063                *s1,
1064                *p2,
1065                *s2,
1066                cast_options,
1067            )
1068        }
1069        (Decimal128(p1, s1), Decimal256(p2, s2)) => {
1070            cast_decimal_to_decimal::<Decimal128Type, Decimal256Type>(
1071                array.as_primitive(),
1072                *p1,
1073                *s1,
1074                *p2,
1075                *s2,
1076                cast_options,
1077            )
1078        }
1079        (Decimal256(p1, s1), Decimal32(p2, s2)) => {
1080            cast_decimal_to_decimal::<Decimal256Type, Decimal32Type>(
1081                array.as_primitive(),
1082                *p1,
1083                *s1,
1084                *p2,
1085                *s2,
1086                cast_options,
1087            )
1088        }
1089        (Decimal256(p1, s1), Decimal64(p2, s2)) => {
1090            cast_decimal_to_decimal::<Decimal256Type, Decimal64Type>(
1091                array.as_primitive(),
1092                *p1,
1093                *s1,
1094                *p2,
1095                *s2,
1096                cast_options,
1097            )
1098        }
1099        (Decimal256(p1, s1), Decimal128(p2, s2)) => {
1100            cast_decimal_to_decimal::<Decimal256Type, Decimal128Type>(
1101                array.as_primitive(),
1102                *p1,
1103                *s1,
1104                *p2,
1105                *s2,
1106                cast_options,
1107            )
1108        }
1109        // Decimal to non-decimal
1110        (Decimal32(_, scale), _) if !to_type.is_temporal() => {
1111            cast_from_decimal::<Decimal32Type, _>(
1112                array,
1113                10_i32,
1114                scale,
1115                from_type,
1116                to_type,
1117                |x: i32| x as f64,
1118                cast_options,
1119            )
1120        }
1121        (Decimal64(_, scale), _) if !to_type.is_temporal() => {
1122            cast_from_decimal::<Decimal64Type, _>(
1123                array,
1124                10_i64,
1125                scale,
1126                from_type,
1127                to_type,
1128                |x: i64| x as f64,
1129                cast_options,
1130            )
1131        }
1132        (Decimal128(_, scale), _) if !to_type.is_temporal() => {
1133            cast_from_decimal::<Decimal128Type, _>(
1134                array,
1135                10_i128,
1136                scale,
1137                from_type,
1138                to_type,
1139                |x: i128| x as f64,
1140                cast_options,
1141            )
1142        }
1143        (Decimal256(_, scale), _) if !to_type.is_temporal() => {
1144            cast_from_decimal::<Decimal256Type, _>(
1145                array,
1146                i256::from_i128(10_i128),
1147                scale,
1148                from_type,
1149                to_type,
1150                |x: i256| x.to_f64().expect("All i256 values fit in f64"),
1151                cast_options,
1152            )
1153        }
1154        // Non-decimal to decimal
1155        (_, Decimal32(precision, scale)) if !from_type.is_temporal() => {
1156            cast_to_decimal::<Decimal32Type, _>(
1157                array,
1158                10_i32,
1159                precision,
1160                scale,
1161                from_type,
1162                to_type,
1163                cast_options,
1164            )
1165        }
1166        (_, Decimal64(precision, scale)) if !from_type.is_temporal() => {
1167            cast_to_decimal::<Decimal64Type, _>(
1168                array,
1169                10_i64,
1170                precision,
1171                scale,
1172                from_type,
1173                to_type,
1174                cast_options,
1175            )
1176        }
1177        (_, Decimal128(precision, scale)) if !from_type.is_temporal() => {
1178            cast_to_decimal::<Decimal128Type, _>(
1179                array,
1180                10_i128,
1181                precision,
1182                scale,
1183                from_type,
1184                to_type,
1185                cast_options,
1186            )
1187        }
1188        (_, Decimal256(precision, scale)) if !from_type.is_temporal() => {
1189            cast_to_decimal::<Decimal256Type, _>(
1190                array,
1191                i256::from_i128(10_i128),
1192                precision,
1193                scale,
1194                from_type,
1195                to_type,
1196                cast_options,
1197            )
1198        }
1199        (Struct(from_fields), Struct(to_fields)) => cast_struct_to_struct(
1200            array.as_struct(),
1201            from_fields.clone(),
1202            to_fields.clone(),
1203            cast_options,
1204        ),
1205        (Struct(_), _) => Err(ArrowError::CastError(format!(
1206            "Casting from {from_type} to {to_type} not supported"
1207        ))),
1208        (_, Struct(_)) => Err(ArrowError::CastError(format!(
1209            "Casting from {from_type} to {to_type} not supported"
1210        ))),
1211        (_, Boolean) => match from_type {
1212            UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
1213            UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
1214            UInt32 => cast_numeric_to_bool::<UInt32Type>(array),
1215            UInt64 => cast_numeric_to_bool::<UInt64Type>(array),
1216            Int8 => cast_numeric_to_bool::<Int8Type>(array),
1217            Int16 => cast_numeric_to_bool::<Int16Type>(array),
1218            Int32 => cast_numeric_to_bool::<Int32Type>(array),
1219            Int64 => cast_numeric_to_bool::<Int64Type>(array),
1220            Float16 => cast_numeric_to_bool::<Float16Type>(array),
1221            Float32 => cast_numeric_to_bool::<Float32Type>(array),
1222            Float64 => cast_numeric_to_bool::<Float64Type>(array),
1223            Utf8View => cast_utf8view_to_boolean(array, cast_options),
1224            Utf8 => cast_utf8_to_boolean::<i32>(array, cast_options),
1225            LargeUtf8 => cast_utf8_to_boolean::<i64>(array, cast_options),
1226            _ => Err(ArrowError::CastError(format!(
1227                "Casting from {from_type} to {to_type} not supported",
1228            ))),
1229        },
1230        (Boolean, _) => match to_type {
1231            UInt8 => cast_bool_to_numeric::<UInt8Type>(array, cast_options),
1232            UInt16 => cast_bool_to_numeric::<UInt16Type>(array, cast_options),
1233            UInt32 => cast_bool_to_numeric::<UInt32Type>(array, cast_options),
1234            UInt64 => cast_bool_to_numeric::<UInt64Type>(array, cast_options),
1235            Int8 => cast_bool_to_numeric::<Int8Type>(array, cast_options),
1236            Int16 => cast_bool_to_numeric::<Int16Type>(array, cast_options),
1237            Int32 => cast_bool_to_numeric::<Int32Type>(array, cast_options),
1238            Int64 => cast_bool_to_numeric::<Int64Type>(array, cast_options),
1239            Float16 => cast_bool_to_numeric::<Float16Type>(array, cast_options),
1240            Float32 => cast_bool_to_numeric::<Float32Type>(array, cast_options),
1241            Float64 => cast_bool_to_numeric::<Float64Type>(array, cast_options),
1242            Utf8View => value_to_string_view(array, cast_options),
1243            Utf8 => value_to_string::<i32>(array, cast_options),
1244            LargeUtf8 => value_to_string::<i64>(array, cast_options),
1245            _ => Err(ArrowError::CastError(format!(
1246                "Casting from {from_type} to {to_type} not supported",
1247            ))),
1248        },
1249        (Utf8, _) => match to_type {
1250            UInt8 => parse_string::<UInt8Type, i32>(array, cast_options),
1251            UInt16 => parse_string::<UInt16Type, i32>(array, cast_options),
1252            UInt32 => parse_string::<UInt32Type, i32>(array, cast_options),
1253            UInt64 => parse_string::<UInt64Type, i32>(array, cast_options),
1254            Int8 => parse_string::<Int8Type, i32>(array, cast_options),
1255            Int16 => parse_string::<Int16Type, i32>(array, cast_options),
1256            Int32 => parse_string::<Int32Type, i32>(array, cast_options),
1257            Int64 => parse_string::<Int64Type, i32>(array, cast_options),
1258            Float16 => parse_string::<Float16Type, i32>(array, cast_options),
1259            Float32 => parse_string::<Float32Type, i32>(array, cast_options),
1260            Float64 => parse_string::<Float64Type, i32>(array, cast_options),
1261            Date32 => parse_string::<Date32Type, i32>(array, cast_options),
1262            Date64 => parse_string::<Date64Type, i32>(array, cast_options),
1263            Binary => Ok(Arc::new(BinaryArray::from(
1264                array.as_string::<i32>().clone(),
1265            ))),
1266            LargeBinary => {
1267                let binary = BinaryArray::from(array.as_string::<i32>().clone());
1268                cast_byte_container::<BinaryType, LargeBinaryType>(&binary)
1269            }
1270            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i32>()))),
1271            BinaryView => Ok(Arc::new(
1272                StringViewArray::from(array.as_string::<i32>()).to_binary_view(),
1273            )),
1274            LargeUtf8 => cast_byte_container::<Utf8Type, LargeUtf8Type>(array),
1275            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i32>(array, cast_options),
1276            Time32(TimeUnit::Millisecond) => {
1277                parse_string::<Time32MillisecondType, i32>(array, cast_options)
1278            }
1279            Time64(TimeUnit::Microsecond) => {
1280                parse_string::<Time64MicrosecondType, i32>(array, cast_options)
1281            }
1282            Time64(TimeUnit::Nanosecond) => {
1283                parse_string::<Time64NanosecondType, i32>(array, cast_options)
1284            }
1285            Timestamp(TimeUnit::Second, to_tz) => {
1286                cast_string_to_timestamp::<i32, TimestampSecondType>(array, to_tz, cast_options)
1287            }
1288            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1289                i32,
1290                TimestampMillisecondType,
1291            >(array, to_tz, cast_options),
1292            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1293                i32,
1294                TimestampMicrosecondType,
1295            >(array, to_tz, cast_options),
1296            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1297                cast_string_to_timestamp::<i32, TimestampNanosecondType>(array, to_tz, cast_options)
1298            }
1299            Interval(IntervalUnit::YearMonth) => {
1300                cast_string_to_year_month_interval::<i32>(array, cast_options)
1301            }
1302            Interval(IntervalUnit::DayTime) => {
1303                cast_string_to_day_time_interval::<i32>(array, cast_options)
1304            }
1305            Interval(IntervalUnit::MonthDayNano) => {
1306                cast_string_to_month_day_nano_interval::<i32>(array, cast_options)
1307            }
1308            _ => Err(ArrowError::CastError(format!(
1309                "Casting from {from_type} to {to_type} not supported",
1310            ))),
1311        },
1312        (Utf8View, _) => match to_type {
1313            UInt8 => parse_string_view::<UInt8Type>(array, cast_options),
1314            UInt16 => parse_string_view::<UInt16Type>(array, cast_options),
1315            UInt32 => parse_string_view::<UInt32Type>(array, cast_options),
1316            UInt64 => parse_string_view::<UInt64Type>(array, cast_options),
1317            Int8 => parse_string_view::<Int8Type>(array, cast_options),
1318            Int16 => parse_string_view::<Int16Type>(array, cast_options),
1319            Int32 => parse_string_view::<Int32Type>(array, cast_options),
1320            Int64 => parse_string_view::<Int64Type>(array, cast_options),
1321            Float16 => parse_string_view::<Float16Type>(array, cast_options),
1322            Float32 => parse_string_view::<Float32Type>(array, cast_options),
1323            Float64 => parse_string_view::<Float64Type>(array, cast_options),
1324            Date32 => parse_string_view::<Date32Type>(array, cast_options),
1325            Date64 => parse_string_view::<Date64Type>(array, cast_options),
1326            Binary => cast_view_to_byte::<StringViewType, GenericBinaryType<i32>>(array),
1327            LargeBinary => cast_view_to_byte::<StringViewType, GenericBinaryType<i64>>(array),
1328            BinaryView => Ok(Arc::new(array.as_string_view().clone().to_binary_view())),
1329            Utf8 => cast_view_to_byte::<StringViewType, GenericStringType<i32>>(array),
1330            LargeUtf8 => cast_view_to_byte::<StringViewType, GenericStringType<i64>>(array),
1331            Time32(TimeUnit::Second) => parse_string_view::<Time32SecondType>(array, cast_options),
1332            Time32(TimeUnit::Millisecond) => {
1333                parse_string_view::<Time32MillisecondType>(array, cast_options)
1334            }
1335            Time64(TimeUnit::Microsecond) => {
1336                parse_string_view::<Time64MicrosecondType>(array, cast_options)
1337            }
1338            Time64(TimeUnit::Nanosecond) => {
1339                parse_string_view::<Time64NanosecondType>(array, cast_options)
1340            }
1341            Timestamp(TimeUnit::Second, to_tz) => {
1342                cast_view_to_timestamp::<TimestampSecondType>(array, to_tz, cast_options)
1343            }
1344            Timestamp(TimeUnit::Millisecond, to_tz) => {
1345                cast_view_to_timestamp::<TimestampMillisecondType>(array, to_tz, cast_options)
1346            }
1347            Timestamp(TimeUnit::Microsecond, to_tz) => {
1348                cast_view_to_timestamp::<TimestampMicrosecondType>(array, to_tz, cast_options)
1349            }
1350            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1351                cast_view_to_timestamp::<TimestampNanosecondType>(array, to_tz, cast_options)
1352            }
1353            Interval(IntervalUnit::YearMonth) => {
1354                cast_view_to_year_month_interval(array, cast_options)
1355            }
1356            Interval(IntervalUnit::DayTime) => cast_view_to_day_time_interval(array, cast_options),
1357            Interval(IntervalUnit::MonthDayNano) => {
1358                cast_view_to_month_day_nano_interval(array, cast_options)
1359            }
1360            _ => Err(ArrowError::CastError(format!(
1361                "Casting from {from_type} to {to_type} not supported",
1362            ))),
1363        },
1364        (LargeUtf8, _) => match to_type {
1365            UInt8 => parse_string::<UInt8Type, i64>(array, cast_options),
1366            UInt16 => parse_string::<UInt16Type, i64>(array, cast_options),
1367            UInt32 => parse_string::<UInt32Type, i64>(array, cast_options),
1368            UInt64 => parse_string::<UInt64Type, i64>(array, cast_options),
1369            Int8 => parse_string::<Int8Type, i64>(array, cast_options),
1370            Int16 => parse_string::<Int16Type, i64>(array, cast_options),
1371            Int32 => parse_string::<Int32Type, i64>(array, cast_options),
1372            Int64 => parse_string::<Int64Type, i64>(array, cast_options),
1373            Float16 => parse_string::<Float16Type, i64>(array, cast_options),
1374            Float32 => parse_string::<Float32Type, i64>(array, cast_options),
1375            Float64 => parse_string::<Float64Type, i64>(array, cast_options),
1376            Date32 => parse_string::<Date32Type, i64>(array, cast_options),
1377            Date64 => parse_string::<Date64Type, i64>(array, cast_options),
1378            Utf8 => cast_byte_container::<LargeUtf8Type, Utf8Type>(array),
1379            Binary => {
1380                let large_binary = LargeBinaryArray::from(array.as_string::<i64>().clone());
1381                cast_byte_container::<LargeBinaryType, BinaryType>(&large_binary)
1382            }
1383            LargeBinary => Ok(Arc::new(LargeBinaryArray::from(
1384                array.as_string::<i64>().clone(),
1385            ))),
1386            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i64>()))),
1387            BinaryView => Ok(Arc::new(BinaryViewArray::from(
1388                array
1389                    .as_string::<i64>()
1390                    .into_iter()
1391                    .map(|x| x.map(|x| x.as_bytes()))
1392                    .collect::<Vec<_>>(),
1393            ))),
1394            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i64>(array, cast_options),
1395            Time32(TimeUnit::Millisecond) => {
1396                parse_string::<Time32MillisecondType, i64>(array, cast_options)
1397            }
1398            Time64(TimeUnit::Microsecond) => {
1399                parse_string::<Time64MicrosecondType, i64>(array, cast_options)
1400            }
1401            Time64(TimeUnit::Nanosecond) => {
1402                parse_string::<Time64NanosecondType, i64>(array, cast_options)
1403            }
1404            Timestamp(TimeUnit::Second, to_tz) => {
1405                cast_string_to_timestamp::<i64, TimestampSecondType>(array, to_tz, cast_options)
1406            }
1407            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1408                i64,
1409                TimestampMillisecondType,
1410            >(array, to_tz, cast_options),
1411            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1412                i64,
1413                TimestampMicrosecondType,
1414            >(array, to_tz, cast_options),
1415            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1416                cast_string_to_timestamp::<i64, TimestampNanosecondType>(array, to_tz, cast_options)
1417            }
1418            Interval(IntervalUnit::YearMonth) => {
1419                cast_string_to_year_month_interval::<i64>(array, cast_options)
1420            }
1421            Interval(IntervalUnit::DayTime) => {
1422                cast_string_to_day_time_interval::<i64>(array, cast_options)
1423            }
1424            Interval(IntervalUnit::MonthDayNano) => {
1425                cast_string_to_month_day_nano_interval::<i64>(array, cast_options)
1426            }
1427            _ => Err(ArrowError::CastError(format!(
1428                "Casting from {from_type} to {to_type} not supported",
1429            ))),
1430        },
1431        (Binary, _) => match to_type {
1432            Utf8 => cast_binary_to_string::<i32>(array, cast_options),
1433            LargeUtf8 => {
1434                let array = cast_binary_to_string::<i32>(array, cast_options)?;
1435                cast_byte_container::<Utf8Type, LargeUtf8Type>(array.as_ref())
1436            }
1437            LargeBinary => cast_byte_container::<BinaryType, LargeBinaryType>(array),
1438            FixedSizeBinary(size) => {
1439                cast_binary_to_fixed_size_binary::<i32>(array, *size, cast_options)
1440            }
1441            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i32>()))),
1442            Utf8View => Ok(Arc::new(StringViewArray::from(
1443                cast_binary_to_string::<i32>(array, cast_options)?.as_string::<i32>(),
1444            ))),
1445            _ => Err(ArrowError::CastError(format!(
1446                "Casting from {from_type} to {to_type} not supported",
1447            ))),
1448        },
1449        (LargeBinary, _) => match to_type {
1450            Utf8 => {
1451                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1452                cast_byte_container::<LargeUtf8Type, Utf8Type>(array.as_ref())
1453            }
1454            LargeUtf8 => cast_binary_to_string::<i64>(array, cast_options),
1455            Binary => cast_byte_container::<LargeBinaryType, BinaryType>(array),
1456            FixedSizeBinary(size) => {
1457                cast_binary_to_fixed_size_binary::<i64>(array, *size, cast_options)
1458            }
1459            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i64>()))),
1460            Utf8View => {
1461                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1462                Ok(Arc::new(StringViewArray::from(array.as_string::<i64>())))
1463            }
1464            _ => Err(ArrowError::CastError(format!(
1465                "Casting from {from_type} to {to_type} not supported",
1466            ))),
1467        },
1468        (FixedSizeBinary(size), _) => match to_type {
1469            Binary => cast_fixed_size_binary_to_binary::<i32>(array, *size),
1470            LargeBinary => cast_fixed_size_binary_to_binary::<i64>(array, *size),
1471            BinaryView => cast_fixed_size_binary_to_binary_view(array, *size),
1472            _ => Err(ArrowError::CastError(format!(
1473                "Casting from {from_type} to {to_type} not supported",
1474            ))),
1475        },
1476        (BinaryView, Binary) => cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array),
1477        (BinaryView, LargeBinary) => {
1478            cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)
1479        }
1480        (BinaryView, Utf8) => {
1481            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array)?;
1482            cast_binary_to_string::<i32>(&binary_arr, cast_options)
1483        }
1484        (BinaryView, LargeUtf8) => {
1485            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)?;
1486            cast_binary_to_string::<i64>(&binary_arr, cast_options)
1487        }
1488        (BinaryView, Utf8View) => cast_binary_view_to_string_view(array, cast_options),
1489        (BinaryView, _) => Err(ArrowError::CastError(format!(
1490            "Casting from {from_type} to {to_type} not supported",
1491        ))),
1492        (from_type, Utf8View) if from_type.is_primitive() => {
1493            value_to_string_view(array, cast_options)
1494        }
1495        (from_type, LargeUtf8) if from_type.is_primitive() => {
1496            value_to_string::<i64>(array, cast_options)
1497        }
1498        (from_type, Utf8) if from_type.is_primitive() => {
1499            value_to_string::<i32>(array, cast_options)
1500        }
1501        (from_type, Binary) if from_type.is_integer() => match from_type {
1502            UInt8 => cast_numeric_to_binary::<UInt8Type, i32>(array),
1503            UInt16 => cast_numeric_to_binary::<UInt16Type, i32>(array),
1504            UInt32 => cast_numeric_to_binary::<UInt32Type, i32>(array),
1505            UInt64 => cast_numeric_to_binary::<UInt64Type, i32>(array),
1506            Int8 => cast_numeric_to_binary::<Int8Type, i32>(array),
1507            Int16 => cast_numeric_to_binary::<Int16Type, i32>(array),
1508            Int32 => cast_numeric_to_binary::<Int32Type, i32>(array),
1509            Int64 => cast_numeric_to_binary::<Int64Type, i32>(array),
1510            _ => unreachable!(),
1511        },
1512        (from_type, LargeBinary) if from_type.is_integer() => match from_type {
1513            UInt8 => cast_numeric_to_binary::<UInt8Type, i64>(array),
1514            UInt16 => cast_numeric_to_binary::<UInt16Type, i64>(array),
1515            UInt32 => cast_numeric_to_binary::<UInt32Type, i64>(array),
1516            UInt64 => cast_numeric_to_binary::<UInt64Type, i64>(array),
1517            Int8 => cast_numeric_to_binary::<Int8Type, i64>(array),
1518            Int16 => cast_numeric_to_binary::<Int16Type, i64>(array),
1519            Int32 => cast_numeric_to_binary::<Int32Type, i64>(array),
1520            Int64 => cast_numeric_to_binary::<Int64Type, i64>(array),
1521            _ => unreachable!(),
1522        },
1523        // start numeric casts
1524        (UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array, cast_options),
1525        (UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array, cast_options),
1526        (UInt8, UInt64) => cast_numeric_arrays::<UInt8Type, UInt64Type>(array, cast_options),
1527        (UInt8, Int8) => cast_numeric_arrays::<UInt8Type, Int8Type>(array, cast_options),
1528        (UInt8, Int16) => cast_numeric_arrays::<UInt8Type, Int16Type>(array, cast_options),
1529        (UInt8, Int32) => cast_numeric_arrays::<UInt8Type, Int32Type>(array, cast_options),
1530        (UInt8, Int64) => cast_numeric_arrays::<UInt8Type, Int64Type>(array, cast_options),
1531        (UInt8, Float16) => cast_numeric_arrays::<UInt8Type, Float16Type>(array, cast_options),
1532        (UInt8, Float32) => cast_numeric_arrays::<UInt8Type, Float32Type>(array, cast_options),
1533        (UInt8, Float64) => cast_numeric_arrays::<UInt8Type, Float64Type>(array, cast_options),
1534
1535        (UInt16, UInt8) => cast_numeric_arrays::<UInt16Type, UInt8Type>(array, cast_options),
1536        (UInt16, UInt32) => cast_numeric_arrays::<UInt16Type, UInt32Type>(array, cast_options),
1537        (UInt16, UInt64) => cast_numeric_arrays::<UInt16Type, UInt64Type>(array, cast_options),
1538        (UInt16, Int8) => cast_numeric_arrays::<UInt16Type, Int8Type>(array, cast_options),
1539        (UInt16, Int16) => cast_numeric_arrays::<UInt16Type, Int16Type>(array, cast_options),
1540        (UInt16, Int32) => cast_numeric_arrays::<UInt16Type, Int32Type>(array, cast_options),
1541        (UInt16, Int64) => cast_numeric_arrays::<UInt16Type, Int64Type>(array, cast_options),
1542        (UInt16, Float16) => cast_numeric_arrays::<UInt16Type, Float16Type>(array, cast_options),
1543        (UInt16, Float32) => cast_numeric_arrays::<UInt16Type, Float32Type>(array, cast_options),
1544        (UInt16, Float64) => cast_numeric_arrays::<UInt16Type, Float64Type>(array, cast_options),
1545
1546        (UInt32, UInt8) => cast_numeric_arrays::<UInt32Type, UInt8Type>(array, cast_options),
1547        (UInt32, UInt16) => cast_numeric_arrays::<UInt32Type, UInt16Type>(array, cast_options),
1548        (UInt32, UInt64) => cast_numeric_arrays::<UInt32Type, UInt64Type>(array, cast_options),
1549        (UInt32, Int8) => cast_numeric_arrays::<UInt32Type, Int8Type>(array, cast_options),
1550        (UInt32, Int16) => cast_numeric_arrays::<UInt32Type, Int16Type>(array, cast_options),
1551        (UInt32, Int32) => cast_numeric_arrays::<UInt32Type, Int32Type>(array, cast_options),
1552        (UInt32, Int64) => cast_numeric_arrays::<UInt32Type, Int64Type>(array, cast_options),
1553        (UInt32, Float16) => cast_numeric_arrays::<UInt32Type, Float16Type>(array, cast_options),
1554        (UInt32, Float32) => cast_numeric_arrays::<UInt32Type, Float32Type>(array, cast_options),
1555        (UInt32, Float64) => cast_numeric_arrays::<UInt32Type, Float64Type>(array, cast_options),
1556
1557        (UInt64, UInt8) => cast_numeric_arrays::<UInt64Type, UInt8Type>(array, cast_options),
1558        (UInt64, UInt16) => cast_numeric_arrays::<UInt64Type, UInt16Type>(array, cast_options),
1559        (UInt64, UInt32) => cast_numeric_arrays::<UInt64Type, UInt32Type>(array, cast_options),
1560        (UInt64, Int8) => cast_numeric_arrays::<UInt64Type, Int8Type>(array, cast_options),
1561        (UInt64, Int16) => cast_numeric_arrays::<UInt64Type, Int16Type>(array, cast_options),
1562        (UInt64, Int32) => cast_numeric_arrays::<UInt64Type, Int32Type>(array, cast_options),
1563        (UInt64, Int64) => cast_numeric_arrays::<UInt64Type, Int64Type>(array, cast_options),
1564        (UInt64, Float16) => cast_numeric_arrays::<UInt64Type, Float16Type>(array, cast_options),
1565        (UInt64, Float32) => cast_numeric_arrays::<UInt64Type, Float32Type>(array, cast_options),
1566        (UInt64, Float64) => cast_numeric_arrays::<UInt64Type, Float64Type>(array, cast_options),
1567
1568        (Int8, UInt8) => cast_numeric_arrays::<Int8Type, UInt8Type>(array, cast_options),
1569        (Int8, UInt16) => cast_numeric_arrays::<Int8Type, UInt16Type>(array, cast_options),
1570        (Int8, UInt32) => cast_numeric_arrays::<Int8Type, UInt32Type>(array, cast_options),
1571        (Int8, UInt64) => cast_numeric_arrays::<Int8Type, UInt64Type>(array, cast_options),
1572        (Int8, Int16) => cast_numeric_arrays::<Int8Type, Int16Type>(array, cast_options),
1573        (Int8, Int32) => cast_numeric_arrays::<Int8Type, Int32Type>(array, cast_options),
1574        (Int8, Int64) => cast_numeric_arrays::<Int8Type, Int64Type>(array, cast_options),
1575        (Int8, Float16) => cast_numeric_arrays::<Int8Type, Float16Type>(array, cast_options),
1576        (Int8, Float32) => cast_numeric_arrays::<Int8Type, Float32Type>(array, cast_options),
1577        (Int8, Float64) => cast_numeric_arrays::<Int8Type, Float64Type>(array, cast_options),
1578
1579        (Int16, UInt8) => cast_numeric_arrays::<Int16Type, UInt8Type>(array, cast_options),
1580        (Int16, UInt16) => cast_numeric_arrays::<Int16Type, UInt16Type>(array, cast_options),
1581        (Int16, UInt32) => cast_numeric_arrays::<Int16Type, UInt32Type>(array, cast_options),
1582        (Int16, UInt64) => cast_numeric_arrays::<Int16Type, UInt64Type>(array, cast_options),
1583        (Int16, Int8) => cast_numeric_arrays::<Int16Type, Int8Type>(array, cast_options),
1584        (Int16, Int32) => cast_numeric_arrays::<Int16Type, Int32Type>(array, cast_options),
1585        (Int16, Int64) => cast_numeric_arrays::<Int16Type, Int64Type>(array, cast_options),
1586        (Int16, Float16) => cast_numeric_arrays::<Int16Type, Float16Type>(array, cast_options),
1587        (Int16, Float32) => cast_numeric_arrays::<Int16Type, Float32Type>(array, cast_options),
1588        (Int16, Float64) => cast_numeric_arrays::<Int16Type, Float64Type>(array, cast_options),
1589
1590        (Int32, UInt8) => cast_numeric_arrays::<Int32Type, UInt8Type>(array, cast_options),
1591        (Int32, UInt16) => cast_numeric_arrays::<Int32Type, UInt16Type>(array, cast_options),
1592        (Int32, UInt32) => cast_numeric_arrays::<Int32Type, UInt32Type>(array, cast_options),
1593        (Int32, UInt64) => cast_numeric_arrays::<Int32Type, UInt64Type>(array, cast_options),
1594        (Int32, Int8) => cast_numeric_arrays::<Int32Type, Int8Type>(array, cast_options),
1595        (Int32, Int16) => cast_numeric_arrays::<Int32Type, Int16Type>(array, cast_options),
1596        (Int32, Int64) => cast_numeric_arrays::<Int32Type, Int64Type>(array, cast_options),
1597        (Int32, Float16) => cast_numeric_arrays::<Int32Type, Float16Type>(array, cast_options),
1598        (Int32, Float32) => cast_numeric_arrays::<Int32Type, Float32Type>(array, cast_options),
1599        (Int32, Float64) => cast_numeric_arrays::<Int32Type, Float64Type>(array, cast_options),
1600
1601        (Int64, UInt8) => cast_numeric_arrays::<Int64Type, UInt8Type>(array, cast_options),
1602        (Int64, UInt16) => cast_numeric_arrays::<Int64Type, UInt16Type>(array, cast_options),
1603        (Int64, UInt32) => cast_numeric_arrays::<Int64Type, UInt32Type>(array, cast_options),
1604        (Int64, UInt64) => cast_numeric_arrays::<Int64Type, UInt64Type>(array, cast_options),
1605        (Int64, Int8) => cast_numeric_arrays::<Int64Type, Int8Type>(array, cast_options),
1606        (Int64, Int16) => cast_numeric_arrays::<Int64Type, Int16Type>(array, cast_options),
1607        (Int64, Int32) => cast_numeric_arrays::<Int64Type, Int32Type>(array, cast_options),
1608        (Int64, Float16) => cast_numeric_arrays::<Int64Type, Float16Type>(array, cast_options),
1609        (Int64, Float32) => cast_numeric_arrays::<Int64Type, Float32Type>(array, cast_options),
1610        (Int64, Float64) => cast_numeric_arrays::<Int64Type, Float64Type>(array, cast_options),
1611
1612        (Float16, UInt8) => cast_numeric_arrays::<Float16Type, UInt8Type>(array, cast_options),
1613        (Float16, UInt16) => cast_numeric_arrays::<Float16Type, UInt16Type>(array, cast_options),
1614        (Float16, UInt32) => cast_numeric_arrays::<Float16Type, UInt32Type>(array, cast_options),
1615        (Float16, UInt64) => cast_numeric_arrays::<Float16Type, UInt64Type>(array, cast_options),
1616        (Float16, Int8) => cast_numeric_arrays::<Float16Type, Int8Type>(array, cast_options),
1617        (Float16, Int16) => cast_numeric_arrays::<Float16Type, Int16Type>(array, cast_options),
1618        (Float16, Int32) => cast_numeric_arrays::<Float16Type, Int32Type>(array, cast_options),
1619        (Float16, Int64) => cast_numeric_arrays::<Float16Type, Int64Type>(array, cast_options),
1620        (Float16, Float32) => cast_numeric_arrays::<Float16Type, Float32Type>(array, cast_options),
1621        (Float16, Float64) => cast_numeric_arrays::<Float16Type, Float64Type>(array, cast_options),
1622
1623        (Float32, UInt8) => cast_numeric_arrays::<Float32Type, UInt8Type>(array, cast_options),
1624        (Float32, UInt16) => cast_numeric_arrays::<Float32Type, UInt16Type>(array, cast_options),
1625        (Float32, UInt32) => cast_numeric_arrays::<Float32Type, UInt32Type>(array, cast_options),
1626        (Float32, UInt64) => cast_numeric_arrays::<Float32Type, UInt64Type>(array, cast_options),
1627        (Float32, Int8) => cast_numeric_arrays::<Float32Type, Int8Type>(array, cast_options),
1628        (Float32, Int16) => cast_numeric_arrays::<Float32Type, Int16Type>(array, cast_options),
1629        (Float32, Int32) => cast_numeric_arrays::<Float32Type, Int32Type>(array, cast_options),
1630        (Float32, Int64) => cast_numeric_arrays::<Float32Type, Int64Type>(array, cast_options),
1631        (Float32, Float16) => cast_numeric_arrays::<Float32Type, Float16Type>(array, cast_options),
1632        (Float32, Float64) => cast_numeric_arrays::<Float32Type, Float64Type>(array, cast_options),
1633
1634        (Float64, UInt8) => cast_numeric_arrays::<Float64Type, UInt8Type>(array, cast_options),
1635        (Float64, UInt16) => cast_numeric_arrays::<Float64Type, UInt16Type>(array, cast_options),
1636        (Float64, UInt32) => cast_numeric_arrays::<Float64Type, UInt32Type>(array, cast_options),
1637        (Float64, UInt64) => cast_numeric_arrays::<Float64Type, UInt64Type>(array, cast_options),
1638        (Float64, Int8) => cast_numeric_arrays::<Float64Type, Int8Type>(array, cast_options),
1639        (Float64, Int16) => cast_numeric_arrays::<Float64Type, Int16Type>(array, cast_options),
1640        (Float64, Int32) => cast_numeric_arrays::<Float64Type, Int32Type>(array, cast_options),
1641        (Float64, Int64) => cast_numeric_arrays::<Float64Type, Int64Type>(array, cast_options),
1642        (Float64, Float16) => cast_numeric_arrays::<Float64Type, Float16Type>(array, cast_options),
1643        (Float64, Float32) => cast_numeric_arrays::<Float64Type, Float32Type>(array, cast_options),
1644        // end numeric casts
1645
1646        // temporal casts
1647        (Int32, Date32) => cast_reinterpret_arrays::<Int32Type, Date32Type>(array),
1648        (Int32, Date64) => cast_with_options(
1649            &cast_with_options(array, &Date32, cast_options)?,
1650            &Date64,
1651            cast_options,
1652        ),
1653        (Int32, Time32(TimeUnit::Second)) => {
1654            cast_reinterpret_arrays::<Int32Type, Time32SecondType>(array)
1655        }
1656        (Int32, Time32(TimeUnit::Millisecond)) => {
1657            cast_reinterpret_arrays::<Int32Type, Time32MillisecondType>(array)
1658        }
1659        // No support for microsecond/nanosecond with i32
1660        (Date32, Int32) => cast_reinterpret_arrays::<Date32Type, Int32Type>(array),
1661        (Date32, Int64) => cast_with_options(
1662            &cast_with_options(array, &Int32, cast_options)?,
1663            &Int64,
1664            cast_options,
1665        ),
1666        (Time32(TimeUnit::Second), Int32) => {
1667            cast_reinterpret_arrays::<Time32SecondType, Int32Type>(array)
1668        }
1669        (Time32(TimeUnit::Millisecond), Int32) => {
1670            cast_reinterpret_arrays::<Time32MillisecondType, Int32Type>(array)
1671        }
1672        (Time32(TimeUnit::Second), Int64) => cast_with_options(
1673            &cast_with_options(array, &Int32, cast_options)?,
1674            &Int64,
1675            cast_options,
1676        ),
1677        (Time32(TimeUnit::Millisecond), Int64) => cast_with_options(
1678            &cast_with_options(array, &Int32, cast_options)?,
1679            &Int64,
1680            cast_options,
1681        ),
1682        (Int64, Date64) => cast_reinterpret_arrays::<Int64Type, Date64Type>(array),
1683        (Int64, Date32) => cast_with_options(
1684            &cast_with_options(array, &Int32, cast_options)?,
1685            &Date32,
1686            cast_options,
1687        ),
1688        // No support for second/milliseconds with i64
1689        (Int64, Time64(TimeUnit::Microsecond)) => {
1690            cast_reinterpret_arrays::<Int64Type, Time64MicrosecondType>(array)
1691        }
1692        (Int64, Time64(TimeUnit::Nanosecond)) => {
1693            cast_reinterpret_arrays::<Int64Type, Time64NanosecondType>(array)
1694        }
1695
1696        (Date64, Int64) => cast_reinterpret_arrays::<Date64Type, Int64Type>(array),
1697        (Date64, Int32) => cast_with_options(
1698            &cast_with_options(array, &Int64, cast_options)?,
1699            &Int32,
1700            cast_options,
1701        ),
1702        (Time64(TimeUnit::Microsecond), Int64) => {
1703            cast_reinterpret_arrays::<Time64MicrosecondType, Int64Type>(array)
1704        }
1705        (Time64(TimeUnit::Nanosecond), Int64) => {
1706            cast_reinterpret_arrays::<Time64NanosecondType, Int64Type>(array)
1707        }
1708        (Date32, Date64) => Ok(Arc::new(
1709            array
1710                .as_primitive::<Date32Type>()
1711                .unary::<_, Date64Type>(|x| x as i64 * MILLISECONDS_IN_DAY),
1712        )),
1713        (Date64, Date32) => Ok(Arc::new(
1714            array
1715                .as_primitive::<Date64Type>()
1716                .unary::<_, Date32Type>(|x| (x / MILLISECONDS_IN_DAY) as i32),
1717        )),
1718
1719        (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1720            array
1721                .as_primitive::<Time32SecondType>()
1722                .unary::<_, Time32MillisecondType>(|x| x * MILLISECONDS as i32),
1723        )),
1724        (Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1725            array
1726                .as_primitive::<Time32SecondType>()
1727                .unary::<_, Time64MicrosecondType>(|x| x as i64 * MICROSECONDS),
1728        )),
1729        (Time32(TimeUnit::Second), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1730            array
1731                .as_primitive::<Time32SecondType>()
1732                .unary::<_, Time64NanosecondType>(|x| x as i64 * NANOSECONDS),
1733        )),
1734
1735        (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1736            array
1737                .as_primitive::<Time32MillisecondType>()
1738                .unary::<_, Time32SecondType>(|x| x / MILLISECONDS as i32),
1739        )),
1740        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1741            array
1742                .as_primitive::<Time32MillisecondType>()
1743                .unary::<_, Time64MicrosecondType>(|x| x as i64 * (MICROSECONDS / MILLISECONDS)),
1744        )),
1745        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1746            array
1747                .as_primitive::<Time32MillisecondType>()
1748                .unary::<_, Time64NanosecondType>(|x| x as i64 * (NANOSECONDS / MILLISECONDS)),
1749        )),
1750
1751        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1752            array
1753                .as_primitive::<Time64MicrosecondType>()
1754                .unary::<_, Time32SecondType>(|x| (x / MICROSECONDS) as i32),
1755        )),
1756        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1757            array
1758                .as_primitive::<Time64MicrosecondType>()
1759                .unary::<_, Time32MillisecondType>(|x| (x / (MICROSECONDS / MILLISECONDS)) as i32),
1760        )),
1761        (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1762            array
1763                .as_primitive::<Time64MicrosecondType>()
1764                .unary::<_, Time64NanosecondType>(|x| x * (NANOSECONDS / MICROSECONDS)),
1765        )),
1766
1767        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1768            array
1769                .as_primitive::<Time64NanosecondType>()
1770                .unary::<_, Time32SecondType>(|x| (x / NANOSECONDS) as i32),
1771        )),
1772        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1773            array
1774                .as_primitive::<Time64NanosecondType>()
1775                .unary::<_, Time32MillisecondType>(|x| (x / (NANOSECONDS / MILLISECONDS)) as i32),
1776        )),
1777        (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1778            array
1779                .as_primitive::<Time64NanosecondType>()
1780                .unary::<_, Time64MicrosecondType>(|x| x / (NANOSECONDS / MICROSECONDS)),
1781        )),
1782
1783        // Timestamp to integer/floating/decimals
1784        (Timestamp(TimeUnit::Second, _), _) if to_type.is_numeric() => {
1785            let array = cast_reinterpret_arrays::<TimestampSecondType, Int64Type>(array)?;
1786            cast_with_options(&array, to_type, cast_options)
1787        }
1788        (Timestamp(TimeUnit::Millisecond, _), _) if to_type.is_numeric() => {
1789            let array = cast_reinterpret_arrays::<TimestampMillisecondType, Int64Type>(array)?;
1790            cast_with_options(&array, to_type, cast_options)
1791        }
1792        (Timestamp(TimeUnit::Microsecond, _), _) if to_type.is_numeric() => {
1793            let array = cast_reinterpret_arrays::<TimestampMicrosecondType, Int64Type>(array)?;
1794            cast_with_options(&array, to_type, cast_options)
1795        }
1796        (Timestamp(TimeUnit::Nanosecond, _), _) if to_type.is_numeric() => {
1797            let array = cast_reinterpret_arrays::<TimestampNanosecondType, Int64Type>(array)?;
1798            cast_with_options(&array, to_type, cast_options)
1799        }
1800
1801        (_, Timestamp(unit, tz)) if from_type.is_numeric() => {
1802            let array = cast_with_options(array, &Int64, cast_options)?;
1803            Ok(make_timestamp_array(
1804                array.as_primitive(),
1805                *unit,
1806                tz.clone(),
1807            ))
1808        }
1809
1810        (Timestamp(from_unit, from_tz), Timestamp(to_unit, to_tz)) => {
1811            let array = cast_with_options(array, &Int64, cast_options)?;
1812            let time_array = array.as_primitive::<Int64Type>();
1813            let from_size = time_unit_multiple(from_unit);
1814            let to_size = time_unit_multiple(to_unit);
1815            // we either divide or multiply, depending on size of each unit
1816            // units are never the same when the types are the same
1817            let converted = match from_size.cmp(&to_size) {
1818                Ordering::Greater => {
1819                    let divisor = from_size / to_size;
1820                    time_array.unary::<_, Int64Type>(|o| o / divisor)
1821                }
1822                Ordering::Equal => time_array.clone(),
1823                Ordering::Less => {
1824                    let mul = to_size / from_size;
1825                    if cast_options.safe {
1826                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1827                    } else {
1828                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1829                    }
1830                }
1831            };
1832            // Normalize timezone
1833            let adjusted = match (from_tz, to_tz) {
1834                // Only this case needs to be adjusted because we're casting from
1835                // unknown time offset to some time offset, we want the time to be
1836                // unchanged.
1837                //
1838                // i.e. Timestamp('2001-01-01T00:00', None) -> Timestamp('2001-01-01T00:00', '+0700')
1839                (None, Some(to_tz)) => {
1840                    let to_tz: Tz = to_tz.parse()?;
1841                    match to_unit {
1842                        TimeUnit::Second => adjust_timestamp_to_timezone::<TimestampSecondType>(
1843                            converted,
1844                            &to_tz,
1845                            cast_options,
1846                        )?,
1847                        TimeUnit::Millisecond => adjust_timestamp_to_timezone::<
1848                            TimestampMillisecondType,
1849                        >(
1850                            converted, &to_tz, cast_options
1851                        )?,
1852                        TimeUnit::Microsecond => adjust_timestamp_to_timezone::<
1853                            TimestampMicrosecondType,
1854                        >(
1855                            converted, &to_tz, cast_options
1856                        )?,
1857                        TimeUnit::Nanosecond => adjust_timestamp_to_timezone::<
1858                            TimestampNanosecondType,
1859                        >(
1860                            converted, &to_tz, cast_options
1861                        )?,
1862                    }
1863                }
1864                _ => converted,
1865            };
1866            Ok(make_timestamp_array(&adjusted, *to_unit, to_tz.clone()))
1867        }
1868        (Timestamp(TimeUnit::Microsecond, _), Date32) => {
1869            timestamp_to_date32(array.as_primitive::<TimestampMicrosecondType>())
1870        }
1871        (Timestamp(TimeUnit::Millisecond, _), Date32) => {
1872            timestamp_to_date32(array.as_primitive::<TimestampMillisecondType>())
1873        }
1874        (Timestamp(TimeUnit::Second, _), Date32) => {
1875            timestamp_to_date32(array.as_primitive::<TimestampSecondType>())
1876        }
1877        (Timestamp(TimeUnit::Nanosecond, _), Date32) => {
1878            timestamp_to_date32(array.as_primitive::<TimestampNanosecondType>())
1879        }
1880        (Timestamp(TimeUnit::Second, _), Date64) => Ok(Arc::new(match cast_options.safe {
1881            true => {
1882                // change error to None
1883                array
1884                    .as_primitive::<TimestampSecondType>()
1885                    .unary_opt::<_, Date64Type>(|x| x.checked_mul(MILLISECONDS))
1886            }
1887            false => array
1888                .as_primitive::<TimestampSecondType>()
1889                .try_unary::<_, Date64Type, _>(|x| x.mul_checked(MILLISECONDS))?,
1890        })),
1891        (Timestamp(TimeUnit::Millisecond, _), Date64) => {
1892            cast_reinterpret_arrays::<TimestampMillisecondType, Date64Type>(array)
1893        }
1894        (Timestamp(TimeUnit::Microsecond, _), Date64) => Ok(Arc::new(
1895            array
1896                .as_primitive::<TimestampMicrosecondType>()
1897                .unary::<_, Date64Type>(|x| x / (MICROSECONDS / MILLISECONDS)),
1898        )),
1899        (Timestamp(TimeUnit::Nanosecond, _), Date64) => Ok(Arc::new(
1900            array
1901                .as_primitive::<TimestampNanosecondType>()
1902                .unary::<_, Date64Type>(|x| x / (NANOSECONDS / MILLISECONDS)),
1903        )),
1904        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Microsecond)) => {
1905            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1906            Ok(Arc::new(
1907                array
1908                    .as_primitive::<TimestampSecondType>()
1909                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1910                        Ok(time_to_time64us(as_time_res_with_timezone::<
1911                            TimestampSecondType,
1912                        >(x, tz)?))
1913                    })?,
1914            ))
1915        }
1916        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Nanosecond)) => {
1917            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1918            Ok(Arc::new(
1919                array
1920                    .as_primitive::<TimestampSecondType>()
1921                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1922                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1923                            TimestampSecondType,
1924                        >(x, tz)?))
1925                    })?,
1926            ))
1927        }
1928        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Microsecond)) => {
1929            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1930            Ok(Arc::new(
1931                array
1932                    .as_primitive::<TimestampMillisecondType>()
1933                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1934                        Ok(time_to_time64us(as_time_res_with_timezone::<
1935                            TimestampMillisecondType,
1936                        >(x, tz)?))
1937                    })?,
1938            ))
1939        }
1940        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Nanosecond)) => {
1941            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1942            Ok(Arc::new(
1943                array
1944                    .as_primitive::<TimestampMillisecondType>()
1945                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1946                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1947                            TimestampMillisecondType,
1948                        >(x, tz)?))
1949                    })?,
1950            ))
1951        }
1952        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Microsecond)) => {
1953            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1954            Ok(Arc::new(
1955                array
1956                    .as_primitive::<TimestampMicrosecondType>()
1957                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1958                        Ok(time_to_time64us(as_time_res_with_timezone::<
1959                            TimestampMicrosecondType,
1960                        >(x, tz)?))
1961                    })?,
1962            ))
1963        }
1964        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Nanosecond)) => {
1965            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1966            Ok(Arc::new(
1967                array
1968                    .as_primitive::<TimestampMicrosecondType>()
1969                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1970                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1971                            TimestampMicrosecondType,
1972                        >(x, tz)?))
1973                    })?,
1974            ))
1975        }
1976        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Microsecond)) => {
1977            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1978            Ok(Arc::new(
1979                array
1980                    .as_primitive::<TimestampNanosecondType>()
1981                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1982                        Ok(time_to_time64us(as_time_res_with_timezone::<
1983                            TimestampNanosecondType,
1984                        >(x, tz)?))
1985                    })?,
1986            ))
1987        }
1988        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Nanosecond)) => {
1989            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1990            Ok(Arc::new(
1991                array
1992                    .as_primitive::<TimestampNanosecondType>()
1993                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1994                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1995                            TimestampNanosecondType,
1996                        >(x, tz)?))
1997                    })?,
1998            ))
1999        }
2000        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Second)) => {
2001            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2002            Ok(Arc::new(
2003                array
2004                    .as_primitive::<TimestampSecondType>()
2005                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2006                        Ok(time_to_time32s(as_time_res_with_timezone::<
2007                            TimestampSecondType,
2008                        >(x, tz)?))
2009                    })?,
2010            ))
2011        }
2012        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Millisecond)) => {
2013            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2014            Ok(Arc::new(
2015                array
2016                    .as_primitive::<TimestampSecondType>()
2017                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2018                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2019                            TimestampSecondType,
2020                        >(x, tz)?))
2021                    })?,
2022            ))
2023        }
2024        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Second)) => {
2025            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2026            Ok(Arc::new(
2027                array
2028                    .as_primitive::<TimestampMillisecondType>()
2029                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2030                        Ok(time_to_time32s(as_time_res_with_timezone::<
2031                            TimestampMillisecondType,
2032                        >(x, tz)?))
2033                    })?,
2034            ))
2035        }
2036        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Millisecond)) => {
2037            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2038            Ok(Arc::new(
2039                array
2040                    .as_primitive::<TimestampMillisecondType>()
2041                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2042                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2043                            TimestampMillisecondType,
2044                        >(x, tz)?))
2045                    })?,
2046            ))
2047        }
2048        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Second)) => {
2049            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2050            Ok(Arc::new(
2051                array
2052                    .as_primitive::<TimestampMicrosecondType>()
2053                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2054                        Ok(time_to_time32s(as_time_res_with_timezone::<
2055                            TimestampMicrosecondType,
2056                        >(x, tz)?))
2057                    })?,
2058            ))
2059        }
2060        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Millisecond)) => {
2061            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2062            Ok(Arc::new(
2063                array
2064                    .as_primitive::<TimestampMicrosecondType>()
2065                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2066                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2067                            TimestampMicrosecondType,
2068                        >(x, tz)?))
2069                    })?,
2070            ))
2071        }
2072        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Second)) => {
2073            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2074            Ok(Arc::new(
2075                array
2076                    .as_primitive::<TimestampNanosecondType>()
2077                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2078                        Ok(time_to_time32s(as_time_res_with_timezone::<
2079                            TimestampNanosecondType,
2080                        >(x, tz)?))
2081                    })?,
2082            ))
2083        }
2084        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Millisecond)) => {
2085            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2086            Ok(Arc::new(
2087                array
2088                    .as_primitive::<TimestampNanosecondType>()
2089                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2090                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2091                            TimestampNanosecondType,
2092                        >(x, tz)?))
2093                    })?,
2094            ))
2095        }
2096        (Date64, Timestamp(TimeUnit::Second, _)) => {
2097            let array = array
2098                .as_primitive::<Date64Type>()
2099                .unary::<_, TimestampSecondType>(|x| x / MILLISECONDS);
2100
2101            cast_with_options(&array, to_type, cast_options)
2102        }
2103        (Date64, Timestamp(TimeUnit::Millisecond, _)) => {
2104            let array = array
2105                .as_primitive::<Date64Type>()
2106                .reinterpret_cast::<TimestampMillisecondType>();
2107
2108            cast_with_options(&array, to_type, cast_options)
2109        }
2110
2111        (Date64, Timestamp(TimeUnit::Microsecond, _)) => {
2112            let array = array
2113                .as_primitive::<Date64Type>()
2114                .unary::<_, TimestampMicrosecondType>(|x| x * (MICROSECONDS / MILLISECONDS));
2115
2116            cast_with_options(&array, to_type, cast_options)
2117        }
2118        (Date64, Timestamp(TimeUnit::Nanosecond, _)) => {
2119            let array = array
2120                .as_primitive::<Date64Type>()
2121                .unary::<_, TimestampNanosecondType>(|x| x * (NANOSECONDS / MILLISECONDS));
2122
2123            cast_with_options(&array, to_type, cast_options)
2124        }
2125        (Date32, Timestamp(TimeUnit::Second, _)) => {
2126            let array = array
2127                .as_primitive::<Date32Type>()
2128                .unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY);
2129
2130            cast_with_options(&array, to_type, cast_options)
2131        }
2132        (Date32, Timestamp(TimeUnit::Millisecond, _)) => {
2133            let array = array
2134                .as_primitive::<Date32Type>()
2135                .unary::<_, TimestampMillisecondType>(|x| (x as i64) * MILLISECONDS_IN_DAY);
2136
2137            cast_with_options(&array, to_type, cast_options)
2138        }
2139        (Date32, Timestamp(TimeUnit::Microsecond, _)) => {
2140            let array = array
2141                .as_primitive::<Date32Type>()
2142                .unary::<_, TimestampMicrosecondType>(|x| (x as i64) * MICROSECONDS_IN_DAY);
2143
2144            cast_with_options(&array, to_type, cast_options)
2145        }
2146        (Date32, Timestamp(TimeUnit::Nanosecond, _)) => {
2147            let array = array
2148                .as_primitive::<Date32Type>()
2149                .unary::<_, TimestampNanosecondType>(|x| (x as i64) * NANOSECONDS_IN_DAY);
2150
2151            cast_with_options(&array, to_type, cast_options)
2152        }
2153
2154        (_, Duration(unit)) if from_type.is_numeric() => {
2155            let array = cast_with_options(array, &Int64, cast_options)?;
2156            Ok(make_duration_array(array.as_primitive(), *unit))
2157        }
2158        (Duration(TimeUnit::Second), _) if to_type.is_numeric() => {
2159            let array = cast_reinterpret_arrays::<DurationSecondType, Int64Type>(array)?;
2160            cast_with_options(&array, to_type, cast_options)
2161        }
2162        (Duration(TimeUnit::Millisecond), _) if to_type.is_numeric() => {
2163            let array = cast_reinterpret_arrays::<DurationMillisecondType, Int64Type>(array)?;
2164            cast_with_options(&array, to_type, cast_options)
2165        }
2166        (Duration(TimeUnit::Microsecond), _) if to_type.is_numeric() => {
2167            let array = cast_reinterpret_arrays::<DurationMicrosecondType, Int64Type>(array)?;
2168            cast_with_options(&array, to_type, cast_options)
2169        }
2170        (Duration(TimeUnit::Nanosecond), _) if to_type.is_numeric() => {
2171            let array = cast_reinterpret_arrays::<DurationNanosecondType, Int64Type>(array)?;
2172            cast_with_options(&array, to_type, cast_options)
2173        }
2174
2175        (Duration(from_unit), Duration(to_unit)) => {
2176            let array = cast_with_options(array, &Int64, cast_options)?;
2177            let time_array = array.as_primitive::<Int64Type>();
2178            let from_size = time_unit_multiple(from_unit);
2179            let to_size = time_unit_multiple(to_unit);
2180            // we either divide or multiply, depending on size of each unit
2181            // units are never the same when the types are the same
2182            let converted = match from_size.cmp(&to_size) {
2183                Ordering::Greater => {
2184                    let divisor = from_size / to_size;
2185                    time_array.unary::<_, Int64Type>(|o| o / divisor)
2186                }
2187                Ordering::Equal => time_array.clone(),
2188                Ordering::Less => {
2189                    let mul = to_size / from_size;
2190                    if cast_options.safe {
2191                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
2192                    } else {
2193                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
2194                    }
2195                }
2196            };
2197            Ok(make_duration_array(&converted, *to_unit))
2198        }
2199
2200        (Duration(TimeUnit::Second), Interval(IntervalUnit::MonthDayNano)) => {
2201            cast_duration_to_interval::<DurationSecondType>(array, cast_options)
2202        }
2203        (Duration(TimeUnit::Millisecond), Interval(IntervalUnit::MonthDayNano)) => {
2204            cast_duration_to_interval::<DurationMillisecondType>(array, cast_options)
2205        }
2206        (Duration(TimeUnit::Microsecond), Interval(IntervalUnit::MonthDayNano)) => {
2207            cast_duration_to_interval::<DurationMicrosecondType>(array, cast_options)
2208        }
2209        (Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano)) => {
2210            cast_duration_to_interval::<DurationNanosecondType>(array, cast_options)
2211        }
2212        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Second)) => {
2213            cast_month_day_nano_to_duration::<DurationSecondType>(array, cast_options)
2214        }
2215        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Millisecond)) => {
2216            cast_month_day_nano_to_duration::<DurationMillisecondType>(array, cast_options)
2217        }
2218        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Microsecond)) => {
2219            cast_month_day_nano_to_duration::<DurationMicrosecondType>(array, cast_options)
2220        }
2221        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Nanosecond)) => {
2222            cast_month_day_nano_to_duration::<DurationNanosecondType>(array, cast_options)
2223        }
2224        (Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
2225            cast_interval_year_month_to_interval_month_day_nano(array, cast_options)
2226        }
2227        (Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
2228            cast_interval_day_time_to_interval_month_day_nano(array, cast_options)
2229        }
2230        (Int32, Interval(IntervalUnit::YearMonth)) => {
2231            cast_reinterpret_arrays::<Int32Type, IntervalYearMonthType>(array)
2232        }
2233        (_, _) => Err(ArrowError::CastError(format!(
2234            "Casting from {from_type} to {to_type} not supported",
2235        ))),
2236    }
2237}
2238
2239fn cast_struct_to_struct(
2240    array: &StructArray,
2241    from_fields: Fields,
2242    to_fields: Fields,
2243    cast_options: &CastOptions,
2244) -> Result<ArrayRef, ArrowError> {
2245    // Fast path: if field names are in the same order, we can just zip and cast
2246    let fields_match_order = from_fields.len() == to_fields.len()
2247        && from_fields
2248            .iter()
2249            .zip(to_fields.iter())
2250            .all(|(f1, f2)| f1.name() == f2.name());
2251
2252    let fields = if fields_match_order {
2253        // Fast path: cast columns in order if their names match
2254        cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2255    } else {
2256        let all_fields_match_by_name = to_fields.iter().all(|to_field| {
2257            from_fields
2258                .iter()
2259                .any(|from_field| from_field.name() == to_field.name())
2260        });
2261
2262        if all_fields_match_by_name {
2263            // Slow path: match fields by name and reorder
2264            cast_struct_fields_by_name(array, from_fields.clone(), to_fields.clone(), cast_options)?
2265        } else {
2266            // Fallback: cast field by field in order
2267            cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2268        }
2269    };
2270
2271    let array = StructArray::try_new(to_fields.clone(), fields, array.nulls().cloned())?;
2272    Ok(Arc::new(array) as ArrayRef)
2273}
2274
2275fn cast_struct_fields_by_name(
2276    array: &StructArray,
2277    from_fields: Fields,
2278    to_fields: Fields,
2279    cast_options: &CastOptions,
2280) -> Result<Vec<ArrayRef>, ArrowError> {
2281    to_fields
2282        .iter()
2283        .map(|to_field| {
2284            let from_field_idx = from_fields
2285                .iter()
2286                .position(|from_field| from_field.name() == to_field.name())
2287                .unwrap(); // safe because we checked above
2288            let column = array.column(from_field_idx);
2289            cast_with_options(column, to_field.data_type(), cast_options)
2290        })
2291        .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2292}
2293
2294fn cast_struct_fields_in_order(
2295    array: &StructArray,
2296    to_fields: Fields,
2297    cast_options: &CastOptions,
2298) -> Result<Vec<ArrayRef>, ArrowError> {
2299    array
2300        .columns()
2301        .iter()
2302        .zip(to_fields.iter())
2303        .map(|(l, field)| cast_with_options(l, field.data_type(), cast_options))
2304        .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2305}
2306
2307fn cast_from_decimal<D, F>(
2308    array: &dyn Array,
2309    base: D::Native,
2310    scale: &i8,
2311    from_type: &DataType,
2312    to_type: &DataType,
2313    as_float: F,
2314    cast_options: &CastOptions,
2315) -> Result<ArrayRef, ArrowError>
2316where
2317    D: DecimalType + ArrowPrimitiveType,
2318    <D as ArrowPrimitiveType>::Native: ToPrimitive,
2319    F: Fn(D::Native) -> f64,
2320{
2321    use DataType::*;
2322    // cast decimal to other type
2323    match to_type {
2324        UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, base, *scale, cast_options),
2325        UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, base, *scale, cast_options),
2326        UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, base, *scale, cast_options),
2327        UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, base, *scale, cast_options),
2328        Int8 => cast_decimal_to_integer::<D, Int8Type>(array, base, *scale, cast_options),
2329        Int16 => cast_decimal_to_integer::<D, Int16Type>(array, base, *scale, cast_options),
2330        Int32 => cast_decimal_to_integer::<D, Int32Type>(array, base, *scale, cast_options),
2331        Int64 => cast_decimal_to_integer::<D, Int64Type>(array, base, *scale, cast_options),
2332        Float16 => cast_decimal_to_float::<D, Float16Type, _>(array, |x| {
2333            half::f16::from_f64(single_decimal_to_float_lossy::<D, F>(
2334                &as_float,
2335                x,
2336                <i32 as From<i8>>::from(*scale),
2337            ))
2338        }),
2339        Float32 => cast_decimal_to_float::<D, Float32Type, _>(array, |x| {
2340            single_decimal_to_float_lossy::<D, F>(&as_float, x, <i32 as From<i8>>::from(*scale))
2341                as f32
2342        }),
2343        Float64 => cast_decimal_to_float::<D, Float64Type, _>(array, |x| {
2344            single_decimal_to_float_lossy::<D, F>(&as_float, x, <i32 as From<i8>>::from(*scale))
2345        }),
2346        Utf8View => value_to_string_view(array, cast_options),
2347        Utf8 => value_to_string::<i32>(array, cast_options),
2348        LargeUtf8 => value_to_string::<i64>(array, cast_options),
2349        Null => Ok(new_null_array(to_type, array.len())),
2350        _ => Err(ArrowError::CastError(format!(
2351            "Casting from {from_type} to {to_type} not supported"
2352        ))),
2353    }
2354}
2355
2356fn cast_to_decimal<D, M>(
2357    array: &dyn Array,
2358    base: M,
2359    precision: &u8,
2360    scale: &i8,
2361    from_type: &DataType,
2362    to_type: &DataType,
2363    cast_options: &CastOptions,
2364) -> Result<ArrayRef, ArrowError>
2365where
2366    D: DecimalType + ArrowPrimitiveType<Native = M>,
2367    M: ArrowNativeTypeOp + DecimalCast,
2368    u8: num_traits::AsPrimitive<M>,
2369    u16: num_traits::AsPrimitive<M>,
2370    u32: num_traits::AsPrimitive<M>,
2371    u64: num_traits::AsPrimitive<M>,
2372    i8: num_traits::AsPrimitive<M>,
2373    i16: num_traits::AsPrimitive<M>,
2374    i32: num_traits::AsPrimitive<M>,
2375    i64: num_traits::AsPrimitive<M>,
2376{
2377    use DataType::*;
2378    // cast data to decimal
2379    match from_type {
2380        UInt8 => cast_integer_to_decimal::<_, D, M>(
2381            array.as_primitive::<UInt8Type>(),
2382            *precision,
2383            *scale,
2384            base,
2385            cast_options,
2386        ),
2387        UInt16 => cast_integer_to_decimal::<_, D, _>(
2388            array.as_primitive::<UInt16Type>(),
2389            *precision,
2390            *scale,
2391            base,
2392            cast_options,
2393        ),
2394        UInt32 => cast_integer_to_decimal::<_, D, _>(
2395            array.as_primitive::<UInt32Type>(),
2396            *precision,
2397            *scale,
2398            base,
2399            cast_options,
2400        ),
2401        UInt64 => cast_integer_to_decimal::<_, D, _>(
2402            array.as_primitive::<UInt64Type>(),
2403            *precision,
2404            *scale,
2405            base,
2406            cast_options,
2407        ),
2408        Int8 => cast_integer_to_decimal::<_, D, _>(
2409            array.as_primitive::<Int8Type>(),
2410            *precision,
2411            *scale,
2412            base,
2413            cast_options,
2414        ),
2415        Int16 => cast_integer_to_decimal::<_, D, _>(
2416            array.as_primitive::<Int16Type>(),
2417            *precision,
2418            *scale,
2419            base,
2420            cast_options,
2421        ),
2422        Int32 => cast_integer_to_decimal::<_, D, _>(
2423            array.as_primitive::<Int32Type>(),
2424            *precision,
2425            *scale,
2426            base,
2427            cast_options,
2428        ),
2429        Int64 => cast_integer_to_decimal::<_, D, _>(
2430            array.as_primitive::<Int64Type>(),
2431            *precision,
2432            *scale,
2433            base,
2434            cast_options,
2435        ),
2436        Float16 => cast_floating_point_to_decimal::<_, D>(
2437            array.as_primitive::<Float16Type>(),
2438            *precision,
2439            *scale,
2440            cast_options,
2441        ),
2442        Float32 => cast_floating_point_to_decimal::<_, D>(
2443            array.as_primitive::<Float32Type>(),
2444            *precision,
2445            *scale,
2446            cast_options,
2447        ),
2448        Float64 => cast_floating_point_to_decimal::<_, D>(
2449            array.as_primitive::<Float64Type>(),
2450            *precision,
2451            *scale,
2452            cast_options,
2453        ),
2454        Utf8View | Utf8 => {
2455            cast_string_to_decimal::<D, i32>(array, *precision, *scale, cast_options)
2456        }
2457        LargeUtf8 => cast_string_to_decimal::<D, i64>(array, *precision, *scale, cast_options),
2458        Null => Ok(new_null_array(to_type, array.len())),
2459        _ => Err(ArrowError::CastError(format!(
2460            "Casting from {from_type} to {to_type} not supported"
2461        ))),
2462    }
2463}
2464
2465/// Get the time unit as a multiple of a second
2466const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
2467    match unit {
2468        TimeUnit::Second => 1,
2469        TimeUnit::Millisecond => MILLISECONDS,
2470        TimeUnit::Microsecond => MICROSECONDS,
2471        TimeUnit::Nanosecond => NANOSECONDS,
2472    }
2473}
2474
2475/// Convert Array into a PrimitiveArray of type, and apply numeric cast
2476fn cast_numeric_arrays<FROM, TO>(
2477    from: &dyn Array,
2478    cast_options: &CastOptions,
2479) -> Result<ArrayRef, ArrowError>
2480where
2481    FROM: ArrowPrimitiveType,
2482    TO: ArrowPrimitiveType,
2483    FROM::Native: NumCast,
2484    TO::Native: NumCast,
2485{
2486    if cast_options.safe {
2487        // If the value can't be casted to the `TO::Native`, return null
2488        Ok(Arc::new(numeric_cast::<FROM, TO>(
2489            from.as_primitive::<FROM>(),
2490        )))
2491    } else {
2492        // If the value can't be casted to the `TO::Native`, return error
2493        Ok(Arc::new(try_numeric_cast::<FROM, TO>(
2494            from.as_primitive::<FROM>(),
2495        )?))
2496    }
2497}
2498
2499// Natural cast between numeric types
2500// If the value of T can't be casted to R, will throw error
2501fn try_numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>, ArrowError>
2502where
2503    T: ArrowPrimitiveType,
2504    R: ArrowPrimitiveType,
2505    T::Native: NumCast,
2506    R::Native: NumCast,
2507{
2508    from.try_unary(|value| {
2509        num_cast::<T::Native, R::Native>(value).ok_or_else(|| {
2510            ArrowError::CastError(format!(
2511                "Can't cast value {:?} to type {}",
2512                value,
2513                R::DATA_TYPE
2514            ))
2515        })
2516    })
2517}
2518
2519/// Natural cast between numeric types
2520/// Return None if the input `value` can't be casted to type `O`.
2521#[inline]
2522pub fn num_cast<I, O>(value: I) -> Option<O>
2523where
2524    I: NumCast,
2525    O: NumCast,
2526{
2527    num_traits::cast::cast::<I, O>(value)
2528}
2529
2530// Natural cast between numeric types
2531// If the value of T can't be casted to R, it will be converted to null
2532fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> PrimitiveArray<R>
2533where
2534    T: ArrowPrimitiveType,
2535    R: ArrowPrimitiveType,
2536    T::Native: NumCast,
2537    R::Native: NumCast,
2538{
2539    from.unary_opt::<_, R>(num_cast::<T::Native, R::Native>)
2540}
2541
2542fn cast_numeric_to_binary<FROM: ArrowPrimitiveType, O: OffsetSizeTrait>(
2543    array: &dyn Array,
2544) -> Result<ArrayRef, ArrowError> {
2545    let array = array.as_primitive::<FROM>();
2546    let size = std::mem::size_of::<FROM::Native>();
2547    let offsets = OffsetBuffer::from_repeated_length(size, array.len());
2548    Ok(Arc::new(GenericBinaryArray::<O>::try_new(
2549        offsets,
2550        array.values().inner().clone(),
2551        array.nulls().cloned(),
2552    )?))
2553}
2554
2555fn adjust_timestamp_to_timezone<T: ArrowTimestampType>(
2556    array: PrimitiveArray<Int64Type>,
2557    to_tz: &Tz,
2558    cast_options: &CastOptions,
2559) -> Result<PrimitiveArray<Int64Type>, ArrowError> {
2560    let adjust = |o| {
2561        let local = as_datetime::<T>(o)?;
2562        let offset = to_tz.offset_from_local_datetime(&local).single()?;
2563        T::from_naive_datetime(local - offset.fix(), None)
2564    };
2565    let adjusted = if cast_options.safe {
2566        array.unary_opt::<_, Int64Type>(adjust)
2567    } else {
2568        array.try_unary::<_, Int64Type, _>(|o| {
2569            adjust(o).ok_or_else(|| {
2570                ArrowError::CastError("Cannot cast timezone to different timezone".to_string())
2571            })
2572        })?
2573    };
2574    Ok(adjusted)
2575}
2576
2577/// Cast numeric types to Boolean
2578///
2579/// Any zero value returns `false` while non-zero returns `true`
2580fn cast_numeric_to_bool<FROM>(from: &dyn Array) -> Result<ArrayRef, ArrowError>
2581where
2582    FROM: ArrowPrimitiveType,
2583{
2584    numeric_to_bool_cast::<FROM>(from.as_primitive::<FROM>()).map(|to| Arc::new(to) as ArrayRef)
2585}
2586
2587fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray, ArrowError>
2588where
2589    T: ArrowPrimitiveType + ArrowPrimitiveType,
2590{
2591    let mut b = BooleanBuilder::with_capacity(from.len());
2592
2593    for i in 0..from.len() {
2594        if from.is_null(i) {
2595            b.append_null();
2596        } else {
2597            b.append_value(cast_num_to_bool::<T::Native>(from.value(i)));
2598        }
2599    }
2600
2601    Ok(b.finish())
2602}
2603
2604/// Cast numeric types to boolean
2605#[inline]
2606pub fn cast_num_to_bool<I>(value: I) -> bool
2607where
2608    I: Default + PartialEq,
2609{
2610    value != I::default()
2611}
2612
2613/// Cast Boolean types to numeric
2614///
2615/// `false` returns 0 while `true` returns 1
2616fn cast_bool_to_numeric<TO>(
2617    from: &dyn Array,
2618    cast_options: &CastOptions,
2619) -> Result<ArrayRef, ArrowError>
2620where
2621    TO: ArrowPrimitiveType,
2622    TO::Native: num_traits::cast::NumCast,
2623{
2624    Ok(Arc::new(bool_to_numeric_cast::<TO>(
2625        from.as_any().downcast_ref::<BooleanArray>().unwrap(),
2626        cast_options,
2627    )))
2628}
2629
2630fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions) -> PrimitiveArray<T>
2631where
2632    T: ArrowPrimitiveType,
2633    T::Native: num_traits::NumCast,
2634{
2635    let iter = (0..from.len()).map(|i| {
2636        if from.is_null(i) {
2637            None
2638        } else {
2639            single_bool_to_numeric::<T::Native>(from.value(i))
2640        }
2641    });
2642    // Benefit:
2643    //     20% performance improvement
2644    // Soundness:
2645    //     The iterator is trustedLen because it comes from a Range
2646    unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
2647}
2648
2649/// Cast single bool value to numeric value.
2650#[inline]
2651pub fn single_bool_to_numeric<O>(value: bool) -> Option<O>
2652where
2653    O: num_traits::NumCast + Default,
2654{
2655    if value {
2656        // a workaround to cast a primitive to type O, infallible
2657        num_traits::cast::cast(1)
2658    } else {
2659        Some(O::default())
2660    }
2661}
2662
2663/// Helper function to cast from one `BinaryArray` or 'LargeBinaryArray' to 'FixedSizeBinaryArray'.
2664fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
2665    array: &dyn Array,
2666    byte_width: i32,
2667    cast_options: &CastOptions,
2668) -> Result<ArrayRef, ArrowError> {
2669    let array = array.as_binary::<O>();
2670    let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(), byte_width);
2671
2672    for i in 0..array.len() {
2673        if array.is_null(i) {
2674            builder.append_null();
2675        } else {
2676            match builder.append_value(array.value(i)) {
2677                Ok(_) => {}
2678                Err(e) => match cast_options.safe {
2679                    true => builder.append_null(),
2680                    false => return Err(e),
2681                },
2682            }
2683        }
2684    }
2685
2686    Ok(Arc::new(builder.finish()))
2687}
2688
2689/// Helper function to cast from 'FixedSizeBinaryArray' to one `BinaryArray` or 'LargeBinaryArray'.
2690/// If the target one is too large for the source array it will return an Error.
2691fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
2692    array: &dyn Array,
2693    byte_width: i32,
2694) -> Result<ArrayRef, ArrowError> {
2695    let array = array
2696        .as_any()
2697        .downcast_ref::<FixedSizeBinaryArray>()
2698        .unwrap();
2699
2700    let offsets: i128 = byte_width as i128 * array.len() as i128;
2701
2702    let is_binary = matches!(GenericBinaryType::<O>::DATA_TYPE, DataType::Binary);
2703    if is_binary && offsets > i32::MAX as i128 {
2704        return Err(ArrowError::ComputeError(
2705            "FixedSizeBinary array too large to cast to Binary array".to_string(),
2706        ));
2707    } else if !is_binary && offsets > i64::MAX as i128 {
2708        return Err(ArrowError::ComputeError(
2709            "FixedSizeBinary array too large to cast to LargeBinary array".to_string(),
2710        ));
2711    }
2712
2713    let mut builder = GenericBinaryBuilder::<O>::with_capacity(array.len(), array.len());
2714
2715    for i in 0..array.len() {
2716        if array.is_null(i) {
2717            builder.append_null();
2718        } else {
2719            builder.append_value(array.value(i));
2720        }
2721    }
2722
2723    Ok(Arc::new(builder.finish()))
2724}
2725
2726fn cast_fixed_size_binary_to_binary_view(
2727    array: &dyn Array,
2728    _byte_width: i32,
2729) -> Result<ArrayRef, ArrowError> {
2730    let array = array
2731        .as_any()
2732        .downcast_ref::<FixedSizeBinaryArray>()
2733        .unwrap();
2734
2735    let mut builder = BinaryViewBuilder::with_capacity(array.len());
2736    for i in 0..array.len() {
2737        if array.is_null(i) {
2738            builder.append_null();
2739        } else {
2740            builder.append_value(array.value(i));
2741        }
2742    }
2743
2744    Ok(Arc::new(builder.finish()))
2745}
2746
2747/// Helper function to cast from one `ByteArrayType` to another and vice versa.
2748/// If the target one (e.g., `LargeUtf8`) is too large for the source array it will return an Error.
2749fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2750where
2751    FROM: ByteArrayType,
2752    TO: ByteArrayType<Native = FROM::Native>,
2753    FROM::Offset: OffsetSizeTrait + ToPrimitive,
2754    TO::Offset: OffsetSizeTrait + NumCast,
2755{
2756    let data = array.to_data();
2757    assert_eq!(data.data_type(), &FROM::DATA_TYPE);
2758    let str_values_buf = data.buffers()[1].clone();
2759    let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
2760
2761    let mut offset_builder = BufferBuilder::<TO::Offset>::new(offsets.len());
2762    offsets
2763        .iter()
2764        .try_for_each::<_, Result<_, ArrowError>>(|offset| {
2765            let offset =
2766                <<TO as ByteArrayType>::Offset as NumCast>::from(*offset).ok_or_else(|| {
2767                    ArrowError::ComputeError(format!(
2768                        "{}{} array too large to cast to {}{} array",
2769                        FROM::Offset::PREFIX,
2770                        FROM::PREFIX,
2771                        TO::Offset::PREFIX,
2772                        TO::PREFIX
2773                    ))
2774                })?;
2775            offset_builder.append(offset);
2776            Ok(())
2777        })?;
2778
2779    let offset_buffer = offset_builder.finish();
2780
2781    let dtype = TO::DATA_TYPE;
2782
2783    let builder = ArrayData::builder(dtype)
2784        .offset(array.offset())
2785        .len(array.len())
2786        .add_buffer(offset_buffer)
2787        .add_buffer(str_values_buf)
2788        .nulls(data.nulls().cloned());
2789
2790    let array_data = unsafe { builder.build_unchecked() };
2791
2792    Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
2793}
2794
2795/// Helper function to cast from one `ByteViewType` array to `ByteArrayType` array.
2796fn cast_view_to_byte<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2797where
2798    FROM: ByteViewType,
2799    TO: ByteArrayType,
2800    FROM::Native: AsRef<TO::Native>,
2801{
2802    let data = array.to_data();
2803    let view_array = GenericByteViewArray::<FROM>::from(data);
2804
2805    let len = view_array.len();
2806    let bytes = view_array
2807        .views()
2808        .iter()
2809        .map(|v| ByteView::from(*v).length as usize)
2810        .sum::<usize>();
2811
2812    let mut byte_array_builder = GenericByteBuilder::<TO>::with_capacity(len, bytes);
2813
2814    for val in view_array.iter() {
2815        byte_array_builder.append_option(val);
2816    }
2817
2818    Ok(Arc::new(byte_array_builder.finish()))
2819}
2820
2821#[cfg(test)]
2822mod tests {
2823    use super::*;
2824    use DataType::*;
2825    use arrow_array::{Int64Array, RunArray, StringArray};
2826    use arrow_buffer::{Buffer, IntervalDayTime, NullBuffer};
2827    use arrow_buffer::{ScalarBuffer, i256};
2828    use arrow_schema::{DataType, Field};
2829    use chrono::NaiveDate;
2830    use half::f16;
2831    use std::sync::Arc;
2832
2833    #[derive(Clone)]
2834    struct DecimalCastTestConfig {
2835        input_prec: u8,
2836        input_scale: i8,
2837        input_repr: i128,
2838        output_prec: u8,
2839        output_scale: i8,
2840        expected_output_repr: Result<i128, String>, // the error variant can contain a string
2841                                                    // template where the "{}" will be
2842                                                    // replaced with the decimal type name
2843                                                    // (e.g. Decimal128)
2844    }
2845
2846    macro_rules! generate_cast_test_case {
2847        ($INPUT_ARRAY: expr, $OUTPUT_TYPE_ARRAY: ident, $OUTPUT_TYPE: expr, $OUTPUT_VALUES: expr) => {
2848            let output =
2849                $OUTPUT_TYPE_ARRAY::from($OUTPUT_VALUES).with_data_type($OUTPUT_TYPE.clone());
2850
2851            // assert cast type
2852            let input_array_type = $INPUT_ARRAY.data_type();
2853            assert!(can_cast_types(input_array_type, $OUTPUT_TYPE));
2854            let result = cast($INPUT_ARRAY, $OUTPUT_TYPE).unwrap();
2855            assert_eq!($OUTPUT_TYPE, result.data_type());
2856            assert_eq!(result.as_ref(), &output);
2857
2858            let cast_option = CastOptions {
2859                safe: false,
2860                format_options: FormatOptions::default(),
2861            };
2862            let result = cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap();
2863            assert_eq!($OUTPUT_TYPE, result.data_type());
2864            assert_eq!(result.as_ref(), &output);
2865        };
2866    }
2867
2868    fn run_decimal_cast_test_case<I, O>(t: DecimalCastTestConfig)
2869    where
2870        I: DecimalType,
2871        O: DecimalType,
2872        I::Native: DecimalCast,
2873        O::Native: DecimalCast,
2874    {
2875        let array = vec![I::Native::from_decimal(t.input_repr)];
2876        let array = array
2877            .into_iter()
2878            .collect::<PrimitiveArray<I>>()
2879            .with_precision_and_scale(t.input_prec, t.input_scale)
2880            .unwrap();
2881        let input_type = array.data_type();
2882        let output_type = O::TYPE_CONSTRUCTOR(t.output_prec, t.output_scale);
2883        assert!(can_cast_types(input_type, &output_type));
2884
2885        let options = CastOptions {
2886            safe: false,
2887            ..Default::default()
2888        };
2889        let result = cast_with_options(&array, &output_type, &options);
2890
2891        match t.expected_output_repr {
2892            Ok(v) => {
2893                let expected_array = vec![O::Native::from_decimal(v)];
2894                let expected_array = expected_array
2895                    .into_iter()
2896                    .collect::<PrimitiveArray<O>>()
2897                    .with_precision_and_scale(t.output_prec, t.output_scale)
2898                    .unwrap();
2899                assert_eq!(*result.unwrap(), expected_array);
2900            }
2901            Err(expected_output_message_template) => {
2902                assert!(result.is_err());
2903                let expected_error_message =
2904                    expected_output_message_template.replace("{}", O::PREFIX);
2905                assert_eq!(result.unwrap_err().to_string(), expected_error_message);
2906            }
2907        }
2908    }
2909
2910    fn create_decimal32_array(
2911        array: Vec<Option<i32>>,
2912        precision: u8,
2913        scale: i8,
2914    ) -> Result<Decimal32Array, ArrowError> {
2915        array
2916            .into_iter()
2917            .collect::<Decimal32Array>()
2918            .with_precision_and_scale(precision, scale)
2919    }
2920
2921    fn create_decimal64_array(
2922        array: Vec<Option<i64>>,
2923        precision: u8,
2924        scale: i8,
2925    ) -> Result<Decimal64Array, ArrowError> {
2926        array
2927            .into_iter()
2928            .collect::<Decimal64Array>()
2929            .with_precision_and_scale(precision, scale)
2930    }
2931
2932    fn create_decimal128_array(
2933        array: Vec<Option<i128>>,
2934        precision: u8,
2935        scale: i8,
2936    ) -> Result<Decimal128Array, ArrowError> {
2937        array
2938            .into_iter()
2939            .collect::<Decimal128Array>()
2940            .with_precision_and_scale(precision, scale)
2941    }
2942
2943    fn create_decimal256_array(
2944        array: Vec<Option<i256>>,
2945        precision: u8,
2946        scale: i8,
2947    ) -> Result<Decimal256Array, ArrowError> {
2948        array
2949            .into_iter()
2950            .collect::<Decimal256Array>()
2951            .with_precision_and_scale(precision, scale)
2952    }
2953
2954    #[test]
2955    #[cfg(not(feature = "force_validate"))]
2956    #[should_panic(
2957        expected = "Cannot cast to Decimal128(20, 3). Overflowing on 57896044618658097711785492504343953926634992332820282019728792003956564819967"
2958    )]
2959    fn test_cast_decimal_to_decimal_round_with_error() {
2960        // decimal256 to decimal128 overflow
2961        let array = vec![
2962            Some(i256::from_i128(1123454)),
2963            Some(i256::from_i128(2123456)),
2964            Some(i256::from_i128(-3123453)),
2965            Some(i256::from_i128(-3123456)),
2966            None,
2967            Some(i256::MAX),
2968            Some(i256::MIN),
2969        ];
2970        let input_decimal_array = create_decimal256_array(array, 76, 4).unwrap();
2971        let array = Arc::new(input_decimal_array) as ArrayRef;
2972        let input_type = DataType::Decimal256(76, 4);
2973        let output_type = DataType::Decimal128(20, 3);
2974        assert!(can_cast_types(&input_type, &output_type));
2975        generate_cast_test_case!(
2976            &array,
2977            Decimal128Array,
2978            &output_type,
2979            vec![
2980                Some(112345_i128),
2981                Some(212346_i128),
2982                Some(-312345_i128),
2983                Some(-312346_i128),
2984                None,
2985                None,
2986                None,
2987            ]
2988        );
2989    }
2990
2991    #[test]
2992    #[cfg(not(feature = "force_validate"))]
2993    fn test_cast_decimal_to_decimal_round() {
2994        let array = vec![
2995            Some(1123454),
2996            Some(2123456),
2997            Some(-3123453),
2998            Some(-3123456),
2999            None,
3000        ];
3001        let array = create_decimal128_array(array, 20, 4).unwrap();
3002        // decimal128 to decimal128
3003        let input_type = DataType::Decimal128(20, 4);
3004        let output_type = DataType::Decimal128(20, 3);
3005        assert!(can_cast_types(&input_type, &output_type));
3006        generate_cast_test_case!(
3007            &array,
3008            Decimal128Array,
3009            &output_type,
3010            vec![
3011                Some(112345_i128),
3012                Some(212346_i128),
3013                Some(-312345_i128),
3014                Some(-312346_i128),
3015                None
3016            ]
3017        );
3018
3019        // decimal128 to decimal256
3020        let input_type = DataType::Decimal128(20, 4);
3021        let output_type = DataType::Decimal256(20, 3);
3022        assert!(can_cast_types(&input_type, &output_type));
3023        generate_cast_test_case!(
3024            &array,
3025            Decimal256Array,
3026            &output_type,
3027            vec![
3028                Some(i256::from_i128(112345_i128)),
3029                Some(i256::from_i128(212346_i128)),
3030                Some(i256::from_i128(-312345_i128)),
3031                Some(i256::from_i128(-312346_i128)),
3032                None
3033            ]
3034        );
3035
3036        // decimal256
3037        let array = vec![
3038            Some(i256::from_i128(1123454)),
3039            Some(i256::from_i128(2123456)),
3040            Some(i256::from_i128(-3123453)),
3041            Some(i256::from_i128(-3123456)),
3042            None,
3043        ];
3044        let array = create_decimal256_array(array, 20, 4).unwrap();
3045
3046        // decimal256 to decimal256
3047        let input_type = DataType::Decimal256(20, 4);
3048        let output_type = DataType::Decimal256(20, 3);
3049        assert!(can_cast_types(&input_type, &output_type));
3050        generate_cast_test_case!(
3051            &array,
3052            Decimal256Array,
3053            &output_type,
3054            vec![
3055                Some(i256::from_i128(112345_i128)),
3056                Some(i256::from_i128(212346_i128)),
3057                Some(i256::from_i128(-312345_i128)),
3058                Some(i256::from_i128(-312346_i128)),
3059                None
3060            ]
3061        );
3062        // decimal256 to decimal128
3063        let input_type = DataType::Decimal256(20, 4);
3064        let output_type = DataType::Decimal128(20, 3);
3065        assert!(can_cast_types(&input_type, &output_type));
3066        generate_cast_test_case!(
3067            &array,
3068            Decimal128Array,
3069            &output_type,
3070            vec![
3071                Some(112345_i128),
3072                Some(212346_i128),
3073                Some(-312345_i128),
3074                Some(-312346_i128),
3075                None
3076            ]
3077        );
3078    }
3079
3080    #[test]
3081    fn test_cast_decimal32_to_decimal32() {
3082        // test changing precision
3083        let input_type = DataType::Decimal32(9, 3);
3084        let output_type = DataType::Decimal32(9, 4);
3085        assert!(can_cast_types(&input_type, &output_type));
3086        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3087        let array = create_decimal32_array(array, 9, 3).unwrap();
3088        generate_cast_test_case!(
3089            &array,
3090            Decimal32Array,
3091            &output_type,
3092            vec![
3093                Some(11234560_i32),
3094                Some(21234560_i32),
3095                Some(31234560_i32),
3096                None
3097            ]
3098        );
3099        // negative test
3100        let array = vec![Some(123456), None];
3101        let array = create_decimal32_array(array, 9, 0).unwrap();
3102        let result_safe = cast(&array, &DataType::Decimal32(2, 2));
3103        assert!(result_safe.is_ok());
3104        let options = CastOptions {
3105            safe: false,
3106            ..Default::default()
3107        };
3108
3109        let result_unsafe = cast_with_options(&array, &DataType::Decimal32(2, 2), &options);
3110        assert_eq!(
3111            "Invalid argument error: 123456.00 is too large to store in a Decimal32 of precision 2. Max is 0.99",
3112            result_unsafe.unwrap_err().to_string()
3113        );
3114    }
3115
3116    #[test]
3117    fn test_cast_decimal64_to_decimal64() {
3118        // test changing precision
3119        let input_type = DataType::Decimal64(17, 3);
3120        let output_type = DataType::Decimal64(17, 4);
3121        assert!(can_cast_types(&input_type, &output_type));
3122        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3123        let array = create_decimal64_array(array, 17, 3).unwrap();
3124        generate_cast_test_case!(
3125            &array,
3126            Decimal64Array,
3127            &output_type,
3128            vec![
3129                Some(11234560_i64),
3130                Some(21234560_i64),
3131                Some(31234560_i64),
3132                None
3133            ]
3134        );
3135        // negative test
3136        let array = vec![Some(123456), None];
3137        let array = create_decimal64_array(array, 9, 0).unwrap();
3138        let result_safe = cast(&array, &DataType::Decimal64(2, 2));
3139        assert!(result_safe.is_ok());
3140        let options = CastOptions {
3141            safe: false,
3142            ..Default::default()
3143        };
3144
3145        let result_unsafe = cast_with_options(&array, &DataType::Decimal64(2, 2), &options);
3146        assert_eq!(
3147            "Invalid argument error: 123456.00 is too large to store in a Decimal64 of precision 2. Max is 0.99",
3148            result_unsafe.unwrap_err().to_string()
3149        );
3150    }
3151
3152    #[test]
3153    fn test_cast_decimal128_to_decimal128() {
3154        // test changing precision
3155        let input_type = DataType::Decimal128(20, 3);
3156        let output_type = DataType::Decimal128(20, 4);
3157        assert!(can_cast_types(&input_type, &output_type));
3158        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3159        let array = create_decimal128_array(array, 20, 3).unwrap();
3160        generate_cast_test_case!(
3161            &array,
3162            Decimal128Array,
3163            &output_type,
3164            vec![
3165                Some(11234560_i128),
3166                Some(21234560_i128),
3167                Some(31234560_i128),
3168                None
3169            ]
3170        );
3171        // negative test
3172        let array = vec![Some(123456), None];
3173        let array = create_decimal128_array(array, 10, 0).unwrap();
3174        let result_safe = cast(&array, &DataType::Decimal128(2, 2));
3175        assert!(result_safe.is_ok());
3176        let options = CastOptions {
3177            safe: false,
3178            ..Default::default()
3179        };
3180
3181        let result_unsafe = cast_with_options(&array, &DataType::Decimal128(2, 2), &options);
3182        assert_eq!(
3183            "Invalid argument error: 123456.00 is too large to store in a Decimal128 of precision 2. Max is 0.99",
3184            result_unsafe.unwrap_err().to_string()
3185        );
3186    }
3187
3188    #[test]
3189    fn test_cast_decimal32_to_decimal32_dict() {
3190        let p = 9;
3191        let s = 3;
3192        let input_type = DataType::Decimal32(p, s);
3193        let output_type = DataType::Dictionary(
3194            Box::new(DataType::Int32),
3195            Box::new(DataType::Decimal32(p, s)),
3196        );
3197        assert!(can_cast_types(&input_type, &output_type));
3198        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3199        let array = create_decimal32_array(array, p, s).unwrap();
3200        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3201        assert_eq!(cast_array.data_type(), &output_type);
3202    }
3203
3204    #[test]
3205    fn test_cast_decimal64_to_decimal64_dict() {
3206        let p = 15;
3207        let s = 3;
3208        let input_type = DataType::Decimal64(p, s);
3209        let output_type = DataType::Dictionary(
3210            Box::new(DataType::Int32),
3211            Box::new(DataType::Decimal64(p, s)),
3212        );
3213        assert!(can_cast_types(&input_type, &output_type));
3214        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3215        let array = create_decimal64_array(array, p, s).unwrap();
3216        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3217        assert_eq!(cast_array.data_type(), &output_type);
3218    }
3219
3220    #[test]
3221    fn test_cast_decimal128_to_decimal128_dict() {
3222        let p = 20;
3223        let s = 3;
3224        let input_type = DataType::Decimal128(p, s);
3225        let output_type = DataType::Dictionary(
3226            Box::new(DataType::Int32),
3227            Box::new(DataType::Decimal128(p, s)),
3228        );
3229        assert!(can_cast_types(&input_type, &output_type));
3230        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3231        let array = create_decimal128_array(array, p, s).unwrap();
3232        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3233        assert_eq!(cast_array.data_type(), &output_type);
3234    }
3235
3236    #[test]
3237    fn test_cast_decimal256_to_decimal256_dict() {
3238        let p = 20;
3239        let s = 3;
3240        let input_type = DataType::Decimal256(p, s);
3241        let output_type = DataType::Dictionary(
3242            Box::new(DataType::Int32),
3243            Box::new(DataType::Decimal256(p, s)),
3244        );
3245        assert!(can_cast_types(&input_type, &output_type));
3246        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3247        let array = create_decimal128_array(array, p, s).unwrap();
3248        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3249        assert_eq!(cast_array.data_type(), &output_type);
3250    }
3251
3252    #[test]
3253    fn test_cast_decimal32_to_decimal32_overflow() {
3254        let input_type = DataType::Decimal32(9, 3);
3255        let output_type = DataType::Decimal32(9, 9);
3256        assert!(can_cast_types(&input_type, &output_type));
3257
3258        let array = vec![Some(i32::MAX)];
3259        let array = create_decimal32_array(array, 9, 3).unwrap();
3260        let result = cast_with_options(
3261            &array,
3262            &output_type,
3263            &CastOptions {
3264                safe: false,
3265                format_options: FormatOptions::default(),
3266            },
3267        );
3268        assert_eq!(
3269            "Cast error: Cannot cast to Decimal32(9, 9). Overflowing on 2147483647",
3270            result.unwrap_err().to_string()
3271        );
3272    }
3273
3274    #[test]
3275    fn test_cast_decimal32_to_decimal32_large_scale_reduction() {
3276        let array = vec![Some(-999999999), Some(0), Some(999999999), None];
3277        let array = create_decimal32_array(array, 9, 3).unwrap();
3278
3279        // Divide out all digits of precision -- rounding could still produce +/- 1
3280        let output_type = DataType::Decimal32(9, -6);
3281        assert!(can_cast_types(array.data_type(), &output_type));
3282        generate_cast_test_case!(
3283            &array,
3284            Decimal32Array,
3285            &output_type,
3286            vec![Some(-1), Some(0), Some(1), None]
3287        );
3288
3289        // Divide out more digits than we have precision -- all-zero result
3290        let output_type = DataType::Decimal32(9, -7);
3291        assert!(can_cast_types(array.data_type(), &output_type));
3292        generate_cast_test_case!(
3293            &array,
3294            Decimal32Array,
3295            &output_type,
3296            vec![Some(0), Some(0), Some(0), None]
3297        );
3298    }
3299
3300    #[test]
3301    fn test_cast_decimal64_to_decimal64_overflow() {
3302        let input_type = DataType::Decimal64(18, 3);
3303        let output_type = DataType::Decimal64(18, 18);
3304        assert!(can_cast_types(&input_type, &output_type));
3305
3306        let array = vec![Some(i64::MAX)];
3307        let array = create_decimal64_array(array, 18, 3).unwrap();
3308        let result = cast_with_options(
3309            &array,
3310            &output_type,
3311            &CastOptions {
3312                safe: false,
3313                format_options: FormatOptions::default(),
3314            },
3315        );
3316        assert_eq!(
3317            "Cast error: Cannot cast to Decimal64(18, 18). Overflowing on 9223372036854775807",
3318            result.unwrap_err().to_string()
3319        );
3320    }
3321
3322    #[test]
3323    fn test_cast_decimal64_to_decimal64_large_scale_reduction() {
3324        let array = vec![
3325            Some(-999999999999999999),
3326            Some(0),
3327            Some(999999999999999999),
3328            None,
3329        ];
3330        let array = create_decimal64_array(array, 18, 3).unwrap();
3331
3332        // Divide out all digits of precision -- rounding could still produce +/- 1
3333        let output_type = DataType::Decimal64(18, -15);
3334        assert!(can_cast_types(array.data_type(), &output_type));
3335        generate_cast_test_case!(
3336            &array,
3337            Decimal64Array,
3338            &output_type,
3339            vec![Some(-1), Some(0), Some(1), None]
3340        );
3341
3342        // Divide out more digits than we have precision -- all-zero result
3343        let output_type = DataType::Decimal64(18, -16);
3344        assert!(can_cast_types(array.data_type(), &output_type));
3345        generate_cast_test_case!(
3346            &array,
3347            Decimal64Array,
3348            &output_type,
3349            vec![Some(0), Some(0), Some(0), None]
3350        );
3351    }
3352
3353    #[test]
3354    fn test_cast_floating_to_decimals() {
3355        for output_type in [
3356            DataType::Decimal32(9, 3),
3357            DataType::Decimal64(9, 3),
3358            DataType::Decimal128(9, 3),
3359            DataType::Decimal256(9, 3),
3360        ] {
3361            let input_type = DataType::Float64;
3362            assert!(can_cast_types(&input_type, &output_type));
3363
3364            let array = vec![Some(1.1_f64)];
3365            let array = PrimitiveArray::<Float64Type>::from_iter(array);
3366            let result = cast_with_options(
3367                &array,
3368                &output_type,
3369                &CastOptions {
3370                    safe: false,
3371                    format_options: FormatOptions::default(),
3372                },
3373            );
3374            assert!(
3375                result.is_ok(),
3376                "Failed to cast to {output_type} with: {}",
3377                result.unwrap_err()
3378            );
3379        }
3380    }
3381
3382    #[test]
3383    fn test_cast_float16_to_decimals() {
3384        let array = Float16Array::from(vec![
3385            Some(f16::from_f32(1.25)),
3386            Some(f16::from_f32(-2.5)),
3387            Some(f16::from_f32(1.125)),
3388            Some(f16::from_f32(-1.125)),
3389            Some(f16::from_f32(0.0)),
3390            None,
3391        ]);
3392
3393        generate_cast_test_case!(
3394            &array,
3395            Decimal32Array,
3396            &DataType::Decimal32(9, 2),
3397            vec![
3398                Some(125_i32),
3399                Some(-250_i32),
3400                Some(113_i32),
3401                Some(-113_i32),
3402                Some(0_i32),
3403                None
3404            ]
3405        );
3406        generate_cast_test_case!(
3407            &array,
3408            Decimal64Array,
3409            &DataType::Decimal64(18, 2),
3410            vec![
3411                Some(125_i64),
3412                Some(-250_i64),
3413                Some(113_i64),
3414                Some(-113_i64),
3415                Some(0_i64),
3416                None
3417            ]
3418        );
3419        generate_cast_test_case!(
3420            &array,
3421            Decimal128Array,
3422            &DataType::Decimal128(38, 2),
3423            vec![
3424                Some(125_i128),
3425                Some(-250_i128),
3426                Some(113_i128),
3427                Some(-113_i128),
3428                Some(0_i128),
3429                None
3430            ]
3431        );
3432        generate_cast_test_case!(
3433            &array,
3434            Decimal256Array,
3435            &DataType::Decimal256(76, 2),
3436            vec![
3437                Some(i256::from_i128(125_i128)),
3438                Some(i256::from_i128(-250_i128)),
3439                Some(i256::from_i128(113_i128)),
3440                Some(i256::from_i128(-113_i128)),
3441                Some(i256::from_i128(0_i128)),
3442                None
3443            ]
3444        );
3445
3446        let array = Float16Array::from(vec![
3447            Some(f16::from_f32(1250.0)),
3448            Some(f16::from_f32(-1250.0)),
3449            Some(f16::from_f32(1249.0)),
3450            None,
3451        ]);
3452        generate_cast_test_case!(
3453            &array,
3454            Decimal128Array,
3455            &DataType::Decimal128(5, -2),
3456            vec![Some(13_i128), Some(-13_i128), Some(12_i128), None]
3457        );
3458    }
3459
3460    #[test]
3461    fn test_cast_decimal128_to_decimal128_overflow() {
3462        let input_type = DataType::Decimal128(38, 3);
3463        let output_type = DataType::Decimal128(38, 38);
3464        assert!(can_cast_types(&input_type, &output_type));
3465
3466        let array = vec![Some(i128::MAX)];
3467        let array = create_decimal128_array(array, 38, 3).unwrap();
3468        let result = cast_with_options(
3469            &array,
3470            &output_type,
3471            &CastOptions {
3472                safe: false,
3473                format_options: FormatOptions::default(),
3474            },
3475        );
3476        assert_eq!(
3477            "Cast error: Cannot cast to Decimal128(38, 38). Overflowing on 170141183460469231731687303715884105727",
3478            result.unwrap_err().to_string()
3479        );
3480    }
3481
3482    #[test]
3483    fn test_cast_decimal128_to_decimal256_overflow() {
3484        let input_type = DataType::Decimal128(38, 3);
3485        let output_type = DataType::Decimal256(76, 76);
3486        assert!(can_cast_types(&input_type, &output_type));
3487
3488        let array = vec![Some(i128::MAX)];
3489        let array = create_decimal128_array(array, 38, 3).unwrap();
3490        let result = cast_with_options(
3491            &array,
3492            &output_type,
3493            &CastOptions {
3494                safe: false,
3495                format_options: FormatOptions::default(),
3496            },
3497        );
3498        assert_eq!(
3499            "Cast error: Cannot cast to Decimal256(76, 76). Overflowing on 170141183460469231731687303715884105727",
3500            result.unwrap_err().to_string()
3501        );
3502    }
3503
3504    #[test]
3505    fn test_cast_decimal32_to_decimal256() {
3506        let input_type = DataType::Decimal32(8, 3);
3507        let output_type = DataType::Decimal256(20, 4);
3508        assert!(can_cast_types(&input_type, &output_type));
3509        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3510        let array = create_decimal32_array(array, 8, 3).unwrap();
3511        generate_cast_test_case!(
3512            &array,
3513            Decimal256Array,
3514            &output_type,
3515            vec![
3516                Some(i256::from_i128(11234560_i128)),
3517                Some(i256::from_i128(21234560_i128)),
3518                Some(i256::from_i128(31234560_i128)),
3519                None
3520            ]
3521        );
3522    }
3523    #[test]
3524    fn test_cast_decimal64_to_decimal256() {
3525        let input_type = DataType::Decimal64(12, 3);
3526        let output_type = DataType::Decimal256(20, 4);
3527        assert!(can_cast_types(&input_type, &output_type));
3528        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3529        let array = create_decimal64_array(array, 12, 3).unwrap();
3530        generate_cast_test_case!(
3531            &array,
3532            Decimal256Array,
3533            &output_type,
3534            vec![
3535                Some(i256::from_i128(11234560_i128)),
3536                Some(i256::from_i128(21234560_i128)),
3537                Some(i256::from_i128(31234560_i128)),
3538                None
3539            ]
3540        );
3541    }
3542    #[test]
3543    fn test_cast_decimal128_to_decimal256() {
3544        let input_type = DataType::Decimal128(20, 3);
3545        let output_type = DataType::Decimal256(20, 4);
3546        assert!(can_cast_types(&input_type, &output_type));
3547        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3548        let array = create_decimal128_array(array, 20, 3).unwrap();
3549        generate_cast_test_case!(
3550            &array,
3551            Decimal256Array,
3552            &output_type,
3553            vec![
3554                Some(i256::from_i128(11234560_i128)),
3555                Some(i256::from_i128(21234560_i128)),
3556                Some(i256::from_i128(31234560_i128)),
3557                None
3558            ]
3559        );
3560    }
3561
3562    #[test]
3563    fn test_cast_decimal256_to_decimal128_overflow() {
3564        let input_type = DataType::Decimal256(76, 5);
3565        let output_type = DataType::Decimal128(38, 7);
3566        assert!(can_cast_types(&input_type, &output_type));
3567        let array = vec![Some(i256::from_i128(i128::MAX))];
3568        let array = create_decimal256_array(array, 76, 5).unwrap();
3569        let result = cast_with_options(
3570            &array,
3571            &output_type,
3572            &CastOptions {
3573                safe: false,
3574                format_options: FormatOptions::default(),
3575            },
3576        );
3577        assert_eq!(
3578            "Cast error: Cannot cast to Decimal128(38, 7). Overflowing on 170141183460469231731687303715884105727",
3579            result.unwrap_err().to_string()
3580        );
3581    }
3582
3583    #[test]
3584    fn test_cast_decimal256_to_decimal256_overflow() {
3585        let input_type = DataType::Decimal256(76, 5);
3586        let output_type = DataType::Decimal256(76, 55);
3587        assert!(can_cast_types(&input_type, &output_type));
3588        let array = vec![Some(i256::from_i128(i128::MAX))];
3589        let array = create_decimal256_array(array, 76, 5).unwrap();
3590        let result = cast_with_options(
3591            &array,
3592            &output_type,
3593            &CastOptions {
3594                safe: false,
3595                format_options: FormatOptions::default(),
3596            },
3597        );
3598        assert_eq!(
3599            "Cast error: Cannot cast to Decimal256(76, 55). Overflowing on 170141183460469231731687303715884105727",
3600            result.unwrap_err().to_string()
3601        );
3602    }
3603
3604    #[test]
3605    fn test_cast_decimal256_to_decimal128() {
3606        let input_type = DataType::Decimal256(20, 3);
3607        let output_type = DataType::Decimal128(20, 4);
3608        assert!(can_cast_types(&input_type, &output_type));
3609        let array = vec![
3610            Some(i256::from_i128(1123456)),
3611            Some(i256::from_i128(2123456)),
3612            Some(i256::from_i128(3123456)),
3613            None,
3614        ];
3615        let array = create_decimal256_array(array, 20, 3).unwrap();
3616        generate_cast_test_case!(
3617            &array,
3618            Decimal128Array,
3619            &output_type,
3620            vec![
3621                Some(11234560_i128),
3622                Some(21234560_i128),
3623                Some(31234560_i128),
3624                None
3625            ]
3626        );
3627    }
3628
3629    #[test]
3630    fn test_cast_decimal256_to_decimal256() {
3631        let input_type = DataType::Decimal256(20, 3);
3632        let output_type = DataType::Decimal256(20, 4);
3633        assert!(can_cast_types(&input_type, &output_type));
3634        let array = vec![
3635            Some(i256::from_i128(1123456)),
3636            Some(i256::from_i128(2123456)),
3637            Some(i256::from_i128(3123456)),
3638            None,
3639        ];
3640        let array = create_decimal256_array(array, 20, 3).unwrap();
3641        generate_cast_test_case!(
3642            &array,
3643            Decimal256Array,
3644            &output_type,
3645            vec![
3646                Some(i256::from_i128(11234560_i128)),
3647                Some(i256::from_i128(21234560_i128)),
3648                Some(i256::from_i128(31234560_i128)),
3649                None
3650            ]
3651        );
3652    }
3653
3654    fn generate_decimal_to_numeric_cast_test_case<T>(array: &PrimitiveArray<T>)
3655    where
3656        T: ArrowPrimitiveType + DecimalType,
3657    {
3658        // u8
3659        generate_cast_test_case!(
3660            array,
3661            UInt8Array,
3662            &DataType::UInt8,
3663            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3664        );
3665        // u16
3666        generate_cast_test_case!(
3667            array,
3668            UInt16Array,
3669            &DataType::UInt16,
3670            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3671        );
3672        // u32
3673        generate_cast_test_case!(
3674            array,
3675            UInt32Array,
3676            &DataType::UInt32,
3677            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3678        );
3679        // u64
3680        generate_cast_test_case!(
3681            array,
3682            UInt64Array,
3683            &DataType::UInt64,
3684            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3685        );
3686        // i8
3687        generate_cast_test_case!(
3688            array,
3689            Int8Array,
3690            &DataType::Int8,
3691            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3692        );
3693        // i16
3694        generate_cast_test_case!(
3695            array,
3696            Int16Array,
3697            &DataType::Int16,
3698            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3699        );
3700        // i32
3701        generate_cast_test_case!(
3702            array,
3703            Int32Array,
3704            &DataType::Int32,
3705            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3706        );
3707        // i64
3708        generate_cast_test_case!(
3709            array,
3710            Int64Array,
3711            &DataType::Int64,
3712            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3713        );
3714        // f16
3715        generate_cast_test_case!(
3716            array,
3717            Float16Array,
3718            &DataType::Float16,
3719            vec![
3720                Some(f16::from_f32(1.25)),
3721                Some(f16::from_f32(2.25)),
3722                Some(f16::from_f32(3.25)),
3723                None,
3724                Some(f16::from_f32(5.25))
3725            ]
3726        );
3727        // f32
3728        generate_cast_test_case!(
3729            array,
3730            Float32Array,
3731            &DataType::Float32,
3732            vec![
3733                Some(1.25_f32),
3734                Some(2.25_f32),
3735                Some(3.25_f32),
3736                None,
3737                Some(5.25_f32)
3738            ]
3739        );
3740        // f64
3741        generate_cast_test_case!(
3742            array,
3743            Float64Array,
3744            &DataType::Float64,
3745            vec![
3746                Some(1.25_f64),
3747                Some(2.25_f64),
3748                Some(3.25_f64),
3749                None,
3750                Some(5.25_f64)
3751            ]
3752        );
3753    }
3754
3755    #[test]
3756    fn test_cast_decimal32_to_numeric() {
3757        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3758        let array = create_decimal32_array(value_array, 8, 2).unwrap();
3759
3760        generate_decimal_to_numeric_cast_test_case(&array);
3761    }
3762
3763    #[test]
3764    fn test_cast_decimal64_to_numeric() {
3765        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3766        let array = create_decimal64_array(value_array, 8, 2).unwrap();
3767
3768        generate_decimal_to_numeric_cast_test_case(&array);
3769    }
3770
3771    #[test]
3772    fn test_cast_decimal128_to_numeric() {
3773        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3774        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3775
3776        generate_decimal_to_numeric_cast_test_case(&array);
3777
3778        // overflow test: out of range of max u8
3779        let value_array: Vec<Option<i128>> = vec![Some(51300)];
3780        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3781        let casted_array = cast_with_options(
3782            &array,
3783            &DataType::UInt8,
3784            &CastOptions {
3785                safe: false,
3786                format_options: FormatOptions::default(),
3787            },
3788        );
3789        assert_eq!(
3790            "Cast error: value of 513 is out of range UInt8".to_string(),
3791            casted_array.unwrap_err().to_string()
3792        );
3793
3794        let casted_array = cast_with_options(
3795            &array,
3796            &DataType::UInt8,
3797            &CastOptions {
3798                safe: true,
3799                format_options: FormatOptions::default(),
3800            },
3801        );
3802        assert!(casted_array.is_ok());
3803        assert!(casted_array.unwrap().is_null(0));
3804
3805        // overflow test: out of range of max i8
3806        let value_array: Vec<Option<i128>> = vec![Some(24400)];
3807        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3808        let casted_array = cast_with_options(
3809            &array,
3810            &DataType::Int8,
3811            &CastOptions {
3812                safe: false,
3813                format_options: FormatOptions::default(),
3814            },
3815        );
3816        assert_eq!(
3817            "Cast error: value of 244 is out of range Int8".to_string(),
3818            casted_array.unwrap_err().to_string()
3819        );
3820
3821        let casted_array = cast_with_options(
3822            &array,
3823            &DataType::Int8,
3824            &CastOptions {
3825                safe: true,
3826                format_options: FormatOptions::default(),
3827            },
3828        );
3829        assert!(casted_array.is_ok());
3830        assert!(casted_array.unwrap().is_null(0));
3831
3832        // loss the precision: convert decimal to f32、f64
3833        // f32
3834        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3835        let value_array: Vec<Option<i128>> = vec![
3836            Some(125),
3837            Some(225),
3838            Some(325),
3839            None,
3840            Some(525),
3841            Some(112345678),
3842            Some(112345679),
3843        ];
3844        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3845        generate_cast_test_case!(
3846            &array,
3847            Float32Array,
3848            &DataType::Float32,
3849            vec![
3850                Some(1.25_f32),
3851                Some(2.25_f32),
3852                Some(3.25_f32),
3853                None,
3854                Some(5.25_f32),
3855                Some(1_123_456.7_f32),
3856                Some(1_123_456.7_f32)
3857            ]
3858        );
3859
3860        // f64
3861        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3862        let value_array: Vec<Option<i128>> = vec![
3863            Some(125),
3864            Some(225),
3865            Some(325),
3866            None,
3867            Some(525),
3868            Some(112345678901234568),
3869            Some(112345678901234560),
3870        ];
3871        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3872        generate_cast_test_case!(
3873            &array,
3874            Float64Array,
3875            &DataType::Float64,
3876            vec![
3877                Some(1.25_f64),
3878                Some(2.25_f64),
3879                Some(3.25_f64),
3880                None,
3881                Some(5.25_f64),
3882                Some(1_123_456_789_012_345.6_f64),
3883                Some(1_123_456_789_012_345.6_f64),
3884            ]
3885        );
3886    }
3887
3888    #[test]
3889    fn test_cast_decimal256_to_numeric() {
3890        let value_array: Vec<Option<i256>> = vec![
3891            Some(i256::from_i128(125)),
3892            Some(i256::from_i128(225)),
3893            Some(i256::from_i128(325)),
3894            None,
3895            Some(i256::from_i128(525)),
3896        ];
3897        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3898        // u8
3899        generate_cast_test_case!(
3900            &array,
3901            UInt8Array,
3902            &DataType::UInt8,
3903            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3904        );
3905        // u16
3906        generate_cast_test_case!(
3907            &array,
3908            UInt16Array,
3909            &DataType::UInt16,
3910            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3911        );
3912        // u32
3913        generate_cast_test_case!(
3914            &array,
3915            UInt32Array,
3916            &DataType::UInt32,
3917            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3918        );
3919        // u64
3920        generate_cast_test_case!(
3921            &array,
3922            UInt64Array,
3923            &DataType::UInt64,
3924            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3925        );
3926        // i8
3927        generate_cast_test_case!(
3928            &array,
3929            Int8Array,
3930            &DataType::Int8,
3931            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3932        );
3933        // i16
3934        generate_cast_test_case!(
3935            &array,
3936            Int16Array,
3937            &DataType::Int16,
3938            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3939        );
3940        // i32
3941        generate_cast_test_case!(
3942            &array,
3943            Int32Array,
3944            &DataType::Int32,
3945            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3946        );
3947        // i64
3948        generate_cast_test_case!(
3949            &array,
3950            Int64Array,
3951            &DataType::Int64,
3952            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3953        );
3954        // f16
3955        generate_cast_test_case!(
3956            &array,
3957            Float16Array,
3958            &DataType::Float16,
3959            vec![
3960                Some(f16::from_f32(1.25)),
3961                Some(f16::from_f32(2.25)),
3962                Some(f16::from_f32(3.25)),
3963                None,
3964                Some(f16::from_f32(5.25))
3965            ]
3966        );
3967        // f32
3968        generate_cast_test_case!(
3969            &array,
3970            Float32Array,
3971            &DataType::Float32,
3972            vec![
3973                Some(1.25_f32),
3974                Some(2.25_f32),
3975                Some(3.25_f32),
3976                None,
3977                Some(5.25_f32)
3978            ]
3979        );
3980        // f64
3981        generate_cast_test_case!(
3982            &array,
3983            Float64Array,
3984            &DataType::Float64,
3985            vec![
3986                Some(1.25_f64),
3987                Some(2.25_f64),
3988                Some(3.25_f64),
3989                None,
3990                Some(5.25_f64)
3991            ]
3992        );
3993
3994        // overflow test: out of range of max i8
3995        let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128(24400))];
3996        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3997        let casted_array = cast_with_options(
3998            &array,
3999            &DataType::Int8,
4000            &CastOptions {
4001                safe: false,
4002                format_options: FormatOptions::default(),
4003            },
4004        );
4005        assert_eq!(
4006            "Cast error: value of 244 is out of range Int8".to_string(),
4007            casted_array.unwrap_err().to_string()
4008        );
4009
4010        let casted_array = cast_with_options(
4011            &array,
4012            &DataType::Int8,
4013            &CastOptions {
4014                safe: true,
4015                format_options: FormatOptions::default(),
4016            },
4017        );
4018        assert!(casted_array.is_ok());
4019        assert!(casted_array.unwrap().is_null(0));
4020
4021        // loss the precision: convert decimal to f32、f64
4022        // f32
4023        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
4024        let value_array: Vec<Option<i256>> = vec![
4025            Some(i256::from_i128(125)),
4026            Some(i256::from_i128(225)),
4027            Some(i256::from_i128(325)),
4028            None,
4029            Some(i256::from_i128(525)),
4030            Some(i256::from_i128(112345678)),
4031            Some(i256::from_i128(112345679)),
4032        ];
4033        let array = create_decimal256_array(value_array, 76, 2).unwrap();
4034        generate_cast_test_case!(
4035            &array,
4036            Float32Array,
4037            &DataType::Float32,
4038            vec![
4039                Some(1.25_f32),
4040                Some(2.25_f32),
4041                Some(3.25_f32),
4042                None,
4043                Some(5.25_f32),
4044                Some(1_123_456.7_f32),
4045                Some(1_123_456.7_f32)
4046            ]
4047        );
4048
4049        // f64
4050        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
4051        let value_array: Vec<Option<i256>> = vec![
4052            Some(i256::from_i128(125)),
4053            Some(i256::from_i128(225)),
4054            Some(i256::from_i128(325)),
4055            None,
4056            Some(i256::from_i128(525)),
4057            Some(i256::from_i128(112345678901234568)),
4058            Some(i256::from_i128(112345678901234560)),
4059        ];
4060        let array = create_decimal256_array(value_array, 76, 2).unwrap();
4061        generate_cast_test_case!(
4062            &array,
4063            Float64Array,
4064            &DataType::Float64,
4065            vec![
4066                Some(1.25_f64),
4067                Some(2.25_f64),
4068                Some(3.25_f64),
4069                None,
4070                Some(5.25_f64),
4071                Some(1_123_456_789_012_345.6_f64),
4072                Some(1_123_456_789_012_345.6_f64),
4073            ]
4074        );
4075    }
4076
4077    #[test]
4078    fn test_cast_decimal128_to_float16_overflow() {
4079        let array = create_decimal128_array(
4080            vec![
4081                Some(6_550_400_i128),
4082                Some(100_000_000_i128),
4083                Some(-100_000_000_i128),
4084                None,
4085            ],
4086            10,
4087            2,
4088        )
4089        .unwrap();
4090
4091        generate_cast_test_case!(
4092            &array,
4093            Float16Array,
4094            &DataType::Float16,
4095            vec![
4096                Some(f16::from_f64(65504.0)),
4097                Some(f16::INFINITY),
4098                Some(f16::NEG_INFINITY),
4099                None
4100            ]
4101        );
4102    }
4103
4104    #[test]
4105    fn test_cast_decimal256_to_float16_overflow() {
4106        let array = create_decimal256_array(
4107            vec![
4108                Some(i256::from_i128(6_550_400_i128)),
4109                Some(i256::from_i128(100_000_000_i128)),
4110                Some(i256::from_i128(-100_000_000_i128)),
4111                None,
4112            ],
4113            10,
4114            2,
4115        )
4116        .unwrap();
4117
4118        generate_cast_test_case!(
4119            &array,
4120            Float16Array,
4121            &DataType::Float16,
4122            vec![
4123                Some(f16::from_f64(65504.0)),
4124                Some(f16::INFINITY),
4125                Some(f16::NEG_INFINITY),
4126                None
4127            ]
4128        );
4129    }
4130
4131    #[test]
4132    fn test_cast_decimal_to_numeric_negative_scale() {
4133        let value_array: Vec<Option<i256>> = vec![
4134            Some(i256::from_i128(125)),
4135            Some(i256::from_i128(225)),
4136            Some(i256::from_i128(325)),
4137            None,
4138            Some(i256::from_i128(525)),
4139        ];
4140        let array = create_decimal256_array(value_array, 38, -1).unwrap();
4141
4142        generate_cast_test_case!(
4143            &array,
4144            Int64Array,
4145            &DataType::Int64,
4146            vec![Some(1_250), Some(2_250), Some(3_250), None, Some(5_250)]
4147        );
4148
4149        let value_array: Vec<Option<i128>> = vec![Some(12), Some(-12), None];
4150        let array = create_decimal128_array(value_array, 10, -2).unwrap();
4151        generate_cast_test_case!(
4152            &array,
4153            Float16Array,
4154            &DataType::Float16,
4155            vec![
4156                Some(f16::from_f32(1200.0)),
4157                Some(f16::from_f32(-1200.0)),
4158                None
4159            ]
4160        );
4161
4162        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4163        let array = create_decimal32_array(value_array, 8, -2).unwrap();
4164        generate_cast_test_case!(
4165            &array,
4166            Int64Array,
4167            &DataType::Int64,
4168            vec![Some(12_500), Some(22_500), Some(32_500), None, Some(52_500)]
4169        );
4170
4171        let value_array: Vec<Option<i32>> = vec![Some(2), Some(1), None];
4172        let array = create_decimal32_array(value_array, 9, -9).unwrap();
4173        generate_cast_test_case!(
4174            &array,
4175            Int64Array,
4176            &DataType::Int64,
4177            vec![Some(2_000_000_000), Some(1_000_000_000), None]
4178        );
4179
4180        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4181        let array = create_decimal64_array(value_array, 18, -3).unwrap();
4182        generate_cast_test_case!(
4183            &array,
4184            Int64Array,
4185            &DataType::Int64,
4186            vec![
4187                Some(125_000),
4188                Some(225_000),
4189                Some(325_000),
4190                None,
4191                Some(525_000)
4192            ]
4193        );
4194
4195        let value_array: Vec<Option<i64>> = vec![Some(12), Some(34), None];
4196        let array = create_decimal64_array(value_array, 18, -10).unwrap();
4197        generate_cast_test_case!(
4198            &array,
4199            Int64Array,
4200            &DataType::Int64,
4201            vec![Some(120_000_000_000), Some(340_000_000_000), None]
4202        );
4203
4204        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4205        let array = create_decimal128_array(value_array, 38, -4).unwrap();
4206        generate_cast_test_case!(
4207            &array,
4208            Int64Array,
4209            &DataType::Int64,
4210            vec![
4211                Some(1_250_000),
4212                Some(2_250_000),
4213                Some(3_250_000),
4214                None,
4215                Some(5_250_000)
4216            ]
4217        );
4218
4219        let value_array: Vec<Option<i128>> = vec![Some(9), Some(1), None];
4220        let array = create_decimal128_array(value_array, 38, -18).unwrap();
4221        generate_cast_test_case!(
4222            &array,
4223            Int64Array,
4224            &DataType::Int64,
4225            vec![
4226                Some(9_000_000_000_000_000_000),
4227                Some(1_000_000_000_000_000_000),
4228                None
4229            ]
4230        );
4231
4232        let array = create_decimal32_array(vec![Some(999_999_999)], 9, -1).unwrap();
4233        let casted_array = cast_with_options(
4234            &array,
4235            &DataType::Int64,
4236            &CastOptions {
4237                safe: false,
4238                format_options: FormatOptions::default(),
4239            },
4240        );
4241        assert_eq!(
4242            "Arithmetic overflow: Overflow happened on: 999999999 * 10".to_string(),
4243            casted_array.unwrap_err().to_string()
4244        );
4245
4246        let casted_array = cast_with_options(
4247            &array,
4248            &DataType::Int64,
4249            &CastOptions {
4250                safe: true,
4251                format_options: FormatOptions::default(),
4252            },
4253        );
4254        assert!(casted_array.is_ok());
4255        assert!(casted_array.unwrap().is_null(0));
4256
4257        let array = create_decimal64_array(vec![Some(13)], 18, -1).unwrap();
4258        let casted_array = cast_with_options(
4259            &array,
4260            &DataType::Int8,
4261            &CastOptions {
4262                safe: false,
4263                format_options: FormatOptions::default(),
4264            },
4265        );
4266        assert_eq!(
4267            "Cast error: value of 130 is out of range Int8".to_string(),
4268            casted_array.unwrap_err().to_string()
4269        );
4270
4271        let casted_array = cast_with_options(
4272            &array,
4273            &DataType::Int8,
4274            &CastOptions {
4275                safe: true,
4276                format_options: FormatOptions::default(),
4277            },
4278        );
4279        assert!(casted_array.is_ok());
4280        assert!(casted_array.unwrap().is_null(0));
4281    }
4282
4283    #[test]
4284    fn test_cast_numeric_to_decimal128() {
4285        let decimal_type = DataType::Decimal128(38, 6);
4286        // u8, u16, u32, u64
4287        let input_datas = vec![
4288            Arc::new(UInt8Array::from(vec![
4289                Some(1),
4290                Some(2),
4291                Some(3),
4292                None,
4293                Some(5),
4294            ])) as ArrayRef, // u8
4295            Arc::new(UInt16Array::from(vec![
4296                Some(1),
4297                Some(2),
4298                Some(3),
4299                None,
4300                Some(5),
4301            ])) as ArrayRef, // u16
4302            Arc::new(UInt32Array::from(vec![
4303                Some(1),
4304                Some(2),
4305                Some(3),
4306                None,
4307                Some(5),
4308            ])) as ArrayRef, // u32
4309            Arc::new(UInt64Array::from(vec![
4310                Some(1),
4311                Some(2),
4312                Some(3),
4313                None,
4314                Some(5),
4315            ])) as ArrayRef, // u64
4316        ];
4317
4318        for array in input_datas {
4319            generate_cast_test_case!(
4320                &array,
4321                Decimal128Array,
4322                &decimal_type,
4323                vec![
4324                    Some(1000000_i128),
4325                    Some(2000000_i128),
4326                    Some(3000000_i128),
4327                    None,
4328                    Some(5000000_i128)
4329                ]
4330            );
4331        }
4332
4333        // i8, i16, i32, i64
4334        let input_datas = vec![
4335            Arc::new(Int8Array::from(vec![
4336                Some(1),
4337                Some(2),
4338                Some(3),
4339                None,
4340                Some(5),
4341            ])) as ArrayRef, // i8
4342            Arc::new(Int16Array::from(vec![
4343                Some(1),
4344                Some(2),
4345                Some(3),
4346                None,
4347                Some(5),
4348            ])) as ArrayRef, // i16
4349            Arc::new(Int32Array::from(vec![
4350                Some(1),
4351                Some(2),
4352                Some(3),
4353                None,
4354                Some(5),
4355            ])) as ArrayRef, // i32
4356            Arc::new(Int64Array::from(vec![
4357                Some(1),
4358                Some(2),
4359                Some(3),
4360                None,
4361                Some(5),
4362            ])) as ArrayRef, // i64
4363        ];
4364        for array in input_datas {
4365            generate_cast_test_case!(
4366                &array,
4367                Decimal128Array,
4368                &decimal_type,
4369                vec![
4370                    Some(1000000_i128),
4371                    Some(2000000_i128),
4372                    Some(3000000_i128),
4373                    None,
4374                    Some(5000000_i128)
4375                ]
4376            );
4377        }
4378
4379        // test u8 to decimal type with overflow the result type
4380        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4381        let array = UInt8Array::from(vec![1, 2, 3, 4, 100]);
4382        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4383        assert!(casted_array.is_ok());
4384        let array = casted_array.unwrap();
4385        let array: &Decimal128Array = array.as_primitive();
4386        assert!(array.is_null(4));
4387
4388        // test i8 to decimal type with overflow the result type
4389        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4390        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4391        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4392        assert!(casted_array.is_ok());
4393        let array = casted_array.unwrap();
4394        let array: &Decimal128Array = array.as_primitive();
4395        assert!(array.is_null(4));
4396
4397        // test f32 to decimal type
4398        let array = Float32Array::from(vec![
4399            Some(1.1),
4400            Some(2.2),
4401            Some(4.4),
4402            None,
4403            Some(1.123_456_4), // round down
4404            Some(1.123_456_7), // round up
4405        ]);
4406        let array = Arc::new(array) as ArrayRef;
4407        generate_cast_test_case!(
4408            &array,
4409            Decimal128Array,
4410            &decimal_type,
4411            vec![
4412                Some(1100000_i128),
4413                Some(2200000_i128),
4414                Some(4400000_i128),
4415                None,
4416                Some(1123456_i128), // round down
4417                Some(1123457_i128), // round up
4418            ]
4419        );
4420
4421        // test f64 to decimal type
4422        let array = Float64Array::from(vec![
4423            Some(1.1),
4424            Some(2.2),
4425            Some(4.4),
4426            None,
4427            Some(1.123_456_489_123_4),     // round up
4428            Some(1.123_456_789_123_4),     // round up
4429            Some(1.123_456_489_012_345_6), // round down
4430            Some(1.123_456_789_012_345_6), // round up
4431        ]);
4432        generate_cast_test_case!(
4433            &array,
4434            Decimal128Array,
4435            &decimal_type,
4436            vec![
4437                Some(1100000_i128),
4438                Some(2200000_i128),
4439                Some(4400000_i128),
4440                None,
4441                Some(1123456_i128), // round down
4442                Some(1123457_i128), // round up
4443                Some(1123456_i128), // round down
4444                Some(1123457_i128), // round up
4445            ]
4446        );
4447    }
4448
4449    #[test]
4450    fn test_cast_numeric_to_decimal256() {
4451        let decimal_type = DataType::Decimal256(76, 6);
4452        // u8, u16, u32, u64
4453        let input_datas = vec![
4454            Arc::new(UInt8Array::from(vec![
4455                Some(1),
4456                Some(2),
4457                Some(3),
4458                None,
4459                Some(5),
4460            ])) as ArrayRef, // u8
4461            Arc::new(UInt16Array::from(vec![
4462                Some(1),
4463                Some(2),
4464                Some(3),
4465                None,
4466                Some(5),
4467            ])) as ArrayRef, // u16
4468            Arc::new(UInt32Array::from(vec![
4469                Some(1),
4470                Some(2),
4471                Some(3),
4472                None,
4473                Some(5),
4474            ])) as ArrayRef, // u32
4475            Arc::new(UInt64Array::from(vec![
4476                Some(1),
4477                Some(2),
4478                Some(3),
4479                None,
4480                Some(5),
4481            ])) as ArrayRef, // u64
4482        ];
4483
4484        for array in input_datas {
4485            generate_cast_test_case!(
4486                &array,
4487                Decimal256Array,
4488                &decimal_type,
4489                vec![
4490                    Some(i256::from_i128(1000000_i128)),
4491                    Some(i256::from_i128(2000000_i128)),
4492                    Some(i256::from_i128(3000000_i128)),
4493                    None,
4494                    Some(i256::from_i128(5000000_i128))
4495                ]
4496            );
4497        }
4498
4499        // i8, i16, i32, i64
4500        let input_datas = vec![
4501            Arc::new(Int8Array::from(vec![
4502                Some(1),
4503                Some(2),
4504                Some(3),
4505                None,
4506                Some(5),
4507            ])) as ArrayRef, // i8
4508            Arc::new(Int16Array::from(vec![
4509                Some(1),
4510                Some(2),
4511                Some(3),
4512                None,
4513                Some(5),
4514            ])) as ArrayRef, // i16
4515            Arc::new(Int32Array::from(vec![
4516                Some(1),
4517                Some(2),
4518                Some(3),
4519                None,
4520                Some(5),
4521            ])) as ArrayRef, // i32
4522            Arc::new(Int64Array::from(vec![
4523                Some(1),
4524                Some(2),
4525                Some(3),
4526                None,
4527                Some(5),
4528            ])) as ArrayRef, // i64
4529        ];
4530        for array in input_datas {
4531            generate_cast_test_case!(
4532                &array,
4533                Decimal256Array,
4534                &decimal_type,
4535                vec![
4536                    Some(i256::from_i128(1000000_i128)),
4537                    Some(i256::from_i128(2000000_i128)),
4538                    Some(i256::from_i128(3000000_i128)),
4539                    None,
4540                    Some(i256::from_i128(5000000_i128))
4541                ]
4542            );
4543        }
4544
4545        // test i8 to decimal type with overflow the result type
4546        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4547        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4548        let array = Arc::new(array) as ArrayRef;
4549        let casted_array = cast(&array, &DataType::Decimal256(3, 1));
4550        assert!(casted_array.is_ok());
4551        let array = casted_array.unwrap();
4552        let array: &Decimal256Array = array.as_primitive();
4553        assert!(array.is_null(4));
4554
4555        // test f32 to decimal type
4556        let array = Float32Array::from(vec![
4557            Some(1.1),
4558            Some(2.2),
4559            Some(4.4),
4560            None,
4561            Some(1.123_456_4), // round down
4562            Some(1.123_456_7), // round up
4563        ]);
4564        generate_cast_test_case!(
4565            &array,
4566            Decimal256Array,
4567            &decimal_type,
4568            vec![
4569                Some(i256::from_i128(1100000_i128)),
4570                Some(i256::from_i128(2200000_i128)),
4571                Some(i256::from_i128(4400000_i128)),
4572                None,
4573                Some(i256::from_i128(1123456_i128)), // round down
4574                Some(i256::from_i128(1123457_i128)), // round up
4575            ]
4576        );
4577
4578        // test f64 to decimal type
4579        let array = Float64Array::from(vec![
4580            Some(1.1),
4581            Some(2.2),
4582            Some(4.4),
4583            None,
4584            Some(1.123_456_489_123_4),     // round down
4585            Some(1.123_456_789_123_4),     // round up
4586            Some(1.123_456_489_012_345_6), // round down
4587            Some(1.123_456_789_012_345_6), // round up
4588        ]);
4589        generate_cast_test_case!(
4590            &array,
4591            Decimal256Array,
4592            &decimal_type,
4593            vec![
4594                Some(i256::from_i128(1100000_i128)),
4595                Some(i256::from_i128(2200000_i128)),
4596                Some(i256::from_i128(4400000_i128)),
4597                None,
4598                Some(i256::from_i128(1123456_i128)), // round down
4599                Some(i256::from_i128(1123457_i128)), // round up
4600                Some(i256::from_i128(1123456_i128)), // round down
4601                Some(i256::from_i128(1123457_i128)), // round up
4602            ]
4603        );
4604    }
4605
4606    #[test]
4607    fn test_cast_i32_to_f64() {
4608        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4609        let b = cast(&array, &DataType::Float64).unwrap();
4610        let c = b.as_primitive::<Float64Type>();
4611        assert_eq!(5.0, c.value(0));
4612        assert_eq!(6.0, c.value(1));
4613        assert_eq!(7.0, c.value(2));
4614        assert_eq!(8.0, c.value(3));
4615        assert_eq!(9.0, c.value(4));
4616    }
4617
4618    #[test]
4619    fn test_cast_i32_to_u8() {
4620        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4621        let b = cast(&array, &DataType::UInt8).unwrap();
4622        let c = b.as_primitive::<UInt8Type>();
4623        assert!(!c.is_valid(0));
4624        assert_eq!(6, c.value(1));
4625        assert!(!c.is_valid(2));
4626        assert_eq!(8, c.value(3));
4627        // overflows return None
4628        assert!(!c.is_valid(4));
4629    }
4630
4631    #[test]
4632    #[should_panic(expected = "Can't cast value -5 to type UInt8")]
4633    fn test_cast_int32_to_u8_with_error() {
4634        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4635        // overflow with the error
4636        let cast_option = CastOptions {
4637            safe: false,
4638            format_options: FormatOptions::default(),
4639        };
4640        let result = cast_with_options(&array, &DataType::UInt8, &cast_option);
4641        assert!(result.is_err());
4642        result.unwrap();
4643    }
4644
4645    #[test]
4646    fn test_cast_i32_to_u8_sliced() {
4647        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4648        assert_eq!(0, array.offset());
4649        let array = array.slice(2, 3);
4650        let b = cast(&array, &DataType::UInt8).unwrap();
4651        assert_eq!(3, b.len());
4652        let c = b.as_primitive::<UInt8Type>();
4653        assert!(!c.is_valid(0));
4654        assert_eq!(8, c.value(1));
4655        // overflows return None
4656        assert!(!c.is_valid(2));
4657    }
4658
4659    #[test]
4660    fn test_cast_i32_to_i32() {
4661        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4662        let b = cast(&array, &DataType::Int32).unwrap();
4663        let c = b.as_primitive::<Int32Type>();
4664        assert_eq!(5, c.value(0));
4665        assert_eq!(6, c.value(1));
4666        assert_eq!(7, c.value(2));
4667        assert_eq!(8, c.value(3));
4668        assert_eq!(9, c.value(4));
4669    }
4670
4671    #[test]
4672    fn test_cast_i32_to_list_i32() {
4673        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4674        let b = cast(
4675            &array,
4676            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4677        )
4678        .unwrap();
4679        assert_eq!(5, b.len());
4680        let arr = b.as_list::<i32>();
4681        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4682        assert_eq!(1, arr.value_length(0));
4683        assert_eq!(1, arr.value_length(1));
4684        assert_eq!(1, arr.value_length(2));
4685        assert_eq!(1, arr.value_length(3));
4686        assert_eq!(1, arr.value_length(4));
4687        let c = arr.values().as_primitive::<Int32Type>();
4688        assert_eq!(5, c.value(0));
4689        assert_eq!(6, c.value(1));
4690        assert_eq!(7, c.value(2));
4691        assert_eq!(8, c.value(3));
4692        assert_eq!(9, c.value(4));
4693    }
4694
4695    #[test]
4696    fn test_cast_i32_to_list_i32_nullable() {
4697        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), Some(9)]);
4698        let b = cast(
4699            &array,
4700            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4701        )
4702        .unwrap();
4703        assert_eq!(5, b.len());
4704        assert_eq!(0, b.null_count());
4705        let arr = b.as_list::<i32>();
4706        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4707        assert_eq!(1, arr.value_length(0));
4708        assert_eq!(1, arr.value_length(1));
4709        assert_eq!(1, arr.value_length(2));
4710        assert_eq!(1, arr.value_length(3));
4711        assert_eq!(1, arr.value_length(4));
4712
4713        let c = arr.values().as_primitive::<Int32Type>();
4714        assert_eq!(1, c.null_count());
4715        assert_eq!(5, c.value(0));
4716        assert!(!c.is_valid(1));
4717        assert_eq!(7, c.value(2));
4718        assert_eq!(8, c.value(3));
4719        assert_eq!(9, c.value(4));
4720    }
4721
4722    #[test]
4723    fn test_cast_i32_to_list_f64_nullable_sliced() {
4724        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), None, Some(10)]);
4725        let array = array.slice(2, 4);
4726        let b = cast(
4727            &array,
4728            &DataType::List(Arc::new(Field::new_list_field(DataType::Float64, true))),
4729        )
4730        .unwrap();
4731        assert_eq!(4, b.len());
4732        assert_eq!(0, b.null_count());
4733        let arr = b.as_list::<i32>();
4734        assert_eq!(&[0, 1, 2, 3, 4], arr.value_offsets());
4735        assert_eq!(1, arr.value_length(0));
4736        assert_eq!(1, arr.value_length(1));
4737        assert_eq!(1, arr.value_length(2));
4738        assert_eq!(1, arr.value_length(3));
4739        let c = arr.values().as_primitive::<Float64Type>();
4740        assert_eq!(1, c.null_count());
4741        assert_eq!(7.0, c.value(0));
4742        assert_eq!(8.0, c.value(1));
4743        assert!(!c.is_valid(2));
4744        assert_eq!(10.0, c.value(3));
4745    }
4746
4747    #[test]
4748    fn test_cast_int_to_utf8view() {
4749        let inputs = vec![
4750            Arc::new(Int8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4751            Arc::new(Int16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4752            Arc::new(Int32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4753            Arc::new(Int64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4754            Arc::new(UInt8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4755            Arc::new(UInt16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4756            Arc::new(UInt32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4757            Arc::new(UInt64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4758        ];
4759        let expected: ArrayRef = Arc::new(StringViewArray::from(vec![
4760            None,
4761            Some("8"),
4762            Some("9"),
4763            Some("10"),
4764        ]));
4765
4766        for array in inputs {
4767            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4768            let arr = cast(&array, &DataType::Utf8View).unwrap();
4769            assert_eq!(expected.as_ref(), arr.as_ref());
4770        }
4771    }
4772
4773    #[test]
4774    fn test_cast_float_to_utf8view() {
4775        let inputs = vec![
4776            Arc::new(Float16Array::from(vec![
4777                Some(f16::from_f64(1.5)),
4778                Some(f16::from_f64(2.5)),
4779                None,
4780            ])) as ArrayRef,
4781            Arc::new(Float32Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4782            Arc::new(Float64Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4783        ];
4784
4785        let expected: ArrayRef =
4786            Arc::new(StringViewArray::from(vec![Some("1.5"), Some("2.5"), None]));
4787
4788        for array in inputs {
4789            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4790            let arr = cast(&array, &DataType::Utf8View).unwrap();
4791            assert_eq!(expected.as_ref(), arr.as_ref());
4792        }
4793    }
4794
4795    #[test]
4796    fn test_cast_utf8_to_i32() {
4797        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4798        let b = cast(&array, &DataType::Int32).unwrap();
4799        let c = b.as_primitive::<Int32Type>();
4800        assert_eq!(5, c.value(0));
4801        assert_eq!(6, c.value(1));
4802        assert!(!c.is_valid(2));
4803        assert_eq!(8, c.value(3));
4804        assert!(!c.is_valid(4));
4805    }
4806
4807    #[test]
4808    fn test_cast_utf8view_to_i32() {
4809        let array = StringViewArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4810        let b = cast(&array, &DataType::Int32).unwrap();
4811        let c = b.as_primitive::<Int32Type>();
4812        assert_eq!(5, c.value(0));
4813        assert_eq!(6, c.value(1));
4814        assert!(!c.is_valid(2));
4815        assert_eq!(8, c.value(3));
4816        assert!(!c.is_valid(4));
4817    }
4818
4819    #[test]
4820    fn test_cast_utf8view_to_f32() {
4821        let array = StringViewArray::from(vec!["3", "4.56", "seven", "8.9"]);
4822        let b = cast(&array, &DataType::Float32).unwrap();
4823        let c = b.as_primitive::<Float32Type>();
4824        assert_eq!(3.0, c.value(0));
4825        assert_eq!(4.56, c.value(1));
4826        assert!(!c.is_valid(2));
4827        assert_eq!(8.9, c.value(3));
4828    }
4829
4830    #[test]
4831    fn test_cast_string_to_f16() {
4832        let arrays = [
4833            Arc::new(StringViewArray::from(vec!["3", "4.56", "seven", "8.9"])) as ArrayRef,
4834            Arc::new(StringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4835            Arc::new(LargeStringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4836        ];
4837        for array in arrays {
4838            let b = cast(&array, &DataType::Float16).unwrap();
4839            let c = b.as_primitive::<Float16Type>();
4840            assert_eq!(half::f16::from_f32(3.0), c.value(0));
4841            assert_eq!(half::f16::from_f32(4.56), c.value(1));
4842            assert!(!c.is_valid(2));
4843            assert_eq!(half::f16::from_f32(8.9), c.value(3));
4844        }
4845    }
4846
4847    #[test]
4848    fn test_cast_utf8view_to_decimal128() {
4849        let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
4850        let arr = Arc::new(array) as ArrayRef;
4851        generate_cast_test_case!(
4852            &arr,
4853            Decimal128Array,
4854            &DataType::Decimal128(4, 2),
4855            vec![None, Some(400_i128), Some(560_i128), Some(789_i128)]
4856        );
4857    }
4858
4859    #[test]
4860    fn test_cast_with_options_utf8_to_i32() {
4861        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4862        let result = cast_with_options(
4863            &array,
4864            &DataType::Int32,
4865            &CastOptions {
4866                safe: false,
4867                format_options: FormatOptions::default(),
4868            },
4869        );
4870        match result {
4871            Ok(_) => panic!("expected error"),
4872            Err(e) => {
4873                assert!(
4874                    e.to_string()
4875                        .contains("Cast error: Cannot cast string 'seven' to value of Int32 type",),
4876                    "Error: {e}"
4877                )
4878            }
4879        }
4880    }
4881
4882    #[test]
4883    fn test_cast_utf8_to_bool() {
4884        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4885        let casted = cast(&strings, &DataType::Boolean).unwrap();
4886        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4887        assert_eq!(*as_boolean_array(&casted), expected);
4888    }
4889
4890    #[test]
4891    fn test_cast_utf8view_to_bool() {
4892        let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4893        let casted = cast(&strings, &DataType::Boolean).unwrap();
4894        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4895        assert_eq!(*as_boolean_array(&casted), expected);
4896    }
4897
4898    #[test]
4899    fn test_cast_with_options_utf8_to_bool() {
4900        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4901        let casted = cast_with_options(
4902            &strings,
4903            &DataType::Boolean,
4904            &CastOptions {
4905                safe: false,
4906                format_options: FormatOptions::default(),
4907            },
4908        );
4909        match casted {
4910            Ok(_) => panic!("expected error"),
4911            Err(e) => {
4912                assert!(
4913                    e.to_string().contains(
4914                        "Cast error: Cannot cast value 'invalid' to value of Boolean type"
4915                    )
4916                )
4917            }
4918        }
4919    }
4920
4921    #[test]
4922    fn test_cast_bool_to_i32() {
4923        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4924        let b = cast(&array, &DataType::Int32).unwrap();
4925        let c = b.as_primitive::<Int32Type>();
4926        assert_eq!(1, c.value(0));
4927        assert_eq!(0, c.value(1));
4928        assert!(!c.is_valid(2));
4929    }
4930
4931    #[test]
4932    fn test_cast_bool_to_utf8view() {
4933        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4934        let b = cast(&array, &DataType::Utf8View).unwrap();
4935        let c = b.as_any().downcast_ref::<StringViewArray>().unwrap();
4936        assert_eq!("true", c.value(0));
4937        assert_eq!("false", c.value(1));
4938        assert!(!c.is_valid(2));
4939    }
4940
4941    #[test]
4942    fn test_cast_bool_to_utf8() {
4943        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4944        let b = cast(&array, &DataType::Utf8).unwrap();
4945        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
4946        assert_eq!("true", c.value(0));
4947        assert_eq!("false", c.value(1));
4948        assert!(!c.is_valid(2));
4949    }
4950
4951    #[test]
4952    fn test_cast_bool_to_large_utf8() {
4953        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4954        let b = cast(&array, &DataType::LargeUtf8).unwrap();
4955        let c = b.as_any().downcast_ref::<LargeStringArray>().unwrap();
4956        assert_eq!("true", c.value(0));
4957        assert_eq!("false", c.value(1));
4958        assert!(!c.is_valid(2));
4959    }
4960
4961    #[test]
4962    fn test_cast_bool_to_f64() {
4963        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4964        let b = cast(&array, &DataType::Float64).unwrap();
4965        let c = b.as_primitive::<Float64Type>();
4966        assert_eq!(1.0, c.value(0));
4967        assert_eq!(0.0, c.value(1));
4968        assert!(!c.is_valid(2));
4969    }
4970
4971    #[test]
4972    fn test_cast_integer_to_timestamp() {
4973        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4974        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4975
4976        let array = Int8Array::from(vec![Some(2), Some(10), None]);
4977        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4978
4979        assert_eq!(&actual, &expected);
4980
4981        let array = Int16Array::from(vec![Some(2), Some(10), None]);
4982        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4983
4984        assert_eq!(&actual, &expected);
4985
4986        let array = Int32Array::from(vec![Some(2), Some(10), None]);
4987        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4988
4989        assert_eq!(&actual, &expected);
4990
4991        let array = UInt8Array::from(vec![Some(2), Some(10), None]);
4992        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4993
4994        assert_eq!(&actual, &expected);
4995
4996        let array = UInt16Array::from(vec![Some(2), Some(10), None]);
4997        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4998
4999        assert_eq!(&actual, &expected);
5000
5001        let array = UInt32Array::from(vec![Some(2), Some(10), None]);
5002        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5003
5004        assert_eq!(&actual, &expected);
5005
5006        let array = UInt64Array::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
5012    #[test]
5013    fn test_cast_timestamp_to_integer() {
5014        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
5015            .with_timezone("UTC".to_string());
5016        let expected = cast(&array, &DataType::Int64).unwrap();
5017
5018        let actual = cast(&cast(&array, &DataType::Int8).unwrap(), &DataType::Int64).unwrap();
5019        assert_eq!(&actual, &expected);
5020
5021        let actual = cast(&cast(&array, &DataType::Int16).unwrap(), &DataType::Int64).unwrap();
5022        assert_eq!(&actual, &expected);
5023
5024        let actual = cast(&cast(&array, &DataType::Int32).unwrap(), &DataType::Int64).unwrap();
5025        assert_eq!(&actual, &expected);
5026
5027        let actual = cast(&cast(&array, &DataType::UInt8).unwrap(), &DataType::Int64).unwrap();
5028        assert_eq!(&actual, &expected);
5029
5030        let actual = cast(&cast(&array, &DataType::UInt16).unwrap(), &DataType::Int64).unwrap();
5031        assert_eq!(&actual, &expected);
5032
5033        let actual = cast(&cast(&array, &DataType::UInt32).unwrap(), &DataType::Int64).unwrap();
5034        assert_eq!(&actual, &expected);
5035
5036        let actual = cast(&cast(&array, &DataType::UInt64).unwrap(), &DataType::Int64).unwrap();
5037        assert_eq!(&actual, &expected);
5038    }
5039
5040    #[test]
5041    fn test_cast_floating_to_timestamp() {
5042        let array = Int64Array::from(vec![Some(2), Some(10), None]);
5043        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5044
5045        let array = Float16Array::from(vec![
5046            Some(f16::from_f32(2.0)),
5047            Some(f16::from_f32(10.6)),
5048            None,
5049        ]);
5050        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5051
5052        assert_eq!(&actual, &expected);
5053
5054        let array = Float32Array::from(vec![Some(2.0), Some(10.6), None]);
5055        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5056
5057        assert_eq!(&actual, &expected);
5058
5059        let array = Float64Array::from(vec![Some(2.1), Some(10.2), None]);
5060        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5061
5062        assert_eq!(&actual, &expected);
5063    }
5064
5065    #[test]
5066    fn test_cast_timestamp_to_floating() {
5067        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
5068            .with_timezone("UTC".to_string());
5069        let expected = cast(&array, &DataType::Int64).unwrap();
5070
5071        let actual = cast(&cast(&array, &DataType::Float16).unwrap(), &DataType::Int64).unwrap();
5072        assert_eq!(&actual, &expected);
5073
5074        let actual = cast(&cast(&array, &DataType::Float32).unwrap(), &DataType::Int64).unwrap();
5075        assert_eq!(&actual, &expected);
5076
5077        let actual = cast(&cast(&array, &DataType::Float64).unwrap(), &DataType::Int64).unwrap();
5078        assert_eq!(&actual, &expected);
5079    }
5080
5081    #[test]
5082    fn test_cast_decimal_to_timestamp() {
5083        let array = Int64Array::from(vec![Some(2), Some(10), None]);
5084        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5085
5086        let array = Decimal128Array::from(vec![Some(200), Some(1000), None])
5087            .with_precision_and_scale(4, 2)
5088            .unwrap();
5089        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5090
5091        assert_eq!(&actual, &expected);
5092
5093        let array = Decimal256Array::from(vec![
5094            Some(i256::from_i128(2000)),
5095            Some(i256::from_i128(10000)),
5096            None,
5097        ])
5098        .with_precision_and_scale(5, 3)
5099        .unwrap();
5100        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5101
5102        assert_eq!(&actual, &expected);
5103    }
5104
5105    #[test]
5106    fn test_cast_timestamp_to_decimal() {
5107        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
5108            .with_timezone("UTC".to_string());
5109        let expected = cast(&array, &DataType::Int64).unwrap();
5110
5111        let actual = cast(
5112            &cast(&array, &DataType::Decimal128(5, 2)).unwrap(),
5113            &DataType::Int64,
5114        )
5115        .unwrap();
5116        assert_eq!(&actual, &expected);
5117
5118        let actual = cast(
5119            &cast(&array, &DataType::Decimal256(10, 5)).unwrap(),
5120            &DataType::Int64,
5121        )
5122        .unwrap();
5123        assert_eq!(&actual, &expected);
5124    }
5125
5126    #[test]
5127    fn test_cast_list_i32_to_list_u16() {
5128        let values = vec![
5129            Some(vec![Some(0), Some(0), Some(0)]),
5130            Some(vec![Some(-1), Some(-2), Some(-1)]),
5131            Some(vec![Some(2), Some(100000000)]),
5132        ];
5133        let list_array = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
5134
5135        let target_type = DataType::List(Arc::new(Field::new("item", DataType::UInt16, true)));
5136        assert!(can_cast_types(list_array.data_type(), &target_type));
5137        let cast_array = cast(&list_array, &target_type).unwrap();
5138
5139        // For the ListArray itself, there are no null values (as there were no nulls when they went in)
5140        //
5141        // 3 negative values should get lost when casting to unsigned,
5142        // 1 value should overflow
5143        assert_eq!(0, cast_array.null_count());
5144
5145        // offsets should be the same
5146        let array = cast_array.as_list::<i32>();
5147        assert_eq!(list_array.value_offsets(), array.value_offsets());
5148
5149        assert_eq!(DataType::UInt16, array.value_type());
5150        assert_eq!(3, array.value_length(0));
5151        assert_eq!(3, array.value_length(1));
5152        assert_eq!(2, array.value_length(2));
5153
5154        // expect 4 nulls: negative numbers and overflow
5155        let u16arr = array.values().as_primitive::<UInt16Type>();
5156        assert_eq!(4, u16arr.null_count());
5157
5158        // expect 4 nulls: negative numbers and overflow
5159        let expected: UInt16Array =
5160            vec![Some(0), Some(0), Some(0), None, None, None, Some(2), None]
5161                .into_iter()
5162                .collect();
5163
5164        assert_eq!(u16arr, &expected);
5165    }
5166
5167    #[test]
5168    fn test_cast_list_i32_to_list_timestamp() {
5169        // Construct a value array
5170        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 8, 100000000]).into_data();
5171
5172        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 9]);
5173
5174        // Construct a list array from the above two
5175        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
5176        let list_data = ArrayData::builder(list_data_type)
5177            .len(3)
5178            .add_buffer(value_offsets)
5179            .add_child_data(value_data)
5180            .build()
5181            .unwrap();
5182        let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
5183
5184        let actual = cast(
5185            &list_array,
5186            &DataType::List(Arc::new(Field::new_list_field(
5187                DataType::Timestamp(TimeUnit::Microsecond, None),
5188                true,
5189            ))),
5190        )
5191        .unwrap();
5192
5193        let expected = cast(
5194            &cast(
5195                &list_array,
5196                &DataType::List(Arc::new(Field::new_list_field(DataType::Int64, true))),
5197            )
5198            .unwrap(),
5199            &DataType::List(Arc::new(Field::new_list_field(
5200                DataType::Timestamp(TimeUnit::Microsecond, None),
5201                true,
5202            ))),
5203        )
5204        .unwrap();
5205
5206        assert_eq!(&actual, &expected);
5207    }
5208
5209    #[test]
5210    fn test_cast_date32_to_date64() {
5211        let a = Date32Array::from(vec![10000, 17890]);
5212        let array = Arc::new(a) as ArrayRef;
5213        let b = cast(&array, &DataType::Date64).unwrap();
5214        let c = b.as_primitive::<Date64Type>();
5215        assert_eq!(864000000000, c.value(0));
5216        assert_eq!(1545696000000, c.value(1));
5217    }
5218
5219    #[test]
5220    fn test_cast_date64_to_date32() {
5221        let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5222        let array = Arc::new(a) as ArrayRef;
5223        let b = cast(&array, &DataType::Date32).unwrap();
5224        let c = b.as_primitive::<Date32Type>();
5225        assert_eq!(10000, c.value(0));
5226        assert_eq!(17890, c.value(1));
5227        assert!(c.is_null(2));
5228    }
5229
5230    #[test]
5231    fn test_cast_string_to_integral_overflow() {
5232        let str = Arc::new(StringArray::from(vec![
5233            Some("123"),
5234            Some("-123"),
5235            Some("86374"),
5236            None,
5237        ])) as ArrayRef;
5238
5239        let options = CastOptions {
5240            safe: true,
5241            format_options: FormatOptions::default(),
5242        };
5243        let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
5244        let expected =
5245            Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
5246        assert_eq!(&res, &expected);
5247    }
5248
5249    #[test]
5250    fn test_cast_string_to_timestamp() {
5251        let a0 = Arc::new(StringViewArray::from(vec![
5252            Some("2020-09-08T12:00:00.123456789+00:00"),
5253            Some("Not a valid date"),
5254            None,
5255        ])) as ArrayRef;
5256        let a1 = Arc::new(StringArray::from(vec![
5257            Some("2020-09-08T12:00:00.123456789+00:00"),
5258            Some("Not a valid date"),
5259            None,
5260        ])) as ArrayRef;
5261        let a2 = Arc::new(LargeStringArray::from(vec![
5262            Some("2020-09-08T12:00:00.123456789+00:00"),
5263            Some("Not a valid date"),
5264            None,
5265        ])) as ArrayRef;
5266        for array in &[a0, a1, a2] {
5267            for time_unit in &[
5268                TimeUnit::Second,
5269                TimeUnit::Millisecond,
5270                TimeUnit::Microsecond,
5271                TimeUnit::Nanosecond,
5272            ] {
5273                let to_type = DataType::Timestamp(*time_unit, None);
5274                let b = cast(array, &to_type).unwrap();
5275
5276                match time_unit {
5277                    TimeUnit::Second => {
5278                        let c = b.as_primitive::<TimestampSecondType>();
5279                        assert_eq!(1599566400, c.value(0));
5280                        assert!(c.is_null(1));
5281                        assert!(c.is_null(2));
5282                    }
5283                    TimeUnit::Millisecond => {
5284                        let c = b
5285                            .as_any()
5286                            .downcast_ref::<TimestampMillisecondArray>()
5287                            .unwrap();
5288                        assert_eq!(1599566400123, c.value(0));
5289                        assert!(c.is_null(1));
5290                        assert!(c.is_null(2));
5291                    }
5292                    TimeUnit::Microsecond => {
5293                        let c = b
5294                            .as_any()
5295                            .downcast_ref::<TimestampMicrosecondArray>()
5296                            .unwrap();
5297                        assert_eq!(1599566400123456, c.value(0));
5298                        assert!(c.is_null(1));
5299                        assert!(c.is_null(2));
5300                    }
5301                    TimeUnit::Nanosecond => {
5302                        let c = b
5303                            .as_any()
5304                            .downcast_ref::<TimestampNanosecondArray>()
5305                            .unwrap();
5306                        assert_eq!(1599566400123456789, c.value(0));
5307                        assert!(c.is_null(1));
5308                        assert!(c.is_null(2));
5309                    }
5310                }
5311
5312                let options = CastOptions {
5313                    safe: false,
5314                    format_options: FormatOptions::default(),
5315                };
5316                let err = cast_with_options(array, &to_type, &options).unwrap_err();
5317                assert_eq!(
5318                    err.to_string(),
5319                    "Parser error: Error parsing timestamp from 'Not a valid date': error parsing date"
5320                );
5321            }
5322        }
5323    }
5324
5325    #[test]
5326    fn test_cast_string_to_timestamp_overflow() {
5327        let array = StringArray::from(vec!["9800-09-08T12:00:00.123456789"]);
5328        let result = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5329        let result = result.as_primitive::<TimestampSecondType>();
5330        assert_eq!(result.values(), &[247112596800]);
5331    }
5332
5333    #[test]
5334    fn test_cast_string_to_date32() {
5335        let a0 = Arc::new(StringViewArray::from(vec![
5336            Some("2018-12-25"),
5337            Some("Not a valid date"),
5338            None,
5339        ])) as ArrayRef;
5340        let a1 = Arc::new(StringArray::from(vec![
5341            Some("2018-12-25"),
5342            Some("Not a valid date"),
5343            None,
5344        ])) as ArrayRef;
5345        let a2 = Arc::new(LargeStringArray::from(vec![
5346            Some("2018-12-25"),
5347            Some("Not a valid date"),
5348            None,
5349        ])) as ArrayRef;
5350        for array in &[a0, a1, a2] {
5351            let to_type = DataType::Date32;
5352            let b = cast(array, &to_type).unwrap();
5353            let c = b.as_primitive::<Date32Type>();
5354            assert_eq!(17890, c.value(0));
5355            assert!(c.is_null(1));
5356            assert!(c.is_null(2));
5357
5358            let options = CastOptions {
5359                safe: false,
5360                format_options: FormatOptions::default(),
5361            };
5362            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5363            assert_eq!(
5364                err.to_string(),
5365                "Cast error: Cannot cast string 'Not a valid date' to value of Date32 type"
5366            );
5367        }
5368    }
5369
5370    #[test]
5371    fn test_cast_string_with_large_date_to_date32() {
5372        let array = Arc::new(StringArray::from(vec![
5373            Some("+10999-12-31"),
5374            Some("-0010-02-28"),
5375            Some("0010-02-28"),
5376            Some("0000-01-01"),
5377            Some("-0000-01-01"),
5378            Some("-0001-01-01"),
5379        ])) as ArrayRef;
5380        let to_type = DataType::Date32;
5381        let options = CastOptions {
5382            safe: false,
5383            format_options: FormatOptions::default(),
5384        };
5385        let b = cast_with_options(&array, &to_type, &options).unwrap();
5386        let c = b.as_primitive::<Date32Type>();
5387        assert_eq!(3298139, c.value(0)); // 10999-12-31
5388        assert_eq!(-723122, c.value(1)); // -0010-02-28
5389        assert_eq!(-715817, c.value(2)); // 0010-02-28
5390        assert_eq!(c.value(3), c.value(4)); // Expect 0000-01-01 and -0000-01-01 to be parsed the same
5391        assert_eq!(-719528, c.value(3)); // 0000-01-01
5392        assert_eq!(-719528, c.value(4)); // -0000-01-01
5393        assert_eq!(-719893, c.value(5)); // -0001-01-01
5394    }
5395
5396    #[test]
5397    fn test_cast_invalid_string_with_large_date_to_date32() {
5398        // Large dates need to be prefixed with a + or - sign, otherwise they are not parsed correctly
5399        let array = Arc::new(StringArray::from(vec![Some("10999-12-31")])) as ArrayRef;
5400        let to_type = DataType::Date32;
5401        let options = CastOptions {
5402            safe: false,
5403            format_options: FormatOptions::default(),
5404        };
5405        let err = cast_with_options(&array, &to_type, &options).unwrap_err();
5406        assert_eq!(
5407            err.to_string(),
5408            "Cast error: Cannot cast string '10999-12-31' to value of Date32 type"
5409        );
5410    }
5411
5412    #[test]
5413    fn test_cast_string_format_yyyymmdd_to_date32() {
5414        let a0 = Arc::new(StringViewArray::from(vec![
5415            Some("2020-12-25"),
5416            Some("20201117"),
5417        ])) as ArrayRef;
5418        let a1 = Arc::new(StringArray::from(vec![
5419            Some("2020-12-25"),
5420            Some("20201117"),
5421        ])) as ArrayRef;
5422        let a2 = Arc::new(LargeStringArray::from(vec![
5423            Some("2020-12-25"),
5424            Some("20201117"),
5425        ])) as ArrayRef;
5426
5427        for array in &[a0, a1, a2] {
5428            let to_type = DataType::Date32;
5429            let options = CastOptions {
5430                safe: false,
5431                format_options: FormatOptions::default(),
5432            };
5433            let result = cast_with_options(&array, &to_type, &options).unwrap();
5434            let c = result.as_primitive::<Date32Type>();
5435            assert_eq!(
5436                chrono::NaiveDate::from_ymd_opt(2020, 12, 25),
5437                c.value_as_date(0)
5438            );
5439            assert_eq!(
5440                chrono::NaiveDate::from_ymd_opt(2020, 11, 17),
5441                c.value_as_date(1)
5442            );
5443        }
5444    }
5445
5446    #[test]
5447    fn test_cast_string_to_time32second() {
5448        let a0 = Arc::new(StringViewArray::from(vec![
5449            Some("08:08:35.091323414"),
5450            Some("08:08:60.091323414"), // leap second
5451            Some("08:08:61.091323414"), // not valid
5452            Some("Not a valid time"),
5453            None,
5454        ])) as ArrayRef;
5455        let a1 = Arc::new(StringArray::from(vec![
5456            Some("08:08:35.091323414"),
5457            Some("08:08:60.091323414"), // leap second
5458            Some("08:08:61.091323414"), // not valid
5459            Some("Not a valid time"),
5460            None,
5461        ])) as ArrayRef;
5462        let a2 = Arc::new(LargeStringArray::from(vec![
5463            Some("08:08:35.091323414"),
5464            Some("08:08:60.091323414"), // leap second
5465            Some("08:08:61.091323414"), // not valid
5466            Some("Not a valid time"),
5467            None,
5468        ])) as ArrayRef;
5469        for array in &[a0, a1, a2] {
5470            let to_type = DataType::Time32(TimeUnit::Second);
5471            let b = cast(array, &to_type).unwrap();
5472            let c = b.as_primitive::<Time32SecondType>();
5473            assert_eq!(29315, c.value(0));
5474            assert_eq!(29340, c.value(1));
5475            assert!(c.is_null(2));
5476            assert!(c.is_null(3));
5477            assert!(c.is_null(4));
5478
5479            let options = CastOptions {
5480                safe: false,
5481                format_options: FormatOptions::default(),
5482            };
5483            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5484            assert_eq!(
5485                err.to_string(),
5486                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(s) type"
5487            );
5488        }
5489    }
5490
5491    #[test]
5492    fn test_cast_string_to_time32millisecond() {
5493        let a0 = Arc::new(StringViewArray::from(vec![
5494            Some("08:08:35.091323414"),
5495            Some("08:08:60.091323414"), // leap second
5496            Some("08:08:61.091323414"), // not valid
5497            Some("Not a valid time"),
5498            None,
5499        ])) as ArrayRef;
5500        let a1 = Arc::new(StringArray::from(vec![
5501            Some("08:08:35.091323414"),
5502            Some("08:08:60.091323414"), // leap second
5503            Some("08:08:61.091323414"), // not valid
5504            Some("Not a valid time"),
5505            None,
5506        ])) as ArrayRef;
5507        let a2 = Arc::new(LargeStringArray::from(vec![
5508            Some("08:08:35.091323414"),
5509            Some("08:08:60.091323414"), // leap second
5510            Some("08:08:61.091323414"), // not valid
5511            Some("Not a valid time"),
5512            None,
5513        ])) as ArrayRef;
5514        for array in &[a0, a1, a2] {
5515            let to_type = DataType::Time32(TimeUnit::Millisecond);
5516            let b = cast(array, &to_type).unwrap();
5517            let c = b.as_primitive::<Time32MillisecondType>();
5518            assert_eq!(29315091, c.value(0));
5519            assert_eq!(29340091, c.value(1));
5520            assert!(c.is_null(2));
5521            assert!(c.is_null(3));
5522            assert!(c.is_null(4));
5523
5524            let options = CastOptions {
5525                safe: false,
5526                format_options: FormatOptions::default(),
5527            };
5528            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5529            assert_eq!(
5530                err.to_string(),
5531                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(ms) type"
5532            );
5533        }
5534    }
5535
5536    #[test]
5537    fn test_cast_string_to_time64microsecond() {
5538        let a0 = Arc::new(StringViewArray::from(vec![
5539            Some("08:08:35.091323414"),
5540            Some("Not a valid time"),
5541            None,
5542        ])) as ArrayRef;
5543        let a1 = Arc::new(StringArray::from(vec![
5544            Some("08:08:35.091323414"),
5545            Some("Not a valid time"),
5546            None,
5547        ])) as ArrayRef;
5548        let a2 = Arc::new(LargeStringArray::from(vec![
5549            Some("08:08:35.091323414"),
5550            Some("Not a valid time"),
5551            None,
5552        ])) as ArrayRef;
5553        for array in &[a0, a1, a2] {
5554            let to_type = DataType::Time64(TimeUnit::Microsecond);
5555            let b = cast(array, &to_type).unwrap();
5556            let c = b.as_primitive::<Time64MicrosecondType>();
5557            assert_eq!(29315091323, c.value(0));
5558            assert!(c.is_null(1));
5559            assert!(c.is_null(2));
5560
5561            let options = CastOptions {
5562                safe: false,
5563                format_options: FormatOptions::default(),
5564            };
5565            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5566            assert_eq!(
5567                err.to_string(),
5568                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(µs) type"
5569            );
5570        }
5571    }
5572
5573    #[test]
5574    fn test_cast_string_to_time64nanosecond() {
5575        let a0 = Arc::new(StringViewArray::from(vec![
5576            Some("08:08:35.091323414"),
5577            Some("Not a valid time"),
5578            None,
5579        ])) as ArrayRef;
5580        let a1 = Arc::new(StringArray::from(vec![
5581            Some("08:08:35.091323414"),
5582            Some("Not a valid time"),
5583            None,
5584        ])) as ArrayRef;
5585        let a2 = Arc::new(LargeStringArray::from(vec![
5586            Some("08:08:35.091323414"),
5587            Some("Not a valid time"),
5588            None,
5589        ])) as ArrayRef;
5590        for array in &[a0, a1, a2] {
5591            let to_type = DataType::Time64(TimeUnit::Nanosecond);
5592            let b = cast(array, &to_type).unwrap();
5593            let c = b.as_primitive::<Time64NanosecondType>();
5594            assert_eq!(29315091323414, c.value(0));
5595            assert!(c.is_null(1));
5596            assert!(c.is_null(2));
5597
5598            let options = CastOptions {
5599                safe: false,
5600                format_options: FormatOptions::default(),
5601            };
5602            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5603            assert_eq!(
5604                err.to_string(),
5605                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(ns) type"
5606            );
5607        }
5608    }
5609
5610    #[test]
5611    fn test_cast_string_to_date64() {
5612        let a0 = Arc::new(StringViewArray::from(vec![
5613            Some("2020-09-08T12:00:00"),
5614            Some("Not a valid date"),
5615            None,
5616        ])) as ArrayRef;
5617        let a1 = Arc::new(StringArray::from(vec![
5618            Some("2020-09-08T12:00:00"),
5619            Some("Not a valid date"),
5620            None,
5621        ])) as ArrayRef;
5622        let a2 = Arc::new(LargeStringArray::from(vec![
5623            Some("2020-09-08T12:00:00"),
5624            Some("Not a valid date"),
5625            None,
5626        ])) as ArrayRef;
5627        for array in &[a0, a1, a2] {
5628            let to_type = DataType::Date64;
5629            let b = cast(array, &to_type).unwrap();
5630            let c = b.as_primitive::<Date64Type>();
5631            assert_eq!(1599566400000, c.value(0));
5632            assert!(c.is_null(1));
5633            assert!(c.is_null(2));
5634
5635            let options = CastOptions {
5636                safe: false,
5637                format_options: FormatOptions::default(),
5638            };
5639            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5640            assert_eq!(
5641                err.to_string(),
5642                "Cast error: Cannot cast string 'Not a valid date' to value of Date64 type"
5643            );
5644        }
5645    }
5646
5647    macro_rules! test_safe_string_to_interval {
5648        ($data_vec:expr, $interval_unit:expr, $array_ty:ty, $expect_vec:expr) => {
5649            let source_string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5650
5651            let options = CastOptions {
5652                safe: true,
5653                format_options: FormatOptions::default(),
5654            };
5655
5656            let target_interval_array = cast_with_options(
5657                &source_string_array.clone(),
5658                &DataType::Interval($interval_unit),
5659                &options,
5660            )
5661            .unwrap()
5662            .as_any()
5663            .downcast_ref::<$array_ty>()
5664            .unwrap()
5665            .clone() as $array_ty;
5666
5667            let target_string_array =
5668                cast_with_options(&target_interval_array, &DataType::Utf8, &options)
5669                    .unwrap()
5670                    .as_any()
5671                    .downcast_ref::<StringArray>()
5672                    .unwrap()
5673                    .clone();
5674
5675            let expect_string_array = StringArray::from($expect_vec);
5676
5677            assert_eq!(target_string_array, expect_string_array);
5678
5679            let target_large_string_array =
5680                cast_with_options(&target_interval_array, &DataType::LargeUtf8, &options)
5681                    .unwrap()
5682                    .as_any()
5683                    .downcast_ref::<LargeStringArray>()
5684                    .unwrap()
5685                    .clone();
5686
5687            let expect_large_string_array = LargeStringArray::from($expect_vec);
5688
5689            assert_eq!(target_large_string_array, expect_large_string_array);
5690        };
5691    }
5692
5693    #[test]
5694    fn test_cast_string_to_interval_year_month() {
5695        test_safe_string_to_interval!(
5696            vec![
5697                Some("1 year 1 month"),
5698                Some("1.5 years 13 month"),
5699                Some("30 days"),
5700                Some("31 days"),
5701                Some("2 months 31 days"),
5702                Some("2 months 31 days 1 second"),
5703                Some("foobar"),
5704            ],
5705            IntervalUnit::YearMonth,
5706            IntervalYearMonthArray,
5707            vec![
5708                Some("1 years 1 mons"),
5709                Some("2 years 7 mons"),
5710                None,
5711                None,
5712                None,
5713                None,
5714                None,
5715            ]
5716        );
5717    }
5718
5719    #[test]
5720    fn test_cast_string_to_interval_day_time() {
5721        test_safe_string_to_interval!(
5722            vec![
5723                Some("1 year 1 month"),
5724                Some("1.5 years 13 month"),
5725                Some("30 days"),
5726                Some("1 day 2 second 3.5 milliseconds"),
5727                Some("foobar"),
5728            ],
5729            IntervalUnit::DayTime,
5730            IntervalDayTimeArray,
5731            vec![
5732                Some("390 days"),
5733                Some("930 days"),
5734                Some("30 days"),
5735                None,
5736                None,
5737            ]
5738        );
5739    }
5740
5741    #[test]
5742    fn test_cast_string_to_interval_month_day_nano() {
5743        test_safe_string_to_interval!(
5744            vec![
5745                Some("1 year 1 month 1 day"),
5746                None,
5747                Some("1.5 years 13 month 35 days 1.4 milliseconds"),
5748                Some("3 days"),
5749                Some("8 seconds"),
5750                None,
5751                Some("1 day 29800 milliseconds"),
5752                Some("3 months 1 second"),
5753                Some("6 minutes 120 second"),
5754                Some("2 years 39 months 9 days 19 hours 1 minute 83 seconds 399222 milliseconds"),
5755                Some("foobar"),
5756            ],
5757            IntervalUnit::MonthDayNano,
5758            IntervalMonthDayNanoArray,
5759            vec![
5760                Some("13 mons 1 days"),
5761                None,
5762                Some("31 mons 35 days 0.001400000 secs"),
5763                Some("3 days"),
5764                Some("8.000000000 secs"),
5765                None,
5766                Some("1 days 29.800000000 secs"),
5767                Some("3 mons 1.000000000 secs"),
5768                Some("8 mins"),
5769                Some("63 mons 9 days 19 hours 9 mins 2.222000000 secs"),
5770                None,
5771            ]
5772        );
5773    }
5774
5775    macro_rules! test_unsafe_string_to_interval_err {
5776        ($data_vec:expr, $interval_unit:expr, $error_msg:expr) => {
5777            let string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5778            let options = CastOptions {
5779                safe: false,
5780                format_options: FormatOptions::default(),
5781            };
5782            let arrow_err = cast_with_options(
5783                &string_array.clone(),
5784                &DataType::Interval($interval_unit),
5785                &options,
5786            )
5787            .unwrap_err();
5788            assert_eq!($error_msg, arrow_err.to_string());
5789        };
5790    }
5791
5792    #[test]
5793    fn test_cast_string_to_interval_err() {
5794        test_unsafe_string_to_interval_err!(
5795            vec![Some("foobar")],
5796            IntervalUnit::YearMonth,
5797            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5798        );
5799        test_unsafe_string_to_interval_err!(
5800            vec![Some("foobar")],
5801            IntervalUnit::DayTime,
5802            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5803        );
5804        test_unsafe_string_to_interval_err!(
5805            vec![Some("foobar")],
5806            IntervalUnit::MonthDayNano,
5807            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5808        );
5809        test_unsafe_string_to_interval_err!(
5810            vec![Some("2 months 31 days 1 second")],
5811            IntervalUnit::YearMonth,
5812            r#"Cast error: Cannot cast 2 months 31 days 1 second to IntervalYearMonth. Only year and month fields are allowed."#
5813        );
5814        test_unsafe_string_to_interval_err!(
5815            vec![Some("1 day 1.5 milliseconds")],
5816            IntervalUnit::DayTime,
5817            r#"Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"#
5818        );
5819
5820        // overflow
5821        test_unsafe_string_to_interval_err!(
5822            vec![Some(format!(
5823                "{} century {} year {} month",
5824                i64::MAX - 2,
5825                i64::MAX - 2,
5826                i64::MAX - 2
5827            ))],
5828            IntervalUnit::DayTime,
5829            format!(
5830                "Arithmetic overflow: Overflow happened on: {} * 100",
5831                i64::MAX - 2
5832            )
5833        );
5834        test_unsafe_string_to_interval_err!(
5835            vec![Some(format!(
5836                "{} year {} month {} day",
5837                i64::MAX - 2,
5838                i64::MAX - 2,
5839                i64::MAX - 2
5840            ))],
5841            IntervalUnit::MonthDayNano,
5842            format!(
5843                "Arithmetic overflow: Overflow happened on: {} * 12",
5844                i64::MAX - 2
5845            )
5846        );
5847    }
5848
5849    #[test]
5850    fn test_cast_binary_to_fixed_size_binary() {
5851        let bytes_1 = "Hiiii".as_bytes();
5852        let bytes_2 = "Hello".as_bytes();
5853
5854        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5855        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5856        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5857
5858        let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
5859        let down_cast = array_ref
5860            .as_any()
5861            .downcast_ref::<FixedSizeBinaryArray>()
5862            .unwrap();
5863        assert_eq!(bytes_1, down_cast.value(0));
5864        assert_eq!(bytes_2, down_cast.value(1));
5865        assert!(down_cast.is_null(2));
5866
5867        let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
5868        let down_cast = array_ref
5869            .as_any()
5870            .downcast_ref::<FixedSizeBinaryArray>()
5871            .unwrap();
5872        assert_eq!(bytes_1, down_cast.value(0));
5873        assert_eq!(bytes_2, down_cast.value(1));
5874        assert!(down_cast.is_null(2));
5875
5876        // test error cases when the length of binary are not same
5877        let bytes_1 = "Hi".as_bytes();
5878        let bytes_2 = "Hello".as_bytes();
5879
5880        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5881        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5882        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5883
5884        let array_ref = cast_with_options(
5885            &a1,
5886            &DataType::FixedSizeBinary(5),
5887            &CastOptions {
5888                safe: false,
5889                format_options: FormatOptions::default(),
5890            },
5891        );
5892        assert!(array_ref.is_err());
5893
5894        let array_ref = cast_with_options(
5895            &a2,
5896            &DataType::FixedSizeBinary(5),
5897            &CastOptions {
5898                safe: false,
5899                format_options: FormatOptions::default(),
5900            },
5901        );
5902        assert!(array_ref.is_err());
5903    }
5904
5905    #[test]
5906    fn test_fixed_size_binary_to_binary() {
5907        let bytes_1 = "Hiiii".as_bytes();
5908        let bytes_2 = "Hello".as_bytes();
5909
5910        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5911        let a1 = Arc::new(FixedSizeBinaryArray::try_from(binary_data.clone()).unwrap()) as ArrayRef;
5912
5913        let array_ref = cast(&a1, &DataType::Binary).unwrap();
5914        let down_cast = array_ref.as_binary::<i32>();
5915        assert_eq!(bytes_1, down_cast.value(0));
5916        assert_eq!(bytes_2, down_cast.value(1));
5917        assert!(down_cast.is_null(2));
5918
5919        let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
5920        let down_cast = array_ref.as_binary::<i64>();
5921        assert_eq!(bytes_1, down_cast.value(0));
5922        assert_eq!(bytes_2, down_cast.value(1));
5923        assert!(down_cast.is_null(2));
5924
5925        let array_ref = cast(&a1, &DataType::BinaryView).unwrap();
5926        let down_cast = array_ref.as_binary_view();
5927        assert_eq!(bytes_1, down_cast.value(0));
5928        assert_eq!(bytes_2, down_cast.value(1));
5929        assert!(down_cast.is_null(2));
5930    }
5931
5932    #[test]
5933    fn test_fixed_size_binary_to_dictionary() {
5934        let bytes_1 = "Hiiii".as_bytes();
5935        let bytes_2 = "Hello".as_bytes();
5936
5937        let binary_data = vec![Some(bytes_1), Some(bytes_2), Some(bytes_1), None];
5938        let a1 = Arc::new(FixedSizeBinaryArray::try_from(binary_data.clone()).unwrap()) as ArrayRef;
5939
5940        let cast_type = DataType::Dictionary(
5941            Box::new(DataType::Int8),
5942            Box::new(DataType::FixedSizeBinary(5)),
5943        );
5944        let cast_array = cast(&a1, &cast_type).unwrap();
5945        assert_eq!(cast_array.data_type(), &cast_type);
5946        assert_eq!(
5947            array_to_strings(&cast_array),
5948            vec!["4869696969", "48656c6c6f", "4869696969", "null"]
5949        );
5950        // dictionary should only have two distinct values
5951        let dict_array = cast_array.as_dictionary::<Int8Type>();
5952        assert_eq!(dict_array.values().len(), 2);
5953    }
5954
5955    #[test]
5956    fn test_binary_to_dictionary() {
5957        let mut builder = GenericBinaryBuilder::<i32>::new();
5958        builder.append_value(b"hello");
5959        builder.append_value(b"hiiii");
5960        builder.append_value(b"hiiii"); // duplicate
5961        builder.append_null();
5962        builder.append_value(b"rustt");
5963
5964        let a1 = builder.finish();
5965
5966        let cast_type = DataType::Dictionary(
5967            Box::new(DataType::Int8),
5968            Box::new(DataType::FixedSizeBinary(5)),
5969        );
5970        let cast_array = cast(&a1, &cast_type).unwrap();
5971        assert_eq!(cast_array.data_type(), &cast_type);
5972        assert_eq!(
5973            array_to_strings(&cast_array),
5974            vec![
5975                "68656c6c6f",
5976                "6869696969",
5977                "6869696969",
5978                "null",
5979                "7275737474"
5980            ]
5981        );
5982        // dictionary should only have three distinct values
5983        let dict_array = cast_array.as_dictionary::<Int8Type>();
5984        assert_eq!(dict_array.values().len(), 3);
5985    }
5986
5987    #[test]
5988    fn test_cast_string_array_to_dict_utf8_view() {
5989        let array = StringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
5990
5991        let cast_type =
5992            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5993        assert!(can_cast_types(array.data_type(), &cast_type));
5994        let cast_array = cast(&array, &cast_type).unwrap();
5995        assert_eq!(cast_array.data_type(), &cast_type);
5996
5997        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5998        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5999        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
6000
6001        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6002        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6003        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6004
6005        let keys = dict_array.keys();
6006        assert!(keys.is_null(1));
6007        assert_eq!(keys.value(0), keys.value(3));
6008        assert_ne!(keys.value(0), keys.value(2));
6009    }
6010
6011    #[test]
6012    fn test_cast_string_array_to_dict_utf8_view_null_vs_literal_null() {
6013        let array = StringArray::from(vec![Some("one"), None, Some("null"), Some("one")]);
6014
6015        let cast_type =
6016            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6017        assert!(can_cast_types(array.data_type(), &cast_type));
6018        let cast_array = cast(&array, &cast_type).unwrap();
6019        assert_eq!(cast_array.data_type(), &cast_type);
6020
6021        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6022        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6023        assert_eq!(dict_array.values().len(), 2);
6024
6025        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6026        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6027        assert_eq!(actual, vec![Some("one"), None, Some("null"), Some("one")]);
6028
6029        let keys = dict_array.keys();
6030        assert!(keys.is_null(1));
6031        assert_eq!(keys.value(0), keys.value(3));
6032        assert_ne!(keys.value(0), keys.value(2));
6033    }
6034
6035    #[test]
6036    fn test_cast_string_view_array_to_dict_utf8_view() {
6037        let array = StringViewArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
6038
6039        let cast_type =
6040            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6041        assert!(can_cast_types(array.data_type(), &cast_type));
6042        let cast_array = cast(&array, &cast_type).unwrap();
6043        assert_eq!(cast_array.data_type(), &cast_type);
6044
6045        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6046        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6047        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
6048
6049        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6050        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6051        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6052
6053        let keys = dict_array.keys();
6054        assert!(keys.is_null(1));
6055        assert_eq!(keys.value(0), keys.value(3));
6056        assert_ne!(keys.value(0), keys.value(2));
6057    }
6058
6059    #[test]
6060    fn test_cast_string_view_slice_to_dict_utf8_view() {
6061        let array = StringViewArray::from(vec![
6062            Some("zero"),
6063            Some("one"),
6064            None,
6065            Some("three"),
6066            Some("one"),
6067        ]);
6068        let view = array.slice(1, 4);
6069
6070        let cast_type =
6071            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6072        assert!(can_cast_types(view.data_type(), &cast_type));
6073        let cast_array = cast(&view, &cast_type).unwrap();
6074        assert_eq!(cast_array.data_type(), &cast_type);
6075
6076        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6077        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6078        assert_eq!(dict_array.values().len(), 2);
6079
6080        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6081        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6082        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6083
6084        let keys = dict_array.keys();
6085        assert!(keys.is_null(1));
6086        assert_eq!(keys.value(0), keys.value(3));
6087        assert_ne!(keys.value(0), keys.value(2));
6088    }
6089
6090    #[test]
6091    fn test_cast_binary_array_to_dict_binary_view() {
6092        let mut builder = GenericBinaryBuilder::<i32>::new();
6093        builder.append_value(b"hello");
6094        builder.append_value(b"hiiii");
6095        builder.append_value(b"hiiii"); // duplicate
6096        builder.append_null();
6097        builder.append_value(b"rustt");
6098
6099        let array = builder.finish();
6100
6101        let cast_type =
6102            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6103        assert!(can_cast_types(array.data_type(), &cast_type));
6104        let cast_array = cast(&array, &cast_type).unwrap();
6105        assert_eq!(cast_array.data_type(), &cast_type);
6106
6107        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6108        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6109        assert_eq!(dict_array.values().len(), 3);
6110
6111        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6112        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6113        assert_eq!(
6114            actual,
6115            vec![
6116                Some(b"hello".as_slice()),
6117                Some(b"hiiii".as_slice()),
6118                Some(b"hiiii".as_slice()),
6119                None,
6120                Some(b"rustt".as_slice())
6121            ]
6122        );
6123
6124        let keys = dict_array.keys();
6125        assert!(keys.is_null(3));
6126        assert_eq!(keys.value(1), keys.value(2));
6127        assert_ne!(keys.value(0), keys.value(1));
6128    }
6129
6130    #[test]
6131    fn test_cast_binary_view_array_to_dict_binary_view() {
6132        let view = BinaryViewArray::from_iter([
6133            Some(b"hello".as_slice()),
6134            Some(b"hiiii".as_slice()),
6135            Some(b"hiiii".as_slice()), // duplicate
6136            None,
6137            Some(b"rustt".as_slice()),
6138        ]);
6139
6140        let cast_type =
6141            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6142        assert!(can_cast_types(view.data_type(), &cast_type));
6143        let cast_array = cast(&view, &cast_type).unwrap();
6144        assert_eq!(cast_array.data_type(), &cast_type);
6145
6146        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6147        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6148        assert_eq!(dict_array.values().len(), 3);
6149
6150        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6151        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6152        assert_eq!(
6153            actual,
6154            vec![
6155                Some(b"hello".as_slice()),
6156                Some(b"hiiii".as_slice()),
6157                Some(b"hiiii".as_slice()),
6158                None,
6159                Some(b"rustt".as_slice())
6160            ]
6161        );
6162
6163        let keys = dict_array.keys();
6164        assert!(keys.is_null(3));
6165        assert_eq!(keys.value(1), keys.value(2));
6166        assert_ne!(keys.value(0), keys.value(1));
6167    }
6168
6169    #[test]
6170    fn test_cast_binary_view_slice_to_dict_binary_view() {
6171        let view = BinaryViewArray::from_iter([
6172            Some(b"hello".as_slice()),
6173            Some(b"hiiii".as_slice()),
6174            Some(b"hiiii".as_slice()), // duplicate
6175            None,
6176            Some(b"rustt".as_slice()),
6177        ]);
6178        let sliced = view.slice(1, 4);
6179
6180        let cast_type =
6181            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6182        assert!(can_cast_types(sliced.data_type(), &cast_type));
6183        let cast_array = cast(&sliced, &cast_type).unwrap();
6184        assert_eq!(cast_array.data_type(), &cast_type);
6185
6186        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6187        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6188        assert_eq!(dict_array.values().len(), 2);
6189
6190        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6191        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6192        assert_eq!(
6193            actual,
6194            vec![
6195                Some(b"hiiii".as_slice()),
6196                Some(b"hiiii".as_slice()),
6197                None,
6198                Some(b"rustt".as_slice())
6199            ]
6200        );
6201
6202        let keys = dict_array.keys();
6203        assert!(keys.is_null(2));
6204        assert_eq!(keys.value(0), keys.value(1));
6205        assert_ne!(keys.value(0), keys.value(3));
6206    }
6207
6208    #[test]
6209    fn test_cast_string_array_to_dict_utf8_view_key_overflow_u8() {
6210        let array = StringArray::from_iter_values((0..257).map(|i| format!("v{i}")));
6211
6212        let cast_type =
6213            DataType::Dictionary(Box::new(DataType::UInt8), Box::new(DataType::Utf8View));
6214        assert!(can_cast_types(array.data_type(), &cast_type));
6215        let err = cast(&array, &cast_type).unwrap_err();
6216        assert!(matches!(err, ArrowError::DictionaryKeyOverflowError));
6217    }
6218
6219    #[test]
6220    fn test_cast_large_string_array_to_dict_utf8_view() {
6221        let array = LargeStringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
6222
6223        let cast_type =
6224            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6225        assert!(can_cast_types(array.data_type(), &cast_type));
6226        let cast_array = cast(&array, &cast_type).unwrap();
6227        assert_eq!(cast_array.data_type(), &cast_type);
6228
6229        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6230        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6231        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
6232
6233        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6234        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6235        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6236
6237        let keys = dict_array.keys();
6238        assert!(keys.is_null(1));
6239        assert_eq!(keys.value(0), keys.value(3));
6240        assert_ne!(keys.value(0), keys.value(2));
6241    }
6242
6243    #[test]
6244    fn test_cast_large_binary_array_to_dict_binary_view() {
6245        let mut builder = GenericBinaryBuilder::<i64>::new();
6246        builder.append_value(b"hello");
6247        builder.append_value(b"world");
6248        builder.append_value(b"hello"); // duplicate
6249        builder.append_null();
6250
6251        let array = builder.finish();
6252
6253        let cast_type =
6254            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6255        assert!(can_cast_types(array.data_type(), &cast_type));
6256        let cast_array = cast(&array, &cast_type).unwrap();
6257        assert_eq!(cast_array.data_type(), &cast_type);
6258
6259        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6260        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6261        assert_eq!(dict_array.values().len(), 2); // "hello" and "world" deduplicated
6262
6263        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6264        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6265        assert_eq!(
6266            actual,
6267            vec![
6268                Some(b"hello".as_slice()),
6269                Some(b"world".as_slice()),
6270                Some(b"hello".as_slice()),
6271                None
6272            ]
6273        );
6274
6275        let keys = dict_array.keys();
6276        assert!(keys.is_null(3));
6277        assert_eq!(keys.value(0), keys.value(2));
6278        assert_ne!(keys.value(0), keys.value(1));
6279    }
6280
6281    #[test]
6282    fn test_cast_struct_array_to_dict_struct() {
6283        // Cast a StructArray into Dictionary<UInt32, Struct{…}>. The dictionary
6284        // value type's child fields may differ from the source's (here:
6285        // Utf8 source → Utf8View child for `name`), so the per-field cast
6286        // must run before identity keys are emitted. This is the "as long as
6287        // the struct can be cast to the dict value" contract.
6288        let names = StringArray::from(vec![Some("alpha"), None, Some("gamma")]);
6289        let ids = Int32Array::from(vec![Some(1), Some(2), Some(3)]);
6290        let source = StructArray::from(vec![
6291            (
6292                Arc::new(Field::new("name", DataType::Utf8, true)),
6293                Arc::new(names) as ArrayRef,
6294            ),
6295            (
6296                Arc::new(Field::new("id", DataType::Int32, false)),
6297                Arc::new(ids) as ArrayRef,
6298            ),
6299        ]);
6300
6301        let target_value_type = DataType::Struct(
6302            vec![
6303                Field::new("name", DataType::Utf8View, true),
6304                Field::new("id", DataType::Int64, false),
6305            ]
6306            .into(),
6307        );
6308        let cast_type = DataType::Dictionary(
6309            Box::new(DataType::UInt32),
6310            Box::new(target_value_type.clone()),
6311        );
6312        assert!(can_cast_types(source.data_type(), &cast_type));
6313
6314        let cast_array = cast(&source, &cast_type).unwrap();
6315        assert_eq!(cast_array.data_type(), &cast_type);
6316        assert_eq!(cast_array.len(), 3);
6317
6318        let dict = cast_array.as_dictionary::<UInt32Type>();
6319        assert_eq!(dict.values().data_type(), &target_value_type);
6320        // No dedup is performed for struct values — one row, one key.
6321        assert_eq!(dict.values().len(), 3);
6322
6323        // Source row 1 was a `Utf8`-null in the `name` field but the whole
6324        // struct row was valid (StructArray::from above takes per-field
6325        // nulls only). The dictionary's logical null mask therefore mirrors
6326        // the source struct's row-level null mask — all rows valid here.
6327        let keys = dict.keys();
6328        assert_eq!(keys.values(), &[0u32, 1, 2]);
6329        assert_eq!(keys.null_count(), 0);
6330
6331        let struct_values = dict.values().as_struct();
6332        let names_out = struct_values
6333            .column_by_name("name")
6334            .unwrap()
6335            .as_string_view();
6336        assert_eq!(names_out.value(0), "alpha");
6337        assert!(names_out.is_null(1));
6338        assert_eq!(names_out.value(2), "gamma");
6339        let ids_out = struct_values
6340            .column_by_name("id")
6341            .unwrap()
6342            .as_primitive::<Int64Type>();
6343        assert_eq!(ids_out.values(), &[1i64, 2, 3]);
6344    }
6345
6346    #[test]
6347    fn test_cast_struct_array_to_dict_struct_row_nulls() {
6348        // Row-level nulls on the source struct must surface as null keys on
6349        // the dictionary, since the dictionary's logical null mask is
6350        // determined by the keys.
6351        let names = StringArray::from(vec![Some("alpha"), Some("beta"), Some("gamma")]);
6352        let ids = Int32Array::from(vec![Some(1), Some(2), Some(3)]);
6353        let source = StructArray::try_new(
6354            vec![
6355                Field::new("name", DataType::Utf8, true),
6356                Field::new("id", DataType::Int32, false),
6357            ]
6358            .into(),
6359            vec![Arc::new(names) as ArrayRef, Arc::new(ids) as ArrayRef],
6360            Some(NullBuffer::from(vec![true, false, true])),
6361        )
6362        .unwrap();
6363
6364        let target_value_type = DataType::Struct(
6365            vec![
6366                Field::new("name", DataType::Utf8, true),
6367                Field::new("id", DataType::Int32, false),
6368            ]
6369            .into(),
6370        );
6371        let cast_type =
6372            DataType::Dictionary(Box::new(DataType::UInt32), Box::new(target_value_type));
6373
6374        let cast_array = cast(&source, &cast_type).unwrap();
6375        let dict = cast_array.as_dictionary::<UInt32Type>();
6376        assert_eq!(dict.len(), 3);
6377        let keys = dict.keys();
6378        assert!(!keys.is_null(0));
6379        assert!(keys.is_null(1));
6380        assert!(!keys.is_null(2));
6381    }
6382
6383    #[test]
6384    fn test_cast_struct_array_to_dict_struct_key_overflow() {
6385        // Source has 300 rows but the dictionary key type is UInt8 (max 255).
6386        // We must return a CastError instead of silently truncating.
6387        let n = 300;
6388        let names = StringArray::from((0..n).map(|i| Some(format!("v{i}"))).collect::<Vec<_>>());
6389        let source = StructArray::from(vec![(
6390            Arc::new(Field::new("name", DataType::Utf8, true)),
6391            Arc::new(names) as ArrayRef,
6392        )]);
6393
6394        let cast_type = DataType::Dictionary(
6395            Box::new(DataType::UInt8),
6396            Box::new(DataType::Struct(
6397                vec![Field::new("name", DataType::Utf8, true)].into(),
6398            )),
6399        );
6400        let err = cast(&source, &cast_type).unwrap_err().to_string();
6401        assert!(
6402            err.contains("Cannot fit") && err.contains("dictionary keys"),
6403            "expected key-overflow error, got: {err}"
6404        );
6405    }
6406
6407    #[test]
6408    fn test_cast_empty_string_array_to_dict_utf8_view() {
6409        let array = StringArray::from(Vec::<Option<&str>>::new());
6410
6411        let cast_type =
6412            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6413        assert!(can_cast_types(array.data_type(), &cast_type));
6414        let cast_array = cast(&array, &cast_type).unwrap();
6415        assert_eq!(cast_array.data_type(), &cast_type);
6416        assert_eq!(cast_array.len(), 0);
6417    }
6418
6419    #[test]
6420    fn test_cast_empty_binary_array_to_dict_binary_view() {
6421        let array = BinaryArray::from(Vec::<Option<&[u8]>>::new());
6422
6423        let cast_type =
6424            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6425        assert!(can_cast_types(array.data_type(), &cast_type));
6426        let cast_array = cast(&array, &cast_type).unwrap();
6427        assert_eq!(cast_array.data_type(), &cast_type);
6428        assert_eq!(cast_array.len(), 0);
6429    }
6430
6431    #[test]
6432    fn test_cast_all_null_string_array_to_dict_utf8_view() {
6433        let array = StringArray::from(vec![None::<&str>, None, None]);
6434
6435        let cast_type =
6436            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6437        assert!(can_cast_types(array.data_type(), &cast_type));
6438        let cast_array = cast(&array, &cast_type).unwrap();
6439        assert_eq!(cast_array.data_type(), &cast_type);
6440        assert_eq!(cast_array.null_count(), 3);
6441
6442        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6443        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6444        assert_eq!(dict_array.values().len(), 0);
6445        assert_eq!(dict_array.keys().null_count(), 3);
6446
6447        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6448        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6449        assert_eq!(actual, vec![None, None, None]);
6450    }
6451
6452    #[test]
6453    fn test_cast_all_null_binary_array_to_dict_binary_view() {
6454        let array = BinaryArray::from(vec![None::<&[u8]>, None, None]);
6455
6456        let cast_type =
6457            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6458        assert!(can_cast_types(array.data_type(), &cast_type));
6459        let cast_array = cast(&array, &cast_type).unwrap();
6460        assert_eq!(cast_array.data_type(), &cast_type);
6461        assert_eq!(cast_array.null_count(), 3);
6462
6463        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6464        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6465        assert_eq!(dict_array.values().len(), 0);
6466        assert_eq!(dict_array.keys().null_count(), 3);
6467
6468        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6469        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6470        assert_eq!(actual, vec![None, None, None]);
6471    }
6472
6473    #[test]
6474    fn test_numeric_to_binary() {
6475        let a = Int16Array::from(vec![Some(1), Some(511), None]);
6476
6477        let array_ref = cast(&a, &DataType::Binary).unwrap();
6478        let down_cast = array_ref.as_binary::<i32>();
6479        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6480        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6481        assert!(down_cast.is_null(2));
6482
6483        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6484
6485        let array_ref = cast(&a, &DataType::Binary).unwrap();
6486        let down_cast = array_ref.as_binary::<i32>();
6487        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6488        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6489        assert!(down_cast.is_null(2));
6490    }
6491
6492    #[test]
6493    fn test_numeric_to_large_binary() {
6494        let a = Int16Array::from(vec![Some(1), Some(511), None]);
6495
6496        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6497        let down_cast = array_ref.as_binary::<i64>();
6498        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6499        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6500        assert!(down_cast.is_null(2));
6501
6502        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6503
6504        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6505        let down_cast = array_ref.as_binary::<i64>();
6506        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6507        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6508        assert!(down_cast.is_null(2));
6509    }
6510
6511    #[test]
6512    fn test_cast_date32_to_int32() {
6513        let array = Date32Array::from(vec![10000, 17890]);
6514        let b = cast(&array, &DataType::Int32).unwrap();
6515        let c = b.as_primitive::<Int32Type>();
6516        assert_eq!(10000, c.value(0));
6517        assert_eq!(17890, c.value(1));
6518    }
6519
6520    #[test]
6521    fn test_cast_int32_to_date32() {
6522        let array = Int32Array::from(vec![10000, 17890]);
6523        let b = cast(&array, &DataType::Date32).unwrap();
6524        let c = b.as_primitive::<Date32Type>();
6525        assert_eq!(10000, c.value(0));
6526        assert_eq!(17890, c.value(1));
6527    }
6528
6529    #[test]
6530    fn test_cast_timestamp_to_date32() {
6531        let array =
6532            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6533                .with_timezone("+00:00".to_string());
6534        let b = cast(&array, &DataType::Date32).unwrap();
6535        let c = b.as_primitive::<Date32Type>();
6536        assert_eq!(10000, c.value(0));
6537        assert_eq!(17890, c.value(1));
6538        assert!(c.is_null(2));
6539    }
6540    #[test]
6541    fn test_cast_timestamp_to_date32_zone() {
6542        let strings = StringArray::from_iter([
6543            Some("1970-01-01T00:00:01"),
6544            Some("1970-01-01T23:59:59"),
6545            None,
6546            Some("2020-03-01T02:00:23+00:00"),
6547        ]);
6548        let dt = DataType::Timestamp(TimeUnit::Millisecond, Some("-07:00".into()));
6549        let timestamps = cast(&strings, &dt).unwrap();
6550        let dates = cast(timestamps.as_ref(), &DataType::Date32).unwrap();
6551
6552        let c = dates.as_primitive::<Date32Type>();
6553        let expected = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
6554        assert_eq!(c.value_as_date(0).unwrap(), expected);
6555        assert_eq!(c.value_as_date(1).unwrap(), expected);
6556        assert!(c.is_null(2));
6557        let expected = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
6558        assert_eq!(c.value_as_date(3).unwrap(), expected);
6559    }
6560    #[test]
6561    fn test_cast_timestamp_to_date64() {
6562        let array =
6563            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None]);
6564        let b = cast(&array, &DataType::Date64).unwrap();
6565        let c = b.as_primitive::<Date64Type>();
6566        assert_eq!(864000000005, c.value(0));
6567        assert_eq!(1545696000001, c.value(1));
6568        assert!(c.is_null(2));
6569
6570        let array = TimestampSecondArray::from(vec![Some(864000000005), Some(1545696000001)]);
6571        let b = cast(&array, &DataType::Date64).unwrap();
6572        let c = b.as_primitive::<Date64Type>();
6573        assert_eq!(864000000005000, c.value(0));
6574        assert_eq!(1545696000001000, c.value(1));
6575
6576        // test overflow, safe cast
6577        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6578        let b = cast(&array, &DataType::Date64).unwrap();
6579        assert!(b.is_null(0));
6580        // test overflow, unsafe cast
6581        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6582        let options = CastOptions {
6583            safe: false,
6584            format_options: FormatOptions::default(),
6585        };
6586        let b = cast_with_options(&array, &DataType::Date64, &options);
6587        assert!(b.is_err());
6588    }
6589
6590    #[test]
6591    fn test_cast_timestamp_to_time64() {
6592        // test timestamp secs
6593        let array = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6594            .with_timezone("+01:00".to_string());
6595        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6596        let c = b.as_primitive::<Time64MicrosecondType>();
6597        assert_eq!(3605000000, c.value(0));
6598        assert_eq!(3601000000, c.value(1));
6599        assert!(c.is_null(2));
6600        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6601        let c = b.as_primitive::<Time64NanosecondType>();
6602        assert_eq!(3605000000000, c.value(0));
6603        assert_eq!(3601000000000, c.value(1));
6604        assert!(c.is_null(2));
6605
6606        // test timestamp milliseconds
6607        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6608            .with_timezone("+01:00".to_string());
6609        let array = Arc::new(a) as ArrayRef;
6610        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6611        let c = b.as_primitive::<Time64MicrosecondType>();
6612        assert_eq!(3605000000, c.value(0));
6613        assert_eq!(3601000000, c.value(1));
6614        assert!(c.is_null(2));
6615        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6616        let c = b.as_primitive::<Time64NanosecondType>();
6617        assert_eq!(3605000000000, c.value(0));
6618        assert_eq!(3601000000000, c.value(1));
6619        assert!(c.is_null(2));
6620
6621        // test timestamp microseconds
6622        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6623            .with_timezone("+01:00".to_string());
6624        let array = Arc::new(a) as ArrayRef;
6625        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6626        let c = b.as_primitive::<Time64MicrosecondType>();
6627        assert_eq!(3605000000, c.value(0));
6628        assert_eq!(3601000000, c.value(1));
6629        assert!(c.is_null(2));
6630        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6631        let c = b.as_primitive::<Time64NanosecondType>();
6632        assert_eq!(3605000000000, c.value(0));
6633        assert_eq!(3601000000000, c.value(1));
6634        assert!(c.is_null(2));
6635
6636        // test timestamp nanoseconds
6637        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
6638            .with_timezone("+01:00".to_string());
6639        let array = Arc::new(a) as ArrayRef;
6640        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6641        let c = b.as_primitive::<Time64MicrosecondType>();
6642        assert_eq!(3605000000, c.value(0));
6643        assert_eq!(3601000000, c.value(1));
6644        assert!(c.is_null(2));
6645        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6646        let c = b.as_primitive::<Time64NanosecondType>();
6647        assert_eq!(3605000000000, c.value(0));
6648        assert_eq!(3601000000000, c.value(1));
6649        assert!(c.is_null(2));
6650
6651        // test overflow
6652        let a =
6653            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6654        let array = Arc::new(a) as ArrayRef;
6655        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond));
6656        assert!(b.is_err());
6657        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond));
6658        assert!(b.is_err());
6659        let b = cast(&array, &DataType::Time64(TimeUnit::Millisecond));
6660        assert!(b.is_err());
6661    }
6662
6663    #[test]
6664    fn test_cast_timestamp_to_time32() {
6665        // test timestamp secs
6666        let a = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6667            .with_timezone("+01:00".to_string());
6668        let array = Arc::new(a) as ArrayRef;
6669        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6670        let c = b.as_primitive::<Time32SecondType>();
6671        assert_eq!(3605, c.value(0));
6672        assert_eq!(3601, c.value(1));
6673        assert!(c.is_null(2));
6674        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6675        let c = b.as_primitive::<Time32MillisecondType>();
6676        assert_eq!(3605000, c.value(0));
6677        assert_eq!(3601000, c.value(1));
6678        assert!(c.is_null(2));
6679
6680        // test timestamp milliseconds
6681        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6682            .with_timezone("+01:00".to_string());
6683        let array = Arc::new(a) as ArrayRef;
6684        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6685        let c = b.as_primitive::<Time32SecondType>();
6686        assert_eq!(3605, c.value(0));
6687        assert_eq!(3601, c.value(1));
6688        assert!(c.is_null(2));
6689        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6690        let c = b.as_primitive::<Time32MillisecondType>();
6691        assert_eq!(3605000, c.value(0));
6692        assert_eq!(3601000, c.value(1));
6693        assert!(c.is_null(2));
6694
6695        // test timestamp microseconds
6696        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6697            .with_timezone("+01:00".to_string());
6698        let array = Arc::new(a) as ArrayRef;
6699        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6700        let c = b.as_primitive::<Time32SecondType>();
6701        assert_eq!(3605, c.value(0));
6702        assert_eq!(3601, c.value(1));
6703        assert!(c.is_null(2));
6704        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6705        let c = b.as_primitive::<Time32MillisecondType>();
6706        assert_eq!(3605000, c.value(0));
6707        assert_eq!(3601000, c.value(1));
6708        assert!(c.is_null(2));
6709
6710        // test timestamp nanoseconds
6711        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
6712            .with_timezone("+01:00".to_string());
6713        let array = Arc::new(a) as ArrayRef;
6714        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6715        let c = b.as_primitive::<Time32SecondType>();
6716        assert_eq!(3605, c.value(0));
6717        assert_eq!(3601, c.value(1));
6718        assert!(c.is_null(2));
6719        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6720        let c = b.as_primitive::<Time32MillisecondType>();
6721        assert_eq!(3605000, c.value(0));
6722        assert_eq!(3601000, c.value(1));
6723        assert!(c.is_null(2));
6724
6725        // test overflow
6726        let a =
6727            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6728        let array = Arc::new(a) as ArrayRef;
6729        let b = cast(&array, &DataType::Time32(TimeUnit::Second));
6730        assert!(b.is_err());
6731        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond));
6732        assert!(b.is_err());
6733    }
6734
6735    // Cast Timestamp(_, None) -> Timestamp(_, Some(timezone))
6736    #[test]
6737    fn test_cast_timestamp_with_timezone_1() {
6738        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6739            Some("2000-01-01T00:00:00.123456789"),
6740            Some("2010-01-01T00:00:00.123456789"),
6741            None,
6742        ]));
6743        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6744        let timestamp_array = cast(&string_array, &to_type).unwrap();
6745
6746        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6747        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6748
6749        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6750        let result = string_array.as_string::<i32>();
6751        assert_eq!("2000-01-01T00:00:00.123456+07:00", result.value(0));
6752        assert_eq!("2010-01-01T00:00:00.123456+07:00", result.value(1));
6753        assert!(result.is_null(2));
6754    }
6755
6756    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, None)
6757    #[test]
6758    fn test_cast_timestamp_with_timezone_2() {
6759        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6760            Some("2000-01-01T07:00:00.123456789"),
6761            Some("2010-01-01T07:00:00.123456789"),
6762            None,
6763        ]));
6764        let to_type = DataType::Timestamp(TimeUnit::Millisecond, Some("+0700".into()));
6765        let timestamp_array = cast(&string_array, &to_type).unwrap();
6766
6767        // Check intermediate representation is correct
6768        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6769        let result = string_array.as_string::<i32>();
6770        assert_eq!("2000-01-01T07:00:00.123+07:00", result.value(0));
6771        assert_eq!("2010-01-01T07:00:00.123+07:00", result.value(1));
6772        assert!(result.is_null(2));
6773
6774        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6775        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6776
6777        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6778        let result = string_array.as_string::<i32>();
6779        assert_eq!("2000-01-01T00:00:00.123", result.value(0));
6780        assert_eq!("2010-01-01T00:00:00.123", result.value(1));
6781        assert!(result.is_null(2));
6782    }
6783
6784    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, Some(timezone))
6785    #[test]
6786    fn test_cast_timestamp_with_timezone_3() {
6787        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6788            Some("2000-01-01T07:00:00.123456789"),
6789            Some("2010-01-01T07:00:00.123456789"),
6790            None,
6791        ]));
6792        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6793        let timestamp_array = cast(&string_array, &to_type).unwrap();
6794
6795        // Check intermediate representation is correct
6796        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6797        let result = string_array.as_string::<i32>();
6798        assert_eq!("2000-01-01T07:00:00.123456+07:00", result.value(0));
6799        assert_eq!("2010-01-01T07:00:00.123456+07:00", result.value(1));
6800        assert!(result.is_null(2));
6801
6802        let to_type = DataType::Timestamp(TimeUnit::Second, Some("-08:00".into()));
6803        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6804
6805        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6806        let result = string_array.as_string::<i32>();
6807        assert_eq!("1999-12-31T16:00:00-08:00", result.value(0));
6808        assert_eq!("2009-12-31T16:00:00-08:00", result.value(1));
6809        assert!(result.is_null(2));
6810    }
6811
6812    #[test]
6813    fn test_cast_date64_to_timestamp() {
6814        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6815        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6816        let c = b.as_primitive::<TimestampSecondType>();
6817        assert_eq!(864000000, c.value(0));
6818        assert_eq!(1545696000, c.value(1));
6819        assert!(c.is_null(2));
6820    }
6821
6822    #[test]
6823    fn test_cast_date64_to_timestamp_ms() {
6824        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6825        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
6826        let c = b
6827            .as_any()
6828            .downcast_ref::<TimestampMillisecondArray>()
6829            .unwrap();
6830        assert_eq!(864000000005, c.value(0));
6831        assert_eq!(1545696000001, c.value(1));
6832        assert!(c.is_null(2));
6833    }
6834
6835    #[test]
6836    fn test_cast_date64_to_timestamp_us() {
6837        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6838        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
6839        let c = b
6840            .as_any()
6841            .downcast_ref::<TimestampMicrosecondArray>()
6842            .unwrap();
6843        assert_eq!(864000000005000, c.value(0));
6844        assert_eq!(1545696000001000, c.value(1));
6845        assert!(c.is_null(2));
6846    }
6847
6848    #[test]
6849    fn test_cast_date64_to_timestamp_ns() {
6850        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6851        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
6852        let c = b
6853            .as_any()
6854            .downcast_ref::<TimestampNanosecondArray>()
6855            .unwrap();
6856        assert_eq!(864000000005000000, c.value(0));
6857        assert_eq!(1545696000001000000, c.value(1));
6858        assert!(c.is_null(2));
6859    }
6860
6861    #[test]
6862    fn test_cast_timestamp_to_i64() {
6863        let array =
6864            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6865                .with_timezone("UTC".to_string());
6866        let b = cast(&array, &DataType::Int64).unwrap();
6867        let c = b.as_primitive::<Int64Type>();
6868        assert_eq!(&DataType::Int64, c.data_type());
6869        assert_eq!(864000000005, c.value(0));
6870        assert_eq!(1545696000001, c.value(1));
6871        assert!(c.is_null(2));
6872    }
6873
6874    macro_rules! assert_cast {
6875        ($array:expr, $datatype:expr, $output_array_type: ty, $expected:expr) => {{
6876            assert!(can_cast_types($array.data_type(), &$datatype));
6877            let out = cast(&$array, &$datatype).unwrap();
6878            let actual = out
6879                .as_any()
6880                .downcast_ref::<$output_array_type>()
6881                .unwrap()
6882                .into_iter()
6883                .collect::<Vec<_>>();
6884            assert_eq!(actual, $expected);
6885        }};
6886        ($array:expr, $datatype:expr, $output_array_type: ty, $options:expr, $expected:expr) => {{
6887            assert!(can_cast_types($array.data_type(), &$datatype));
6888            let out = cast_with_options(&$array, &$datatype, &$options).unwrap();
6889            let actual = out
6890                .as_any()
6891                .downcast_ref::<$output_array_type>()
6892                .unwrap()
6893                .into_iter()
6894                .collect::<Vec<_>>();
6895            assert_eq!(actual, $expected);
6896        }};
6897    }
6898
6899    #[test]
6900    fn test_cast_date32_to_string() {
6901        let array = Date32Array::from(vec![Some(0), Some(10000), Some(13036), Some(17890), None]);
6902        let expected = vec![
6903            Some("1970-01-01"),
6904            Some("1997-05-19"),
6905            Some("2005-09-10"),
6906            Some("2018-12-25"),
6907            None,
6908        ];
6909
6910        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6911        assert_cast!(array, DataType::Utf8, StringArray, expected);
6912        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6913    }
6914
6915    #[test]
6916    fn test_cast_date64_to_string() {
6917        let array = Date64Array::from(vec![
6918            Some(0),
6919            Some(10000 * 86400000),
6920            Some(13036 * 86400000),
6921            Some(17890 * 86400000),
6922            None,
6923        ]);
6924        let expected = vec![
6925            Some("1970-01-01T00:00:00"),
6926            Some("1997-05-19T00:00:00"),
6927            Some("2005-09-10T00:00:00"),
6928            Some("2018-12-25T00:00:00"),
6929            None,
6930        ];
6931
6932        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6933        assert_cast!(array, DataType::Utf8, StringArray, expected);
6934        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6935    }
6936
6937    #[test]
6938    fn test_cast_date32_to_timestamp_and_timestamp_with_timezone() {
6939        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6940        let a = Date32Array::from(vec![Some(18628), None, None]); // 2021-1-1, 2022-1-1
6941        let array = Arc::new(a) as ArrayRef;
6942
6943        let b = cast(
6944            &array,
6945            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6946        )
6947        .unwrap();
6948        let c = b.as_primitive::<TimestampSecondType>();
6949        let string_array = cast(&c, &DataType::Utf8).unwrap();
6950        let result = string_array.as_string::<i32>();
6951        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6952
6953        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6954        let c = b.as_primitive::<TimestampSecondType>();
6955        let string_array = cast(&c, &DataType::Utf8).unwrap();
6956        let result = string_array.as_string::<i32>();
6957        assert_eq!("2021-01-01T00:00:00", result.value(0));
6958    }
6959
6960    #[test]
6961    fn test_cast_date32_to_timestamp_with_timezone() {
6962        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6963        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6964        let array = Arc::new(a) as ArrayRef;
6965        let b = cast(
6966            &array,
6967            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6968        )
6969        .unwrap();
6970        let c = b.as_primitive::<TimestampSecondType>();
6971        assert_eq!(1609438500, c.value(0));
6972        assert_eq!(1640974500, c.value(1));
6973        assert!(c.is_null(2));
6974
6975        let string_array = cast(&c, &DataType::Utf8).unwrap();
6976        let result = string_array.as_string::<i32>();
6977        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6978        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6979    }
6980
6981    #[test]
6982    fn test_cast_date32_to_timestamp_with_timezone_ms() {
6983        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6984        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6985        let array = Arc::new(a) as ArrayRef;
6986        let b = cast(
6987            &array,
6988            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
6989        )
6990        .unwrap();
6991        let c = b.as_primitive::<TimestampMillisecondType>();
6992        assert_eq!(1609438500000, c.value(0));
6993        assert_eq!(1640974500000, c.value(1));
6994        assert!(c.is_null(2));
6995
6996        let string_array = cast(&c, &DataType::Utf8).unwrap();
6997        let result = string_array.as_string::<i32>();
6998        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6999        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7000    }
7001
7002    #[test]
7003    fn test_cast_date32_to_timestamp_with_timezone_us() {
7004        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7005        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7006        let array = Arc::new(a) as ArrayRef;
7007        let b = cast(
7008            &array,
7009            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
7010        )
7011        .unwrap();
7012        let c = b.as_primitive::<TimestampMicrosecondType>();
7013        assert_eq!(1609438500000000, c.value(0));
7014        assert_eq!(1640974500000000, c.value(1));
7015        assert!(c.is_null(2));
7016
7017        let string_array = cast(&c, &DataType::Utf8).unwrap();
7018        let result = string_array.as_string::<i32>();
7019        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7020        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7021    }
7022
7023    #[test]
7024    fn test_cast_date32_to_timestamp_with_timezone_ns() {
7025        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7026        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7027        let array = Arc::new(a) as ArrayRef;
7028        let b = cast(
7029            &array,
7030            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
7031        )
7032        .unwrap();
7033        let c = b.as_primitive::<TimestampNanosecondType>();
7034        assert_eq!(1609438500000000000, c.value(0));
7035        assert_eq!(1640974500000000000, c.value(1));
7036        assert!(c.is_null(2));
7037
7038        let string_array = cast(&c, &DataType::Utf8).unwrap();
7039        let result = string_array.as_string::<i32>();
7040        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7041        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7042    }
7043
7044    #[test]
7045    fn test_cast_date64_to_timestamp_with_timezone() {
7046        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7047        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7048        let b = cast(
7049            &array,
7050            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
7051        )
7052        .unwrap();
7053
7054        let c = b.as_primitive::<TimestampSecondType>();
7055        assert_eq!(863979300, c.value(0));
7056        assert_eq!(1545675300, c.value(1));
7057        assert!(c.is_null(2));
7058
7059        let string_array = cast(&c, &DataType::Utf8).unwrap();
7060        let result = string_array.as_string::<i32>();
7061        assert_eq!("1997-05-19T00:00:00+05:45", result.value(0));
7062        assert_eq!("2018-12-25T00:00:00+05:45", result.value(1));
7063    }
7064
7065    #[test]
7066    fn test_cast_date64_to_timestamp_with_timezone_ms() {
7067        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7068        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7069        let b = cast(
7070            &array,
7071            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
7072        )
7073        .unwrap();
7074
7075        let c = b.as_primitive::<TimestampMillisecondType>();
7076        assert_eq!(863979300005, c.value(0));
7077        assert_eq!(1545675300001, c.value(1));
7078        assert!(c.is_null(2));
7079
7080        let string_array = cast(&c, &DataType::Utf8).unwrap();
7081        let result = string_array.as_string::<i32>();
7082        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
7083        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7084    }
7085
7086    #[test]
7087    fn test_cast_date64_to_timestamp_with_timezone_us() {
7088        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7089        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7090        let b = cast(
7091            &array,
7092            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
7093        )
7094        .unwrap();
7095
7096        let c = b.as_primitive::<TimestampMicrosecondType>();
7097        assert_eq!(863979300005000, c.value(0));
7098        assert_eq!(1545675300001000, c.value(1));
7099        assert!(c.is_null(2));
7100
7101        let string_array = cast(&c, &DataType::Utf8).unwrap();
7102        let result = string_array.as_string::<i32>();
7103        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
7104        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7105    }
7106
7107    #[test]
7108    fn test_cast_date64_to_timestamp_with_timezone_ns() {
7109        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7110        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7111        let b = cast(
7112            &array,
7113            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
7114        )
7115        .unwrap();
7116
7117        let c = b.as_primitive::<TimestampNanosecondType>();
7118        assert_eq!(863979300005000000, c.value(0));
7119        assert_eq!(1545675300001000000, c.value(1));
7120        assert!(c.is_null(2));
7121
7122        let string_array = cast(&c, &DataType::Utf8).unwrap();
7123        let result = string_array.as_string::<i32>();
7124        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
7125        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7126    }
7127
7128    #[test]
7129    fn test_cast_timestamp_to_strings() {
7130        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
7131        let array =
7132            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7133        let expected = vec![
7134            Some("1997-05-19T00:00:03.005"),
7135            Some("2018-12-25T00:00:02.001"),
7136            None,
7137        ];
7138
7139        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
7140        assert_cast!(array, DataType::Utf8, StringArray, expected);
7141        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
7142    }
7143
7144    #[test]
7145    fn test_cast_timestamp_to_strings_opt() {
7146        let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
7147        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7148        let cast_options = CastOptions {
7149            safe: true,
7150            format_options: FormatOptions::default()
7151                .with_timestamp_format(Some(ts_format))
7152                .with_timestamp_tz_format(Some(ts_format)),
7153        };
7154
7155        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
7156        let array_without_tz =
7157            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7158        let expected = vec![
7159            Some("1997-05-19 00:00:03.005000"),
7160            Some("2018-12-25 00:00:02.001000"),
7161            None,
7162        ];
7163        assert_cast!(
7164            array_without_tz,
7165            DataType::Utf8View,
7166            StringViewArray,
7167            cast_options,
7168            expected
7169        );
7170        assert_cast!(
7171            array_without_tz,
7172            DataType::Utf8,
7173            StringArray,
7174            cast_options,
7175            expected
7176        );
7177        assert_cast!(
7178            array_without_tz,
7179            DataType::LargeUtf8,
7180            LargeStringArray,
7181            cast_options,
7182            expected
7183        );
7184
7185        let array_with_tz =
7186            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None])
7187                .with_timezone(tz.to_string());
7188        let expected = vec![
7189            Some("1997-05-19 05:45:03.005000"),
7190            Some("2018-12-25 05:45:02.001000"),
7191            None,
7192        ];
7193        assert_cast!(
7194            array_with_tz,
7195            DataType::Utf8View,
7196            StringViewArray,
7197            cast_options,
7198            expected
7199        );
7200        assert_cast!(
7201            array_with_tz,
7202            DataType::Utf8,
7203            StringArray,
7204            cast_options,
7205            expected
7206        );
7207        assert_cast!(
7208            array_with_tz,
7209            DataType::LargeUtf8,
7210            LargeStringArray,
7211            cast_options,
7212            expected
7213        );
7214    }
7215
7216    #[test]
7217    fn test_cast_between_timestamps() {
7218        let array =
7219            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7220        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
7221        let c = b.as_primitive::<TimestampSecondType>();
7222        assert_eq!(864000003, c.value(0));
7223        assert_eq!(1545696002, c.value(1));
7224        assert!(c.is_null(2));
7225    }
7226
7227    #[test]
7228    fn test_cast_duration_to_i64() {
7229        let base = vec![5, 6, 7, 8, 100000000];
7230
7231        let duration_arrays = vec![
7232            Arc::new(DurationNanosecondArray::from(base.clone())) as ArrayRef,
7233            Arc::new(DurationMicrosecondArray::from(base.clone())) as ArrayRef,
7234            Arc::new(DurationMillisecondArray::from(base.clone())) as ArrayRef,
7235            Arc::new(DurationSecondArray::from(base.clone())) as ArrayRef,
7236        ];
7237
7238        for arr in duration_arrays {
7239            assert!(can_cast_types(arr.data_type(), &DataType::Int64));
7240            let result = cast(&arr, &DataType::Int64).unwrap();
7241            let result = result.as_primitive::<Int64Type>();
7242            assert_eq!(base.as_slice(), result.values());
7243        }
7244    }
7245
7246    #[test]
7247    fn test_cast_between_durations_and_numerics() {
7248        fn test_cast_between_durations<FromType, ToType>()
7249        where
7250            FromType: ArrowPrimitiveType<Native = i64>,
7251            ToType: ArrowPrimitiveType<Native = i64>,
7252            PrimitiveArray<FromType>: From<Vec<Option<i64>>>,
7253        {
7254            let from_unit = match FromType::DATA_TYPE {
7255                DataType::Duration(unit) => unit,
7256                _ => panic!("Expected a duration type"),
7257            };
7258            let to_unit = match ToType::DATA_TYPE {
7259                DataType::Duration(unit) => unit,
7260                _ => panic!("Expected a duration type"),
7261            };
7262            let from_size = time_unit_multiple(&from_unit);
7263            let to_size = time_unit_multiple(&to_unit);
7264
7265            let (v1_before, v2_before) = (8640003005, 1696002001);
7266            let (v1_after, v2_after) = if from_size >= to_size {
7267                (
7268                    v1_before / (from_size / to_size),
7269                    v2_before / (from_size / to_size),
7270                )
7271            } else {
7272                (
7273                    v1_before * (to_size / from_size),
7274                    v2_before * (to_size / from_size),
7275                )
7276            };
7277
7278            let array =
7279                PrimitiveArray::<FromType>::from(vec![Some(v1_before), Some(v2_before), None]);
7280            let b = cast(&array, &ToType::DATA_TYPE).unwrap();
7281            let c = b.as_primitive::<ToType>();
7282            assert_eq!(v1_after, c.value(0));
7283            assert_eq!(v2_after, c.value(1));
7284            assert!(c.is_null(2));
7285        }
7286
7287        // between each individual duration type
7288        test_cast_between_durations::<DurationSecondType, DurationMillisecondType>();
7289        test_cast_between_durations::<DurationSecondType, DurationMicrosecondType>();
7290        test_cast_between_durations::<DurationSecondType, DurationNanosecondType>();
7291        test_cast_between_durations::<DurationMillisecondType, DurationSecondType>();
7292        test_cast_between_durations::<DurationMillisecondType, DurationMicrosecondType>();
7293        test_cast_between_durations::<DurationMillisecondType, DurationNanosecondType>();
7294        test_cast_between_durations::<DurationMicrosecondType, DurationSecondType>();
7295        test_cast_between_durations::<DurationMicrosecondType, DurationMillisecondType>();
7296        test_cast_between_durations::<DurationMicrosecondType, DurationNanosecondType>();
7297        test_cast_between_durations::<DurationNanosecondType, DurationSecondType>();
7298        test_cast_between_durations::<DurationNanosecondType, DurationMillisecondType>();
7299        test_cast_between_durations::<DurationNanosecondType, DurationMicrosecondType>();
7300
7301        // cast failed
7302        let array = DurationSecondArray::from(vec![
7303            Some(i64::MAX),
7304            Some(8640203410378005),
7305            Some(10241096),
7306            None,
7307        ]);
7308        let b = cast(&array, &DataType::Duration(TimeUnit::Nanosecond)).unwrap();
7309        let c = b.as_primitive::<DurationNanosecondType>();
7310        assert!(c.is_null(0));
7311        assert!(c.is_null(1));
7312        assert_eq!(10241096000000000, c.value(2));
7313        assert!(c.is_null(3));
7314
7315        // durations to numerics
7316        let array = DurationSecondArray::from(vec![
7317            Some(i64::MAX),
7318            Some(8640203410378005),
7319            Some(10241096),
7320            None,
7321        ]);
7322        let b = cast(&array, &DataType::Int64).unwrap();
7323        let c = b.as_primitive::<Int64Type>();
7324        assert_eq!(i64::MAX, c.value(0));
7325        assert_eq!(8640203410378005, c.value(1));
7326        assert_eq!(10241096, c.value(2));
7327        assert!(c.is_null(3));
7328
7329        let b = cast(&array, &DataType::Int32).unwrap();
7330        let c = b.as_primitive::<Int32Type>();
7331        assert_eq!(0, c.value(0));
7332        assert_eq!(0, c.value(1));
7333        assert_eq!(10241096, c.value(2));
7334        assert!(c.is_null(3));
7335
7336        // numerics to durations
7337        let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), Some(10241096), None]);
7338        let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
7339        let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
7340        assert_eq!(i32::MAX as i64, c.value(0));
7341        assert_eq!(802034103, c.value(1));
7342        assert_eq!(10241096, c.value(2));
7343        assert!(c.is_null(3));
7344    }
7345
7346    #[test]
7347    fn test_cast_to_strings() {
7348        let a = Int32Array::from(vec![1, 2, 3]);
7349        let out = cast(&a, &DataType::Utf8).unwrap();
7350        let out = out
7351            .as_any()
7352            .downcast_ref::<StringArray>()
7353            .unwrap()
7354            .into_iter()
7355            .collect::<Vec<_>>();
7356        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
7357        let out = cast(&a, &DataType::LargeUtf8).unwrap();
7358        let out = out
7359            .as_any()
7360            .downcast_ref::<LargeStringArray>()
7361            .unwrap()
7362            .into_iter()
7363            .collect::<Vec<_>>();
7364        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
7365    }
7366
7367    #[test]
7368    fn test_str_to_str_casts() {
7369        for data in [
7370            vec![Some("foo"), Some("bar"), Some("ham")],
7371            vec![Some("foo"), None, Some("bar")],
7372        ] {
7373            let a = LargeStringArray::from(data.clone());
7374            let to = cast(&a, &DataType::Utf8).unwrap();
7375            let expect = a
7376                .as_any()
7377                .downcast_ref::<LargeStringArray>()
7378                .unwrap()
7379                .into_iter()
7380                .collect::<Vec<_>>();
7381            let out = to
7382                .as_any()
7383                .downcast_ref::<StringArray>()
7384                .unwrap()
7385                .into_iter()
7386                .collect::<Vec<_>>();
7387            assert_eq!(expect, out);
7388
7389            let a = StringArray::from(data);
7390            let to = cast(&a, &DataType::LargeUtf8).unwrap();
7391            let expect = a
7392                .as_any()
7393                .downcast_ref::<StringArray>()
7394                .unwrap()
7395                .into_iter()
7396                .collect::<Vec<_>>();
7397            let out = to
7398                .as_any()
7399                .downcast_ref::<LargeStringArray>()
7400                .unwrap()
7401                .into_iter()
7402                .collect::<Vec<_>>();
7403            assert_eq!(expect, out);
7404        }
7405    }
7406
7407    const VIEW_TEST_DATA: [Option<&str>; 5] = [
7408        Some("hello"),
7409        Some("repeated"),
7410        None,
7411        Some("large payload over 12 bytes"),
7412        Some("repeated"),
7413    ];
7414
7415    #[test]
7416    fn test_string_view_to_binary_view() {
7417        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7418
7419        assert!(can_cast_types(
7420            string_view_array.data_type(),
7421            &DataType::BinaryView
7422        ));
7423
7424        let binary_view_array = cast(&string_view_array, &DataType::BinaryView).unwrap();
7425        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7426
7427        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7428        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7429    }
7430
7431    #[test]
7432    fn test_binary_view_to_string_view() {
7433        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7434
7435        assert!(can_cast_types(
7436            binary_view_array.data_type(),
7437            &DataType::Utf8View
7438        ));
7439
7440        let string_view_array = cast(&binary_view_array, &DataType::Utf8View).unwrap();
7441        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7442
7443        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7444        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7445    }
7446
7447    #[test]
7448    fn test_binary_view_to_string_view_with_invalid_utf8() {
7449        let binary_view_array = BinaryViewArray::from_iter(vec![
7450            Some("valid".as_bytes()),
7451            Some(&[0xff]),
7452            Some("utf8".as_bytes()),
7453            None,
7454        ]);
7455
7456        let strict_options = CastOptions {
7457            safe: false,
7458            ..Default::default()
7459        };
7460
7461        assert!(
7462            cast_with_options(&binary_view_array, &DataType::Utf8View, &strict_options).is_err()
7463        );
7464
7465        let safe_options = CastOptions {
7466            safe: true,
7467            ..Default::default()
7468        };
7469
7470        let string_view_array =
7471            cast_with_options(&binary_view_array, &DataType::Utf8View, &safe_options).unwrap();
7472        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7473
7474        let values: Vec<_> = string_view_array.as_string_view().iter().collect();
7475
7476        assert_eq!(values, vec![Some("valid"), None, Some("utf8"), None]);
7477    }
7478
7479    #[test]
7480    fn test_string_to_view() {
7481        _test_string_to_view::<i32>();
7482        _test_string_to_view::<i64>();
7483    }
7484
7485    fn _test_string_to_view<O>()
7486    where
7487        O: OffsetSizeTrait,
7488    {
7489        let string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7490
7491        assert!(can_cast_types(
7492            string_array.data_type(),
7493            &DataType::Utf8View
7494        ));
7495
7496        assert!(can_cast_types(
7497            string_array.data_type(),
7498            &DataType::BinaryView
7499        ));
7500
7501        let string_view_array = cast(&string_array, &DataType::Utf8View).unwrap();
7502        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7503
7504        let binary_view_array = cast(&string_array, &DataType::BinaryView).unwrap();
7505        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7506
7507        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7508        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7509
7510        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7511        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7512    }
7513
7514    #[test]
7515    fn test_bianry_to_view() {
7516        _test_binary_to_view::<i32>();
7517        _test_binary_to_view::<i64>();
7518    }
7519
7520    fn _test_binary_to_view<O>()
7521    where
7522        O: OffsetSizeTrait,
7523    {
7524        let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7525
7526        assert!(can_cast_types(
7527            binary_array.data_type(),
7528            &DataType::Utf8View
7529        ));
7530
7531        assert!(can_cast_types(
7532            binary_array.data_type(),
7533            &DataType::BinaryView
7534        ));
7535
7536        let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
7537        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7538
7539        let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
7540        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7541
7542        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7543        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7544
7545        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7546        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7547    }
7548
7549    #[test]
7550    fn test_dict_to_view() {
7551        let values = StringArray::from_iter(VIEW_TEST_DATA);
7552        let keys = Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
7553        let string_dict_array =
7554            DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
7555        let typed_dict = string_dict_array.downcast_dict::<StringArray>().unwrap();
7556
7557        let string_view_array = {
7558            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7559            for v in typed_dict.into_iter() {
7560                builder.append_option(v);
7561            }
7562            builder.finish()
7563        };
7564        let expected_string_array_type = string_view_array.data_type();
7565        let casted_string_array = cast(&string_dict_array, expected_string_array_type).unwrap();
7566        assert_eq!(casted_string_array.data_type(), expected_string_array_type);
7567        assert_eq!(casted_string_array.as_ref(), &string_view_array);
7568
7569        let binary_buffer = cast(&typed_dict.values(), &DataType::Binary).unwrap();
7570        let binary_dict_array =
7571            DictionaryArray::<Int8Type>::new(typed_dict.keys().clone(), binary_buffer);
7572        let typed_binary_dict = binary_dict_array.downcast_dict::<BinaryArray>().unwrap();
7573
7574        let binary_view_array = {
7575            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7576            for v in typed_binary_dict.into_iter() {
7577                builder.append_option(v);
7578            }
7579            builder.finish()
7580        };
7581        let expected_binary_array_type = binary_view_array.data_type();
7582        let casted_binary_array = cast(&binary_dict_array, expected_binary_array_type).unwrap();
7583        assert_eq!(casted_binary_array.data_type(), expected_binary_array_type);
7584        assert_eq!(casted_binary_array.as_ref(), &binary_view_array);
7585    }
7586
7587    #[test]
7588    fn test_view_to_dict() {
7589        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7590        let string_dict_array: DictionaryArray<Int8Type> = VIEW_TEST_DATA.into_iter().collect();
7591        let casted_type = string_dict_array.data_type();
7592        let casted_dict_array = cast(&string_view_array, casted_type).unwrap();
7593        assert_eq!(casted_dict_array.data_type(), casted_type);
7594        assert_eq!(casted_dict_array.as_ref(), &string_dict_array);
7595
7596        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7597        let binary_dict_array = string_dict_array.downcast_dict::<StringArray>().unwrap();
7598        let binary_buffer = cast(&binary_dict_array.values(), &DataType::Binary).unwrap();
7599        let binary_dict_array =
7600            DictionaryArray::<Int8Type>::new(binary_dict_array.keys().clone(), binary_buffer);
7601        let casted_type = binary_dict_array.data_type();
7602        let casted_binary_array = cast(&binary_view_array, casted_type).unwrap();
7603        assert_eq!(casted_binary_array.data_type(), casted_type);
7604        assert_eq!(casted_binary_array.as_ref(), &binary_dict_array);
7605    }
7606
7607    #[test]
7608    fn test_view_to_string() {
7609        _test_view_to_string::<i32>();
7610        _test_view_to_string::<i64>();
7611    }
7612
7613    fn _test_view_to_string<O>()
7614    where
7615        O: OffsetSizeTrait,
7616    {
7617        let string_view_array = {
7618            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7619            for s in VIEW_TEST_DATA.iter() {
7620                builder.append_option(*s);
7621            }
7622            builder.finish()
7623        };
7624
7625        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7626
7627        let expected_string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7628        let expected_type = expected_string_array.data_type();
7629
7630        assert!(can_cast_types(string_view_array.data_type(), expected_type));
7631        assert!(can_cast_types(binary_view_array.data_type(), expected_type));
7632
7633        let string_view_casted_array = cast(&string_view_array, expected_type).unwrap();
7634        assert_eq!(string_view_casted_array.data_type(), expected_type);
7635        assert_eq!(string_view_casted_array.as_ref(), &expected_string_array);
7636
7637        let binary_view_casted_array = cast(&binary_view_array, expected_type).unwrap();
7638        assert_eq!(binary_view_casted_array.data_type(), expected_type);
7639        assert_eq!(binary_view_casted_array.as_ref(), &expected_string_array);
7640    }
7641
7642    #[test]
7643    fn test_view_to_binary() {
7644        _test_view_to_binary::<i32>();
7645        _test_view_to_binary::<i64>();
7646    }
7647
7648    fn _test_view_to_binary<O>()
7649    where
7650        O: OffsetSizeTrait,
7651    {
7652        let view_array = {
7653            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7654            for s in VIEW_TEST_DATA.iter() {
7655                builder.append_option(*s);
7656            }
7657            builder.finish()
7658        };
7659
7660        let expected_binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7661        let expected_type = expected_binary_array.data_type();
7662
7663        assert!(can_cast_types(view_array.data_type(), expected_type));
7664
7665        let binary_array = cast(&view_array, expected_type).unwrap();
7666        assert_eq!(binary_array.data_type(), expected_type);
7667
7668        assert_eq!(binary_array.as_ref(), &expected_binary_array);
7669    }
7670
7671    #[test]
7672    fn test_cast_from_f64() {
7673        let f64_values: Vec<f64> = vec![
7674            i64::MIN as f64,
7675            i32::MIN as f64,
7676            i16::MIN as f64,
7677            i8::MIN as f64,
7678            0_f64,
7679            u8::MAX as f64,
7680            u16::MAX as f64,
7681            u32::MAX as f64,
7682            u64::MAX as f64,
7683        ];
7684        let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values));
7685
7686        let f64_expected = vec![
7687            -9223372036854776000.0,
7688            -2147483648.0,
7689            -32768.0,
7690            -128.0,
7691            0.0,
7692            255.0,
7693            65535.0,
7694            4294967295.0,
7695            18446744073709552000.0,
7696        ];
7697        assert_eq!(
7698            f64_expected,
7699            get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
7700                .iter()
7701                .map(|i| i.parse::<f64>().unwrap())
7702                .collect::<Vec<f64>>()
7703        );
7704
7705        let f32_expected = vec![
7706            -9223372000000000000.0,
7707            -2147483600.0,
7708            -32768.0,
7709            -128.0,
7710            0.0,
7711            255.0,
7712            65535.0,
7713            4294967300.0,
7714            18446744000000000000.0,
7715        ];
7716        assert_eq!(
7717            f32_expected,
7718            get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
7719                .iter()
7720                .map(|i| i.parse::<f32>().unwrap())
7721                .collect::<Vec<f32>>()
7722        );
7723
7724        let f16_expected = vec![
7725            f16::from_f64(-9223372000000000000.0),
7726            f16::from_f64(-2147483600.0),
7727            f16::from_f64(-32768.0),
7728            f16::from_f64(-128.0),
7729            f16::from_f64(0.0),
7730            f16::from_f64(255.0),
7731            f16::from_f64(65535.0),
7732            f16::from_f64(4294967300.0),
7733            f16::from_f64(18446744000000000000.0),
7734        ];
7735        assert_eq!(
7736            f16_expected,
7737            get_cast_values::<Float16Type>(&f64_array, &DataType::Float16)
7738                .iter()
7739                .map(|i| i.parse::<f16>().unwrap())
7740                .collect::<Vec<f16>>()
7741        );
7742
7743        let i64_expected = vec![
7744            "-9223372036854775808",
7745            "-2147483648",
7746            "-32768",
7747            "-128",
7748            "0",
7749            "255",
7750            "65535",
7751            "4294967295",
7752            "null",
7753        ];
7754        assert_eq!(
7755            i64_expected,
7756            get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
7757        );
7758
7759        let i32_expected = vec![
7760            "null",
7761            "-2147483648",
7762            "-32768",
7763            "-128",
7764            "0",
7765            "255",
7766            "65535",
7767            "null",
7768            "null",
7769        ];
7770        assert_eq!(
7771            i32_expected,
7772            get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
7773        );
7774
7775        let i16_expected = vec![
7776            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7777        ];
7778        assert_eq!(
7779            i16_expected,
7780            get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
7781        );
7782
7783        let i8_expected = vec![
7784            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7785        ];
7786        assert_eq!(
7787            i8_expected,
7788            get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
7789        );
7790
7791        let u64_expected = vec![
7792            "null",
7793            "null",
7794            "null",
7795            "null",
7796            "0",
7797            "255",
7798            "65535",
7799            "4294967295",
7800            "null",
7801        ];
7802        assert_eq!(
7803            u64_expected,
7804            get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
7805        );
7806
7807        let u32_expected = vec![
7808            "null",
7809            "null",
7810            "null",
7811            "null",
7812            "0",
7813            "255",
7814            "65535",
7815            "4294967295",
7816            "null",
7817        ];
7818        assert_eq!(
7819            u32_expected,
7820            get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
7821        );
7822
7823        let u16_expected = vec![
7824            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7825        ];
7826        assert_eq!(
7827            u16_expected,
7828            get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
7829        );
7830
7831        let u8_expected = vec![
7832            "null", "null", "null", "null", "0", "255", "null", "null", "null",
7833        ];
7834        assert_eq!(
7835            u8_expected,
7836            get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
7837        );
7838    }
7839
7840    #[test]
7841    fn test_cast_from_f32() {
7842        let f32_values: Vec<f32> = vec![
7843            i32::MIN as f32,
7844            i32::MIN as f32,
7845            i16::MIN as f32,
7846            i8::MIN as f32,
7847            0_f32,
7848            u8::MAX as f32,
7849            u16::MAX as f32,
7850            u32::MAX as f32,
7851            u32::MAX as f32,
7852        ];
7853        let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values));
7854
7855        let f64_expected = vec![
7856            "-2147483648.0",
7857            "-2147483648.0",
7858            "-32768.0",
7859            "-128.0",
7860            "0.0",
7861            "255.0",
7862            "65535.0",
7863            "4294967296.0",
7864            "4294967296.0",
7865        ];
7866        assert_eq!(
7867            f64_expected,
7868            get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
7869        );
7870
7871        let f32_expected = vec![
7872            "-2147483600.0",
7873            "-2147483600.0",
7874            "-32768.0",
7875            "-128.0",
7876            "0.0",
7877            "255.0",
7878            "65535.0",
7879            "4294967300.0",
7880            "4294967300.0",
7881        ];
7882        assert_eq!(
7883            f32_expected,
7884            get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
7885        );
7886
7887        let f16_expected = vec![
7888            "-inf", "-inf", "-32768.0", "-128.0", "0.0", "255.0", "inf", "inf", "inf",
7889        ];
7890        assert_eq!(
7891            f16_expected,
7892            get_cast_values::<Float16Type>(&f32_array, &DataType::Float16)
7893        );
7894
7895        let i64_expected = vec![
7896            "-2147483648",
7897            "-2147483648",
7898            "-32768",
7899            "-128",
7900            "0",
7901            "255",
7902            "65535",
7903            "4294967296",
7904            "4294967296",
7905        ];
7906        assert_eq!(
7907            i64_expected,
7908            get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
7909        );
7910
7911        let i32_expected = vec![
7912            "-2147483648",
7913            "-2147483648",
7914            "-32768",
7915            "-128",
7916            "0",
7917            "255",
7918            "65535",
7919            "null",
7920            "null",
7921        ];
7922        assert_eq!(
7923            i32_expected,
7924            get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
7925        );
7926
7927        let i16_expected = vec![
7928            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7929        ];
7930        assert_eq!(
7931            i16_expected,
7932            get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
7933        );
7934
7935        let i8_expected = vec![
7936            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7937        ];
7938        assert_eq!(
7939            i8_expected,
7940            get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
7941        );
7942
7943        let u64_expected = vec![
7944            "null",
7945            "null",
7946            "null",
7947            "null",
7948            "0",
7949            "255",
7950            "65535",
7951            "4294967296",
7952            "4294967296",
7953        ];
7954        assert_eq!(
7955            u64_expected,
7956            get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
7957        );
7958
7959        let u32_expected = vec![
7960            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7961        ];
7962        assert_eq!(
7963            u32_expected,
7964            get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
7965        );
7966
7967        let u16_expected = vec![
7968            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7969        ];
7970        assert_eq!(
7971            u16_expected,
7972            get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
7973        );
7974
7975        let u8_expected = vec![
7976            "null", "null", "null", "null", "0", "255", "null", "null", "null",
7977        ];
7978        assert_eq!(
7979            u8_expected,
7980            get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
7981        );
7982    }
7983
7984    #[test]
7985    fn test_cast_from_uint64() {
7986        let u64_values: Vec<u64> = vec![
7987            0,
7988            u8::MAX as u64,
7989            u16::MAX as u64,
7990            u32::MAX as u64,
7991            u64::MAX,
7992        ];
7993        let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values));
7994
7995        let f64_expected = vec![0.0, 255.0, 65535.0, 4294967295.0, 18446744073709552000.0];
7996        assert_eq!(
7997            f64_expected,
7998            get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
7999                .iter()
8000                .map(|i| i.parse::<f64>().unwrap())
8001                .collect::<Vec<f64>>()
8002        );
8003
8004        let f32_expected = vec![0.0, 255.0, 65535.0, 4294967300.0, 18446744000000000000.0];
8005        assert_eq!(
8006            f32_expected,
8007            get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
8008                .iter()
8009                .map(|i| i.parse::<f32>().unwrap())
8010                .collect::<Vec<f32>>()
8011        );
8012
8013        let f16_expected = vec![
8014            f16::from_f64(0.0),
8015            f16::from_f64(255.0),
8016            f16::from_f64(65535.0),
8017            f16::from_f64(4294967300.0),
8018            f16::from_f64(18446744000000000000.0),
8019        ];
8020        assert_eq!(
8021            f16_expected,
8022            get_cast_values::<Float16Type>(&u64_array, &DataType::Float16)
8023                .iter()
8024                .map(|i| i.parse::<f16>().unwrap())
8025                .collect::<Vec<f16>>()
8026        );
8027
8028        let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
8029        assert_eq!(
8030            i64_expected,
8031            get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
8032        );
8033
8034        let i32_expected = vec!["0", "255", "65535", "null", "null"];
8035        assert_eq!(
8036            i32_expected,
8037            get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
8038        );
8039
8040        let i16_expected = vec!["0", "255", "null", "null", "null"];
8041        assert_eq!(
8042            i16_expected,
8043            get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
8044        );
8045
8046        let i8_expected = vec!["0", "null", "null", "null", "null"];
8047        assert_eq!(
8048            i8_expected,
8049            get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
8050        );
8051
8052        let u64_expected = vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
8053        assert_eq!(
8054            u64_expected,
8055            get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
8056        );
8057
8058        let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
8059        assert_eq!(
8060            u32_expected,
8061            get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
8062        );
8063
8064        let u16_expected = vec!["0", "255", "65535", "null", "null"];
8065        assert_eq!(
8066            u16_expected,
8067            get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
8068        );
8069
8070        let u8_expected = vec!["0", "255", "null", "null", "null"];
8071        assert_eq!(
8072            u8_expected,
8073            get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
8074        );
8075    }
8076
8077    #[test]
8078    fn test_cast_from_uint32() {
8079        let u32_values: Vec<u32> = vec![0, u8::MAX as u32, u16::MAX as u32, u32::MAX];
8080        let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values));
8081
8082        let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
8083        assert_eq!(
8084            f64_expected,
8085            get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
8086        );
8087
8088        let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
8089        assert_eq!(
8090            f32_expected,
8091            get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
8092        );
8093
8094        let f16_expected = vec!["0.0", "255.0", "inf", "inf"];
8095        assert_eq!(
8096            f16_expected,
8097            get_cast_values::<Float16Type>(&u32_array, &DataType::Float16)
8098        );
8099
8100        let i64_expected = vec!["0", "255", "65535", "4294967295"];
8101        assert_eq!(
8102            i64_expected,
8103            get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
8104        );
8105
8106        let i32_expected = vec!["0", "255", "65535", "null"];
8107        assert_eq!(
8108            i32_expected,
8109            get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
8110        );
8111
8112        let i16_expected = vec!["0", "255", "null", "null"];
8113        assert_eq!(
8114            i16_expected,
8115            get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
8116        );
8117
8118        let i8_expected = vec!["0", "null", "null", "null"];
8119        assert_eq!(
8120            i8_expected,
8121            get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
8122        );
8123
8124        let u64_expected = vec!["0", "255", "65535", "4294967295"];
8125        assert_eq!(
8126            u64_expected,
8127            get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
8128        );
8129
8130        let u32_expected = vec!["0", "255", "65535", "4294967295"];
8131        assert_eq!(
8132            u32_expected,
8133            get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
8134        );
8135
8136        let u16_expected = vec!["0", "255", "65535", "null"];
8137        assert_eq!(
8138            u16_expected,
8139            get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
8140        );
8141
8142        let u8_expected = vec!["0", "255", "null", "null"];
8143        assert_eq!(
8144            u8_expected,
8145            get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
8146        );
8147    }
8148
8149    #[test]
8150    fn test_cast_from_uint16() {
8151        let u16_values: Vec<u16> = vec![0, u8::MAX as u16, u16::MAX];
8152        let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values));
8153
8154        let f64_expected = vec!["0.0", "255.0", "65535.0"];
8155        assert_eq!(
8156            f64_expected,
8157            get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
8158        );
8159
8160        let f32_expected = vec!["0.0", "255.0", "65535.0"];
8161        assert_eq!(
8162            f32_expected,
8163            get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
8164        );
8165
8166        let f16_expected = vec!["0.0", "255.0", "inf"];
8167        assert_eq!(
8168            f16_expected,
8169            get_cast_values::<Float16Type>(&u16_array, &DataType::Float16)
8170        );
8171
8172        let i64_expected = vec!["0", "255", "65535"];
8173        assert_eq!(
8174            i64_expected,
8175            get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
8176        );
8177
8178        let i32_expected = vec!["0", "255", "65535"];
8179        assert_eq!(
8180            i32_expected,
8181            get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
8182        );
8183
8184        let i16_expected = vec!["0", "255", "null"];
8185        assert_eq!(
8186            i16_expected,
8187            get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
8188        );
8189
8190        let i8_expected = vec!["0", "null", "null"];
8191        assert_eq!(
8192            i8_expected,
8193            get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
8194        );
8195
8196        let u64_expected = vec!["0", "255", "65535"];
8197        assert_eq!(
8198            u64_expected,
8199            get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
8200        );
8201
8202        let u32_expected = vec!["0", "255", "65535"];
8203        assert_eq!(
8204            u32_expected,
8205            get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
8206        );
8207
8208        let u16_expected = vec!["0", "255", "65535"];
8209        assert_eq!(
8210            u16_expected,
8211            get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
8212        );
8213
8214        let u8_expected = vec!["0", "255", "null"];
8215        assert_eq!(
8216            u8_expected,
8217            get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
8218        );
8219    }
8220
8221    #[test]
8222    fn test_cast_from_uint8() {
8223        let u8_values: Vec<u8> = vec![0, u8::MAX];
8224        let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values));
8225
8226        let f64_expected = vec!["0.0", "255.0"];
8227        assert_eq!(
8228            f64_expected,
8229            get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
8230        );
8231
8232        let f32_expected = vec!["0.0", "255.0"];
8233        assert_eq!(
8234            f32_expected,
8235            get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
8236        );
8237
8238        let f16_expected = vec!["0.0", "255.0"];
8239        assert_eq!(
8240            f16_expected,
8241            get_cast_values::<Float16Type>(&u8_array, &DataType::Float16)
8242        );
8243
8244        let i64_expected = vec!["0", "255"];
8245        assert_eq!(
8246            i64_expected,
8247            get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
8248        );
8249
8250        let i32_expected = vec!["0", "255"];
8251        assert_eq!(
8252            i32_expected,
8253            get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
8254        );
8255
8256        let i16_expected = vec!["0", "255"];
8257        assert_eq!(
8258            i16_expected,
8259            get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
8260        );
8261
8262        let i8_expected = vec!["0", "null"];
8263        assert_eq!(
8264            i8_expected,
8265            get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
8266        );
8267
8268        let u64_expected = vec!["0", "255"];
8269        assert_eq!(
8270            u64_expected,
8271            get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
8272        );
8273
8274        let u32_expected = vec!["0", "255"];
8275        assert_eq!(
8276            u32_expected,
8277            get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
8278        );
8279
8280        let u16_expected = vec!["0", "255"];
8281        assert_eq!(
8282            u16_expected,
8283            get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
8284        );
8285
8286        let u8_expected = vec!["0", "255"];
8287        assert_eq!(
8288            u8_expected,
8289            get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
8290        );
8291    }
8292
8293    #[test]
8294    fn test_cast_from_int64() {
8295        let i64_values: Vec<i64> = vec![
8296            i64::MIN,
8297            i32::MIN as i64,
8298            i16::MIN as i64,
8299            i8::MIN as i64,
8300            0,
8301            i8::MAX as i64,
8302            i16::MAX as i64,
8303            i32::MAX as i64,
8304            i64::MAX,
8305        ];
8306        let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values));
8307
8308        let f64_expected = vec![
8309            -9223372036854776000.0,
8310            -2147483648.0,
8311            -32768.0,
8312            -128.0,
8313            0.0,
8314            127.0,
8315            32767.0,
8316            2147483647.0,
8317            9223372036854776000.0,
8318        ];
8319        assert_eq!(
8320            f64_expected,
8321            get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
8322                .iter()
8323                .map(|i| i.parse::<f64>().unwrap())
8324                .collect::<Vec<f64>>()
8325        );
8326
8327        let f32_expected = vec![
8328            -9223372000000000000.0,
8329            -2147483600.0,
8330            -32768.0,
8331            -128.0,
8332            0.0,
8333            127.0,
8334            32767.0,
8335            2147483600.0,
8336            9223372000000000000.0,
8337        ];
8338        assert_eq!(
8339            f32_expected,
8340            get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
8341                .iter()
8342                .map(|i| i.parse::<f32>().unwrap())
8343                .collect::<Vec<f32>>()
8344        );
8345
8346        let f16_expected = vec![
8347            f16::from_f64(-9223372000000000000.0),
8348            f16::from_f64(-2147483600.0),
8349            f16::from_f64(-32768.0),
8350            f16::from_f64(-128.0),
8351            f16::from_f64(0.0),
8352            f16::from_f64(127.0),
8353            f16::from_f64(32767.0),
8354            f16::from_f64(2147483600.0),
8355            f16::from_f64(9223372000000000000.0),
8356        ];
8357        assert_eq!(
8358            f16_expected,
8359            get_cast_values::<Float16Type>(&i64_array, &DataType::Float16)
8360                .iter()
8361                .map(|i| i.parse::<f16>().unwrap())
8362                .collect::<Vec<f16>>()
8363        );
8364
8365        let i64_expected = vec![
8366            "-9223372036854775808",
8367            "-2147483648",
8368            "-32768",
8369            "-128",
8370            "0",
8371            "127",
8372            "32767",
8373            "2147483647",
8374            "9223372036854775807",
8375        ];
8376        assert_eq!(
8377            i64_expected,
8378            get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
8379        );
8380
8381        let i32_expected = vec![
8382            "null",
8383            "-2147483648",
8384            "-32768",
8385            "-128",
8386            "0",
8387            "127",
8388            "32767",
8389            "2147483647",
8390            "null",
8391        ];
8392        assert_eq!(
8393            i32_expected,
8394            get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
8395        );
8396
8397        assert_eq!(
8398            i32_expected,
8399            get_cast_values::<Date32Type>(&i64_array, &DataType::Date32)
8400        );
8401
8402        let i16_expected = vec![
8403            "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
8404        ];
8405        assert_eq!(
8406            i16_expected,
8407            get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
8408        );
8409
8410        let i8_expected = vec![
8411            "null", "null", "null", "-128", "0", "127", "null", "null", "null",
8412        ];
8413        assert_eq!(
8414            i8_expected,
8415            get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
8416        );
8417
8418        let u64_expected = vec![
8419            "null",
8420            "null",
8421            "null",
8422            "null",
8423            "0",
8424            "127",
8425            "32767",
8426            "2147483647",
8427            "9223372036854775807",
8428        ];
8429        assert_eq!(
8430            u64_expected,
8431            get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
8432        );
8433
8434        let u32_expected = vec![
8435            "null",
8436            "null",
8437            "null",
8438            "null",
8439            "0",
8440            "127",
8441            "32767",
8442            "2147483647",
8443            "null",
8444        ];
8445        assert_eq!(
8446            u32_expected,
8447            get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
8448        );
8449
8450        let u16_expected = vec![
8451            "null", "null", "null", "null", "0", "127", "32767", "null", "null",
8452        ];
8453        assert_eq!(
8454            u16_expected,
8455            get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
8456        );
8457
8458        let u8_expected = vec![
8459            "null", "null", "null", "null", "0", "127", "null", "null", "null",
8460        ];
8461        assert_eq!(
8462            u8_expected,
8463            get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
8464        );
8465    }
8466
8467    #[test]
8468    fn test_cast_from_int32() {
8469        let i32_values: Vec<i32> = vec![
8470            i32::MIN,
8471            i16::MIN as i32,
8472            i8::MIN as i32,
8473            0,
8474            i8::MAX as i32,
8475            i16::MAX as i32,
8476            i32::MAX,
8477        ];
8478        let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values));
8479
8480        let f64_expected = vec![
8481            "-2147483648.0",
8482            "-32768.0",
8483            "-128.0",
8484            "0.0",
8485            "127.0",
8486            "32767.0",
8487            "2147483647.0",
8488        ];
8489        assert_eq!(
8490            f64_expected,
8491            get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
8492        );
8493
8494        let f32_expected = vec![
8495            "-2147483600.0",
8496            "-32768.0",
8497            "-128.0",
8498            "0.0",
8499            "127.0",
8500            "32767.0",
8501            "2147483600.0",
8502        ];
8503        assert_eq!(
8504            f32_expected,
8505            get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
8506        );
8507
8508        let f16_expected = vec![
8509            f16::from_f64(-2147483600.0),
8510            f16::from_f64(-32768.0),
8511            f16::from_f64(-128.0),
8512            f16::from_f64(0.0),
8513            f16::from_f64(127.0),
8514            f16::from_f64(32767.0),
8515            f16::from_f64(2147483600.0),
8516        ];
8517        assert_eq!(
8518            f16_expected,
8519            get_cast_values::<Float16Type>(&i32_array, &DataType::Float16)
8520                .iter()
8521                .map(|i| i.parse::<f16>().unwrap())
8522                .collect::<Vec<f16>>()
8523        );
8524
8525        let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
8526        assert_eq!(
8527            i16_expected,
8528            get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
8529        );
8530
8531        let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
8532        assert_eq!(
8533            i8_expected,
8534            get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
8535        );
8536
8537        let u64_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8538        assert_eq!(
8539            u64_expected,
8540            get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
8541        );
8542
8543        let u32_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8544        assert_eq!(
8545            u32_expected,
8546            get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
8547        );
8548
8549        let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
8550        assert_eq!(
8551            u16_expected,
8552            get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
8553        );
8554
8555        let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
8556        assert_eq!(
8557            u8_expected,
8558            get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
8559        );
8560
8561        // The date32 to date64 cast increases the numerical values in order to keep the same dates.
8562        let i64_expected = vec![
8563            "-185542587187200000",
8564            "-2831155200000",
8565            "-11059200000",
8566            "0",
8567            "10972800000",
8568            "2831068800000",
8569            "185542587100800000",
8570        ];
8571        assert_eq!(
8572            i64_expected,
8573            get_cast_values::<Date64Type>(&i32_array, &DataType::Date64)
8574        );
8575    }
8576
8577    #[test]
8578    fn test_cast_from_int16() {
8579        let i16_values: Vec<i16> = vec![i16::MIN, i8::MIN as i16, 0, i8::MAX as i16, i16::MAX];
8580        let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values));
8581
8582        let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8583        assert_eq!(
8584            f64_expected,
8585            get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
8586        );
8587
8588        let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8589        assert_eq!(
8590            f32_expected,
8591            get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
8592        );
8593
8594        let f16_expected = vec![
8595            f16::from_f64(-32768.0),
8596            f16::from_f64(-128.0),
8597            f16::from_f64(0.0),
8598            f16::from_f64(127.0),
8599            f16::from_f64(32767.0),
8600        ];
8601        assert_eq!(
8602            f16_expected,
8603            get_cast_values::<Float16Type>(&i16_array, &DataType::Float16)
8604                .iter()
8605                .map(|i| i.parse::<f16>().unwrap())
8606                .collect::<Vec<f16>>()
8607        );
8608
8609        let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
8610        assert_eq!(
8611            i64_expected,
8612            get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
8613        );
8614
8615        let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
8616        assert_eq!(
8617            i32_expected,
8618            get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
8619        );
8620
8621        let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
8622        assert_eq!(
8623            i16_expected,
8624            get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
8625        );
8626
8627        let i8_expected = vec!["null", "-128", "0", "127", "null"];
8628        assert_eq!(
8629            i8_expected,
8630            get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
8631        );
8632
8633        let u64_expected = vec!["null", "null", "0", "127", "32767"];
8634        assert_eq!(
8635            u64_expected,
8636            get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
8637        );
8638
8639        let u32_expected = vec!["null", "null", "0", "127", "32767"];
8640        assert_eq!(
8641            u32_expected,
8642            get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
8643        );
8644
8645        let u16_expected = vec!["null", "null", "0", "127", "32767"];
8646        assert_eq!(
8647            u16_expected,
8648            get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
8649        );
8650
8651        let u8_expected = vec!["null", "null", "0", "127", "null"];
8652        assert_eq!(
8653            u8_expected,
8654            get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
8655        );
8656    }
8657
8658    #[test]
8659    fn test_cast_from_date32() {
8660        let i32_values: Vec<i32> = vec![
8661            i32::MIN,
8662            i16::MIN as i32,
8663            i8::MIN as i32,
8664            0,
8665            i8::MAX as i32,
8666            i16::MAX as i32,
8667            i32::MAX,
8668        ];
8669        let date32_array: ArrayRef = Arc::new(Date32Array::from(i32_values));
8670
8671        let i64_expected = vec![
8672            "-2147483648",
8673            "-32768",
8674            "-128",
8675            "0",
8676            "127",
8677            "32767",
8678            "2147483647",
8679        ];
8680        assert_eq!(
8681            i64_expected,
8682            get_cast_values::<Int64Type>(&date32_array, &DataType::Int64)
8683        );
8684    }
8685
8686    #[test]
8687    fn test_cast_from_int8() {
8688        let i8_values: Vec<i8> = vec![i8::MIN, 0, i8::MAX];
8689        let i8_array = Int8Array::from(i8_values);
8690
8691        let f64_expected = vec!["-128.0", "0.0", "127.0"];
8692        assert_eq!(
8693            f64_expected,
8694            get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
8695        );
8696
8697        let f32_expected = vec!["-128.0", "0.0", "127.0"];
8698        assert_eq!(
8699            f32_expected,
8700            get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
8701        );
8702
8703        let f16_expected = vec!["-128.0", "0.0", "127.0"];
8704        assert_eq!(
8705            f16_expected,
8706            get_cast_values::<Float16Type>(&i8_array, &DataType::Float16)
8707        );
8708
8709        let i64_expected = vec!["-128", "0", "127"];
8710        assert_eq!(
8711            i64_expected,
8712            get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
8713        );
8714
8715        let i32_expected = vec!["-128", "0", "127"];
8716        assert_eq!(
8717            i32_expected,
8718            get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
8719        );
8720
8721        let i16_expected = vec!["-128", "0", "127"];
8722        assert_eq!(
8723            i16_expected,
8724            get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
8725        );
8726
8727        let i8_expected = vec!["-128", "0", "127"];
8728        assert_eq!(
8729            i8_expected,
8730            get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
8731        );
8732
8733        let u64_expected = vec!["null", "0", "127"];
8734        assert_eq!(
8735            u64_expected,
8736            get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
8737        );
8738
8739        let u32_expected = vec!["null", "0", "127"];
8740        assert_eq!(
8741            u32_expected,
8742            get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
8743        );
8744
8745        let u16_expected = vec!["null", "0", "127"];
8746        assert_eq!(
8747            u16_expected,
8748            get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
8749        );
8750
8751        let u8_expected = vec!["null", "0", "127"];
8752        assert_eq!(
8753            u8_expected,
8754            get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
8755        );
8756    }
8757
8758    /// Convert `array` into a vector of strings by casting to data type dt
8759    fn get_cast_values<T>(array: &dyn Array, dt: &DataType) -> Vec<String>
8760    where
8761        T: ArrowPrimitiveType,
8762    {
8763        let c = cast(array, dt).unwrap();
8764        let a = c.as_primitive::<T>();
8765        let mut v: Vec<String> = vec![];
8766        for i in 0..array.len() {
8767            if a.is_null(i) {
8768                v.push("null".to_string())
8769            } else {
8770                v.push(format!("{:?}", a.value(i)));
8771            }
8772        }
8773        v
8774    }
8775
8776    #[test]
8777    fn test_cast_utf8_dict() {
8778        // FROM a dictionary with of Utf8 values
8779        let mut builder = StringDictionaryBuilder::<Int8Type>::new();
8780        builder.append("one").unwrap();
8781        builder.append_null();
8782        builder.append("three").unwrap();
8783        let array: ArrayRef = Arc::new(builder.finish());
8784
8785        let expected = vec!["one", "null", "three"];
8786
8787        // Test casting TO StringArray
8788        let cast_type = Utf8;
8789        let cast_array = cast(&array, &cast_type).expect("cast to UTF-8 failed");
8790        assert_eq!(cast_array.data_type(), &cast_type);
8791        assert_eq!(array_to_strings(&cast_array), expected);
8792
8793        // Test casting TO Dictionary (with different index sizes)
8794
8795        let cast_type = Dictionary(Box::new(Int16), Box::new(Utf8));
8796        let cast_array = cast(&array, &cast_type).expect("cast failed");
8797        assert_eq!(cast_array.data_type(), &cast_type);
8798        assert_eq!(array_to_strings(&cast_array), expected);
8799
8800        let cast_type = Dictionary(Box::new(Int32), Box::new(Utf8));
8801        let cast_array = cast(&array, &cast_type).expect("cast failed");
8802        assert_eq!(cast_array.data_type(), &cast_type);
8803        assert_eq!(array_to_strings(&cast_array), expected);
8804
8805        let cast_type = Dictionary(Box::new(Int64), Box::new(Utf8));
8806        let cast_array = cast(&array, &cast_type).expect("cast failed");
8807        assert_eq!(cast_array.data_type(), &cast_type);
8808        assert_eq!(array_to_strings(&cast_array), expected);
8809
8810        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8811        let cast_array = cast(&array, &cast_type).expect("cast failed");
8812        assert_eq!(cast_array.data_type(), &cast_type);
8813        assert_eq!(array_to_strings(&cast_array), expected);
8814
8815        let cast_type = Dictionary(Box::new(UInt16), Box::new(Utf8));
8816        let cast_array = cast(&array, &cast_type).expect("cast failed");
8817        assert_eq!(cast_array.data_type(), &cast_type);
8818        assert_eq!(array_to_strings(&cast_array), expected);
8819
8820        let cast_type = Dictionary(Box::new(UInt32), Box::new(Utf8));
8821        let cast_array = cast(&array, &cast_type).expect("cast failed");
8822        assert_eq!(cast_array.data_type(), &cast_type);
8823        assert_eq!(array_to_strings(&cast_array), expected);
8824
8825        let cast_type = Dictionary(Box::new(UInt64), Box::new(Utf8));
8826        let cast_array = cast(&array, &cast_type).expect("cast failed");
8827        assert_eq!(cast_array.data_type(), &cast_type);
8828        assert_eq!(array_to_strings(&cast_array), expected);
8829    }
8830
8831    #[test]
8832    fn test_cast_dict_to_dict_bad_index_value_primitive() {
8833        // test converting from an array that has indexes of a type
8834        // that are out of bounds for a particular other kind of
8835        // index.
8836
8837        let mut builder = PrimitiveDictionaryBuilder::<Int32Type, Int64Type>::new();
8838
8839        // add 200 distinct values (which can be stored by a
8840        // dictionary indexed by int32, but not a dictionary indexed
8841        // with int8)
8842        for i in 0..200 {
8843            builder.append(i).unwrap();
8844        }
8845        let array: ArrayRef = Arc::new(builder.finish());
8846
8847        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8848        let res = cast(&array, &cast_type);
8849        assert!(res.is_err());
8850        let actual_error = format!("{res:?}");
8851        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8852        assert!(
8853            actual_error.contains(expected_error),
8854            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8855        );
8856    }
8857
8858    #[test]
8859    fn test_cast_dict_to_dict_bad_index_value_utf8() {
8860        // Same test as test_cast_dict_to_dict_bad_index_value but use
8861        // string values (and encode the expected behavior here);
8862
8863        let mut builder = StringDictionaryBuilder::<Int32Type>::new();
8864
8865        // add 200 distinct values (which can be stored by a
8866        // dictionary indexed by int32, but not a dictionary indexed
8867        // with int8)
8868        for i in 0..200 {
8869            let val = format!("val{i}");
8870            builder.append(&val).unwrap();
8871        }
8872        let array = builder.finish();
8873
8874        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8875        let res = cast(&array, &cast_type);
8876        assert!(res.is_err());
8877        let actual_error = format!("{res:?}");
8878        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8879        assert!(
8880            actual_error.contains(expected_error),
8881            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8882        );
8883    }
8884
8885    #[test]
8886    fn test_cast_primitive_dict() {
8887        // FROM a dictionary with of INT32 values
8888        let mut builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
8889        builder.append(1).unwrap();
8890        builder.append_null();
8891        builder.append(3).unwrap();
8892        let array: ArrayRef = Arc::new(builder.finish());
8893
8894        let expected = vec!["1", "null", "3"];
8895
8896        // Test casting TO PrimitiveArray, different dictionary type
8897        let cast_array = cast(&array, &Utf8).expect("cast to UTF-8 failed");
8898        assert_eq!(array_to_strings(&cast_array), expected);
8899        assert_eq!(cast_array.data_type(), &Utf8);
8900
8901        let cast_array = cast(&array, &Int64).expect("cast to int64 failed");
8902        assert_eq!(array_to_strings(&cast_array), expected);
8903        assert_eq!(cast_array.data_type(), &Int64);
8904    }
8905
8906    #[test]
8907    fn test_cast_primitive_array_to_dict() {
8908        let mut builder = PrimitiveBuilder::<Int32Type>::new();
8909        builder.append_value(1);
8910        builder.append_null();
8911        builder.append_value(3);
8912        let array: ArrayRef = Arc::new(builder.finish());
8913
8914        let expected = vec!["1", "null", "3"];
8915
8916        // Cast to a dictionary (same value type, Int32)
8917        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int32));
8918        let cast_array = cast(&array, &cast_type).expect("cast failed");
8919        assert_eq!(cast_array.data_type(), &cast_type);
8920        assert_eq!(array_to_strings(&cast_array), expected);
8921
8922        // Cast to a dictionary (different value type, Int8)
8923        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int8));
8924        let cast_array = cast(&array, &cast_type).expect("cast failed");
8925        assert_eq!(cast_array.data_type(), &cast_type);
8926        assert_eq!(array_to_strings(&cast_array), expected);
8927    }
8928
8929    #[test]
8930    fn test_cast_time_array_to_dict() {
8931        use DataType::*;
8932
8933        let array = Arc::new(Date32Array::from(vec![Some(1000), None, Some(2000)])) as ArrayRef;
8934
8935        let expected = vec!["1972-09-27", "null", "1975-06-24"];
8936
8937        let cast_type = Dictionary(Box::new(UInt8), Box::new(Date32));
8938        let cast_array = cast(&array, &cast_type).expect("cast failed");
8939        assert_eq!(cast_array.data_type(), &cast_type);
8940        assert_eq!(array_to_strings(&cast_array), expected);
8941    }
8942
8943    #[test]
8944    fn test_cast_timestamp_array_to_dict() {
8945        use DataType::*;
8946
8947        let array = Arc::new(
8948            TimestampSecondArray::from(vec![Some(1000), None, Some(2000)]).with_timezone_utc(),
8949        ) as ArrayRef;
8950
8951        let expected = vec!["1970-01-01T00:16:40", "null", "1970-01-01T00:33:20"];
8952
8953        let cast_type = Dictionary(Box::new(UInt8), Box::new(Timestamp(TimeUnit::Second, None)));
8954        let cast_array = cast(&array, &cast_type).expect("cast failed");
8955        assert_eq!(cast_array.data_type(), &cast_type);
8956        assert_eq!(array_to_strings(&cast_array), expected);
8957    }
8958
8959    #[test]
8960    fn test_cast_string_array_to_dict() {
8961        use DataType::*;
8962
8963        let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) as ArrayRef;
8964
8965        let expected = vec!["one", "null", "three"];
8966
8967        // Cast to a dictionary (same value type, Utf8)
8968        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8969        let cast_array = cast(&array, &cast_type).expect("cast failed");
8970        assert_eq!(cast_array.data_type(), &cast_type);
8971        assert_eq!(array_to_strings(&cast_array), expected);
8972    }
8973
8974    #[test]
8975    fn test_cast_null_array_to_from_decimal_array() {
8976        let data_type = DataType::Decimal128(12, 4);
8977        let array = new_null_array(&DataType::Null, 4);
8978        assert_eq!(array.data_type(), &DataType::Null);
8979        let cast_array = cast(&array, &data_type).expect("cast failed");
8980        assert_eq!(cast_array.data_type(), &data_type);
8981        for i in 0..4 {
8982            assert!(cast_array.is_null(i));
8983        }
8984
8985        let array = new_null_array(&data_type, 4);
8986        assert_eq!(array.data_type(), &data_type);
8987        let cast_array = cast(&array, &DataType::Null).expect("cast failed");
8988        assert_eq!(cast_array.data_type(), &DataType::Null);
8989        assert_eq!(cast_array.len(), 4);
8990        assert_eq!(cast_array.logical_nulls().unwrap().null_count(), 4);
8991    }
8992
8993    #[test]
8994    fn test_cast_null_array_from_and_to_primitive_array() {
8995        macro_rules! typed_test {
8996            ($ARR_TYPE:ident, $DATATYPE:ident, $TYPE:tt) => {{
8997                {
8998                    let array = Arc::new(NullArray::new(6)) as ArrayRef;
8999                    let expected = $ARR_TYPE::from(vec![None; 6]);
9000                    let cast_type = DataType::$DATATYPE;
9001                    let cast_array = cast(&array, &cast_type).expect("cast failed");
9002                    let cast_array = cast_array.as_primitive::<$TYPE>();
9003                    assert_eq!(cast_array.data_type(), &cast_type);
9004                    assert_eq!(cast_array, &expected);
9005                }
9006            }};
9007        }
9008
9009        typed_test!(Int16Array, Int16, Int16Type);
9010        typed_test!(Int32Array, Int32, Int32Type);
9011        typed_test!(Int64Array, Int64, Int64Type);
9012
9013        typed_test!(UInt16Array, UInt16, UInt16Type);
9014        typed_test!(UInt32Array, UInt32, UInt32Type);
9015        typed_test!(UInt64Array, UInt64, UInt64Type);
9016
9017        typed_test!(Float16Array, Float16, Float16Type);
9018        typed_test!(Float32Array, Float32, Float32Type);
9019        typed_test!(Float64Array, Float64, Float64Type);
9020
9021        typed_test!(Date32Array, Date32, Date32Type);
9022        typed_test!(Date64Array, Date64, Date64Type);
9023    }
9024
9025    fn cast_from_null_to_other_base(data_type: &DataType, is_complex: bool) {
9026        // Cast from null to data_type
9027        let array = new_null_array(&DataType::Null, 4);
9028        assert_eq!(array.data_type(), &DataType::Null);
9029        let cast_array = cast(&array, data_type).expect("cast failed");
9030        assert_eq!(cast_array.data_type(), data_type);
9031        for i in 0..4 {
9032            if is_complex {
9033                assert!(cast_array.logical_nulls().unwrap().is_null(i));
9034            } else {
9035                assert!(cast_array.is_null(i));
9036            }
9037        }
9038    }
9039
9040    fn cast_from_null_to_other(data_type: &DataType) {
9041        cast_from_null_to_other_base(data_type, false);
9042    }
9043
9044    fn cast_from_null_to_other_complex(data_type: &DataType) {
9045        cast_from_null_to_other_base(data_type, true);
9046    }
9047
9048    #[test]
9049    fn test_cast_null_from_and_to_variable_sized() {
9050        cast_from_null_to_other(&DataType::Utf8);
9051        cast_from_null_to_other(&DataType::LargeUtf8);
9052        cast_from_null_to_other(&DataType::Binary);
9053        cast_from_null_to_other(&DataType::LargeBinary);
9054    }
9055
9056    #[test]
9057    fn test_cast_null_from_and_to_nested_type() {
9058        // Cast null from and to map
9059        let data_type = DataType::Map(
9060            Arc::new(Field::new_struct(
9061                "entry",
9062                vec![
9063                    Field::new("key", DataType::Utf8, false),
9064                    Field::new("value", DataType::Int32, true),
9065                ],
9066                false,
9067            )),
9068            false,
9069        );
9070        cast_from_null_to_other(&data_type);
9071
9072        // Cast null from and to list
9073        let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
9074        cast_from_null_to_other(&data_type);
9075        let data_type = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
9076        cast_from_null_to_other(&data_type);
9077        let data_type =
9078            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
9079        cast_from_null_to_other(&data_type);
9080
9081        // Cast null from and to dictionary
9082        let values = vec![None, None, None, None] as Vec<Option<&str>>;
9083        let array: DictionaryArray<Int8Type> = values.into_iter().collect();
9084        let array = Arc::new(array) as ArrayRef;
9085        let data_type = array.data_type().to_owned();
9086        cast_from_null_to_other(&data_type);
9087
9088        // Cast null from and to struct
9089        let data_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
9090        cast_from_null_to_other(&data_type);
9091
9092        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
9093        cast_from_null_to_other(&target_type);
9094
9095        let target_type =
9096            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
9097        cast_from_null_to_other(&target_type);
9098
9099        let fields = UnionFields::from_fields(vec![Field::new("a", DataType::Int64, false)]);
9100        let target_type = DataType::Union(fields, UnionMode::Sparse);
9101        cast_from_null_to_other_complex(&target_type);
9102
9103        let target_type = DataType::RunEndEncoded(
9104            Arc::new(Field::new("item", DataType::Int32, true)),
9105            Arc::new(Field::new("item", DataType::Int32, true)),
9106        );
9107        cast_from_null_to_other_complex(&target_type);
9108    }
9109
9110    /// Print the `DictionaryArray` `array` as a vector of strings
9111    fn array_to_strings(array: &ArrayRef) -> Vec<String> {
9112        let options = FormatOptions::new().with_null("null");
9113        let formatter = ArrayFormatter::try_new(array.as_ref(), &options).unwrap();
9114        (0..array.len())
9115            .map(|i| formatter.value(i).to_string())
9116            .collect()
9117    }
9118
9119    #[test]
9120    fn test_cast_utf8_to_date32() {
9121        use chrono::NaiveDate;
9122        let from_ymd = chrono::NaiveDate::from_ymd_opt;
9123        let since = chrono::NaiveDate::signed_duration_since;
9124
9125        let a = StringArray::from(vec![
9126            "2000-01-01",          // valid date with leading 0s
9127            "2000-01-01T12:00:00", // valid datetime, will throw away the time part
9128            "2000-2-2",            // valid date without leading 0s
9129            "2000-00-00",          // invalid month and day
9130            "2000",                // just a year is invalid
9131        ]);
9132        let array = Arc::new(a) as ArrayRef;
9133        let b = cast(&array, &DataType::Date32).unwrap();
9134        let c = b.as_primitive::<Date32Type>();
9135
9136        // test valid inputs
9137        let date_value = since(
9138            NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
9139            from_ymd(1970, 1, 1).unwrap(),
9140        )
9141        .num_days() as i32;
9142        assert!(c.is_valid(0)); // "2000-01-01"
9143        assert_eq!(date_value, c.value(0));
9144
9145        assert!(c.is_valid(1)); // "2000-01-01T12:00:00"
9146        assert_eq!(date_value, c.value(1));
9147
9148        let date_value = since(
9149            NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
9150            from_ymd(1970, 1, 1).unwrap(),
9151        )
9152        .num_days() as i32;
9153        assert!(c.is_valid(2)); // "2000-2-2"
9154        assert_eq!(date_value, c.value(2));
9155
9156        // test invalid inputs
9157        assert!(!c.is_valid(3)); // "2000-00-00"
9158        assert!(!c.is_valid(4)); // "2000"
9159    }
9160
9161    #[test]
9162    fn test_cast_utf8_to_date64() {
9163        let a = StringArray::from(vec![
9164            "2000-01-01T12:00:00", // date + time valid
9165            "2020-12-15T12:34:56", // date + time valid
9166            "2020-2-2T12:34:56",   // valid date time without leading 0s
9167            "2000-00-00T12:00:00", // invalid month and day
9168            "2000-01-01 12:00:00", // missing the 'T'
9169            "2000-01-01",          // just a date is invalid
9170        ]);
9171        let array = Arc::new(a) as ArrayRef;
9172        let b = cast(&array, &DataType::Date64).unwrap();
9173        let c = b.as_primitive::<Date64Type>();
9174
9175        // test valid inputs
9176        assert!(c.is_valid(0)); // "2000-01-01T12:00:00"
9177        assert_eq!(946728000000, c.value(0));
9178        assert!(c.is_valid(1)); // "2020-12-15T12:34:56"
9179        assert_eq!(1608035696000, c.value(1));
9180        assert!(!c.is_valid(2)); // "2020-2-2T12:34:56"
9181
9182        assert!(!c.is_valid(3)); // "2000-00-00T12:00:00"
9183        assert!(c.is_valid(4)); // "2000-01-01 12:00:00"
9184        assert_eq!(946728000000, c.value(4));
9185        assert!(c.is_valid(5)); // "2000-01-01"
9186        assert_eq!(946684800000, c.value(5));
9187    }
9188
9189    #[test]
9190    fn test_can_cast_fsl_to_fsl() {
9191        let from_array = Arc::new(
9192            FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
9193                [Some([Some(1.0), Some(2.0)]), None],
9194                2,
9195            ),
9196        ) as ArrayRef;
9197        let to_array = Arc::new(
9198            FixedSizeListArray::from_iter_primitive::<Float16Type, _, _>(
9199                [
9200                    Some([Some(f16::from_f32(1.0)), Some(f16::from_f32(2.0))]),
9201                    None,
9202                ],
9203                2,
9204            ),
9205        ) as ArrayRef;
9206
9207        assert!(can_cast_types(from_array.data_type(), to_array.data_type()));
9208        let actual = cast(&from_array, to_array.data_type()).unwrap();
9209        assert_eq!(actual.data_type(), to_array.data_type());
9210
9211        let invalid_target =
9212            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Binary, true)), 2);
9213        assert!(!can_cast_types(from_array.data_type(), &invalid_target));
9214
9215        let invalid_size =
9216            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Float16, true)), 5);
9217        assert!(!can_cast_types(from_array.data_type(), &invalid_size));
9218    }
9219
9220    #[test]
9221    fn test_can_cast_types_fixed_size_list_to_list() {
9222        // DataType::List
9223        let array1 = make_fixed_size_list_array();
9224        assert!(can_cast_types(
9225            array1.data_type(),
9226            &DataType::List(Arc::new(Field::new("", DataType::Int32, false)))
9227        ));
9228
9229        // DataType::LargeList
9230        let array2 = make_fixed_size_list_array_for_large_list();
9231        assert!(can_cast_types(
9232            array2.data_type(),
9233            &DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false)))
9234        ));
9235    }
9236
9237    #[test]
9238    fn test_cast_fixed_size_list_to_list() {
9239        // Important cases:
9240        // 1. With/without nulls
9241        // 2. List/LargeList/ListView/LargeListView
9242        // 3. With and without inner casts
9243
9244        let cases = [
9245            // fixed_size_list<i32, 2> => list<i32>
9246            (
9247                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9248                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9249                    2,
9250                )) as ArrayRef,
9251                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
9252                    Some([Some(1), Some(1)]),
9253                    Some([Some(2), Some(2)]),
9254                ])) as ArrayRef,
9255            ),
9256            // fixed_size_list<i32, 2> => list<i32> (nullable)
9257            (
9258                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9259                    [None, Some([Some(2), Some(2)])],
9260                    2,
9261                )) as ArrayRef,
9262                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
9263                    None,
9264                    Some([Some(2), Some(2)]),
9265                ])) as ArrayRef,
9266            ),
9267            // fixed_size_list<i32, 2> => large_list<i64>
9268            (
9269                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9270                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9271                    2,
9272                )) as ArrayRef,
9273                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
9274                    Some([Some(1), Some(1)]),
9275                    Some([Some(2), Some(2)]),
9276                ])) as ArrayRef,
9277            ),
9278            // fixed_size_list<i32, 2> => large_list<i64> (nullable)
9279            (
9280                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9281                    [None, Some([Some(2), Some(2)])],
9282                    2,
9283                )) as ArrayRef,
9284                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
9285                    None,
9286                    Some([Some(2), Some(2)]),
9287                ])) as ArrayRef,
9288            ),
9289            // fixed_size_list<i32, 2> => list_view<i32>
9290            (
9291                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9292                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9293                    2,
9294                )) as ArrayRef,
9295                Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>([
9296                    Some([Some(1), Some(1)]),
9297                    Some([Some(2), Some(2)]),
9298                ])) as ArrayRef,
9299            ),
9300            // fixed_size_list<i32, 2> => list_view<i32> (nullable)
9301            (
9302                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9303                    [None, Some([Some(2), Some(2)])],
9304                    2,
9305                )) as ArrayRef,
9306                Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>([
9307                    None,
9308                    Some([Some(2), Some(2)]),
9309                ])) as ArrayRef,
9310            ),
9311            // fixed_size_list<i32, 2> => large_list_view<i64>
9312            (
9313                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9314                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9315                    2,
9316                )) as ArrayRef,
9317                Arc::new(LargeListViewArray::from_iter_primitive::<Int64Type, _, _>(
9318                    [Some([Some(1), Some(1)]), Some([Some(2), Some(2)])],
9319                )) as ArrayRef,
9320            ),
9321            // fixed_size_list<i32, 2> => large_list_view<i64> (nullable)
9322            (
9323                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9324                    [None, Some([Some(2), Some(2)])],
9325                    2,
9326                )) as ArrayRef,
9327                Arc::new(LargeListViewArray::from_iter_primitive::<Int64Type, _, _>(
9328                    [None, Some([Some(2), Some(2)])],
9329                )) as ArrayRef,
9330            ),
9331        ];
9332
9333        for (array, expected) in cases {
9334            assert!(
9335                can_cast_types(array.data_type(), expected.data_type()),
9336                "can_cast_types claims we cannot cast {:?} to {:?}",
9337                array.data_type(),
9338                expected.data_type()
9339            );
9340
9341            let list_array = cast(&array, expected.data_type())
9342                .unwrap_or_else(|_| panic!("Failed to cast {array:?} to {expected:?}"));
9343            assert_eq!(
9344                list_array.as_ref(),
9345                &expected,
9346                "Incorrect result from casting {array:?} to {expected:?}",
9347            );
9348        }
9349    }
9350
9351    #[test]
9352    fn test_cast_fixed_size_list_to_list_preserves_field_metadata() {
9353        use std::collections::HashMap;
9354
9355        let metadata: HashMap<String, String> =
9356            HashMap::from([("PARQUET:field_id".to_string(), "89".to_string())]);
9357
9358        let src = Arc::new(
9359            FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
9360                [[1.0_f32, 2.0].map(Some), [3.0, 4.0].map(Some)].map(Some),
9361                2,
9362            ),
9363        ) as ArrayRef;
9364
9365        let target_field = Arc::new(
9366            Field::new("element", DataType::Float32, true).with_metadata(metadata.clone()),
9367        );
9368
9369        let target_types = [
9370            DataType::List(target_field.clone()),
9371            DataType::LargeList(target_field.clone()),
9372            DataType::ListView(target_field.clone()),
9373            DataType::LargeListView(target_field.clone()),
9374        ];
9375
9376        for target_type in &target_types {
9377            let result = cast(&src, target_type).unwrap();
9378            assert_eq!(
9379                result.data_type(),
9380                target_type,
9381                "Cast to {target_type:?} should preserve field metadata"
9382            );
9383        }
9384    }
9385
9386    #[test]
9387    fn test_cast_utf8_to_list() {
9388        // DataType::List
9389        let array = Arc::new(StringArray::from(vec!["5"])) as ArrayRef;
9390        let field = Arc::new(Field::new("", DataType::Int32, false));
9391        let list_array = cast(&array, &DataType::List(field.clone())).unwrap();
9392        let actual = list_array.as_list_opt::<i32>().unwrap();
9393        let expect = ListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
9394        assert_eq!(&expect.value(0), &actual.value(0));
9395
9396        // DataType::LargeList
9397        let list_array = cast(&array, &DataType::LargeList(field.clone())).unwrap();
9398        let actual = list_array.as_list_opt::<i64>().unwrap();
9399        let expect = LargeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
9400        assert_eq!(&expect.value(0), &actual.value(0));
9401
9402        // DataType::FixedSizeList
9403        let list_array = cast(&array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9404        let actual = list_array.as_fixed_size_list_opt().unwrap();
9405        let expect =
9406            FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])], 1);
9407        assert_eq!(&expect.value(0), &actual.value(0));
9408    }
9409
9410    #[test]
9411    fn test_cast_single_element_fixed_size_list() {
9412        // FixedSizeList<T>[1] => T
9413        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9414            [(Some([Some(5)]))],
9415            1,
9416        )) as ArrayRef;
9417        let casted_array = cast(&from_array, &DataType::Int32).unwrap();
9418        let actual: &Int32Array = casted_array.as_primitive();
9419        let expected = Int32Array::from(vec![Some(5)]);
9420        assert_eq!(&expected, actual);
9421
9422        // FixedSizeList<T>[1] => FixedSizeList<U>[1]
9423        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9424            [(Some([Some(5)]))],
9425            1,
9426        )) as ArrayRef;
9427        let to_field = Arc::new(Field::new("dummy", DataType::Float32, false));
9428        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
9429        let expected = Arc::new(FixedSizeListArray::new(
9430            to_field.clone(),
9431            1,
9432            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9433            None,
9434        )) as ArrayRef;
9435        assert_eq!(*expected, *actual);
9436
9437        // FixedSizeList<T>[1] => FixedSizeList<FixdSizedList<U>[1]>[1]
9438        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9439            [(Some([Some(5)]))],
9440            1,
9441        )) as ArrayRef;
9442        let to_field_inner = Arc::new(Field::new_list_field(DataType::Float32, false));
9443        let to_field = Arc::new(Field::new(
9444            "dummy",
9445            DataType::FixedSizeList(to_field_inner.clone(), 1),
9446            false,
9447        ));
9448        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
9449        let expected = Arc::new(FixedSizeListArray::new(
9450            to_field.clone(),
9451            1,
9452            Arc::new(FixedSizeListArray::new(
9453                to_field_inner.clone(),
9454                1,
9455                Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9456                None,
9457            )) as ArrayRef,
9458            None,
9459        )) as ArrayRef;
9460        assert_eq!(*expected, *actual);
9461
9462        // T => FixedSizeList<T>[1] (non-nullable)
9463        let field = Arc::new(Field::new("dummy", DataType::Float32, false));
9464        let from_array = Arc::new(Int8Array::from(vec![Some(5)])) as ArrayRef;
9465        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9466        let actual = casted_array.as_fixed_size_list();
9467        let expected = Arc::new(FixedSizeListArray::new(
9468            field.clone(),
9469            1,
9470            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9471            None,
9472        )) as ArrayRef;
9473        assert_eq!(expected.as_ref(), actual);
9474
9475        // T => FixedSizeList<T>[1] (nullable)
9476        let field = Arc::new(Field::new("nullable", DataType::Float32, true));
9477        let from_array = Arc::new(Int8Array::from(vec![None])) as ArrayRef;
9478        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9479        let actual = casted_array.as_fixed_size_list();
9480        let expected = Arc::new(FixedSizeListArray::new(
9481            field.clone(),
9482            1,
9483            Arc::new(Float32Array::from(vec![None])) as ArrayRef,
9484            None,
9485        )) as ArrayRef;
9486        assert_eq!(expected.as_ref(), actual);
9487    }
9488
9489    #[test]
9490    fn test_cast_list_containers() {
9491        // large-list to list
9492        let array = make_large_list_array();
9493        let list_array = cast(
9494            &array,
9495            &DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
9496        )
9497        .unwrap();
9498        let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
9499        let expected = array.as_any().downcast_ref::<LargeListArray>().unwrap();
9500
9501        assert_eq!(&expected.value(0), &actual.value(0));
9502        assert_eq!(&expected.value(1), &actual.value(1));
9503        assert_eq!(&expected.value(2), &actual.value(2));
9504
9505        // list to large-list
9506        let array = make_list_array();
9507        let large_list_array = cast(
9508            &array,
9509            &DataType::LargeList(Arc::new(Field::new("", DataType::Int32, false))),
9510        )
9511        .unwrap();
9512        let actual = large_list_array
9513            .as_any()
9514            .downcast_ref::<LargeListArray>()
9515            .unwrap();
9516        let expected = array.as_any().downcast_ref::<ListArray>().unwrap();
9517
9518        assert_eq!(&expected.value(0), &actual.value(0));
9519        assert_eq!(&expected.value(1), &actual.value(1));
9520        assert_eq!(&expected.value(2), &actual.value(2));
9521    }
9522
9523    #[test]
9524    fn test_cast_list_view() {
9525        // cast between list view and list view
9526        let array = make_list_view_array();
9527        let to = DataType::ListView(Field::new_list_field(DataType::Float32, true).into());
9528        assert!(can_cast_types(array.data_type(), &to));
9529        let actual = cast(&array, &to).unwrap();
9530        let actual = actual.as_list_view::<i32>();
9531
9532        assert_eq!(
9533            &Float32Array::from(vec![0.0, 1.0, 2.0]) as &dyn Array,
9534            actual.value(0).as_ref()
9535        );
9536        assert_eq!(
9537            &Float32Array::from(vec![3.0, 4.0, 5.0]) as &dyn Array,
9538            actual.value(1).as_ref()
9539        );
9540        assert_eq!(
9541            &Float32Array::from(vec![6.0, 7.0]) as &dyn Array,
9542            actual.value(2).as_ref()
9543        );
9544
9545        // cast between large list view and large list view
9546        let array = make_large_list_view_array();
9547        let to = DataType::LargeListView(Field::new_list_field(DataType::Float32, true).into());
9548        assert!(can_cast_types(array.data_type(), &to));
9549        let actual = cast(&array, &to).unwrap();
9550        let actual = actual.as_list_view::<i64>();
9551
9552        assert_eq!(
9553            &Float32Array::from(vec![0.0, 1.0, 2.0]) as &dyn Array,
9554            actual.value(0).as_ref()
9555        );
9556        assert_eq!(
9557            &Float32Array::from(vec![3.0, 4.0, 5.0]) as &dyn Array,
9558            actual.value(1).as_ref()
9559        );
9560        assert_eq!(
9561            &Float32Array::from(vec![6.0, 7.0]) as &dyn Array,
9562            actual.value(2).as_ref()
9563        );
9564    }
9565
9566    #[test]
9567    fn test_non_list_to_list_view() {
9568        let input = Arc::new(Int32Array::from(vec![Some(0), None, Some(2)])) as ArrayRef;
9569        let expected_primitive =
9570            Arc::new(Float32Array::from(vec![Some(0.0), None, Some(2.0)])) as ArrayRef;
9571
9572        // [[0], [NULL], [2]]
9573        let expected = ListViewArray::new(
9574            Field::new_list_field(DataType::Float32, true).into(),
9575            vec![0, 1, 2].into(),
9576            vec![1, 1, 1].into(),
9577            expected_primitive.clone(),
9578            None,
9579        );
9580        assert!(can_cast_types(input.data_type(), expected.data_type()));
9581        let actual = cast(&input, expected.data_type()).unwrap();
9582        assert_eq!(actual.as_ref(), &expected);
9583
9584        // [[0], [NULL], [2]]
9585        let expected = LargeListViewArray::new(
9586            Field::new_list_field(DataType::Float32, true).into(),
9587            vec![0, 1, 2].into(),
9588            vec![1, 1, 1].into(),
9589            expected_primitive.clone(),
9590            None,
9591        );
9592        assert!(can_cast_types(input.data_type(), expected.data_type()));
9593        let actual = cast(&input, expected.data_type()).unwrap();
9594        assert_eq!(actual.as_ref(), &expected);
9595    }
9596
9597    #[test]
9598    fn test_cast_list_to_fsl() {
9599        // There four noteworthy cases we should handle:
9600        // 1. No nulls
9601        // 2. Nulls that are always empty
9602        // 3. Nulls that have varying lengths
9603        // 4. Nulls that are correctly sized (same as target list size)
9604
9605        // Non-null case
9606        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9607        let values = vec![
9608            Some(vec![Some(1), Some(2), Some(3)]),
9609            Some(vec![Some(4), Some(5), Some(6)]),
9610        ];
9611        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
9612            values.clone(),
9613        )) as ArrayRef;
9614        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9615            values, 3,
9616        )) as ArrayRef;
9617        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9618        assert_eq!(expected.as_ref(), actual.as_ref());
9619
9620        // Null cases
9621        // Array is [[1, 2, 3], null, [4, 5, 6], null]
9622        let cases = [
9623            (
9624                // Zero-length nulls
9625                vec![1, 2, 3, 4, 5, 6],
9626                vec![3, 0, 3, 0],
9627            ),
9628            (
9629                // Varying-length nulls
9630                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
9631                vec![3, 2, 3, 1],
9632            ),
9633            (
9634                // Correctly-sized nulls
9635                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
9636                vec![3, 3, 3, 3],
9637            ),
9638            (
9639                // Mixed nulls
9640                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
9641                vec![3, 0, 3, 3],
9642            ),
9643        ];
9644        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
9645
9646        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9647            vec![
9648                Some(vec![Some(1), Some(2), Some(3)]),
9649                None,
9650                Some(vec![Some(4), Some(5), Some(6)]),
9651                None,
9652            ],
9653            3,
9654        )) as ArrayRef;
9655
9656        for (values, lengths) in cases.iter() {
9657            let array = Arc::new(ListArray::new(
9658                field.clone(),
9659                OffsetBuffer::from_lengths(lengths.clone()),
9660                Arc::new(Int32Array::from(values.clone())),
9661                Some(null_buffer.clone()),
9662            )) as ArrayRef;
9663            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9664            assert_eq!(expected.as_ref(), actual.as_ref());
9665        }
9666    }
9667
9668    #[test]
9669    fn test_cast_list_view_to_fsl() {
9670        // There four noteworthy cases we should handle:
9671        // 1. No nulls
9672        // 2. Nulls that are always empty
9673        // 3. Nulls that have varying lengths
9674        // 4. Nulls that are correctly sized (same as target list size)
9675
9676        // Non-null case
9677        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9678        let values = vec![
9679            Some(vec![Some(1), Some(2), Some(3)]),
9680            Some(vec![Some(4), Some(5), Some(6)]),
9681        ];
9682        let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(
9683            values.clone(),
9684        )) as ArrayRef;
9685        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9686            values, 3,
9687        )) as ArrayRef;
9688        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9689        assert_eq!(expected.as_ref(), actual.as_ref());
9690
9691        // Null cases
9692        // Array is [[1, 2, 3], null, [4, 5, 6], null]
9693        let cases = [
9694            (
9695                // Zero-length nulls
9696                vec![1, 2, 3, 4, 5, 6],
9697                vec![0, 0, 3, 0],
9698                vec![3, 0, 3, 0],
9699            ),
9700            (
9701                // Varying-length nulls
9702                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
9703                vec![0, 1, 5, 0],
9704                vec![3, 2, 3, 1],
9705            ),
9706            (
9707                // Correctly-sized nulls
9708                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
9709                vec![0, 3, 6, 9],
9710                vec![3, 3, 3, 3],
9711            ),
9712            (
9713                // Mixed nulls
9714                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
9715                vec![0, 0, 3, 6],
9716                vec![3, 0, 3, 3],
9717            ),
9718        ];
9719        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
9720
9721        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9722            vec![
9723                Some(vec![Some(1), Some(2), Some(3)]),
9724                None,
9725                Some(vec![Some(4), Some(5), Some(6)]),
9726                None,
9727            ],
9728            3,
9729        )) as ArrayRef;
9730
9731        for (values, offsets, lengths) in cases.iter() {
9732            let array = Arc::new(ListViewArray::new(
9733                field.clone(),
9734                offsets.clone().into(),
9735                lengths.clone().into(),
9736                Arc::new(Int32Array::from(values.clone())),
9737                Some(null_buffer.clone()),
9738            )) as ArrayRef;
9739            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9740            assert_eq!(expected.as_ref(), actual.as_ref());
9741        }
9742    }
9743
9744    #[test]
9745    fn test_cast_list_to_fsl_safety() {
9746        let values = vec![
9747            Some(vec![Some(1), Some(2), Some(3)]),
9748            Some(vec![Some(4), Some(5)]),
9749            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
9750            Some(vec![Some(3), Some(4), Some(5)]),
9751        ];
9752        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
9753            values.clone(),
9754        )) as ArrayRef;
9755
9756        let res = cast_with_options(
9757            array.as_ref(),
9758            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9759            &CastOptions {
9760                safe: false,
9761                ..Default::default()
9762            },
9763        );
9764        assert!(res.is_err());
9765        assert!(
9766            format!("{res:?}")
9767                .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
9768        );
9769
9770        // When safe=true (default), the cast will fill nulls for lists that are
9771        // too short and truncate lists that are too long.
9772        let res = cast(
9773            array.as_ref(),
9774            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9775        )
9776        .unwrap();
9777        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9778            vec![
9779                Some(vec![Some(1), Some(2), Some(3)]),
9780                None, // Too short -> replaced with null
9781                None, // Too long -> replaced with null
9782                Some(vec![Some(3), Some(4), Some(5)]),
9783            ],
9784            3,
9785        )) as ArrayRef;
9786        assert_eq!(expected.as_ref(), res.as_ref());
9787
9788        // The safe option is false and the source array contains a null list.
9789        // issue: https://github.com/apache/arrow-rs/issues/5642
9790        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
9791            Some(vec![Some(1), Some(2), Some(3)]),
9792            None,
9793        ])) as ArrayRef;
9794        let res = cast_with_options(
9795            array.as_ref(),
9796            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9797            &CastOptions {
9798                safe: false,
9799                ..Default::default()
9800            },
9801        )
9802        .unwrap();
9803        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9804            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
9805            3,
9806        )) as ArrayRef;
9807        assert_eq!(expected.as_ref(), res.as_ref());
9808    }
9809
9810    #[test]
9811    fn test_cast_list_view_to_fsl_safety() {
9812        let values = vec![
9813            Some(vec![Some(1), Some(2), Some(3)]),
9814            Some(vec![Some(4), Some(5)]),
9815            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
9816            Some(vec![Some(3), Some(4), Some(5)]),
9817        ];
9818        let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(
9819            values.clone(),
9820        )) as ArrayRef;
9821
9822        let res = cast_with_options(
9823            array.as_ref(),
9824            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9825            &CastOptions {
9826                safe: false,
9827                ..Default::default()
9828            },
9829        );
9830        assert!(res.is_err());
9831        assert!(
9832            format!("{res:?}")
9833                .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
9834        );
9835
9836        // When safe=true (default), the cast will fill nulls for lists that are
9837        // too short and truncate lists that are too long.
9838        let res = cast(
9839            array.as_ref(),
9840            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9841        )
9842        .unwrap();
9843        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9844            vec![
9845                Some(vec![Some(1), Some(2), Some(3)]),
9846                None, // Too short -> replaced with null
9847                None, // Too long -> replaced with null
9848                Some(vec![Some(3), Some(4), Some(5)]),
9849            ],
9850            3,
9851        )) as ArrayRef;
9852        assert_eq!(expected.as_ref(), res.as_ref());
9853
9854        // The safe option is false and the source array contains a null list.
9855        // issue: https://github.com/apache/arrow-rs/issues/5642
9856        let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(vec![
9857            Some(vec![Some(1), Some(2), Some(3)]),
9858            None,
9859        ])) as ArrayRef;
9860        let res = cast_with_options(
9861            array.as_ref(),
9862            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9863            &CastOptions {
9864                safe: false,
9865                ..Default::default()
9866            },
9867        )
9868        .unwrap();
9869        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9870            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
9871            3,
9872        )) as ArrayRef;
9873        assert_eq!(expected.as_ref(), res.as_ref());
9874    }
9875
9876    #[test]
9877    fn test_cast_large_list_to_fsl() {
9878        let values = vec![Some(vec![Some(1), Some(2)]), Some(vec![Some(3), Some(4)])];
9879        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9880            values.clone(),
9881            2,
9882        )) as ArrayRef;
9883        let target_type =
9884            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 2);
9885
9886        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
9887            values.clone(),
9888        )) as ArrayRef;
9889        let actual = cast(array.as_ref(), &target_type).unwrap();
9890        assert_eq!(expected.as_ref(), actual.as_ref());
9891
9892        let array = Arc::new(LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(
9893            values.clone(),
9894        )) as ArrayRef;
9895        let actual = cast(array.as_ref(), &target_type).unwrap();
9896        assert_eq!(expected.as_ref(), actual.as_ref());
9897    }
9898
9899    #[test]
9900    fn test_cast_list_to_fsl_subcast() {
9901        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
9902            vec![
9903                Some(vec![Some(1), Some(2)]),
9904                Some(vec![Some(3), Some(i32::MAX)]),
9905            ],
9906        )) as ArrayRef;
9907        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
9908            vec![
9909                Some(vec![Some(1), Some(2)]),
9910                Some(vec![Some(3), Some(i32::MAX as i64)]),
9911            ],
9912            2,
9913        )) as ArrayRef;
9914        let actual = cast(
9915            array.as_ref(),
9916            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 2),
9917        )
9918        .unwrap();
9919        assert_eq!(expected.as_ref(), actual.as_ref());
9920
9921        let res = cast_with_options(
9922            array.as_ref(),
9923            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int16, true)), 2),
9924            &CastOptions {
9925                safe: false,
9926                ..Default::default()
9927            },
9928        );
9929        assert!(res.is_err());
9930        assert!(format!("{res:?}").contains("Can't cast value 2147483647 to type Int16"));
9931    }
9932
9933    #[test]
9934    fn test_cast_list_to_fsl_empty() {
9935        let inner_field = Arc::new(Field::new_list_field(DataType::Int32, true));
9936        let target_type = DataType::FixedSizeList(inner_field.clone(), 3);
9937        let expected = new_empty_array(&target_type);
9938
9939        // list
9940        let array = new_empty_array(&DataType::List(inner_field.clone()));
9941        assert!(can_cast_types(array.data_type(), &target_type));
9942        let actual = cast(array.as_ref(), &target_type).unwrap();
9943        assert_eq!(expected.as_ref(), actual.as_ref());
9944
9945        // largelist
9946        let array = new_empty_array(&DataType::LargeList(inner_field.clone()));
9947        assert!(can_cast_types(array.data_type(), &target_type));
9948        let actual = cast(array.as_ref(), &target_type).unwrap();
9949        assert_eq!(expected.as_ref(), actual.as_ref());
9950
9951        // listview
9952        let array = new_empty_array(&DataType::ListView(inner_field.clone()));
9953        assert!(can_cast_types(array.data_type(), &target_type));
9954        let actual = cast(array.as_ref(), &target_type).unwrap();
9955        assert_eq!(expected.as_ref(), actual.as_ref());
9956
9957        // largelistview
9958        let array = new_empty_array(&DataType::LargeListView(inner_field.clone()));
9959        assert!(can_cast_types(array.data_type(), &target_type));
9960        let actual = cast(array.as_ref(), &target_type).unwrap();
9961        assert_eq!(expected.as_ref(), actual.as_ref());
9962    }
9963
9964    fn make_list_array() -> ArrayRef {
9965        // [[0, 1, 2], [3, 4, 5], [6, 7]]
9966        Arc::new(ListArray::new(
9967            Field::new_list_field(DataType::Int32, true).into(),
9968            OffsetBuffer::from_lengths(vec![3, 3, 2]),
9969            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9970            None,
9971        ))
9972    }
9973
9974    fn make_large_list_array() -> ArrayRef {
9975        // [[0, 1, 2], [3, 4, 5], [6, 7]]
9976        Arc::new(LargeListArray::new(
9977            Field::new_list_field(DataType::Int32, true).into(),
9978            OffsetBuffer::from_lengths(vec![3, 3, 2]),
9979            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9980            None,
9981        ))
9982    }
9983
9984    fn make_list_view_array() -> ArrayRef {
9985        // [[0, 1, 2], [3, 4, 5], [6, 7]]
9986        Arc::new(ListViewArray::new(
9987            Field::new_list_field(DataType::Int32, true).into(),
9988            vec![0, 3, 6].into(),
9989            vec![3, 3, 2].into(),
9990            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9991            None,
9992        ))
9993    }
9994
9995    fn make_large_list_view_array() -> ArrayRef {
9996        // [[0, 1, 2], [3, 4, 5], [6, 7]]
9997        Arc::new(LargeListViewArray::new(
9998            Field::new_list_field(DataType::Int32, true).into(),
9999            vec![0, 3, 6].into(),
10000            vec![3, 3, 2].into(),
10001            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10002            None,
10003        ))
10004    }
10005
10006    fn make_fixed_size_list_array() -> ArrayRef {
10007        // [[0, 1, 2, 3], [4, 5, 6, 7]]
10008        Arc::new(FixedSizeListArray::new(
10009            Field::new_list_field(DataType::Int32, true).into(),
10010            4,
10011            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10012            None,
10013        ))
10014    }
10015
10016    fn make_fixed_size_list_array_for_large_list() -> ArrayRef {
10017        // [[0, 1, 2, 3], [4, 5, 6, 7]]
10018        Arc::new(FixedSizeListArray::new(
10019            Field::new_list_field(DataType::Int64, true).into(),
10020            4,
10021            Arc::new(Int64Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10022            None,
10023        ))
10024    }
10025
10026    #[test]
10027    fn test_cast_map_dont_allow_change_of_order() {
10028        let string_builder = StringBuilder::new();
10029        let value_builder = StringBuilder::new();
10030        let mut builder = MapBuilder::new(
10031            Some(MapFieldNames {
10032                entry: "entries".to_string(),
10033                key: "key".to_string(),
10034                value: "value".to_string(),
10035            }),
10036            string_builder,
10037            value_builder,
10038        );
10039
10040        builder.keys().append_value("0");
10041        builder.values().append_value("test_val_1");
10042        builder.append(true).unwrap();
10043        builder.keys().append_value("1");
10044        builder.values().append_value("test_val_2");
10045        builder.append(true).unwrap();
10046
10047        // map builder returns unsorted map by default
10048        let array = builder.finish();
10049
10050        let new_ordered = true;
10051        let new_type = DataType::Map(
10052            Arc::new(Field::new(
10053                "entries",
10054                DataType::Struct(
10055                    vec![
10056                        Field::new("key", DataType::Utf8, false),
10057                        Field::new("value", DataType::Utf8, false),
10058                    ]
10059                    .into(),
10060                ),
10061                false,
10062            )),
10063            new_ordered,
10064        );
10065
10066        let new_array_result = cast(&array, &new_type.clone());
10067        assert!(!can_cast_types(array.data_type(), &new_type));
10068        let Err(ArrowError::CastError(t)) = new_array_result else {
10069            panic!();
10070        };
10071        assert_eq!(
10072            t,
10073            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"#
10074        );
10075    }
10076
10077    #[test]
10078    fn test_cast_map_dont_allow_when_container_cant_cast() {
10079        let string_builder = StringBuilder::new();
10080        let value_builder = IntervalDayTimeArray::builder(2);
10081        let mut builder = MapBuilder::new(
10082            Some(MapFieldNames {
10083                entry: "entries".to_string(),
10084                key: "key".to_string(),
10085                value: "value".to_string(),
10086            }),
10087            string_builder,
10088            value_builder,
10089        );
10090
10091        builder.keys().append_value("0");
10092        builder.values().append_value(IntervalDayTime::new(1, 1));
10093        builder.append(true).unwrap();
10094        builder.keys().append_value("1");
10095        builder.values().append_value(IntervalDayTime::new(2, 2));
10096        builder.append(true).unwrap();
10097
10098        // map builder returns unsorted map by default
10099        let array = builder.finish();
10100
10101        let new_ordered = true;
10102        let new_type = DataType::Map(
10103            Arc::new(Field::new(
10104                "entries",
10105                DataType::Struct(
10106                    vec![
10107                        Field::new("key", DataType::Utf8, false),
10108                        Field::new("value", DataType::Duration(TimeUnit::Second), false),
10109                    ]
10110                    .into(),
10111                ),
10112                false,
10113            )),
10114            new_ordered,
10115        );
10116
10117        let new_array_result = cast(&array, &new_type.clone());
10118        assert!(!can_cast_types(array.data_type(), &new_type));
10119        let Err(ArrowError::CastError(t)) = new_array_result else {
10120            panic!();
10121        };
10122        assert_eq!(
10123            t,
10124            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"#
10125        );
10126    }
10127
10128    #[test]
10129    fn test_cast_map_field_names() {
10130        let string_builder = StringBuilder::new();
10131        let value_builder = StringBuilder::new();
10132        let mut builder = MapBuilder::new(
10133            Some(MapFieldNames {
10134                entry: "entries".to_string(),
10135                key: "key".to_string(),
10136                value: "value".to_string(),
10137            }),
10138            string_builder,
10139            value_builder,
10140        );
10141
10142        builder.keys().append_value("0");
10143        builder.values().append_value("test_val_1");
10144        builder.append(true).unwrap();
10145        builder.keys().append_value("1");
10146        builder.values().append_value("test_val_2");
10147        builder.append(true).unwrap();
10148        builder.append(false).unwrap();
10149
10150        let array = builder.finish();
10151
10152        let new_type = DataType::Map(
10153            Arc::new(Field::new(
10154                "entries_new",
10155                DataType::Struct(
10156                    vec![
10157                        Field::new("key_new", DataType::Utf8, false),
10158                        Field::new("value_values", DataType::Utf8, false),
10159                    ]
10160                    .into(),
10161                ),
10162                false,
10163            )),
10164            false,
10165        );
10166
10167        assert_ne!(new_type, array.data_type().clone());
10168
10169        let new_array = cast(&array, &new_type.clone()).unwrap();
10170        assert_eq!(new_type, new_array.data_type().clone());
10171        let map_array = new_array.as_map();
10172
10173        assert_ne!(new_type, array.data_type().clone());
10174        assert_eq!(new_type, map_array.data_type().clone());
10175
10176        let key_string = map_array
10177            .keys()
10178            .as_any()
10179            .downcast_ref::<StringArray>()
10180            .unwrap()
10181            .into_iter()
10182            .flatten()
10183            .collect::<Vec<_>>();
10184        assert_eq!(&key_string, &vec!["0", "1"]);
10185
10186        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
10187        let values_string = values_string_array
10188            .as_any()
10189            .downcast_ref::<StringArray>()
10190            .unwrap()
10191            .into_iter()
10192            .flatten()
10193            .collect::<Vec<_>>();
10194        assert_eq!(&values_string, &vec!["test_val_1", "test_val_2"]);
10195
10196        assert_eq!(
10197            map_array.nulls(),
10198            Some(&NullBuffer::from(vec![true, true, false]))
10199        );
10200    }
10201
10202    #[test]
10203    fn test_cast_map_contained_values() {
10204        let string_builder = StringBuilder::new();
10205        let value_builder = Int8Builder::new();
10206        let mut builder = MapBuilder::new(
10207            Some(MapFieldNames {
10208                entry: "entries".to_string(),
10209                key: "key".to_string(),
10210                value: "value".to_string(),
10211            }),
10212            string_builder,
10213            value_builder,
10214        );
10215
10216        builder.keys().append_value("0");
10217        builder.values().append_value(44);
10218        builder.append(true).unwrap();
10219        builder.keys().append_value("1");
10220        builder.values().append_value(22);
10221        builder.append(true).unwrap();
10222
10223        let array = builder.finish();
10224
10225        let new_type = DataType::Map(
10226            Arc::new(Field::new(
10227                "entries",
10228                DataType::Struct(
10229                    vec![
10230                        Field::new("key", DataType::Utf8, false),
10231                        Field::new("value", DataType::Utf8, false),
10232                    ]
10233                    .into(),
10234                ),
10235                false,
10236            )),
10237            false,
10238        );
10239
10240        let new_array = cast(&array, &new_type.clone()).unwrap();
10241        assert_eq!(new_type, new_array.data_type().clone());
10242        let map_array = new_array.as_map();
10243
10244        assert_ne!(new_type, array.data_type().clone());
10245        assert_eq!(new_type, map_array.data_type().clone());
10246
10247        let key_string = map_array
10248            .keys()
10249            .as_any()
10250            .downcast_ref::<StringArray>()
10251            .unwrap()
10252            .into_iter()
10253            .flatten()
10254            .collect::<Vec<_>>();
10255        assert_eq!(&key_string, &vec!["0", "1"]);
10256
10257        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
10258        let values_string = values_string_array
10259            .as_any()
10260            .downcast_ref::<StringArray>()
10261            .unwrap()
10262            .into_iter()
10263            .flatten()
10264            .collect::<Vec<_>>();
10265        assert_eq!(&values_string, &vec!["44", "22"]);
10266    }
10267
10268    #[test]
10269    fn test_utf8_cast_offsets() {
10270        // test if offset of the array is taken into account during cast
10271        let str_array = StringArray::from(vec!["a", "b", "c"]);
10272        let str_array = str_array.slice(1, 2);
10273
10274        let out = cast(&str_array, &DataType::LargeUtf8).unwrap();
10275
10276        let large_str_array = out.as_any().downcast_ref::<LargeStringArray>().unwrap();
10277        let strs = large_str_array.into_iter().flatten().collect::<Vec<_>>();
10278        assert_eq!(strs, &["b", "c"])
10279    }
10280
10281    #[test]
10282    fn test_list_cast_offsets() {
10283        // test if offset of the array is taken into account during cast
10284        let array1 = make_list_array().slice(1, 2);
10285        let array2 = make_list_array();
10286
10287        let dt = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
10288        let out1 = cast(&array1, &dt).unwrap();
10289        let out2 = cast(&array2, &dt).unwrap();
10290
10291        assert_eq!(&out1, &out2.slice(1, 2))
10292    }
10293
10294    #[test]
10295    fn test_list_to_string() {
10296        fn assert_cast(array: &ArrayRef, expected: &[&str]) {
10297            assert!(can_cast_types(array.data_type(), &DataType::Utf8));
10298            let out = cast(array, &DataType::Utf8).unwrap();
10299            let out = out
10300                .as_string::<i32>()
10301                .into_iter()
10302                .flatten()
10303                .collect::<Vec<_>>();
10304            assert_eq!(out, expected);
10305
10306            assert!(can_cast_types(array.data_type(), &DataType::LargeUtf8));
10307            let out = cast(array, &DataType::LargeUtf8).unwrap();
10308            let out = out
10309                .as_string::<i64>()
10310                .into_iter()
10311                .flatten()
10312                .collect::<Vec<_>>();
10313            assert_eq!(out, expected);
10314
10315            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
10316            let out = cast(array, &DataType::Utf8View).unwrap();
10317            let out = out
10318                .as_string_view()
10319                .into_iter()
10320                .flatten()
10321                .collect::<Vec<_>>();
10322            assert_eq!(out, expected);
10323        }
10324
10325        let array = Arc::new(ListArray::new(
10326            Field::new_list_field(DataType::Utf8, true).into(),
10327            OffsetBuffer::from_lengths(vec![3, 3, 2]),
10328            Arc::new(StringArray::from(vec![
10329                "a", "b", "c", "d", "e", "f", "g", "h",
10330            ])),
10331            None,
10332        )) as ArrayRef;
10333
10334        assert_cast(&array, &["[a, b, c]", "[d, e, f]", "[g, h]"]);
10335
10336        let array = make_list_array();
10337        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10338
10339        let array = make_large_list_array();
10340        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10341
10342        let array = make_list_view_array();
10343        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10344
10345        let array = make_large_list_view_array();
10346        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10347    }
10348
10349    #[test]
10350    fn test_cast_f64_to_decimal128() {
10351        // to reproduce https://github.com/apache/arrow-rs/issues/2997
10352
10353        let decimal_type = DataType::Decimal128(18, 2);
10354        let array = Float64Array::from(vec![
10355            Some(0.0699999999),
10356            Some(0.0659999999),
10357            Some(0.0650000000),
10358            Some(0.0649999999),
10359        ]);
10360        let array = Arc::new(array) as ArrayRef;
10361        generate_cast_test_case!(
10362            &array,
10363            Decimal128Array,
10364            &decimal_type,
10365            vec![
10366                Some(7_i128), // round up
10367                Some(7_i128), // round up
10368                Some(7_i128), // round up
10369                Some(6_i128), // round down
10370            ]
10371        );
10372
10373        let decimal_type = DataType::Decimal128(18, 3);
10374        let array = Float64Array::from(vec![
10375            Some(0.0699999999),
10376            Some(0.0659999999),
10377            Some(0.0650000000),
10378            Some(0.0649999999),
10379        ]);
10380        let array = Arc::new(array) as ArrayRef;
10381        generate_cast_test_case!(
10382            &array,
10383            Decimal128Array,
10384            &decimal_type,
10385            vec![
10386                Some(70_i128), // round up
10387                Some(66_i128), // round up
10388                Some(65_i128), // round down
10389                Some(65_i128), // round up
10390            ]
10391        );
10392    }
10393
10394    #[test]
10395    fn test_cast_numeric_to_decimal128_overflow() {
10396        let array = Int64Array::from(vec![i64::MAX]);
10397        let array = Arc::new(array) as ArrayRef;
10398        let casted_array = cast_with_options(
10399            &array,
10400            &DataType::Decimal128(38, 30),
10401            &CastOptions {
10402                safe: true,
10403                format_options: FormatOptions::default(),
10404            },
10405        );
10406        assert!(casted_array.is_ok());
10407        assert!(casted_array.unwrap().is_null(0));
10408
10409        let casted_array = cast_with_options(
10410            &array,
10411            &DataType::Decimal128(38, 30),
10412            &CastOptions {
10413                safe: false,
10414                format_options: FormatOptions::default(),
10415            },
10416        );
10417        assert!(casted_array.is_err());
10418    }
10419
10420    #[test]
10421    fn test_cast_numeric_to_decimal256_overflow() {
10422        let array = Int64Array::from(vec![i64::MAX]);
10423        let array = Arc::new(array) as ArrayRef;
10424        let casted_array = cast_with_options(
10425            &array,
10426            &DataType::Decimal256(76, 76),
10427            &CastOptions {
10428                safe: true,
10429                format_options: FormatOptions::default(),
10430            },
10431        );
10432        assert!(casted_array.is_ok());
10433        assert!(casted_array.unwrap().is_null(0));
10434
10435        let casted_array = cast_with_options(
10436            &array,
10437            &DataType::Decimal256(76, 76),
10438            &CastOptions {
10439                safe: false,
10440                format_options: FormatOptions::default(),
10441            },
10442        );
10443        assert!(casted_array.is_err());
10444    }
10445
10446    #[test]
10447    fn test_cast_floating_point_to_decimal128_precision_overflow() {
10448        let array = Float64Array::from(vec![1.1]);
10449        let array = Arc::new(array) as ArrayRef;
10450        let casted_array = cast_with_options(
10451            &array,
10452            &DataType::Decimal128(2, 2),
10453            &CastOptions {
10454                safe: true,
10455                format_options: FormatOptions::default(),
10456            },
10457        );
10458        assert!(casted_array.is_ok());
10459        assert!(casted_array.unwrap().is_null(0));
10460
10461        let casted_array = cast_with_options(
10462            &array,
10463            &DataType::Decimal128(2, 2),
10464            &CastOptions {
10465                safe: false,
10466                format_options: FormatOptions::default(),
10467            },
10468        );
10469        let err = casted_array.unwrap_err().to_string();
10470        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
10471        assert!(
10472            err.contains(expected_error),
10473            "did not find expected error '{expected_error}' in actual error '{err}'"
10474        );
10475    }
10476
10477    #[test]
10478    fn test_cast_float16_to_decimal128_precision_overflow() {
10479        let array = Float16Array::from(vec![f16::from_f32(1.1)]);
10480        let array = Arc::new(array) as ArrayRef;
10481        let casted_array = cast_with_options(
10482            &array,
10483            &DataType::Decimal128(2, 2),
10484            &CastOptions {
10485                safe: true,
10486                format_options: FormatOptions::default(),
10487            },
10488        );
10489        assert!(casted_array.is_ok());
10490        assert!(casted_array.unwrap().is_null(0));
10491
10492        let casted_array = cast_with_options(
10493            &array,
10494            &DataType::Decimal128(2, 2),
10495            &CastOptions {
10496                safe: false,
10497                format_options: FormatOptions::default(),
10498            },
10499        );
10500        let err = casted_array.unwrap_err().to_string();
10501        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
10502        assert_eq!(err, expected_error);
10503    }
10504
10505    #[test]
10506    fn test_cast_float16_to_decimal256_precision_overflow() {
10507        let array = Float16Array::from(vec![f16::from_f32(1.1)]);
10508        let array = Arc::new(array) as ArrayRef;
10509        let casted_array = cast_with_options(
10510            &array,
10511            &DataType::Decimal256(2, 2),
10512            &CastOptions {
10513                safe: true,
10514                format_options: FormatOptions::default(),
10515            },
10516        );
10517        assert!(casted_array.is_ok());
10518        assert!(casted_array.unwrap().is_null(0));
10519
10520        let casted_array = cast_with_options(
10521            &array,
10522            &DataType::Decimal256(2, 2),
10523            &CastOptions {
10524                safe: false,
10525                format_options: FormatOptions::default(),
10526            },
10527        );
10528        let err = casted_array.unwrap_err().to_string();
10529        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
10530        assert_eq!(err, expected_error);
10531    }
10532
10533    #[test]
10534    fn test_cast_float16_to_decimal128_non_finite() {
10535        let array = Float16Array::from(vec![f16::NAN, f16::INFINITY, f16::NEG_INFINITY]);
10536        let array = Arc::new(array) as ArrayRef;
10537        let casted_array = cast_with_options(
10538            &array,
10539            &DataType::Decimal128(38, 2),
10540            &CastOptions {
10541                safe: true,
10542                format_options: FormatOptions::default(),
10543            },
10544        )
10545        .unwrap();
10546
10547        assert!(casted_array.is_null(0));
10548        assert!(casted_array.is_null(1));
10549        assert!(casted_array.is_null(2));
10550
10551        let casted_array = cast_with_options(
10552            &array,
10553            &DataType::Decimal128(38, 2),
10554            &CastOptions {
10555                safe: false,
10556                format_options: FormatOptions::default(),
10557            },
10558        );
10559        let err = casted_array.unwrap_err().to_string();
10560        let expected_error = "Cannot cast to Decimal128(38, 2)";
10561        assert!(
10562            err.contains(expected_error),
10563            "did not find expected error '{expected_error}' in actual error '{err}'"
10564        );
10565    }
10566
10567    #[test]
10568    fn test_cast_floating_point_to_decimal256_precision_overflow() {
10569        let array = Float64Array::from(vec![1.1]);
10570        let array = Arc::new(array) as ArrayRef;
10571        let casted_array = cast_with_options(
10572            &array,
10573            &DataType::Decimal256(2, 2),
10574            &CastOptions {
10575                safe: true,
10576                format_options: FormatOptions::default(),
10577            },
10578        );
10579        assert!(casted_array.is_ok());
10580        assert!(casted_array.unwrap().is_null(0));
10581
10582        let casted_array = cast_with_options(
10583            &array,
10584            &DataType::Decimal256(2, 2),
10585            &CastOptions {
10586                safe: false,
10587                format_options: FormatOptions::default(),
10588            },
10589        );
10590        let err = casted_array.unwrap_err().to_string();
10591        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
10592        assert_eq!(err, expected_error);
10593    }
10594
10595    #[test]
10596    fn test_cast_floating_point_to_decimal128_overflow() {
10597        let array = Float64Array::from(vec![f64::MAX]);
10598        let array = Arc::new(array) as ArrayRef;
10599        let casted_array = cast_with_options(
10600            &array,
10601            &DataType::Decimal128(38, 30),
10602            &CastOptions {
10603                safe: true,
10604                format_options: FormatOptions::default(),
10605            },
10606        );
10607        assert!(casted_array.is_ok());
10608        assert!(casted_array.unwrap().is_null(0));
10609
10610        let casted_array = cast_with_options(
10611            &array,
10612            &DataType::Decimal128(38, 30),
10613            &CastOptions {
10614                safe: false,
10615                format_options: FormatOptions::default(),
10616            },
10617        );
10618        let err = casted_array.unwrap_err().to_string();
10619        let expected_error = "Cast error: Cannot cast to Decimal128(38, 30)";
10620        assert!(
10621            err.contains(expected_error),
10622            "did not find expected error '{expected_error}' in actual error '{err}'"
10623        );
10624    }
10625
10626    #[test]
10627    fn test_cast_floating_point_to_decimal256_overflow() {
10628        let array = Float64Array::from(vec![f64::MAX]);
10629        let array = Arc::new(array) as ArrayRef;
10630        let casted_array = cast_with_options(
10631            &array,
10632            &DataType::Decimal256(76, 50),
10633            &CastOptions {
10634                safe: true,
10635                format_options: FormatOptions::default(),
10636            },
10637        );
10638        assert!(casted_array.is_ok());
10639        assert!(casted_array.unwrap().is_null(0));
10640
10641        let casted_array = cast_with_options(
10642            &array,
10643            &DataType::Decimal256(76, 50),
10644            &CastOptions {
10645                safe: false,
10646                format_options: FormatOptions::default(),
10647            },
10648        );
10649        let err = casted_array.unwrap_err().to_string();
10650        let expected_error = "Cast error: Cannot cast to Decimal256(76, 50)";
10651        assert!(
10652            err.contains(expected_error),
10653            "did not find expected error '{expected_error}' in actual error '{err}'"
10654        );
10655    }
10656    #[test]
10657    fn test_cast_decimal256_to_f64_no_overflow() {
10658        // Test casting i256::MAX: should produce a large finite positive value
10659        let array = vec![Some(i256::MAX)];
10660        let array = create_decimal256_array(array, 76, 2).unwrap();
10661        let array = Arc::new(array) as ArrayRef;
10662
10663        let result = cast(&array, &DataType::Float64).unwrap();
10664        let result = result.as_primitive::<Float64Type>();
10665        assert!(result.value(0).is_finite());
10666        assert!(result.value(0) > 0.0); // Positive result
10667
10668        // Test casting i256::MIN: should produce a large finite negative value
10669        let array = vec![Some(i256::MIN)];
10670        let array = create_decimal256_array(array, 76, 2).unwrap();
10671        let array = Arc::new(array) as ArrayRef;
10672
10673        let result = cast(&array, &DataType::Float64).unwrap();
10674        let result = result.as_primitive::<Float64Type>();
10675        assert!(result.value(0).is_finite());
10676        assert!(result.value(0) < 0.0); // Negative result
10677    }
10678
10679    #[test]
10680    fn test_cast_decimal128_to_decimal128_negative_scale() {
10681        let input_type = DataType::Decimal128(20, 0);
10682        let output_type = DataType::Decimal128(20, -1);
10683        assert!(can_cast_types(&input_type, &output_type));
10684        let array = vec![Some(1123450), Some(2123455), Some(3123456), None];
10685        let input_decimal_array = create_decimal128_array(array, 20, 0).unwrap();
10686        let array = Arc::new(input_decimal_array) as ArrayRef;
10687        generate_cast_test_case!(
10688            &array,
10689            Decimal128Array,
10690            &output_type,
10691            vec![
10692                Some(112345_i128),
10693                Some(212346_i128),
10694                Some(312346_i128),
10695                None
10696            ]
10697        );
10698
10699        let casted_array = cast(&array, &output_type).unwrap();
10700        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10701
10702        assert_eq!("1123450", decimal_arr.value_as_string(0));
10703        assert_eq!("2123460", decimal_arr.value_as_string(1));
10704        assert_eq!("3123460", decimal_arr.value_as_string(2));
10705    }
10706
10707    #[test]
10708    fn decimal128_min_max_to_f64() {
10709        // Ensure Decimal128 i128::MIN/MAX round-trip cast
10710        let min128 = i128::MIN;
10711        let max128 = i128::MAX;
10712        assert_eq!(min128 as f64, min128 as f64);
10713        assert_eq!(max128 as f64, max128 as f64);
10714    }
10715
10716    #[test]
10717    fn test_cast_numeric_to_decimal128_negative() {
10718        let decimal_type = DataType::Decimal128(38, -1);
10719        let array = Arc::new(Int32Array::from(vec![
10720            Some(1123456),
10721            Some(2123456),
10722            Some(3123456),
10723        ])) as ArrayRef;
10724
10725        let casted_array = cast(&array, &decimal_type).unwrap();
10726        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10727
10728        assert_eq!("1123450", decimal_arr.value_as_string(0));
10729        assert_eq!("2123450", decimal_arr.value_as_string(1));
10730        assert_eq!("3123450", decimal_arr.value_as_string(2));
10731
10732        let array = Arc::new(Float32Array::from(vec![
10733            Some(1123.456),
10734            Some(2123.456),
10735            Some(3123.456),
10736        ])) as ArrayRef;
10737
10738        let casted_array = cast(&array, &decimal_type).unwrap();
10739        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10740
10741        assert_eq!("1120", decimal_arr.value_as_string(0));
10742        assert_eq!("2120", decimal_arr.value_as_string(1));
10743        assert_eq!("3120", decimal_arr.value_as_string(2));
10744    }
10745
10746    #[test]
10747    fn test_cast_decimal128_to_decimal128_negative() {
10748        let input_type = DataType::Decimal128(10, -1);
10749        let output_type = DataType::Decimal128(10, -2);
10750        assert!(can_cast_types(&input_type, &output_type));
10751        let array = vec![Some(123)];
10752        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
10753        let array = Arc::new(input_decimal_array) as ArrayRef;
10754        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(12_i128),]);
10755
10756        let casted_array = cast(&array, &output_type).unwrap();
10757        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10758
10759        assert_eq!("1200", decimal_arr.value_as_string(0));
10760
10761        let array = vec![Some(125)];
10762        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
10763        let array = Arc::new(input_decimal_array) as ArrayRef;
10764        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(13_i128),]);
10765
10766        let casted_array = cast(&array, &output_type).unwrap();
10767        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10768
10769        assert_eq!("1300", decimal_arr.value_as_string(0));
10770    }
10771
10772    #[test]
10773    fn test_cast_decimal128_to_decimal256_negative() {
10774        let input_type = DataType::Decimal128(10, 3);
10775        let output_type = DataType::Decimal256(10, 5);
10776        assert!(can_cast_types(&input_type, &output_type));
10777        let array = vec![Some(123456), Some(-123456)];
10778        let input_decimal_array = create_decimal128_array(array, 10, 3).unwrap();
10779        let array = Arc::new(input_decimal_array) as ArrayRef;
10780
10781        let hundred = i256::from_i128(100);
10782        generate_cast_test_case!(
10783            &array,
10784            Decimal256Array,
10785            &output_type,
10786            vec![
10787                Some(i256::from_i128(123456).mul_wrapping(hundred)),
10788                Some(i256::from_i128(-123456).mul_wrapping(hundred))
10789            ]
10790        );
10791    }
10792
10793    #[test]
10794    fn test_parse_string_to_decimal() {
10795        assert_eq!(
10796            Decimal128Type::format_decimal(
10797                parse_string_to_decimal_native::<Decimal128Type>("123.45", 2).unwrap(),
10798                38,
10799                2,
10800            ),
10801            "123.45"
10802        );
10803        assert_eq!(
10804            Decimal128Type::format_decimal(
10805                parse_string_to_decimal_native::<Decimal128Type>("12345", 2).unwrap(),
10806                38,
10807                2,
10808            ),
10809            "12345.00"
10810        );
10811        assert_eq!(
10812            Decimal128Type::format_decimal(
10813                parse_string_to_decimal_native::<Decimal128Type>("0.12345", 2).unwrap(),
10814                38,
10815                2,
10816            ),
10817            "0.12"
10818        );
10819        assert_eq!(
10820            Decimal128Type::format_decimal(
10821                parse_string_to_decimal_native::<Decimal128Type>(".12345", 2).unwrap(),
10822                38,
10823                2,
10824            ),
10825            "0.12"
10826        );
10827        assert_eq!(
10828            Decimal128Type::format_decimal(
10829                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
10830                38,
10831                2,
10832            ),
10833            "0.13"
10834        );
10835        assert_eq!(
10836            Decimal128Type::format_decimal(
10837                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
10838                38,
10839                2,
10840            ),
10841            "0.13"
10842        );
10843
10844        assert_eq!(
10845            Decimal256Type::format_decimal(
10846                parse_string_to_decimal_native::<Decimal256Type>("123.45", 3).unwrap(),
10847                38,
10848                3,
10849            ),
10850            "123.450"
10851        );
10852        assert_eq!(
10853            Decimal256Type::format_decimal(
10854                parse_string_to_decimal_native::<Decimal256Type>("12345", 3).unwrap(),
10855                38,
10856                3,
10857            ),
10858            "12345.000"
10859        );
10860        assert_eq!(
10861            Decimal256Type::format_decimal(
10862                parse_string_to_decimal_native::<Decimal256Type>("0.12345", 3).unwrap(),
10863                38,
10864                3,
10865            ),
10866            "0.123"
10867        );
10868        assert_eq!(
10869            Decimal256Type::format_decimal(
10870                parse_string_to_decimal_native::<Decimal256Type>(".12345", 3).unwrap(),
10871                38,
10872                3,
10873            ),
10874            "0.123"
10875        );
10876        assert_eq!(
10877            Decimal256Type::format_decimal(
10878                parse_string_to_decimal_native::<Decimal256Type>(".1265", 3).unwrap(),
10879                38,
10880                3,
10881            ),
10882            "0.127"
10883        );
10884    }
10885
10886    fn test_cast_string_to_decimal(array: ArrayRef) {
10887        // Decimal128
10888        let output_type = DataType::Decimal128(38, 2);
10889        assert!(can_cast_types(array.data_type(), &output_type));
10890
10891        let casted_array = cast(&array, &output_type).unwrap();
10892        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10893
10894        assert_eq!("123.45", decimal_arr.value_as_string(0));
10895        assert_eq!("1.23", decimal_arr.value_as_string(1));
10896        assert_eq!("0.12", decimal_arr.value_as_string(2));
10897        assert_eq!("0.13", decimal_arr.value_as_string(3));
10898        assert_eq!("1.26", decimal_arr.value_as_string(4));
10899        assert_eq!("12345.00", decimal_arr.value_as_string(5));
10900        assert_eq!("12345.00", decimal_arr.value_as_string(6));
10901        assert_eq!("0.12", decimal_arr.value_as_string(7));
10902        assert_eq!("12.23", decimal_arr.value_as_string(8));
10903        assert!(decimal_arr.is_null(9));
10904        assert!(decimal_arr.is_null(10));
10905        assert!(decimal_arr.is_null(11));
10906        assert!(decimal_arr.is_null(12));
10907        assert_eq!("-1.23", decimal_arr.value_as_string(13));
10908        assert_eq!("-1.24", decimal_arr.value_as_string(14));
10909        assert_eq!("0.00", decimal_arr.value_as_string(15));
10910        assert_eq!("-123.00", decimal_arr.value_as_string(16));
10911        assert_eq!("-123.23", decimal_arr.value_as_string(17));
10912        assert_eq!("-0.12", decimal_arr.value_as_string(18));
10913        assert_eq!("1.23", decimal_arr.value_as_string(19));
10914        assert_eq!("1.24", decimal_arr.value_as_string(20));
10915        assert_eq!("0.00", decimal_arr.value_as_string(21));
10916        assert_eq!("123.00", decimal_arr.value_as_string(22));
10917        assert_eq!("123.23", decimal_arr.value_as_string(23));
10918        assert_eq!("0.12", decimal_arr.value_as_string(24));
10919        assert!(decimal_arr.is_null(25));
10920        assert!(decimal_arr.is_null(26));
10921        assert!(decimal_arr.is_null(27));
10922        assert_eq!("0.00", decimal_arr.value_as_string(28));
10923        assert_eq!("0.00", decimal_arr.value_as_string(29));
10924        assert_eq!("12345.00", decimal_arr.value_as_string(30));
10925        assert_eq!(decimal_arr.len(), 31);
10926
10927        // Decimal256
10928        let output_type = DataType::Decimal256(76, 3);
10929        assert!(can_cast_types(array.data_type(), &output_type));
10930
10931        let casted_array = cast(&array, &output_type).unwrap();
10932        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
10933
10934        assert_eq!("123.450", decimal_arr.value_as_string(0));
10935        assert_eq!("1.235", decimal_arr.value_as_string(1));
10936        assert_eq!("0.123", decimal_arr.value_as_string(2));
10937        assert_eq!("0.127", decimal_arr.value_as_string(3));
10938        assert_eq!("1.263", decimal_arr.value_as_string(4));
10939        assert_eq!("12345.000", decimal_arr.value_as_string(5));
10940        assert_eq!("12345.000", decimal_arr.value_as_string(6));
10941        assert_eq!("0.123", decimal_arr.value_as_string(7));
10942        assert_eq!("12.234", decimal_arr.value_as_string(8));
10943        assert!(decimal_arr.is_null(9));
10944        assert!(decimal_arr.is_null(10));
10945        assert!(decimal_arr.is_null(11));
10946        assert!(decimal_arr.is_null(12));
10947        assert_eq!("-1.235", decimal_arr.value_as_string(13));
10948        assert_eq!("-1.236", decimal_arr.value_as_string(14));
10949        assert_eq!("0.000", decimal_arr.value_as_string(15));
10950        assert_eq!("-123.000", decimal_arr.value_as_string(16));
10951        assert_eq!("-123.234", decimal_arr.value_as_string(17));
10952        assert_eq!("-0.123", decimal_arr.value_as_string(18));
10953        assert_eq!("1.235", decimal_arr.value_as_string(19));
10954        assert_eq!("1.236", decimal_arr.value_as_string(20));
10955        assert_eq!("0.000", decimal_arr.value_as_string(21));
10956        assert_eq!("123.000", decimal_arr.value_as_string(22));
10957        assert_eq!("123.234", decimal_arr.value_as_string(23));
10958        assert_eq!("0.123", decimal_arr.value_as_string(24));
10959        assert!(decimal_arr.is_null(25));
10960        assert!(decimal_arr.is_null(26));
10961        assert!(decimal_arr.is_null(27));
10962        assert_eq!("0.000", decimal_arr.value_as_string(28));
10963        assert_eq!("0.000", decimal_arr.value_as_string(29));
10964        assert_eq!("12345.000", decimal_arr.value_as_string(30));
10965        assert_eq!(decimal_arr.len(), 31);
10966    }
10967
10968    #[test]
10969    fn test_cast_utf8_to_decimal() {
10970        let str_array = StringArray::from(vec![
10971            Some("123.45"),
10972            Some("1.2345"),
10973            Some("0.12345"),
10974            Some("0.1267"),
10975            Some("1.263"),
10976            Some("12345.0"),
10977            Some("12345"),
10978            Some("000.123"),
10979            Some("12.234000"),
10980            None,
10981            Some(""),
10982            Some(" "),
10983            None,
10984            Some("-1.23499999"),
10985            Some("-1.23599999"),
10986            Some("-0.00001"),
10987            Some("-123"),
10988            Some("-123.234000"),
10989            Some("-000.123"),
10990            Some("+1.23499999"),
10991            Some("+1.23599999"),
10992            Some("+0.00001"),
10993            Some("+123"),
10994            Some("+123.234000"),
10995            Some("+000.123"),
10996            Some("1.-23499999"),
10997            Some("-1.-23499999"),
10998            Some("--1.23499999"),
10999            Some("0"),
11000            Some("000.000"),
11001            Some("0000000000000000012345.000"),
11002        ]);
11003        let array = Arc::new(str_array) as ArrayRef;
11004
11005        test_cast_string_to_decimal(array);
11006
11007        let test_cases = [
11008            (None, None),
11009            (Some(""), None),
11010            (Some("   "), None),
11011            (Some("0"), Some("0")),
11012            (Some("000.000"), Some("0")),
11013            (Some("12345"), Some("12345")),
11014            (Some("000000000000000000000000000012345"), Some("12345")),
11015            (Some("-123"), Some("-123")),
11016            (Some("+123"), Some("123")),
11017        ];
11018        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
11019        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
11020
11021        let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
11022        test_cast_string_to_decimal_scale_zero(array, &expected);
11023    }
11024
11025    #[test]
11026    fn test_cast_large_utf8_to_decimal() {
11027        let str_array = LargeStringArray::from(vec![
11028            Some("123.45"),
11029            Some("1.2345"),
11030            Some("0.12345"),
11031            Some("0.1267"),
11032            Some("1.263"),
11033            Some("12345.0"),
11034            Some("12345"),
11035            Some("000.123"),
11036            Some("12.234000"),
11037            None,
11038            Some(""),
11039            Some(" "),
11040            None,
11041            Some("-1.23499999"),
11042            Some("-1.23599999"),
11043            Some("-0.00001"),
11044            Some("-123"),
11045            Some("-123.234000"),
11046            Some("-000.123"),
11047            Some("+1.23499999"),
11048            Some("+1.23599999"),
11049            Some("+0.00001"),
11050            Some("+123"),
11051            Some("+123.234000"),
11052            Some("+000.123"),
11053            Some("1.-23499999"),
11054            Some("-1.-23499999"),
11055            Some("--1.23499999"),
11056            Some("0"),
11057            Some("000.000"),
11058            Some("0000000000000000012345.000"),
11059        ]);
11060        let array = Arc::new(str_array) as ArrayRef;
11061
11062        test_cast_string_to_decimal(array);
11063
11064        let test_cases = [
11065            (None, None),
11066            (Some(""), None),
11067            (Some("   "), None),
11068            (Some("0"), Some("0")),
11069            (Some("000.000"), Some("0")),
11070            (Some("12345"), Some("12345")),
11071            (Some("000000000000000000000000000012345"), Some("12345")),
11072            (Some("-123"), Some("-123")),
11073            (Some("+123"), Some("123")),
11074        ];
11075        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
11076        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
11077
11078        let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
11079        test_cast_string_to_decimal_scale_zero(array, &expected);
11080    }
11081
11082    fn test_cast_string_to_decimal_scale_zero(
11083        array: ArrayRef,
11084        expected_as_string: &[Option<&str>],
11085    ) {
11086        // Decimal128
11087        let output_type = DataType::Decimal128(38, 0);
11088        assert!(can_cast_types(array.data_type(), &output_type));
11089        let casted_array = cast(&array, &output_type).unwrap();
11090        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11091        assert_decimal_array_contents(decimal_arr, expected_as_string);
11092
11093        // Decimal256
11094        let output_type = DataType::Decimal256(76, 0);
11095        assert!(can_cast_types(array.data_type(), &output_type));
11096        let casted_array = cast(&array, &output_type).unwrap();
11097        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
11098        assert_decimal_array_contents(decimal_arr, expected_as_string);
11099    }
11100
11101    fn assert_decimal_array_contents<T>(
11102        array: &PrimitiveArray<T>,
11103        expected_as_string: &[Option<&str>],
11104    ) where
11105        T: DecimalType + ArrowPrimitiveType,
11106    {
11107        assert_eq!(array.len(), expected_as_string.len());
11108        for (i, expected) in expected_as_string.iter().enumerate() {
11109            let actual = if array.is_null(i) {
11110                None
11111            } else {
11112                Some(array.value_as_string(i))
11113            };
11114            let actual = actual.as_ref().map(|s| s.as_ref());
11115            assert_eq!(*expected, actual, "Expected at position {i}");
11116        }
11117    }
11118
11119    #[test]
11120    fn test_cast_invalid_utf8_to_decimal() {
11121        let str_array = StringArray::from(vec!["4.4.5", ". 0.123"]);
11122        let array = Arc::new(str_array) as ArrayRef;
11123
11124        // Safe cast
11125        let output_type = DataType::Decimal128(38, 2);
11126        let casted_array = cast(&array, &output_type).unwrap();
11127        assert!(casted_array.is_null(0));
11128        assert!(casted_array.is_null(1));
11129
11130        let output_type = DataType::Decimal256(76, 2);
11131        let casted_array = cast(&array, &output_type).unwrap();
11132        assert!(casted_array.is_null(0));
11133        assert!(casted_array.is_null(1));
11134
11135        // Non-safe cast
11136        let output_type = DataType::Decimal128(38, 2);
11137        let str_array = StringArray::from(vec!["4.4.5"]);
11138        let array = Arc::new(str_array) as ArrayRef;
11139        let option = CastOptions {
11140            safe: false,
11141            format_options: FormatOptions::default(),
11142        };
11143        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
11144        assert!(
11145            casted_err
11146                .to_string()
11147                .contains("Cannot cast string '4.4.5' to value of Decimal128(38, 10) type")
11148        );
11149
11150        let str_array = StringArray::from(vec![". 0.123"]);
11151        let array = Arc::new(str_array) as ArrayRef;
11152        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
11153        assert!(
11154            casted_err
11155                .to_string()
11156                .contains("Cannot cast string '. 0.123' to value of Decimal128(38, 10) type")
11157        );
11158
11159        let str_array = StringArray::from(vec![""]);
11160        let array = Arc::new(str_array) as ArrayRef;
11161        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
11162        assert!(
11163            casted_err
11164                .to_string()
11165                .contains("Cannot cast string '' to value of Decimal128(38, 10) type")
11166        );
11167    }
11168
11169    fn test_cast_string_to_decimal128_overflow(overflow_array: ArrayRef) {
11170        let output_type = DataType::Decimal128(38, 2);
11171        let casted_array = cast(&overflow_array, &output_type).unwrap();
11172        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11173
11174        assert!(decimal_arr.is_null(0));
11175        assert!(decimal_arr.is_null(1));
11176        assert!(decimal_arr.is_null(2));
11177        assert_eq!(
11178            "999999999999999999999999999999999999.99",
11179            decimal_arr.value_as_string(3)
11180        );
11181        assert_eq!(
11182            "100000000000000000000000000000000000.00",
11183            decimal_arr.value_as_string(4)
11184        );
11185    }
11186
11187    #[test]
11188    fn test_cast_string_to_decimal128_precision_overflow() {
11189        let array = StringArray::from(vec!["1000".to_string()]);
11190        let array = Arc::new(array) as ArrayRef;
11191        let casted_array = cast_with_options(
11192            &array,
11193            &DataType::Decimal128(10, 8),
11194            &CastOptions {
11195                safe: true,
11196                format_options: FormatOptions::default(),
11197            },
11198        );
11199        assert!(casted_array.is_ok());
11200        assert!(casted_array.unwrap().is_null(0));
11201
11202        let err = cast_with_options(
11203            &array,
11204            &DataType::Decimal128(10, 8),
11205            &CastOptions {
11206                safe: false,
11207                format_options: FormatOptions::default(),
11208            },
11209        );
11210        assert_eq!(
11211            "Invalid argument error: 1000.00000000 is too large to store in a Decimal128 of precision 10. Max is 99.99999999",
11212            err.unwrap_err().to_string()
11213        );
11214    }
11215
11216    #[test]
11217    fn test_cast_utf8_to_decimal128_overflow() {
11218        let overflow_str_array = StringArray::from(vec![
11219            i128::MAX.to_string(),
11220            i128::MIN.to_string(),
11221            "99999999999999999999999999999999999999".to_string(),
11222            "999999999999999999999999999999999999.99".to_string(),
11223            "99999999999999999999999999999999999.999".to_string(),
11224        ]);
11225        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11226
11227        test_cast_string_to_decimal128_overflow(overflow_array);
11228    }
11229
11230    #[test]
11231    fn test_cast_large_utf8_to_decimal128_overflow() {
11232        let overflow_str_array = LargeStringArray::from(vec![
11233            i128::MAX.to_string(),
11234            i128::MIN.to_string(),
11235            "99999999999999999999999999999999999999".to_string(),
11236            "999999999999999999999999999999999999.99".to_string(),
11237            "99999999999999999999999999999999999.999".to_string(),
11238        ]);
11239        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11240
11241        test_cast_string_to_decimal128_overflow(overflow_array);
11242    }
11243
11244    fn test_cast_string_to_decimal256_overflow(overflow_array: ArrayRef) {
11245        let output_type = DataType::Decimal256(76, 2);
11246        let casted_array = cast(&overflow_array, &output_type).unwrap();
11247        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
11248
11249        assert_eq!(
11250            "170141183460469231731687303715884105727.00",
11251            decimal_arr.value_as_string(0)
11252        );
11253        assert_eq!(
11254            "-170141183460469231731687303715884105728.00",
11255            decimal_arr.value_as_string(1)
11256        );
11257        assert_eq!(
11258            "99999999999999999999999999999999999999.00",
11259            decimal_arr.value_as_string(2)
11260        );
11261        assert_eq!(
11262            "999999999999999999999999999999999999.99",
11263            decimal_arr.value_as_string(3)
11264        );
11265        assert_eq!(
11266            "100000000000000000000000000000000000.00",
11267            decimal_arr.value_as_string(4)
11268        );
11269        assert!(decimal_arr.is_null(5));
11270        assert!(decimal_arr.is_null(6));
11271    }
11272
11273    #[test]
11274    fn test_cast_string_to_decimal256_precision_overflow() {
11275        let array = StringArray::from(vec!["1000".to_string()]);
11276        let array = Arc::new(array) as ArrayRef;
11277        let casted_array = cast_with_options(
11278            &array,
11279            &DataType::Decimal256(10, 8),
11280            &CastOptions {
11281                safe: true,
11282                format_options: FormatOptions::default(),
11283            },
11284        );
11285        assert!(casted_array.is_ok());
11286        assert!(casted_array.unwrap().is_null(0));
11287
11288        let err = cast_with_options(
11289            &array,
11290            &DataType::Decimal256(10, 8),
11291            &CastOptions {
11292                safe: false,
11293                format_options: FormatOptions::default(),
11294            },
11295        );
11296        assert_eq!(
11297            "Invalid argument error: 1000.00000000 is too large to store in a Decimal256 of precision 10. Max is 99.99999999",
11298            err.unwrap_err().to_string()
11299        );
11300    }
11301
11302    #[test]
11303    fn test_cast_utf8_to_decimal256_overflow() {
11304        let overflow_str_array = StringArray::from(vec![
11305            i128::MAX.to_string(),
11306            i128::MIN.to_string(),
11307            "99999999999999999999999999999999999999".to_string(),
11308            "999999999999999999999999999999999999.99".to_string(),
11309            "99999999999999999999999999999999999.999".to_string(),
11310            i256::MAX.to_string(),
11311            i256::MIN.to_string(),
11312        ]);
11313        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11314
11315        test_cast_string_to_decimal256_overflow(overflow_array);
11316    }
11317
11318    #[test]
11319    fn test_cast_large_utf8_to_decimal256_overflow() {
11320        let overflow_str_array = LargeStringArray::from(vec![
11321            i128::MAX.to_string(),
11322            i128::MIN.to_string(),
11323            "99999999999999999999999999999999999999".to_string(),
11324            "999999999999999999999999999999999999.99".to_string(),
11325            "99999999999999999999999999999999999.999".to_string(),
11326            i256::MAX.to_string(),
11327            i256::MIN.to_string(),
11328        ]);
11329        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11330
11331        test_cast_string_to_decimal256_overflow(overflow_array);
11332    }
11333
11334    #[test]
11335    fn test_cast_outside_supported_range_for_nanoseconds() {
11336        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";
11337
11338        let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
11339
11340        let cast_options = CastOptions {
11341            safe: false,
11342            format_options: FormatOptions::default(),
11343        };
11344
11345        let result = cast_string_to_timestamp::<i32, TimestampNanosecondType>(
11346            &array,
11347            &None::<Arc<str>>,
11348            &cast_options,
11349        );
11350
11351        let err = result.unwrap_err();
11352        assert_eq!(
11353            err.to_string(),
11354            format!(
11355                "Cast error: Overflow converting {} to Nanosecond. {}",
11356                array.value(0),
11357                EXPECTED_ERROR_MESSAGE
11358            )
11359        );
11360    }
11361
11362    #[test]
11363    fn test_cast_date32_to_timestamp() {
11364        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
11365        let array = Arc::new(a) as ArrayRef;
11366        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
11367        let c = b.as_primitive::<TimestampSecondType>();
11368        assert_eq!(1609459200, c.value(0));
11369        assert_eq!(1640995200, c.value(1));
11370        assert!(c.is_null(2));
11371    }
11372
11373    #[test]
11374    fn test_cast_date32_to_timestamp_ms() {
11375        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
11376        let array = Arc::new(a) as ArrayRef;
11377        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
11378        let c = b
11379            .as_any()
11380            .downcast_ref::<TimestampMillisecondArray>()
11381            .unwrap();
11382        assert_eq!(1609459200000, c.value(0));
11383        assert_eq!(1640995200000, c.value(1));
11384        assert!(c.is_null(2));
11385    }
11386
11387    #[test]
11388    fn test_cast_date32_to_timestamp_us() {
11389        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
11390        let array = Arc::new(a) as ArrayRef;
11391        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
11392        let c = b
11393            .as_any()
11394            .downcast_ref::<TimestampMicrosecondArray>()
11395            .unwrap();
11396        assert_eq!(1609459200000000, c.value(0));
11397        assert_eq!(1640995200000000, c.value(1));
11398        assert!(c.is_null(2));
11399    }
11400
11401    #[test]
11402    fn test_cast_date32_to_timestamp_ns() {
11403        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
11404        let array = Arc::new(a) as ArrayRef;
11405        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
11406        let c = b
11407            .as_any()
11408            .downcast_ref::<TimestampNanosecondArray>()
11409            .unwrap();
11410        assert_eq!(1609459200000000000, c.value(0));
11411        assert_eq!(1640995200000000000, c.value(1));
11412        assert!(c.is_null(2));
11413    }
11414
11415    #[test]
11416    fn test_timezone_cast() {
11417        let a = StringArray::from(vec![
11418            "2000-01-01T12:00:00", // date + time valid
11419            "2020-12-15T12:34:56", // date + time valid
11420        ]);
11421        let array = Arc::new(a) as ArrayRef;
11422        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
11423        let v = b.as_primitive::<TimestampNanosecondType>();
11424
11425        assert_eq!(v.value(0), 946728000000000000);
11426        assert_eq!(v.value(1), 1608035696000000000);
11427
11428        let b = cast(
11429            &b,
11430            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11431        )
11432        .unwrap();
11433        let v = b.as_primitive::<TimestampNanosecondType>();
11434
11435        assert_eq!(v.value(0), 946728000000000000);
11436        assert_eq!(v.value(1), 1608035696000000000);
11437
11438        let b = cast(
11439            &b,
11440            &DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
11441        )
11442        .unwrap();
11443        let v = b.as_primitive::<TimestampMillisecondType>();
11444
11445        assert_eq!(v.value(0), 946728000000);
11446        assert_eq!(v.value(1), 1608035696000);
11447    }
11448
11449    #[test]
11450    fn test_cast_utf8_to_timestamp() {
11451        fn test_tz(tz: Arc<str>) {
11452            let valid = StringArray::from(vec![
11453                "2023-01-01 04:05:06.789000-08:00",
11454                "2023-01-01 04:05:06.789000-07:00",
11455                "2023-01-01 04:05:06.789 -0800",
11456                "2023-01-01 04:05:06.789 -08:00",
11457                "2023-01-01 040506 +0730",
11458                "2023-01-01 040506 +07:30",
11459                "2023-01-01 04:05:06.789",
11460                "2023-01-01 04:05:06",
11461                "2023-01-01",
11462            ]);
11463
11464            let array = Arc::new(valid) as ArrayRef;
11465            let b = cast_with_options(
11466                &array,
11467                &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
11468                &CastOptions {
11469                    safe: false,
11470                    format_options: FormatOptions::default(),
11471                },
11472            )
11473            .unwrap();
11474
11475            let tz = tz.as_ref().parse().unwrap();
11476
11477            let as_tz =
11478                |v: i64| as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap();
11479
11480            let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
11481            let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
11482
11483            let values = b.as_primitive::<TimestampNanosecondType>().values();
11484            let utc_results: Vec<_> = values.iter().map(as_utc).collect();
11485            let local_results: Vec<_> = values.iter().map(as_local).collect();
11486
11487            // Absolute timestamps should be parsed preserving the same UTC instant
11488            assert_eq!(
11489                &utc_results[..6],
11490                &[
11491                    "2023-01-01 12:05:06.789".to_string(),
11492                    "2023-01-01 11:05:06.789".to_string(),
11493                    "2023-01-01 12:05:06.789".to_string(),
11494                    "2023-01-01 12:05:06.789".to_string(),
11495                    "2022-12-31 20:35:06".to_string(),
11496                    "2022-12-31 20:35:06".to_string(),
11497                ]
11498            );
11499            // Non-absolute timestamps should be parsed preserving the same local instant
11500            assert_eq!(
11501                &local_results[6..],
11502                &[
11503                    "2023-01-01 04:05:06.789".to_string(),
11504                    "2023-01-01 04:05:06".to_string(),
11505                    "2023-01-01 00:00:00".to_string()
11506                ]
11507            )
11508        }
11509
11510        test_tz("+00:00".into());
11511        test_tz("+02:00".into());
11512    }
11513
11514    #[test]
11515    fn test_cast_invalid_utf8() {
11516        let v1: &[u8] = b"\xFF invalid";
11517        let v2: &[u8] = b"\x00 Foo";
11518        let s = BinaryArray::from(vec![v1, v2]);
11519        let options = CastOptions {
11520            safe: true,
11521            format_options: FormatOptions::default(),
11522        };
11523        let array = cast_with_options(&s, &DataType::Utf8, &options).unwrap();
11524        let a = array.as_string::<i32>();
11525        a.to_data().validate_full().unwrap();
11526
11527        assert_eq!(a.null_count(), 1);
11528        assert_eq!(a.len(), 2);
11529        assert!(a.is_null(0));
11530        assert_eq!(a.value(0), "");
11531        assert_eq!(a.value(1), "\x00 Foo");
11532    }
11533
11534    #[test]
11535    fn test_cast_utf8_to_timestamptz() {
11536        let valid = StringArray::from(vec!["2023-01-01"]);
11537
11538        let array = Arc::new(valid) as ArrayRef;
11539        let b = cast(
11540            &array,
11541            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11542        )
11543        .unwrap();
11544
11545        let expect = DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into()));
11546
11547        assert_eq!(b.data_type(), &expect);
11548        let c = b
11549            .as_any()
11550            .downcast_ref::<TimestampNanosecondArray>()
11551            .unwrap();
11552        assert_eq!(1672531200000000000, c.value(0));
11553    }
11554
11555    #[test]
11556    fn test_cast_decimal_to_string() {
11557        assert!(can_cast_types(
11558            &DataType::Decimal32(9, 4),
11559            &DataType::Utf8View
11560        ));
11561        assert!(can_cast_types(
11562            &DataType::Decimal64(16, 4),
11563            &DataType::Utf8View
11564        ));
11565        assert!(can_cast_types(
11566            &DataType::Decimal128(10, 4),
11567            &DataType::Utf8View
11568        ));
11569        assert!(can_cast_types(
11570            &DataType::Decimal256(38, 10),
11571            &DataType::Utf8View
11572        ));
11573
11574        macro_rules! assert_decimal_values {
11575            ($array:expr) => {
11576                let c = $array;
11577                assert_eq!("1123.454", c.value(0));
11578                assert_eq!("2123.456", c.value(1));
11579                assert_eq!("-3123.453", c.value(2));
11580                assert_eq!("-3123.456", c.value(3));
11581                assert_eq!("0.000", c.value(4));
11582                assert_eq!("0.123", c.value(5));
11583                assert_eq!("1234.567", c.value(6));
11584                assert_eq!("-1234.567", c.value(7));
11585                assert!(c.is_null(8));
11586            };
11587        }
11588
11589        fn test_decimal_to_string<IN: ArrowPrimitiveType, OffsetSize: OffsetSizeTrait>(
11590            output_type: DataType,
11591            array: PrimitiveArray<IN>,
11592        ) {
11593            let b = cast(&array, &output_type).unwrap();
11594
11595            assert_eq!(b.data_type(), &output_type);
11596            match b.data_type() {
11597                DataType::Utf8View => {
11598                    let c = b.as_string_view();
11599                    assert_decimal_values!(c);
11600                }
11601                DataType::Utf8 | DataType::LargeUtf8 => {
11602                    let c = b.as_string::<OffsetSize>();
11603                    assert_decimal_values!(c);
11604                }
11605                _ => (),
11606            }
11607        }
11608
11609        let array32: Vec<Option<i32>> = vec![
11610            Some(1123454),
11611            Some(2123456),
11612            Some(-3123453),
11613            Some(-3123456),
11614            Some(0),
11615            Some(123),
11616            Some(123456789),
11617            Some(-123456789),
11618            None,
11619        ];
11620        let array64: Vec<Option<i64>> = array32.iter().map(|num| num.map(|x| x as i64)).collect();
11621        let array128: Vec<Option<i128>> =
11622            array64.iter().map(|num| num.map(|x| x as i128)).collect();
11623        let array256: Vec<Option<i256>> = array128
11624            .iter()
11625            .map(|num| num.map(i256::from_i128))
11626            .collect();
11627
11628        test_decimal_to_string::<Decimal32Type, i32>(
11629            DataType::Utf8View,
11630            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
11631        );
11632        test_decimal_to_string::<Decimal32Type, i32>(
11633            DataType::Utf8,
11634            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
11635        );
11636        test_decimal_to_string::<Decimal32Type, i64>(
11637            DataType::LargeUtf8,
11638            create_decimal32_array(array32, 7, 3).unwrap(),
11639        );
11640
11641        test_decimal_to_string::<Decimal64Type, i32>(
11642            DataType::Utf8View,
11643            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
11644        );
11645        test_decimal_to_string::<Decimal64Type, i32>(
11646            DataType::Utf8,
11647            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
11648        );
11649        test_decimal_to_string::<Decimal64Type, i64>(
11650            DataType::LargeUtf8,
11651            create_decimal64_array(array64, 7, 3).unwrap(),
11652        );
11653
11654        test_decimal_to_string::<Decimal128Type, i32>(
11655            DataType::Utf8View,
11656            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
11657        );
11658        test_decimal_to_string::<Decimal128Type, i32>(
11659            DataType::Utf8,
11660            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
11661        );
11662        test_decimal_to_string::<Decimal128Type, i64>(
11663            DataType::LargeUtf8,
11664            create_decimal128_array(array128, 7, 3).unwrap(),
11665        );
11666
11667        test_decimal_to_string::<Decimal256Type, i32>(
11668            DataType::Utf8View,
11669            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
11670        );
11671        test_decimal_to_string::<Decimal256Type, i32>(
11672            DataType::Utf8,
11673            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
11674        );
11675        test_decimal_to_string::<Decimal256Type, i64>(
11676            DataType::LargeUtf8,
11677            create_decimal256_array(array256, 7, 3).unwrap(),
11678        );
11679    }
11680
11681    #[test]
11682    fn test_cast_numeric_to_decimal128_precision_overflow() {
11683        let array = Int64Array::from(vec![1234567]);
11684        let array = Arc::new(array) as ArrayRef;
11685        let casted_array = cast_with_options(
11686            &array,
11687            &DataType::Decimal128(7, 3),
11688            &CastOptions {
11689                safe: true,
11690                format_options: FormatOptions::default(),
11691            },
11692        );
11693        assert!(casted_array.is_ok());
11694        assert!(casted_array.unwrap().is_null(0));
11695
11696        let err = cast_with_options(
11697            &array,
11698            &DataType::Decimal128(7, 3),
11699            &CastOptions {
11700                safe: false,
11701                format_options: FormatOptions::default(),
11702            },
11703        );
11704        assert_eq!(
11705            "Invalid argument error: 1234567.000 is too large to store in a Decimal128 of precision 7. Max is 9999.999",
11706            err.unwrap_err().to_string()
11707        );
11708    }
11709
11710    #[test]
11711    fn test_cast_numeric_to_decimal256_precision_overflow() {
11712        let array = Int64Array::from(vec![1234567]);
11713        let array = Arc::new(array) as ArrayRef;
11714        let casted_array = cast_with_options(
11715            &array,
11716            &DataType::Decimal256(7, 3),
11717            &CastOptions {
11718                safe: true,
11719                format_options: FormatOptions::default(),
11720            },
11721        );
11722        assert!(casted_array.is_ok());
11723        assert!(casted_array.unwrap().is_null(0));
11724
11725        let err = cast_with_options(
11726            &array,
11727            &DataType::Decimal256(7, 3),
11728            &CastOptions {
11729                safe: false,
11730                format_options: FormatOptions::default(),
11731            },
11732        );
11733        assert_eq!(
11734            "Invalid argument error: 1234567.000 is too large to store in a Decimal256 of precision 7. Max is 9999.999",
11735            err.unwrap_err().to_string()
11736        );
11737    }
11738
11739    /// helper function to test casting from duration to interval
11740    fn cast_from_duration_to_interval<T: ArrowTemporalType<Native = i64>>(
11741        array: Vec<i64>,
11742        cast_options: &CastOptions,
11743    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
11744        let array = PrimitiveArray::<T>::new(array.into(), None);
11745        let array = Arc::new(array) as ArrayRef;
11746        let interval = DataType::Interval(IntervalUnit::MonthDayNano);
11747        let out = cast_with_options(&array, &interval, cast_options)?;
11748        let out = out.as_primitive::<IntervalMonthDayNanoType>().clone();
11749        Ok(out)
11750    }
11751
11752    #[test]
11753    fn test_cast_from_duration_to_interval() {
11754        // from duration second to interval month day nano
11755        let array = vec![1234567];
11756        let casted_array =
11757            cast_from_duration_to_interval::<DurationSecondType>(array, &CastOptions::default())
11758                .unwrap();
11759        assert_eq!(
11760            casted_array.data_type(),
11761            &DataType::Interval(IntervalUnit::MonthDayNano)
11762        );
11763        assert_eq!(
11764            casted_array.value(0),
11765            IntervalMonthDayNano::new(0, 0, 1234567000000000)
11766        );
11767
11768        let array = vec![i64::MAX];
11769        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
11770            array.clone(),
11771            &CastOptions::default(),
11772        )
11773        .unwrap();
11774        assert!(!casted_array.is_valid(0));
11775
11776        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
11777            array,
11778            &CastOptions {
11779                safe: false,
11780                format_options: FormatOptions::default(),
11781            },
11782        );
11783        assert!(casted_array.is_err());
11784
11785        // from duration millisecond to interval month day nano
11786        let array = vec![1234567];
11787        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11788            array,
11789            &CastOptions::default(),
11790        )
11791        .unwrap();
11792        assert_eq!(
11793            casted_array.data_type(),
11794            &DataType::Interval(IntervalUnit::MonthDayNano)
11795        );
11796        assert_eq!(
11797            casted_array.value(0),
11798            IntervalMonthDayNano::new(0, 0, 1234567000000)
11799        );
11800
11801        let array = vec![i64::MAX];
11802        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11803            array.clone(),
11804            &CastOptions::default(),
11805        )
11806        .unwrap();
11807        assert!(!casted_array.is_valid(0));
11808
11809        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11810            array,
11811            &CastOptions {
11812                safe: false,
11813                format_options: FormatOptions::default(),
11814            },
11815        );
11816        assert!(casted_array.is_err());
11817
11818        // from duration microsecond to interval month day nano
11819        let array = vec![1234567];
11820        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11821            array,
11822            &CastOptions::default(),
11823        )
11824        .unwrap();
11825        assert_eq!(
11826            casted_array.data_type(),
11827            &DataType::Interval(IntervalUnit::MonthDayNano)
11828        );
11829        assert_eq!(
11830            casted_array.value(0),
11831            IntervalMonthDayNano::new(0, 0, 1234567000)
11832        );
11833
11834        let array = vec![i64::MAX];
11835        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11836            array.clone(),
11837            &CastOptions::default(),
11838        )
11839        .unwrap();
11840        assert!(!casted_array.is_valid(0));
11841
11842        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11843            array,
11844            &CastOptions {
11845                safe: false,
11846                format_options: FormatOptions::default(),
11847            },
11848        );
11849        assert!(casted_array.is_err());
11850
11851        // from duration nanosecond to interval month day nano
11852        let array = vec![1234567];
11853        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
11854            array,
11855            &CastOptions::default(),
11856        )
11857        .unwrap();
11858        assert_eq!(
11859            casted_array.data_type(),
11860            &DataType::Interval(IntervalUnit::MonthDayNano)
11861        );
11862        assert_eq!(
11863            casted_array.value(0),
11864            IntervalMonthDayNano::new(0, 0, 1234567)
11865        );
11866
11867        let array = vec![i64::MAX];
11868        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
11869            array,
11870            &CastOptions {
11871                safe: false,
11872                format_options: FormatOptions::default(),
11873            },
11874        )
11875        .unwrap();
11876        assert_eq!(
11877            casted_array.value(0),
11878            IntervalMonthDayNano::new(0, 0, i64::MAX)
11879        );
11880    }
11881
11882    /// helper function to test casting from interval to duration
11883    fn cast_from_interval_to_duration<T: ArrowTemporalType>(
11884        array: &IntervalMonthDayNanoArray,
11885        cast_options: &CastOptions,
11886    ) -> Result<PrimitiveArray<T>, ArrowError> {
11887        let casted_array = cast_with_options(&array, &T::DATA_TYPE, cast_options)?;
11888        casted_array
11889            .as_any()
11890            .downcast_ref::<PrimitiveArray<T>>()
11891            .ok_or_else(|| {
11892                ArrowError::ComputeError(format!("Failed to downcast to {}", T::DATA_TYPE))
11893            })
11894            .cloned()
11895    }
11896
11897    #[test]
11898    fn test_cast_from_interval_to_duration() {
11899        let nullable = CastOptions::default();
11900        let fallible = CastOptions {
11901            safe: false,
11902            format_options: FormatOptions::default(),
11903        };
11904        let v = IntervalMonthDayNano::new(0, 0, 1234567);
11905
11906        // from interval month day nano to duration second
11907        let array = vec![v].into();
11908        let casted_array: DurationSecondArray =
11909            cast_from_interval_to_duration(&array, &nullable).unwrap();
11910        assert_eq!(casted_array.value(0), 0);
11911
11912        let array = vec![IntervalMonthDayNano::MAX].into();
11913        let casted_array: DurationSecondArray =
11914            cast_from_interval_to_duration(&array, &nullable).unwrap();
11915        assert!(!casted_array.is_valid(0));
11916
11917        let res = cast_from_interval_to_duration::<DurationSecondType>(&array, &fallible);
11918        assert!(res.is_err());
11919
11920        // from interval month day nano to duration millisecond
11921        let array = vec![v].into();
11922        let casted_array: DurationMillisecondArray =
11923            cast_from_interval_to_duration(&array, &nullable).unwrap();
11924        assert_eq!(casted_array.value(0), 1);
11925
11926        let array = vec![IntervalMonthDayNano::MAX].into();
11927        let casted_array: DurationMillisecondArray =
11928            cast_from_interval_to_duration(&array, &nullable).unwrap();
11929        assert!(!casted_array.is_valid(0));
11930
11931        let res = cast_from_interval_to_duration::<DurationMillisecondType>(&array, &fallible);
11932        assert!(res.is_err());
11933
11934        // from interval month day nano to duration microsecond
11935        let array = vec![v].into();
11936        let casted_array: DurationMicrosecondArray =
11937            cast_from_interval_to_duration(&array, &nullable).unwrap();
11938        assert_eq!(casted_array.value(0), 1234);
11939
11940        let array = vec![IntervalMonthDayNano::MAX].into();
11941        let casted_array =
11942            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &nullable).unwrap();
11943        assert!(!casted_array.is_valid(0));
11944
11945        let casted_array =
11946            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &fallible);
11947        assert!(casted_array.is_err());
11948
11949        // from interval month day nano to duration nanosecond
11950        let array = vec![v].into();
11951        let casted_array: DurationNanosecondArray =
11952            cast_from_interval_to_duration(&array, &nullable).unwrap();
11953        assert_eq!(casted_array.value(0), 1234567);
11954
11955        let array = vec![IntervalMonthDayNano::MAX].into();
11956        let casted_array: DurationNanosecondArray =
11957            cast_from_interval_to_duration(&array, &nullable).unwrap();
11958        assert!(!casted_array.is_valid(0));
11959
11960        let casted_array =
11961            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &fallible);
11962        assert!(casted_array.is_err());
11963
11964        let array = vec![
11965            IntervalMonthDayNanoType::make_value(0, 1, 0),
11966            IntervalMonthDayNanoType::make_value(-1, 0, 0),
11967            IntervalMonthDayNanoType::make_value(1, 1, 0),
11968            IntervalMonthDayNanoType::make_value(1, 0, 1),
11969            IntervalMonthDayNanoType::make_value(0, 0, -1),
11970        ]
11971        .into();
11972        let casted_array =
11973            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable).unwrap();
11974        assert!(!casted_array.is_valid(0));
11975        assert!(!casted_array.is_valid(1));
11976        assert!(!casted_array.is_valid(2));
11977        assert!(!casted_array.is_valid(3));
11978        assert!(casted_array.is_valid(4));
11979        assert_eq!(casted_array.value(4), -1);
11980    }
11981
11982    /// helper function to test casting from interval year month to interval month day nano
11983    fn cast_from_interval_year_month_to_interval_month_day_nano(
11984        array: Vec<i32>,
11985        cast_options: &CastOptions,
11986    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
11987        let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
11988        let array = Arc::new(array) as ArrayRef;
11989        let casted_array = cast_with_options(
11990            &array,
11991            &DataType::Interval(IntervalUnit::MonthDayNano),
11992            cast_options,
11993        )?;
11994        casted_array
11995            .as_any()
11996            .downcast_ref::<IntervalMonthDayNanoArray>()
11997            .ok_or_else(|| {
11998                ArrowError::ComputeError(
11999                    "Failed to downcast to IntervalMonthDayNanoArray".to_string(),
12000                )
12001            })
12002            .cloned()
12003    }
12004
12005    #[test]
12006    fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
12007        // from interval year month to interval month day nano
12008        let array = vec![1234567];
12009        let casted_array = cast_from_interval_year_month_to_interval_month_day_nano(
12010            array,
12011            &CastOptions::default(),
12012        )
12013        .unwrap();
12014        assert_eq!(
12015            casted_array.data_type(),
12016            &DataType::Interval(IntervalUnit::MonthDayNano)
12017        );
12018        assert_eq!(
12019            casted_array.value(0),
12020            IntervalMonthDayNano::new(1234567, 0, 0)
12021        );
12022    }
12023
12024    /// helper function to test casting from interval day time to interval month day nano
12025    fn cast_from_interval_day_time_to_interval_month_day_nano(
12026        array: Vec<IntervalDayTime>,
12027        cast_options: &CastOptions,
12028    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
12029        let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
12030        let array = Arc::new(array) as ArrayRef;
12031        let casted_array = cast_with_options(
12032            &array,
12033            &DataType::Interval(IntervalUnit::MonthDayNano),
12034            cast_options,
12035        )?;
12036        Ok(casted_array
12037            .as_primitive::<IntervalMonthDayNanoType>()
12038            .clone())
12039    }
12040
12041    #[test]
12042    fn test_cast_from_interval_day_time_to_interval_month_day_nano() {
12043        // from interval day time to interval month day nano
12044        let array = vec![IntervalDayTime::new(123, 0)];
12045        let casted_array =
12046            cast_from_interval_day_time_to_interval_month_day_nano(array, &CastOptions::default())
12047                .unwrap();
12048        assert_eq!(
12049            casted_array.data_type(),
12050            &DataType::Interval(IntervalUnit::MonthDayNano)
12051        );
12052        assert_eq!(casted_array.value(0), IntervalMonthDayNano::new(0, 123, 0));
12053    }
12054
12055    #[test]
12056    fn test_cast_below_unixtimestamp() {
12057        let valid = StringArray::from(vec![
12058            "1900-01-03 23:59:59",
12059            "1969-12-31 00:00:01",
12060            "1989-12-31 00:00:01",
12061        ]);
12062
12063        let array = Arc::new(valid) as ArrayRef;
12064        let casted_array = cast_with_options(
12065            &array,
12066            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
12067            &CastOptions {
12068                safe: false,
12069                format_options: FormatOptions::default(),
12070            },
12071        )
12072        .unwrap();
12073
12074        let ts_array = casted_array
12075            .as_primitive::<TimestampNanosecondType>()
12076            .values()
12077            .iter()
12078            .map(|ts| ts / 1_000_000)
12079            .collect::<Vec<_>>();
12080
12081        let array = TimestampMillisecondArray::from(ts_array).with_timezone("+00:00".to_string());
12082        let casted_array = cast(&array, &DataType::Date32).unwrap();
12083        let date_array = casted_array.as_primitive::<Date32Type>();
12084        let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
12085        let string_array = casted_array.as_string::<i32>();
12086        assert_eq!("1900-01-03", string_array.value(0));
12087        assert_eq!("1969-12-31", string_array.value(1));
12088        assert_eq!("1989-12-31", string_array.value(2));
12089    }
12090
12091    #[test]
12092    fn test_nested_list() {
12093        let mut list = ListBuilder::new(Int32Builder::new());
12094        list.append_value([Some(1), Some(2), Some(3)]);
12095        list.append_value([Some(4), None, Some(6)]);
12096        let list = list.finish();
12097
12098        let to_field = Field::new("nested", list.data_type().clone(), false);
12099        let to = DataType::List(Arc::new(to_field));
12100        let out = cast(&list, &to).unwrap();
12101        let opts = FormatOptions::default().with_null("null");
12102        let formatted = ArrayFormatter::try_new(out.as_ref(), &opts).unwrap();
12103
12104        assert_eq!(formatted.value(0).to_string(), "[[1], [2], [3]]");
12105        assert_eq!(formatted.value(1).to_string(), "[[4], [null], [6]]");
12106    }
12107
12108    #[test]
12109    fn test_nested_list_cast() {
12110        let mut builder = ListBuilder::new(ListBuilder::new(Int32Builder::new()));
12111        builder.append_value([Some([Some(1), Some(2), None]), None]);
12112        builder.append_value([None, Some([]), None]);
12113        builder.append_null();
12114        builder.append_value([Some([Some(2), Some(3)])]);
12115        let start = builder.finish();
12116
12117        let mut builder = LargeListBuilder::new(LargeListBuilder::new(Int8Builder::new()));
12118        builder.append_value([Some([Some(1), Some(2), None]), None]);
12119        builder.append_value([None, Some([]), None]);
12120        builder.append_null();
12121        builder.append_value([Some([Some(2), Some(3)])]);
12122        let expected = builder.finish();
12123
12124        let actual = cast(&start, expected.data_type()).unwrap();
12125        assert_eq!(actual.as_ref(), &expected);
12126    }
12127
12128    const CAST_OPTIONS: CastOptions<'static> = CastOptions {
12129        safe: true,
12130        format_options: FormatOptions::new(),
12131    };
12132
12133    #[test]
12134    #[allow(clippy::assertions_on_constants)]
12135    fn test_const_options() {
12136        assert!(CAST_OPTIONS.safe)
12137    }
12138
12139    #[test]
12140    fn test_list_format_options() {
12141        let options = CastOptions {
12142            safe: false,
12143            format_options: FormatOptions::default().with_null("null"),
12144        };
12145        let array = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
12146            Some(vec![Some(0), Some(1), Some(2)]),
12147            Some(vec![Some(0), None, Some(2)]),
12148        ]);
12149        let a = cast_with_options(&array, &DataType::Utf8, &options).unwrap();
12150        let r: Vec<_> = a.as_string::<i32>().iter().flatten().collect();
12151        assert_eq!(r, &["[0, 1, 2]", "[0, null, 2]"]);
12152    }
12153    #[test]
12154    fn test_cast_string_to_timestamp_invalid_tz() {
12155        // content after Z should be ignored
12156        let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
12157        let array = StringArray::from(vec![Some(bad_timestamp)]);
12158
12159        let data_types = [
12160            DataType::Timestamp(TimeUnit::Second, None),
12161            DataType::Timestamp(TimeUnit::Millisecond, None),
12162            DataType::Timestamp(TimeUnit::Microsecond, None),
12163            DataType::Timestamp(TimeUnit::Nanosecond, None),
12164        ];
12165
12166        let cast_options = CastOptions {
12167            safe: false,
12168            ..Default::default()
12169        };
12170
12171        for dt in data_types {
12172            assert_eq!(
12173                cast_with_options(&array, &dt, &cast_options)
12174                    .unwrap_err()
12175                    .to_string(),
12176                "Parser error: Invalid timezone \"ZZTOP\": only offset based timezones supported without chrono-tz feature"
12177            );
12178        }
12179    }
12180    #[test]
12181    fn test_cast_struct_to_struct() {
12182        let struct_type = DataType::Struct(
12183            vec![
12184                Field::new("a", DataType::Boolean, false),
12185                Field::new("b", DataType::Int32, false),
12186            ]
12187            .into(),
12188        );
12189        let to_type = DataType::Struct(
12190            vec![
12191                Field::new("a", DataType::Utf8, false),
12192                Field::new("b", DataType::Utf8, false),
12193            ]
12194            .into(),
12195        );
12196        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12197        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
12198        let struct_array = StructArray::from(vec![
12199            (
12200                Arc::new(Field::new("b", DataType::Boolean, false)),
12201                boolean.clone() as ArrayRef,
12202            ),
12203            (
12204                Arc::new(Field::new("c", DataType::Int32, false)),
12205                int.clone() as ArrayRef,
12206            ),
12207        ]);
12208        let casted_array = cast(&struct_array, &to_type).unwrap();
12209        let casted_array = casted_array.as_struct();
12210        assert_eq!(casted_array.data_type(), &to_type);
12211        let casted_boolean_array = casted_array
12212            .column(0)
12213            .as_string::<i32>()
12214            .into_iter()
12215            .flatten()
12216            .collect::<Vec<_>>();
12217        let casted_int_array = casted_array
12218            .column(1)
12219            .as_string::<i32>()
12220            .into_iter()
12221            .flatten()
12222            .collect::<Vec<_>>();
12223        assert_eq!(casted_boolean_array, vec!["false", "false", "true", "true"]);
12224        assert_eq!(casted_int_array, vec!["42", "28", "19", "31"]);
12225
12226        // test for can't cast
12227        let to_type = DataType::Struct(
12228            vec![
12229                Field::new("a", DataType::Date32, false),
12230                Field::new("b", DataType::Utf8, false),
12231            ]
12232            .into(),
12233        );
12234        assert!(!can_cast_types(&struct_type, &to_type));
12235        let result = cast(&struct_array, &to_type);
12236        assert_eq!(
12237            "Cast error: Casting from Boolean to Date32 not supported",
12238            result.unwrap_err().to_string()
12239        );
12240    }
12241
12242    #[test]
12243    fn test_cast_struct_to_struct_nullability() {
12244        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12245        let int = Arc::new(Int32Array::from(vec![Some(42), None, Some(19), None]));
12246        let struct_array = StructArray::from(vec![
12247            (
12248                Arc::new(Field::new("b", DataType::Boolean, false)),
12249                boolean.clone() as ArrayRef,
12250            ),
12251            (
12252                Arc::new(Field::new("c", DataType::Int32, true)),
12253                int.clone() as ArrayRef,
12254            ),
12255        ]);
12256
12257        // okay: nullable to nullable
12258        let to_type = DataType::Struct(
12259            vec![
12260                Field::new("a", DataType::Utf8, false),
12261                Field::new("b", DataType::Utf8, true),
12262            ]
12263            .into(),
12264        );
12265        cast(&struct_array, &to_type).expect("Cast nullable to nullable struct field should work");
12266
12267        // error: nullable to non-nullable
12268        let to_type = DataType::Struct(
12269            vec![
12270                Field::new("a", DataType::Utf8, false),
12271                Field::new("b", DataType::Utf8, false),
12272            ]
12273            .into(),
12274        );
12275        cast(&struct_array, &to_type)
12276            .expect_err("Cast nullable to non-nullable struct field should fail");
12277
12278        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12279        let int = Arc::new(Int32Array::from(vec![i32::MAX, 25, 1, 100]));
12280        let struct_array = StructArray::from(vec![
12281            (
12282                Arc::new(Field::new("b", DataType::Boolean, false)),
12283                boolean.clone() as ArrayRef,
12284            ),
12285            (
12286                Arc::new(Field::new("c", DataType::Int32, false)),
12287                int.clone() as ArrayRef,
12288            ),
12289        ]);
12290
12291        // okay: non-nullable to non-nullable
12292        let to_type = DataType::Struct(
12293            vec![
12294                Field::new("a", DataType::Utf8, false),
12295                Field::new("b", DataType::Utf8, false),
12296            ]
12297            .into(),
12298        );
12299        cast(&struct_array, &to_type)
12300            .expect("Cast non-nullable to non-nullable struct field should work");
12301
12302        // err: non-nullable to non-nullable but overflowing return null during casting
12303        let to_type = DataType::Struct(
12304            vec![
12305                Field::new("a", DataType::Utf8, false),
12306                Field::new("b", DataType::Int8, false),
12307            ]
12308            .into(),
12309        );
12310        cast(&struct_array, &to_type).expect_err(
12311            "Cast non-nullable to non-nullable struct field returning null should fail",
12312        );
12313    }
12314
12315    #[test]
12316    fn test_cast_struct_to_non_struct() {
12317        let boolean = Arc::new(BooleanArray::from(vec![true, false]));
12318        let struct_array = StructArray::from(vec![(
12319            Arc::new(Field::new("a", DataType::Boolean, false)),
12320            boolean.clone() as ArrayRef,
12321        )]);
12322        let to_type = DataType::Utf8;
12323        let result = cast(&struct_array, &to_type);
12324        assert_eq!(
12325            r#"Cast error: Casting from Struct("a": non-null Boolean) to Utf8 not supported"#,
12326            result.unwrap_err().to_string()
12327        );
12328    }
12329
12330    #[test]
12331    fn test_cast_non_struct_to_struct() {
12332        let array = StringArray::from(vec!["a", "b"]);
12333        let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
12334        let result = cast(&array, &to_type);
12335        assert_eq!(
12336            r#"Cast error: Casting from Utf8 to Struct("a": non-null Boolean) not supported"#,
12337            result.unwrap_err().to_string()
12338        );
12339    }
12340
12341    #[test]
12342    fn test_cast_struct_with_different_field_order() {
12343        // Test slow path: fields are in different order
12344        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12345        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
12346        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
12347
12348        let struct_array = StructArray::from(vec![
12349            (
12350                Arc::new(Field::new("a", DataType::Boolean, false)),
12351                boolean.clone() as ArrayRef,
12352            ),
12353            (
12354                Arc::new(Field::new("b", DataType::Int32, false)),
12355                int.clone() as ArrayRef,
12356            ),
12357            (
12358                Arc::new(Field::new("c", DataType::Utf8, false)),
12359                string.clone() as ArrayRef,
12360            ),
12361        ]);
12362
12363        // Target has fields in different order: c, a, b instead of a, b, c
12364        let to_type = DataType::Struct(
12365            vec![
12366                Field::new("c", DataType::Utf8, false),
12367                Field::new("a", DataType::Utf8, false), // Boolean to Utf8
12368                Field::new("b", DataType::Utf8, false), // Int32 to Utf8
12369            ]
12370            .into(),
12371        );
12372
12373        let result = cast(&struct_array, &to_type).unwrap();
12374        let result_struct = result.as_struct();
12375
12376        assert_eq!(result_struct.data_type(), &to_type);
12377        assert_eq!(result_struct.num_columns(), 3);
12378
12379        // Verify field "c" (originally position 2, now position 0) remains Utf8
12380        let c_column = result_struct.column(0).as_string::<i32>();
12381        assert_eq!(
12382            c_column.into_iter().flatten().collect::<Vec<_>>(),
12383            vec!["foo", "bar", "baz", "qux"]
12384        );
12385
12386        // Verify field "a" (originally position 0, now position 1) was cast from Boolean to Utf8
12387        let a_column = result_struct.column(1).as_string::<i32>();
12388        assert_eq!(
12389            a_column.into_iter().flatten().collect::<Vec<_>>(),
12390            vec!["false", "false", "true", "true"]
12391        );
12392
12393        // Verify field "b" (originally position 1, now position 2) was cast from Int32 to Utf8
12394        let b_column = result_struct.column(2).as_string::<i32>();
12395        assert_eq!(
12396            b_column.into_iter().flatten().collect::<Vec<_>>(),
12397            vec!["42", "28", "19", "31"]
12398        );
12399    }
12400
12401    #[test]
12402    fn test_cast_struct_with_missing_field() {
12403        // Test that casting fails when target has a field not present in source
12404        let boolean = Arc::new(BooleanArray::from(vec![false, true]));
12405        let struct_array = StructArray::from(vec![(
12406            Arc::new(Field::new("a", DataType::Boolean, false)),
12407            boolean.clone() as ArrayRef,
12408        )]);
12409
12410        let to_type = DataType::Struct(
12411            vec![
12412                Field::new("a", DataType::Utf8, false),
12413                Field::new("b", DataType::Int32, false), // Field "b" doesn't exist in source
12414            ]
12415            .into(),
12416        );
12417
12418        let result = cast(&struct_array, &to_type);
12419        assert!(result.is_err());
12420        assert_eq!(
12421            result.unwrap_err().to_string(),
12422            "Invalid argument error: Incorrect number of arrays for StructArray fields, expected 2 got 1"
12423        );
12424    }
12425
12426    #[test]
12427    fn test_cast_struct_with_subset_of_fields() {
12428        // Test casting to a struct with fewer fields (selecting a subset)
12429        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12430        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
12431        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
12432
12433        let struct_array = StructArray::from(vec![
12434            (
12435                Arc::new(Field::new("a", DataType::Boolean, false)),
12436                boolean.clone() as ArrayRef,
12437            ),
12438            (
12439                Arc::new(Field::new("b", DataType::Int32, false)),
12440                int.clone() as ArrayRef,
12441            ),
12442            (
12443                Arc::new(Field::new("c", DataType::Utf8, false)),
12444                string.clone() as ArrayRef,
12445            ),
12446        ]);
12447
12448        // Target has only fields "c" and "a", omitting "b"
12449        let to_type = DataType::Struct(
12450            vec![
12451                Field::new("c", DataType::Utf8, false),
12452                Field::new("a", DataType::Utf8, false),
12453            ]
12454            .into(),
12455        );
12456
12457        let result = cast(&struct_array, &to_type).unwrap();
12458        let result_struct = result.as_struct();
12459
12460        assert_eq!(result_struct.data_type(), &to_type);
12461        assert_eq!(result_struct.num_columns(), 2);
12462
12463        // Verify field "c" remains Utf8
12464        let c_column = result_struct.column(0).as_string::<i32>();
12465        assert_eq!(
12466            c_column.into_iter().flatten().collect::<Vec<_>>(),
12467            vec!["foo", "bar", "baz", "qux"]
12468        );
12469
12470        // Verify field "a" was cast from Boolean to Utf8
12471        let a_column = result_struct.column(1).as_string::<i32>();
12472        assert_eq!(
12473            a_column.into_iter().flatten().collect::<Vec<_>>(),
12474            vec!["false", "false", "true", "true"]
12475        );
12476    }
12477
12478    #[test]
12479    fn test_can_cast_struct_rename_field() {
12480        // Test that can_cast_types returns false when target has a field not in source
12481        let from_type = DataType::Struct(
12482            vec![
12483                Field::new("a", DataType::Int32, false),
12484                Field::new("b", DataType::Utf8, false),
12485            ]
12486            .into(),
12487        );
12488
12489        let to_type = DataType::Struct(
12490            vec![
12491                Field::new("a", DataType::Int64, false),
12492                Field::new("c", DataType::Boolean, false), // Field "c" not in source
12493            ]
12494            .into(),
12495        );
12496
12497        assert!(can_cast_types(&from_type, &to_type));
12498    }
12499
12500    fn run_decimal_cast_test_case_between_multiple_types(t: DecimalCastTestConfig) {
12501        run_decimal_cast_test_case::<Decimal128Type, Decimal128Type>(t.clone());
12502        run_decimal_cast_test_case::<Decimal128Type, Decimal256Type>(t.clone());
12503        run_decimal_cast_test_case::<Decimal256Type, Decimal128Type>(t.clone());
12504        run_decimal_cast_test_case::<Decimal256Type, Decimal256Type>(t.clone());
12505    }
12506
12507    #[test]
12508    fn test_decimal_to_decimal_coverage() {
12509        let test_cases = [
12510            // increase precision, increase scale, infallible
12511            DecimalCastTestConfig {
12512                input_prec: 5,
12513                input_scale: 1,
12514                input_repr: 99999, // 9999.9
12515                output_prec: 10,
12516                output_scale: 6,
12517                expected_output_repr: Ok(9999900000), // 9999.900000
12518            },
12519            // increase precision, increase scale, fallible, safe
12520            DecimalCastTestConfig {
12521                input_prec: 5,
12522                input_scale: 1,
12523                input_repr: 99, // 9999.9
12524                output_prec: 7,
12525                output_scale: 6,
12526                expected_output_repr: Ok(9900000), // 9.900000
12527            },
12528            // increase precision, increase scale, fallible, unsafe
12529            DecimalCastTestConfig {
12530                input_prec: 5,
12531                input_scale: 1,
12532                input_repr: 99999, // 9999.9
12533                output_prec: 7,
12534                output_scale: 6,
12535                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
12536            },
12537            // increase precision, decrease scale, always infallible
12538            DecimalCastTestConfig {
12539                input_prec: 5,
12540                input_scale: 3,
12541                input_repr: 99999, // 99.999
12542                output_prec: 10,
12543                output_scale: 2,
12544                expected_output_repr: Ok(10000), // 100.00
12545            },
12546            // increase precision, decrease scale, no rouding
12547            DecimalCastTestConfig {
12548                input_prec: 5,
12549                input_scale: 3,
12550                input_repr: 99994, // 99.994
12551                output_prec: 10,
12552                output_scale: 2,
12553                expected_output_repr: Ok(9999), // 99.99
12554            },
12555            // increase precision, don't change scale, always infallible
12556            DecimalCastTestConfig {
12557                input_prec: 5,
12558                input_scale: 3,
12559                input_repr: 99999, // 99.999
12560                output_prec: 10,
12561                output_scale: 3,
12562                expected_output_repr: Ok(99999), // 99.999
12563            },
12564            // decrease precision, increase scale, safe
12565            DecimalCastTestConfig {
12566                input_prec: 10,
12567                input_scale: 5,
12568                input_repr: 999999, // 9.99999
12569                output_prec: 8,
12570                output_scale: 7,
12571                expected_output_repr: Ok(99999900), // 9.9999900
12572            },
12573            // decrease precision, increase scale, unsafe
12574            DecimalCastTestConfig {
12575                input_prec: 10,
12576                input_scale: 5,
12577                input_repr: 9999999, // 99.99999
12578                output_prec: 8,
12579                output_scale: 7,
12580                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
12581            },
12582            // decrease precision, decrease scale, safe, infallible
12583            DecimalCastTestConfig {
12584                input_prec: 7,
12585                input_scale: 4,
12586                input_repr: 9999999, // 999.9999
12587                output_prec: 6,
12588                output_scale: 2,
12589                expected_output_repr: Ok(100000),
12590            },
12591            // decrease precision, decrease scale, safe, fallible
12592            DecimalCastTestConfig {
12593                input_prec: 10,
12594                input_scale: 5,
12595                input_repr: 12345678, // 123.45678
12596                output_prec: 8,
12597                output_scale: 3,
12598                expected_output_repr: Ok(123457), // 123.457
12599            },
12600            // decrease precision, decrease scale, unsafe
12601            DecimalCastTestConfig {
12602                input_prec: 10,
12603                input_scale: 5,
12604                input_repr: 9999999, // 99.99999
12605                output_prec: 4,
12606                output_scale: 3,
12607                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
12608            },
12609            // decrease precision, same scale, safe
12610            DecimalCastTestConfig {
12611                input_prec: 10,
12612                input_scale: 5,
12613                input_repr: 999999, // 9.99999
12614                output_prec: 6,
12615                output_scale: 5,
12616                expected_output_repr: Ok(999999), // 9.99999
12617            },
12618            // decrease precision, same scale, unsafe
12619            DecimalCastTestConfig {
12620                input_prec: 10,
12621                input_scale: 5,
12622                input_repr: 9999999, // 99.99999
12623                output_prec: 6,
12624                output_scale: 5,
12625                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
12626            },
12627            // same precision, increase scale, safe
12628            DecimalCastTestConfig {
12629                input_prec: 7,
12630                input_scale: 4,
12631                input_repr: 12345, // 1.2345
12632                output_prec: 7,
12633                output_scale: 6,
12634                expected_output_repr: Ok(1234500), // 1.234500
12635            },
12636            // same precision, increase scale, unsafe
12637            DecimalCastTestConfig {
12638                input_prec: 7,
12639                input_scale: 4,
12640                input_repr: 123456, // 12.3456
12641                output_prec: 7,
12642                output_scale: 6,
12643                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
12644            },
12645            // same precision, decrease scale, infallible
12646            DecimalCastTestConfig {
12647                input_prec: 7,
12648                input_scale: 5,
12649                input_repr: 1234567, // 12.34567
12650                output_prec: 7,
12651                output_scale: 4,
12652                expected_output_repr: Ok(123457), // 12.3457
12653            },
12654            // same precision, same scale, infallible
12655            DecimalCastTestConfig {
12656                input_prec: 7,
12657                input_scale: 5,
12658                input_repr: 9999999, // 99.99999
12659                output_prec: 7,
12660                output_scale: 5,
12661                expected_output_repr: Ok(9999999), // 99.99999
12662            },
12663            // precision increase, input scale & output scale = 0, infallible
12664            DecimalCastTestConfig {
12665                input_prec: 7,
12666                input_scale: 0,
12667                input_repr: 1234567, // 1234567
12668                output_prec: 8,
12669                output_scale: 0,
12670                expected_output_repr: Ok(1234567), // 1234567
12671            },
12672            // precision decrease, input scale & output scale = 0, failure
12673            DecimalCastTestConfig {
12674                input_prec: 7,
12675                input_scale: 0,
12676                input_repr: 1234567, // 1234567
12677                output_prec: 6,
12678                output_scale: 0,
12679                expected_output_repr: Err("Invalid argument error: 1234567 is too large to store in a {} of precision 6. Max is 999999".to_string())
12680            },
12681            // precision decrease, input scale & output scale = 0, success
12682            DecimalCastTestConfig {
12683                input_prec: 7,
12684                input_scale: 0,
12685                input_repr: 123456, // 123456
12686                output_prec: 6,
12687                output_scale: 0,
12688                expected_output_repr: Ok(123456), // 123456
12689            },
12690        ];
12691
12692        for t in test_cases {
12693            run_decimal_cast_test_case_between_multiple_types(t);
12694        }
12695    }
12696
12697    #[test]
12698    fn test_decimal_to_decimal_increase_scale_and_precision_unchecked() {
12699        let test_cases = [
12700            DecimalCastTestConfig {
12701                input_prec: 5,
12702                input_scale: 0,
12703                input_repr: 99999,
12704                output_prec: 10,
12705                output_scale: 5,
12706                expected_output_repr: Ok(9999900000),
12707            },
12708            DecimalCastTestConfig {
12709                input_prec: 5,
12710                input_scale: 0,
12711                input_repr: -99999,
12712                output_prec: 10,
12713                output_scale: 5,
12714                expected_output_repr: Ok(-9999900000),
12715            },
12716            DecimalCastTestConfig {
12717                input_prec: 5,
12718                input_scale: 2,
12719                input_repr: 99999,
12720                output_prec: 10,
12721                output_scale: 5,
12722                expected_output_repr: Ok(99999000),
12723            },
12724            DecimalCastTestConfig {
12725                input_prec: 5,
12726                input_scale: -2,
12727                input_repr: -99999,
12728                output_prec: 10,
12729                output_scale: 3,
12730                expected_output_repr: Ok(-9999900000),
12731            },
12732            DecimalCastTestConfig {
12733                input_prec: 5,
12734                input_scale: 3,
12735                input_repr: -12345,
12736                output_prec: 6,
12737                output_scale: 5,
12738                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())
12739            },
12740        ];
12741
12742        for t in test_cases {
12743            run_decimal_cast_test_case_between_multiple_types(t);
12744        }
12745    }
12746
12747    #[test]
12748    fn test_decimal_to_decimal_decrease_scale_and_precision_unchecked() {
12749        let test_cases = [
12750            DecimalCastTestConfig {
12751                input_prec: 5,
12752                input_scale: 0,
12753                input_repr: 99999,
12754                output_scale: -3,
12755                output_prec: 3,
12756                expected_output_repr: Ok(100),
12757            },
12758            DecimalCastTestConfig {
12759                input_prec: 5,
12760                input_scale: 0,
12761                input_repr: -99999,
12762                output_prec: 1,
12763                output_scale: -5,
12764                expected_output_repr: Ok(-1),
12765            },
12766            DecimalCastTestConfig {
12767                input_prec: 10,
12768                input_scale: 2,
12769                input_repr: 123456789,
12770                output_prec: 5,
12771                output_scale: -2,
12772                expected_output_repr: Ok(12346),
12773            },
12774            DecimalCastTestConfig {
12775                input_prec: 10,
12776                input_scale: 4,
12777                input_repr: -9876543210,
12778                output_prec: 7,
12779                output_scale: 0,
12780                expected_output_repr: Ok(-987654),
12781            },
12782            DecimalCastTestConfig {
12783                input_prec: 7,
12784                input_scale: 4,
12785                input_repr: 9999999,
12786                output_prec: 6,
12787                output_scale: 3,
12788                expected_output_repr:
12789                    Err("Invalid argument error: 1000.000 is too large to store in a {} of precision 6. Max is 999.999".to_string()),
12790            },
12791        ];
12792        for t in test_cases {
12793            run_decimal_cast_test_case_between_multiple_types(t);
12794        }
12795    }
12796
12797    #[test]
12798    fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
12799        let array = vec![Some(123456789)];
12800        let array = create_decimal128_array(array, 24, 2).unwrap();
12801        let input_type = DataType::Decimal128(24, 2);
12802        let output_type = DataType::Decimal128(6, 2);
12803        assert!(can_cast_types(&input_type, &output_type));
12804
12805        let options = CastOptions {
12806            safe: false,
12807            ..Default::default()
12808        };
12809        let result = cast_with_options(&array, &output_type, &options);
12810        assert_eq!(
12811            result.unwrap_err().to_string(),
12812            "Invalid argument error: 1234567.89 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
12813        );
12814    }
12815
12816    #[test]
12817    fn test_decimal_to_decimal_same_scale() {
12818        let array = vec![Some(520)];
12819        let array = create_decimal128_array(array, 4, 2).unwrap();
12820        let input_type = DataType::Decimal128(4, 2);
12821        let output_type = DataType::Decimal128(3, 2);
12822        assert!(can_cast_types(&input_type, &output_type));
12823
12824        let options = CastOptions {
12825            safe: false,
12826            ..Default::default()
12827        };
12828        let result = cast_with_options(&array, &output_type, &options);
12829        assert_eq!(
12830            result.unwrap().as_primitive::<Decimal128Type>().value(0),
12831            520
12832        );
12833
12834        // Cast 0 of decimal(3, 0) type to decimal(2, 0)
12835        assert_eq!(
12836            &cast(
12837                &create_decimal128_array(vec![Some(0)], 3, 0).unwrap(),
12838                &DataType::Decimal128(2, 0)
12839            )
12840            .unwrap(),
12841            &(Arc::new(create_decimal128_array(vec![Some(0)], 2, 0).unwrap()) as ArrayRef)
12842        );
12843    }
12844
12845    #[test]
12846    fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale() {
12847        let array = vec![Some(123456789)];
12848        let array = create_decimal128_array(array, 24, 4).unwrap();
12849        let input_type = DataType::Decimal128(24, 4);
12850        let output_type = DataType::Decimal128(6, 2);
12851        assert!(can_cast_types(&input_type, &output_type));
12852
12853        let options = CastOptions {
12854            safe: false,
12855            ..Default::default()
12856        };
12857        let result = cast_with_options(&array, &output_type, &options);
12858        assert_eq!(
12859            result.unwrap_err().to_string(),
12860            "Invalid argument error: 12345.68 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
12861        );
12862    }
12863
12864    #[test]
12865    fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale() {
12866        let array = vec![Some(123456789)];
12867        let array = create_decimal128_array(array, 24, 2).unwrap();
12868        let input_type = DataType::Decimal128(24, 2);
12869        let output_type = DataType::Decimal128(6, 3);
12870        assert!(can_cast_types(&input_type, &output_type));
12871
12872        let options = CastOptions {
12873            safe: false,
12874            ..Default::default()
12875        };
12876        let result = cast_with_options(&array, &output_type, &options);
12877        assert_eq!(
12878            result.unwrap_err().to_string(),
12879            "Invalid argument error: 1234567.890 is too large to store in a Decimal128 of precision 6. Max is 999.999"
12880        );
12881    }
12882
12883    #[test]
12884    fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type() {
12885        let array = vec![Some(123456789)];
12886        let array = create_decimal128_array(array, 24, 2).unwrap();
12887        let input_type = DataType::Decimal128(24, 2);
12888        let output_type = DataType::Decimal256(6, 2);
12889        assert!(can_cast_types(&input_type, &output_type));
12890
12891        let options = CastOptions {
12892            safe: false,
12893            ..Default::default()
12894        };
12895        let result = cast_with_options(&array, &output_type, &options).unwrap_err();
12896        assert_eq!(
12897            result.to_string(),
12898            "Invalid argument error: 1234567.89 is too large to store in a Decimal256 of precision 6. Max is 9999.99"
12899        );
12900    }
12901
12902    #[test]
12903    fn test_first_none() {
12904        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
12905            None,
12906            Some(vec![Some(1), Some(2)]),
12907        ])) as ArrayRef;
12908        let data_type =
12909            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
12910        let opt = CastOptions::default();
12911        let r = cast_with_options(&array, &data_type, &opt).unwrap();
12912
12913        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
12914            vec![None, Some(vec![Some(1), Some(2)])],
12915            2,
12916        )) as ArrayRef;
12917        assert_eq!(*fixed_array, *r);
12918    }
12919
12920    #[test]
12921    fn test_first_last_none() {
12922        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
12923            None,
12924            Some(vec![Some(1), Some(2)]),
12925            None,
12926        ])) as ArrayRef;
12927        let data_type =
12928            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
12929        let opt = CastOptions::default();
12930        let r = cast_with_options(&array, &data_type, &opt).unwrap();
12931
12932        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
12933            vec![None, Some(vec![Some(1), Some(2)]), None],
12934            2,
12935        )) as ArrayRef;
12936        assert_eq!(*fixed_array, *r);
12937    }
12938
12939    #[test]
12940    fn test_cast_decimal_error_output() {
12941        let array = Int64Array::from(vec![1]);
12942        let error = cast_with_options(
12943            &array,
12944            &DataType::Decimal32(1, 1),
12945            &CastOptions {
12946                safe: false,
12947                format_options: FormatOptions::default(),
12948            },
12949        )
12950        .unwrap_err();
12951        assert_eq!(
12952            error.to_string(),
12953            "Invalid argument error: 1.0 is too large to store in a Decimal32 of precision 1. Max is 0.9"
12954        );
12955
12956        let array = Int64Array::from(vec![-1]);
12957        let error = cast_with_options(
12958            &array,
12959            &DataType::Decimal32(1, 1),
12960            &CastOptions {
12961                safe: false,
12962                format_options: FormatOptions::default(),
12963            },
12964        )
12965        .unwrap_err();
12966        assert_eq!(
12967            error.to_string(),
12968            "Invalid argument error: -1.0 is too small to store in a Decimal32 of precision 1. Min is -0.9"
12969        );
12970    }
12971
12972    #[test]
12973    fn test_run_end_encoded_to_primitive() {
12974        // Create a RunEndEncoded array: [1, 1, 2, 2, 2, 3]
12975        let run_ends = Int32Array::from(vec![2, 5, 6]);
12976        let values = Int32Array::from(vec![1, 2, 3]);
12977        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12978        let array_ref = Arc::new(run_array) as ArrayRef;
12979        // Cast to Int64
12980        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
12981        // Verify the result is a RunArray with Int64 values
12982        let result_run_array = cast_result.as_any().downcast_ref::<Int64Array>().unwrap();
12983        assert_eq!(
12984            result_run_array.values(),
12985            &[1i64, 1i64, 2i64, 2i64, 2i64, 3i64]
12986        );
12987    }
12988
12989    #[test]
12990    fn test_sliced_run_end_encoded_to_primitive() {
12991        let run_ends = Int32Array::from(vec![2, 5, 6]);
12992        let values = Int32Array::from(vec![1, 2, 3]);
12993        // [1, 1, 2, 2, 2, 3]
12994        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12995        let run_array = run_array.slice(3, 3); // [2, 2, 3]
12996        let array_ref = Arc::new(run_array) as ArrayRef;
12997
12998        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
12999        let result_run_array = cast_result.as_primitive::<Int64Type>();
13000        assert_eq!(result_run_array.values(), &[2, 2, 3]);
13001    }
13002
13003    #[test]
13004    fn test_run_end_encoded_to_string() {
13005        let run_ends = Int32Array::from(vec![2, 3, 5]);
13006        let values = Int32Array::from(vec![10, 20, 30]);
13007        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13008        let array_ref = Arc::new(run_array) as ArrayRef;
13009
13010        // Cast to String
13011        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
13012
13013        // Verify the result is a RunArray with String values
13014        let result_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
13015        // Check that values are correct
13016        assert_eq!(result_array.value(0), "10");
13017        assert_eq!(result_array.value(1), "10");
13018        assert_eq!(result_array.value(2), "20");
13019    }
13020
13021    #[test]
13022    fn test_primitive_to_run_end_encoded() {
13023        // Create an Int32 array with repeated values: [1, 1, 2, 2, 2, 3]
13024        let source_array = Int32Array::from(vec![1, 1, 2, 2, 2, 3]);
13025        let array_ref = Arc::new(source_array) as ArrayRef;
13026
13027        // Cast to RunEndEncoded<Int32, Int32>
13028        let target_type = DataType::RunEndEncoded(
13029            Arc::new(Field::new("run_ends", DataType::Int32, false)),
13030            Arc::new(Field::new("values", DataType::Int32, true)),
13031        );
13032        let cast_result = cast(&array_ref, &target_type).unwrap();
13033
13034        // Verify the result is a RunArray
13035        let result_run_array = cast_result
13036            .as_any()
13037            .downcast_ref::<RunArray<Int32Type>>()
13038            .unwrap();
13039
13040        // Check run structure: runs should end at positions [2, 5, 6]
13041        assert_eq!(result_run_array.run_ends().values(), &[2, 5, 6]);
13042
13043        // Check values: should be [1, 2, 3]
13044        let values_array = result_run_array.values().as_primitive::<Int32Type>();
13045        assert_eq!(values_array.values(), &[1, 2, 3]);
13046    }
13047
13048    #[test]
13049    fn test_primitive_to_run_end_encoded_with_nulls() {
13050        let source_array = Int32Array::from(vec![
13051            Some(1),
13052            Some(1),
13053            None,
13054            None,
13055            Some(2),
13056            Some(2),
13057            Some(3),
13058            Some(3),
13059            None,
13060            None,
13061            Some(4),
13062            Some(4),
13063            Some(5),
13064            Some(5),
13065            None,
13066            None,
13067        ]);
13068        let array_ref = Arc::new(source_array) as ArrayRef;
13069        let target_type = DataType::RunEndEncoded(
13070            Arc::new(Field::new("run_ends", DataType::Int32, false)),
13071            Arc::new(Field::new("values", DataType::Int32, true)),
13072        );
13073        let cast_result = cast(&array_ref, &target_type).unwrap();
13074        let result_run_array = cast_result
13075            .as_any()
13076            .downcast_ref::<RunArray<Int32Type>>()
13077            .unwrap();
13078        assert_eq!(
13079            result_run_array.run_ends().values(),
13080            &[2, 4, 6, 8, 10, 12, 14, 16]
13081        );
13082        assert_eq!(
13083            result_run_array
13084                .values()
13085                .as_primitive::<Int32Type>()
13086                .values(),
13087            &[1, 0, 2, 3, 0, 4, 5, 0]
13088        );
13089        assert_eq!(result_run_array.values().null_count(), 3);
13090    }
13091
13092    #[test]
13093    fn test_primitive_to_run_end_encoded_with_nulls_consecutive() {
13094        let source_array = Int64Array::from(vec![
13095            Some(1),
13096            Some(1),
13097            None,
13098            None,
13099            None,
13100            None,
13101            None,
13102            None,
13103            None,
13104            None,
13105            Some(4),
13106            Some(20),
13107            Some(500),
13108            Some(500),
13109            None,
13110            None,
13111        ]);
13112        let array_ref = Arc::new(source_array) as ArrayRef;
13113        let target_type = DataType::RunEndEncoded(
13114            Arc::new(Field::new("run_ends", DataType::Int16, false)),
13115            Arc::new(Field::new("values", DataType::Int64, true)),
13116        );
13117        let cast_result = cast(&array_ref, &target_type).unwrap();
13118        let result_run_array = cast_result
13119            .as_any()
13120            .downcast_ref::<RunArray<Int16Type>>()
13121            .unwrap();
13122        assert_eq!(
13123            result_run_array.run_ends().values(),
13124            &[2, 10, 11, 12, 14, 16]
13125        );
13126        assert_eq!(
13127            result_run_array
13128                .values()
13129                .as_primitive::<Int64Type>()
13130                .values(),
13131            &[1, 0, 4, 20, 500, 0]
13132        );
13133        assert_eq!(result_run_array.values().null_count(), 2);
13134    }
13135
13136    #[test]
13137    fn test_string_to_run_end_encoded() {
13138        // Create a String array with repeated values: ["a", "a", "b", "c", "c"]
13139        let source_array = StringArray::from(vec!["a", "a", "b", "c", "c"]);
13140        let array_ref = Arc::new(source_array) as ArrayRef;
13141
13142        // Cast to RunEndEncoded<Int32, String>
13143        let target_type = DataType::RunEndEncoded(
13144            Arc::new(Field::new("run_ends", DataType::Int32, false)),
13145            Arc::new(Field::new("values", DataType::Utf8, true)),
13146        );
13147        let cast_result = cast(&array_ref, &target_type).unwrap();
13148
13149        // Verify the result is a RunArray
13150        let result_run_array = cast_result
13151            .as_any()
13152            .downcast_ref::<RunArray<Int32Type>>()
13153            .unwrap();
13154
13155        // Check run structure: runs should end at positions [2, 3, 5]
13156        assert_eq!(result_run_array.run_ends().values(), &[2, 3, 5]);
13157
13158        // Check values: should be ["a", "b", "c"]
13159        let values_array = result_run_array.values().as_string::<i32>();
13160        assert_eq!(values_array.value(0), "a");
13161        assert_eq!(values_array.value(1), "b");
13162        assert_eq!(values_array.value(2), "c");
13163    }
13164
13165    #[test]
13166    fn test_empty_array_to_run_end_encoded() {
13167        // Create an empty Int32 array
13168        let source_array = Int32Array::from(Vec::<i32>::new());
13169        let array_ref = Arc::new(source_array) as ArrayRef;
13170
13171        // Cast to RunEndEncoded<Int32, Int32>
13172        let target_type = DataType::RunEndEncoded(
13173            Arc::new(Field::new("run_ends", DataType::Int32, false)),
13174            Arc::new(Field::new("values", DataType::Int32, true)),
13175        );
13176        let cast_result = cast(&array_ref, &target_type).unwrap();
13177
13178        // Verify the result is an empty RunArray
13179        let result_run_array = cast_result
13180            .as_any()
13181            .downcast_ref::<RunArray<Int32Type>>()
13182            .unwrap();
13183
13184        // Check that both run_ends and values are empty
13185        assert_eq!(result_run_array.run_ends().len(), 0);
13186        assert_eq!(result_run_array.values().len(), 0);
13187    }
13188
13189    #[test]
13190    fn test_run_end_encoded_with_nulls() {
13191        // Create a RunEndEncoded array with nulls: [1, 1, null, 2, 2]
13192        let run_ends = Int32Array::from(vec![2, 3, 5]);
13193        let values = Int32Array::from(vec![Some(1), None, Some(2)]);
13194        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13195        let array_ref = Arc::new(run_array) as ArrayRef;
13196
13197        // Cast to String
13198        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
13199
13200        // Verify the result preserves nulls
13201        let result_run_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
13202        assert_eq!(result_run_array.value(0), "1");
13203        assert!(result_run_array.is_null(2));
13204        assert_eq!(result_run_array.value(4), "2");
13205    }
13206
13207    #[test]
13208    fn test_different_index_types() {
13209        // Test with Int16 index type
13210        let source_array = Int32Array::from(vec![1, 1, 2, 3, 3]);
13211        let array_ref = Arc::new(source_array) as ArrayRef;
13212
13213        let target_type = DataType::RunEndEncoded(
13214            Arc::new(Field::new("run_ends", DataType::Int16, false)),
13215            Arc::new(Field::new("values", DataType::Int32, true)),
13216        );
13217        let cast_result = cast(&array_ref, &target_type).unwrap();
13218        assert_eq!(cast_result.data_type(), &target_type);
13219
13220        // Verify the cast worked correctly: values are [1, 2, 3]
13221        // and run-ends are [2, 3, 5]
13222        let run_array = cast_result
13223            .as_any()
13224            .downcast_ref::<RunArray<Int16Type>>()
13225            .unwrap();
13226        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
13227        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
13228        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
13229        assert_eq!(run_array.run_ends().values(), &[2i16, 3i16, 5i16]);
13230
13231        // Test again with Int64 index type
13232        let target_type = DataType::RunEndEncoded(
13233            Arc::new(Field::new("run_ends", DataType::Int64, false)),
13234            Arc::new(Field::new("values", DataType::Int32, true)),
13235        );
13236        let cast_result = cast(&array_ref, &target_type).unwrap();
13237        assert_eq!(cast_result.data_type(), &target_type);
13238
13239        // Verify the cast worked correctly: values are [1, 2, 3]
13240        // and run-ends are [2, 3, 5]
13241        let run_array = cast_result
13242            .as_any()
13243            .downcast_ref::<RunArray<Int64Type>>()
13244            .unwrap();
13245        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
13246        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
13247        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
13248        assert_eq!(run_array.run_ends().values(), &[2i64, 3i64, 5i64]);
13249    }
13250
13251    #[test]
13252    fn test_unsupported_cast_to_run_end_encoded() {
13253        // Create a Struct array - complex nested type that might not be supported
13254        let field = Field::new("item", DataType::Int32, false);
13255        let struct_array = StructArray::from(vec![(
13256            Arc::new(field),
13257            Arc::new(Int32Array::from(vec![1, 2, 3])) as ArrayRef,
13258        )]);
13259        let array_ref = Arc::new(struct_array) as ArrayRef;
13260
13261        // This should fail because:
13262        // 1. The target type is not RunEndEncoded
13263        // 2. The target type is not supported for casting from StructArray
13264        let cast_result = cast(&array_ref, &DataType::FixedSizeBinary(10));
13265
13266        // Expect this to fail
13267        assert!(cast_result.is_err());
13268    }
13269
13270    /// Test casting RunEndEncoded<Int64, String> to RunEndEncoded<Int16, String> should fail
13271    #[test]
13272    fn test_cast_run_end_encoded_int64_to_int16_should_fail() {
13273        // Construct a valid REE array with Int64 run-ends
13274        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
13275        let values = StringArray::from(vec!["a", "b", "c"]);
13276
13277        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
13278        let array_ref = Arc::new(ree_array) as ArrayRef;
13279
13280        // Attempt to cast to RunEndEncoded<Int16, Utf8>
13281        let target_type = DataType::RunEndEncoded(
13282            Arc::new(Field::new("run_ends", DataType::Int16, false)),
13283            Arc::new(Field::new("values", DataType::Utf8, true)),
13284        );
13285        let cast_options = CastOptions {
13286            safe: false, // This should make it fail instead of returning nulls
13287            format_options: FormatOptions::default(),
13288        };
13289
13290        // This should fail due to run-end overflow
13291        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
13292            cast_with_options(&array_ref, &target_type, &cast_options);
13293
13294        let e = result.expect_err("Cast should have failed but succeeded");
13295        assert!(
13296            e.to_string()
13297                .contains("Cast error: Can't cast value 100000 to type Int16")
13298        );
13299    }
13300
13301    #[test]
13302    fn test_cast_run_end_encoded_int64_to_int16_with_safe_should_fail_with_null_invalid_error() {
13303        // Construct a valid REE array with Int64 run-ends
13304        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
13305        let values = StringArray::from(vec!["a", "b", "c"]);
13306
13307        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
13308        let array_ref = Arc::new(ree_array) as ArrayRef;
13309
13310        // Attempt to cast to RunEndEncoded<Int16, Utf8>
13311        let target_type = DataType::RunEndEncoded(
13312            Arc::new(Field::new("run_ends", DataType::Int16, false)),
13313            Arc::new(Field::new("values", DataType::Utf8, true)),
13314        );
13315        let cast_options = CastOptions {
13316            safe: true,
13317            format_options: FormatOptions::default(),
13318        };
13319
13320        // This fails even though safe is true because the run_ends array has null values
13321        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
13322            cast_with_options(&array_ref, &target_type, &cast_options);
13323        let e = result.expect_err("Cast should have failed but succeeded");
13324        assert!(
13325            e.to_string()
13326                .contains("Invalid argument error: Found null values in run_ends array. The run_ends array should not have null values.")
13327        );
13328    }
13329
13330    /// Test casting RunEndEncoded<Int16, String> to RunEndEncoded<Int64, String> should succeed
13331    #[test]
13332    fn test_cast_run_end_encoded_int16_to_int64_should_succeed() {
13333        // Construct a valid REE array with Int16 run-ends
13334        let run_ends = Int16Array::from(vec![2, 5, 8]); // values that fit in Int16
13335        let values = StringArray::from(vec!["a", "b", "c"]);
13336
13337        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
13338        let array_ref = Arc::new(ree_array) as ArrayRef;
13339
13340        // Attempt to cast to RunEndEncoded<Int64, Utf8> (upcast should succeed)
13341        let target_type = DataType::RunEndEncoded(
13342            Arc::new(Field::new("run_ends", DataType::Int64, false)),
13343            Arc::new(Field::new("values", DataType::Utf8, true)),
13344        );
13345        let cast_options = CastOptions {
13346            safe: false,
13347            format_options: FormatOptions::default(),
13348        };
13349
13350        // This should succeed due to valid upcast
13351        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
13352            cast_with_options(&array_ref, &target_type, &cast_options);
13353
13354        let array_ref = result.expect("Cast should have succeeded but failed");
13355        // Downcast to RunArray<Int64Type>
13356        let run_array = array_ref
13357            .as_any()
13358            .downcast_ref::<RunArray<Int64Type>>()
13359            .unwrap();
13360
13361        // Verify the cast worked correctly
13362        // Assert the values were cast correctly
13363        assert_eq!(run_array.run_ends().values(), &[2i64, 5i64, 8i64]);
13364        assert_eq!(run_array.values().as_string::<i32>().value(0), "a");
13365        assert_eq!(run_array.values().as_string::<i32>().value(1), "b");
13366        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
13367    }
13368
13369    #[test]
13370    fn test_cast_run_end_encoded_dictionary_to_run_end_encoded() {
13371        // Construct a valid dictionary encoded array
13372        let values = StringArray::from_iter([Some("a"), Some("b"), Some("c")]);
13373        let keys = UInt64Array::from_iter(vec![1, 1, 1, 0, 0, 0, 2, 2, 2]);
13374        let array_ref = Arc::new(DictionaryArray::new(keys, Arc::new(values))) as ArrayRef;
13375
13376        // Attempt to cast to RunEndEncoded<Int64, Utf8>
13377        let target_type = DataType::RunEndEncoded(
13378            Arc::new(Field::new("run_ends", DataType::Int64, false)),
13379            Arc::new(Field::new("values", DataType::Utf8, true)),
13380        );
13381        let cast_options = CastOptions {
13382            safe: false,
13383            format_options: FormatOptions::default(),
13384        };
13385
13386        // This should succeed
13387        let result = cast_with_options(&array_ref, &target_type, &cast_options)
13388            .expect("Cast should have succeeded but failed");
13389
13390        // Verify the cast worked correctly
13391        // Assert the values were cast correctly
13392        let run_array = result
13393            .as_any()
13394            .downcast_ref::<RunArray<Int64Type>>()
13395            .unwrap();
13396        assert_eq!(run_array.values().as_string::<i32>().value(0), "b");
13397        assert_eq!(run_array.values().as_string::<i32>().value(1), "a");
13398        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
13399
13400        // Verify the run-ends were cast correctly (run ends at 3, 6, 9)
13401        assert_eq!(run_array.run_ends().values(), &[3i64, 6i64, 9i64]);
13402    }
13403
13404    fn int32_list_values() -> Vec<Option<Vec<Option<i32>>>> {
13405        vec![
13406            Some(vec![Some(1), Some(2), Some(3)]),
13407            Some(vec![Some(4), Some(5), Some(6)]),
13408            None,
13409            Some(vec![Some(7), Some(8), Some(9)]),
13410            Some(vec![None, Some(10)]),
13411        ]
13412    }
13413
13414    #[test]
13415    fn test_cast_list_view_to_list() {
13416        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13417        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13418        assert!(can_cast_types(list_view.data_type(), &target_type));
13419        let cast_result = cast(&list_view, &target_type).unwrap();
13420        let got_list = cast_result.as_list::<i32>();
13421        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13422        assert_eq!(got_list, &expected_list);
13423    }
13424
13425    #[test]
13426    fn test_cast_list_view_to_large_list() {
13427        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13428        let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
13429        assert!(can_cast_types(list_view.data_type(), &target_type));
13430        let cast_result = cast(&list_view, &target_type).unwrap();
13431        let got_list = cast_result.as_list::<i64>();
13432        let expected_list =
13433            LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13434        assert_eq!(got_list, &expected_list);
13435    }
13436
13437    #[test]
13438    fn test_cast_list_to_list_view() {
13439        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13440        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
13441        assert!(can_cast_types(list.data_type(), &target_type));
13442        let cast_result = cast(&list, &target_type).unwrap();
13443
13444        let got_list_view = cast_result.as_list_view::<i32>();
13445        let expected_list_view =
13446            ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13447        assert_eq!(got_list_view, &expected_list_view);
13448
13449        // inner types get cast
13450        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13451            Some(vec![Some(1), Some(2)]),
13452            None,
13453            Some(vec![None, Some(3)]),
13454        ]);
13455        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Float32, true)));
13456        assert!(can_cast_types(list.data_type(), &target_type));
13457        let cast_result = cast(&list, &target_type).unwrap();
13458
13459        let got_list_view = cast_result.as_list_view::<i32>();
13460        let expected_list_view = ListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13461            Some(vec![Some(1.0), Some(2.0)]),
13462            None,
13463            Some(vec![None, Some(3.0)]),
13464        ]);
13465        assert_eq!(got_list_view, &expected_list_view);
13466    }
13467
13468    #[test]
13469    fn test_cast_list_to_large_list_view() {
13470        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13471            Some(vec![Some(1), Some(2)]),
13472            None,
13473            Some(vec![None, Some(3)]),
13474        ]);
13475        let target_type =
13476            DataType::LargeListView(Arc::new(Field::new("item", DataType::Float32, true)));
13477        assert!(can_cast_types(list.data_type(), &target_type));
13478        let cast_result = cast(&list, &target_type).unwrap();
13479
13480        let got_list_view = cast_result.as_list_view::<i64>();
13481        let expected_list_view =
13482            LargeListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13483                Some(vec![Some(1.0), Some(2.0)]),
13484                None,
13485                Some(vec![None, Some(3.0)]),
13486            ]);
13487        assert_eq!(got_list_view, &expected_list_view);
13488    }
13489
13490    #[test]
13491    fn test_cast_large_list_view_to_large_list() {
13492        let list_view =
13493            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13494        let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
13495        assert!(can_cast_types(list_view.data_type(), &target_type));
13496        let cast_result = cast(&list_view, &target_type).unwrap();
13497        let got_list = cast_result.as_list::<i64>();
13498
13499        let expected_list =
13500            LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13501        assert_eq!(got_list, &expected_list);
13502    }
13503
13504    #[test]
13505    fn test_cast_large_list_view_to_list() {
13506        let list_view =
13507            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13508        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13509        assert!(can_cast_types(list_view.data_type(), &target_type));
13510        let cast_result = cast(&list_view, &target_type).unwrap();
13511        let got_list = cast_result.as_list::<i32>();
13512
13513        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13514        assert_eq!(got_list, &expected_list);
13515    }
13516
13517    #[test]
13518    fn test_cast_large_list_to_large_list_view() {
13519        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13520        let target_type =
13521            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
13522        assert!(can_cast_types(list.data_type(), &target_type));
13523        let cast_result = cast(&list, &target_type).unwrap();
13524
13525        let got_list_view = cast_result.as_list_view::<i64>();
13526        let expected_list_view =
13527            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13528        assert_eq!(got_list_view, &expected_list_view);
13529
13530        // inner types get cast
13531        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13532            Some(vec![Some(1), Some(2)]),
13533            None,
13534            Some(vec![None, Some(3)]),
13535        ]);
13536        let target_type =
13537            DataType::LargeListView(Arc::new(Field::new("item", DataType::Float32, true)));
13538        assert!(can_cast_types(list.data_type(), &target_type));
13539        let cast_result = cast(&list, &target_type).unwrap();
13540
13541        let got_list_view = cast_result.as_list_view::<i64>();
13542        let expected_list_view =
13543            LargeListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13544                Some(vec![Some(1.0), Some(2.0)]),
13545                None,
13546                Some(vec![None, Some(3.0)]),
13547            ]);
13548        assert_eq!(got_list_view, &expected_list_view);
13549    }
13550
13551    #[test]
13552    fn test_cast_large_list_to_list_view() {
13553        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13554            Some(vec![Some(1), Some(2)]),
13555            None,
13556            Some(vec![None, Some(3)]),
13557        ]);
13558        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Float32, true)));
13559        assert!(can_cast_types(list.data_type(), &target_type));
13560        let cast_result = cast(&list, &target_type).unwrap();
13561
13562        let got_list_view = cast_result.as_list_view::<i32>();
13563        let expected_list_view = ListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13564            Some(vec![Some(1.0), Some(2.0)]),
13565            None,
13566            Some(vec![None, Some(3.0)]),
13567        ]);
13568        assert_eq!(got_list_view, &expected_list_view);
13569    }
13570
13571    #[test]
13572    fn test_cast_list_view_to_list_out_of_order() {
13573        let list_view = ListViewArray::new(
13574            Arc::new(Field::new("item", DataType::Int32, true)),
13575            ScalarBuffer::from(vec![0, 6, 3]),
13576            ScalarBuffer::from(vec![3, 3, 3]),
13577            Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
13578            None,
13579        );
13580        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13581        assert!(can_cast_types(list_view.data_type(), &target_type));
13582        let cast_result = cast(&list_view, &target_type).unwrap();
13583        let got_list = cast_result.as_list::<i32>();
13584        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13585            Some(vec![Some(1), Some(2), Some(3)]),
13586            Some(vec![Some(7), Some(8), Some(9)]),
13587            Some(vec![Some(4), Some(5), Some(6)]),
13588        ]);
13589        assert_eq!(got_list, &expected_list);
13590    }
13591
13592    #[test]
13593    fn test_cast_list_view_to_list_overlapping() {
13594        let list_view = ListViewArray::new(
13595            Arc::new(Field::new("item", DataType::Int32, true)),
13596            ScalarBuffer::from(vec![0, 0]),
13597            ScalarBuffer::from(vec![1, 2]),
13598            Arc::new(Int32Array::from(vec![1, 2])),
13599            None,
13600        );
13601        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13602        assert!(can_cast_types(list_view.data_type(), &target_type));
13603        let cast_result = cast(&list_view, &target_type).unwrap();
13604        let got_list = cast_result.as_list::<i32>();
13605        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13606            Some(vec![Some(1)]),
13607            Some(vec![Some(1), Some(2)]),
13608        ]);
13609        assert_eq!(got_list, &expected_list);
13610    }
13611
13612    #[test]
13613    fn test_cast_list_view_to_list_empty() {
13614        let values: Vec<Option<Vec<Option<i32>>>> = vec![];
13615        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
13616        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13617        assert!(can_cast_types(list_view.data_type(), &target_type));
13618        let cast_result = cast(&list_view, &target_type).unwrap();
13619        let got_list = cast_result.as_list::<i32>();
13620        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
13621        assert_eq!(got_list, &expected_list);
13622    }
13623
13624    #[test]
13625    fn test_cast_list_view_to_list_different_inner_type() {
13626        let values = int32_list_values();
13627        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
13628        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int64, true)));
13629        assert!(can_cast_types(list_view.data_type(), &target_type));
13630        let cast_result = cast(&list_view, &target_type).unwrap();
13631        let got_list = cast_result.as_list::<i32>();
13632
13633        let expected_list =
13634            ListArray::from_iter_primitive::<Int64Type, _, _>(values.into_iter().map(|list| {
13635                list.map(|list| {
13636                    list.into_iter()
13637                        .map(|v| v.map(|v| v as i64))
13638                        .collect::<Vec<_>>()
13639                })
13640            }));
13641        assert_eq!(got_list, &expected_list);
13642    }
13643
13644    #[test]
13645    fn test_cast_list_view_to_list_out_of_order_with_nulls() {
13646        let list_view = ListViewArray::new(
13647            Arc::new(Field::new("item", DataType::Int32, true)),
13648            ScalarBuffer::from(vec![0, 6, 3]),
13649            ScalarBuffer::from(vec![3, 3, 3]),
13650            Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
13651            Some(NullBuffer::from(vec![false, true, false])),
13652        );
13653        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13654        assert!(can_cast_types(list_view.data_type(), &target_type));
13655        let cast_result = cast(&list_view, &target_type).unwrap();
13656        let got_list = cast_result.as_list::<i32>();
13657        let expected_list = ListArray::new(
13658            Arc::new(Field::new("item", DataType::Int32, true)),
13659            OffsetBuffer::from_lengths([3, 3, 3]),
13660            Arc::new(Int32Array::from(vec![1, 2, 3, 7, 8, 9, 4, 5, 6])),
13661            Some(NullBuffer::from(vec![false, true, false])),
13662        );
13663        assert_eq!(got_list, &expected_list);
13664    }
13665
13666    #[test]
13667    fn test_cast_list_view_to_large_list_view() {
13668        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13669        let target_type =
13670            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
13671        assert!(can_cast_types(list_view.data_type(), &target_type));
13672        let cast_result = cast(&list_view, &target_type).unwrap();
13673        let got = cast_result.as_list_view::<i64>();
13674
13675        let expected =
13676            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13677        assert_eq!(got, &expected);
13678    }
13679
13680    #[test]
13681    fn test_cast_large_list_view_to_list_view() {
13682        let list_view =
13683            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13684        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
13685        assert!(can_cast_types(list_view.data_type(), &target_type));
13686        let cast_result = cast(&list_view, &target_type).unwrap();
13687        let got = cast_result.as_list_view::<i32>();
13688
13689        let expected = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13690        assert_eq!(got, &expected);
13691    }
13692
13693    #[test]
13694    fn test_cast_time32_second_to_int64() {
13695        let array = Time32SecondArray::from(vec![1000, 2000, 3000]);
13696        let array = Arc::new(array) as Arc<dyn Array>;
13697        let to_type = DataType::Int64;
13698        let cast_options = CastOptions::default();
13699
13700        assert!(can_cast_types(array.data_type(), &to_type));
13701
13702        let result = cast_with_options(&array, &to_type, &cast_options);
13703        assert!(
13704            result.is_ok(),
13705            "Failed to cast Time32(Second) to Int64: {:?}",
13706            result.err()
13707        );
13708
13709        let cast_array = result.unwrap();
13710        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13711
13712        assert_eq!(cast_array.value(0), 1000);
13713        assert_eq!(cast_array.value(1), 2000);
13714        assert_eq!(cast_array.value(2), 3000);
13715    }
13716
13717    #[test]
13718    fn test_cast_time32_millisecond_to_int64() {
13719        let array = Time32MillisecondArray::from(vec![1000, 2000, 3000]);
13720        let array = Arc::new(array) as Arc<dyn Array>;
13721        let to_type = DataType::Int64;
13722        let cast_options = CastOptions::default();
13723
13724        assert!(can_cast_types(array.data_type(), &to_type));
13725
13726        let result = cast_with_options(&array, &to_type, &cast_options);
13727        assert!(
13728            result.is_ok(),
13729            "Failed to cast Time32(Millisecond) to Int64: {:?}",
13730            result.err()
13731        );
13732
13733        let cast_array = result.unwrap();
13734        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13735
13736        assert_eq!(cast_array.value(0), 1000);
13737        assert_eq!(cast_array.value(1), 2000);
13738        assert_eq!(cast_array.value(2), 3000);
13739    }
13740
13741    #[test]
13742    fn test_cast_time32_millisecond_to_time64_nanosecond() {
13743        let array =
13744            Time32MillisecondArray::from(vec![Some(1_000), Some(2_000), None, Some(43_200_000)]);
13745        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
13746        let c = b.as_primitive::<Time64NanosecondType>();
13747        assert_eq!(c.value(0), 1_000_000_000);
13748        assert_eq!(c.value(1), 2_000_000_000);
13749        assert!(c.is_null(2));
13750        assert_eq!(c.value(3), 43_200_000_000_000);
13751    }
13752
13753    #[test]
13754    fn test_cast_time32_millisecond_to_time64_microsecond() {
13755        let array =
13756            Time32MillisecondArray::from(vec![Some(1_000), Some(2_000), None, Some(43_200_000)]);
13757        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
13758        let c = b.as_primitive::<Time64MicrosecondType>();
13759        assert_eq!(c.value(0), 1_000_000);
13760        assert_eq!(c.value(1), 2_000_000);
13761        assert!(c.is_null(2));
13762        assert_eq!(c.value(3), 43_200_000_000);
13763    }
13764
13765    #[test]
13766    fn test_cast_time32_second_to_time64_nanosecond() {
13767        let array = Time32SecondArray::from(vec![Some(1), Some(60), None, Some(43_200)]);
13768        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
13769        let c = b.as_primitive::<Time64NanosecondType>();
13770        assert_eq!(c.value(0), 1_000_000_000);
13771        assert_eq!(c.value(1), 60_000_000_000);
13772        assert!(c.is_null(2));
13773        assert_eq!(c.value(3), 43_200_000_000_000);
13774    }
13775
13776    #[test]
13777    fn test_cast_time32_second_to_time64_microsecond() {
13778        let array = Time32SecondArray::from(vec![Some(1), Some(60), None, Some(43_200)]);
13779        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
13780        let c = b.as_primitive::<Time64MicrosecondType>();
13781        assert_eq!(c.value(0), 1_000_000);
13782        assert_eq!(c.value(1), 60_000_000);
13783        assert!(c.is_null(2));
13784        assert_eq!(c.value(3), 43_200_000_000);
13785    }
13786
13787    #[test]
13788    fn test_cast_string_to_time32_second_to_int64() {
13789        // Mimic: select arrow_cast('03:12:44'::time, 'Time32(Second)')::bigint;
13790        // raised in https://github.com/apache/datafusion/issues/19036
13791        let array = StringArray::from(vec!["03:12:44"]);
13792        let array = Arc::new(array) as Arc<dyn Array>;
13793        let cast_options = CastOptions::default();
13794
13795        // 1. Cast String to Time32(Second)
13796        let time32_type = DataType::Time32(TimeUnit::Second);
13797        let time32_array = cast_with_options(&array, &time32_type, &cast_options).unwrap();
13798
13799        // 2. Cast Time32(Second) to Int64
13800        let int64_type = DataType::Int64;
13801        assert!(can_cast_types(time32_array.data_type(), &int64_type));
13802
13803        let result = cast_with_options(&time32_array, &int64_type, &cast_options);
13804
13805        assert!(
13806            result.is_ok(),
13807            "Failed to cast Time32(Second) to Int64: {:?}",
13808            result.err()
13809        );
13810
13811        let cast_array = result.unwrap();
13812        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13813
13814        // 03:12:44 = 3*3600 + 12*60 + 44 = 10800 + 720 + 44 = 11564
13815        assert_eq!(cast_array.value(0), 11564);
13816    }
13817    #[test]
13818    fn test_string_dicts_to_binary_view() {
13819        let expected = BinaryViewArray::from_iter(vec![
13820            VIEW_TEST_DATA[1],
13821            VIEW_TEST_DATA[0],
13822            None,
13823            VIEW_TEST_DATA[3],
13824            None,
13825            VIEW_TEST_DATA[1],
13826            VIEW_TEST_DATA[4],
13827        ]);
13828
13829        let values_arrays: [ArrayRef; _] = [
13830            Arc::new(StringArray::from_iter(VIEW_TEST_DATA)),
13831            Arc::new(StringViewArray::from_iter(VIEW_TEST_DATA)),
13832            Arc::new(LargeStringArray::from_iter(VIEW_TEST_DATA)),
13833        ];
13834        for values in values_arrays {
13835            let keys =
13836                Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
13837            let string_dict_array = DictionaryArray::<Int8Type>::try_new(keys, values).unwrap();
13838
13839            let casted = cast(&string_dict_array, &DataType::BinaryView).unwrap();
13840            assert_eq!(casted.as_ref(), &expected);
13841        }
13842    }
13843
13844    #[test]
13845    fn test_binary_dicts_to_string_view() {
13846        let expected = StringViewArray::from_iter(vec![
13847            VIEW_TEST_DATA[1],
13848            VIEW_TEST_DATA[0],
13849            None,
13850            VIEW_TEST_DATA[3],
13851            None,
13852            VIEW_TEST_DATA[1],
13853            VIEW_TEST_DATA[4],
13854        ]);
13855
13856        let values_arrays: [ArrayRef; _] = [
13857            Arc::new(BinaryArray::from_iter(VIEW_TEST_DATA)),
13858            Arc::new(BinaryViewArray::from_iter(VIEW_TEST_DATA)),
13859            Arc::new(LargeBinaryArray::from_iter(VIEW_TEST_DATA)),
13860        ];
13861        for values in values_arrays {
13862            let keys =
13863                Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
13864            let string_dict_array = DictionaryArray::<Int8Type>::try_new(keys, values).unwrap();
13865
13866            let casted = cast(&string_dict_array, &DataType::Utf8View).unwrap();
13867            assert_eq!(casted.as_ref(), &expected);
13868        }
13869    }
13870
13871    #[test]
13872    fn test_cast_between_sliced_run_end_encoded() {
13873        let run_ends = Int16Array::from(vec![2, 5, 8]);
13874        let values = StringArray::from(vec!["a", "b", "c"]);
13875
13876        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
13877        let ree_array = ree_array.slice(1, 2);
13878        let array_ref = Arc::new(ree_array) as ArrayRef;
13879
13880        let target_type = DataType::RunEndEncoded(
13881            Arc::new(Field::new("run_ends", DataType::Int64, false)),
13882            Arc::new(Field::new("values", DataType::Utf8, true)),
13883        );
13884        let cast_options = CastOptions {
13885            safe: false,
13886            format_options: FormatOptions::default(),
13887        };
13888
13889        let result = cast_with_options(&array_ref, &target_type, &cast_options).unwrap();
13890        let run_array = result.as_run::<Int64Type>();
13891        let run_array = run_array.downcast::<StringArray>().unwrap();
13892
13893        let expected = vec!["a", "b"];
13894        let actual = run_array.into_iter().flatten().collect::<Vec<_>>();
13895
13896        assert_eq!(expected, actual);
13897    }
13898}