Skip to main content

arrow_cast/cast/
mod.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Cast kernels to convert [`ArrayRef`]  between supported datatypes.
19//!
20//! See [`cast_with_options`] for more information on specific conversions.
21//!
22//! Example:
23//!
24//! ```
25//! # use arrow_array::*;
26//! # use arrow_cast::cast;
27//! # use arrow_schema::DataType;
28//! # use std::sync::Arc;
29//! # use arrow_array::types::Float64Type;
30//! # use arrow_array::cast::AsArray;
31//! // int32 to float64
32//! let a = Int32Array::from(vec![5, 6, 7]);
33//! let b = cast(&a, &DataType::Float64).unwrap();
34//! let c = b.as_primitive::<Float64Type>();
35//! assert_eq!(5.0, c.value(0));
36//! assert_eq!(6.0, c.value(1));
37//! assert_eq!(7.0, c.value(2));
38//! ```
39
40mod decimal;
41mod dictionary;
42mod list;
43mod map;
44mod run_array;
45mod string;
46mod union;
47
48use crate::cast::decimal::*;
49use crate::cast::dictionary::*;
50use crate::cast::list::*;
51use crate::cast::map::*;
52use crate::cast::run_array::*;
53use crate::cast::string::*;
54pub use crate::cast::union::*;
55
56use arrow_buffer::IntervalMonthDayNano;
57use arrow_data::ByteView;
58use chrono::{NaiveTime, Offset, TimeZone, Utc};
59use std::cmp::Ordering;
60use std::sync::Arc;
61
62use crate::display::{ArrayFormatter, FormatOptions};
63use crate::parse::{
64    Parser, parse_interval_day_time, parse_interval_month_day_nano, parse_interval_year_month,
65    string_to_datetime,
66};
67use arrow_array::{builder::*, cast::*, temporal_conversions::*, timezone::Tz, types::*, *};
68use arrow_buffer::{ArrowNativeType, OffsetBuffer, i256};
69use arrow_data::ArrayData;
70use arrow_data::transform::MutableArrayData;
71use arrow_schema::*;
72use arrow_select::take::take;
73use num_traits::{NumCast, ToPrimitive, cast::AsPrimitive};
74
75pub use decimal::{
76    DecimalCast, parse_string_to_decimal_native, rescale_decimal, single_float_to_decimal,
77};
78pub use string::cast_single_string_to_boolean_default;
79
80/// Lossy conversion from decimal to float.
81///
82/// Conversion is lossy and follows standard floating point semantics. Values
83/// that exceed the representable range become `INFINITY` or `-INFINITY` without
84/// returning an error.
85#[inline(always)]
86pub fn single_decimal_to_float_lossy<D, F>(f: &F, x: D::Native, scale: i32) -> f64
87where
88    D: DecimalType,
89    F: Fn(D::Native) -> f64,
90{
91    f(x) / 10_f64.powi(scale)
92}
93
94/// CastOptions provides a way to override the default cast behaviors
95#[derive(Debug, Clone, PartialEq, Eq, Hash)]
96pub struct CastOptions<'a> {
97    /// how to handle cast failures, either return NULL (safe=true) or return ERR (safe=false)
98    pub safe: bool,
99    /// Formatting options when casting from temporal types to string
100    pub format_options: FormatOptions<'a>,
101}
102
103impl Default for CastOptions<'_> {
104    fn default() -> Self {
105        Self {
106            safe: true,
107            format_options: FormatOptions::default(),
108        }
109    }
110}
111
112/// Return true if a value of type `from_type` can be cast into a value of `to_type`.
113///
114/// See [`cast_with_options`] for more information
115pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
116    use self::DataType::*;
117    use self::IntervalUnit::*;
118    use self::TimeUnit::*;
119    if from_type == to_type {
120        return true;
121    }
122
123    match (from_type, to_type) {
124        (Null, _) => true,
125        // Dictionary/List conditions should be put in front of others
126        (Dictionary(_, from_value_type), Dictionary(_, to_value_type)) => {
127            can_cast_types(from_value_type, to_value_type)
128        }
129        (Dictionary(_, value_type), _) => can_cast_types(value_type, to_type),
130        (Union(fields, _), _) => union::resolve_child_array(fields, to_type).is_some(),
131        (_, Union(_, _)) => false,
132        (RunEndEncoded(_, value_type), _) => can_cast_types(value_type.data_type(), to_type),
133        (_, RunEndEncoded(_, value_type)) => can_cast_types(from_type, value_type.data_type()),
134        (_, Dictionary(_, value_type)) => can_cast_types(from_type, value_type),
135        (
136            List(list_from) | LargeList(list_from) | ListView(list_from) | LargeListView(list_from),
137            List(list_to) | LargeList(list_to) | ListView(list_to) | LargeListView(list_to),
138        ) => can_cast_types(list_from.data_type(), list_to.data_type()),
139        (
140            List(list_from) | LargeList(list_from) | ListView(list_from) | LargeListView(list_from),
141            Utf8 | LargeUtf8 | Utf8View,
142        ) => can_cast_types(list_from.data_type(), to_type),
143        (
144            FixedSizeList(list_from, _),
145            List(list_to) | LargeList(list_to) | ListView(list_to) | LargeListView(list_to),
146        ) => can_cast_types(list_from.data_type(), list_to.data_type()),
147        (
148            List(list_from) | LargeList(list_from) | ListView(list_from) | LargeListView(list_from),
149            FixedSizeList(list_to, _),
150        ) => can_cast_types(list_from.data_type(), list_to.data_type()),
151        (FixedSizeList(inner, size), FixedSizeList(inner_to, size_to)) if size == size_to => {
152            can_cast_types(inner.data_type(), inner_to.data_type())
153        }
154        (_, List(list_to) | LargeList(list_to) | ListView(list_to) | LargeListView(list_to)) => {
155            can_cast_types(from_type, list_to.data_type())
156        }
157        (_, FixedSizeList(list_to, size)) if *size == 1 => {
158            can_cast_types(from_type, list_to.data_type())
159        }
160        (FixedSizeList(list_from, size), _) if *size == 1 => {
161            can_cast_types(list_from.data_type(), to_type)
162        }
163        (Map(from_entries, ordered_from), Map(to_entries, ordered_to))
164            if ordered_from == ordered_to =>
165        {
166            match (
167                key_field(from_entries),
168                key_field(to_entries),
169                value_field(from_entries),
170                value_field(to_entries),
171            ) {
172                (Some(from_key), Some(to_key), Some(from_value), Some(to_value)) => {
173                    can_cast_types(from_key.data_type(), to_key.data_type())
174                        && can_cast_types(from_value.data_type(), to_value.data_type())
175                }
176                _ => false,
177            }
178        }
179        // cast one decimal type to another decimal type
180        (
181            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
182            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
183        ) => true,
184        // unsigned integer to decimal
185        (
186            UInt8 | UInt16 | UInt32 | UInt64,
187            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
188        ) => true,
189        // signed numeric to decimal
190        (
191            Int8 | Int16 | Int32 | Int64 | Float16 | Float32 | Float64,
192            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
193        ) => true,
194        // decimal to unsigned numeric
195        (
196            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
197            UInt8 | UInt16 | UInt32 | UInt64,
198        ) => true,
199        // decimal to signed numeric
200        (
201            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
202            Null | Int8 | Int16 | Int32 | Int64 | Float16 | Float32 | Float64,
203        ) => true,
204        // decimal to string
205        (
206            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
207            Utf8View | Utf8 | LargeUtf8,
208        ) => true,
209        // string to decimal
210        (
211            Utf8View | Utf8 | LargeUtf8,
212            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
213        ) => true,
214        (Struct(from_fields), Struct(to_fields)) => {
215            if from_fields.len() != to_fields.len() {
216                return false;
217            }
218
219            // fast path, all field names are in the same order and same number of fields
220            if from_fields
221                .iter()
222                .zip(to_fields.iter())
223                .all(|(f1, f2)| f1.name() == f2.name())
224            {
225                return from_fields.iter().zip(to_fields.iter()).all(|(f1, f2)| {
226                    // Assume that nullability between two structs are compatible, if not,
227                    // cast kernel will return error.
228                    can_cast_types(f1.data_type(), f2.data_type())
229                });
230            }
231
232            // slow path, we match the fields by name
233            if to_fields.iter().all(|to_field| {
234                from_fields
235                    .iter()
236                    .find(|from_field| from_field.name() == to_field.name())
237                    .is_some_and(|from_field| {
238                        // Assume that nullability between two structs are compatible, if not,
239                        // cast kernel will return error.
240                        can_cast_types(from_field.data_type(), to_field.data_type())
241                    })
242            }) {
243                return true;
244            }
245
246            // if we couldn't match by name, we try to see if they can be matched by position
247            from_fields
248                .iter()
249                .zip(to_fields.iter())
250                .all(|(f1, f2)| can_cast_types(f1.data_type(), f2.data_type()))
251        }
252        (Struct(_), _) => false,
253        (_, Struct(_)) => false,
254        (_, Boolean) => from_type.is_integer() || from_type.is_floating() || from_type.is_string(),
255        (Boolean, _) => to_type.is_integer() || to_type.is_floating() || to_type.is_string(),
256
257        (Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
258            true
259        }
260        (LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
261            true
262        }
263        (FixedSizeBinary(_), Binary | LargeBinary | BinaryView) => true,
264        (
265            Utf8 | LargeUtf8 | Utf8View,
266            Binary
267            | LargeBinary
268            | Utf8
269            | LargeUtf8
270            | Date32
271            | Date64
272            | Time32(Second)
273            | Time32(Millisecond)
274            | Time64(Microsecond)
275            | Time64(Nanosecond)
276            | Timestamp(Second, _)
277            | Timestamp(Millisecond, _)
278            | Timestamp(Microsecond, _)
279            | Timestamp(Nanosecond, _)
280            | Interval(_)
281            | BinaryView,
282        ) => true,
283        (Utf8 | LargeUtf8, Utf8View) => true,
284        (BinaryView, Binary | LargeBinary | Utf8 | LargeUtf8 | Utf8View) => true,
285        (Utf8View | Utf8 | LargeUtf8, _) => to_type.is_numeric(),
286        (_, Utf8 | Utf8View | LargeUtf8) => from_type.is_primitive(),
287
288        (_, Binary | LargeBinary) => from_type.is_integer(),
289
290        // start numeric casts
291        (
292            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
293            | Float64,
294            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
295            | Float64,
296        ) => true,
297        // end numeric casts
298
299        // temporal casts
300        (Int32, Date32 | Date64 | Time32(_)) => true,
301        (Date32, Int32 | Int64) => true,
302        (Time32(_), Int32 | Int64) => true,
303        (Int64, Date64 | Date32 | Time64(_)) => true,
304        (Date64, Int64 | Int32) => true,
305        (Time64(_), Int64) => true,
306        (Date32 | Date64, Date32 | Date64) => true,
307        // time casts
308        (Time32(_), Time32(_)) => true,
309        (Time32(_), Time64(_)) => true,
310        (Time64(_), Time64(_)) => true,
311        (Time64(_), Time32(to_unit)) => {
312            matches!(to_unit, Second | Millisecond)
313        }
314        (Timestamp(_, _), _) if to_type.is_numeric() => true,
315        (_, Timestamp(_, _)) if from_type.is_numeric() => true,
316        (Date64, Timestamp(_, _)) => true,
317        (Date32, Timestamp(_, _)) => true,
318        (
319            Timestamp(_, _),
320            Timestamp(_, _)
321            | Date32
322            | Date64
323            | Time32(Second)
324            | Time32(Millisecond)
325            | Time64(Microsecond)
326            | Time64(Nanosecond),
327        ) => true,
328        (_, Duration(_)) if from_type.is_numeric() => true,
329        (Duration(_), _) if to_type.is_numeric() => true,
330        (Duration(_), Duration(_)) => true,
331        (Interval(from_type), Int64) => {
332            match from_type {
333                YearMonth => true,
334                DayTime => true,
335                MonthDayNano => false, // Native type is i128
336            }
337        }
338        (Int32, Interval(to_type)) => match to_type {
339            YearMonth => true,
340            DayTime => false,
341            MonthDayNano => false,
342        },
343        (Duration(_), Interval(MonthDayNano)) => true,
344        (Interval(MonthDayNano), Duration(_)) => true,
345        (Interval(YearMonth), Interval(MonthDayNano)) => true,
346        (Interval(DayTime), Interval(MonthDayNano)) => true,
347        (_, _) => false,
348    }
349}
350
351/// Cast `array` to the provided data type and return a new Array with type `to_type`, if possible.
352///
353/// See [`cast_with_options`] for more information
354pub fn cast(array: &dyn Array, to_type: &DataType) -> Result<ArrayRef, ArrowError> {
355    cast_with_options(array, to_type, &CastOptions::default())
356}
357
358fn cast_integer_to_decimal<
359    T: ArrowPrimitiveType,
360    D: DecimalType + ArrowPrimitiveType<Native = M>,
361    M,
362>(
363    array: &PrimitiveArray<T>,
364    precision: u8,
365    scale: i8,
366    base: M,
367    cast_options: &CastOptions,
368) -> Result<ArrayRef, ArrowError>
369where
370    <T as ArrowPrimitiveType>::Native: AsPrimitive<M>,
371    M: ArrowNativeTypeOp,
372{
373    let scale_factor = base.pow_checked(scale.unsigned_abs() as u32).map_err(|_| {
374        ArrowError::CastError(format!(
375            "Cannot cast to {:?}({}, {}). The scale causes overflow.",
376            D::PREFIX,
377            precision,
378            scale,
379        ))
380    })?;
381
382    let array = if scale < 0 {
383        match cast_options.safe {
384            true => array.unary_opt::<_, D>(|v| {
385                v.as_()
386                    .div_checked(scale_factor)
387                    .ok()
388                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
389            }),
390            false => array.try_unary::<_, D, _>(|v| {
391                v.as_()
392                    .div_checked(scale_factor)
393                    .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|_| v))
394            })?,
395        }
396    } else {
397        match cast_options.safe {
398            true => array.unary_opt::<_, D>(|v| {
399                v.as_()
400                    .mul_checked(scale_factor)
401                    .ok()
402                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
403            }),
404            false => array.try_unary::<_, D, _>(|v| {
405                v.as_()
406                    .mul_checked(scale_factor)
407                    .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|_| v))
408            })?,
409        }
410    };
411
412    Ok(Arc::new(array.with_precision_and_scale(precision, scale)?))
413}
414
415/// Cast the array from interval year month to month day nano
416fn cast_interval_year_month_to_interval_month_day_nano(
417    array: &dyn Array,
418    _cast_options: &CastOptions,
419) -> Result<ArrayRef, ArrowError> {
420    let array = array.as_primitive::<IntervalYearMonthType>();
421
422    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
423        let months = IntervalYearMonthType::to_months(v);
424        IntervalMonthDayNanoType::make_value(months, 0, 0)
425    })))
426}
427
428/// Cast the array from interval day time to month day nano
429fn cast_interval_day_time_to_interval_month_day_nano(
430    array: &dyn Array,
431    _cast_options: &CastOptions,
432) -> Result<ArrayRef, ArrowError> {
433    let array = array.as_primitive::<IntervalDayTimeType>();
434    let mul = 1_000_000;
435
436    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
437        let (days, ms) = IntervalDayTimeType::to_parts(v);
438        IntervalMonthDayNanoType::make_value(0, days, ms as i64 * mul)
439    })))
440}
441
442/// Cast the array from interval to duration
443fn cast_month_day_nano_to_duration<D: ArrowTemporalType<Native = i64>>(
444    array: &dyn Array,
445    cast_options: &CastOptions,
446) -> Result<ArrayRef, ArrowError> {
447    let array = array.as_primitive::<IntervalMonthDayNanoType>();
448    let scale = match D::DATA_TYPE {
449        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
450        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
451        DataType::Duration(TimeUnit::Microsecond) => 1_000,
452        DataType::Duration(TimeUnit::Nanosecond) => 1,
453        _ => unreachable!(),
454    };
455
456    if cast_options.safe {
457        let iter = array.iter().map(|v| {
458            v.and_then(|v| (v.days == 0 && v.months == 0).then_some(v.nanoseconds / scale))
459        });
460        Ok(Arc::new(unsafe {
461            PrimitiveArray::<D>::from_trusted_len_iter(iter)
462        }))
463    } else {
464        let vec = array
465            .iter()
466            .map(|v| {
467                v.map(|v| match v.days == 0 && v.months == 0 {
468                    true => Ok((v.nanoseconds) / scale),
469                    _ => Err(ArrowError::ComputeError(
470                        "Cannot convert interval containing non-zero months or days to duration"
471                            .to_string(),
472                    )),
473                })
474                .transpose()
475            })
476            .collect::<Result<Vec<_>, _>>()?;
477        Ok(Arc::new(unsafe {
478            PrimitiveArray::<D>::from_trusted_len_iter(vec.iter())
479        }))
480    }
481}
482
483/// Cast the array from duration and interval
484fn cast_duration_to_interval<D: ArrowTemporalType<Native = i64>>(
485    array: &dyn Array,
486    cast_options: &CastOptions,
487) -> Result<ArrayRef, ArrowError> {
488    let array = array
489        .as_any()
490        .downcast_ref::<PrimitiveArray<D>>()
491        .ok_or_else(|| {
492            ArrowError::ComputeError(
493                "Internal Error: Cannot cast duration to DurationArray of expected type"
494                    .to_string(),
495            )
496        })?;
497
498    let scale = match array.data_type() {
499        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
500        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
501        DataType::Duration(TimeUnit::Microsecond) => 1_000,
502        DataType::Duration(TimeUnit::Nanosecond) => 1,
503        _ => unreachable!(),
504    };
505
506    if cast_options.safe {
507        let iter = array.iter().map(|v| {
508            v.and_then(|v| {
509                v.checked_mul(scale)
510                    .map(|v| IntervalMonthDayNano::new(0, 0, v))
511            })
512        });
513        Ok(Arc::new(unsafe {
514            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(iter)
515        }))
516    } else {
517        let vec = array
518            .iter()
519            .map(|v| {
520                v.map(|v| {
521                    if let Ok(v) = v.mul_checked(scale) {
522                        Ok(IntervalMonthDayNano::new(0, 0, v))
523                    } else {
524                        Err(ArrowError::ComputeError(format!(
525                            "Cannot cast to {:?}. Overflowing on {:?}",
526                            IntervalMonthDayNanoType::DATA_TYPE,
527                            v
528                        )))
529                    }
530                })
531                .transpose()
532            })
533            .collect::<Result<Vec<_>, _>>()?;
534        Ok(Arc::new(unsafe {
535            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(vec.iter())
536        }))
537    }
538}
539
540/// Cast the primitive array using [`PrimitiveArray::reinterpret_cast`]
541fn cast_reinterpret_arrays<I: ArrowPrimitiveType, O: ArrowPrimitiveType<Native = I::Native>>(
542    array: &dyn Array,
543) -> Result<ArrayRef, ArrowError> {
544    Ok(Arc::new(array.as_primitive::<I>().reinterpret_cast::<O>()))
545}
546
547fn make_timestamp_array(
548    array: &PrimitiveArray<Int64Type>,
549    unit: TimeUnit,
550    tz: Option<Arc<str>>,
551) -> ArrayRef {
552    match unit {
553        TimeUnit::Second => Arc::new(
554            array
555                .reinterpret_cast::<TimestampSecondType>()
556                .with_timezone_opt(tz),
557        ),
558        TimeUnit::Millisecond => Arc::new(
559            array
560                .reinterpret_cast::<TimestampMillisecondType>()
561                .with_timezone_opt(tz),
562        ),
563        TimeUnit::Microsecond => Arc::new(
564            array
565                .reinterpret_cast::<TimestampMicrosecondType>()
566                .with_timezone_opt(tz),
567        ),
568        TimeUnit::Nanosecond => Arc::new(
569            array
570                .reinterpret_cast::<TimestampNanosecondType>()
571                .with_timezone_opt(tz),
572        ),
573    }
574}
575
576fn make_duration_array(array: &PrimitiveArray<Int64Type>, unit: TimeUnit) -> ArrayRef {
577    match unit {
578        TimeUnit::Second => Arc::new(array.reinterpret_cast::<DurationSecondType>()),
579        TimeUnit::Millisecond => Arc::new(array.reinterpret_cast::<DurationMillisecondType>()),
580        TimeUnit::Microsecond => Arc::new(array.reinterpret_cast::<DurationMicrosecondType>()),
581        TimeUnit::Nanosecond => Arc::new(array.reinterpret_cast::<DurationNanosecondType>()),
582    }
583}
584
585fn as_time_res_with_timezone<T: ArrowPrimitiveType>(
586    v: i64,
587    tz: Option<Tz>,
588) -> Result<NaiveTime, ArrowError> {
589    let time = match tz {
590        Some(tz) => as_datetime_with_timezone::<T>(v, tz).map(|d| d.time()),
591        None => as_datetime::<T>(v).map(|d| d.time()),
592    };
593
594    time.ok_or_else(|| {
595        ArrowError::CastError(format!(
596            "Failed to create naive time with {} {}",
597            std::any::type_name::<T>(),
598            v
599        ))
600    })
601}
602
603fn timestamp_to_date32<T: ArrowTimestampType>(
604    array: &PrimitiveArray<T>,
605) -> Result<ArrayRef, ArrowError> {
606    let err = |x: i64| {
607        ArrowError::CastError(format!(
608            "Cannot convert {} {x} to datetime",
609            std::any::type_name::<T>()
610        ))
611    };
612
613    let array: Date32Array = match array.timezone() {
614        Some(tz) => {
615            let tz: Tz = tz.parse()?;
616            array.try_unary(|x| {
617                as_datetime_with_timezone::<T>(x, tz)
618                    .ok_or_else(|| err(x))
619                    .map(|d| Date32Type::from_naive_date(d.date_naive()))
620            })?
621        }
622        None => array.try_unary(|x| {
623            as_datetime::<T>(x)
624                .ok_or_else(|| err(x))
625                .map(|d| Date32Type::from_naive_date(d.date()))
626        })?,
627    };
628    Ok(Arc::new(array))
629}
630
631/// Try to cast `array` to `to_type` if possible.
632///
633/// Returns a new Array with type `to_type` if possible.
634///
635/// Accepts [`CastOptions`] to specify cast behavior. See also [`cast()`].
636///
637/// # Behavior
638/// * `Boolean` to `Utf8`: `true` => '1', `false` => `0`
639/// * `Utf8` to `Boolean`: `true`, `yes`, `on`, `1` => `true`, `false`, `no`, `off`, `0` => `false`,
640///   short variants are accepted, other strings return null or error
641/// * `Utf8` to Numeric: strings that can't be parsed to numbers return null, float strings
642///   in integer casts return null
643/// * Numeric to `Boolean`: 0 returns `false`, any other value returns `true`
644/// * `List` to `List`: the underlying data type is cast
645/// * `List` to `FixedSizeList`: the underlying data type is cast. If safe is true and a list element
646///   has the wrong length it will be replaced with NULL, otherwise an error will be returned
647/// * Primitive to `List`: a list array with 1 value per slot is created
648/// * `Date32` and `Date64`: precision lost when going to higher interval
649/// * `Time32 and `Time64`: precision lost when going to higher interval
650/// * `Timestamp` and `Date{32|64}`: precision lost when going to higher interval
651/// * Temporal to/from backing Primitive: zero-copy with data type change
652/// * `Float16/Float32/Float64` to `Decimal(precision, scale)` rounds to the `scale` decimals
653///   (i.e. casting `6.4999` to `Decimal(10, 1)` becomes `6.5`).
654/// * `Decimal` to `Float16/Float32/Float64` is lossy and values outside the representable
655///   range become `INFINITY` or `-INFINITY` without error.
656///
657/// Unsupported Casts (check with `can_cast_types` before calling):
658/// * To or from `StructArray`
659/// * `List` to `Primitive`
660/// * `Interval` and `Duration`
661///
662/// # Durations and Intervals
663///
664/// Casting integer types directly to interval types such as
665/// [`IntervalMonthDayNano`] is not supported because the meaning of the integer
666/// is ambiguous. For example, the integer  could represent either nanoseconds
667/// or months.
668///
669/// To cast an integer type to an interval type, first convert to a Duration
670/// type, and then cast that to the desired interval type.
671///
672/// For example, to convert an `Int64` representing nanoseconds to an
673/// `IntervalMonthDayNano` you would first convert the `Int64` to a
674/// `DurationNanoseconds`, and then cast that to `IntervalMonthDayNano`.
675///
676/// # Timestamps and Timezones
677///
678/// Timestamps are stored with an optional timezone in Arrow.
679///
680/// ## Casting timestamps to a timestamp without timezone / UTC
681/// ```
682/// # use arrow_array::Int64Array;
683/// # use arrow_array::types::TimestampSecondType;
684/// # use arrow_cast::{cast, display};
685/// # use arrow_array::cast::AsArray;
686/// # use arrow_schema::{DataType, TimeUnit};
687/// // can use "UTC" if chrono-tz feature is enabled, here use offset based timezone
688/// let data_type = DataType::Timestamp(TimeUnit::Second, None);
689/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
690/// let b = cast(&a, &data_type).unwrap();
691/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
692/// assert_eq!(2_000_000_000, b.value(1)); // values are the same as the type has no timezone
693/// // use display to show them (note has no trailing Z)
694/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
695/// ```
696///
697/// ## Casting timestamps to a timestamp with timezone
698///
699/// Similarly to the previous example, if you cast numeric values to a timestamp
700/// with timezone, the cast kernel will not change the underlying values
701/// but display and other functions will interpret them as being in the provided timezone.
702///
703/// ```
704/// # use arrow_array::Int64Array;
705/// # use arrow_array::types::TimestampSecondType;
706/// # use arrow_cast::{cast, display};
707/// # use arrow_array::cast::AsArray;
708/// # use arrow_schema::{DataType, TimeUnit};
709/// // can use "Americas/New_York" if chrono-tz feature is enabled, here use offset based timezone
710/// let data_type = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
711/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
712/// let b = cast(&a, &data_type).unwrap();
713/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
714/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
715/// // displayed in the target timezone (note the offset -05:00)
716/// assert_eq!("2033-05-17T22:33:20-05:00", display::array_value_to_string(&b, 1).unwrap());
717/// ```
718/// # Casting timestamps without timezone to timestamps with timezone
719///
720/// When casting from a timestamp without timezone to a timestamp with
721/// timezone, the cast kernel interprets the timestamp values as being in
722/// the destination timezone and then adjusts the underlying value to UTC as required
723///
724/// However, note that when casting from a timestamp with timezone BACK to a
725/// timestamp without timezone the cast kernel does not adjust the values.
726///
727/// Thus round trip casting a timestamp without timezone to a timestamp with
728/// timezone and back to a timestamp without timezone results in different
729/// values than the starting values.
730///
731/// ```
732/// # use arrow_array::Int64Array;
733/// # use arrow_array::types::{TimestampSecondType};
734/// # use arrow_cast::{cast, display};
735/// # use arrow_array::cast::AsArray;
736/// # use arrow_schema::{DataType, TimeUnit};
737/// let data_type  = DataType::Timestamp(TimeUnit::Second, None);
738/// let data_type_tz = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
739/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
740/// let b = cast(&a, &data_type).unwrap(); // cast to timestamp without timezone
741/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
742/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
743/// // displayed without a timezone (note lack of offset or Z)
744/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
745///
746/// // Convert timestamps without a timezone to timestamps with a timezone
747/// let c = cast(&b, &data_type_tz).unwrap();
748/// let c = c.as_primitive::<TimestampSecondType>(); // downcast to result type
749/// assert_eq!(2_000_018_000, c.value(1)); // value has been adjusted by offset
750/// // displayed with the target timezone offset (-05:00)
751/// assert_eq!("2033-05-18T03:33:20-05:00", display::array_value_to_string(&c, 1).unwrap());
752///
753/// // Convert from timestamp with timezone back to timestamp without timezone
754/// let d = cast(&c, &data_type).unwrap();
755/// let d = d.as_primitive::<TimestampSecondType>(); // downcast to result type
756/// assert_eq!(2_000_018_000, d.value(1)); // value has not been adjusted
757/// // NOTE: the timestamp is adjusted (08:33:20 instead of 03:33:20 as in previous example)
758/// assert_eq!("2033-05-18T08:33:20", display::array_value_to_string(&d, 1).unwrap());
759/// ```
760pub fn cast_with_options(
761    array: &dyn Array,
762    to_type: &DataType,
763    cast_options: &CastOptions,
764) -> Result<ArrayRef, ArrowError> {
765    use DataType::*;
766    let from_type = array.data_type();
767    // clone array if types are the same
768    if from_type == to_type {
769        return Ok(make_array(array.to_data()));
770    }
771    match (from_type, to_type) {
772        (Null, _) => Ok(new_null_array(to_type, array.len())),
773        (RunEndEncoded(index_type, _), _) => match index_type.data_type() {
774            Int16 => run_end_encoded_cast::<Int16Type>(array, to_type, cast_options),
775            Int32 => run_end_encoded_cast::<Int32Type>(array, to_type, cast_options),
776            Int64 => run_end_encoded_cast::<Int64Type>(array, to_type, cast_options),
777            _ => Err(ArrowError::CastError(format!(
778                "Casting from run end encoded type {from_type:?} to {to_type:?} not supported",
779            ))),
780        },
781        (_, RunEndEncoded(index_type, value_type)) => {
782            let array_ref = make_array(array.to_data());
783            match index_type.data_type() {
784                Int16 => cast_to_run_end_encoded::<Int16Type>(
785                    &array_ref,
786                    value_type.data_type(),
787                    cast_options,
788                ),
789                Int32 => cast_to_run_end_encoded::<Int32Type>(
790                    &array_ref,
791                    value_type.data_type(),
792                    cast_options,
793                ),
794                Int64 => cast_to_run_end_encoded::<Int64Type>(
795                    &array_ref,
796                    value_type.data_type(),
797                    cast_options,
798                ),
799                _ => Err(ArrowError::CastError(format!(
800                    "Casting from type {from_type:?} to run end encoded type {to_type:?} not supported",
801                ))),
802            }
803        }
804        (Union(_, _), _) => union_extract_by_type(
805            array.as_any().downcast_ref::<UnionArray>().unwrap(),
806            to_type,
807            cast_options,
808        ),
809        (_, Union(_, _)) => Err(ArrowError::CastError(format!(
810            "Casting from {from_type} to {to_type} not supported"
811        ))),
812        (Dictionary(index_type, _), _) => match **index_type {
813            Int8 => dictionary_cast::<Int8Type>(array, to_type, cast_options),
814            Int16 => dictionary_cast::<Int16Type>(array, to_type, cast_options),
815            Int32 => dictionary_cast::<Int32Type>(array, to_type, cast_options),
816            Int64 => dictionary_cast::<Int64Type>(array, to_type, cast_options),
817            UInt8 => dictionary_cast::<UInt8Type>(array, to_type, cast_options),
818            UInt16 => dictionary_cast::<UInt16Type>(array, to_type, cast_options),
819            UInt32 => dictionary_cast::<UInt32Type>(array, to_type, cast_options),
820            UInt64 => dictionary_cast::<UInt64Type>(array, to_type, cast_options),
821            _ => Err(ArrowError::CastError(format!(
822                "Casting from dictionary type {from_type} to {to_type} not supported",
823            ))),
824        },
825        (_, Dictionary(index_type, value_type)) => match **index_type {
826            Int8 => cast_to_dictionary::<Int8Type>(array, value_type, cast_options),
827            Int16 => cast_to_dictionary::<Int16Type>(array, value_type, cast_options),
828            Int32 => cast_to_dictionary::<Int32Type>(array, value_type, cast_options),
829            Int64 => cast_to_dictionary::<Int64Type>(array, value_type, cast_options),
830            UInt8 => cast_to_dictionary::<UInt8Type>(array, value_type, cast_options),
831            UInt16 => cast_to_dictionary::<UInt16Type>(array, value_type, cast_options),
832            UInt32 => cast_to_dictionary::<UInt32Type>(array, value_type, cast_options),
833            UInt64 => cast_to_dictionary::<UInt64Type>(array, value_type, cast_options),
834            _ => Err(ArrowError::CastError(format!(
835                "Casting from type {from_type} to dictionary type {to_type} not supported",
836            ))),
837        },
838        // Casting between lists of same types (cast inner values)
839        (List(_), List(to)) => cast_list_values::<i32>(array, to, cast_options),
840        (LargeList(_), LargeList(to)) => cast_list_values::<i64>(array, to, cast_options),
841        (FixedSizeList(_, size_from), FixedSizeList(list_to, size_to)) => {
842            if size_from != size_to {
843                return Err(ArrowError::CastError(
844                    "cannot cast fixed-size-list to fixed-size-list with different size".into(),
845                ));
846            }
847            let array = array.as_fixed_size_list();
848            let values = cast_with_options(array.values(), list_to.data_type(), cast_options)?;
849            Ok(Arc::new(FixedSizeListArray::try_new(
850                list_to.clone(),
851                *size_from,
852                values,
853                array.nulls().cloned(),
854            )?))
855        }
856        (ListView(_), ListView(to)) => cast_list_view_values::<i32>(array, to, cast_options),
857        (LargeListView(_), LargeListView(to)) => {
858            cast_list_view_values::<i64>(array, to, cast_options)
859        }
860        // Casting between different types of lists
861        // List
862        (List(_), LargeList(list_to)) => cast_list::<i32, i64>(array, list_to, cast_options),
863        (List(_), FixedSizeList(field, size)) => {
864            cast_list_to_fixed_size_list::<i32>(array, field, *size, cast_options)
865        }
866        (List(_), ListView(list_to)) => {
867            cast_list_to_list_view::<i32, i32>(array, list_to, cast_options)
868        }
869        (List(_), LargeListView(list_to)) => {
870            cast_list_to_list_view::<i32, i64>(array, list_to, cast_options)
871        }
872        // LargeList
873        (LargeList(_), List(list_to)) => cast_list::<i64, i32>(array, list_to, cast_options),
874        (LargeList(_), FixedSizeList(field, size)) => {
875            cast_list_to_fixed_size_list::<i64>(array, field, *size, cast_options)
876        }
877        (LargeList(_), ListView(list_to)) => {
878            cast_list_to_list_view::<i64, i32>(array, list_to, cast_options)
879        }
880        (LargeList(_), LargeListView(list_to)) => {
881            cast_list_to_list_view::<i64, i64>(array, list_to, cast_options)
882        }
883        // ListView
884        (ListView(_), List(list_to)) => {
885            cast_list_view_to_list::<i32, Int32Type>(array, list_to, cast_options)
886        }
887        (ListView(_), LargeList(list_to)) => {
888            cast_list_view_to_list::<i32, Int64Type>(array, list_to, cast_options)
889        }
890        (ListView(_), LargeListView(list_to)) => {
891            cast_list_view::<i32, i64>(array, list_to, cast_options)
892        }
893        (ListView(_), FixedSizeList(field, size)) => {
894            cast_list_view_to_fixed_size_list::<i32>(array, field, *size, cast_options)
895        }
896        // LargeListView
897        (LargeListView(_), LargeList(list_to)) => {
898            cast_list_view_to_list::<i64, Int64Type>(array, list_to, cast_options)
899        }
900        (LargeListView(_), List(list_to)) => {
901            cast_list_view_to_list::<i64, Int32Type>(array, list_to, cast_options)
902        }
903        (LargeListView(_), ListView(list_to)) => {
904            cast_list_view::<i64, i32>(array, list_to, cast_options)
905        }
906        (LargeListView(_), FixedSizeList(field, size)) => {
907            cast_list_view_to_fixed_size_list::<i64>(array, field, *size, cast_options)
908        }
909        // FixedSizeList
910        (FixedSizeList(_, _), List(list_to)) => {
911            cast_fixed_size_list_to_list::<i32>(array, list_to, cast_options)
912        }
913        (FixedSizeList(_, _), LargeList(list_to)) => {
914            cast_fixed_size_list_to_list::<i64>(array, list_to, cast_options)
915        }
916        (FixedSizeList(_, _), ListView(list_to)) => {
917            cast_fixed_size_list_to_list_view::<i32>(array, list_to, cast_options)
918        }
919        (FixedSizeList(_, _), LargeListView(list_to)) => {
920            cast_fixed_size_list_to_list_view::<i64>(array, list_to, cast_options)
921        }
922        // List to/from other types
923        (FixedSizeList(_, size), _) if *size == 1 => {
924            cast_single_element_fixed_size_list_to_values(array, to_type, cast_options)
925        }
926        // NOTE: we could support FSL to string here too but might be confusing
927        //       since behaviour for size 1 would be different (see arm above)
928        (List(_) | LargeList(_) | ListView(_) | LargeListView(_), _) => match to_type {
929            Utf8 => value_to_string::<i32>(array, cast_options),
930            LargeUtf8 => value_to_string::<i64>(array, cast_options),
931            Utf8View => value_to_string_view(array, cast_options),
932            dt => Err(ArrowError::CastError(format!(
933                "Cannot cast LIST to non-list data type {dt}"
934            ))),
935        },
936        (_, List(to)) => cast_values_to_list::<i32>(array, to, cast_options),
937        (_, LargeList(to)) => cast_values_to_list::<i64>(array, to, cast_options),
938        (_, ListView(to)) => cast_values_to_list_view::<i32>(array, to, cast_options),
939        (_, LargeListView(to)) => cast_values_to_list_view::<i64>(array, to, cast_options),
940        (_, FixedSizeList(to, size)) if *size == 1 => {
941            let values = cast_with_options(array, to.data_type(), cast_options)?;
942            let list = FixedSizeListArray::try_new(to.clone(), 1, values, None)?;
943            Ok(Arc::new(list))
944        }
945        // Map
946        (Map(_, ordered1), Map(_, ordered2)) if ordered1 == ordered2 => {
947            cast_map_values(array.as_map(), to_type, cast_options, ordered1.to_owned())
948        }
949        // Decimal to decimal, same width
950        (Decimal32(p1, s1), Decimal32(p2, s2)) => {
951            cast_decimal_to_decimal_same_type::<Decimal32Type>(
952                array.as_primitive(),
953                *p1,
954                *s1,
955                *p2,
956                *s2,
957                cast_options,
958            )
959        }
960        (Decimal64(p1, s1), Decimal64(p2, s2)) => {
961            cast_decimal_to_decimal_same_type::<Decimal64Type>(
962                array.as_primitive(),
963                *p1,
964                *s1,
965                *p2,
966                *s2,
967                cast_options,
968            )
969        }
970        (Decimal128(p1, s1), Decimal128(p2, s2)) => {
971            cast_decimal_to_decimal_same_type::<Decimal128Type>(
972                array.as_primitive(),
973                *p1,
974                *s1,
975                *p2,
976                *s2,
977                cast_options,
978            )
979        }
980        (Decimal256(p1, s1), Decimal256(p2, s2)) => {
981            cast_decimal_to_decimal_same_type::<Decimal256Type>(
982                array.as_primitive(),
983                *p1,
984                *s1,
985                *p2,
986                *s2,
987                cast_options,
988            )
989        }
990        // Decimal to decimal, different width
991        (Decimal32(p1, s1), Decimal64(p2, s2)) => {
992            cast_decimal_to_decimal::<Decimal32Type, Decimal64Type>(
993                array.as_primitive(),
994                *p1,
995                *s1,
996                *p2,
997                *s2,
998                cast_options,
999            )
1000        }
1001        (Decimal32(p1, s1), Decimal128(p2, s2)) => {
1002            cast_decimal_to_decimal::<Decimal32Type, Decimal128Type>(
1003                array.as_primitive(),
1004                *p1,
1005                *s1,
1006                *p2,
1007                *s2,
1008                cast_options,
1009            )
1010        }
1011        (Decimal32(p1, s1), Decimal256(p2, s2)) => {
1012            cast_decimal_to_decimal::<Decimal32Type, Decimal256Type>(
1013                array.as_primitive(),
1014                *p1,
1015                *s1,
1016                *p2,
1017                *s2,
1018                cast_options,
1019            )
1020        }
1021        (Decimal64(p1, s1), Decimal32(p2, s2)) => {
1022            cast_decimal_to_decimal::<Decimal64Type, Decimal32Type>(
1023                array.as_primitive(),
1024                *p1,
1025                *s1,
1026                *p2,
1027                *s2,
1028                cast_options,
1029            )
1030        }
1031        (Decimal64(p1, s1), Decimal128(p2, s2)) => {
1032            cast_decimal_to_decimal::<Decimal64Type, Decimal128Type>(
1033                array.as_primitive(),
1034                *p1,
1035                *s1,
1036                *p2,
1037                *s2,
1038                cast_options,
1039            )
1040        }
1041        (Decimal64(p1, s1), Decimal256(p2, s2)) => {
1042            cast_decimal_to_decimal::<Decimal64Type, Decimal256Type>(
1043                array.as_primitive(),
1044                *p1,
1045                *s1,
1046                *p2,
1047                *s2,
1048                cast_options,
1049            )
1050        }
1051        (Decimal128(p1, s1), Decimal32(p2, s2)) => {
1052            cast_decimal_to_decimal::<Decimal128Type, Decimal32Type>(
1053                array.as_primitive(),
1054                *p1,
1055                *s1,
1056                *p2,
1057                *s2,
1058                cast_options,
1059            )
1060        }
1061        (Decimal128(p1, s1), Decimal64(p2, s2)) => {
1062            cast_decimal_to_decimal::<Decimal128Type, Decimal64Type>(
1063                array.as_primitive(),
1064                *p1,
1065                *s1,
1066                *p2,
1067                *s2,
1068                cast_options,
1069            )
1070        }
1071        (Decimal128(p1, s1), Decimal256(p2, s2)) => {
1072            cast_decimal_to_decimal::<Decimal128Type, Decimal256Type>(
1073                array.as_primitive(),
1074                *p1,
1075                *s1,
1076                *p2,
1077                *s2,
1078                cast_options,
1079            )
1080        }
1081        (Decimal256(p1, s1), Decimal32(p2, s2)) => {
1082            cast_decimal_to_decimal::<Decimal256Type, Decimal32Type>(
1083                array.as_primitive(),
1084                *p1,
1085                *s1,
1086                *p2,
1087                *s2,
1088                cast_options,
1089            )
1090        }
1091        (Decimal256(p1, s1), Decimal64(p2, s2)) => {
1092            cast_decimal_to_decimal::<Decimal256Type, Decimal64Type>(
1093                array.as_primitive(),
1094                *p1,
1095                *s1,
1096                *p2,
1097                *s2,
1098                cast_options,
1099            )
1100        }
1101        (Decimal256(p1, s1), Decimal128(p2, s2)) => {
1102            cast_decimal_to_decimal::<Decimal256Type, Decimal128Type>(
1103                array.as_primitive(),
1104                *p1,
1105                *s1,
1106                *p2,
1107                *s2,
1108                cast_options,
1109            )
1110        }
1111        // Decimal to non-decimal
1112        (Decimal32(_, scale), _) if !to_type.is_temporal() => {
1113            cast_from_decimal::<Decimal32Type, _>(
1114                array,
1115                10_i32,
1116                scale,
1117                from_type,
1118                to_type,
1119                |x: i32| x as f64,
1120                cast_options,
1121            )
1122        }
1123        (Decimal64(_, scale), _) if !to_type.is_temporal() => {
1124            cast_from_decimal::<Decimal64Type, _>(
1125                array,
1126                10_i64,
1127                scale,
1128                from_type,
1129                to_type,
1130                |x: i64| x as f64,
1131                cast_options,
1132            )
1133        }
1134        (Decimal128(_, scale), _) if !to_type.is_temporal() => {
1135            cast_from_decimal::<Decimal128Type, _>(
1136                array,
1137                10_i128,
1138                scale,
1139                from_type,
1140                to_type,
1141                |x: i128| x as f64,
1142                cast_options,
1143            )
1144        }
1145        (Decimal256(_, scale), _) if !to_type.is_temporal() => {
1146            cast_from_decimal::<Decimal256Type, _>(
1147                array,
1148                i256::from_i128(10_i128),
1149                scale,
1150                from_type,
1151                to_type,
1152                |x: i256| x.to_f64().expect("All i256 values fit in f64"),
1153                cast_options,
1154            )
1155        }
1156        // Non-decimal to decimal
1157        (_, Decimal32(precision, scale)) if !from_type.is_temporal() => {
1158            cast_to_decimal::<Decimal32Type, _>(
1159                array,
1160                10_i32,
1161                precision,
1162                scale,
1163                from_type,
1164                to_type,
1165                cast_options,
1166            )
1167        }
1168        (_, Decimal64(precision, scale)) if !from_type.is_temporal() => {
1169            cast_to_decimal::<Decimal64Type, _>(
1170                array,
1171                10_i64,
1172                precision,
1173                scale,
1174                from_type,
1175                to_type,
1176                cast_options,
1177            )
1178        }
1179        (_, Decimal128(precision, scale)) if !from_type.is_temporal() => {
1180            cast_to_decimal::<Decimal128Type, _>(
1181                array,
1182                10_i128,
1183                precision,
1184                scale,
1185                from_type,
1186                to_type,
1187                cast_options,
1188            )
1189        }
1190        (_, Decimal256(precision, scale)) if !from_type.is_temporal() => {
1191            cast_to_decimal::<Decimal256Type, _>(
1192                array,
1193                i256::from_i128(10_i128),
1194                precision,
1195                scale,
1196                from_type,
1197                to_type,
1198                cast_options,
1199            )
1200        }
1201        (Struct(from_fields), Struct(to_fields)) => cast_struct_to_struct(
1202            array.as_struct(),
1203            from_fields.clone(),
1204            to_fields.clone(),
1205            cast_options,
1206        ),
1207        (Struct(_), _) => Err(ArrowError::CastError(format!(
1208            "Casting from {from_type} to {to_type} not supported"
1209        ))),
1210        (_, Struct(_)) => Err(ArrowError::CastError(format!(
1211            "Casting from {from_type} to {to_type} not supported"
1212        ))),
1213        (_, Boolean) => match from_type {
1214            UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
1215            UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
1216            UInt32 => cast_numeric_to_bool::<UInt32Type>(array),
1217            UInt64 => cast_numeric_to_bool::<UInt64Type>(array),
1218            Int8 => cast_numeric_to_bool::<Int8Type>(array),
1219            Int16 => cast_numeric_to_bool::<Int16Type>(array),
1220            Int32 => cast_numeric_to_bool::<Int32Type>(array),
1221            Int64 => cast_numeric_to_bool::<Int64Type>(array),
1222            Float16 => cast_numeric_to_bool::<Float16Type>(array),
1223            Float32 => cast_numeric_to_bool::<Float32Type>(array),
1224            Float64 => cast_numeric_to_bool::<Float64Type>(array),
1225            Utf8View => cast_utf8view_to_boolean(array, cast_options),
1226            Utf8 => cast_utf8_to_boolean::<i32>(array, cast_options),
1227            LargeUtf8 => cast_utf8_to_boolean::<i64>(array, cast_options),
1228            _ => Err(ArrowError::CastError(format!(
1229                "Casting from {from_type} to {to_type} not supported",
1230            ))),
1231        },
1232        (Boolean, _) => match to_type {
1233            UInt8 => cast_bool_to_numeric::<UInt8Type>(array, cast_options),
1234            UInt16 => cast_bool_to_numeric::<UInt16Type>(array, cast_options),
1235            UInt32 => cast_bool_to_numeric::<UInt32Type>(array, cast_options),
1236            UInt64 => cast_bool_to_numeric::<UInt64Type>(array, cast_options),
1237            Int8 => cast_bool_to_numeric::<Int8Type>(array, cast_options),
1238            Int16 => cast_bool_to_numeric::<Int16Type>(array, cast_options),
1239            Int32 => cast_bool_to_numeric::<Int32Type>(array, cast_options),
1240            Int64 => cast_bool_to_numeric::<Int64Type>(array, cast_options),
1241            Float16 => cast_bool_to_numeric::<Float16Type>(array, cast_options),
1242            Float32 => cast_bool_to_numeric::<Float32Type>(array, cast_options),
1243            Float64 => cast_bool_to_numeric::<Float64Type>(array, cast_options),
1244            Utf8View => value_to_string_view(array, cast_options),
1245            Utf8 => value_to_string::<i32>(array, cast_options),
1246            LargeUtf8 => value_to_string::<i64>(array, cast_options),
1247            _ => Err(ArrowError::CastError(format!(
1248                "Casting from {from_type} to {to_type} not supported",
1249            ))),
1250        },
1251        (Utf8, _) => match to_type {
1252            UInt8 => parse_string::<UInt8Type, i32>(array, cast_options),
1253            UInt16 => parse_string::<UInt16Type, i32>(array, cast_options),
1254            UInt32 => parse_string::<UInt32Type, i32>(array, cast_options),
1255            UInt64 => parse_string::<UInt64Type, i32>(array, cast_options),
1256            Int8 => parse_string::<Int8Type, i32>(array, cast_options),
1257            Int16 => parse_string::<Int16Type, i32>(array, cast_options),
1258            Int32 => parse_string::<Int32Type, i32>(array, cast_options),
1259            Int64 => parse_string::<Int64Type, i32>(array, cast_options),
1260            Float16 => parse_string::<Float16Type, i32>(array, cast_options),
1261            Float32 => parse_string::<Float32Type, i32>(array, cast_options),
1262            Float64 => parse_string::<Float64Type, i32>(array, cast_options),
1263            Date32 => parse_string::<Date32Type, i32>(array, cast_options),
1264            Date64 => parse_string::<Date64Type, i32>(array, cast_options),
1265            Binary => Ok(Arc::new(BinaryArray::from(
1266                array.as_string::<i32>().clone(),
1267            ))),
1268            LargeBinary => {
1269                let binary = BinaryArray::from(array.as_string::<i32>().clone());
1270                cast_byte_container::<BinaryType, LargeBinaryType>(&binary)
1271            }
1272            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i32>()))),
1273            BinaryView => Ok(Arc::new(
1274                StringViewArray::from(array.as_string::<i32>()).to_binary_view(),
1275            )),
1276            LargeUtf8 => cast_byte_container::<Utf8Type, LargeUtf8Type>(array),
1277            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i32>(array, cast_options),
1278            Time32(TimeUnit::Millisecond) => {
1279                parse_string::<Time32MillisecondType, i32>(array, cast_options)
1280            }
1281            Time64(TimeUnit::Microsecond) => {
1282                parse_string::<Time64MicrosecondType, i32>(array, cast_options)
1283            }
1284            Time64(TimeUnit::Nanosecond) => {
1285                parse_string::<Time64NanosecondType, i32>(array, cast_options)
1286            }
1287            Timestamp(TimeUnit::Second, to_tz) => {
1288                cast_string_to_timestamp::<i32, TimestampSecondType>(array, to_tz, cast_options)
1289            }
1290            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1291                i32,
1292                TimestampMillisecondType,
1293            >(array, to_tz, cast_options),
1294            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1295                i32,
1296                TimestampMicrosecondType,
1297            >(array, to_tz, cast_options),
1298            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1299                cast_string_to_timestamp::<i32, TimestampNanosecondType>(array, to_tz, cast_options)
1300            }
1301            Interval(IntervalUnit::YearMonth) => {
1302                cast_string_to_year_month_interval::<i32>(array, cast_options)
1303            }
1304            Interval(IntervalUnit::DayTime) => {
1305                cast_string_to_day_time_interval::<i32>(array, cast_options)
1306            }
1307            Interval(IntervalUnit::MonthDayNano) => {
1308                cast_string_to_month_day_nano_interval::<i32>(array, cast_options)
1309            }
1310            _ => Err(ArrowError::CastError(format!(
1311                "Casting from {from_type} to {to_type} not supported",
1312            ))),
1313        },
1314        (Utf8View, _) => match to_type {
1315            UInt8 => parse_string_view::<UInt8Type>(array, cast_options),
1316            UInt16 => parse_string_view::<UInt16Type>(array, cast_options),
1317            UInt32 => parse_string_view::<UInt32Type>(array, cast_options),
1318            UInt64 => parse_string_view::<UInt64Type>(array, cast_options),
1319            Int8 => parse_string_view::<Int8Type>(array, cast_options),
1320            Int16 => parse_string_view::<Int16Type>(array, cast_options),
1321            Int32 => parse_string_view::<Int32Type>(array, cast_options),
1322            Int64 => parse_string_view::<Int64Type>(array, cast_options),
1323            Float16 => parse_string_view::<Float16Type>(array, cast_options),
1324            Float32 => parse_string_view::<Float32Type>(array, cast_options),
1325            Float64 => parse_string_view::<Float64Type>(array, cast_options),
1326            Date32 => parse_string_view::<Date32Type>(array, cast_options),
1327            Date64 => parse_string_view::<Date64Type>(array, cast_options),
1328            Binary => cast_view_to_byte::<StringViewType, GenericBinaryType<i32>>(array),
1329            LargeBinary => cast_view_to_byte::<StringViewType, GenericBinaryType<i64>>(array),
1330            BinaryView => Ok(Arc::new(array.as_string_view().clone().to_binary_view())),
1331            Utf8 => cast_view_to_byte::<StringViewType, GenericStringType<i32>>(array),
1332            LargeUtf8 => cast_view_to_byte::<StringViewType, GenericStringType<i64>>(array),
1333            Time32(TimeUnit::Second) => parse_string_view::<Time32SecondType>(array, cast_options),
1334            Time32(TimeUnit::Millisecond) => {
1335                parse_string_view::<Time32MillisecondType>(array, cast_options)
1336            }
1337            Time64(TimeUnit::Microsecond) => {
1338                parse_string_view::<Time64MicrosecondType>(array, cast_options)
1339            }
1340            Time64(TimeUnit::Nanosecond) => {
1341                parse_string_view::<Time64NanosecondType>(array, cast_options)
1342            }
1343            Timestamp(TimeUnit::Second, to_tz) => {
1344                cast_view_to_timestamp::<TimestampSecondType>(array, to_tz, cast_options)
1345            }
1346            Timestamp(TimeUnit::Millisecond, to_tz) => {
1347                cast_view_to_timestamp::<TimestampMillisecondType>(array, to_tz, cast_options)
1348            }
1349            Timestamp(TimeUnit::Microsecond, to_tz) => {
1350                cast_view_to_timestamp::<TimestampMicrosecondType>(array, to_tz, cast_options)
1351            }
1352            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1353                cast_view_to_timestamp::<TimestampNanosecondType>(array, to_tz, cast_options)
1354            }
1355            Interval(IntervalUnit::YearMonth) => {
1356                cast_view_to_year_month_interval(array, cast_options)
1357            }
1358            Interval(IntervalUnit::DayTime) => cast_view_to_day_time_interval(array, cast_options),
1359            Interval(IntervalUnit::MonthDayNano) => {
1360                cast_view_to_month_day_nano_interval(array, cast_options)
1361            }
1362            _ => Err(ArrowError::CastError(format!(
1363                "Casting from {from_type} to {to_type} not supported",
1364            ))),
1365        },
1366        (LargeUtf8, _) => match to_type {
1367            UInt8 => parse_string::<UInt8Type, i64>(array, cast_options),
1368            UInt16 => parse_string::<UInt16Type, i64>(array, cast_options),
1369            UInt32 => parse_string::<UInt32Type, i64>(array, cast_options),
1370            UInt64 => parse_string::<UInt64Type, i64>(array, cast_options),
1371            Int8 => parse_string::<Int8Type, i64>(array, cast_options),
1372            Int16 => parse_string::<Int16Type, i64>(array, cast_options),
1373            Int32 => parse_string::<Int32Type, i64>(array, cast_options),
1374            Int64 => parse_string::<Int64Type, i64>(array, cast_options),
1375            Float16 => parse_string::<Float16Type, i64>(array, cast_options),
1376            Float32 => parse_string::<Float32Type, i64>(array, cast_options),
1377            Float64 => parse_string::<Float64Type, i64>(array, cast_options),
1378            Date32 => parse_string::<Date32Type, i64>(array, cast_options),
1379            Date64 => parse_string::<Date64Type, i64>(array, cast_options),
1380            Utf8 => cast_byte_container::<LargeUtf8Type, Utf8Type>(array),
1381            Binary => {
1382                let large_binary = LargeBinaryArray::from(array.as_string::<i64>().clone());
1383                cast_byte_container::<LargeBinaryType, BinaryType>(&large_binary)
1384            }
1385            LargeBinary => Ok(Arc::new(LargeBinaryArray::from(
1386                array.as_string::<i64>().clone(),
1387            ))),
1388            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i64>()))),
1389            BinaryView => Ok(Arc::new(BinaryViewArray::from(
1390                array
1391                    .as_string::<i64>()
1392                    .into_iter()
1393                    .map(|x| x.map(|x| x.as_bytes()))
1394                    .collect::<Vec<_>>(),
1395            ))),
1396            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i64>(array, cast_options),
1397            Time32(TimeUnit::Millisecond) => {
1398                parse_string::<Time32MillisecondType, i64>(array, cast_options)
1399            }
1400            Time64(TimeUnit::Microsecond) => {
1401                parse_string::<Time64MicrosecondType, i64>(array, cast_options)
1402            }
1403            Time64(TimeUnit::Nanosecond) => {
1404                parse_string::<Time64NanosecondType, i64>(array, cast_options)
1405            }
1406            Timestamp(TimeUnit::Second, to_tz) => {
1407                cast_string_to_timestamp::<i64, TimestampSecondType>(array, to_tz, cast_options)
1408            }
1409            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1410                i64,
1411                TimestampMillisecondType,
1412            >(array, to_tz, cast_options),
1413            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1414                i64,
1415                TimestampMicrosecondType,
1416            >(array, to_tz, cast_options),
1417            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1418                cast_string_to_timestamp::<i64, TimestampNanosecondType>(array, to_tz, cast_options)
1419            }
1420            Interval(IntervalUnit::YearMonth) => {
1421                cast_string_to_year_month_interval::<i64>(array, cast_options)
1422            }
1423            Interval(IntervalUnit::DayTime) => {
1424                cast_string_to_day_time_interval::<i64>(array, cast_options)
1425            }
1426            Interval(IntervalUnit::MonthDayNano) => {
1427                cast_string_to_month_day_nano_interval::<i64>(array, cast_options)
1428            }
1429            _ => Err(ArrowError::CastError(format!(
1430                "Casting from {from_type} to {to_type} not supported",
1431            ))),
1432        },
1433        (Binary, _) => match to_type {
1434            Utf8 => cast_binary_to_string::<i32>(array, cast_options),
1435            LargeUtf8 => {
1436                let array = cast_binary_to_string::<i32>(array, cast_options)?;
1437                cast_byte_container::<Utf8Type, LargeUtf8Type>(array.as_ref())
1438            }
1439            LargeBinary => cast_byte_container::<BinaryType, LargeBinaryType>(array),
1440            FixedSizeBinary(size) => {
1441                cast_binary_to_fixed_size_binary::<i32>(array, *size, cast_options)
1442            }
1443            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i32>()))),
1444            Utf8View => Ok(Arc::new(StringViewArray::from(
1445                cast_binary_to_string::<i32>(array, cast_options)?.as_string::<i32>(),
1446            ))),
1447            _ => Err(ArrowError::CastError(format!(
1448                "Casting from {from_type} to {to_type} not supported",
1449            ))),
1450        },
1451        (LargeBinary, _) => match to_type {
1452            Utf8 => {
1453                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1454                cast_byte_container::<LargeUtf8Type, Utf8Type>(array.as_ref())
1455            }
1456            LargeUtf8 => cast_binary_to_string::<i64>(array, cast_options),
1457            Binary => cast_byte_container::<LargeBinaryType, BinaryType>(array),
1458            FixedSizeBinary(size) => {
1459                cast_binary_to_fixed_size_binary::<i64>(array, *size, cast_options)
1460            }
1461            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i64>()))),
1462            Utf8View => {
1463                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1464                Ok(Arc::new(StringViewArray::from(array.as_string::<i64>())))
1465            }
1466            _ => Err(ArrowError::CastError(format!(
1467                "Casting from {from_type} to {to_type} not supported",
1468            ))),
1469        },
1470        (FixedSizeBinary(size), _) => match to_type {
1471            Binary => cast_fixed_size_binary_to_binary::<i32>(array, *size),
1472            LargeBinary => cast_fixed_size_binary_to_binary::<i64>(array, *size),
1473            BinaryView => cast_fixed_size_binary_to_binary_view(array, *size),
1474            _ => Err(ArrowError::CastError(format!(
1475                "Casting from {from_type} to {to_type} not supported",
1476            ))),
1477        },
1478        (BinaryView, Binary) => cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array),
1479        (BinaryView, LargeBinary) => {
1480            cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)
1481        }
1482        (BinaryView, Utf8) => {
1483            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array)?;
1484            cast_binary_to_string::<i32>(&binary_arr, cast_options)
1485        }
1486        (BinaryView, LargeUtf8) => {
1487            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)?;
1488            cast_binary_to_string::<i64>(&binary_arr, cast_options)
1489        }
1490        (BinaryView, Utf8View) => cast_binary_view_to_string_view(array, cast_options),
1491        (BinaryView, _) => Err(ArrowError::CastError(format!(
1492            "Casting from {from_type} to {to_type} not supported",
1493        ))),
1494        (from_type, Utf8View) if from_type.is_primitive() => {
1495            value_to_string_view(array, cast_options)
1496        }
1497        (from_type, LargeUtf8) if from_type.is_primitive() => {
1498            value_to_string::<i64>(array, cast_options)
1499        }
1500        (from_type, Utf8) if from_type.is_primitive() => {
1501            value_to_string::<i32>(array, cast_options)
1502        }
1503        (from_type, Binary) if from_type.is_integer() => match from_type {
1504            UInt8 => cast_numeric_to_binary::<UInt8Type, i32>(array),
1505            UInt16 => cast_numeric_to_binary::<UInt16Type, i32>(array),
1506            UInt32 => cast_numeric_to_binary::<UInt32Type, i32>(array),
1507            UInt64 => cast_numeric_to_binary::<UInt64Type, i32>(array),
1508            Int8 => cast_numeric_to_binary::<Int8Type, i32>(array),
1509            Int16 => cast_numeric_to_binary::<Int16Type, i32>(array),
1510            Int32 => cast_numeric_to_binary::<Int32Type, i32>(array),
1511            Int64 => cast_numeric_to_binary::<Int64Type, i32>(array),
1512            _ => unreachable!(),
1513        },
1514        (from_type, LargeBinary) if from_type.is_integer() => match from_type {
1515            UInt8 => cast_numeric_to_binary::<UInt8Type, i64>(array),
1516            UInt16 => cast_numeric_to_binary::<UInt16Type, i64>(array),
1517            UInt32 => cast_numeric_to_binary::<UInt32Type, i64>(array),
1518            UInt64 => cast_numeric_to_binary::<UInt64Type, i64>(array),
1519            Int8 => cast_numeric_to_binary::<Int8Type, i64>(array),
1520            Int16 => cast_numeric_to_binary::<Int16Type, i64>(array),
1521            Int32 => cast_numeric_to_binary::<Int32Type, i64>(array),
1522            Int64 => cast_numeric_to_binary::<Int64Type, i64>(array),
1523            _ => unreachable!(),
1524        },
1525        // start numeric casts
1526        (UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array, cast_options),
1527        (UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array, cast_options),
1528        (UInt8, UInt64) => cast_numeric_arrays::<UInt8Type, UInt64Type>(array, cast_options),
1529        (UInt8, Int8) => cast_numeric_arrays::<UInt8Type, Int8Type>(array, cast_options),
1530        (UInt8, Int16) => cast_numeric_arrays::<UInt8Type, Int16Type>(array, cast_options),
1531        (UInt8, Int32) => cast_numeric_arrays::<UInt8Type, Int32Type>(array, cast_options),
1532        (UInt8, Int64) => cast_numeric_arrays::<UInt8Type, Int64Type>(array, cast_options),
1533        (UInt8, Float16) => cast_numeric_arrays::<UInt8Type, Float16Type>(array, cast_options),
1534        (UInt8, Float32) => cast_numeric_arrays::<UInt8Type, Float32Type>(array, cast_options),
1535        (UInt8, Float64) => cast_numeric_arrays::<UInt8Type, Float64Type>(array, cast_options),
1536
1537        (UInt16, UInt8) => cast_numeric_arrays::<UInt16Type, UInt8Type>(array, cast_options),
1538        (UInt16, UInt32) => cast_numeric_arrays::<UInt16Type, UInt32Type>(array, cast_options),
1539        (UInt16, UInt64) => cast_numeric_arrays::<UInt16Type, UInt64Type>(array, cast_options),
1540        (UInt16, Int8) => cast_numeric_arrays::<UInt16Type, Int8Type>(array, cast_options),
1541        (UInt16, Int16) => cast_numeric_arrays::<UInt16Type, Int16Type>(array, cast_options),
1542        (UInt16, Int32) => cast_numeric_arrays::<UInt16Type, Int32Type>(array, cast_options),
1543        (UInt16, Int64) => cast_numeric_arrays::<UInt16Type, Int64Type>(array, cast_options),
1544        (UInt16, Float16) => cast_numeric_arrays::<UInt16Type, Float16Type>(array, cast_options),
1545        (UInt16, Float32) => cast_numeric_arrays::<UInt16Type, Float32Type>(array, cast_options),
1546        (UInt16, Float64) => cast_numeric_arrays::<UInt16Type, Float64Type>(array, cast_options),
1547
1548        (UInt32, UInt8) => cast_numeric_arrays::<UInt32Type, UInt8Type>(array, cast_options),
1549        (UInt32, UInt16) => cast_numeric_arrays::<UInt32Type, UInt16Type>(array, cast_options),
1550        (UInt32, UInt64) => cast_numeric_arrays::<UInt32Type, UInt64Type>(array, cast_options),
1551        (UInt32, Int8) => cast_numeric_arrays::<UInt32Type, Int8Type>(array, cast_options),
1552        (UInt32, Int16) => cast_numeric_arrays::<UInt32Type, Int16Type>(array, cast_options),
1553        (UInt32, Int32) => cast_numeric_arrays::<UInt32Type, Int32Type>(array, cast_options),
1554        (UInt32, Int64) => cast_numeric_arrays::<UInt32Type, Int64Type>(array, cast_options),
1555        (UInt32, Float16) => cast_numeric_arrays::<UInt32Type, Float16Type>(array, cast_options),
1556        (UInt32, Float32) => cast_numeric_arrays::<UInt32Type, Float32Type>(array, cast_options),
1557        (UInt32, Float64) => cast_numeric_arrays::<UInt32Type, Float64Type>(array, cast_options),
1558
1559        (UInt64, UInt8) => cast_numeric_arrays::<UInt64Type, UInt8Type>(array, cast_options),
1560        (UInt64, UInt16) => cast_numeric_arrays::<UInt64Type, UInt16Type>(array, cast_options),
1561        (UInt64, UInt32) => cast_numeric_arrays::<UInt64Type, UInt32Type>(array, cast_options),
1562        (UInt64, Int8) => cast_numeric_arrays::<UInt64Type, Int8Type>(array, cast_options),
1563        (UInt64, Int16) => cast_numeric_arrays::<UInt64Type, Int16Type>(array, cast_options),
1564        (UInt64, Int32) => cast_numeric_arrays::<UInt64Type, Int32Type>(array, cast_options),
1565        (UInt64, Int64) => cast_numeric_arrays::<UInt64Type, Int64Type>(array, cast_options),
1566        (UInt64, Float16) => cast_numeric_arrays::<UInt64Type, Float16Type>(array, cast_options),
1567        (UInt64, Float32) => cast_numeric_arrays::<UInt64Type, Float32Type>(array, cast_options),
1568        (UInt64, Float64) => cast_numeric_arrays::<UInt64Type, Float64Type>(array, cast_options),
1569
1570        (Int8, UInt8) => cast_numeric_arrays::<Int8Type, UInt8Type>(array, cast_options),
1571        (Int8, UInt16) => cast_numeric_arrays::<Int8Type, UInt16Type>(array, cast_options),
1572        (Int8, UInt32) => cast_numeric_arrays::<Int8Type, UInt32Type>(array, cast_options),
1573        (Int8, UInt64) => cast_numeric_arrays::<Int8Type, UInt64Type>(array, cast_options),
1574        (Int8, Int16) => cast_numeric_arrays::<Int8Type, Int16Type>(array, cast_options),
1575        (Int8, Int32) => cast_numeric_arrays::<Int8Type, Int32Type>(array, cast_options),
1576        (Int8, Int64) => cast_numeric_arrays::<Int8Type, Int64Type>(array, cast_options),
1577        (Int8, Float16) => cast_numeric_arrays::<Int8Type, Float16Type>(array, cast_options),
1578        (Int8, Float32) => cast_numeric_arrays::<Int8Type, Float32Type>(array, cast_options),
1579        (Int8, Float64) => cast_numeric_arrays::<Int8Type, Float64Type>(array, cast_options),
1580
1581        (Int16, UInt8) => cast_numeric_arrays::<Int16Type, UInt8Type>(array, cast_options),
1582        (Int16, UInt16) => cast_numeric_arrays::<Int16Type, UInt16Type>(array, cast_options),
1583        (Int16, UInt32) => cast_numeric_arrays::<Int16Type, UInt32Type>(array, cast_options),
1584        (Int16, UInt64) => cast_numeric_arrays::<Int16Type, UInt64Type>(array, cast_options),
1585        (Int16, Int8) => cast_numeric_arrays::<Int16Type, Int8Type>(array, cast_options),
1586        (Int16, Int32) => cast_numeric_arrays::<Int16Type, Int32Type>(array, cast_options),
1587        (Int16, Int64) => cast_numeric_arrays::<Int16Type, Int64Type>(array, cast_options),
1588        (Int16, Float16) => cast_numeric_arrays::<Int16Type, Float16Type>(array, cast_options),
1589        (Int16, Float32) => cast_numeric_arrays::<Int16Type, Float32Type>(array, cast_options),
1590        (Int16, Float64) => cast_numeric_arrays::<Int16Type, Float64Type>(array, cast_options),
1591
1592        (Int32, UInt8) => cast_numeric_arrays::<Int32Type, UInt8Type>(array, cast_options),
1593        (Int32, UInt16) => cast_numeric_arrays::<Int32Type, UInt16Type>(array, cast_options),
1594        (Int32, UInt32) => cast_numeric_arrays::<Int32Type, UInt32Type>(array, cast_options),
1595        (Int32, UInt64) => cast_numeric_arrays::<Int32Type, UInt64Type>(array, cast_options),
1596        (Int32, Int8) => cast_numeric_arrays::<Int32Type, Int8Type>(array, cast_options),
1597        (Int32, Int16) => cast_numeric_arrays::<Int32Type, Int16Type>(array, cast_options),
1598        (Int32, Int64) => cast_numeric_arrays::<Int32Type, Int64Type>(array, cast_options),
1599        (Int32, Float16) => cast_numeric_arrays::<Int32Type, Float16Type>(array, cast_options),
1600        (Int32, Float32) => cast_numeric_arrays::<Int32Type, Float32Type>(array, cast_options),
1601        (Int32, Float64) => cast_numeric_arrays::<Int32Type, Float64Type>(array, cast_options),
1602
1603        (Int64, UInt8) => cast_numeric_arrays::<Int64Type, UInt8Type>(array, cast_options),
1604        (Int64, UInt16) => cast_numeric_arrays::<Int64Type, UInt16Type>(array, cast_options),
1605        (Int64, UInt32) => cast_numeric_arrays::<Int64Type, UInt32Type>(array, cast_options),
1606        (Int64, UInt64) => cast_numeric_arrays::<Int64Type, UInt64Type>(array, cast_options),
1607        (Int64, Int8) => cast_numeric_arrays::<Int64Type, Int8Type>(array, cast_options),
1608        (Int64, Int16) => cast_numeric_arrays::<Int64Type, Int16Type>(array, cast_options),
1609        (Int64, Int32) => cast_numeric_arrays::<Int64Type, Int32Type>(array, cast_options),
1610        (Int64, Float16) => cast_numeric_arrays::<Int64Type, Float16Type>(array, cast_options),
1611        (Int64, Float32) => cast_numeric_arrays::<Int64Type, Float32Type>(array, cast_options),
1612        (Int64, Float64) => cast_numeric_arrays::<Int64Type, Float64Type>(array, cast_options),
1613
1614        (Float16, UInt8) => cast_numeric_arrays::<Float16Type, UInt8Type>(array, cast_options),
1615        (Float16, UInt16) => cast_numeric_arrays::<Float16Type, UInt16Type>(array, cast_options),
1616        (Float16, UInt32) => cast_numeric_arrays::<Float16Type, UInt32Type>(array, cast_options),
1617        (Float16, UInt64) => cast_numeric_arrays::<Float16Type, UInt64Type>(array, cast_options),
1618        (Float16, Int8) => cast_numeric_arrays::<Float16Type, Int8Type>(array, cast_options),
1619        (Float16, Int16) => cast_numeric_arrays::<Float16Type, Int16Type>(array, cast_options),
1620        (Float16, Int32) => cast_numeric_arrays::<Float16Type, Int32Type>(array, cast_options),
1621        (Float16, Int64) => cast_numeric_arrays::<Float16Type, Int64Type>(array, cast_options),
1622        (Float16, Float32) => cast_numeric_arrays::<Float16Type, Float32Type>(array, cast_options),
1623        (Float16, Float64) => cast_numeric_arrays::<Float16Type, Float64Type>(array, cast_options),
1624
1625        (Float32, UInt8) => cast_numeric_arrays::<Float32Type, UInt8Type>(array, cast_options),
1626        (Float32, UInt16) => cast_numeric_arrays::<Float32Type, UInt16Type>(array, cast_options),
1627        (Float32, UInt32) => cast_numeric_arrays::<Float32Type, UInt32Type>(array, cast_options),
1628        (Float32, UInt64) => cast_numeric_arrays::<Float32Type, UInt64Type>(array, cast_options),
1629        (Float32, Int8) => cast_numeric_arrays::<Float32Type, Int8Type>(array, cast_options),
1630        (Float32, Int16) => cast_numeric_arrays::<Float32Type, Int16Type>(array, cast_options),
1631        (Float32, Int32) => cast_numeric_arrays::<Float32Type, Int32Type>(array, cast_options),
1632        (Float32, Int64) => cast_numeric_arrays::<Float32Type, Int64Type>(array, cast_options),
1633        (Float32, Float16) => cast_numeric_arrays::<Float32Type, Float16Type>(array, cast_options),
1634        (Float32, Float64) => cast_numeric_arrays::<Float32Type, Float64Type>(array, cast_options),
1635
1636        (Float64, UInt8) => cast_numeric_arrays::<Float64Type, UInt8Type>(array, cast_options),
1637        (Float64, UInt16) => cast_numeric_arrays::<Float64Type, UInt16Type>(array, cast_options),
1638        (Float64, UInt32) => cast_numeric_arrays::<Float64Type, UInt32Type>(array, cast_options),
1639        (Float64, UInt64) => cast_numeric_arrays::<Float64Type, UInt64Type>(array, cast_options),
1640        (Float64, Int8) => cast_numeric_arrays::<Float64Type, Int8Type>(array, cast_options),
1641        (Float64, Int16) => cast_numeric_arrays::<Float64Type, Int16Type>(array, cast_options),
1642        (Float64, Int32) => cast_numeric_arrays::<Float64Type, Int32Type>(array, cast_options),
1643        (Float64, Int64) => cast_numeric_arrays::<Float64Type, Int64Type>(array, cast_options),
1644        (Float64, Float16) => cast_numeric_arrays::<Float64Type, Float16Type>(array, cast_options),
1645        (Float64, Float32) => cast_numeric_arrays::<Float64Type, Float32Type>(array, cast_options),
1646        // end numeric casts
1647
1648        // temporal casts
1649        (Int32, Date32) => cast_reinterpret_arrays::<Int32Type, Date32Type>(array),
1650        (Int32, Date64) => cast_with_options(
1651            &cast_with_options(array, &Date32, cast_options)?,
1652            &Date64,
1653            cast_options,
1654        ),
1655        (Int32, Time32(TimeUnit::Second)) => {
1656            cast_reinterpret_arrays::<Int32Type, Time32SecondType>(array)
1657        }
1658        (Int32, Time32(TimeUnit::Millisecond)) => {
1659            cast_reinterpret_arrays::<Int32Type, Time32MillisecondType>(array)
1660        }
1661        // No support for microsecond/nanosecond with i32
1662        (Date32, Int32) => cast_reinterpret_arrays::<Date32Type, Int32Type>(array),
1663        (Date32, Int64) => cast_with_options(
1664            &cast_with_options(array, &Int32, cast_options)?,
1665            &Int64,
1666            cast_options,
1667        ),
1668        (Time32(TimeUnit::Second), Int32) => {
1669            cast_reinterpret_arrays::<Time32SecondType, Int32Type>(array)
1670        }
1671        (Time32(TimeUnit::Millisecond), Int32) => {
1672            cast_reinterpret_arrays::<Time32MillisecondType, Int32Type>(array)
1673        }
1674        (Time32(TimeUnit::Second), Int64) => cast_with_options(
1675            &cast_with_options(array, &Int32, cast_options)?,
1676            &Int64,
1677            cast_options,
1678        ),
1679        (Time32(TimeUnit::Millisecond), Int64) => cast_with_options(
1680            &cast_with_options(array, &Int32, cast_options)?,
1681            &Int64,
1682            cast_options,
1683        ),
1684        (Int64, Date64) => cast_reinterpret_arrays::<Int64Type, Date64Type>(array),
1685        (Int64, Date32) => cast_with_options(
1686            &cast_with_options(array, &Int32, cast_options)?,
1687            &Date32,
1688            cast_options,
1689        ),
1690        // No support for second/milliseconds with i64
1691        (Int64, Time64(TimeUnit::Microsecond)) => {
1692            cast_reinterpret_arrays::<Int64Type, Time64MicrosecondType>(array)
1693        }
1694        (Int64, Time64(TimeUnit::Nanosecond)) => {
1695            cast_reinterpret_arrays::<Int64Type, Time64NanosecondType>(array)
1696        }
1697
1698        (Date64, Int64) => cast_reinterpret_arrays::<Date64Type, Int64Type>(array),
1699        (Date64, Int32) => cast_with_options(
1700            &cast_with_options(array, &Int64, cast_options)?,
1701            &Int32,
1702            cast_options,
1703        ),
1704        (Time64(TimeUnit::Microsecond), Int64) => {
1705            cast_reinterpret_arrays::<Time64MicrosecondType, Int64Type>(array)
1706        }
1707        (Time64(TimeUnit::Nanosecond), Int64) => {
1708            cast_reinterpret_arrays::<Time64NanosecondType, Int64Type>(array)
1709        }
1710        (Date32, Date64) => Ok(Arc::new(
1711            array
1712                .as_primitive::<Date32Type>()
1713                .unary::<_, Date64Type>(|x| x as i64 * MILLISECONDS_IN_DAY),
1714        )),
1715        (Date64, Date32) => {
1716            let array = array.as_primitive::<Date64Type>();
1717            let result = if cast_options.safe {
1718                array.unary_opt::<_, Date32Type>(|x| i32::try_from(x / MILLISECONDS_IN_DAY).ok())
1719            } else {
1720                array.try_unary::<_, Date32Type, _>(|x| {
1721                    i32::try_from(x / MILLISECONDS_IN_DAY).map_err(|_| {
1722                        ArrowError::CastError(format!(
1723                            "Cannot cast Date64 value {x} to Date32 without overflow"
1724                        ))
1725                    })
1726                })?
1727            };
1728            Ok(Arc::new(result))
1729        }
1730
1731        (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => {
1732            let array = array.as_primitive::<Time32SecondType>();
1733            let result = if cast_options.safe {
1734                array.unary_opt::<_, Time32MillisecondType>(|x| x.checked_mul(MILLISECONDS as i32))
1735            } else {
1736                array.try_unary::<_, Time32MillisecondType, _>(|x| {
1737                    x.mul_checked(MILLISECONDS as i32)
1738                })?
1739            };
1740            Ok(Arc::new(result))
1741        }
1742        (Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1743            array
1744                .as_primitive::<Time32SecondType>()
1745                .unary::<_, Time64MicrosecondType>(|x| x as i64 * MICROSECONDS),
1746        )),
1747        (Time32(TimeUnit::Second), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1748            array
1749                .as_primitive::<Time32SecondType>()
1750                .unary::<_, Time64NanosecondType>(|x| x as i64 * NANOSECONDS),
1751        )),
1752
1753        (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1754            array
1755                .as_primitive::<Time32MillisecondType>()
1756                .unary::<_, Time32SecondType>(|x| x / MILLISECONDS as i32),
1757        )),
1758        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1759            array
1760                .as_primitive::<Time32MillisecondType>()
1761                .unary::<_, Time64MicrosecondType>(|x| x as i64 * (MICROSECONDS / MILLISECONDS)),
1762        )),
1763        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1764            array
1765                .as_primitive::<Time32MillisecondType>()
1766                .unary::<_, Time64NanosecondType>(|x| x as i64 * (NANOSECONDS / MILLISECONDS)),
1767        )),
1768
1769        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1770            array
1771                .as_primitive::<Time64MicrosecondType>()
1772                .unary::<_, Time32SecondType>(|x| (x / MICROSECONDS) as i32),
1773        )),
1774        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1775            array
1776                .as_primitive::<Time64MicrosecondType>()
1777                .unary::<_, Time32MillisecondType>(|x| (x / (MICROSECONDS / MILLISECONDS)) as i32),
1778        )),
1779        (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1780            array
1781                .as_primitive::<Time64MicrosecondType>()
1782                .unary::<_, Time64NanosecondType>(|x| x * (NANOSECONDS / MICROSECONDS)),
1783        )),
1784
1785        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1786            array
1787                .as_primitive::<Time64NanosecondType>()
1788                .unary::<_, Time32SecondType>(|x| (x / NANOSECONDS) as i32),
1789        )),
1790        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1791            array
1792                .as_primitive::<Time64NanosecondType>()
1793                .unary::<_, Time32MillisecondType>(|x| (x / (NANOSECONDS / MILLISECONDS)) as i32),
1794        )),
1795        (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1796            array
1797                .as_primitive::<Time64NanosecondType>()
1798                .unary::<_, Time64MicrosecondType>(|x| x / (NANOSECONDS / MICROSECONDS)),
1799        )),
1800
1801        // Timestamp to integer/floating/decimals
1802        (Timestamp(TimeUnit::Second, _), _) if to_type.is_numeric() => {
1803            let array = cast_reinterpret_arrays::<TimestampSecondType, Int64Type>(array)?;
1804            cast_with_options(&array, to_type, cast_options)
1805        }
1806        (Timestamp(TimeUnit::Millisecond, _), _) if to_type.is_numeric() => {
1807            let array = cast_reinterpret_arrays::<TimestampMillisecondType, Int64Type>(array)?;
1808            cast_with_options(&array, to_type, cast_options)
1809        }
1810        (Timestamp(TimeUnit::Microsecond, _), _) if to_type.is_numeric() => {
1811            let array = cast_reinterpret_arrays::<TimestampMicrosecondType, Int64Type>(array)?;
1812            cast_with_options(&array, to_type, cast_options)
1813        }
1814        (Timestamp(TimeUnit::Nanosecond, _), _) if to_type.is_numeric() => {
1815            let array = cast_reinterpret_arrays::<TimestampNanosecondType, Int64Type>(array)?;
1816            cast_with_options(&array, to_type, cast_options)
1817        }
1818
1819        (_, Timestamp(unit, tz)) if from_type.is_numeric() => {
1820            let array = cast_with_options(array, &Int64, cast_options)?;
1821            Ok(make_timestamp_array(
1822                array.as_primitive(),
1823                *unit,
1824                tz.clone(),
1825            ))
1826        }
1827
1828        (Timestamp(from_unit, from_tz), Timestamp(to_unit, to_tz)) => {
1829            let array = cast_with_options(array, &Int64, cast_options)?;
1830            let time_array = array.as_primitive::<Int64Type>();
1831            let from_size = time_unit_multiple(from_unit);
1832            let to_size = time_unit_multiple(to_unit);
1833            // we either divide or multiply, depending on size of each unit
1834            // units are never the same when the types are the same
1835            let converted = match from_size.cmp(&to_size) {
1836                Ordering::Greater => {
1837                    let divisor = from_size / to_size;
1838                    time_array.unary::<_, Int64Type>(|o| o / divisor)
1839                }
1840                Ordering::Equal => time_array.clone(),
1841                Ordering::Less => {
1842                    let mul = to_size / from_size;
1843                    if cast_options.safe {
1844                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1845                    } else {
1846                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1847                    }
1848                }
1849            };
1850            // Normalize timezone
1851            let adjusted = match (from_tz, to_tz) {
1852                // Only this case needs to be adjusted because we're casting from
1853                // unknown time offset to some time offset, we want the time to be
1854                // unchanged.
1855                //
1856                // i.e. Timestamp('2001-01-01T00:00', None) -> Timestamp('2001-01-01T00:00', '+0700')
1857                (None, Some(to_tz)) => {
1858                    let to_tz: Tz = to_tz.parse()?;
1859                    match to_unit {
1860                        TimeUnit::Second => adjust_timestamp_to_timezone::<TimestampSecondType>(
1861                            converted,
1862                            &to_tz,
1863                            cast_options,
1864                        )?,
1865                        TimeUnit::Millisecond => adjust_timestamp_to_timezone::<
1866                            TimestampMillisecondType,
1867                        >(
1868                            converted, &to_tz, cast_options
1869                        )?,
1870                        TimeUnit::Microsecond => adjust_timestamp_to_timezone::<
1871                            TimestampMicrosecondType,
1872                        >(
1873                            converted, &to_tz, cast_options
1874                        )?,
1875                        TimeUnit::Nanosecond => adjust_timestamp_to_timezone::<
1876                            TimestampNanosecondType,
1877                        >(
1878                            converted, &to_tz, cast_options
1879                        )?,
1880                    }
1881                }
1882                _ => converted,
1883            };
1884            Ok(make_timestamp_array(&adjusted, *to_unit, to_tz.clone()))
1885        }
1886        (Timestamp(TimeUnit::Microsecond, _), Date32) => {
1887            timestamp_to_date32(array.as_primitive::<TimestampMicrosecondType>())
1888        }
1889        (Timestamp(TimeUnit::Millisecond, _), Date32) => {
1890            timestamp_to_date32(array.as_primitive::<TimestampMillisecondType>())
1891        }
1892        (Timestamp(TimeUnit::Second, _), Date32) => {
1893            timestamp_to_date32(array.as_primitive::<TimestampSecondType>())
1894        }
1895        (Timestamp(TimeUnit::Nanosecond, _), Date32) => {
1896            timestamp_to_date32(array.as_primitive::<TimestampNanosecondType>())
1897        }
1898        (Timestamp(TimeUnit::Second, _), Date64) => Ok(Arc::new(match cast_options.safe {
1899            true => {
1900                // change error to None
1901                array
1902                    .as_primitive::<TimestampSecondType>()
1903                    .unary_opt::<_, Date64Type>(|x| x.checked_mul(MILLISECONDS))
1904            }
1905            false => array
1906                .as_primitive::<TimestampSecondType>()
1907                .try_unary::<_, Date64Type, _>(|x| x.mul_checked(MILLISECONDS))?,
1908        })),
1909        (Timestamp(TimeUnit::Millisecond, _), Date64) => {
1910            cast_reinterpret_arrays::<TimestampMillisecondType, Date64Type>(array)
1911        }
1912        (Timestamp(TimeUnit::Microsecond, _), Date64) => Ok(Arc::new(
1913            array
1914                .as_primitive::<TimestampMicrosecondType>()
1915                .unary::<_, Date64Type>(|x| x / (MICROSECONDS / MILLISECONDS)),
1916        )),
1917        (Timestamp(TimeUnit::Nanosecond, _), Date64) => Ok(Arc::new(
1918            array
1919                .as_primitive::<TimestampNanosecondType>()
1920                .unary::<_, Date64Type>(|x| x / (NANOSECONDS / MILLISECONDS)),
1921        )),
1922        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Microsecond)) => {
1923            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1924            Ok(Arc::new(
1925                array
1926                    .as_primitive::<TimestampSecondType>()
1927                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1928                        Ok(time_to_time64us(as_time_res_with_timezone::<
1929                            TimestampSecondType,
1930                        >(x, tz)?))
1931                    })?,
1932            ))
1933        }
1934        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Nanosecond)) => {
1935            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1936            Ok(Arc::new(
1937                array
1938                    .as_primitive::<TimestampSecondType>()
1939                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1940                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1941                            TimestampSecondType,
1942                        >(x, tz)?))
1943                    })?,
1944            ))
1945        }
1946        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Microsecond)) => {
1947            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1948            Ok(Arc::new(
1949                array
1950                    .as_primitive::<TimestampMillisecondType>()
1951                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1952                        Ok(time_to_time64us(as_time_res_with_timezone::<
1953                            TimestampMillisecondType,
1954                        >(x, tz)?))
1955                    })?,
1956            ))
1957        }
1958        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Nanosecond)) => {
1959            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1960            Ok(Arc::new(
1961                array
1962                    .as_primitive::<TimestampMillisecondType>()
1963                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1964                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1965                            TimestampMillisecondType,
1966                        >(x, tz)?))
1967                    })?,
1968            ))
1969        }
1970        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Microsecond)) => {
1971            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1972            Ok(Arc::new(
1973                array
1974                    .as_primitive::<TimestampMicrosecondType>()
1975                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1976                        Ok(time_to_time64us(as_time_res_with_timezone::<
1977                            TimestampMicrosecondType,
1978                        >(x, tz)?))
1979                    })?,
1980            ))
1981        }
1982        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Nanosecond)) => {
1983            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1984            Ok(Arc::new(
1985                array
1986                    .as_primitive::<TimestampMicrosecondType>()
1987                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1988                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1989                            TimestampMicrosecondType,
1990                        >(x, tz)?))
1991                    })?,
1992            ))
1993        }
1994        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Microsecond)) => {
1995            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1996            Ok(Arc::new(
1997                array
1998                    .as_primitive::<TimestampNanosecondType>()
1999                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
2000                        Ok(time_to_time64us(as_time_res_with_timezone::<
2001                            TimestampNanosecondType,
2002                        >(x, tz)?))
2003                    })?,
2004            ))
2005        }
2006        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Nanosecond)) => {
2007            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2008            Ok(Arc::new(
2009                array
2010                    .as_primitive::<TimestampNanosecondType>()
2011                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
2012                        Ok(time_to_time64ns(as_time_res_with_timezone::<
2013                            TimestampNanosecondType,
2014                        >(x, tz)?))
2015                    })?,
2016            ))
2017        }
2018        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Second)) => {
2019            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2020            Ok(Arc::new(
2021                array
2022                    .as_primitive::<TimestampSecondType>()
2023                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2024                        Ok(time_to_time32s(as_time_res_with_timezone::<
2025                            TimestampSecondType,
2026                        >(x, tz)?))
2027                    })?,
2028            ))
2029        }
2030        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Millisecond)) => {
2031            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2032            Ok(Arc::new(
2033                array
2034                    .as_primitive::<TimestampSecondType>()
2035                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2036                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2037                            TimestampSecondType,
2038                        >(x, tz)?))
2039                    })?,
2040            ))
2041        }
2042        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Second)) => {
2043            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2044            Ok(Arc::new(
2045                array
2046                    .as_primitive::<TimestampMillisecondType>()
2047                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2048                        Ok(time_to_time32s(as_time_res_with_timezone::<
2049                            TimestampMillisecondType,
2050                        >(x, tz)?))
2051                    })?,
2052            ))
2053        }
2054        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Millisecond)) => {
2055            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2056            Ok(Arc::new(
2057                array
2058                    .as_primitive::<TimestampMillisecondType>()
2059                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2060                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2061                            TimestampMillisecondType,
2062                        >(x, tz)?))
2063                    })?,
2064            ))
2065        }
2066        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Second)) => {
2067            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2068            Ok(Arc::new(
2069                array
2070                    .as_primitive::<TimestampMicrosecondType>()
2071                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2072                        Ok(time_to_time32s(as_time_res_with_timezone::<
2073                            TimestampMicrosecondType,
2074                        >(x, tz)?))
2075                    })?,
2076            ))
2077        }
2078        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Millisecond)) => {
2079            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2080            Ok(Arc::new(
2081                array
2082                    .as_primitive::<TimestampMicrosecondType>()
2083                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2084                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2085                            TimestampMicrosecondType,
2086                        >(x, tz)?))
2087                    })?,
2088            ))
2089        }
2090        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Second)) => {
2091            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2092            Ok(Arc::new(
2093                array
2094                    .as_primitive::<TimestampNanosecondType>()
2095                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2096                        Ok(time_to_time32s(as_time_res_with_timezone::<
2097                            TimestampNanosecondType,
2098                        >(x, tz)?))
2099                    })?,
2100            ))
2101        }
2102        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Millisecond)) => {
2103            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2104            Ok(Arc::new(
2105                array
2106                    .as_primitive::<TimestampNanosecondType>()
2107                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2108                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2109                            TimestampNanosecondType,
2110                        >(x, tz)?))
2111                    })?,
2112            ))
2113        }
2114        (Date64, Timestamp(TimeUnit::Second, _)) => {
2115            let array = array
2116                .as_primitive::<Date64Type>()
2117                .unary::<_, TimestampSecondType>(|x| x / MILLISECONDS);
2118
2119            cast_with_options(&array, to_type, cast_options)
2120        }
2121        (Date64, Timestamp(TimeUnit::Millisecond, _)) => {
2122            let array = array
2123                .as_primitive::<Date64Type>()
2124                .reinterpret_cast::<TimestampMillisecondType>();
2125
2126            cast_with_options(&array, to_type, cast_options)
2127        }
2128
2129        (Date64, Timestamp(TimeUnit::Microsecond, _)) => {
2130            let array = array
2131                .as_primitive::<Date64Type>()
2132                .unary::<_, TimestampMicrosecondType>(|x| x * (MICROSECONDS / MILLISECONDS));
2133
2134            cast_with_options(&array, to_type, cast_options)
2135        }
2136        (Date64, Timestamp(TimeUnit::Nanosecond, _)) => {
2137            let array = array
2138                .as_primitive::<Date64Type>()
2139                .unary::<_, TimestampNanosecondType>(|x| x * (NANOSECONDS / MILLISECONDS));
2140
2141            cast_with_options(&array, to_type, cast_options)
2142        }
2143        (Date32, Timestamp(TimeUnit::Second, _)) => {
2144            let array = array
2145                .as_primitive::<Date32Type>()
2146                .unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY);
2147
2148            cast_with_options(&array, to_type, cast_options)
2149        }
2150        (Date32, Timestamp(TimeUnit::Millisecond, _)) => {
2151            let array = array
2152                .as_primitive::<Date32Type>()
2153                .unary::<_, TimestampMillisecondType>(|x| (x as i64) * MILLISECONDS_IN_DAY);
2154
2155            cast_with_options(&array, to_type, cast_options)
2156        }
2157        (Date32, Timestamp(TimeUnit::Microsecond, _)) => {
2158            let date_array = array.as_primitive::<Date32Type>();
2159            let converted = if cast_options.safe {
2160                date_array.unary_opt::<_, TimestampMicrosecondType>(|x| {
2161                    (x as i64).checked_mul(MICROSECONDS_IN_DAY)
2162                })
2163            } else {
2164                date_array.try_unary::<_, TimestampMicrosecondType, _>(|x| {
2165                    (x as i64).mul_checked(MICROSECONDS_IN_DAY)
2166                })?
2167            };
2168            cast_with_options(&converted, to_type, cast_options)
2169        }
2170        (Date32, Timestamp(TimeUnit::Nanosecond, _)) => {
2171            let date_array = array.as_primitive::<Date32Type>();
2172            let converted = if cast_options.safe {
2173                date_array.unary_opt::<_, TimestampNanosecondType>(|x| {
2174                    (x as i64).checked_mul(NANOSECONDS_IN_DAY)
2175                })
2176            } else {
2177                date_array.try_unary::<_, TimestampNanosecondType, _>(|x| {
2178                    (x as i64).mul_checked(NANOSECONDS_IN_DAY)
2179                })?
2180            };
2181            cast_with_options(&converted, to_type, cast_options)
2182        }
2183
2184        (_, Duration(unit)) if from_type.is_numeric() => {
2185            let array = cast_with_options(array, &Int64, cast_options)?;
2186            Ok(make_duration_array(array.as_primitive(), *unit))
2187        }
2188        (Duration(TimeUnit::Second), _) if to_type.is_numeric() => {
2189            let array = cast_reinterpret_arrays::<DurationSecondType, Int64Type>(array)?;
2190            cast_with_options(&array, to_type, cast_options)
2191        }
2192        (Duration(TimeUnit::Millisecond), _) if to_type.is_numeric() => {
2193            let array = cast_reinterpret_arrays::<DurationMillisecondType, Int64Type>(array)?;
2194            cast_with_options(&array, to_type, cast_options)
2195        }
2196        (Duration(TimeUnit::Microsecond), _) if to_type.is_numeric() => {
2197            let array = cast_reinterpret_arrays::<DurationMicrosecondType, Int64Type>(array)?;
2198            cast_with_options(&array, to_type, cast_options)
2199        }
2200        (Duration(TimeUnit::Nanosecond), _) if to_type.is_numeric() => {
2201            let array = cast_reinterpret_arrays::<DurationNanosecondType, Int64Type>(array)?;
2202            cast_with_options(&array, to_type, cast_options)
2203        }
2204
2205        (Duration(from_unit), Duration(to_unit)) => {
2206            let array = cast_with_options(array, &Int64, cast_options)?;
2207            let time_array = array.as_primitive::<Int64Type>();
2208            let from_size = time_unit_multiple(from_unit);
2209            let to_size = time_unit_multiple(to_unit);
2210            // we either divide or multiply, depending on size of each unit
2211            // units are never the same when the types are the same
2212            let converted = match from_size.cmp(&to_size) {
2213                Ordering::Greater => {
2214                    let divisor = from_size / to_size;
2215                    time_array.unary::<_, Int64Type>(|o| o / divisor)
2216                }
2217                Ordering::Equal => time_array.clone(),
2218                Ordering::Less => {
2219                    let mul = to_size / from_size;
2220                    if cast_options.safe {
2221                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
2222                    } else {
2223                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
2224                    }
2225                }
2226            };
2227            Ok(make_duration_array(&converted, *to_unit))
2228        }
2229
2230        (Duration(TimeUnit::Second), Interval(IntervalUnit::MonthDayNano)) => {
2231            cast_duration_to_interval::<DurationSecondType>(array, cast_options)
2232        }
2233        (Duration(TimeUnit::Millisecond), Interval(IntervalUnit::MonthDayNano)) => {
2234            cast_duration_to_interval::<DurationMillisecondType>(array, cast_options)
2235        }
2236        (Duration(TimeUnit::Microsecond), Interval(IntervalUnit::MonthDayNano)) => {
2237            cast_duration_to_interval::<DurationMicrosecondType>(array, cast_options)
2238        }
2239        (Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano)) => {
2240            cast_duration_to_interval::<DurationNanosecondType>(array, cast_options)
2241        }
2242        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Second)) => {
2243            cast_month_day_nano_to_duration::<DurationSecondType>(array, cast_options)
2244        }
2245        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Millisecond)) => {
2246            cast_month_day_nano_to_duration::<DurationMillisecondType>(array, cast_options)
2247        }
2248        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Microsecond)) => {
2249            cast_month_day_nano_to_duration::<DurationMicrosecondType>(array, cast_options)
2250        }
2251        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Nanosecond)) => {
2252            cast_month_day_nano_to_duration::<DurationNanosecondType>(array, cast_options)
2253        }
2254        (Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
2255            cast_interval_year_month_to_interval_month_day_nano(array, cast_options)
2256        }
2257        (Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
2258            cast_interval_day_time_to_interval_month_day_nano(array, cast_options)
2259        }
2260        (Int32, Interval(IntervalUnit::YearMonth)) => {
2261            cast_reinterpret_arrays::<Int32Type, IntervalYearMonthType>(array)
2262        }
2263        (_, _) => Err(ArrowError::CastError(format!(
2264            "Casting from {from_type} to {to_type} not supported",
2265        ))),
2266    }
2267}
2268
2269fn cast_struct_to_struct(
2270    array: &StructArray,
2271    from_fields: Fields,
2272    to_fields: Fields,
2273    cast_options: &CastOptions,
2274) -> Result<ArrayRef, ArrowError> {
2275    // Fast path: if field names are in the same order, we can just zip and cast
2276    let fields_match_order = from_fields.len() == to_fields.len()
2277        && from_fields
2278            .iter()
2279            .zip(to_fields.iter())
2280            .all(|(f1, f2)| f1.name() == f2.name());
2281
2282    let fields = if fields_match_order {
2283        // Fast path: cast columns in order if their names match
2284        cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2285    } else {
2286        let all_fields_match_by_name = to_fields.iter().all(|to_field| {
2287            from_fields
2288                .iter()
2289                .any(|from_field| from_field.name() == to_field.name())
2290        });
2291
2292        if all_fields_match_by_name {
2293            // Slow path: match fields by name and reorder
2294            cast_struct_fields_by_name(array, from_fields.clone(), to_fields.clone(), cast_options)?
2295        } else {
2296            // Fallback: cast field by field in order
2297            cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2298        }
2299    };
2300
2301    let array = StructArray::try_new(to_fields.clone(), fields, array.nulls().cloned())?;
2302    Ok(Arc::new(array) as ArrayRef)
2303}
2304
2305fn cast_struct_fields_by_name(
2306    array: &StructArray,
2307    from_fields: Fields,
2308    to_fields: Fields,
2309    cast_options: &CastOptions,
2310) -> Result<Vec<ArrayRef>, ArrowError> {
2311    to_fields
2312        .iter()
2313        .map(|to_field| {
2314            let from_field_idx = from_fields
2315                .iter()
2316                .position(|from_field| from_field.name() == to_field.name())
2317                .unwrap(); // safe because we checked above
2318            let column = array.column(from_field_idx);
2319            cast_with_options(column, to_field.data_type(), cast_options)
2320        })
2321        .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2322}
2323
2324fn cast_struct_fields_in_order(
2325    array: &StructArray,
2326    to_fields: Fields,
2327    cast_options: &CastOptions,
2328) -> Result<Vec<ArrayRef>, ArrowError> {
2329    array
2330        .columns()
2331        .iter()
2332        .zip(to_fields.iter())
2333        .map(|(l, field)| cast_with_options(l, field.data_type(), cast_options))
2334        .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2335}
2336
2337fn cast_from_decimal<D, F>(
2338    array: &dyn Array,
2339    base: D::Native,
2340    scale: &i8,
2341    from_type: &DataType,
2342    to_type: &DataType,
2343    as_float: F,
2344    cast_options: &CastOptions,
2345) -> Result<ArrayRef, ArrowError>
2346where
2347    D: DecimalType + ArrowPrimitiveType,
2348    <D as ArrowPrimitiveType>::Native: ToPrimitive,
2349    F: Fn(D::Native) -> f64,
2350{
2351    use DataType::*;
2352    // cast decimal to other type
2353    match to_type {
2354        UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, base, *scale, cast_options),
2355        UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, base, *scale, cast_options),
2356        UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, base, *scale, cast_options),
2357        UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, base, *scale, cast_options),
2358        Int8 => cast_decimal_to_integer::<D, Int8Type>(array, base, *scale, cast_options),
2359        Int16 => cast_decimal_to_integer::<D, Int16Type>(array, base, *scale, cast_options),
2360        Int32 => cast_decimal_to_integer::<D, Int32Type>(array, base, *scale, cast_options),
2361        Int64 => cast_decimal_to_integer::<D, Int64Type>(array, base, *scale, cast_options),
2362        Float16 => cast_decimal_to_float::<D, Float16Type, _>(array, |x| {
2363            half::f16::from_f64(single_decimal_to_float_lossy::<D, F>(
2364                &as_float,
2365                x,
2366                <i32 as From<i8>>::from(*scale),
2367            ))
2368        }),
2369        Float32 => cast_decimal_to_float::<D, Float32Type, _>(array, |x| {
2370            single_decimal_to_float_lossy::<D, F>(&as_float, x, <i32 as From<i8>>::from(*scale))
2371                as f32
2372        }),
2373        Float64 => cast_decimal_to_float::<D, Float64Type, _>(array, |x| {
2374            single_decimal_to_float_lossy::<D, F>(&as_float, x, <i32 as From<i8>>::from(*scale))
2375        }),
2376        Utf8View => value_to_string_view(array, cast_options),
2377        Utf8 => value_to_string::<i32>(array, cast_options),
2378        LargeUtf8 => value_to_string::<i64>(array, cast_options),
2379        Null => Ok(new_null_array(to_type, array.len())),
2380        _ => Err(ArrowError::CastError(format!(
2381            "Casting from {from_type} to {to_type} not supported"
2382        ))),
2383    }
2384}
2385
2386fn cast_to_decimal<D, M>(
2387    array: &dyn Array,
2388    base: M,
2389    precision: &u8,
2390    scale: &i8,
2391    from_type: &DataType,
2392    to_type: &DataType,
2393    cast_options: &CastOptions,
2394) -> Result<ArrayRef, ArrowError>
2395where
2396    D: DecimalType + ArrowPrimitiveType<Native = M>,
2397    M: ArrowNativeTypeOp + DecimalCast,
2398    u8: num_traits::AsPrimitive<M>,
2399    u16: num_traits::AsPrimitive<M>,
2400    u32: num_traits::AsPrimitive<M>,
2401    u64: num_traits::AsPrimitive<M>,
2402    i8: num_traits::AsPrimitive<M>,
2403    i16: num_traits::AsPrimitive<M>,
2404    i32: num_traits::AsPrimitive<M>,
2405    i64: num_traits::AsPrimitive<M>,
2406{
2407    use DataType::*;
2408    // cast data to decimal
2409    match from_type {
2410        UInt8 => cast_integer_to_decimal::<_, D, M>(
2411            array.as_primitive::<UInt8Type>(),
2412            *precision,
2413            *scale,
2414            base,
2415            cast_options,
2416        ),
2417        UInt16 => cast_integer_to_decimal::<_, D, _>(
2418            array.as_primitive::<UInt16Type>(),
2419            *precision,
2420            *scale,
2421            base,
2422            cast_options,
2423        ),
2424        UInt32 => cast_integer_to_decimal::<_, D, _>(
2425            array.as_primitive::<UInt32Type>(),
2426            *precision,
2427            *scale,
2428            base,
2429            cast_options,
2430        ),
2431        UInt64 => cast_integer_to_decimal::<_, D, _>(
2432            array.as_primitive::<UInt64Type>(),
2433            *precision,
2434            *scale,
2435            base,
2436            cast_options,
2437        ),
2438        Int8 => cast_integer_to_decimal::<_, D, _>(
2439            array.as_primitive::<Int8Type>(),
2440            *precision,
2441            *scale,
2442            base,
2443            cast_options,
2444        ),
2445        Int16 => cast_integer_to_decimal::<_, D, _>(
2446            array.as_primitive::<Int16Type>(),
2447            *precision,
2448            *scale,
2449            base,
2450            cast_options,
2451        ),
2452        Int32 => cast_integer_to_decimal::<_, D, _>(
2453            array.as_primitive::<Int32Type>(),
2454            *precision,
2455            *scale,
2456            base,
2457            cast_options,
2458        ),
2459        Int64 => cast_integer_to_decimal::<_, D, _>(
2460            array.as_primitive::<Int64Type>(),
2461            *precision,
2462            *scale,
2463            base,
2464            cast_options,
2465        ),
2466        Float16 => cast_floating_point_to_decimal::<_, D>(
2467            array.as_primitive::<Float16Type>(),
2468            *precision,
2469            *scale,
2470            cast_options,
2471        ),
2472        Float32 => cast_floating_point_to_decimal::<_, D>(
2473            array.as_primitive::<Float32Type>(),
2474            *precision,
2475            *scale,
2476            cast_options,
2477        ),
2478        Float64 => cast_floating_point_to_decimal::<_, D>(
2479            array.as_primitive::<Float64Type>(),
2480            *precision,
2481            *scale,
2482            cast_options,
2483        ),
2484        Utf8View | Utf8 => {
2485            cast_string_to_decimal::<D, i32>(array, *precision, *scale, cast_options)
2486        }
2487        LargeUtf8 => cast_string_to_decimal::<D, i64>(array, *precision, *scale, cast_options),
2488        Null => Ok(new_null_array(to_type, array.len())),
2489        _ => Err(ArrowError::CastError(format!(
2490            "Casting from {from_type} to {to_type} not supported"
2491        ))),
2492    }
2493}
2494
2495/// Get the time unit as a multiple of a second
2496const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
2497    match unit {
2498        TimeUnit::Second => 1,
2499        TimeUnit::Millisecond => MILLISECONDS,
2500        TimeUnit::Microsecond => MICROSECONDS,
2501        TimeUnit::Nanosecond => NANOSECONDS,
2502    }
2503}
2504
2505/// Convert Array into a PrimitiveArray of type, and apply numeric cast
2506fn cast_numeric_arrays<FROM, TO>(
2507    from: &dyn Array,
2508    cast_options: &CastOptions,
2509) -> Result<ArrayRef, ArrowError>
2510where
2511    FROM: ArrowPrimitiveType,
2512    TO: ArrowPrimitiveType,
2513    FROM::Native: NumCast,
2514    TO::Native: NumCast,
2515{
2516    if cast_options.safe {
2517        // If the value can't be casted to the `TO::Native`, return null
2518        Ok(Arc::new(numeric_cast::<FROM, TO>(
2519            from.as_primitive::<FROM>(),
2520        )))
2521    } else {
2522        // If the value can't be casted to the `TO::Native`, return error
2523        Ok(Arc::new(try_numeric_cast::<FROM, TO>(
2524            from.as_primitive::<FROM>(),
2525        )?))
2526    }
2527}
2528
2529// Natural cast between numeric types
2530// If the value of T can't be casted to R, will throw error
2531fn try_numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>, ArrowError>
2532where
2533    T: ArrowPrimitiveType,
2534    R: ArrowPrimitiveType,
2535    T::Native: NumCast,
2536    R::Native: NumCast,
2537{
2538    from.try_unary(|value| {
2539        num_cast::<T::Native, R::Native>(value).ok_or_else(|| {
2540            ArrowError::CastError(format!(
2541                "Can't cast value {:?} to type {}",
2542                value,
2543                R::DATA_TYPE
2544            ))
2545        })
2546    })
2547}
2548
2549/// Natural cast between numeric types
2550/// Return None if the input `value` can't be casted to type `O`.
2551#[inline]
2552pub fn num_cast<I, O>(value: I) -> Option<O>
2553where
2554    I: NumCast,
2555    O: NumCast,
2556{
2557    num_traits::cast::cast::<I, O>(value)
2558}
2559
2560// Natural cast between numeric types
2561// If the value of T can't be casted to R, it will be converted to null
2562fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> PrimitiveArray<R>
2563where
2564    T: ArrowPrimitiveType,
2565    R: ArrowPrimitiveType,
2566    T::Native: NumCast,
2567    R::Native: NumCast,
2568{
2569    from.unary_opt::<_, R>(num_cast::<T::Native, R::Native>)
2570}
2571
2572fn cast_numeric_to_binary<FROM: ArrowPrimitiveType, O: OffsetSizeTrait>(
2573    array: &dyn Array,
2574) -> Result<ArrayRef, ArrowError> {
2575    let array = array.as_primitive::<FROM>();
2576    let size = std::mem::size_of::<FROM::Native>();
2577    let offsets = OffsetBuffer::from_repeated_length(size, array.len());
2578    Ok(Arc::new(GenericBinaryArray::<O>::try_new(
2579        offsets,
2580        array.values().inner().clone(),
2581        array.nulls().cloned(),
2582    )?))
2583}
2584
2585fn adjust_timestamp_to_timezone<T: ArrowTimestampType>(
2586    array: PrimitiveArray<Int64Type>,
2587    to_tz: &Tz,
2588    cast_options: &CastOptions,
2589) -> Result<PrimitiveArray<Int64Type>, ArrowError> {
2590    let adjust = |o| {
2591        let local = as_datetime::<T>(o)?;
2592        let offset = to_tz.offset_from_local_datetime(&local).single()?;
2593        T::from_naive_datetime(local - offset.fix(), None)
2594    };
2595    let adjusted = if cast_options.safe {
2596        array.unary_opt::<_, Int64Type>(adjust)
2597    } else {
2598        array.try_unary::<_, Int64Type, _>(|o| {
2599            adjust(o).ok_or_else(|| {
2600                ArrowError::CastError("Cannot cast timezone to different timezone".to_string())
2601            })
2602        })?
2603    };
2604    Ok(adjusted)
2605}
2606
2607/// Cast numeric types to Boolean
2608///
2609/// Any zero value returns `false` while non-zero returns `true`
2610fn cast_numeric_to_bool<FROM>(from: &dyn Array) -> Result<ArrayRef, ArrowError>
2611where
2612    FROM: ArrowPrimitiveType,
2613{
2614    numeric_to_bool_cast::<FROM>(from.as_primitive::<FROM>()).map(|to| Arc::new(to) as ArrayRef)
2615}
2616
2617fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray, ArrowError>
2618where
2619    T: ArrowPrimitiveType + ArrowPrimitiveType,
2620{
2621    let mut b = BooleanBuilder::with_capacity(from.len());
2622
2623    for i in 0..from.len() {
2624        if from.is_null(i) {
2625            b.append_null();
2626        } else {
2627            b.append_value(cast_num_to_bool::<T::Native>(from.value(i)));
2628        }
2629    }
2630
2631    Ok(b.finish())
2632}
2633
2634/// Cast numeric types to boolean
2635#[inline]
2636pub fn cast_num_to_bool<I>(value: I) -> bool
2637where
2638    I: Default + PartialEq,
2639{
2640    value != I::default()
2641}
2642
2643/// Cast Boolean types to numeric
2644///
2645/// `false` returns 0 while `true` returns 1
2646fn cast_bool_to_numeric<TO>(
2647    from: &dyn Array,
2648    cast_options: &CastOptions,
2649) -> Result<ArrayRef, ArrowError>
2650where
2651    TO: ArrowPrimitiveType,
2652    TO::Native: num_traits::cast::NumCast,
2653{
2654    Ok(Arc::new(bool_to_numeric_cast::<TO>(
2655        from.as_any().downcast_ref::<BooleanArray>().unwrap(),
2656        cast_options,
2657    )))
2658}
2659
2660fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions) -> PrimitiveArray<T>
2661where
2662    T: ArrowPrimitiveType,
2663    T::Native: num_traits::NumCast,
2664{
2665    let iter = (0..from.len()).map(|i| {
2666        if from.is_null(i) {
2667            None
2668        } else {
2669            single_bool_to_numeric::<T::Native>(from.value(i))
2670        }
2671    });
2672    // Benefit:
2673    //     20% performance improvement
2674    // Soundness:
2675    //     The iterator is trustedLen because it comes from a Range
2676    unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
2677}
2678
2679/// Cast single bool value to numeric value.
2680#[inline]
2681pub fn single_bool_to_numeric<O>(value: bool) -> Option<O>
2682where
2683    O: num_traits::NumCast + Default,
2684{
2685    if value {
2686        // a workaround to cast a primitive to type O, infallible
2687        num_traits::cast::cast(1)
2688    } else {
2689        Some(O::default())
2690    }
2691}
2692
2693/// Helper function to cast from one `BinaryArray` or 'LargeBinaryArray' to 'FixedSizeBinaryArray'.
2694fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
2695    array: &dyn Array,
2696    byte_width: i32,
2697    cast_options: &CastOptions,
2698) -> Result<ArrayRef, ArrowError> {
2699    let array = array.as_binary::<O>();
2700    let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(), byte_width);
2701
2702    for i in 0..array.len() {
2703        if array.is_null(i) {
2704            builder.append_null();
2705        } else {
2706            match builder.append_value(array.value(i)) {
2707                Ok(_) => {}
2708                Err(e) => match cast_options.safe {
2709                    true => builder.append_null(),
2710                    false => return Err(e),
2711                },
2712            }
2713        }
2714    }
2715
2716    Ok(Arc::new(builder.finish()))
2717}
2718
2719/// Helper function to cast from 'FixedSizeBinaryArray' to one `BinaryArray` or 'LargeBinaryArray'.
2720/// If the target one is too large for the source array it will return an Error.
2721fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
2722    array: &dyn Array,
2723    byte_width: i32,
2724) -> Result<ArrayRef, ArrowError> {
2725    let array = array
2726        .as_any()
2727        .downcast_ref::<FixedSizeBinaryArray>()
2728        .unwrap();
2729
2730    let offsets: i128 = byte_width as i128 * array.len() as i128;
2731
2732    let is_binary = matches!(GenericBinaryType::<O>::DATA_TYPE, DataType::Binary);
2733    if is_binary && offsets > i32::MAX as i128 {
2734        return Err(ArrowError::ComputeError(
2735            "FixedSizeBinary array too large to cast to Binary array".to_string(),
2736        ));
2737    } else if !is_binary && offsets > i64::MAX as i128 {
2738        return Err(ArrowError::ComputeError(
2739            "FixedSizeBinary array too large to cast to LargeBinary array".to_string(),
2740        ));
2741    }
2742
2743    let mut builder = GenericBinaryBuilder::<O>::with_capacity(array.len(), array.len());
2744
2745    for i in 0..array.len() {
2746        if array.is_null(i) {
2747            builder.append_null();
2748        } else {
2749            builder.append_value(array.value(i));
2750        }
2751    }
2752
2753    Ok(Arc::new(builder.finish()))
2754}
2755
2756fn cast_fixed_size_binary_to_binary_view(
2757    array: &dyn Array,
2758    _byte_width: i32,
2759) -> Result<ArrayRef, ArrowError> {
2760    let array = array
2761        .as_any()
2762        .downcast_ref::<FixedSizeBinaryArray>()
2763        .unwrap();
2764
2765    let mut builder = BinaryViewBuilder::with_capacity(array.len());
2766    for i in 0..array.len() {
2767        if array.is_null(i) {
2768            builder.append_null();
2769        } else {
2770            builder.append_value(array.value(i));
2771        }
2772    }
2773
2774    Ok(Arc::new(builder.finish()))
2775}
2776
2777/// Helper function to cast from one `ByteArrayType` to another and vice versa.
2778/// If the target one (e.g., `LargeUtf8`) is too large for the source array it will return an Error.
2779fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2780where
2781    FROM: ByteArrayType,
2782    TO: ByteArrayType<Native = FROM::Native>,
2783    FROM::Offset: OffsetSizeTrait + ToPrimitive,
2784    TO::Offset: OffsetSizeTrait + NumCast,
2785{
2786    let data = array.to_data();
2787    assert_eq!(data.data_type(), &FROM::DATA_TYPE);
2788    let str_values_buf = data.buffers()[1].clone();
2789    let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
2790
2791    let mut offset_builder = BufferBuilder::<TO::Offset>::new(offsets.len());
2792    offsets
2793        .iter()
2794        .try_for_each::<_, Result<_, ArrowError>>(|offset| {
2795            let offset =
2796                <<TO as ByteArrayType>::Offset as NumCast>::from(*offset).ok_or_else(|| {
2797                    ArrowError::ComputeError(format!(
2798                        "{}{} array too large to cast to {}{} array",
2799                        FROM::Offset::PREFIX,
2800                        FROM::PREFIX,
2801                        TO::Offset::PREFIX,
2802                        TO::PREFIX
2803                    ))
2804                })?;
2805            offset_builder.append(offset);
2806            Ok(())
2807        })?;
2808
2809    let offset_buffer = offset_builder.finish();
2810
2811    let dtype = TO::DATA_TYPE;
2812
2813    let builder = ArrayData::builder(dtype)
2814        .offset(array.offset())
2815        .len(array.len())
2816        .add_buffer(offset_buffer)
2817        .add_buffer(str_values_buf)
2818        .nulls(data.nulls().cloned());
2819
2820    let array_data = unsafe { builder.build_unchecked() };
2821
2822    Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
2823}
2824
2825/// Helper function to cast from one `ByteViewType` array to `ByteArrayType` array.
2826fn cast_view_to_byte<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2827where
2828    FROM: ByteViewType,
2829    TO: ByteArrayType,
2830    FROM::Native: AsRef<TO::Native>,
2831{
2832    let data = array.to_data();
2833    let view_array = GenericByteViewArray::<FROM>::from(data);
2834
2835    let len = view_array.len();
2836    let bytes = view_array
2837        .views()
2838        .iter()
2839        .map(|v| ByteView::from(*v).length as usize)
2840        .sum::<usize>();
2841
2842    let mut byte_array_builder = GenericByteBuilder::<TO>::with_capacity(len, bytes);
2843
2844    for val in view_array.iter() {
2845        byte_array_builder.append_option(val);
2846    }
2847
2848    Ok(Arc::new(byte_array_builder.finish()))
2849}
2850
2851#[cfg(test)]
2852mod tests {
2853    use super::*;
2854    use DataType::*;
2855    use arrow_array::{Int64Array, RunArray, StringArray};
2856    use arrow_buffer::{Buffer, IntervalDayTime, NullBuffer};
2857    use arrow_buffer::{ScalarBuffer, i256};
2858    use arrow_schema::{DataType, Field};
2859    use chrono::NaiveDate;
2860    use half::f16;
2861    use std::sync::Arc;
2862
2863    #[derive(Clone)]
2864    struct DecimalCastTestConfig {
2865        input_prec: u8,
2866        input_scale: i8,
2867        input_repr: i128,
2868        output_prec: u8,
2869        output_scale: i8,
2870        expected_output_repr: Result<i128, String>, // the error variant can contain a string
2871                                                    // template where the "{}" will be
2872                                                    // replaced with the decimal type name
2873                                                    // (e.g. Decimal128)
2874    }
2875
2876    macro_rules! generate_cast_test_case {
2877        ($INPUT_ARRAY: expr, $OUTPUT_TYPE_ARRAY: ident, $OUTPUT_TYPE: expr, $OUTPUT_VALUES: expr) => {
2878            let output =
2879                $OUTPUT_TYPE_ARRAY::from($OUTPUT_VALUES).with_data_type($OUTPUT_TYPE.clone());
2880
2881            // assert cast type
2882            let input_array_type = $INPUT_ARRAY.data_type();
2883            assert!(can_cast_types(input_array_type, $OUTPUT_TYPE));
2884            let result = cast($INPUT_ARRAY, $OUTPUT_TYPE).unwrap();
2885            assert_eq!($OUTPUT_TYPE, result.data_type());
2886            assert_eq!(result.as_ref(), &output);
2887
2888            let cast_option = CastOptions {
2889                safe: false,
2890                format_options: FormatOptions::default(),
2891            };
2892            let result = cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap();
2893            assert_eq!($OUTPUT_TYPE, result.data_type());
2894            assert_eq!(result.as_ref(), &output);
2895        };
2896    }
2897
2898    fn run_decimal_cast_test_case<I, O>(t: DecimalCastTestConfig)
2899    where
2900        I: DecimalType,
2901        O: DecimalType,
2902        I::Native: DecimalCast,
2903        O::Native: DecimalCast,
2904    {
2905        let array = vec![I::Native::from_decimal(t.input_repr)];
2906        let array = array
2907            .into_iter()
2908            .collect::<PrimitiveArray<I>>()
2909            .with_precision_and_scale(t.input_prec, t.input_scale)
2910            .unwrap();
2911        let input_type = array.data_type();
2912        let output_type = O::TYPE_CONSTRUCTOR(t.output_prec, t.output_scale);
2913        assert!(can_cast_types(input_type, &output_type));
2914
2915        let options = CastOptions {
2916            safe: false,
2917            ..Default::default()
2918        };
2919        let result = cast_with_options(&array, &output_type, &options);
2920
2921        match t.expected_output_repr {
2922            Ok(v) => {
2923                let expected_array = vec![O::Native::from_decimal(v)];
2924                let expected_array = expected_array
2925                    .into_iter()
2926                    .collect::<PrimitiveArray<O>>()
2927                    .with_precision_and_scale(t.output_prec, t.output_scale)
2928                    .unwrap();
2929                assert_eq!(*result.unwrap(), expected_array);
2930            }
2931            Err(expected_output_message_template) => {
2932                assert!(result.is_err());
2933                let expected_error_message =
2934                    expected_output_message_template.replace("{}", O::PREFIX);
2935                assert_eq!(result.unwrap_err().to_string(), expected_error_message);
2936            }
2937        }
2938    }
2939
2940    fn create_decimal32_array(
2941        array: Vec<Option<i32>>,
2942        precision: u8,
2943        scale: i8,
2944    ) -> Result<Decimal32Array, ArrowError> {
2945        array
2946            .into_iter()
2947            .collect::<Decimal32Array>()
2948            .with_precision_and_scale(precision, scale)
2949    }
2950
2951    fn create_decimal64_array(
2952        array: Vec<Option<i64>>,
2953        precision: u8,
2954        scale: i8,
2955    ) -> Result<Decimal64Array, ArrowError> {
2956        array
2957            .into_iter()
2958            .collect::<Decimal64Array>()
2959            .with_precision_and_scale(precision, scale)
2960    }
2961
2962    fn create_decimal128_array(
2963        array: Vec<Option<i128>>,
2964        precision: u8,
2965        scale: i8,
2966    ) -> Result<Decimal128Array, ArrowError> {
2967        array
2968            .into_iter()
2969            .collect::<Decimal128Array>()
2970            .with_precision_and_scale(precision, scale)
2971    }
2972
2973    fn create_decimal256_array(
2974        array: Vec<Option<i256>>,
2975        precision: u8,
2976        scale: i8,
2977    ) -> Result<Decimal256Array, ArrowError> {
2978        array
2979            .into_iter()
2980            .collect::<Decimal256Array>()
2981            .with_precision_and_scale(precision, scale)
2982    }
2983
2984    #[test]
2985    #[cfg(not(feature = "force_validate"))]
2986    #[should_panic(
2987        expected = "Cannot cast to Decimal128(20, 3). Overflowing on 57896044618658097711785492504343953926634992332820282019728792003956564819967"
2988    )]
2989    fn test_cast_decimal_to_decimal_round_with_error() {
2990        // decimal256 to decimal128 overflow
2991        let array = vec![
2992            Some(i256::from_i128(1123454)),
2993            Some(i256::from_i128(2123456)),
2994            Some(i256::from_i128(-3123453)),
2995            Some(i256::from_i128(-3123456)),
2996            None,
2997            Some(i256::MAX),
2998            Some(i256::MIN),
2999        ];
3000        let input_decimal_array = create_decimal256_array(array, 76, 4).unwrap();
3001        let array = Arc::new(input_decimal_array) as ArrayRef;
3002        let input_type = DataType::Decimal256(76, 4);
3003        let output_type = DataType::Decimal128(20, 3);
3004        assert!(can_cast_types(&input_type, &output_type));
3005        generate_cast_test_case!(
3006            &array,
3007            Decimal128Array,
3008            &output_type,
3009            vec![
3010                Some(112345_i128),
3011                Some(212346_i128),
3012                Some(-312345_i128),
3013                Some(-312346_i128),
3014                None,
3015                None,
3016                None,
3017            ]
3018        );
3019    }
3020
3021    #[test]
3022    #[cfg(not(feature = "force_validate"))]
3023    fn test_cast_decimal_to_decimal_round() {
3024        let array = vec![
3025            Some(1123454),
3026            Some(2123456),
3027            Some(-3123453),
3028            Some(-3123456),
3029            None,
3030        ];
3031        let array = create_decimal128_array(array, 20, 4).unwrap();
3032        // decimal128 to decimal128
3033        let input_type = DataType::Decimal128(20, 4);
3034        let output_type = DataType::Decimal128(20, 3);
3035        assert!(can_cast_types(&input_type, &output_type));
3036        generate_cast_test_case!(
3037            &array,
3038            Decimal128Array,
3039            &output_type,
3040            vec![
3041                Some(112345_i128),
3042                Some(212346_i128),
3043                Some(-312345_i128),
3044                Some(-312346_i128),
3045                None
3046            ]
3047        );
3048
3049        // decimal128 to decimal256
3050        let input_type = DataType::Decimal128(20, 4);
3051        let output_type = DataType::Decimal256(20, 3);
3052        assert!(can_cast_types(&input_type, &output_type));
3053        generate_cast_test_case!(
3054            &array,
3055            Decimal256Array,
3056            &output_type,
3057            vec![
3058                Some(i256::from_i128(112345_i128)),
3059                Some(i256::from_i128(212346_i128)),
3060                Some(i256::from_i128(-312345_i128)),
3061                Some(i256::from_i128(-312346_i128)),
3062                None
3063            ]
3064        );
3065
3066        // decimal256
3067        let array = vec![
3068            Some(i256::from_i128(1123454)),
3069            Some(i256::from_i128(2123456)),
3070            Some(i256::from_i128(-3123453)),
3071            Some(i256::from_i128(-3123456)),
3072            None,
3073        ];
3074        let array = create_decimal256_array(array, 20, 4).unwrap();
3075
3076        // decimal256 to decimal256
3077        let input_type = DataType::Decimal256(20, 4);
3078        let output_type = DataType::Decimal256(20, 3);
3079        assert!(can_cast_types(&input_type, &output_type));
3080        generate_cast_test_case!(
3081            &array,
3082            Decimal256Array,
3083            &output_type,
3084            vec![
3085                Some(i256::from_i128(112345_i128)),
3086                Some(i256::from_i128(212346_i128)),
3087                Some(i256::from_i128(-312345_i128)),
3088                Some(i256::from_i128(-312346_i128)),
3089                None
3090            ]
3091        );
3092        // decimal256 to decimal128
3093        let input_type = DataType::Decimal256(20, 4);
3094        let output_type = DataType::Decimal128(20, 3);
3095        assert!(can_cast_types(&input_type, &output_type));
3096        generate_cast_test_case!(
3097            &array,
3098            Decimal128Array,
3099            &output_type,
3100            vec![
3101                Some(112345_i128),
3102                Some(212346_i128),
3103                Some(-312345_i128),
3104                Some(-312346_i128),
3105                None
3106            ]
3107        );
3108    }
3109
3110    #[test]
3111    fn test_cast_decimal32_to_decimal32() {
3112        // test changing precision
3113        let input_type = DataType::Decimal32(9, 3);
3114        let output_type = DataType::Decimal32(9, 4);
3115        assert!(can_cast_types(&input_type, &output_type));
3116        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3117        let array = create_decimal32_array(array, 9, 3).unwrap();
3118        generate_cast_test_case!(
3119            &array,
3120            Decimal32Array,
3121            &output_type,
3122            vec![
3123                Some(11234560_i32),
3124                Some(21234560_i32),
3125                Some(31234560_i32),
3126                None
3127            ]
3128        );
3129        // negative test
3130        let array = vec![Some(123456), None];
3131        let array = create_decimal32_array(array, 9, 0).unwrap();
3132        let result_safe = cast(&array, &DataType::Decimal32(2, 2));
3133        assert!(result_safe.is_ok());
3134        let options = CastOptions {
3135            safe: false,
3136            ..Default::default()
3137        };
3138
3139        let result_unsafe = cast_with_options(&array, &DataType::Decimal32(2, 2), &options);
3140        assert_eq!(
3141            "Invalid argument error: 123456.00 is too large to store in a Decimal32 of precision 2. Max is 0.99",
3142            result_unsafe.unwrap_err().to_string()
3143        );
3144    }
3145
3146    #[test]
3147    fn test_cast_decimal64_to_decimal64() {
3148        // test changing precision
3149        let input_type = DataType::Decimal64(17, 3);
3150        let output_type = DataType::Decimal64(17, 4);
3151        assert!(can_cast_types(&input_type, &output_type));
3152        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3153        let array = create_decimal64_array(array, 17, 3).unwrap();
3154        generate_cast_test_case!(
3155            &array,
3156            Decimal64Array,
3157            &output_type,
3158            vec![
3159                Some(11234560_i64),
3160                Some(21234560_i64),
3161                Some(31234560_i64),
3162                None
3163            ]
3164        );
3165        // negative test
3166        let array = vec![Some(123456), None];
3167        let array = create_decimal64_array(array, 9, 0).unwrap();
3168        let result_safe = cast(&array, &DataType::Decimal64(2, 2));
3169        assert!(result_safe.is_ok());
3170        let options = CastOptions {
3171            safe: false,
3172            ..Default::default()
3173        };
3174
3175        let result_unsafe = cast_with_options(&array, &DataType::Decimal64(2, 2), &options);
3176        assert_eq!(
3177            "Invalid argument error: 123456.00 is too large to store in a Decimal64 of precision 2. Max is 0.99",
3178            result_unsafe.unwrap_err().to_string()
3179        );
3180    }
3181
3182    #[test]
3183    fn test_cast_decimal128_to_decimal128() {
3184        // test changing precision
3185        let input_type = DataType::Decimal128(20, 3);
3186        let output_type = DataType::Decimal128(20, 4);
3187        assert!(can_cast_types(&input_type, &output_type));
3188        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3189        let array = create_decimal128_array(array, 20, 3).unwrap();
3190        generate_cast_test_case!(
3191            &array,
3192            Decimal128Array,
3193            &output_type,
3194            vec![
3195                Some(11234560_i128),
3196                Some(21234560_i128),
3197                Some(31234560_i128),
3198                None
3199            ]
3200        );
3201        // negative test
3202        let array = vec![Some(123456), None];
3203        let array = create_decimal128_array(array, 10, 0).unwrap();
3204        let result_safe = cast(&array, &DataType::Decimal128(2, 2));
3205        assert!(result_safe.is_ok());
3206        let options = CastOptions {
3207            safe: false,
3208            ..Default::default()
3209        };
3210
3211        let result_unsafe = cast_with_options(&array, &DataType::Decimal128(2, 2), &options);
3212        assert_eq!(
3213            "Invalid argument error: 123456.00 is too large to store in a Decimal128 of precision 2. Max is 0.99",
3214            result_unsafe.unwrap_err().to_string()
3215        );
3216    }
3217
3218    #[test]
3219    fn test_cast_decimal32_to_decimal32_dict() {
3220        let p = 9;
3221        let s = 3;
3222        let input_type = DataType::Decimal32(p, s);
3223        let output_type = DataType::Dictionary(
3224            Box::new(DataType::Int32),
3225            Box::new(DataType::Decimal32(p, s)),
3226        );
3227        assert!(can_cast_types(&input_type, &output_type));
3228        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3229        let array = create_decimal32_array(array, p, s).unwrap();
3230        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3231        assert_eq!(cast_array.data_type(), &output_type);
3232    }
3233
3234    #[test]
3235    fn test_cast_decimal64_to_decimal64_dict() {
3236        let p = 15;
3237        let s = 3;
3238        let input_type = DataType::Decimal64(p, s);
3239        let output_type = DataType::Dictionary(
3240            Box::new(DataType::Int32),
3241            Box::new(DataType::Decimal64(p, s)),
3242        );
3243        assert!(can_cast_types(&input_type, &output_type));
3244        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3245        let array = create_decimal64_array(array, p, s).unwrap();
3246        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3247        assert_eq!(cast_array.data_type(), &output_type);
3248    }
3249
3250    #[test]
3251    fn test_cast_decimal128_to_decimal128_dict() {
3252        let p = 20;
3253        let s = 3;
3254        let input_type = DataType::Decimal128(p, s);
3255        let output_type = DataType::Dictionary(
3256            Box::new(DataType::Int32),
3257            Box::new(DataType::Decimal128(p, s)),
3258        );
3259        assert!(can_cast_types(&input_type, &output_type));
3260        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3261        let array = create_decimal128_array(array, p, s).unwrap();
3262        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3263        assert_eq!(cast_array.data_type(), &output_type);
3264    }
3265
3266    #[test]
3267    fn test_cast_decimal256_to_decimal256_dict() {
3268        let p = 20;
3269        let s = 3;
3270        let input_type = DataType::Decimal256(p, s);
3271        let output_type = DataType::Dictionary(
3272            Box::new(DataType::Int32),
3273            Box::new(DataType::Decimal256(p, s)),
3274        );
3275        assert!(can_cast_types(&input_type, &output_type));
3276        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3277        let array = create_decimal128_array(array, p, s).unwrap();
3278        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3279        assert_eq!(cast_array.data_type(), &output_type);
3280    }
3281
3282    #[test]
3283    fn test_cast_decimal32_to_decimal32_overflow() {
3284        let input_type = DataType::Decimal32(9, 3);
3285        let output_type = DataType::Decimal32(9, 9);
3286        assert!(can_cast_types(&input_type, &output_type));
3287
3288        let array = vec![Some(i32::MAX)];
3289        let array = create_decimal32_array(array, 9, 3).unwrap();
3290        let result = cast_with_options(
3291            &array,
3292            &output_type,
3293            &CastOptions {
3294                safe: false,
3295                format_options: FormatOptions::default(),
3296            },
3297        );
3298        assert_eq!(
3299            "Cast error: Cannot cast to Decimal32(9, 9). Overflowing on 2147483647",
3300            result.unwrap_err().to_string()
3301        );
3302    }
3303
3304    #[test]
3305    fn test_cast_decimal32_to_decimal32_large_scale_reduction() {
3306        let array = vec![Some(-999999999), Some(0), Some(999999999), None];
3307        let array = create_decimal32_array(array, 9, 3).unwrap();
3308
3309        // Divide out all digits of precision -- rounding could still produce +/- 1
3310        let output_type = DataType::Decimal32(9, -6);
3311        assert!(can_cast_types(array.data_type(), &output_type));
3312        generate_cast_test_case!(
3313            &array,
3314            Decimal32Array,
3315            &output_type,
3316            vec![Some(-1), Some(0), Some(1), None]
3317        );
3318
3319        // Divide out more digits than we have precision -- all-zero result
3320        let output_type = DataType::Decimal32(9, -7);
3321        assert!(can_cast_types(array.data_type(), &output_type));
3322        generate_cast_test_case!(
3323            &array,
3324            Decimal32Array,
3325            &output_type,
3326            vec![Some(0), Some(0), Some(0), None]
3327        );
3328    }
3329
3330    #[test]
3331    fn test_cast_decimal64_to_decimal64_overflow() {
3332        let input_type = DataType::Decimal64(18, 3);
3333        let output_type = DataType::Decimal64(18, 18);
3334        assert!(can_cast_types(&input_type, &output_type));
3335
3336        let array = vec![Some(i64::MAX)];
3337        let array = create_decimal64_array(array, 18, 3).unwrap();
3338        let result = cast_with_options(
3339            &array,
3340            &output_type,
3341            &CastOptions {
3342                safe: false,
3343                format_options: FormatOptions::default(),
3344            },
3345        );
3346        assert_eq!(
3347            "Cast error: Cannot cast to Decimal64(18, 18). Overflowing on 9223372036854775807",
3348            result.unwrap_err().to_string()
3349        );
3350    }
3351
3352    #[test]
3353    fn test_cast_decimal64_to_decimal64_large_scale_reduction() {
3354        let array = vec![
3355            Some(-999999999999999999),
3356            Some(0),
3357            Some(999999999999999999),
3358            None,
3359        ];
3360        let array = create_decimal64_array(array, 18, 3).unwrap();
3361
3362        // Divide out all digits of precision -- rounding could still produce +/- 1
3363        let output_type = DataType::Decimal64(18, -15);
3364        assert!(can_cast_types(array.data_type(), &output_type));
3365        generate_cast_test_case!(
3366            &array,
3367            Decimal64Array,
3368            &output_type,
3369            vec![Some(-1), Some(0), Some(1), None]
3370        );
3371
3372        // Divide out more digits than we have precision -- all-zero result
3373        let output_type = DataType::Decimal64(18, -16);
3374        assert!(can_cast_types(array.data_type(), &output_type));
3375        generate_cast_test_case!(
3376            &array,
3377            Decimal64Array,
3378            &output_type,
3379            vec![Some(0), Some(0), Some(0), None]
3380        );
3381    }
3382
3383    #[test]
3384    fn test_cast_floating_to_decimals() {
3385        for output_type in [
3386            DataType::Decimal32(9, 3),
3387            DataType::Decimal64(9, 3),
3388            DataType::Decimal128(9, 3),
3389            DataType::Decimal256(9, 3),
3390        ] {
3391            let input_type = DataType::Float64;
3392            assert!(can_cast_types(&input_type, &output_type));
3393
3394            let array = vec![Some(1.1_f64)];
3395            let array = PrimitiveArray::<Float64Type>::from_iter(array);
3396            let result = cast_with_options(
3397                &array,
3398                &output_type,
3399                &CastOptions {
3400                    safe: false,
3401                    format_options: FormatOptions::default(),
3402                },
3403            );
3404            assert!(
3405                result.is_ok(),
3406                "Failed to cast to {output_type} with: {}",
3407                result.unwrap_err()
3408            );
3409        }
3410    }
3411
3412    #[test]
3413    fn test_cast_float16_to_decimals() {
3414        let array = Float16Array::from(vec![
3415            Some(f16::from_f32(1.25)),
3416            Some(f16::from_f32(-2.5)),
3417            Some(f16::from_f32(1.125)),
3418            Some(f16::from_f32(-1.125)),
3419            Some(f16::from_f32(0.0)),
3420            None,
3421        ]);
3422
3423        generate_cast_test_case!(
3424            &array,
3425            Decimal32Array,
3426            &DataType::Decimal32(9, 2),
3427            vec![
3428                Some(125_i32),
3429                Some(-250_i32),
3430                Some(113_i32),
3431                Some(-113_i32),
3432                Some(0_i32),
3433                None
3434            ]
3435        );
3436        generate_cast_test_case!(
3437            &array,
3438            Decimal64Array,
3439            &DataType::Decimal64(18, 2),
3440            vec![
3441                Some(125_i64),
3442                Some(-250_i64),
3443                Some(113_i64),
3444                Some(-113_i64),
3445                Some(0_i64),
3446                None
3447            ]
3448        );
3449        generate_cast_test_case!(
3450            &array,
3451            Decimal128Array,
3452            &DataType::Decimal128(38, 2),
3453            vec![
3454                Some(125_i128),
3455                Some(-250_i128),
3456                Some(113_i128),
3457                Some(-113_i128),
3458                Some(0_i128),
3459                None
3460            ]
3461        );
3462        generate_cast_test_case!(
3463            &array,
3464            Decimal256Array,
3465            &DataType::Decimal256(76, 2),
3466            vec![
3467                Some(i256::from_i128(125_i128)),
3468                Some(i256::from_i128(-250_i128)),
3469                Some(i256::from_i128(113_i128)),
3470                Some(i256::from_i128(-113_i128)),
3471                Some(i256::from_i128(0_i128)),
3472                None
3473            ]
3474        );
3475
3476        let array = Float16Array::from(vec![
3477            Some(f16::from_f32(1250.0)),
3478            Some(f16::from_f32(-1250.0)),
3479            Some(f16::from_f32(1249.0)),
3480            None,
3481        ]);
3482        generate_cast_test_case!(
3483            &array,
3484            Decimal128Array,
3485            &DataType::Decimal128(5, -2),
3486            vec![Some(13_i128), Some(-13_i128), Some(12_i128), None]
3487        );
3488    }
3489
3490    #[test]
3491    fn test_cast_decimal128_to_decimal128_overflow() {
3492        let input_type = DataType::Decimal128(38, 3);
3493        let output_type = DataType::Decimal128(38, 38);
3494        assert!(can_cast_types(&input_type, &output_type));
3495
3496        let array = vec![Some(i128::MAX)];
3497        let array = create_decimal128_array(array, 38, 3).unwrap();
3498        let result = cast_with_options(
3499            &array,
3500            &output_type,
3501            &CastOptions {
3502                safe: false,
3503                format_options: FormatOptions::default(),
3504            },
3505        );
3506        assert_eq!(
3507            "Cast error: Cannot cast to Decimal128(38, 38). Overflowing on 170141183460469231731687303715884105727",
3508            result.unwrap_err().to_string()
3509        );
3510    }
3511
3512    #[test]
3513    fn test_cast_decimal128_to_decimal256_overflow() {
3514        let input_type = DataType::Decimal128(38, 3);
3515        let output_type = DataType::Decimal256(76, 76);
3516        assert!(can_cast_types(&input_type, &output_type));
3517
3518        let array = vec![Some(i128::MAX)];
3519        let array = create_decimal128_array(array, 38, 3).unwrap();
3520        let result = cast_with_options(
3521            &array,
3522            &output_type,
3523            &CastOptions {
3524                safe: false,
3525                format_options: FormatOptions::default(),
3526            },
3527        );
3528        assert_eq!(
3529            "Cast error: Cannot cast to Decimal256(76, 76). Overflowing on 170141183460469231731687303715884105727",
3530            result.unwrap_err().to_string()
3531        );
3532    }
3533
3534    #[test]
3535    fn test_cast_decimal32_to_decimal256() {
3536        let input_type = DataType::Decimal32(8, 3);
3537        let output_type = DataType::Decimal256(20, 4);
3538        assert!(can_cast_types(&input_type, &output_type));
3539        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3540        let array = create_decimal32_array(array, 8, 3).unwrap();
3541        generate_cast_test_case!(
3542            &array,
3543            Decimal256Array,
3544            &output_type,
3545            vec![
3546                Some(i256::from_i128(11234560_i128)),
3547                Some(i256::from_i128(21234560_i128)),
3548                Some(i256::from_i128(31234560_i128)),
3549                None
3550            ]
3551        );
3552    }
3553    #[test]
3554    fn test_cast_decimal64_to_decimal256() {
3555        let input_type = DataType::Decimal64(12, 3);
3556        let output_type = DataType::Decimal256(20, 4);
3557        assert!(can_cast_types(&input_type, &output_type));
3558        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3559        let array = create_decimal64_array(array, 12, 3).unwrap();
3560        generate_cast_test_case!(
3561            &array,
3562            Decimal256Array,
3563            &output_type,
3564            vec![
3565                Some(i256::from_i128(11234560_i128)),
3566                Some(i256::from_i128(21234560_i128)),
3567                Some(i256::from_i128(31234560_i128)),
3568                None
3569            ]
3570        );
3571    }
3572    #[test]
3573    fn test_cast_decimal128_to_decimal256() {
3574        let input_type = DataType::Decimal128(20, 3);
3575        let output_type = DataType::Decimal256(20, 4);
3576        assert!(can_cast_types(&input_type, &output_type));
3577        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3578        let array = create_decimal128_array(array, 20, 3).unwrap();
3579        generate_cast_test_case!(
3580            &array,
3581            Decimal256Array,
3582            &output_type,
3583            vec![
3584                Some(i256::from_i128(11234560_i128)),
3585                Some(i256::from_i128(21234560_i128)),
3586                Some(i256::from_i128(31234560_i128)),
3587                None
3588            ]
3589        );
3590    }
3591
3592    #[test]
3593    fn test_cast_decimal256_to_decimal128_overflow() {
3594        let input_type = DataType::Decimal256(76, 5);
3595        let output_type = DataType::Decimal128(38, 7);
3596        assert!(can_cast_types(&input_type, &output_type));
3597        let array = vec![Some(i256::from_i128(i128::MAX))];
3598        let array = create_decimal256_array(array, 76, 5).unwrap();
3599        let result = cast_with_options(
3600            &array,
3601            &output_type,
3602            &CastOptions {
3603                safe: false,
3604                format_options: FormatOptions::default(),
3605            },
3606        );
3607        assert_eq!(
3608            "Cast error: Cannot cast to Decimal128(38, 7). Overflowing on 170141183460469231731687303715884105727",
3609            result.unwrap_err().to_string()
3610        );
3611    }
3612
3613    #[test]
3614    fn test_cast_decimal256_to_decimal256_overflow() {
3615        let input_type = DataType::Decimal256(76, 5);
3616        let output_type = DataType::Decimal256(76, 55);
3617        assert!(can_cast_types(&input_type, &output_type));
3618        let array = vec![Some(i256::from_i128(i128::MAX))];
3619        let array = create_decimal256_array(array, 76, 5).unwrap();
3620        let result = cast_with_options(
3621            &array,
3622            &output_type,
3623            &CastOptions {
3624                safe: false,
3625                format_options: FormatOptions::default(),
3626            },
3627        );
3628        assert_eq!(
3629            "Cast error: Cannot cast to Decimal256(76, 55). Overflowing on 170141183460469231731687303715884105727",
3630            result.unwrap_err().to_string()
3631        );
3632    }
3633
3634    #[test]
3635    fn test_cast_decimal256_to_decimal128() {
3636        let input_type = DataType::Decimal256(20, 3);
3637        let output_type = DataType::Decimal128(20, 4);
3638        assert!(can_cast_types(&input_type, &output_type));
3639        let array = vec![
3640            Some(i256::from_i128(1123456)),
3641            Some(i256::from_i128(2123456)),
3642            Some(i256::from_i128(3123456)),
3643            None,
3644        ];
3645        let array = create_decimal256_array(array, 20, 3).unwrap();
3646        generate_cast_test_case!(
3647            &array,
3648            Decimal128Array,
3649            &output_type,
3650            vec![
3651                Some(11234560_i128),
3652                Some(21234560_i128),
3653                Some(31234560_i128),
3654                None
3655            ]
3656        );
3657    }
3658
3659    #[test]
3660    fn test_cast_decimal256_to_decimal256() {
3661        let input_type = DataType::Decimal256(20, 3);
3662        let output_type = DataType::Decimal256(20, 4);
3663        assert!(can_cast_types(&input_type, &output_type));
3664        let array = vec![
3665            Some(i256::from_i128(1123456)),
3666            Some(i256::from_i128(2123456)),
3667            Some(i256::from_i128(3123456)),
3668            None,
3669        ];
3670        let array = create_decimal256_array(array, 20, 3).unwrap();
3671        generate_cast_test_case!(
3672            &array,
3673            Decimal256Array,
3674            &output_type,
3675            vec![
3676                Some(i256::from_i128(11234560_i128)),
3677                Some(i256::from_i128(21234560_i128)),
3678                Some(i256::from_i128(31234560_i128)),
3679                None
3680            ]
3681        );
3682    }
3683
3684    fn generate_decimal_to_numeric_cast_test_case<T>(array: &PrimitiveArray<T>)
3685    where
3686        T: ArrowPrimitiveType + DecimalType,
3687    {
3688        // u8
3689        generate_cast_test_case!(
3690            array,
3691            UInt8Array,
3692            &DataType::UInt8,
3693            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3694        );
3695        // u16
3696        generate_cast_test_case!(
3697            array,
3698            UInt16Array,
3699            &DataType::UInt16,
3700            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3701        );
3702        // u32
3703        generate_cast_test_case!(
3704            array,
3705            UInt32Array,
3706            &DataType::UInt32,
3707            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3708        );
3709        // u64
3710        generate_cast_test_case!(
3711            array,
3712            UInt64Array,
3713            &DataType::UInt64,
3714            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3715        );
3716        // i8
3717        generate_cast_test_case!(
3718            array,
3719            Int8Array,
3720            &DataType::Int8,
3721            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3722        );
3723        // i16
3724        generate_cast_test_case!(
3725            array,
3726            Int16Array,
3727            &DataType::Int16,
3728            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3729        );
3730        // i32
3731        generate_cast_test_case!(
3732            array,
3733            Int32Array,
3734            &DataType::Int32,
3735            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3736        );
3737        // i64
3738        generate_cast_test_case!(
3739            array,
3740            Int64Array,
3741            &DataType::Int64,
3742            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3743        );
3744        // f16
3745        generate_cast_test_case!(
3746            array,
3747            Float16Array,
3748            &DataType::Float16,
3749            vec![
3750                Some(f16::from_f32(1.25)),
3751                Some(f16::from_f32(2.25)),
3752                Some(f16::from_f32(3.25)),
3753                None,
3754                Some(f16::from_f32(5.25))
3755            ]
3756        );
3757        // f32
3758        generate_cast_test_case!(
3759            array,
3760            Float32Array,
3761            &DataType::Float32,
3762            vec![
3763                Some(1.25_f32),
3764                Some(2.25_f32),
3765                Some(3.25_f32),
3766                None,
3767                Some(5.25_f32)
3768            ]
3769        );
3770        // f64
3771        generate_cast_test_case!(
3772            array,
3773            Float64Array,
3774            &DataType::Float64,
3775            vec![
3776                Some(1.25_f64),
3777                Some(2.25_f64),
3778                Some(3.25_f64),
3779                None,
3780                Some(5.25_f64)
3781            ]
3782        );
3783    }
3784
3785    #[test]
3786    fn test_cast_decimal32_to_numeric() {
3787        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3788        let array = create_decimal32_array(value_array, 8, 2).unwrap();
3789
3790        generate_decimal_to_numeric_cast_test_case(&array);
3791    }
3792
3793    #[test]
3794    fn test_cast_decimal64_to_numeric() {
3795        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3796        let array = create_decimal64_array(value_array, 8, 2).unwrap();
3797
3798        generate_decimal_to_numeric_cast_test_case(&array);
3799    }
3800
3801    #[test]
3802    fn test_cast_decimal128_to_numeric() {
3803        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3804        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3805
3806        generate_decimal_to_numeric_cast_test_case(&array);
3807
3808        // overflow test: out of range of max u8
3809        let value_array: Vec<Option<i128>> = vec![Some(51300)];
3810        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3811        let casted_array = cast_with_options(
3812            &array,
3813            &DataType::UInt8,
3814            &CastOptions {
3815                safe: false,
3816                format_options: FormatOptions::default(),
3817            },
3818        );
3819        assert_eq!(
3820            "Cast error: value of 513 is out of range UInt8".to_string(),
3821            casted_array.unwrap_err().to_string()
3822        );
3823
3824        let casted_array = cast_with_options(
3825            &array,
3826            &DataType::UInt8,
3827            &CastOptions {
3828                safe: true,
3829                format_options: FormatOptions::default(),
3830            },
3831        );
3832        assert!(casted_array.is_ok());
3833        assert!(casted_array.unwrap().is_null(0));
3834
3835        // overflow test: out of range of max i8
3836        let value_array: Vec<Option<i128>> = vec![Some(24400)];
3837        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3838        let casted_array = cast_with_options(
3839            &array,
3840            &DataType::Int8,
3841            &CastOptions {
3842                safe: false,
3843                format_options: FormatOptions::default(),
3844            },
3845        );
3846        assert_eq!(
3847            "Cast error: value of 244 is out of range Int8".to_string(),
3848            casted_array.unwrap_err().to_string()
3849        );
3850
3851        let casted_array = cast_with_options(
3852            &array,
3853            &DataType::Int8,
3854            &CastOptions {
3855                safe: true,
3856                format_options: FormatOptions::default(),
3857            },
3858        );
3859        assert!(casted_array.is_ok());
3860        assert!(casted_array.unwrap().is_null(0));
3861
3862        // loss the precision: convert decimal to f32、f64
3863        // f32
3864        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3865        let value_array: Vec<Option<i128>> = vec![
3866            Some(125),
3867            Some(225),
3868            Some(325),
3869            None,
3870            Some(525),
3871            Some(112345678),
3872            Some(112345679),
3873        ];
3874        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3875        generate_cast_test_case!(
3876            &array,
3877            Float32Array,
3878            &DataType::Float32,
3879            vec![
3880                Some(1.25_f32),
3881                Some(2.25_f32),
3882                Some(3.25_f32),
3883                None,
3884                Some(5.25_f32),
3885                Some(1_123_456.7_f32),
3886                Some(1_123_456.7_f32)
3887            ]
3888        );
3889
3890        // f64
3891        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3892        let value_array: Vec<Option<i128>> = vec![
3893            Some(125),
3894            Some(225),
3895            Some(325),
3896            None,
3897            Some(525),
3898            Some(112345678901234568),
3899            Some(112345678901234560),
3900        ];
3901        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3902        generate_cast_test_case!(
3903            &array,
3904            Float64Array,
3905            &DataType::Float64,
3906            vec![
3907                Some(1.25_f64),
3908                Some(2.25_f64),
3909                Some(3.25_f64),
3910                None,
3911                Some(5.25_f64),
3912                Some(1_123_456_789_012_345.6_f64),
3913                Some(1_123_456_789_012_345.6_f64),
3914            ]
3915        );
3916    }
3917
3918    #[test]
3919    fn test_cast_decimal256_to_numeric() {
3920        let value_array: Vec<Option<i256>> = vec![
3921            Some(i256::from_i128(125)),
3922            Some(i256::from_i128(225)),
3923            Some(i256::from_i128(325)),
3924            None,
3925            Some(i256::from_i128(525)),
3926        ];
3927        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3928        // u8
3929        generate_cast_test_case!(
3930            &array,
3931            UInt8Array,
3932            &DataType::UInt8,
3933            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3934        );
3935        // u16
3936        generate_cast_test_case!(
3937            &array,
3938            UInt16Array,
3939            &DataType::UInt16,
3940            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3941        );
3942        // u32
3943        generate_cast_test_case!(
3944            &array,
3945            UInt32Array,
3946            &DataType::UInt32,
3947            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3948        );
3949        // u64
3950        generate_cast_test_case!(
3951            &array,
3952            UInt64Array,
3953            &DataType::UInt64,
3954            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3955        );
3956        // i8
3957        generate_cast_test_case!(
3958            &array,
3959            Int8Array,
3960            &DataType::Int8,
3961            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3962        );
3963        // i16
3964        generate_cast_test_case!(
3965            &array,
3966            Int16Array,
3967            &DataType::Int16,
3968            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3969        );
3970        // i32
3971        generate_cast_test_case!(
3972            &array,
3973            Int32Array,
3974            &DataType::Int32,
3975            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3976        );
3977        // i64
3978        generate_cast_test_case!(
3979            &array,
3980            Int64Array,
3981            &DataType::Int64,
3982            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3983        );
3984        // f16
3985        generate_cast_test_case!(
3986            &array,
3987            Float16Array,
3988            &DataType::Float16,
3989            vec![
3990                Some(f16::from_f32(1.25)),
3991                Some(f16::from_f32(2.25)),
3992                Some(f16::from_f32(3.25)),
3993                None,
3994                Some(f16::from_f32(5.25))
3995            ]
3996        );
3997        // f32
3998        generate_cast_test_case!(
3999            &array,
4000            Float32Array,
4001            &DataType::Float32,
4002            vec![
4003                Some(1.25_f32),
4004                Some(2.25_f32),
4005                Some(3.25_f32),
4006                None,
4007                Some(5.25_f32)
4008            ]
4009        );
4010        // f64
4011        generate_cast_test_case!(
4012            &array,
4013            Float64Array,
4014            &DataType::Float64,
4015            vec![
4016                Some(1.25_f64),
4017                Some(2.25_f64),
4018                Some(3.25_f64),
4019                None,
4020                Some(5.25_f64)
4021            ]
4022        );
4023
4024        // overflow test: out of range of max i8
4025        let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128(24400))];
4026        let array = create_decimal256_array(value_array, 38, 2).unwrap();
4027        let casted_array = cast_with_options(
4028            &array,
4029            &DataType::Int8,
4030            &CastOptions {
4031                safe: false,
4032                format_options: FormatOptions::default(),
4033            },
4034        );
4035        assert_eq!(
4036            "Cast error: value of 244 is out of range Int8".to_string(),
4037            casted_array.unwrap_err().to_string()
4038        );
4039
4040        let casted_array = cast_with_options(
4041            &array,
4042            &DataType::Int8,
4043            &CastOptions {
4044                safe: true,
4045                format_options: FormatOptions::default(),
4046            },
4047        );
4048        assert!(casted_array.is_ok());
4049        assert!(casted_array.unwrap().is_null(0));
4050
4051        // loss the precision: convert decimal to f32、f64
4052        // f32
4053        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
4054        let value_array: Vec<Option<i256>> = vec![
4055            Some(i256::from_i128(125)),
4056            Some(i256::from_i128(225)),
4057            Some(i256::from_i128(325)),
4058            None,
4059            Some(i256::from_i128(525)),
4060            Some(i256::from_i128(112345678)),
4061            Some(i256::from_i128(112345679)),
4062        ];
4063        let array = create_decimal256_array(value_array, 76, 2).unwrap();
4064        generate_cast_test_case!(
4065            &array,
4066            Float32Array,
4067            &DataType::Float32,
4068            vec![
4069                Some(1.25_f32),
4070                Some(2.25_f32),
4071                Some(3.25_f32),
4072                None,
4073                Some(5.25_f32),
4074                Some(1_123_456.7_f32),
4075                Some(1_123_456.7_f32)
4076            ]
4077        );
4078
4079        // f64
4080        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
4081        let value_array: Vec<Option<i256>> = vec![
4082            Some(i256::from_i128(125)),
4083            Some(i256::from_i128(225)),
4084            Some(i256::from_i128(325)),
4085            None,
4086            Some(i256::from_i128(525)),
4087            Some(i256::from_i128(112345678901234568)),
4088            Some(i256::from_i128(112345678901234560)),
4089        ];
4090        let array = create_decimal256_array(value_array, 76, 2).unwrap();
4091        generate_cast_test_case!(
4092            &array,
4093            Float64Array,
4094            &DataType::Float64,
4095            vec![
4096                Some(1.25_f64),
4097                Some(2.25_f64),
4098                Some(3.25_f64),
4099                None,
4100                Some(5.25_f64),
4101                Some(1_123_456_789_012_345.6_f64),
4102                Some(1_123_456_789_012_345.6_f64),
4103            ]
4104        );
4105    }
4106
4107    #[test]
4108    fn test_cast_decimal128_to_float16_overflow() {
4109        let array = create_decimal128_array(
4110            vec![
4111                Some(6_550_400_i128),
4112                Some(100_000_000_i128),
4113                Some(-100_000_000_i128),
4114                None,
4115            ],
4116            10,
4117            2,
4118        )
4119        .unwrap();
4120
4121        generate_cast_test_case!(
4122            &array,
4123            Float16Array,
4124            &DataType::Float16,
4125            vec![
4126                Some(f16::from_f64(65504.0)),
4127                Some(f16::INFINITY),
4128                Some(f16::NEG_INFINITY),
4129                None
4130            ]
4131        );
4132    }
4133
4134    #[test]
4135    fn test_cast_decimal256_to_float16_overflow() {
4136        let array = create_decimal256_array(
4137            vec![
4138                Some(i256::from_i128(6_550_400_i128)),
4139                Some(i256::from_i128(100_000_000_i128)),
4140                Some(i256::from_i128(-100_000_000_i128)),
4141                None,
4142            ],
4143            10,
4144            2,
4145        )
4146        .unwrap();
4147
4148        generate_cast_test_case!(
4149            &array,
4150            Float16Array,
4151            &DataType::Float16,
4152            vec![
4153                Some(f16::from_f64(65504.0)),
4154                Some(f16::INFINITY),
4155                Some(f16::NEG_INFINITY),
4156                None
4157            ]
4158        );
4159    }
4160
4161    #[test]
4162    fn test_cast_decimal_to_numeric_negative_scale() {
4163        let value_array: Vec<Option<i256>> = vec![
4164            Some(i256::from_i128(125)),
4165            Some(i256::from_i128(225)),
4166            Some(i256::from_i128(325)),
4167            None,
4168            Some(i256::from_i128(525)),
4169        ];
4170        let array = create_decimal256_array(value_array, 38, -1).unwrap();
4171
4172        generate_cast_test_case!(
4173            &array,
4174            Int64Array,
4175            &DataType::Int64,
4176            vec![Some(1_250), Some(2_250), Some(3_250), None, Some(5_250)]
4177        );
4178
4179        let value_array: Vec<Option<i128>> = vec![Some(12), Some(-12), None];
4180        let array = create_decimal128_array(value_array, 10, -2).unwrap();
4181        generate_cast_test_case!(
4182            &array,
4183            Float16Array,
4184            &DataType::Float16,
4185            vec![
4186                Some(f16::from_f32(1200.0)),
4187                Some(f16::from_f32(-1200.0)),
4188                None
4189            ]
4190        );
4191
4192        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4193        let array = create_decimal32_array(value_array, 8, -2).unwrap();
4194        generate_cast_test_case!(
4195            &array,
4196            Int64Array,
4197            &DataType::Int64,
4198            vec![Some(12_500), Some(22_500), Some(32_500), None, Some(52_500)]
4199        );
4200
4201        let value_array: Vec<Option<i32>> = vec![Some(2), Some(1), None];
4202        let array = create_decimal32_array(value_array, 9, -9).unwrap();
4203        generate_cast_test_case!(
4204            &array,
4205            Int64Array,
4206            &DataType::Int64,
4207            vec![Some(2_000_000_000), Some(1_000_000_000), None]
4208        );
4209
4210        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4211        let array = create_decimal64_array(value_array, 18, -3).unwrap();
4212        generate_cast_test_case!(
4213            &array,
4214            Int64Array,
4215            &DataType::Int64,
4216            vec![
4217                Some(125_000),
4218                Some(225_000),
4219                Some(325_000),
4220                None,
4221                Some(525_000)
4222            ]
4223        );
4224
4225        let value_array: Vec<Option<i64>> = vec![Some(12), Some(34), None];
4226        let array = create_decimal64_array(value_array, 18, -10).unwrap();
4227        generate_cast_test_case!(
4228            &array,
4229            Int64Array,
4230            &DataType::Int64,
4231            vec![Some(120_000_000_000), Some(340_000_000_000), None]
4232        );
4233
4234        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4235        let array = create_decimal128_array(value_array, 38, -4).unwrap();
4236        generate_cast_test_case!(
4237            &array,
4238            Int64Array,
4239            &DataType::Int64,
4240            vec![
4241                Some(1_250_000),
4242                Some(2_250_000),
4243                Some(3_250_000),
4244                None,
4245                Some(5_250_000)
4246            ]
4247        );
4248
4249        let value_array: Vec<Option<i128>> = vec![Some(9), Some(1), None];
4250        let array = create_decimal128_array(value_array, 38, -18).unwrap();
4251        generate_cast_test_case!(
4252            &array,
4253            Int64Array,
4254            &DataType::Int64,
4255            vec![
4256                Some(9_000_000_000_000_000_000),
4257                Some(1_000_000_000_000_000_000),
4258                None
4259            ]
4260        );
4261
4262        let array = create_decimal32_array(vec![Some(999_999_999)], 9, -1).unwrap();
4263        let casted_array = cast_with_options(
4264            &array,
4265            &DataType::Int64,
4266            &CastOptions {
4267                safe: false,
4268                format_options: FormatOptions::default(),
4269            },
4270        );
4271        assert_eq!(
4272            "Arithmetic overflow: Overflow happened on: 999999999 * 10".to_string(),
4273            casted_array.unwrap_err().to_string()
4274        );
4275
4276        let casted_array = cast_with_options(
4277            &array,
4278            &DataType::Int64,
4279            &CastOptions {
4280                safe: true,
4281                format_options: FormatOptions::default(),
4282            },
4283        );
4284        assert!(casted_array.is_ok());
4285        assert!(casted_array.unwrap().is_null(0));
4286
4287        let array = create_decimal64_array(vec![Some(13)], 18, -1).unwrap();
4288        let casted_array = cast_with_options(
4289            &array,
4290            &DataType::Int8,
4291            &CastOptions {
4292                safe: false,
4293                format_options: FormatOptions::default(),
4294            },
4295        );
4296        assert_eq!(
4297            "Cast error: value of 130 is out of range Int8".to_string(),
4298            casted_array.unwrap_err().to_string()
4299        );
4300
4301        let casted_array = cast_with_options(
4302            &array,
4303            &DataType::Int8,
4304            &CastOptions {
4305                safe: true,
4306                format_options: FormatOptions::default(),
4307            },
4308        );
4309        assert!(casted_array.is_ok());
4310        assert!(casted_array.unwrap().is_null(0));
4311    }
4312
4313    #[test]
4314    fn test_cast_numeric_to_decimal128() {
4315        let decimal_type = DataType::Decimal128(38, 6);
4316        // u8, u16, u32, u64
4317        let input_datas = vec![
4318            Arc::new(UInt8Array::from(vec![
4319                Some(1),
4320                Some(2),
4321                Some(3),
4322                None,
4323                Some(5),
4324            ])) as ArrayRef, // u8
4325            Arc::new(UInt16Array::from(vec![
4326                Some(1),
4327                Some(2),
4328                Some(3),
4329                None,
4330                Some(5),
4331            ])) as ArrayRef, // u16
4332            Arc::new(UInt32Array::from(vec![
4333                Some(1),
4334                Some(2),
4335                Some(3),
4336                None,
4337                Some(5),
4338            ])) as ArrayRef, // u32
4339            Arc::new(UInt64Array::from(vec![
4340                Some(1),
4341                Some(2),
4342                Some(3),
4343                None,
4344                Some(5),
4345            ])) as ArrayRef, // u64
4346        ];
4347
4348        for array in input_datas {
4349            generate_cast_test_case!(
4350                &array,
4351                Decimal128Array,
4352                &decimal_type,
4353                vec![
4354                    Some(1000000_i128),
4355                    Some(2000000_i128),
4356                    Some(3000000_i128),
4357                    None,
4358                    Some(5000000_i128)
4359                ]
4360            );
4361        }
4362
4363        // i8, i16, i32, i64
4364        let input_datas = vec![
4365            Arc::new(Int8Array::from(vec![
4366                Some(1),
4367                Some(2),
4368                Some(3),
4369                None,
4370                Some(5),
4371            ])) as ArrayRef, // i8
4372            Arc::new(Int16Array::from(vec![
4373                Some(1),
4374                Some(2),
4375                Some(3),
4376                None,
4377                Some(5),
4378            ])) as ArrayRef, // i16
4379            Arc::new(Int32Array::from(vec![
4380                Some(1),
4381                Some(2),
4382                Some(3),
4383                None,
4384                Some(5),
4385            ])) as ArrayRef, // i32
4386            Arc::new(Int64Array::from(vec![
4387                Some(1),
4388                Some(2),
4389                Some(3),
4390                None,
4391                Some(5),
4392            ])) as ArrayRef, // i64
4393        ];
4394        for array in input_datas {
4395            generate_cast_test_case!(
4396                &array,
4397                Decimal128Array,
4398                &decimal_type,
4399                vec![
4400                    Some(1000000_i128),
4401                    Some(2000000_i128),
4402                    Some(3000000_i128),
4403                    None,
4404                    Some(5000000_i128)
4405                ]
4406            );
4407        }
4408
4409        // test u8 to decimal type with overflow the result type
4410        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4411        let array = UInt8Array::from(vec![1, 2, 3, 4, 100]);
4412        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4413        assert!(casted_array.is_ok());
4414        let array = casted_array.unwrap();
4415        let array: &Decimal128Array = array.as_primitive();
4416        assert!(array.is_null(4));
4417
4418        // test i8 to decimal type with overflow the result type
4419        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4420        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4421        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4422        assert!(casted_array.is_ok());
4423        let array = casted_array.unwrap();
4424        let array: &Decimal128Array = array.as_primitive();
4425        assert!(array.is_null(4));
4426
4427        // test f32 to decimal type
4428        let array = Float32Array::from(vec![
4429            Some(1.1),
4430            Some(2.2),
4431            Some(4.4),
4432            None,
4433            Some(1.123_456_4), // round down
4434            Some(1.123_456_7), // round up
4435        ]);
4436        let array = Arc::new(array) as ArrayRef;
4437        generate_cast_test_case!(
4438            &array,
4439            Decimal128Array,
4440            &decimal_type,
4441            vec![
4442                Some(1100000_i128),
4443                Some(2200000_i128),
4444                Some(4400000_i128),
4445                None,
4446                Some(1123456_i128), // round down
4447                Some(1123457_i128), // round up
4448            ]
4449        );
4450
4451        // test f64 to decimal type
4452        let array = Float64Array::from(vec![
4453            Some(1.1),
4454            Some(2.2),
4455            Some(4.4),
4456            None,
4457            Some(1.123_456_489_123_4),     // round up
4458            Some(1.123_456_789_123_4),     // round up
4459            Some(1.123_456_489_012_345_6), // round down
4460            Some(1.123_456_789_012_345_6), // round up
4461        ]);
4462        generate_cast_test_case!(
4463            &array,
4464            Decimal128Array,
4465            &decimal_type,
4466            vec![
4467                Some(1100000_i128),
4468                Some(2200000_i128),
4469                Some(4400000_i128),
4470                None,
4471                Some(1123456_i128), // round down
4472                Some(1123457_i128), // round up
4473                Some(1123456_i128), // round down
4474                Some(1123457_i128), // round up
4475            ]
4476        );
4477    }
4478
4479    #[test]
4480    fn test_cast_numeric_to_decimal256() {
4481        let decimal_type = DataType::Decimal256(76, 6);
4482        // u8, u16, u32, u64
4483        let input_datas = vec![
4484            Arc::new(UInt8Array::from(vec![
4485                Some(1),
4486                Some(2),
4487                Some(3),
4488                None,
4489                Some(5),
4490            ])) as ArrayRef, // u8
4491            Arc::new(UInt16Array::from(vec![
4492                Some(1),
4493                Some(2),
4494                Some(3),
4495                None,
4496                Some(5),
4497            ])) as ArrayRef, // u16
4498            Arc::new(UInt32Array::from(vec![
4499                Some(1),
4500                Some(2),
4501                Some(3),
4502                None,
4503                Some(5),
4504            ])) as ArrayRef, // u32
4505            Arc::new(UInt64Array::from(vec![
4506                Some(1),
4507                Some(2),
4508                Some(3),
4509                None,
4510                Some(5),
4511            ])) as ArrayRef, // u64
4512        ];
4513
4514        for array in input_datas {
4515            generate_cast_test_case!(
4516                &array,
4517                Decimal256Array,
4518                &decimal_type,
4519                vec![
4520                    Some(i256::from_i128(1000000_i128)),
4521                    Some(i256::from_i128(2000000_i128)),
4522                    Some(i256::from_i128(3000000_i128)),
4523                    None,
4524                    Some(i256::from_i128(5000000_i128))
4525                ]
4526            );
4527        }
4528
4529        // i8, i16, i32, i64
4530        let input_datas = vec![
4531            Arc::new(Int8Array::from(vec![
4532                Some(1),
4533                Some(2),
4534                Some(3),
4535                None,
4536                Some(5),
4537            ])) as ArrayRef, // i8
4538            Arc::new(Int16Array::from(vec![
4539                Some(1),
4540                Some(2),
4541                Some(3),
4542                None,
4543                Some(5),
4544            ])) as ArrayRef, // i16
4545            Arc::new(Int32Array::from(vec![
4546                Some(1),
4547                Some(2),
4548                Some(3),
4549                None,
4550                Some(5),
4551            ])) as ArrayRef, // i32
4552            Arc::new(Int64Array::from(vec![
4553                Some(1),
4554                Some(2),
4555                Some(3),
4556                None,
4557                Some(5),
4558            ])) as ArrayRef, // i64
4559        ];
4560        for array in input_datas {
4561            generate_cast_test_case!(
4562                &array,
4563                Decimal256Array,
4564                &decimal_type,
4565                vec![
4566                    Some(i256::from_i128(1000000_i128)),
4567                    Some(i256::from_i128(2000000_i128)),
4568                    Some(i256::from_i128(3000000_i128)),
4569                    None,
4570                    Some(i256::from_i128(5000000_i128))
4571                ]
4572            );
4573        }
4574
4575        // test i8 to decimal type with overflow the result type
4576        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4577        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4578        let array = Arc::new(array) as ArrayRef;
4579        let casted_array = cast(&array, &DataType::Decimal256(3, 1));
4580        assert!(casted_array.is_ok());
4581        let array = casted_array.unwrap();
4582        let array: &Decimal256Array = array.as_primitive();
4583        assert!(array.is_null(4));
4584
4585        // test f32 to decimal type
4586        let array = Float32Array::from(vec![
4587            Some(1.1),
4588            Some(2.2),
4589            Some(4.4),
4590            None,
4591            Some(1.123_456_4), // round down
4592            Some(1.123_456_7), // round up
4593        ]);
4594        generate_cast_test_case!(
4595            &array,
4596            Decimal256Array,
4597            &decimal_type,
4598            vec![
4599                Some(i256::from_i128(1100000_i128)),
4600                Some(i256::from_i128(2200000_i128)),
4601                Some(i256::from_i128(4400000_i128)),
4602                None,
4603                Some(i256::from_i128(1123456_i128)), // round down
4604                Some(i256::from_i128(1123457_i128)), // round up
4605            ]
4606        );
4607
4608        // test f64 to decimal type
4609        let array = Float64Array::from(vec![
4610            Some(1.1),
4611            Some(2.2),
4612            Some(4.4),
4613            None,
4614            Some(1.123_456_489_123_4),     // round down
4615            Some(1.123_456_789_123_4),     // round up
4616            Some(1.123_456_489_012_345_6), // round down
4617            Some(1.123_456_789_012_345_6), // round up
4618        ]);
4619        generate_cast_test_case!(
4620            &array,
4621            Decimal256Array,
4622            &decimal_type,
4623            vec![
4624                Some(i256::from_i128(1100000_i128)),
4625                Some(i256::from_i128(2200000_i128)),
4626                Some(i256::from_i128(4400000_i128)),
4627                None,
4628                Some(i256::from_i128(1123456_i128)), // round down
4629                Some(i256::from_i128(1123457_i128)), // round up
4630                Some(i256::from_i128(1123456_i128)), // round down
4631                Some(i256::from_i128(1123457_i128)), // round up
4632            ]
4633        );
4634    }
4635
4636    #[test]
4637    fn test_cast_i32_to_f64() {
4638        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4639        let b = cast(&array, &DataType::Float64).unwrap();
4640        let c = b.as_primitive::<Float64Type>();
4641        assert_eq!(5.0, c.value(0));
4642        assert_eq!(6.0, c.value(1));
4643        assert_eq!(7.0, c.value(2));
4644        assert_eq!(8.0, c.value(3));
4645        assert_eq!(9.0, c.value(4));
4646    }
4647
4648    #[test]
4649    fn test_cast_i32_to_u8() {
4650        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4651        let b = cast(&array, &DataType::UInt8).unwrap();
4652        let c = b.as_primitive::<UInt8Type>();
4653        assert!(!c.is_valid(0));
4654        assert_eq!(6, c.value(1));
4655        assert!(!c.is_valid(2));
4656        assert_eq!(8, c.value(3));
4657        // overflows return None
4658        assert!(!c.is_valid(4));
4659    }
4660
4661    #[test]
4662    #[should_panic(expected = "Can't cast value -5 to type UInt8")]
4663    fn test_cast_int32_to_u8_with_error() {
4664        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4665        // overflow with the error
4666        let cast_option = CastOptions {
4667            safe: false,
4668            format_options: FormatOptions::default(),
4669        };
4670        let result = cast_with_options(&array, &DataType::UInt8, &cast_option);
4671        assert!(result.is_err());
4672        result.unwrap();
4673    }
4674
4675    #[test]
4676    fn test_cast_i32_to_u8_sliced() {
4677        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4678        assert_eq!(0, array.offset());
4679        let array = array.slice(2, 3);
4680        let b = cast(&array, &DataType::UInt8).unwrap();
4681        assert_eq!(3, b.len());
4682        let c = b.as_primitive::<UInt8Type>();
4683        assert!(!c.is_valid(0));
4684        assert_eq!(8, c.value(1));
4685        // overflows return None
4686        assert!(!c.is_valid(2));
4687    }
4688
4689    #[test]
4690    fn test_cast_i32_to_i32() {
4691        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4692        let b = cast(&array, &DataType::Int32).unwrap();
4693        let c = b.as_primitive::<Int32Type>();
4694        assert_eq!(5, c.value(0));
4695        assert_eq!(6, c.value(1));
4696        assert_eq!(7, c.value(2));
4697        assert_eq!(8, c.value(3));
4698        assert_eq!(9, c.value(4));
4699    }
4700
4701    #[test]
4702    fn test_cast_i32_to_list_i32() {
4703        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4704        let b = cast(
4705            &array,
4706            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4707        )
4708        .unwrap();
4709        assert_eq!(5, b.len());
4710        let arr = b.as_list::<i32>();
4711        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4712        assert_eq!(1, arr.value_length(0));
4713        assert_eq!(1, arr.value_length(1));
4714        assert_eq!(1, arr.value_length(2));
4715        assert_eq!(1, arr.value_length(3));
4716        assert_eq!(1, arr.value_length(4));
4717        let c = arr.values().as_primitive::<Int32Type>();
4718        assert_eq!(5, c.value(0));
4719        assert_eq!(6, c.value(1));
4720        assert_eq!(7, c.value(2));
4721        assert_eq!(8, c.value(3));
4722        assert_eq!(9, c.value(4));
4723    }
4724
4725    #[test]
4726    fn test_cast_i32_to_list_i32_nullable() {
4727        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), Some(9)]);
4728        let b = cast(
4729            &array,
4730            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4731        )
4732        .unwrap();
4733        assert_eq!(5, b.len());
4734        assert_eq!(0, b.null_count());
4735        let arr = b.as_list::<i32>();
4736        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4737        assert_eq!(1, arr.value_length(0));
4738        assert_eq!(1, arr.value_length(1));
4739        assert_eq!(1, arr.value_length(2));
4740        assert_eq!(1, arr.value_length(3));
4741        assert_eq!(1, arr.value_length(4));
4742
4743        let c = arr.values().as_primitive::<Int32Type>();
4744        assert_eq!(1, c.null_count());
4745        assert_eq!(5, c.value(0));
4746        assert!(!c.is_valid(1));
4747        assert_eq!(7, c.value(2));
4748        assert_eq!(8, c.value(3));
4749        assert_eq!(9, c.value(4));
4750    }
4751
4752    #[test]
4753    fn test_cast_i32_to_list_f64_nullable_sliced() {
4754        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), None, Some(10)]);
4755        let array = array.slice(2, 4);
4756        let b = cast(
4757            &array,
4758            &DataType::List(Arc::new(Field::new_list_field(DataType::Float64, true))),
4759        )
4760        .unwrap();
4761        assert_eq!(4, b.len());
4762        assert_eq!(0, b.null_count());
4763        let arr = b.as_list::<i32>();
4764        assert_eq!(&[0, 1, 2, 3, 4], arr.value_offsets());
4765        assert_eq!(1, arr.value_length(0));
4766        assert_eq!(1, arr.value_length(1));
4767        assert_eq!(1, arr.value_length(2));
4768        assert_eq!(1, arr.value_length(3));
4769        let c = arr.values().as_primitive::<Float64Type>();
4770        assert_eq!(1, c.null_count());
4771        assert_eq!(7.0, c.value(0));
4772        assert_eq!(8.0, c.value(1));
4773        assert!(!c.is_valid(2));
4774        assert_eq!(10.0, c.value(3));
4775    }
4776
4777    #[test]
4778    fn test_cast_int_to_utf8view() {
4779        let inputs = vec![
4780            Arc::new(Int8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4781            Arc::new(Int16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4782            Arc::new(Int32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4783            Arc::new(Int64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4784            Arc::new(UInt8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4785            Arc::new(UInt16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4786            Arc::new(UInt32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4787            Arc::new(UInt64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4788        ];
4789        let expected: ArrayRef = Arc::new(StringViewArray::from(vec![
4790            None,
4791            Some("8"),
4792            Some("9"),
4793            Some("10"),
4794        ]));
4795
4796        for array in inputs {
4797            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4798            let arr = cast(&array, &DataType::Utf8View).unwrap();
4799            assert_eq!(expected.as_ref(), arr.as_ref());
4800        }
4801    }
4802
4803    #[test]
4804    fn test_cast_float_to_utf8view() {
4805        let inputs = vec![
4806            Arc::new(Float16Array::from(vec![
4807                Some(f16::from_f64(1.5)),
4808                Some(f16::from_f64(2.5)),
4809                None,
4810            ])) as ArrayRef,
4811            Arc::new(Float32Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4812            Arc::new(Float64Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4813        ];
4814
4815        let expected: ArrayRef =
4816            Arc::new(StringViewArray::from(vec![Some("1.5"), Some("2.5"), None]));
4817
4818        for array in inputs {
4819            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4820            let arr = cast(&array, &DataType::Utf8View).unwrap();
4821            assert_eq!(expected.as_ref(), arr.as_ref());
4822        }
4823    }
4824
4825    #[test]
4826    fn test_cast_utf8_to_i32() {
4827        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4828        let b = cast(&array, &DataType::Int32).unwrap();
4829        let c = b.as_primitive::<Int32Type>();
4830        assert_eq!(5, c.value(0));
4831        assert_eq!(6, c.value(1));
4832        assert!(!c.is_valid(2));
4833        assert_eq!(8, c.value(3));
4834        assert!(!c.is_valid(4));
4835    }
4836
4837    #[test]
4838    fn test_cast_utf8view_to_i32() {
4839        let array = StringViewArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4840        let b = cast(&array, &DataType::Int32).unwrap();
4841        let c = b.as_primitive::<Int32Type>();
4842        assert_eq!(5, c.value(0));
4843        assert_eq!(6, c.value(1));
4844        assert!(!c.is_valid(2));
4845        assert_eq!(8, c.value(3));
4846        assert!(!c.is_valid(4));
4847    }
4848
4849    #[test]
4850    fn test_cast_utf8view_to_f32() {
4851        let array = StringViewArray::from(vec!["3", "4.56", "seven", "8.9"]);
4852        let b = cast(&array, &DataType::Float32).unwrap();
4853        let c = b.as_primitive::<Float32Type>();
4854        assert_eq!(3.0, c.value(0));
4855        assert_eq!(4.56, c.value(1));
4856        assert!(!c.is_valid(2));
4857        assert_eq!(8.9, c.value(3));
4858    }
4859
4860    #[test]
4861    fn test_cast_string_to_f16() {
4862        let arrays = [
4863            Arc::new(StringViewArray::from(vec!["3", "4.56", "seven", "8.9"])) as ArrayRef,
4864            Arc::new(StringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4865            Arc::new(LargeStringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4866        ];
4867        for array in arrays {
4868            let b = cast(&array, &DataType::Float16).unwrap();
4869            let c = b.as_primitive::<Float16Type>();
4870            assert_eq!(half::f16::from_f32(3.0), c.value(0));
4871            assert_eq!(half::f16::from_f32(4.56), c.value(1));
4872            assert!(!c.is_valid(2));
4873            assert_eq!(half::f16::from_f32(8.9), c.value(3));
4874        }
4875    }
4876
4877    #[test]
4878    fn test_cast_utf8view_to_decimal128() {
4879        let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
4880        let arr = Arc::new(array) as ArrayRef;
4881        generate_cast_test_case!(
4882            &arr,
4883            Decimal128Array,
4884            &DataType::Decimal128(4, 2),
4885            vec![None, Some(400_i128), Some(560_i128), Some(789_i128)]
4886        );
4887    }
4888
4889    #[test]
4890    fn test_cast_with_options_utf8_to_i32() {
4891        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4892        let result = cast_with_options(
4893            &array,
4894            &DataType::Int32,
4895            &CastOptions {
4896                safe: false,
4897                format_options: FormatOptions::default(),
4898            },
4899        );
4900        match result {
4901            Ok(_) => panic!("expected error"),
4902            Err(e) => {
4903                assert!(
4904                    e.to_string()
4905                        .contains("Cast error: Cannot cast string 'seven' to value of Int32 type",),
4906                    "Error: {e}"
4907                )
4908            }
4909        }
4910    }
4911
4912    #[test]
4913    fn test_cast_utf8_to_bool() {
4914        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4915        let casted = cast(&strings, &DataType::Boolean).unwrap();
4916        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4917        assert_eq!(*as_boolean_array(&casted), expected);
4918    }
4919
4920    #[test]
4921    fn test_cast_utf8view_to_bool() {
4922        let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4923        let casted = cast(&strings, &DataType::Boolean).unwrap();
4924        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4925        assert_eq!(*as_boolean_array(&casted), expected);
4926    }
4927
4928    #[test]
4929    fn test_cast_with_options_utf8_to_bool() {
4930        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4931        let casted = cast_with_options(
4932            &strings,
4933            &DataType::Boolean,
4934            &CastOptions {
4935                safe: false,
4936                format_options: FormatOptions::default(),
4937            },
4938        );
4939        match casted {
4940            Ok(_) => panic!("expected error"),
4941            Err(e) => {
4942                assert!(
4943                    e.to_string().contains(
4944                        "Cast error: Cannot cast value 'invalid' to value of Boolean type"
4945                    )
4946                )
4947            }
4948        }
4949    }
4950
4951    #[test]
4952    fn test_cast_bool_to_i32() {
4953        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4954        let b = cast(&array, &DataType::Int32).unwrap();
4955        let c = b.as_primitive::<Int32Type>();
4956        assert_eq!(1, c.value(0));
4957        assert_eq!(0, c.value(1));
4958        assert!(!c.is_valid(2));
4959    }
4960
4961    #[test]
4962    fn test_cast_bool_to_utf8view() {
4963        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4964        let b = cast(&array, &DataType::Utf8View).unwrap();
4965        let c = b.as_any().downcast_ref::<StringViewArray>().unwrap();
4966        assert_eq!("true", c.value(0));
4967        assert_eq!("false", c.value(1));
4968        assert!(!c.is_valid(2));
4969    }
4970
4971    #[test]
4972    fn test_cast_bool_to_utf8() {
4973        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4974        let b = cast(&array, &DataType::Utf8).unwrap();
4975        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
4976        assert_eq!("true", c.value(0));
4977        assert_eq!("false", c.value(1));
4978        assert!(!c.is_valid(2));
4979    }
4980
4981    #[test]
4982    fn test_cast_bool_to_large_utf8() {
4983        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4984        let b = cast(&array, &DataType::LargeUtf8).unwrap();
4985        let c = b.as_any().downcast_ref::<LargeStringArray>().unwrap();
4986        assert_eq!("true", c.value(0));
4987        assert_eq!("false", c.value(1));
4988        assert!(!c.is_valid(2));
4989    }
4990
4991    #[test]
4992    fn test_cast_bool_to_f64() {
4993        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4994        let b = cast(&array, &DataType::Float64).unwrap();
4995        let c = b.as_primitive::<Float64Type>();
4996        assert_eq!(1.0, c.value(0));
4997        assert_eq!(0.0, c.value(1));
4998        assert!(!c.is_valid(2));
4999    }
5000
5001    #[test]
5002    fn test_cast_integer_to_timestamp() {
5003        let array = Int64Array::from(vec![Some(2), Some(10), None]);
5004        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5005
5006        let array = Int8Array::from(vec![Some(2), Some(10), None]);
5007        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5008
5009        assert_eq!(&actual, &expected);
5010
5011        let array = Int16Array::from(vec![Some(2), Some(10), None]);
5012        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5013
5014        assert_eq!(&actual, &expected);
5015
5016        let array = Int32Array::from(vec![Some(2), Some(10), None]);
5017        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5018
5019        assert_eq!(&actual, &expected);
5020
5021        let array = UInt8Array::from(vec![Some(2), Some(10), None]);
5022        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5023
5024        assert_eq!(&actual, &expected);
5025
5026        let array = UInt16Array::from(vec![Some(2), Some(10), None]);
5027        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5028
5029        assert_eq!(&actual, &expected);
5030
5031        let array = UInt32Array::from(vec![Some(2), Some(10), None]);
5032        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5033
5034        assert_eq!(&actual, &expected);
5035
5036        let array = UInt64Array::from(vec![Some(2), Some(10), None]);
5037        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5038
5039        assert_eq!(&actual, &expected);
5040    }
5041
5042    #[test]
5043    fn test_cast_timestamp_to_integer() {
5044        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
5045            .with_timezone("UTC".to_string());
5046        let expected = cast(&array, &DataType::Int64).unwrap();
5047
5048        let actual = cast(&cast(&array, &DataType::Int8).unwrap(), &DataType::Int64).unwrap();
5049        assert_eq!(&actual, &expected);
5050
5051        let actual = cast(&cast(&array, &DataType::Int16).unwrap(), &DataType::Int64).unwrap();
5052        assert_eq!(&actual, &expected);
5053
5054        let actual = cast(&cast(&array, &DataType::Int32).unwrap(), &DataType::Int64).unwrap();
5055        assert_eq!(&actual, &expected);
5056
5057        let actual = cast(&cast(&array, &DataType::UInt8).unwrap(), &DataType::Int64).unwrap();
5058        assert_eq!(&actual, &expected);
5059
5060        let actual = cast(&cast(&array, &DataType::UInt16).unwrap(), &DataType::Int64).unwrap();
5061        assert_eq!(&actual, &expected);
5062
5063        let actual = cast(&cast(&array, &DataType::UInt32).unwrap(), &DataType::Int64).unwrap();
5064        assert_eq!(&actual, &expected);
5065
5066        let actual = cast(&cast(&array, &DataType::UInt64).unwrap(), &DataType::Int64).unwrap();
5067        assert_eq!(&actual, &expected);
5068    }
5069
5070    #[test]
5071    fn test_cast_floating_to_timestamp() {
5072        let array = Int64Array::from(vec![Some(2), Some(10), None]);
5073        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5074
5075        let array = Float16Array::from(vec![
5076            Some(f16::from_f32(2.0)),
5077            Some(f16::from_f32(10.6)),
5078            None,
5079        ]);
5080        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5081
5082        assert_eq!(&actual, &expected);
5083
5084        let array = Float32Array::from(vec![Some(2.0), Some(10.6), None]);
5085        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5086
5087        assert_eq!(&actual, &expected);
5088
5089        let array = Float64Array::from(vec![Some(2.1), Some(10.2), None]);
5090        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5091
5092        assert_eq!(&actual, &expected);
5093    }
5094
5095    #[test]
5096    fn test_cast_timestamp_to_floating() {
5097        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
5098            .with_timezone("UTC".to_string());
5099        let expected = cast(&array, &DataType::Int64).unwrap();
5100
5101        let actual = cast(&cast(&array, &DataType::Float16).unwrap(), &DataType::Int64).unwrap();
5102        assert_eq!(&actual, &expected);
5103
5104        let actual = cast(&cast(&array, &DataType::Float32).unwrap(), &DataType::Int64).unwrap();
5105        assert_eq!(&actual, &expected);
5106
5107        let actual = cast(&cast(&array, &DataType::Float64).unwrap(), &DataType::Int64).unwrap();
5108        assert_eq!(&actual, &expected);
5109    }
5110
5111    #[test]
5112    fn test_cast_decimal_to_timestamp() {
5113        let array = Int64Array::from(vec![Some(2), Some(10), None]);
5114        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5115
5116        let array = Decimal128Array::from(vec![Some(200), Some(1000), None])
5117            .with_precision_and_scale(4, 2)
5118            .unwrap();
5119        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5120
5121        assert_eq!(&actual, &expected);
5122
5123        let array = Decimal256Array::from(vec![
5124            Some(i256::from_i128(2000)),
5125            Some(i256::from_i128(10000)),
5126            None,
5127        ])
5128        .with_precision_and_scale(5, 3)
5129        .unwrap();
5130        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
5131
5132        assert_eq!(&actual, &expected);
5133    }
5134
5135    #[test]
5136    fn test_cast_timestamp_to_decimal() {
5137        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
5138            .with_timezone("UTC".to_string());
5139        let expected = cast(&array, &DataType::Int64).unwrap();
5140
5141        let actual = cast(
5142            &cast(&array, &DataType::Decimal128(5, 2)).unwrap(),
5143            &DataType::Int64,
5144        )
5145        .unwrap();
5146        assert_eq!(&actual, &expected);
5147
5148        let actual = cast(
5149            &cast(&array, &DataType::Decimal256(10, 5)).unwrap(),
5150            &DataType::Int64,
5151        )
5152        .unwrap();
5153        assert_eq!(&actual, &expected);
5154    }
5155
5156    #[test]
5157    fn test_cast_list_i32_to_list_u16() {
5158        let values = vec![
5159            Some(vec![Some(0), Some(0), Some(0)]),
5160            Some(vec![Some(-1), Some(-2), Some(-1)]),
5161            Some(vec![Some(2), Some(100000000)]),
5162        ];
5163        let list_array = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
5164
5165        let target_type = DataType::List(Arc::new(Field::new("item", DataType::UInt16, true)));
5166        assert!(can_cast_types(list_array.data_type(), &target_type));
5167        let cast_array = cast(&list_array, &target_type).unwrap();
5168
5169        // For the ListArray itself, there are no null values (as there were no nulls when they went in)
5170        //
5171        // 3 negative values should get lost when casting to unsigned,
5172        // 1 value should overflow
5173        assert_eq!(0, cast_array.null_count());
5174
5175        // offsets should be the same
5176        let array = cast_array.as_list::<i32>();
5177        assert_eq!(list_array.value_offsets(), array.value_offsets());
5178
5179        assert_eq!(DataType::UInt16, array.value_type());
5180        assert_eq!(3, array.value_length(0));
5181        assert_eq!(3, array.value_length(1));
5182        assert_eq!(2, array.value_length(2));
5183
5184        // expect 4 nulls: negative numbers and overflow
5185        let u16arr = array.values().as_primitive::<UInt16Type>();
5186        assert_eq!(4, u16arr.null_count());
5187
5188        // expect 4 nulls: negative numbers and overflow
5189        let expected: UInt16Array =
5190            vec![Some(0), Some(0), Some(0), None, None, None, Some(2), None]
5191                .into_iter()
5192                .collect();
5193
5194        assert_eq!(u16arr, &expected);
5195    }
5196
5197    #[test]
5198    fn test_cast_list_i32_to_list_timestamp() {
5199        // Construct a value array
5200        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 8, 100000000]).into_data();
5201
5202        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 9]);
5203
5204        // Construct a list array from the above two
5205        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
5206        let list_data = ArrayData::builder(list_data_type)
5207            .len(3)
5208            .add_buffer(value_offsets)
5209            .add_child_data(value_data)
5210            .build()
5211            .unwrap();
5212        let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
5213
5214        let actual = cast(
5215            &list_array,
5216            &DataType::List(Arc::new(Field::new_list_field(
5217                DataType::Timestamp(TimeUnit::Microsecond, None),
5218                true,
5219            ))),
5220        )
5221        .unwrap();
5222
5223        let expected = cast(
5224            &cast(
5225                &list_array,
5226                &DataType::List(Arc::new(Field::new_list_field(DataType::Int64, true))),
5227            )
5228            .unwrap(),
5229            &DataType::List(Arc::new(Field::new_list_field(
5230                DataType::Timestamp(TimeUnit::Microsecond, None),
5231                true,
5232            ))),
5233        )
5234        .unwrap();
5235
5236        assert_eq!(&actual, &expected);
5237    }
5238
5239    #[test]
5240    fn test_cast_date32_to_date64() {
5241        let a = Date32Array::from(vec![10000, 17890]);
5242        let array = Arc::new(a) as ArrayRef;
5243        let b = cast(&array, &DataType::Date64).unwrap();
5244        let c = b.as_primitive::<Date64Type>();
5245        assert_eq!(864000000000, c.value(0));
5246        assert_eq!(1545696000000, c.value(1));
5247    }
5248
5249    #[test]
5250    fn test_cast_date64_to_date32() {
5251        let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5252        let array = Arc::new(a) as ArrayRef;
5253        let b = cast(&array, &DataType::Date32).unwrap();
5254        let c = b.as_primitive::<Date32Type>();
5255        assert_eq!(10000, c.value(0));
5256        assert_eq!(17890, c.value(1));
5257        assert!(c.is_null(2));
5258    }
5259
5260    #[test]
5261    fn test_cast_date64_to_date32_overflow() {
5262        let a = Date64Array::from(vec![i64::MAX]);
5263        let array = Arc::new(a) as ArrayRef;
5264
5265        let b = cast(&array, &DataType::Date32).unwrap();
5266        let c = b.as_primitive::<Date32Type>();
5267        assert!(c.is_null(0));
5268
5269        let options = CastOptions {
5270            safe: false,
5271            ..Default::default()
5272        };
5273        let err = cast_with_options(&array, &DataType::Date32, &options).unwrap_err();
5274        assert!(
5275            err.to_string().contains("Cannot cast Date64 value"),
5276            "{err}"
5277        );
5278    }
5279
5280    #[test]
5281    fn test_cast_string_to_integral_overflow() {
5282        let str = Arc::new(StringArray::from(vec![
5283            Some("123"),
5284            Some("-123"),
5285            Some("86374"),
5286            None,
5287        ])) as ArrayRef;
5288
5289        let options = CastOptions {
5290            safe: true,
5291            format_options: FormatOptions::default(),
5292        };
5293        let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
5294        let expected =
5295            Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
5296        assert_eq!(&res, &expected);
5297    }
5298
5299    #[test]
5300    fn test_cast_string_to_timestamp() {
5301        let a0 = Arc::new(StringViewArray::from(vec![
5302            Some("2020-09-08T12:00:00.123456789+00:00"),
5303            Some("Not a valid date"),
5304            None,
5305        ])) as ArrayRef;
5306        let a1 = Arc::new(StringArray::from(vec![
5307            Some("2020-09-08T12:00:00.123456789+00:00"),
5308            Some("Not a valid date"),
5309            None,
5310        ])) as ArrayRef;
5311        let a2 = Arc::new(LargeStringArray::from(vec![
5312            Some("2020-09-08T12:00:00.123456789+00:00"),
5313            Some("Not a valid date"),
5314            None,
5315        ])) as ArrayRef;
5316        for array in &[a0, a1, a2] {
5317            for time_unit in &[
5318                TimeUnit::Second,
5319                TimeUnit::Millisecond,
5320                TimeUnit::Microsecond,
5321                TimeUnit::Nanosecond,
5322            ] {
5323                let to_type = DataType::Timestamp(*time_unit, None);
5324                let b = cast(array, &to_type).unwrap();
5325
5326                match time_unit {
5327                    TimeUnit::Second => {
5328                        let c = b.as_primitive::<TimestampSecondType>();
5329                        assert_eq!(1599566400, c.value(0));
5330                        assert!(c.is_null(1));
5331                        assert!(c.is_null(2));
5332                    }
5333                    TimeUnit::Millisecond => {
5334                        let c = b
5335                            .as_any()
5336                            .downcast_ref::<TimestampMillisecondArray>()
5337                            .unwrap();
5338                        assert_eq!(1599566400123, c.value(0));
5339                        assert!(c.is_null(1));
5340                        assert!(c.is_null(2));
5341                    }
5342                    TimeUnit::Microsecond => {
5343                        let c = b
5344                            .as_any()
5345                            .downcast_ref::<TimestampMicrosecondArray>()
5346                            .unwrap();
5347                        assert_eq!(1599566400123456, c.value(0));
5348                        assert!(c.is_null(1));
5349                        assert!(c.is_null(2));
5350                    }
5351                    TimeUnit::Nanosecond => {
5352                        let c = b
5353                            .as_any()
5354                            .downcast_ref::<TimestampNanosecondArray>()
5355                            .unwrap();
5356                        assert_eq!(1599566400123456789, c.value(0));
5357                        assert!(c.is_null(1));
5358                        assert!(c.is_null(2));
5359                    }
5360                }
5361
5362                let options = CastOptions {
5363                    safe: false,
5364                    format_options: FormatOptions::default(),
5365                };
5366                let err = cast_with_options(array, &to_type, &options).unwrap_err();
5367                assert_eq!(
5368                    err.to_string(),
5369                    "Parser error: Error parsing timestamp from 'Not a valid date': error parsing date"
5370                );
5371            }
5372        }
5373    }
5374
5375    #[test]
5376    fn test_cast_string_to_timestamp_overflow() {
5377        let array = StringArray::from(vec!["9800-09-08T12:00:00.123456789"]);
5378        let result = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5379        let result = result.as_primitive::<TimestampSecondType>();
5380        assert_eq!(result.values(), &[247112596800]);
5381    }
5382
5383    #[test]
5384    fn test_cast_string_to_date32() {
5385        let a0 = Arc::new(StringViewArray::from(vec![
5386            Some("2018-12-25"),
5387            Some("Not a valid date"),
5388            None,
5389        ])) as ArrayRef;
5390        let a1 = Arc::new(StringArray::from(vec![
5391            Some("2018-12-25"),
5392            Some("Not a valid date"),
5393            None,
5394        ])) as ArrayRef;
5395        let a2 = Arc::new(LargeStringArray::from(vec![
5396            Some("2018-12-25"),
5397            Some("Not a valid date"),
5398            None,
5399        ])) as ArrayRef;
5400        for array in &[a0, a1, a2] {
5401            let to_type = DataType::Date32;
5402            let b = cast(array, &to_type).unwrap();
5403            let c = b.as_primitive::<Date32Type>();
5404            assert_eq!(17890, c.value(0));
5405            assert!(c.is_null(1));
5406            assert!(c.is_null(2));
5407
5408            let options = CastOptions {
5409                safe: false,
5410                format_options: FormatOptions::default(),
5411            };
5412            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5413            assert_eq!(
5414                err.to_string(),
5415                "Cast error: Cannot cast string 'Not a valid date' to value of Date32 type"
5416            );
5417        }
5418    }
5419
5420    #[test]
5421    fn test_cast_string_with_large_date_to_date32() {
5422        let array = Arc::new(StringArray::from(vec![
5423            Some("+10999-12-31"),
5424            Some("-0010-02-28"),
5425            Some("0010-02-28"),
5426            Some("0000-01-01"),
5427            Some("-0000-01-01"),
5428            Some("-0001-01-01"),
5429        ])) as ArrayRef;
5430        let to_type = DataType::Date32;
5431        let options = CastOptions {
5432            safe: false,
5433            format_options: FormatOptions::default(),
5434        };
5435        let b = cast_with_options(&array, &to_type, &options).unwrap();
5436        let c = b.as_primitive::<Date32Type>();
5437        assert_eq!(3298139, c.value(0)); // 10999-12-31
5438        assert_eq!(-723122, c.value(1)); // -0010-02-28
5439        assert_eq!(-715817, c.value(2)); // 0010-02-28
5440        assert_eq!(c.value(3), c.value(4)); // Expect 0000-01-01 and -0000-01-01 to be parsed the same
5441        assert_eq!(-719528, c.value(3)); // 0000-01-01
5442        assert_eq!(-719528, c.value(4)); // -0000-01-01
5443        assert_eq!(-719893, c.value(5)); // -0001-01-01
5444    }
5445
5446    #[test]
5447    fn test_cast_invalid_string_with_large_date_to_date32() {
5448        // Large dates need to be prefixed with a + or - sign, otherwise they are not parsed correctly
5449        let array = Arc::new(StringArray::from(vec![Some("10999-12-31")])) as ArrayRef;
5450        let to_type = DataType::Date32;
5451        let options = CastOptions {
5452            safe: false,
5453            format_options: FormatOptions::default(),
5454        };
5455        let err = cast_with_options(&array, &to_type, &options).unwrap_err();
5456        assert_eq!(
5457            err.to_string(),
5458            "Cast error: Cannot cast string '10999-12-31' to value of Date32 type"
5459        );
5460    }
5461
5462    #[test]
5463    fn test_cast_string_format_yyyymmdd_to_date32() {
5464        let a0 = Arc::new(StringViewArray::from(vec![
5465            Some("2020-12-25"),
5466            Some("20201117"),
5467        ])) as ArrayRef;
5468        let a1 = Arc::new(StringArray::from(vec![
5469            Some("2020-12-25"),
5470            Some("20201117"),
5471        ])) as ArrayRef;
5472        let a2 = Arc::new(LargeStringArray::from(vec![
5473            Some("2020-12-25"),
5474            Some("20201117"),
5475        ])) as ArrayRef;
5476
5477        for array in &[a0, a1, a2] {
5478            let to_type = DataType::Date32;
5479            let options = CastOptions {
5480                safe: false,
5481                format_options: FormatOptions::default(),
5482            };
5483            let result = cast_with_options(&array, &to_type, &options).unwrap();
5484            let c = result.as_primitive::<Date32Type>();
5485            assert_eq!(
5486                chrono::NaiveDate::from_ymd_opt(2020, 12, 25),
5487                c.value_as_date(0)
5488            );
5489            assert_eq!(
5490                chrono::NaiveDate::from_ymd_opt(2020, 11, 17),
5491                c.value_as_date(1)
5492            );
5493        }
5494    }
5495
5496    #[test]
5497    fn test_cast_string_to_time32second() {
5498        let a0 = Arc::new(StringViewArray::from(vec![
5499            Some("08:08:35.091323414"),
5500            Some("08:08:60.091323414"), // leap second
5501            Some("08:08:61.091323414"), // not valid
5502            Some("Not a valid time"),
5503            None,
5504        ])) as ArrayRef;
5505        let a1 = Arc::new(StringArray::from(vec![
5506            Some("08:08:35.091323414"),
5507            Some("08:08:60.091323414"), // leap second
5508            Some("08:08:61.091323414"), // not valid
5509            Some("Not a valid time"),
5510            None,
5511        ])) as ArrayRef;
5512        let a2 = Arc::new(LargeStringArray::from(vec![
5513            Some("08:08:35.091323414"),
5514            Some("08:08:60.091323414"), // leap second
5515            Some("08:08:61.091323414"), // not valid
5516            Some("Not a valid time"),
5517            None,
5518        ])) as ArrayRef;
5519        for array in &[a0, a1, a2] {
5520            let to_type = DataType::Time32(TimeUnit::Second);
5521            let b = cast(array, &to_type).unwrap();
5522            let c = b.as_primitive::<Time32SecondType>();
5523            assert_eq!(29315, c.value(0));
5524            assert_eq!(29340, c.value(1));
5525            assert!(c.is_null(2));
5526            assert!(c.is_null(3));
5527            assert!(c.is_null(4));
5528
5529            let options = CastOptions {
5530                safe: false,
5531                format_options: FormatOptions::default(),
5532            };
5533            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5534            assert_eq!(
5535                err.to_string(),
5536                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(s) type"
5537            );
5538        }
5539    }
5540
5541    #[test]
5542    fn test_cast_string_to_time32millisecond() {
5543        let a0 = Arc::new(StringViewArray::from(vec![
5544            Some("08:08:35.091323414"),
5545            Some("08:08:60.091323414"), // leap second
5546            Some("08:08:61.091323414"), // not valid
5547            Some("Not a valid time"),
5548            None,
5549        ])) as ArrayRef;
5550        let a1 = Arc::new(StringArray::from(vec![
5551            Some("08:08:35.091323414"),
5552            Some("08:08:60.091323414"), // leap second
5553            Some("08:08:61.091323414"), // not valid
5554            Some("Not a valid time"),
5555            None,
5556        ])) as ArrayRef;
5557        let a2 = Arc::new(LargeStringArray::from(vec![
5558            Some("08:08:35.091323414"),
5559            Some("08:08:60.091323414"), // leap second
5560            Some("08:08:61.091323414"), // not valid
5561            Some("Not a valid time"),
5562            None,
5563        ])) as ArrayRef;
5564        for array in &[a0, a1, a2] {
5565            let to_type = DataType::Time32(TimeUnit::Millisecond);
5566            let b = cast(array, &to_type).unwrap();
5567            let c = b.as_primitive::<Time32MillisecondType>();
5568            assert_eq!(29315091, c.value(0));
5569            assert_eq!(29340091, c.value(1));
5570            assert!(c.is_null(2));
5571            assert!(c.is_null(3));
5572            assert!(c.is_null(4));
5573
5574            let options = CastOptions {
5575                safe: false,
5576                format_options: FormatOptions::default(),
5577            };
5578            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5579            assert_eq!(
5580                err.to_string(),
5581                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(ms) type"
5582            );
5583        }
5584    }
5585
5586    #[test]
5587    fn test_cast_string_to_time64microsecond() {
5588        let a0 = Arc::new(StringViewArray::from(vec![
5589            Some("08:08:35.091323414"),
5590            Some("Not a valid time"),
5591            None,
5592        ])) as ArrayRef;
5593        let a1 = Arc::new(StringArray::from(vec![
5594            Some("08:08:35.091323414"),
5595            Some("Not a valid time"),
5596            None,
5597        ])) as ArrayRef;
5598        let a2 = Arc::new(LargeStringArray::from(vec![
5599            Some("08:08:35.091323414"),
5600            Some("Not a valid time"),
5601            None,
5602        ])) as ArrayRef;
5603        for array in &[a0, a1, a2] {
5604            let to_type = DataType::Time64(TimeUnit::Microsecond);
5605            let b = cast(array, &to_type).unwrap();
5606            let c = b.as_primitive::<Time64MicrosecondType>();
5607            assert_eq!(29315091323, c.value(0));
5608            assert!(c.is_null(1));
5609            assert!(c.is_null(2));
5610
5611            let options = CastOptions {
5612                safe: false,
5613                format_options: FormatOptions::default(),
5614            };
5615            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5616            assert_eq!(
5617                err.to_string(),
5618                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(µs) type"
5619            );
5620        }
5621    }
5622
5623    #[test]
5624    fn test_cast_string_to_time64nanosecond() {
5625        let a0 = Arc::new(StringViewArray::from(vec![
5626            Some("08:08:35.091323414"),
5627            Some("Not a valid time"),
5628            None,
5629        ])) as ArrayRef;
5630        let a1 = Arc::new(StringArray::from(vec![
5631            Some("08:08:35.091323414"),
5632            Some("Not a valid time"),
5633            None,
5634        ])) as ArrayRef;
5635        let a2 = Arc::new(LargeStringArray::from(vec![
5636            Some("08:08:35.091323414"),
5637            Some("Not a valid time"),
5638            None,
5639        ])) as ArrayRef;
5640        for array in &[a0, a1, a2] {
5641            let to_type = DataType::Time64(TimeUnit::Nanosecond);
5642            let b = cast(array, &to_type).unwrap();
5643            let c = b.as_primitive::<Time64NanosecondType>();
5644            assert_eq!(29315091323414, c.value(0));
5645            assert!(c.is_null(1));
5646            assert!(c.is_null(2));
5647
5648            let options = CastOptions {
5649                safe: false,
5650                format_options: FormatOptions::default(),
5651            };
5652            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5653            assert_eq!(
5654                err.to_string(),
5655                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(ns) type"
5656            );
5657        }
5658    }
5659
5660    #[test]
5661    fn test_cast_string_to_date64() {
5662        let a0 = Arc::new(StringViewArray::from(vec![
5663            Some("2020-09-08T12:00:00"),
5664            Some("Not a valid date"),
5665            None,
5666        ])) as ArrayRef;
5667        let a1 = Arc::new(StringArray::from(vec![
5668            Some("2020-09-08T12:00:00"),
5669            Some("Not a valid date"),
5670            None,
5671        ])) as ArrayRef;
5672        let a2 = Arc::new(LargeStringArray::from(vec![
5673            Some("2020-09-08T12:00:00"),
5674            Some("Not a valid date"),
5675            None,
5676        ])) as ArrayRef;
5677        for array in &[a0, a1, a2] {
5678            let to_type = DataType::Date64;
5679            let b = cast(array, &to_type).unwrap();
5680            let c = b.as_primitive::<Date64Type>();
5681            assert_eq!(1599566400000, c.value(0));
5682            assert!(c.is_null(1));
5683            assert!(c.is_null(2));
5684
5685            let options = CastOptions {
5686                safe: false,
5687                format_options: FormatOptions::default(),
5688            };
5689            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5690            assert_eq!(
5691                err.to_string(),
5692                "Cast error: Cannot cast string 'Not a valid date' to value of Date64 type"
5693            );
5694        }
5695    }
5696
5697    macro_rules! test_safe_string_to_interval {
5698        ($data_vec:expr, $interval_unit:expr, $array_ty:ty, $expect_vec:expr) => {
5699            let source_string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5700
5701            let options = CastOptions {
5702                safe: true,
5703                format_options: FormatOptions::default(),
5704            };
5705
5706            let target_interval_array = cast_with_options(
5707                &source_string_array.clone(),
5708                &DataType::Interval($interval_unit),
5709                &options,
5710            )
5711            .unwrap()
5712            .as_any()
5713            .downcast_ref::<$array_ty>()
5714            .unwrap()
5715            .clone() as $array_ty;
5716
5717            let target_string_array =
5718                cast_with_options(&target_interval_array, &DataType::Utf8, &options)
5719                    .unwrap()
5720                    .as_any()
5721                    .downcast_ref::<StringArray>()
5722                    .unwrap()
5723                    .clone();
5724
5725            let expect_string_array = StringArray::from($expect_vec);
5726
5727            assert_eq!(target_string_array, expect_string_array);
5728
5729            let target_large_string_array =
5730                cast_with_options(&target_interval_array, &DataType::LargeUtf8, &options)
5731                    .unwrap()
5732                    .as_any()
5733                    .downcast_ref::<LargeStringArray>()
5734                    .unwrap()
5735                    .clone();
5736
5737            let expect_large_string_array = LargeStringArray::from($expect_vec);
5738
5739            assert_eq!(target_large_string_array, expect_large_string_array);
5740        };
5741    }
5742
5743    #[test]
5744    fn test_cast_string_to_interval_year_month() {
5745        test_safe_string_to_interval!(
5746            vec![
5747                Some("1 year 1 month"),
5748                Some("1.5 years 13 month"),
5749                Some("30 days"),
5750                Some("31 days"),
5751                Some("2 months 31 days"),
5752                Some("2 months 31 days 1 second"),
5753                Some("foobar"),
5754            ],
5755            IntervalUnit::YearMonth,
5756            IntervalYearMonthArray,
5757            vec![
5758                Some("1 years 1 mons"),
5759                Some("2 years 7 mons"),
5760                None,
5761                None,
5762                None,
5763                None,
5764                None,
5765            ]
5766        );
5767    }
5768
5769    #[test]
5770    fn test_cast_string_to_interval_day_time() {
5771        test_safe_string_to_interval!(
5772            vec![
5773                Some("1 year 1 month"),
5774                Some("1.5 years 13 month"),
5775                Some("30 days"),
5776                Some("1 day 2 second 3.5 milliseconds"),
5777                Some("foobar"),
5778            ],
5779            IntervalUnit::DayTime,
5780            IntervalDayTimeArray,
5781            vec![
5782                Some("390 days"),
5783                Some("930 days"),
5784                Some("30 days"),
5785                None,
5786                None,
5787            ]
5788        );
5789    }
5790
5791    #[test]
5792    fn test_cast_string_to_interval_month_day_nano() {
5793        test_safe_string_to_interval!(
5794            vec![
5795                Some("1 year 1 month 1 day"),
5796                None,
5797                Some("1.5 years 13 month 35 days 1.4 milliseconds"),
5798                Some("3 days"),
5799                Some("8 seconds"),
5800                None,
5801                Some("1 day 29800 milliseconds"),
5802                Some("3 months 1 second"),
5803                Some("6 minutes 120 second"),
5804                Some("2 years 39 months 9 days 19 hours 1 minute 83 seconds 399222 milliseconds"),
5805                Some("foobar"),
5806            ],
5807            IntervalUnit::MonthDayNano,
5808            IntervalMonthDayNanoArray,
5809            vec![
5810                Some("13 mons 1 days"),
5811                None,
5812                Some("31 mons 35 days 0.001400000 secs"),
5813                Some("3 days"),
5814                Some("8.000000000 secs"),
5815                None,
5816                Some("1 days 29.800000000 secs"),
5817                Some("3 mons 1.000000000 secs"),
5818                Some("8 mins"),
5819                Some("63 mons 9 days 19 hours 9 mins 2.222000000 secs"),
5820                None,
5821            ]
5822        );
5823    }
5824
5825    macro_rules! test_unsafe_string_to_interval_err {
5826        ($data_vec:expr, $interval_unit:expr, $error_msg:expr) => {
5827            let string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5828            let options = CastOptions {
5829                safe: false,
5830                format_options: FormatOptions::default(),
5831            };
5832            let arrow_err = cast_with_options(
5833                &string_array.clone(),
5834                &DataType::Interval($interval_unit),
5835                &options,
5836            )
5837            .unwrap_err();
5838            assert_eq!($error_msg, arrow_err.to_string());
5839        };
5840    }
5841
5842    #[test]
5843    fn test_cast_string_to_interval_err() {
5844        test_unsafe_string_to_interval_err!(
5845            vec![Some("foobar")],
5846            IntervalUnit::YearMonth,
5847            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5848        );
5849        test_unsafe_string_to_interval_err!(
5850            vec![Some("foobar")],
5851            IntervalUnit::DayTime,
5852            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5853        );
5854        test_unsafe_string_to_interval_err!(
5855            vec![Some("foobar")],
5856            IntervalUnit::MonthDayNano,
5857            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5858        );
5859        test_unsafe_string_to_interval_err!(
5860            vec![Some("2 months 31 days 1 second")],
5861            IntervalUnit::YearMonth,
5862            r#"Cast error: Cannot cast 2 months 31 days 1 second to IntervalYearMonth. Only year and month fields are allowed."#
5863        );
5864        test_unsafe_string_to_interval_err!(
5865            vec![Some("1 day 1.5 milliseconds")],
5866            IntervalUnit::DayTime,
5867            r#"Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"#
5868        );
5869
5870        // overflow
5871        test_unsafe_string_to_interval_err!(
5872            vec![Some(format!(
5873                "{} century {} year {} month",
5874                i64::MAX - 2,
5875                i64::MAX - 2,
5876                i64::MAX - 2
5877            ))],
5878            IntervalUnit::DayTime,
5879            format!(
5880                "Arithmetic overflow: Overflow happened on: {} * 100",
5881                i64::MAX - 2
5882            )
5883        );
5884        test_unsafe_string_to_interval_err!(
5885            vec![Some(format!(
5886                "{} year {} month {} day",
5887                i64::MAX - 2,
5888                i64::MAX - 2,
5889                i64::MAX - 2
5890            ))],
5891            IntervalUnit::MonthDayNano,
5892            format!(
5893                "Arithmetic overflow: Overflow happened on: {} * 12",
5894                i64::MAX - 2
5895            )
5896        );
5897    }
5898
5899    #[test]
5900    fn test_cast_binary_to_fixed_size_binary() {
5901        let bytes_1 = "Hiiii".as_bytes();
5902        let bytes_2 = "Hello".as_bytes();
5903
5904        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5905        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5906        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5907
5908        let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
5909        let down_cast = array_ref
5910            .as_any()
5911            .downcast_ref::<FixedSizeBinaryArray>()
5912            .unwrap();
5913        assert_eq!(bytes_1, down_cast.value(0));
5914        assert_eq!(bytes_2, down_cast.value(1));
5915        assert!(down_cast.is_null(2));
5916
5917        let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
5918        let down_cast = array_ref
5919            .as_any()
5920            .downcast_ref::<FixedSizeBinaryArray>()
5921            .unwrap();
5922        assert_eq!(bytes_1, down_cast.value(0));
5923        assert_eq!(bytes_2, down_cast.value(1));
5924        assert!(down_cast.is_null(2));
5925
5926        // test error cases when the length of binary are not same
5927        let bytes_1 = "Hi".as_bytes();
5928        let bytes_2 = "Hello".as_bytes();
5929
5930        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5931        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5932        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5933
5934        let array_ref = cast_with_options(
5935            &a1,
5936            &DataType::FixedSizeBinary(5),
5937            &CastOptions {
5938                safe: false,
5939                format_options: FormatOptions::default(),
5940            },
5941        );
5942        assert!(array_ref.is_err());
5943
5944        let array_ref = cast_with_options(
5945            &a2,
5946            &DataType::FixedSizeBinary(5),
5947            &CastOptions {
5948                safe: false,
5949                format_options: FormatOptions::default(),
5950            },
5951        );
5952        assert!(array_ref.is_err());
5953    }
5954
5955    #[test]
5956    fn test_fixed_size_binary_to_binary() {
5957        let bytes_1 = "Hiiii".as_bytes();
5958        let bytes_2 = "Hello".as_bytes();
5959
5960        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5961        let a1 = Arc::new(FixedSizeBinaryArray::try_from(binary_data.clone()).unwrap()) as ArrayRef;
5962
5963        let array_ref = cast(&a1, &DataType::Binary).unwrap();
5964        let down_cast = array_ref.as_binary::<i32>();
5965        assert_eq!(bytes_1, down_cast.value(0));
5966        assert_eq!(bytes_2, down_cast.value(1));
5967        assert!(down_cast.is_null(2));
5968
5969        let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
5970        let down_cast = array_ref.as_binary::<i64>();
5971        assert_eq!(bytes_1, down_cast.value(0));
5972        assert_eq!(bytes_2, down_cast.value(1));
5973        assert!(down_cast.is_null(2));
5974
5975        let array_ref = cast(&a1, &DataType::BinaryView).unwrap();
5976        let down_cast = array_ref.as_binary_view();
5977        assert_eq!(bytes_1, down_cast.value(0));
5978        assert_eq!(bytes_2, down_cast.value(1));
5979        assert!(down_cast.is_null(2));
5980    }
5981
5982    #[test]
5983    fn test_fixed_size_binary_to_dictionary() {
5984        let bytes_1 = "Hiiii".as_bytes();
5985        let bytes_2 = "Hello".as_bytes();
5986
5987        let binary_data = vec![Some(bytes_1), Some(bytes_2), Some(bytes_1), None];
5988        let a1 = Arc::new(FixedSizeBinaryArray::try_from(binary_data.clone()).unwrap()) as ArrayRef;
5989
5990        let cast_type = DataType::Dictionary(
5991            Box::new(DataType::Int8),
5992            Box::new(DataType::FixedSizeBinary(5)),
5993        );
5994        let cast_array = cast(&a1, &cast_type).unwrap();
5995        assert_eq!(cast_array.data_type(), &cast_type);
5996        assert_eq!(
5997            array_to_strings(&cast_array),
5998            vec!["4869696969", "48656c6c6f", "4869696969", "null"]
5999        );
6000        // dictionary should only have two distinct values
6001        let dict_array = cast_array.as_dictionary::<Int8Type>();
6002        assert_eq!(dict_array.values().len(), 2);
6003    }
6004
6005    #[test]
6006    fn test_binary_to_dictionary() {
6007        let mut builder = GenericBinaryBuilder::<i32>::new();
6008        builder.append_value(b"hello");
6009        builder.append_value(b"hiiii");
6010        builder.append_value(b"hiiii"); // duplicate
6011        builder.append_null();
6012        builder.append_value(b"rustt");
6013
6014        let a1 = builder.finish();
6015
6016        let cast_type = DataType::Dictionary(
6017            Box::new(DataType::Int8),
6018            Box::new(DataType::FixedSizeBinary(5)),
6019        );
6020        let cast_array = cast(&a1, &cast_type).unwrap();
6021        assert_eq!(cast_array.data_type(), &cast_type);
6022        assert_eq!(
6023            array_to_strings(&cast_array),
6024            vec![
6025                "68656c6c6f",
6026                "6869696969",
6027                "6869696969",
6028                "null",
6029                "7275737474"
6030            ]
6031        );
6032        // dictionary should only have three distinct values
6033        let dict_array = cast_array.as_dictionary::<Int8Type>();
6034        assert_eq!(dict_array.values().len(), 3);
6035    }
6036
6037    #[test]
6038    fn test_cast_string_array_to_dict_utf8_view() {
6039        let array = StringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
6040
6041        let cast_type =
6042            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6043        assert!(can_cast_types(array.data_type(), &cast_type));
6044        let cast_array = cast(&array, &cast_type).unwrap();
6045        assert_eq!(cast_array.data_type(), &cast_type);
6046
6047        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6048        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6049        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
6050
6051        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6052        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6053        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6054
6055        let keys = dict_array.keys();
6056        assert!(keys.is_null(1));
6057        assert_eq!(keys.value(0), keys.value(3));
6058        assert_ne!(keys.value(0), keys.value(2));
6059    }
6060
6061    #[test]
6062    fn test_cast_string_array_to_dict_utf8_view_null_vs_literal_null() {
6063        let array = StringArray::from(vec![Some("one"), None, Some("null"), Some("one")]);
6064
6065        let cast_type =
6066            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6067        assert!(can_cast_types(array.data_type(), &cast_type));
6068        let cast_array = cast(&array, &cast_type).unwrap();
6069        assert_eq!(cast_array.data_type(), &cast_type);
6070
6071        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6072        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6073        assert_eq!(dict_array.values().len(), 2);
6074
6075        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6076        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6077        assert_eq!(actual, vec![Some("one"), None, Some("null"), Some("one")]);
6078
6079        let keys = dict_array.keys();
6080        assert!(keys.is_null(1));
6081        assert_eq!(keys.value(0), keys.value(3));
6082        assert_ne!(keys.value(0), keys.value(2));
6083    }
6084
6085    #[test]
6086    fn test_cast_string_view_array_to_dict_utf8_view() {
6087        let array = StringViewArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
6088
6089        let cast_type =
6090            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6091        assert!(can_cast_types(array.data_type(), &cast_type));
6092        let cast_array = cast(&array, &cast_type).unwrap();
6093        assert_eq!(cast_array.data_type(), &cast_type);
6094
6095        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6096        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6097        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
6098
6099        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6100        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6101        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6102
6103        let keys = dict_array.keys();
6104        assert!(keys.is_null(1));
6105        assert_eq!(keys.value(0), keys.value(3));
6106        assert_ne!(keys.value(0), keys.value(2));
6107    }
6108
6109    #[test]
6110    fn test_cast_string_view_slice_to_dict_utf8_view() {
6111        let array = StringViewArray::from(vec![
6112            Some("zero"),
6113            Some("one"),
6114            None,
6115            Some("three"),
6116            Some("one"),
6117        ]);
6118        let view = array.slice(1, 4);
6119
6120        let cast_type =
6121            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6122        assert!(can_cast_types(view.data_type(), &cast_type));
6123        let cast_array = cast(&view, &cast_type).unwrap();
6124        assert_eq!(cast_array.data_type(), &cast_type);
6125
6126        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6127        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6128        assert_eq!(dict_array.values().len(), 2);
6129
6130        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6131        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6132        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6133
6134        let keys = dict_array.keys();
6135        assert!(keys.is_null(1));
6136        assert_eq!(keys.value(0), keys.value(3));
6137        assert_ne!(keys.value(0), keys.value(2));
6138    }
6139
6140    #[test]
6141    fn test_cast_binary_array_to_dict_binary_view() {
6142        let mut builder = GenericBinaryBuilder::<i32>::new();
6143        builder.append_value(b"hello");
6144        builder.append_value(b"hiiii");
6145        builder.append_value(b"hiiii"); // duplicate
6146        builder.append_null();
6147        builder.append_value(b"rustt");
6148
6149        let array = builder.finish();
6150
6151        let cast_type =
6152            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6153        assert!(can_cast_types(array.data_type(), &cast_type));
6154        let cast_array = cast(&array, &cast_type).unwrap();
6155        assert_eq!(cast_array.data_type(), &cast_type);
6156
6157        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6158        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6159        assert_eq!(dict_array.values().len(), 3);
6160
6161        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6162        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6163        assert_eq!(
6164            actual,
6165            vec![
6166                Some(b"hello".as_slice()),
6167                Some(b"hiiii".as_slice()),
6168                Some(b"hiiii".as_slice()),
6169                None,
6170                Some(b"rustt".as_slice())
6171            ]
6172        );
6173
6174        let keys = dict_array.keys();
6175        assert!(keys.is_null(3));
6176        assert_eq!(keys.value(1), keys.value(2));
6177        assert_ne!(keys.value(0), keys.value(1));
6178    }
6179
6180    #[test]
6181    fn test_cast_binary_view_array_to_dict_binary_view() {
6182        let view = BinaryViewArray::from_iter([
6183            Some(b"hello".as_slice()),
6184            Some(b"hiiii".as_slice()),
6185            Some(b"hiiii".as_slice()), // duplicate
6186            None,
6187            Some(b"rustt".as_slice()),
6188        ]);
6189
6190        let cast_type =
6191            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6192        assert!(can_cast_types(view.data_type(), &cast_type));
6193        let cast_array = cast(&view, &cast_type).unwrap();
6194        assert_eq!(cast_array.data_type(), &cast_type);
6195
6196        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6197        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6198        assert_eq!(dict_array.values().len(), 3);
6199
6200        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6201        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6202        assert_eq!(
6203            actual,
6204            vec![
6205                Some(b"hello".as_slice()),
6206                Some(b"hiiii".as_slice()),
6207                Some(b"hiiii".as_slice()),
6208                None,
6209                Some(b"rustt".as_slice())
6210            ]
6211        );
6212
6213        let keys = dict_array.keys();
6214        assert!(keys.is_null(3));
6215        assert_eq!(keys.value(1), keys.value(2));
6216        assert_ne!(keys.value(0), keys.value(1));
6217    }
6218
6219    #[test]
6220    fn test_cast_binary_view_slice_to_dict_binary_view() {
6221        let view = BinaryViewArray::from_iter([
6222            Some(b"hello".as_slice()),
6223            Some(b"hiiii".as_slice()),
6224            Some(b"hiiii".as_slice()), // duplicate
6225            None,
6226            Some(b"rustt".as_slice()),
6227        ]);
6228        let sliced = view.slice(1, 4);
6229
6230        let cast_type =
6231            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6232        assert!(can_cast_types(sliced.data_type(), &cast_type));
6233        let cast_array = cast(&sliced, &cast_type).unwrap();
6234        assert_eq!(cast_array.data_type(), &cast_type);
6235
6236        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6237        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6238        assert_eq!(dict_array.values().len(), 2);
6239
6240        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6241        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6242        assert_eq!(
6243            actual,
6244            vec![
6245                Some(b"hiiii".as_slice()),
6246                Some(b"hiiii".as_slice()),
6247                None,
6248                Some(b"rustt".as_slice())
6249            ]
6250        );
6251
6252        let keys = dict_array.keys();
6253        assert!(keys.is_null(2));
6254        assert_eq!(keys.value(0), keys.value(1));
6255        assert_ne!(keys.value(0), keys.value(3));
6256    }
6257
6258    #[test]
6259    fn test_cast_string_array_to_dict_utf8_view_key_overflow_u8() {
6260        let array = StringArray::from_iter_values((0..257).map(|i| format!("v{i}")));
6261
6262        let cast_type =
6263            DataType::Dictionary(Box::new(DataType::UInt8), Box::new(DataType::Utf8View));
6264        assert!(can_cast_types(array.data_type(), &cast_type));
6265        let err = cast(&array, &cast_type).unwrap_err();
6266        assert!(matches!(err, ArrowError::DictionaryKeyOverflowError));
6267    }
6268
6269    #[test]
6270    fn test_cast_large_string_array_to_dict_utf8_view() {
6271        let array = LargeStringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
6272
6273        let cast_type =
6274            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6275        assert!(can_cast_types(array.data_type(), &cast_type));
6276        let cast_array = cast(&array, &cast_type).unwrap();
6277        assert_eq!(cast_array.data_type(), &cast_type);
6278
6279        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6280        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6281        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
6282
6283        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6284        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6285        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6286
6287        let keys = dict_array.keys();
6288        assert!(keys.is_null(1));
6289        assert_eq!(keys.value(0), keys.value(3));
6290        assert_ne!(keys.value(0), keys.value(2));
6291    }
6292
6293    #[test]
6294    fn test_cast_large_binary_array_to_dict_binary_view() {
6295        let mut builder = GenericBinaryBuilder::<i64>::new();
6296        builder.append_value(b"hello");
6297        builder.append_value(b"world");
6298        builder.append_value(b"hello"); // duplicate
6299        builder.append_null();
6300
6301        let array = builder.finish();
6302
6303        let cast_type =
6304            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6305        assert!(can_cast_types(array.data_type(), &cast_type));
6306        let cast_array = cast(&array, &cast_type).unwrap();
6307        assert_eq!(cast_array.data_type(), &cast_type);
6308
6309        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6310        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6311        assert_eq!(dict_array.values().len(), 2); // "hello" and "world" deduplicated
6312
6313        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6314        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6315        assert_eq!(
6316            actual,
6317            vec![
6318                Some(b"hello".as_slice()),
6319                Some(b"world".as_slice()),
6320                Some(b"hello".as_slice()),
6321                None
6322            ]
6323        );
6324
6325        let keys = dict_array.keys();
6326        assert!(keys.is_null(3));
6327        assert_eq!(keys.value(0), keys.value(2));
6328        assert_ne!(keys.value(0), keys.value(1));
6329    }
6330
6331    #[test]
6332    fn test_cast_struct_array_to_dict_struct() {
6333        // Cast a StructArray into Dictionary<UInt32, Struct{…}>. The dictionary
6334        // value type's child fields may differ from the source's (here:
6335        // Utf8 source → Utf8View child for `name`), so the per-field cast
6336        // must run before identity keys are emitted. This is the "as long as
6337        // the struct can be cast to the dict value" contract.
6338        let names = StringArray::from(vec![Some("alpha"), None, Some("gamma")]);
6339        let ids = Int32Array::from(vec![Some(1), Some(2), Some(3)]);
6340        let source = StructArray::from(vec![
6341            (
6342                Arc::new(Field::new("name", DataType::Utf8, true)),
6343                Arc::new(names) as ArrayRef,
6344            ),
6345            (
6346                Arc::new(Field::new("id", DataType::Int32, false)),
6347                Arc::new(ids) as ArrayRef,
6348            ),
6349        ]);
6350
6351        let target_value_type = DataType::Struct(
6352            vec![
6353                Field::new("name", DataType::Utf8View, true),
6354                Field::new("id", DataType::Int64, false),
6355            ]
6356            .into(),
6357        );
6358        let cast_type = DataType::Dictionary(
6359            Box::new(DataType::UInt32),
6360            Box::new(target_value_type.clone()),
6361        );
6362        assert!(can_cast_types(source.data_type(), &cast_type));
6363
6364        let cast_array = cast(&source, &cast_type).unwrap();
6365        assert_eq!(cast_array.data_type(), &cast_type);
6366        assert_eq!(cast_array.len(), 3);
6367
6368        let dict = cast_array.as_dictionary::<UInt32Type>();
6369        assert_eq!(dict.values().data_type(), &target_value_type);
6370        // No dedup is performed for struct values — one row, one key.
6371        assert_eq!(dict.values().len(), 3);
6372
6373        // Source row 1 was a `Utf8`-null in the `name` field but the whole
6374        // struct row was valid (StructArray::from above takes per-field
6375        // nulls only). The dictionary's logical null mask therefore mirrors
6376        // the source struct's row-level null mask — all rows valid here.
6377        let keys = dict.keys();
6378        assert_eq!(keys.values(), &[0u32, 1, 2]);
6379        assert_eq!(keys.null_count(), 0);
6380
6381        let struct_values = dict.values().as_struct();
6382        let names_out = struct_values
6383            .column_by_name("name")
6384            .unwrap()
6385            .as_string_view();
6386        assert_eq!(names_out.value(0), "alpha");
6387        assert!(names_out.is_null(1));
6388        assert_eq!(names_out.value(2), "gamma");
6389        let ids_out = struct_values
6390            .column_by_name("id")
6391            .unwrap()
6392            .as_primitive::<Int64Type>();
6393        assert_eq!(ids_out.values(), &[1i64, 2, 3]);
6394    }
6395
6396    #[test]
6397    fn test_cast_struct_array_to_dict_struct_row_nulls() {
6398        // Row-level nulls on the source struct must surface as null keys on
6399        // the dictionary, since the dictionary's logical null mask is
6400        // determined by the keys.
6401        let names = StringArray::from(vec![Some("alpha"), Some("beta"), Some("gamma")]);
6402        let ids = Int32Array::from(vec![Some(1), Some(2), Some(3)]);
6403        let source = StructArray::try_new(
6404            vec![
6405                Field::new("name", DataType::Utf8, true),
6406                Field::new("id", DataType::Int32, false),
6407            ]
6408            .into(),
6409            vec![Arc::new(names) as ArrayRef, Arc::new(ids) as ArrayRef],
6410            Some(NullBuffer::from(vec![true, false, true])),
6411        )
6412        .unwrap();
6413
6414        let target_value_type = DataType::Struct(
6415            vec![
6416                Field::new("name", DataType::Utf8, true),
6417                Field::new("id", DataType::Int32, false),
6418            ]
6419            .into(),
6420        );
6421        let cast_type =
6422            DataType::Dictionary(Box::new(DataType::UInt32), Box::new(target_value_type));
6423
6424        let cast_array = cast(&source, &cast_type).unwrap();
6425        let dict = cast_array.as_dictionary::<UInt32Type>();
6426        assert_eq!(dict.len(), 3);
6427        let keys = dict.keys();
6428        assert!(!keys.is_null(0));
6429        assert!(keys.is_null(1));
6430        assert!(!keys.is_null(2));
6431    }
6432
6433    #[test]
6434    fn test_cast_struct_array_to_dict_struct_key_overflow() {
6435        // Source has 300 rows but the dictionary key type is UInt8 (max 255).
6436        // We must return a CastError instead of silently truncating.
6437        let n = 300;
6438        let names = StringArray::from((0..n).map(|i| Some(format!("v{i}"))).collect::<Vec<_>>());
6439        let source = StructArray::from(vec![(
6440            Arc::new(Field::new("name", DataType::Utf8, true)),
6441            Arc::new(names) as ArrayRef,
6442        )]);
6443
6444        let cast_type = DataType::Dictionary(
6445            Box::new(DataType::UInt8),
6446            Box::new(DataType::Struct(
6447                vec![Field::new("name", DataType::Utf8, true)].into(),
6448            )),
6449        );
6450        let err = cast(&source, &cast_type).unwrap_err().to_string();
6451        assert!(
6452            err.contains("Cannot fit") && err.contains("dictionary keys"),
6453            "expected key-overflow error, got: {err}"
6454        );
6455    }
6456
6457    #[test]
6458    fn test_cast_empty_string_array_to_dict_utf8_view() {
6459        let array = StringArray::from(Vec::<Option<&str>>::new());
6460
6461        let cast_type =
6462            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6463        assert!(can_cast_types(array.data_type(), &cast_type));
6464        let cast_array = cast(&array, &cast_type).unwrap();
6465        assert_eq!(cast_array.data_type(), &cast_type);
6466        assert_eq!(cast_array.len(), 0);
6467    }
6468
6469    #[test]
6470    fn test_cast_empty_binary_array_to_dict_binary_view() {
6471        let array = BinaryArray::from(Vec::<Option<&[u8]>>::new());
6472
6473        let cast_type =
6474            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6475        assert!(can_cast_types(array.data_type(), &cast_type));
6476        let cast_array = cast(&array, &cast_type).unwrap();
6477        assert_eq!(cast_array.data_type(), &cast_type);
6478        assert_eq!(cast_array.len(), 0);
6479    }
6480
6481    #[test]
6482    fn test_cast_all_null_string_array_to_dict_utf8_view() {
6483        let array = StringArray::from(vec![None::<&str>, None, None]);
6484
6485        let cast_type =
6486            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6487        assert!(can_cast_types(array.data_type(), &cast_type));
6488        let cast_array = cast(&array, &cast_type).unwrap();
6489        assert_eq!(cast_array.data_type(), &cast_type);
6490        assert_eq!(cast_array.null_count(), 3);
6491
6492        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6493        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6494        assert_eq!(dict_array.values().len(), 0);
6495        assert_eq!(dict_array.keys().null_count(), 3);
6496
6497        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6498        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6499        assert_eq!(actual, vec![None, None, None]);
6500    }
6501
6502    #[test]
6503    fn test_cast_all_null_binary_array_to_dict_binary_view() {
6504        let array = BinaryArray::from(vec![None::<&[u8]>, None, None]);
6505
6506        let cast_type =
6507            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6508        assert!(can_cast_types(array.data_type(), &cast_type));
6509        let cast_array = cast(&array, &cast_type).unwrap();
6510        assert_eq!(cast_array.data_type(), &cast_type);
6511        assert_eq!(cast_array.null_count(), 3);
6512
6513        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6514        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6515        assert_eq!(dict_array.values().len(), 0);
6516        assert_eq!(dict_array.keys().null_count(), 3);
6517
6518        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6519        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6520        assert_eq!(actual, vec![None, None, None]);
6521    }
6522
6523    #[test]
6524    fn test_numeric_to_binary() {
6525        let a = Int16Array::from(vec![Some(1), Some(511), None]);
6526
6527        let array_ref = cast(&a, &DataType::Binary).unwrap();
6528        let down_cast = array_ref.as_binary::<i32>();
6529        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6530        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6531        assert!(down_cast.is_null(2));
6532
6533        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6534
6535        let array_ref = cast(&a, &DataType::Binary).unwrap();
6536        let down_cast = array_ref.as_binary::<i32>();
6537        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6538        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6539        assert!(down_cast.is_null(2));
6540    }
6541
6542    #[test]
6543    fn test_numeric_to_large_binary() {
6544        let a = Int16Array::from(vec![Some(1), Some(511), None]);
6545
6546        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6547        let down_cast = array_ref.as_binary::<i64>();
6548        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6549        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6550        assert!(down_cast.is_null(2));
6551
6552        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6553
6554        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6555        let down_cast = array_ref.as_binary::<i64>();
6556        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6557        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6558        assert!(down_cast.is_null(2));
6559    }
6560
6561    #[test]
6562    fn test_cast_date32_to_int32() {
6563        let array = Date32Array::from(vec![10000, 17890]);
6564        let b = cast(&array, &DataType::Int32).unwrap();
6565        let c = b.as_primitive::<Int32Type>();
6566        assert_eq!(10000, c.value(0));
6567        assert_eq!(17890, c.value(1));
6568    }
6569
6570    #[test]
6571    fn test_cast_int32_to_date32() {
6572        let array = Int32Array::from(vec![10000, 17890]);
6573        let b = cast(&array, &DataType::Date32).unwrap();
6574        let c = b.as_primitive::<Date32Type>();
6575        assert_eq!(10000, c.value(0));
6576        assert_eq!(17890, c.value(1));
6577    }
6578
6579    #[test]
6580    fn test_cast_timestamp_to_date32() {
6581        let array =
6582            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6583                .with_timezone("+00:00".to_string());
6584        let b = cast(&array, &DataType::Date32).unwrap();
6585        let c = b.as_primitive::<Date32Type>();
6586        assert_eq!(10000, c.value(0));
6587        assert_eq!(17890, c.value(1));
6588        assert!(c.is_null(2));
6589    }
6590    #[test]
6591    fn test_cast_timestamp_to_date32_zone() {
6592        let strings = StringArray::from_iter([
6593            Some("1970-01-01T00:00:01"),
6594            Some("1970-01-01T23:59:59"),
6595            None,
6596            Some("2020-03-01T02:00:23+00:00"),
6597        ]);
6598        let dt = DataType::Timestamp(TimeUnit::Millisecond, Some("-07:00".into()));
6599        let timestamps = cast(&strings, &dt).unwrap();
6600        let dates = cast(timestamps.as_ref(), &DataType::Date32).unwrap();
6601
6602        let c = dates.as_primitive::<Date32Type>();
6603        let expected = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
6604        assert_eq!(c.value_as_date(0).unwrap(), expected);
6605        assert_eq!(c.value_as_date(1).unwrap(), expected);
6606        assert!(c.is_null(2));
6607        let expected = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
6608        assert_eq!(c.value_as_date(3).unwrap(), expected);
6609    }
6610    #[test]
6611    fn test_cast_timestamp_to_date64() {
6612        let array =
6613            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None]);
6614        let b = cast(&array, &DataType::Date64).unwrap();
6615        let c = b.as_primitive::<Date64Type>();
6616        assert_eq!(864000000005, c.value(0));
6617        assert_eq!(1545696000001, c.value(1));
6618        assert!(c.is_null(2));
6619
6620        let array = TimestampSecondArray::from(vec![Some(864000000005), Some(1545696000001)]);
6621        let b = cast(&array, &DataType::Date64).unwrap();
6622        let c = b.as_primitive::<Date64Type>();
6623        assert_eq!(864000000005000, c.value(0));
6624        assert_eq!(1545696000001000, c.value(1));
6625
6626        // test overflow, safe cast
6627        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6628        let b = cast(&array, &DataType::Date64).unwrap();
6629        assert!(b.is_null(0));
6630        // test overflow, unsafe cast
6631        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6632        let options = CastOptions {
6633            safe: false,
6634            format_options: FormatOptions::default(),
6635        };
6636        let b = cast_with_options(&array, &DataType::Date64, &options);
6637        assert!(b.is_err());
6638    }
6639
6640    #[test]
6641    fn test_cast_timestamp_to_time64() {
6642        // test timestamp secs
6643        let array = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6644            .with_timezone("+01:00".to_string());
6645        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6646        let c = b.as_primitive::<Time64MicrosecondType>();
6647        assert_eq!(3605000000, c.value(0));
6648        assert_eq!(3601000000, c.value(1));
6649        assert!(c.is_null(2));
6650        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6651        let c = b.as_primitive::<Time64NanosecondType>();
6652        assert_eq!(3605000000000, c.value(0));
6653        assert_eq!(3601000000000, c.value(1));
6654        assert!(c.is_null(2));
6655
6656        // test timestamp milliseconds
6657        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6658            .with_timezone("+01:00".to_string());
6659        let array = Arc::new(a) as ArrayRef;
6660        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6661        let c = b.as_primitive::<Time64MicrosecondType>();
6662        assert_eq!(3605000000, c.value(0));
6663        assert_eq!(3601000000, c.value(1));
6664        assert!(c.is_null(2));
6665        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6666        let c = b.as_primitive::<Time64NanosecondType>();
6667        assert_eq!(3605000000000, c.value(0));
6668        assert_eq!(3601000000000, c.value(1));
6669        assert!(c.is_null(2));
6670
6671        // test timestamp microseconds
6672        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6673            .with_timezone("+01:00".to_string());
6674        let array = Arc::new(a) as ArrayRef;
6675        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6676        let c = b.as_primitive::<Time64MicrosecondType>();
6677        assert_eq!(3605000000, c.value(0));
6678        assert_eq!(3601000000, c.value(1));
6679        assert!(c.is_null(2));
6680        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6681        let c = b.as_primitive::<Time64NanosecondType>();
6682        assert_eq!(3605000000000, c.value(0));
6683        assert_eq!(3601000000000, c.value(1));
6684        assert!(c.is_null(2));
6685
6686        // test timestamp nanoseconds
6687        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
6688            .with_timezone("+01:00".to_string());
6689        let array = Arc::new(a) as ArrayRef;
6690        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6691        let c = b.as_primitive::<Time64MicrosecondType>();
6692        assert_eq!(3605000000, c.value(0));
6693        assert_eq!(3601000000, c.value(1));
6694        assert!(c.is_null(2));
6695        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6696        let c = b.as_primitive::<Time64NanosecondType>();
6697        assert_eq!(3605000000000, c.value(0));
6698        assert_eq!(3601000000000, c.value(1));
6699        assert!(c.is_null(2));
6700
6701        // test overflow
6702        let a =
6703            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6704        let array = Arc::new(a) as ArrayRef;
6705        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond));
6706        assert!(b.is_err());
6707        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond));
6708        assert!(b.is_err());
6709        let b = cast(&array, &DataType::Time64(TimeUnit::Millisecond));
6710        assert!(b.is_err());
6711    }
6712
6713    #[test]
6714    fn test_cast_timestamp_to_time32() {
6715        // test timestamp secs
6716        let a = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6717            .with_timezone("+01:00".to_string());
6718        let array = Arc::new(a) as ArrayRef;
6719        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6720        let c = b.as_primitive::<Time32SecondType>();
6721        assert_eq!(3605, c.value(0));
6722        assert_eq!(3601, c.value(1));
6723        assert!(c.is_null(2));
6724        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6725        let c = b.as_primitive::<Time32MillisecondType>();
6726        assert_eq!(3605000, c.value(0));
6727        assert_eq!(3601000, c.value(1));
6728        assert!(c.is_null(2));
6729
6730        // test timestamp milliseconds
6731        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6732            .with_timezone("+01:00".to_string());
6733        let array = Arc::new(a) as ArrayRef;
6734        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6735        let c = b.as_primitive::<Time32SecondType>();
6736        assert_eq!(3605, c.value(0));
6737        assert_eq!(3601, c.value(1));
6738        assert!(c.is_null(2));
6739        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6740        let c = b.as_primitive::<Time32MillisecondType>();
6741        assert_eq!(3605000, c.value(0));
6742        assert_eq!(3601000, c.value(1));
6743        assert!(c.is_null(2));
6744
6745        // test timestamp microseconds
6746        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6747            .with_timezone("+01:00".to_string());
6748        let array = Arc::new(a) as ArrayRef;
6749        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6750        let c = b.as_primitive::<Time32SecondType>();
6751        assert_eq!(3605, c.value(0));
6752        assert_eq!(3601, c.value(1));
6753        assert!(c.is_null(2));
6754        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6755        let c = b.as_primitive::<Time32MillisecondType>();
6756        assert_eq!(3605000, c.value(0));
6757        assert_eq!(3601000, c.value(1));
6758        assert!(c.is_null(2));
6759
6760        // test timestamp nanoseconds
6761        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
6762            .with_timezone("+01:00".to_string());
6763        let array = Arc::new(a) as ArrayRef;
6764        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6765        let c = b.as_primitive::<Time32SecondType>();
6766        assert_eq!(3605, c.value(0));
6767        assert_eq!(3601, c.value(1));
6768        assert!(c.is_null(2));
6769        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6770        let c = b.as_primitive::<Time32MillisecondType>();
6771        assert_eq!(3605000, c.value(0));
6772        assert_eq!(3601000, c.value(1));
6773        assert!(c.is_null(2));
6774
6775        // test overflow
6776        let a =
6777            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6778        let array = Arc::new(a) as ArrayRef;
6779        let b = cast(&array, &DataType::Time32(TimeUnit::Second));
6780        assert!(b.is_err());
6781        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond));
6782        assert!(b.is_err());
6783    }
6784
6785    // Cast Timestamp(_, None) -> Timestamp(_, Some(timezone))
6786    #[test]
6787    fn test_cast_timestamp_with_timezone_1() {
6788        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6789            Some("2000-01-01T00:00:00.123456789"),
6790            Some("2010-01-01T00:00:00.123456789"),
6791            None,
6792        ]));
6793        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6794        let timestamp_array = cast(&string_array, &to_type).unwrap();
6795
6796        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6797        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6798
6799        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6800        let result = string_array.as_string::<i32>();
6801        assert_eq!("2000-01-01T00:00:00.123456+07:00", result.value(0));
6802        assert_eq!("2010-01-01T00:00:00.123456+07:00", result.value(1));
6803        assert!(result.is_null(2));
6804    }
6805
6806    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, None)
6807    #[test]
6808    fn test_cast_timestamp_with_timezone_2() {
6809        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6810            Some("2000-01-01T07:00:00.123456789"),
6811            Some("2010-01-01T07:00:00.123456789"),
6812            None,
6813        ]));
6814        let to_type = DataType::Timestamp(TimeUnit::Millisecond, Some("+0700".into()));
6815        let timestamp_array = cast(&string_array, &to_type).unwrap();
6816
6817        // Check intermediate representation is correct
6818        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6819        let result = string_array.as_string::<i32>();
6820        assert_eq!("2000-01-01T07:00:00.123+07:00", result.value(0));
6821        assert_eq!("2010-01-01T07:00:00.123+07:00", result.value(1));
6822        assert!(result.is_null(2));
6823
6824        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6825        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6826
6827        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6828        let result = string_array.as_string::<i32>();
6829        assert_eq!("2000-01-01T00:00:00.123", result.value(0));
6830        assert_eq!("2010-01-01T00:00:00.123", result.value(1));
6831        assert!(result.is_null(2));
6832    }
6833
6834    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, Some(timezone))
6835    #[test]
6836    fn test_cast_timestamp_with_timezone_3() {
6837        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6838            Some("2000-01-01T07:00:00.123456789"),
6839            Some("2010-01-01T07:00:00.123456789"),
6840            None,
6841        ]));
6842        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6843        let timestamp_array = cast(&string_array, &to_type).unwrap();
6844
6845        // Check intermediate representation is correct
6846        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6847        let result = string_array.as_string::<i32>();
6848        assert_eq!("2000-01-01T07:00:00.123456+07:00", result.value(0));
6849        assert_eq!("2010-01-01T07:00:00.123456+07:00", result.value(1));
6850        assert!(result.is_null(2));
6851
6852        let to_type = DataType::Timestamp(TimeUnit::Second, Some("-08:00".into()));
6853        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6854
6855        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6856        let result = string_array.as_string::<i32>();
6857        assert_eq!("1999-12-31T16:00:00-08:00", result.value(0));
6858        assert_eq!("2009-12-31T16:00:00-08:00", result.value(1));
6859        assert!(result.is_null(2));
6860    }
6861
6862    #[test]
6863    fn test_cast_date64_to_timestamp() {
6864        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6865        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6866        let c = b.as_primitive::<TimestampSecondType>();
6867        assert_eq!(864000000, c.value(0));
6868        assert_eq!(1545696000, c.value(1));
6869        assert!(c.is_null(2));
6870    }
6871
6872    #[test]
6873    fn test_cast_date64_to_timestamp_ms() {
6874        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6875        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
6876        let c = b
6877            .as_any()
6878            .downcast_ref::<TimestampMillisecondArray>()
6879            .unwrap();
6880        assert_eq!(864000000005, c.value(0));
6881        assert_eq!(1545696000001, c.value(1));
6882        assert!(c.is_null(2));
6883    }
6884
6885    #[test]
6886    fn test_cast_date64_to_timestamp_us() {
6887        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6888        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
6889        let c = b
6890            .as_any()
6891            .downcast_ref::<TimestampMicrosecondArray>()
6892            .unwrap();
6893        assert_eq!(864000000005000, c.value(0));
6894        assert_eq!(1545696000001000, c.value(1));
6895        assert!(c.is_null(2));
6896    }
6897
6898    #[test]
6899    fn test_cast_date64_to_timestamp_ns() {
6900        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6901        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
6902        let c = b
6903            .as_any()
6904            .downcast_ref::<TimestampNanosecondArray>()
6905            .unwrap();
6906        assert_eq!(864000000005000000, c.value(0));
6907        assert_eq!(1545696000001000000, c.value(1));
6908        assert!(c.is_null(2));
6909    }
6910
6911    #[test]
6912    fn test_cast_timestamp_to_i64() {
6913        let array =
6914            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6915                .with_timezone("UTC".to_string());
6916        let b = cast(&array, &DataType::Int64).unwrap();
6917        let c = b.as_primitive::<Int64Type>();
6918        assert_eq!(&DataType::Int64, c.data_type());
6919        assert_eq!(864000000005, c.value(0));
6920        assert_eq!(1545696000001, c.value(1));
6921        assert!(c.is_null(2));
6922    }
6923
6924    macro_rules! assert_cast {
6925        ($array:expr, $datatype:expr, $output_array_type: ty, $expected:expr) => {{
6926            assert!(can_cast_types($array.data_type(), &$datatype));
6927            let out = cast(&$array, &$datatype).unwrap();
6928            let actual = out
6929                .as_any()
6930                .downcast_ref::<$output_array_type>()
6931                .unwrap()
6932                .into_iter()
6933                .collect::<Vec<_>>();
6934            assert_eq!(actual, $expected);
6935        }};
6936        ($array:expr, $datatype:expr, $output_array_type: ty, $options:expr, $expected:expr) => {{
6937            assert!(can_cast_types($array.data_type(), &$datatype));
6938            let out = cast_with_options(&$array, &$datatype, &$options).unwrap();
6939            let actual = out
6940                .as_any()
6941                .downcast_ref::<$output_array_type>()
6942                .unwrap()
6943                .into_iter()
6944                .collect::<Vec<_>>();
6945            assert_eq!(actual, $expected);
6946        }};
6947    }
6948
6949    #[test]
6950    fn test_cast_date32_to_string() {
6951        let array = Date32Array::from(vec![Some(0), Some(10000), Some(13036), Some(17890), None]);
6952        let expected = vec![
6953            Some("1970-01-01"),
6954            Some("1997-05-19"),
6955            Some("2005-09-10"),
6956            Some("2018-12-25"),
6957            None,
6958        ];
6959
6960        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6961        assert_cast!(array, DataType::Utf8, StringArray, expected);
6962        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6963    }
6964
6965    #[test]
6966    fn test_cast_date64_to_string() {
6967        let array = Date64Array::from(vec![
6968            Some(0),
6969            Some(10000 * 86400000),
6970            Some(13036 * 86400000),
6971            Some(17890 * 86400000),
6972            None,
6973        ]);
6974        let expected = vec![
6975            Some("1970-01-01T00:00:00"),
6976            Some("1997-05-19T00:00:00"),
6977            Some("2005-09-10T00:00:00"),
6978            Some("2018-12-25T00:00:00"),
6979            None,
6980        ];
6981
6982        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6983        assert_cast!(array, DataType::Utf8, StringArray, expected);
6984        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6985    }
6986
6987    #[test]
6988    fn test_cast_date32_to_timestamp_and_timestamp_with_timezone() {
6989        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6990        let a = Date32Array::from(vec![Some(18628), None, None]); // 2021-1-1, 2022-1-1
6991        let array = Arc::new(a) as ArrayRef;
6992
6993        let b = cast(
6994            &array,
6995            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6996        )
6997        .unwrap();
6998        let c = b.as_primitive::<TimestampSecondType>();
6999        let string_array = cast(&c, &DataType::Utf8).unwrap();
7000        let result = string_array.as_string::<i32>();
7001        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7002
7003        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
7004        let c = b.as_primitive::<TimestampSecondType>();
7005        let string_array = cast(&c, &DataType::Utf8).unwrap();
7006        let result = string_array.as_string::<i32>();
7007        assert_eq!("2021-01-01T00:00:00", result.value(0));
7008    }
7009
7010    #[test]
7011    fn test_cast_date32_to_timestamp_with_timezone() {
7012        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7013        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7014        let array = Arc::new(a) as ArrayRef;
7015        let b = cast(
7016            &array,
7017            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
7018        )
7019        .unwrap();
7020        let c = b.as_primitive::<TimestampSecondType>();
7021        assert_eq!(1609438500, c.value(0));
7022        assert_eq!(1640974500, c.value(1));
7023        assert!(c.is_null(2));
7024
7025        let string_array = cast(&c, &DataType::Utf8).unwrap();
7026        let result = string_array.as_string::<i32>();
7027        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7028        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7029    }
7030
7031    #[test]
7032    fn test_cast_date32_to_timestamp_with_timezone_ms() {
7033        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7034        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7035        let array = Arc::new(a) as ArrayRef;
7036        let b = cast(
7037            &array,
7038            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
7039        )
7040        .unwrap();
7041        let c = b.as_primitive::<TimestampMillisecondType>();
7042        assert_eq!(1609438500000, c.value(0));
7043        assert_eq!(1640974500000, c.value(1));
7044        assert!(c.is_null(2));
7045
7046        let string_array = cast(&c, &DataType::Utf8).unwrap();
7047        let result = string_array.as_string::<i32>();
7048        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7049        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7050    }
7051
7052    #[test]
7053    fn test_cast_date32_to_timestamp_with_timezone_us() {
7054        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7055        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7056        let array = Arc::new(a) as ArrayRef;
7057        let b = cast(
7058            &array,
7059            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
7060        )
7061        .unwrap();
7062        let c = b.as_primitive::<TimestampMicrosecondType>();
7063        assert_eq!(1609438500000000, c.value(0));
7064        assert_eq!(1640974500000000, c.value(1));
7065        assert!(c.is_null(2));
7066
7067        let string_array = cast(&c, &DataType::Utf8).unwrap();
7068        let result = string_array.as_string::<i32>();
7069        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7070        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7071    }
7072
7073    #[test]
7074    fn test_cast_date32_to_timestamp_with_timezone_ns() {
7075        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7076        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
7077        let array = Arc::new(a) as ArrayRef;
7078        let b = cast(
7079            &array,
7080            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
7081        )
7082        .unwrap();
7083        let c = b.as_primitive::<TimestampNanosecondType>();
7084        assert_eq!(1609438500000000000, c.value(0));
7085        assert_eq!(1640974500000000000, c.value(1));
7086        assert!(c.is_null(2));
7087
7088        let string_array = cast(&c, &DataType::Utf8).unwrap();
7089        let result = string_array.as_string::<i32>();
7090        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
7091        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
7092    }
7093
7094    #[test]
7095    fn test_cast_date64_to_timestamp_with_timezone() {
7096        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7097        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7098        let b = cast(
7099            &array,
7100            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
7101        )
7102        .unwrap();
7103
7104        let c = b.as_primitive::<TimestampSecondType>();
7105        assert_eq!(863979300, c.value(0));
7106        assert_eq!(1545675300, c.value(1));
7107        assert!(c.is_null(2));
7108
7109        let string_array = cast(&c, &DataType::Utf8).unwrap();
7110        let result = string_array.as_string::<i32>();
7111        assert_eq!("1997-05-19T00:00:00+05:45", result.value(0));
7112        assert_eq!("2018-12-25T00:00:00+05:45", result.value(1));
7113    }
7114
7115    #[test]
7116    fn test_cast_date64_to_timestamp_with_timezone_ms() {
7117        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7118        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7119        let b = cast(
7120            &array,
7121            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
7122        )
7123        .unwrap();
7124
7125        let c = b.as_primitive::<TimestampMillisecondType>();
7126        assert_eq!(863979300005, c.value(0));
7127        assert_eq!(1545675300001, c.value(1));
7128        assert!(c.is_null(2));
7129
7130        let string_array = cast(&c, &DataType::Utf8).unwrap();
7131        let result = string_array.as_string::<i32>();
7132        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
7133        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7134    }
7135
7136    #[test]
7137    fn test_cast_date64_to_timestamp_with_timezone_us() {
7138        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7139        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7140        let b = cast(
7141            &array,
7142            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
7143        )
7144        .unwrap();
7145
7146        let c = b.as_primitive::<TimestampMicrosecondType>();
7147        assert_eq!(863979300005000, c.value(0));
7148        assert_eq!(1545675300001000, c.value(1));
7149        assert!(c.is_null(2));
7150
7151        let string_array = cast(&c, &DataType::Utf8).unwrap();
7152        let result = string_array.as_string::<i32>();
7153        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
7154        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7155    }
7156
7157    #[test]
7158    fn test_cast_date64_to_timestamp_with_timezone_ns() {
7159        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
7160        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7161        let b = cast(
7162            &array,
7163            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
7164        )
7165        .unwrap();
7166
7167        let c = b.as_primitive::<TimestampNanosecondType>();
7168        assert_eq!(863979300005000000, c.value(0));
7169        assert_eq!(1545675300001000000, c.value(1));
7170        assert!(c.is_null(2));
7171
7172        let string_array = cast(&c, &DataType::Utf8).unwrap();
7173        let result = string_array.as_string::<i32>();
7174        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
7175        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
7176    }
7177
7178    #[test]
7179    fn test_cast_timestamp_to_strings() {
7180        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
7181        let array =
7182            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7183        let expected = vec![
7184            Some("1997-05-19T00:00:03.005"),
7185            Some("2018-12-25T00:00:02.001"),
7186            None,
7187        ];
7188
7189        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
7190        assert_cast!(array, DataType::Utf8, StringArray, expected);
7191        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
7192    }
7193
7194    #[test]
7195    fn test_cast_timestamp_to_strings_opt() {
7196        let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
7197        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
7198        let cast_options = CastOptions {
7199            safe: true,
7200            format_options: FormatOptions::default()
7201                .with_timestamp_format(Some(ts_format))
7202                .with_timestamp_tz_format(Some(ts_format)),
7203        };
7204
7205        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
7206        let array_without_tz =
7207            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7208        let expected = vec![
7209            Some("1997-05-19 00:00:03.005000"),
7210            Some("2018-12-25 00:00:02.001000"),
7211            None,
7212        ];
7213        assert_cast!(
7214            array_without_tz,
7215            DataType::Utf8View,
7216            StringViewArray,
7217            cast_options,
7218            expected
7219        );
7220        assert_cast!(
7221            array_without_tz,
7222            DataType::Utf8,
7223            StringArray,
7224            cast_options,
7225            expected
7226        );
7227        assert_cast!(
7228            array_without_tz,
7229            DataType::LargeUtf8,
7230            LargeStringArray,
7231            cast_options,
7232            expected
7233        );
7234
7235        let array_with_tz =
7236            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None])
7237                .with_timezone(tz.to_string());
7238        let expected = vec![
7239            Some("1997-05-19 05:45:03.005000"),
7240            Some("2018-12-25 05:45:02.001000"),
7241            None,
7242        ];
7243        assert_cast!(
7244            array_with_tz,
7245            DataType::Utf8View,
7246            StringViewArray,
7247            cast_options,
7248            expected
7249        );
7250        assert_cast!(
7251            array_with_tz,
7252            DataType::Utf8,
7253            StringArray,
7254            cast_options,
7255            expected
7256        );
7257        assert_cast!(
7258            array_with_tz,
7259            DataType::LargeUtf8,
7260            LargeStringArray,
7261            cast_options,
7262            expected
7263        );
7264    }
7265
7266    #[test]
7267    fn test_cast_between_timestamps() {
7268        let array =
7269            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
7270        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
7271        let c = b.as_primitive::<TimestampSecondType>();
7272        assert_eq!(864000003, c.value(0));
7273        assert_eq!(1545696002, c.value(1));
7274        assert!(c.is_null(2));
7275    }
7276
7277    #[test]
7278    fn test_cast_duration_to_i64() {
7279        let base = vec![5, 6, 7, 8, 100000000];
7280
7281        let duration_arrays = vec![
7282            Arc::new(DurationNanosecondArray::from(base.clone())) as ArrayRef,
7283            Arc::new(DurationMicrosecondArray::from(base.clone())) as ArrayRef,
7284            Arc::new(DurationMillisecondArray::from(base.clone())) as ArrayRef,
7285            Arc::new(DurationSecondArray::from(base.clone())) as ArrayRef,
7286        ];
7287
7288        for arr in duration_arrays {
7289            assert!(can_cast_types(arr.data_type(), &DataType::Int64));
7290            let result = cast(&arr, &DataType::Int64).unwrap();
7291            let result = result.as_primitive::<Int64Type>();
7292            assert_eq!(base.as_slice(), result.values());
7293        }
7294    }
7295
7296    #[test]
7297    fn test_cast_between_durations_and_numerics() {
7298        fn test_cast_between_durations<FromType, ToType>()
7299        where
7300            FromType: ArrowPrimitiveType<Native = i64>,
7301            ToType: ArrowPrimitiveType<Native = i64>,
7302            PrimitiveArray<FromType>: From<Vec<Option<i64>>>,
7303        {
7304            let from_unit = match FromType::DATA_TYPE {
7305                DataType::Duration(unit) => unit,
7306                _ => panic!("Expected a duration type"),
7307            };
7308            let to_unit = match ToType::DATA_TYPE {
7309                DataType::Duration(unit) => unit,
7310                _ => panic!("Expected a duration type"),
7311            };
7312            let from_size = time_unit_multiple(&from_unit);
7313            let to_size = time_unit_multiple(&to_unit);
7314
7315            let (v1_before, v2_before) = (8640003005, 1696002001);
7316            let (v1_after, v2_after) = if from_size >= to_size {
7317                (
7318                    v1_before / (from_size / to_size),
7319                    v2_before / (from_size / to_size),
7320                )
7321            } else {
7322                (
7323                    v1_before * (to_size / from_size),
7324                    v2_before * (to_size / from_size),
7325                )
7326            };
7327
7328            let array =
7329                PrimitiveArray::<FromType>::from(vec![Some(v1_before), Some(v2_before), None]);
7330            let b = cast(&array, &ToType::DATA_TYPE).unwrap();
7331            let c = b.as_primitive::<ToType>();
7332            assert_eq!(v1_after, c.value(0));
7333            assert_eq!(v2_after, c.value(1));
7334            assert!(c.is_null(2));
7335        }
7336
7337        // between each individual duration type
7338        test_cast_between_durations::<DurationSecondType, DurationMillisecondType>();
7339        test_cast_between_durations::<DurationSecondType, DurationMicrosecondType>();
7340        test_cast_between_durations::<DurationSecondType, DurationNanosecondType>();
7341        test_cast_between_durations::<DurationMillisecondType, DurationSecondType>();
7342        test_cast_between_durations::<DurationMillisecondType, DurationMicrosecondType>();
7343        test_cast_between_durations::<DurationMillisecondType, DurationNanosecondType>();
7344        test_cast_between_durations::<DurationMicrosecondType, DurationSecondType>();
7345        test_cast_between_durations::<DurationMicrosecondType, DurationMillisecondType>();
7346        test_cast_between_durations::<DurationMicrosecondType, DurationNanosecondType>();
7347        test_cast_between_durations::<DurationNanosecondType, DurationSecondType>();
7348        test_cast_between_durations::<DurationNanosecondType, DurationMillisecondType>();
7349        test_cast_between_durations::<DurationNanosecondType, DurationMicrosecondType>();
7350
7351        // cast failed
7352        let array = DurationSecondArray::from(vec![
7353            Some(i64::MAX),
7354            Some(8640203410378005),
7355            Some(10241096),
7356            None,
7357        ]);
7358        let b = cast(&array, &DataType::Duration(TimeUnit::Nanosecond)).unwrap();
7359        let c = b.as_primitive::<DurationNanosecondType>();
7360        assert!(c.is_null(0));
7361        assert!(c.is_null(1));
7362        assert_eq!(10241096000000000, c.value(2));
7363        assert!(c.is_null(3));
7364
7365        // durations to numerics
7366        let array = DurationSecondArray::from(vec![
7367            Some(i64::MAX),
7368            Some(8640203410378005),
7369            Some(10241096),
7370            None,
7371        ]);
7372        let b = cast(&array, &DataType::Int64).unwrap();
7373        let c = b.as_primitive::<Int64Type>();
7374        assert_eq!(i64::MAX, c.value(0));
7375        assert_eq!(8640203410378005, c.value(1));
7376        assert_eq!(10241096, c.value(2));
7377        assert!(c.is_null(3));
7378
7379        let b = cast(&array, &DataType::Int32).unwrap();
7380        let c = b.as_primitive::<Int32Type>();
7381        assert_eq!(0, c.value(0));
7382        assert_eq!(0, c.value(1));
7383        assert_eq!(10241096, c.value(2));
7384        assert!(c.is_null(3));
7385
7386        // numerics to durations
7387        let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), Some(10241096), None]);
7388        let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
7389        let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
7390        assert_eq!(i32::MAX as i64, c.value(0));
7391        assert_eq!(802034103, c.value(1));
7392        assert_eq!(10241096, c.value(2));
7393        assert!(c.is_null(3));
7394    }
7395
7396    #[test]
7397    fn test_cast_to_strings() {
7398        let a = Int32Array::from(vec![1, 2, 3]);
7399        let out = cast(&a, &DataType::Utf8).unwrap();
7400        let out = out
7401            .as_any()
7402            .downcast_ref::<StringArray>()
7403            .unwrap()
7404            .into_iter()
7405            .collect::<Vec<_>>();
7406        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
7407        let out = cast(&a, &DataType::LargeUtf8).unwrap();
7408        let out = out
7409            .as_any()
7410            .downcast_ref::<LargeStringArray>()
7411            .unwrap()
7412            .into_iter()
7413            .collect::<Vec<_>>();
7414        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
7415    }
7416
7417    #[test]
7418    fn test_str_to_str_casts() {
7419        for data in [
7420            vec![Some("foo"), Some("bar"), Some("ham")],
7421            vec![Some("foo"), None, Some("bar")],
7422        ] {
7423            let a = LargeStringArray::from(data.clone());
7424            let to = cast(&a, &DataType::Utf8).unwrap();
7425            let expect = a
7426                .as_any()
7427                .downcast_ref::<LargeStringArray>()
7428                .unwrap()
7429                .into_iter()
7430                .collect::<Vec<_>>();
7431            let out = to
7432                .as_any()
7433                .downcast_ref::<StringArray>()
7434                .unwrap()
7435                .into_iter()
7436                .collect::<Vec<_>>();
7437            assert_eq!(expect, out);
7438
7439            let a = StringArray::from(data);
7440            let to = cast(&a, &DataType::LargeUtf8).unwrap();
7441            let expect = a
7442                .as_any()
7443                .downcast_ref::<StringArray>()
7444                .unwrap()
7445                .into_iter()
7446                .collect::<Vec<_>>();
7447            let out = to
7448                .as_any()
7449                .downcast_ref::<LargeStringArray>()
7450                .unwrap()
7451                .into_iter()
7452                .collect::<Vec<_>>();
7453            assert_eq!(expect, out);
7454        }
7455    }
7456
7457    const VIEW_TEST_DATA: [Option<&str>; 5] = [
7458        Some("hello"),
7459        Some("repeated"),
7460        None,
7461        Some("large payload over 12 bytes"),
7462        Some("repeated"),
7463    ];
7464
7465    #[test]
7466    fn test_string_view_to_binary_view() {
7467        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7468
7469        assert!(can_cast_types(
7470            string_view_array.data_type(),
7471            &DataType::BinaryView
7472        ));
7473
7474        let binary_view_array = cast(&string_view_array, &DataType::BinaryView).unwrap();
7475        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7476
7477        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7478        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7479    }
7480
7481    #[test]
7482    fn test_binary_view_to_string_view() {
7483        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7484
7485        assert!(can_cast_types(
7486            binary_view_array.data_type(),
7487            &DataType::Utf8View
7488        ));
7489
7490        let string_view_array = cast(&binary_view_array, &DataType::Utf8View).unwrap();
7491        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7492
7493        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7494        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7495    }
7496
7497    #[test]
7498    fn test_binary_view_to_string_view_with_invalid_utf8() {
7499        let binary_view_array = BinaryViewArray::from_iter(vec![
7500            Some("valid".as_bytes()),
7501            Some(&[0xff]),
7502            Some("utf8".as_bytes()),
7503            None,
7504        ]);
7505
7506        let strict_options = CastOptions {
7507            safe: false,
7508            ..Default::default()
7509        };
7510
7511        assert!(
7512            cast_with_options(&binary_view_array, &DataType::Utf8View, &strict_options).is_err()
7513        );
7514
7515        let safe_options = CastOptions {
7516            safe: true,
7517            ..Default::default()
7518        };
7519
7520        let string_view_array =
7521            cast_with_options(&binary_view_array, &DataType::Utf8View, &safe_options).unwrap();
7522        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7523
7524        let values: Vec<_> = string_view_array.as_string_view().iter().collect();
7525
7526        assert_eq!(values, vec![Some("valid"), None, Some("utf8"), None]);
7527    }
7528
7529    #[test]
7530    fn test_string_to_view() {
7531        _test_string_to_view::<i32>();
7532        _test_string_to_view::<i64>();
7533    }
7534
7535    fn _test_string_to_view<O>()
7536    where
7537        O: OffsetSizeTrait,
7538    {
7539        let string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7540
7541        assert!(can_cast_types(
7542            string_array.data_type(),
7543            &DataType::Utf8View
7544        ));
7545
7546        assert!(can_cast_types(
7547            string_array.data_type(),
7548            &DataType::BinaryView
7549        ));
7550
7551        let string_view_array = cast(&string_array, &DataType::Utf8View).unwrap();
7552        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7553
7554        let binary_view_array = cast(&string_array, &DataType::BinaryView).unwrap();
7555        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7556
7557        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7558        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7559
7560        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7561        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7562    }
7563
7564    #[test]
7565    fn test_bianry_to_view() {
7566        _test_binary_to_view::<i32>();
7567        _test_binary_to_view::<i64>();
7568    }
7569
7570    fn _test_binary_to_view<O>()
7571    where
7572        O: OffsetSizeTrait,
7573    {
7574        let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7575
7576        assert!(can_cast_types(
7577            binary_array.data_type(),
7578            &DataType::Utf8View
7579        ));
7580
7581        assert!(can_cast_types(
7582            binary_array.data_type(),
7583            &DataType::BinaryView
7584        ));
7585
7586        let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
7587        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7588
7589        let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
7590        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7591
7592        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7593        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7594
7595        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7596        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7597    }
7598
7599    #[test]
7600    fn test_dict_to_view() {
7601        let values = StringArray::from_iter(VIEW_TEST_DATA);
7602        let keys = Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
7603        let string_dict_array =
7604            DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
7605        let typed_dict = string_dict_array.downcast_dict::<StringArray>().unwrap();
7606
7607        let string_view_array = {
7608            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7609            for v in typed_dict.into_iter() {
7610                builder.append_option(v);
7611            }
7612            builder.finish()
7613        };
7614        let expected_string_array_type = string_view_array.data_type();
7615        let casted_string_array = cast(&string_dict_array, expected_string_array_type).unwrap();
7616        assert_eq!(casted_string_array.data_type(), expected_string_array_type);
7617        assert_eq!(casted_string_array.as_ref(), &string_view_array);
7618
7619        let binary_buffer = cast(&typed_dict.values(), &DataType::Binary).unwrap();
7620        let binary_dict_array =
7621            DictionaryArray::<Int8Type>::new(typed_dict.keys().clone(), binary_buffer);
7622        let typed_binary_dict = binary_dict_array.downcast_dict::<BinaryArray>().unwrap();
7623
7624        let binary_view_array = {
7625            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7626            for v in typed_binary_dict.into_iter() {
7627                builder.append_option(v);
7628            }
7629            builder.finish()
7630        };
7631        let expected_binary_array_type = binary_view_array.data_type();
7632        let casted_binary_array = cast(&binary_dict_array, expected_binary_array_type).unwrap();
7633        assert_eq!(casted_binary_array.data_type(), expected_binary_array_type);
7634        assert_eq!(casted_binary_array.as_ref(), &binary_view_array);
7635    }
7636
7637    #[test]
7638    fn test_view_to_dict() {
7639        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7640        let string_dict_array: DictionaryArray<Int8Type> = VIEW_TEST_DATA.into_iter().collect();
7641        let casted_type = string_dict_array.data_type();
7642        let casted_dict_array = cast(&string_view_array, casted_type).unwrap();
7643        assert_eq!(casted_dict_array.data_type(), casted_type);
7644        assert_eq!(casted_dict_array.as_ref(), &string_dict_array);
7645
7646        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7647        let binary_dict_array = string_dict_array.downcast_dict::<StringArray>().unwrap();
7648        let binary_buffer = cast(&binary_dict_array.values(), &DataType::Binary).unwrap();
7649        let binary_dict_array =
7650            DictionaryArray::<Int8Type>::new(binary_dict_array.keys().clone(), binary_buffer);
7651        let casted_type = binary_dict_array.data_type();
7652        let casted_binary_array = cast(&binary_view_array, casted_type).unwrap();
7653        assert_eq!(casted_binary_array.data_type(), casted_type);
7654        assert_eq!(casted_binary_array.as_ref(), &binary_dict_array);
7655    }
7656
7657    #[test]
7658    fn test_view_to_string() {
7659        _test_view_to_string::<i32>();
7660        _test_view_to_string::<i64>();
7661    }
7662
7663    fn _test_view_to_string<O>()
7664    where
7665        O: OffsetSizeTrait,
7666    {
7667        let string_view_array = {
7668            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7669            for s in VIEW_TEST_DATA.iter() {
7670                builder.append_option(*s);
7671            }
7672            builder.finish()
7673        };
7674
7675        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7676
7677        let expected_string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7678        let expected_type = expected_string_array.data_type();
7679
7680        assert!(can_cast_types(string_view_array.data_type(), expected_type));
7681        assert!(can_cast_types(binary_view_array.data_type(), expected_type));
7682
7683        let string_view_casted_array = cast(&string_view_array, expected_type).unwrap();
7684        assert_eq!(string_view_casted_array.data_type(), expected_type);
7685        assert_eq!(string_view_casted_array.as_ref(), &expected_string_array);
7686
7687        let binary_view_casted_array = cast(&binary_view_array, expected_type).unwrap();
7688        assert_eq!(binary_view_casted_array.data_type(), expected_type);
7689        assert_eq!(binary_view_casted_array.as_ref(), &expected_string_array);
7690    }
7691
7692    #[test]
7693    fn test_view_to_binary() {
7694        _test_view_to_binary::<i32>();
7695        _test_view_to_binary::<i64>();
7696    }
7697
7698    fn _test_view_to_binary<O>()
7699    where
7700        O: OffsetSizeTrait,
7701    {
7702        let view_array = {
7703            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7704            for s in VIEW_TEST_DATA.iter() {
7705                builder.append_option(*s);
7706            }
7707            builder.finish()
7708        };
7709
7710        let expected_binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7711        let expected_type = expected_binary_array.data_type();
7712
7713        assert!(can_cast_types(view_array.data_type(), expected_type));
7714
7715        let binary_array = cast(&view_array, expected_type).unwrap();
7716        assert_eq!(binary_array.data_type(), expected_type);
7717
7718        assert_eq!(binary_array.as_ref(), &expected_binary_array);
7719    }
7720
7721    #[test]
7722    fn test_cast_from_f64() {
7723        let f64_values: Vec<f64> = vec![
7724            i64::MIN as f64,
7725            i32::MIN as f64,
7726            i16::MIN as f64,
7727            i8::MIN as f64,
7728            0_f64,
7729            u8::MAX as f64,
7730            u16::MAX as f64,
7731            u32::MAX as f64,
7732            u64::MAX as f64,
7733        ];
7734        let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values));
7735
7736        let f64_expected = vec![
7737            -9223372036854776000.0,
7738            -2147483648.0,
7739            -32768.0,
7740            -128.0,
7741            0.0,
7742            255.0,
7743            65535.0,
7744            4294967295.0,
7745            18446744073709552000.0,
7746        ];
7747        assert_eq!(
7748            f64_expected,
7749            get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
7750                .iter()
7751                .map(|i| i.parse::<f64>().unwrap())
7752                .collect::<Vec<f64>>()
7753        );
7754
7755        let f32_expected = vec![
7756            -9223372000000000000.0,
7757            -2147483600.0,
7758            -32768.0,
7759            -128.0,
7760            0.0,
7761            255.0,
7762            65535.0,
7763            4294967300.0,
7764            18446744000000000000.0,
7765        ];
7766        assert_eq!(
7767            f32_expected,
7768            get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
7769                .iter()
7770                .map(|i| i.parse::<f32>().unwrap())
7771                .collect::<Vec<f32>>()
7772        );
7773
7774        let f16_expected = vec![
7775            f16::from_f64(-9223372000000000000.0),
7776            f16::from_f64(-2147483600.0),
7777            f16::from_f64(-32768.0),
7778            f16::from_f64(-128.0),
7779            f16::from_f64(0.0),
7780            f16::from_f64(255.0),
7781            f16::from_f64(65535.0),
7782            f16::from_f64(4294967300.0),
7783            f16::from_f64(18446744000000000000.0),
7784        ];
7785        assert_eq!(
7786            f16_expected,
7787            get_cast_values::<Float16Type>(&f64_array, &DataType::Float16)
7788                .iter()
7789                .map(|i| i.parse::<f16>().unwrap())
7790                .collect::<Vec<f16>>()
7791        );
7792
7793        let i64_expected = vec![
7794            "-9223372036854775808",
7795            "-2147483648",
7796            "-32768",
7797            "-128",
7798            "0",
7799            "255",
7800            "65535",
7801            "4294967295",
7802            "null",
7803        ];
7804        assert_eq!(
7805            i64_expected,
7806            get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
7807        );
7808
7809        let i32_expected = vec![
7810            "null",
7811            "-2147483648",
7812            "-32768",
7813            "-128",
7814            "0",
7815            "255",
7816            "65535",
7817            "null",
7818            "null",
7819        ];
7820        assert_eq!(
7821            i32_expected,
7822            get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
7823        );
7824
7825        let i16_expected = vec![
7826            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7827        ];
7828        assert_eq!(
7829            i16_expected,
7830            get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
7831        );
7832
7833        let i8_expected = vec![
7834            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7835        ];
7836        assert_eq!(
7837            i8_expected,
7838            get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
7839        );
7840
7841        let u64_expected = vec![
7842            "null",
7843            "null",
7844            "null",
7845            "null",
7846            "0",
7847            "255",
7848            "65535",
7849            "4294967295",
7850            "null",
7851        ];
7852        assert_eq!(
7853            u64_expected,
7854            get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
7855        );
7856
7857        let u32_expected = vec![
7858            "null",
7859            "null",
7860            "null",
7861            "null",
7862            "0",
7863            "255",
7864            "65535",
7865            "4294967295",
7866            "null",
7867        ];
7868        assert_eq!(
7869            u32_expected,
7870            get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
7871        );
7872
7873        let u16_expected = vec![
7874            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7875        ];
7876        assert_eq!(
7877            u16_expected,
7878            get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
7879        );
7880
7881        let u8_expected = vec![
7882            "null", "null", "null", "null", "0", "255", "null", "null", "null",
7883        ];
7884        assert_eq!(
7885            u8_expected,
7886            get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
7887        );
7888    }
7889
7890    #[test]
7891    fn test_cast_from_f32() {
7892        let f32_values: Vec<f32> = vec![
7893            i32::MIN as f32,
7894            i32::MIN as f32,
7895            i16::MIN as f32,
7896            i8::MIN as f32,
7897            0_f32,
7898            u8::MAX as f32,
7899            u16::MAX as f32,
7900            u32::MAX as f32,
7901            u32::MAX as f32,
7902        ];
7903        let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values));
7904
7905        let f64_expected = vec![
7906            "-2147483648.0",
7907            "-2147483648.0",
7908            "-32768.0",
7909            "-128.0",
7910            "0.0",
7911            "255.0",
7912            "65535.0",
7913            "4294967296.0",
7914            "4294967296.0",
7915        ];
7916        assert_eq!(
7917            f64_expected,
7918            get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
7919        );
7920
7921        let f32_expected = vec![
7922            "-2147483600.0",
7923            "-2147483600.0",
7924            "-32768.0",
7925            "-128.0",
7926            "0.0",
7927            "255.0",
7928            "65535.0",
7929            "4294967300.0",
7930            "4294967300.0",
7931        ];
7932        assert_eq!(
7933            f32_expected,
7934            get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
7935        );
7936
7937        let f16_expected = vec![
7938            "-inf", "-inf", "-32768.0", "-128.0", "0.0", "255.0", "inf", "inf", "inf",
7939        ];
7940        assert_eq!(
7941            f16_expected,
7942            get_cast_values::<Float16Type>(&f32_array, &DataType::Float16)
7943        );
7944
7945        let i64_expected = vec![
7946            "-2147483648",
7947            "-2147483648",
7948            "-32768",
7949            "-128",
7950            "0",
7951            "255",
7952            "65535",
7953            "4294967296",
7954            "4294967296",
7955        ];
7956        assert_eq!(
7957            i64_expected,
7958            get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
7959        );
7960
7961        let i32_expected = vec![
7962            "-2147483648",
7963            "-2147483648",
7964            "-32768",
7965            "-128",
7966            "0",
7967            "255",
7968            "65535",
7969            "null",
7970            "null",
7971        ];
7972        assert_eq!(
7973            i32_expected,
7974            get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
7975        );
7976
7977        let i16_expected = vec![
7978            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7979        ];
7980        assert_eq!(
7981            i16_expected,
7982            get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
7983        );
7984
7985        let i8_expected = vec![
7986            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7987        ];
7988        assert_eq!(
7989            i8_expected,
7990            get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
7991        );
7992
7993        let u64_expected = vec![
7994            "null",
7995            "null",
7996            "null",
7997            "null",
7998            "0",
7999            "255",
8000            "65535",
8001            "4294967296",
8002            "4294967296",
8003        ];
8004        assert_eq!(
8005            u64_expected,
8006            get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
8007        );
8008
8009        let u32_expected = vec![
8010            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
8011        ];
8012        assert_eq!(
8013            u32_expected,
8014            get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
8015        );
8016
8017        let u16_expected = vec![
8018            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
8019        ];
8020        assert_eq!(
8021            u16_expected,
8022            get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
8023        );
8024
8025        let u8_expected = vec![
8026            "null", "null", "null", "null", "0", "255", "null", "null", "null",
8027        ];
8028        assert_eq!(
8029            u8_expected,
8030            get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
8031        );
8032    }
8033
8034    #[test]
8035    fn test_cast_from_uint64() {
8036        let u64_values: Vec<u64> = vec![
8037            0,
8038            u8::MAX as u64,
8039            u16::MAX as u64,
8040            u32::MAX as u64,
8041            u64::MAX,
8042        ];
8043        let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values));
8044
8045        let f64_expected = vec![0.0, 255.0, 65535.0, 4294967295.0, 18446744073709552000.0];
8046        assert_eq!(
8047            f64_expected,
8048            get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
8049                .iter()
8050                .map(|i| i.parse::<f64>().unwrap())
8051                .collect::<Vec<f64>>()
8052        );
8053
8054        let f32_expected = vec![0.0, 255.0, 65535.0, 4294967300.0, 18446744000000000000.0];
8055        assert_eq!(
8056            f32_expected,
8057            get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
8058                .iter()
8059                .map(|i| i.parse::<f32>().unwrap())
8060                .collect::<Vec<f32>>()
8061        );
8062
8063        let f16_expected = vec![
8064            f16::from_f64(0.0),
8065            f16::from_f64(255.0),
8066            f16::from_f64(65535.0),
8067            f16::from_f64(4294967300.0),
8068            f16::from_f64(18446744000000000000.0),
8069        ];
8070        assert_eq!(
8071            f16_expected,
8072            get_cast_values::<Float16Type>(&u64_array, &DataType::Float16)
8073                .iter()
8074                .map(|i| i.parse::<f16>().unwrap())
8075                .collect::<Vec<f16>>()
8076        );
8077
8078        let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
8079        assert_eq!(
8080            i64_expected,
8081            get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
8082        );
8083
8084        let i32_expected = vec!["0", "255", "65535", "null", "null"];
8085        assert_eq!(
8086            i32_expected,
8087            get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
8088        );
8089
8090        let i16_expected = vec!["0", "255", "null", "null", "null"];
8091        assert_eq!(
8092            i16_expected,
8093            get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
8094        );
8095
8096        let i8_expected = vec!["0", "null", "null", "null", "null"];
8097        assert_eq!(
8098            i8_expected,
8099            get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
8100        );
8101
8102        let u64_expected = vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
8103        assert_eq!(
8104            u64_expected,
8105            get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
8106        );
8107
8108        let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
8109        assert_eq!(
8110            u32_expected,
8111            get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
8112        );
8113
8114        let u16_expected = vec!["0", "255", "65535", "null", "null"];
8115        assert_eq!(
8116            u16_expected,
8117            get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
8118        );
8119
8120        let u8_expected = vec!["0", "255", "null", "null", "null"];
8121        assert_eq!(
8122            u8_expected,
8123            get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
8124        );
8125    }
8126
8127    #[test]
8128    fn test_cast_from_uint32() {
8129        let u32_values: Vec<u32> = vec![0, u8::MAX as u32, u16::MAX as u32, u32::MAX];
8130        let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values));
8131
8132        let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
8133        assert_eq!(
8134            f64_expected,
8135            get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
8136        );
8137
8138        let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
8139        assert_eq!(
8140            f32_expected,
8141            get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
8142        );
8143
8144        let f16_expected = vec!["0.0", "255.0", "inf", "inf"];
8145        assert_eq!(
8146            f16_expected,
8147            get_cast_values::<Float16Type>(&u32_array, &DataType::Float16)
8148        );
8149
8150        let i64_expected = vec!["0", "255", "65535", "4294967295"];
8151        assert_eq!(
8152            i64_expected,
8153            get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
8154        );
8155
8156        let i32_expected = vec!["0", "255", "65535", "null"];
8157        assert_eq!(
8158            i32_expected,
8159            get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
8160        );
8161
8162        let i16_expected = vec!["0", "255", "null", "null"];
8163        assert_eq!(
8164            i16_expected,
8165            get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
8166        );
8167
8168        let i8_expected = vec!["0", "null", "null", "null"];
8169        assert_eq!(
8170            i8_expected,
8171            get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
8172        );
8173
8174        let u64_expected = vec!["0", "255", "65535", "4294967295"];
8175        assert_eq!(
8176            u64_expected,
8177            get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
8178        );
8179
8180        let u32_expected = vec!["0", "255", "65535", "4294967295"];
8181        assert_eq!(
8182            u32_expected,
8183            get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
8184        );
8185
8186        let u16_expected = vec!["0", "255", "65535", "null"];
8187        assert_eq!(
8188            u16_expected,
8189            get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
8190        );
8191
8192        let u8_expected = vec!["0", "255", "null", "null"];
8193        assert_eq!(
8194            u8_expected,
8195            get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
8196        );
8197    }
8198
8199    #[test]
8200    fn test_cast_from_uint16() {
8201        let u16_values: Vec<u16> = vec![0, u8::MAX as u16, u16::MAX];
8202        let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values));
8203
8204        let f64_expected = vec!["0.0", "255.0", "65535.0"];
8205        assert_eq!(
8206            f64_expected,
8207            get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
8208        );
8209
8210        let f32_expected = vec!["0.0", "255.0", "65535.0"];
8211        assert_eq!(
8212            f32_expected,
8213            get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
8214        );
8215
8216        let f16_expected = vec!["0.0", "255.0", "inf"];
8217        assert_eq!(
8218            f16_expected,
8219            get_cast_values::<Float16Type>(&u16_array, &DataType::Float16)
8220        );
8221
8222        let i64_expected = vec!["0", "255", "65535"];
8223        assert_eq!(
8224            i64_expected,
8225            get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
8226        );
8227
8228        let i32_expected = vec!["0", "255", "65535"];
8229        assert_eq!(
8230            i32_expected,
8231            get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
8232        );
8233
8234        let i16_expected = vec!["0", "255", "null"];
8235        assert_eq!(
8236            i16_expected,
8237            get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
8238        );
8239
8240        let i8_expected = vec!["0", "null", "null"];
8241        assert_eq!(
8242            i8_expected,
8243            get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
8244        );
8245
8246        let u64_expected = vec!["0", "255", "65535"];
8247        assert_eq!(
8248            u64_expected,
8249            get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
8250        );
8251
8252        let u32_expected = vec!["0", "255", "65535"];
8253        assert_eq!(
8254            u32_expected,
8255            get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
8256        );
8257
8258        let u16_expected = vec!["0", "255", "65535"];
8259        assert_eq!(
8260            u16_expected,
8261            get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
8262        );
8263
8264        let u8_expected = vec!["0", "255", "null"];
8265        assert_eq!(
8266            u8_expected,
8267            get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
8268        );
8269    }
8270
8271    #[test]
8272    fn test_cast_from_uint8() {
8273        let u8_values: Vec<u8> = vec![0, u8::MAX];
8274        let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values));
8275
8276        let f64_expected = vec!["0.0", "255.0"];
8277        assert_eq!(
8278            f64_expected,
8279            get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
8280        );
8281
8282        let f32_expected = vec!["0.0", "255.0"];
8283        assert_eq!(
8284            f32_expected,
8285            get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
8286        );
8287
8288        let f16_expected = vec!["0.0", "255.0"];
8289        assert_eq!(
8290            f16_expected,
8291            get_cast_values::<Float16Type>(&u8_array, &DataType::Float16)
8292        );
8293
8294        let i64_expected = vec!["0", "255"];
8295        assert_eq!(
8296            i64_expected,
8297            get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
8298        );
8299
8300        let i32_expected = vec!["0", "255"];
8301        assert_eq!(
8302            i32_expected,
8303            get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
8304        );
8305
8306        let i16_expected = vec!["0", "255"];
8307        assert_eq!(
8308            i16_expected,
8309            get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
8310        );
8311
8312        let i8_expected = vec!["0", "null"];
8313        assert_eq!(
8314            i8_expected,
8315            get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
8316        );
8317
8318        let u64_expected = vec!["0", "255"];
8319        assert_eq!(
8320            u64_expected,
8321            get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
8322        );
8323
8324        let u32_expected = vec!["0", "255"];
8325        assert_eq!(
8326            u32_expected,
8327            get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
8328        );
8329
8330        let u16_expected = vec!["0", "255"];
8331        assert_eq!(
8332            u16_expected,
8333            get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
8334        );
8335
8336        let u8_expected = vec!["0", "255"];
8337        assert_eq!(
8338            u8_expected,
8339            get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
8340        );
8341    }
8342
8343    #[test]
8344    fn test_cast_from_int64() {
8345        let i64_values: Vec<i64> = vec![
8346            i64::MIN,
8347            i32::MIN as i64,
8348            i16::MIN as i64,
8349            i8::MIN as i64,
8350            0,
8351            i8::MAX as i64,
8352            i16::MAX as i64,
8353            i32::MAX as i64,
8354            i64::MAX,
8355        ];
8356        let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values));
8357
8358        let f64_expected = vec![
8359            -9223372036854776000.0,
8360            -2147483648.0,
8361            -32768.0,
8362            -128.0,
8363            0.0,
8364            127.0,
8365            32767.0,
8366            2147483647.0,
8367            9223372036854776000.0,
8368        ];
8369        assert_eq!(
8370            f64_expected,
8371            get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
8372                .iter()
8373                .map(|i| i.parse::<f64>().unwrap())
8374                .collect::<Vec<f64>>()
8375        );
8376
8377        let f32_expected = vec![
8378            -9223372000000000000.0,
8379            -2147483600.0,
8380            -32768.0,
8381            -128.0,
8382            0.0,
8383            127.0,
8384            32767.0,
8385            2147483600.0,
8386            9223372000000000000.0,
8387        ];
8388        assert_eq!(
8389            f32_expected,
8390            get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
8391                .iter()
8392                .map(|i| i.parse::<f32>().unwrap())
8393                .collect::<Vec<f32>>()
8394        );
8395
8396        let f16_expected = vec![
8397            f16::from_f64(-9223372000000000000.0),
8398            f16::from_f64(-2147483600.0),
8399            f16::from_f64(-32768.0),
8400            f16::from_f64(-128.0),
8401            f16::from_f64(0.0),
8402            f16::from_f64(127.0),
8403            f16::from_f64(32767.0),
8404            f16::from_f64(2147483600.0),
8405            f16::from_f64(9223372000000000000.0),
8406        ];
8407        assert_eq!(
8408            f16_expected,
8409            get_cast_values::<Float16Type>(&i64_array, &DataType::Float16)
8410                .iter()
8411                .map(|i| i.parse::<f16>().unwrap())
8412                .collect::<Vec<f16>>()
8413        );
8414
8415        let i64_expected = vec![
8416            "-9223372036854775808",
8417            "-2147483648",
8418            "-32768",
8419            "-128",
8420            "0",
8421            "127",
8422            "32767",
8423            "2147483647",
8424            "9223372036854775807",
8425        ];
8426        assert_eq!(
8427            i64_expected,
8428            get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
8429        );
8430
8431        let i32_expected = vec![
8432            "null",
8433            "-2147483648",
8434            "-32768",
8435            "-128",
8436            "0",
8437            "127",
8438            "32767",
8439            "2147483647",
8440            "null",
8441        ];
8442        assert_eq!(
8443            i32_expected,
8444            get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
8445        );
8446
8447        assert_eq!(
8448            i32_expected,
8449            get_cast_values::<Date32Type>(&i64_array, &DataType::Date32)
8450        );
8451
8452        let i16_expected = vec![
8453            "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
8454        ];
8455        assert_eq!(
8456            i16_expected,
8457            get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
8458        );
8459
8460        let i8_expected = vec![
8461            "null", "null", "null", "-128", "0", "127", "null", "null", "null",
8462        ];
8463        assert_eq!(
8464            i8_expected,
8465            get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
8466        );
8467
8468        let u64_expected = vec![
8469            "null",
8470            "null",
8471            "null",
8472            "null",
8473            "0",
8474            "127",
8475            "32767",
8476            "2147483647",
8477            "9223372036854775807",
8478        ];
8479        assert_eq!(
8480            u64_expected,
8481            get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
8482        );
8483
8484        let u32_expected = vec![
8485            "null",
8486            "null",
8487            "null",
8488            "null",
8489            "0",
8490            "127",
8491            "32767",
8492            "2147483647",
8493            "null",
8494        ];
8495        assert_eq!(
8496            u32_expected,
8497            get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
8498        );
8499
8500        let u16_expected = vec![
8501            "null", "null", "null", "null", "0", "127", "32767", "null", "null",
8502        ];
8503        assert_eq!(
8504            u16_expected,
8505            get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
8506        );
8507
8508        let u8_expected = vec![
8509            "null", "null", "null", "null", "0", "127", "null", "null", "null",
8510        ];
8511        assert_eq!(
8512            u8_expected,
8513            get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
8514        );
8515    }
8516
8517    #[test]
8518    fn test_cast_from_int32() {
8519        let i32_values: Vec<i32> = vec![
8520            i32::MIN,
8521            i16::MIN as i32,
8522            i8::MIN as i32,
8523            0,
8524            i8::MAX as i32,
8525            i16::MAX as i32,
8526            i32::MAX,
8527        ];
8528        let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values));
8529
8530        let f64_expected = vec![
8531            "-2147483648.0",
8532            "-32768.0",
8533            "-128.0",
8534            "0.0",
8535            "127.0",
8536            "32767.0",
8537            "2147483647.0",
8538        ];
8539        assert_eq!(
8540            f64_expected,
8541            get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
8542        );
8543
8544        let f32_expected = vec![
8545            "-2147483600.0",
8546            "-32768.0",
8547            "-128.0",
8548            "0.0",
8549            "127.0",
8550            "32767.0",
8551            "2147483600.0",
8552        ];
8553        assert_eq!(
8554            f32_expected,
8555            get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
8556        );
8557
8558        let f16_expected = vec![
8559            f16::from_f64(-2147483600.0),
8560            f16::from_f64(-32768.0),
8561            f16::from_f64(-128.0),
8562            f16::from_f64(0.0),
8563            f16::from_f64(127.0),
8564            f16::from_f64(32767.0),
8565            f16::from_f64(2147483600.0),
8566        ];
8567        assert_eq!(
8568            f16_expected,
8569            get_cast_values::<Float16Type>(&i32_array, &DataType::Float16)
8570                .iter()
8571                .map(|i| i.parse::<f16>().unwrap())
8572                .collect::<Vec<f16>>()
8573        );
8574
8575        let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
8576        assert_eq!(
8577            i16_expected,
8578            get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
8579        );
8580
8581        let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
8582        assert_eq!(
8583            i8_expected,
8584            get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
8585        );
8586
8587        let u64_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8588        assert_eq!(
8589            u64_expected,
8590            get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
8591        );
8592
8593        let u32_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8594        assert_eq!(
8595            u32_expected,
8596            get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
8597        );
8598
8599        let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
8600        assert_eq!(
8601            u16_expected,
8602            get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
8603        );
8604
8605        let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
8606        assert_eq!(
8607            u8_expected,
8608            get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
8609        );
8610
8611        // The date32 to date64 cast increases the numerical values in order to keep the same dates.
8612        let i64_expected = vec![
8613            "-185542587187200000",
8614            "-2831155200000",
8615            "-11059200000",
8616            "0",
8617            "10972800000",
8618            "2831068800000",
8619            "185542587100800000",
8620        ];
8621        assert_eq!(
8622            i64_expected,
8623            get_cast_values::<Date64Type>(&i32_array, &DataType::Date64)
8624        );
8625    }
8626
8627    #[test]
8628    fn test_cast_from_int16() {
8629        let i16_values: Vec<i16> = vec![i16::MIN, i8::MIN as i16, 0, i8::MAX as i16, i16::MAX];
8630        let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values));
8631
8632        let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8633        assert_eq!(
8634            f64_expected,
8635            get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
8636        );
8637
8638        let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8639        assert_eq!(
8640            f32_expected,
8641            get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
8642        );
8643
8644        let f16_expected = vec![
8645            f16::from_f64(-32768.0),
8646            f16::from_f64(-128.0),
8647            f16::from_f64(0.0),
8648            f16::from_f64(127.0),
8649            f16::from_f64(32767.0),
8650        ];
8651        assert_eq!(
8652            f16_expected,
8653            get_cast_values::<Float16Type>(&i16_array, &DataType::Float16)
8654                .iter()
8655                .map(|i| i.parse::<f16>().unwrap())
8656                .collect::<Vec<f16>>()
8657        );
8658
8659        let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
8660        assert_eq!(
8661            i64_expected,
8662            get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
8663        );
8664
8665        let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
8666        assert_eq!(
8667            i32_expected,
8668            get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
8669        );
8670
8671        let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
8672        assert_eq!(
8673            i16_expected,
8674            get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
8675        );
8676
8677        let i8_expected = vec!["null", "-128", "0", "127", "null"];
8678        assert_eq!(
8679            i8_expected,
8680            get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
8681        );
8682
8683        let u64_expected = vec!["null", "null", "0", "127", "32767"];
8684        assert_eq!(
8685            u64_expected,
8686            get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
8687        );
8688
8689        let u32_expected = vec!["null", "null", "0", "127", "32767"];
8690        assert_eq!(
8691            u32_expected,
8692            get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
8693        );
8694
8695        let u16_expected = vec!["null", "null", "0", "127", "32767"];
8696        assert_eq!(
8697            u16_expected,
8698            get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
8699        );
8700
8701        let u8_expected = vec!["null", "null", "0", "127", "null"];
8702        assert_eq!(
8703            u8_expected,
8704            get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
8705        );
8706    }
8707
8708    #[test]
8709    fn test_cast_from_date32() {
8710        let i32_values: Vec<i32> = vec![
8711            i32::MIN,
8712            i16::MIN as i32,
8713            i8::MIN as i32,
8714            0,
8715            i8::MAX as i32,
8716            i16::MAX as i32,
8717            i32::MAX,
8718        ];
8719        let date32_array: ArrayRef = Arc::new(Date32Array::from(i32_values));
8720
8721        let i64_expected = vec![
8722            "-2147483648",
8723            "-32768",
8724            "-128",
8725            "0",
8726            "127",
8727            "32767",
8728            "2147483647",
8729        ];
8730        assert_eq!(
8731            i64_expected,
8732            get_cast_values::<Int64Type>(&date32_array, &DataType::Int64)
8733        );
8734    }
8735
8736    #[test]
8737    fn test_cast_from_int8() {
8738        let i8_values: Vec<i8> = vec![i8::MIN, 0, i8::MAX];
8739        let i8_array = Int8Array::from(i8_values);
8740
8741        let f64_expected = vec!["-128.0", "0.0", "127.0"];
8742        assert_eq!(
8743            f64_expected,
8744            get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
8745        );
8746
8747        let f32_expected = vec!["-128.0", "0.0", "127.0"];
8748        assert_eq!(
8749            f32_expected,
8750            get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
8751        );
8752
8753        let f16_expected = vec!["-128.0", "0.0", "127.0"];
8754        assert_eq!(
8755            f16_expected,
8756            get_cast_values::<Float16Type>(&i8_array, &DataType::Float16)
8757        );
8758
8759        let i64_expected = vec!["-128", "0", "127"];
8760        assert_eq!(
8761            i64_expected,
8762            get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
8763        );
8764
8765        let i32_expected = vec!["-128", "0", "127"];
8766        assert_eq!(
8767            i32_expected,
8768            get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
8769        );
8770
8771        let i16_expected = vec!["-128", "0", "127"];
8772        assert_eq!(
8773            i16_expected,
8774            get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
8775        );
8776
8777        let i8_expected = vec!["-128", "0", "127"];
8778        assert_eq!(
8779            i8_expected,
8780            get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
8781        );
8782
8783        let u64_expected = vec!["null", "0", "127"];
8784        assert_eq!(
8785            u64_expected,
8786            get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
8787        );
8788
8789        let u32_expected = vec!["null", "0", "127"];
8790        assert_eq!(
8791            u32_expected,
8792            get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
8793        );
8794
8795        let u16_expected = vec!["null", "0", "127"];
8796        assert_eq!(
8797            u16_expected,
8798            get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
8799        );
8800
8801        let u8_expected = vec!["null", "0", "127"];
8802        assert_eq!(
8803            u8_expected,
8804            get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
8805        );
8806    }
8807
8808    /// Convert `array` into a vector of strings by casting to data type dt
8809    fn get_cast_values<T>(array: &dyn Array, dt: &DataType) -> Vec<String>
8810    where
8811        T: ArrowPrimitiveType,
8812    {
8813        let c = cast(array, dt).unwrap();
8814        let a = c.as_primitive::<T>();
8815        let mut v: Vec<String> = vec![];
8816        for i in 0..array.len() {
8817            if a.is_null(i) {
8818                v.push("null".to_string())
8819            } else {
8820                v.push(format!("{:?}", a.value(i)));
8821            }
8822        }
8823        v
8824    }
8825
8826    #[test]
8827    fn test_cast_utf8_dict() {
8828        // FROM a dictionary with of Utf8 values
8829        let mut builder = StringDictionaryBuilder::<Int8Type>::new();
8830        builder.append("one").unwrap();
8831        builder.append_null();
8832        builder.append("three").unwrap();
8833        let array: ArrayRef = Arc::new(builder.finish());
8834
8835        let expected = vec!["one", "null", "three"];
8836
8837        // Test casting TO StringArray
8838        let cast_type = Utf8;
8839        let cast_array = cast(&array, &cast_type).expect("cast to UTF-8 failed");
8840        assert_eq!(cast_array.data_type(), &cast_type);
8841        assert_eq!(array_to_strings(&cast_array), expected);
8842
8843        // Test casting TO Dictionary (with different index sizes)
8844
8845        let cast_type = Dictionary(Box::new(Int16), Box::new(Utf8));
8846        let cast_array = cast(&array, &cast_type).expect("cast failed");
8847        assert_eq!(cast_array.data_type(), &cast_type);
8848        assert_eq!(array_to_strings(&cast_array), expected);
8849
8850        let cast_type = Dictionary(Box::new(Int32), Box::new(Utf8));
8851        let cast_array = cast(&array, &cast_type).expect("cast failed");
8852        assert_eq!(cast_array.data_type(), &cast_type);
8853        assert_eq!(array_to_strings(&cast_array), expected);
8854
8855        let cast_type = Dictionary(Box::new(Int64), Box::new(Utf8));
8856        let cast_array = cast(&array, &cast_type).expect("cast failed");
8857        assert_eq!(cast_array.data_type(), &cast_type);
8858        assert_eq!(array_to_strings(&cast_array), expected);
8859
8860        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8861        let cast_array = cast(&array, &cast_type).expect("cast failed");
8862        assert_eq!(cast_array.data_type(), &cast_type);
8863        assert_eq!(array_to_strings(&cast_array), expected);
8864
8865        let cast_type = Dictionary(Box::new(UInt16), Box::new(Utf8));
8866        let cast_array = cast(&array, &cast_type).expect("cast failed");
8867        assert_eq!(cast_array.data_type(), &cast_type);
8868        assert_eq!(array_to_strings(&cast_array), expected);
8869
8870        let cast_type = Dictionary(Box::new(UInt32), Box::new(Utf8));
8871        let cast_array = cast(&array, &cast_type).expect("cast failed");
8872        assert_eq!(cast_array.data_type(), &cast_type);
8873        assert_eq!(array_to_strings(&cast_array), expected);
8874
8875        let cast_type = Dictionary(Box::new(UInt64), Box::new(Utf8));
8876        let cast_array = cast(&array, &cast_type).expect("cast failed");
8877        assert_eq!(cast_array.data_type(), &cast_type);
8878        assert_eq!(array_to_strings(&cast_array), expected);
8879    }
8880
8881    #[test]
8882    fn test_cast_dict_to_dict_bad_index_value_primitive() {
8883        // test converting from an array that has indexes of a type
8884        // that are out of bounds for a particular other kind of
8885        // index.
8886
8887        let mut builder = PrimitiveDictionaryBuilder::<Int32Type, Int64Type>::new();
8888
8889        // add 200 distinct values (which can be stored by a
8890        // dictionary indexed by int32, but not a dictionary indexed
8891        // with int8)
8892        for i in 0..200 {
8893            builder.append(i).unwrap();
8894        }
8895        let array: ArrayRef = Arc::new(builder.finish());
8896
8897        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8898        let res = cast(&array, &cast_type);
8899        assert!(res.is_err());
8900        let actual_error = format!("{res:?}");
8901        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8902        assert!(
8903            actual_error.contains(expected_error),
8904            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8905        );
8906    }
8907
8908    #[test]
8909    fn test_cast_dict_to_dict_bad_index_value_utf8() {
8910        // Same test as test_cast_dict_to_dict_bad_index_value but use
8911        // string values (and encode the expected behavior here);
8912
8913        let mut builder = StringDictionaryBuilder::<Int32Type>::new();
8914
8915        // add 200 distinct values (which can be stored by a
8916        // dictionary indexed by int32, but not a dictionary indexed
8917        // with int8)
8918        for i in 0..200 {
8919            let val = format!("val{i}");
8920            builder.append(&val).unwrap();
8921        }
8922        let array = builder.finish();
8923
8924        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8925        let res = cast(&array, &cast_type);
8926        assert!(res.is_err());
8927        let actual_error = format!("{res:?}");
8928        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8929        assert!(
8930            actual_error.contains(expected_error),
8931            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8932        );
8933    }
8934
8935    #[test]
8936    fn test_cast_nested_dictionary_to_dictionary_reuses_values() {
8937        let inner = DictionaryArray::<Int32Type>::new(
8938            Int32Array::from(vec![Some(0), None, Some(1)]),
8939            Arc::new(StringArray::from(vec!["x", "y"])),
8940        );
8941        let nested = DictionaryArray::<Int32Type>::new(
8942            Int32Array::from(vec![Some(0), Some(1), Some(2), None, Some(0)]),
8943            Arc::new(inner),
8944        );
8945
8946        let result = cast(&nested, &Dictionary(Box::new(Int32), Box::new(Utf8))).unwrap();
8947        let result = result.as_dictionary::<Int32Type>();
8948
8949        assert_eq!(
8950            result.keys(),
8951            &Int32Array::from(vec![Some(0), None, Some(1), None, Some(0)])
8952        );
8953        assert_eq!(
8954            result.values().as_string::<i32>(),
8955            &StringArray::from(vec!["x", "y"])
8956        );
8957        let logical: Vec<Option<&str>> = result
8958            .downcast_dict::<StringArray>()
8959            .unwrap()
8960            .into_iter()
8961            .collect();
8962        assert_eq!(logical, vec![Some("x"), None, Some("y"), None, Some("x")]);
8963    }
8964
8965    #[test]
8966    fn test_cast_primitive_dict() {
8967        // FROM a dictionary with of INT32 values
8968        let mut builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
8969        builder.append(1).unwrap();
8970        builder.append_null();
8971        builder.append(3).unwrap();
8972        let array: ArrayRef = Arc::new(builder.finish());
8973
8974        let expected = vec!["1", "null", "3"];
8975
8976        // Test casting TO PrimitiveArray, different dictionary type
8977        let cast_array = cast(&array, &Utf8).expect("cast to UTF-8 failed");
8978        assert_eq!(array_to_strings(&cast_array), expected);
8979        assert_eq!(cast_array.data_type(), &Utf8);
8980
8981        let cast_array = cast(&array, &Int64).expect("cast to int64 failed");
8982        assert_eq!(array_to_strings(&cast_array), expected);
8983        assert_eq!(cast_array.data_type(), &Int64);
8984    }
8985
8986    #[test]
8987    fn test_cast_primitive_array_to_dict() {
8988        let mut builder = PrimitiveBuilder::<Int32Type>::new();
8989        builder.append_value(1);
8990        builder.append_null();
8991        builder.append_value(3);
8992        let array: ArrayRef = Arc::new(builder.finish());
8993
8994        let expected = vec!["1", "null", "3"];
8995
8996        // Cast to a dictionary (same value type, Int32)
8997        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int32));
8998        let cast_array = cast(&array, &cast_type).expect("cast failed");
8999        assert_eq!(cast_array.data_type(), &cast_type);
9000        assert_eq!(array_to_strings(&cast_array), expected);
9001
9002        // Cast to a dictionary (different value type, Int8)
9003        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int8));
9004        let cast_array = cast(&array, &cast_type).expect("cast failed");
9005        assert_eq!(cast_array.data_type(), &cast_type);
9006        assert_eq!(array_to_strings(&cast_array), expected);
9007    }
9008
9009    #[test]
9010    fn test_cast_time_array_to_dict() {
9011        use DataType::*;
9012
9013        let array = Arc::new(Date32Array::from(vec![Some(1000), None, Some(2000)])) as ArrayRef;
9014
9015        let expected = vec!["1972-09-27", "null", "1975-06-24"];
9016
9017        let cast_type = Dictionary(Box::new(UInt8), Box::new(Date32));
9018        let cast_array = cast(&array, &cast_type).expect("cast failed");
9019        assert_eq!(cast_array.data_type(), &cast_type);
9020        assert_eq!(array_to_strings(&cast_array), expected);
9021    }
9022
9023    #[test]
9024    fn test_cast_timestamp_array_to_dict() {
9025        use DataType::*;
9026
9027        let array = Arc::new(
9028            TimestampSecondArray::from(vec![Some(1000), None, Some(2000)]).with_timezone_utc(),
9029        ) as ArrayRef;
9030
9031        let expected = vec!["1970-01-01T00:16:40", "null", "1970-01-01T00:33:20"];
9032
9033        let cast_type = Dictionary(Box::new(UInt8), Box::new(Timestamp(TimeUnit::Second, None)));
9034        let cast_array = cast(&array, &cast_type).expect("cast failed");
9035        assert_eq!(cast_array.data_type(), &cast_type);
9036        assert_eq!(array_to_strings(&cast_array), expected);
9037    }
9038
9039    #[test]
9040    fn test_cast_string_array_to_dict() {
9041        use DataType::*;
9042
9043        let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) as ArrayRef;
9044
9045        let expected = vec!["one", "null", "three"];
9046
9047        // Cast to a dictionary (same value type, Utf8)
9048        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
9049        let cast_array = cast(&array, &cast_type).expect("cast failed");
9050        assert_eq!(cast_array.data_type(), &cast_type);
9051        assert_eq!(array_to_strings(&cast_array), expected);
9052    }
9053
9054    #[test]
9055    fn test_cast_null_array_to_from_decimal_array() {
9056        let data_type = DataType::Decimal128(12, 4);
9057        let array = new_null_array(&DataType::Null, 4);
9058        assert_eq!(array.data_type(), &DataType::Null);
9059        let cast_array = cast(&array, &data_type).expect("cast failed");
9060        assert_eq!(cast_array.data_type(), &data_type);
9061        for i in 0..4 {
9062            assert!(cast_array.is_null(i));
9063        }
9064
9065        let array = new_null_array(&data_type, 4);
9066        assert_eq!(array.data_type(), &data_type);
9067        let cast_array = cast(&array, &DataType::Null).expect("cast failed");
9068        assert_eq!(cast_array.data_type(), &DataType::Null);
9069        assert_eq!(cast_array.len(), 4);
9070        assert_eq!(cast_array.logical_nulls().unwrap().null_count(), 4);
9071    }
9072
9073    #[test]
9074    fn test_cast_null_array_from_and_to_primitive_array() {
9075        macro_rules! typed_test {
9076            ($ARR_TYPE:ident, $DATATYPE:ident, $TYPE:tt) => {{
9077                {
9078                    let array = Arc::new(NullArray::new(6)) as ArrayRef;
9079                    let expected = $ARR_TYPE::from(vec![None; 6]);
9080                    let cast_type = DataType::$DATATYPE;
9081                    let cast_array = cast(&array, &cast_type).expect("cast failed");
9082                    let cast_array = cast_array.as_primitive::<$TYPE>();
9083                    assert_eq!(cast_array.data_type(), &cast_type);
9084                    assert_eq!(cast_array, &expected);
9085                }
9086            }};
9087        }
9088
9089        typed_test!(Int16Array, Int16, Int16Type);
9090        typed_test!(Int32Array, Int32, Int32Type);
9091        typed_test!(Int64Array, Int64, Int64Type);
9092
9093        typed_test!(UInt16Array, UInt16, UInt16Type);
9094        typed_test!(UInt32Array, UInt32, UInt32Type);
9095        typed_test!(UInt64Array, UInt64, UInt64Type);
9096
9097        typed_test!(Float16Array, Float16, Float16Type);
9098        typed_test!(Float32Array, Float32, Float32Type);
9099        typed_test!(Float64Array, Float64, Float64Type);
9100
9101        typed_test!(Date32Array, Date32, Date32Type);
9102        typed_test!(Date64Array, Date64, Date64Type);
9103    }
9104
9105    fn cast_from_null_to_other_base(data_type: &DataType, is_complex: bool) {
9106        // Cast from null to data_type
9107        let array = new_null_array(&DataType::Null, 4);
9108        assert_eq!(array.data_type(), &DataType::Null);
9109        let cast_array = cast(&array, data_type).expect("cast failed");
9110        assert_eq!(cast_array.data_type(), data_type);
9111        for i in 0..4 {
9112            if is_complex {
9113                assert!(cast_array.logical_nulls().unwrap().is_null(i));
9114            } else {
9115                assert!(cast_array.is_null(i));
9116            }
9117        }
9118    }
9119
9120    fn cast_from_null_to_other(data_type: &DataType) {
9121        cast_from_null_to_other_base(data_type, false);
9122    }
9123
9124    fn cast_from_null_to_other_complex(data_type: &DataType) {
9125        cast_from_null_to_other_base(data_type, true);
9126    }
9127
9128    #[test]
9129    fn test_cast_null_from_and_to_variable_sized() {
9130        cast_from_null_to_other(&DataType::Utf8);
9131        cast_from_null_to_other(&DataType::LargeUtf8);
9132        cast_from_null_to_other(&DataType::Binary);
9133        cast_from_null_to_other(&DataType::LargeBinary);
9134    }
9135
9136    #[test]
9137    fn test_cast_null_from_and_to_nested_type() {
9138        // Cast null from and to map
9139        let data_type = DataType::Map(
9140            Arc::new(Field::new_struct(
9141                Field::MAP_ENTRIES_FIELD_DEFAULT_NAME,
9142                vec![
9143                    Field::new(Field::MAP_KEY_FIELD_DEFAULT_NAME, DataType::Utf8, false),
9144                    Field::new(Field::MAP_VALUE_FIELD_DEFAULT_NAME, DataType::Int32, true),
9145                ],
9146                false,
9147            )),
9148            false,
9149        );
9150        cast_from_null_to_other(&data_type);
9151
9152        // Cast null from and to list
9153        let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
9154        cast_from_null_to_other(&data_type);
9155        let data_type = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
9156        cast_from_null_to_other(&data_type);
9157        let data_type =
9158            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
9159        cast_from_null_to_other(&data_type);
9160
9161        // Cast null from and to dictionary
9162        let values = vec![None, None, None, None] as Vec<Option<&str>>;
9163        let array: DictionaryArray<Int8Type> = values.into_iter().collect();
9164        let array = Arc::new(array) as ArrayRef;
9165        let data_type = array.data_type().to_owned();
9166        cast_from_null_to_other(&data_type);
9167
9168        // Cast null from and to struct
9169        let data_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
9170        cast_from_null_to_other(&data_type);
9171
9172        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
9173        cast_from_null_to_other(&target_type);
9174
9175        let target_type =
9176            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
9177        cast_from_null_to_other(&target_type);
9178
9179        let fields = UnionFields::from_fields(vec![Field::new("a", DataType::Int64, false)]);
9180        let target_type = DataType::Union(fields, UnionMode::Sparse);
9181        cast_from_null_to_other_complex(&target_type);
9182
9183        let target_type = DataType::RunEndEncoded(
9184            Arc::new(Field::new("item", DataType::Int32, true)),
9185            Arc::new(Field::new("item", DataType::Int32, true)),
9186        );
9187        cast_from_null_to_other_complex(&target_type);
9188    }
9189
9190    /// Print the `DictionaryArray` `array` as a vector of strings
9191    fn array_to_strings(array: &ArrayRef) -> Vec<String> {
9192        let options = FormatOptions::new().with_null("null");
9193        let formatter = ArrayFormatter::try_new(array.as_ref(), &options).unwrap();
9194        (0..array.len())
9195            .map(|i| formatter.value(i).to_string())
9196            .collect()
9197    }
9198
9199    #[test]
9200    fn test_cast_utf8_to_date32() {
9201        use chrono::NaiveDate;
9202        let from_ymd = chrono::NaiveDate::from_ymd_opt;
9203        let since = chrono::NaiveDate::signed_duration_since;
9204
9205        let a = StringArray::from(vec![
9206            "2000-01-01",          // valid date with leading 0s
9207            "2000-01-01T12:00:00", // valid datetime, will throw away the time part
9208            "2000-2-2",            // valid date without leading 0s
9209            "2000-00-00",          // invalid month and day
9210            "2000",                // just a year is invalid
9211        ]);
9212        let array = Arc::new(a) as ArrayRef;
9213        let b = cast(&array, &DataType::Date32).unwrap();
9214        let c = b.as_primitive::<Date32Type>();
9215
9216        // test valid inputs
9217        let date_value = since(
9218            NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
9219            from_ymd(1970, 1, 1).unwrap(),
9220        )
9221        .num_days() as i32;
9222        assert!(c.is_valid(0)); // "2000-01-01"
9223        assert_eq!(date_value, c.value(0));
9224
9225        assert!(c.is_valid(1)); // "2000-01-01T12:00:00"
9226        assert_eq!(date_value, c.value(1));
9227
9228        let date_value = since(
9229            NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
9230            from_ymd(1970, 1, 1).unwrap(),
9231        )
9232        .num_days() as i32;
9233        assert!(c.is_valid(2)); // "2000-2-2"
9234        assert_eq!(date_value, c.value(2));
9235
9236        // test invalid inputs
9237        assert!(!c.is_valid(3)); // "2000-00-00"
9238        assert!(!c.is_valid(4)); // "2000"
9239    }
9240
9241    #[test]
9242    fn test_cast_utf8_to_date64() {
9243        let a = StringArray::from(vec![
9244            "2000-01-01T12:00:00", // date + time valid
9245            "2020-12-15T12:34:56", // date + time valid
9246            "2020-2-2T12:34:56",   // valid date time without leading 0s
9247            "2000-00-00T12:00:00", // invalid month and day
9248            "2000-01-01 12:00:00", // missing the 'T'
9249            "2000-01-01",          // just a date is invalid
9250        ]);
9251        let array = Arc::new(a) as ArrayRef;
9252        let b = cast(&array, &DataType::Date64).unwrap();
9253        let c = b.as_primitive::<Date64Type>();
9254
9255        // test valid inputs
9256        assert!(c.is_valid(0)); // "2000-01-01T12:00:00"
9257        assert_eq!(946728000000, c.value(0));
9258        assert!(c.is_valid(1)); // "2020-12-15T12:34:56"
9259        assert_eq!(1608035696000, c.value(1));
9260        assert!(!c.is_valid(2)); // "2020-2-2T12:34:56"
9261
9262        assert!(!c.is_valid(3)); // "2000-00-00T12:00:00"
9263        assert!(c.is_valid(4)); // "2000-01-01 12:00:00"
9264        assert_eq!(946728000000, c.value(4));
9265        assert!(c.is_valid(5)); // "2000-01-01"
9266        assert_eq!(946684800000, c.value(5));
9267    }
9268
9269    #[test]
9270    fn test_can_cast_fsl_to_fsl() {
9271        let from_array = Arc::new(
9272            FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
9273                [Some([Some(1.0), Some(2.0)]), None],
9274                2,
9275            ),
9276        ) as ArrayRef;
9277        let to_array = Arc::new(
9278            FixedSizeListArray::from_iter_primitive::<Float16Type, _, _>(
9279                [
9280                    Some([Some(f16::from_f32(1.0)), Some(f16::from_f32(2.0))]),
9281                    None,
9282                ],
9283                2,
9284            ),
9285        ) as ArrayRef;
9286
9287        assert!(can_cast_types(from_array.data_type(), to_array.data_type()));
9288        let actual = cast(&from_array, to_array.data_type()).unwrap();
9289        assert_eq!(actual.data_type(), to_array.data_type());
9290
9291        let invalid_target =
9292            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Binary, true)), 2);
9293        assert!(!can_cast_types(from_array.data_type(), &invalid_target));
9294
9295        let invalid_size =
9296            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Float16, true)), 5);
9297        assert!(!can_cast_types(from_array.data_type(), &invalid_size));
9298    }
9299
9300    #[test]
9301    fn test_can_cast_types_fixed_size_list_to_list() {
9302        // DataType::List
9303        let array1 = make_fixed_size_list_array();
9304        assert!(can_cast_types(
9305            array1.data_type(),
9306            &DataType::List(Arc::new(Field::new("", DataType::Int32, false)))
9307        ));
9308
9309        // DataType::LargeList
9310        let array2 = make_fixed_size_list_array_for_large_list();
9311        assert!(can_cast_types(
9312            array2.data_type(),
9313            &DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false)))
9314        ));
9315    }
9316
9317    #[test]
9318    fn test_cast_fixed_size_list_to_list() {
9319        // Important cases:
9320        // 1. With/without nulls
9321        // 2. List/LargeList/ListView/LargeListView
9322        // 3. With and without inner casts
9323
9324        let cases = [
9325            // fixed_size_list<i32, 2> => list<i32>
9326            (
9327                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9328                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9329                    2,
9330                )) as ArrayRef,
9331                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
9332                    Some([Some(1), Some(1)]),
9333                    Some([Some(2), Some(2)]),
9334                ])) as ArrayRef,
9335            ),
9336            // fixed_size_list<i32, 2> => list<i32> (nullable)
9337            (
9338                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9339                    [None, Some([Some(2), Some(2)])],
9340                    2,
9341                )) as ArrayRef,
9342                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
9343                    None,
9344                    Some([Some(2), Some(2)]),
9345                ])) as ArrayRef,
9346            ),
9347            // fixed_size_list<i32, 2> => large_list<i64>
9348            (
9349                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9350                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9351                    2,
9352                )) as ArrayRef,
9353                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
9354                    Some([Some(1), Some(1)]),
9355                    Some([Some(2), Some(2)]),
9356                ])) as ArrayRef,
9357            ),
9358            // fixed_size_list<i32, 2> => large_list<i64> (nullable)
9359            (
9360                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9361                    [None, Some([Some(2), Some(2)])],
9362                    2,
9363                )) as ArrayRef,
9364                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
9365                    None,
9366                    Some([Some(2), Some(2)]),
9367                ])) as ArrayRef,
9368            ),
9369            // fixed_size_list<i32, 2> => list_view<i32>
9370            (
9371                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9372                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9373                    2,
9374                )) as ArrayRef,
9375                Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>([
9376                    Some([Some(1), Some(1)]),
9377                    Some([Some(2), Some(2)]),
9378                ])) as ArrayRef,
9379            ),
9380            // fixed_size_list<i32, 2> => list_view<i32> (nullable)
9381            (
9382                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9383                    [None, Some([Some(2), Some(2)])],
9384                    2,
9385                )) as ArrayRef,
9386                Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>([
9387                    None,
9388                    Some([Some(2), Some(2)]),
9389                ])) as ArrayRef,
9390            ),
9391            // fixed_size_list<i32, 2> => large_list_view<i64>
9392            (
9393                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9394                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
9395                    2,
9396                )) as ArrayRef,
9397                Arc::new(LargeListViewArray::from_iter_primitive::<Int64Type, _, _>(
9398                    [Some([Some(1), Some(1)]), Some([Some(2), Some(2)])],
9399                )) as ArrayRef,
9400            ),
9401            // fixed_size_list<i32, 2> => large_list_view<i64> (nullable)
9402            (
9403                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9404                    [None, Some([Some(2), Some(2)])],
9405                    2,
9406                )) as ArrayRef,
9407                Arc::new(LargeListViewArray::from_iter_primitive::<Int64Type, _, _>(
9408                    [None, Some([Some(2), Some(2)])],
9409                )) as ArrayRef,
9410            ),
9411        ];
9412
9413        for (array, expected) in cases {
9414            assert!(
9415                can_cast_types(array.data_type(), expected.data_type()),
9416                "can_cast_types claims we cannot cast {:?} to {:?}",
9417                array.data_type(),
9418                expected.data_type()
9419            );
9420
9421            let list_array = cast(&array, expected.data_type())
9422                .unwrap_or_else(|_| panic!("Failed to cast {array:?} to {expected:?}"));
9423            assert_eq!(
9424                list_array.as_ref(),
9425                &expected,
9426                "Incorrect result from casting {array:?} to {expected:?}",
9427            );
9428        }
9429    }
9430
9431    #[test]
9432    fn test_cast_fixed_size_list_to_list_preserves_field_metadata() {
9433        use std::collections::HashMap;
9434
9435        let metadata: HashMap<String, String> =
9436            HashMap::from([("PARQUET:field_id".to_string(), "89".to_string())]);
9437
9438        let src = Arc::new(
9439            FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
9440                [[1.0_f32, 2.0].map(Some), [3.0, 4.0].map(Some)].map(Some),
9441                2,
9442            ),
9443        ) as ArrayRef;
9444
9445        let target_field = Arc::new(
9446            Field::new("element", DataType::Float32, true).with_metadata(metadata.clone()),
9447        );
9448
9449        let target_types = [
9450            DataType::List(target_field.clone()),
9451            DataType::LargeList(target_field.clone()),
9452            DataType::ListView(target_field.clone()),
9453            DataType::LargeListView(target_field.clone()),
9454        ];
9455
9456        for target_type in &target_types {
9457            let result = cast(&src, target_type).unwrap();
9458            assert_eq!(
9459                result.data_type(),
9460                target_type,
9461                "Cast to {target_type:?} should preserve field metadata"
9462            );
9463        }
9464    }
9465
9466    #[test]
9467    fn test_cast_utf8_to_list() {
9468        // DataType::List
9469        let array = Arc::new(StringArray::from(vec!["5"])) as ArrayRef;
9470        let field = Arc::new(Field::new("", DataType::Int32, false));
9471        let list_array = cast(&array, &DataType::List(field.clone())).unwrap();
9472        let actual = list_array.as_list_opt::<i32>().unwrap();
9473        let expect = ListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
9474        assert_eq!(&expect.value(0), &actual.value(0));
9475
9476        // DataType::LargeList
9477        let list_array = cast(&array, &DataType::LargeList(field.clone())).unwrap();
9478        let actual = list_array.as_list_opt::<i64>().unwrap();
9479        let expect = LargeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
9480        assert_eq!(&expect.value(0), &actual.value(0));
9481
9482        // DataType::FixedSizeList
9483        let list_array = cast(&array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9484        let actual = list_array.as_fixed_size_list_opt().unwrap();
9485        let expect =
9486            FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])], 1);
9487        assert_eq!(&expect.value(0), &actual.value(0));
9488    }
9489
9490    #[test]
9491    fn test_cast_single_element_fixed_size_list() {
9492        // FixedSizeList<T>[1] => T
9493        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9494            [(Some([Some(5)]))],
9495            1,
9496        )) as ArrayRef;
9497        let casted_array = cast(&from_array, &DataType::Int32).unwrap();
9498        let actual: &Int32Array = casted_array.as_primitive();
9499        let expected = Int32Array::from(vec![Some(5)]);
9500        assert_eq!(&expected, actual);
9501
9502        // FixedSizeList<T>[1] => FixedSizeList<U>[1]
9503        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9504            [(Some([Some(5)]))],
9505            1,
9506        )) as ArrayRef;
9507        let to_field = Arc::new(Field::new("dummy", DataType::Float32, false));
9508        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
9509        let expected = Arc::new(FixedSizeListArray::new(
9510            to_field.clone(),
9511            1,
9512            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9513            None,
9514        )) as ArrayRef;
9515        assert_eq!(*expected, *actual);
9516
9517        // FixedSizeList<T>[1] => FixedSizeList<FixdSizedList<U>[1]>[1]
9518        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9519            [(Some([Some(5)]))],
9520            1,
9521        )) as ArrayRef;
9522        let to_field_inner = Arc::new(Field::new_list_field(DataType::Float32, false));
9523        let to_field = Arc::new(Field::new(
9524            "dummy",
9525            DataType::FixedSizeList(to_field_inner.clone(), 1),
9526            false,
9527        ));
9528        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
9529        let expected = Arc::new(FixedSizeListArray::new(
9530            to_field.clone(),
9531            1,
9532            Arc::new(FixedSizeListArray::new(
9533                to_field_inner.clone(),
9534                1,
9535                Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9536                None,
9537            )) as ArrayRef,
9538            None,
9539        )) as ArrayRef;
9540        assert_eq!(*expected, *actual);
9541
9542        // T => FixedSizeList<T>[1] (non-nullable)
9543        let field = Arc::new(Field::new("dummy", DataType::Float32, false));
9544        let from_array = Arc::new(Int8Array::from(vec![Some(5)])) as ArrayRef;
9545        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9546        let actual = casted_array.as_fixed_size_list();
9547        let expected = Arc::new(FixedSizeListArray::new(
9548            field.clone(),
9549            1,
9550            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9551            None,
9552        )) as ArrayRef;
9553        assert_eq!(expected.as_ref(), actual);
9554
9555        // T => FixedSizeList<T>[1] (nullable)
9556        let field = Arc::new(Field::new("nullable", DataType::Float32, true));
9557        let from_array = Arc::new(Int8Array::from(vec![None])) as ArrayRef;
9558        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9559        let actual = casted_array.as_fixed_size_list();
9560        let expected = Arc::new(FixedSizeListArray::new(
9561            field.clone(),
9562            1,
9563            Arc::new(Float32Array::from(vec![None])) as ArrayRef,
9564            None,
9565        )) as ArrayRef;
9566        assert_eq!(expected.as_ref(), actual);
9567    }
9568
9569    #[test]
9570    fn test_cast_list_containers() {
9571        // large-list to list
9572        let array = make_large_list_array();
9573        let list_array = cast(
9574            &array,
9575            &DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
9576        )
9577        .unwrap();
9578        let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
9579        let expected = array.as_any().downcast_ref::<LargeListArray>().unwrap();
9580
9581        assert_eq!(&expected.value(0), &actual.value(0));
9582        assert_eq!(&expected.value(1), &actual.value(1));
9583        assert_eq!(&expected.value(2), &actual.value(2));
9584
9585        // list to large-list
9586        let array = make_list_array();
9587        let large_list_array = cast(
9588            &array,
9589            &DataType::LargeList(Arc::new(Field::new("", DataType::Int32, false))),
9590        )
9591        .unwrap();
9592        let actual = large_list_array
9593            .as_any()
9594            .downcast_ref::<LargeListArray>()
9595            .unwrap();
9596        let expected = array.as_any().downcast_ref::<ListArray>().unwrap();
9597
9598        assert_eq!(&expected.value(0), &actual.value(0));
9599        assert_eq!(&expected.value(1), &actual.value(1));
9600        assert_eq!(&expected.value(2), &actual.value(2));
9601    }
9602
9603    #[test]
9604    fn test_cast_list_view() {
9605        // cast between list view and list view
9606        let array = make_list_view_array();
9607        let to = DataType::ListView(Field::new_list_field(DataType::Float32, true).into());
9608        assert!(can_cast_types(array.data_type(), &to));
9609        let actual = cast(&array, &to).unwrap();
9610        let actual = actual.as_list_view::<i32>();
9611
9612        assert_eq!(
9613            &Float32Array::from(vec![0.0, 1.0, 2.0]) as &dyn Array,
9614            actual.value(0).as_ref()
9615        );
9616        assert_eq!(
9617            &Float32Array::from(vec![3.0, 4.0, 5.0]) as &dyn Array,
9618            actual.value(1).as_ref()
9619        );
9620        assert_eq!(
9621            &Float32Array::from(vec![6.0, 7.0]) as &dyn Array,
9622            actual.value(2).as_ref()
9623        );
9624
9625        // cast between large list view and large list view
9626        let array = make_large_list_view_array();
9627        let to = DataType::LargeListView(Field::new_list_field(DataType::Float32, true).into());
9628        assert!(can_cast_types(array.data_type(), &to));
9629        let actual = cast(&array, &to).unwrap();
9630        let actual = actual.as_list_view::<i64>();
9631
9632        assert_eq!(
9633            &Float32Array::from(vec![0.0, 1.0, 2.0]) as &dyn Array,
9634            actual.value(0).as_ref()
9635        );
9636        assert_eq!(
9637            &Float32Array::from(vec![3.0, 4.0, 5.0]) as &dyn Array,
9638            actual.value(1).as_ref()
9639        );
9640        assert_eq!(
9641            &Float32Array::from(vec![6.0, 7.0]) as &dyn Array,
9642            actual.value(2).as_ref()
9643        );
9644    }
9645
9646    #[test]
9647    fn test_non_list_to_list_view() {
9648        let input = Arc::new(Int32Array::from(vec![Some(0), None, Some(2)])) as ArrayRef;
9649        let expected_primitive =
9650            Arc::new(Float32Array::from(vec![Some(0.0), None, Some(2.0)])) as ArrayRef;
9651
9652        // [[0], [NULL], [2]]
9653        let expected = ListViewArray::new(
9654            Field::new_list_field(DataType::Float32, true).into(),
9655            vec![0, 1, 2].into(),
9656            vec![1, 1, 1].into(),
9657            expected_primitive.clone(),
9658            None,
9659        );
9660        assert!(can_cast_types(input.data_type(), expected.data_type()));
9661        let actual = cast(&input, expected.data_type()).unwrap();
9662        assert_eq!(actual.as_ref(), &expected);
9663
9664        // [[0], [NULL], [2]]
9665        let expected = LargeListViewArray::new(
9666            Field::new_list_field(DataType::Float32, true).into(),
9667            vec![0, 1, 2].into(),
9668            vec![1, 1, 1].into(),
9669            expected_primitive.clone(),
9670            None,
9671        );
9672        assert!(can_cast_types(input.data_type(), expected.data_type()));
9673        let actual = cast(&input, expected.data_type()).unwrap();
9674        assert_eq!(actual.as_ref(), &expected);
9675    }
9676
9677    #[test]
9678    fn test_cast_list_to_zero_size_fsl() {
9679        let field = Arc::new(Field::new("a", DataType::Null, true));
9680        let length = 2;
9681        let expected = Arc::new(
9682            FixedSizeListArray::try_new_with_length(
9683                field.clone(),
9684                0,
9685                new_empty_array(&DataType::Null),
9686                None,
9687                2,
9688            )
9689            .unwrap(),
9690        ) as ArrayRef;
9691
9692        let list = Arc::new(ListArray::new(
9693            field.clone(),
9694            OffsetBuffer::from_repeated_length(0, length),
9695            new_empty_array(&DataType::Null),
9696            None,
9697        ));
9698        let fsl = cast(list.as_ref(), expected.data_type()).unwrap();
9699        assert_eq!(&expected, &fsl);
9700
9701        let list = Arc::new(ListViewArray::new(
9702            field.clone(),
9703            vec![0; length].into(),
9704            vec![0; length].into(),
9705            new_empty_array(&DataType::Null),
9706            None,
9707        ));
9708        let fsl = cast(list.as_ref(), expected.data_type()).unwrap();
9709        assert_eq!(&expected, &fsl);
9710    }
9711
9712    #[test]
9713    fn test_cast_list_to_fsl() {
9714        // There four noteworthy cases we should handle:
9715        // 1. No nulls
9716        // 2. Nulls that are always empty
9717        // 3. Nulls that have varying lengths
9718        // 4. Nulls that are correctly sized (same as target list size)
9719
9720        // Non-null case
9721        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9722        let values = vec![
9723            Some(vec![Some(1), Some(2), Some(3)]),
9724            Some(vec![Some(4), Some(5), Some(6)]),
9725        ];
9726        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
9727            values.clone(),
9728        )) as ArrayRef;
9729        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9730            values, 3,
9731        )) as ArrayRef;
9732        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9733        assert_eq!(expected.as_ref(), actual.as_ref());
9734
9735        // Null cases
9736        // Array is [[1, 2, 3], null, [4, 5, 6], null]
9737        let cases = [
9738            (
9739                // Zero-length nulls
9740                vec![1, 2, 3, 4, 5, 6],
9741                vec![3, 0, 3, 0],
9742            ),
9743            (
9744                // Varying-length nulls
9745                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
9746                vec![3, 2, 3, 1],
9747            ),
9748            (
9749                // Correctly-sized nulls
9750                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
9751                vec![3, 3, 3, 3],
9752            ),
9753            (
9754                // Mixed nulls
9755                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
9756                vec![3, 0, 3, 3],
9757            ),
9758        ];
9759        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
9760
9761        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9762            vec![
9763                Some(vec![Some(1), Some(2), Some(3)]),
9764                None,
9765                Some(vec![Some(4), Some(5), Some(6)]),
9766                None,
9767            ],
9768            3,
9769        )) as ArrayRef;
9770
9771        for (values, lengths) in cases.iter() {
9772            let array = Arc::new(ListArray::new(
9773                field.clone(),
9774                OffsetBuffer::from_lengths(lengths.clone()),
9775                Arc::new(Int32Array::from(values.clone())),
9776                Some(null_buffer.clone()),
9777            )) as ArrayRef;
9778            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9779            assert_eq!(expected.as_ref(), actual.as_ref());
9780        }
9781    }
9782
9783    #[test]
9784    fn test_cast_list_view_to_fsl() {
9785        // There four noteworthy cases we should handle:
9786        // 1. No nulls
9787        // 2. Nulls that are always empty
9788        // 3. Nulls that have varying lengths
9789        // 4. Nulls that are correctly sized (same as target list size)
9790
9791        // Non-null case
9792        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9793        let values = vec![
9794            Some(vec![Some(1), Some(2), Some(3)]),
9795            Some(vec![Some(4), Some(5), Some(6)]),
9796        ];
9797        let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(
9798            values.clone(),
9799        )) as ArrayRef;
9800        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9801            values, 3,
9802        )) as ArrayRef;
9803        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9804        assert_eq!(expected.as_ref(), actual.as_ref());
9805
9806        // Null cases
9807        // Array is [[1, 2, 3], null, [4, 5, 6], null]
9808        let cases = [
9809            (
9810                // Zero-length nulls
9811                vec![1, 2, 3, 4, 5, 6],
9812                vec![0, 0, 3, 0],
9813                vec![3, 0, 3, 0],
9814            ),
9815            (
9816                // Varying-length nulls
9817                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
9818                vec![0, 1, 5, 0],
9819                vec![3, 2, 3, 1],
9820            ),
9821            (
9822                // Correctly-sized nulls
9823                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
9824                vec![0, 3, 6, 9],
9825                vec![3, 3, 3, 3],
9826            ),
9827            (
9828                // Mixed nulls
9829                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
9830                vec![0, 0, 3, 6],
9831                vec![3, 0, 3, 3],
9832            ),
9833        ];
9834        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
9835
9836        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9837            vec![
9838                Some(vec![Some(1), Some(2), Some(3)]),
9839                None,
9840                Some(vec![Some(4), Some(5), Some(6)]),
9841                None,
9842            ],
9843            3,
9844        )) as ArrayRef;
9845
9846        for (values, offsets, lengths) in cases.iter() {
9847            let array = Arc::new(ListViewArray::new(
9848                field.clone(),
9849                offsets.clone().into(),
9850                lengths.clone().into(),
9851                Arc::new(Int32Array::from(values.clone())),
9852                Some(null_buffer.clone()),
9853            )) as ArrayRef;
9854            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9855            assert_eq!(expected.as_ref(), actual.as_ref());
9856        }
9857    }
9858
9859    #[test]
9860    fn test_cast_list_to_fsl_safety() {
9861        let values = vec![
9862            Some(vec![Some(1), Some(2), Some(3)]),
9863            Some(vec![Some(4), Some(5)]),
9864            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
9865            Some(vec![Some(3), Some(4), Some(5)]),
9866        ];
9867        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
9868            values.clone(),
9869        )) as ArrayRef;
9870
9871        let res = cast_with_options(
9872            array.as_ref(),
9873            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9874            &CastOptions {
9875                safe: false,
9876                ..Default::default()
9877            },
9878        );
9879        assert!(res.is_err());
9880        assert!(
9881            format!("{res:?}")
9882                .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
9883        );
9884
9885        // When safe=true (default), the cast will fill nulls for lists that are
9886        // too short and truncate lists that are too long.
9887        let res = cast(
9888            array.as_ref(),
9889            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9890        )
9891        .unwrap();
9892        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9893            vec![
9894                Some(vec![Some(1), Some(2), Some(3)]),
9895                None, // Too short -> replaced with null
9896                None, // Too long -> replaced with null
9897                Some(vec![Some(3), Some(4), Some(5)]),
9898            ],
9899            3,
9900        )) as ArrayRef;
9901        assert_eq!(expected.as_ref(), res.as_ref());
9902
9903        // The safe option is false and the source array contains a null list.
9904        // issue: https://github.com/apache/arrow-rs/issues/5642
9905        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
9906            Some(vec![Some(1), Some(2), Some(3)]),
9907            None,
9908        ])) as ArrayRef;
9909        let res = cast_with_options(
9910            array.as_ref(),
9911            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9912            &CastOptions {
9913                safe: false,
9914                ..Default::default()
9915            },
9916        )
9917        .unwrap();
9918        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9919            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
9920            3,
9921        )) as ArrayRef;
9922        assert_eq!(expected.as_ref(), res.as_ref());
9923    }
9924
9925    #[test]
9926    fn test_cast_list_view_to_fsl_safety() {
9927        let values = vec![
9928            Some(vec![Some(1), Some(2), Some(3)]),
9929            Some(vec![Some(4), Some(5)]),
9930            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
9931            Some(vec![Some(3), Some(4), Some(5)]),
9932        ];
9933        let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(
9934            values.clone(),
9935        )) as ArrayRef;
9936
9937        let res = cast_with_options(
9938            array.as_ref(),
9939            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9940            &CastOptions {
9941                safe: false,
9942                ..Default::default()
9943            },
9944        );
9945        assert!(res.is_err());
9946        assert!(
9947            format!("{res:?}")
9948                .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
9949        );
9950
9951        // When safe=true (default), the cast will fill nulls for lists that are
9952        // too short and truncate lists that are too long.
9953        let res = cast(
9954            array.as_ref(),
9955            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9956        )
9957        .unwrap();
9958        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9959            vec![
9960                Some(vec![Some(1), Some(2), Some(3)]),
9961                None, // Too short -> replaced with null
9962                None, // Too long -> replaced with null
9963                Some(vec![Some(3), Some(4), Some(5)]),
9964            ],
9965            3,
9966        )) as ArrayRef;
9967        assert_eq!(expected.as_ref(), res.as_ref());
9968
9969        // The safe option is false and the source array contains a null list.
9970        // issue: https://github.com/apache/arrow-rs/issues/5642
9971        let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(vec![
9972            Some(vec![Some(1), Some(2), Some(3)]),
9973            None,
9974        ])) as ArrayRef;
9975        let res = cast_with_options(
9976            array.as_ref(),
9977            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9978            &CastOptions {
9979                safe: false,
9980                ..Default::default()
9981            },
9982        )
9983        .unwrap();
9984        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9985            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
9986            3,
9987        )) as ArrayRef;
9988        assert_eq!(expected.as_ref(), res.as_ref());
9989    }
9990
9991    #[test]
9992    fn test_cast_large_list_to_fsl() {
9993        let values = vec![Some(vec![Some(1), Some(2)]), Some(vec![Some(3), Some(4)])];
9994        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9995            values.clone(),
9996            2,
9997        )) as ArrayRef;
9998        let target_type =
9999            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 2);
10000
10001        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
10002            values.clone(),
10003        )) as ArrayRef;
10004        let actual = cast(array.as_ref(), &target_type).unwrap();
10005        assert_eq!(expected.as_ref(), actual.as_ref());
10006
10007        let array = Arc::new(LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(
10008            values.clone(),
10009        )) as ArrayRef;
10010        let actual = cast(array.as_ref(), &target_type).unwrap();
10011        assert_eq!(expected.as_ref(), actual.as_ref());
10012    }
10013
10014    #[test]
10015    fn test_cast_list_to_fsl_subcast() {
10016        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
10017            vec![
10018                Some(vec![Some(1), Some(2)]),
10019                Some(vec![Some(3), Some(i32::MAX)]),
10020            ],
10021        )) as ArrayRef;
10022        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
10023            vec![
10024                Some(vec![Some(1), Some(2)]),
10025                Some(vec![Some(3), Some(i32::MAX as i64)]),
10026            ],
10027            2,
10028        )) as ArrayRef;
10029        let actual = cast(
10030            array.as_ref(),
10031            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 2),
10032        )
10033        .unwrap();
10034        assert_eq!(expected.as_ref(), actual.as_ref());
10035
10036        let res = cast_with_options(
10037            array.as_ref(),
10038            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int16, true)), 2),
10039            &CastOptions {
10040                safe: false,
10041                ..Default::default()
10042            },
10043        );
10044        assert!(res.is_err());
10045        assert!(format!("{res:?}").contains("Can't cast value 2147483647 to type Int16"));
10046    }
10047
10048    #[test]
10049    fn test_cast_list_to_fsl_empty() {
10050        let inner_field = Arc::new(Field::new_list_field(DataType::Int32, true));
10051        let target_type = DataType::FixedSizeList(inner_field.clone(), 3);
10052        let expected = new_empty_array(&target_type);
10053
10054        // list
10055        let array = new_empty_array(&DataType::List(inner_field.clone()));
10056        assert!(can_cast_types(array.data_type(), &target_type));
10057        let actual = cast(array.as_ref(), &target_type).unwrap();
10058        assert_eq!(expected.as_ref(), actual.as_ref());
10059
10060        // largelist
10061        let array = new_empty_array(&DataType::LargeList(inner_field.clone()));
10062        assert!(can_cast_types(array.data_type(), &target_type));
10063        let actual = cast(array.as_ref(), &target_type).unwrap();
10064        assert_eq!(expected.as_ref(), actual.as_ref());
10065
10066        // listview
10067        let array = new_empty_array(&DataType::ListView(inner_field.clone()));
10068        assert!(can_cast_types(array.data_type(), &target_type));
10069        let actual = cast(array.as_ref(), &target_type).unwrap();
10070        assert_eq!(expected.as_ref(), actual.as_ref());
10071
10072        // largelistview
10073        let array = new_empty_array(&DataType::LargeListView(inner_field.clone()));
10074        assert!(can_cast_types(array.data_type(), &target_type));
10075        let actual = cast(array.as_ref(), &target_type).unwrap();
10076        assert_eq!(expected.as_ref(), actual.as_ref());
10077    }
10078
10079    fn make_list_array() -> ArrayRef {
10080        // [[0, 1, 2], [3, 4, 5], [6, 7]]
10081        Arc::new(ListArray::new(
10082            Field::new_list_field(DataType::Int32, true).into(),
10083            OffsetBuffer::from_lengths(vec![3, 3, 2]),
10084            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10085            None,
10086        ))
10087    }
10088
10089    fn make_large_list_array() -> ArrayRef {
10090        // [[0, 1, 2], [3, 4, 5], [6, 7]]
10091        Arc::new(LargeListArray::new(
10092            Field::new_list_field(DataType::Int32, true).into(),
10093            OffsetBuffer::from_lengths(vec![3, 3, 2]),
10094            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10095            None,
10096        ))
10097    }
10098
10099    fn make_list_view_array() -> ArrayRef {
10100        // [[0, 1, 2], [3, 4, 5], [6, 7]]
10101        Arc::new(ListViewArray::new(
10102            Field::new_list_field(DataType::Int32, true).into(),
10103            vec![0, 3, 6].into(),
10104            vec![3, 3, 2].into(),
10105            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10106            None,
10107        ))
10108    }
10109
10110    fn make_large_list_view_array() -> ArrayRef {
10111        // [[0, 1, 2], [3, 4, 5], [6, 7]]
10112        Arc::new(LargeListViewArray::new(
10113            Field::new_list_field(DataType::Int32, true).into(),
10114            vec![0, 3, 6].into(),
10115            vec![3, 3, 2].into(),
10116            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10117            None,
10118        ))
10119    }
10120
10121    fn make_fixed_size_list_array() -> ArrayRef {
10122        // [[0, 1, 2, 3], [4, 5, 6, 7]]
10123        Arc::new(FixedSizeListArray::new(
10124            Field::new_list_field(DataType::Int32, true).into(),
10125            4,
10126            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10127            None,
10128        ))
10129    }
10130
10131    fn make_fixed_size_list_array_for_large_list() -> ArrayRef {
10132        // [[0, 1, 2, 3], [4, 5, 6, 7]]
10133        Arc::new(FixedSizeListArray::new(
10134            Field::new_list_field(DataType::Int64, true).into(),
10135            4,
10136            Arc::new(Int64Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
10137            None,
10138        ))
10139    }
10140
10141    #[test]
10142    fn test_cast_map_dont_allow_change_of_order() {
10143        let string_builder = StringBuilder::new();
10144        let value_builder = StringBuilder::new();
10145        let mut builder = MapBuilder::new(None, string_builder, value_builder);
10146
10147        builder.keys().append_value("0");
10148        builder.values().append_value("test_val_1");
10149        builder.append(true).unwrap();
10150        builder.keys().append_value("1");
10151        builder.values().append_value("test_val_2");
10152        builder.append(true).unwrap();
10153
10154        // map builder returns unsorted map by default
10155        let array = builder.finish();
10156
10157        let new_ordered = true;
10158        let new_type = DataType::Map(
10159            Arc::new(Field::new(
10160                Field::MAP_ENTRIES_FIELD_DEFAULT_NAME,
10161                DataType::Struct(
10162                    vec![
10163                        Field::new(Field::MAP_KEY_FIELD_DEFAULT_NAME, DataType::Utf8, false),
10164                        Field::new(Field::MAP_VALUE_FIELD_DEFAULT_NAME, DataType::Utf8, false),
10165                    ]
10166                    .into(),
10167                ),
10168                false,
10169            )),
10170            new_ordered,
10171        );
10172
10173        let new_array_result = cast(&array, &new_type.clone());
10174        assert!(!can_cast_types(array.data_type(), &new_type));
10175        let Err(ArrowError::CastError(t)) = new_array_result else {
10176            panic!();
10177        };
10178        assert_eq!(
10179            t,
10180            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"#
10181        );
10182    }
10183
10184    #[test]
10185    fn test_cast_map_dont_allow_when_container_cant_cast() {
10186        let string_builder = StringBuilder::new();
10187        let value_builder = IntervalDayTimeArray::builder(2);
10188        let mut builder = MapBuilder::new(None, string_builder, value_builder);
10189
10190        builder.keys().append_value("0");
10191        builder.values().append_value(IntervalDayTime::new(1, 1));
10192        builder.append(true).unwrap();
10193        builder.keys().append_value("1");
10194        builder.values().append_value(IntervalDayTime::new(2, 2));
10195        builder.append(true).unwrap();
10196
10197        // map builder returns unsorted map by default
10198        let array = builder.finish();
10199
10200        let new_ordered = true;
10201        let new_type = DataType::Map(
10202            Arc::new(Field::new(
10203                Field::MAP_ENTRIES_FIELD_DEFAULT_NAME,
10204                DataType::Struct(
10205                    vec![
10206                        Field::new(Field::MAP_KEY_FIELD_DEFAULT_NAME, DataType::Utf8, false),
10207                        Field::new(
10208                            Field::MAP_VALUE_FIELD_DEFAULT_NAME,
10209                            DataType::Duration(TimeUnit::Second),
10210                            false,
10211                        ),
10212                    ]
10213                    .into(),
10214                ),
10215                false,
10216            )),
10217            new_ordered,
10218        );
10219
10220        let new_array_result = cast(&array, &new_type.clone());
10221        assert!(!can_cast_types(array.data_type(), &new_type));
10222        let Err(ArrowError::CastError(t)) = new_array_result else {
10223            panic!();
10224        };
10225        assert_eq!(
10226            t,
10227            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"#
10228        );
10229    }
10230
10231    #[test]
10232    fn test_cast_map_field_names() {
10233        let string_builder = StringBuilder::new();
10234        let value_builder = StringBuilder::new();
10235        let mut builder = MapBuilder::new(
10236            Some(MapFieldNames {
10237                // Explicitly writing the name so it will be apparent from what names to what names are we converting to
10238                entry: Field::MAP_ENTRIES_FIELD_DEFAULT_NAME.to_string(),
10239                key: Field::MAP_KEY_FIELD_DEFAULT_NAME.to_string(),
10240                value: Field::MAP_VALUE_FIELD_DEFAULT_NAME.to_string(),
10241            }),
10242            string_builder,
10243            value_builder,
10244        );
10245
10246        builder.keys().append_value("0");
10247        builder.values().append_value("test_val_1");
10248        builder.append(true).unwrap();
10249        builder.keys().append_value("1");
10250        builder.values().append_value("test_val_2");
10251        builder.append(true).unwrap();
10252        builder.append(false).unwrap();
10253
10254        let array = builder.finish();
10255
10256        let new_type = DataType::Map(
10257            Arc::new(Field::new(
10258                "entries_new",
10259                DataType::Struct(
10260                    vec![
10261                        Field::new("key_new", DataType::Utf8, false),
10262                        Field::new("value_values", DataType::Utf8, false),
10263                    ]
10264                    .into(),
10265                ),
10266                false,
10267            )),
10268            false,
10269        );
10270
10271        assert_ne!(new_type, array.data_type().clone());
10272
10273        let new_array = cast(&array, &new_type.clone()).unwrap();
10274        assert_eq!(new_type, new_array.data_type().clone());
10275        let map_array = new_array.as_map();
10276
10277        assert_ne!(new_type, array.data_type().clone());
10278        assert_eq!(new_type, map_array.data_type().clone());
10279
10280        let key_string = map_array
10281            .keys()
10282            .as_any()
10283            .downcast_ref::<StringArray>()
10284            .unwrap()
10285            .into_iter()
10286            .flatten()
10287            .collect::<Vec<_>>();
10288        assert_eq!(&key_string, &vec!["0", "1"]);
10289
10290        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
10291        let values_string = values_string_array
10292            .as_any()
10293            .downcast_ref::<StringArray>()
10294            .unwrap()
10295            .into_iter()
10296            .flatten()
10297            .collect::<Vec<_>>();
10298        assert_eq!(&values_string, &vec!["test_val_1", "test_val_2"]);
10299
10300        assert_eq!(
10301            map_array.nulls(),
10302            Some(&NullBuffer::from(vec![true, true, false]))
10303        );
10304    }
10305
10306    #[test]
10307    fn test_cast_map_contained_values() {
10308        let string_builder = StringBuilder::new();
10309        let value_builder = Int8Builder::new();
10310        let mut builder = MapBuilder::new(None, string_builder, value_builder);
10311
10312        builder.keys().append_value("0");
10313        builder.values().append_value(44);
10314        builder.append(true).unwrap();
10315        builder.keys().append_value("1");
10316        builder.values().append_value(22);
10317        builder.append(true).unwrap();
10318
10319        let array = builder.finish();
10320
10321        let new_type = DataType::Map(
10322            Arc::new(Field::new(
10323                Field::MAP_ENTRIES_FIELD_DEFAULT_NAME,
10324                DataType::Struct(
10325                    vec![
10326                        Field::new(Field::MAP_KEY_FIELD_DEFAULT_NAME, DataType::Utf8, false),
10327                        Field::new(Field::MAP_VALUE_FIELD_DEFAULT_NAME, DataType::Utf8, false),
10328                    ]
10329                    .into(),
10330                ),
10331                false,
10332            )),
10333            false,
10334        );
10335
10336        let new_array = cast(&array, &new_type.clone()).unwrap();
10337        assert_eq!(new_type, new_array.data_type().clone());
10338        let map_array = new_array.as_map();
10339
10340        assert_ne!(new_type, array.data_type().clone());
10341        assert_eq!(new_type, map_array.data_type().clone());
10342
10343        let key_string = map_array
10344            .keys()
10345            .as_any()
10346            .downcast_ref::<StringArray>()
10347            .unwrap()
10348            .into_iter()
10349            .flatten()
10350            .collect::<Vec<_>>();
10351        assert_eq!(&key_string, &vec!["0", "1"]);
10352
10353        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
10354        let values_string = values_string_array
10355            .as_any()
10356            .downcast_ref::<StringArray>()
10357            .unwrap()
10358            .into_iter()
10359            .flatten()
10360            .collect::<Vec<_>>();
10361        assert_eq!(&values_string, &vec!["44", "22"]);
10362    }
10363
10364    #[test]
10365    fn test_utf8_cast_offsets() {
10366        // test if offset of the array is taken into account during cast
10367        let str_array = StringArray::from(vec!["a", "b", "c"]);
10368        let str_array = str_array.slice(1, 2);
10369
10370        let out = cast(&str_array, &DataType::LargeUtf8).unwrap();
10371
10372        let large_str_array = out.as_any().downcast_ref::<LargeStringArray>().unwrap();
10373        let strs = large_str_array.into_iter().flatten().collect::<Vec<_>>();
10374        assert_eq!(strs, &["b", "c"])
10375    }
10376
10377    #[test]
10378    fn test_list_cast_offsets() {
10379        // test if offset of the array is taken into account during cast
10380        let array1 = make_list_array().slice(1, 2);
10381        let array2 = make_list_array();
10382
10383        let dt = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
10384        let out1 = cast(&array1, &dt).unwrap();
10385        let out2 = cast(&array2, &dt).unwrap();
10386
10387        assert_eq!(&out1, &out2.slice(1, 2))
10388    }
10389
10390    #[test]
10391    fn test_list_to_string() {
10392        fn assert_cast(array: &ArrayRef, expected: &[&str]) {
10393            assert!(can_cast_types(array.data_type(), &DataType::Utf8));
10394            let out = cast(array, &DataType::Utf8).unwrap();
10395            let out = out
10396                .as_string::<i32>()
10397                .into_iter()
10398                .flatten()
10399                .collect::<Vec<_>>();
10400            assert_eq!(out, expected);
10401
10402            assert!(can_cast_types(array.data_type(), &DataType::LargeUtf8));
10403            let out = cast(array, &DataType::LargeUtf8).unwrap();
10404            let out = out
10405                .as_string::<i64>()
10406                .into_iter()
10407                .flatten()
10408                .collect::<Vec<_>>();
10409            assert_eq!(out, expected);
10410
10411            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
10412            let out = cast(array, &DataType::Utf8View).unwrap();
10413            let out = out
10414                .as_string_view()
10415                .into_iter()
10416                .flatten()
10417                .collect::<Vec<_>>();
10418            assert_eq!(out, expected);
10419        }
10420
10421        let array = Arc::new(ListArray::new(
10422            Field::new_list_field(DataType::Utf8, true).into(),
10423            OffsetBuffer::from_lengths(vec![3, 3, 2]),
10424            Arc::new(StringArray::from(vec![
10425                "a", "b", "c", "d", "e", "f", "g", "h",
10426            ])),
10427            None,
10428        )) as ArrayRef;
10429
10430        assert_cast(&array, &["[a, b, c]", "[d, e, f]", "[g, h]"]);
10431
10432        let array = make_list_array();
10433        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10434
10435        let array = make_large_list_array();
10436        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10437
10438        let array = make_list_view_array();
10439        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10440
10441        let array = make_large_list_view_array();
10442        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
10443    }
10444
10445    #[test]
10446    fn test_cast_f64_to_decimal128() {
10447        // to reproduce https://github.com/apache/arrow-rs/issues/2997
10448
10449        let decimal_type = DataType::Decimal128(18, 2);
10450        let array = Float64Array::from(vec![
10451            Some(0.0699999999),
10452            Some(0.0659999999),
10453            Some(0.0650000000),
10454            Some(0.0649999999),
10455        ]);
10456        let array = Arc::new(array) as ArrayRef;
10457        generate_cast_test_case!(
10458            &array,
10459            Decimal128Array,
10460            &decimal_type,
10461            vec![
10462                Some(7_i128), // round up
10463                Some(7_i128), // round up
10464                Some(7_i128), // round up
10465                Some(6_i128), // round down
10466            ]
10467        );
10468
10469        let decimal_type = DataType::Decimal128(18, 3);
10470        let array = Float64Array::from(vec![
10471            Some(0.0699999999),
10472            Some(0.0659999999),
10473            Some(0.0650000000),
10474            Some(0.0649999999),
10475        ]);
10476        let array = Arc::new(array) as ArrayRef;
10477        generate_cast_test_case!(
10478            &array,
10479            Decimal128Array,
10480            &decimal_type,
10481            vec![
10482                Some(70_i128), // round up
10483                Some(66_i128), // round up
10484                Some(65_i128), // round down
10485                Some(65_i128), // round up
10486            ]
10487        );
10488    }
10489
10490    #[test]
10491    fn test_cast_numeric_to_decimal128_overflow() {
10492        let array = Int64Array::from(vec![i64::MAX]);
10493        let array = Arc::new(array) as ArrayRef;
10494        let casted_array = cast_with_options(
10495            &array,
10496            &DataType::Decimal128(38, 30),
10497            &CastOptions {
10498                safe: true,
10499                format_options: FormatOptions::default(),
10500            },
10501        );
10502        assert!(casted_array.is_ok());
10503        assert!(casted_array.unwrap().is_null(0));
10504
10505        let casted_array = cast_with_options(
10506            &array,
10507            &DataType::Decimal128(38, 30),
10508            &CastOptions {
10509                safe: false,
10510                format_options: FormatOptions::default(),
10511            },
10512        );
10513        assert!(casted_array.is_err());
10514    }
10515
10516    #[test]
10517    fn test_cast_numeric_to_decimal256_overflow() {
10518        let array = Int64Array::from(vec![i64::MAX]);
10519        let array = Arc::new(array) as ArrayRef;
10520        let casted_array = cast_with_options(
10521            &array,
10522            &DataType::Decimal256(76, 76),
10523            &CastOptions {
10524                safe: true,
10525                format_options: FormatOptions::default(),
10526            },
10527        );
10528        assert!(casted_array.is_ok());
10529        assert!(casted_array.unwrap().is_null(0));
10530
10531        let casted_array = cast_with_options(
10532            &array,
10533            &DataType::Decimal256(76, 76),
10534            &CastOptions {
10535                safe: false,
10536                format_options: FormatOptions::default(),
10537            },
10538        );
10539        assert!(casted_array.is_err());
10540    }
10541
10542    #[test]
10543    fn test_cast_floating_point_to_decimal128_precision_overflow() {
10544        let array = Float64Array::from(vec![1.1]);
10545        let array = Arc::new(array) as ArrayRef;
10546        let casted_array = cast_with_options(
10547            &array,
10548            &DataType::Decimal128(2, 2),
10549            &CastOptions {
10550                safe: true,
10551                format_options: FormatOptions::default(),
10552            },
10553        );
10554        assert!(casted_array.is_ok());
10555        assert!(casted_array.unwrap().is_null(0));
10556
10557        let casted_array = cast_with_options(
10558            &array,
10559            &DataType::Decimal128(2, 2),
10560            &CastOptions {
10561                safe: false,
10562                format_options: FormatOptions::default(),
10563            },
10564        );
10565        let err = casted_array.unwrap_err().to_string();
10566        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
10567        assert!(
10568            err.contains(expected_error),
10569            "did not find expected error '{expected_error}' in actual error '{err}'"
10570        );
10571    }
10572
10573    #[test]
10574    fn test_cast_float16_to_decimal128_precision_overflow() {
10575        let array = Float16Array::from(vec![f16::from_f32(1.1)]);
10576        let array = Arc::new(array) as ArrayRef;
10577        let casted_array = cast_with_options(
10578            &array,
10579            &DataType::Decimal128(2, 2),
10580            &CastOptions {
10581                safe: true,
10582                format_options: FormatOptions::default(),
10583            },
10584        );
10585        assert!(casted_array.is_ok());
10586        assert!(casted_array.unwrap().is_null(0));
10587
10588        let casted_array = cast_with_options(
10589            &array,
10590            &DataType::Decimal128(2, 2),
10591            &CastOptions {
10592                safe: false,
10593                format_options: FormatOptions::default(),
10594            },
10595        );
10596        let err = casted_array.unwrap_err().to_string();
10597        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
10598        assert_eq!(err, expected_error);
10599    }
10600
10601    #[test]
10602    fn test_cast_float16_to_decimal256_precision_overflow() {
10603        let array = Float16Array::from(vec![f16::from_f32(1.1)]);
10604        let array = Arc::new(array) as ArrayRef;
10605        let casted_array = cast_with_options(
10606            &array,
10607            &DataType::Decimal256(2, 2),
10608            &CastOptions {
10609                safe: true,
10610                format_options: FormatOptions::default(),
10611            },
10612        );
10613        assert!(casted_array.is_ok());
10614        assert!(casted_array.unwrap().is_null(0));
10615
10616        let casted_array = cast_with_options(
10617            &array,
10618            &DataType::Decimal256(2, 2),
10619            &CastOptions {
10620                safe: false,
10621                format_options: FormatOptions::default(),
10622            },
10623        );
10624        let err = casted_array.unwrap_err().to_string();
10625        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
10626        assert_eq!(err, expected_error);
10627    }
10628
10629    #[test]
10630    fn test_cast_float16_to_decimal128_non_finite() {
10631        let array = Float16Array::from(vec![f16::NAN, f16::INFINITY, f16::NEG_INFINITY]);
10632        let array = Arc::new(array) as ArrayRef;
10633        let casted_array = cast_with_options(
10634            &array,
10635            &DataType::Decimal128(38, 2),
10636            &CastOptions {
10637                safe: true,
10638                format_options: FormatOptions::default(),
10639            },
10640        )
10641        .unwrap();
10642
10643        assert!(casted_array.is_null(0));
10644        assert!(casted_array.is_null(1));
10645        assert!(casted_array.is_null(2));
10646
10647        let casted_array = cast_with_options(
10648            &array,
10649            &DataType::Decimal128(38, 2),
10650            &CastOptions {
10651                safe: false,
10652                format_options: FormatOptions::default(),
10653            },
10654        );
10655        let err = casted_array.unwrap_err().to_string();
10656        let expected_error = "Cannot cast to Decimal128(38, 2)";
10657        assert!(
10658            err.contains(expected_error),
10659            "did not find expected error '{expected_error}' in actual error '{err}'"
10660        );
10661    }
10662
10663    #[test]
10664    fn test_cast_floating_point_to_decimal256_precision_overflow() {
10665        let array = Float64Array::from(vec![1.1]);
10666        let array = Arc::new(array) as ArrayRef;
10667        let casted_array = cast_with_options(
10668            &array,
10669            &DataType::Decimal256(2, 2),
10670            &CastOptions {
10671                safe: true,
10672                format_options: FormatOptions::default(),
10673            },
10674        );
10675        assert!(casted_array.is_ok());
10676        assert!(casted_array.unwrap().is_null(0));
10677
10678        let casted_array = cast_with_options(
10679            &array,
10680            &DataType::Decimal256(2, 2),
10681            &CastOptions {
10682                safe: false,
10683                format_options: FormatOptions::default(),
10684            },
10685        );
10686        let err = casted_array.unwrap_err().to_string();
10687        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
10688        assert_eq!(err, expected_error);
10689    }
10690
10691    #[test]
10692    fn test_cast_floating_point_to_decimal128_overflow() {
10693        let array = Float64Array::from(vec![f64::MAX]);
10694        let array = Arc::new(array) as ArrayRef;
10695        let casted_array = cast_with_options(
10696            &array,
10697            &DataType::Decimal128(38, 30),
10698            &CastOptions {
10699                safe: true,
10700                format_options: FormatOptions::default(),
10701            },
10702        );
10703        assert!(casted_array.is_ok());
10704        assert!(casted_array.unwrap().is_null(0));
10705
10706        let casted_array = cast_with_options(
10707            &array,
10708            &DataType::Decimal128(38, 30),
10709            &CastOptions {
10710                safe: false,
10711                format_options: FormatOptions::default(),
10712            },
10713        );
10714        let err = casted_array.unwrap_err().to_string();
10715        let expected_error = "Cast error: Cannot cast to Decimal128(38, 30)";
10716        assert!(
10717            err.contains(expected_error),
10718            "did not find expected error '{expected_error}' in actual error '{err}'"
10719        );
10720    }
10721
10722    #[test]
10723    fn test_cast_floating_point_to_decimal256_overflow() {
10724        let array = Float64Array::from(vec![f64::MAX]);
10725        let array = Arc::new(array) as ArrayRef;
10726        let casted_array = cast_with_options(
10727            &array,
10728            &DataType::Decimal256(76, 50),
10729            &CastOptions {
10730                safe: true,
10731                format_options: FormatOptions::default(),
10732            },
10733        );
10734        assert!(casted_array.is_ok());
10735        assert!(casted_array.unwrap().is_null(0));
10736
10737        let casted_array = cast_with_options(
10738            &array,
10739            &DataType::Decimal256(76, 50),
10740            &CastOptions {
10741                safe: false,
10742                format_options: FormatOptions::default(),
10743            },
10744        );
10745        let err = casted_array.unwrap_err().to_string();
10746        let expected_error = "Cast error: Cannot cast to Decimal256(76, 50)";
10747        assert!(
10748            err.contains(expected_error),
10749            "did not find expected error '{expected_error}' in actual error '{err}'"
10750        );
10751    }
10752    #[test]
10753    fn test_cast_decimal256_to_f64_no_overflow() {
10754        // Test casting i256::MAX: should produce a large finite positive value
10755        let array = vec![Some(i256::MAX)];
10756        let array = create_decimal256_array(array, 76, 2).unwrap();
10757        let array = Arc::new(array) as ArrayRef;
10758
10759        let result = cast(&array, &DataType::Float64).unwrap();
10760        let result = result.as_primitive::<Float64Type>();
10761        assert!(result.value(0).is_finite());
10762        assert!(result.value(0) > 0.0); // Positive result
10763
10764        // Test casting i256::MIN: should produce a large finite negative value
10765        let array = vec![Some(i256::MIN)];
10766        let array = create_decimal256_array(array, 76, 2).unwrap();
10767        let array = Arc::new(array) as ArrayRef;
10768
10769        let result = cast(&array, &DataType::Float64).unwrap();
10770        let result = result.as_primitive::<Float64Type>();
10771        assert!(result.value(0).is_finite());
10772        assert!(result.value(0) < 0.0); // Negative result
10773    }
10774
10775    #[test]
10776    fn test_cast_decimal128_to_decimal128_negative_scale() {
10777        let input_type = DataType::Decimal128(20, 0);
10778        let output_type = DataType::Decimal128(20, -1);
10779        assert!(can_cast_types(&input_type, &output_type));
10780        let array = vec![Some(1123450), Some(2123455), Some(3123456), None];
10781        let input_decimal_array = create_decimal128_array(array, 20, 0).unwrap();
10782        let array = Arc::new(input_decimal_array) as ArrayRef;
10783        generate_cast_test_case!(
10784            &array,
10785            Decimal128Array,
10786            &output_type,
10787            vec![
10788                Some(112345_i128),
10789                Some(212346_i128),
10790                Some(312346_i128),
10791                None
10792            ]
10793        );
10794
10795        let casted_array = cast(&array, &output_type).unwrap();
10796        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10797
10798        assert_eq!("1123450", decimal_arr.value_as_string(0));
10799        assert_eq!("2123460", decimal_arr.value_as_string(1));
10800        assert_eq!("3123460", decimal_arr.value_as_string(2));
10801    }
10802
10803    #[test]
10804    fn decimal128_min_max_to_f64() {
10805        // Ensure Decimal128 i128::MIN/MAX round-trip cast
10806        let min128 = i128::MIN;
10807        let max128 = i128::MAX;
10808        assert_eq!(min128 as f64, min128 as f64);
10809        assert_eq!(max128 as f64, max128 as f64);
10810    }
10811
10812    #[test]
10813    fn test_cast_numeric_to_decimal128_negative() {
10814        let decimal_type = DataType::Decimal128(38, -1);
10815        let array = Arc::new(Int32Array::from(vec![
10816            Some(1123456),
10817            Some(2123456),
10818            Some(3123456),
10819        ])) as ArrayRef;
10820
10821        let casted_array = cast(&array, &decimal_type).unwrap();
10822        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10823
10824        assert_eq!("1123450", decimal_arr.value_as_string(0));
10825        assert_eq!("2123450", decimal_arr.value_as_string(1));
10826        assert_eq!("3123450", decimal_arr.value_as_string(2));
10827
10828        let array = Arc::new(Float32Array::from(vec![
10829            Some(1123.456),
10830            Some(2123.456),
10831            Some(3123.456),
10832        ])) as ArrayRef;
10833
10834        let casted_array = cast(&array, &decimal_type).unwrap();
10835        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10836
10837        assert_eq!("1120", decimal_arr.value_as_string(0));
10838        assert_eq!("2120", decimal_arr.value_as_string(1));
10839        assert_eq!("3120", decimal_arr.value_as_string(2));
10840    }
10841
10842    #[test]
10843    fn test_cast_decimal128_to_decimal128_negative() {
10844        let input_type = DataType::Decimal128(10, -1);
10845        let output_type = DataType::Decimal128(10, -2);
10846        assert!(can_cast_types(&input_type, &output_type));
10847        let array = vec![Some(123)];
10848        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
10849        let array = Arc::new(input_decimal_array) as ArrayRef;
10850        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(12_i128),]);
10851
10852        let casted_array = cast(&array, &output_type).unwrap();
10853        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10854
10855        assert_eq!("1200", decimal_arr.value_as_string(0));
10856
10857        let array = vec![Some(125)];
10858        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
10859        let array = Arc::new(input_decimal_array) as ArrayRef;
10860        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(13_i128),]);
10861
10862        let casted_array = cast(&array, &output_type).unwrap();
10863        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10864
10865        assert_eq!("1300", decimal_arr.value_as_string(0));
10866    }
10867
10868    #[test]
10869    fn test_cast_decimal128_to_decimal256_negative() {
10870        let input_type = DataType::Decimal128(10, 3);
10871        let output_type = DataType::Decimal256(10, 5);
10872        assert!(can_cast_types(&input_type, &output_type));
10873        let array = vec![Some(123456), Some(-123456)];
10874        let input_decimal_array = create_decimal128_array(array, 10, 3).unwrap();
10875        let array = Arc::new(input_decimal_array) as ArrayRef;
10876
10877        let hundred = i256::from_i128(100);
10878        generate_cast_test_case!(
10879            &array,
10880            Decimal256Array,
10881            &output_type,
10882            vec![
10883                Some(i256::from_i128(123456).mul_wrapping(hundred)),
10884                Some(i256::from_i128(-123456).mul_wrapping(hundred))
10885            ]
10886        );
10887    }
10888
10889    #[test]
10890    fn test_parse_string_to_decimal() {
10891        assert_eq!(
10892            Decimal128Type::format_decimal(
10893                parse_string_to_decimal_native::<Decimal128Type>("123.45", 2).unwrap(),
10894                38,
10895                2,
10896            ),
10897            "123.45"
10898        );
10899        assert_eq!(
10900            Decimal128Type::format_decimal(
10901                parse_string_to_decimal_native::<Decimal128Type>("12345", 2).unwrap(),
10902                38,
10903                2,
10904            ),
10905            "12345.00"
10906        );
10907        assert_eq!(
10908            Decimal128Type::format_decimal(
10909                parse_string_to_decimal_native::<Decimal128Type>("0.12345", 2).unwrap(),
10910                38,
10911                2,
10912            ),
10913            "0.12"
10914        );
10915        assert_eq!(
10916            Decimal128Type::format_decimal(
10917                parse_string_to_decimal_native::<Decimal128Type>(".12345", 2).unwrap(),
10918                38,
10919                2,
10920            ),
10921            "0.12"
10922        );
10923        assert_eq!(
10924            Decimal128Type::format_decimal(
10925                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
10926                38,
10927                2,
10928            ),
10929            "0.13"
10930        );
10931        assert_eq!(
10932            Decimal128Type::format_decimal(
10933                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
10934                38,
10935                2,
10936            ),
10937            "0.13"
10938        );
10939
10940        assert_eq!(
10941            Decimal256Type::format_decimal(
10942                parse_string_to_decimal_native::<Decimal256Type>("123.45", 3).unwrap(),
10943                38,
10944                3,
10945            ),
10946            "123.450"
10947        );
10948        assert_eq!(
10949            Decimal256Type::format_decimal(
10950                parse_string_to_decimal_native::<Decimal256Type>("12345", 3).unwrap(),
10951                38,
10952                3,
10953            ),
10954            "12345.000"
10955        );
10956        assert_eq!(
10957            Decimal256Type::format_decimal(
10958                parse_string_to_decimal_native::<Decimal256Type>("0.12345", 3).unwrap(),
10959                38,
10960                3,
10961            ),
10962            "0.123"
10963        );
10964        assert_eq!(
10965            Decimal256Type::format_decimal(
10966                parse_string_to_decimal_native::<Decimal256Type>(".12345", 3).unwrap(),
10967                38,
10968                3,
10969            ),
10970            "0.123"
10971        );
10972        assert_eq!(
10973            Decimal256Type::format_decimal(
10974                parse_string_to_decimal_native::<Decimal256Type>(".1265", 3).unwrap(),
10975                38,
10976                3,
10977            ),
10978            "0.127"
10979        );
10980    }
10981
10982    fn test_cast_string_to_decimal(array: ArrayRef) {
10983        // Decimal128
10984        let output_type = DataType::Decimal128(38, 2);
10985        assert!(can_cast_types(array.data_type(), &output_type));
10986
10987        let casted_array = cast(&array, &output_type).unwrap();
10988        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10989
10990        assert_eq!("123.45", decimal_arr.value_as_string(0));
10991        assert_eq!("1.23", decimal_arr.value_as_string(1));
10992        assert_eq!("0.12", decimal_arr.value_as_string(2));
10993        assert_eq!("0.13", decimal_arr.value_as_string(3));
10994        assert_eq!("1.26", decimal_arr.value_as_string(4));
10995        assert_eq!("12345.00", decimal_arr.value_as_string(5));
10996        assert_eq!("12345.00", decimal_arr.value_as_string(6));
10997        assert_eq!("0.12", decimal_arr.value_as_string(7));
10998        assert_eq!("12.23", decimal_arr.value_as_string(8));
10999        assert!(decimal_arr.is_null(9));
11000        assert!(decimal_arr.is_null(10));
11001        assert!(decimal_arr.is_null(11));
11002        assert!(decimal_arr.is_null(12));
11003        assert_eq!("-1.23", decimal_arr.value_as_string(13));
11004        assert_eq!("-1.24", decimal_arr.value_as_string(14));
11005        assert_eq!("0.00", decimal_arr.value_as_string(15));
11006        assert_eq!("-123.00", decimal_arr.value_as_string(16));
11007        assert_eq!("-123.23", decimal_arr.value_as_string(17));
11008        assert_eq!("-0.12", decimal_arr.value_as_string(18));
11009        assert_eq!("1.23", decimal_arr.value_as_string(19));
11010        assert_eq!("1.24", decimal_arr.value_as_string(20));
11011        assert_eq!("0.00", decimal_arr.value_as_string(21));
11012        assert_eq!("123.00", decimal_arr.value_as_string(22));
11013        assert_eq!("123.23", decimal_arr.value_as_string(23));
11014        assert_eq!("0.12", decimal_arr.value_as_string(24));
11015        assert!(decimal_arr.is_null(25));
11016        assert!(decimal_arr.is_null(26));
11017        assert!(decimal_arr.is_null(27));
11018        assert_eq!("0.00", decimal_arr.value_as_string(28));
11019        assert_eq!("0.00", decimal_arr.value_as_string(29));
11020        assert_eq!("12345.00", decimal_arr.value_as_string(30));
11021        assert_eq!(decimal_arr.len(), 31);
11022
11023        // Decimal256
11024        let output_type = DataType::Decimal256(76, 3);
11025        assert!(can_cast_types(array.data_type(), &output_type));
11026
11027        let casted_array = cast(&array, &output_type).unwrap();
11028        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
11029
11030        assert_eq!("123.450", decimal_arr.value_as_string(0));
11031        assert_eq!("1.235", decimal_arr.value_as_string(1));
11032        assert_eq!("0.123", decimal_arr.value_as_string(2));
11033        assert_eq!("0.127", decimal_arr.value_as_string(3));
11034        assert_eq!("1.263", decimal_arr.value_as_string(4));
11035        assert_eq!("12345.000", decimal_arr.value_as_string(5));
11036        assert_eq!("12345.000", decimal_arr.value_as_string(6));
11037        assert_eq!("0.123", decimal_arr.value_as_string(7));
11038        assert_eq!("12.234", decimal_arr.value_as_string(8));
11039        assert!(decimal_arr.is_null(9));
11040        assert!(decimal_arr.is_null(10));
11041        assert!(decimal_arr.is_null(11));
11042        assert!(decimal_arr.is_null(12));
11043        assert_eq!("-1.235", decimal_arr.value_as_string(13));
11044        assert_eq!("-1.236", decimal_arr.value_as_string(14));
11045        assert_eq!("0.000", decimal_arr.value_as_string(15));
11046        assert_eq!("-123.000", decimal_arr.value_as_string(16));
11047        assert_eq!("-123.234", decimal_arr.value_as_string(17));
11048        assert_eq!("-0.123", decimal_arr.value_as_string(18));
11049        assert_eq!("1.235", decimal_arr.value_as_string(19));
11050        assert_eq!("1.236", decimal_arr.value_as_string(20));
11051        assert_eq!("0.000", decimal_arr.value_as_string(21));
11052        assert_eq!("123.000", decimal_arr.value_as_string(22));
11053        assert_eq!("123.234", decimal_arr.value_as_string(23));
11054        assert_eq!("0.123", decimal_arr.value_as_string(24));
11055        assert!(decimal_arr.is_null(25));
11056        assert!(decimal_arr.is_null(26));
11057        assert!(decimal_arr.is_null(27));
11058        assert_eq!("0.000", decimal_arr.value_as_string(28));
11059        assert_eq!("0.000", decimal_arr.value_as_string(29));
11060        assert_eq!("12345.000", decimal_arr.value_as_string(30));
11061        assert_eq!(decimal_arr.len(), 31);
11062    }
11063
11064    #[test]
11065    fn test_cast_utf8_to_decimal() {
11066        let str_array = StringArray::from(vec![
11067            Some("123.45"),
11068            Some("1.2345"),
11069            Some("0.12345"),
11070            Some("0.1267"),
11071            Some("1.263"),
11072            Some("12345.0"),
11073            Some("12345"),
11074            Some("000.123"),
11075            Some("12.234000"),
11076            None,
11077            Some(""),
11078            Some(" "),
11079            None,
11080            Some("-1.23499999"),
11081            Some("-1.23599999"),
11082            Some("-0.00001"),
11083            Some("-123"),
11084            Some("-123.234000"),
11085            Some("-000.123"),
11086            Some("+1.23499999"),
11087            Some("+1.23599999"),
11088            Some("+0.00001"),
11089            Some("+123"),
11090            Some("+123.234000"),
11091            Some("+000.123"),
11092            Some("1.-23499999"),
11093            Some("-1.-23499999"),
11094            Some("--1.23499999"),
11095            Some("0"),
11096            Some("000.000"),
11097            Some("0000000000000000012345.000"),
11098        ]);
11099        let array = Arc::new(str_array) as ArrayRef;
11100
11101        test_cast_string_to_decimal(array);
11102
11103        let test_cases = [
11104            (None, None),
11105            (Some(""), None),
11106            (Some("   "), None),
11107            (Some("0"), Some("0")),
11108            (Some("000.000"), Some("0")),
11109            (Some("12345"), Some("12345")),
11110            (Some("000000000000000000000000000012345"), Some("12345")),
11111            (Some("-123"), Some("-123")),
11112            (Some("+123"), Some("123")),
11113        ];
11114        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
11115        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
11116
11117        let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
11118        test_cast_string_to_decimal_scale_zero(array, &expected);
11119    }
11120
11121    #[test]
11122    fn test_cast_large_utf8_to_decimal() {
11123        let str_array = LargeStringArray::from(vec![
11124            Some("123.45"),
11125            Some("1.2345"),
11126            Some("0.12345"),
11127            Some("0.1267"),
11128            Some("1.263"),
11129            Some("12345.0"),
11130            Some("12345"),
11131            Some("000.123"),
11132            Some("12.234000"),
11133            None,
11134            Some(""),
11135            Some(" "),
11136            None,
11137            Some("-1.23499999"),
11138            Some("-1.23599999"),
11139            Some("-0.00001"),
11140            Some("-123"),
11141            Some("-123.234000"),
11142            Some("-000.123"),
11143            Some("+1.23499999"),
11144            Some("+1.23599999"),
11145            Some("+0.00001"),
11146            Some("+123"),
11147            Some("+123.234000"),
11148            Some("+000.123"),
11149            Some("1.-23499999"),
11150            Some("-1.-23499999"),
11151            Some("--1.23499999"),
11152            Some("0"),
11153            Some("000.000"),
11154            Some("0000000000000000012345.000"),
11155        ]);
11156        let array = Arc::new(str_array) as ArrayRef;
11157
11158        test_cast_string_to_decimal(array);
11159
11160        let test_cases = [
11161            (None, None),
11162            (Some(""), None),
11163            (Some("   "), None),
11164            (Some("0"), Some("0")),
11165            (Some("000.000"), Some("0")),
11166            (Some("12345"), Some("12345")),
11167            (Some("000000000000000000000000000012345"), Some("12345")),
11168            (Some("-123"), Some("-123")),
11169            (Some("+123"), Some("123")),
11170        ];
11171        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
11172        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
11173
11174        let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
11175        test_cast_string_to_decimal_scale_zero(array, &expected);
11176    }
11177
11178    fn test_cast_string_to_decimal_scale_zero(
11179        array: ArrayRef,
11180        expected_as_string: &[Option<&str>],
11181    ) {
11182        // Decimal128
11183        let output_type = DataType::Decimal128(38, 0);
11184        assert!(can_cast_types(array.data_type(), &output_type));
11185        let casted_array = cast(&array, &output_type).unwrap();
11186        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11187        assert_decimal_array_contents(decimal_arr, expected_as_string);
11188
11189        // Decimal256
11190        let output_type = DataType::Decimal256(76, 0);
11191        assert!(can_cast_types(array.data_type(), &output_type));
11192        let casted_array = cast(&array, &output_type).unwrap();
11193        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
11194        assert_decimal_array_contents(decimal_arr, expected_as_string);
11195    }
11196
11197    fn assert_decimal_array_contents<T>(
11198        array: &PrimitiveArray<T>,
11199        expected_as_string: &[Option<&str>],
11200    ) where
11201        T: DecimalType + ArrowPrimitiveType,
11202    {
11203        assert_eq!(array.len(), expected_as_string.len());
11204        for (i, expected) in expected_as_string.iter().enumerate() {
11205            let actual = if array.is_null(i) {
11206                None
11207            } else {
11208                Some(array.value_as_string(i))
11209            };
11210            let actual = actual.as_ref().map(|s| s.as_ref());
11211            assert_eq!(*expected, actual, "Expected at position {i}");
11212        }
11213    }
11214
11215    #[test]
11216    fn test_cast_invalid_utf8_to_decimal() {
11217        let str_array = StringArray::from(vec!["4.4.5", ". 0.123"]);
11218        let array = Arc::new(str_array) as ArrayRef;
11219
11220        // Safe cast
11221        let output_type = DataType::Decimal128(38, 2);
11222        let casted_array = cast(&array, &output_type).unwrap();
11223        assert!(casted_array.is_null(0));
11224        assert!(casted_array.is_null(1));
11225
11226        let output_type = DataType::Decimal256(76, 2);
11227        let casted_array = cast(&array, &output_type).unwrap();
11228        assert!(casted_array.is_null(0));
11229        assert!(casted_array.is_null(1));
11230
11231        // Non-safe cast
11232        let output_type = DataType::Decimal128(38, 2);
11233        let str_array = StringArray::from(vec!["4.4.5"]);
11234        let array = Arc::new(str_array) as ArrayRef;
11235        let option = CastOptions {
11236            safe: false,
11237            format_options: FormatOptions::default(),
11238        };
11239        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
11240        assert!(
11241            casted_err
11242                .to_string()
11243                .contains("Cannot cast string '4.4.5' to value of Decimal128(38, 10) type")
11244        );
11245
11246        let str_array = StringArray::from(vec![". 0.123"]);
11247        let array = Arc::new(str_array) as ArrayRef;
11248        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
11249        assert!(
11250            casted_err
11251                .to_string()
11252                .contains("Cannot cast string '. 0.123' to value of Decimal128(38, 10) type")
11253        );
11254
11255        let str_array = StringArray::from(vec![""]);
11256        let array = Arc::new(str_array) as ArrayRef;
11257        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
11258        assert!(
11259            casted_err
11260                .to_string()
11261                .contains("Cannot cast string '' to value of Decimal128(38, 10) type")
11262        );
11263    }
11264
11265    fn test_cast_string_to_decimal128_overflow(overflow_array: ArrayRef) {
11266        let output_type = DataType::Decimal128(38, 2);
11267        let casted_array = cast(&overflow_array, &output_type).unwrap();
11268        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
11269
11270        assert!(decimal_arr.is_null(0));
11271        assert!(decimal_arr.is_null(1));
11272        assert!(decimal_arr.is_null(2));
11273        assert_eq!(
11274            "999999999999999999999999999999999999.99",
11275            decimal_arr.value_as_string(3)
11276        );
11277        assert_eq!(
11278            "100000000000000000000000000000000000.00",
11279            decimal_arr.value_as_string(4)
11280        );
11281    }
11282
11283    #[test]
11284    fn test_cast_string_to_decimal128_precision_overflow() {
11285        let array = StringArray::from(vec!["1000".to_string()]);
11286        let array = Arc::new(array) as ArrayRef;
11287        let casted_array = cast_with_options(
11288            &array,
11289            &DataType::Decimal128(10, 8),
11290            &CastOptions {
11291                safe: true,
11292                format_options: FormatOptions::default(),
11293            },
11294        );
11295        assert!(casted_array.is_ok());
11296        assert!(casted_array.unwrap().is_null(0));
11297
11298        let err = cast_with_options(
11299            &array,
11300            &DataType::Decimal128(10, 8),
11301            &CastOptions {
11302                safe: false,
11303                format_options: FormatOptions::default(),
11304            },
11305        );
11306        assert_eq!(
11307            "Invalid argument error: 1000.00000000 is too large to store in a Decimal128 of precision 10. Max is 99.99999999",
11308            err.unwrap_err().to_string()
11309        );
11310    }
11311
11312    #[test]
11313    fn test_cast_utf8_to_decimal128_overflow() {
11314        let overflow_str_array = StringArray::from(vec![
11315            i128::MAX.to_string(),
11316            i128::MIN.to_string(),
11317            "99999999999999999999999999999999999999".to_string(),
11318            "999999999999999999999999999999999999.99".to_string(),
11319            "99999999999999999999999999999999999.999".to_string(),
11320        ]);
11321        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11322
11323        test_cast_string_to_decimal128_overflow(overflow_array);
11324    }
11325
11326    #[test]
11327    fn test_cast_large_utf8_to_decimal128_overflow() {
11328        let overflow_str_array = LargeStringArray::from(vec![
11329            i128::MAX.to_string(),
11330            i128::MIN.to_string(),
11331            "99999999999999999999999999999999999999".to_string(),
11332            "999999999999999999999999999999999999.99".to_string(),
11333            "99999999999999999999999999999999999.999".to_string(),
11334        ]);
11335        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11336
11337        test_cast_string_to_decimal128_overflow(overflow_array);
11338    }
11339
11340    fn test_cast_string_to_decimal256_overflow(overflow_array: ArrayRef) {
11341        let output_type = DataType::Decimal256(76, 2);
11342        let casted_array = cast(&overflow_array, &output_type).unwrap();
11343        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
11344
11345        assert_eq!(
11346            "170141183460469231731687303715884105727.00",
11347            decimal_arr.value_as_string(0)
11348        );
11349        assert_eq!(
11350            "-170141183460469231731687303715884105728.00",
11351            decimal_arr.value_as_string(1)
11352        );
11353        assert_eq!(
11354            "99999999999999999999999999999999999999.00",
11355            decimal_arr.value_as_string(2)
11356        );
11357        assert_eq!(
11358            "999999999999999999999999999999999999.99",
11359            decimal_arr.value_as_string(3)
11360        );
11361        assert_eq!(
11362            "100000000000000000000000000000000000.00",
11363            decimal_arr.value_as_string(4)
11364        );
11365        assert!(decimal_arr.is_null(5));
11366        assert!(decimal_arr.is_null(6));
11367    }
11368
11369    #[test]
11370    fn test_cast_string_to_decimal256_precision_overflow() {
11371        let array = StringArray::from(vec!["1000".to_string()]);
11372        let array = Arc::new(array) as ArrayRef;
11373        let casted_array = cast_with_options(
11374            &array,
11375            &DataType::Decimal256(10, 8),
11376            &CastOptions {
11377                safe: true,
11378                format_options: FormatOptions::default(),
11379            },
11380        );
11381        assert!(casted_array.is_ok());
11382        assert!(casted_array.unwrap().is_null(0));
11383
11384        let err = cast_with_options(
11385            &array,
11386            &DataType::Decimal256(10, 8),
11387            &CastOptions {
11388                safe: false,
11389                format_options: FormatOptions::default(),
11390            },
11391        );
11392        assert_eq!(
11393            "Invalid argument error: 1000.00000000 is too large to store in a Decimal256 of precision 10. Max is 99.99999999",
11394            err.unwrap_err().to_string()
11395        );
11396    }
11397
11398    #[test]
11399    fn test_cast_utf8_to_decimal256_overflow() {
11400        let overflow_str_array = StringArray::from(vec![
11401            i128::MAX.to_string(),
11402            i128::MIN.to_string(),
11403            "99999999999999999999999999999999999999".to_string(),
11404            "999999999999999999999999999999999999.99".to_string(),
11405            "99999999999999999999999999999999999.999".to_string(),
11406            i256::MAX.to_string(),
11407            i256::MIN.to_string(),
11408        ]);
11409        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11410
11411        test_cast_string_to_decimal256_overflow(overflow_array);
11412    }
11413
11414    #[test]
11415    fn test_cast_large_utf8_to_decimal256_overflow() {
11416        let overflow_str_array = LargeStringArray::from(vec![
11417            i128::MAX.to_string(),
11418            i128::MIN.to_string(),
11419            "99999999999999999999999999999999999999".to_string(),
11420            "999999999999999999999999999999999999.99".to_string(),
11421            "99999999999999999999999999999999999.999".to_string(),
11422            i256::MAX.to_string(),
11423            i256::MIN.to_string(),
11424        ]);
11425        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
11426
11427        test_cast_string_to_decimal256_overflow(overflow_array);
11428    }
11429
11430    #[test]
11431    fn test_cast_outside_supported_range_for_nanoseconds() {
11432        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";
11433
11434        let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
11435
11436        let cast_options = CastOptions {
11437            safe: false,
11438            format_options: FormatOptions::default(),
11439        };
11440
11441        let result = cast_string_to_timestamp::<i32, TimestampNanosecondType>(
11442            &array,
11443            &None::<Arc<str>>,
11444            &cast_options,
11445        );
11446
11447        let err = result.unwrap_err();
11448        assert_eq!(
11449            err.to_string(),
11450            format!(
11451                "Cast error: Overflow converting {} to Nanosecond. {}",
11452                array.value(0),
11453                EXPECTED_ERROR_MESSAGE
11454            )
11455        );
11456    }
11457
11458    #[test]
11459    fn test_cast_date32_to_timestamp() {
11460        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
11461        let array = Arc::new(a) as ArrayRef;
11462        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
11463        let c = b.as_primitive::<TimestampSecondType>();
11464        assert_eq!(1609459200, c.value(0));
11465        assert_eq!(1640995200, c.value(1));
11466        assert!(c.is_null(2));
11467    }
11468
11469    #[test]
11470    fn test_cast_date32_to_timestamp_ms() {
11471        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
11472        let array = Arc::new(a) as ArrayRef;
11473        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
11474        let c = b
11475            .as_any()
11476            .downcast_ref::<TimestampMillisecondArray>()
11477            .unwrap();
11478        assert_eq!(1609459200000, c.value(0));
11479        assert_eq!(1640995200000, c.value(1));
11480        assert!(c.is_null(2));
11481    }
11482
11483    #[test]
11484    fn test_cast_date32_to_timestamp_us() {
11485        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
11486        let array = Arc::new(a) as ArrayRef;
11487        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
11488        let c = b
11489            .as_any()
11490            .downcast_ref::<TimestampMicrosecondArray>()
11491            .unwrap();
11492        assert_eq!(1609459200000000, c.value(0));
11493        assert_eq!(1640995200000000, c.value(1));
11494        assert!(c.is_null(2));
11495    }
11496
11497    #[test]
11498    fn test_cast_date32_to_timestamp_ns() {
11499        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
11500        let array = Arc::new(a) as ArrayRef;
11501        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
11502        let c = b
11503            .as_any()
11504            .downcast_ref::<TimestampNanosecondArray>()
11505            .unwrap();
11506        assert_eq!(1609459200000000000, c.value(0));
11507        assert_eq!(1640995200000000000, c.value(1));
11508        assert!(c.is_null(2));
11509    }
11510
11511    #[test]
11512    fn test_cast_date32_to_timestamp_us_overflow() {
11513        const MAX_DAYS_MICROS: i32 = (i64::MAX / MICROSECONDS_IN_DAY) as i32;
11514        let a = Date32Array::from(vec![Some(MAX_DAYS_MICROS), Some(MAX_DAYS_MICROS + 1), None]);
11515        let array = Arc::new(a) as ArrayRef;
11516        let err = cast_with_options(
11517            &array,
11518            &DataType::Timestamp(TimeUnit::Microsecond, None),
11519            &CastOptions {
11520                safe: false,
11521                format_options: FormatOptions::default(),
11522            },
11523        );
11524        assert!(err.is_err());
11525
11526        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
11527        let c = b.as_primitive::<TimestampMicrosecondType>();
11528        assert_eq!(MAX_DAYS_MICROS as i64 * MICROSECONDS_IN_DAY, c.value(0));
11529        assert!(c.is_null(1));
11530        assert!(c.is_null(2));
11531    }
11532
11533    #[test]
11534    fn test_cast_date32_to_timestamp_ns_overflow() {
11535        // 2262-04-11, 2062-04-12
11536        let upper_limit = 106_751;
11537        let a = Date32Array::from(vec![Some(upper_limit), Some(upper_limit + 1), None]);
11538        let array = Arc::new(a) as ArrayRef;
11539        let err = cast_with_options(
11540            &array,
11541            &DataType::Timestamp(TimeUnit::Nanosecond, None),
11542            &CastOptions {
11543                safe: false,
11544                format_options: FormatOptions::default(),
11545            },
11546        );
11547        assert!(err.is_err());
11548
11549        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
11550        let c = b.as_primitive::<TimestampNanosecondType>();
11551        assert_eq!(upper_limit as i64 * NANOSECONDS_IN_DAY, c.value(0));
11552        assert!(c.is_null(1));
11553        assert!(c.is_null(2));
11554    }
11555
11556    #[test]
11557    fn test_timezone_cast() {
11558        let a = StringArray::from(vec![
11559            "2000-01-01T12:00:00", // date + time valid
11560            "2020-12-15T12:34:56", // date + time valid
11561        ]);
11562        let array = Arc::new(a) as ArrayRef;
11563        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
11564        let v = b.as_primitive::<TimestampNanosecondType>();
11565
11566        assert_eq!(v.value(0), 946728000000000000);
11567        assert_eq!(v.value(1), 1608035696000000000);
11568
11569        let b = cast(
11570            &b,
11571            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11572        )
11573        .unwrap();
11574        let v = b.as_primitive::<TimestampNanosecondType>();
11575
11576        assert_eq!(v.value(0), 946728000000000000);
11577        assert_eq!(v.value(1), 1608035696000000000);
11578
11579        let b = cast(
11580            &b,
11581            &DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
11582        )
11583        .unwrap();
11584        let v = b.as_primitive::<TimestampMillisecondType>();
11585
11586        assert_eq!(v.value(0), 946728000000);
11587        assert_eq!(v.value(1), 1608035696000);
11588    }
11589
11590    #[test]
11591    fn test_cast_utf8_to_timestamp() {
11592        fn test_tz(tz: Arc<str>) {
11593            let valid = StringArray::from(vec![
11594                "2023-01-01 04:05:06.789000-08:00",
11595                "2023-01-01 04:05:06.789000-07:00",
11596                "2023-01-01 04:05:06.789 -0800",
11597                "2023-01-01 04:05:06.789 -08:00",
11598                "2023-01-01 040506 +0730",
11599                "2023-01-01 040506 +07:30",
11600                "2023-01-01 04:05:06.789",
11601                "2023-01-01 04:05:06",
11602                "2023-01-01",
11603            ]);
11604
11605            let array = Arc::new(valid) as ArrayRef;
11606            let b = cast_with_options(
11607                &array,
11608                &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
11609                &CastOptions {
11610                    safe: false,
11611                    format_options: FormatOptions::default(),
11612                },
11613            )
11614            .unwrap();
11615
11616            let tz = tz.as_ref().parse().unwrap();
11617
11618            let as_tz =
11619                |v: i64| as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap();
11620
11621            let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
11622            let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
11623
11624            let values = b.as_primitive::<TimestampNanosecondType>().values();
11625            let utc_results: Vec<_> = values.iter().map(as_utc).collect();
11626            let local_results: Vec<_> = values.iter().map(as_local).collect();
11627
11628            // Absolute timestamps should be parsed preserving the same UTC instant
11629            assert_eq!(
11630                &utc_results[..6],
11631                &[
11632                    "2023-01-01 12:05:06.789".to_string(),
11633                    "2023-01-01 11:05:06.789".to_string(),
11634                    "2023-01-01 12:05:06.789".to_string(),
11635                    "2023-01-01 12:05:06.789".to_string(),
11636                    "2022-12-31 20:35:06".to_string(),
11637                    "2022-12-31 20:35:06".to_string(),
11638                ]
11639            );
11640            // Non-absolute timestamps should be parsed preserving the same local instant
11641            assert_eq!(
11642                &local_results[6..],
11643                &[
11644                    "2023-01-01 04:05:06.789".to_string(),
11645                    "2023-01-01 04:05:06".to_string(),
11646                    "2023-01-01 00:00:00".to_string()
11647                ]
11648            )
11649        }
11650
11651        test_tz("+00:00".into());
11652        test_tz("+02:00".into());
11653    }
11654
11655    #[test]
11656    fn test_cast_invalid_utf8() {
11657        let v1: &[u8] = b"\xFF invalid";
11658        let v2: &[u8] = b"\x00 Foo";
11659        let s = BinaryArray::from(vec![v1, v2]);
11660        let options = CastOptions {
11661            safe: true,
11662            format_options: FormatOptions::default(),
11663        };
11664        let array = cast_with_options(&s, &DataType::Utf8, &options).unwrap();
11665        let a = array.as_string::<i32>();
11666        a.to_data().validate_full().unwrap();
11667
11668        assert_eq!(a.null_count(), 1);
11669        assert_eq!(a.len(), 2);
11670        assert!(a.is_null(0));
11671        assert_eq!(a.value(0), "");
11672        assert_eq!(a.value(1), "\x00 Foo");
11673    }
11674
11675    #[test]
11676    fn test_cast_utf8_to_timestamptz() {
11677        let valid = StringArray::from(vec!["2023-01-01"]);
11678
11679        let array = Arc::new(valid) as ArrayRef;
11680        let b = cast(
11681            &array,
11682            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11683        )
11684        .unwrap();
11685
11686        let expect = DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into()));
11687
11688        assert_eq!(b.data_type(), &expect);
11689        let c = b
11690            .as_any()
11691            .downcast_ref::<TimestampNanosecondArray>()
11692            .unwrap();
11693        assert_eq!(1672531200000000000, c.value(0));
11694    }
11695
11696    #[test]
11697    fn test_cast_decimal_to_string() {
11698        assert!(can_cast_types(
11699            &DataType::Decimal32(9, 4),
11700            &DataType::Utf8View
11701        ));
11702        assert!(can_cast_types(
11703            &DataType::Decimal64(16, 4),
11704            &DataType::Utf8View
11705        ));
11706        assert!(can_cast_types(
11707            &DataType::Decimal128(10, 4),
11708            &DataType::Utf8View
11709        ));
11710        assert!(can_cast_types(
11711            &DataType::Decimal256(38, 10),
11712            &DataType::Utf8View
11713        ));
11714
11715        macro_rules! assert_decimal_values {
11716            ($array:expr) => {
11717                let c = $array;
11718                assert_eq!("1123.454", c.value(0));
11719                assert_eq!("2123.456", c.value(1));
11720                assert_eq!("-3123.453", c.value(2));
11721                assert_eq!("-3123.456", c.value(3));
11722                assert_eq!("0.000", c.value(4));
11723                assert_eq!("0.123", c.value(5));
11724                assert_eq!("1234.567", c.value(6));
11725                assert_eq!("-1234.567", c.value(7));
11726                assert!(c.is_null(8));
11727            };
11728        }
11729
11730        fn test_decimal_to_string<IN: ArrowPrimitiveType, OffsetSize: OffsetSizeTrait>(
11731            output_type: DataType,
11732            array: PrimitiveArray<IN>,
11733        ) {
11734            let b = cast(&array, &output_type).unwrap();
11735
11736            assert_eq!(b.data_type(), &output_type);
11737            match b.data_type() {
11738                DataType::Utf8View => {
11739                    let c = b.as_string_view();
11740                    assert_decimal_values!(c);
11741                }
11742                DataType::Utf8 | DataType::LargeUtf8 => {
11743                    let c = b.as_string::<OffsetSize>();
11744                    assert_decimal_values!(c);
11745                }
11746                _ => (),
11747            }
11748        }
11749
11750        let array32: Vec<Option<i32>> = vec![
11751            Some(1123454),
11752            Some(2123456),
11753            Some(-3123453),
11754            Some(-3123456),
11755            Some(0),
11756            Some(123),
11757            Some(123456789),
11758            Some(-123456789),
11759            None,
11760        ];
11761        let array64: Vec<Option<i64>> = array32.iter().map(|num| num.map(|x| x as i64)).collect();
11762        let array128: Vec<Option<i128>> =
11763            array64.iter().map(|num| num.map(|x| x as i128)).collect();
11764        let array256: Vec<Option<i256>> = array128
11765            .iter()
11766            .map(|num| num.map(i256::from_i128))
11767            .collect();
11768
11769        test_decimal_to_string::<Decimal32Type, i32>(
11770            DataType::Utf8View,
11771            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
11772        );
11773        test_decimal_to_string::<Decimal32Type, i32>(
11774            DataType::Utf8,
11775            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
11776        );
11777        test_decimal_to_string::<Decimal32Type, i64>(
11778            DataType::LargeUtf8,
11779            create_decimal32_array(array32, 7, 3).unwrap(),
11780        );
11781
11782        test_decimal_to_string::<Decimal64Type, i32>(
11783            DataType::Utf8View,
11784            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
11785        );
11786        test_decimal_to_string::<Decimal64Type, i32>(
11787            DataType::Utf8,
11788            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
11789        );
11790        test_decimal_to_string::<Decimal64Type, i64>(
11791            DataType::LargeUtf8,
11792            create_decimal64_array(array64, 7, 3).unwrap(),
11793        );
11794
11795        test_decimal_to_string::<Decimal128Type, i32>(
11796            DataType::Utf8View,
11797            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
11798        );
11799        test_decimal_to_string::<Decimal128Type, i32>(
11800            DataType::Utf8,
11801            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
11802        );
11803        test_decimal_to_string::<Decimal128Type, i64>(
11804            DataType::LargeUtf8,
11805            create_decimal128_array(array128, 7, 3).unwrap(),
11806        );
11807
11808        test_decimal_to_string::<Decimal256Type, i32>(
11809            DataType::Utf8View,
11810            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
11811        );
11812        test_decimal_to_string::<Decimal256Type, i32>(
11813            DataType::Utf8,
11814            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
11815        );
11816        test_decimal_to_string::<Decimal256Type, i64>(
11817            DataType::LargeUtf8,
11818            create_decimal256_array(array256, 7, 3).unwrap(),
11819        );
11820    }
11821
11822    #[test]
11823    fn test_cast_numeric_to_decimal128_precision_overflow() {
11824        let array = Int64Array::from(vec![1234567]);
11825        let array = Arc::new(array) as ArrayRef;
11826        let casted_array = cast_with_options(
11827            &array,
11828            &DataType::Decimal128(7, 3),
11829            &CastOptions {
11830                safe: true,
11831                format_options: FormatOptions::default(),
11832            },
11833        );
11834        assert!(casted_array.is_ok());
11835        assert!(casted_array.unwrap().is_null(0));
11836
11837        let err = cast_with_options(
11838            &array,
11839            &DataType::Decimal128(7, 3),
11840            &CastOptions {
11841                safe: false,
11842                format_options: FormatOptions::default(),
11843            },
11844        );
11845        assert_eq!(
11846            "Invalid argument error: 1234567.000 is too large to store in a Decimal128 of precision 7. Max is 9999.999",
11847            err.unwrap_err().to_string()
11848        );
11849    }
11850
11851    #[test]
11852    fn test_cast_numeric_to_decimal256_precision_overflow() {
11853        let array = Int64Array::from(vec![1234567]);
11854        let array = Arc::new(array) as ArrayRef;
11855        let casted_array = cast_with_options(
11856            &array,
11857            &DataType::Decimal256(7, 3),
11858            &CastOptions {
11859                safe: true,
11860                format_options: FormatOptions::default(),
11861            },
11862        );
11863        assert!(casted_array.is_ok());
11864        assert!(casted_array.unwrap().is_null(0));
11865
11866        let err = cast_with_options(
11867            &array,
11868            &DataType::Decimal256(7, 3),
11869            &CastOptions {
11870                safe: false,
11871                format_options: FormatOptions::default(),
11872            },
11873        );
11874        assert_eq!(
11875            "Invalid argument error: 1234567.000 is too large to store in a Decimal256 of precision 7. Max is 9999.999",
11876            err.unwrap_err().to_string()
11877        );
11878    }
11879
11880    /// helper function to test casting from duration to interval
11881    fn cast_from_duration_to_interval<T: ArrowTemporalType<Native = i64>>(
11882        array: Vec<i64>,
11883        cast_options: &CastOptions,
11884    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
11885        let array = PrimitiveArray::<T>::new(array.into(), None);
11886        let array = Arc::new(array) as ArrayRef;
11887        let interval = DataType::Interval(IntervalUnit::MonthDayNano);
11888        let out = cast_with_options(&array, &interval, cast_options)?;
11889        let out = out.as_primitive::<IntervalMonthDayNanoType>().clone();
11890        Ok(out)
11891    }
11892
11893    #[test]
11894    fn test_cast_from_duration_to_interval() {
11895        // from duration second to interval month day nano
11896        let array = vec![1234567];
11897        let casted_array =
11898            cast_from_duration_to_interval::<DurationSecondType>(array, &CastOptions::default())
11899                .unwrap();
11900        assert_eq!(
11901            casted_array.data_type(),
11902            &DataType::Interval(IntervalUnit::MonthDayNano)
11903        );
11904        assert_eq!(
11905            casted_array.value(0),
11906            IntervalMonthDayNano::new(0, 0, 1234567000000000)
11907        );
11908
11909        let array = vec![i64::MAX];
11910        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
11911            array.clone(),
11912            &CastOptions::default(),
11913        )
11914        .unwrap();
11915        assert!(!casted_array.is_valid(0));
11916
11917        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
11918            array,
11919            &CastOptions {
11920                safe: false,
11921                format_options: FormatOptions::default(),
11922            },
11923        );
11924        assert!(casted_array.is_err());
11925
11926        // from duration millisecond to interval month day nano
11927        let array = vec![1234567];
11928        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11929            array,
11930            &CastOptions::default(),
11931        )
11932        .unwrap();
11933        assert_eq!(
11934            casted_array.data_type(),
11935            &DataType::Interval(IntervalUnit::MonthDayNano)
11936        );
11937        assert_eq!(
11938            casted_array.value(0),
11939            IntervalMonthDayNano::new(0, 0, 1234567000000)
11940        );
11941
11942        let array = vec![i64::MAX];
11943        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11944            array.clone(),
11945            &CastOptions::default(),
11946        )
11947        .unwrap();
11948        assert!(!casted_array.is_valid(0));
11949
11950        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11951            array,
11952            &CastOptions {
11953                safe: false,
11954                format_options: FormatOptions::default(),
11955            },
11956        );
11957        assert!(casted_array.is_err());
11958
11959        // from duration microsecond to interval month day nano
11960        let array = vec![1234567];
11961        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11962            array,
11963            &CastOptions::default(),
11964        )
11965        .unwrap();
11966        assert_eq!(
11967            casted_array.data_type(),
11968            &DataType::Interval(IntervalUnit::MonthDayNano)
11969        );
11970        assert_eq!(
11971            casted_array.value(0),
11972            IntervalMonthDayNano::new(0, 0, 1234567000)
11973        );
11974
11975        let array = vec![i64::MAX];
11976        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11977            array.clone(),
11978            &CastOptions::default(),
11979        )
11980        .unwrap();
11981        assert!(!casted_array.is_valid(0));
11982
11983        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11984            array,
11985            &CastOptions {
11986                safe: false,
11987                format_options: FormatOptions::default(),
11988            },
11989        );
11990        assert!(casted_array.is_err());
11991
11992        // from duration nanosecond to interval month day nano
11993        let array = vec![1234567];
11994        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
11995            array,
11996            &CastOptions::default(),
11997        )
11998        .unwrap();
11999        assert_eq!(
12000            casted_array.data_type(),
12001            &DataType::Interval(IntervalUnit::MonthDayNano)
12002        );
12003        assert_eq!(
12004            casted_array.value(0),
12005            IntervalMonthDayNano::new(0, 0, 1234567)
12006        );
12007
12008        let array = vec![i64::MAX];
12009        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
12010            array,
12011            &CastOptions {
12012                safe: false,
12013                format_options: FormatOptions::default(),
12014            },
12015        )
12016        .unwrap();
12017        assert_eq!(
12018            casted_array.value(0),
12019            IntervalMonthDayNano::new(0, 0, i64::MAX)
12020        );
12021    }
12022
12023    /// helper function to test casting from interval to duration
12024    fn cast_from_interval_to_duration<T: ArrowTemporalType>(
12025        array: &IntervalMonthDayNanoArray,
12026        cast_options: &CastOptions,
12027    ) -> Result<PrimitiveArray<T>, ArrowError> {
12028        let casted_array = cast_with_options(&array, &T::DATA_TYPE, cast_options)?;
12029        casted_array
12030            .as_any()
12031            .downcast_ref::<PrimitiveArray<T>>()
12032            .ok_or_else(|| {
12033                ArrowError::ComputeError(format!("Failed to downcast to {}", T::DATA_TYPE))
12034            })
12035            .cloned()
12036    }
12037
12038    #[test]
12039    fn test_cast_from_interval_to_duration() {
12040        let nullable = CastOptions::default();
12041        let fallible = CastOptions {
12042            safe: false,
12043            format_options: FormatOptions::default(),
12044        };
12045        let v = IntervalMonthDayNano::new(0, 0, 1234567);
12046
12047        // from interval month day nano to duration second
12048        let array = vec![v].into();
12049        let casted_array: DurationSecondArray =
12050            cast_from_interval_to_duration(&array, &nullable).unwrap();
12051        assert_eq!(casted_array.value(0), 0);
12052
12053        let array = vec![IntervalMonthDayNano::MAX].into();
12054        let casted_array: DurationSecondArray =
12055            cast_from_interval_to_duration(&array, &nullable).unwrap();
12056        assert!(!casted_array.is_valid(0));
12057
12058        let res = cast_from_interval_to_duration::<DurationSecondType>(&array, &fallible);
12059        assert!(res.is_err());
12060
12061        // from interval month day nano to duration millisecond
12062        let array = vec![v].into();
12063        let casted_array: DurationMillisecondArray =
12064            cast_from_interval_to_duration(&array, &nullable).unwrap();
12065        assert_eq!(casted_array.value(0), 1);
12066
12067        let array = vec![IntervalMonthDayNano::MAX].into();
12068        let casted_array: DurationMillisecondArray =
12069            cast_from_interval_to_duration(&array, &nullable).unwrap();
12070        assert!(!casted_array.is_valid(0));
12071
12072        let res = cast_from_interval_to_duration::<DurationMillisecondType>(&array, &fallible);
12073        assert!(res.is_err());
12074
12075        // from interval month day nano to duration microsecond
12076        let array = vec![v].into();
12077        let casted_array: DurationMicrosecondArray =
12078            cast_from_interval_to_duration(&array, &nullable).unwrap();
12079        assert_eq!(casted_array.value(0), 1234);
12080
12081        let array = vec![IntervalMonthDayNano::MAX].into();
12082        let casted_array =
12083            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &nullable).unwrap();
12084        assert!(!casted_array.is_valid(0));
12085
12086        let casted_array =
12087            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &fallible);
12088        assert!(casted_array.is_err());
12089
12090        // from interval month day nano to duration nanosecond
12091        let array = vec![v].into();
12092        let casted_array: DurationNanosecondArray =
12093            cast_from_interval_to_duration(&array, &nullable).unwrap();
12094        assert_eq!(casted_array.value(0), 1234567);
12095
12096        let array = vec![IntervalMonthDayNano::MAX].into();
12097        let casted_array: DurationNanosecondArray =
12098            cast_from_interval_to_duration(&array, &nullable).unwrap();
12099        assert!(!casted_array.is_valid(0));
12100
12101        let casted_array =
12102            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &fallible);
12103        assert!(casted_array.is_err());
12104
12105        let array = vec![
12106            IntervalMonthDayNanoType::make_value(0, 1, 0),
12107            IntervalMonthDayNanoType::make_value(-1, 0, 0),
12108            IntervalMonthDayNanoType::make_value(1, 1, 0),
12109            IntervalMonthDayNanoType::make_value(1, 0, 1),
12110            IntervalMonthDayNanoType::make_value(0, 0, -1),
12111        ]
12112        .into();
12113        let casted_array =
12114            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable).unwrap();
12115        assert!(!casted_array.is_valid(0));
12116        assert!(!casted_array.is_valid(1));
12117        assert!(!casted_array.is_valid(2));
12118        assert!(!casted_array.is_valid(3));
12119        assert!(casted_array.is_valid(4));
12120        assert_eq!(casted_array.value(4), -1);
12121    }
12122
12123    /// helper function to test casting from interval year month to interval month day nano
12124    fn cast_from_interval_year_month_to_interval_month_day_nano(
12125        array: Vec<i32>,
12126        cast_options: &CastOptions,
12127    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
12128        let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
12129        let array = Arc::new(array) as ArrayRef;
12130        let casted_array = cast_with_options(
12131            &array,
12132            &DataType::Interval(IntervalUnit::MonthDayNano),
12133            cast_options,
12134        )?;
12135        casted_array
12136            .as_any()
12137            .downcast_ref::<IntervalMonthDayNanoArray>()
12138            .ok_or_else(|| {
12139                ArrowError::ComputeError(
12140                    "Failed to downcast to IntervalMonthDayNanoArray".to_string(),
12141                )
12142            })
12143            .cloned()
12144    }
12145
12146    #[test]
12147    fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
12148        // from interval year month to interval month day nano
12149        let array = vec![1234567];
12150        let casted_array = cast_from_interval_year_month_to_interval_month_day_nano(
12151            array,
12152            &CastOptions::default(),
12153        )
12154        .unwrap();
12155        assert_eq!(
12156            casted_array.data_type(),
12157            &DataType::Interval(IntervalUnit::MonthDayNano)
12158        );
12159        assert_eq!(
12160            casted_array.value(0),
12161            IntervalMonthDayNano::new(1234567, 0, 0)
12162        );
12163    }
12164
12165    /// helper function to test casting from interval day time to interval month day nano
12166    fn cast_from_interval_day_time_to_interval_month_day_nano(
12167        array: Vec<IntervalDayTime>,
12168        cast_options: &CastOptions,
12169    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
12170        let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
12171        let array = Arc::new(array) as ArrayRef;
12172        let casted_array = cast_with_options(
12173            &array,
12174            &DataType::Interval(IntervalUnit::MonthDayNano),
12175            cast_options,
12176        )?;
12177        Ok(casted_array
12178            .as_primitive::<IntervalMonthDayNanoType>()
12179            .clone())
12180    }
12181
12182    #[test]
12183    fn test_cast_from_interval_day_time_to_interval_month_day_nano() {
12184        // from interval day time to interval month day nano
12185        let array = vec![IntervalDayTime::new(123, 0)];
12186        let casted_array =
12187            cast_from_interval_day_time_to_interval_month_day_nano(array, &CastOptions::default())
12188                .unwrap();
12189        assert_eq!(
12190            casted_array.data_type(),
12191            &DataType::Interval(IntervalUnit::MonthDayNano)
12192        );
12193        assert_eq!(casted_array.value(0), IntervalMonthDayNano::new(0, 123, 0));
12194    }
12195
12196    #[test]
12197    fn test_cast_below_unixtimestamp() {
12198        let valid = StringArray::from(vec![
12199            "1900-01-03 23:59:59",
12200            "1969-12-31 00:00:01",
12201            "1989-12-31 00:00:01",
12202        ]);
12203
12204        let array = Arc::new(valid) as ArrayRef;
12205        let casted_array = cast_with_options(
12206            &array,
12207            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
12208            &CastOptions {
12209                safe: false,
12210                format_options: FormatOptions::default(),
12211            },
12212        )
12213        .unwrap();
12214
12215        let ts_array = casted_array
12216            .as_primitive::<TimestampNanosecondType>()
12217            .values()
12218            .iter()
12219            .map(|ts| ts / 1_000_000)
12220            .collect::<Vec<_>>();
12221
12222        let array = TimestampMillisecondArray::from(ts_array).with_timezone("+00:00".to_string());
12223        let casted_array = cast(&array, &DataType::Date32).unwrap();
12224        let date_array = casted_array.as_primitive::<Date32Type>();
12225        let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
12226        let string_array = casted_array.as_string::<i32>();
12227        assert_eq!("1900-01-03", string_array.value(0));
12228        assert_eq!("1969-12-31", string_array.value(1));
12229        assert_eq!("1989-12-31", string_array.value(2));
12230    }
12231
12232    #[test]
12233    fn test_nested_list() {
12234        let mut list = ListBuilder::new(Int32Builder::new());
12235        list.append_value([Some(1), Some(2), Some(3)]);
12236        list.append_value([Some(4), None, Some(6)]);
12237        let list = list.finish();
12238
12239        let to_field = Field::new("nested", list.data_type().clone(), false);
12240        let to = DataType::List(Arc::new(to_field));
12241        let out = cast(&list, &to).unwrap();
12242        let opts = FormatOptions::default().with_null("null");
12243        let formatted = ArrayFormatter::try_new(out.as_ref(), &opts).unwrap();
12244
12245        assert_eq!(formatted.value(0).to_string(), "[[1], [2], [3]]");
12246        assert_eq!(formatted.value(1).to_string(), "[[4], [null], [6]]");
12247    }
12248
12249    #[test]
12250    fn test_nested_list_cast() {
12251        let mut builder = ListBuilder::new(ListBuilder::new(Int32Builder::new()));
12252        builder.append_value([Some([Some(1), Some(2), None]), None]);
12253        builder.append_value([None, Some([]), None]);
12254        builder.append_null();
12255        builder.append_value([Some([Some(2), Some(3)])]);
12256        let start = builder.finish();
12257
12258        let mut builder = LargeListBuilder::new(LargeListBuilder::new(Int8Builder::new()));
12259        builder.append_value([Some([Some(1), Some(2), None]), None]);
12260        builder.append_value([None, Some([]), None]);
12261        builder.append_null();
12262        builder.append_value([Some([Some(2), Some(3)])]);
12263        let expected = builder.finish();
12264
12265        let actual = cast(&start, expected.data_type()).unwrap();
12266        assert_eq!(actual.as_ref(), &expected);
12267    }
12268
12269    const CAST_OPTIONS: CastOptions<'static> = CastOptions {
12270        safe: true,
12271        format_options: FormatOptions::new(),
12272    };
12273
12274    #[test]
12275    #[allow(clippy::assertions_on_constants)]
12276    fn test_const_options() {
12277        assert!(CAST_OPTIONS.safe)
12278    }
12279
12280    #[test]
12281    fn test_list_format_options() {
12282        let options = CastOptions {
12283            safe: false,
12284            format_options: FormatOptions::default().with_null("null"),
12285        };
12286        let array = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
12287            Some(vec![Some(0), Some(1), Some(2)]),
12288            Some(vec![Some(0), None, Some(2)]),
12289        ]);
12290        let a = cast_with_options(&array, &DataType::Utf8, &options).unwrap();
12291        let r: Vec<_> = a.as_string::<i32>().iter().flatten().collect();
12292        assert_eq!(r, &["[0, 1, 2]", "[0, null, 2]"]);
12293    }
12294    #[test]
12295    fn test_cast_string_to_timestamp_invalid_tz() {
12296        // content after Z should be ignored
12297        let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
12298        let array = StringArray::from(vec![Some(bad_timestamp)]);
12299
12300        let data_types = [
12301            DataType::Timestamp(TimeUnit::Second, None),
12302            DataType::Timestamp(TimeUnit::Millisecond, None),
12303            DataType::Timestamp(TimeUnit::Microsecond, None),
12304            DataType::Timestamp(TimeUnit::Nanosecond, None),
12305        ];
12306
12307        let cast_options = CastOptions {
12308            safe: false,
12309            ..Default::default()
12310        };
12311
12312        for dt in data_types {
12313            assert_eq!(
12314                cast_with_options(&array, &dt, &cast_options)
12315                    .unwrap_err()
12316                    .to_string(),
12317                "Parser error: Invalid timezone \"ZZTOP\": only offset based timezones supported without chrono-tz feature"
12318            );
12319        }
12320    }
12321    #[test]
12322    fn test_cast_struct_to_struct() {
12323        let struct_type = DataType::Struct(
12324            vec![
12325                Field::new("a", DataType::Boolean, false),
12326                Field::new("b", DataType::Int32, false),
12327            ]
12328            .into(),
12329        );
12330        let to_type = DataType::Struct(
12331            vec![
12332                Field::new("a", DataType::Utf8, false),
12333                Field::new("b", DataType::Utf8, false),
12334            ]
12335            .into(),
12336        );
12337        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12338        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
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, false)),
12346                int.clone() as ArrayRef,
12347            ),
12348        ]);
12349        let casted_array = cast(&struct_array, &to_type).unwrap();
12350        let casted_array = casted_array.as_struct();
12351        assert_eq!(casted_array.data_type(), &to_type);
12352        let casted_boolean_array = casted_array
12353            .column(0)
12354            .as_string::<i32>()
12355            .into_iter()
12356            .flatten()
12357            .collect::<Vec<_>>();
12358        let casted_int_array = casted_array
12359            .column(1)
12360            .as_string::<i32>()
12361            .into_iter()
12362            .flatten()
12363            .collect::<Vec<_>>();
12364        assert_eq!(casted_boolean_array, vec!["false", "false", "true", "true"]);
12365        assert_eq!(casted_int_array, vec!["42", "28", "19", "31"]);
12366
12367        // test for can't cast
12368        let to_type = DataType::Struct(
12369            vec![
12370                Field::new("a", DataType::Date32, false),
12371                Field::new("b", DataType::Utf8, false),
12372            ]
12373            .into(),
12374        );
12375        assert!(!can_cast_types(&struct_type, &to_type));
12376        let result = cast(&struct_array, &to_type);
12377        assert_eq!(
12378            "Cast error: Casting from Boolean to Date32 not supported",
12379            result.unwrap_err().to_string()
12380        );
12381    }
12382
12383    #[test]
12384    fn test_cast_struct_to_struct_nullability() {
12385        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12386        let int = Arc::new(Int32Array::from(vec![Some(42), None, Some(19), None]));
12387        let struct_array = StructArray::from(vec![
12388            (
12389                Arc::new(Field::new("b", DataType::Boolean, false)),
12390                boolean.clone() as ArrayRef,
12391            ),
12392            (
12393                Arc::new(Field::new("c", DataType::Int32, true)),
12394                int.clone() as ArrayRef,
12395            ),
12396        ]);
12397
12398        // okay: nullable to nullable
12399        let to_type = DataType::Struct(
12400            vec![
12401                Field::new("a", DataType::Utf8, false),
12402                Field::new("b", DataType::Utf8, true),
12403            ]
12404            .into(),
12405        );
12406        cast(&struct_array, &to_type).expect("Cast nullable to nullable struct field should work");
12407
12408        // error: nullable to non-nullable
12409        let to_type = DataType::Struct(
12410            vec![
12411                Field::new("a", DataType::Utf8, false),
12412                Field::new("b", DataType::Utf8, false),
12413            ]
12414            .into(),
12415        );
12416        cast(&struct_array, &to_type)
12417            .expect_err("Cast nullable to non-nullable struct field should fail");
12418
12419        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12420        let int = Arc::new(Int32Array::from(vec![i32::MAX, 25, 1, 100]));
12421        let struct_array = StructArray::from(vec![
12422            (
12423                Arc::new(Field::new("b", DataType::Boolean, false)),
12424                boolean.clone() as ArrayRef,
12425            ),
12426            (
12427                Arc::new(Field::new("c", DataType::Int32, false)),
12428                int.clone() as ArrayRef,
12429            ),
12430        ]);
12431
12432        // okay: non-nullable to non-nullable
12433        let to_type = DataType::Struct(
12434            vec![
12435                Field::new("a", DataType::Utf8, false),
12436                Field::new("b", DataType::Utf8, false),
12437            ]
12438            .into(),
12439        );
12440        cast(&struct_array, &to_type)
12441            .expect("Cast non-nullable to non-nullable struct field should work");
12442
12443        // err: non-nullable to non-nullable but overflowing return null during casting
12444        let to_type = DataType::Struct(
12445            vec![
12446                Field::new("a", DataType::Utf8, false),
12447                Field::new("b", DataType::Int8, false),
12448            ]
12449            .into(),
12450        );
12451        cast(&struct_array, &to_type).expect_err(
12452            "Cast non-nullable to non-nullable struct field returning null should fail",
12453        );
12454    }
12455
12456    #[test]
12457    fn test_cast_struct_to_non_struct() {
12458        let boolean = Arc::new(BooleanArray::from(vec![true, false]));
12459        let struct_array = StructArray::from(vec![(
12460            Arc::new(Field::new("a", DataType::Boolean, false)),
12461            boolean.clone() as ArrayRef,
12462        )]);
12463        let to_type = DataType::Utf8;
12464        let result = cast(&struct_array, &to_type);
12465        assert_eq!(
12466            r#"Cast error: Casting from Struct("a": non-null Boolean) to Utf8 not supported"#,
12467            result.unwrap_err().to_string()
12468        );
12469    }
12470
12471    #[test]
12472    fn test_cast_non_struct_to_struct() {
12473        let array = StringArray::from(vec!["a", "b"]);
12474        let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
12475        let result = cast(&array, &to_type);
12476        assert_eq!(
12477            r#"Cast error: Casting from Utf8 to Struct("a": non-null Boolean) not supported"#,
12478            result.unwrap_err().to_string()
12479        );
12480    }
12481
12482    #[test]
12483    fn test_cast_struct_with_different_field_order() {
12484        // Test slow path: fields are in different order
12485        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12486        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
12487        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
12488
12489        let struct_array = StructArray::from(vec![
12490            (
12491                Arc::new(Field::new("a", DataType::Boolean, false)),
12492                boolean.clone() as ArrayRef,
12493            ),
12494            (
12495                Arc::new(Field::new("b", DataType::Int32, false)),
12496                int.clone() as ArrayRef,
12497            ),
12498            (
12499                Arc::new(Field::new("c", DataType::Utf8, false)),
12500                string.clone() as ArrayRef,
12501            ),
12502        ]);
12503
12504        // Target has fields in different order: c, a, b instead of a, b, c
12505        let to_type = DataType::Struct(
12506            vec![
12507                Field::new("c", DataType::Utf8, false),
12508                Field::new("a", DataType::Utf8, false), // Boolean to Utf8
12509                Field::new("b", DataType::Utf8, false), // Int32 to Utf8
12510            ]
12511            .into(),
12512        );
12513
12514        let result = cast(&struct_array, &to_type).unwrap();
12515        let result_struct = result.as_struct();
12516
12517        assert_eq!(result_struct.data_type(), &to_type);
12518        assert_eq!(result_struct.num_columns(), 3);
12519
12520        // Verify field "c" (originally position 2, now position 0) remains Utf8
12521        let c_column = result_struct.column(0).as_string::<i32>();
12522        assert_eq!(
12523            c_column.into_iter().flatten().collect::<Vec<_>>(),
12524            vec!["foo", "bar", "baz", "qux"]
12525        );
12526
12527        // Verify field "a" (originally position 0, now position 1) was cast from Boolean to Utf8
12528        let a_column = result_struct.column(1).as_string::<i32>();
12529        assert_eq!(
12530            a_column.into_iter().flatten().collect::<Vec<_>>(),
12531            vec!["false", "false", "true", "true"]
12532        );
12533
12534        // Verify field "b" (originally position 1, now position 2) was cast from Int32 to Utf8
12535        let b_column = result_struct.column(2).as_string::<i32>();
12536        assert_eq!(
12537            b_column.into_iter().flatten().collect::<Vec<_>>(),
12538            vec!["42", "28", "19", "31"]
12539        );
12540    }
12541
12542    #[test]
12543    fn test_cast_struct_with_missing_field() {
12544        // Test that casting fails when target has a field not present in source
12545        let boolean = Arc::new(BooleanArray::from(vec![false, true]));
12546        let struct_array = StructArray::from(vec![(
12547            Arc::new(Field::new("a", DataType::Boolean, false)),
12548            boolean.clone() as ArrayRef,
12549        )]);
12550
12551        let to_type = DataType::Struct(
12552            vec![
12553                Field::new("a", DataType::Utf8, false),
12554                Field::new("b", DataType::Int32, false), // Field "b" doesn't exist in source
12555            ]
12556            .into(),
12557        );
12558
12559        let result = cast(&struct_array, &to_type);
12560        assert!(result.is_err());
12561        assert_eq!(
12562            result.unwrap_err().to_string(),
12563            "Invalid argument error: Incorrect number of arrays for StructArray fields, expected 2 got 1"
12564        );
12565    }
12566
12567    #[test]
12568    fn test_cast_struct_with_subset_of_fields() {
12569        // Test casting to a struct with fewer fields (selecting a subset)
12570        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
12571        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
12572        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
12573
12574        let struct_array = StructArray::from(vec![
12575            (
12576                Arc::new(Field::new("a", DataType::Boolean, false)),
12577                boolean.clone() as ArrayRef,
12578            ),
12579            (
12580                Arc::new(Field::new("b", DataType::Int32, false)),
12581                int.clone() as ArrayRef,
12582            ),
12583            (
12584                Arc::new(Field::new("c", DataType::Utf8, false)),
12585                string.clone() as ArrayRef,
12586            ),
12587        ]);
12588
12589        // Target has only fields "c" and "a", omitting "b"
12590        let to_type = DataType::Struct(
12591            vec![
12592                Field::new("c", DataType::Utf8, false),
12593                Field::new("a", DataType::Utf8, false),
12594            ]
12595            .into(),
12596        );
12597
12598        let result = cast(&struct_array, &to_type).unwrap();
12599        let result_struct = result.as_struct();
12600
12601        assert_eq!(result_struct.data_type(), &to_type);
12602        assert_eq!(result_struct.num_columns(), 2);
12603
12604        // Verify field "c" remains Utf8
12605        let c_column = result_struct.column(0).as_string::<i32>();
12606        assert_eq!(
12607            c_column.into_iter().flatten().collect::<Vec<_>>(),
12608            vec!["foo", "bar", "baz", "qux"]
12609        );
12610
12611        // Verify field "a" was cast from Boolean to Utf8
12612        let a_column = result_struct.column(1).as_string::<i32>();
12613        assert_eq!(
12614            a_column.into_iter().flatten().collect::<Vec<_>>(),
12615            vec!["false", "false", "true", "true"]
12616        );
12617    }
12618
12619    #[test]
12620    fn test_can_cast_struct_rename_field() {
12621        // Test that can_cast_types returns false when target has a field not in source
12622        let from_type = DataType::Struct(
12623            vec![
12624                Field::new("a", DataType::Int32, false),
12625                Field::new("b", DataType::Utf8, false),
12626            ]
12627            .into(),
12628        );
12629
12630        let to_type = DataType::Struct(
12631            vec![
12632                Field::new("a", DataType::Int64, false),
12633                Field::new("c", DataType::Boolean, false), // Field "c" not in source
12634            ]
12635            .into(),
12636        );
12637
12638        assert!(can_cast_types(&from_type, &to_type));
12639    }
12640
12641    fn run_decimal_cast_test_case_between_multiple_types(t: DecimalCastTestConfig) {
12642        run_decimal_cast_test_case::<Decimal128Type, Decimal128Type>(t.clone());
12643        run_decimal_cast_test_case::<Decimal128Type, Decimal256Type>(t.clone());
12644        run_decimal_cast_test_case::<Decimal256Type, Decimal128Type>(t.clone());
12645        run_decimal_cast_test_case::<Decimal256Type, Decimal256Type>(t.clone());
12646    }
12647
12648    #[test]
12649    fn test_decimal_to_decimal_coverage() {
12650        let test_cases = [
12651            // increase precision, increase scale, infallible
12652            DecimalCastTestConfig {
12653                input_prec: 5,
12654                input_scale: 1,
12655                input_repr: 99999, // 9999.9
12656                output_prec: 10,
12657                output_scale: 6,
12658                expected_output_repr: Ok(9999900000), // 9999.900000
12659            },
12660            // increase precision, increase scale, fallible, safe
12661            DecimalCastTestConfig {
12662                input_prec: 5,
12663                input_scale: 1,
12664                input_repr: 99, // 9999.9
12665                output_prec: 7,
12666                output_scale: 6,
12667                expected_output_repr: Ok(9900000), // 9.900000
12668            },
12669            // increase precision, increase scale, fallible, unsafe
12670            DecimalCastTestConfig {
12671                input_prec: 5,
12672                input_scale: 1,
12673                input_repr: 99999, // 9999.9
12674                output_prec: 7,
12675                output_scale: 6,
12676                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
12677            },
12678            // increase precision, decrease scale, always infallible
12679            DecimalCastTestConfig {
12680                input_prec: 5,
12681                input_scale: 3,
12682                input_repr: 99999, // 99.999
12683                output_prec: 10,
12684                output_scale: 2,
12685                expected_output_repr: Ok(10000), // 100.00
12686            },
12687            // increase precision, decrease scale, no rouding
12688            DecimalCastTestConfig {
12689                input_prec: 5,
12690                input_scale: 3,
12691                input_repr: 99994, // 99.994
12692                output_prec: 10,
12693                output_scale: 2,
12694                expected_output_repr: Ok(9999), // 99.99
12695            },
12696            // increase precision, don't change scale, always infallible
12697            DecimalCastTestConfig {
12698                input_prec: 5,
12699                input_scale: 3,
12700                input_repr: 99999, // 99.999
12701                output_prec: 10,
12702                output_scale: 3,
12703                expected_output_repr: Ok(99999), // 99.999
12704            },
12705            // decrease precision, increase scale, safe
12706            DecimalCastTestConfig {
12707                input_prec: 10,
12708                input_scale: 5,
12709                input_repr: 999999, // 9.99999
12710                output_prec: 8,
12711                output_scale: 7,
12712                expected_output_repr: Ok(99999900), // 9.9999900
12713            },
12714            // decrease precision, increase scale, unsafe
12715            DecimalCastTestConfig {
12716                input_prec: 10,
12717                input_scale: 5,
12718                input_repr: 9999999, // 99.99999
12719                output_prec: 8,
12720                output_scale: 7,
12721                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
12722            },
12723            // decrease precision, decrease scale, safe, infallible
12724            DecimalCastTestConfig {
12725                input_prec: 7,
12726                input_scale: 4,
12727                input_repr: 9999999, // 999.9999
12728                output_prec: 6,
12729                output_scale: 2,
12730                expected_output_repr: Ok(100000),
12731            },
12732            // decrease precision, decrease scale, safe, fallible
12733            DecimalCastTestConfig {
12734                input_prec: 10,
12735                input_scale: 5,
12736                input_repr: 12345678, // 123.45678
12737                output_prec: 8,
12738                output_scale: 3,
12739                expected_output_repr: Ok(123457), // 123.457
12740            },
12741            // decrease precision, decrease scale, unsafe
12742            DecimalCastTestConfig {
12743                input_prec: 10,
12744                input_scale: 5,
12745                input_repr: 9999999, // 99.99999
12746                output_prec: 4,
12747                output_scale: 3,
12748                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
12749            },
12750            // decrease precision, same scale, safe
12751            DecimalCastTestConfig {
12752                input_prec: 10,
12753                input_scale: 5,
12754                input_repr: 999999, // 9.99999
12755                output_prec: 6,
12756                output_scale: 5,
12757                expected_output_repr: Ok(999999), // 9.99999
12758            },
12759            // decrease precision, same scale, unsafe
12760            DecimalCastTestConfig {
12761                input_prec: 10,
12762                input_scale: 5,
12763                input_repr: 9999999, // 99.99999
12764                output_prec: 6,
12765                output_scale: 5,
12766                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
12767            },
12768            // same precision, increase scale, safe
12769            DecimalCastTestConfig {
12770                input_prec: 7,
12771                input_scale: 4,
12772                input_repr: 12345, // 1.2345
12773                output_prec: 7,
12774                output_scale: 6,
12775                expected_output_repr: Ok(1234500), // 1.234500
12776            },
12777            // same precision, increase scale, unsafe
12778            DecimalCastTestConfig {
12779                input_prec: 7,
12780                input_scale: 4,
12781                input_repr: 123456, // 12.3456
12782                output_prec: 7,
12783                output_scale: 6,
12784                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
12785            },
12786            // same precision, decrease scale, infallible
12787            DecimalCastTestConfig {
12788                input_prec: 7,
12789                input_scale: 5,
12790                input_repr: 1234567, // 12.34567
12791                output_prec: 7,
12792                output_scale: 4,
12793                expected_output_repr: Ok(123457), // 12.3457
12794            },
12795            // same precision, same scale, infallible
12796            DecimalCastTestConfig {
12797                input_prec: 7,
12798                input_scale: 5,
12799                input_repr: 9999999, // 99.99999
12800                output_prec: 7,
12801                output_scale: 5,
12802                expected_output_repr: Ok(9999999), // 99.99999
12803            },
12804            // precision increase, input scale & output scale = 0, infallible
12805            DecimalCastTestConfig {
12806                input_prec: 7,
12807                input_scale: 0,
12808                input_repr: 1234567, // 1234567
12809                output_prec: 8,
12810                output_scale: 0,
12811                expected_output_repr: Ok(1234567), // 1234567
12812            },
12813            // precision decrease, input scale & output scale = 0, failure
12814            DecimalCastTestConfig {
12815                input_prec: 7,
12816                input_scale: 0,
12817                input_repr: 1234567, // 1234567
12818                output_prec: 6,
12819                output_scale: 0,
12820                expected_output_repr: Err("Invalid argument error: 1234567 is too large to store in a {} of precision 6. Max is 999999".to_string())
12821            },
12822            // precision decrease, input scale & output scale = 0, success
12823            DecimalCastTestConfig {
12824                input_prec: 7,
12825                input_scale: 0,
12826                input_repr: 123456, // 123456
12827                output_prec: 6,
12828                output_scale: 0,
12829                expected_output_repr: Ok(123456), // 123456
12830            },
12831        ];
12832
12833        for t in test_cases {
12834            run_decimal_cast_test_case_between_multiple_types(t);
12835        }
12836    }
12837
12838    #[test]
12839    fn test_decimal_to_decimal_increase_scale_and_precision_unchecked() {
12840        let test_cases = [
12841            DecimalCastTestConfig {
12842                input_prec: 5,
12843                input_scale: 0,
12844                input_repr: 99999,
12845                output_prec: 10,
12846                output_scale: 5,
12847                expected_output_repr: Ok(9999900000),
12848            },
12849            DecimalCastTestConfig {
12850                input_prec: 5,
12851                input_scale: 0,
12852                input_repr: -99999,
12853                output_prec: 10,
12854                output_scale: 5,
12855                expected_output_repr: Ok(-9999900000),
12856            },
12857            DecimalCastTestConfig {
12858                input_prec: 5,
12859                input_scale: 2,
12860                input_repr: 99999,
12861                output_prec: 10,
12862                output_scale: 5,
12863                expected_output_repr: Ok(99999000),
12864            },
12865            DecimalCastTestConfig {
12866                input_prec: 5,
12867                input_scale: -2,
12868                input_repr: -99999,
12869                output_prec: 10,
12870                output_scale: 3,
12871                expected_output_repr: Ok(-9999900000),
12872            },
12873            DecimalCastTestConfig {
12874                input_prec: 5,
12875                input_scale: 3,
12876                input_repr: -12345,
12877                output_prec: 6,
12878                output_scale: 5,
12879                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())
12880            },
12881        ];
12882
12883        for t in test_cases {
12884            run_decimal_cast_test_case_between_multiple_types(t);
12885        }
12886    }
12887
12888    #[test]
12889    fn test_decimal_to_decimal_decrease_scale_and_precision_unchecked() {
12890        let test_cases = [
12891            DecimalCastTestConfig {
12892                input_prec: 5,
12893                input_scale: 0,
12894                input_repr: 99999,
12895                output_scale: -3,
12896                output_prec: 3,
12897                expected_output_repr: Ok(100),
12898            },
12899            DecimalCastTestConfig {
12900                input_prec: 5,
12901                input_scale: 0,
12902                input_repr: -99999,
12903                output_prec: 1,
12904                output_scale: -5,
12905                expected_output_repr: Ok(-1),
12906            },
12907            DecimalCastTestConfig {
12908                input_prec: 10,
12909                input_scale: 2,
12910                input_repr: 123456789,
12911                output_prec: 5,
12912                output_scale: -2,
12913                expected_output_repr: Ok(12346),
12914            },
12915            DecimalCastTestConfig {
12916                input_prec: 10,
12917                input_scale: 4,
12918                input_repr: -9876543210,
12919                output_prec: 7,
12920                output_scale: 0,
12921                expected_output_repr: Ok(-987654),
12922            },
12923            DecimalCastTestConfig {
12924                input_prec: 7,
12925                input_scale: 4,
12926                input_repr: 9999999,
12927                output_prec: 6,
12928                output_scale: 3,
12929                expected_output_repr:
12930                    Err("Invalid argument error: 1000.000 is too large to store in a {} of precision 6. Max is 999.999".to_string()),
12931            },
12932        ];
12933        for t in test_cases {
12934            run_decimal_cast_test_case_between_multiple_types(t);
12935        }
12936    }
12937
12938    #[test]
12939    fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
12940        let array = vec![Some(123456789)];
12941        let array = create_decimal128_array(array, 24, 2).unwrap();
12942        let input_type = DataType::Decimal128(24, 2);
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: 1234567.89 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_same_scale() {
12959        let array = vec![Some(520)];
12960        let array = create_decimal128_array(array, 4, 2).unwrap();
12961        let input_type = DataType::Decimal128(4, 2);
12962        let output_type = DataType::Decimal128(3, 2);
12963        assert!(can_cast_types(&input_type, &output_type));
12964
12965        let options = CastOptions {
12966            safe: false,
12967            ..Default::default()
12968        };
12969        let result = cast_with_options(&array, &output_type, &options);
12970        assert_eq!(
12971            result.unwrap().as_primitive::<Decimal128Type>().value(0),
12972            520
12973        );
12974
12975        // Cast 0 of decimal(3, 0) type to decimal(2, 0)
12976        assert_eq!(
12977            &cast(
12978                &create_decimal128_array(vec![Some(0)], 3, 0).unwrap(),
12979                &DataType::Decimal128(2, 0)
12980            )
12981            .unwrap(),
12982            &(Arc::new(create_decimal128_array(vec![Some(0)], 2, 0).unwrap()) as ArrayRef)
12983        );
12984    }
12985
12986    #[test]
12987    fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale() {
12988        let array = vec![Some(123456789)];
12989        let array = create_decimal128_array(array, 24, 4).unwrap();
12990        let input_type = DataType::Decimal128(24, 4);
12991        let output_type = DataType::Decimal128(6, 2);
12992        assert!(can_cast_types(&input_type, &output_type));
12993
12994        let options = CastOptions {
12995            safe: false,
12996            ..Default::default()
12997        };
12998        let result = cast_with_options(&array, &output_type, &options);
12999        assert_eq!(
13000            result.unwrap_err().to_string(),
13001            "Invalid argument error: 12345.68 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
13002        );
13003    }
13004
13005    #[test]
13006    fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale() {
13007        let array = vec![Some(123456789)];
13008        let array = create_decimal128_array(array, 24, 2).unwrap();
13009        let input_type = DataType::Decimal128(24, 2);
13010        let output_type = DataType::Decimal128(6, 3);
13011        assert!(can_cast_types(&input_type, &output_type));
13012
13013        let options = CastOptions {
13014            safe: false,
13015            ..Default::default()
13016        };
13017        let result = cast_with_options(&array, &output_type, &options);
13018        assert_eq!(
13019            result.unwrap_err().to_string(),
13020            "Invalid argument error: 1234567.890 is too large to store in a Decimal128 of precision 6. Max is 999.999"
13021        );
13022    }
13023
13024    #[test]
13025    fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type() {
13026        let array = vec![Some(123456789)];
13027        let array = create_decimal128_array(array, 24, 2).unwrap();
13028        let input_type = DataType::Decimal128(24, 2);
13029        let output_type = DataType::Decimal256(6, 2);
13030        assert!(can_cast_types(&input_type, &output_type));
13031
13032        let options = CastOptions {
13033            safe: false,
13034            ..Default::default()
13035        };
13036        let result = cast_with_options(&array, &output_type, &options).unwrap_err();
13037        assert_eq!(
13038            result.to_string(),
13039            "Invalid argument error: 1234567.89 is too large to store in a Decimal256 of precision 6. Max is 9999.99"
13040        );
13041    }
13042
13043    #[test]
13044    fn test_first_none() {
13045        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
13046            None,
13047            Some(vec![Some(1), Some(2)]),
13048        ])) as ArrayRef;
13049        let data_type =
13050            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
13051        let opt = CastOptions::default();
13052        let r = cast_with_options(&array, &data_type, &opt).unwrap();
13053
13054        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
13055            vec![None, Some(vec![Some(1), Some(2)])],
13056            2,
13057        )) as ArrayRef;
13058        assert_eq!(*fixed_array, *r);
13059    }
13060
13061    #[test]
13062    fn test_first_last_none() {
13063        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
13064            None,
13065            Some(vec![Some(1), Some(2)]),
13066            None,
13067        ])) as ArrayRef;
13068        let data_type =
13069            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
13070        let opt = CastOptions::default();
13071        let r = cast_with_options(&array, &data_type, &opt).unwrap();
13072
13073        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
13074            vec![None, Some(vec![Some(1), Some(2)]), None],
13075            2,
13076        )) as ArrayRef;
13077        assert_eq!(*fixed_array, *r);
13078    }
13079
13080    #[test]
13081    fn test_cast_decimal_error_output() {
13082        let array = Int64Array::from(vec![1]);
13083        let error = cast_with_options(
13084            &array,
13085            &DataType::Decimal32(1, 1),
13086            &CastOptions {
13087                safe: false,
13088                format_options: FormatOptions::default(),
13089            },
13090        )
13091        .unwrap_err();
13092        assert_eq!(
13093            error.to_string(),
13094            "Invalid argument error: 1.0 is too large to store in a Decimal32 of precision 1. Max is 0.9"
13095        );
13096
13097        let array = Int64Array::from(vec![-1]);
13098        let error = cast_with_options(
13099            &array,
13100            &DataType::Decimal32(1, 1),
13101            &CastOptions {
13102                safe: false,
13103                format_options: FormatOptions::default(),
13104            },
13105        )
13106        .unwrap_err();
13107        assert_eq!(
13108            error.to_string(),
13109            "Invalid argument error: -1.0 is too small to store in a Decimal32 of precision 1. Min is -0.9"
13110        );
13111    }
13112
13113    #[test]
13114    fn test_run_end_encoded_to_primitive() {
13115        // Create a RunEndEncoded array: [1, 1, 2, 2, 2, 3]
13116        let run_ends = Int32Array::from(vec![2, 5, 6]);
13117        let values = Int32Array::from(vec![1, 2, 3]);
13118        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13119        let array_ref = Arc::new(run_array) as ArrayRef;
13120        // Cast to Int64
13121        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
13122        // Verify the result is a RunArray with Int64 values
13123        let result_run_array = cast_result.as_any().downcast_ref::<Int64Array>().unwrap();
13124        assert_eq!(
13125            result_run_array.values(),
13126            &[1i64, 1i64, 2i64, 2i64, 2i64, 3i64]
13127        );
13128    }
13129
13130    #[test]
13131    fn test_sliced_run_end_encoded_to_primitive() {
13132        let run_ends = Int32Array::from(vec![2, 5, 6]);
13133        let values = Int32Array::from(vec![1, 2, 3]);
13134        // [1, 1, 2, 2, 2, 3]
13135        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13136        let run_array = run_array.slice(3, 3); // [2, 2, 3]
13137        let array_ref = Arc::new(run_array) as ArrayRef;
13138
13139        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
13140        let result_run_array = cast_result.as_primitive::<Int64Type>();
13141        assert_eq!(result_run_array.values(), &[2, 2, 3]);
13142    }
13143
13144    #[test]
13145    fn test_run_end_encoded_to_string() {
13146        let run_ends = Int32Array::from(vec![2, 3, 5]);
13147        let values = Int32Array::from(vec![10, 20, 30]);
13148        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13149        let array_ref = Arc::new(run_array) as ArrayRef;
13150
13151        // Cast to String
13152        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
13153
13154        // Verify the result is a RunArray with String values
13155        let result_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
13156        // Check that values are correct
13157        assert_eq!(result_array.value(0), "10");
13158        assert_eq!(result_array.value(1), "10");
13159        assert_eq!(result_array.value(2), "20");
13160    }
13161
13162    #[test]
13163    fn test_primitive_to_run_end_encoded() {
13164        // Create an Int32 array with repeated values: [1, 1, 2, 2, 2, 3]
13165        let source_array = Int32Array::from(vec![1, 1, 2, 2, 2, 3]);
13166        let array_ref = Arc::new(source_array) as ArrayRef;
13167
13168        // Cast to RunEndEncoded<Int32, Int32>
13169        let target_type = DataType::RunEndEncoded(
13170            Arc::new(Field::new("run_ends", DataType::Int32, false)),
13171            Arc::new(Field::new("values", DataType::Int32, true)),
13172        );
13173        let cast_result = cast(&array_ref, &target_type).unwrap();
13174
13175        // Verify the result is a RunArray
13176        let result_run_array = cast_result
13177            .as_any()
13178            .downcast_ref::<RunArray<Int32Type>>()
13179            .unwrap();
13180
13181        // Check run structure: runs should end at positions [2, 5, 6]
13182        assert_eq!(result_run_array.run_ends().values(), &[2, 5, 6]);
13183
13184        // Check values: should be [1, 2, 3]
13185        let values_array = result_run_array.values().as_primitive::<Int32Type>();
13186        assert_eq!(values_array.values(), &[1, 2, 3]);
13187    }
13188
13189    #[test]
13190    fn test_primitive_to_run_end_encoded_with_nulls() {
13191        let source_array = Int32Array::from(vec![
13192            Some(1),
13193            Some(1),
13194            None,
13195            None,
13196            Some(2),
13197            Some(2),
13198            Some(3),
13199            Some(3),
13200            None,
13201            None,
13202            Some(4),
13203            Some(4),
13204            Some(5),
13205            Some(5),
13206            None,
13207            None,
13208        ]);
13209        let array_ref = Arc::new(source_array) as ArrayRef;
13210        let target_type = DataType::RunEndEncoded(
13211            Arc::new(Field::new("run_ends", DataType::Int32, false)),
13212            Arc::new(Field::new("values", DataType::Int32, true)),
13213        );
13214        let cast_result = cast(&array_ref, &target_type).unwrap();
13215        let result_run_array = cast_result
13216            .as_any()
13217            .downcast_ref::<RunArray<Int32Type>>()
13218            .unwrap();
13219        assert_eq!(
13220            result_run_array.run_ends().values(),
13221            &[2, 4, 6, 8, 10, 12, 14, 16]
13222        );
13223        assert_eq!(
13224            result_run_array
13225                .values()
13226                .as_primitive::<Int32Type>()
13227                .values(),
13228            &[1, 0, 2, 3, 0, 4, 5, 0]
13229        );
13230        assert_eq!(result_run_array.values().null_count(), 3);
13231    }
13232
13233    #[test]
13234    fn test_primitive_to_run_end_encoded_with_nulls_consecutive() {
13235        let source_array = Int64Array::from(vec![
13236            Some(1),
13237            Some(1),
13238            None,
13239            None,
13240            None,
13241            None,
13242            None,
13243            None,
13244            None,
13245            None,
13246            Some(4),
13247            Some(20),
13248            Some(500),
13249            Some(500),
13250            None,
13251            None,
13252        ]);
13253        let array_ref = Arc::new(source_array) as ArrayRef;
13254        let target_type = DataType::RunEndEncoded(
13255            Arc::new(Field::new("run_ends", DataType::Int16, false)),
13256            Arc::new(Field::new("values", DataType::Int64, true)),
13257        );
13258        let cast_result = cast(&array_ref, &target_type).unwrap();
13259        let result_run_array = cast_result
13260            .as_any()
13261            .downcast_ref::<RunArray<Int16Type>>()
13262            .unwrap();
13263        assert_eq!(
13264            result_run_array.run_ends().values(),
13265            &[2, 10, 11, 12, 14, 16]
13266        );
13267        assert_eq!(
13268            result_run_array
13269                .values()
13270                .as_primitive::<Int64Type>()
13271                .values(),
13272            &[1, 0, 4, 20, 500, 0]
13273        );
13274        assert_eq!(result_run_array.values().null_count(), 2);
13275    }
13276
13277    #[test]
13278    fn test_string_to_run_end_encoded() {
13279        // Create a String array with repeated values: ["a", "a", "b", "c", "c"]
13280        let source_array = StringArray::from(vec!["a", "a", "b", "c", "c"]);
13281        let array_ref = Arc::new(source_array) as ArrayRef;
13282
13283        // Cast to RunEndEncoded<Int32, String>
13284        let target_type = DataType::RunEndEncoded(
13285            Arc::new(Field::new("run_ends", DataType::Int32, false)),
13286            Arc::new(Field::new("values", DataType::Utf8, true)),
13287        );
13288        let cast_result = cast(&array_ref, &target_type).unwrap();
13289
13290        // Verify the result is a RunArray
13291        let result_run_array = cast_result
13292            .as_any()
13293            .downcast_ref::<RunArray<Int32Type>>()
13294            .unwrap();
13295
13296        // Check run structure: runs should end at positions [2, 3, 5]
13297        assert_eq!(result_run_array.run_ends().values(), &[2, 3, 5]);
13298
13299        // Check values: should be ["a", "b", "c"]
13300        let values_array = result_run_array.values().as_string::<i32>();
13301        assert_eq!(values_array.value(0), "a");
13302        assert_eq!(values_array.value(1), "b");
13303        assert_eq!(values_array.value(2), "c");
13304    }
13305
13306    #[test]
13307    fn test_empty_array_to_run_end_encoded() {
13308        // Create an empty Int32 array
13309        let source_array = Int32Array::from(Vec::<i32>::new());
13310        let array_ref = Arc::new(source_array) as ArrayRef;
13311
13312        // Cast to RunEndEncoded<Int32, Int32>
13313        let target_type = DataType::RunEndEncoded(
13314            Arc::new(Field::new("run_ends", DataType::Int32, false)),
13315            Arc::new(Field::new("values", DataType::Int32, true)),
13316        );
13317        let cast_result = cast(&array_ref, &target_type).unwrap();
13318
13319        // Verify the result is an empty RunArray
13320        let result_run_array = cast_result
13321            .as_any()
13322            .downcast_ref::<RunArray<Int32Type>>()
13323            .unwrap();
13324
13325        // Check that both run_ends and values are empty
13326        assert_eq!(result_run_array.run_ends().len(), 0);
13327        assert_eq!(result_run_array.values().len(), 0);
13328    }
13329
13330    #[test]
13331    fn test_run_end_encoded_with_nulls() {
13332        // Create a RunEndEncoded array with nulls: [1, 1, null, 2, 2]
13333        let run_ends = Int32Array::from(vec![2, 3, 5]);
13334        let values = Int32Array::from(vec![Some(1), None, Some(2)]);
13335        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
13336        let array_ref = Arc::new(run_array) as ArrayRef;
13337
13338        // Cast to String
13339        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
13340
13341        // Verify the result preserves nulls
13342        let result_run_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
13343        assert_eq!(result_run_array.value(0), "1");
13344        assert!(result_run_array.is_null(2));
13345        assert_eq!(result_run_array.value(4), "2");
13346    }
13347
13348    #[test]
13349    fn test_different_index_types() {
13350        // Test with Int16 index type
13351        let source_array = Int32Array::from(vec![1, 1, 2, 3, 3]);
13352        let array_ref = Arc::new(source_array) as ArrayRef;
13353
13354        let target_type = DataType::RunEndEncoded(
13355            Arc::new(Field::new("run_ends", DataType::Int16, false)),
13356            Arc::new(Field::new("values", DataType::Int32, true)),
13357        );
13358        let cast_result = cast(&array_ref, &target_type).unwrap();
13359        assert_eq!(cast_result.data_type(), &target_type);
13360
13361        // Verify the cast worked correctly: values are [1, 2, 3]
13362        // and run-ends are [2, 3, 5]
13363        let run_array = cast_result
13364            .as_any()
13365            .downcast_ref::<RunArray<Int16Type>>()
13366            .unwrap();
13367        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
13368        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
13369        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
13370        assert_eq!(run_array.run_ends().values(), &[2i16, 3i16, 5i16]);
13371
13372        // Test again with Int64 index type
13373        let target_type = DataType::RunEndEncoded(
13374            Arc::new(Field::new("run_ends", DataType::Int64, false)),
13375            Arc::new(Field::new("values", DataType::Int32, true)),
13376        );
13377        let cast_result = cast(&array_ref, &target_type).unwrap();
13378        assert_eq!(cast_result.data_type(), &target_type);
13379
13380        // Verify the cast worked correctly: values are [1, 2, 3]
13381        // and run-ends are [2, 3, 5]
13382        let run_array = cast_result
13383            .as_any()
13384            .downcast_ref::<RunArray<Int64Type>>()
13385            .unwrap();
13386        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
13387        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
13388        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
13389        assert_eq!(run_array.run_ends().values(), &[2i64, 3i64, 5i64]);
13390    }
13391
13392    #[test]
13393    fn test_unsupported_cast_to_run_end_encoded() {
13394        // Create a Struct array - complex nested type that might not be supported
13395        let field = Field::new("item", DataType::Int32, false);
13396        let struct_array = StructArray::from(vec![(
13397            Arc::new(field),
13398            Arc::new(Int32Array::from(vec![1, 2, 3])) as ArrayRef,
13399        )]);
13400        let array_ref = Arc::new(struct_array) as ArrayRef;
13401
13402        // This should fail because:
13403        // 1. The target type is not RunEndEncoded
13404        // 2. The target type is not supported for casting from StructArray
13405        let cast_result = cast(&array_ref, &DataType::FixedSizeBinary(10));
13406
13407        // Expect this to fail
13408        assert!(cast_result.is_err());
13409    }
13410
13411    /// Test casting RunEndEncoded<Int64, String> to RunEndEncoded<Int16, String> should fail
13412    #[test]
13413    fn test_cast_run_end_encoded_int64_to_int16_should_fail() {
13414        // Construct a valid REE array with Int64 run-ends
13415        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
13416        let values = StringArray::from(vec!["a", "b", "c"]);
13417
13418        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
13419        let array_ref = Arc::new(ree_array) as ArrayRef;
13420
13421        // Attempt to cast to RunEndEncoded<Int16, Utf8>
13422        let target_type = DataType::RunEndEncoded(
13423            Arc::new(Field::new("run_ends", DataType::Int16, false)),
13424            Arc::new(Field::new("values", DataType::Utf8, true)),
13425        );
13426        let cast_options = CastOptions {
13427            safe: false, // This should make it fail instead of returning nulls
13428            format_options: FormatOptions::default(),
13429        };
13430
13431        // This should fail due to run-end overflow
13432        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
13433            cast_with_options(&array_ref, &target_type, &cast_options);
13434
13435        let e = result.expect_err("Cast should have failed but succeeded");
13436        assert!(
13437            e.to_string()
13438                .contains("Cast error: Can't cast value 100000 to type Int16")
13439        );
13440    }
13441
13442    #[test]
13443    fn test_cast_run_end_encoded_int64_to_int16_with_safe_should_fail_with_null_invalid_error() {
13444        // Construct a valid REE array with Int64 run-ends
13445        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
13446        let values = StringArray::from(vec!["a", "b", "c"]);
13447
13448        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
13449        let array_ref = Arc::new(ree_array) as ArrayRef;
13450
13451        // Attempt to cast to RunEndEncoded<Int16, Utf8>
13452        let target_type = DataType::RunEndEncoded(
13453            Arc::new(Field::new("run_ends", DataType::Int16, false)),
13454            Arc::new(Field::new("values", DataType::Utf8, true)),
13455        );
13456        let cast_options = CastOptions {
13457            safe: true,
13458            format_options: FormatOptions::default(),
13459        };
13460
13461        // This fails even though safe is true because the run_ends array has null values
13462        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
13463            cast_with_options(&array_ref, &target_type, &cast_options);
13464        let e = result.expect_err("Cast should have failed but succeeded");
13465        assert!(
13466            e.to_string()
13467                .contains("Invalid argument error: Found null values in run_ends array. The run_ends array should not have null values.")
13468        );
13469    }
13470
13471    /// Test casting RunEndEncoded<Int16, String> to RunEndEncoded<Int64, String> should succeed
13472    #[test]
13473    fn test_cast_run_end_encoded_int16_to_int64_should_succeed() {
13474        // Construct a valid REE array with Int16 run-ends
13475        let run_ends = Int16Array::from(vec![2, 5, 8]); // values that fit in Int16
13476        let values = StringArray::from(vec!["a", "b", "c"]);
13477
13478        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
13479        let array_ref = Arc::new(ree_array) as ArrayRef;
13480
13481        // Attempt to cast to RunEndEncoded<Int64, Utf8> (upcast should succeed)
13482        let target_type = DataType::RunEndEncoded(
13483            Arc::new(Field::new("run_ends", DataType::Int64, false)),
13484            Arc::new(Field::new("values", DataType::Utf8, true)),
13485        );
13486        let cast_options = CastOptions {
13487            safe: false,
13488            format_options: FormatOptions::default(),
13489        };
13490
13491        // This should succeed due to valid upcast
13492        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
13493            cast_with_options(&array_ref, &target_type, &cast_options);
13494
13495        let array_ref = result.expect("Cast should have succeeded but failed");
13496        // Downcast to RunArray<Int64Type>
13497        let run_array = array_ref
13498            .as_any()
13499            .downcast_ref::<RunArray<Int64Type>>()
13500            .unwrap();
13501
13502        // Verify the cast worked correctly
13503        // Assert the values were cast correctly
13504        assert_eq!(run_array.run_ends().values(), &[2i64, 5i64, 8i64]);
13505        assert_eq!(run_array.values().as_string::<i32>().value(0), "a");
13506        assert_eq!(run_array.values().as_string::<i32>().value(1), "b");
13507        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
13508    }
13509
13510    #[test]
13511    fn test_cast_run_end_encoded_dictionary_to_run_end_encoded() {
13512        // Construct a valid dictionary encoded array
13513        let values = StringArray::from_iter([Some("a"), Some("b"), Some("c")]);
13514        let keys = UInt64Array::from_iter(vec![1, 1, 1, 0, 0, 0, 2, 2, 2]);
13515        let array_ref = Arc::new(DictionaryArray::new(keys, Arc::new(values))) as ArrayRef;
13516
13517        // Attempt to cast to RunEndEncoded<Int64, Utf8>
13518        let target_type = DataType::RunEndEncoded(
13519            Arc::new(Field::new("run_ends", DataType::Int64, false)),
13520            Arc::new(Field::new("values", DataType::Utf8, true)),
13521        );
13522        let cast_options = CastOptions {
13523            safe: false,
13524            format_options: FormatOptions::default(),
13525        };
13526
13527        // This should succeed
13528        let result = cast_with_options(&array_ref, &target_type, &cast_options)
13529            .expect("Cast should have succeeded but failed");
13530
13531        // Verify the cast worked correctly
13532        // Assert the values were cast correctly
13533        let run_array = result
13534            .as_any()
13535            .downcast_ref::<RunArray<Int64Type>>()
13536            .unwrap();
13537        assert_eq!(run_array.values().as_string::<i32>().value(0), "b");
13538        assert_eq!(run_array.values().as_string::<i32>().value(1), "a");
13539        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
13540
13541        // Verify the run-ends were cast correctly (run ends at 3, 6, 9)
13542        assert_eq!(run_array.run_ends().values(), &[3i64, 6i64, 9i64]);
13543    }
13544
13545    fn int32_list_values() -> Vec<Option<Vec<Option<i32>>>> {
13546        vec![
13547            Some(vec![Some(1), Some(2), Some(3)]),
13548            Some(vec![Some(4), Some(5), Some(6)]),
13549            None,
13550            Some(vec![Some(7), Some(8), Some(9)]),
13551            Some(vec![None, Some(10)]),
13552        ]
13553    }
13554
13555    #[test]
13556    fn test_cast_list_view_to_list() {
13557        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13558        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13559        assert!(can_cast_types(list_view.data_type(), &target_type));
13560        let cast_result = cast(&list_view, &target_type).unwrap();
13561        let got_list = cast_result.as_list::<i32>();
13562        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13563        assert_eq!(got_list, &expected_list);
13564    }
13565
13566    #[test]
13567    fn test_cast_list_view_to_large_list() {
13568        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13569        let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
13570        assert!(can_cast_types(list_view.data_type(), &target_type));
13571        let cast_result = cast(&list_view, &target_type).unwrap();
13572        let got_list = cast_result.as_list::<i64>();
13573        let expected_list =
13574            LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13575        assert_eq!(got_list, &expected_list);
13576    }
13577
13578    #[test]
13579    fn test_cast_list_to_list_view() {
13580        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13581        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
13582        assert!(can_cast_types(list.data_type(), &target_type));
13583        let cast_result = cast(&list, &target_type).unwrap();
13584
13585        let got_list_view = cast_result.as_list_view::<i32>();
13586        let expected_list_view =
13587            ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13588        assert_eq!(got_list_view, &expected_list_view);
13589
13590        // inner types get cast
13591        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13592            Some(vec![Some(1), Some(2)]),
13593            None,
13594            Some(vec![None, Some(3)]),
13595        ]);
13596        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Float32, true)));
13597        assert!(can_cast_types(list.data_type(), &target_type));
13598        let cast_result = cast(&list, &target_type).unwrap();
13599
13600        let got_list_view = cast_result.as_list_view::<i32>();
13601        let expected_list_view = ListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13602            Some(vec![Some(1.0), Some(2.0)]),
13603            None,
13604            Some(vec![None, Some(3.0)]),
13605        ]);
13606        assert_eq!(got_list_view, &expected_list_view);
13607    }
13608
13609    #[test]
13610    fn test_cast_list_to_large_list_view() {
13611        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13612            Some(vec![Some(1), Some(2)]),
13613            None,
13614            Some(vec![None, Some(3)]),
13615        ]);
13616        let target_type =
13617            DataType::LargeListView(Arc::new(Field::new("item", DataType::Float32, true)));
13618        assert!(can_cast_types(list.data_type(), &target_type));
13619        let cast_result = cast(&list, &target_type).unwrap();
13620
13621        let got_list_view = cast_result.as_list_view::<i64>();
13622        let expected_list_view =
13623            LargeListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13624                Some(vec![Some(1.0), Some(2.0)]),
13625                None,
13626                Some(vec![None, Some(3.0)]),
13627            ]);
13628        assert_eq!(got_list_view, &expected_list_view);
13629    }
13630
13631    #[test]
13632    fn test_cast_large_list_view_to_large_list() {
13633        let list_view =
13634            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13635        let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
13636        assert!(can_cast_types(list_view.data_type(), &target_type));
13637        let cast_result = cast(&list_view, &target_type).unwrap();
13638        let got_list = cast_result.as_list::<i64>();
13639
13640        let expected_list =
13641            LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13642        assert_eq!(got_list, &expected_list);
13643    }
13644
13645    #[test]
13646    fn test_cast_large_list_view_to_list() {
13647        let list_view =
13648            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13649        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13650        assert!(can_cast_types(list_view.data_type(), &target_type));
13651        let cast_result = cast(&list_view, &target_type).unwrap();
13652        let got_list = cast_result.as_list::<i32>();
13653
13654        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13655        assert_eq!(got_list, &expected_list);
13656    }
13657
13658    #[test]
13659    fn test_cast_large_list_to_large_list_view() {
13660        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13661        let target_type =
13662            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
13663        assert!(can_cast_types(list.data_type(), &target_type));
13664        let cast_result = cast(&list, &target_type).unwrap();
13665
13666        let got_list_view = cast_result.as_list_view::<i64>();
13667        let expected_list_view =
13668            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13669        assert_eq!(got_list_view, &expected_list_view);
13670
13671        // inner types get cast
13672        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13673            Some(vec![Some(1), Some(2)]),
13674            None,
13675            Some(vec![None, Some(3)]),
13676        ]);
13677        let target_type =
13678            DataType::LargeListView(Arc::new(Field::new("item", DataType::Float32, true)));
13679        assert!(can_cast_types(list.data_type(), &target_type));
13680        let cast_result = cast(&list, &target_type).unwrap();
13681
13682        let got_list_view = cast_result.as_list_view::<i64>();
13683        let expected_list_view =
13684            LargeListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13685                Some(vec![Some(1.0), Some(2.0)]),
13686                None,
13687                Some(vec![None, Some(3.0)]),
13688            ]);
13689        assert_eq!(got_list_view, &expected_list_view);
13690    }
13691
13692    #[test]
13693    fn test_cast_large_list_to_list_view() {
13694        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13695            Some(vec![Some(1), Some(2)]),
13696            None,
13697            Some(vec![None, Some(3)]),
13698        ]);
13699        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Float32, true)));
13700        assert!(can_cast_types(list.data_type(), &target_type));
13701        let cast_result = cast(&list, &target_type).unwrap();
13702
13703        let got_list_view = cast_result.as_list_view::<i32>();
13704        let expected_list_view = ListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13705            Some(vec![Some(1.0), Some(2.0)]),
13706            None,
13707            Some(vec![None, Some(3.0)]),
13708        ]);
13709        assert_eq!(got_list_view, &expected_list_view);
13710    }
13711
13712    #[test]
13713    fn test_cast_list_view_to_list_out_of_order() {
13714        let list_view = ListViewArray::new(
13715            Arc::new(Field::new("item", DataType::Int32, true)),
13716            ScalarBuffer::from(vec![0, 6, 3]),
13717            ScalarBuffer::from(vec![3, 3, 3]),
13718            Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
13719            None,
13720        );
13721        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, 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        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13726            Some(vec![Some(1), Some(2), Some(3)]),
13727            Some(vec![Some(7), Some(8), Some(9)]),
13728            Some(vec![Some(4), Some(5), Some(6)]),
13729        ]);
13730        assert_eq!(got_list, &expected_list);
13731    }
13732
13733    #[test]
13734    fn test_cast_list_view_to_list_overlapping() {
13735        let list_view = ListViewArray::new(
13736            Arc::new(Field::new("item", DataType::Int32, true)),
13737            ScalarBuffer::from(vec![0, 0]),
13738            ScalarBuffer::from(vec![1, 2]),
13739            Arc::new(Int32Array::from(vec![1, 2])),
13740            None,
13741        );
13742        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13743        assert!(can_cast_types(list_view.data_type(), &target_type));
13744        let cast_result = cast(&list_view, &target_type).unwrap();
13745        let got_list = cast_result.as_list::<i32>();
13746        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13747            Some(vec![Some(1)]),
13748            Some(vec![Some(1), Some(2)]),
13749        ]);
13750        assert_eq!(got_list, &expected_list);
13751    }
13752
13753    #[test]
13754    fn test_cast_list_view_to_list_empty() {
13755        let values: Vec<Option<Vec<Option<i32>>>> = vec![];
13756        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
13757        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13758        assert!(can_cast_types(list_view.data_type(), &target_type));
13759        let cast_result = cast(&list_view, &target_type).unwrap();
13760        let got_list = cast_result.as_list::<i32>();
13761        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
13762        assert_eq!(got_list, &expected_list);
13763    }
13764
13765    #[test]
13766    fn test_cast_list_view_to_list_different_inner_type() {
13767        let values = int32_list_values();
13768        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
13769        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int64, true)));
13770        assert!(can_cast_types(list_view.data_type(), &target_type));
13771        let cast_result = cast(&list_view, &target_type).unwrap();
13772        let got_list = cast_result.as_list::<i32>();
13773
13774        let expected_list =
13775            ListArray::from_iter_primitive::<Int64Type, _, _>(values.into_iter().map(|list| {
13776                list.map(|list| {
13777                    list.into_iter()
13778                        .map(|v| v.map(|v| v as i64))
13779                        .collect::<Vec<_>>()
13780                })
13781            }));
13782        assert_eq!(got_list, &expected_list);
13783    }
13784
13785    #[test]
13786    fn test_cast_list_view_to_list_out_of_order_with_nulls() {
13787        let list_view = ListViewArray::new(
13788            Arc::new(Field::new("item", DataType::Int32, true)),
13789            ScalarBuffer::from(vec![0, 6, 3]),
13790            ScalarBuffer::from(vec![3, 3, 3]),
13791            Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
13792            Some(NullBuffer::from(vec![false, true, false])),
13793        );
13794        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13795        assert!(can_cast_types(list_view.data_type(), &target_type));
13796        let cast_result = cast(&list_view, &target_type).unwrap();
13797        let got_list = cast_result.as_list::<i32>();
13798        let expected_list = ListArray::new(
13799            Arc::new(Field::new("item", DataType::Int32, true)),
13800            OffsetBuffer::from_lengths([3, 3, 3]),
13801            Arc::new(Int32Array::from(vec![1, 2, 3, 7, 8, 9, 4, 5, 6])),
13802            Some(NullBuffer::from(vec![false, true, false])),
13803        );
13804        assert_eq!(got_list, &expected_list);
13805    }
13806
13807    #[test]
13808    fn test_cast_list_view_to_large_list_view() {
13809        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13810        let target_type =
13811            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
13812        assert!(can_cast_types(list_view.data_type(), &target_type));
13813        let cast_result = cast(&list_view, &target_type).unwrap();
13814        let got = cast_result.as_list_view::<i64>();
13815
13816        let expected =
13817            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13818        assert_eq!(got, &expected);
13819    }
13820
13821    #[test]
13822    fn test_cast_large_list_view_to_list_view() {
13823        let list_view =
13824            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13825        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
13826        assert!(can_cast_types(list_view.data_type(), &target_type));
13827        let cast_result = cast(&list_view, &target_type).unwrap();
13828        let got = cast_result.as_list_view::<i32>();
13829
13830        let expected = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13831        assert_eq!(got, &expected);
13832    }
13833
13834    #[test]
13835    fn test_cast_time32_second_to_int64() {
13836        let array = Time32SecondArray::from(vec![1000, 2000, 3000]);
13837        let array = Arc::new(array) as Arc<dyn Array>;
13838        let to_type = DataType::Int64;
13839        let cast_options = CastOptions::default();
13840
13841        assert!(can_cast_types(array.data_type(), &to_type));
13842
13843        let result = cast_with_options(&array, &to_type, &cast_options);
13844        assert!(
13845            result.is_ok(),
13846            "Failed to cast Time32(Second) to Int64: {:?}",
13847            result.err()
13848        );
13849
13850        let cast_array = result.unwrap();
13851        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13852
13853        assert_eq!(cast_array.value(0), 1000);
13854        assert_eq!(cast_array.value(1), 2000);
13855        assert_eq!(cast_array.value(2), 3000);
13856    }
13857
13858    #[test]
13859    fn test_cast_time32_millisecond_to_int64() {
13860        let array = Time32MillisecondArray::from(vec![1000, 2000, 3000]);
13861        let array = Arc::new(array) as Arc<dyn Array>;
13862        let to_type = DataType::Int64;
13863        let cast_options = CastOptions::default();
13864
13865        assert!(can_cast_types(array.data_type(), &to_type));
13866
13867        let result = cast_with_options(&array, &to_type, &cast_options);
13868        assert!(
13869            result.is_ok(),
13870            "Failed to cast Time32(Millisecond) to Int64: {:?}",
13871            result.err()
13872        );
13873
13874        let cast_array = result.unwrap();
13875        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13876
13877        assert_eq!(cast_array.value(0), 1000);
13878        assert_eq!(cast_array.value(1), 2000);
13879        assert_eq!(cast_array.value(2), 3000);
13880    }
13881
13882    #[test]
13883    fn test_cast_time32_millisecond_to_time64_nanosecond() {
13884        let array =
13885            Time32MillisecondArray::from(vec![Some(1_000), Some(2_000), None, Some(43_200_000)]);
13886        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
13887        let c = b.as_primitive::<Time64NanosecondType>();
13888        assert_eq!(c.value(0), 1_000_000_000);
13889        assert_eq!(c.value(1), 2_000_000_000);
13890        assert!(c.is_null(2));
13891        assert_eq!(c.value(3), 43_200_000_000_000);
13892    }
13893
13894    #[test]
13895    fn test_cast_time32_millisecond_to_time64_microsecond() {
13896        let array =
13897            Time32MillisecondArray::from(vec![Some(1_000), Some(2_000), None, Some(43_200_000)]);
13898        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
13899        let c = b.as_primitive::<Time64MicrosecondType>();
13900        assert_eq!(c.value(0), 1_000_000);
13901        assert_eq!(c.value(1), 2_000_000);
13902        assert!(c.is_null(2));
13903        assert_eq!(c.value(3), 43_200_000_000);
13904    }
13905
13906    #[test]
13907    fn test_cast_time32_second_to_time64_nanosecond() {
13908        let array = Time32SecondArray::from(vec![Some(1), Some(60), None, Some(43_200)]);
13909        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
13910        let c = b.as_primitive::<Time64NanosecondType>();
13911        assert_eq!(c.value(0), 1_000_000_000);
13912        assert_eq!(c.value(1), 60_000_000_000);
13913        assert!(c.is_null(2));
13914        assert_eq!(c.value(3), 43_200_000_000_000);
13915    }
13916
13917    #[test]
13918    fn test_cast_time32_second_to_time64_microsecond() {
13919        let array = Time32SecondArray::from(vec![Some(1), Some(60), None, Some(43_200)]);
13920        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
13921        let c = b.as_primitive::<Time64MicrosecondType>();
13922        assert_eq!(c.value(0), 1_000_000);
13923        assert_eq!(c.value(1), 60_000_000);
13924        assert!(c.is_null(2));
13925        assert_eq!(c.value(3), 43_200_000_000);
13926    }
13927
13928    #[test]
13929    fn test_cast_time32_second_to_time32_millisecond_overflow() {
13930        let array = Time32SecondArray::from(vec![i32::MAX]);
13931
13932        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
13933        let c = b.as_primitive::<Time32MillisecondType>();
13934        assert!(c.is_null(0));
13935
13936        let options = CastOptions {
13937            safe: false,
13938            ..Default::default()
13939        };
13940        let err = cast_with_options(&array, &DataType::Time32(TimeUnit::Millisecond), &options)
13941            .unwrap_err();
13942        assert!(err.to_string().contains("Overflow"), "{err}");
13943    }
13944
13945    #[test]
13946    fn test_cast_string_to_time32_second_to_int64() {
13947        // Mimic: select arrow_cast('03:12:44'::time, 'Time32(Second)')::bigint;
13948        // raised in https://github.com/apache/datafusion/issues/19036
13949        let array = StringArray::from(vec!["03:12:44"]);
13950        let array = Arc::new(array) as Arc<dyn Array>;
13951        let cast_options = CastOptions::default();
13952
13953        // 1. Cast String to Time32(Second)
13954        let time32_type = DataType::Time32(TimeUnit::Second);
13955        let time32_array = cast_with_options(&array, &time32_type, &cast_options).unwrap();
13956
13957        // 2. Cast Time32(Second) to Int64
13958        let int64_type = DataType::Int64;
13959        assert!(can_cast_types(time32_array.data_type(), &int64_type));
13960
13961        let result = cast_with_options(&time32_array, &int64_type, &cast_options);
13962
13963        assert!(
13964            result.is_ok(),
13965            "Failed to cast Time32(Second) to Int64: {:?}",
13966            result.err()
13967        );
13968
13969        let cast_array = result.unwrap();
13970        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13971
13972        // 03:12:44 = 3*3600 + 12*60 + 44 = 10800 + 720 + 44 = 11564
13973        assert_eq!(cast_array.value(0), 11564);
13974    }
13975    #[test]
13976    fn test_string_dicts_to_binary_view() {
13977        let expected = BinaryViewArray::from_iter(vec![
13978            VIEW_TEST_DATA[1],
13979            VIEW_TEST_DATA[0],
13980            None,
13981            VIEW_TEST_DATA[3],
13982            None,
13983            VIEW_TEST_DATA[1],
13984            VIEW_TEST_DATA[4],
13985        ]);
13986
13987        let values_arrays: [ArrayRef; _] = [
13988            Arc::new(StringArray::from_iter(VIEW_TEST_DATA)),
13989            Arc::new(StringViewArray::from_iter(VIEW_TEST_DATA)),
13990            Arc::new(LargeStringArray::from_iter(VIEW_TEST_DATA)),
13991        ];
13992        for values in values_arrays {
13993            let keys =
13994                Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
13995            let string_dict_array = DictionaryArray::<Int8Type>::try_new(keys, values).unwrap();
13996
13997            let casted = cast(&string_dict_array, &DataType::BinaryView).unwrap();
13998            assert_eq!(casted.as_ref(), &expected);
13999        }
14000    }
14001
14002    #[test]
14003    fn test_binary_dicts_to_string_view() {
14004        let expected = StringViewArray::from_iter(vec![
14005            VIEW_TEST_DATA[1],
14006            VIEW_TEST_DATA[0],
14007            None,
14008            VIEW_TEST_DATA[3],
14009            None,
14010            VIEW_TEST_DATA[1],
14011            VIEW_TEST_DATA[4],
14012        ]);
14013
14014        let values_arrays: [ArrayRef; _] = [
14015            Arc::new(BinaryArray::from_iter(VIEW_TEST_DATA)),
14016            Arc::new(BinaryViewArray::from_iter(VIEW_TEST_DATA)),
14017            Arc::new(LargeBinaryArray::from_iter(VIEW_TEST_DATA)),
14018        ];
14019        for values in values_arrays {
14020            let keys =
14021                Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
14022            let string_dict_array = DictionaryArray::<Int8Type>::try_new(keys, values).unwrap();
14023
14024            let casted = cast(&string_dict_array, &DataType::Utf8View).unwrap();
14025            assert_eq!(casted.as_ref(), &expected);
14026        }
14027    }
14028
14029    #[test]
14030    fn test_cast_between_sliced_run_end_encoded() {
14031        let run_ends = Int16Array::from(vec![2, 5, 8]);
14032        let values = StringArray::from(vec!["a", "b", "c"]);
14033
14034        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
14035        let ree_array = ree_array.slice(1, 2);
14036        let array_ref = Arc::new(ree_array) as ArrayRef;
14037
14038        let target_type = DataType::RunEndEncoded(
14039            Arc::new(Field::new("run_ends", DataType::Int64, false)),
14040            Arc::new(Field::new("values", DataType::Utf8, true)),
14041        );
14042        let cast_options = CastOptions {
14043            safe: false,
14044            format_options: FormatOptions::default(),
14045        };
14046
14047        let result = cast_with_options(&array_ref, &target_type, &cast_options).unwrap();
14048        let run_array = result.as_run::<Int64Type>();
14049        let run_array = run_array.downcast::<StringArray>().unwrap();
14050
14051        let expected = vec!["a", "b"];
14052        let actual = run_array.into_iter().flatten().collect::<Vec<_>>();
14053
14054        assert_eq!(expected, actual);
14055    }
14056}