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) => {
1714            let array = array.as_primitive::<Date64Type>();
1715            let result = if cast_options.safe {
1716                array.unary_opt::<_, Date32Type>(|x| i32::try_from(x / MILLISECONDS_IN_DAY).ok())
1717            } else {
1718                array.try_unary::<_, Date32Type, _>(|x| {
1719                    i32::try_from(x / MILLISECONDS_IN_DAY).map_err(|_| {
1720                        ArrowError::CastError(format!(
1721                            "Cannot cast Date64 value {x} to Date32 without overflow"
1722                        ))
1723                    })
1724                })?
1725            };
1726            Ok(Arc::new(result))
1727        }
1728
1729        (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => {
1730            let array = array.as_primitive::<Time32SecondType>();
1731            let result = if cast_options.safe {
1732                array.unary_opt::<_, Time32MillisecondType>(|x| x.checked_mul(MILLISECONDS as i32))
1733            } else {
1734                array.try_unary::<_, Time32MillisecondType, _>(|x| {
1735                    x.mul_checked(MILLISECONDS as i32)
1736                })?
1737            };
1738            Ok(Arc::new(result))
1739        }
1740        (Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1741            array
1742                .as_primitive::<Time32SecondType>()
1743                .unary::<_, Time64MicrosecondType>(|x| x as i64 * MICROSECONDS),
1744        )),
1745        (Time32(TimeUnit::Second), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1746            array
1747                .as_primitive::<Time32SecondType>()
1748                .unary::<_, Time64NanosecondType>(|x| x as i64 * NANOSECONDS),
1749        )),
1750
1751        (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1752            array
1753                .as_primitive::<Time32MillisecondType>()
1754                .unary::<_, Time32SecondType>(|x| x / MILLISECONDS as i32),
1755        )),
1756        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1757            array
1758                .as_primitive::<Time32MillisecondType>()
1759                .unary::<_, Time64MicrosecondType>(|x| x as i64 * (MICROSECONDS / MILLISECONDS)),
1760        )),
1761        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1762            array
1763                .as_primitive::<Time32MillisecondType>()
1764                .unary::<_, Time64NanosecondType>(|x| x as i64 * (NANOSECONDS / MILLISECONDS)),
1765        )),
1766
1767        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1768            array
1769                .as_primitive::<Time64MicrosecondType>()
1770                .unary::<_, Time32SecondType>(|x| (x / MICROSECONDS) as i32),
1771        )),
1772        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1773            array
1774                .as_primitive::<Time64MicrosecondType>()
1775                .unary::<_, Time32MillisecondType>(|x| (x / (MICROSECONDS / MILLISECONDS)) as i32),
1776        )),
1777        (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1778            array
1779                .as_primitive::<Time64MicrosecondType>()
1780                .unary::<_, Time64NanosecondType>(|x| x * (NANOSECONDS / MICROSECONDS)),
1781        )),
1782
1783        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1784            array
1785                .as_primitive::<Time64NanosecondType>()
1786                .unary::<_, Time32SecondType>(|x| (x / NANOSECONDS) as i32),
1787        )),
1788        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1789            array
1790                .as_primitive::<Time64NanosecondType>()
1791                .unary::<_, Time32MillisecondType>(|x| (x / (NANOSECONDS / MILLISECONDS)) as i32),
1792        )),
1793        (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1794            array
1795                .as_primitive::<Time64NanosecondType>()
1796                .unary::<_, Time64MicrosecondType>(|x| x / (NANOSECONDS / MICROSECONDS)),
1797        )),
1798
1799        // Timestamp to integer/floating/decimals
1800        (Timestamp(TimeUnit::Second, _), _) if to_type.is_numeric() => {
1801            let array = cast_reinterpret_arrays::<TimestampSecondType, Int64Type>(array)?;
1802            cast_with_options(&array, to_type, cast_options)
1803        }
1804        (Timestamp(TimeUnit::Millisecond, _), _) if to_type.is_numeric() => {
1805            let array = cast_reinterpret_arrays::<TimestampMillisecondType, Int64Type>(array)?;
1806            cast_with_options(&array, to_type, cast_options)
1807        }
1808        (Timestamp(TimeUnit::Microsecond, _), _) if to_type.is_numeric() => {
1809            let array = cast_reinterpret_arrays::<TimestampMicrosecondType, Int64Type>(array)?;
1810            cast_with_options(&array, to_type, cast_options)
1811        }
1812        (Timestamp(TimeUnit::Nanosecond, _), _) if to_type.is_numeric() => {
1813            let array = cast_reinterpret_arrays::<TimestampNanosecondType, Int64Type>(array)?;
1814            cast_with_options(&array, to_type, cast_options)
1815        }
1816
1817        (_, Timestamp(unit, tz)) if from_type.is_numeric() => {
1818            let array = cast_with_options(array, &Int64, cast_options)?;
1819            Ok(make_timestamp_array(
1820                array.as_primitive(),
1821                *unit,
1822                tz.clone(),
1823            ))
1824        }
1825
1826        (Timestamp(from_unit, from_tz), Timestamp(to_unit, to_tz)) => {
1827            let array = cast_with_options(array, &Int64, cast_options)?;
1828            let time_array = array.as_primitive::<Int64Type>();
1829            let from_size = time_unit_multiple(from_unit);
1830            let to_size = time_unit_multiple(to_unit);
1831            // we either divide or multiply, depending on size of each unit
1832            // units are never the same when the types are the same
1833            let converted = match from_size.cmp(&to_size) {
1834                Ordering::Greater => {
1835                    let divisor = from_size / to_size;
1836                    time_array.unary::<_, Int64Type>(|o| o / divisor)
1837                }
1838                Ordering::Equal => time_array.clone(),
1839                Ordering::Less => {
1840                    let mul = to_size / from_size;
1841                    if cast_options.safe {
1842                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1843                    } else {
1844                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1845                    }
1846                }
1847            };
1848            // Normalize timezone
1849            let adjusted = match (from_tz, to_tz) {
1850                // Only this case needs to be adjusted because we're casting from
1851                // unknown time offset to some time offset, we want the time to be
1852                // unchanged.
1853                //
1854                // i.e. Timestamp('2001-01-01T00:00', None) -> Timestamp('2001-01-01T00:00', '+0700')
1855                (None, Some(to_tz)) => {
1856                    let to_tz: Tz = to_tz.parse()?;
1857                    match to_unit {
1858                        TimeUnit::Second => adjust_timestamp_to_timezone::<TimestampSecondType>(
1859                            converted,
1860                            &to_tz,
1861                            cast_options,
1862                        )?,
1863                        TimeUnit::Millisecond => adjust_timestamp_to_timezone::<
1864                            TimestampMillisecondType,
1865                        >(
1866                            converted, &to_tz, cast_options
1867                        )?,
1868                        TimeUnit::Microsecond => adjust_timestamp_to_timezone::<
1869                            TimestampMicrosecondType,
1870                        >(
1871                            converted, &to_tz, cast_options
1872                        )?,
1873                        TimeUnit::Nanosecond => adjust_timestamp_to_timezone::<
1874                            TimestampNanosecondType,
1875                        >(
1876                            converted, &to_tz, cast_options
1877                        )?,
1878                    }
1879                }
1880                _ => converted,
1881            };
1882            Ok(make_timestamp_array(&adjusted, *to_unit, to_tz.clone()))
1883        }
1884        (Timestamp(TimeUnit::Microsecond, _), Date32) => {
1885            timestamp_to_date32(array.as_primitive::<TimestampMicrosecondType>())
1886        }
1887        (Timestamp(TimeUnit::Millisecond, _), Date32) => {
1888            timestamp_to_date32(array.as_primitive::<TimestampMillisecondType>())
1889        }
1890        (Timestamp(TimeUnit::Second, _), Date32) => {
1891            timestamp_to_date32(array.as_primitive::<TimestampSecondType>())
1892        }
1893        (Timestamp(TimeUnit::Nanosecond, _), Date32) => {
1894            timestamp_to_date32(array.as_primitive::<TimestampNanosecondType>())
1895        }
1896        (Timestamp(TimeUnit::Second, _), Date64) => Ok(Arc::new(match cast_options.safe {
1897            true => {
1898                // change error to None
1899                array
1900                    .as_primitive::<TimestampSecondType>()
1901                    .unary_opt::<_, Date64Type>(|x| x.checked_mul(MILLISECONDS))
1902            }
1903            false => array
1904                .as_primitive::<TimestampSecondType>()
1905                .try_unary::<_, Date64Type, _>(|x| x.mul_checked(MILLISECONDS))?,
1906        })),
1907        (Timestamp(TimeUnit::Millisecond, _), Date64) => {
1908            cast_reinterpret_arrays::<TimestampMillisecondType, Date64Type>(array)
1909        }
1910        (Timestamp(TimeUnit::Microsecond, _), Date64) => Ok(Arc::new(
1911            array
1912                .as_primitive::<TimestampMicrosecondType>()
1913                .unary::<_, Date64Type>(|x| x / (MICROSECONDS / MILLISECONDS)),
1914        )),
1915        (Timestamp(TimeUnit::Nanosecond, _), Date64) => Ok(Arc::new(
1916            array
1917                .as_primitive::<TimestampNanosecondType>()
1918                .unary::<_, Date64Type>(|x| x / (NANOSECONDS / MILLISECONDS)),
1919        )),
1920        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Microsecond)) => {
1921            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1922            Ok(Arc::new(
1923                array
1924                    .as_primitive::<TimestampSecondType>()
1925                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1926                        Ok(time_to_time64us(as_time_res_with_timezone::<
1927                            TimestampSecondType,
1928                        >(x, tz)?))
1929                    })?,
1930            ))
1931        }
1932        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Nanosecond)) => {
1933            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1934            Ok(Arc::new(
1935                array
1936                    .as_primitive::<TimestampSecondType>()
1937                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1938                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1939                            TimestampSecondType,
1940                        >(x, tz)?))
1941                    })?,
1942            ))
1943        }
1944        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Microsecond)) => {
1945            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1946            Ok(Arc::new(
1947                array
1948                    .as_primitive::<TimestampMillisecondType>()
1949                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1950                        Ok(time_to_time64us(as_time_res_with_timezone::<
1951                            TimestampMillisecondType,
1952                        >(x, tz)?))
1953                    })?,
1954            ))
1955        }
1956        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Nanosecond)) => {
1957            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1958            Ok(Arc::new(
1959                array
1960                    .as_primitive::<TimestampMillisecondType>()
1961                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1962                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1963                            TimestampMillisecondType,
1964                        >(x, tz)?))
1965                    })?,
1966            ))
1967        }
1968        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Microsecond)) => {
1969            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1970            Ok(Arc::new(
1971                array
1972                    .as_primitive::<TimestampMicrosecondType>()
1973                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1974                        Ok(time_to_time64us(as_time_res_with_timezone::<
1975                            TimestampMicrosecondType,
1976                        >(x, tz)?))
1977                    })?,
1978            ))
1979        }
1980        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Nanosecond)) => {
1981            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1982            Ok(Arc::new(
1983                array
1984                    .as_primitive::<TimestampMicrosecondType>()
1985                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1986                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1987                            TimestampMicrosecondType,
1988                        >(x, tz)?))
1989                    })?,
1990            ))
1991        }
1992        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Microsecond)) => {
1993            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1994            Ok(Arc::new(
1995                array
1996                    .as_primitive::<TimestampNanosecondType>()
1997                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1998                        Ok(time_to_time64us(as_time_res_with_timezone::<
1999                            TimestampNanosecondType,
2000                        >(x, tz)?))
2001                    })?,
2002            ))
2003        }
2004        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Nanosecond)) => {
2005            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2006            Ok(Arc::new(
2007                array
2008                    .as_primitive::<TimestampNanosecondType>()
2009                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
2010                        Ok(time_to_time64ns(as_time_res_with_timezone::<
2011                            TimestampNanosecondType,
2012                        >(x, tz)?))
2013                    })?,
2014            ))
2015        }
2016        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Second)) => {
2017            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2018            Ok(Arc::new(
2019                array
2020                    .as_primitive::<TimestampSecondType>()
2021                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2022                        Ok(time_to_time32s(as_time_res_with_timezone::<
2023                            TimestampSecondType,
2024                        >(x, tz)?))
2025                    })?,
2026            ))
2027        }
2028        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Millisecond)) => {
2029            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2030            Ok(Arc::new(
2031                array
2032                    .as_primitive::<TimestampSecondType>()
2033                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2034                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2035                            TimestampSecondType,
2036                        >(x, tz)?))
2037                    })?,
2038            ))
2039        }
2040        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Second)) => {
2041            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2042            Ok(Arc::new(
2043                array
2044                    .as_primitive::<TimestampMillisecondType>()
2045                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2046                        Ok(time_to_time32s(as_time_res_with_timezone::<
2047                            TimestampMillisecondType,
2048                        >(x, tz)?))
2049                    })?,
2050            ))
2051        }
2052        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Millisecond)) => {
2053            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2054            Ok(Arc::new(
2055                array
2056                    .as_primitive::<TimestampMillisecondType>()
2057                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2058                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2059                            TimestampMillisecondType,
2060                        >(x, tz)?))
2061                    })?,
2062            ))
2063        }
2064        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Second)) => {
2065            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2066            Ok(Arc::new(
2067                array
2068                    .as_primitive::<TimestampMicrosecondType>()
2069                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2070                        Ok(time_to_time32s(as_time_res_with_timezone::<
2071                            TimestampMicrosecondType,
2072                        >(x, tz)?))
2073                    })?,
2074            ))
2075        }
2076        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Millisecond)) => {
2077            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2078            Ok(Arc::new(
2079                array
2080                    .as_primitive::<TimestampMicrosecondType>()
2081                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2082                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2083                            TimestampMicrosecondType,
2084                        >(x, tz)?))
2085                    })?,
2086            ))
2087        }
2088        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Second)) => {
2089            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2090            Ok(Arc::new(
2091                array
2092                    .as_primitive::<TimestampNanosecondType>()
2093                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2094                        Ok(time_to_time32s(as_time_res_with_timezone::<
2095                            TimestampNanosecondType,
2096                        >(x, tz)?))
2097                    })?,
2098            ))
2099        }
2100        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Millisecond)) => {
2101            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2102            Ok(Arc::new(
2103                array
2104                    .as_primitive::<TimestampNanosecondType>()
2105                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2106                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2107                            TimestampNanosecondType,
2108                        >(x, tz)?))
2109                    })?,
2110            ))
2111        }
2112        (Date64, Timestamp(TimeUnit::Second, _)) => {
2113            let array = array
2114                .as_primitive::<Date64Type>()
2115                .unary::<_, TimestampSecondType>(|x| x / MILLISECONDS);
2116
2117            cast_with_options(&array, to_type, cast_options)
2118        }
2119        (Date64, Timestamp(TimeUnit::Millisecond, _)) => {
2120            let array = array
2121                .as_primitive::<Date64Type>()
2122                .reinterpret_cast::<TimestampMillisecondType>();
2123
2124            cast_with_options(&array, to_type, cast_options)
2125        }
2126
2127        (Date64, Timestamp(TimeUnit::Microsecond, _)) => {
2128            let array = array
2129                .as_primitive::<Date64Type>()
2130                .unary::<_, TimestampMicrosecondType>(|x| x * (MICROSECONDS / MILLISECONDS));
2131
2132            cast_with_options(&array, to_type, cast_options)
2133        }
2134        (Date64, Timestamp(TimeUnit::Nanosecond, _)) => {
2135            let array = array
2136                .as_primitive::<Date64Type>()
2137                .unary::<_, TimestampNanosecondType>(|x| x * (NANOSECONDS / MILLISECONDS));
2138
2139            cast_with_options(&array, to_type, cast_options)
2140        }
2141        (Date32, Timestamp(TimeUnit::Second, _)) => {
2142            let array = array
2143                .as_primitive::<Date32Type>()
2144                .unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY);
2145
2146            cast_with_options(&array, to_type, cast_options)
2147        }
2148        (Date32, Timestamp(TimeUnit::Millisecond, _)) => {
2149            let array = array
2150                .as_primitive::<Date32Type>()
2151                .unary::<_, TimestampMillisecondType>(|x| (x as i64) * MILLISECONDS_IN_DAY);
2152
2153            cast_with_options(&array, to_type, cast_options)
2154        }
2155        (Date32, Timestamp(TimeUnit::Microsecond, _)) => {
2156            let date_array = array.as_primitive::<Date32Type>();
2157            let converted = if cast_options.safe {
2158                date_array.unary_opt::<_, TimestampMicrosecondType>(|x| {
2159                    (x as i64).checked_mul(MICROSECONDS_IN_DAY)
2160                })
2161            } else {
2162                date_array.try_unary::<_, TimestampMicrosecondType, _>(|x| {
2163                    (x as i64).mul_checked(MICROSECONDS_IN_DAY)
2164                })?
2165            };
2166            cast_with_options(&converted, to_type, cast_options)
2167        }
2168        (Date32, Timestamp(TimeUnit::Nanosecond, _)) => {
2169            let date_array = array.as_primitive::<Date32Type>();
2170            let converted = if cast_options.safe {
2171                date_array.unary_opt::<_, TimestampNanosecondType>(|x| {
2172                    (x as i64).checked_mul(NANOSECONDS_IN_DAY)
2173                })
2174            } else {
2175                date_array.try_unary::<_, TimestampNanosecondType, _>(|x| {
2176                    (x as i64).mul_checked(NANOSECONDS_IN_DAY)
2177                })?
2178            };
2179            cast_with_options(&converted, to_type, cast_options)
2180        }
2181
2182        (_, Duration(unit)) if from_type.is_numeric() => {
2183            let array = cast_with_options(array, &Int64, cast_options)?;
2184            Ok(make_duration_array(array.as_primitive(), *unit))
2185        }
2186        (Duration(TimeUnit::Second), _) if to_type.is_numeric() => {
2187            let array = cast_reinterpret_arrays::<DurationSecondType, Int64Type>(array)?;
2188            cast_with_options(&array, to_type, cast_options)
2189        }
2190        (Duration(TimeUnit::Millisecond), _) if to_type.is_numeric() => {
2191            let array = cast_reinterpret_arrays::<DurationMillisecondType, Int64Type>(array)?;
2192            cast_with_options(&array, to_type, cast_options)
2193        }
2194        (Duration(TimeUnit::Microsecond), _) if to_type.is_numeric() => {
2195            let array = cast_reinterpret_arrays::<DurationMicrosecondType, Int64Type>(array)?;
2196            cast_with_options(&array, to_type, cast_options)
2197        }
2198        (Duration(TimeUnit::Nanosecond), _) if to_type.is_numeric() => {
2199            let array = cast_reinterpret_arrays::<DurationNanosecondType, Int64Type>(array)?;
2200            cast_with_options(&array, to_type, cast_options)
2201        }
2202
2203        (Duration(from_unit), Duration(to_unit)) => {
2204            let array = cast_with_options(array, &Int64, cast_options)?;
2205            let time_array = array.as_primitive::<Int64Type>();
2206            let from_size = time_unit_multiple(from_unit);
2207            let to_size = time_unit_multiple(to_unit);
2208            // we either divide or multiply, depending on size of each unit
2209            // units are never the same when the types are the same
2210            let converted = match from_size.cmp(&to_size) {
2211                Ordering::Greater => {
2212                    let divisor = from_size / to_size;
2213                    time_array.unary::<_, Int64Type>(|o| o / divisor)
2214                }
2215                Ordering::Equal => time_array.clone(),
2216                Ordering::Less => {
2217                    let mul = to_size / from_size;
2218                    if cast_options.safe {
2219                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
2220                    } else {
2221                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
2222                    }
2223                }
2224            };
2225            Ok(make_duration_array(&converted, *to_unit))
2226        }
2227
2228        (Duration(TimeUnit::Second), Interval(IntervalUnit::MonthDayNano)) => {
2229            cast_duration_to_interval::<DurationSecondType>(array, cast_options)
2230        }
2231        (Duration(TimeUnit::Millisecond), Interval(IntervalUnit::MonthDayNano)) => {
2232            cast_duration_to_interval::<DurationMillisecondType>(array, cast_options)
2233        }
2234        (Duration(TimeUnit::Microsecond), Interval(IntervalUnit::MonthDayNano)) => {
2235            cast_duration_to_interval::<DurationMicrosecondType>(array, cast_options)
2236        }
2237        (Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano)) => {
2238            cast_duration_to_interval::<DurationNanosecondType>(array, cast_options)
2239        }
2240        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Second)) => {
2241            cast_month_day_nano_to_duration::<DurationSecondType>(array, cast_options)
2242        }
2243        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Millisecond)) => {
2244            cast_month_day_nano_to_duration::<DurationMillisecondType>(array, cast_options)
2245        }
2246        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Microsecond)) => {
2247            cast_month_day_nano_to_duration::<DurationMicrosecondType>(array, cast_options)
2248        }
2249        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Nanosecond)) => {
2250            cast_month_day_nano_to_duration::<DurationNanosecondType>(array, cast_options)
2251        }
2252        (Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
2253            cast_interval_year_month_to_interval_month_day_nano(array, cast_options)
2254        }
2255        (Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
2256            cast_interval_day_time_to_interval_month_day_nano(array, cast_options)
2257        }
2258        (Int32, Interval(IntervalUnit::YearMonth)) => {
2259            cast_reinterpret_arrays::<Int32Type, IntervalYearMonthType>(array)
2260        }
2261        (_, _) => Err(ArrowError::CastError(format!(
2262            "Casting from {from_type} to {to_type} not supported",
2263        ))),
2264    }
2265}
2266
2267fn cast_struct_to_struct(
2268    array: &StructArray,
2269    from_fields: Fields,
2270    to_fields: Fields,
2271    cast_options: &CastOptions,
2272) -> Result<ArrayRef, ArrowError> {
2273    // Fast path: if field names are in the same order, we can just zip and cast
2274    let fields_match_order = from_fields.len() == to_fields.len()
2275        && from_fields
2276            .iter()
2277            .zip(to_fields.iter())
2278            .all(|(f1, f2)| f1.name() == f2.name());
2279
2280    let fields = if fields_match_order {
2281        // Fast path: cast columns in order if their names match
2282        cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2283    } else {
2284        let all_fields_match_by_name = to_fields.iter().all(|to_field| {
2285            from_fields
2286                .iter()
2287                .any(|from_field| from_field.name() == to_field.name())
2288        });
2289
2290        if all_fields_match_by_name {
2291            // Slow path: match fields by name and reorder
2292            cast_struct_fields_by_name(array, from_fields.clone(), to_fields.clone(), cast_options)?
2293        } else {
2294            // Fallback: cast field by field in order
2295            cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2296        }
2297    };
2298
2299    let array = StructArray::try_new(to_fields.clone(), fields, array.nulls().cloned())?;
2300    Ok(Arc::new(array) as ArrayRef)
2301}
2302
2303fn cast_struct_fields_by_name(
2304    array: &StructArray,
2305    from_fields: Fields,
2306    to_fields: Fields,
2307    cast_options: &CastOptions,
2308) -> Result<Vec<ArrayRef>, ArrowError> {
2309    to_fields
2310        .iter()
2311        .map(|to_field| {
2312            let from_field_idx = from_fields
2313                .iter()
2314                .position(|from_field| from_field.name() == to_field.name())
2315                .unwrap(); // safe because we checked above
2316            let column = array.column(from_field_idx);
2317            cast_with_options(column, to_field.data_type(), cast_options)
2318        })
2319        .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2320}
2321
2322fn cast_struct_fields_in_order(
2323    array: &StructArray,
2324    to_fields: Fields,
2325    cast_options: &CastOptions,
2326) -> Result<Vec<ArrayRef>, ArrowError> {
2327    array
2328        .columns()
2329        .iter()
2330        .zip(to_fields.iter())
2331        .map(|(l, field)| cast_with_options(l, field.data_type(), cast_options))
2332        .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2333}
2334
2335fn cast_from_decimal<D, F>(
2336    array: &dyn Array,
2337    base: D::Native,
2338    scale: &i8,
2339    from_type: &DataType,
2340    to_type: &DataType,
2341    as_float: F,
2342    cast_options: &CastOptions,
2343) -> Result<ArrayRef, ArrowError>
2344where
2345    D: DecimalType + ArrowPrimitiveType,
2346    <D as ArrowPrimitiveType>::Native: ToPrimitive,
2347    F: Fn(D::Native) -> f64,
2348{
2349    use DataType::*;
2350    // cast decimal to other type
2351    match to_type {
2352        UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, base, *scale, cast_options),
2353        UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, base, *scale, cast_options),
2354        UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, base, *scale, cast_options),
2355        UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, base, *scale, cast_options),
2356        Int8 => cast_decimal_to_integer::<D, Int8Type>(array, base, *scale, cast_options),
2357        Int16 => cast_decimal_to_integer::<D, Int16Type>(array, base, *scale, cast_options),
2358        Int32 => cast_decimal_to_integer::<D, Int32Type>(array, base, *scale, cast_options),
2359        Int64 => cast_decimal_to_integer::<D, Int64Type>(array, base, *scale, cast_options),
2360        Float16 => cast_decimal_to_float::<D, Float16Type, _>(array, |x| {
2361            half::f16::from_f64(single_decimal_to_float_lossy::<D, F>(
2362                &as_float,
2363                x,
2364                <i32 as From<i8>>::from(*scale),
2365            ))
2366        }),
2367        Float32 => cast_decimal_to_float::<D, Float32Type, _>(array, |x| {
2368            single_decimal_to_float_lossy::<D, F>(&as_float, x, <i32 as From<i8>>::from(*scale))
2369                as f32
2370        }),
2371        Float64 => cast_decimal_to_float::<D, Float64Type, _>(array, |x| {
2372            single_decimal_to_float_lossy::<D, F>(&as_float, x, <i32 as From<i8>>::from(*scale))
2373        }),
2374        Utf8View => value_to_string_view(array, cast_options),
2375        Utf8 => value_to_string::<i32>(array, cast_options),
2376        LargeUtf8 => value_to_string::<i64>(array, cast_options),
2377        Null => Ok(new_null_array(to_type, array.len())),
2378        _ => Err(ArrowError::CastError(format!(
2379            "Casting from {from_type} to {to_type} not supported"
2380        ))),
2381    }
2382}
2383
2384fn cast_to_decimal<D, M>(
2385    array: &dyn Array,
2386    base: M,
2387    precision: &u8,
2388    scale: &i8,
2389    from_type: &DataType,
2390    to_type: &DataType,
2391    cast_options: &CastOptions,
2392) -> Result<ArrayRef, ArrowError>
2393where
2394    D: DecimalType + ArrowPrimitiveType<Native = M>,
2395    M: ArrowNativeTypeOp + DecimalCast,
2396    u8: num_traits::AsPrimitive<M>,
2397    u16: num_traits::AsPrimitive<M>,
2398    u32: num_traits::AsPrimitive<M>,
2399    u64: num_traits::AsPrimitive<M>,
2400    i8: num_traits::AsPrimitive<M>,
2401    i16: num_traits::AsPrimitive<M>,
2402    i32: num_traits::AsPrimitive<M>,
2403    i64: num_traits::AsPrimitive<M>,
2404{
2405    use DataType::*;
2406    // cast data to decimal
2407    match from_type {
2408        UInt8 => cast_integer_to_decimal::<_, D, M>(
2409            array.as_primitive::<UInt8Type>(),
2410            *precision,
2411            *scale,
2412            base,
2413            cast_options,
2414        ),
2415        UInt16 => cast_integer_to_decimal::<_, D, _>(
2416            array.as_primitive::<UInt16Type>(),
2417            *precision,
2418            *scale,
2419            base,
2420            cast_options,
2421        ),
2422        UInt32 => cast_integer_to_decimal::<_, D, _>(
2423            array.as_primitive::<UInt32Type>(),
2424            *precision,
2425            *scale,
2426            base,
2427            cast_options,
2428        ),
2429        UInt64 => cast_integer_to_decimal::<_, D, _>(
2430            array.as_primitive::<UInt64Type>(),
2431            *precision,
2432            *scale,
2433            base,
2434            cast_options,
2435        ),
2436        Int8 => cast_integer_to_decimal::<_, D, _>(
2437            array.as_primitive::<Int8Type>(),
2438            *precision,
2439            *scale,
2440            base,
2441            cast_options,
2442        ),
2443        Int16 => cast_integer_to_decimal::<_, D, _>(
2444            array.as_primitive::<Int16Type>(),
2445            *precision,
2446            *scale,
2447            base,
2448            cast_options,
2449        ),
2450        Int32 => cast_integer_to_decimal::<_, D, _>(
2451            array.as_primitive::<Int32Type>(),
2452            *precision,
2453            *scale,
2454            base,
2455            cast_options,
2456        ),
2457        Int64 => cast_integer_to_decimal::<_, D, _>(
2458            array.as_primitive::<Int64Type>(),
2459            *precision,
2460            *scale,
2461            base,
2462            cast_options,
2463        ),
2464        Float16 => cast_floating_point_to_decimal::<_, D>(
2465            array.as_primitive::<Float16Type>(),
2466            *precision,
2467            *scale,
2468            cast_options,
2469        ),
2470        Float32 => cast_floating_point_to_decimal::<_, D>(
2471            array.as_primitive::<Float32Type>(),
2472            *precision,
2473            *scale,
2474            cast_options,
2475        ),
2476        Float64 => cast_floating_point_to_decimal::<_, D>(
2477            array.as_primitive::<Float64Type>(),
2478            *precision,
2479            *scale,
2480            cast_options,
2481        ),
2482        Utf8View | Utf8 => {
2483            cast_string_to_decimal::<D, i32>(array, *precision, *scale, cast_options)
2484        }
2485        LargeUtf8 => cast_string_to_decimal::<D, i64>(array, *precision, *scale, cast_options),
2486        Null => Ok(new_null_array(to_type, array.len())),
2487        _ => Err(ArrowError::CastError(format!(
2488            "Casting from {from_type} to {to_type} not supported"
2489        ))),
2490    }
2491}
2492
2493/// Get the time unit as a multiple of a second
2494const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
2495    match unit {
2496        TimeUnit::Second => 1,
2497        TimeUnit::Millisecond => MILLISECONDS,
2498        TimeUnit::Microsecond => MICROSECONDS,
2499        TimeUnit::Nanosecond => NANOSECONDS,
2500    }
2501}
2502
2503/// Convert Array into a PrimitiveArray of type, and apply numeric cast
2504fn cast_numeric_arrays<FROM, TO>(
2505    from: &dyn Array,
2506    cast_options: &CastOptions,
2507) -> Result<ArrayRef, ArrowError>
2508where
2509    FROM: ArrowPrimitiveType,
2510    TO: ArrowPrimitiveType,
2511    FROM::Native: NumCast,
2512    TO::Native: NumCast,
2513{
2514    if cast_options.safe {
2515        // If the value can't be casted to the `TO::Native`, return null
2516        Ok(Arc::new(numeric_cast::<FROM, TO>(
2517            from.as_primitive::<FROM>(),
2518        )))
2519    } else {
2520        // If the value can't be casted to the `TO::Native`, return error
2521        Ok(Arc::new(try_numeric_cast::<FROM, TO>(
2522            from.as_primitive::<FROM>(),
2523        )?))
2524    }
2525}
2526
2527// Natural cast between numeric types
2528// If the value of T can't be casted to R, will throw error
2529fn try_numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>, ArrowError>
2530where
2531    T: ArrowPrimitiveType,
2532    R: ArrowPrimitiveType,
2533    T::Native: NumCast,
2534    R::Native: NumCast,
2535{
2536    from.try_unary(|value| {
2537        num_cast::<T::Native, R::Native>(value).ok_or_else(|| {
2538            ArrowError::CastError(format!(
2539                "Can't cast value {:?} to type {}",
2540                value,
2541                R::DATA_TYPE
2542            ))
2543        })
2544    })
2545}
2546
2547/// Natural cast between numeric types
2548/// Return None if the input `value` can't be casted to type `O`.
2549#[inline]
2550pub fn num_cast<I, O>(value: I) -> Option<O>
2551where
2552    I: NumCast,
2553    O: NumCast,
2554{
2555    num_traits::cast::cast::<I, O>(value)
2556}
2557
2558// Natural cast between numeric types
2559// If the value of T can't be casted to R, it will be converted to null
2560fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> PrimitiveArray<R>
2561where
2562    T: ArrowPrimitiveType,
2563    R: ArrowPrimitiveType,
2564    T::Native: NumCast,
2565    R::Native: NumCast,
2566{
2567    from.unary_opt::<_, R>(num_cast::<T::Native, R::Native>)
2568}
2569
2570fn cast_numeric_to_binary<FROM: ArrowPrimitiveType, O: OffsetSizeTrait>(
2571    array: &dyn Array,
2572) -> Result<ArrayRef, ArrowError> {
2573    let array = array.as_primitive::<FROM>();
2574    let size = std::mem::size_of::<FROM::Native>();
2575    let offsets = OffsetBuffer::from_repeated_length(size, array.len());
2576    Ok(Arc::new(GenericBinaryArray::<O>::try_new(
2577        offsets,
2578        array.values().inner().clone(),
2579        array.nulls().cloned(),
2580    )?))
2581}
2582
2583fn adjust_timestamp_to_timezone<T: ArrowTimestampType>(
2584    array: PrimitiveArray<Int64Type>,
2585    to_tz: &Tz,
2586    cast_options: &CastOptions,
2587) -> Result<PrimitiveArray<Int64Type>, ArrowError> {
2588    let adjust = |o| {
2589        let local = as_datetime::<T>(o)?;
2590        let offset = to_tz.offset_from_local_datetime(&local).single()?;
2591        T::from_naive_datetime(local - offset.fix(), None)
2592    };
2593    let adjusted = if cast_options.safe {
2594        array.unary_opt::<_, Int64Type>(adjust)
2595    } else {
2596        array.try_unary::<_, Int64Type, _>(|o| {
2597            adjust(o).ok_or_else(|| {
2598                ArrowError::CastError("Cannot cast timezone to different timezone".to_string())
2599            })
2600        })?
2601    };
2602    Ok(adjusted)
2603}
2604
2605/// Cast numeric types to Boolean
2606///
2607/// Any zero value returns `false` while non-zero returns `true`
2608fn cast_numeric_to_bool<FROM>(from: &dyn Array) -> Result<ArrayRef, ArrowError>
2609where
2610    FROM: ArrowPrimitiveType,
2611{
2612    numeric_to_bool_cast::<FROM>(from.as_primitive::<FROM>()).map(|to| Arc::new(to) as ArrayRef)
2613}
2614
2615fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray, ArrowError>
2616where
2617    T: ArrowPrimitiveType + ArrowPrimitiveType,
2618{
2619    let mut b = BooleanBuilder::with_capacity(from.len());
2620
2621    for i in 0..from.len() {
2622        if from.is_null(i) {
2623            b.append_null();
2624        } else {
2625            b.append_value(cast_num_to_bool::<T::Native>(from.value(i)));
2626        }
2627    }
2628
2629    Ok(b.finish())
2630}
2631
2632/// Cast numeric types to boolean
2633#[inline]
2634pub fn cast_num_to_bool<I>(value: I) -> bool
2635where
2636    I: Default + PartialEq,
2637{
2638    value != I::default()
2639}
2640
2641/// Cast Boolean types to numeric
2642///
2643/// `false` returns 0 while `true` returns 1
2644fn cast_bool_to_numeric<TO>(
2645    from: &dyn Array,
2646    cast_options: &CastOptions,
2647) -> Result<ArrayRef, ArrowError>
2648where
2649    TO: ArrowPrimitiveType,
2650    TO::Native: num_traits::cast::NumCast,
2651{
2652    Ok(Arc::new(bool_to_numeric_cast::<TO>(
2653        from.as_any().downcast_ref::<BooleanArray>().unwrap(),
2654        cast_options,
2655    )))
2656}
2657
2658fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions) -> PrimitiveArray<T>
2659where
2660    T: ArrowPrimitiveType,
2661    T::Native: num_traits::NumCast,
2662{
2663    let iter = (0..from.len()).map(|i| {
2664        if from.is_null(i) {
2665            None
2666        } else {
2667            single_bool_to_numeric::<T::Native>(from.value(i))
2668        }
2669    });
2670    // Benefit:
2671    //     20% performance improvement
2672    // Soundness:
2673    //     The iterator is trustedLen because it comes from a Range
2674    unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
2675}
2676
2677/// Cast single bool value to numeric value.
2678#[inline]
2679pub fn single_bool_to_numeric<O>(value: bool) -> Option<O>
2680where
2681    O: num_traits::NumCast + Default,
2682{
2683    if value {
2684        // a workaround to cast a primitive to type O, infallible
2685        num_traits::cast::cast(1)
2686    } else {
2687        Some(O::default())
2688    }
2689}
2690
2691/// Helper function to cast from one `BinaryArray` or 'LargeBinaryArray' to 'FixedSizeBinaryArray'.
2692fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
2693    array: &dyn Array,
2694    byte_width: i32,
2695    cast_options: &CastOptions,
2696) -> Result<ArrayRef, ArrowError> {
2697    let array = array.as_binary::<O>();
2698    let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(), byte_width);
2699
2700    for i in 0..array.len() {
2701        if array.is_null(i) {
2702            builder.append_null();
2703        } else {
2704            match builder.append_value(array.value(i)) {
2705                Ok(_) => {}
2706                Err(e) => match cast_options.safe {
2707                    true => builder.append_null(),
2708                    false => return Err(e),
2709                },
2710            }
2711        }
2712    }
2713
2714    Ok(Arc::new(builder.finish()))
2715}
2716
2717/// Helper function to cast from 'FixedSizeBinaryArray' to one `BinaryArray` or 'LargeBinaryArray'.
2718/// If the target one is too large for the source array it will return an Error.
2719fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
2720    array: &dyn Array,
2721    byte_width: i32,
2722) -> Result<ArrayRef, ArrowError> {
2723    let array = array
2724        .as_any()
2725        .downcast_ref::<FixedSizeBinaryArray>()
2726        .unwrap();
2727
2728    let offsets: i128 = byte_width as i128 * array.len() as i128;
2729
2730    let is_binary = matches!(GenericBinaryType::<O>::DATA_TYPE, DataType::Binary);
2731    if is_binary && offsets > i32::MAX as i128 {
2732        return Err(ArrowError::ComputeError(
2733            "FixedSizeBinary array too large to cast to Binary array".to_string(),
2734        ));
2735    } else if !is_binary && offsets > i64::MAX as i128 {
2736        return Err(ArrowError::ComputeError(
2737            "FixedSizeBinary array too large to cast to LargeBinary array".to_string(),
2738        ));
2739    }
2740
2741    let mut builder = GenericBinaryBuilder::<O>::with_capacity(array.len(), array.len());
2742
2743    for i in 0..array.len() {
2744        if array.is_null(i) {
2745            builder.append_null();
2746        } else {
2747            builder.append_value(array.value(i));
2748        }
2749    }
2750
2751    Ok(Arc::new(builder.finish()))
2752}
2753
2754fn cast_fixed_size_binary_to_binary_view(
2755    array: &dyn Array,
2756    _byte_width: i32,
2757) -> Result<ArrayRef, ArrowError> {
2758    let array = array
2759        .as_any()
2760        .downcast_ref::<FixedSizeBinaryArray>()
2761        .unwrap();
2762
2763    let mut builder = BinaryViewBuilder::with_capacity(array.len());
2764    for i in 0..array.len() {
2765        if array.is_null(i) {
2766            builder.append_null();
2767        } else {
2768            builder.append_value(array.value(i));
2769        }
2770    }
2771
2772    Ok(Arc::new(builder.finish()))
2773}
2774
2775/// Helper function to cast from one `ByteArrayType` to another and vice versa.
2776/// If the target one (e.g., `LargeUtf8`) is too large for the source array it will return an Error.
2777fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2778where
2779    FROM: ByteArrayType,
2780    TO: ByteArrayType<Native = FROM::Native>,
2781    FROM::Offset: OffsetSizeTrait + ToPrimitive,
2782    TO::Offset: OffsetSizeTrait + NumCast,
2783{
2784    let data = array.to_data();
2785    assert_eq!(data.data_type(), &FROM::DATA_TYPE);
2786    let str_values_buf = data.buffers()[1].clone();
2787    let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
2788
2789    let mut offset_builder = BufferBuilder::<TO::Offset>::new(offsets.len());
2790    offsets
2791        .iter()
2792        .try_for_each::<_, Result<_, ArrowError>>(|offset| {
2793            let offset =
2794                <<TO as ByteArrayType>::Offset as NumCast>::from(*offset).ok_or_else(|| {
2795                    ArrowError::ComputeError(format!(
2796                        "{}{} array too large to cast to {}{} array",
2797                        FROM::Offset::PREFIX,
2798                        FROM::PREFIX,
2799                        TO::Offset::PREFIX,
2800                        TO::PREFIX
2801                    ))
2802                })?;
2803            offset_builder.append(offset);
2804            Ok(())
2805        })?;
2806
2807    let offset_buffer = offset_builder.finish();
2808
2809    let dtype = TO::DATA_TYPE;
2810
2811    let builder = ArrayData::builder(dtype)
2812        .offset(array.offset())
2813        .len(array.len())
2814        .add_buffer(offset_buffer)
2815        .add_buffer(str_values_buf)
2816        .nulls(data.nulls().cloned());
2817
2818    let array_data = unsafe { builder.build_unchecked() };
2819
2820    Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
2821}
2822
2823/// Helper function to cast from one `ByteViewType` array to `ByteArrayType` array.
2824fn cast_view_to_byte<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2825where
2826    FROM: ByteViewType,
2827    TO: ByteArrayType,
2828    FROM::Native: AsRef<TO::Native>,
2829{
2830    let data = array.to_data();
2831    let view_array = GenericByteViewArray::<FROM>::from(data);
2832
2833    let len = view_array.len();
2834    let bytes = view_array
2835        .views()
2836        .iter()
2837        .map(|v| ByteView::from(*v).length as usize)
2838        .sum::<usize>();
2839
2840    let mut byte_array_builder = GenericByteBuilder::<TO>::with_capacity(len, bytes);
2841
2842    for val in view_array.iter() {
2843        byte_array_builder.append_option(val);
2844    }
2845
2846    Ok(Arc::new(byte_array_builder.finish()))
2847}
2848
2849#[cfg(test)]
2850mod tests {
2851    use super::*;
2852    use DataType::*;
2853    use arrow_array::{Int64Array, RunArray, StringArray};
2854    use arrow_buffer::{Buffer, IntervalDayTime, NullBuffer};
2855    use arrow_buffer::{ScalarBuffer, i256};
2856    use arrow_schema::{DataType, Field};
2857    use chrono::NaiveDate;
2858    use half::f16;
2859    use std::sync::Arc;
2860
2861    #[derive(Clone)]
2862    struct DecimalCastTestConfig {
2863        input_prec: u8,
2864        input_scale: i8,
2865        input_repr: i128,
2866        output_prec: u8,
2867        output_scale: i8,
2868        expected_output_repr: Result<i128, String>, // the error variant can contain a string
2869                                                    // template where the "{}" will be
2870                                                    // replaced with the decimal type name
2871                                                    // (e.g. Decimal128)
2872    }
2873
2874    macro_rules! generate_cast_test_case {
2875        ($INPUT_ARRAY: expr, $OUTPUT_TYPE_ARRAY: ident, $OUTPUT_TYPE: expr, $OUTPUT_VALUES: expr) => {
2876            let output =
2877                $OUTPUT_TYPE_ARRAY::from($OUTPUT_VALUES).with_data_type($OUTPUT_TYPE.clone());
2878
2879            // assert cast type
2880            let input_array_type = $INPUT_ARRAY.data_type();
2881            assert!(can_cast_types(input_array_type, $OUTPUT_TYPE));
2882            let result = cast($INPUT_ARRAY, $OUTPUT_TYPE).unwrap();
2883            assert_eq!($OUTPUT_TYPE, result.data_type());
2884            assert_eq!(result.as_ref(), &output);
2885
2886            let cast_option = CastOptions {
2887                safe: false,
2888                format_options: FormatOptions::default(),
2889            };
2890            let result = cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap();
2891            assert_eq!($OUTPUT_TYPE, result.data_type());
2892            assert_eq!(result.as_ref(), &output);
2893        };
2894    }
2895
2896    fn run_decimal_cast_test_case<I, O>(t: DecimalCastTestConfig)
2897    where
2898        I: DecimalType,
2899        O: DecimalType,
2900        I::Native: DecimalCast,
2901        O::Native: DecimalCast,
2902    {
2903        let array = vec![I::Native::from_decimal(t.input_repr)];
2904        let array = array
2905            .into_iter()
2906            .collect::<PrimitiveArray<I>>()
2907            .with_precision_and_scale(t.input_prec, t.input_scale)
2908            .unwrap();
2909        let input_type = array.data_type();
2910        let output_type = O::TYPE_CONSTRUCTOR(t.output_prec, t.output_scale);
2911        assert!(can_cast_types(input_type, &output_type));
2912
2913        let options = CastOptions {
2914            safe: false,
2915            ..Default::default()
2916        };
2917        let result = cast_with_options(&array, &output_type, &options);
2918
2919        match t.expected_output_repr {
2920            Ok(v) => {
2921                let expected_array = vec![O::Native::from_decimal(v)];
2922                let expected_array = expected_array
2923                    .into_iter()
2924                    .collect::<PrimitiveArray<O>>()
2925                    .with_precision_and_scale(t.output_prec, t.output_scale)
2926                    .unwrap();
2927                assert_eq!(*result.unwrap(), expected_array);
2928            }
2929            Err(expected_output_message_template) => {
2930                assert!(result.is_err());
2931                let expected_error_message =
2932                    expected_output_message_template.replace("{}", O::PREFIX);
2933                assert_eq!(result.unwrap_err().to_string(), expected_error_message);
2934            }
2935        }
2936    }
2937
2938    fn create_decimal32_array(
2939        array: Vec<Option<i32>>,
2940        precision: u8,
2941        scale: i8,
2942    ) -> Result<Decimal32Array, ArrowError> {
2943        array
2944            .into_iter()
2945            .collect::<Decimal32Array>()
2946            .with_precision_and_scale(precision, scale)
2947    }
2948
2949    fn create_decimal64_array(
2950        array: Vec<Option<i64>>,
2951        precision: u8,
2952        scale: i8,
2953    ) -> Result<Decimal64Array, ArrowError> {
2954        array
2955            .into_iter()
2956            .collect::<Decimal64Array>()
2957            .with_precision_and_scale(precision, scale)
2958    }
2959
2960    fn create_decimal128_array(
2961        array: Vec<Option<i128>>,
2962        precision: u8,
2963        scale: i8,
2964    ) -> Result<Decimal128Array, ArrowError> {
2965        array
2966            .into_iter()
2967            .collect::<Decimal128Array>()
2968            .with_precision_and_scale(precision, scale)
2969    }
2970
2971    fn create_decimal256_array(
2972        array: Vec<Option<i256>>,
2973        precision: u8,
2974        scale: i8,
2975    ) -> Result<Decimal256Array, ArrowError> {
2976        array
2977            .into_iter()
2978            .collect::<Decimal256Array>()
2979            .with_precision_and_scale(precision, scale)
2980    }
2981
2982    #[test]
2983    #[cfg(not(feature = "force_validate"))]
2984    #[should_panic(
2985        expected = "Cannot cast to Decimal128(20, 3). Overflowing on 57896044618658097711785492504343953926634992332820282019728792003956564819967"
2986    )]
2987    fn test_cast_decimal_to_decimal_round_with_error() {
2988        // decimal256 to decimal128 overflow
2989        let array = vec![
2990            Some(i256::from_i128(1123454)),
2991            Some(i256::from_i128(2123456)),
2992            Some(i256::from_i128(-3123453)),
2993            Some(i256::from_i128(-3123456)),
2994            None,
2995            Some(i256::MAX),
2996            Some(i256::MIN),
2997        ];
2998        let input_decimal_array = create_decimal256_array(array, 76, 4).unwrap();
2999        let array = Arc::new(input_decimal_array) as ArrayRef;
3000        let input_type = DataType::Decimal256(76, 4);
3001        let output_type = DataType::Decimal128(20, 3);
3002        assert!(can_cast_types(&input_type, &output_type));
3003        generate_cast_test_case!(
3004            &array,
3005            Decimal128Array,
3006            &output_type,
3007            vec![
3008                Some(112345_i128),
3009                Some(212346_i128),
3010                Some(-312345_i128),
3011                Some(-312346_i128),
3012                None,
3013                None,
3014                None,
3015            ]
3016        );
3017    }
3018
3019    #[test]
3020    #[cfg(not(feature = "force_validate"))]
3021    fn test_cast_decimal_to_decimal_round() {
3022        let array = vec![
3023            Some(1123454),
3024            Some(2123456),
3025            Some(-3123453),
3026            Some(-3123456),
3027            None,
3028        ];
3029        let array = create_decimal128_array(array, 20, 4).unwrap();
3030        // decimal128 to decimal128
3031        let input_type = DataType::Decimal128(20, 4);
3032        let output_type = DataType::Decimal128(20, 3);
3033        assert!(can_cast_types(&input_type, &output_type));
3034        generate_cast_test_case!(
3035            &array,
3036            Decimal128Array,
3037            &output_type,
3038            vec![
3039                Some(112345_i128),
3040                Some(212346_i128),
3041                Some(-312345_i128),
3042                Some(-312346_i128),
3043                None
3044            ]
3045        );
3046
3047        // decimal128 to decimal256
3048        let input_type = DataType::Decimal128(20, 4);
3049        let output_type = DataType::Decimal256(20, 3);
3050        assert!(can_cast_types(&input_type, &output_type));
3051        generate_cast_test_case!(
3052            &array,
3053            Decimal256Array,
3054            &output_type,
3055            vec![
3056                Some(i256::from_i128(112345_i128)),
3057                Some(i256::from_i128(212346_i128)),
3058                Some(i256::from_i128(-312345_i128)),
3059                Some(i256::from_i128(-312346_i128)),
3060                None
3061            ]
3062        );
3063
3064        // decimal256
3065        let array = vec![
3066            Some(i256::from_i128(1123454)),
3067            Some(i256::from_i128(2123456)),
3068            Some(i256::from_i128(-3123453)),
3069            Some(i256::from_i128(-3123456)),
3070            None,
3071        ];
3072        let array = create_decimal256_array(array, 20, 4).unwrap();
3073
3074        // decimal256 to decimal256
3075        let input_type = DataType::Decimal256(20, 4);
3076        let output_type = DataType::Decimal256(20, 3);
3077        assert!(can_cast_types(&input_type, &output_type));
3078        generate_cast_test_case!(
3079            &array,
3080            Decimal256Array,
3081            &output_type,
3082            vec![
3083                Some(i256::from_i128(112345_i128)),
3084                Some(i256::from_i128(212346_i128)),
3085                Some(i256::from_i128(-312345_i128)),
3086                Some(i256::from_i128(-312346_i128)),
3087                None
3088            ]
3089        );
3090        // decimal256 to decimal128
3091        let input_type = DataType::Decimal256(20, 4);
3092        let output_type = DataType::Decimal128(20, 3);
3093        assert!(can_cast_types(&input_type, &output_type));
3094        generate_cast_test_case!(
3095            &array,
3096            Decimal128Array,
3097            &output_type,
3098            vec![
3099                Some(112345_i128),
3100                Some(212346_i128),
3101                Some(-312345_i128),
3102                Some(-312346_i128),
3103                None
3104            ]
3105        );
3106    }
3107
3108    #[test]
3109    fn test_cast_decimal32_to_decimal32() {
3110        // test changing precision
3111        let input_type = DataType::Decimal32(9, 3);
3112        let output_type = DataType::Decimal32(9, 4);
3113        assert!(can_cast_types(&input_type, &output_type));
3114        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3115        let array = create_decimal32_array(array, 9, 3).unwrap();
3116        generate_cast_test_case!(
3117            &array,
3118            Decimal32Array,
3119            &output_type,
3120            vec![
3121                Some(11234560_i32),
3122                Some(21234560_i32),
3123                Some(31234560_i32),
3124                None
3125            ]
3126        );
3127        // negative test
3128        let array = vec![Some(123456), None];
3129        let array = create_decimal32_array(array, 9, 0).unwrap();
3130        let result_safe = cast(&array, &DataType::Decimal32(2, 2));
3131        assert!(result_safe.is_ok());
3132        let options = CastOptions {
3133            safe: false,
3134            ..Default::default()
3135        };
3136
3137        let result_unsafe = cast_with_options(&array, &DataType::Decimal32(2, 2), &options);
3138        assert_eq!(
3139            "Invalid argument error: 123456.00 is too large to store in a Decimal32 of precision 2. Max is 0.99",
3140            result_unsafe.unwrap_err().to_string()
3141        );
3142    }
3143
3144    #[test]
3145    fn test_cast_decimal64_to_decimal64() {
3146        // test changing precision
3147        let input_type = DataType::Decimal64(17, 3);
3148        let output_type = DataType::Decimal64(17, 4);
3149        assert!(can_cast_types(&input_type, &output_type));
3150        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3151        let array = create_decimal64_array(array, 17, 3).unwrap();
3152        generate_cast_test_case!(
3153            &array,
3154            Decimal64Array,
3155            &output_type,
3156            vec![
3157                Some(11234560_i64),
3158                Some(21234560_i64),
3159                Some(31234560_i64),
3160                None
3161            ]
3162        );
3163        // negative test
3164        let array = vec![Some(123456), None];
3165        let array = create_decimal64_array(array, 9, 0).unwrap();
3166        let result_safe = cast(&array, &DataType::Decimal64(2, 2));
3167        assert!(result_safe.is_ok());
3168        let options = CastOptions {
3169            safe: false,
3170            ..Default::default()
3171        };
3172
3173        let result_unsafe = cast_with_options(&array, &DataType::Decimal64(2, 2), &options);
3174        assert_eq!(
3175            "Invalid argument error: 123456.00 is too large to store in a Decimal64 of precision 2. Max is 0.99",
3176            result_unsafe.unwrap_err().to_string()
3177        );
3178    }
3179
3180    #[test]
3181    fn test_cast_decimal128_to_decimal128() {
3182        // test changing precision
3183        let input_type = DataType::Decimal128(20, 3);
3184        let output_type = DataType::Decimal128(20, 4);
3185        assert!(can_cast_types(&input_type, &output_type));
3186        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3187        let array = create_decimal128_array(array, 20, 3).unwrap();
3188        generate_cast_test_case!(
3189            &array,
3190            Decimal128Array,
3191            &output_type,
3192            vec![
3193                Some(11234560_i128),
3194                Some(21234560_i128),
3195                Some(31234560_i128),
3196                None
3197            ]
3198        );
3199        // negative test
3200        let array = vec![Some(123456), None];
3201        let array = create_decimal128_array(array, 10, 0).unwrap();
3202        let result_safe = cast(&array, &DataType::Decimal128(2, 2));
3203        assert!(result_safe.is_ok());
3204        let options = CastOptions {
3205            safe: false,
3206            ..Default::default()
3207        };
3208
3209        let result_unsafe = cast_with_options(&array, &DataType::Decimal128(2, 2), &options);
3210        assert_eq!(
3211            "Invalid argument error: 123456.00 is too large to store in a Decimal128 of precision 2. Max is 0.99",
3212            result_unsafe.unwrap_err().to_string()
3213        );
3214    }
3215
3216    #[test]
3217    fn test_cast_decimal32_to_decimal32_dict() {
3218        let p = 9;
3219        let s = 3;
3220        let input_type = DataType::Decimal32(p, s);
3221        let output_type = DataType::Dictionary(
3222            Box::new(DataType::Int32),
3223            Box::new(DataType::Decimal32(p, s)),
3224        );
3225        assert!(can_cast_types(&input_type, &output_type));
3226        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3227        let array = create_decimal32_array(array, p, s).unwrap();
3228        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3229        assert_eq!(cast_array.data_type(), &output_type);
3230    }
3231
3232    #[test]
3233    fn test_cast_decimal64_to_decimal64_dict() {
3234        let p = 15;
3235        let s = 3;
3236        let input_type = DataType::Decimal64(p, s);
3237        let output_type = DataType::Dictionary(
3238            Box::new(DataType::Int32),
3239            Box::new(DataType::Decimal64(p, s)),
3240        );
3241        assert!(can_cast_types(&input_type, &output_type));
3242        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3243        let array = create_decimal64_array(array, p, s).unwrap();
3244        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3245        assert_eq!(cast_array.data_type(), &output_type);
3246    }
3247
3248    #[test]
3249    fn test_cast_decimal128_to_decimal128_dict() {
3250        let p = 20;
3251        let s = 3;
3252        let input_type = DataType::Decimal128(p, s);
3253        let output_type = DataType::Dictionary(
3254            Box::new(DataType::Int32),
3255            Box::new(DataType::Decimal128(p, s)),
3256        );
3257        assert!(can_cast_types(&input_type, &output_type));
3258        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3259        let array = create_decimal128_array(array, p, s).unwrap();
3260        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3261        assert_eq!(cast_array.data_type(), &output_type);
3262    }
3263
3264    #[test]
3265    fn test_cast_decimal256_to_decimal256_dict() {
3266        let p = 20;
3267        let s = 3;
3268        let input_type = DataType::Decimal256(p, s);
3269        let output_type = DataType::Dictionary(
3270            Box::new(DataType::Int32),
3271            Box::new(DataType::Decimal256(p, s)),
3272        );
3273        assert!(can_cast_types(&input_type, &output_type));
3274        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3275        let array = create_decimal128_array(array, p, s).unwrap();
3276        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3277        assert_eq!(cast_array.data_type(), &output_type);
3278    }
3279
3280    #[test]
3281    fn test_cast_decimal32_to_decimal32_overflow() {
3282        let input_type = DataType::Decimal32(9, 3);
3283        let output_type = DataType::Decimal32(9, 9);
3284        assert!(can_cast_types(&input_type, &output_type));
3285
3286        let array = vec![Some(i32::MAX)];
3287        let array = create_decimal32_array(array, 9, 3).unwrap();
3288        let result = cast_with_options(
3289            &array,
3290            &output_type,
3291            &CastOptions {
3292                safe: false,
3293                format_options: FormatOptions::default(),
3294            },
3295        );
3296        assert_eq!(
3297            "Cast error: Cannot cast to Decimal32(9, 9). Overflowing on 2147483647",
3298            result.unwrap_err().to_string()
3299        );
3300    }
3301
3302    #[test]
3303    fn test_cast_decimal32_to_decimal32_large_scale_reduction() {
3304        let array = vec![Some(-999999999), Some(0), Some(999999999), None];
3305        let array = create_decimal32_array(array, 9, 3).unwrap();
3306
3307        // Divide out all digits of precision -- rounding could still produce +/- 1
3308        let output_type = DataType::Decimal32(9, -6);
3309        assert!(can_cast_types(array.data_type(), &output_type));
3310        generate_cast_test_case!(
3311            &array,
3312            Decimal32Array,
3313            &output_type,
3314            vec![Some(-1), Some(0), Some(1), None]
3315        );
3316
3317        // Divide out more digits than we have precision -- all-zero result
3318        let output_type = DataType::Decimal32(9, -7);
3319        assert!(can_cast_types(array.data_type(), &output_type));
3320        generate_cast_test_case!(
3321            &array,
3322            Decimal32Array,
3323            &output_type,
3324            vec![Some(0), Some(0), Some(0), None]
3325        );
3326    }
3327
3328    #[test]
3329    fn test_cast_decimal64_to_decimal64_overflow() {
3330        let input_type = DataType::Decimal64(18, 3);
3331        let output_type = DataType::Decimal64(18, 18);
3332        assert!(can_cast_types(&input_type, &output_type));
3333
3334        let array = vec![Some(i64::MAX)];
3335        let array = create_decimal64_array(array, 18, 3).unwrap();
3336        let result = cast_with_options(
3337            &array,
3338            &output_type,
3339            &CastOptions {
3340                safe: false,
3341                format_options: FormatOptions::default(),
3342            },
3343        );
3344        assert_eq!(
3345            "Cast error: Cannot cast to Decimal64(18, 18). Overflowing on 9223372036854775807",
3346            result.unwrap_err().to_string()
3347        );
3348    }
3349
3350    #[test]
3351    fn test_cast_decimal64_to_decimal64_large_scale_reduction() {
3352        let array = vec![
3353            Some(-999999999999999999),
3354            Some(0),
3355            Some(999999999999999999),
3356            None,
3357        ];
3358        let array = create_decimal64_array(array, 18, 3).unwrap();
3359
3360        // Divide out all digits of precision -- rounding could still produce +/- 1
3361        let output_type = DataType::Decimal64(18, -15);
3362        assert!(can_cast_types(array.data_type(), &output_type));
3363        generate_cast_test_case!(
3364            &array,
3365            Decimal64Array,
3366            &output_type,
3367            vec![Some(-1), Some(0), Some(1), None]
3368        );
3369
3370        // Divide out more digits than we have precision -- all-zero result
3371        let output_type = DataType::Decimal64(18, -16);
3372        assert!(can_cast_types(array.data_type(), &output_type));
3373        generate_cast_test_case!(
3374            &array,
3375            Decimal64Array,
3376            &output_type,
3377            vec![Some(0), Some(0), Some(0), None]
3378        );
3379    }
3380
3381    #[test]
3382    fn test_cast_floating_to_decimals() {
3383        for output_type in [
3384            DataType::Decimal32(9, 3),
3385            DataType::Decimal64(9, 3),
3386            DataType::Decimal128(9, 3),
3387            DataType::Decimal256(9, 3),
3388        ] {
3389            let input_type = DataType::Float64;
3390            assert!(can_cast_types(&input_type, &output_type));
3391
3392            let array = vec![Some(1.1_f64)];
3393            let array = PrimitiveArray::<Float64Type>::from_iter(array);
3394            let result = cast_with_options(
3395                &array,
3396                &output_type,
3397                &CastOptions {
3398                    safe: false,
3399                    format_options: FormatOptions::default(),
3400                },
3401            );
3402            assert!(
3403                result.is_ok(),
3404                "Failed to cast to {output_type} with: {}",
3405                result.unwrap_err()
3406            );
3407        }
3408    }
3409
3410    #[test]
3411    fn test_cast_float16_to_decimals() {
3412        let array = Float16Array::from(vec![
3413            Some(f16::from_f32(1.25)),
3414            Some(f16::from_f32(-2.5)),
3415            Some(f16::from_f32(1.125)),
3416            Some(f16::from_f32(-1.125)),
3417            Some(f16::from_f32(0.0)),
3418            None,
3419        ]);
3420
3421        generate_cast_test_case!(
3422            &array,
3423            Decimal32Array,
3424            &DataType::Decimal32(9, 2),
3425            vec![
3426                Some(125_i32),
3427                Some(-250_i32),
3428                Some(113_i32),
3429                Some(-113_i32),
3430                Some(0_i32),
3431                None
3432            ]
3433        );
3434        generate_cast_test_case!(
3435            &array,
3436            Decimal64Array,
3437            &DataType::Decimal64(18, 2),
3438            vec![
3439                Some(125_i64),
3440                Some(-250_i64),
3441                Some(113_i64),
3442                Some(-113_i64),
3443                Some(0_i64),
3444                None
3445            ]
3446        );
3447        generate_cast_test_case!(
3448            &array,
3449            Decimal128Array,
3450            &DataType::Decimal128(38, 2),
3451            vec![
3452                Some(125_i128),
3453                Some(-250_i128),
3454                Some(113_i128),
3455                Some(-113_i128),
3456                Some(0_i128),
3457                None
3458            ]
3459        );
3460        generate_cast_test_case!(
3461            &array,
3462            Decimal256Array,
3463            &DataType::Decimal256(76, 2),
3464            vec![
3465                Some(i256::from_i128(125_i128)),
3466                Some(i256::from_i128(-250_i128)),
3467                Some(i256::from_i128(113_i128)),
3468                Some(i256::from_i128(-113_i128)),
3469                Some(i256::from_i128(0_i128)),
3470                None
3471            ]
3472        );
3473
3474        let array = Float16Array::from(vec![
3475            Some(f16::from_f32(1250.0)),
3476            Some(f16::from_f32(-1250.0)),
3477            Some(f16::from_f32(1249.0)),
3478            None,
3479        ]);
3480        generate_cast_test_case!(
3481            &array,
3482            Decimal128Array,
3483            &DataType::Decimal128(5, -2),
3484            vec![Some(13_i128), Some(-13_i128), Some(12_i128), None]
3485        );
3486    }
3487
3488    #[test]
3489    fn test_cast_decimal128_to_decimal128_overflow() {
3490        let input_type = DataType::Decimal128(38, 3);
3491        let output_type = DataType::Decimal128(38, 38);
3492        assert!(can_cast_types(&input_type, &output_type));
3493
3494        let array = vec![Some(i128::MAX)];
3495        let array = create_decimal128_array(array, 38, 3).unwrap();
3496        let result = cast_with_options(
3497            &array,
3498            &output_type,
3499            &CastOptions {
3500                safe: false,
3501                format_options: FormatOptions::default(),
3502            },
3503        );
3504        assert_eq!(
3505            "Cast error: Cannot cast to Decimal128(38, 38). Overflowing on 170141183460469231731687303715884105727",
3506            result.unwrap_err().to_string()
3507        );
3508    }
3509
3510    #[test]
3511    fn test_cast_decimal128_to_decimal256_overflow() {
3512        let input_type = DataType::Decimal128(38, 3);
3513        let output_type = DataType::Decimal256(76, 76);
3514        assert!(can_cast_types(&input_type, &output_type));
3515
3516        let array = vec![Some(i128::MAX)];
3517        let array = create_decimal128_array(array, 38, 3).unwrap();
3518        let result = cast_with_options(
3519            &array,
3520            &output_type,
3521            &CastOptions {
3522                safe: false,
3523                format_options: FormatOptions::default(),
3524            },
3525        );
3526        assert_eq!(
3527            "Cast error: Cannot cast to Decimal256(76, 76). Overflowing on 170141183460469231731687303715884105727",
3528            result.unwrap_err().to_string()
3529        );
3530    }
3531
3532    #[test]
3533    fn test_cast_decimal32_to_decimal256() {
3534        let input_type = DataType::Decimal32(8, 3);
3535        let output_type = DataType::Decimal256(20, 4);
3536        assert!(can_cast_types(&input_type, &output_type));
3537        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3538        let array = create_decimal32_array(array, 8, 3).unwrap();
3539        generate_cast_test_case!(
3540            &array,
3541            Decimal256Array,
3542            &output_type,
3543            vec![
3544                Some(i256::from_i128(11234560_i128)),
3545                Some(i256::from_i128(21234560_i128)),
3546                Some(i256::from_i128(31234560_i128)),
3547                None
3548            ]
3549        );
3550    }
3551    #[test]
3552    fn test_cast_decimal64_to_decimal256() {
3553        let input_type = DataType::Decimal64(12, 3);
3554        let output_type = DataType::Decimal256(20, 4);
3555        assert!(can_cast_types(&input_type, &output_type));
3556        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3557        let array = create_decimal64_array(array, 12, 3).unwrap();
3558        generate_cast_test_case!(
3559            &array,
3560            Decimal256Array,
3561            &output_type,
3562            vec![
3563                Some(i256::from_i128(11234560_i128)),
3564                Some(i256::from_i128(21234560_i128)),
3565                Some(i256::from_i128(31234560_i128)),
3566                None
3567            ]
3568        );
3569    }
3570    #[test]
3571    fn test_cast_decimal128_to_decimal256() {
3572        let input_type = DataType::Decimal128(20, 3);
3573        let output_type = DataType::Decimal256(20, 4);
3574        assert!(can_cast_types(&input_type, &output_type));
3575        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3576        let array = create_decimal128_array(array, 20, 3).unwrap();
3577        generate_cast_test_case!(
3578            &array,
3579            Decimal256Array,
3580            &output_type,
3581            vec![
3582                Some(i256::from_i128(11234560_i128)),
3583                Some(i256::from_i128(21234560_i128)),
3584                Some(i256::from_i128(31234560_i128)),
3585                None
3586            ]
3587        );
3588    }
3589
3590    #[test]
3591    fn test_cast_decimal256_to_decimal128_overflow() {
3592        let input_type = DataType::Decimal256(76, 5);
3593        let output_type = DataType::Decimal128(38, 7);
3594        assert!(can_cast_types(&input_type, &output_type));
3595        let array = vec![Some(i256::from_i128(i128::MAX))];
3596        let array = create_decimal256_array(array, 76, 5).unwrap();
3597        let result = cast_with_options(
3598            &array,
3599            &output_type,
3600            &CastOptions {
3601                safe: false,
3602                format_options: FormatOptions::default(),
3603            },
3604        );
3605        assert_eq!(
3606            "Cast error: Cannot cast to Decimal128(38, 7). Overflowing on 170141183460469231731687303715884105727",
3607            result.unwrap_err().to_string()
3608        );
3609    }
3610
3611    #[test]
3612    fn test_cast_decimal256_to_decimal256_overflow() {
3613        let input_type = DataType::Decimal256(76, 5);
3614        let output_type = DataType::Decimal256(76, 55);
3615        assert!(can_cast_types(&input_type, &output_type));
3616        let array = vec![Some(i256::from_i128(i128::MAX))];
3617        let array = create_decimal256_array(array, 76, 5).unwrap();
3618        let result = cast_with_options(
3619            &array,
3620            &output_type,
3621            &CastOptions {
3622                safe: false,
3623                format_options: FormatOptions::default(),
3624            },
3625        );
3626        assert_eq!(
3627            "Cast error: Cannot cast to Decimal256(76, 55). Overflowing on 170141183460469231731687303715884105727",
3628            result.unwrap_err().to_string()
3629        );
3630    }
3631
3632    #[test]
3633    fn test_cast_decimal256_to_decimal128() {
3634        let input_type = DataType::Decimal256(20, 3);
3635        let output_type = DataType::Decimal128(20, 4);
3636        assert!(can_cast_types(&input_type, &output_type));
3637        let array = vec![
3638            Some(i256::from_i128(1123456)),
3639            Some(i256::from_i128(2123456)),
3640            Some(i256::from_i128(3123456)),
3641            None,
3642        ];
3643        let array = create_decimal256_array(array, 20, 3).unwrap();
3644        generate_cast_test_case!(
3645            &array,
3646            Decimal128Array,
3647            &output_type,
3648            vec![
3649                Some(11234560_i128),
3650                Some(21234560_i128),
3651                Some(31234560_i128),
3652                None
3653            ]
3654        );
3655    }
3656
3657    #[test]
3658    fn test_cast_decimal256_to_decimal256() {
3659        let input_type = DataType::Decimal256(20, 3);
3660        let output_type = DataType::Decimal256(20, 4);
3661        assert!(can_cast_types(&input_type, &output_type));
3662        let array = vec![
3663            Some(i256::from_i128(1123456)),
3664            Some(i256::from_i128(2123456)),
3665            Some(i256::from_i128(3123456)),
3666            None,
3667        ];
3668        let array = create_decimal256_array(array, 20, 3).unwrap();
3669        generate_cast_test_case!(
3670            &array,
3671            Decimal256Array,
3672            &output_type,
3673            vec![
3674                Some(i256::from_i128(11234560_i128)),
3675                Some(i256::from_i128(21234560_i128)),
3676                Some(i256::from_i128(31234560_i128)),
3677                None
3678            ]
3679        );
3680    }
3681
3682    fn generate_decimal_to_numeric_cast_test_case<T>(array: &PrimitiveArray<T>)
3683    where
3684        T: ArrowPrimitiveType + DecimalType,
3685    {
3686        // u8
3687        generate_cast_test_case!(
3688            array,
3689            UInt8Array,
3690            &DataType::UInt8,
3691            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3692        );
3693        // u16
3694        generate_cast_test_case!(
3695            array,
3696            UInt16Array,
3697            &DataType::UInt16,
3698            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3699        );
3700        // u32
3701        generate_cast_test_case!(
3702            array,
3703            UInt32Array,
3704            &DataType::UInt32,
3705            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3706        );
3707        // u64
3708        generate_cast_test_case!(
3709            array,
3710            UInt64Array,
3711            &DataType::UInt64,
3712            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3713        );
3714        // i8
3715        generate_cast_test_case!(
3716            array,
3717            Int8Array,
3718            &DataType::Int8,
3719            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3720        );
3721        // i16
3722        generate_cast_test_case!(
3723            array,
3724            Int16Array,
3725            &DataType::Int16,
3726            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3727        );
3728        // i32
3729        generate_cast_test_case!(
3730            array,
3731            Int32Array,
3732            &DataType::Int32,
3733            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3734        );
3735        // i64
3736        generate_cast_test_case!(
3737            array,
3738            Int64Array,
3739            &DataType::Int64,
3740            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3741        );
3742        // f16
3743        generate_cast_test_case!(
3744            array,
3745            Float16Array,
3746            &DataType::Float16,
3747            vec![
3748                Some(f16::from_f32(1.25)),
3749                Some(f16::from_f32(2.25)),
3750                Some(f16::from_f32(3.25)),
3751                None,
3752                Some(f16::from_f32(5.25))
3753            ]
3754        );
3755        // f32
3756        generate_cast_test_case!(
3757            array,
3758            Float32Array,
3759            &DataType::Float32,
3760            vec![
3761                Some(1.25_f32),
3762                Some(2.25_f32),
3763                Some(3.25_f32),
3764                None,
3765                Some(5.25_f32)
3766            ]
3767        );
3768        // f64
3769        generate_cast_test_case!(
3770            array,
3771            Float64Array,
3772            &DataType::Float64,
3773            vec![
3774                Some(1.25_f64),
3775                Some(2.25_f64),
3776                Some(3.25_f64),
3777                None,
3778                Some(5.25_f64)
3779            ]
3780        );
3781    }
3782
3783    #[test]
3784    fn test_cast_decimal32_to_numeric() {
3785        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3786        let array = create_decimal32_array(value_array, 8, 2).unwrap();
3787
3788        generate_decimal_to_numeric_cast_test_case(&array);
3789    }
3790
3791    #[test]
3792    fn test_cast_decimal64_to_numeric() {
3793        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3794        let array = create_decimal64_array(value_array, 8, 2).unwrap();
3795
3796        generate_decimal_to_numeric_cast_test_case(&array);
3797    }
3798
3799    #[test]
3800    fn test_cast_decimal128_to_numeric() {
3801        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3802        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3803
3804        generate_decimal_to_numeric_cast_test_case(&array);
3805
3806        // overflow test: out of range of max u8
3807        let value_array: Vec<Option<i128>> = vec![Some(51300)];
3808        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3809        let casted_array = cast_with_options(
3810            &array,
3811            &DataType::UInt8,
3812            &CastOptions {
3813                safe: false,
3814                format_options: FormatOptions::default(),
3815            },
3816        );
3817        assert_eq!(
3818            "Cast error: value of 513 is out of range UInt8".to_string(),
3819            casted_array.unwrap_err().to_string()
3820        );
3821
3822        let casted_array = cast_with_options(
3823            &array,
3824            &DataType::UInt8,
3825            &CastOptions {
3826                safe: true,
3827                format_options: FormatOptions::default(),
3828            },
3829        );
3830        assert!(casted_array.is_ok());
3831        assert!(casted_array.unwrap().is_null(0));
3832
3833        // overflow test: out of range of max i8
3834        let value_array: Vec<Option<i128>> = vec![Some(24400)];
3835        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3836        let casted_array = cast_with_options(
3837            &array,
3838            &DataType::Int8,
3839            &CastOptions {
3840                safe: false,
3841                format_options: FormatOptions::default(),
3842            },
3843        );
3844        assert_eq!(
3845            "Cast error: value of 244 is out of range Int8".to_string(),
3846            casted_array.unwrap_err().to_string()
3847        );
3848
3849        let casted_array = cast_with_options(
3850            &array,
3851            &DataType::Int8,
3852            &CastOptions {
3853                safe: true,
3854                format_options: FormatOptions::default(),
3855            },
3856        );
3857        assert!(casted_array.is_ok());
3858        assert!(casted_array.unwrap().is_null(0));
3859
3860        // loss the precision: convert decimal to f32、f64
3861        // f32
3862        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3863        let value_array: Vec<Option<i128>> = vec![
3864            Some(125),
3865            Some(225),
3866            Some(325),
3867            None,
3868            Some(525),
3869            Some(112345678),
3870            Some(112345679),
3871        ];
3872        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3873        generate_cast_test_case!(
3874            &array,
3875            Float32Array,
3876            &DataType::Float32,
3877            vec![
3878                Some(1.25_f32),
3879                Some(2.25_f32),
3880                Some(3.25_f32),
3881                None,
3882                Some(5.25_f32),
3883                Some(1_123_456.7_f32),
3884                Some(1_123_456.7_f32)
3885            ]
3886        );
3887
3888        // f64
3889        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3890        let value_array: Vec<Option<i128>> = vec![
3891            Some(125),
3892            Some(225),
3893            Some(325),
3894            None,
3895            Some(525),
3896            Some(112345678901234568),
3897            Some(112345678901234560),
3898        ];
3899        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3900        generate_cast_test_case!(
3901            &array,
3902            Float64Array,
3903            &DataType::Float64,
3904            vec![
3905                Some(1.25_f64),
3906                Some(2.25_f64),
3907                Some(3.25_f64),
3908                None,
3909                Some(5.25_f64),
3910                Some(1_123_456_789_012_345.6_f64),
3911                Some(1_123_456_789_012_345.6_f64),
3912            ]
3913        );
3914    }
3915
3916    #[test]
3917    fn test_cast_decimal256_to_numeric() {
3918        let value_array: Vec<Option<i256>> = vec![
3919            Some(i256::from_i128(125)),
3920            Some(i256::from_i128(225)),
3921            Some(i256::from_i128(325)),
3922            None,
3923            Some(i256::from_i128(525)),
3924        ];
3925        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3926        // u8
3927        generate_cast_test_case!(
3928            &array,
3929            UInt8Array,
3930            &DataType::UInt8,
3931            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3932        );
3933        // u16
3934        generate_cast_test_case!(
3935            &array,
3936            UInt16Array,
3937            &DataType::UInt16,
3938            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3939        );
3940        // u32
3941        generate_cast_test_case!(
3942            &array,
3943            UInt32Array,
3944            &DataType::UInt32,
3945            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3946        );
3947        // u64
3948        generate_cast_test_case!(
3949            &array,
3950            UInt64Array,
3951            &DataType::UInt64,
3952            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3953        );
3954        // i8
3955        generate_cast_test_case!(
3956            &array,
3957            Int8Array,
3958            &DataType::Int8,
3959            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3960        );
3961        // i16
3962        generate_cast_test_case!(
3963            &array,
3964            Int16Array,
3965            &DataType::Int16,
3966            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3967        );
3968        // i32
3969        generate_cast_test_case!(
3970            &array,
3971            Int32Array,
3972            &DataType::Int32,
3973            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3974        );
3975        // i64
3976        generate_cast_test_case!(
3977            &array,
3978            Int64Array,
3979            &DataType::Int64,
3980            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3981        );
3982        // f16
3983        generate_cast_test_case!(
3984            &array,
3985            Float16Array,
3986            &DataType::Float16,
3987            vec![
3988                Some(f16::from_f32(1.25)),
3989                Some(f16::from_f32(2.25)),
3990                Some(f16::from_f32(3.25)),
3991                None,
3992                Some(f16::from_f32(5.25))
3993            ]
3994        );
3995        // f32
3996        generate_cast_test_case!(
3997            &array,
3998            Float32Array,
3999            &DataType::Float32,
4000            vec![
4001                Some(1.25_f32),
4002                Some(2.25_f32),
4003                Some(3.25_f32),
4004                None,
4005                Some(5.25_f32)
4006            ]
4007        );
4008        // f64
4009        generate_cast_test_case!(
4010            &array,
4011            Float64Array,
4012            &DataType::Float64,
4013            vec![
4014                Some(1.25_f64),
4015                Some(2.25_f64),
4016                Some(3.25_f64),
4017                None,
4018                Some(5.25_f64)
4019            ]
4020        );
4021
4022        // overflow test: out of range of max i8
4023        let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128(24400))];
4024        let array = create_decimal256_array(value_array, 38, 2).unwrap();
4025        let casted_array = cast_with_options(
4026            &array,
4027            &DataType::Int8,
4028            &CastOptions {
4029                safe: false,
4030                format_options: FormatOptions::default(),
4031            },
4032        );
4033        assert_eq!(
4034            "Cast error: value of 244 is out of range Int8".to_string(),
4035            casted_array.unwrap_err().to_string()
4036        );
4037
4038        let casted_array = cast_with_options(
4039            &array,
4040            &DataType::Int8,
4041            &CastOptions {
4042                safe: true,
4043                format_options: FormatOptions::default(),
4044            },
4045        );
4046        assert!(casted_array.is_ok());
4047        assert!(casted_array.unwrap().is_null(0));
4048
4049        // loss the precision: convert decimal to f32、f64
4050        // f32
4051        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
4052        let value_array: Vec<Option<i256>> = vec![
4053            Some(i256::from_i128(125)),
4054            Some(i256::from_i128(225)),
4055            Some(i256::from_i128(325)),
4056            None,
4057            Some(i256::from_i128(525)),
4058            Some(i256::from_i128(112345678)),
4059            Some(i256::from_i128(112345679)),
4060        ];
4061        let array = create_decimal256_array(value_array, 76, 2).unwrap();
4062        generate_cast_test_case!(
4063            &array,
4064            Float32Array,
4065            &DataType::Float32,
4066            vec![
4067                Some(1.25_f32),
4068                Some(2.25_f32),
4069                Some(3.25_f32),
4070                None,
4071                Some(5.25_f32),
4072                Some(1_123_456.7_f32),
4073                Some(1_123_456.7_f32)
4074            ]
4075        );
4076
4077        // f64
4078        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
4079        let value_array: Vec<Option<i256>> = vec![
4080            Some(i256::from_i128(125)),
4081            Some(i256::from_i128(225)),
4082            Some(i256::from_i128(325)),
4083            None,
4084            Some(i256::from_i128(525)),
4085            Some(i256::from_i128(112345678901234568)),
4086            Some(i256::from_i128(112345678901234560)),
4087        ];
4088        let array = create_decimal256_array(value_array, 76, 2).unwrap();
4089        generate_cast_test_case!(
4090            &array,
4091            Float64Array,
4092            &DataType::Float64,
4093            vec![
4094                Some(1.25_f64),
4095                Some(2.25_f64),
4096                Some(3.25_f64),
4097                None,
4098                Some(5.25_f64),
4099                Some(1_123_456_789_012_345.6_f64),
4100                Some(1_123_456_789_012_345.6_f64),
4101            ]
4102        );
4103    }
4104
4105    #[test]
4106    fn test_cast_decimal128_to_float16_overflow() {
4107        let array = create_decimal128_array(
4108            vec![
4109                Some(6_550_400_i128),
4110                Some(100_000_000_i128),
4111                Some(-100_000_000_i128),
4112                None,
4113            ],
4114            10,
4115            2,
4116        )
4117        .unwrap();
4118
4119        generate_cast_test_case!(
4120            &array,
4121            Float16Array,
4122            &DataType::Float16,
4123            vec![
4124                Some(f16::from_f64(65504.0)),
4125                Some(f16::INFINITY),
4126                Some(f16::NEG_INFINITY),
4127                None
4128            ]
4129        );
4130    }
4131
4132    #[test]
4133    fn test_cast_decimal256_to_float16_overflow() {
4134        let array = create_decimal256_array(
4135            vec![
4136                Some(i256::from_i128(6_550_400_i128)),
4137                Some(i256::from_i128(100_000_000_i128)),
4138                Some(i256::from_i128(-100_000_000_i128)),
4139                None,
4140            ],
4141            10,
4142            2,
4143        )
4144        .unwrap();
4145
4146        generate_cast_test_case!(
4147            &array,
4148            Float16Array,
4149            &DataType::Float16,
4150            vec![
4151                Some(f16::from_f64(65504.0)),
4152                Some(f16::INFINITY),
4153                Some(f16::NEG_INFINITY),
4154                None
4155            ]
4156        );
4157    }
4158
4159    #[test]
4160    fn test_cast_decimal_to_numeric_negative_scale() {
4161        let value_array: Vec<Option<i256>> = vec![
4162            Some(i256::from_i128(125)),
4163            Some(i256::from_i128(225)),
4164            Some(i256::from_i128(325)),
4165            None,
4166            Some(i256::from_i128(525)),
4167        ];
4168        let array = create_decimal256_array(value_array, 38, -1).unwrap();
4169
4170        generate_cast_test_case!(
4171            &array,
4172            Int64Array,
4173            &DataType::Int64,
4174            vec![Some(1_250), Some(2_250), Some(3_250), None, Some(5_250)]
4175        );
4176
4177        let value_array: Vec<Option<i128>> = vec![Some(12), Some(-12), None];
4178        let array = create_decimal128_array(value_array, 10, -2).unwrap();
4179        generate_cast_test_case!(
4180            &array,
4181            Float16Array,
4182            &DataType::Float16,
4183            vec![
4184                Some(f16::from_f32(1200.0)),
4185                Some(f16::from_f32(-1200.0)),
4186                None
4187            ]
4188        );
4189
4190        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4191        let array = create_decimal32_array(value_array, 8, -2).unwrap();
4192        generate_cast_test_case!(
4193            &array,
4194            Int64Array,
4195            &DataType::Int64,
4196            vec![Some(12_500), Some(22_500), Some(32_500), None, Some(52_500)]
4197        );
4198
4199        let value_array: Vec<Option<i32>> = vec![Some(2), Some(1), None];
4200        let array = create_decimal32_array(value_array, 9, -9).unwrap();
4201        generate_cast_test_case!(
4202            &array,
4203            Int64Array,
4204            &DataType::Int64,
4205            vec![Some(2_000_000_000), Some(1_000_000_000), None]
4206        );
4207
4208        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4209        let array = create_decimal64_array(value_array, 18, -3).unwrap();
4210        generate_cast_test_case!(
4211            &array,
4212            Int64Array,
4213            &DataType::Int64,
4214            vec![
4215                Some(125_000),
4216                Some(225_000),
4217                Some(325_000),
4218                None,
4219                Some(525_000)
4220            ]
4221        );
4222
4223        let value_array: Vec<Option<i64>> = vec![Some(12), Some(34), None];
4224        let array = create_decimal64_array(value_array, 18, -10).unwrap();
4225        generate_cast_test_case!(
4226            &array,
4227            Int64Array,
4228            &DataType::Int64,
4229            vec![Some(120_000_000_000), Some(340_000_000_000), None]
4230        );
4231
4232        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4233        let array = create_decimal128_array(value_array, 38, -4).unwrap();
4234        generate_cast_test_case!(
4235            &array,
4236            Int64Array,
4237            &DataType::Int64,
4238            vec![
4239                Some(1_250_000),
4240                Some(2_250_000),
4241                Some(3_250_000),
4242                None,
4243                Some(5_250_000)
4244            ]
4245        );
4246
4247        let value_array: Vec<Option<i128>> = vec![Some(9), Some(1), None];
4248        let array = create_decimal128_array(value_array, 38, -18).unwrap();
4249        generate_cast_test_case!(
4250            &array,
4251            Int64Array,
4252            &DataType::Int64,
4253            vec![
4254                Some(9_000_000_000_000_000_000),
4255                Some(1_000_000_000_000_000_000),
4256                None
4257            ]
4258        );
4259
4260        let array = create_decimal32_array(vec![Some(999_999_999)], 9, -1).unwrap();
4261        let casted_array = cast_with_options(
4262            &array,
4263            &DataType::Int64,
4264            &CastOptions {
4265                safe: false,
4266                format_options: FormatOptions::default(),
4267            },
4268        );
4269        assert_eq!(
4270            "Arithmetic overflow: Overflow happened on: 999999999 * 10".to_string(),
4271            casted_array.unwrap_err().to_string()
4272        );
4273
4274        let casted_array = cast_with_options(
4275            &array,
4276            &DataType::Int64,
4277            &CastOptions {
4278                safe: true,
4279                format_options: FormatOptions::default(),
4280            },
4281        );
4282        assert!(casted_array.is_ok());
4283        assert!(casted_array.unwrap().is_null(0));
4284
4285        let array = create_decimal64_array(vec![Some(13)], 18, -1).unwrap();
4286        let casted_array = cast_with_options(
4287            &array,
4288            &DataType::Int8,
4289            &CastOptions {
4290                safe: false,
4291                format_options: FormatOptions::default(),
4292            },
4293        );
4294        assert_eq!(
4295            "Cast error: value of 130 is out of range Int8".to_string(),
4296            casted_array.unwrap_err().to_string()
4297        );
4298
4299        let casted_array = cast_with_options(
4300            &array,
4301            &DataType::Int8,
4302            &CastOptions {
4303                safe: true,
4304                format_options: FormatOptions::default(),
4305            },
4306        );
4307        assert!(casted_array.is_ok());
4308        assert!(casted_array.unwrap().is_null(0));
4309    }
4310
4311    #[test]
4312    fn test_cast_numeric_to_decimal128() {
4313        let decimal_type = DataType::Decimal128(38, 6);
4314        // u8, u16, u32, u64
4315        let input_datas = vec![
4316            Arc::new(UInt8Array::from(vec![
4317                Some(1),
4318                Some(2),
4319                Some(3),
4320                None,
4321                Some(5),
4322            ])) as ArrayRef, // u8
4323            Arc::new(UInt16Array::from(vec![
4324                Some(1),
4325                Some(2),
4326                Some(3),
4327                None,
4328                Some(5),
4329            ])) as ArrayRef, // u16
4330            Arc::new(UInt32Array::from(vec![
4331                Some(1),
4332                Some(2),
4333                Some(3),
4334                None,
4335                Some(5),
4336            ])) as ArrayRef, // u32
4337            Arc::new(UInt64Array::from(vec![
4338                Some(1),
4339                Some(2),
4340                Some(3),
4341                None,
4342                Some(5),
4343            ])) as ArrayRef, // u64
4344        ];
4345
4346        for array in input_datas {
4347            generate_cast_test_case!(
4348                &array,
4349                Decimal128Array,
4350                &decimal_type,
4351                vec![
4352                    Some(1000000_i128),
4353                    Some(2000000_i128),
4354                    Some(3000000_i128),
4355                    None,
4356                    Some(5000000_i128)
4357                ]
4358            );
4359        }
4360
4361        // i8, i16, i32, i64
4362        let input_datas = vec![
4363            Arc::new(Int8Array::from(vec![
4364                Some(1),
4365                Some(2),
4366                Some(3),
4367                None,
4368                Some(5),
4369            ])) as ArrayRef, // i8
4370            Arc::new(Int16Array::from(vec![
4371                Some(1),
4372                Some(2),
4373                Some(3),
4374                None,
4375                Some(5),
4376            ])) as ArrayRef, // i16
4377            Arc::new(Int32Array::from(vec![
4378                Some(1),
4379                Some(2),
4380                Some(3),
4381                None,
4382                Some(5),
4383            ])) as ArrayRef, // i32
4384            Arc::new(Int64Array::from(vec![
4385                Some(1),
4386                Some(2),
4387                Some(3),
4388                None,
4389                Some(5),
4390            ])) as ArrayRef, // i64
4391        ];
4392        for array in input_datas {
4393            generate_cast_test_case!(
4394                &array,
4395                Decimal128Array,
4396                &decimal_type,
4397                vec![
4398                    Some(1000000_i128),
4399                    Some(2000000_i128),
4400                    Some(3000000_i128),
4401                    None,
4402                    Some(5000000_i128)
4403                ]
4404            );
4405        }
4406
4407        // test u8 to decimal type with overflow the result type
4408        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4409        let array = UInt8Array::from(vec![1, 2, 3, 4, 100]);
4410        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4411        assert!(casted_array.is_ok());
4412        let array = casted_array.unwrap();
4413        let array: &Decimal128Array = array.as_primitive();
4414        assert!(array.is_null(4));
4415
4416        // test i8 to decimal type with overflow the result type
4417        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4418        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4419        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4420        assert!(casted_array.is_ok());
4421        let array = casted_array.unwrap();
4422        let array: &Decimal128Array = array.as_primitive();
4423        assert!(array.is_null(4));
4424
4425        // test f32 to decimal type
4426        let array = Float32Array::from(vec![
4427            Some(1.1),
4428            Some(2.2),
4429            Some(4.4),
4430            None,
4431            Some(1.123_456_4), // round down
4432            Some(1.123_456_7), // round up
4433        ]);
4434        let array = Arc::new(array) as ArrayRef;
4435        generate_cast_test_case!(
4436            &array,
4437            Decimal128Array,
4438            &decimal_type,
4439            vec![
4440                Some(1100000_i128),
4441                Some(2200000_i128),
4442                Some(4400000_i128),
4443                None,
4444                Some(1123456_i128), // round down
4445                Some(1123457_i128), // round up
4446            ]
4447        );
4448
4449        // test f64 to decimal type
4450        let array = Float64Array::from(vec![
4451            Some(1.1),
4452            Some(2.2),
4453            Some(4.4),
4454            None,
4455            Some(1.123_456_489_123_4),     // round up
4456            Some(1.123_456_789_123_4),     // round up
4457            Some(1.123_456_489_012_345_6), // round down
4458            Some(1.123_456_789_012_345_6), // round up
4459        ]);
4460        generate_cast_test_case!(
4461            &array,
4462            Decimal128Array,
4463            &decimal_type,
4464            vec![
4465                Some(1100000_i128),
4466                Some(2200000_i128),
4467                Some(4400000_i128),
4468                None,
4469                Some(1123456_i128), // round down
4470                Some(1123457_i128), // round up
4471                Some(1123456_i128), // round down
4472                Some(1123457_i128), // round up
4473            ]
4474        );
4475    }
4476
4477    #[test]
4478    fn test_cast_numeric_to_decimal256() {
4479        let decimal_type = DataType::Decimal256(76, 6);
4480        // u8, u16, u32, u64
4481        let input_datas = vec![
4482            Arc::new(UInt8Array::from(vec![
4483                Some(1),
4484                Some(2),
4485                Some(3),
4486                None,
4487                Some(5),
4488            ])) as ArrayRef, // u8
4489            Arc::new(UInt16Array::from(vec![
4490                Some(1),
4491                Some(2),
4492                Some(3),
4493                None,
4494                Some(5),
4495            ])) as ArrayRef, // u16
4496            Arc::new(UInt32Array::from(vec![
4497                Some(1),
4498                Some(2),
4499                Some(3),
4500                None,
4501                Some(5),
4502            ])) as ArrayRef, // u32
4503            Arc::new(UInt64Array::from(vec![
4504                Some(1),
4505                Some(2),
4506                Some(3),
4507                None,
4508                Some(5),
4509            ])) as ArrayRef, // u64
4510        ];
4511
4512        for array in input_datas {
4513            generate_cast_test_case!(
4514                &array,
4515                Decimal256Array,
4516                &decimal_type,
4517                vec![
4518                    Some(i256::from_i128(1000000_i128)),
4519                    Some(i256::from_i128(2000000_i128)),
4520                    Some(i256::from_i128(3000000_i128)),
4521                    None,
4522                    Some(i256::from_i128(5000000_i128))
4523                ]
4524            );
4525        }
4526
4527        // i8, i16, i32, i64
4528        let input_datas = vec![
4529            Arc::new(Int8Array::from(vec![
4530                Some(1),
4531                Some(2),
4532                Some(3),
4533                None,
4534                Some(5),
4535            ])) as ArrayRef, // i8
4536            Arc::new(Int16Array::from(vec![
4537                Some(1),
4538                Some(2),
4539                Some(3),
4540                None,
4541                Some(5),
4542            ])) as ArrayRef, // i16
4543            Arc::new(Int32Array::from(vec![
4544                Some(1),
4545                Some(2),
4546                Some(3),
4547                None,
4548                Some(5),
4549            ])) as ArrayRef, // i32
4550            Arc::new(Int64Array::from(vec![
4551                Some(1),
4552                Some(2),
4553                Some(3),
4554                None,
4555                Some(5),
4556            ])) as ArrayRef, // i64
4557        ];
4558        for array in input_datas {
4559            generate_cast_test_case!(
4560                &array,
4561                Decimal256Array,
4562                &decimal_type,
4563                vec![
4564                    Some(i256::from_i128(1000000_i128)),
4565                    Some(i256::from_i128(2000000_i128)),
4566                    Some(i256::from_i128(3000000_i128)),
4567                    None,
4568                    Some(i256::from_i128(5000000_i128))
4569                ]
4570            );
4571        }
4572
4573        // test i8 to decimal type with overflow the result type
4574        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4575        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4576        let array = Arc::new(array) as ArrayRef;
4577        let casted_array = cast(&array, &DataType::Decimal256(3, 1));
4578        assert!(casted_array.is_ok());
4579        let array = casted_array.unwrap();
4580        let array: &Decimal256Array = array.as_primitive();
4581        assert!(array.is_null(4));
4582
4583        // test f32 to decimal type
4584        let array = Float32Array::from(vec![
4585            Some(1.1),
4586            Some(2.2),
4587            Some(4.4),
4588            None,
4589            Some(1.123_456_4), // round down
4590            Some(1.123_456_7), // round up
4591        ]);
4592        generate_cast_test_case!(
4593            &array,
4594            Decimal256Array,
4595            &decimal_type,
4596            vec![
4597                Some(i256::from_i128(1100000_i128)),
4598                Some(i256::from_i128(2200000_i128)),
4599                Some(i256::from_i128(4400000_i128)),
4600                None,
4601                Some(i256::from_i128(1123456_i128)), // round down
4602                Some(i256::from_i128(1123457_i128)), // round up
4603            ]
4604        );
4605
4606        // test f64 to decimal type
4607        let array = Float64Array::from(vec![
4608            Some(1.1),
4609            Some(2.2),
4610            Some(4.4),
4611            None,
4612            Some(1.123_456_489_123_4),     // round down
4613            Some(1.123_456_789_123_4),     // round up
4614            Some(1.123_456_489_012_345_6), // round down
4615            Some(1.123_456_789_012_345_6), // round up
4616        ]);
4617        generate_cast_test_case!(
4618            &array,
4619            Decimal256Array,
4620            &decimal_type,
4621            vec![
4622                Some(i256::from_i128(1100000_i128)),
4623                Some(i256::from_i128(2200000_i128)),
4624                Some(i256::from_i128(4400000_i128)),
4625                None,
4626                Some(i256::from_i128(1123456_i128)), // round down
4627                Some(i256::from_i128(1123457_i128)), // round up
4628                Some(i256::from_i128(1123456_i128)), // round down
4629                Some(i256::from_i128(1123457_i128)), // round up
4630            ]
4631        );
4632    }
4633
4634    #[test]
4635    fn test_cast_i32_to_f64() {
4636        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4637        let b = cast(&array, &DataType::Float64).unwrap();
4638        let c = b.as_primitive::<Float64Type>();
4639        assert_eq!(5.0, c.value(0));
4640        assert_eq!(6.0, c.value(1));
4641        assert_eq!(7.0, c.value(2));
4642        assert_eq!(8.0, c.value(3));
4643        assert_eq!(9.0, c.value(4));
4644    }
4645
4646    #[test]
4647    fn test_cast_i32_to_u8() {
4648        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4649        let b = cast(&array, &DataType::UInt8).unwrap();
4650        let c = b.as_primitive::<UInt8Type>();
4651        assert!(!c.is_valid(0));
4652        assert_eq!(6, c.value(1));
4653        assert!(!c.is_valid(2));
4654        assert_eq!(8, c.value(3));
4655        // overflows return None
4656        assert!(!c.is_valid(4));
4657    }
4658
4659    #[test]
4660    #[should_panic(expected = "Can't cast value -5 to type UInt8")]
4661    fn test_cast_int32_to_u8_with_error() {
4662        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4663        // overflow with the error
4664        let cast_option = CastOptions {
4665            safe: false,
4666            format_options: FormatOptions::default(),
4667        };
4668        let result = cast_with_options(&array, &DataType::UInt8, &cast_option);
4669        assert!(result.is_err());
4670        result.unwrap();
4671    }
4672
4673    #[test]
4674    fn test_cast_i32_to_u8_sliced() {
4675        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4676        assert_eq!(0, array.offset());
4677        let array = array.slice(2, 3);
4678        let b = cast(&array, &DataType::UInt8).unwrap();
4679        assert_eq!(3, b.len());
4680        let c = b.as_primitive::<UInt8Type>();
4681        assert!(!c.is_valid(0));
4682        assert_eq!(8, c.value(1));
4683        // overflows return None
4684        assert!(!c.is_valid(2));
4685    }
4686
4687    #[test]
4688    fn test_cast_i32_to_i32() {
4689        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4690        let b = cast(&array, &DataType::Int32).unwrap();
4691        let c = b.as_primitive::<Int32Type>();
4692        assert_eq!(5, c.value(0));
4693        assert_eq!(6, c.value(1));
4694        assert_eq!(7, c.value(2));
4695        assert_eq!(8, c.value(3));
4696        assert_eq!(9, c.value(4));
4697    }
4698
4699    #[test]
4700    fn test_cast_i32_to_list_i32() {
4701        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4702        let b = cast(
4703            &array,
4704            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4705        )
4706        .unwrap();
4707        assert_eq!(5, b.len());
4708        let arr = b.as_list::<i32>();
4709        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4710        assert_eq!(1, arr.value_length(0));
4711        assert_eq!(1, arr.value_length(1));
4712        assert_eq!(1, arr.value_length(2));
4713        assert_eq!(1, arr.value_length(3));
4714        assert_eq!(1, arr.value_length(4));
4715        let c = arr.values().as_primitive::<Int32Type>();
4716        assert_eq!(5, c.value(0));
4717        assert_eq!(6, c.value(1));
4718        assert_eq!(7, c.value(2));
4719        assert_eq!(8, c.value(3));
4720        assert_eq!(9, c.value(4));
4721    }
4722
4723    #[test]
4724    fn test_cast_i32_to_list_i32_nullable() {
4725        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), Some(9)]);
4726        let b = cast(
4727            &array,
4728            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4729        )
4730        .unwrap();
4731        assert_eq!(5, b.len());
4732        assert_eq!(0, b.null_count());
4733        let arr = b.as_list::<i32>();
4734        assert_eq!(&[0, 1, 2, 3, 4, 5], 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        assert_eq!(1, arr.value_length(4));
4740
4741        let c = arr.values().as_primitive::<Int32Type>();
4742        assert_eq!(1, c.null_count());
4743        assert_eq!(5, c.value(0));
4744        assert!(!c.is_valid(1));
4745        assert_eq!(7, c.value(2));
4746        assert_eq!(8, c.value(3));
4747        assert_eq!(9, c.value(4));
4748    }
4749
4750    #[test]
4751    fn test_cast_i32_to_list_f64_nullable_sliced() {
4752        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), None, Some(10)]);
4753        let array = array.slice(2, 4);
4754        let b = cast(
4755            &array,
4756            &DataType::List(Arc::new(Field::new_list_field(DataType::Float64, true))),
4757        )
4758        .unwrap();
4759        assert_eq!(4, b.len());
4760        assert_eq!(0, b.null_count());
4761        let arr = b.as_list::<i32>();
4762        assert_eq!(&[0, 1, 2, 3, 4], arr.value_offsets());
4763        assert_eq!(1, arr.value_length(0));
4764        assert_eq!(1, arr.value_length(1));
4765        assert_eq!(1, arr.value_length(2));
4766        assert_eq!(1, arr.value_length(3));
4767        let c = arr.values().as_primitive::<Float64Type>();
4768        assert_eq!(1, c.null_count());
4769        assert_eq!(7.0, c.value(0));
4770        assert_eq!(8.0, c.value(1));
4771        assert!(!c.is_valid(2));
4772        assert_eq!(10.0, c.value(3));
4773    }
4774
4775    #[test]
4776    fn test_cast_int_to_utf8view() {
4777        let inputs = vec![
4778            Arc::new(Int8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4779            Arc::new(Int16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4780            Arc::new(Int32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4781            Arc::new(Int64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4782            Arc::new(UInt8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4783            Arc::new(UInt16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4784            Arc::new(UInt32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4785            Arc::new(UInt64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4786        ];
4787        let expected: ArrayRef = Arc::new(StringViewArray::from(vec![
4788            None,
4789            Some("8"),
4790            Some("9"),
4791            Some("10"),
4792        ]));
4793
4794        for array in inputs {
4795            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4796            let arr = cast(&array, &DataType::Utf8View).unwrap();
4797            assert_eq!(expected.as_ref(), arr.as_ref());
4798        }
4799    }
4800
4801    #[test]
4802    fn test_cast_float_to_utf8view() {
4803        let inputs = vec![
4804            Arc::new(Float16Array::from(vec![
4805                Some(f16::from_f64(1.5)),
4806                Some(f16::from_f64(2.5)),
4807                None,
4808            ])) as ArrayRef,
4809            Arc::new(Float32Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4810            Arc::new(Float64Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4811        ];
4812
4813        let expected: ArrayRef =
4814            Arc::new(StringViewArray::from(vec![Some("1.5"), Some("2.5"), None]));
4815
4816        for array in inputs {
4817            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4818            let arr = cast(&array, &DataType::Utf8View).unwrap();
4819            assert_eq!(expected.as_ref(), arr.as_ref());
4820        }
4821    }
4822
4823    #[test]
4824    fn test_cast_utf8_to_i32() {
4825        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4826        let b = cast(&array, &DataType::Int32).unwrap();
4827        let c = b.as_primitive::<Int32Type>();
4828        assert_eq!(5, c.value(0));
4829        assert_eq!(6, c.value(1));
4830        assert!(!c.is_valid(2));
4831        assert_eq!(8, c.value(3));
4832        assert!(!c.is_valid(4));
4833    }
4834
4835    #[test]
4836    fn test_cast_utf8view_to_i32() {
4837        let array = StringViewArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4838        let b = cast(&array, &DataType::Int32).unwrap();
4839        let c = b.as_primitive::<Int32Type>();
4840        assert_eq!(5, c.value(0));
4841        assert_eq!(6, c.value(1));
4842        assert!(!c.is_valid(2));
4843        assert_eq!(8, c.value(3));
4844        assert!(!c.is_valid(4));
4845    }
4846
4847    #[test]
4848    fn test_cast_utf8view_to_f32() {
4849        let array = StringViewArray::from(vec!["3", "4.56", "seven", "8.9"]);
4850        let b = cast(&array, &DataType::Float32).unwrap();
4851        let c = b.as_primitive::<Float32Type>();
4852        assert_eq!(3.0, c.value(0));
4853        assert_eq!(4.56, c.value(1));
4854        assert!(!c.is_valid(2));
4855        assert_eq!(8.9, c.value(3));
4856    }
4857
4858    #[test]
4859    fn test_cast_string_to_f16() {
4860        let arrays = [
4861            Arc::new(StringViewArray::from(vec!["3", "4.56", "seven", "8.9"])) as ArrayRef,
4862            Arc::new(StringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4863            Arc::new(LargeStringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4864        ];
4865        for array in arrays {
4866            let b = cast(&array, &DataType::Float16).unwrap();
4867            let c = b.as_primitive::<Float16Type>();
4868            assert_eq!(half::f16::from_f32(3.0), c.value(0));
4869            assert_eq!(half::f16::from_f32(4.56), c.value(1));
4870            assert!(!c.is_valid(2));
4871            assert_eq!(half::f16::from_f32(8.9), c.value(3));
4872        }
4873    }
4874
4875    #[test]
4876    fn test_cast_utf8view_to_decimal128() {
4877        let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
4878        let arr = Arc::new(array) as ArrayRef;
4879        generate_cast_test_case!(
4880            &arr,
4881            Decimal128Array,
4882            &DataType::Decimal128(4, 2),
4883            vec![None, Some(400_i128), Some(560_i128), Some(789_i128)]
4884        );
4885    }
4886
4887    #[test]
4888    fn test_cast_with_options_utf8_to_i32() {
4889        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4890        let result = cast_with_options(
4891            &array,
4892            &DataType::Int32,
4893            &CastOptions {
4894                safe: false,
4895                format_options: FormatOptions::default(),
4896            },
4897        );
4898        match result {
4899            Ok(_) => panic!("expected error"),
4900            Err(e) => {
4901                assert!(
4902                    e.to_string()
4903                        .contains("Cast error: Cannot cast string 'seven' to value of Int32 type",),
4904                    "Error: {e}"
4905                )
4906            }
4907        }
4908    }
4909
4910    #[test]
4911    fn test_cast_utf8_to_bool() {
4912        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4913        let casted = cast(&strings, &DataType::Boolean).unwrap();
4914        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4915        assert_eq!(*as_boolean_array(&casted), expected);
4916    }
4917
4918    #[test]
4919    fn test_cast_utf8view_to_bool() {
4920        let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4921        let casted = cast(&strings, &DataType::Boolean).unwrap();
4922        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4923        assert_eq!(*as_boolean_array(&casted), expected);
4924    }
4925
4926    #[test]
4927    fn test_cast_with_options_utf8_to_bool() {
4928        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4929        let casted = cast_with_options(
4930            &strings,
4931            &DataType::Boolean,
4932            &CastOptions {
4933                safe: false,
4934                format_options: FormatOptions::default(),
4935            },
4936        );
4937        match casted {
4938            Ok(_) => panic!("expected error"),
4939            Err(e) => {
4940                assert!(
4941                    e.to_string().contains(
4942                        "Cast error: Cannot cast value 'invalid' to value of Boolean type"
4943                    )
4944                )
4945            }
4946        }
4947    }
4948
4949    #[test]
4950    fn test_cast_bool_to_i32() {
4951        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4952        let b = cast(&array, &DataType::Int32).unwrap();
4953        let c = b.as_primitive::<Int32Type>();
4954        assert_eq!(1, c.value(0));
4955        assert_eq!(0, c.value(1));
4956        assert!(!c.is_valid(2));
4957    }
4958
4959    #[test]
4960    fn test_cast_bool_to_utf8view() {
4961        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4962        let b = cast(&array, &DataType::Utf8View).unwrap();
4963        let c = b.as_any().downcast_ref::<StringViewArray>().unwrap();
4964        assert_eq!("true", c.value(0));
4965        assert_eq!("false", c.value(1));
4966        assert!(!c.is_valid(2));
4967    }
4968
4969    #[test]
4970    fn test_cast_bool_to_utf8() {
4971        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4972        let b = cast(&array, &DataType::Utf8).unwrap();
4973        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
4974        assert_eq!("true", c.value(0));
4975        assert_eq!("false", c.value(1));
4976        assert!(!c.is_valid(2));
4977    }
4978
4979    #[test]
4980    fn test_cast_bool_to_large_utf8() {
4981        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4982        let b = cast(&array, &DataType::LargeUtf8).unwrap();
4983        let c = b.as_any().downcast_ref::<LargeStringArray>().unwrap();
4984        assert_eq!("true", c.value(0));
4985        assert_eq!("false", c.value(1));
4986        assert!(!c.is_valid(2));
4987    }
4988
4989    #[test]
4990    fn test_cast_bool_to_f64() {
4991        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4992        let b = cast(&array, &DataType::Float64).unwrap();
4993        let c = b.as_primitive::<Float64Type>();
4994        assert_eq!(1.0, c.value(0));
4995        assert_eq!(0.0, c.value(1));
4996        assert!(!c.is_valid(2));
4997    }
4998
4999    #[test]
5000    fn test_cast_integer_to_timestamp() {
5001        let array = Int64Array::from(vec![Some(2), Some(10), None]);
5002        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5003
5004        let array = Int8Array::from(vec![Some(2), Some(10), None]);
5005        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5006
5007        assert_eq!(&actual, &expected);
5008
5009        let array = Int16Array::from(vec![Some(2), Some(10), None]);
5010        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5011
5012        assert_eq!(&actual, &expected);
5013
5014        let array = Int32Array::from(vec![Some(2), Some(10), None]);
5015        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5016
5017        assert_eq!(&actual, &expected);
5018
5019        let array = UInt8Array::from(vec![Some(2), Some(10), None]);
5020        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5021
5022        assert_eq!(&actual, &expected);
5023
5024        let array = UInt16Array::from(vec![Some(2), Some(10), None]);
5025        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5026
5027        assert_eq!(&actual, &expected);
5028
5029        let array = UInt32Array::from(vec![Some(2), Some(10), None]);
5030        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5031
5032        assert_eq!(&actual, &expected);
5033
5034        let array = UInt64Array::from(vec![Some(2), Some(10), None]);
5035        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5036
5037        assert_eq!(&actual, &expected);
5038    }
5039
5040    #[test]
5041    fn test_cast_timestamp_to_integer() {
5042        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
5043            .with_timezone("UTC".to_string());
5044        let expected = cast(&array, &DataType::Int64).unwrap();
5045
5046        let actual = cast(&cast(&array, &DataType::Int8).unwrap(), &DataType::Int64).unwrap();
5047        assert_eq!(&actual, &expected);
5048
5049        let actual = cast(&cast(&array, &DataType::Int16).unwrap(), &DataType::Int64).unwrap();
5050        assert_eq!(&actual, &expected);
5051
5052        let actual = cast(&cast(&array, &DataType::Int32).unwrap(), &DataType::Int64).unwrap();
5053        assert_eq!(&actual, &expected);
5054
5055        let actual = cast(&cast(&array, &DataType::UInt8).unwrap(), &DataType::Int64).unwrap();
5056        assert_eq!(&actual, &expected);
5057
5058        let actual = cast(&cast(&array, &DataType::UInt16).unwrap(), &DataType::Int64).unwrap();
5059        assert_eq!(&actual, &expected);
5060
5061        let actual = cast(&cast(&array, &DataType::UInt32).unwrap(), &DataType::Int64).unwrap();
5062        assert_eq!(&actual, &expected);
5063
5064        let actual = cast(&cast(&array, &DataType::UInt64).unwrap(), &DataType::Int64).unwrap();
5065        assert_eq!(&actual, &expected);
5066    }
5067
5068    #[test]
5069    fn test_cast_floating_to_timestamp() {
5070        let array = Int64Array::from(vec![Some(2), Some(10), None]);
5071        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5072
5073        let array = Float16Array::from(vec![
5074            Some(f16::from_f32(2.0)),
5075            Some(f16::from_f32(10.6)),
5076            None,
5077        ]);
5078        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5079
5080        assert_eq!(&actual, &expected);
5081
5082        let array = Float32Array::from(vec![Some(2.0), Some(10.6), None]);
5083        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5084
5085        assert_eq!(&actual, &expected);
5086
5087        let array = Float64Array::from(vec![Some(2.1), Some(10.2), None]);
5088        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5089
5090        assert_eq!(&actual, &expected);
5091    }
5092
5093    #[test]
5094    fn test_cast_timestamp_to_floating() {
5095        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
5096            .with_timezone("UTC".to_string());
5097        let expected = cast(&array, &DataType::Int64).unwrap();
5098
5099        let actual = cast(&cast(&array, &DataType::Float16).unwrap(), &DataType::Int64).unwrap();
5100        assert_eq!(&actual, &expected);
5101
5102        let actual = cast(&cast(&array, &DataType::Float32).unwrap(), &DataType::Int64).unwrap();
5103        assert_eq!(&actual, &expected);
5104
5105        let actual = cast(&cast(&array, &DataType::Float64).unwrap(), &DataType::Int64).unwrap();
5106        assert_eq!(&actual, &expected);
5107    }
5108
5109    #[test]
5110    fn test_cast_decimal_to_timestamp() {
5111        let array = Int64Array::from(vec![Some(2), Some(10), None]);
5112        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5113
5114        let array = Decimal128Array::from(vec![Some(200), Some(1000), None])
5115            .with_precision_and_scale(4, 2)
5116            .unwrap();
5117        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5118
5119        assert_eq!(&actual, &expected);
5120
5121        let array = Decimal256Array::from(vec![
5122            Some(i256::from_i128(2000)),
5123            Some(i256::from_i128(10000)),
5124            None,
5125        ])
5126        .with_precision_and_scale(5, 3)
5127        .unwrap();
5128        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5129
5130        assert_eq!(&actual, &expected);
5131    }
5132
5133    #[test]
5134    fn test_cast_timestamp_to_decimal() {
5135        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
5136            .with_timezone("UTC".to_string());
5137        let expected = cast(&array, &DataType::Int64).unwrap();
5138
5139        let actual = cast(
5140            &cast(&array, &DataType::Decimal128(5, 2)).unwrap(),
5141            &DataType::Int64,
5142        )
5143        .unwrap();
5144        assert_eq!(&actual, &expected);
5145
5146        let actual = cast(
5147            &cast(&array, &DataType::Decimal256(10, 5)).unwrap(),
5148            &DataType::Int64,
5149        )
5150        .unwrap();
5151        assert_eq!(&actual, &expected);
5152    }
5153
5154    #[test]
5155    fn test_cast_list_i32_to_list_u16() {
5156        let values = vec![
5157            Some(vec![Some(0), Some(0), Some(0)]),
5158            Some(vec![Some(-1), Some(-2), Some(-1)]),
5159            Some(vec![Some(2), Some(100000000)]),
5160        ];
5161        let list_array = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
5162
5163        let target_type = DataType::List(Arc::new(Field::new("item", DataType::UInt16, true)));
5164        assert!(can_cast_types(list_array.data_type(), &target_type));
5165        let cast_array = cast(&list_array, &target_type).unwrap();
5166
5167        // For the ListArray itself, there are no null values (as there were no nulls when they went in)
5168        //
5169        // 3 negative values should get lost when casting to unsigned,
5170        // 1 value should overflow
5171        assert_eq!(0, cast_array.null_count());
5172
5173        // offsets should be the same
5174        let array = cast_array.as_list::<i32>();
5175        assert_eq!(list_array.value_offsets(), array.value_offsets());
5176
5177        assert_eq!(DataType::UInt16, array.value_type());
5178        assert_eq!(3, array.value_length(0));
5179        assert_eq!(3, array.value_length(1));
5180        assert_eq!(2, array.value_length(2));
5181
5182        // expect 4 nulls: negative numbers and overflow
5183        let u16arr = array.values().as_primitive::<UInt16Type>();
5184        assert_eq!(4, u16arr.null_count());
5185
5186        // expect 4 nulls: negative numbers and overflow
5187        let expected: UInt16Array =
5188            vec![Some(0), Some(0), Some(0), None, None, None, Some(2), None]
5189                .into_iter()
5190                .collect();
5191
5192        assert_eq!(u16arr, &expected);
5193    }
5194
5195    #[test]
5196    fn test_cast_list_i32_to_list_timestamp() {
5197        // Construct a value array
5198        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 8, 100000000]).into_data();
5199
5200        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 9]);
5201
5202        // Construct a list array from the above two
5203        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
5204        let list_data = ArrayData::builder(list_data_type)
5205            .len(3)
5206            .add_buffer(value_offsets)
5207            .add_child_data(value_data)
5208            .build()
5209            .unwrap();
5210        let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
5211
5212        let actual = cast(
5213            &list_array,
5214            &DataType::List(Arc::new(Field::new_list_field(
5215                DataType::Timestamp(TimeUnit::Microsecond, None),
5216                true,
5217            ))),
5218        )
5219        .unwrap();
5220
5221        let expected = cast(
5222            &cast(
5223                &list_array,
5224                &DataType::List(Arc::new(Field::new_list_field(DataType::Int64, true))),
5225            )
5226            .unwrap(),
5227            &DataType::List(Arc::new(Field::new_list_field(
5228                DataType::Timestamp(TimeUnit::Microsecond, None),
5229                true,
5230            ))),
5231        )
5232        .unwrap();
5233
5234        assert_eq!(&actual, &expected);
5235    }
5236
5237    #[test]
5238    fn test_cast_date32_to_date64() {
5239        let a = Date32Array::from(vec![10000, 17890]);
5240        let array = Arc::new(a) as ArrayRef;
5241        let b = cast(&array, &DataType::Date64).unwrap();
5242        let c = b.as_primitive::<Date64Type>();
5243        assert_eq!(864000000000, c.value(0));
5244        assert_eq!(1545696000000, c.value(1));
5245    }
5246
5247    #[test]
5248    fn test_cast_date64_to_date32() {
5249        let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5250        let array = Arc::new(a) as ArrayRef;
5251        let b = cast(&array, &DataType::Date32).unwrap();
5252        let c = b.as_primitive::<Date32Type>();
5253        assert_eq!(10000, c.value(0));
5254        assert_eq!(17890, c.value(1));
5255        assert!(c.is_null(2));
5256    }
5257
5258    #[test]
5259    fn test_cast_date64_to_date32_overflow() {
5260        let a = Date64Array::from(vec![i64::MAX]);
5261        let array = Arc::new(a) as ArrayRef;
5262
5263        let b = cast(&array, &DataType::Date32).unwrap();
5264        let c = b.as_primitive::<Date32Type>();
5265        assert!(c.is_null(0));
5266
5267        let options = CastOptions {
5268            safe: false,
5269            ..Default::default()
5270        };
5271        let err = cast_with_options(&array, &DataType::Date32, &options).unwrap_err();
5272        assert!(
5273            err.to_string().contains("Cannot cast Date64 value"),
5274            "{err}"
5275        );
5276    }
5277
5278    #[test]
5279    fn test_cast_string_to_integral_overflow() {
5280        let str = Arc::new(StringArray::from(vec![
5281            Some("123"),
5282            Some("-123"),
5283            Some("86374"),
5284            None,
5285        ])) as ArrayRef;
5286
5287        let options = CastOptions {
5288            safe: true,
5289            format_options: FormatOptions::default(),
5290        };
5291        let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
5292        let expected =
5293            Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
5294        assert_eq!(&res, &expected);
5295    }
5296
5297    #[test]
5298    fn test_cast_string_to_timestamp() {
5299        let a0 = Arc::new(StringViewArray::from(vec![
5300            Some("2020-09-08T12:00:00.123456789+00:00"),
5301            Some("Not a valid date"),
5302            None,
5303        ])) as ArrayRef;
5304        let a1 = Arc::new(StringArray::from(vec![
5305            Some("2020-09-08T12:00:00.123456789+00:00"),
5306            Some("Not a valid date"),
5307            None,
5308        ])) as ArrayRef;
5309        let a2 = Arc::new(LargeStringArray::from(vec![
5310            Some("2020-09-08T12:00:00.123456789+00:00"),
5311            Some("Not a valid date"),
5312            None,
5313        ])) as ArrayRef;
5314        for array in &[a0, a1, a2] {
5315            for time_unit in &[
5316                TimeUnit::Second,
5317                TimeUnit::Millisecond,
5318                TimeUnit::Microsecond,
5319                TimeUnit::Nanosecond,
5320            ] {
5321                let to_type = DataType::Timestamp(*time_unit, None);
5322                let b = cast(array, &to_type).unwrap();
5323
5324                match time_unit {
5325                    TimeUnit::Second => {
5326                        let c = b.as_primitive::<TimestampSecondType>();
5327                        assert_eq!(1599566400, c.value(0));
5328                        assert!(c.is_null(1));
5329                        assert!(c.is_null(2));
5330                    }
5331                    TimeUnit::Millisecond => {
5332                        let c = b
5333                            .as_any()
5334                            .downcast_ref::<TimestampMillisecondArray>()
5335                            .unwrap();
5336                        assert_eq!(1599566400123, c.value(0));
5337                        assert!(c.is_null(1));
5338                        assert!(c.is_null(2));
5339                    }
5340                    TimeUnit::Microsecond => {
5341                        let c = b
5342                            .as_any()
5343                            .downcast_ref::<TimestampMicrosecondArray>()
5344                            .unwrap();
5345                        assert_eq!(1599566400123456, c.value(0));
5346                        assert!(c.is_null(1));
5347                        assert!(c.is_null(2));
5348                    }
5349                    TimeUnit::Nanosecond => {
5350                        let c = b
5351                            .as_any()
5352                            .downcast_ref::<TimestampNanosecondArray>()
5353                            .unwrap();
5354                        assert_eq!(1599566400123456789, c.value(0));
5355                        assert!(c.is_null(1));
5356                        assert!(c.is_null(2));
5357                    }
5358                }
5359
5360                let options = CastOptions {
5361                    safe: false,
5362                    format_options: FormatOptions::default(),
5363                };
5364                let err = cast_with_options(array, &to_type, &options).unwrap_err();
5365                assert_eq!(
5366                    err.to_string(),
5367                    "Parser error: Error parsing timestamp from 'Not a valid date': error parsing date"
5368                );
5369            }
5370        }
5371    }
5372
5373    #[test]
5374    fn test_cast_string_to_timestamp_overflow() {
5375        let array = StringArray::from(vec!["9800-09-08T12:00:00.123456789"]);
5376        let result = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5377        let result = result.as_primitive::<TimestampSecondType>();
5378        assert_eq!(result.values(), &[247112596800]);
5379    }
5380
5381    #[test]
5382    fn test_cast_string_to_date32() {
5383        let a0 = Arc::new(StringViewArray::from(vec![
5384            Some("2018-12-25"),
5385            Some("Not a valid date"),
5386            None,
5387        ])) as ArrayRef;
5388        let a1 = Arc::new(StringArray::from(vec![
5389            Some("2018-12-25"),
5390            Some("Not a valid date"),
5391            None,
5392        ])) as ArrayRef;
5393        let a2 = Arc::new(LargeStringArray::from(vec![
5394            Some("2018-12-25"),
5395            Some("Not a valid date"),
5396            None,
5397        ])) as ArrayRef;
5398        for array in &[a0, a1, a2] {
5399            let to_type = DataType::Date32;
5400            let b = cast(array, &to_type).unwrap();
5401            let c = b.as_primitive::<Date32Type>();
5402            assert_eq!(17890, c.value(0));
5403            assert!(c.is_null(1));
5404            assert!(c.is_null(2));
5405
5406            let options = CastOptions {
5407                safe: false,
5408                format_options: FormatOptions::default(),
5409            };
5410            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5411            assert_eq!(
5412                err.to_string(),
5413                "Cast error: Cannot cast string 'Not a valid date' to value of Date32 type"
5414            );
5415        }
5416    }
5417
5418    #[test]
5419    fn test_cast_string_with_large_date_to_date32() {
5420        let array = Arc::new(StringArray::from(vec![
5421            Some("+10999-12-31"),
5422            Some("-0010-02-28"),
5423            Some("0010-02-28"),
5424            Some("0000-01-01"),
5425            Some("-0000-01-01"),
5426            Some("-0001-01-01"),
5427        ])) as ArrayRef;
5428        let to_type = DataType::Date32;
5429        let options = CastOptions {
5430            safe: false,
5431            format_options: FormatOptions::default(),
5432        };
5433        let b = cast_with_options(&array, &to_type, &options).unwrap();
5434        let c = b.as_primitive::<Date32Type>();
5435        assert_eq!(3298139, c.value(0)); // 10999-12-31
5436        assert_eq!(-723122, c.value(1)); // -0010-02-28
5437        assert_eq!(-715817, c.value(2)); // 0010-02-28
5438        assert_eq!(c.value(3), c.value(4)); // Expect 0000-01-01 and -0000-01-01 to be parsed the same
5439        assert_eq!(-719528, c.value(3)); // 0000-01-01
5440        assert_eq!(-719528, c.value(4)); // -0000-01-01
5441        assert_eq!(-719893, c.value(5)); // -0001-01-01
5442    }
5443
5444    #[test]
5445    fn test_cast_invalid_string_with_large_date_to_date32() {
5446        // Large dates need to be prefixed with a + or - sign, otherwise they are not parsed correctly
5447        let array = Arc::new(StringArray::from(vec![Some("10999-12-31")])) as ArrayRef;
5448        let to_type = DataType::Date32;
5449        let options = CastOptions {
5450            safe: false,
5451            format_options: FormatOptions::default(),
5452        };
5453        let err = cast_with_options(&array, &to_type, &options).unwrap_err();
5454        assert_eq!(
5455            err.to_string(),
5456            "Cast error: Cannot cast string '10999-12-31' to value of Date32 type"
5457        );
5458    }
5459
5460    #[test]
5461    fn test_cast_string_format_yyyymmdd_to_date32() {
5462        let a0 = Arc::new(StringViewArray::from(vec![
5463            Some("2020-12-25"),
5464            Some("20201117"),
5465        ])) as ArrayRef;
5466        let a1 = Arc::new(StringArray::from(vec![
5467            Some("2020-12-25"),
5468            Some("20201117"),
5469        ])) as ArrayRef;
5470        let a2 = Arc::new(LargeStringArray::from(vec![
5471            Some("2020-12-25"),
5472            Some("20201117"),
5473        ])) as ArrayRef;
5474
5475        for array in &[a0, a1, a2] {
5476            let to_type = DataType::Date32;
5477            let options = CastOptions {
5478                safe: false,
5479                format_options: FormatOptions::default(),
5480            };
5481            let result = cast_with_options(&array, &to_type, &options).unwrap();
5482            let c = result.as_primitive::<Date32Type>();
5483            assert_eq!(
5484                chrono::NaiveDate::from_ymd_opt(2020, 12, 25),
5485                c.value_as_date(0)
5486            );
5487            assert_eq!(
5488                chrono::NaiveDate::from_ymd_opt(2020, 11, 17),
5489                c.value_as_date(1)
5490            );
5491        }
5492    }
5493
5494    #[test]
5495    fn test_cast_string_to_time32second() {
5496        let a0 = Arc::new(StringViewArray::from(vec![
5497            Some("08:08:35.091323414"),
5498            Some("08:08:60.091323414"), // leap second
5499            Some("08:08:61.091323414"), // not valid
5500            Some("Not a valid time"),
5501            None,
5502        ])) as ArrayRef;
5503        let a1 = Arc::new(StringArray::from(vec![
5504            Some("08:08:35.091323414"),
5505            Some("08:08:60.091323414"), // leap second
5506            Some("08:08:61.091323414"), // not valid
5507            Some("Not a valid time"),
5508            None,
5509        ])) as ArrayRef;
5510        let a2 = Arc::new(LargeStringArray::from(vec![
5511            Some("08:08:35.091323414"),
5512            Some("08:08:60.091323414"), // leap second
5513            Some("08:08:61.091323414"), // not valid
5514            Some("Not a valid time"),
5515            None,
5516        ])) as ArrayRef;
5517        for array in &[a0, a1, a2] {
5518            let to_type = DataType::Time32(TimeUnit::Second);
5519            let b = cast(array, &to_type).unwrap();
5520            let c = b.as_primitive::<Time32SecondType>();
5521            assert_eq!(29315, c.value(0));
5522            assert_eq!(29340, c.value(1));
5523            assert!(c.is_null(2));
5524            assert!(c.is_null(3));
5525            assert!(c.is_null(4));
5526
5527            let options = CastOptions {
5528                safe: false,
5529                format_options: FormatOptions::default(),
5530            };
5531            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5532            assert_eq!(
5533                err.to_string(),
5534                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(s) type"
5535            );
5536        }
5537    }
5538
5539    #[test]
5540    fn test_cast_string_to_time32millisecond() {
5541        let a0 = Arc::new(StringViewArray::from(vec![
5542            Some("08:08:35.091323414"),
5543            Some("08:08:60.091323414"), // leap second
5544            Some("08:08:61.091323414"), // not valid
5545            Some("Not a valid time"),
5546            None,
5547        ])) as ArrayRef;
5548        let a1 = Arc::new(StringArray::from(vec![
5549            Some("08:08:35.091323414"),
5550            Some("08:08:60.091323414"), // leap second
5551            Some("08:08:61.091323414"), // not valid
5552            Some("Not a valid time"),
5553            None,
5554        ])) as ArrayRef;
5555        let a2 = Arc::new(LargeStringArray::from(vec![
5556            Some("08:08:35.091323414"),
5557            Some("08:08:60.091323414"), // leap second
5558            Some("08:08:61.091323414"), // not valid
5559            Some("Not a valid time"),
5560            None,
5561        ])) as ArrayRef;
5562        for array in &[a0, a1, a2] {
5563            let to_type = DataType::Time32(TimeUnit::Millisecond);
5564            let b = cast(array, &to_type).unwrap();
5565            let c = b.as_primitive::<Time32MillisecondType>();
5566            assert_eq!(29315091, c.value(0));
5567            assert_eq!(29340091, c.value(1));
5568            assert!(c.is_null(2));
5569            assert!(c.is_null(3));
5570            assert!(c.is_null(4));
5571
5572            let options = CastOptions {
5573                safe: false,
5574                format_options: FormatOptions::default(),
5575            };
5576            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5577            assert_eq!(
5578                err.to_string(),
5579                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(ms) type"
5580            );
5581        }
5582    }
5583
5584    #[test]
5585    fn test_cast_string_to_time64microsecond() {
5586        let a0 = Arc::new(StringViewArray::from(vec![
5587            Some("08:08:35.091323414"),
5588            Some("Not a valid time"),
5589            None,
5590        ])) as ArrayRef;
5591        let a1 = Arc::new(StringArray::from(vec![
5592            Some("08:08:35.091323414"),
5593            Some("Not a valid time"),
5594            None,
5595        ])) as ArrayRef;
5596        let a2 = Arc::new(LargeStringArray::from(vec![
5597            Some("08:08:35.091323414"),
5598            Some("Not a valid time"),
5599            None,
5600        ])) as ArrayRef;
5601        for array in &[a0, a1, a2] {
5602            let to_type = DataType::Time64(TimeUnit::Microsecond);
5603            let b = cast(array, &to_type).unwrap();
5604            let c = b.as_primitive::<Time64MicrosecondType>();
5605            assert_eq!(29315091323, c.value(0));
5606            assert!(c.is_null(1));
5607            assert!(c.is_null(2));
5608
5609            let options = CastOptions {
5610                safe: false,
5611                format_options: FormatOptions::default(),
5612            };
5613            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5614            assert_eq!(
5615                err.to_string(),
5616                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(µs) type"
5617            );
5618        }
5619    }
5620
5621    #[test]
5622    fn test_cast_string_to_time64nanosecond() {
5623        let a0 = Arc::new(StringViewArray::from(vec![
5624            Some("08:08:35.091323414"),
5625            Some("Not a valid time"),
5626            None,
5627        ])) as ArrayRef;
5628        let a1 = Arc::new(StringArray::from(vec![
5629            Some("08:08:35.091323414"),
5630            Some("Not a valid time"),
5631            None,
5632        ])) as ArrayRef;
5633        let a2 = Arc::new(LargeStringArray::from(vec![
5634            Some("08:08:35.091323414"),
5635            Some("Not a valid time"),
5636            None,
5637        ])) as ArrayRef;
5638        for array in &[a0, a1, a2] {
5639            let to_type = DataType::Time64(TimeUnit::Nanosecond);
5640            let b = cast(array, &to_type).unwrap();
5641            let c = b.as_primitive::<Time64NanosecondType>();
5642            assert_eq!(29315091323414, c.value(0));
5643            assert!(c.is_null(1));
5644            assert!(c.is_null(2));
5645
5646            let options = CastOptions {
5647                safe: false,
5648                format_options: FormatOptions::default(),
5649            };
5650            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5651            assert_eq!(
5652                err.to_string(),
5653                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(ns) type"
5654            );
5655        }
5656    }
5657
5658    #[test]
5659    fn test_cast_string_to_date64() {
5660        let a0 = Arc::new(StringViewArray::from(vec![
5661            Some("2020-09-08T12:00:00"),
5662            Some("Not a valid date"),
5663            None,
5664        ])) as ArrayRef;
5665        let a1 = Arc::new(StringArray::from(vec![
5666            Some("2020-09-08T12:00:00"),
5667            Some("Not a valid date"),
5668            None,
5669        ])) as ArrayRef;
5670        let a2 = Arc::new(LargeStringArray::from(vec![
5671            Some("2020-09-08T12:00:00"),
5672            Some("Not a valid date"),
5673            None,
5674        ])) as ArrayRef;
5675        for array in &[a0, a1, a2] {
5676            let to_type = DataType::Date64;
5677            let b = cast(array, &to_type).unwrap();
5678            let c = b.as_primitive::<Date64Type>();
5679            assert_eq!(1599566400000, c.value(0));
5680            assert!(c.is_null(1));
5681            assert!(c.is_null(2));
5682
5683            let options = CastOptions {
5684                safe: false,
5685                format_options: FormatOptions::default(),
5686            };
5687            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5688            assert_eq!(
5689                err.to_string(),
5690                "Cast error: Cannot cast string 'Not a valid date' to value of Date64 type"
5691            );
5692        }
5693    }
5694
5695    macro_rules! test_safe_string_to_interval {
5696        ($data_vec:expr, $interval_unit:expr, $array_ty:ty, $expect_vec:expr) => {
5697            let source_string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5698
5699            let options = CastOptions {
5700                safe: true,
5701                format_options: FormatOptions::default(),
5702            };
5703
5704            let target_interval_array = cast_with_options(
5705                &source_string_array.clone(),
5706                &DataType::Interval($interval_unit),
5707                &options,
5708            )
5709            .unwrap()
5710            .as_any()
5711            .downcast_ref::<$array_ty>()
5712            .unwrap()
5713            .clone() as $array_ty;
5714
5715            let target_string_array =
5716                cast_with_options(&target_interval_array, &DataType::Utf8, &options)
5717                    .unwrap()
5718                    .as_any()
5719                    .downcast_ref::<StringArray>()
5720                    .unwrap()
5721                    .clone();
5722
5723            let expect_string_array = StringArray::from($expect_vec);
5724
5725            assert_eq!(target_string_array, expect_string_array);
5726
5727            let target_large_string_array =
5728                cast_with_options(&target_interval_array, &DataType::LargeUtf8, &options)
5729                    .unwrap()
5730                    .as_any()
5731                    .downcast_ref::<LargeStringArray>()
5732                    .unwrap()
5733                    .clone();
5734
5735            let expect_large_string_array = LargeStringArray::from($expect_vec);
5736
5737            assert_eq!(target_large_string_array, expect_large_string_array);
5738        };
5739    }
5740
5741    #[test]
5742    fn test_cast_string_to_interval_year_month() {
5743        test_safe_string_to_interval!(
5744            vec![
5745                Some("1 year 1 month"),
5746                Some("1.5 years 13 month"),
5747                Some("30 days"),
5748                Some("31 days"),
5749                Some("2 months 31 days"),
5750                Some("2 months 31 days 1 second"),
5751                Some("foobar"),
5752            ],
5753            IntervalUnit::YearMonth,
5754            IntervalYearMonthArray,
5755            vec![
5756                Some("1 years 1 mons"),
5757                Some("2 years 7 mons"),
5758                None,
5759                None,
5760                None,
5761                None,
5762                None,
5763            ]
5764        );
5765    }
5766
5767    #[test]
5768    fn test_cast_string_to_interval_day_time() {
5769        test_safe_string_to_interval!(
5770            vec![
5771                Some("1 year 1 month"),
5772                Some("1.5 years 13 month"),
5773                Some("30 days"),
5774                Some("1 day 2 second 3.5 milliseconds"),
5775                Some("foobar"),
5776            ],
5777            IntervalUnit::DayTime,
5778            IntervalDayTimeArray,
5779            vec![
5780                Some("390 days"),
5781                Some("930 days"),
5782                Some("30 days"),
5783                None,
5784                None,
5785            ]
5786        );
5787    }
5788
5789    #[test]
5790    fn test_cast_string_to_interval_month_day_nano() {
5791        test_safe_string_to_interval!(
5792            vec![
5793                Some("1 year 1 month 1 day"),
5794                None,
5795                Some("1.5 years 13 month 35 days 1.4 milliseconds"),
5796                Some("3 days"),
5797                Some("8 seconds"),
5798                None,
5799                Some("1 day 29800 milliseconds"),
5800                Some("3 months 1 second"),
5801                Some("6 minutes 120 second"),
5802                Some("2 years 39 months 9 days 19 hours 1 minute 83 seconds 399222 milliseconds"),
5803                Some("foobar"),
5804            ],
5805            IntervalUnit::MonthDayNano,
5806            IntervalMonthDayNanoArray,
5807            vec![
5808                Some("13 mons 1 days"),
5809                None,
5810                Some("31 mons 35 days 0.001400000 secs"),
5811                Some("3 days"),
5812                Some("8.000000000 secs"),
5813                None,
5814                Some("1 days 29.800000000 secs"),
5815                Some("3 mons 1.000000000 secs"),
5816                Some("8 mins"),
5817                Some("63 mons 9 days 19 hours 9 mins 2.222000000 secs"),
5818                None,
5819            ]
5820        );
5821    }
5822
5823    macro_rules! test_unsafe_string_to_interval_err {
5824        ($data_vec:expr, $interval_unit:expr, $error_msg:expr) => {
5825            let string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5826            let options = CastOptions {
5827                safe: false,
5828                format_options: FormatOptions::default(),
5829            };
5830            let arrow_err = cast_with_options(
5831                &string_array.clone(),
5832                &DataType::Interval($interval_unit),
5833                &options,
5834            )
5835            .unwrap_err();
5836            assert_eq!($error_msg, arrow_err.to_string());
5837        };
5838    }
5839
5840    #[test]
5841    fn test_cast_string_to_interval_err() {
5842        test_unsafe_string_to_interval_err!(
5843            vec![Some("foobar")],
5844            IntervalUnit::YearMonth,
5845            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5846        );
5847        test_unsafe_string_to_interval_err!(
5848            vec![Some("foobar")],
5849            IntervalUnit::DayTime,
5850            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5851        );
5852        test_unsafe_string_to_interval_err!(
5853            vec![Some("foobar")],
5854            IntervalUnit::MonthDayNano,
5855            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5856        );
5857        test_unsafe_string_to_interval_err!(
5858            vec![Some("2 months 31 days 1 second")],
5859            IntervalUnit::YearMonth,
5860            r#"Cast error: Cannot cast 2 months 31 days 1 second to IntervalYearMonth. Only year and month fields are allowed."#
5861        );
5862        test_unsafe_string_to_interval_err!(
5863            vec![Some("1 day 1.5 milliseconds")],
5864            IntervalUnit::DayTime,
5865            r#"Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"#
5866        );
5867
5868        // overflow
5869        test_unsafe_string_to_interval_err!(
5870            vec![Some(format!(
5871                "{} century {} year {} month",
5872                i64::MAX - 2,
5873                i64::MAX - 2,
5874                i64::MAX - 2
5875            ))],
5876            IntervalUnit::DayTime,
5877            format!(
5878                "Arithmetic overflow: Overflow happened on: {} * 100",
5879                i64::MAX - 2
5880            )
5881        );
5882        test_unsafe_string_to_interval_err!(
5883            vec![Some(format!(
5884                "{} year {} month {} day",
5885                i64::MAX - 2,
5886                i64::MAX - 2,
5887                i64::MAX - 2
5888            ))],
5889            IntervalUnit::MonthDayNano,
5890            format!(
5891                "Arithmetic overflow: Overflow happened on: {} * 12",
5892                i64::MAX - 2
5893            )
5894        );
5895    }
5896
5897    #[test]
5898    fn test_cast_binary_to_fixed_size_binary() {
5899        let bytes_1 = "Hiiii".as_bytes();
5900        let bytes_2 = "Hello".as_bytes();
5901
5902        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5903        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5904        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5905
5906        let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
5907        let down_cast = array_ref
5908            .as_any()
5909            .downcast_ref::<FixedSizeBinaryArray>()
5910            .unwrap();
5911        assert_eq!(bytes_1, down_cast.value(0));
5912        assert_eq!(bytes_2, down_cast.value(1));
5913        assert!(down_cast.is_null(2));
5914
5915        let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
5916        let down_cast = array_ref
5917            .as_any()
5918            .downcast_ref::<FixedSizeBinaryArray>()
5919            .unwrap();
5920        assert_eq!(bytes_1, down_cast.value(0));
5921        assert_eq!(bytes_2, down_cast.value(1));
5922        assert!(down_cast.is_null(2));
5923
5924        // test error cases when the length of binary are not same
5925        let bytes_1 = "Hi".as_bytes();
5926        let bytes_2 = "Hello".as_bytes();
5927
5928        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5929        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5930        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5931
5932        let array_ref = cast_with_options(
5933            &a1,
5934            &DataType::FixedSizeBinary(5),
5935            &CastOptions {
5936                safe: false,
5937                format_options: FormatOptions::default(),
5938            },
5939        );
5940        assert!(array_ref.is_err());
5941
5942        let array_ref = cast_with_options(
5943            &a2,
5944            &DataType::FixedSizeBinary(5),
5945            &CastOptions {
5946                safe: false,
5947                format_options: FormatOptions::default(),
5948            },
5949        );
5950        assert!(array_ref.is_err());
5951    }
5952
5953    #[test]
5954    fn test_fixed_size_binary_to_binary() {
5955        let bytes_1 = "Hiiii".as_bytes();
5956        let bytes_2 = "Hello".as_bytes();
5957
5958        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5959        let a1 = Arc::new(FixedSizeBinaryArray::try_from(binary_data.clone()).unwrap()) as ArrayRef;
5960
5961        let array_ref = cast(&a1, &DataType::Binary).unwrap();
5962        let down_cast = array_ref.as_binary::<i32>();
5963        assert_eq!(bytes_1, down_cast.value(0));
5964        assert_eq!(bytes_2, down_cast.value(1));
5965        assert!(down_cast.is_null(2));
5966
5967        let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
5968        let down_cast = array_ref.as_binary::<i64>();
5969        assert_eq!(bytes_1, down_cast.value(0));
5970        assert_eq!(bytes_2, down_cast.value(1));
5971        assert!(down_cast.is_null(2));
5972
5973        let array_ref = cast(&a1, &DataType::BinaryView).unwrap();
5974        let down_cast = array_ref.as_binary_view();
5975        assert_eq!(bytes_1, down_cast.value(0));
5976        assert_eq!(bytes_2, down_cast.value(1));
5977        assert!(down_cast.is_null(2));
5978    }
5979
5980    #[test]
5981    fn test_fixed_size_binary_to_dictionary() {
5982        let bytes_1 = "Hiiii".as_bytes();
5983        let bytes_2 = "Hello".as_bytes();
5984
5985        let binary_data = vec![Some(bytes_1), Some(bytes_2), Some(bytes_1), None];
5986        let a1 = Arc::new(FixedSizeBinaryArray::try_from(binary_data.clone()).unwrap()) as ArrayRef;
5987
5988        let cast_type = DataType::Dictionary(
5989            Box::new(DataType::Int8),
5990            Box::new(DataType::FixedSizeBinary(5)),
5991        );
5992        let cast_array = cast(&a1, &cast_type).unwrap();
5993        assert_eq!(cast_array.data_type(), &cast_type);
5994        assert_eq!(
5995            array_to_strings(&cast_array),
5996            vec!["4869696969", "48656c6c6f", "4869696969", "null"]
5997        );
5998        // dictionary should only have two distinct values
5999        let dict_array = cast_array.as_dictionary::<Int8Type>();
6000        assert_eq!(dict_array.values().len(), 2);
6001    }
6002
6003    #[test]
6004    fn test_binary_to_dictionary() {
6005        let mut builder = GenericBinaryBuilder::<i32>::new();
6006        builder.append_value(b"hello");
6007        builder.append_value(b"hiiii");
6008        builder.append_value(b"hiiii"); // duplicate
6009        builder.append_null();
6010        builder.append_value(b"rustt");
6011
6012        let a1 = builder.finish();
6013
6014        let cast_type = DataType::Dictionary(
6015            Box::new(DataType::Int8),
6016            Box::new(DataType::FixedSizeBinary(5)),
6017        );
6018        let cast_array = cast(&a1, &cast_type).unwrap();
6019        assert_eq!(cast_array.data_type(), &cast_type);
6020        assert_eq!(
6021            array_to_strings(&cast_array),
6022            vec![
6023                "68656c6c6f",
6024                "6869696969",
6025                "6869696969",
6026                "null",
6027                "7275737474"
6028            ]
6029        );
6030        // dictionary should only have three distinct values
6031        let dict_array = cast_array.as_dictionary::<Int8Type>();
6032        assert_eq!(dict_array.values().len(), 3);
6033    }
6034
6035    #[test]
6036    fn test_cast_string_array_to_dict_utf8_view() {
6037        let array = StringArray::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_array_to_dict_utf8_view_null_vs_literal_null() {
6061        let array = StringArray::from(vec![Some("one"), None, Some("null"), Some("one")]);
6062
6063        let cast_type =
6064            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6065        assert!(can_cast_types(array.data_type(), &cast_type));
6066        let cast_array = cast(&array, &cast_type).unwrap();
6067        assert_eq!(cast_array.data_type(), &cast_type);
6068
6069        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6070        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6071        assert_eq!(dict_array.values().len(), 2);
6072
6073        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6074        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6075        assert_eq!(actual, vec![Some("one"), None, Some("null"), Some("one")]);
6076
6077        let keys = dict_array.keys();
6078        assert!(keys.is_null(1));
6079        assert_eq!(keys.value(0), keys.value(3));
6080        assert_ne!(keys.value(0), keys.value(2));
6081    }
6082
6083    #[test]
6084    fn test_cast_string_view_array_to_dict_utf8_view() {
6085        let array = StringViewArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
6086
6087        let cast_type =
6088            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6089        assert!(can_cast_types(array.data_type(), &cast_type));
6090        let cast_array = cast(&array, &cast_type).unwrap();
6091        assert_eq!(cast_array.data_type(), &cast_type);
6092
6093        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6094        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6095        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
6096
6097        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6098        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6099        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6100
6101        let keys = dict_array.keys();
6102        assert!(keys.is_null(1));
6103        assert_eq!(keys.value(0), keys.value(3));
6104        assert_ne!(keys.value(0), keys.value(2));
6105    }
6106
6107    #[test]
6108    fn test_cast_string_view_slice_to_dict_utf8_view() {
6109        let array = StringViewArray::from(vec![
6110            Some("zero"),
6111            Some("one"),
6112            None,
6113            Some("three"),
6114            Some("one"),
6115        ]);
6116        let view = array.slice(1, 4);
6117
6118        let cast_type =
6119            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6120        assert!(can_cast_types(view.data_type(), &cast_type));
6121        let cast_array = cast(&view, &cast_type).unwrap();
6122        assert_eq!(cast_array.data_type(), &cast_type);
6123
6124        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6125        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6126        assert_eq!(dict_array.values().len(), 2);
6127
6128        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6129        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6130        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6131
6132        let keys = dict_array.keys();
6133        assert!(keys.is_null(1));
6134        assert_eq!(keys.value(0), keys.value(3));
6135        assert_ne!(keys.value(0), keys.value(2));
6136    }
6137
6138    #[test]
6139    fn test_cast_binary_array_to_dict_binary_view() {
6140        let mut builder = GenericBinaryBuilder::<i32>::new();
6141        builder.append_value(b"hello");
6142        builder.append_value(b"hiiii");
6143        builder.append_value(b"hiiii"); // duplicate
6144        builder.append_null();
6145        builder.append_value(b"rustt");
6146
6147        let array = builder.finish();
6148
6149        let cast_type =
6150            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6151        assert!(can_cast_types(array.data_type(), &cast_type));
6152        let cast_array = cast(&array, &cast_type).unwrap();
6153        assert_eq!(cast_array.data_type(), &cast_type);
6154
6155        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6156        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6157        assert_eq!(dict_array.values().len(), 3);
6158
6159        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6160        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6161        assert_eq!(
6162            actual,
6163            vec![
6164                Some(b"hello".as_slice()),
6165                Some(b"hiiii".as_slice()),
6166                Some(b"hiiii".as_slice()),
6167                None,
6168                Some(b"rustt".as_slice())
6169            ]
6170        );
6171
6172        let keys = dict_array.keys();
6173        assert!(keys.is_null(3));
6174        assert_eq!(keys.value(1), keys.value(2));
6175        assert_ne!(keys.value(0), keys.value(1));
6176    }
6177
6178    #[test]
6179    fn test_cast_binary_view_array_to_dict_binary_view() {
6180        let view = BinaryViewArray::from_iter([
6181            Some(b"hello".as_slice()),
6182            Some(b"hiiii".as_slice()),
6183            Some(b"hiiii".as_slice()), // duplicate
6184            None,
6185            Some(b"rustt".as_slice()),
6186        ]);
6187
6188        let cast_type =
6189            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6190        assert!(can_cast_types(view.data_type(), &cast_type));
6191        let cast_array = cast(&view, &cast_type).unwrap();
6192        assert_eq!(cast_array.data_type(), &cast_type);
6193
6194        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6195        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6196        assert_eq!(dict_array.values().len(), 3);
6197
6198        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6199        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6200        assert_eq!(
6201            actual,
6202            vec![
6203                Some(b"hello".as_slice()),
6204                Some(b"hiiii".as_slice()),
6205                Some(b"hiiii".as_slice()),
6206                None,
6207                Some(b"rustt".as_slice())
6208            ]
6209        );
6210
6211        let keys = dict_array.keys();
6212        assert!(keys.is_null(3));
6213        assert_eq!(keys.value(1), keys.value(2));
6214        assert_ne!(keys.value(0), keys.value(1));
6215    }
6216
6217    #[test]
6218    fn test_cast_binary_view_slice_to_dict_binary_view() {
6219        let view = BinaryViewArray::from_iter([
6220            Some(b"hello".as_slice()),
6221            Some(b"hiiii".as_slice()),
6222            Some(b"hiiii".as_slice()), // duplicate
6223            None,
6224            Some(b"rustt".as_slice()),
6225        ]);
6226        let sliced = view.slice(1, 4);
6227
6228        let cast_type =
6229            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6230        assert!(can_cast_types(sliced.data_type(), &cast_type));
6231        let cast_array = cast(&sliced, &cast_type).unwrap();
6232        assert_eq!(cast_array.data_type(), &cast_type);
6233
6234        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6235        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6236        assert_eq!(dict_array.values().len(), 2);
6237
6238        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6239        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6240        assert_eq!(
6241            actual,
6242            vec![
6243                Some(b"hiiii".as_slice()),
6244                Some(b"hiiii".as_slice()),
6245                None,
6246                Some(b"rustt".as_slice())
6247            ]
6248        );
6249
6250        let keys = dict_array.keys();
6251        assert!(keys.is_null(2));
6252        assert_eq!(keys.value(0), keys.value(1));
6253        assert_ne!(keys.value(0), keys.value(3));
6254    }
6255
6256    #[test]
6257    fn test_cast_string_array_to_dict_utf8_view_key_overflow_u8() {
6258        let array = StringArray::from_iter_values((0..257).map(|i| format!("v{i}")));
6259
6260        let cast_type =
6261            DataType::Dictionary(Box::new(DataType::UInt8), Box::new(DataType::Utf8View));
6262        assert!(can_cast_types(array.data_type(), &cast_type));
6263        let err = cast(&array, &cast_type).unwrap_err();
6264        assert!(matches!(err, ArrowError::DictionaryKeyOverflowError));
6265    }
6266
6267    #[test]
6268    fn test_cast_large_string_array_to_dict_utf8_view() {
6269        let array = LargeStringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
6270
6271        let cast_type =
6272            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6273        assert!(can_cast_types(array.data_type(), &cast_type));
6274        let cast_array = cast(&array, &cast_type).unwrap();
6275        assert_eq!(cast_array.data_type(), &cast_type);
6276
6277        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6278        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6279        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
6280
6281        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6282        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6283        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6284
6285        let keys = dict_array.keys();
6286        assert!(keys.is_null(1));
6287        assert_eq!(keys.value(0), keys.value(3));
6288        assert_ne!(keys.value(0), keys.value(2));
6289    }
6290
6291    #[test]
6292    fn test_cast_large_binary_array_to_dict_binary_view() {
6293        let mut builder = GenericBinaryBuilder::<i64>::new();
6294        builder.append_value(b"hello");
6295        builder.append_value(b"world");
6296        builder.append_value(b"hello"); // duplicate
6297        builder.append_null();
6298
6299        let array = builder.finish();
6300
6301        let cast_type =
6302            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6303        assert!(can_cast_types(array.data_type(), &cast_type));
6304        let cast_array = cast(&array, &cast_type).unwrap();
6305        assert_eq!(cast_array.data_type(), &cast_type);
6306
6307        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6308        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6309        assert_eq!(dict_array.values().len(), 2); // "hello" and "world" deduplicated
6310
6311        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6312        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6313        assert_eq!(
6314            actual,
6315            vec![
6316                Some(b"hello".as_slice()),
6317                Some(b"world".as_slice()),
6318                Some(b"hello".as_slice()),
6319                None
6320            ]
6321        );
6322
6323        let keys = dict_array.keys();
6324        assert!(keys.is_null(3));
6325        assert_eq!(keys.value(0), keys.value(2));
6326        assert_ne!(keys.value(0), keys.value(1));
6327    }
6328
6329    #[test]
6330    fn test_cast_struct_array_to_dict_struct() {
6331        // Cast a StructArray into Dictionary<UInt32, Struct{…}>. The dictionary
6332        // value type's child fields may differ from the source's (here:
6333        // Utf8 source → Utf8View child for `name`), so the per-field cast
6334        // must run before identity keys are emitted. This is the "as long as
6335        // the struct can be cast to the dict value" contract.
6336        let names = StringArray::from(vec![Some("alpha"), None, Some("gamma")]);
6337        let ids = Int32Array::from(vec![Some(1), Some(2), Some(3)]);
6338        let source = StructArray::from(vec![
6339            (
6340                Arc::new(Field::new("name", DataType::Utf8, true)),
6341                Arc::new(names) as ArrayRef,
6342            ),
6343            (
6344                Arc::new(Field::new("id", DataType::Int32, false)),
6345                Arc::new(ids) as ArrayRef,
6346            ),
6347        ]);
6348
6349        let target_value_type = DataType::Struct(
6350            vec![
6351                Field::new("name", DataType::Utf8View, true),
6352                Field::new("id", DataType::Int64, false),
6353            ]
6354            .into(),
6355        );
6356        let cast_type = DataType::Dictionary(
6357            Box::new(DataType::UInt32),
6358            Box::new(target_value_type.clone()),
6359        );
6360        assert!(can_cast_types(source.data_type(), &cast_type));
6361
6362        let cast_array = cast(&source, &cast_type).unwrap();
6363        assert_eq!(cast_array.data_type(), &cast_type);
6364        assert_eq!(cast_array.len(), 3);
6365
6366        let dict = cast_array.as_dictionary::<UInt32Type>();
6367        assert_eq!(dict.values().data_type(), &target_value_type);
6368        // No dedup is performed for struct values — one row, one key.
6369        assert_eq!(dict.values().len(), 3);
6370
6371        // Source row 1 was a `Utf8`-null in the `name` field but the whole
6372        // struct row was valid (StructArray::from above takes per-field
6373        // nulls only). The dictionary's logical null mask therefore mirrors
6374        // the source struct's row-level null mask — all rows valid here.
6375        let keys = dict.keys();
6376        assert_eq!(keys.values(), &[0u32, 1, 2]);
6377        assert_eq!(keys.null_count(), 0);
6378
6379        let struct_values = dict.values().as_struct();
6380        let names_out = struct_values
6381            .column_by_name("name")
6382            .unwrap()
6383            .as_string_view();
6384        assert_eq!(names_out.value(0), "alpha");
6385        assert!(names_out.is_null(1));
6386        assert_eq!(names_out.value(2), "gamma");
6387        let ids_out = struct_values
6388            .column_by_name("id")
6389            .unwrap()
6390            .as_primitive::<Int64Type>();
6391        assert_eq!(ids_out.values(), &[1i64, 2, 3]);
6392    }
6393
6394    #[test]
6395    fn test_cast_struct_array_to_dict_struct_row_nulls() {
6396        // Row-level nulls on the source struct must surface as null keys on
6397        // the dictionary, since the dictionary's logical null mask is
6398        // determined by the keys.
6399        let names = StringArray::from(vec![Some("alpha"), Some("beta"), Some("gamma")]);
6400        let ids = Int32Array::from(vec![Some(1), Some(2), Some(3)]);
6401        let source = StructArray::try_new(
6402            vec![
6403                Field::new("name", DataType::Utf8, true),
6404                Field::new("id", DataType::Int32, false),
6405            ]
6406            .into(),
6407            vec![Arc::new(names) as ArrayRef, Arc::new(ids) as ArrayRef],
6408            Some(NullBuffer::from(vec![true, false, true])),
6409        )
6410        .unwrap();
6411
6412        let target_value_type = DataType::Struct(
6413            vec![
6414                Field::new("name", DataType::Utf8, true),
6415                Field::new("id", DataType::Int32, false),
6416            ]
6417            .into(),
6418        );
6419        let cast_type =
6420            DataType::Dictionary(Box::new(DataType::UInt32), Box::new(target_value_type));
6421
6422        let cast_array = cast(&source, &cast_type).unwrap();
6423        let dict = cast_array.as_dictionary::<UInt32Type>();
6424        assert_eq!(dict.len(), 3);
6425        let keys = dict.keys();
6426        assert!(!keys.is_null(0));
6427        assert!(keys.is_null(1));
6428        assert!(!keys.is_null(2));
6429    }
6430
6431    #[test]
6432    fn test_cast_struct_array_to_dict_struct_key_overflow() {
6433        // Source has 300 rows but the dictionary key type is UInt8 (max 255).
6434        // We must return a CastError instead of silently truncating.
6435        let n = 300;
6436        let names = StringArray::from((0..n).map(|i| Some(format!("v{i}"))).collect::<Vec<_>>());
6437        let source = StructArray::from(vec![(
6438            Arc::new(Field::new("name", DataType::Utf8, true)),
6439            Arc::new(names) as ArrayRef,
6440        )]);
6441
6442        let cast_type = DataType::Dictionary(
6443            Box::new(DataType::UInt8),
6444            Box::new(DataType::Struct(
6445                vec![Field::new("name", DataType::Utf8, true)].into(),
6446            )),
6447        );
6448        let err = cast(&source, &cast_type).unwrap_err().to_string();
6449        assert!(
6450            err.contains("Cannot fit") && err.contains("dictionary keys"),
6451            "expected key-overflow error, got: {err}"
6452        );
6453    }
6454
6455    #[test]
6456    fn test_cast_empty_string_array_to_dict_utf8_view() {
6457        let array = StringArray::from(Vec::<Option<&str>>::new());
6458
6459        let cast_type =
6460            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6461        assert!(can_cast_types(array.data_type(), &cast_type));
6462        let cast_array = cast(&array, &cast_type).unwrap();
6463        assert_eq!(cast_array.data_type(), &cast_type);
6464        assert_eq!(cast_array.len(), 0);
6465    }
6466
6467    #[test]
6468    fn test_cast_empty_binary_array_to_dict_binary_view() {
6469        let array = BinaryArray::from(Vec::<Option<&[u8]>>::new());
6470
6471        let cast_type =
6472            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6473        assert!(can_cast_types(array.data_type(), &cast_type));
6474        let cast_array = cast(&array, &cast_type).unwrap();
6475        assert_eq!(cast_array.data_type(), &cast_type);
6476        assert_eq!(cast_array.len(), 0);
6477    }
6478
6479    #[test]
6480    fn test_cast_all_null_string_array_to_dict_utf8_view() {
6481        let array = StringArray::from(vec![None::<&str>, None, None]);
6482
6483        let cast_type =
6484            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6485        assert!(can_cast_types(array.data_type(), &cast_type));
6486        let cast_array = cast(&array, &cast_type).unwrap();
6487        assert_eq!(cast_array.data_type(), &cast_type);
6488        assert_eq!(cast_array.null_count(), 3);
6489
6490        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6491        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6492        assert_eq!(dict_array.values().len(), 0);
6493        assert_eq!(dict_array.keys().null_count(), 3);
6494
6495        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6496        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6497        assert_eq!(actual, vec![None, None, None]);
6498    }
6499
6500    #[test]
6501    fn test_cast_all_null_binary_array_to_dict_binary_view() {
6502        let array = BinaryArray::from(vec![None::<&[u8]>, None, None]);
6503
6504        let cast_type =
6505            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6506        assert!(can_cast_types(array.data_type(), &cast_type));
6507        let cast_array = cast(&array, &cast_type).unwrap();
6508        assert_eq!(cast_array.data_type(), &cast_type);
6509        assert_eq!(cast_array.null_count(), 3);
6510
6511        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6512        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6513        assert_eq!(dict_array.values().len(), 0);
6514        assert_eq!(dict_array.keys().null_count(), 3);
6515
6516        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6517        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6518        assert_eq!(actual, vec![None, None, None]);
6519    }
6520
6521    #[test]
6522    fn test_numeric_to_binary() {
6523        let a = Int16Array::from(vec![Some(1), Some(511), None]);
6524
6525        let array_ref = cast(&a, &DataType::Binary).unwrap();
6526        let down_cast = array_ref.as_binary::<i32>();
6527        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6528        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6529        assert!(down_cast.is_null(2));
6530
6531        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6532
6533        let array_ref = cast(&a, &DataType::Binary).unwrap();
6534        let down_cast = array_ref.as_binary::<i32>();
6535        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6536        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6537        assert!(down_cast.is_null(2));
6538    }
6539
6540    #[test]
6541    fn test_numeric_to_large_binary() {
6542        let a = Int16Array::from(vec![Some(1), Some(511), None]);
6543
6544        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6545        let down_cast = array_ref.as_binary::<i64>();
6546        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6547        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6548        assert!(down_cast.is_null(2));
6549
6550        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6551
6552        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6553        let down_cast = array_ref.as_binary::<i64>();
6554        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6555        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6556        assert!(down_cast.is_null(2));
6557    }
6558
6559    #[test]
6560    fn test_cast_date32_to_int32() {
6561        let array = Date32Array::from(vec![10000, 17890]);
6562        let b = cast(&array, &DataType::Int32).unwrap();
6563        let c = b.as_primitive::<Int32Type>();
6564        assert_eq!(10000, c.value(0));
6565        assert_eq!(17890, c.value(1));
6566    }
6567
6568    #[test]
6569    fn test_cast_int32_to_date32() {
6570        let array = Int32Array::from(vec![10000, 17890]);
6571        let b = cast(&array, &DataType::Date32).unwrap();
6572        let c = b.as_primitive::<Date32Type>();
6573        assert_eq!(10000, c.value(0));
6574        assert_eq!(17890, c.value(1));
6575    }
6576
6577    #[test]
6578    fn test_cast_timestamp_to_date32() {
6579        let array =
6580            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6581                .with_timezone("+00:00".to_string());
6582        let b = cast(&array, &DataType::Date32).unwrap();
6583        let c = b.as_primitive::<Date32Type>();
6584        assert_eq!(10000, c.value(0));
6585        assert_eq!(17890, c.value(1));
6586        assert!(c.is_null(2));
6587    }
6588    #[test]
6589    fn test_cast_timestamp_to_date32_zone() {
6590        let strings = StringArray::from_iter([
6591            Some("1970-01-01T00:00:01"),
6592            Some("1970-01-01T23:59:59"),
6593            None,
6594            Some("2020-03-01T02:00:23+00:00"),
6595        ]);
6596        let dt = DataType::Timestamp(TimeUnit::Millisecond, Some("-07:00".into()));
6597        let timestamps = cast(&strings, &dt).unwrap();
6598        let dates = cast(timestamps.as_ref(), &DataType::Date32).unwrap();
6599
6600        let c = dates.as_primitive::<Date32Type>();
6601        let expected = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
6602        assert_eq!(c.value_as_date(0).unwrap(), expected);
6603        assert_eq!(c.value_as_date(1).unwrap(), expected);
6604        assert!(c.is_null(2));
6605        let expected = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
6606        assert_eq!(c.value_as_date(3).unwrap(), expected);
6607    }
6608    #[test]
6609    fn test_cast_timestamp_to_date64() {
6610        let array =
6611            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None]);
6612        let b = cast(&array, &DataType::Date64).unwrap();
6613        let c = b.as_primitive::<Date64Type>();
6614        assert_eq!(864000000005, c.value(0));
6615        assert_eq!(1545696000001, c.value(1));
6616        assert!(c.is_null(2));
6617
6618        let array = TimestampSecondArray::from(vec![Some(864000000005), Some(1545696000001)]);
6619        let b = cast(&array, &DataType::Date64).unwrap();
6620        let c = b.as_primitive::<Date64Type>();
6621        assert_eq!(864000000005000, c.value(0));
6622        assert_eq!(1545696000001000, c.value(1));
6623
6624        // test overflow, safe cast
6625        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6626        let b = cast(&array, &DataType::Date64).unwrap();
6627        assert!(b.is_null(0));
6628        // test overflow, unsafe cast
6629        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6630        let options = CastOptions {
6631            safe: false,
6632            format_options: FormatOptions::default(),
6633        };
6634        let b = cast_with_options(&array, &DataType::Date64, &options);
6635        assert!(b.is_err());
6636    }
6637
6638    #[test]
6639    fn test_cast_timestamp_to_time64() {
6640        // test timestamp secs
6641        let array = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6642            .with_timezone("+01:00".to_string());
6643        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6644        let c = b.as_primitive::<Time64MicrosecondType>();
6645        assert_eq!(3605000000, c.value(0));
6646        assert_eq!(3601000000, c.value(1));
6647        assert!(c.is_null(2));
6648        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6649        let c = b.as_primitive::<Time64NanosecondType>();
6650        assert_eq!(3605000000000, c.value(0));
6651        assert_eq!(3601000000000, c.value(1));
6652        assert!(c.is_null(2));
6653
6654        // test timestamp milliseconds
6655        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6656            .with_timezone("+01:00".to_string());
6657        let array = Arc::new(a) as ArrayRef;
6658        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6659        let c = b.as_primitive::<Time64MicrosecondType>();
6660        assert_eq!(3605000000, c.value(0));
6661        assert_eq!(3601000000, c.value(1));
6662        assert!(c.is_null(2));
6663        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6664        let c = b.as_primitive::<Time64NanosecondType>();
6665        assert_eq!(3605000000000, c.value(0));
6666        assert_eq!(3601000000000, c.value(1));
6667        assert!(c.is_null(2));
6668
6669        // test timestamp microseconds
6670        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6671            .with_timezone("+01:00".to_string());
6672        let array = Arc::new(a) as ArrayRef;
6673        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6674        let c = b.as_primitive::<Time64MicrosecondType>();
6675        assert_eq!(3605000000, c.value(0));
6676        assert_eq!(3601000000, c.value(1));
6677        assert!(c.is_null(2));
6678        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6679        let c = b.as_primitive::<Time64NanosecondType>();
6680        assert_eq!(3605000000000, c.value(0));
6681        assert_eq!(3601000000000, c.value(1));
6682        assert!(c.is_null(2));
6683
6684        // test timestamp nanoseconds
6685        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
6686            .with_timezone("+01:00".to_string());
6687        let array = Arc::new(a) as ArrayRef;
6688        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6689        let c = b.as_primitive::<Time64MicrosecondType>();
6690        assert_eq!(3605000000, c.value(0));
6691        assert_eq!(3601000000, c.value(1));
6692        assert!(c.is_null(2));
6693        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6694        let c = b.as_primitive::<Time64NanosecondType>();
6695        assert_eq!(3605000000000, c.value(0));
6696        assert_eq!(3601000000000, c.value(1));
6697        assert!(c.is_null(2));
6698
6699        // test overflow
6700        let a =
6701            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6702        let array = Arc::new(a) as ArrayRef;
6703        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond));
6704        assert!(b.is_err());
6705        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond));
6706        assert!(b.is_err());
6707        let b = cast(&array, &DataType::Time64(TimeUnit::Millisecond));
6708        assert!(b.is_err());
6709    }
6710
6711    #[test]
6712    fn test_cast_timestamp_to_time32() {
6713        // test timestamp secs
6714        let a = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6715            .with_timezone("+01:00".to_string());
6716        let array = Arc::new(a) as ArrayRef;
6717        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6718        let c = b.as_primitive::<Time32SecondType>();
6719        assert_eq!(3605, c.value(0));
6720        assert_eq!(3601, c.value(1));
6721        assert!(c.is_null(2));
6722        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6723        let c = b.as_primitive::<Time32MillisecondType>();
6724        assert_eq!(3605000, c.value(0));
6725        assert_eq!(3601000, c.value(1));
6726        assert!(c.is_null(2));
6727
6728        // test timestamp milliseconds
6729        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6730            .with_timezone("+01:00".to_string());
6731        let array = Arc::new(a) as ArrayRef;
6732        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6733        let c = b.as_primitive::<Time32SecondType>();
6734        assert_eq!(3605, c.value(0));
6735        assert_eq!(3601, c.value(1));
6736        assert!(c.is_null(2));
6737        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6738        let c = b.as_primitive::<Time32MillisecondType>();
6739        assert_eq!(3605000, c.value(0));
6740        assert_eq!(3601000, c.value(1));
6741        assert!(c.is_null(2));
6742
6743        // test timestamp microseconds
6744        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6745            .with_timezone("+01:00".to_string());
6746        let array = Arc::new(a) as ArrayRef;
6747        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6748        let c = b.as_primitive::<Time32SecondType>();
6749        assert_eq!(3605, c.value(0));
6750        assert_eq!(3601, c.value(1));
6751        assert!(c.is_null(2));
6752        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6753        let c = b.as_primitive::<Time32MillisecondType>();
6754        assert_eq!(3605000, c.value(0));
6755        assert_eq!(3601000, c.value(1));
6756        assert!(c.is_null(2));
6757
6758        // test timestamp nanoseconds
6759        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
6760            .with_timezone("+01:00".to_string());
6761        let array = Arc::new(a) as ArrayRef;
6762        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6763        let c = b.as_primitive::<Time32SecondType>();
6764        assert_eq!(3605, c.value(0));
6765        assert_eq!(3601, c.value(1));
6766        assert!(c.is_null(2));
6767        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6768        let c = b.as_primitive::<Time32MillisecondType>();
6769        assert_eq!(3605000, c.value(0));
6770        assert_eq!(3601000, c.value(1));
6771        assert!(c.is_null(2));
6772
6773        // test overflow
6774        let a =
6775            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6776        let array = Arc::new(a) as ArrayRef;
6777        let b = cast(&array, &DataType::Time32(TimeUnit::Second));
6778        assert!(b.is_err());
6779        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond));
6780        assert!(b.is_err());
6781    }
6782
6783    // Cast Timestamp(_, None) -> Timestamp(_, Some(timezone))
6784    #[test]
6785    fn test_cast_timestamp_with_timezone_1() {
6786        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6787            Some("2000-01-01T00:00:00.123456789"),
6788            Some("2010-01-01T00:00:00.123456789"),
6789            None,
6790        ]));
6791        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6792        let timestamp_array = cast(&string_array, &to_type).unwrap();
6793
6794        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6795        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6796
6797        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6798        let result = string_array.as_string::<i32>();
6799        assert_eq!("2000-01-01T00:00:00.123456+07:00", result.value(0));
6800        assert_eq!("2010-01-01T00:00:00.123456+07:00", result.value(1));
6801        assert!(result.is_null(2));
6802    }
6803
6804    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, None)
6805    #[test]
6806    fn test_cast_timestamp_with_timezone_2() {
6807        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6808            Some("2000-01-01T07:00:00.123456789"),
6809            Some("2010-01-01T07:00:00.123456789"),
6810            None,
6811        ]));
6812        let to_type = DataType::Timestamp(TimeUnit::Millisecond, Some("+0700".into()));
6813        let timestamp_array = cast(&string_array, &to_type).unwrap();
6814
6815        // Check intermediate representation is correct
6816        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6817        let result = string_array.as_string::<i32>();
6818        assert_eq!("2000-01-01T07:00:00.123+07:00", result.value(0));
6819        assert_eq!("2010-01-01T07:00:00.123+07:00", result.value(1));
6820        assert!(result.is_null(2));
6821
6822        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6823        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6824
6825        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6826        let result = string_array.as_string::<i32>();
6827        assert_eq!("2000-01-01T00:00:00.123", result.value(0));
6828        assert_eq!("2010-01-01T00:00:00.123", result.value(1));
6829        assert!(result.is_null(2));
6830    }
6831
6832    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, Some(timezone))
6833    #[test]
6834    fn test_cast_timestamp_with_timezone_3() {
6835        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6836            Some("2000-01-01T07:00:00.123456789"),
6837            Some("2010-01-01T07:00:00.123456789"),
6838            None,
6839        ]));
6840        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6841        let timestamp_array = cast(&string_array, &to_type).unwrap();
6842
6843        // Check intermediate representation is correct
6844        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6845        let result = string_array.as_string::<i32>();
6846        assert_eq!("2000-01-01T07:00:00.123456+07:00", result.value(0));
6847        assert_eq!("2010-01-01T07:00:00.123456+07:00", result.value(1));
6848        assert!(result.is_null(2));
6849
6850        let to_type = DataType::Timestamp(TimeUnit::Second, Some("-08:00".into()));
6851        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6852
6853        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6854        let result = string_array.as_string::<i32>();
6855        assert_eq!("1999-12-31T16:00:00-08:00", result.value(0));
6856        assert_eq!("2009-12-31T16:00:00-08:00", result.value(1));
6857        assert!(result.is_null(2));
6858    }
6859
6860    #[test]
6861    fn test_cast_date64_to_timestamp() {
6862        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6863        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6864        let c = b.as_primitive::<TimestampSecondType>();
6865        assert_eq!(864000000, c.value(0));
6866        assert_eq!(1545696000, c.value(1));
6867        assert!(c.is_null(2));
6868    }
6869
6870    #[test]
6871    fn test_cast_date64_to_timestamp_ms() {
6872        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6873        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
6874        let c = b
6875            .as_any()
6876            .downcast_ref::<TimestampMillisecondArray>()
6877            .unwrap();
6878        assert_eq!(864000000005, c.value(0));
6879        assert_eq!(1545696000001, c.value(1));
6880        assert!(c.is_null(2));
6881    }
6882
6883    #[test]
6884    fn test_cast_date64_to_timestamp_us() {
6885        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6886        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
6887        let c = b
6888            .as_any()
6889            .downcast_ref::<TimestampMicrosecondArray>()
6890            .unwrap();
6891        assert_eq!(864000000005000, c.value(0));
6892        assert_eq!(1545696000001000, c.value(1));
6893        assert!(c.is_null(2));
6894    }
6895
6896    #[test]
6897    fn test_cast_date64_to_timestamp_ns() {
6898        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6899        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
6900        let c = b
6901            .as_any()
6902            .downcast_ref::<TimestampNanosecondArray>()
6903            .unwrap();
6904        assert_eq!(864000000005000000, c.value(0));
6905        assert_eq!(1545696000001000000, c.value(1));
6906        assert!(c.is_null(2));
6907    }
6908
6909    #[test]
6910    fn test_cast_timestamp_to_i64() {
6911        let array =
6912            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6913                .with_timezone("UTC".to_string());
6914        let b = cast(&array, &DataType::Int64).unwrap();
6915        let c = b.as_primitive::<Int64Type>();
6916        assert_eq!(&DataType::Int64, c.data_type());
6917        assert_eq!(864000000005, c.value(0));
6918        assert_eq!(1545696000001, c.value(1));
6919        assert!(c.is_null(2));
6920    }
6921
6922    macro_rules! assert_cast {
6923        ($array:expr, $datatype:expr, $output_array_type: ty, $expected:expr) => {{
6924            assert!(can_cast_types($array.data_type(), &$datatype));
6925            let out = cast(&$array, &$datatype).unwrap();
6926            let actual = out
6927                .as_any()
6928                .downcast_ref::<$output_array_type>()
6929                .unwrap()
6930                .into_iter()
6931                .collect::<Vec<_>>();
6932            assert_eq!(actual, $expected);
6933        }};
6934        ($array:expr, $datatype:expr, $output_array_type: ty, $options:expr, $expected:expr) => {{
6935            assert!(can_cast_types($array.data_type(), &$datatype));
6936            let out = cast_with_options(&$array, &$datatype, &$options).unwrap();
6937            let actual = out
6938                .as_any()
6939                .downcast_ref::<$output_array_type>()
6940                .unwrap()
6941                .into_iter()
6942                .collect::<Vec<_>>();
6943            assert_eq!(actual, $expected);
6944        }};
6945    }
6946
6947    #[test]
6948    fn test_cast_date32_to_string() {
6949        let array = Date32Array::from(vec![Some(0), Some(10000), Some(13036), Some(17890), None]);
6950        let expected = vec![
6951            Some("1970-01-01"),
6952            Some("1997-05-19"),
6953            Some("2005-09-10"),
6954            Some("2018-12-25"),
6955            None,
6956        ];
6957
6958        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6959        assert_cast!(array, DataType::Utf8, StringArray, expected);
6960        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6961    }
6962
6963    #[test]
6964    fn test_cast_date64_to_string() {
6965        let array = Date64Array::from(vec![
6966            Some(0),
6967            Some(10000 * 86400000),
6968            Some(13036 * 86400000),
6969            Some(17890 * 86400000),
6970            None,
6971        ]);
6972        let expected = vec![
6973            Some("1970-01-01T00:00:00"),
6974            Some("1997-05-19T00:00:00"),
6975            Some("2005-09-10T00:00:00"),
6976            Some("2018-12-25T00:00:00"),
6977            None,
6978        ];
6979
6980        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6981        assert_cast!(array, DataType::Utf8, StringArray, expected);
6982        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6983    }
6984
6985    #[test]
6986    fn test_cast_date32_to_timestamp_and_timestamp_with_timezone() {
6987        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6988        let a = Date32Array::from(vec![Some(18628), None, None]); // 2021-1-1, 2022-1-1
6989        let array = Arc::new(a) as ArrayRef;
6990
6991        let b = cast(
6992            &array,
6993            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6994        )
6995        .unwrap();
6996        let c = b.as_primitive::<TimestampSecondType>();
6997        let string_array = cast(&c, &DataType::Utf8).unwrap();
6998        let result = string_array.as_string::<i32>();
6999        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7000
7001        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
7002        let c = b.as_primitive::<TimestampSecondType>();
7003        let string_array = cast(&c, &DataType::Utf8).unwrap();
7004        let result = string_array.as_string::<i32>();
7005        assert_eq!("2021-01-01T00:00:00", result.value(0));
7006    }
7007
7008    #[test]
7009    fn test_cast_date32_to_timestamp_with_timezone() {
7010        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7011        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7012        let array = Arc::new(a) as ArrayRef;
7013        let b = cast(
7014            &array,
7015            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
7016        )
7017        .unwrap();
7018        let c = b.as_primitive::<TimestampSecondType>();
7019        assert_eq!(1609438500, c.value(0));
7020        assert_eq!(1640974500, c.value(1));
7021        assert!(c.is_null(2));
7022
7023        let string_array = cast(&c, &DataType::Utf8).unwrap();
7024        let result = string_array.as_string::<i32>();
7025        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7026        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7027    }
7028
7029    #[test]
7030    fn test_cast_date32_to_timestamp_with_timezone_ms() {
7031        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7032        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7033        let array = Arc::new(a) as ArrayRef;
7034        let b = cast(
7035            &array,
7036            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
7037        )
7038        .unwrap();
7039        let c = b.as_primitive::<TimestampMillisecondType>();
7040        assert_eq!(1609438500000, c.value(0));
7041        assert_eq!(1640974500000, c.value(1));
7042        assert!(c.is_null(2));
7043
7044        let string_array = cast(&c, &DataType::Utf8).unwrap();
7045        let result = string_array.as_string::<i32>();
7046        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7047        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7048    }
7049
7050    #[test]
7051    fn test_cast_date32_to_timestamp_with_timezone_us() {
7052        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7053        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7054        let array = Arc::new(a) as ArrayRef;
7055        let b = cast(
7056            &array,
7057            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
7058        )
7059        .unwrap();
7060        let c = b.as_primitive::<TimestampMicrosecondType>();
7061        assert_eq!(1609438500000000, c.value(0));
7062        assert_eq!(1640974500000000, c.value(1));
7063        assert!(c.is_null(2));
7064
7065        let string_array = cast(&c, &DataType::Utf8).unwrap();
7066        let result = string_array.as_string::<i32>();
7067        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7068        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7069    }
7070
7071    #[test]
7072    fn test_cast_date32_to_timestamp_with_timezone_ns() {
7073        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7074        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7075        let array = Arc::new(a) as ArrayRef;
7076        let b = cast(
7077            &array,
7078            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
7079        )
7080        .unwrap();
7081        let c = b.as_primitive::<TimestampNanosecondType>();
7082        assert_eq!(1609438500000000000, c.value(0));
7083        assert_eq!(1640974500000000000, c.value(1));
7084        assert!(c.is_null(2));
7085
7086        let string_array = cast(&c, &DataType::Utf8).unwrap();
7087        let result = string_array.as_string::<i32>();
7088        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7089        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7090    }
7091
7092    #[test]
7093    fn test_cast_date64_to_timestamp_with_timezone() {
7094        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7095        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7096        let b = cast(
7097            &array,
7098            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
7099        )
7100        .unwrap();
7101
7102        let c = b.as_primitive::<TimestampSecondType>();
7103        assert_eq!(863979300, c.value(0));
7104        assert_eq!(1545675300, c.value(1));
7105        assert!(c.is_null(2));
7106
7107        let string_array = cast(&c, &DataType::Utf8).unwrap();
7108        let result = string_array.as_string::<i32>();
7109        assert_eq!("1997-05-19T00:00:00+05:45", result.value(0));
7110        assert_eq!("2018-12-25T00:00:00+05:45", result.value(1));
7111    }
7112
7113    #[test]
7114    fn test_cast_date64_to_timestamp_with_timezone_ms() {
7115        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7116        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7117        let b = cast(
7118            &array,
7119            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
7120        )
7121        .unwrap();
7122
7123        let c = b.as_primitive::<TimestampMillisecondType>();
7124        assert_eq!(863979300005, c.value(0));
7125        assert_eq!(1545675300001, c.value(1));
7126        assert!(c.is_null(2));
7127
7128        let string_array = cast(&c, &DataType::Utf8).unwrap();
7129        let result = string_array.as_string::<i32>();
7130        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
7131        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7132    }
7133
7134    #[test]
7135    fn test_cast_date64_to_timestamp_with_timezone_us() {
7136        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7137        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7138        let b = cast(
7139            &array,
7140            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
7141        )
7142        .unwrap();
7143
7144        let c = b.as_primitive::<TimestampMicrosecondType>();
7145        assert_eq!(863979300005000, c.value(0));
7146        assert_eq!(1545675300001000, c.value(1));
7147        assert!(c.is_null(2));
7148
7149        let string_array = cast(&c, &DataType::Utf8).unwrap();
7150        let result = string_array.as_string::<i32>();
7151        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
7152        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7153    }
7154
7155    #[test]
7156    fn test_cast_date64_to_timestamp_with_timezone_ns() {
7157        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7158        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7159        let b = cast(
7160            &array,
7161            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
7162        )
7163        .unwrap();
7164
7165        let c = b.as_primitive::<TimestampNanosecondType>();
7166        assert_eq!(863979300005000000, c.value(0));
7167        assert_eq!(1545675300001000000, c.value(1));
7168        assert!(c.is_null(2));
7169
7170        let string_array = cast(&c, &DataType::Utf8).unwrap();
7171        let result = string_array.as_string::<i32>();
7172        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
7173        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7174    }
7175
7176    #[test]
7177    fn test_cast_timestamp_to_strings() {
7178        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
7179        let array =
7180            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7181        let expected = vec![
7182            Some("1997-05-19T00:00:03.005"),
7183            Some("2018-12-25T00:00:02.001"),
7184            None,
7185        ];
7186
7187        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
7188        assert_cast!(array, DataType::Utf8, StringArray, expected);
7189        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
7190    }
7191
7192    #[test]
7193    fn test_cast_timestamp_to_strings_opt() {
7194        let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
7195        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7196        let cast_options = CastOptions {
7197            safe: true,
7198            format_options: FormatOptions::default()
7199                .with_timestamp_format(Some(ts_format))
7200                .with_timestamp_tz_format(Some(ts_format)),
7201        };
7202
7203        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
7204        let array_without_tz =
7205            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7206        let expected = vec![
7207            Some("1997-05-19 00:00:03.005000"),
7208            Some("2018-12-25 00:00:02.001000"),
7209            None,
7210        ];
7211        assert_cast!(
7212            array_without_tz,
7213            DataType::Utf8View,
7214            StringViewArray,
7215            cast_options,
7216            expected
7217        );
7218        assert_cast!(
7219            array_without_tz,
7220            DataType::Utf8,
7221            StringArray,
7222            cast_options,
7223            expected
7224        );
7225        assert_cast!(
7226            array_without_tz,
7227            DataType::LargeUtf8,
7228            LargeStringArray,
7229            cast_options,
7230            expected
7231        );
7232
7233        let array_with_tz =
7234            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None])
7235                .with_timezone(tz.to_string());
7236        let expected = vec![
7237            Some("1997-05-19 05:45:03.005000"),
7238            Some("2018-12-25 05:45:02.001000"),
7239            None,
7240        ];
7241        assert_cast!(
7242            array_with_tz,
7243            DataType::Utf8View,
7244            StringViewArray,
7245            cast_options,
7246            expected
7247        );
7248        assert_cast!(
7249            array_with_tz,
7250            DataType::Utf8,
7251            StringArray,
7252            cast_options,
7253            expected
7254        );
7255        assert_cast!(
7256            array_with_tz,
7257            DataType::LargeUtf8,
7258            LargeStringArray,
7259            cast_options,
7260            expected
7261        );
7262    }
7263
7264    #[test]
7265    fn test_cast_between_timestamps() {
7266        let array =
7267            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7268        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
7269        let c = b.as_primitive::<TimestampSecondType>();
7270        assert_eq!(864000003, c.value(0));
7271        assert_eq!(1545696002, c.value(1));
7272        assert!(c.is_null(2));
7273    }
7274
7275    #[test]
7276    fn test_cast_duration_to_i64() {
7277        let base = vec![5, 6, 7, 8, 100000000];
7278
7279        let duration_arrays = vec![
7280            Arc::new(DurationNanosecondArray::from(base.clone())) as ArrayRef,
7281            Arc::new(DurationMicrosecondArray::from(base.clone())) as ArrayRef,
7282            Arc::new(DurationMillisecondArray::from(base.clone())) as ArrayRef,
7283            Arc::new(DurationSecondArray::from(base.clone())) as ArrayRef,
7284        ];
7285
7286        for arr in duration_arrays {
7287            assert!(can_cast_types(arr.data_type(), &DataType::Int64));
7288            let result = cast(&arr, &DataType::Int64).unwrap();
7289            let result = result.as_primitive::<Int64Type>();
7290            assert_eq!(base.as_slice(), result.values());
7291        }
7292    }
7293
7294    #[test]
7295    fn test_cast_between_durations_and_numerics() {
7296        fn test_cast_between_durations<FromType, ToType>()
7297        where
7298            FromType: ArrowPrimitiveType<Native = i64>,
7299            ToType: ArrowPrimitiveType<Native = i64>,
7300            PrimitiveArray<FromType>: From<Vec<Option<i64>>>,
7301        {
7302            let from_unit = match FromType::DATA_TYPE {
7303                DataType::Duration(unit) => unit,
7304                _ => panic!("Expected a duration type"),
7305            };
7306            let to_unit = match ToType::DATA_TYPE {
7307                DataType::Duration(unit) => unit,
7308                _ => panic!("Expected a duration type"),
7309            };
7310            let from_size = time_unit_multiple(&from_unit);
7311            let to_size = time_unit_multiple(&to_unit);
7312
7313            let (v1_before, v2_before) = (8640003005, 1696002001);
7314            let (v1_after, v2_after) = if from_size >= to_size {
7315                (
7316                    v1_before / (from_size / to_size),
7317                    v2_before / (from_size / to_size),
7318                )
7319            } else {
7320                (
7321                    v1_before * (to_size / from_size),
7322                    v2_before * (to_size / from_size),
7323                )
7324            };
7325
7326            let array =
7327                PrimitiveArray::<FromType>::from(vec![Some(v1_before), Some(v2_before), None]);
7328            let b = cast(&array, &ToType::DATA_TYPE).unwrap();
7329            let c = b.as_primitive::<ToType>();
7330            assert_eq!(v1_after, c.value(0));
7331            assert_eq!(v2_after, c.value(1));
7332            assert!(c.is_null(2));
7333        }
7334
7335        // between each individual duration type
7336        test_cast_between_durations::<DurationSecondType, DurationMillisecondType>();
7337        test_cast_between_durations::<DurationSecondType, DurationMicrosecondType>();
7338        test_cast_between_durations::<DurationSecondType, DurationNanosecondType>();
7339        test_cast_between_durations::<DurationMillisecondType, DurationSecondType>();
7340        test_cast_between_durations::<DurationMillisecondType, DurationMicrosecondType>();
7341        test_cast_between_durations::<DurationMillisecondType, DurationNanosecondType>();
7342        test_cast_between_durations::<DurationMicrosecondType, DurationSecondType>();
7343        test_cast_between_durations::<DurationMicrosecondType, DurationMillisecondType>();
7344        test_cast_between_durations::<DurationMicrosecondType, DurationNanosecondType>();
7345        test_cast_between_durations::<DurationNanosecondType, DurationSecondType>();
7346        test_cast_between_durations::<DurationNanosecondType, DurationMillisecondType>();
7347        test_cast_between_durations::<DurationNanosecondType, DurationMicrosecondType>();
7348
7349        // cast failed
7350        let array = DurationSecondArray::from(vec![
7351            Some(i64::MAX),
7352            Some(8640203410378005),
7353            Some(10241096),
7354            None,
7355        ]);
7356        let b = cast(&array, &DataType::Duration(TimeUnit::Nanosecond)).unwrap();
7357        let c = b.as_primitive::<DurationNanosecondType>();
7358        assert!(c.is_null(0));
7359        assert!(c.is_null(1));
7360        assert_eq!(10241096000000000, c.value(2));
7361        assert!(c.is_null(3));
7362
7363        // durations to numerics
7364        let array = DurationSecondArray::from(vec![
7365            Some(i64::MAX),
7366            Some(8640203410378005),
7367            Some(10241096),
7368            None,
7369        ]);
7370        let b = cast(&array, &DataType::Int64).unwrap();
7371        let c = b.as_primitive::<Int64Type>();
7372        assert_eq!(i64::MAX, c.value(0));
7373        assert_eq!(8640203410378005, c.value(1));
7374        assert_eq!(10241096, c.value(2));
7375        assert!(c.is_null(3));
7376
7377        let b = cast(&array, &DataType::Int32).unwrap();
7378        let c = b.as_primitive::<Int32Type>();
7379        assert_eq!(0, c.value(0));
7380        assert_eq!(0, c.value(1));
7381        assert_eq!(10241096, c.value(2));
7382        assert!(c.is_null(3));
7383
7384        // numerics to durations
7385        let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), Some(10241096), None]);
7386        let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
7387        let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
7388        assert_eq!(i32::MAX as i64, c.value(0));
7389        assert_eq!(802034103, c.value(1));
7390        assert_eq!(10241096, c.value(2));
7391        assert!(c.is_null(3));
7392    }
7393
7394    #[test]
7395    fn test_cast_to_strings() {
7396        let a = Int32Array::from(vec![1, 2, 3]);
7397        let out = cast(&a, &DataType::Utf8).unwrap();
7398        let out = out
7399            .as_any()
7400            .downcast_ref::<StringArray>()
7401            .unwrap()
7402            .into_iter()
7403            .collect::<Vec<_>>();
7404        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
7405        let out = cast(&a, &DataType::LargeUtf8).unwrap();
7406        let out = out
7407            .as_any()
7408            .downcast_ref::<LargeStringArray>()
7409            .unwrap()
7410            .into_iter()
7411            .collect::<Vec<_>>();
7412        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
7413    }
7414
7415    #[test]
7416    fn test_str_to_str_casts() {
7417        for data in [
7418            vec![Some("foo"), Some("bar"), Some("ham")],
7419            vec![Some("foo"), None, Some("bar")],
7420        ] {
7421            let a = LargeStringArray::from(data.clone());
7422            let to = cast(&a, &DataType::Utf8).unwrap();
7423            let expect = a
7424                .as_any()
7425                .downcast_ref::<LargeStringArray>()
7426                .unwrap()
7427                .into_iter()
7428                .collect::<Vec<_>>();
7429            let out = to
7430                .as_any()
7431                .downcast_ref::<StringArray>()
7432                .unwrap()
7433                .into_iter()
7434                .collect::<Vec<_>>();
7435            assert_eq!(expect, out);
7436
7437            let a = StringArray::from(data);
7438            let to = cast(&a, &DataType::LargeUtf8).unwrap();
7439            let expect = a
7440                .as_any()
7441                .downcast_ref::<StringArray>()
7442                .unwrap()
7443                .into_iter()
7444                .collect::<Vec<_>>();
7445            let out = to
7446                .as_any()
7447                .downcast_ref::<LargeStringArray>()
7448                .unwrap()
7449                .into_iter()
7450                .collect::<Vec<_>>();
7451            assert_eq!(expect, out);
7452        }
7453    }
7454
7455    const VIEW_TEST_DATA: [Option<&str>; 5] = [
7456        Some("hello"),
7457        Some("repeated"),
7458        None,
7459        Some("large payload over 12 bytes"),
7460        Some("repeated"),
7461    ];
7462
7463    #[test]
7464    fn test_string_view_to_binary_view() {
7465        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7466
7467        assert!(can_cast_types(
7468            string_view_array.data_type(),
7469            &DataType::BinaryView
7470        ));
7471
7472        let binary_view_array = cast(&string_view_array, &DataType::BinaryView).unwrap();
7473        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7474
7475        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7476        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7477    }
7478
7479    #[test]
7480    fn test_binary_view_to_string_view() {
7481        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7482
7483        assert!(can_cast_types(
7484            binary_view_array.data_type(),
7485            &DataType::Utf8View
7486        ));
7487
7488        let string_view_array = cast(&binary_view_array, &DataType::Utf8View).unwrap();
7489        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7490
7491        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7492        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7493    }
7494
7495    #[test]
7496    fn test_binary_view_to_string_view_with_invalid_utf8() {
7497        let binary_view_array = BinaryViewArray::from_iter(vec![
7498            Some("valid".as_bytes()),
7499            Some(&[0xff]),
7500            Some("utf8".as_bytes()),
7501            None,
7502        ]);
7503
7504        let strict_options = CastOptions {
7505            safe: false,
7506            ..Default::default()
7507        };
7508
7509        assert!(
7510            cast_with_options(&binary_view_array, &DataType::Utf8View, &strict_options).is_err()
7511        );
7512
7513        let safe_options = CastOptions {
7514            safe: true,
7515            ..Default::default()
7516        };
7517
7518        let string_view_array =
7519            cast_with_options(&binary_view_array, &DataType::Utf8View, &safe_options).unwrap();
7520        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7521
7522        let values: Vec<_> = string_view_array.as_string_view().iter().collect();
7523
7524        assert_eq!(values, vec![Some("valid"), None, Some("utf8"), None]);
7525    }
7526
7527    #[test]
7528    fn test_string_to_view() {
7529        _test_string_to_view::<i32>();
7530        _test_string_to_view::<i64>();
7531    }
7532
7533    fn _test_string_to_view<O>()
7534    where
7535        O: OffsetSizeTrait,
7536    {
7537        let string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7538
7539        assert!(can_cast_types(
7540            string_array.data_type(),
7541            &DataType::Utf8View
7542        ));
7543
7544        assert!(can_cast_types(
7545            string_array.data_type(),
7546            &DataType::BinaryView
7547        ));
7548
7549        let string_view_array = cast(&string_array, &DataType::Utf8View).unwrap();
7550        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7551
7552        let binary_view_array = cast(&string_array, &DataType::BinaryView).unwrap();
7553        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7554
7555        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7556        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7557
7558        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7559        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7560    }
7561
7562    #[test]
7563    fn test_bianry_to_view() {
7564        _test_binary_to_view::<i32>();
7565        _test_binary_to_view::<i64>();
7566    }
7567
7568    fn _test_binary_to_view<O>()
7569    where
7570        O: OffsetSizeTrait,
7571    {
7572        let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7573
7574        assert!(can_cast_types(
7575            binary_array.data_type(),
7576            &DataType::Utf8View
7577        ));
7578
7579        assert!(can_cast_types(
7580            binary_array.data_type(),
7581            &DataType::BinaryView
7582        ));
7583
7584        let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
7585        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7586
7587        let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
7588        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7589
7590        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7591        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7592
7593        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7594        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7595    }
7596
7597    #[test]
7598    fn test_dict_to_view() {
7599        let values = StringArray::from_iter(VIEW_TEST_DATA);
7600        let keys = Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
7601        let string_dict_array =
7602            DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
7603        let typed_dict = string_dict_array.downcast_dict::<StringArray>().unwrap();
7604
7605        let string_view_array = {
7606            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7607            for v in typed_dict.into_iter() {
7608                builder.append_option(v);
7609            }
7610            builder.finish()
7611        };
7612        let expected_string_array_type = string_view_array.data_type();
7613        let casted_string_array = cast(&string_dict_array, expected_string_array_type).unwrap();
7614        assert_eq!(casted_string_array.data_type(), expected_string_array_type);
7615        assert_eq!(casted_string_array.as_ref(), &string_view_array);
7616
7617        let binary_buffer = cast(&typed_dict.values(), &DataType::Binary).unwrap();
7618        let binary_dict_array =
7619            DictionaryArray::<Int8Type>::new(typed_dict.keys().clone(), binary_buffer);
7620        let typed_binary_dict = binary_dict_array.downcast_dict::<BinaryArray>().unwrap();
7621
7622        let binary_view_array = {
7623            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7624            for v in typed_binary_dict.into_iter() {
7625                builder.append_option(v);
7626            }
7627            builder.finish()
7628        };
7629        let expected_binary_array_type = binary_view_array.data_type();
7630        let casted_binary_array = cast(&binary_dict_array, expected_binary_array_type).unwrap();
7631        assert_eq!(casted_binary_array.data_type(), expected_binary_array_type);
7632        assert_eq!(casted_binary_array.as_ref(), &binary_view_array);
7633    }
7634
7635    #[test]
7636    fn test_view_to_dict() {
7637        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7638        let string_dict_array: DictionaryArray<Int8Type> = VIEW_TEST_DATA.into_iter().collect();
7639        let casted_type = string_dict_array.data_type();
7640        let casted_dict_array = cast(&string_view_array, casted_type).unwrap();
7641        assert_eq!(casted_dict_array.data_type(), casted_type);
7642        assert_eq!(casted_dict_array.as_ref(), &string_dict_array);
7643
7644        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7645        let binary_dict_array = string_dict_array.downcast_dict::<StringArray>().unwrap();
7646        let binary_buffer = cast(&binary_dict_array.values(), &DataType::Binary).unwrap();
7647        let binary_dict_array =
7648            DictionaryArray::<Int8Type>::new(binary_dict_array.keys().clone(), binary_buffer);
7649        let casted_type = binary_dict_array.data_type();
7650        let casted_binary_array = cast(&binary_view_array, casted_type).unwrap();
7651        assert_eq!(casted_binary_array.data_type(), casted_type);
7652        assert_eq!(casted_binary_array.as_ref(), &binary_dict_array);
7653    }
7654
7655    #[test]
7656    fn test_view_to_string() {
7657        _test_view_to_string::<i32>();
7658        _test_view_to_string::<i64>();
7659    }
7660
7661    fn _test_view_to_string<O>()
7662    where
7663        O: OffsetSizeTrait,
7664    {
7665        let string_view_array = {
7666            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7667            for s in VIEW_TEST_DATA.iter() {
7668                builder.append_option(*s);
7669            }
7670            builder.finish()
7671        };
7672
7673        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7674
7675        let expected_string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7676        let expected_type = expected_string_array.data_type();
7677
7678        assert!(can_cast_types(string_view_array.data_type(), expected_type));
7679        assert!(can_cast_types(binary_view_array.data_type(), expected_type));
7680
7681        let string_view_casted_array = cast(&string_view_array, expected_type).unwrap();
7682        assert_eq!(string_view_casted_array.data_type(), expected_type);
7683        assert_eq!(string_view_casted_array.as_ref(), &expected_string_array);
7684
7685        let binary_view_casted_array = cast(&binary_view_array, expected_type).unwrap();
7686        assert_eq!(binary_view_casted_array.data_type(), expected_type);
7687        assert_eq!(binary_view_casted_array.as_ref(), &expected_string_array);
7688    }
7689
7690    #[test]
7691    fn test_view_to_binary() {
7692        _test_view_to_binary::<i32>();
7693        _test_view_to_binary::<i64>();
7694    }
7695
7696    fn _test_view_to_binary<O>()
7697    where
7698        O: OffsetSizeTrait,
7699    {
7700        let view_array = {
7701            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7702            for s in VIEW_TEST_DATA.iter() {
7703                builder.append_option(*s);
7704            }
7705            builder.finish()
7706        };
7707
7708        let expected_binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7709        let expected_type = expected_binary_array.data_type();
7710
7711        assert!(can_cast_types(view_array.data_type(), expected_type));
7712
7713        let binary_array = cast(&view_array, expected_type).unwrap();
7714        assert_eq!(binary_array.data_type(), expected_type);
7715
7716        assert_eq!(binary_array.as_ref(), &expected_binary_array);
7717    }
7718
7719    #[test]
7720    fn test_cast_from_f64() {
7721        let f64_values: Vec<f64> = vec![
7722            i64::MIN as f64,
7723            i32::MIN as f64,
7724            i16::MIN as f64,
7725            i8::MIN as f64,
7726            0_f64,
7727            u8::MAX as f64,
7728            u16::MAX as f64,
7729            u32::MAX as f64,
7730            u64::MAX as f64,
7731        ];
7732        let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values));
7733
7734        let f64_expected = vec![
7735            -9223372036854776000.0,
7736            -2147483648.0,
7737            -32768.0,
7738            -128.0,
7739            0.0,
7740            255.0,
7741            65535.0,
7742            4294967295.0,
7743            18446744073709552000.0,
7744        ];
7745        assert_eq!(
7746            f64_expected,
7747            get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
7748                .iter()
7749                .map(|i| i.parse::<f64>().unwrap())
7750                .collect::<Vec<f64>>()
7751        );
7752
7753        let f32_expected = vec![
7754            -9223372000000000000.0,
7755            -2147483600.0,
7756            -32768.0,
7757            -128.0,
7758            0.0,
7759            255.0,
7760            65535.0,
7761            4294967300.0,
7762            18446744000000000000.0,
7763        ];
7764        assert_eq!(
7765            f32_expected,
7766            get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
7767                .iter()
7768                .map(|i| i.parse::<f32>().unwrap())
7769                .collect::<Vec<f32>>()
7770        );
7771
7772        let f16_expected = vec![
7773            f16::from_f64(-9223372000000000000.0),
7774            f16::from_f64(-2147483600.0),
7775            f16::from_f64(-32768.0),
7776            f16::from_f64(-128.0),
7777            f16::from_f64(0.0),
7778            f16::from_f64(255.0),
7779            f16::from_f64(65535.0),
7780            f16::from_f64(4294967300.0),
7781            f16::from_f64(18446744000000000000.0),
7782        ];
7783        assert_eq!(
7784            f16_expected,
7785            get_cast_values::<Float16Type>(&f64_array, &DataType::Float16)
7786                .iter()
7787                .map(|i| i.parse::<f16>().unwrap())
7788                .collect::<Vec<f16>>()
7789        );
7790
7791        let i64_expected = vec![
7792            "-9223372036854775808",
7793            "-2147483648",
7794            "-32768",
7795            "-128",
7796            "0",
7797            "255",
7798            "65535",
7799            "4294967295",
7800            "null",
7801        ];
7802        assert_eq!(
7803            i64_expected,
7804            get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
7805        );
7806
7807        let i32_expected = vec![
7808            "null",
7809            "-2147483648",
7810            "-32768",
7811            "-128",
7812            "0",
7813            "255",
7814            "65535",
7815            "null",
7816            "null",
7817        ];
7818        assert_eq!(
7819            i32_expected,
7820            get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
7821        );
7822
7823        let i16_expected = vec![
7824            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7825        ];
7826        assert_eq!(
7827            i16_expected,
7828            get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
7829        );
7830
7831        let i8_expected = vec![
7832            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7833        ];
7834        assert_eq!(
7835            i8_expected,
7836            get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
7837        );
7838
7839        let u64_expected = vec![
7840            "null",
7841            "null",
7842            "null",
7843            "null",
7844            "0",
7845            "255",
7846            "65535",
7847            "4294967295",
7848            "null",
7849        ];
7850        assert_eq!(
7851            u64_expected,
7852            get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
7853        );
7854
7855        let u32_expected = vec![
7856            "null",
7857            "null",
7858            "null",
7859            "null",
7860            "0",
7861            "255",
7862            "65535",
7863            "4294967295",
7864            "null",
7865        ];
7866        assert_eq!(
7867            u32_expected,
7868            get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
7869        );
7870
7871        let u16_expected = vec![
7872            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7873        ];
7874        assert_eq!(
7875            u16_expected,
7876            get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
7877        );
7878
7879        let u8_expected = vec![
7880            "null", "null", "null", "null", "0", "255", "null", "null", "null",
7881        ];
7882        assert_eq!(
7883            u8_expected,
7884            get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
7885        );
7886    }
7887
7888    #[test]
7889    fn test_cast_from_f32() {
7890        let f32_values: Vec<f32> = vec![
7891            i32::MIN as f32,
7892            i32::MIN as f32,
7893            i16::MIN as f32,
7894            i8::MIN as f32,
7895            0_f32,
7896            u8::MAX as f32,
7897            u16::MAX as f32,
7898            u32::MAX as f32,
7899            u32::MAX as f32,
7900        ];
7901        let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values));
7902
7903        let f64_expected = vec![
7904            "-2147483648.0",
7905            "-2147483648.0",
7906            "-32768.0",
7907            "-128.0",
7908            "0.0",
7909            "255.0",
7910            "65535.0",
7911            "4294967296.0",
7912            "4294967296.0",
7913        ];
7914        assert_eq!(
7915            f64_expected,
7916            get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
7917        );
7918
7919        let f32_expected = vec![
7920            "-2147483600.0",
7921            "-2147483600.0",
7922            "-32768.0",
7923            "-128.0",
7924            "0.0",
7925            "255.0",
7926            "65535.0",
7927            "4294967300.0",
7928            "4294967300.0",
7929        ];
7930        assert_eq!(
7931            f32_expected,
7932            get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
7933        );
7934
7935        let f16_expected = vec![
7936            "-inf", "-inf", "-32768.0", "-128.0", "0.0", "255.0", "inf", "inf", "inf",
7937        ];
7938        assert_eq!(
7939            f16_expected,
7940            get_cast_values::<Float16Type>(&f32_array, &DataType::Float16)
7941        );
7942
7943        let i64_expected = vec![
7944            "-2147483648",
7945            "-2147483648",
7946            "-32768",
7947            "-128",
7948            "0",
7949            "255",
7950            "65535",
7951            "4294967296",
7952            "4294967296",
7953        ];
7954        assert_eq!(
7955            i64_expected,
7956            get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
7957        );
7958
7959        let i32_expected = vec![
7960            "-2147483648",
7961            "-2147483648",
7962            "-32768",
7963            "-128",
7964            "0",
7965            "255",
7966            "65535",
7967            "null",
7968            "null",
7969        ];
7970        assert_eq!(
7971            i32_expected,
7972            get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
7973        );
7974
7975        let i16_expected = vec![
7976            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7977        ];
7978        assert_eq!(
7979            i16_expected,
7980            get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
7981        );
7982
7983        let i8_expected = vec![
7984            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7985        ];
7986        assert_eq!(
7987            i8_expected,
7988            get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
7989        );
7990
7991        let u64_expected = vec![
7992            "null",
7993            "null",
7994            "null",
7995            "null",
7996            "0",
7997            "255",
7998            "65535",
7999            "4294967296",
8000            "4294967296",
8001        ];
8002        assert_eq!(
8003            u64_expected,
8004            get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
8005        );
8006
8007        let u32_expected = vec![
8008            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
8009        ];
8010        assert_eq!(
8011            u32_expected,
8012            get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
8013        );
8014
8015        let u16_expected = vec![
8016            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
8017        ];
8018        assert_eq!(
8019            u16_expected,
8020            get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
8021        );
8022
8023        let u8_expected = vec![
8024            "null", "null", "null", "null", "0", "255", "null", "null", "null",
8025        ];
8026        assert_eq!(
8027            u8_expected,
8028            get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
8029        );
8030    }
8031
8032    #[test]
8033    fn test_cast_from_uint64() {
8034        let u64_values: Vec<u64> = vec![
8035            0,
8036            u8::MAX as u64,
8037            u16::MAX as u64,
8038            u32::MAX as u64,
8039            u64::MAX,
8040        ];
8041        let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values));
8042
8043        let f64_expected = vec![0.0, 255.0, 65535.0, 4294967295.0, 18446744073709552000.0];
8044        assert_eq!(
8045            f64_expected,
8046            get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
8047                .iter()
8048                .map(|i| i.parse::<f64>().unwrap())
8049                .collect::<Vec<f64>>()
8050        );
8051
8052        let f32_expected = vec![0.0, 255.0, 65535.0, 4294967300.0, 18446744000000000000.0];
8053        assert_eq!(
8054            f32_expected,
8055            get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
8056                .iter()
8057                .map(|i| i.parse::<f32>().unwrap())
8058                .collect::<Vec<f32>>()
8059        );
8060
8061        let f16_expected = vec![
8062            f16::from_f64(0.0),
8063            f16::from_f64(255.0),
8064            f16::from_f64(65535.0),
8065            f16::from_f64(4294967300.0),
8066            f16::from_f64(18446744000000000000.0),
8067        ];
8068        assert_eq!(
8069            f16_expected,
8070            get_cast_values::<Float16Type>(&u64_array, &DataType::Float16)
8071                .iter()
8072                .map(|i| i.parse::<f16>().unwrap())
8073                .collect::<Vec<f16>>()
8074        );
8075
8076        let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
8077        assert_eq!(
8078            i64_expected,
8079            get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
8080        );
8081
8082        let i32_expected = vec!["0", "255", "65535", "null", "null"];
8083        assert_eq!(
8084            i32_expected,
8085            get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
8086        );
8087
8088        let i16_expected = vec!["0", "255", "null", "null", "null"];
8089        assert_eq!(
8090            i16_expected,
8091            get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
8092        );
8093
8094        let i8_expected = vec!["0", "null", "null", "null", "null"];
8095        assert_eq!(
8096            i8_expected,
8097            get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
8098        );
8099
8100        let u64_expected = vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
8101        assert_eq!(
8102            u64_expected,
8103            get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
8104        );
8105
8106        let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
8107        assert_eq!(
8108            u32_expected,
8109            get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
8110        );
8111
8112        let u16_expected = vec!["0", "255", "65535", "null", "null"];
8113        assert_eq!(
8114            u16_expected,
8115            get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
8116        );
8117
8118        let u8_expected = vec!["0", "255", "null", "null", "null"];
8119        assert_eq!(
8120            u8_expected,
8121            get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
8122        );
8123    }
8124
8125    #[test]
8126    fn test_cast_from_uint32() {
8127        let u32_values: Vec<u32> = vec![0, u8::MAX as u32, u16::MAX as u32, u32::MAX];
8128        let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values));
8129
8130        let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
8131        assert_eq!(
8132            f64_expected,
8133            get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
8134        );
8135
8136        let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
8137        assert_eq!(
8138            f32_expected,
8139            get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
8140        );
8141
8142        let f16_expected = vec!["0.0", "255.0", "inf", "inf"];
8143        assert_eq!(
8144            f16_expected,
8145            get_cast_values::<Float16Type>(&u32_array, &DataType::Float16)
8146        );
8147
8148        let i64_expected = vec!["0", "255", "65535", "4294967295"];
8149        assert_eq!(
8150            i64_expected,
8151            get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
8152        );
8153
8154        let i32_expected = vec!["0", "255", "65535", "null"];
8155        assert_eq!(
8156            i32_expected,
8157            get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
8158        );
8159
8160        let i16_expected = vec!["0", "255", "null", "null"];
8161        assert_eq!(
8162            i16_expected,
8163            get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
8164        );
8165
8166        let i8_expected = vec!["0", "null", "null", "null"];
8167        assert_eq!(
8168            i8_expected,
8169            get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
8170        );
8171
8172        let u64_expected = vec!["0", "255", "65535", "4294967295"];
8173        assert_eq!(
8174            u64_expected,
8175            get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
8176        );
8177
8178        let u32_expected = vec!["0", "255", "65535", "4294967295"];
8179        assert_eq!(
8180            u32_expected,
8181            get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
8182        );
8183
8184        let u16_expected = vec!["0", "255", "65535", "null"];
8185        assert_eq!(
8186            u16_expected,
8187            get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
8188        );
8189
8190        let u8_expected = vec!["0", "255", "null", "null"];
8191        assert_eq!(
8192            u8_expected,
8193            get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
8194        );
8195    }
8196
8197    #[test]
8198    fn test_cast_from_uint16() {
8199        let u16_values: Vec<u16> = vec![0, u8::MAX as u16, u16::MAX];
8200        let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values));
8201
8202        let f64_expected = vec!["0.0", "255.0", "65535.0"];
8203        assert_eq!(
8204            f64_expected,
8205            get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
8206        );
8207
8208        let f32_expected = vec!["0.0", "255.0", "65535.0"];
8209        assert_eq!(
8210            f32_expected,
8211            get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
8212        );
8213
8214        let f16_expected = vec!["0.0", "255.0", "inf"];
8215        assert_eq!(
8216            f16_expected,
8217            get_cast_values::<Float16Type>(&u16_array, &DataType::Float16)
8218        );
8219
8220        let i64_expected = vec!["0", "255", "65535"];
8221        assert_eq!(
8222            i64_expected,
8223            get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
8224        );
8225
8226        let i32_expected = vec!["0", "255", "65535"];
8227        assert_eq!(
8228            i32_expected,
8229            get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
8230        );
8231
8232        let i16_expected = vec!["0", "255", "null"];
8233        assert_eq!(
8234            i16_expected,
8235            get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
8236        );
8237
8238        let i8_expected = vec!["0", "null", "null"];
8239        assert_eq!(
8240            i8_expected,
8241            get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
8242        );
8243
8244        let u64_expected = vec!["0", "255", "65535"];
8245        assert_eq!(
8246            u64_expected,
8247            get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
8248        );
8249
8250        let u32_expected = vec!["0", "255", "65535"];
8251        assert_eq!(
8252            u32_expected,
8253            get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
8254        );
8255
8256        let u16_expected = vec!["0", "255", "65535"];
8257        assert_eq!(
8258            u16_expected,
8259            get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
8260        );
8261
8262        let u8_expected = vec!["0", "255", "null"];
8263        assert_eq!(
8264            u8_expected,
8265            get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
8266        );
8267    }
8268
8269    #[test]
8270    fn test_cast_from_uint8() {
8271        let u8_values: Vec<u8> = vec![0, u8::MAX];
8272        let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values));
8273
8274        let f64_expected = vec!["0.0", "255.0"];
8275        assert_eq!(
8276            f64_expected,
8277            get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
8278        );
8279
8280        let f32_expected = vec!["0.0", "255.0"];
8281        assert_eq!(
8282            f32_expected,
8283            get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
8284        );
8285
8286        let f16_expected = vec!["0.0", "255.0"];
8287        assert_eq!(
8288            f16_expected,
8289            get_cast_values::<Float16Type>(&u8_array, &DataType::Float16)
8290        );
8291
8292        let i64_expected = vec!["0", "255"];
8293        assert_eq!(
8294            i64_expected,
8295            get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
8296        );
8297
8298        let i32_expected = vec!["0", "255"];
8299        assert_eq!(
8300            i32_expected,
8301            get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
8302        );
8303
8304        let i16_expected = vec!["0", "255"];
8305        assert_eq!(
8306            i16_expected,
8307            get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
8308        );
8309
8310        let i8_expected = vec!["0", "null"];
8311        assert_eq!(
8312            i8_expected,
8313            get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
8314        );
8315
8316        let u64_expected = vec!["0", "255"];
8317        assert_eq!(
8318            u64_expected,
8319            get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
8320        );
8321
8322        let u32_expected = vec!["0", "255"];
8323        assert_eq!(
8324            u32_expected,
8325            get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
8326        );
8327
8328        let u16_expected = vec!["0", "255"];
8329        assert_eq!(
8330            u16_expected,
8331            get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
8332        );
8333
8334        let u8_expected = vec!["0", "255"];
8335        assert_eq!(
8336            u8_expected,
8337            get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
8338        );
8339    }
8340
8341    #[test]
8342    fn test_cast_from_int64() {
8343        let i64_values: Vec<i64> = vec![
8344            i64::MIN,
8345            i32::MIN as i64,
8346            i16::MIN as i64,
8347            i8::MIN as i64,
8348            0,
8349            i8::MAX as i64,
8350            i16::MAX as i64,
8351            i32::MAX as i64,
8352            i64::MAX,
8353        ];
8354        let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values));
8355
8356        let f64_expected = vec![
8357            -9223372036854776000.0,
8358            -2147483648.0,
8359            -32768.0,
8360            -128.0,
8361            0.0,
8362            127.0,
8363            32767.0,
8364            2147483647.0,
8365            9223372036854776000.0,
8366        ];
8367        assert_eq!(
8368            f64_expected,
8369            get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
8370                .iter()
8371                .map(|i| i.parse::<f64>().unwrap())
8372                .collect::<Vec<f64>>()
8373        );
8374
8375        let f32_expected = vec![
8376            -9223372000000000000.0,
8377            -2147483600.0,
8378            -32768.0,
8379            -128.0,
8380            0.0,
8381            127.0,
8382            32767.0,
8383            2147483600.0,
8384            9223372000000000000.0,
8385        ];
8386        assert_eq!(
8387            f32_expected,
8388            get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
8389                .iter()
8390                .map(|i| i.parse::<f32>().unwrap())
8391                .collect::<Vec<f32>>()
8392        );
8393
8394        let f16_expected = vec![
8395            f16::from_f64(-9223372000000000000.0),
8396            f16::from_f64(-2147483600.0),
8397            f16::from_f64(-32768.0),
8398            f16::from_f64(-128.0),
8399            f16::from_f64(0.0),
8400            f16::from_f64(127.0),
8401            f16::from_f64(32767.0),
8402            f16::from_f64(2147483600.0),
8403            f16::from_f64(9223372000000000000.0),
8404        ];
8405        assert_eq!(
8406            f16_expected,
8407            get_cast_values::<Float16Type>(&i64_array, &DataType::Float16)
8408                .iter()
8409                .map(|i| i.parse::<f16>().unwrap())
8410                .collect::<Vec<f16>>()
8411        );
8412
8413        let i64_expected = vec![
8414            "-9223372036854775808",
8415            "-2147483648",
8416            "-32768",
8417            "-128",
8418            "0",
8419            "127",
8420            "32767",
8421            "2147483647",
8422            "9223372036854775807",
8423        ];
8424        assert_eq!(
8425            i64_expected,
8426            get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
8427        );
8428
8429        let i32_expected = vec![
8430            "null",
8431            "-2147483648",
8432            "-32768",
8433            "-128",
8434            "0",
8435            "127",
8436            "32767",
8437            "2147483647",
8438            "null",
8439        ];
8440        assert_eq!(
8441            i32_expected,
8442            get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
8443        );
8444
8445        assert_eq!(
8446            i32_expected,
8447            get_cast_values::<Date32Type>(&i64_array, &DataType::Date32)
8448        );
8449
8450        let i16_expected = vec![
8451            "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
8452        ];
8453        assert_eq!(
8454            i16_expected,
8455            get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
8456        );
8457
8458        let i8_expected = vec![
8459            "null", "null", "null", "-128", "0", "127", "null", "null", "null",
8460        ];
8461        assert_eq!(
8462            i8_expected,
8463            get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
8464        );
8465
8466        let u64_expected = vec![
8467            "null",
8468            "null",
8469            "null",
8470            "null",
8471            "0",
8472            "127",
8473            "32767",
8474            "2147483647",
8475            "9223372036854775807",
8476        ];
8477        assert_eq!(
8478            u64_expected,
8479            get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
8480        );
8481
8482        let u32_expected = vec![
8483            "null",
8484            "null",
8485            "null",
8486            "null",
8487            "0",
8488            "127",
8489            "32767",
8490            "2147483647",
8491            "null",
8492        ];
8493        assert_eq!(
8494            u32_expected,
8495            get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
8496        );
8497
8498        let u16_expected = vec![
8499            "null", "null", "null", "null", "0", "127", "32767", "null", "null",
8500        ];
8501        assert_eq!(
8502            u16_expected,
8503            get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
8504        );
8505
8506        let u8_expected = vec![
8507            "null", "null", "null", "null", "0", "127", "null", "null", "null",
8508        ];
8509        assert_eq!(
8510            u8_expected,
8511            get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
8512        );
8513    }
8514
8515    #[test]
8516    fn test_cast_from_int32() {
8517        let i32_values: Vec<i32> = vec![
8518            i32::MIN,
8519            i16::MIN as i32,
8520            i8::MIN as i32,
8521            0,
8522            i8::MAX as i32,
8523            i16::MAX as i32,
8524            i32::MAX,
8525        ];
8526        let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values));
8527
8528        let f64_expected = vec![
8529            "-2147483648.0",
8530            "-32768.0",
8531            "-128.0",
8532            "0.0",
8533            "127.0",
8534            "32767.0",
8535            "2147483647.0",
8536        ];
8537        assert_eq!(
8538            f64_expected,
8539            get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
8540        );
8541
8542        let f32_expected = vec![
8543            "-2147483600.0",
8544            "-32768.0",
8545            "-128.0",
8546            "0.0",
8547            "127.0",
8548            "32767.0",
8549            "2147483600.0",
8550        ];
8551        assert_eq!(
8552            f32_expected,
8553            get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
8554        );
8555
8556        let f16_expected = vec![
8557            f16::from_f64(-2147483600.0),
8558            f16::from_f64(-32768.0),
8559            f16::from_f64(-128.0),
8560            f16::from_f64(0.0),
8561            f16::from_f64(127.0),
8562            f16::from_f64(32767.0),
8563            f16::from_f64(2147483600.0),
8564        ];
8565        assert_eq!(
8566            f16_expected,
8567            get_cast_values::<Float16Type>(&i32_array, &DataType::Float16)
8568                .iter()
8569                .map(|i| i.parse::<f16>().unwrap())
8570                .collect::<Vec<f16>>()
8571        );
8572
8573        let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
8574        assert_eq!(
8575            i16_expected,
8576            get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
8577        );
8578
8579        let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
8580        assert_eq!(
8581            i8_expected,
8582            get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
8583        );
8584
8585        let u64_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8586        assert_eq!(
8587            u64_expected,
8588            get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
8589        );
8590
8591        let u32_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8592        assert_eq!(
8593            u32_expected,
8594            get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
8595        );
8596
8597        let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
8598        assert_eq!(
8599            u16_expected,
8600            get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
8601        );
8602
8603        let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
8604        assert_eq!(
8605            u8_expected,
8606            get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
8607        );
8608
8609        // The date32 to date64 cast increases the numerical values in order to keep the same dates.
8610        let i64_expected = vec![
8611            "-185542587187200000",
8612            "-2831155200000",
8613            "-11059200000",
8614            "0",
8615            "10972800000",
8616            "2831068800000",
8617            "185542587100800000",
8618        ];
8619        assert_eq!(
8620            i64_expected,
8621            get_cast_values::<Date64Type>(&i32_array, &DataType::Date64)
8622        );
8623    }
8624
8625    #[test]
8626    fn test_cast_from_int16() {
8627        let i16_values: Vec<i16> = vec![i16::MIN, i8::MIN as i16, 0, i8::MAX as i16, i16::MAX];
8628        let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values));
8629
8630        let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8631        assert_eq!(
8632            f64_expected,
8633            get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
8634        );
8635
8636        let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8637        assert_eq!(
8638            f32_expected,
8639            get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
8640        );
8641
8642        let f16_expected = vec![
8643            f16::from_f64(-32768.0),
8644            f16::from_f64(-128.0),
8645            f16::from_f64(0.0),
8646            f16::from_f64(127.0),
8647            f16::from_f64(32767.0),
8648        ];
8649        assert_eq!(
8650            f16_expected,
8651            get_cast_values::<Float16Type>(&i16_array, &DataType::Float16)
8652                .iter()
8653                .map(|i| i.parse::<f16>().unwrap())
8654                .collect::<Vec<f16>>()
8655        );
8656
8657        let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
8658        assert_eq!(
8659            i64_expected,
8660            get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
8661        );
8662
8663        let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
8664        assert_eq!(
8665            i32_expected,
8666            get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
8667        );
8668
8669        let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
8670        assert_eq!(
8671            i16_expected,
8672            get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
8673        );
8674
8675        let i8_expected = vec!["null", "-128", "0", "127", "null"];
8676        assert_eq!(
8677            i8_expected,
8678            get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
8679        );
8680
8681        let u64_expected = vec!["null", "null", "0", "127", "32767"];
8682        assert_eq!(
8683            u64_expected,
8684            get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
8685        );
8686
8687        let u32_expected = vec!["null", "null", "0", "127", "32767"];
8688        assert_eq!(
8689            u32_expected,
8690            get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
8691        );
8692
8693        let u16_expected = vec!["null", "null", "0", "127", "32767"];
8694        assert_eq!(
8695            u16_expected,
8696            get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
8697        );
8698
8699        let u8_expected = vec!["null", "null", "0", "127", "null"];
8700        assert_eq!(
8701            u8_expected,
8702            get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
8703        );
8704    }
8705
8706    #[test]
8707    fn test_cast_from_date32() {
8708        let i32_values: Vec<i32> = vec![
8709            i32::MIN,
8710            i16::MIN as i32,
8711            i8::MIN as i32,
8712            0,
8713            i8::MAX as i32,
8714            i16::MAX as i32,
8715            i32::MAX,
8716        ];
8717        let date32_array: ArrayRef = Arc::new(Date32Array::from(i32_values));
8718
8719        let i64_expected = vec![
8720            "-2147483648",
8721            "-32768",
8722            "-128",
8723            "0",
8724            "127",
8725            "32767",
8726            "2147483647",
8727        ];
8728        assert_eq!(
8729            i64_expected,
8730            get_cast_values::<Int64Type>(&date32_array, &DataType::Int64)
8731        );
8732    }
8733
8734    #[test]
8735    fn test_cast_from_int8() {
8736        let i8_values: Vec<i8> = vec![i8::MIN, 0, i8::MAX];
8737        let i8_array = Int8Array::from(i8_values);
8738
8739        let f64_expected = vec!["-128.0", "0.0", "127.0"];
8740        assert_eq!(
8741            f64_expected,
8742            get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
8743        );
8744
8745        let f32_expected = vec!["-128.0", "0.0", "127.0"];
8746        assert_eq!(
8747            f32_expected,
8748            get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
8749        );
8750
8751        let f16_expected = vec!["-128.0", "0.0", "127.0"];
8752        assert_eq!(
8753            f16_expected,
8754            get_cast_values::<Float16Type>(&i8_array, &DataType::Float16)
8755        );
8756
8757        let i64_expected = vec!["-128", "0", "127"];
8758        assert_eq!(
8759            i64_expected,
8760            get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
8761        );
8762
8763        let i32_expected = vec!["-128", "0", "127"];
8764        assert_eq!(
8765            i32_expected,
8766            get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
8767        );
8768
8769        let i16_expected = vec!["-128", "0", "127"];
8770        assert_eq!(
8771            i16_expected,
8772            get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
8773        );
8774
8775        let i8_expected = vec!["-128", "0", "127"];
8776        assert_eq!(
8777            i8_expected,
8778            get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
8779        );
8780
8781        let u64_expected = vec!["null", "0", "127"];
8782        assert_eq!(
8783            u64_expected,
8784            get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
8785        );
8786
8787        let u32_expected = vec!["null", "0", "127"];
8788        assert_eq!(
8789            u32_expected,
8790            get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
8791        );
8792
8793        let u16_expected = vec!["null", "0", "127"];
8794        assert_eq!(
8795            u16_expected,
8796            get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
8797        );
8798
8799        let u8_expected = vec!["null", "0", "127"];
8800        assert_eq!(
8801            u8_expected,
8802            get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
8803        );
8804    }
8805
8806    /// Convert `array` into a vector of strings by casting to data type dt
8807    fn get_cast_values<T>(array: &dyn Array, dt: &DataType) -> Vec<String>
8808    where
8809        T: ArrowPrimitiveType,
8810    {
8811        let c = cast(array, dt).unwrap();
8812        let a = c.as_primitive::<T>();
8813        let mut v: Vec<String> = vec![];
8814        for i in 0..array.len() {
8815            if a.is_null(i) {
8816                v.push("null".to_string())
8817            } else {
8818                v.push(format!("{:?}", a.value(i)));
8819            }
8820        }
8821        v
8822    }
8823
8824    #[test]
8825    fn test_cast_utf8_dict() {
8826        // FROM a dictionary with of Utf8 values
8827        let mut builder = StringDictionaryBuilder::<Int8Type>::new();
8828        builder.append("one").unwrap();
8829        builder.append_null();
8830        builder.append("three").unwrap();
8831        let array: ArrayRef = Arc::new(builder.finish());
8832
8833        let expected = vec!["one", "null", "three"];
8834
8835        // Test casting TO StringArray
8836        let cast_type = Utf8;
8837        let cast_array = cast(&array, &cast_type).expect("cast to UTF-8 failed");
8838        assert_eq!(cast_array.data_type(), &cast_type);
8839        assert_eq!(array_to_strings(&cast_array), expected);
8840
8841        // Test casting TO Dictionary (with different index sizes)
8842
8843        let cast_type = Dictionary(Box::new(Int16), Box::new(Utf8));
8844        let cast_array = cast(&array, &cast_type).expect("cast failed");
8845        assert_eq!(cast_array.data_type(), &cast_type);
8846        assert_eq!(array_to_strings(&cast_array), expected);
8847
8848        let cast_type = Dictionary(Box::new(Int32), Box::new(Utf8));
8849        let cast_array = cast(&array, &cast_type).expect("cast failed");
8850        assert_eq!(cast_array.data_type(), &cast_type);
8851        assert_eq!(array_to_strings(&cast_array), expected);
8852
8853        let cast_type = Dictionary(Box::new(Int64), Box::new(Utf8));
8854        let cast_array = cast(&array, &cast_type).expect("cast failed");
8855        assert_eq!(cast_array.data_type(), &cast_type);
8856        assert_eq!(array_to_strings(&cast_array), expected);
8857
8858        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8859        let cast_array = cast(&array, &cast_type).expect("cast failed");
8860        assert_eq!(cast_array.data_type(), &cast_type);
8861        assert_eq!(array_to_strings(&cast_array), expected);
8862
8863        let cast_type = Dictionary(Box::new(UInt16), Box::new(Utf8));
8864        let cast_array = cast(&array, &cast_type).expect("cast failed");
8865        assert_eq!(cast_array.data_type(), &cast_type);
8866        assert_eq!(array_to_strings(&cast_array), expected);
8867
8868        let cast_type = Dictionary(Box::new(UInt32), Box::new(Utf8));
8869        let cast_array = cast(&array, &cast_type).expect("cast failed");
8870        assert_eq!(cast_array.data_type(), &cast_type);
8871        assert_eq!(array_to_strings(&cast_array), expected);
8872
8873        let cast_type = Dictionary(Box::new(UInt64), Box::new(Utf8));
8874        let cast_array = cast(&array, &cast_type).expect("cast failed");
8875        assert_eq!(cast_array.data_type(), &cast_type);
8876        assert_eq!(array_to_strings(&cast_array), expected);
8877    }
8878
8879    #[test]
8880    fn test_cast_dict_to_dict_bad_index_value_primitive() {
8881        // test converting from an array that has indexes of a type
8882        // that are out of bounds for a particular other kind of
8883        // index.
8884
8885        let mut builder = PrimitiveDictionaryBuilder::<Int32Type, Int64Type>::new();
8886
8887        // add 200 distinct values (which can be stored by a
8888        // dictionary indexed by int32, but not a dictionary indexed
8889        // with int8)
8890        for i in 0..200 {
8891            builder.append(i).unwrap();
8892        }
8893        let array: ArrayRef = Arc::new(builder.finish());
8894
8895        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8896        let res = cast(&array, &cast_type);
8897        assert!(res.is_err());
8898        let actual_error = format!("{res:?}");
8899        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8900        assert!(
8901            actual_error.contains(expected_error),
8902            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8903        );
8904    }
8905
8906    #[test]
8907    fn test_cast_dict_to_dict_bad_index_value_utf8() {
8908        // Same test as test_cast_dict_to_dict_bad_index_value but use
8909        // string values (and encode the expected behavior here);
8910
8911        let mut builder = StringDictionaryBuilder::<Int32Type>::new();
8912
8913        // add 200 distinct values (which can be stored by a
8914        // dictionary indexed by int32, but not a dictionary indexed
8915        // with int8)
8916        for i in 0..200 {
8917            let val = format!("val{i}");
8918            builder.append(&val).unwrap();
8919        }
8920        let array = builder.finish();
8921
8922        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8923        let res = cast(&array, &cast_type);
8924        assert!(res.is_err());
8925        let actual_error = format!("{res:?}");
8926        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8927        assert!(
8928            actual_error.contains(expected_error),
8929            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8930        );
8931    }
8932
8933    #[test]
8934    fn test_cast_primitive_dict() {
8935        // FROM a dictionary with of INT32 values
8936        let mut builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
8937        builder.append(1).unwrap();
8938        builder.append_null();
8939        builder.append(3).unwrap();
8940        let array: ArrayRef = Arc::new(builder.finish());
8941
8942        let expected = vec!["1", "null", "3"];
8943
8944        // Test casting TO PrimitiveArray, different dictionary type
8945        let cast_array = cast(&array, &Utf8).expect("cast to UTF-8 failed");
8946        assert_eq!(array_to_strings(&cast_array), expected);
8947        assert_eq!(cast_array.data_type(), &Utf8);
8948
8949        let cast_array = cast(&array, &Int64).expect("cast to int64 failed");
8950        assert_eq!(array_to_strings(&cast_array), expected);
8951        assert_eq!(cast_array.data_type(), &Int64);
8952    }
8953
8954    #[test]
8955    fn test_cast_primitive_array_to_dict() {
8956        let mut builder = PrimitiveBuilder::<Int32Type>::new();
8957        builder.append_value(1);
8958        builder.append_null();
8959        builder.append_value(3);
8960        let array: ArrayRef = Arc::new(builder.finish());
8961
8962        let expected = vec!["1", "null", "3"];
8963
8964        // Cast to a dictionary (same value type, Int32)
8965        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int32));
8966        let cast_array = cast(&array, &cast_type).expect("cast failed");
8967        assert_eq!(cast_array.data_type(), &cast_type);
8968        assert_eq!(array_to_strings(&cast_array), expected);
8969
8970        // Cast to a dictionary (different value type, Int8)
8971        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int8));
8972        let cast_array = cast(&array, &cast_type).expect("cast failed");
8973        assert_eq!(cast_array.data_type(), &cast_type);
8974        assert_eq!(array_to_strings(&cast_array), expected);
8975    }
8976
8977    #[test]
8978    fn test_cast_time_array_to_dict() {
8979        use DataType::*;
8980
8981        let array = Arc::new(Date32Array::from(vec![Some(1000), None, Some(2000)])) as ArrayRef;
8982
8983        let expected = vec!["1972-09-27", "null", "1975-06-24"];
8984
8985        let cast_type = Dictionary(Box::new(UInt8), Box::new(Date32));
8986        let cast_array = cast(&array, &cast_type).expect("cast failed");
8987        assert_eq!(cast_array.data_type(), &cast_type);
8988        assert_eq!(array_to_strings(&cast_array), expected);
8989    }
8990
8991    #[test]
8992    fn test_cast_timestamp_array_to_dict() {
8993        use DataType::*;
8994
8995        let array = Arc::new(
8996            TimestampSecondArray::from(vec![Some(1000), None, Some(2000)]).with_timezone_utc(),
8997        ) as ArrayRef;
8998
8999        let expected = vec!["1970-01-01T00:16:40", "null", "1970-01-01T00:33:20"];
9000
9001        let cast_type = Dictionary(Box::new(UInt8), Box::new(Timestamp(TimeUnit::Second, None)));
9002        let cast_array = cast(&array, &cast_type).expect("cast failed");
9003        assert_eq!(cast_array.data_type(), &cast_type);
9004        assert_eq!(array_to_strings(&cast_array), expected);
9005    }
9006
9007    #[test]
9008    fn test_cast_string_array_to_dict() {
9009        use DataType::*;
9010
9011        let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) as ArrayRef;
9012
9013        let expected = vec!["one", "null", "three"];
9014
9015        // Cast to a dictionary (same value type, Utf8)
9016        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
9017        let cast_array = cast(&array, &cast_type).expect("cast failed");
9018        assert_eq!(cast_array.data_type(), &cast_type);
9019        assert_eq!(array_to_strings(&cast_array), expected);
9020    }
9021
9022    #[test]
9023    fn test_cast_null_array_to_from_decimal_array() {
9024        let data_type = DataType::Decimal128(12, 4);
9025        let array = new_null_array(&DataType::Null, 4);
9026        assert_eq!(array.data_type(), &DataType::Null);
9027        let cast_array = cast(&array, &data_type).expect("cast failed");
9028        assert_eq!(cast_array.data_type(), &data_type);
9029        for i in 0..4 {
9030            assert!(cast_array.is_null(i));
9031        }
9032
9033        let array = new_null_array(&data_type, 4);
9034        assert_eq!(array.data_type(), &data_type);
9035        let cast_array = cast(&array, &DataType::Null).expect("cast failed");
9036        assert_eq!(cast_array.data_type(), &DataType::Null);
9037        assert_eq!(cast_array.len(), 4);
9038        assert_eq!(cast_array.logical_nulls().unwrap().null_count(), 4);
9039    }
9040
9041    #[test]
9042    fn test_cast_null_array_from_and_to_primitive_array() {
9043        macro_rules! typed_test {
9044            ($ARR_TYPE:ident, $DATATYPE:ident, $TYPE:tt) => {{
9045                {
9046                    let array = Arc::new(NullArray::new(6)) as ArrayRef;
9047                    let expected = $ARR_TYPE::from(vec![None; 6]);
9048                    let cast_type = DataType::$DATATYPE;
9049                    let cast_array = cast(&array, &cast_type).expect("cast failed");
9050                    let cast_array = cast_array.as_primitive::<$TYPE>();
9051                    assert_eq!(cast_array.data_type(), &cast_type);
9052                    assert_eq!(cast_array, &expected);
9053                }
9054            }};
9055        }
9056
9057        typed_test!(Int16Array, Int16, Int16Type);
9058        typed_test!(Int32Array, Int32, Int32Type);
9059        typed_test!(Int64Array, Int64, Int64Type);
9060
9061        typed_test!(UInt16Array, UInt16, UInt16Type);
9062        typed_test!(UInt32Array, UInt32, UInt32Type);
9063        typed_test!(UInt64Array, UInt64, UInt64Type);
9064
9065        typed_test!(Float16Array, Float16, Float16Type);
9066        typed_test!(Float32Array, Float32, Float32Type);
9067        typed_test!(Float64Array, Float64, Float64Type);
9068
9069        typed_test!(Date32Array, Date32, Date32Type);
9070        typed_test!(Date64Array, Date64, Date64Type);
9071    }
9072
9073    fn cast_from_null_to_other_base(data_type: &DataType, is_complex: bool) {
9074        // Cast from null to data_type
9075        let array = new_null_array(&DataType::Null, 4);
9076        assert_eq!(array.data_type(), &DataType::Null);
9077        let cast_array = cast(&array, data_type).expect("cast failed");
9078        assert_eq!(cast_array.data_type(), data_type);
9079        for i in 0..4 {
9080            if is_complex {
9081                assert!(cast_array.logical_nulls().unwrap().is_null(i));
9082            } else {
9083                assert!(cast_array.is_null(i));
9084            }
9085        }
9086    }
9087
9088    fn cast_from_null_to_other(data_type: &DataType) {
9089        cast_from_null_to_other_base(data_type, false);
9090    }
9091
9092    fn cast_from_null_to_other_complex(data_type: &DataType) {
9093        cast_from_null_to_other_base(data_type, true);
9094    }
9095
9096    #[test]
9097    fn test_cast_null_from_and_to_variable_sized() {
9098        cast_from_null_to_other(&DataType::Utf8);
9099        cast_from_null_to_other(&DataType::LargeUtf8);
9100        cast_from_null_to_other(&DataType::Binary);
9101        cast_from_null_to_other(&DataType::LargeBinary);
9102    }
9103
9104    #[test]
9105    fn test_cast_null_from_and_to_nested_type() {
9106        // Cast null from and to map
9107        let data_type = DataType::Map(
9108            Arc::new(Field::new_struct(
9109                "entry",
9110                vec![
9111                    Field::new("key", DataType::Utf8, false),
9112                    Field::new("value", DataType::Int32, true),
9113                ],
9114                false,
9115            )),
9116            false,
9117        );
9118        cast_from_null_to_other(&data_type);
9119
9120        // Cast null from and to list
9121        let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
9122        cast_from_null_to_other(&data_type);
9123        let data_type = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
9124        cast_from_null_to_other(&data_type);
9125        let data_type =
9126            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
9127        cast_from_null_to_other(&data_type);
9128
9129        // Cast null from and to dictionary
9130        let values = vec![None, None, None, None] as Vec<Option<&str>>;
9131        let array: DictionaryArray<Int8Type> = values.into_iter().collect();
9132        let array = Arc::new(array) as ArrayRef;
9133        let data_type = array.data_type().to_owned();
9134        cast_from_null_to_other(&data_type);
9135
9136        // Cast null from and to struct
9137        let data_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
9138        cast_from_null_to_other(&data_type);
9139
9140        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
9141        cast_from_null_to_other(&target_type);
9142
9143        let target_type =
9144            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
9145        cast_from_null_to_other(&target_type);
9146
9147        let fields = UnionFields::from_fields(vec![Field::new("a", DataType::Int64, false)]);
9148        let target_type = DataType::Union(fields, UnionMode::Sparse);
9149        cast_from_null_to_other_complex(&target_type);
9150
9151        let target_type = DataType::RunEndEncoded(
9152            Arc::new(Field::new("item", DataType::Int32, true)),
9153            Arc::new(Field::new("item", DataType::Int32, true)),
9154        );
9155        cast_from_null_to_other_complex(&target_type);
9156    }
9157
9158    /// Print the `DictionaryArray` `array` as a vector of strings
9159    fn array_to_strings(array: &ArrayRef) -> Vec<String> {
9160        let options = FormatOptions::new().with_null("null");
9161        let formatter = ArrayFormatter::try_new(array.as_ref(), &options).unwrap();
9162        (0..array.len())
9163            .map(|i| formatter.value(i).to_string())
9164            .collect()
9165    }
9166
9167    #[test]
9168    fn test_cast_utf8_to_date32() {
9169        use chrono::NaiveDate;
9170        let from_ymd = chrono::NaiveDate::from_ymd_opt;
9171        let since = chrono::NaiveDate::signed_duration_since;
9172
9173        let a = StringArray::from(vec![
9174            "2000-01-01",          // valid date with leading 0s
9175            "2000-01-01T12:00:00", // valid datetime, will throw away the time part
9176            "2000-2-2",            // valid date without leading 0s
9177            "2000-00-00",          // invalid month and day
9178            "2000",                // just a year is invalid
9179        ]);
9180        let array = Arc::new(a) as ArrayRef;
9181        let b = cast(&array, &DataType::Date32).unwrap();
9182        let c = b.as_primitive::<Date32Type>();
9183
9184        // test valid inputs
9185        let date_value = since(
9186            NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
9187            from_ymd(1970, 1, 1).unwrap(),
9188        )
9189        .num_days() as i32;
9190        assert!(c.is_valid(0)); // "2000-01-01"
9191        assert_eq!(date_value, c.value(0));
9192
9193        assert!(c.is_valid(1)); // "2000-01-01T12:00:00"
9194        assert_eq!(date_value, c.value(1));
9195
9196        let date_value = since(
9197            NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
9198            from_ymd(1970, 1, 1).unwrap(),
9199        )
9200        .num_days() as i32;
9201        assert!(c.is_valid(2)); // "2000-2-2"
9202        assert_eq!(date_value, c.value(2));
9203
9204        // test invalid inputs
9205        assert!(!c.is_valid(3)); // "2000-00-00"
9206        assert!(!c.is_valid(4)); // "2000"
9207    }
9208
9209    #[test]
9210    fn test_cast_utf8_to_date64() {
9211        let a = StringArray::from(vec![
9212            "2000-01-01T12:00:00", // date + time valid
9213            "2020-12-15T12:34:56", // date + time valid
9214            "2020-2-2T12:34:56",   // valid date time without leading 0s
9215            "2000-00-00T12:00:00", // invalid month and day
9216            "2000-01-01 12:00:00", // missing the 'T'
9217            "2000-01-01",          // just a date is invalid
9218        ]);
9219        let array = Arc::new(a) as ArrayRef;
9220        let b = cast(&array, &DataType::Date64).unwrap();
9221        let c = b.as_primitive::<Date64Type>();
9222
9223        // test valid inputs
9224        assert!(c.is_valid(0)); // "2000-01-01T12:00:00"
9225        assert_eq!(946728000000, c.value(0));
9226        assert!(c.is_valid(1)); // "2020-12-15T12:34:56"
9227        assert_eq!(1608035696000, c.value(1));
9228        assert!(!c.is_valid(2)); // "2020-2-2T12:34:56"
9229
9230        assert!(!c.is_valid(3)); // "2000-00-00T12:00:00"
9231        assert!(c.is_valid(4)); // "2000-01-01 12:00:00"
9232        assert_eq!(946728000000, c.value(4));
9233        assert!(c.is_valid(5)); // "2000-01-01"
9234        assert_eq!(946684800000, c.value(5));
9235    }
9236
9237    #[test]
9238    fn test_can_cast_fsl_to_fsl() {
9239        let from_array = Arc::new(
9240            FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
9241                [Some([Some(1.0), Some(2.0)]), None],
9242                2,
9243            ),
9244        ) as ArrayRef;
9245        let to_array = Arc::new(
9246            FixedSizeListArray::from_iter_primitive::<Float16Type, _, _>(
9247                [
9248                    Some([Some(f16::from_f32(1.0)), Some(f16::from_f32(2.0))]),
9249                    None,
9250                ],
9251                2,
9252            ),
9253        ) as ArrayRef;
9254
9255        assert!(can_cast_types(from_array.data_type(), to_array.data_type()));
9256        let actual = cast(&from_array, to_array.data_type()).unwrap();
9257        assert_eq!(actual.data_type(), to_array.data_type());
9258
9259        let invalid_target =
9260            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Binary, true)), 2);
9261        assert!(!can_cast_types(from_array.data_type(), &invalid_target));
9262
9263        let invalid_size =
9264            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Float16, true)), 5);
9265        assert!(!can_cast_types(from_array.data_type(), &invalid_size));
9266    }
9267
9268    #[test]
9269    fn test_can_cast_types_fixed_size_list_to_list() {
9270        // DataType::List
9271        let array1 = make_fixed_size_list_array();
9272        assert!(can_cast_types(
9273            array1.data_type(),
9274            &DataType::List(Arc::new(Field::new("", DataType::Int32, false)))
9275        ));
9276
9277        // DataType::LargeList
9278        let array2 = make_fixed_size_list_array_for_large_list();
9279        assert!(can_cast_types(
9280            array2.data_type(),
9281            &DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false)))
9282        ));
9283    }
9284
9285    #[test]
9286    fn test_cast_fixed_size_list_to_list() {
9287        // Important cases:
9288        // 1. With/without nulls
9289        // 2. List/LargeList/ListView/LargeListView
9290        // 3. With and without inner casts
9291
9292        let cases = [
9293            // fixed_size_list<i32, 2> => list<i32>
9294            (
9295                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9296                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9297                    2,
9298                )) as ArrayRef,
9299                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
9300                    Some([Some(1), Some(1)]),
9301                    Some([Some(2), Some(2)]),
9302                ])) as ArrayRef,
9303            ),
9304            // fixed_size_list<i32, 2> => list<i32> (nullable)
9305            (
9306                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9307                    [None, Some([Some(2), Some(2)])],
9308                    2,
9309                )) as ArrayRef,
9310                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
9311                    None,
9312                    Some([Some(2), Some(2)]),
9313                ])) as ArrayRef,
9314            ),
9315            // fixed_size_list<i32, 2> => large_list<i64>
9316            (
9317                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9318                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9319                    2,
9320                )) as ArrayRef,
9321                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
9322                    Some([Some(1), Some(1)]),
9323                    Some([Some(2), Some(2)]),
9324                ])) as ArrayRef,
9325            ),
9326            // fixed_size_list<i32, 2> => large_list<i64> (nullable)
9327            (
9328                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9329                    [None, Some([Some(2), Some(2)])],
9330                    2,
9331                )) as ArrayRef,
9332                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
9333                    None,
9334                    Some([Some(2), Some(2)]),
9335                ])) as ArrayRef,
9336            ),
9337            // fixed_size_list<i32, 2> => list_view<i32>
9338            (
9339                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9340                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9341                    2,
9342                )) as ArrayRef,
9343                Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>([
9344                    Some([Some(1), Some(1)]),
9345                    Some([Some(2), Some(2)]),
9346                ])) as ArrayRef,
9347            ),
9348            // fixed_size_list<i32, 2> => list_view<i32> (nullable)
9349            (
9350                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9351                    [None, Some([Some(2), Some(2)])],
9352                    2,
9353                )) as ArrayRef,
9354                Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>([
9355                    None,
9356                    Some([Some(2), Some(2)]),
9357                ])) as ArrayRef,
9358            ),
9359            // fixed_size_list<i32, 2> => large_list_view<i64>
9360            (
9361                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9362                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9363                    2,
9364                )) as ArrayRef,
9365                Arc::new(LargeListViewArray::from_iter_primitive::<Int64Type, _, _>(
9366                    [Some([Some(1), Some(1)]), Some([Some(2), Some(2)])],
9367                )) as ArrayRef,
9368            ),
9369            // fixed_size_list<i32, 2> => large_list_view<i64> (nullable)
9370            (
9371                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9372                    [None, Some([Some(2), Some(2)])],
9373                    2,
9374                )) as ArrayRef,
9375                Arc::new(LargeListViewArray::from_iter_primitive::<Int64Type, _, _>(
9376                    [None, Some([Some(2), Some(2)])],
9377                )) as ArrayRef,
9378            ),
9379        ];
9380
9381        for (array, expected) in cases {
9382            assert!(
9383                can_cast_types(array.data_type(), expected.data_type()),
9384                "can_cast_types claims we cannot cast {:?} to {:?}",
9385                array.data_type(),
9386                expected.data_type()
9387            );
9388
9389            let list_array = cast(&array, expected.data_type())
9390                .unwrap_or_else(|_| panic!("Failed to cast {array:?} to {expected:?}"));
9391            assert_eq!(
9392                list_array.as_ref(),
9393                &expected,
9394                "Incorrect result from casting {array:?} to {expected:?}",
9395            );
9396        }
9397    }
9398
9399    #[test]
9400    fn test_cast_fixed_size_list_to_list_preserves_field_metadata() {
9401        use std::collections::HashMap;
9402
9403        let metadata: HashMap<String, String> =
9404            HashMap::from([("PARQUET:field_id".to_string(), "89".to_string())]);
9405
9406        let src = Arc::new(
9407            FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
9408                [[1.0_f32, 2.0].map(Some), [3.0, 4.0].map(Some)].map(Some),
9409                2,
9410            ),
9411        ) as ArrayRef;
9412
9413        let target_field = Arc::new(
9414            Field::new("element", DataType::Float32, true).with_metadata(metadata.clone()),
9415        );
9416
9417        let target_types = [
9418            DataType::List(target_field.clone()),
9419            DataType::LargeList(target_field.clone()),
9420            DataType::ListView(target_field.clone()),
9421            DataType::LargeListView(target_field.clone()),
9422        ];
9423
9424        for target_type in &target_types {
9425            let result = cast(&src, target_type).unwrap();
9426            assert_eq!(
9427                result.data_type(),
9428                target_type,
9429                "Cast to {target_type:?} should preserve field metadata"
9430            );
9431        }
9432    }
9433
9434    #[test]
9435    fn test_cast_utf8_to_list() {
9436        // DataType::List
9437        let array = Arc::new(StringArray::from(vec!["5"])) as ArrayRef;
9438        let field = Arc::new(Field::new("", DataType::Int32, false));
9439        let list_array = cast(&array, &DataType::List(field.clone())).unwrap();
9440        let actual = list_array.as_list_opt::<i32>().unwrap();
9441        let expect = ListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
9442        assert_eq!(&expect.value(0), &actual.value(0));
9443
9444        // DataType::LargeList
9445        let list_array = cast(&array, &DataType::LargeList(field.clone())).unwrap();
9446        let actual = list_array.as_list_opt::<i64>().unwrap();
9447        let expect = LargeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
9448        assert_eq!(&expect.value(0), &actual.value(0));
9449
9450        // DataType::FixedSizeList
9451        let list_array = cast(&array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9452        let actual = list_array.as_fixed_size_list_opt().unwrap();
9453        let expect =
9454            FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])], 1);
9455        assert_eq!(&expect.value(0), &actual.value(0));
9456    }
9457
9458    #[test]
9459    fn test_cast_single_element_fixed_size_list() {
9460        // FixedSizeList<T>[1] => T
9461        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9462            [(Some([Some(5)]))],
9463            1,
9464        )) as ArrayRef;
9465        let casted_array = cast(&from_array, &DataType::Int32).unwrap();
9466        let actual: &Int32Array = casted_array.as_primitive();
9467        let expected = Int32Array::from(vec![Some(5)]);
9468        assert_eq!(&expected, actual);
9469
9470        // FixedSizeList<T>[1] => FixedSizeList<U>[1]
9471        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9472            [(Some([Some(5)]))],
9473            1,
9474        )) as ArrayRef;
9475        let to_field = Arc::new(Field::new("dummy", DataType::Float32, false));
9476        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
9477        let expected = Arc::new(FixedSizeListArray::new(
9478            to_field.clone(),
9479            1,
9480            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9481            None,
9482        )) as ArrayRef;
9483        assert_eq!(*expected, *actual);
9484
9485        // FixedSizeList<T>[1] => FixedSizeList<FixdSizedList<U>[1]>[1]
9486        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9487            [(Some([Some(5)]))],
9488            1,
9489        )) as ArrayRef;
9490        let to_field_inner = Arc::new(Field::new_list_field(DataType::Float32, false));
9491        let to_field = Arc::new(Field::new(
9492            "dummy",
9493            DataType::FixedSizeList(to_field_inner.clone(), 1),
9494            false,
9495        ));
9496        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
9497        let expected = Arc::new(FixedSizeListArray::new(
9498            to_field.clone(),
9499            1,
9500            Arc::new(FixedSizeListArray::new(
9501                to_field_inner.clone(),
9502                1,
9503                Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9504                None,
9505            )) as ArrayRef,
9506            None,
9507        )) as ArrayRef;
9508        assert_eq!(*expected, *actual);
9509
9510        // T => FixedSizeList<T>[1] (non-nullable)
9511        let field = Arc::new(Field::new("dummy", DataType::Float32, false));
9512        let from_array = Arc::new(Int8Array::from(vec![Some(5)])) as ArrayRef;
9513        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9514        let actual = casted_array.as_fixed_size_list();
9515        let expected = Arc::new(FixedSizeListArray::new(
9516            field.clone(),
9517            1,
9518            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9519            None,
9520        )) as ArrayRef;
9521        assert_eq!(expected.as_ref(), actual);
9522
9523        // T => FixedSizeList<T>[1] (nullable)
9524        let field = Arc::new(Field::new("nullable", DataType::Float32, true));
9525        let from_array = Arc::new(Int8Array::from(vec![None])) as ArrayRef;
9526        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9527        let actual = casted_array.as_fixed_size_list();
9528        let expected = Arc::new(FixedSizeListArray::new(
9529            field.clone(),
9530            1,
9531            Arc::new(Float32Array::from(vec![None])) as ArrayRef,
9532            None,
9533        )) as ArrayRef;
9534        assert_eq!(expected.as_ref(), actual);
9535    }
9536
9537    #[test]
9538    fn test_cast_list_containers() {
9539        // large-list to list
9540        let array = make_large_list_array();
9541        let list_array = cast(
9542            &array,
9543            &DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
9544        )
9545        .unwrap();
9546        let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
9547        let expected = array.as_any().downcast_ref::<LargeListArray>().unwrap();
9548
9549        assert_eq!(&expected.value(0), &actual.value(0));
9550        assert_eq!(&expected.value(1), &actual.value(1));
9551        assert_eq!(&expected.value(2), &actual.value(2));
9552
9553        // list to large-list
9554        let array = make_list_array();
9555        let large_list_array = cast(
9556            &array,
9557            &DataType::LargeList(Arc::new(Field::new("", DataType::Int32, false))),
9558        )
9559        .unwrap();
9560        let actual = large_list_array
9561            .as_any()
9562            .downcast_ref::<LargeListArray>()
9563            .unwrap();
9564        let expected = array.as_any().downcast_ref::<ListArray>().unwrap();
9565
9566        assert_eq!(&expected.value(0), &actual.value(0));
9567        assert_eq!(&expected.value(1), &actual.value(1));
9568        assert_eq!(&expected.value(2), &actual.value(2));
9569    }
9570
9571    #[test]
9572    fn test_cast_list_view() {
9573        // cast between list view and list view
9574        let array = make_list_view_array();
9575        let to = DataType::ListView(Field::new_list_field(DataType::Float32, true).into());
9576        assert!(can_cast_types(array.data_type(), &to));
9577        let actual = cast(&array, &to).unwrap();
9578        let actual = actual.as_list_view::<i32>();
9579
9580        assert_eq!(
9581            &Float32Array::from(vec![0.0, 1.0, 2.0]) as &dyn Array,
9582            actual.value(0).as_ref()
9583        );
9584        assert_eq!(
9585            &Float32Array::from(vec![3.0, 4.0, 5.0]) as &dyn Array,
9586            actual.value(1).as_ref()
9587        );
9588        assert_eq!(
9589            &Float32Array::from(vec![6.0, 7.0]) as &dyn Array,
9590            actual.value(2).as_ref()
9591        );
9592
9593        // cast between large list view and large list view
9594        let array = make_large_list_view_array();
9595        let to = DataType::LargeListView(Field::new_list_field(DataType::Float32, true).into());
9596        assert!(can_cast_types(array.data_type(), &to));
9597        let actual = cast(&array, &to).unwrap();
9598        let actual = actual.as_list_view::<i64>();
9599
9600        assert_eq!(
9601            &Float32Array::from(vec![0.0, 1.0, 2.0]) as &dyn Array,
9602            actual.value(0).as_ref()
9603        );
9604        assert_eq!(
9605            &Float32Array::from(vec![3.0, 4.0, 5.0]) as &dyn Array,
9606            actual.value(1).as_ref()
9607        );
9608        assert_eq!(
9609            &Float32Array::from(vec![6.0, 7.0]) as &dyn Array,
9610            actual.value(2).as_ref()
9611        );
9612    }
9613
9614    #[test]
9615    fn test_non_list_to_list_view() {
9616        let input = Arc::new(Int32Array::from(vec![Some(0), None, Some(2)])) as ArrayRef;
9617        let expected_primitive =
9618            Arc::new(Float32Array::from(vec![Some(0.0), None, Some(2.0)])) as ArrayRef;
9619
9620        // [[0], [NULL], [2]]
9621        let expected = ListViewArray::new(
9622            Field::new_list_field(DataType::Float32, true).into(),
9623            vec![0, 1, 2].into(),
9624            vec![1, 1, 1].into(),
9625            expected_primitive.clone(),
9626            None,
9627        );
9628        assert!(can_cast_types(input.data_type(), expected.data_type()));
9629        let actual = cast(&input, expected.data_type()).unwrap();
9630        assert_eq!(actual.as_ref(), &expected);
9631
9632        // [[0], [NULL], [2]]
9633        let expected = LargeListViewArray::new(
9634            Field::new_list_field(DataType::Float32, true).into(),
9635            vec![0, 1, 2].into(),
9636            vec![1, 1, 1].into(),
9637            expected_primitive.clone(),
9638            None,
9639        );
9640        assert!(can_cast_types(input.data_type(), expected.data_type()));
9641        let actual = cast(&input, expected.data_type()).unwrap();
9642        assert_eq!(actual.as_ref(), &expected);
9643    }
9644
9645    #[test]
9646    fn test_cast_list_to_fsl() {
9647        // There four noteworthy cases we should handle:
9648        // 1. No nulls
9649        // 2. Nulls that are always empty
9650        // 3. Nulls that have varying lengths
9651        // 4. Nulls that are correctly sized (same as target list size)
9652
9653        // Non-null case
9654        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9655        let values = vec![
9656            Some(vec![Some(1), Some(2), Some(3)]),
9657            Some(vec![Some(4), Some(5), Some(6)]),
9658        ];
9659        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
9660            values.clone(),
9661        )) as ArrayRef;
9662        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9663            values, 3,
9664        )) as ArrayRef;
9665        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9666        assert_eq!(expected.as_ref(), actual.as_ref());
9667
9668        // Null cases
9669        // Array is [[1, 2, 3], null, [4, 5, 6], null]
9670        let cases = [
9671            (
9672                // Zero-length nulls
9673                vec![1, 2, 3, 4, 5, 6],
9674                vec![3, 0, 3, 0],
9675            ),
9676            (
9677                // Varying-length nulls
9678                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
9679                vec![3, 2, 3, 1],
9680            ),
9681            (
9682                // Correctly-sized nulls
9683                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
9684                vec![3, 3, 3, 3],
9685            ),
9686            (
9687                // Mixed nulls
9688                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
9689                vec![3, 0, 3, 3],
9690            ),
9691        ];
9692        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
9693
9694        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9695            vec![
9696                Some(vec![Some(1), Some(2), Some(3)]),
9697                None,
9698                Some(vec![Some(4), Some(5), Some(6)]),
9699                None,
9700            ],
9701            3,
9702        )) as ArrayRef;
9703
9704        for (values, lengths) in cases.iter() {
9705            let array = Arc::new(ListArray::new(
9706                field.clone(),
9707                OffsetBuffer::from_lengths(lengths.clone()),
9708                Arc::new(Int32Array::from(values.clone())),
9709                Some(null_buffer.clone()),
9710            )) as ArrayRef;
9711            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9712            assert_eq!(expected.as_ref(), actual.as_ref());
9713        }
9714    }
9715
9716    #[test]
9717    fn test_cast_list_view_to_fsl() {
9718        // There four noteworthy cases we should handle:
9719        // 1. No nulls
9720        // 2. Nulls that are always empty
9721        // 3. Nulls that have varying lengths
9722        // 4. Nulls that are correctly sized (same as target list size)
9723
9724        // Non-null case
9725        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9726        let values = vec![
9727            Some(vec![Some(1), Some(2), Some(3)]),
9728            Some(vec![Some(4), Some(5), Some(6)]),
9729        ];
9730        let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(
9731            values.clone(),
9732        )) as ArrayRef;
9733        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9734            values, 3,
9735        )) as ArrayRef;
9736        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9737        assert_eq!(expected.as_ref(), actual.as_ref());
9738
9739        // Null cases
9740        // Array is [[1, 2, 3], null, [4, 5, 6], null]
9741        let cases = [
9742            (
9743                // Zero-length nulls
9744                vec![1, 2, 3, 4, 5, 6],
9745                vec![0, 0, 3, 0],
9746                vec![3, 0, 3, 0],
9747            ),
9748            (
9749                // Varying-length nulls
9750                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
9751                vec![0, 1, 5, 0],
9752                vec![3, 2, 3, 1],
9753            ),
9754            (
9755                // Correctly-sized nulls
9756                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
9757                vec![0, 3, 6, 9],
9758                vec![3, 3, 3, 3],
9759            ),
9760            (
9761                // Mixed nulls
9762                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
9763                vec![0, 0, 3, 6],
9764                vec![3, 0, 3, 3],
9765            ),
9766        ];
9767        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
9768
9769        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9770            vec![
9771                Some(vec![Some(1), Some(2), Some(3)]),
9772                None,
9773                Some(vec![Some(4), Some(5), Some(6)]),
9774                None,
9775            ],
9776            3,
9777        )) as ArrayRef;
9778
9779        for (values, offsets, lengths) in cases.iter() {
9780            let array = Arc::new(ListViewArray::new(
9781                field.clone(),
9782                offsets.clone().into(),
9783                lengths.clone().into(),
9784                Arc::new(Int32Array::from(values.clone())),
9785                Some(null_buffer.clone()),
9786            )) as ArrayRef;
9787            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9788            assert_eq!(expected.as_ref(), actual.as_ref());
9789        }
9790    }
9791
9792    #[test]
9793    fn test_cast_list_to_fsl_safety() {
9794        let values = vec![
9795            Some(vec![Some(1), Some(2), Some(3)]),
9796            Some(vec![Some(4), Some(5)]),
9797            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
9798            Some(vec![Some(3), Some(4), Some(5)]),
9799        ];
9800        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
9801            values.clone(),
9802        )) as ArrayRef;
9803
9804        let res = cast_with_options(
9805            array.as_ref(),
9806            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9807            &CastOptions {
9808                safe: false,
9809                ..Default::default()
9810            },
9811        );
9812        assert!(res.is_err());
9813        assert!(
9814            format!("{res:?}")
9815                .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
9816        );
9817
9818        // When safe=true (default), the cast will fill nulls for lists that are
9819        // too short and truncate lists that are too long.
9820        let res = cast(
9821            array.as_ref(),
9822            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9823        )
9824        .unwrap();
9825        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9826            vec![
9827                Some(vec![Some(1), Some(2), Some(3)]),
9828                None, // Too short -> replaced with null
9829                None, // Too long -> replaced with null
9830                Some(vec![Some(3), Some(4), Some(5)]),
9831            ],
9832            3,
9833        )) as ArrayRef;
9834        assert_eq!(expected.as_ref(), res.as_ref());
9835
9836        // The safe option is false and the source array contains a null list.
9837        // issue: https://github.com/apache/arrow-rs/issues/5642
9838        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
9839            Some(vec![Some(1), Some(2), Some(3)]),
9840            None,
9841        ])) as ArrayRef;
9842        let res = cast_with_options(
9843            array.as_ref(),
9844            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9845            &CastOptions {
9846                safe: false,
9847                ..Default::default()
9848            },
9849        )
9850        .unwrap();
9851        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9852            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
9853            3,
9854        )) as ArrayRef;
9855        assert_eq!(expected.as_ref(), res.as_ref());
9856    }
9857
9858    #[test]
9859    fn test_cast_list_view_to_fsl_safety() {
9860        let values = vec![
9861            Some(vec![Some(1), Some(2), Some(3)]),
9862            Some(vec![Some(4), Some(5)]),
9863            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
9864            Some(vec![Some(3), Some(4), Some(5)]),
9865        ];
9866        let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(
9867            values.clone(),
9868        )) as ArrayRef;
9869
9870        let res = cast_with_options(
9871            array.as_ref(),
9872            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9873            &CastOptions {
9874                safe: false,
9875                ..Default::default()
9876            },
9877        );
9878        assert!(res.is_err());
9879        assert!(
9880            format!("{res:?}")
9881                .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
9882        );
9883
9884        // When safe=true (default), the cast will fill nulls for lists that are
9885        // too short and truncate lists that are too long.
9886        let res = cast(
9887            array.as_ref(),
9888            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9889        )
9890        .unwrap();
9891        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9892            vec![
9893                Some(vec![Some(1), Some(2), Some(3)]),
9894                None, // Too short -> replaced with null
9895                None, // Too long -> replaced with null
9896                Some(vec![Some(3), Some(4), Some(5)]),
9897            ],
9898            3,
9899        )) as ArrayRef;
9900        assert_eq!(expected.as_ref(), res.as_ref());
9901
9902        // The safe option is false and the source array contains a null list.
9903        // issue: https://github.com/apache/arrow-rs/issues/5642
9904        let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(vec![
9905            Some(vec![Some(1), Some(2), Some(3)]),
9906            None,
9907        ])) as ArrayRef;
9908        let res = cast_with_options(
9909            array.as_ref(),
9910            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9911            &CastOptions {
9912                safe: false,
9913                ..Default::default()
9914            },
9915        )
9916        .unwrap();
9917        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9918            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
9919            3,
9920        )) as ArrayRef;
9921        assert_eq!(expected.as_ref(), res.as_ref());
9922    }
9923
9924    #[test]
9925    fn test_cast_large_list_to_fsl() {
9926        let values = vec![Some(vec![Some(1), Some(2)]), Some(vec![Some(3), Some(4)])];
9927        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9928            values.clone(),
9929            2,
9930        )) as ArrayRef;
9931        let target_type =
9932            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 2);
9933
9934        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
9935            values.clone(),
9936        )) as ArrayRef;
9937        let actual = cast(array.as_ref(), &target_type).unwrap();
9938        assert_eq!(expected.as_ref(), actual.as_ref());
9939
9940        let array = Arc::new(LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(
9941            values.clone(),
9942        )) as ArrayRef;
9943        let actual = cast(array.as_ref(), &target_type).unwrap();
9944        assert_eq!(expected.as_ref(), actual.as_ref());
9945    }
9946
9947    #[test]
9948    fn test_cast_list_to_fsl_subcast() {
9949        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
9950            vec![
9951                Some(vec![Some(1), Some(2)]),
9952                Some(vec![Some(3), Some(i32::MAX)]),
9953            ],
9954        )) as ArrayRef;
9955        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
9956            vec![
9957                Some(vec![Some(1), Some(2)]),
9958                Some(vec![Some(3), Some(i32::MAX as i64)]),
9959            ],
9960            2,
9961        )) as ArrayRef;
9962        let actual = cast(
9963            array.as_ref(),
9964            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 2),
9965        )
9966        .unwrap();
9967        assert_eq!(expected.as_ref(), actual.as_ref());
9968
9969        let res = cast_with_options(
9970            array.as_ref(),
9971            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int16, true)), 2),
9972            &CastOptions {
9973                safe: false,
9974                ..Default::default()
9975            },
9976        );
9977        assert!(res.is_err());
9978        assert!(format!("{res:?}").contains("Can't cast value 2147483647 to type Int16"));
9979    }
9980
9981    #[test]
9982    fn test_cast_list_to_fsl_empty() {
9983        let inner_field = Arc::new(Field::new_list_field(DataType::Int32, true));
9984        let target_type = DataType::FixedSizeList(inner_field.clone(), 3);
9985        let expected = new_empty_array(&target_type);
9986
9987        // list
9988        let array = new_empty_array(&DataType::List(inner_field.clone()));
9989        assert!(can_cast_types(array.data_type(), &target_type));
9990        let actual = cast(array.as_ref(), &target_type).unwrap();
9991        assert_eq!(expected.as_ref(), actual.as_ref());
9992
9993        // largelist
9994        let array = new_empty_array(&DataType::LargeList(inner_field.clone()));
9995        assert!(can_cast_types(array.data_type(), &target_type));
9996        let actual = cast(array.as_ref(), &target_type).unwrap();
9997        assert_eq!(expected.as_ref(), actual.as_ref());
9998
9999        // listview
10000        let array = new_empty_array(&DataType::ListView(inner_field.clone()));
10001        assert!(can_cast_types(array.data_type(), &target_type));
10002        let actual = cast(array.as_ref(), &target_type).unwrap();
10003        assert_eq!(expected.as_ref(), actual.as_ref());
10004
10005        // largelistview
10006        let array = new_empty_array(&DataType::LargeListView(inner_field.clone()));
10007        assert!(can_cast_types(array.data_type(), &target_type));
10008        let actual = cast(array.as_ref(), &target_type).unwrap();
10009        assert_eq!(expected.as_ref(), actual.as_ref());
10010    }
10011
10012    fn make_list_array() -> ArrayRef {
10013        // [[0, 1, 2], [3, 4, 5], [6, 7]]
10014        Arc::new(ListArray::new(
10015            Field::new_list_field(DataType::Int32, true).into(),
10016            OffsetBuffer::from_lengths(vec![3, 3, 2]),
10017            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10018            None,
10019        ))
10020    }
10021
10022    fn make_large_list_array() -> ArrayRef {
10023        // [[0, 1, 2], [3, 4, 5], [6, 7]]
10024        Arc::new(LargeListArray::new(
10025            Field::new_list_field(DataType::Int32, true).into(),
10026            OffsetBuffer::from_lengths(vec![3, 3, 2]),
10027            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10028            None,
10029        ))
10030    }
10031
10032    fn make_list_view_array() -> ArrayRef {
10033        // [[0, 1, 2], [3, 4, 5], [6, 7]]
10034        Arc::new(ListViewArray::new(
10035            Field::new_list_field(DataType::Int32, true).into(),
10036            vec![0, 3, 6].into(),
10037            vec![3, 3, 2].into(),
10038            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10039            None,
10040        ))
10041    }
10042
10043    fn make_large_list_view_array() -> ArrayRef {
10044        // [[0, 1, 2], [3, 4, 5], [6, 7]]
10045        Arc::new(LargeListViewArray::new(
10046            Field::new_list_field(DataType::Int32, true).into(),
10047            vec![0, 3, 6].into(),
10048            vec![3, 3, 2].into(),
10049            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10050            None,
10051        ))
10052    }
10053
10054    fn make_fixed_size_list_array() -> ArrayRef {
10055        // [[0, 1, 2, 3], [4, 5, 6, 7]]
10056        Arc::new(FixedSizeListArray::new(
10057            Field::new_list_field(DataType::Int32, true).into(),
10058            4,
10059            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10060            None,
10061        ))
10062    }
10063
10064    fn make_fixed_size_list_array_for_large_list() -> ArrayRef {
10065        // [[0, 1, 2, 3], [4, 5, 6, 7]]
10066        Arc::new(FixedSizeListArray::new(
10067            Field::new_list_field(DataType::Int64, true).into(),
10068            4,
10069            Arc::new(Int64Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10070            None,
10071        ))
10072    }
10073
10074    #[test]
10075    fn test_cast_map_dont_allow_change_of_order() {
10076        let string_builder = StringBuilder::new();
10077        let value_builder = StringBuilder::new();
10078        let mut builder = MapBuilder::new(
10079            Some(MapFieldNames {
10080                entry: "entries".to_string(),
10081                key: "key".to_string(),
10082                value: "value".to_string(),
10083            }),
10084            string_builder,
10085            value_builder,
10086        );
10087
10088        builder.keys().append_value("0");
10089        builder.values().append_value("test_val_1");
10090        builder.append(true).unwrap();
10091        builder.keys().append_value("1");
10092        builder.values().append_value("test_val_2");
10093        builder.append(true).unwrap();
10094
10095        // map builder returns unsorted map by default
10096        let array = builder.finish();
10097
10098        let new_ordered = true;
10099        let new_type = DataType::Map(
10100            Arc::new(Field::new(
10101                "entries",
10102                DataType::Struct(
10103                    vec![
10104                        Field::new("key", DataType::Utf8, false),
10105                        Field::new("value", DataType::Utf8, false),
10106                    ]
10107                    .into(),
10108                ),
10109                false,
10110            )),
10111            new_ordered,
10112        );
10113
10114        let new_array_result = cast(&array, &new_type.clone());
10115        assert!(!can_cast_types(array.data_type(), &new_type));
10116        let Err(ArrowError::CastError(t)) = new_array_result else {
10117            panic!();
10118        };
10119        assert_eq!(
10120            t,
10121            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"#
10122        );
10123    }
10124
10125    #[test]
10126    fn test_cast_map_dont_allow_when_container_cant_cast() {
10127        let string_builder = StringBuilder::new();
10128        let value_builder = IntervalDayTimeArray::builder(2);
10129        let mut builder = MapBuilder::new(
10130            Some(MapFieldNames {
10131                entry: "entries".to_string(),
10132                key: "key".to_string(),
10133                value: "value".to_string(),
10134            }),
10135            string_builder,
10136            value_builder,
10137        );
10138
10139        builder.keys().append_value("0");
10140        builder.values().append_value(IntervalDayTime::new(1, 1));
10141        builder.append(true).unwrap();
10142        builder.keys().append_value("1");
10143        builder.values().append_value(IntervalDayTime::new(2, 2));
10144        builder.append(true).unwrap();
10145
10146        // map builder returns unsorted map by default
10147        let array = builder.finish();
10148
10149        let new_ordered = true;
10150        let new_type = DataType::Map(
10151            Arc::new(Field::new(
10152                "entries",
10153                DataType::Struct(
10154                    vec![
10155                        Field::new("key", DataType::Utf8, false),
10156                        Field::new("value", DataType::Duration(TimeUnit::Second), false),
10157                    ]
10158                    .into(),
10159                ),
10160                false,
10161            )),
10162            new_ordered,
10163        );
10164
10165        let new_array_result = cast(&array, &new_type.clone());
10166        assert!(!can_cast_types(array.data_type(), &new_type));
10167        let Err(ArrowError::CastError(t)) = new_array_result else {
10168            panic!();
10169        };
10170        assert_eq!(
10171            t,
10172            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"#
10173        );
10174    }
10175
10176    #[test]
10177    fn test_cast_map_field_names() {
10178        let string_builder = StringBuilder::new();
10179        let value_builder = StringBuilder::new();
10180        let mut builder = MapBuilder::new(
10181            Some(MapFieldNames {
10182                entry: "entries".to_string(),
10183                key: "key".to_string(),
10184                value: "value".to_string(),
10185            }),
10186            string_builder,
10187            value_builder,
10188        );
10189
10190        builder.keys().append_value("0");
10191        builder.values().append_value("test_val_1");
10192        builder.append(true).unwrap();
10193        builder.keys().append_value("1");
10194        builder.values().append_value("test_val_2");
10195        builder.append(true).unwrap();
10196        builder.append(false).unwrap();
10197
10198        let array = builder.finish();
10199
10200        let new_type = DataType::Map(
10201            Arc::new(Field::new(
10202                "entries_new",
10203                DataType::Struct(
10204                    vec![
10205                        Field::new("key_new", DataType::Utf8, false),
10206                        Field::new("value_values", DataType::Utf8, false),
10207                    ]
10208                    .into(),
10209                ),
10210                false,
10211            )),
10212            false,
10213        );
10214
10215        assert_ne!(new_type, array.data_type().clone());
10216
10217        let new_array = cast(&array, &new_type.clone()).unwrap();
10218        assert_eq!(new_type, new_array.data_type().clone());
10219        let map_array = new_array.as_map();
10220
10221        assert_ne!(new_type, array.data_type().clone());
10222        assert_eq!(new_type, map_array.data_type().clone());
10223
10224        let key_string = map_array
10225            .keys()
10226            .as_any()
10227            .downcast_ref::<StringArray>()
10228            .unwrap()
10229            .into_iter()
10230            .flatten()
10231            .collect::<Vec<_>>();
10232        assert_eq!(&key_string, &vec!["0", "1"]);
10233
10234        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
10235        let values_string = values_string_array
10236            .as_any()
10237            .downcast_ref::<StringArray>()
10238            .unwrap()
10239            .into_iter()
10240            .flatten()
10241            .collect::<Vec<_>>();
10242        assert_eq!(&values_string, &vec!["test_val_1", "test_val_2"]);
10243
10244        assert_eq!(
10245            map_array.nulls(),
10246            Some(&NullBuffer::from(vec![true, true, false]))
10247        );
10248    }
10249
10250    #[test]
10251    fn test_cast_map_contained_values() {
10252        let string_builder = StringBuilder::new();
10253        let value_builder = Int8Builder::new();
10254        let mut builder = MapBuilder::new(
10255            Some(MapFieldNames {
10256                entry: "entries".to_string(),
10257                key: "key".to_string(),
10258                value: "value".to_string(),
10259            }),
10260            string_builder,
10261            value_builder,
10262        );
10263
10264        builder.keys().append_value("0");
10265        builder.values().append_value(44);
10266        builder.append(true).unwrap();
10267        builder.keys().append_value("1");
10268        builder.values().append_value(22);
10269        builder.append(true).unwrap();
10270
10271        let array = builder.finish();
10272
10273        let new_type = DataType::Map(
10274            Arc::new(Field::new(
10275                "entries",
10276                DataType::Struct(
10277                    vec![
10278                        Field::new("key", DataType::Utf8, false),
10279                        Field::new("value", DataType::Utf8, false),
10280                    ]
10281                    .into(),
10282                ),
10283                false,
10284            )),
10285            false,
10286        );
10287
10288        let new_array = cast(&array, &new_type.clone()).unwrap();
10289        assert_eq!(new_type, new_array.data_type().clone());
10290        let map_array = new_array.as_map();
10291
10292        assert_ne!(new_type, array.data_type().clone());
10293        assert_eq!(new_type, map_array.data_type().clone());
10294
10295        let key_string = map_array
10296            .keys()
10297            .as_any()
10298            .downcast_ref::<StringArray>()
10299            .unwrap()
10300            .into_iter()
10301            .flatten()
10302            .collect::<Vec<_>>();
10303        assert_eq!(&key_string, &vec!["0", "1"]);
10304
10305        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
10306        let values_string = values_string_array
10307            .as_any()
10308            .downcast_ref::<StringArray>()
10309            .unwrap()
10310            .into_iter()
10311            .flatten()
10312            .collect::<Vec<_>>();
10313        assert_eq!(&values_string, &vec!["44", "22"]);
10314    }
10315
10316    #[test]
10317    fn test_utf8_cast_offsets() {
10318        // test if offset of the array is taken into account during cast
10319        let str_array = StringArray::from(vec!["a", "b", "c"]);
10320        let str_array = str_array.slice(1, 2);
10321
10322        let out = cast(&str_array, &DataType::LargeUtf8).unwrap();
10323
10324        let large_str_array = out.as_any().downcast_ref::<LargeStringArray>().unwrap();
10325        let strs = large_str_array.into_iter().flatten().collect::<Vec<_>>();
10326        assert_eq!(strs, &["b", "c"])
10327    }
10328
10329    #[test]
10330    fn test_list_cast_offsets() {
10331        // test if offset of the array is taken into account during cast
10332        let array1 = make_list_array().slice(1, 2);
10333        let array2 = make_list_array();
10334
10335        let dt = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
10336        let out1 = cast(&array1, &dt).unwrap();
10337        let out2 = cast(&array2, &dt).unwrap();
10338
10339        assert_eq!(&out1, &out2.slice(1, 2))
10340    }
10341
10342    #[test]
10343    fn test_list_to_string() {
10344        fn assert_cast(array: &ArrayRef, expected: &[&str]) {
10345            assert!(can_cast_types(array.data_type(), &DataType::Utf8));
10346            let out = cast(array, &DataType::Utf8).unwrap();
10347            let out = out
10348                .as_string::<i32>()
10349                .into_iter()
10350                .flatten()
10351                .collect::<Vec<_>>();
10352            assert_eq!(out, expected);
10353
10354            assert!(can_cast_types(array.data_type(), &DataType::LargeUtf8));
10355            let out = cast(array, &DataType::LargeUtf8).unwrap();
10356            let out = out
10357                .as_string::<i64>()
10358                .into_iter()
10359                .flatten()
10360                .collect::<Vec<_>>();
10361            assert_eq!(out, expected);
10362
10363            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
10364            let out = cast(array, &DataType::Utf8View).unwrap();
10365            let out = out
10366                .as_string_view()
10367                .into_iter()
10368                .flatten()
10369                .collect::<Vec<_>>();
10370            assert_eq!(out, expected);
10371        }
10372
10373        let array = Arc::new(ListArray::new(
10374            Field::new_list_field(DataType::Utf8, true).into(),
10375            OffsetBuffer::from_lengths(vec![3, 3, 2]),
10376            Arc::new(StringArray::from(vec![
10377                "a", "b", "c", "d", "e", "f", "g", "h",
10378            ])),
10379            None,
10380        )) as ArrayRef;
10381
10382        assert_cast(&array, &["[a, b, c]", "[d, e, f]", "[g, h]"]);
10383
10384        let array = make_list_array();
10385        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10386
10387        let array = make_large_list_array();
10388        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10389
10390        let array = make_list_view_array();
10391        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10392
10393        let array = make_large_list_view_array();
10394        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10395    }
10396
10397    #[test]
10398    fn test_cast_f64_to_decimal128() {
10399        // to reproduce https://github.com/apache/arrow-rs/issues/2997
10400
10401        let decimal_type = DataType::Decimal128(18, 2);
10402        let array = Float64Array::from(vec![
10403            Some(0.0699999999),
10404            Some(0.0659999999),
10405            Some(0.0650000000),
10406            Some(0.0649999999),
10407        ]);
10408        let array = Arc::new(array) as ArrayRef;
10409        generate_cast_test_case!(
10410            &array,
10411            Decimal128Array,
10412            &decimal_type,
10413            vec![
10414                Some(7_i128), // round up
10415                Some(7_i128), // round up
10416                Some(7_i128), // round up
10417                Some(6_i128), // round down
10418            ]
10419        );
10420
10421        let decimal_type = DataType::Decimal128(18, 3);
10422        let array = Float64Array::from(vec![
10423            Some(0.0699999999),
10424            Some(0.0659999999),
10425            Some(0.0650000000),
10426            Some(0.0649999999),
10427        ]);
10428        let array = Arc::new(array) as ArrayRef;
10429        generate_cast_test_case!(
10430            &array,
10431            Decimal128Array,
10432            &decimal_type,
10433            vec![
10434                Some(70_i128), // round up
10435                Some(66_i128), // round up
10436                Some(65_i128), // round down
10437                Some(65_i128), // round up
10438            ]
10439        );
10440    }
10441
10442    #[test]
10443    fn test_cast_numeric_to_decimal128_overflow() {
10444        let array = Int64Array::from(vec![i64::MAX]);
10445        let array = Arc::new(array) as ArrayRef;
10446        let casted_array = cast_with_options(
10447            &array,
10448            &DataType::Decimal128(38, 30),
10449            &CastOptions {
10450                safe: true,
10451                format_options: FormatOptions::default(),
10452            },
10453        );
10454        assert!(casted_array.is_ok());
10455        assert!(casted_array.unwrap().is_null(0));
10456
10457        let casted_array = cast_with_options(
10458            &array,
10459            &DataType::Decimal128(38, 30),
10460            &CastOptions {
10461                safe: false,
10462                format_options: FormatOptions::default(),
10463            },
10464        );
10465        assert!(casted_array.is_err());
10466    }
10467
10468    #[test]
10469    fn test_cast_numeric_to_decimal256_overflow() {
10470        let array = Int64Array::from(vec![i64::MAX]);
10471        let array = Arc::new(array) as ArrayRef;
10472        let casted_array = cast_with_options(
10473            &array,
10474            &DataType::Decimal256(76, 76),
10475            &CastOptions {
10476                safe: true,
10477                format_options: FormatOptions::default(),
10478            },
10479        );
10480        assert!(casted_array.is_ok());
10481        assert!(casted_array.unwrap().is_null(0));
10482
10483        let casted_array = cast_with_options(
10484            &array,
10485            &DataType::Decimal256(76, 76),
10486            &CastOptions {
10487                safe: false,
10488                format_options: FormatOptions::default(),
10489            },
10490        );
10491        assert!(casted_array.is_err());
10492    }
10493
10494    #[test]
10495    fn test_cast_floating_point_to_decimal128_precision_overflow() {
10496        let array = Float64Array::from(vec![1.1]);
10497        let array = Arc::new(array) as ArrayRef;
10498        let casted_array = cast_with_options(
10499            &array,
10500            &DataType::Decimal128(2, 2),
10501            &CastOptions {
10502                safe: true,
10503                format_options: FormatOptions::default(),
10504            },
10505        );
10506        assert!(casted_array.is_ok());
10507        assert!(casted_array.unwrap().is_null(0));
10508
10509        let casted_array = cast_with_options(
10510            &array,
10511            &DataType::Decimal128(2, 2),
10512            &CastOptions {
10513                safe: false,
10514                format_options: FormatOptions::default(),
10515            },
10516        );
10517        let err = casted_array.unwrap_err().to_string();
10518        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
10519        assert!(
10520            err.contains(expected_error),
10521            "did not find expected error '{expected_error}' in actual error '{err}'"
10522        );
10523    }
10524
10525    #[test]
10526    fn test_cast_float16_to_decimal128_precision_overflow() {
10527        let array = Float16Array::from(vec![f16::from_f32(1.1)]);
10528        let array = Arc::new(array) as ArrayRef;
10529        let casted_array = cast_with_options(
10530            &array,
10531            &DataType::Decimal128(2, 2),
10532            &CastOptions {
10533                safe: true,
10534                format_options: FormatOptions::default(),
10535            },
10536        );
10537        assert!(casted_array.is_ok());
10538        assert!(casted_array.unwrap().is_null(0));
10539
10540        let casted_array = cast_with_options(
10541            &array,
10542            &DataType::Decimal128(2, 2),
10543            &CastOptions {
10544                safe: false,
10545                format_options: FormatOptions::default(),
10546            },
10547        );
10548        let err = casted_array.unwrap_err().to_string();
10549        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
10550        assert_eq!(err, expected_error);
10551    }
10552
10553    #[test]
10554    fn test_cast_float16_to_decimal256_precision_overflow() {
10555        let array = Float16Array::from(vec![f16::from_f32(1.1)]);
10556        let array = Arc::new(array) as ArrayRef;
10557        let casted_array = cast_with_options(
10558            &array,
10559            &DataType::Decimal256(2, 2),
10560            &CastOptions {
10561                safe: true,
10562                format_options: FormatOptions::default(),
10563            },
10564        );
10565        assert!(casted_array.is_ok());
10566        assert!(casted_array.unwrap().is_null(0));
10567
10568        let casted_array = cast_with_options(
10569            &array,
10570            &DataType::Decimal256(2, 2),
10571            &CastOptions {
10572                safe: false,
10573                format_options: FormatOptions::default(),
10574            },
10575        );
10576        let err = casted_array.unwrap_err().to_string();
10577        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
10578        assert_eq!(err, expected_error);
10579    }
10580
10581    #[test]
10582    fn test_cast_float16_to_decimal128_non_finite() {
10583        let array = Float16Array::from(vec![f16::NAN, f16::INFINITY, f16::NEG_INFINITY]);
10584        let array = Arc::new(array) as ArrayRef;
10585        let casted_array = cast_with_options(
10586            &array,
10587            &DataType::Decimal128(38, 2),
10588            &CastOptions {
10589                safe: true,
10590                format_options: FormatOptions::default(),
10591            },
10592        )
10593        .unwrap();
10594
10595        assert!(casted_array.is_null(0));
10596        assert!(casted_array.is_null(1));
10597        assert!(casted_array.is_null(2));
10598
10599        let casted_array = cast_with_options(
10600            &array,
10601            &DataType::Decimal128(38, 2),
10602            &CastOptions {
10603                safe: false,
10604                format_options: FormatOptions::default(),
10605            },
10606        );
10607        let err = casted_array.unwrap_err().to_string();
10608        let expected_error = "Cannot cast to Decimal128(38, 2)";
10609        assert!(
10610            err.contains(expected_error),
10611            "did not find expected error '{expected_error}' in actual error '{err}'"
10612        );
10613    }
10614
10615    #[test]
10616    fn test_cast_floating_point_to_decimal256_precision_overflow() {
10617        let array = Float64Array::from(vec![1.1]);
10618        let array = Arc::new(array) as ArrayRef;
10619        let casted_array = cast_with_options(
10620            &array,
10621            &DataType::Decimal256(2, 2),
10622            &CastOptions {
10623                safe: true,
10624                format_options: FormatOptions::default(),
10625            },
10626        );
10627        assert!(casted_array.is_ok());
10628        assert!(casted_array.unwrap().is_null(0));
10629
10630        let casted_array = cast_with_options(
10631            &array,
10632            &DataType::Decimal256(2, 2),
10633            &CastOptions {
10634                safe: false,
10635                format_options: FormatOptions::default(),
10636            },
10637        );
10638        let err = casted_array.unwrap_err().to_string();
10639        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
10640        assert_eq!(err, expected_error);
10641    }
10642
10643    #[test]
10644    fn test_cast_floating_point_to_decimal128_overflow() {
10645        let array = Float64Array::from(vec![f64::MAX]);
10646        let array = Arc::new(array) as ArrayRef;
10647        let casted_array = cast_with_options(
10648            &array,
10649            &DataType::Decimal128(38, 30),
10650            &CastOptions {
10651                safe: true,
10652                format_options: FormatOptions::default(),
10653            },
10654        );
10655        assert!(casted_array.is_ok());
10656        assert!(casted_array.unwrap().is_null(0));
10657
10658        let casted_array = cast_with_options(
10659            &array,
10660            &DataType::Decimal128(38, 30),
10661            &CastOptions {
10662                safe: false,
10663                format_options: FormatOptions::default(),
10664            },
10665        );
10666        let err = casted_array.unwrap_err().to_string();
10667        let expected_error = "Cast error: Cannot cast to Decimal128(38, 30)";
10668        assert!(
10669            err.contains(expected_error),
10670            "did not find expected error '{expected_error}' in actual error '{err}'"
10671        );
10672    }
10673
10674    #[test]
10675    fn test_cast_floating_point_to_decimal256_overflow() {
10676        let array = Float64Array::from(vec![f64::MAX]);
10677        let array = Arc::new(array) as ArrayRef;
10678        let casted_array = cast_with_options(
10679            &array,
10680            &DataType::Decimal256(76, 50),
10681            &CastOptions {
10682                safe: true,
10683                format_options: FormatOptions::default(),
10684            },
10685        );
10686        assert!(casted_array.is_ok());
10687        assert!(casted_array.unwrap().is_null(0));
10688
10689        let casted_array = cast_with_options(
10690            &array,
10691            &DataType::Decimal256(76, 50),
10692            &CastOptions {
10693                safe: false,
10694                format_options: FormatOptions::default(),
10695            },
10696        );
10697        let err = casted_array.unwrap_err().to_string();
10698        let expected_error = "Cast error: Cannot cast to Decimal256(76, 50)";
10699        assert!(
10700            err.contains(expected_error),
10701            "did not find expected error '{expected_error}' in actual error '{err}'"
10702        );
10703    }
10704    #[test]
10705    fn test_cast_decimal256_to_f64_no_overflow() {
10706        // Test casting i256::MAX: should produce a large finite positive value
10707        let array = vec![Some(i256::MAX)];
10708        let array = create_decimal256_array(array, 76, 2).unwrap();
10709        let array = Arc::new(array) as ArrayRef;
10710
10711        let result = cast(&array, &DataType::Float64).unwrap();
10712        let result = result.as_primitive::<Float64Type>();
10713        assert!(result.value(0).is_finite());
10714        assert!(result.value(0) > 0.0); // Positive result
10715
10716        // Test casting i256::MIN: should produce a large finite negative value
10717        let array = vec![Some(i256::MIN)];
10718        let array = create_decimal256_array(array, 76, 2).unwrap();
10719        let array = Arc::new(array) as ArrayRef;
10720
10721        let result = cast(&array, &DataType::Float64).unwrap();
10722        let result = result.as_primitive::<Float64Type>();
10723        assert!(result.value(0).is_finite());
10724        assert!(result.value(0) < 0.0); // Negative result
10725    }
10726
10727    #[test]
10728    fn test_cast_decimal128_to_decimal128_negative_scale() {
10729        let input_type = DataType::Decimal128(20, 0);
10730        let output_type = DataType::Decimal128(20, -1);
10731        assert!(can_cast_types(&input_type, &output_type));
10732        let array = vec![Some(1123450), Some(2123455), Some(3123456), None];
10733        let input_decimal_array = create_decimal128_array(array, 20, 0).unwrap();
10734        let array = Arc::new(input_decimal_array) as ArrayRef;
10735        generate_cast_test_case!(
10736            &array,
10737            Decimal128Array,
10738            &output_type,
10739            vec![
10740                Some(112345_i128),
10741                Some(212346_i128),
10742                Some(312346_i128),
10743                None
10744            ]
10745        );
10746
10747        let casted_array = cast(&array, &output_type).unwrap();
10748        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10749
10750        assert_eq!("1123450", decimal_arr.value_as_string(0));
10751        assert_eq!("2123460", decimal_arr.value_as_string(1));
10752        assert_eq!("3123460", decimal_arr.value_as_string(2));
10753    }
10754
10755    #[test]
10756    fn decimal128_min_max_to_f64() {
10757        // Ensure Decimal128 i128::MIN/MAX round-trip cast
10758        let min128 = i128::MIN;
10759        let max128 = i128::MAX;
10760        assert_eq!(min128 as f64, min128 as f64);
10761        assert_eq!(max128 as f64, max128 as f64);
10762    }
10763
10764    #[test]
10765    fn test_cast_numeric_to_decimal128_negative() {
10766        let decimal_type = DataType::Decimal128(38, -1);
10767        let array = Arc::new(Int32Array::from(vec![
10768            Some(1123456),
10769            Some(2123456),
10770            Some(3123456),
10771        ])) as ArrayRef;
10772
10773        let casted_array = cast(&array, &decimal_type).unwrap();
10774        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10775
10776        assert_eq!("1123450", decimal_arr.value_as_string(0));
10777        assert_eq!("2123450", decimal_arr.value_as_string(1));
10778        assert_eq!("3123450", decimal_arr.value_as_string(2));
10779
10780        let array = Arc::new(Float32Array::from(vec![
10781            Some(1123.456),
10782            Some(2123.456),
10783            Some(3123.456),
10784        ])) as ArrayRef;
10785
10786        let casted_array = cast(&array, &decimal_type).unwrap();
10787        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10788
10789        assert_eq!("1120", decimal_arr.value_as_string(0));
10790        assert_eq!("2120", decimal_arr.value_as_string(1));
10791        assert_eq!("3120", decimal_arr.value_as_string(2));
10792    }
10793
10794    #[test]
10795    fn test_cast_decimal128_to_decimal128_negative() {
10796        let input_type = DataType::Decimal128(10, -1);
10797        let output_type = DataType::Decimal128(10, -2);
10798        assert!(can_cast_types(&input_type, &output_type));
10799        let array = vec![Some(123)];
10800        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
10801        let array = Arc::new(input_decimal_array) as ArrayRef;
10802        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(12_i128),]);
10803
10804        let casted_array = cast(&array, &output_type).unwrap();
10805        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10806
10807        assert_eq!("1200", decimal_arr.value_as_string(0));
10808
10809        let array = vec![Some(125)];
10810        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
10811        let array = Arc::new(input_decimal_array) as ArrayRef;
10812        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(13_i128),]);
10813
10814        let casted_array = cast(&array, &output_type).unwrap();
10815        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10816
10817        assert_eq!("1300", decimal_arr.value_as_string(0));
10818    }
10819
10820    #[test]
10821    fn test_cast_decimal128_to_decimal256_negative() {
10822        let input_type = DataType::Decimal128(10, 3);
10823        let output_type = DataType::Decimal256(10, 5);
10824        assert!(can_cast_types(&input_type, &output_type));
10825        let array = vec![Some(123456), Some(-123456)];
10826        let input_decimal_array = create_decimal128_array(array, 10, 3).unwrap();
10827        let array = Arc::new(input_decimal_array) as ArrayRef;
10828
10829        let hundred = i256::from_i128(100);
10830        generate_cast_test_case!(
10831            &array,
10832            Decimal256Array,
10833            &output_type,
10834            vec![
10835                Some(i256::from_i128(123456).mul_wrapping(hundred)),
10836                Some(i256::from_i128(-123456).mul_wrapping(hundred))
10837            ]
10838        );
10839    }
10840
10841    #[test]
10842    fn test_parse_string_to_decimal() {
10843        assert_eq!(
10844            Decimal128Type::format_decimal(
10845                parse_string_to_decimal_native::<Decimal128Type>("123.45", 2).unwrap(),
10846                38,
10847                2,
10848            ),
10849            "123.45"
10850        );
10851        assert_eq!(
10852            Decimal128Type::format_decimal(
10853                parse_string_to_decimal_native::<Decimal128Type>("12345", 2).unwrap(),
10854                38,
10855                2,
10856            ),
10857            "12345.00"
10858        );
10859        assert_eq!(
10860            Decimal128Type::format_decimal(
10861                parse_string_to_decimal_native::<Decimal128Type>("0.12345", 2).unwrap(),
10862                38,
10863                2,
10864            ),
10865            "0.12"
10866        );
10867        assert_eq!(
10868            Decimal128Type::format_decimal(
10869                parse_string_to_decimal_native::<Decimal128Type>(".12345", 2).unwrap(),
10870                38,
10871                2,
10872            ),
10873            "0.12"
10874        );
10875        assert_eq!(
10876            Decimal128Type::format_decimal(
10877                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
10878                38,
10879                2,
10880            ),
10881            "0.13"
10882        );
10883        assert_eq!(
10884            Decimal128Type::format_decimal(
10885                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
10886                38,
10887                2,
10888            ),
10889            "0.13"
10890        );
10891
10892        assert_eq!(
10893            Decimal256Type::format_decimal(
10894                parse_string_to_decimal_native::<Decimal256Type>("123.45", 3).unwrap(),
10895                38,
10896                3,
10897            ),
10898            "123.450"
10899        );
10900        assert_eq!(
10901            Decimal256Type::format_decimal(
10902                parse_string_to_decimal_native::<Decimal256Type>("12345", 3).unwrap(),
10903                38,
10904                3,
10905            ),
10906            "12345.000"
10907        );
10908        assert_eq!(
10909            Decimal256Type::format_decimal(
10910                parse_string_to_decimal_native::<Decimal256Type>("0.12345", 3).unwrap(),
10911                38,
10912                3,
10913            ),
10914            "0.123"
10915        );
10916        assert_eq!(
10917            Decimal256Type::format_decimal(
10918                parse_string_to_decimal_native::<Decimal256Type>(".12345", 3).unwrap(),
10919                38,
10920                3,
10921            ),
10922            "0.123"
10923        );
10924        assert_eq!(
10925            Decimal256Type::format_decimal(
10926                parse_string_to_decimal_native::<Decimal256Type>(".1265", 3).unwrap(),
10927                38,
10928                3,
10929            ),
10930            "0.127"
10931        );
10932    }
10933
10934    fn test_cast_string_to_decimal(array: ArrayRef) {
10935        // Decimal128
10936        let output_type = DataType::Decimal128(38, 2);
10937        assert!(can_cast_types(array.data_type(), &output_type));
10938
10939        let casted_array = cast(&array, &output_type).unwrap();
10940        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10941
10942        assert_eq!("123.45", decimal_arr.value_as_string(0));
10943        assert_eq!("1.23", decimal_arr.value_as_string(1));
10944        assert_eq!("0.12", decimal_arr.value_as_string(2));
10945        assert_eq!("0.13", decimal_arr.value_as_string(3));
10946        assert_eq!("1.26", decimal_arr.value_as_string(4));
10947        assert_eq!("12345.00", decimal_arr.value_as_string(5));
10948        assert_eq!("12345.00", decimal_arr.value_as_string(6));
10949        assert_eq!("0.12", decimal_arr.value_as_string(7));
10950        assert_eq!("12.23", decimal_arr.value_as_string(8));
10951        assert!(decimal_arr.is_null(9));
10952        assert!(decimal_arr.is_null(10));
10953        assert!(decimal_arr.is_null(11));
10954        assert!(decimal_arr.is_null(12));
10955        assert_eq!("-1.23", decimal_arr.value_as_string(13));
10956        assert_eq!("-1.24", decimal_arr.value_as_string(14));
10957        assert_eq!("0.00", decimal_arr.value_as_string(15));
10958        assert_eq!("-123.00", decimal_arr.value_as_string(16));
10959        assert_eq!("-123.23", decimal_arr.value_as_string(17));
10960        assert_eq!("-0.12", decimal_arr.value_as_string(18));
10961        assert_eq!("1.23", decimal_arr.value_as_string(19));
10962        assert_eq!("1.24", decimal_arr.value_as_string(20));
10963        assert_eq!("0.00", decimal_arr.value_as_string(21));
10964        assert_eq!("123.00", decimal_arr.value_as_string(22));
10965        assert_eq!("123.23", decimal_arr.value_as_string(23));
10966        assert_eq!("0.12", decimal_arr.value_as_string(24));
10967        assert!(decimal_arr.is_null(25));
10968        assert!(decimal_arr.is_null(26));
10969        assert!(decimal_arr.is_null(27));
10970        assert_eq!("0.00", decimal_arr.value_as_string(28));
10971        assert_eq!("0.00", decimal_arr.value_as_string(29));
10972        assert_eq!("12345.00", decimal_arr.value_as_string(30));
10973        assert_eq!(decimal_arr.len(), 31);
10974
10975        // Decimal256
10976        let output_type = DataType::Decimal256(76, 3);
10977        assert!(can_cast_types(array.data_type(), &output_type));
10978
10979        let casted_array = cast(&array, &output_type).unwrap();
10980        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
10981
10982        assert_eq!("123.450", decimal_arr.value_as_string(0));
10983        assert_eq!("1.235", decimal_arr.value_as_string(1));
10984        assert_eq!("0.123", decimal_arr.value_as_string(2));
10985        assert_eq!("0.127", decimal_arr.value_as_string(3));
10986        assert_eq!("1.263", decimal_arr.value_as_string(4));
10987        assert_eq!("12345.000", decimal_arr.value_as_string(5));
10988        assert_eq!("12345.000", decimal_arr.value_as_string(6));
10989        assert_eq!("0.123", decimal_arr.value_as_string(7));
10990        assert_eq!("12.234", decimal_arr.value_as_string(8));
10991        assert!(decimal_arr.is_null(9));
10992        assert!(decimal_arr.is_null(10));
10993        assert!(decimal_arr.is_null(11));
10994        assert!(decimal_arr.is_null(12));
10995        assert_eq!("-1.235", decimal_arr.value_as_string(13));
10996        assert_eq!("-1.236", decimal_arr.value_as_string(14));
10997        assert_eq!("0.000", decimal_arr.value_as_string(15));
10998        assert_eq!("-123.000", decimal_arr.value_as_string(16));
10999        assert_eq!("-123.234", decimal_arr.value_as_string(17));
11000        assert_eq!("-0.123", decimal_arr.value_as_string(18));
11001        assert_eq!("1.235", decimal_arr.value_as_string(19));
11002        assert_eq!("1.236", decimal_arr.value_as_string(20));
11003        assert_eq!("0.000", decimal_arr.value_as_string(21));
11004        assert_eq!("123.000", decimal_arr.value_as_string(22));
11005        assert_eq!("123.234", decimal_arr.value_as_string(23));
11006        assert_eq!("0.123", decimal_arr.value_as_string(24));
11007        assert!(decimal_arr.is_null(25));
11008        assert!(decimal_arr.is_null(26));
11009        assert!(decimal_arr.is_null(27));
11010        assert_eq!("0.000", decimal_arr.value_as_string(28));
11011        assert_eq!("0.000", decimal_arr.value_as_string(29));
11012        assert_eq!("12345.000", decimal_arr.value_as_string(30));
11013        assert_eq!(decimal_arr.len(), 31);
11014    }
11015
11016    #[test]
11017    fn test_cast_utf8_to_decimal() {
11018        let str_array = StringArray::from(vec![
11019            Some("123.45"),
11020            Some("1.2345"),
11021            Some("0.12345"),
11022            Some("0.1267"),
11023            Some("1.263"),
11024            Some("12345.0"),
11025            Some("12345"),
11026            Some("000.123"),
11027            Some("12.234000"),
11028            None,
11029            Some(""),
11030            Some(" "),
11031            None,
11032            Some("-1.23499999"),
11033            Some("-1.23599999"),
11034            Some("-0.00001"),
11035            Some("-123"),
11036            Some("-123.234000"),
11037            Some("-000.123"),
11038            Some("+1.23499999"),
11039            Some("+1.23599999"),
11040            Some("+0.00001"),
11041            Some("+123"),
11042            Some("+123.234000"),
11043            Some("+000.123"),
11044            Some("1.-23499999"),
11045            Some("-1.-23499999"),
11046            Some("--1.23499999"),
11047            Some("0"),
11048            Some("000.000"),
11049            Some("0000000000000000012345.000"),
11050        ]);
11051        let array = Arc::new(str_array) as ArrayRef;
11052
11053        test_cast_string_to_decimal(array);
11054
11055        let test_cases = [
11056            (None, None),
11057            (Some(""), None),
11058            (Some("   "), None),
11059            (Some("0"), Some("0")),
11060            (Some("000.000"), Some("0")),
11061            (Some("12345"), Some("12345")),
11062            (Some("000000000000000000000000000012345"), Some("12345")),
11063            (Some("-123"), Some("-123")),
11064            (Some("+123"), Some("123")),
11065        ];
11066        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
11067        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
11068
11069        let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
11070        test_cast_string_to_decimal_scale_zero(array, &expected);
11071    }
11072
11073    #[test]
11074    fn test_cast_large_utf8_to_decimal() {
11075        let str_array = LargeStringArray::from(vec![
11076            Some("123.45"),
11077            Some("1.2345"),
11078            Some("0.12345"),
11079            Some("0.1267"),
11080            Some("1.263"),
11081            Some("12345.0"),
11082            Some("12345"),
11083            Some("000.123"),
11084            Some("12.234000"),
11085            None,
11086            Some(""),
11087            Some(" "),
11088            None,
11089            Some("-1.23499999"),
11090            Some("-1.23599999"),
11091            Some("-0.00001"),
11092            Some("-123"),
11093            Some("-123.234000"),
11094            Some("-000.123"),
11095            Some("+1.23499999"),
11096            Some("+1.23599999"),
11097            Some("+0.00001"),
11098            Some("+123"),
11099            Some("+123.234000"),
11100            Some("+000.123"),
11101            Some("1.-23499999"),
11102            Some("-1.-23499999"),
11103            Some("--1.23499999"),
11104            Some("0"),
11105            Some("000.000"),
11106            Some("0000000000000000012345.000"),
11107        ]);
11108        let array = Arc::new(str_array) as ArrayRef;
11109
11110        test_cast_string_to_decimal(array);
11111
11112        let test_cases = [
11113            (None, None),
11114            (Some(""), None),
11115            (Some("   "), None),
11116            (Some("0"), Some("0")),
11117            (Some("000.000"), Some("0")),
11118            (Some("12345"), Some("12345")),
11119            (Some("000000000000000000000000000012345"), Some("12345")),
11120            (Some("-123"), Some("-123")),
11121            (Some("+123"), Some("123")),
11122        ];
11123        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
11124        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
11125
11126        let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
11127        test_cast_string_to_decimal_scale_zero(array, &expected);
11128    }
11129
11130    fn test_cast_string_to_decimal_scale_zero(
11131        array: ArrayRef,
11132        expected_as_string: &[Option<&str>],
11133    ) {
11134        // Decimal128
11135        let output_type = DataType::Decimal128(38, 0);
11136        assert!(can_cast_types(array.data_type(), &output_type));
11137        let casted_array = cast(&array, &output_type).unwrap();
11138        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11139        assert_decimal_array_contents(decimal_arr, expected_as_string);
11140
11141        // Decimal256
11142        let output_type = DataType::Decimal256(76, 0);
11143        assert!(can_cast_types(array.data_type(), &output_type));
11144        let casted_array = cast(&array, &output_type).unwrap();
11145        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
11146        assert_decimal_array_contents(decimal_arr, expected_as_string);
11147    }
11148
11149    fn assert_decimal_array_contents<T>(
11150        array: &PrimitiveArray<T>,
11151        expected_as_string: &[Option<&str>],
11152    ) where
11153        T: DecimalType + ArrowPrimitiveType,
11154    {
11155        assert_eq!(array.len(), expected_as_string.len());
11156        for (i, expected) in expected_as_string.iter().enumerate() {
11157            let actual = if array.is_null(i) {
11158                None
11159            } else {
11160                Some(array.value_as_string(i))
11161            };
11162            let actual = actual.as_ref().map(|s| s.as_ref());
11163            assert_eq!(*expected, actual, "Expected at position {i}");
11164        }
11165    }
11166
11167    #[test]
11168    fn test_cast_invalid_utf8_to_decimal() {
11169        let str_array = StringArray::from(vec!["4.4.5", ". 0.123"]);
11170        let array = Arc::new(str_array) as ArrayRef;
11171
11172        // Safe cast
11173        let output_type = DataType::Decimal128(38, 2);
11174        let casted_array = cast(&array, &output_type).unwrap();
11175        assert!(casted_array.is_null(0));
11176        assert!(casted_array.is_null(1));
11177
11178        let output_type = DataType::Decimal256(76, 2);
11179        let casted_array = cast(&array, &output_type).unwrap();
11180        assert!(casted_array.is_null(0));
11181        assert!(casted_array.is_null(1));
11182
11183        // Non-safe cast
11184        let output_type = DataType::Decimal128(38, 2);
11185        let str_array = StringArray::from(vec!["4.4.5"]);
11186        let array = Arc::new(str_array) as ArrayRef;
11187        let option = CastOptions {
11188            safe: false,
11189            format_options: FormatOptions::default(),
11190        };
11191        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
11192        assert!(
11193            casted_err
11194                .to_string()
11195                .contains("Cannot cast string '4.4.5' to value of Decimal128(38, 10) type")
11196        );
11197
11198        let str_array = StringArray::from(vec![". 0.123"]);
11199        let array = Arc::new(str_array) as ArrayRef;
11200        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
11201        assert!(
11202            casted_err
11203                .to_string()
11204                .contains("Cannot cast string '. 0.123' to value of Decimal128(38, 10) type")
11205        );
11206
11207        let str_array = StringArray::from(vec![""]);
11208        let array = Arc::new(str_array) as ArrayRef;
11209        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
11210        assert!(
11211            casted_err
11212                .to_string()
11213                .contains("Cannot cast string '' to value of Decimal128(38, 10) type")
11214        );
11215    }
11216
11217    fn test_cast_string_to_decimal128_overflow(overflow_array: ArrayRef) {
11218        let output_type = DataType::Decimal128(38, 2);
11219        let casted_array = cast(&overflow_array, &output_type).unwrap();
11220        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11221
11222        assert!(decimal_arr.is_null(0));
11223        assert!(decimal_arr.is_null(1));
11224        assert!(decimal_arr.is_null(2));
11225        assert_eq!(
11226            "999999999999999999999999999999999999.99",
11227            decimal_arr.value_as_string(3)
11228        );
11229        assert_eq!(
11230            "100000000000000000000000000000000000.00",
11231            decimal_arr.value_as_string(4)
11232        );
11233    }
11234
11235    #[test]
11236    fn test_cast_string_to_decimal128_precision_overflow() {
11237        let array = StringArray::from(vec!["1000".to_string()]);
11238        let array = Arc::new(array) as ArrayRef;
11239        let casted_array = cast_with_options(
11240            &array,
11241            &DataType::Decimal128(10, 8),
11242            &CastOptions {
11243                safe: true,
11244                format_options: FormatOptions::default(),
11245            },
11246        );
11247        assert!(casted_array.is_ok());
11248        assert!(casted_array.unwrap().is_null(0));
11249
11250        let err = cast_with_options(
11251            &array,
11252            &DataType::Decimal128(10, 8),
11253            &CastOptions {
11254                safe: false,
11255                format_options: FormatOptions::default(),
11256            },
11257        );
11258        assert_eq!(
11259            "Invalid argument error: 1000.00000000 is too large to store in a Decimal128 of precision 10. Max is 99.99999999",
11260            err.unwrap_err().to_string()
11261        );
11262    }
11263
11264    #[test]
11265    fn test_cast_utf8_to_decimal128_overflow() {
11266        let overflow_str_array = StringArray::from(vec![
11267            i128::MAX.to_string(),
11268            i128::MIN.to_string(),
11269            "99999999999999999999999999999999999999".to_string(),
11270            "999999999999999999999999999999999999.99".to_string(),
11271            "99999999999999999999999999999999999.999".to_string(),
11272        ]);
11273        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11274
11275        test_cast_string_to_decimal128_overflow(overflow_array);
11276    }
11277
11278    #[test]
11279    fn test_cast_large_utf8_to_decimal128_overflow() {
11280        let overflow_str_array = LargeStringArray::from(vec![
11281            i128::MAX.to_string(),
11282            i128::MIN.to_string(),
11283            "99999999999999999999999999999999999999".to_string(),
11284            "999999999999999999999999999999999999.99".to_string(),
11285            "99999999999999999999999999999999999.999".to_string(),
11286        ]);
11287        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11288
11289        test_cast_string_to_decimal128_overflow(overflow_array);
11290    }
11291
11292    fn test_cast_string_to_decimal256_overflow(overflow_array: ArrayRef) {
11293        let output_type = DataType::Decimal256(76, 2);
11294        let casted_array = cast(&overflow_array, &output_type).unwrap();
11295        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
11296
11297        assert_eq!(
11298            "170141183460469231731687303715884105727.00",
11299            decimal_arr.value_as_string(0)
11300        );
11301        assert_eq!(
11302            "-170141183460469231731687303715884105728.00",
11303            decimal_arr.value_as_string(1)
11304        );
11305        assert_eq!(
11306            "99999999999999999999999999999999999999.00",
11307            decimal_arr.value_as_string(2)
11308        );
11309        assert_eq!(
11310            "999999999999999999999999999999999999.99",
11311            decimal_arr.value_as_string(3)
11312        );
11313        assert_eq!(
11314            "100000000000000000000000000000000000.00",
11315            decimal_arr.value_as_string(4)
11316        );
11317        assert!(decimal_arr.is_null(5));
11318        assert!(decimal_arr.is_null(6));
11319    }
11320
11321    #[test]
11322    fn test_cast_string_to_decimal256_precision_overflow() {
11323        let array = StringArray::from(vec!["1000".to_string()]);
11324        let array = Arc::new(array) as ArrayRef;
11325        let casted_array = cast_with_options(
11326            &array,
11327            &DataType::Decimal256(10, 8),
11328            &CastOptions {
11329                safe: true,
11330                format_options: FormatOptions::default(),
11331            },
11332        );
11333        assert!(casted_array.is_ok());
11334        assert!(casted_array.unwrap().is_null(0));
11335
11336        let err = cast_with_options(
11337            &array,
11338            &DataType::Decimal256(10, 8),
11339            &CastOptions {
11340                safe: false,
11341                format_options: FormatOptions::default(),
11342            },
11343        );
11344        assert_eq!(
11345            "Invalid argument error: 1000.00000000 is too large to store in a Decimal256 of precision 10. Max is 99.99999999",
11346            err.unwrap_err().to_string()
11347        );
11348    }
11349
11350    #[test]
11351    fn test_cast_utf8_to_decimal256_overflow() {
11352        let overflow_str_array = StringArray::from(vec![
11353            i128::MAX.to_string(),
11354            i128::MIN.to_string(),
11355            "99999999999999999999999999999999999999".to_string(),
11356            "999999999999999999999999999999999999.99".to_string(),
11357            "99999999999999999999999999999999999.999".to_string(),
11358            i256::MAX.to_string(),
11359            i256::MIN.to_string(),
11360        ]);
11361        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11362
11363        test_cast_string_to_decimal256_overflow(overflow_array);
11364    }
11365
11366    #[test]
11367    fn test_cast_large_utf8_to_decimal256_overflow() {
11368        let overflow_str_array = LargeStringArray::from(vec![
11369            i128::MAX.to_string(),
11370            i128::MIN.to_string(),
11371            "99999999999999999999999999999999999999".to_string(),
11372            "999999999999999999999999999999999999.99".to_string(),
11373            "99999999999999999999999999999999999.999".to_string(),
11374            i256::MAX.to_string(),
11375            i256::MIN.to_string(),
11376        ]);
11377        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11378
11379        test_cast_string_to_decimal256_overflow(overflow_array);
11380    }
11381
11382    #[test]
11383    fn test_cast_outside_supported_range_for_nanoseconds() {
11384        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";
11385
11386        let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
11387
11388        let cast_options = CastOptions {
11389            safe: false,
11390            format_options: FormatOptions::default(),
11391        };
11392
11393        let result = cast_string_to_timestamp::<i32, TimestampNanosecondType>(
11394            &array,
11395            &None::<Arc<str>>,
11396            &cast_options,
11397        );
11398
11399        let err = result.unwrap_err();
11400        assert_eq!(
11401            err.to_string(),
11402            format!(
11403                "Cast error: Overflow converting {} to Nanosecond. {}",
11404                array.value(0),
11405                EXPECTED_ERROR_MESSAGE
11406            )
11407        );
11408    }
11409
11410    #[test]
11411    fn test_cast_date32_to_timestamp() {
11412        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
11413        let array = Arc::new(a) as ArrayRef;
11414        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
11415        let c = b.as_primitive::<TimestampSecondType>();
11416        assert_eq!(1609459200, c.value(0));
11417        assert_eq!(1640995200, c.value(1));
11418        assert!(c.is_null(2));
11419    }
11420
11421    #[test]
11422    fn test_cast_date32_to_timestamp_ms() {
11423        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
11424        let array = Arc::new(a) as ArrayRef;
11425        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
11426        let c = b
11427            .as_any()
11428            .downcast_ref::<TimestampMillisecondArray>()
11429            .unwrap();
11430        assert_eq!(1609459200000, c.value(0));
11431        assert_eq!(1640995200000, c.value(1));
11432        assert!(c.is_null(2));
11433    }
11434
11435    #[test]
11436    fn test_cast_date32_to_timestamp_us() {
11437        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
11438        let array = Arc::new(a) as ArrayRef;
11439        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
11440        let c = b
11441            .as_any()
11442            .downcast_ref::<TimestampMicrosecondArray>()
11443            .unwrap();
11444        assert_eq!(1609459200000000, c.value(0));
11445        assert_eq!(1640995200000000, c.value(1));
11446        assert!(c.is_null(2));
11447    }
11448
11449    #[test]
11450    fn test_cast_date32_to_timestamp_ns() {
11451        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
11452        let array = Arc::new(a) as ArrayRef;
11453        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
11454        let c = b
11455            .as_any()
11456            .downcast_ref::<TimestampNanosecondArray>()
11457            .unwrap();
11458        assert_eq!(1609459200000000000, c.value(0));
11459        assert_eq!(1640995200000000000, c.value(1));
11460        assert!(c.is_null(2));
11461    }
11462
11463    #[test]
11464    fn test_cast_date32_to_timestamp_us_overflow() {
11465        const MAX_DAYS_MICROS: i32 = (i64::MAX / MICROSECONDS_IN_DAY) as i32;
11466        let a = Date32Array::from(vec![Some(MAX_DAYS_MICROS), Some(MAX_DAYS_MICROS + 1), None]);
11467        let array = Arc::new(a) as ArrayRef;
11468        let err = cast_with_options(
11469            &array,
11470            &DataType::Timestamp(TimeUnit::Microsecond, None),
11471            &CastOptions {
11472                safe: false,
11473                format_options: FormatOptions::default(),
11474            },
11475        );
11476        assert!(err.is_err());
11477
11478        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
11479        let c = b.as_primitive::<TimestampMicrosecondType>();
11480        assert_eq!(MAX_DAYS_MICROS as i64 * MICROSECONDS_IN_DAY, c.value(0));
11481        assert!(c.is_null(1));
11482        assert!(c.is_null(2));
11483    }
11484
11485    #[test]
11486    fn test_cast_date32_to_timestamp_ns_overflow() {
11487        // 2262-04-11, 2062-04-12
11488        let upper_limit = 106_751;
11489        let a = Date32Array::from(vec![Some(upper_limit), Some(upper_limit + 1), None]);
11490        let array = Arc::new(a) as ArrayRef;
11491        let err = cast_with_options(
11492            &array,
11493            &DataType::Timestamp(TimeUnit::Nanosecond, None),
11494            &CastOptions {
11495                safe: false,
11496                format_options: FormatOptions::default(),
11497            },
11498        );
11499        assert!(err.is_err());
11500
11501        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
11502        let c = b.as_primitive::<TimestampNanosecondType>();
11503        assert_eq!(upper_limit as i64 * NANOSECONDS_IN_DAY, c.value(0));
11504        assert!(c.is_null(1));
11505        assert!(c.is_null(2));
11506    }
11507
11508    #[test]
11509    fn test_timezone_cast() {
11510        let a = StringArray::from(vec![
11511            "2000-01-01T12:00:00", // date + time valid
11512            "2020-12-15T12:34:56", // date + time valid
11513        ]);
11514        let array = Arc::new(a) as ArrayRef;
11515        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
11516        let v = b.as_primitive::<TimestampNanosecondType>();
11517
11518        assert_eq!(v.value(0), 946728000000000000);
11519        assert_eq!(v.value(1), 1608035696000000000);
11520
11521        let b = cast(
11522            &b,
11523            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11524        )
11525        .unwrap();
11526        let v = b.as_primitive::<TimestampNanosecondType>();
11527
11528        assert_eq!(v.value(0), 946728000000000000);
11529        assert_eq!(v.value(1), 1608035696000000000);
11530
11531        let b = cast(
11532            &b,
11533            &DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
11534        )
11535        .unwrap();
11536        let v = b.as_primitive::<TimestampMillisecondType>();
11537
11538        assert_eq!(v.value(0), 946728000000);
11539        assert_eq!(v.value(1), 1608035696000);
11540    }
11541
11542    #[test]
11543    fn test_cast_utf8_to_timestamp() {
11544        fn test_tz(tz: Arc<str>) {
11545            let valid = StringArray::from(vec![
11546                "2023-01-01 04:05:06.789000-08:00",
11547                "2023-01-01 04:05:06.789000-07:00",
11548                "2023-01-01 04:05:06.789 -0800",
11549                "2023-01-01 04:05:06.789 -08:00",
11550                "2023-01-01 040506 +0730",
11551                "2023-01-01 040506 +07:30",
11552                "2023-01-01 04:05:06.789",
11553                "2023-01-01 04:05:06",
11554                "2023-01-01",
11555            ]);
11556
11557            let array = Arc::new(valid) as ArrayRef;
11558            let b = cast_with_options(
11559                &array,
11560                &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
11561                &CastOptions {
11562                    safe: false,
11563                    format_options: FormatOptions::default(),
11564                },
11565            )
11566            .unwrap();
11567
11568            let tz = tz.as_ref().parse().unwrap();
11569
11570            let as_tz =
11571                |v: i64| as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap();
11572
11573            let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
11574            let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
11575
11576            let values = b.as_primitive::<TimestampNanosecondType>().values();
11577            let utc_results: Vec<_> = values.iter().map(as_utc).collect();
11578            let local_results: Vec<_> = values.iter().map(as_local).collect();
11579
11580            // Absolute timestamps should be parsed preserving the same UTC instant
11581            assert_eq!(
11582                &utc_results[..6],
11583                &[
11584                    "2023-01-01 12:05:06.789".to_string(),
11585                    "2023-01-01 11:05:06.789".to_string(),
11586                    "2023-01-01 12:05:06.789".to_string(),
11587                    "2023-01-01 12:05:06.789".to_string(),
11588                    "2022-12-31 20:35:06".to_string(),
11589                    "2022-12-31 20:35:06".to_string(),
11590                ]
11591            );
11592            // Non-absolute timestamps should be parsed preserving the same local instant
11593            assert_eq!(
11594                &local_results[6..],
11595                &[
11596                    "2023-01-01 04:05:06.789".to_string(),
11597                    "2023-01-01 04:05:06".to_string(),
11598                    "2023-01-01 00:00:00".to_string()
11599                ]
11600            )
11601        }
11602
11603        test_tz("+00:00".into());
11604        test_tz("+02:00".into());
11605    }
11606
11607    #[test]
11608    fn test_cast_invalid_utf8() {
11609        let v1: &[u8] = b"\xFF invalid";
11610        let v2: &[u8] = b"\x00 Foo";
11611        let s = BinaryArray::from(vec![v1, v2]);
11612        let options = CastOptions {
11613            safe: true,
11614            format_options: FormatOptions::default(),
11615        };
11616        let array = cast_with_options(&s, &DataType::Utf8, &options).unwrap();
11617        let a = array.as_string::<i32>();
11618        a.to_data().validate_full().unwrap();
11619
11620        assert_eq!(a.null_count(), 1);
11621        assert_eq!(a.len(), 2);
11622        assert!(a.is_null(0));
11623        assert_eq!(a.value(0), "");
11624        assert_eq!(a.value(1), "\x00 Foo");
11625    }
11626
11627    #[test]
11628    fn test_cast_utf8_to_timestamptz() {
11629        let valid = StringArray::from(vec!["2023-01-01"]);
11630
11631        let array = Arc::new(valid) as ArrayRef;
11632        let b = cast(
11633            &array,
11634            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11635        )
11636        .unwrap();
11637
11638        let expect = DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into()));
11639
11640        assert_eq!(b.data_type(), &expect);
11641        let c = b
11642            .as_any()
11643            .downcast_ref::<TimestampNanosecondArray>()
11644            .unwrap();
11645        assert_eq!(1672531200000000000, c.value(0));
11646    }
11647
11648    #[test]
11649    fn test_cast_decimal_to_string() {
11650        assert!(can_cast_types(
11651            &DataType::Decimal32(9, 4),
11652            &DataType::Utf8View
11653        ));
11654        assert!(can_cast_types(
11655            &DataType::Decimal64(16, 4),
11656            &DataType::Utf8View
11657        ));
11658        assert!(can_cast_types(
11659            &DataType::Decimal128(10, 4),
11660            &DataType::Utf8View
11661        ));
11662        assert!(can_cast_types(
11663            &DataType::Decimal256(38, 10),
11664            &DataType::Utf8View
11665        ));
11666
11667        macro_rules! assert_decimal_values {
11668            ($array:expr) => {
11669                let c = $array;
11670                assert_eq!("1123.454", c.value(0));
11671                assert_eq!("2123.456", c.value(1));
11672                assert_eq!("-3123.453", c.value(2));
11673                assert_eq!("-3123.456", c.value(3));
11674                assert_eq!("0.000", c.value(4));
11675                assert_eq!("0.123", c.value(5));
11676                assert_eq!("1234.567", c.value(6));
11677                assert_eq!("-1234.567", c.value(7));
11678                assert!(c.is_null(8));
11679            };
11680        }
11681
11682        fn test_decimal_to_string<IN: ArrowPrimitiveType, OffsetSize: OffsetSizeTrait>(
11683            output_type: DataType,
11684            array: PrimitiveArray<IN>,
11685        ) {
11686            let b = cast(&array, &output_type).unwrap();
11687
11688            assert_eq!(b.data_type(), &output_type);
11689            match b.data_type() {
11690                DataType::Utf8View => {
11691                    let c = b.as_string_view();
11692                    assert_decimal_values!(c);
11693                }
11694                DataType::Utf8 | DataType::LargeUtf8 => {
11695                    let c = b.as_string::<OffsetSize>();
11696                    assert_decimal_values!(c);
11697                }
11698                _ => (),
11699            }
11700        }
11701
11702        let array32: Vec<Option<i32>> = vec![
11703            Some(1123454),
11704            Some(2123456),
11705            Some(-3123453),
11706            Some(-3123456),
11707            Some(0),
11708            Some(123),
11709            Some(123456789),
11710            Some(-123456789),
11711            None,
11712        ];
11713        let array64: Vec<Option<i64>> = array32.iter().map(|num| num.map(|x| x as i64)).collect();
11714        let array128: Vec<Option<i128>> =
11715            array64.iter().map(|num| num.map(|x| x as i128)).collect();
11716        let array256: Vec<Option<i256>> = array128
11717            .iter()
11718            .map(|num| num.map(i256::from_i128))
11719            .collect();
11720
11721        test_decimal_to_string::<Decimal32Type, i32>(
11722            DataType::Utf8View,
11723            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
11724        );
11725        test_decimal_to_string::<Decimal32Type, i32>(
11726            DataType::Utf8,
11727            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
11728        );
11729        test_decimal_to_string::<Decimal32Type, i64>(
11730            DataType::LargeUtf8,
11731            create_decimal32_array(array32, 7, 3).unwrap(),
11732        );
11733
11734        test_decimal_to_string::<Decimal64Type, i32>(
11735            DataType::Utf8View,
11736            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
11737        );
11738        test_decimal_to_string::<Decimal64Type, i32>(
11739            DataType::Utf8,
11740            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
11741        );
11742        test_decimal_to_string::<Decimal64Type, i64>(
11743            DataType::LargeUtf8,
11744            create_decimal64_array(array64, 7, 3).unwrap(),
11745        );
11746
11747        test_decimal_to_string::<Decimal128Type, i32>(
11748            DataType::Utf8View,
11749            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
11750        );
11751        test_decimal_to_string::<Decimal128Type, i32>(
11752            DataType::Utf8,
11753            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
11754        );
11755        test_decimal_to_string::<Decimal128Type, i64>(
11756            DataType::LargeUtf8,
11757            create_decimal128_array(array128, 7, 3).unwrap(),
11758        );
11759
11760        test_decimal_to_string::<Decimal256Type, i32>(
11761            DataType::Utf8View,
11762            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
11763        );
11764        test_decimal_to_string::<Decimal256Type, i32>(
11765            DataType::Utf8,
11766            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
11767        );
11768        test_decimal_to_string::<Decimal256Type, i64>(
11769            DataType::LargeUtf8,
11770            create_decimal256_array(array256, 7, 3).unwrap(),
11771        );
11772    }
11773
11774    #[test]
11775    fn test_cast_numeric_to_decimal128_precision_overflow() {
11776        let array = Int64Array::from(vec![1234567]);
11777        let array = Arc::new(array) as ArrayRef;
11778        let casted_array = cast_with_options(
11779            &array,
11780            &DataType::Decimal128(7, 3),
11781            &CastOptions {
11782                safe: true,
11783                format_options: FormatOptions::default(),
11784            },
11785        );
11786        assert!(casted_array.is_ok());
11787        assert!(casted_array.unwrap().is_null(0));
11788
11789        let err = cast_with_options(
11790            &array,
11791            &DataType::Decimal128(7, 3),
11792            &CastOptions {
11793                safe: false,
11794                format_options: FormatOptions::default(),
11795            },
11796        );
11797        assert_eq!(
11798            "Invalid argument error: 1234567.000 is too large to store in a Decimal128 of precision 7. Max is 9999.999",
11799            err.unwrap_err().to_string()
11800        );
11801    }
11802
11803    #[test]
11804    fn test_cast_numeric_to_decimal256_precision_overflow() {
11805        let array = Int64Array::from(vec![1234567]);
11806        let array = Arc::new(array) as ArrayRef;
11807        let casted_array = cast_with_options(
11808            &array,
11809            &DataType::Decimal256(7, 3),
11810            &CastOptions {
11811                safe: true,
11812                format_options: FormatOptions::default(),
11813            },
11814        );
11815        assert!(casted_array.is_ok());
11816        assert!(casted_array.unwrap().is_null(0));
11817
11818        let err = cast_with_options(
11819            &array,
11820            &DataType::Decimal256(7, 3),
11821            &CastOptions {
11822                safe: false,
11823                format_options: FormatOptions::default(),
11824            },
11825        );
11826        assert_eq!(
11827            "Invalid argument error: 1234567.000 is too large to store in a Decimal256 of precision 7. Max is 9999.999",
11828            err.unwrap_err().to_string()
11829        );
11830    }
11831
11832    /// helper function to test casting from duration to interval
11833    fn cast_from_duration_to_interval<T: ArrowTemporalType<Native = i64>>(
11834        array: Vec<i64>,
11835        cast_options: &CastOptions,
11836    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
11837        let array = PrimitiveArray::<T>::new(array.into(), None);
11838        let array = Arc::new(array) as ArrayRef;
11839        let interval = DataType::Interval(IntervalUnit::MonthDayNano);
11840        let out = cast_with_options(&array, &interval, cast_options)?;
11841        let out = out.as_primitive::<IntervalMonthDayNanoType>().clone();
11842        Ok(out)
11843    }
11844
11845    #[test]
11846    fn test_cast_from_duration_to_interval() {
11847        // from duration second to interval month day nano
11848        let array = vec![1234567];
11849        let casted_array =
11850            cast_from_duration_to_interval::<DurationSecondType>(array, &CastOptions::default())
11851                .unwrap();
11852        assert_eq!(
11853            casted_array.data_type(),
11854            &DataType::Interval(IntervalUnit::MonthDayNano)
11855        );
11856        assert_eq!(
11857            casted_array.value(0),
11858            IntervalMonthDayNano::new(0, 0, 1234567000000000)
11859        );
11860
11861        let array = vec![i64::MAX];
11862        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
11863            array.clone(),
11864            &CastOptions::default(),
11865        )
11866        .unwrap();
11867        assert!(!casted_array.is_valid(0));
11868
11869        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
11870            array,
11871            &CastOptions {
11872                safe: false,
11873                format_options: FormatOptions::default(),
11874            },
11875        );
11876        assert!(casted_array.is_err());
11877
11878        // from duration millisecond to interval month day nano
11879        let array = vec![1234567];
11880        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11881            array,
11882            &CastOptions::default(),
11883        )
11884        .unwrap();
11885        assert_eq!(
11886            casted_array.data_type(),
11887            &DataType::Interval(IntervalUnit::MonthDayNano)
11888        );
11889        assert_eq!(
11890            casted_array.value(0),
11891            IntervalMonthDayNano::new(0, 0, 1234567000000)
11892        );
11893
11894        let array = vec![i64::MAX];
11895        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11896            array.clone(),
11897            &CastOptions::default(),
11898        )
11899        .unwrap();
11900        assert!(!casted_array.is_valid(0));
11901
11902        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11903            array,
11904            &CastOptions {
11905                safe: false,
11906                format_options: FormatOptions::default(),
11907            },
11908        );
11909        assert!(casted_array.is_err());
11910
11911        // from duration microsecond to interval month day nano
11912        let array = vec![1234567];
11913        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11914            array,
11915            &CastOptions::default(),
11916        )
11917        .unwrap();
11918        assert_eq!(
11919            casted_array.data_type(),
11920            &DataType::Interval(IntervalUnit::MonthDayNano)
11921        );
11922        assert_eq!(
11923            casted_array.value(0),
11924            IntervalMonthDayNano::new(0, 0, 1234567000)
11925        );
11926
11927        let array = vec![i64::MAX];
11928        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11929            array.clone(),
11930            &CastOptions::default(),
11931        )
11932        .unwrap();
11933        assert!(!casted_array.is_valid(0));
11934
11935        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11936            array,
11937            &CastOptions {
11938                safe: false,
11939                format_options: FormatOptions::default(),
11940            },
11941        );
11942        assert!(casted_array.is_err());
11943
11944        // from duration nanosecond to interval month day nano
11945        let array = vec![1234567];
11946        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
11947            array,
11948            &CastOptions::default(),
11949        )
11950        .unwrap();
11951        assert_eq!(
11952            casted_array.data_type(),
11953            &DataType::Interval(IntervalUnit::MonthDayNano)
11954        );
11955        assert_eq!(
11956            casted_array.value(0),
11957            IntervalMonthDayNano::new(0, 0, 1234567)
11958        );
11959
11960        let array = vec![i64::MAX];
11961        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
11962            array,
11963            &CastOptions {
11964                safe: false,
11965                format_options: FormatOptions::default(),
11966            },
11967        )
11968        .unwrap();
11969        assert_eq!(
11970            casted_array.value(0),
11971            IntervalMonthDayNano::new(0, 0, i64::MAX)
11972        );
11973    }
11974
11975    /// helper function to test casting from interval to duration
11976    fn cast_from_interval_to_duration<T: ArrowTemporalType>(
11977        array: &IntervalMonthDayNanoArray,
11978        cast_options: &CastOptions,
11979    ) -> Result<PrimitiveArray<T>, ArrowError> {
11980        let casted_array = cast_with_options(&array, &T::DATA_TYPE, cast_options)?;
11981        casted_array
11982            .as_any()
11983            .downcast_ref::<PrimitiveArray<T>>()
11984            .ok_or_else(|| {
11985                ArrowError::ComputeError(format!("Failed to downcast to {}", T::DATA_TYPE))
11986            })
11987            .cloned()
11988    }
11989
11990    #[test]
11991    fn test_cast_from_interval_to_duration() {
11992        let nullable = CastOptions::default();
11993        let fallible = CastOptions {
11994            safe: false,
11995            format_options: FormatOptions::default(),
11996        };
11997        let v = IntervalMonthDayNano::new(0, 0, 1234567);
11998
11999        // from interval month day nano to duration second
12000        let array = vec![v].into();
12001        let casted_array: DurationSecondArray =
12002            cast_from_interval_to_duration(&array, &nullable).unwrap();
12003        assert_eq!(casted_array.value(0), 0);
12004
12005        let array = vec![IntervalMonthDayNano::MAX].into();
12006        let casted_array: DurationSecondArray =
12007            cast_from_interval_to_duration(&array, &nullable).unwrap();
12008        assert!(!casted_array.is_valid(0));
12009
12010        let res = cast_from_interval_to_duration::<DurationSecondType>(&array, &fallible);
12011        assert!(res.is_err());
12012
12013        // from interval month day nano to duration millisecond
12014        let array = vec![v].into();
12015        let casted_array: DurationMillisecondArray =
12016            cast_from_interval_to_duration(&array, &nullable).unwrap();
12017        assert_eq!(casted_array.value(0), 1);
12018
12019        let array = vec![IntervalMonthDayNano::MAX].into();
12020        let casted_array: DurationMillisecondArray =
12021            cast_from_interval_to_duration(&array, &nullable).unwrap();
12022        assert!(!casted_array.is_valid(0));
12023
12024        let res = cast_from_interval_to_duration::<DurationMillisecondType>(&array, &fallible);
12025        assert!(res.is_err());
12026
12027        // from interval month day nano to duration microsecond
12028        let array = vec![v].into();
12029        let casted_array: DurationMicrosecondArray =
12030            cast_from_interval_to_duration(&array, &nullable).unwrap();
12031        assert_eq!(casted_array.value(0), 1234);
12032
12033        let array = vec![IntervalMonthDayNano::MAX].into();
12034        let casted_array =
12035            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &nullable).unwrap();
12036        assert!(!casted_array.is_valid(0));
12037
12038        let casted_array =
12039            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &fallible);
12040        assert!(casted_array.is_err());
12041
12042        // from interval month day nano to duration nanosecond
12043        let array = vec![v].into();
12044        let casted_array: DurationNanosecondArray =
12045            cast_from_interval_to_duration(&array, &nullable).unwrap();
12046        assert_eq!(casted_array.value(0), 1234567);
12047
12048        let array = vec![IntervalMonthDayNano::MAX].into();
12049        let casted_array: DurationNanosecondArray =
12050            cast_from_interval_to_duration(&array, &nullable).unwrap();
12051        assert!(!casted_array.is_valid(0));
12052
12053        let casted_array =
12054            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &fallible);
12055        assert!(casted_array.is_err());
12056
12057        let array = vec![
12058            IntervalMonthDayNanoType::make_value(0, 1, 0),
12059            IntervalMonthDayNanoType::make_value(-1, 0, 0),
12060            IntervalMonthDayNanoType::make_value(1, 1, 0),
12061            IntervalMonthDayNanoType::make_value(1, 0, 1),
12062            IntervalMonthDayNanoType::make_value(0, 0, -1),
12063        ]
12064        .into();
12065        let casted_array =
12066            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable).unwrap();
12067        assert!(!casted_array.is_valid(0));
12068        assert!(!casted_array.is_valid(1));
12069        assert!(!casted_array.is_valid(2));
12070        assert!(!casted_array.is_valid(3));
12071        assert!(casted_array.is_valid(4));
12072        assert_eq!(casted_array.value(4), -1);
12073    }
12074
12075    /// helper function to test casting from interval year month to interval month day nano
12076    fn cast_from_interval_year_month_to_interval_month_day_nano(
12077        array: Vec<i32>,
12078        cast_options: &CastOptions,
12079    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
12080        let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
12081        let array = Arc::new(array) as ArrayRef;
12082        let casted_array = cast_with_options(
12083            &array,
12084            &DataType::Interval(IntervalUnit::MonthDayNano),
12085            cast_options,
12086        )?;
12087        casted_array
12088            .as_any()
12089            .downcast_ref::<IntervalMonthDayNanoArray>()
12090            .ok_or_else(|| {
12091                ArrowError::ComputeError(
12092                    "Failed to downcast to IntervalMonthDayNanoArray".to_string(),
12093                )
12094            })
12095            .cloned()
12096    }
12097
12098    #[test]
12099    fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
12100        // from interval year month to interval month day nano
12101        let array = vec![1234567];
12102        let casted_array = cast_from_interval_year_month_to_interval_month_day_nano(
12103            array,
12104            &CastOptions::default(),
12105        )
12106        .unwrap();
12107        assert_eq!(
12108            casted_array.data_type(),
12109            &DataType::Interval(IntervalUnit::MonthDayNano)
12110        );
12111        assert_eq!(
12112            casted_array.value(0),
12113            IntervalMonthDayNano::new(1234567, 0, 0)
12114        );
12115    }
12116
12117    /// helper function to test casting from interval day time to interval month day nano
12118    fn cast_from_interval_day_time_to_interval_month_day_nano(
12119        array: Vec<IntervalDayTime>,
12120        cast_options: &CastOptions,
12121    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
12122        let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
12123        let array = Arc::new(array) as ArrayRef;
12124        let casted_array = cast_with_options(
12125            &array,
12126            &DataType::Interval(IntervalUnit::MonthDayNano),
12127            cast_options,
12128        )?;
12129        Ok(casted_array
12130            .as_primitive::<IntervalMonthDayNanoType>()
12131            .clone())
12132    }
12133
12134    #[test]
12135    fn test_cast_from_interval_day_time_to_interval_month_day_nano() {
12136        // from interval day time to interval month day nano
12137        let array = vec![IntervalDayTime::new(123, 0)];
12138        let casted_array =
12139            cast_from_interval_day_time_to_interval_month_day_nano(array, &CastOptions::default())
12140                .unwrap();
12141        assert_eq!(
12142            casted_array.data_type(),
12143            &DataType::Interval(IntervalUnit::MonthDayNano)
12144        );
12145        assert_eq!(casted_array.value(0), IntervalMonthDayNano::new(0, 123, 0));
12146    }
12147
12148    #[test]
12149    fn test_cast_below_unixtimestamp() {
12150        let valid = StringArray::from(vec![
12151            "1900-01-03 23:59:59",
12152            "1969-12-31 00:00:01",
12153            "1989-12-31 00:00:01",
12154        ]);
12155
12156        let array = Arc::new(valid) as ArrayRef;
12157        let casted_array = cast_with_options(
12158            &array,
12159            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
12160            &CastOptions {
12161                safe: false,
12162                format_options: FormatOptions::default(),
12163            },
12164        )
12165        .unwrap();
12166
12167        let ts_array = casted_array
12168            .as_primitive::<TimestampNanosecondType>()
12169            .values()
12170            .iter()
12171            .map(|ts| ts / 1_000_000)
12172            .collect::<Vec<_>>();
12173
12174        let array = TimestampMillisecondArray::from(ts_array).with_timezone("+00:00".to_string());
12175        let casted_array = cast(&array, &DataType::Date32).unwrap();
12176        let date_array = casted_array.as_primitive::<Date32Type>();
12177        let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
12178        let string_array = casted_array.as_string::<i32>();
12179        assert_eq!("1900-01-03", string_array.value(0));
12180        assert_eq!("1969-12-31", string_array.value(1));
12181        assert_eq!("1989-12-31", string_array.value(2));
12182    }
12183
12184    #[test]
12185    fn test_nested_list() {
12186        let mut list = ListBuilder::new(Int32Builder::new());
12187        list.append_value([Some(1), Some(2), Some(3)]);
12188        list.append_value([Some(4), None, Some(6)]);
12189        let list = list.finish();
12190
12191        let to_field = Field::new("nested", list.data_type().clone(), false);
12192        let to = DataType::List(Arc::new(to_field));
12193        let out = cast(&list, &to).unwrap();
12194        let opts = FormatOptions::default().with_null("null");
12195        let formatted = ArrayFormatter::try_new(out.as_ref(), &opts).unwrap();
12196
12197        assert_eq!(formatted.value(0).to_string(), "[[1], [2], [3]]");
12198        assert_eq!(formatted.value(1).to_string(), "[[4], [null], [6]]");
12199    }
12200
12201    #[test]
12202    fn test_nested_list_cast() {
12203        let mut builder = ListBuilder::new(ListBuilder::new(Int32Builder::new()));
12204        builder.append_value([Some([Some(1), Some(2), None]), None]);
12205        builder.append_value([None, Some([]), None]);
12206        builder.append_null();
12207        builder.append_value([Some([Some(2), Some(3)])]);
12208        let start = builder.finish();
12209
12210        let mut builder = LargeListBuilder::new(LargeListBuilder::new(Int8Builder::new()));
12211        builder.append_value([Some([Some(1), Some(2), None]), None]);
12212        builder.append_value([None, Some([]), None]);
12213        builder.append_null();
12214        builder.append_value([Some([Some(2), Some(3)])]);
12215        let expected = builder.finish();
12216
12217        let actual = cast(&start, expected.data_type()).unwrap();
12218        assert_eq!(actual.as_ref(), &expected);
12219    }
12220
12221    const CAST_OPTIONS: CastOptions<'static> = CastOptions {
12222        safe: true,
12223        format_options: FormatOptions::new(),
12224    };
12225
12226    #[test]
12227    #[allow(clippy::assertions_on_constants)]
12228    fn test_const_options() {
12229        assert!(CAST_OPTIONS.safe)
12230    }
12231
12232    #[test]
12233    fn test_list_format_options() {
12234        let options = CastOptions {
12235            safe: false,
12236            format_options: FormatOptions::default().with_null("null"),
12237        };
12238        let array = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
12239            Some(vec![Some(0), Some(1), Some(2)]),
12240            Some(vec![Some(0), None, Some(2)]),
12241        ]);
12242        let a = cast_with_options(&array, &DataType::Utf8, &options).unwrap();
12243        let r: Vec<_> = a.as_string::<i32>().iter().flatten().collect();
12244        assert_eq!(r, &["[0, 1, 2]", "[0, null, 2]"]);
12245    }
12246    #[test]
12247    fn test_cast_string_to_timestamp_invalid_tz() {
12248        // content after Z should be ignored
12249        let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
12250        let array = StringArray::from(vec![Some(bad_timestamp)]);
12251
12252        let data_types = [
12253            DataType::Timestamp(TimeUnit::Second, None),
12254            DataType::Timestamp(TimeUnit::Millisecond, None),
12255            DataType::Timestamp(TimeUnit::Microsecond, None),
12256            DataType::Timestamp(TimeUnit::Nanosecond, None),
12257        ];
12258
12259        let cast_options = CastOptions {
12260            safe: false,
12261            ..Default::default()
12262        };
12263
12264        for dt in data_types {
12265            assert_eq!(
12266                cast_with_options(&array, &dt, &cast_options)
12267                    .unwrap_err()
12268                    .to_string(),
12269                "Parser error: Invalid timezone \"ZZTOP\": only offset based timezones supported without chrono-tz feature"
12270            );
12271        }
12272    }
12273    #[test]
12274    fn test_cast_struct_to_struct() {
12275        let struct_type = DataType::Struct(
12276            vec![
12277                Field::new("a", DataType::Boolean, false),
12278                Field::new("b", DataType::Int32, false),
12279            ]
12280            .into(),
12281        );
12282        let to_type = DataType::Struct(
12283            vec![
12284                Field::new("a", DataType::Utf8, false),
12285                Field::new("b", DataType::Utf8, false),
12286            ]
12287            .into(),
12288        );
12289        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12290        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
12291        let struct_array = StructArray::from(vec![
12292            (
12293                Arc::new(Field::new("b", DataType::Boolean, false)),
12294                boolean.clone() as ArrayRef,
12295            ),
12296            (
12297                Arc::new(Field::new("c", DataType::Int32, false)),
12298                int.clone() as ArrayRef,
12299            ),
12300        ]);
12301        let casted_array = cast(&struct_array, &to_type).unwrap();
12302        let casted_array = casted_array.as_struct();
12303        assert_eq!(casted_array.data_type(), &to_type);
12304        let casted_boolean_array = casted_array
12305            .column(0)
12306            .as_string::<i32>()
12307            .into_iter()
12308            .flatten()
12309            .collect::<Vec<_>>();
12310        let casted_int_array = casted_array
12311            .column(1)
12312            .as_string::<i32>()
12313            .into_iter()
12314            .flatten()
12315            .collect::<Vec<_>>();
12316        assert_eq!(casted_boolean_array, vec!["false", "false", "true", "true"]);
12317        assert_eq!(casted_int_array, vec!["42", "28", "19", "31"]);
12318
12319        // test for can't cast
12320        let to_type = DataType::Struct(
12321            vec![
12322                Field::new("a", DataType::Date32, false),
12323                Field::new("b", DataType::Utf8, false),
12324            ]
12325            .into(),
12326        );
12327        assert!(!can_cast_types(&struct_type, &to_type));
12328        let result = cast(&struct_array, &to_type);
12329        assert_eq!(
12330            "Cast error: Casting from Boolean to Date32 not supported",
12331            result.unwrap_err().to_string()
12332        );
12333    }
12334
12335    #[test]
12336    fn test_cast_struct_to_struct_nullability() {
12337        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12338        let int = Arc::new(Int32Array::from(vec![Some(42), None, Some(19), None]));
12339        let struct_array = StructArray::from(vec![
12340            (
12341                Arc::new(Field::new("b", DataType::Boolean, false)),
12342                boolean.clone() as ArrayRef,
12343            ),
12344            (
12345                Arc::new(Field::new("c", DataType::Int32, true)),
12346                int.clone() as ArrayRef,
12347            ),
12348        ]);
12349
12350        // okay: nullable to nullable
12351        let to_type = DataType::Struct(
12352            vec![
12353                Field::new("a", DataType::Utf8, false),
12354                Field::new("b", DataType::Utf8, true),
12355            ]
12356            .into(),
12357        );
12358        cast(&struct_array, &to_type).expect("Cast nullable to nullable struct field should work");
12359
12360        // error: nullable to non-nullable
12361        let to_type = DataType::Struct(
12362            vec![
12363                Field::new("a", DataType::Utf8, false),
12364                Field::new("b", DataType::Utf8, false),
12365            ]
12366            .into(),
12367        );
12368        cast(&struct_array, &to_type)
12369            .expect_err("Cast nullable to non-nullable struct field should fail");
12370
12371        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12372        let int = Arc::new(Int32Array::from(vec![i32::MAX, 25, 1, 100]));
12373        let struct_array = StructArray::from(vec![
12374            (
12375                Arc::new(Field::new("b", DataType::Boolean, false)),
12376                boolean.clone() as ArrayRef,
12377            ),
12378            (
12379                Arc::new(Field::new("c", DataType::Int32, false)),
12380                int.clone() as ArrayRef,
12381            ),
12382        ]);
12383
12384        // okay: non-nullable to non-nullable
12385        let to_type = DataType::Struct(
12386            vec![
12387                Field::new("a", DataType::Utf8, false),
12388                Field::new("b", DataType::Utf8, false),
12389            ]
12390            .into(),
12391        );
12392        cast(&struct_array, &to_type)
12393            .expect("Cast non-nullable to non-nullable struct field should work");
12394
12395        // err: non-nullable to non-nullable but overflowing return null during casting
12396        let to_type = DataType::Struct(
12397            vec![
12398                Field::new("a", DataType::Utf8, false),
12399                Field::new("b", DataType::Int8, false),
12400            ]
12401            .into(),
12402        );
12403        cast(&struct_array, &to_type).expect_err(
12404            "Cast non-nullable to non-nullable struct field returning null should fail",
12405        );
12406    }
12407
12408    #[test]
12409    fn test_cast_struct_to_non_struct() {
12410        let boolean = Arc::new(BooleanArray::from(vec![true, false]));
12411        let struct_array = StructArray::from(vec![(
12412            Arc::new(Field::new("a", DataType::Boolean, false)),
12413            boolean.clone() as ArrayRef,
12414        )]);
12415        let to_type = DataType::Utf8;
12416        let result = cast(&struct_array, &to_type);
12417        assert_eq!(
12418            r#"Cast error: Casting from Struct("a": non-null Boolean) to Utf8 not supported"#,
12419            result.unwrap_err().to_string()
12420        );
12421    }
12422
12423    #[test]
12424    fn test_cast_non_struct_to_struct() {
12425        let array = StringArray::from(vec!["a", "b"]);
12426        let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
12427        let result = cast(&array, &to_type);
12428        assert_eq!(
12429            r#"Cast error: Casting from Utf8 to Struct("a": non-null Boolean) not supported"#,
12430            result.unwrap_err().to_string()
12431        );
12432    }
12433
12434    #[test]
12435    fn test_cast_struct_with_different_field_order() {
12436        // Test slow path: fields are in different order
12437        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12438        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
12439        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
12440
12441        let struct_array = StructArray::from(vec![
12442            (
12443                Arc::new(Field::new("a", DataType::Boolean, false)),
12444                boolean.clone() as ArrayRef,
12445            ),
12446            (
12447                Arc::new(Field::new("b", DataType::Int32, false)),
12448                int.clone() as ArrayRef,
12449            ),
12450            (
12451                Arc::new(Field::new("c", DataType::Utf8, false)),
12452                string.clone() as ArrayRef,
12453            ),
12454        ]);
12455
12456        // Target has fields in different order: c, a, b instead of a, b, c
12457        let to_type = DataType::Struct(
12458            vec![
12459                Field::new("c", DataType::Utf8, false),
12460                Field::new("a", DataType::Utf8, false), // Boolean to Utf8
12461                Field::new("b", DataType::Utf8, false), // Int32 to Utf8
12462            ]
12463            .into(),
12464        );
12465
12466        let result = cast(&struct_array, &to_type).unwrap();
12467        let result_struct = result.as_struct();
12468
12469        assert_eq!(result_struct.data_type(), &to_type);
12470        assert_eq!(result_struct.num_columns(), 3);
12471
12472        // Verify field "c" (originally position 2, now position 0) remains Utf8
12473        let c_column = result_struct.column(0).as_string::<i32>();
12474        assert_eq!(
12475            c_column.into_iter().flatten().collect::<Vec<_>>(),
12476            vec!["foo", "bar", "baz", "qux"]
12477        );
12478
12479        // Verify field "a" (originally position 0, now position 1) was cast from Boolean to Utf8
12480        let a_column = result_struct.column(1).as_string::<i32>();
12481        assert_eq!(
12482            a_column.into_iter().flatten().collect::<Vec<_>>(),
12483            vec!["false", "false", "true", "true"]
12484        );
12485
12486        // Verify field "b" (originally position 1, now position 2) was cast from Int32 to Utf8
12487        let b_column = result_struct.column(2).as_string::<i32>();
12488        assert_eq!(
12489            b_column.into_iter().flatten().collect::<Vec<_>>(),
12490            vec!["42", "28", "19", "31"]
12491        );
12492    }
12493
12494    #[test]
12495    fn test_cast_struct_with_missing_field() {
12496        // Test that casting fails when target has a field not present in source
12497        let boolean = Arc::new(BooleanArray::from(vec![false, true]));
12498        let struct_array = StructArray::from(vec![(
12499            Arc::new(Field::new("a", DataType::Boolean, false)),
12500            boolean.clone() as ArrayRef,
12501        )]);
12502
12503        let to_type = DataType::Struct(
12504            vec![
12505                Field::new("a", DataType::Utf8, false),
12506                Field::new("b", DataType::Int32, false), // Field "b" doesn't exist in source
12507            ]
12508            .into(),
12509        );
12510
12511        let result = cast(&struct_array, &to_type);
12512        assert!(result.is_err());
12513        assert_eq!(
12514            result.unwrap_err().to_string(),
12515            "Invalid argument error: Incorrect number of arrays for StructArray fields, expected 2 got 1"
12516        );
12517    }
12518
12519    #[test]
12520    fn test_cast_struct_with_subset_of_fields() {
12521        // Test casting to a struct with fewer fields (selecting a subset)
12522        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12523        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
12524        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
12525
12526        let struct_array = StructArray::from(vec![
12527            (
12528                Arc::new(Field::new("a", DataType::Boolean, false)),
12529                boolean.clone() as ArrayRef,
12530            ),
12531            (
12532                Arc::new(Field::new("b", DataType::Int32, false)),
12533                int.clone() as ArrayRef,
12534            ),
12535            (
12536                Arc::new(Field::new("c", DataType::Utf8, false)),
12537                string.clone() as ArrayRef,
12538            ),
12539        ]);
12540
12541        // Target has only fields "c" and "a", omitting "b"
12542        let to_type = DataType::Struct(
12543            vec![
12544                Field::new("c", DataType::Utf8, false),
12545                Field::new("a", DataType::Utf8, false),
12546            ]
12547            .into(),
12548        );
12549
12550        let result = cast(&struct_array, &to_type).unwrap();
12551        let result_struct = result.as_struct();
12552
12553        assert_eq!(result_struct.data_type(), &to_type);
12554        assert_eq!(result_struct.num_columns(), 2);
12555
12556        // Verify field "c" remains Utf8
12557        let c_column = result_struct.column(0).as_string::<i32>();
12558        assert_eq!(
12559            c_column.into_iter().flatten().collect::<Vec<_>>(),
12560            vec!["foo", "bar", "baz", "qux"]
12561        );
12562
12563        // Verify field "a" was cast from Boolean to Utf8
12564        let a_column = result_struct.column(1).as_string::<i32>();
12565        assert_eq!(
12566            a_column.into_iter().flatten().collect::<Vec<_>>(),
12567            vec!["false", "false", "true", "true"]
12568        );
12569    }
12570
12571    #[test]
12572    fn test_can_cast_struct_rename_field() {
12573        // Test that can_cast_types returns false when target has a field not in source
12574        let from_type = DataType::Struct(
12575            vec![
12576                Field::new("a", DataType::Int32, false),
12577                Field::new("b", DataType::Utf8, false),
12578            ]
12579            .into(),
12580        );
12581
12582        let to_type = DataType::Struct(
12583            vec![
12584                Field::new("a", DataType::Int64, false),
12585                Field::new("c", DataType::Boolean, false), // Field "c" not in source
12586            ]
12587            .into(),
12588        );
12589
12590        assert!(can_cast_types(&from_type, &to_type));
12591    }
12592
12593    fn run_decimal_cast_test_case_between_multiple_types(t: DecimalCastTestConfig) {
12594        run_decimal_cast_test_case::<Decimal128Type, Decimal128Type>(t.clone());
12595        run_decimal_cast_test_case::<Decimal128Type, Decimal256Type>(t.clone());
12596        run_decimal_cast_test_case::<Decimal256Type, Decimal128Type>(t.clone());
12597        run_decimal_cast_test_case::<Decimal256Type, Decimal256Type>(t.clone());
12598    }
12599
12600    #[test]
12601    fn test_decimal_to_decimal_coverage() {
12602        let test_cases = [
12603            // increase precision, increase scale, infallible
12604            DecimalCastTestConfig {
12605                input_prec: 5,
12606                input_scale: 1,
12607                input_repr: 99999, // 9999.9
12608                output_prec: 10,
12609                output_scale: 6,
12610                expected_output_repr: Ok(9999900000), // 9999.900000
12611            },
12612            // increase precision, increase scale, fallible, safe
12613            DecimalCastTestConfig {
12614                input_prec: 5,
12615                input_scale: 1,
12616                input_repr: 99, // 9999.9
12617                output_prec: 7,
12618                output_scale: 6,
12619                expected_output_repr: Ok(9900000), // 9.900000
12620            },
12621            // increase precision, increase scale, fallible, unsafe
12622            DecimalCastTestConfig {
12623                input_prec: 5,
12624                input_scale: 1,
12625                input_repr: 99999, // 9999.9
12626                output_prec: 7,
12627                output_scale: 6,
12628                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
12629            },
12630            // increase precision, decrease scale, always infallible
12631            DecimalCastTestConfig {
12632                input_prec: 5,
12633                input_scale: 3,
12634                input_repr: 99999, // 99.999
12635                output_prec: 10,
12636                output_scale: 2,
12637                expected_output_repr: Ok(10000), // 100.00
12638            },
12639            // increase precision, decrease scale, no rouding
12640            DecimalCastTestConfig {
12641                input_prec: 5,
12642                input_scale: 3,
12643                input_repr: 99994, // 99.994
12644                output_prec: 10,
12645                output_scale: 2,
12646                expected_output_repr: Ok(9999), // 99.99
12647            },
12648            // increase precision, don't change scale, always infallible
12649            DecimalCastTestConfig {
12650                input_prec: 5,
12651                input_scale: 3,
12652                input_repr: 99999, // 99.999
12653                output_prec: 10,
12654                output_scale: 3,
12655                expected_output_repr: Ok(99999), // 99.999
12656            },
12657            // decrease precision, increase scale, safe
12658            DecimalCastTestConfig {
12659                input_prec: 10,
12660                input_scale: 5,
12661                input_repr: 999999, // 9.99999
12662                output_prec: 8,
12663                output_scale: 7,
12664                expected_output_repr: Ok(99999900), // 9.9999900
12665            },
12666            // decrease precision, increase scale, unsafe
12667            DecimalCastTestConfig {
12668                input_prec: 10,
12669                input_scale: 5,
12670                input_repr: 9999999, // 99.99999
12671                output_prec: 8,
12672                output_scale: 7,
12673                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
12674            },
12675            // decrease precision, decrease scale, safe, infallible
12676            DecimalCastTestConfig {
12677                input_prec: 7,
12678                input_scale: 4,
12679                input_repr: 9999999, // 999.9999
12680                output_prec: 6,
12681                output_scale: 2,
12682                expected_output_repr: Ok(100000),
12683            },
12684            // decrease precision, decrease scale, safe, fallible
12685            DecimalCastTestConfig {
12686                input_prec: 10,
12687                input_scale: 5,
12688                input_repr: 12345678, // 123.45678
12689                output_prec: 8,
12690                output_scale: 3,
12691                expected_output_repr: Ok(123457), // 123.457
12692            },
12693            // decrease precision, decrease scale, unsafe
12694            DecimalCastTestConfig {
12695                input_prec: 10,
12696                input_scale: 5,
12697                input_repr: 9999999, // 99.99999
12698                output_prec: 4,
12699                output_scale: 3,
12700                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
12701            },
12702            // decrease precision, same scale, safe
12703            DecimalCastTestConfig {
12704                input_prec: 10,
12705                input_scale: 5,
12706                input_repr: 999999, // 9.99999
12707                output_prec: 6,
12708                output_scale: 5,
12709                expected_output_repr: Ok(999999), // 9.99999
12710            },
12711            // decrease precision, same scale, unsafe
12712            DecimalCastTestConfig {
12713                input_prec: 10,
12714                input_scale: 5,
12715                input_repr: 9999999, // 99.99999
12716                output_prec: 6,
12717                output_scale: 5,
12718                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
12719            },
12720            // same precision, increase scale, safe
12721            DecimalCastTestConfig {
12722                input_prec: 7,
12723                input_scale: 4,
12724                input_repr: 12345, // 1.2345
12725                output_prec: 7,
12726                output_scale: 6,
12727                expected_output_repr: Ok(1234500), // 1.234500
12728            },
12729            // same precision, increase scale, unsafe
12730            DecimalCastTestConfig {
12731                input_prec: 7,
12732                input_scale: 4,
12733                input_repr: 123456, // 12.3456
12734                output_prec: 7,
12735                output_scale: 6,
12736                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
12737            },
12738            // same precision, decrease scale, infallible
12739            DecimalCastTestConfig {
12740                input_prec: 7,
12741                input_scale: 5,
12742                input_repr: 1234567, // 12.34567
12743                output_prec: 7,
12744                output_scale: 4,
12745                expected_output_repr: Ok(123457), // 12.3457
12746            },
12747            // same precision, same scale, infallible
12748            DecimalCastTestConfig {
12749                input_prec: 7,
12750                input_scale: 5,
12751                input_repr: 9999999, // 99.99999
12752                output_prec: 7,
12753                output_scale: 5,
12754                expected_output_repr: Ok(9999999), // 99.99999
12755            },
12756            // precision increase, input scale & output scale = 0, infallible
12757            DecimalCastTestConfig {
12758                input_prec: 7,
12759                input_scale: 0,
12760                input_repr: 1234567, // 1234567
12761                output_prec: 8,
12762                output_scale: 0,
12763                expected_output_repr: Ok(1234567), // 1234567
12764            },
12765            // precision decrease, input scale & output scale = 0, failure
12766            DecimalCastTestConfig {
12767                input_prec: 7,
12768                input_scale: 0,
12769                input_repr: 1234567, // 1234567
12770                output_prec: 6,
12771                output_scale: 0,
12772                expected_output_repr: Err("Invalid argument error: 1234567 is too large to store in a {} of precision 6. Max is 999999".to_string())
12773            },
12774            // precision decrease, input scale & output scale = 0, success
12775            DecimalCastTestConfig {
12776                input_prec: 7,
12777                input_scale: 0,
12778                input_repr: 123456, // 123456
12779                output_prec: 6,
12780                output_scale: 0,
12781                expected_output_repr: Ok(123456), // 123456
12782            },
12783        ];
12784
12785        for t in test_cases {
12786            run_decimal_cast_test_case_between_multiple_types(t);
12787        }
12788    }
12789
12790    #[test]
12791    fn test_decimal_to_decimal_increase_scale_and_precision_unchecked() {
12792        let test_cases = [
12793            DecimalCastTestConfig {
12794                input_prec: 5,
12795                input_scale: 0,
12796                input_repr: 99999,
12797                output_prec: 10,
12798                output_scale: 5,
12799                expected_output_repr: Ok(9999900000),
12800            },
12801            DecimalCastTestConfig {
12802                input_prec: 5,
12803                input_scale: 0,
12804                input_repr: -99999,
12805                output_prec: 10,
12806                output_scale: 5,
12807                expected_output_repr: Ok(-9999900000),
12808            },
12809            DecimalCastTestConfig {
12810                input_prec: 5,
12811                input_scale: 2,
12812                input_repr: 99999,
12813                output_prec: 10,
12814                output_scale: 5,
12815                expected_output_repr: Ok(99999000),
12816            },
12817            DecimalCastTestConfig {
12818                input_prec: 5,
12819                input_scale: -2,
12820                input_repr: -99999,
12821                output_prec: 10,
12822                output_scale: 3,
12823                expected_output_repr: Ok(-9999900000),
12824            },
12825            DecimalCastTestConfig {
12826                input_prec: 5,
12827                input_scale: 3,
12828                input_repr: -12345,
12829                output_prec: 6,
12830                output_scale: 5,
12831                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())
12832            },
12833        ];
12834
12835        for t in test_cases {
12836            run_decimal_cast_test_case_between_multiple_types(t);
12837        }
12838    }
12839
12840    #[test]
12841    fn test_decimal_to_decimal_decrease_scale_and_precision_unchecked() {
12842        let test_cases = [
12843            DecimalCastTestConfig {
12844                input_prec: 5,
12845                input_scale: 0,
12846                input_repr: 99999,
12847                output_scale: -3,
12848                output_prec: 3,
12849                expected_output_repr: Ok(100),
12850            },
12851            DecimalCastTestConfig {
12852                input_prec: 5,
12853                input_scale: 0,
12854                input_repr: -99999,
12855                output_prec: 1,
12856                output_scale: -5,
12857                expected_output_repr: Ok(-1),
12858            },
12859            DecimalCastTestConfig {
12860                input_prec: 10,
12861                input_scale: 2,
12862                input_repr: 123456789,
12863                output_prec: 5,
12864                output_scale: -2,
12865                expected_output_repr: Ok(12346),
12866            },
12867            DecimalCastTestConfig {
12868                input_prec: 10,
12869                input_scale: 4,
12870                input_repr: -9876543210,
12871                output_prec: 7,
12872                output_scale: 0,
12873                expected_output_repr: Ok(-987654),
12874            },
12875            DecimalCastTestConfig {
12876                input_prec: 7,
12877                input_scale: 4,
12878                input_repr: 9999999,
12879                output_prec: 6,
12880                output_scale: 3,
12881                expected_output_repr:
12882                    Err("Invalid argument error: 1000.000 is too large to store in a {} of precision 6. Max is 999.999".to_string()),
12883            },
12884        ];
12885        for t in test_cases {
12886            run_decimal_cast_test_case_between_multiple_types(t);
12887        }
12888    }
12889
12890    #[test]
12891    fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
12892        let array = vec![Some(123456789)];
12893        let array = create_decimal128_array(array, 24, 2).unwrap();
12894        let input_type = DataType::Decimal128(24, 2);
12895        let output_type = DataType::Decimal128(6, 2);
12896        assert!(can_cast_types(&input_type, &output_type));
12897
12898        let options = CastOptions {
12899            safe: false,
12900            ..Default::default()
12901        };
12902        let result = cast_with_options(&array, &output_type, &options);
12903        assert_eq!(
12904            result.unwrap_err().to_string(),
12905            "Invalid argument error: 1234567.89 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
12906        );
12907    }
12908
12909    #[test]
12910    fn test_decimal_to_decimal_same_scale() {
12911        let array = vec![Some(520)];
12912        let array = create_decimal128_array(array, 4, 2).unwrap();
12913        let input_type = DataType::Decimal128(4, 2);
12914        let output_type = DataType::Decimal128(3, 2);
12915        assert!(can_cast_types(&input_type, &output_type));
12916
12917        let options = CastOptions {
12918            safe: false,
12919            ..Default::default()
12920        };
12921        let result = cast_with_options(&array, &output_type, &options);
12922        assert_eq!(
12923            result.unwrap().as_primitive::<Decimal128Type>().value(0),
12924            520
12925        );
12926
12927        // Cast 0 of decimal(3, 0) type to decimal(2, 0)
12928        assert_eq!(
12929            &cast(
12930                &create_decimal128_array(vec![Some(0)], 3, 0).unwrap(),
12931                &DataType::Decimal128(2, 0)
12932            )
12933            .unwrap(),
12934            &(Arc::new(create_decimal128_array(vec![Some(0)], 2, 0).unwrap()) as ArrayRef)
12935        );
12936    }
12937
12938    #[test]
12939    fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale() {
12940        let array = vec![Some(123456789)];
12941        let array = create_decimal128_array(array, 24, 4).unwrap();
12942        let input_type = DataType::Decimal128(24, 4);
12943        let output_type = DataType::Decimal128(6, 2);
12944        assert!(can_cast_types(&input_type, &output_type));
12945
12946        let options = CastOptions {
12947            safe: false,
12948            ..Default::default()
12949        };
12950        let result = cast_with_options(&array, &output_type, &options);
12951        assert_eq!(
12952            result.unwrap_err().to_string(),
12953            "Invalid argument error: 12345.68 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
12954        );
12955    }
12956
12957    #[test]
12958    fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale() {
12959        let array = vec![Some(123456789)];
12960        let array = create_decimal128_array(array, 24, 2).unwrap();
12961        let input_type = DataType::Decimal128(24, 2);
12962        let output_type = DataType::Decimal128(6, 3);
12963        assert!(can_cast_types(&input_type, &output_type));
12964
12965        let options = CastOptions {
12966            safe: false,
12967            ..Default::default()
12968        };
12969        let result = cast_with_options(&array, &output_type, &options);
12970        assert_eq!(
12971            result.unwrap_err().to_string(),
12972            "Invalid argument error: 1234567.890 is too large to store in a Decimal128 of precision 6. Max is 999.999"
12973        );
12974    }
12975
12976    #[test]
12977    fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type() {
12978        let array = vec![Some(123456789)];
12979        let array = create_decimal128_array(array, 24, 2).unwrap();
12980        let input_type = DataType::Decimal128(24, 2);
12981        let output_type = DataType::Decimal256(6, 2);
12982        assert!(can_cast_types(&input_type, &output_type));
12983
12984        let options = CastOptions {
12985            safe: false,
12986            ..Default::default()
12987        };
12988        let result = cast_with_options(&array, &output_type, &options).unwrap_err();
12989        assert_eq!(
12990            result.to_string(),
12991            "Invalid argument error: 1234567.89 is too large to store in a Decimal256 of precision 6. Max is 9999.99"
12992        );
12993    }
12994
12995    #[test]
12996    fn test_first_none() {
12997        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
12998            None,
12999            Some(vec![Some(1), Some(2)]),
13000        ])) as ArrayRef;
13001        let data_type =
13002            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
13003        let opt = CastOptions::default();
13004        let r = cast_with_options(&array, &data_type, &opt).unwrap();
13005
13006        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
13007            vec![None, Some(vec![Some(1), Some(2)])],
13008            2,
13009        )) as ArrayRef;
13010        assert_eq!(*fixed_array, *r);
13011    }
13012
13013    #[test]
13014    fn test_first_last_none() {
13015        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
13016            None,
13017            Some(vec![Some(1), Some(2)]),
13018            None,
13019        ])) as ArrayRef;
13020        let data_type =
13021            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
13022        let opt = CastOptions::default();
13023        let r = cast_with_options(&array, &data_type, &opt).unwrap();
13024
13025        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
13026            vec![None, Some(vec![Some(1), Some(2)]), None],
13027            2,
13028        )) as ArrayRef;
13029        assert_eq!(*fixed_array, *r);
13030    }
13031
13032    #[test]
13033    fn test_cast_decimal_error_output() {
13034        let array = Int64Array::from(vec![1]);
13035        let error = cast_with_options(
13036            &array,
13037            &DataType::Decimal32(1, 1),
13038            &CastOptions {
13039                safe: false,
13040                format_options: FormatOptions::default(),
13041            },
13042        )
13043        .unwrap_err();
13044        assert_eq!(
13045            error.to_string(),
13046            "Invalid argument error: 1.0 is too large to store in a Decimal32 of precision 1. Max is 0.9"
13047        );
13048
13049        let array = Int64Array::from(vec![-1]);
13050        let error = cast_with_options(
13051            &array,
13052            &DataType::Decimal32(1, 1),
13053            &CastOptions {
13054                safe: false,
13055                format_options: FormatOptions::default(),
13056            },
13057        )
13058        .unwrap_err();
13059        assert_eq!(
13060            error.to_string(),
13061            "Invalid argument error: -1.0 is too small to store in a Decimal32 of precision 1. Min is -0.9"
13062        );
13063    }
13064
13065    #[test]
13066    fn test_run_end_encoded_to_primitive() {
13067        // Create a RunEndEncoded array: [1, 1, 2, 2, 2, 3]
13068        let run_ends = Int32Array::from(vec![2, 5, 6]);
13069        let values = Int32Array::from(vec![1, 2, 3]);
13070        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13071        let array_ref = Arc::new(run_array) as ArrayRef;
13072        // Cast to Int64
13073        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
13074        // Verify the result is a RunArray with Int64 values
13075        let result_run_array = cast_result.as_any().downcast_ref::<Int64Array>().unwrap();
13076        assert_eq!(
13077            result_run_array.values(),
13078            &[1i64, 1i64, 2i64, 2i64, 2i64, 3i64]
13079        );
13080    }
13081
13082    #[test]
13083    fn test_sliced_run_end_encoded_to_primitive() {
13084        let run_ends = Int32Array::from(vec![2, 5, 6]);
13085        let values = Int32Array::from(vec![1, 2, 3]);
13086        // [1, 1, 2, 2, 2, 3]
13087        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13088        let run_array = run_array.slice(3, 3); // [2, 2, 3]
13089        let array_ref = Arc::new(run_array) as ArrayRef;
13090
13091        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
13092        let result_run_array = cast_result.as_primitive::<Int64Type>();
13093        assert_eq!(result_run_array.values(), &[2, 2, 3]);
13094    }
13095
13096    #[test]
13097    fn test_run_end_encoded_to_string() {
13098        let run_ends = Int32Array::from(vec![2, 3, 5]);
13099        let values = Int32Array::from(vec![10, 20, 30]);
13100        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13101        let array_ref = Arc::new(run_array) as ArrayRef;
13102
13103        // Cast to String
13104        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
13105
13106        // Verify the result is a RunArray with String values
13107        let result_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
13108        // Check that values are correct
13109        assert_eq!(result_array.value(0), "10");
13110        assert_eq!(result_array.value(1), "10");
13111        assert_eq!(result_array.value(2), "20");
13112    }
13113
13114    #[test]
13115    fn test_primitive_to_run_end_encoded() {
13116        // Create an Int32 array with repeated values: [1, 1, 2, 2, 2, 3]
13117        let source_array = Int32Array::from(vec![1, 1, 2, 2, 2, 3]);
13118        let array_ref = Arc::new(source_array) as ArrayRef;
13119
13120        // Cast to RunEndEncoded<Int32, Int32>
13121        let target_type = DataType::RunEndEncoded(
13122            Arc::new(Field::new("run_ends", DataType::Int32, false)),
13123            Arc::new(Field::new("values", DataType::Int32, true)),
13124        );
13125        let cast_result = cast(&array_ref, &target_type).unwrap();
13126
13127        // Verify the result is a RunArray
13128        let result_run_array = cast_result
13129            .as_any()
13130            .downcast_ref::<RunArray<Int32Type>>()
13131            .unwrap();
13132
13133        // Check run structure: runs should end at positions [2, 5, 6]
13134        assert_eq!(result_run_array.run_ends().values(), &[2, 5, 6]);
13135
13136        // Check values: should be [1, 2, 3]
13137        let values_array = result_run_array.values().as_primitive::<Int32Type>();
13138        assert_eq!(values_array.values(), &[1, 2, 3]);
13139    }
13140
13141    #[test]
13142    fn test_primitive_to_run_end_encoded_with_nulls() {
13143        let source_array = Int32Array::from(vec![
13144            Some(1),
13145            Some(1),
13146            None,
13147            None,
13148            Some(2),
13149            Some(2),
13150            Some(3),
13151            Some(3),
13152            None,
13153            None,
13154            Some(4),
13155            Some(4),
13156            Some(5),
13157            Some(5),
13158            None,
13159            None,
13160        ]);
13161        let array_ref = Arc::new(source_array) as ArrayRef;
13162        let target_type = DataType::RunEndEncoded(
13163            Arc::new(Field::new("run_ends", DataType::Int32, false)),
13164            Arc::new(Field::new("values", DataType::Int32, true)),
13165        );
13166        let cast_result = cast(&array_ref, &target_type).unwrap();
13167        let result_run_array = cast_result
13168            .as_any()
13169            .downcast_ref::<RunArray<Int32Type>>()
13170            .unwrap();
13171        assert_eq!(
13172            result_run_array.run_ends().values(),
13173            &[2, 4, 6, 8, 10, 12, 14, 16]
13174        );
13175        assert_eq!(
13176            result_run_array
13177                .values()
13178                .as_primitive::<Int32Type>()
13179                .values(),
13180            &[1, 0, 2, 3, 0, 4, 5, 0]
13181        );
13182        assert_eq!(result_run_array.values().null_count(), 3);
13183    }
13184
13185    #[test]
13186    fn test_primitive_to_run_end_encoded_with_nulls_consecutive() {
13187        let source_array = Int64Array::from(vec![
13188            Some(1),
13189            Some(1),
13190            None,
13191            None,
13192            None,
13193            None,
13194            None,
13195            None,
13196            None,
13197            None,
13198            Some(4),
13199            Some(20),
13200            Some(500),
13201            Some(500),
13202            None,
13203            None,
13204        ]);
13205        let array_ref = Arc::new(source_array) as ArrayRef;
13206        let target_type = DataType::RunEndEncoded(
13207            Arc::new(Field::new("run_ends", DataType::Int16, false)),
13208            Arc::new(Field::new("values", DataType::Int64, true)),
13209        );
13210        let cast_result = cast(&array_ref, &target_type).unwrap();
13211        let result_run_array = cast_result
13212            .as_any()
13213            .downcast_ref::<RunArray<Int16Type>>()
13214            .unwrap();
13215        assert_eq!(
13216            result_run_array.run_ends().values(),
13217            &[2, 10, 11, 12, 14, 16]
13218        );
13219        assert_eq!(
13220            result_run_array
13221                .values()
13222                .as_primitive::<Int64Type>()
13223                .values(),
13224            &[1, 0, 4, 20, 500, 0]
13225        );
13226        assert_eq!(result_run_array.values().null_count(), 2);
13227    }
13228
13229    #[test]
13230    fn test_string_to_run_end_encoded() {
13231        // Create a String array with repeated values: ["a", "a", "b", "c", "c"]
13232        let source_array = StringArray::from(vec!["a", "a", "b", "c", "c"]);
13233        let array_ref = Arc::new(source_array) as ArrayRef;
13234
13235        // Cast to RunEndEncoded<Int32, String>
13236        let target_type = DataType::RunEndEncoded(
13237            Arc::new(Field::new("run_ends", DataType::Int32, false)),
13238            Arc::new(Field::new("values", DataType::Utf8, true)),
13239        );
13240        let cast_result = cast(&array_ref, &target_type).unwrap();
13241
13242        // Verify the result is a RunArray
13243        let result_run_array = cast_result
13244            .as_any()
13245            .downcast_ref::<RunArray<Int32Type>>()
13246            .unwrap();
13247
13248        // Check run structure: runs should end at positions [2, 3, 5]
13249        assert_eq!(result_run_array.run_ends().values(), &[2, 3, 5]);
13250
13251        // Check values: should be ["a", "b", "c"]
13252        let values_array = result_run_array.values().as_string::<i32>();
13253        assert_eq!(values_array.value(0), "a");
13254        assert_eq!(values_array.value(1), "b");
13255        assert_eq!(values_array.value(2), "c");
13256    }
13257
13258    #[test]
13259    fn test_empty_array_to_run_end_encoded() {
13260        // Create an empty Int32 array
13261        let source_array = Int32Array::from(Vec::<i32>::new());
13262        let array_ref = Arc::new(source_array) as ArrayRef;
13263
13264        // Cast to RunEndEncoded<Int32, Int32>
13265        let target_type = DataType::RunEndEncoded(
13266            Arc::new(Field::new("run_ends", DataType::Int32, false)),
13267            Arc::new(Field::new("values", DataType::Int32, true)),
13268        );
13269        let cast_result = cast(&array_ref, &target_type).unwrap();
13270
13271        // Verify the result is an empty RunArray
13272        let result_run_array = cast_result
13273            .as_any()
13274            .downcast_ref::<RunArray<Int32Type>>()
13275            .unwrap();
13276
13277        // Check that both run_ends and values are empty
13278        assert_eq!(result_run_array.run_ends().len(), 0);
13279        assert_eq!(result_run_array.values().len(), 0);
13280    }
13281
13282    #[test]
13283    fn test_run_end_encoded_with_nulls() {
13284        // Create a RunEndEncoded array with nulls: [1, 1, null, 2, 2]
13285        let run_ends = Int32Array::from(vec![2, 3, 5]);
13286        let values = Int32Array::from(vec![Some(1), None, Some(2)]);
13287        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13288        let array_ref = Arc::new(run_array) as ArrayRef;
13289
13290        // Cast to String
13291        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
13292
13293        // Verify the result preserves nulls
13294        let result_run_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
13295        assert_eq!(result_run_array.value(0), "1");
13296        assert!(result_run_array.is_null(2));
13297        assert_eq!(result_run_array.value(4), "2");
13298    }
13299
13300    #[test]
13301    fn test_different_index_types() {
13302        // Test with Int16 index type
13303        let source_array = Int32Array::from(vec![1, 1, 2, 3, 3]);
13304        let array_ref = Arc::new(source_array) as ArrayRef;
13305
13306        let target_type = DataType::RunEndEncoded(
13307            Arc::new(Field::new("run_ends", DataType::Int16, false)),
13308            Arc::new(Field::new("values", DataType::Int32, true)),
13309        );
13310        let cast_result = cast(&array_ref, &target_type).unwrap();
13311        assert_eq!(cast_result.data_type(), &target_type);
13312
13313        // Verify the cast worked correctly: values are [1, 2, 3]
13314        // and run-ends are [2, 3, 5]
13315        let run_array = cast_result
13316            .as_any()
13317            .downcast_ref::<RunArray<Int16Type>>()
13318            .unwrap();
13319        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
13320        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
13321        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
13322        assert_eq!(run_array.run_ends().values(), &[2i16, 3i16, 5i16]);
13323
13324        // Test again with Int64 index type
13325        let target_type = DataType::RunEndEncoded(
13326            Arc::new(Field::new("run_ends", DataType::Int64, false)),
13327            Arc::new(Field::new("values", DataType::Int32, true)),
13328        );
13329        let cast_result = cast(&array_ref, &target_type).unwrap();
13330        assert_eq!(cast_result.data_type(), &target_type);
13331
13332        // Verify the cast worked correctly: values are [1, 2, 3]
13333        // and run-ends are [2, 3, 5]
13334        let run_array = cast_result
13335            .as_any()
13336            .downcast_ref::<RunArray<Int64Type>>()
13337            .unwrap();
13338        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
13339        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
13340        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
13341        assert_eq!(run_array.run_ends().values(), &[2i64, 3i64, 5i64]);
13342    }
13343
13344    #[test]
13345    fn test_unsupported_cast_to_run_end_encoded() {
13346        // Create a Struct array - complex nested type that might not be supported
13347        let field = Field::new("item", DataType::Int32, false);
13348        let struct_array = StructArray::from(vec![(
13349            Arc::new(field),
13350            Arc::new(Int32Array::from(vec![1, 2, 3])) as ArrayRef,
13351        )]);
13352        let array_ref = Arc::new(struct_array) as ArrayRef;
13353
13354        // This should fail because:
13355        // 1. The target type is not RunEndEncoded
13356        // 2. The target type is not supported for casting from StructArray
13357        let cast_result = cast(&array_ref, &DataType::FixedSizeBinary(10));
13358
13359        // Expect this to fail
13360        assert!(cast_result.is_err());
13361    }
13362
13363    /// Test casting RunEndEncoded<Int64, String> to RunEndEncoded<Int16, String> should fail
13364    #[test]
13365    fn test_cast_run_end_encoded_int64_to_int16_should_fail() {
13366        // Construct a valid REE array with Int64 run-ends
13367        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
13368        let values = StringArray::from(vec!["a", "b", "c"]);
13369
13370        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
13371        let array_ref = Arc::new(ree_array) as ArrayRef;
13372
13373        // Attempt to cast to RunEndEncoded<Int16, Utf8>
13374        let target_type = DataType::RunEndEncoded(
13375            Arc::new(Field::new("run_ends", DataType::Int16, false)),
13376            Arc::new(Field::new("values", DataType::Utf8, true)),
13377        );
13378        let cast_options = CastOptions {
13379            safe: false, // This should make it fail instead of returning nulls
13380            format_options: FormatOptions::default(),
13381        };
13382
13383        // This should fail due to run-end overflow
13384        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
13385            cast_with_options(&array_ref, &target_type, &cast_options);
13386
13387        let e = result.expect_err("Cast should have failed but succeeded");
13388        assert!(
13389            e.to_string()
13390                .contains("Cast error: Can't cast value 100000 to type Int16")
13391        );
13392    }
13393
13394    #[test]
13395    fn test_cast_run_end_encoded_int64_to_int16_with_safe_should_fail_with_null_invalid_error() {
13396        // Construct a valid REE array with Int64 run-ends
13397        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
13398        let values = StringArray::from(vec!["a", "b", "c"]);
13399
13400        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
13401        let array_ref = Arc::new(ree_array) as ArrayRef;
13402
13403        // Attempt to cast to RunEndEncoded<Int16, Utf8>
13404        let target_type = DataType::RunEndEncoded(
13405            Arc::new(Field::new("run_ends", DataType::Int16, false)),
13406            Arc::new(Field::new("values", DataType::Utf8, true)),
13407        );
13408        let cast_options = CastOptions {
13409            safe: true,
13410            format_options: FormatOptions::default(),
13411        };
13412
13413        // This fails even though safe is true because the run_ends array has null values
13414        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
13415            cast_with_options(&array_ref, &target_type, &cast_options);
13416        let e = result.expect_err("Cast should have failed but succeeded");
13417        assert!(
13418            e.to_string()
13419                .contains("Invalid argument error: Found null values in run_ends array. The run_ends array should not have null values.")
13420        );
13421    }
13422
13423    /// Test casting RunEndEncoded<Int16, String> to RunEndEncoded<Int64, String> should succeed
13424    #[test]
13425    fn test_cast_run_end_encoded_int16_to_int64_should_succeed() {
13426        // Construct a valid REE array with Int16 run-ends
13427        let run_ends = Int16Array::from(vec![2, 5, 8]); // values that fit in Int16
13428        let values = StringArray::from(vec!["a", "b", "c"]);
13429
13430        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
13431        let array_ref = Arc::new(ree_array) as ArrayRef;
13432
13433        // Attempt to cast to RunEndEncoded<Int64, Utf8> (upcast should succeed)
13434        let target_type = DataType::RunEndEncoded(
13435            Arc::new(Field::new("run_ends", DataType::Int64, false)),
13436            Arc::new(Field::new("values", DataType::Utf8, true)),
13437        );
13438        let cast_options = CastOptions {
13439            safe: false,
13440            format_options: FormatOptions::default(),
13441        };
13442
13443        // This should succeed due to valid upcast
13444        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
13445            cast_with_options(&array_ref, &target_type, &cast_options);
13446
13447        let array_ref = result.expect("Cast should have succeeded but failed");
13448        // Downcast to RunArray<Int64Type>
13449        let run_array = array_ref
13450            .as_any()
13451            .downcast_ref::<RunArray<Int64Type>>()
13452            .unwrap();
13453
13454        // Verify the cast worked correctly
13455        // Assert the values were cast correctly
13456        assert_eq!(run_array.run_ends().values(), &[2i64, 5i64, 8i64]);
13457        assert_eq!(run_array.values().as_string::<i32>().value(0), "a");
13458        assert_eq!(run_array.values().as_string::<i32>().value(1), "b");
13459        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
13460    }
13461
13462    #[test]
13463    fn test_cast_run_end_encoded_dictionary_to_run_end_encoded() {
13464        // Construct a valid dictionary encoded array
13465        let values = StringArray::from_iter([Some("a"), Some("b"), Some("c")]);
13466        let keys = UInt64Array::from_iter(vec![1, 1, 1, 0, 0, 0, 2, 2, 2]);
13467        let array_ref = Arc::new(DictionaryArray::new(keys, Arc::new(values))) as ArrayRef;
13468
13469        // Attempt to cast to RunEndEncoded<Int64, Utf8>
13470        let target_type = DataType::RunEndEncoded(
13471            Arc::new(Field::new("run_ends", DataType::Int64, false)),
13472            Arc::new(Field::new("values", DataType::Utf8, true)),
13473        );
13474        let cast_options = CastOptions {
13475            safe: false,
13476            format_options: FormatOptions::default(),
13477        };
13478
13479        // This should succeed
13480        let result = cast_with_options(&array_ref, &target_type, &cast_options)
13481            .expect("Cast should have succeeded but failed");
13482
13483        // Verify the cast worked correctly
13484        // Assert the values were cast correctly
13485        let run_array = result
13486            .as_any()
13487            .downcast_ref::<RunArray<Int64Type>>()
13488            .unwrap();
13489        assert_eq!(run_array.values().as_string::<i32>().value(0), "b");
13490        assert_eq!(run_array.values().as_string::<i32>().value(1), "a");
13491        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
13492
13493        // Verify the run-ends were cast correctly (run ends at 3, 6, 9)
13494        assert_eq!(run_array.run_ends().values(), &[3i64, 6i64, 9i64]);
13495    }
13496
13497    fn int32_list_values() -> Vec<Option<Vec<Option<i32>>>> {
13498        vec![
13499            Some(vec![Some(1), Some(2), Some(3)]),
13500            Some(vec![Some(4), Some(5), Some(6)]),
13501            None,
13502            Some(vec![Some(7), Some(8), Some(9)]),
13503            Some(vec![None, Some(10)]),
13504        ]
13505    }
13506
13507    #[test]
13508    fn test_cast_list_view_to_list() {
13509        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13510        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13511        assert!(can_cast_types(list_view.data_type(), &target_type));
13512        let cast_result = cast(&list_view, &target_type).unwrap();
13513        let got_list = cast_result.as_list::<i32>();
13514        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13515        assert_eq!(got_list, &expected_list);
13516    }
13517
13518    #[test]
13519    fn test_cast_list_view_to_large_list() {
13520        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13521        let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
13522        assert!(can_cast_types(list_view.data_type(), &target_type));
13523        let cast_result = cast(&list_view, &target_type).unwrap();
13524        let got_list = cast_result.as_list::<i64>();
13525        let expected_list =
13526            LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13527        assert_eq!(got_list, &expected_list);
13528    }
13529
13530    #[test]
13531    fn test_cast_list_to_list_view() {
13532        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13533        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
13534        assert!(can_cast_types(list.data_type(), &target_type));
13535        let cast_result = cast(&list, &target_type).unwrap();
13536
13537        let got_list_view = cast_result.as_list_view::<i32>();
13538        let expected_list_view =
13539            ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13540        assert_eq!(got_list_view, &expected_list_view);
13541
13542        // inner types get cast
13543        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13544            Some(vec![Some(1), Some(2)]),
13545            None,
13546            Some(vec![None, Some(3)]),
13547        ]);
13548        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Float32, true)));
13549        assert!(can_cast_types(list.data_type(), &target_type));
13550        let cast_result = cast(&list, &target_type).unwrap();
13551
13552        let got_list_view = cast_result.as_list_view::<i32>();
13553        let expected_list_view = ListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13554            Some(vec![Some(1.0), Some(2.0)]),
13555            None,
13556            Some(vec![None, Some(3.0)]),
13557        ]);
13558        assert_eq!(got_list_view, &expected_list_view);
13559    }
13560
13561    #[test]
13562    fn test_cast_list_to_large_list_view() {
13563        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13564            Some(vec![Some(1), Some(2)]),
13565            None,
13566            Some(vec![None, Some(3)]),
13567        ]);
13568        let target_type =
13569            DataType::LargeListView(Arc::new(Field::new("item", DataType::Float32, true)));
13570        assert!(can_cast_types(list.data_type(), &target_type));
13571        let cast_result = cast(&list, &target_type).unwrap();
13572
13573        let got_list_view = cast_result.as_list_view::<i64>();
13574        let expected_list_view =
13575            LargeListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13576                Some(vec![Some(1.0), Some(2.0)]),
13577                None,
13578                Some(vec![None, Some(3.0)]),
13579            ]);
13580        assert_eq!(got_list_view, &expected_list_view);
13581    }
13582
13583    #[test]
13584    fn test_cast_large_list_view_to_large_list() {
13585        let list_view =
13586            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13587        let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
13588        assert!(can_cast_types(list_view.data_type(), &target_type));
13589        let cast_result = cast(&list_view, &target_type).unwrap();
13590        let got_list = cast_result.as_list::<i64>();
13591
13592        let expected_list =
13593            LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13594        assert_eq!(got_list, &expected_list);
13595    }
13596
13597    #[test]
13598    fn test_cast_large_list_view_to_list() {
13599        let list_view =
13600            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
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
13606        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13607        assert_eq!(got_list, &expected_list);
13608    }
13609
13610    #[test]
13611    fn test_cast_large_list_to_large_list_view() {
13612        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13613        let target_type =
13614            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
13615        assert!(can_cast_types(list.data_type(), &target_type));
13616        let cast_result = cast(&list, &target_type).unwrap();
13617
13618        let got_list_view = cast_result.as_list_view::<i64>();
13619        let expected_list_view =
13620            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13621        assert_eq!(got_list_view, &expected_list_view);
13622
13623        // inner types get cast
13624        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13625            Some(vec![Some(1), Some(2)]),
13626            None,
13627            Some(vec![None, Some(3)]),
13628        ]);
13629        let target_type =
13630            DataType::LargeListView(Arc::new(Field::new("item", DataType::Float32, true)));
13631        assert!(can_cast_types(list.data_type(), &target_type));
13632        let cast_result = cast(&list, &target_type).unwrap();
13633
13634        let got_list_view = cast_result.as_list_view::<i64>();
13635        let expected_list_view =
13636            LargeListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13637                Some(vec![Some(1.0), Some(2.0)]),
13638                None,
13639                Some(vec![None, Some(3.0)]),
13640            ]);
13641        assert_eq!(got_list_view, &expected_list_view);
13642    }
13643
13644    #[test]
13645    fn test_cast_large_list_to_list_view() {
13646        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13647            Some(vec![Some(1), Some(2)]),
13648            None,
13649            Some(vec![None, Some(3)]),
13650        ]);
13651        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Float32, true)));
13652        assert!(can_cast_types(list.data_type(), &target_type));
13653        let cast_result = cast(&list, &target_type).unwrap();
13654
13655        let got_list_view = cast_result.as_list_view::<i32>();
13656        let expected_list_view = ListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13657            Some(vec![Some(1.0), Some(2.0)]),
13658            None,
13659            Some(vec![None, Some(3.0)]),
13660        ]);
13661        assert_eq!(got_list_view, &expected_list_view);
13662    }
13663
13664    #[test]
13665    fn test_cast_list_view_to_list_out_of_order() {
13666        let list_view = ListViewArray::new(
13667            Arc::new(Field::new("item", DataType::Int32, true)),
13668            ScalarBuffer::from(vec![0, 6, 3]),
13669            ScalarBuffer::from(vec![3, 3, 3]),
13670            Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
13671            None,
13672        );
13673        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13674        assert!(can_cast_types(list_view.data_type(), &target_type));
13675        let cast_result = cast(&list_view, &target_type).unwrap();
13676        let got_list = cast_result.as_list::<i32>();
13677        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13678            Some(vec![Some(1), Some(2), Some(3)]),
13679            Some(vec![Some(7), Some(8), Some(9)]),
13680            Some(vec![Some(4), Some(5), Some(6)]),
13681        ]);
13682        assert_eq!(got_list, &expected_list);
13683    }
13684
13685    #[test]
13686    fn test_cast_list_view_to_list_overlapping() {
13687        let list_view = ListViewArray::new(
13688            Arc::new(Field::new("item", DataType::Int32, true)),
13689            ScalarBuffer::from(vec![0, 0]),
13690            ScalarBuffer::from(vec![1, 2]),
13691            Arc::new(Int32Array::from(vec![1, 2])),
13692            None,
13693        );
13694        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13695        assert!(can_cast_types(list_view.data_type(), &target_type));
13696        let cast_result = cast(&list_view, &target_type).unwrap();
13697        let got_list = cast_result.as_list::<i32>();
13698        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13699            Some(vec![Some(1)]),
13700            Some(vec![Some(1), Some(2)]),
13701        ]);
13702        assert_eq!(got_list, &expected_list);
13703    }
13704
13705    #[test]
13706    fn test_cast_list_view_to_list_empty() {
13707        let values: Vec<Option<Vec<Option<i32>>>> = vec![];
13708        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
13709        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13710        assert!(can_cast_types(list_view.data_type(), &target_type));
13711        let cast_result = cast(&list_view, &target_type).unwrap();
13712        let got_list = cast_result.as_list::<i32>();
13713        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
13714        assert_eq!(got_list, &expected_list);
13715    }
13716
13717    #[test]
13718    fn test_cast_list_view_to_list_different_inner_type() {
13719        let values = int32_list_values();
13720        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
13721        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int64, true)));
13722        assert!(can_cast_types(list_view.data_type(), &target_type));
13723        let cast_result = cast(&list_view, &target_type).unwrap();
13724        let got_list = cast_result.as_list::<i32>();
13725
13726        let expected_list =
13727            ListArray::from_iter_primitive::<Int64Type, _, _>(values.into_iter().map(|list| {
13728                list.map(|list| {
13729                    list.into_iter()
13730                        .map(|v| v.map(|v| v as i64))
13731                        .collect::<Vec<_>>()
13732                })
13733            }));
13734        assert_eq!(got_list, &expected_list);
13735    }
13736
13737    #[test]
13738    fn test_cast_list_view_to_list_out_of_order_with_nulls() {
13739        let list_view = ListViewArray::new(
13740            Arc::new(Field::new("item", DataType::Int32, true)),
13741            ScalarBuffer::from(vec![0, 6, 3]),
13742            ScalarBuffer::from(vec![3, 3, 3]),
13743            Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
13744            Some(NullBuffer::from(vec![false, true, false])),
13745        );
13746        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13747        assert!(can_cast_types(list_view.data_type(), &target_type));
13748        let cast_result = cast(&list_view, &target_type).unwrap();
13749        let got_list = cast_result.as_list::<i32>();
13750        let expected_list = ListArray::new(
13751            Arc::new(Field::new("item", DataType::Int32, true)),
13752            OffsetBuffer::from_lengths([3, 3, 3]),
13753            Arc::new(Int32Array::from(vec![1, 2, 3, 7, 8, 9, 4, 5, 6])),
13754            Some(NullBuffer::from(vec![false, true, false])),
13755        );
13756        assert_eq!(got_list, &expected_list);
13757    }
13758
13759    #[test]
13760    fn test_cast_list_view_to_large_list_view() {
13761        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13762        let target_type =
13763            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
13764        assert!(can_cast_types(list_view.data_type(), &target_type));
13765        let cast_result = cast(&list_view, &target_type).unwrap();
13766        let got = cast_result.as_list_view::<i64>();
13767
13768        let expected =
13769            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13770        assert_eq!(got, &expected);
13771    }
13772
13773    #[test]
13774    fn test_cast_large_list_view_to_list_view() {
13775        let list_view =
13776            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13777        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
13778        assert!(can_cast_types(list_view.data_type(), &target_type));
13779        let cast_result = cast(&list_view, &target_type).unwrap();
13780        let got = cast_result.as_list_view::<i32>();
13781
13782        let expected = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13783        assert_eq!(got, &expected);
13784    }
13785
13786    #[test]
13787    fn test_cast_time32_second_to_int64() {
13788        let array = Time32SecondArray::from(vec![1000, 2000, 3000]);
13789        let array = Arc::new(array) as Arc<dyn Array>;
13790        let to_type = DataType::Int64;
13791        let cast_options = CastOptions::default();
13792
13793        assert!(can_cast_types(array.data_type(), &to_type));
13794
13795        let result = cast_with_options(&array, &to_type, &cast_options);
13796        assert!(
13797            result.is_ok(),
13798            "Failed to cast Time32(Second) to Int64: {:?}",
13799            result.err()
13800        );
13801
13802        let cast_array = result.unwrap();
13803        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13804
13805        assert_eq!(cast_array.value(0), 1000);
13806        assert_eq!(cast_array.value(1), 2000);
13807        assert_eq!(cast_array.value(2), 3000);
13808    }
13809
13810    #[test]
13811    fn test_cast_time32_millisecond_to_int64() {
13812        let array = Time32MillisecondArray::from(vec![1000, 2000, 3000]);
13813        let array = Arc::new(array) as Arc<dyn Array>;
13814        let to_type = DataType::Int64;
13815        let cast_options = CastOptions::default();
13816
13817        assert!(can_cast_types(array.data_type(), &to_type));
13818
13819        let result = cast_with_options(&array, &to_type, &cast_options);
13820        assert!(
13821            result.is_ok(),
13822            "Failed to cast Time32(Millisecond) to Int64: {:?}",
13823            result.err()
13824        );
13825
13826        let cast_array = result.unwrap();
13827        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13828
13829        assert_eq!(cast_array.value(0), 1000);
13830        assert_eq!(cast_array.value(1), 2000);
13831        assert_eq!(cast_array.value(2), 3000);
13832    }
13833
13834    #[test]
13835    fn test_cast_time32_millisecond_to_time64_nanosecond() {
13836        let array =
13837            Time32MillisecondArray::from(vec![Some(1_000), Some(2_000), None, Some(43_200_000)]);
13838        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
13839        let c = b.as_primitive::<Time64NanosecondType>();
13840        assert_eq!(c.value(0), 1_000_000_000);
13841        assert_eq!(c.value(1), 2_000_000_000);
13842        assert!(c.is_null(2));
13843        assert_eq!(c.value(3), 43_200_000_000_000);
13844    }
13845
13846    #[test]
13847    fn test_cast_time32_millisecond_to_time64_microsecond() {
13848        let array =
13849            Time32MillisecondArray::from(vec![Some(1_000), Some(2_000), None, Some(43_200_000)]);
13850        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
13851        let c = b.as_primitive::<Time64MicrosecondType>();
13852        assert_eq!(c.value(0), 1_000_000);
13853        assert_eq!(c.value(1), 2_000_000);
13854        assert!(c.is_null(2));
13855        assert_eq!(c.value(3), 43_200_000_000);
13856    }
13857
13858    #[test]
13859    fn test_cast_time32_second_to_time64_nanosecond() {
13860        let array = Time32SecondArray::from(vec![Some(1), Some(60), None, Some(43_200)]);
13861        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
13862        let c = b.as_primitive::<Time64NanosecondType>();
13863        assert_eq!(c.value(0), 1_000_000_000);
13864        assert_eq!(c.value(1), 60_000_000_000);
13865        assert!(c.is_null(2));
13866        assert_eq!(c.value(3), 43_200_000_000_000);
13867    }
13868
13869    #[test]
13870    fn test_cast_time32_second_to_time64_microsecond() {
13871        let array = Time32SecondArray::from(vec![Some(1), Some(60), None, Some(43_200)]);
13872        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
13873        let c = b.as_primitive::<Time64MicrosecondType>();
13874        assert_eq!(c.value(0), 1_000_000);
13875        assert_eq!(c.value(1), 60_000_000);
13876        assert!(c.is_null(2));
13877        assert_eq!(c.value(3), 43_200_000_000);
13878    }
13879
13880    #[test]
13881    fn test_cast_time32_second_to_time32_millisecond_overflow() {
13882        let array = Time32SecondArray::from(vec![i32::MAX]);
13883
13884        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
13885        let c = b.as_primitive::<Time32MillisecondType>();
13886        assert!(c.is_null(0));
13887
13888        let options = CastOptions {
13889            safe: false,
13890            ..Default::default()
13891        };
13892        let err = cast_with_options(&array, &DataType::Time32(TimeUnit::Millisecond), &options)
13893            .unwrap_err();
13894        assert!(err.to_string().contains("Overflow"), "{err}");
13895    }
13896
13897    #[test]
13898    fn test_cast_string_to_time32_second_to_int64() {
13899        // Mimic: select arrow_cast('03:12:44'::time, 'Time32(Second)')::bigint;
13900        // raised in https://github.com/apache/datafusion/issues/19036
13901        let array = StringArray::from(vec!["03:12:44"]);
13902        let array = Arc::new(array) as Arc<dyn Array>;
13903        let cast_options = CastOptions::default();
13904
13905        // 1. Cast String to Time32(Second)
13906        let time32_type = DataType::Time32(TimeUnit::Second);
13907        let time32_array = cast_with_options(&array, &time32_type, &cast_options).unwrap();
13908
13909        // 2. Cast Time32(Second) to Int64
13910        let int64_type = DataType::Int64;
13911        assert!(can_cast_types(time32_array.data_type(), &int64_type));
13912
13913        let result = cast_with_options(&time32_array, &int64_type, &cast_options);
13914
13915        assert!(
13916            result.is_ok(),
13917            "Failed to cast Time32(Second) to Int64: {:?}",
13918            result.err()
13919        );
13920
13921        let cast_array = result.unwrap();
13922        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13923
13924        // 03:12:44 = 3*3600 + 12*60 + 44 = 10800 + 720 + 44 = 11564
13925        assert_eq!(cast_array.value(0), 11564);
13926    }
13927    #[test]
13928    fn test_string_dicts_to_binary_view() {
13929        let expected = BinaryViewArray::from_iter(vec![
13930            VIEW_TEST_DATA[1],
13931            VIEW_TEST_DATA[0],
13932            None,
13933            VIEW_TEST_DATA[3],
13934            None,
13935            VIEW_TEST_DATA[1],
13936            VIEW_TEST_DATA[4],
13937        ]);
13938
13939        let values_arrays: [ArrayRef; _] = [
13940            Arc::new(StringArray::from_iter(VIEW_TEST_DATA)),
13941            Arc::new(StringViewArray::from_iter(VIEW_TEST_DATA)),
13942            Arc::new(LargeStringArray::from_iter(VIEW_TEST_DATA)),
13943        ];
13944        for values in values_arrays {
13945            let keys =
13946                Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
13947            let string_dict_array = DictionaryArray::<Int8Type>::try_new(keys, values).unwrap();
13948
13949            let casted = cast(&string_dict_array, &DataType::BinaryView).unwrap();
13950            assert_eq!(casted.as_ref(), &expected);
13951        }
13952    }
13953
13954    #[test]
13955    fn test_binary_dicts_to_string_view() {
13956        let expected = StringViewArray::from_iter(vec![
13957            VIEW_TEST_DATA[1],
13958            VIEW_TEST_DATA[0],
13959            None,
13960            VIEW_TEST_DATA[3],
13961            None,
13962            VIEW_TEST_DATA[1],
13963            VIEW_TEST_DATA[4],
13964        ]);
13965
13966        let values_arrays: [ArrayRef; _] = [
13967            Arc::new(BinaryArray::from_iter(VIEW_TEST_DATA)),
13968            Arc::new(BinaryViewArray::from_iter(VIEW_TEST_DATA)),
13969            Arc::new(LargeBinaryArray::from_iter(VIEW_TEST_DATA)),
13970        ];
13971        for values in values_arrays {
13972            let keys =
13973                Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
13974            let string_dict_array = DictionaryArray::<Int8Type>::try_new(keys, values).unwrap();
13975
13976            let casted = cast(&string_dict_array, &DataType::Utf8View).unwrap();
13977            assert_eq!(casted.as_ref(), &expected);
13978        }
13979    }
13980
13981    #[test]
13982    fn test_cast_between_sliced_run_end_encoded() {
13983        let run_ends = Int16Array::from(vec![2, 5, 8]);
13984        let values = StringArray::from(vec!["a", "b", "c"]);
13985
13986        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
13987        let ree_array = ree_array.slice(1, 2);
13988        let array_ref = Arc::new(ree_array) as ArrayRef;
13989
13990        let target_type = DataType::RunEndEncoded(
13991            Arc::new(Field::new("run_ends", DataType::Int64, false)),
13992            Arc::new(Field::new("values", DataType::Utf8, true)),
13993        );
13994        let cast_options = CastOptions {
13995            safe: false,
13996            format_options: FormatOptions::default(),
13997        };
13998
13999        let result = cast_with_options(&array_ref, &target_type, &cast_options).unwrap();
14000        let run_array = result.as_run::<Int64Type>();
14001        let run_array = run_array.downcast::<StringArray>().unwrap();
14002
14003        let expected = vec!["a", "b"];
14004        let actual = run_array.into_iter().flatten().collect::<Vec<_>>();
14005
14006        assert_eq!(expected, actual);
14007    }
14008}