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::{DecimalCast, rescale_decimal};
76pub use string::cast_single_string_to_boolean_default;
77
78/// CastOptions provides a way to override the default cast behaviors
79#[derive(Debug, Clone, PartialEq, Eq, Hash)]
80pub struct CastOptions<'a> {
81    /// how to handle cast failures, either return NULL (safe=true) or return ERR (safe=false)
82    pub safe: bool,
83    /// Formatting options when casting from temporal types to string
84    pub format_options: FormatOptions<'a>,
85}
86
87impl Default for CastOptions<'_> {
88    fn default() -> Self {
89        Self {
90            safe: true,
91            format_options: FormatOptions::default(),
92        }
93    }
94}
95
96/// Return true if a value of type `from_type` can be cast into a value of `to_type`.
97///
98/// See [`cast_with_options`] for more information
99pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
100    use self::DataType::*;
101    use self::IntervalUnit::*;
102    use self::TimeUnit::*;
103    if from_type == to_type {
104        return true;
105    }
106
107    match (from_type, to_type) {
108        (Null, _) => true,
109        // Dictionary/List conditions should be put in front of others
110        (Dictionary(_, from_value_type), Dictionary(_, to_value_type)) => {
111            can_cast_types(from_value_type, to_value_type)
112        }
113        (Dictionary(_, value_type), _) => can_cast_types(value_type, to_type),
114        (Union(fields, _), _) => union::resolve_child_array(fields, to_type).is_some(),
115        (_, Union(_, _)) => false,
116        (RunEndEncoded(_, value_type), _) => can_cast_types(value_type.data_type(), to_type),
117        (_, RunEndEncoded(_, value_type)) => can_cast_types(from_type, value_type.data_type()),
118        (_, Dictionary(_, value_type)) => can_cast_types(from_type, value_type),
119        (
120            List(list_from) | LargeList(list_from) | ListView(list_from) | LargeListView(list_from),
121            List(list_to) | LargeList(list_to) | ListView(list_to) | LargeListView(list_to),
122        ) => can_cast_types(list_from.data_type(), list_to.data_type()),
123        (
124            List(list_from) | LargeList(list_from) | ListView(list_from) | LargeListView(list_from),
125            Utf8 | LargeUtf8 | Utf8View,
126        ) => can_cast_types(list_from.data_type(), to_type),
127        (
128            FixedSizeList(list_from, _),
129            List(list_to) | LargeList(list_to) | ListView(list_to) | LargeListView(list_to),
130        ) => can_cast_types(list_from.data_type(), list_to.data_type()),
131        (
132            List(list_from) | LargeList(list_from) | ListView(list_from) | LargeListView(list_from),
133            FixedSizeList(list_to, _),
134        ) => can_cast_types(list_from.data_type(), list_to.data_type()),
135        (FixedSizeList(inner, size), FixedSizeList(inner_to, size_to)) if size == size_to => {
136            can_cast_types(inner.data_type(), inner_to.data_type())
137        }
138        (_, List(list_to) | LargeList(list_to) | ListView(list_to) | LargeListView(list_to)) => {
139            can_cast_types(from_type, list_to.data_type())
140        }
141        (_, FixedSizeList(list_to, size)) if *size == 1 => {
142            can_cast_types(from_type, list_to.data_type())
143        }
144        (FixedSizeList(list_from, size), _) if *size == 1 => {
145            can_cast_types(list_from.data_type(), to_type)
146        }
147        (Map(from_entries, ordered_from), Map(to_entries, ordered_to))
148            if ordered_from == ordered_to =>
149        {
150            match (
151                key_field(from_entries),
152                key_field(to_entries),
153                value_field(from_entries),
154                value_field(to_entries),
155            ) {
156                (Some(from_key), Some(to_key), Some(from_value), Some(to_value)) => {
157                    can_cast_types(from_key.data_type(), to_key.data_type())
158                        && can_cast_types(from_value.data_type(), to_value.data_type())
159                }
160                _ => false,
161            }
162        }
163        // cast one decimal type to another decimal type
164        (
165            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
166            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
167        ) => true,
168        // unsigned integer to decimal
169        (
170            UInt8 | UInt16 | UInt32 | UInt64,
171            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
172        ) => true,
173        // signed numeric to decimal
174        (
175            Int8 | Int16 | Int32 | Int64 | Float32 | Float64,
176            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
177        ) => true,
178        // decimal to unsigned numeric
179        (
180            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
181            UInt8 | UInt16 | UInt32 | UInt64,
182        ) => true,
183        // decimal to signed numeric
184        (
185            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
186            Null | Int8 | Int16 | Int32 | Int64 | Float32 | Float64,
187        ) => true,
188        // decimal to string
189        (
190            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
191            Utf8View | Utf8 | LargeUtf8,
192        ) => true,
193        // string to decimal
194        (
195            Utf8View | Utf8 | LargeUtf8,
196            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
197        ) => true,
198        (Struct(from_fields), Struct(to_fields)) => {
199            if from_fields.len() != to_fields.len() {
200                return false;
201            }
202
203            // fast path, all field names are in the same order and same number of fields
204            if from_fields
205                .iter()
206                .zip(to_fields.iter())
207                .all(|(f1, f2)| f1.name() == f2.name())
208            {
209                return from_fields.iter().zip(to_fields.iter()).all(|(f1, f2)| {
210                    // Assume that nullability between two structs are compatible, if not,
211                    // cast kernel will return error.
212                    can_cast_types(f1.data_type(), f2.data_type())
213                });
214            }
215
216            // slow path, we match the fields by name
217            if to_fields.iter().all(|to_field| {
218                from_fields
219                    .iter()
220                    .find(|from_field| from_field.name() == to_field.name())
221                    .is_some_and(|from_field| {
222                        // Assume that nullability between two structs are compatible, if not,
223                        // cast kernel will return error.
224                        can_cast_types(from_field.data_type(), to_field.data_type())
225                    })
226            }) {
227                return true;
228            }
229
230            // if we couldn't match by name, we try to see if they can be matched by position
231            from_fields
232                .iter()
233                .zip(to_fields.iter())
234                .all(|(f1, f2)| can_cast_types(f1.data_type(), f2.data_type()))
235        }
236        (Struct(_), _) => false,
237        (_, Struct(_)) => false,
238        (_, Boolean) => from_type.is_integer() || from_type.is_floating() || from_type.is_string(),
239        (Boolean, _) => to_type.is_integer() || to_type.is_floating() || to_type.is_string(),
240
241        (Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
242            true
243        }
244        (LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
245            true
246        }
247        (FixedSizeBinary(_), Binary | LargeBinary | BinaryView) => true,
248        (
249            Utf8 | LargeUtf8 | Utf8View,
250            Binary
251            | LargeBinary
252            | Utf8
253            | LargeUtf8
254            | Date32
255            | Date64
256            | Time32(Second)
257            | Time32(Millisecond)
258            | Time64(Microsecond)
259            | Time64(Nanosecond)
260            | Timestamp(Second, _)
261            | Timestamp(Millisecond, _)
262            | Timestamp(Microsecond, _)
263            | Timestamp(Nanosecond, _)
264            | Interval(_)
265            | BinaryView,
266        ) => true,
267        (Utf8 | LargeUtf8, Utf8View) => true,
268        (BinaryView, Binary | LargeBinary | Utf8 | LargeUtf8 | Utf8View) => true,
269        (Utf8View | Utf8 | LargeUtf8, _) => to_type.is_numeric(),
270        (_, Utf8 | Utf8View | LargeUtf8) => from_type.is_primitive(),
271
272        (_, Binary | LargeBinary) => from_type.is_integer(),
273
274        // start numeric casts
275        (
276            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
277            | Float64,
278            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
279            | Float64,
280        ) => true,
281        // end numeric casts
282
283        // temporal casts
284        (Int32, Date32 | Date64 | Time32(_)) => true,
285        (Date32, Int32 | Int64) => true,
286        (Time32(_), Int32 | Int64) => true,
287        (Int64, Date64 | Date32 | Time64(_)) => true,
288        (Date64, Int64 | Int32) => true,
289        (Time64(_), Int64) => true,
290        (Date32 | Date64, Date32 | Date64) => true,
291        // time casts
292        (Time32(_), Time32(_)) => true,
293        (Time32(_), Time64(_)) => true,
294        (Time64(_), Time64(_)) => true,
295        (Time64(_), Time32(to_unit)) => {
296            matches!(to_unit, Second | Millisecond)
297        }
298        (Timestamp(_, _), _) if to_type.is_numeric() => true,
299        (_, Timestamp(_, _)) if from_type.is_numeric() => true,
300        (Date64, Timestamp(_, _)) => true,
301        (Date32, Timestamp(_, _)) => true,
302        (
303            Timestamp(_, _),
304            Timestamp(_, _)
305            | Date32
306            | Date64
307            | Time32(Second)
308            | Time32(Millisecond)
309            | Time64(Microsecond)
310            | Time64(Nanosecond),
311        ) => true,
312        (_, Duration(_)) if from_type.is_numeric() => true,
313        (Duration(_), _) if to_type.is_numeric() => true,
314        (Duration(_), Duration(_)) => true,
315        (Interval(from_type), Int64) => {
316            match from_type {
317                YearMonth => true,
318                DayTime => true,
319                MonthDayNano => false, // Native type is i128
320            }
321        }
322        (Int32, Interval(to_type)) => match to_type {
323            YearMonth => true,
324            DayTime => false,
325            MonthDayNano => false,
326        },
327        (Duration(_), Interval(MonthDayNano)) => true,
328        (Interval(MonthDayNano), Duration(_)) => true,
329        (Interval(YearMonth), Interval(MonthDayNano)) => true,
330        (Interval(DayTime), Interval(MonthDayNano)) => true,
331        (_, _) => false,
332    }
333}
334
335/// Cast `array` to the provided data type and return a new Array with type `to_type`, if possible.
336///
337/// See [`cast_with_options`] for more information
338pub fn cast(array: &dyn Array, to_type: &DataType) -> Result<ArrayRef, ArrowError> {
339    cast_with_options(array, to_type, &CastOptions::default())
340}
341
342fn cast_integer_to_decimal<
343    T: ArrowPrimitiveType,
344    D: DecimalType + ArrowPrimitiveType<Native = M>,
345    M,
346>(
347    array: &PrimitiveArray<T>,
348    precision: u8,
349    scale: i8,
350    base: M,
351    cast_options: &CastOptions,
352) -> Result<ArrayRef, ArrowError>
353where
354    <T as ArrowPrimitiveType>::Native: AsPrimitive<M>,
355    M: ArrowNativeTypeOp,
356{
357    let scale_factor = base.pow_checked(scale.unsigned_abs() as u32).map_err(|_| {
358        ArrowError::CastError(format!(
359            "Cannot cast to {:?}({}, {}). The scale causes overflow.",
360            D::PREFIX,
361            precision,
362            scale,
363        ))
364    })?;
365
366    let array = if scale < 0 {
367        match cast_options.safe {
368            true => array.unary_opt::<_, D>(|v| {
369                v.as_()
370                    .div_checked(scale_factor)
371                    .ok()
372                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
373            }),
374            false => array.try_unary::<_, D, _>(|v| {
375                v.as_()
376                    .div_checked(scale_factor)
377                    .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|_| v))
378            })?,
379        }
380    } else {
381        match cast_options.safe {
382            true => array.unary_opt::<_, D>(|v| {
383                v.as_()
384                    .mul_checked(scale_factor)
385                    .ok()
386                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
387            }),
388            false => array.try_unary::<_, D, _>(|v| {
389                v.as_()
390                    .mul_checked(scale_factor)
391                    .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|_| v))
392            })?,
393        }
394    };
395
396    Ok(Arc::new(array.with_precision_and_scale(precision, scale)?))
397}
398
399/// Cast the array from interval year month to month day nano
400fn cast_interval_year_month_to_interval_month_day_nano(
401    array: &dyn Array,
402    _cast_options: &CastOptions,
403) -> Result<ArrayRef, ArrowError> {
404    let array = array.as_primitive::<IntervalYearMonthType>();
405
406    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
407        let months = IntervalYearMonthType::to_months(v);
408        IntervalMonthDayNanoType::make_value(months, 0, 0)
409    })))
410}
411
412/// Cast the array from interval day time to month day nano
413fn cast_interval_day_time_to_interval_month_day_nano(
414    array: &dyn Array,
415    _cast_options: &CastOptions,
416) -> Result<ArrayRef, ArrowError> {
417    let array = array.as_primitive::<IntervalDayTimeType>();
418    let mul = 1_000_000;
419
420    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
421        let (days, ms) = IntervalDayTimeType::to_parts(v);
422        IntervalMonthDayNanoType::make_value(0, days, ms as i64 * mul)
423    })))
424}
425
426/// Cast the array from interval to duration
427fn cast_month_day_nano_to_duration<D: ArrowTemporalType<Native = i64>>(
428    array: &dyn Array,
429    cast_options: &CastOptions,
430) -> Result<ArrayRef, ArrowError> {
431    let array = array.as_primitive::<IntervalMonthDayNanoType>();
432    let scale = match D::DATA_TYPE {
433        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
434        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
435        DataType::Duration(TimeUnit::Microsecond) => 1_000,
436        DataType::Duration(TimeUnit::Nanosecond) => 1,
437        _ => unreachable!(),
438    };
439
440    if cast_options.safe {
441        let iter = array.iter().map(|v| {
442            v.and_then(|v| (v.days == 0 && v.months == 0).then_some(v.nanoseconds / scale))
443        });
444        Ok(Arc::new(unsafe {
445            PrimitiveArray::<D>::from_trusted_len_iter(iter)
446        }))
447    } else {
448        let vec = array
449            .iter()
450            .map(|v| {
451                v.map(|v| match v.days == 0 && v.months == 0 {
452                    true => Ok((v.nanoseconds) / scale),
453                    _ => Err(ArrowError::ComputeError(
454                        "Cannot convert interval containing non-zero months or days to duration"
455                            .to_string(),
456                    )),
457                })
458                .transpose()
459            })
460            .collect::<Result<Vec<_>, _>>()?;
461        Ok(Arc::new(unsafe {
462            PrimitiveArray::<D>::from_trusted_len_iter(vec.iter())
463        }))
464    }
465}
466
467/// Cast the array from duration and interval
468fn cast_duration_to_interval<D: ArrowTemporalType<Native = i64>>(
469    array: &dyn Array,
470    cast_options: &CastOptions,
471) -> Result<ArrayRef, ArrowError> {
472    let array = array
473        .as_any()
474        .downcast_ref::<PrimitiveArray<D>>()
475        .ok_or_else(|| {
476            ArrowError::ComputeError(
477                "Internal Error: Cannot cast duration to DurationArray of expected type"
478                    .to_string(),
479            )
480        })?;
481
482    let scale = match array.data_type() {
483        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
484        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
485        DataType::Duration(TimeUnit::Microsecond) => 1_000,
486        DataType::Duration(TimeUnit::Nanosecond) => 1,
487        _ => unreachable!(),
488    };
489
490    if cast_options.safe {
491        let iter = array.iter().map(|v| {
492            v.and_then(|v| {
493                v.checked_mul(scale)
494                    .map(|v| IntervalMonthDayNano::new(0, 0, v))
495            })
496        });
497        Ok(Arc::new(unsafe {
498            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(iter)
499        }))
500    } else {
501        let vec = array
502            .iter()
503            .map(|v| {
504                v.map(|v| {
505                    if let Ok(v) = v.mul_checked(scale) {
506                        Ok(IntervalMonthDayNano::new(0, 0, v))
507                    } else {
508                        Err(ArrowError::ComputeError(format!(
509                            "Cannot cast to {:?}. Overflowing on {:?}",
510                            IntervalMonthDayNanoType::DATA_TYPE,
511                            v
512                        )))
513                    }
514                })
515                .transpose()
516            })
517            .collect::<Result<Vec<_>, _>>()?;
518        Ok(Arc::new(unsafe {
519            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(vec.iter())
520        }))
521    }
522}
523
524/// Cast the primitive array using [`PrimitiveArray::reinterpret_cast`]
525fn cast_reinterpret_arrays<I: ArrowPrimitiveType, O: ArrowPrimitiveType<Native = I::Native>>(
526    array: &dyn Array,
527) -> Result<ArrayRef, ArrowError> {
528    Ok(Arc::new(array.as_primitive::<I>().reinterpret_cast::<O>()))
529}
530
531fn make_timestamp_array(
532    array: &PrimitiveArray<Int64Type>,
533    unit: TimeUnit,
534    tz: Option<Arc<str>>,
535) -> ArrayRef {
536    match unit {
537        TimeUnit::Second => Arc::new(
538            array
539                .reinterpret_cast::<TimestampSecondType>()
540                .with_timezone_opt(tz),
541        ),
542        TimeUnit::Millisecond => Arc::new(
543            array
544                .reinterpret_cast::<TimestampMillisecondType>()
545                .with_timezone_opt(tz),
546        ),
547        TimeUnit::Microsecond => Arc::new(
548            array
549                .reinterpret_cast::<TimestampMicrosecondType>()
550                .with_timezone_opt(tz),
551        ),
552        TimeUnit::Nanosecond => Arc::new(
553            array
554                .reinterpret_cast::<TimestampNanosecondType>()
555                .with_timezone_opt(tz),
556        ),
557    }
558}
559
560fn make_duration_array(array: &PrimitiveArray<Int64Type>, unit: TimeUnit) -> ArrayRef {
561    match unit {
562        TimeUnit::Second => Arc::new(array.reinterpret_cast::<DurationSecondType>()),
563        TimeUnit::Millisecond => Arc::new(array.reinterpret_cast::<DurationMillisecondType>()),
564        TimeUnit::Microsecond => Arc::new(array.reinterpret_cast::<DurationMicrosecondType>()),
565        TimeUnit::Nanosecond => Arc::new(array.reinterpret_cast::<DurationNanosecondType>()),
566    }
567}
568
569fn as_time_res_with_timezone<T: ArrowPrimitiveType>(
570    v: i64,
571    tz: Option<Tz>,
572) -> Result<NaiveTime, ArrowError> {
573    let time = match tz {
574        Some(tz) => as_datetime_with_timezone::<T>(v, tz).map(|d| d.time()),
575        None => as_datetime::<T>(v).map(|d| d.time()),
576    };
577
578    time.ok_or_else(|| {
579        ArrowError::CastError(format!(
580            "Failed to create naive time with {} {}",
581            std::any::type_name::<T>(),
582            v
583        ))
584    })
585}
586
587fn timestamp_to_date32<T: ArrowTimestampType>(
588    array: &PrimitiveArray<T>,
589) -> Result<ArrayRef, ArrowError> {
590    let err = |x: i64| {
591        ArrowError::CastError(format!(
592            "Cannot convert {} {x} to datetime",
593            std::any::type_name::<T>()
594        ))
595    };
596
597    let array: Date32Array = match array.timezone() {
598        Some(tz) => {
599            let tz: Tz = tz.parse()?;
600            array.try_unary(|x| {
601                as_datetime_with_timezone::<T>(x, tz)
602                    .ok_or_else(|| err(x))
603                    .map(|d| Date32Type::from_naive_date(d.date_naive()))
604            })?
605        }
606        None => array.try_unary(|x| {
607            as_datetime::<T>(x)
608                .ok_or_else(|| err(x))
609                .map(|d| Date32Type::from_naive_date(d.date()))
610        })?,
611    };
612    Ok(Arc::new(array))
613}
614
615/// Try to cast `array` to `to_type` if possible.
616///
617/// Returns a new Array with type `to_type` if possible.
618///
619/// Accepts [`CastOptions`] to specify cast behavior. See also [`cast()`].
620///
621/// # Behavior
622/// * `Boolean` to `Utf8`: `true` => '1', `false` => `0`
623/// * `Utf8` to `Boolean`: `true`, `yes`, `on`, `1` => `true`, `false`, `no`, `off`, `0` => `false`,
624///   short variants are accepted, other strings return null or error
625/// * `Utf8` to Numeric: strings that can't be parsed to numbers return null, float strings
626///   in integer casts return null
627/// * Numeric to `Boolean`: 0 returns `false`, any other value returns `true`
628/// * `List` to `List`: the underlying data type is cast
629/// * `List` to `FixedSizeList`: the underlying data type is cast. If safe is true and a list element
630///   has the wrong length it will be replaced with NULL, otherwise an error will be returned
631/// * Primitive to `List`: a list array with 1 value per slot is created
632/// * `Date32` and `Date64`: precision lost when going to higher interval
633/// * `Time32 and `Time64`: precision lost when going to higher interval
634/// * `Timestamp` and `Date{32|64}`: precision lost when going to higher interval
635/// * Temporal to/from backing Primitive: zero-copy with data type change
636/// * `Float32/Float64` to `Decimal(precision, scale)` rounds to the `scale` decimals
637///   (i.e. casting `6.4999` to `Decimal(10, 1)` becomes `6.5`).
638/// * `Decimal` to `Float32/Float64` is lossy and values outside the representable
639///   range become `INFINITY` or `-INFINITY` without error.
640///
641/// Unsupported Casts (check with `can_cast_types` before calling):
642/// * To or from `StructArray`
643/// * `List` to `Primitive`
644/// * `Interval` and `Duration`
645///
646/// # Durations and Intervals
647///
648/// Casting integer types directly to interval types such as
649/// [`IntervalMonthDayNano`] is not supported because the meaning of the integer
650/// is ambiguous. For example, the integer  could represent either nanoseconds
651/// or months.
652///
653/// To cast an integer type to an interval type, first convert to a Duration
654/// type, and then cast that to the desired interval type.
655///
656/// For example, to convert an `Int64` representing nanoseconds to an
657/// `IntervalMonthDayNano` you would first convert the `Int64` to a
658/// `DurationNanoseconds`, and then cast that to `IntervalMonthDayNano`.
659///
660/// # Timestamps and Timezones
661///
662/// Timestamps are stored with an optional timezone in Arrow.
663///
664/// ## Casting timestamps to a timestamp without timezone / UTC
665/// ```
666/// # use arrow_array::Int64Array;
667/// # use arrow_array::types::TimestampSecondType;
668/// # use arrow_cast::{cast, display};
669/// # use arrow_array::cast::AsArray;
670/// # use arrow_schema::{DataType, TimeUnit};
671/// // can use "UTC" if chrono-tz feature is enabled, here use offset based timezone
672/// let data_type = DataType::Timestamp(TimeUnit::Second, None);
673/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
674/// let b = cast(&a, &data_type).unwrap();
675/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
676/// assert_eq!(2_000_000_000, b.value(1)); // values are the same as the type has no timezone
677/// // use display to show them (note has no trailing Z)
678/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
679/// ```
680///
681/// ## Casting timestamps to a timestamp with timezone
682///
683/// Similarly to the previous example, if you cast numeric values to a timestamp
684/// with timezone, the cast kernel will not change the underlying values
685/// but display and other functions will interpret them as being in the provided timezone.
686///
687/// ```
688/// # use arrow_array::Int64Array;
689/// # use arrow_array::types::TimestampSecondType;
690/// # use arrow_cast::{cast, display};
691/// # use arrow_array::cast::AsArray;
692/// # use arrow_schema::{DataType, TimeUnit};
693/// // can use "Americas/New_York" if chrono-tz feature is enabled, here use offset based timezone
694/// let data_type = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
695/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
696/// let b = cast(&a, &data_type).unwrap();
697/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
698/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
699/// // displayed in the target timezone (note the offset -05:00)
700/// assert_eq!("2033-05-17T22:33:20-05:00", display::array_value_to_string(&b, 1).unwrap());
701/// ```
702/// # Casting timestamps without timezone to timestamps with timezone
703///
704/// When casting from a timestamp without timezone to a timestamp with
705/// timezone, the cast kernel interprets the timestamp values as being in
706/// the destination timezone and then adjusts the underlying value to UTC as required
707///
708/// However, note that when casting from a timestamp with timezone BACK to a
709/// timestamp without timezone the cast kernel does not adjust the values.
710///
711/// Thus round trip casting a timestamp without timezone to a timestamp with
712/// timezone and back to a timestamp without timezone results in different
713/// values than the starting values.
714///
715/// ```
716/// # use arrow_array::Int64Array;
717/// # use arrow_array::types::{TimestampSecondType};
718/// # use arrow_cast::{cast, display};
719/// # use arrow_array::cast::AsArray;
720/// # use arrow_schema::{DataType, TimeUnit};
721/// let data_type  = DataType::Timestamp(TimeUnit::Second, None);
722/// let data_type_tz = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
723/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
724/// let b = cast(&a, &data_type).unwrap(); // cast to timestamp without timezone
725/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
726/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
727/// // displayed without a timezone (note lack of offset or Z)
728/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
729///
730/// // Convert timestamps without a timezone to timestamps with a timezone
731/// let c = cast(&b, &data_type_tz).unwrap();
732/// let c = c.as_primitive::<TimestampSecondType>(); // downcast to result type
733/// assert_eq!(2_000_018_000, c.value(1)); // value has been adjusted by offset
734/// // displayed with the target timezone offset (-05:00)
735/// assert_eq!("2033-05-18T03:33:20-05:00", display::array_value_to_string(&c, 1).unwrap());
736///
737/// // Convert from timestamp with timezone back to timestamp without timezone
738/// let d = cast(&c, &data_type).unwrap();
739/// let d = d.as_primitive::<TimestampSecondType>(); // downcast to result type
740/// assert_eq!(2_000_018_000, d.value(1)); // value has not been adjusted
741/// // NOTE: the timestamp is adjusted (08:33:20 instead of 03:33:20 as in previous example)
742/// assert_eq!("2033-05-18T08:33:20", display::array_value_to_string(&d, 1).unwrap());
743/// ```
744pub fn cast_with_options(
745    array: &dyn Array,
746    to_type: &DataType,
747    cast_options: &CastOptions,
748) -> Result<ArrayRef, ArrowError> {
749    use DataType::*;
750    let from_type = array.data_type();
751    // clone array if types are the same
752    if from_type == to_type {
753        return Ok(make_array(array.to_data()));
754    }
755    match (from_type, to_type) {
756        (Null, _) => Ok(new_null_array(to_type, array.len())),
757        (RunEndEncoded(index_type, _), _) => match index_type.data_type() {
758            Int16 => run_end_encoded_cast::<Int16Type>(array, to_type, cast_options),
759            Int32 => run_end_encoded_cast::<Int32Type>(array, to_type, cast_options),
760            Int64 => run_end_encoded_cast::<Int64Type>(array, to_type, cast_options),
761            _ => Err(ArrowError::CastError(format!(
762                "Casting from run end encoded type {from_type:?} to {to_type:?} not supported",
763            ))),
764        },
765        (_, RunEndEncoded(index_type, value_type)) => {
766            let array_ref = make_array(array.to_data());
767            match index_type.data_type() {
768                Int16 => cast_to_run_end_encoded::<Int16Type>(
769                    &array_ref,
770                    value_type.data_type(),
771                    cast_options,
772                ),
773                Int32 => cast_to_run_end_encoded::<Int32Type>(
774                    &array_ref,
775                    value_type.data_type(),
776                    cast_options,
777                ),
778                Int64 => cast_to_run_end_encoded::<Int64Type>(
779                    &array_ref,
780                    value_type.data_type(),
781                    cast_options,
782                ),
783                _ => Err(ArrowError::CastError(format!(
784                    "Casting from type {from_type:?} to run end encoded type {to_type:?} not supported",
785                ))),
786            }
787        }
788        (Union(_, _), _) => union_extract_by_type(
789            array.as_any().downcast_ref::<UnionArray>().unwrap(),
790            to_type,
791            cast_options,
792        ),
793        (_, Union(_, _)) => Err(ArrowError::CastError(format!(
794            "Casting from {from_type} to {to_type} not supported"
795        ))),
796        (Dictionary(index_type, _), _) => match **index_type {
797            Int8 => dictionary_cast::<Int8Type>(array, to_type, cast_options),
798            Int16 => dictionary_cast::<Int16Type>(array, to_type, cast_options),
799            Int32 => dictionary_cast::<Int32Type>(array, to_type, cast_options),
800            Int64 => dictionary_cast::<Int64Type>(array, to_type, cast_options),
801            UInt8 => dictionary_cast::<UInt8Type>(array, to_type, cast_options),
802            UInt16 => dictionary_cast::<UInt16Type>(array, to_type, cast_options),
803            UInt32 => dictionary_cast::<UInt32Type>(array, to_type, cast_options),
804            UInt64 => dictionary_cast::<UInt64Type>(array, to_type, cast_options),
805            _ => Err(ArrowError::CastError(format!(
806                "Casting from dictionary type {from_type} to {to_type} not supported",
807            ))),
808        },
809        (_, Dictionary(index_type, value_type)) => match **index_type {
810            Int8 => cast_to_dictionary::<Int8Type>(array, value_type, cast_options),
811            Int16 => cast_to_dictionary::<Int16Type>(array, value_type, cast_options),
812            Int32 => cast_to_dictionary::<Int32Type>(array, value_type, cast_options),
813            Int64 => cast_to_dictionary::<Int64Type>(array, value_type, cast_options),
814            UInt8 => cast_to_dictionary::<UInt8Type>(array, value_type, cast_options),
815            UInt16 => cast_to_dictionary::<UInt16Type>(array, value_type, cast_options),
816            UInt32 => cast_to_dictionary::<UInt32Type>(array, value_type, cast_options),
817            UInt64 => cast_to_dictionary::<UInt64Type>(array, value_type, cast_options),
818            _ => Err(ArrowError::CastError(format!(
819                "Casting from type {from_type} to dictionary type {to_type} not supported",
820            ))),
821        },
822        // Casting between lists of same types (cast inner values)
823        (List(_), List(to)) => cast_list_values::<i32>(array, to, cast_options),
824        (LargeList(_), LargeList(to)) => cast_list_values::<i64>(array, to, cast_options),
825        (FixedSizeList(_, size_from), FixedSizeList(list_to, size_to)) => {
826            if size_from != size_to {
827                return Err(ArrowError::CastError(
828                    "cannot cast fixed-size-list to fixed-size-list with different size".into(),
829                ));
830            }
831            let array = array.as_fixed_size_list();
832            let values = cast_with_options(array.values(), list_to.data_type(), cast_options)?;
833            Ok(Arc::new(FixedSizeListArray::try_new(
834                list_to.clone(),
835                *size_from,
836                values,
837                array.nulls().cloned(),
838            )?))
839        }
840        (ListView(_), ListView(to)) => cast_list_view_values::<i32>(array, to, cast_options),
841        (LargeListView(_), LargeListView(to)) => {
842            cast_list_view_values::<i64>(array, to, cast_options)
843        }
844        // Casting between different types of lists
845        // List
846        (List(_), LargeList(list_to)) => cast_list::<i32, i64>(array, list_to, cast_options),
847        (List(_), FixedSizeList(field, size)) => {
848            cast_list_to_fixed_size_list::<i32>(array, field, *size, cast_options)
849        }
850        (List(_), ListView(list_to)) => {
851            cast_list_to_list_view::<i32, i32>(array, list_to, cast_options)
852        }
853        (List(_), LargeListView(list_to)) => {
854            cast_list_to_list_view::<i32, i64>(array, list_to, cast_options)
855        }
856        // LargeList
857        (LargeList(_), List(list_to)) => cast_list::<i64, i32>(array, list_to, cast_options),
858        (LargeList(_), FixedSizeList(field, size)) => {
859            cast_list_to_fixed_size_list::<i64>(array, field, *size, cast_options)
860        }
861        (LargeList(_), ListView(list_to)) => {
862            cast_list_to_list_view::<i64, i32>(array, list_to, cast_options)
863        }
864        (LargeList(_), LargeListView(list_to)) => {
865            cast_list_to_list_view::<i64, i64>(array, list_to, cast_options)
866        }
867        // ListView
868        (ListView(_), List(list_to)) => {
869            cast_list_view_to_list::<i32, Int32Type>(array, list_to, cast_options)
870        }
871        (ListView(_), LargeList(list_to)) => {
872            cast_list_view_to_list::<i32, Int64Type>(array, list_to, cast_options)
873        }
874        (ListView(_), LargeListView(list_to)) => {
875            cast_list_view::<i32, i64>(array, list_to, cast_options)
876        }
877        (ListView(_), FixedSizeList(field, size)) => {
878            cast_list_view_to_fixed_size_list::<i32>(array, field, *size, cast_options)
879        }
880        // LargeListView
881        (LargeListView(_), LargeList(list_to)) => {
882            cast_list_view_to_list::<i64, Int64Type>(array, list_to, cast_options)
883        }
884        (LargeListView(_), List(list_to)) => {
885            cast_list_view_to_list::<i64, Int32Type>(array, list_to, cast_options)
886        }
887        (LargeListView(_), ListView(list_to)) => {
888            cast_list_view::<i64, i32>(array, list_to, cast_options)
889        }
890        (LargeListView(_), FixedSizeList(field, size)) => {
891            cast_list_view_to_fixed_size_list::<i64>(array, field, *size, cast_options)
892        }
893        // FixedSizeList
894        (FixedSizeList(_, _), List(list_to)) => {
895            cast_fixed_size_list_to_list::<i32>(array, list_to, cast_options)
896        }
897        (FixedSizeList(_, _), LargeList(list_to)) => {
898            cast_fixed_size_list_to_list::<i64>(array, list_to, cast_options)
899        }
900        (FixedSizeList(_, _), ListView(list_to)) => {
901            cast_fixed_size_list_to_list_view::<i32>(array, list_to, cast_options)
902        }
903        (FixedSizeList(_, _), LargeListView(list_to)) => {
904            cast_fixed_size_list_to_list_view::<i64>(array, list_to, cast_options)
905        }
906        // List to/from other types
907        (FixedSizeList(_, size), _) if *size == 1 => {
908            cast_single_element_fixed_size_list_to_values(array, to_type, cast_options)
909        }
910        // NOTE: we could support FSL to string here too but might be confusing
911        //       since behaviour for size 1 would be different (see arm above)
912        (List(_) | LargeList(_) | ListView(_) | LargeListView(_), _) => match to_type {
913            Utf8 => value_to_string::<i32>(array, cast_options),
914            LargeUtf8 => value_to_string::<i64>(array, cast_options),
915            Utf8View => value_to_string_view(array, cast_options),
916            dt => Err(ArrowError::CastError(format!(
917                "Cannot cast LIST to non-list data type {dt}"
918            ))),
919        },
920        (_, List(to)) => cast_values_to_list::<i32>(array, to, cast_options),
921        (_, LargeList(to)) => cast_values_to_list::<i64>(array, to, cast_options),
922        (_, ListView(to)) => cast_values_to_list_view::<i32>(array, to, cast_options),
923        (_, LargeListView(to)) => cast_values_to_list_view::<i64>(array, to, cast_options),
924        (_, FixedSizeList(to, size)) if *size == 1 => {
925            cast_values_to_fixed_size_list(array, to, *size, cast_options)
926        }
927        // Map
928        (Map(_, ordered1), Map(_, ordered2)) if ordered1 == ordered2 => {
929            cast_map_values(array.as_map(), to_type, cast_options, ordered1.to_owned())
930        }
931        // Decimal to decimal, same width
932        (Decimal32(p1, s1), Decimal32(p2, s2)) => {
933            cast_decimal_to_decimal_same_type::<Decimal32Type>(
934                array.as_primitive(),
935                *p1,
936                *s1,
937                *p2,
938                *s2,
939                cast_options,
940            )
941        }
942        (Decimal64(p1, s1), Decimal64(p2, s2)) => {
943            cast_decimal_to_decimal_same_type::<Decimal64Type>(
944                array.as_primitive(),
945                *p1,
946                *s1,
947                *p2,
948                *s2,
949                cast_options,
950            )
951        }
952        (Decimal128(p1, s1), Decimal128(p2, s2)) => {
953            cast_decimal_to_decimal_same_type::<Decimal128Type>(
954                array.as_primitive(),
955                *p1,
956                *s1,
957                *p2,
958                *s2,
959                cast_options,
960            )
961        }
962        (Decimal256(p1, s1), Decimal256(p2, s2)) => {
963            cast_decimal_to_decimal_same_type::<Decimal256Type>(
964                array.as_primitive(),
965                *p1,
966                *s1,
967                *p2,
968                *s2,
969                cast_options,
970            )
971        }
972        // Decimal to decimal, different width
973        (Decimal32(p1, s1), Decimal64(p2, s2)) => {
974            cast_decimal_to_decimal::<Decimal32Type, Decimal64Type>(
975                array.as_primitive(),
976                *p1,
977                *s1,
978                *p2,
979                *s2,
980                cast_options,
981            )
982        }
983        (Decimal32(p1, s1), Decimal128(p2, s2)) => {
984            cast_decimal_to_decimal::<Decimal32Type, Decimal128Type>(
985                array.as_primitive(),
986                *p1,
987                *s1,
988                *p2,
989                *s2,
990                cast_options,
991            )
992        }
993        (Decimal32(p1, s1), Decimal256(p2, s2)) => {
994            cast_decimal_to_decimal::<Decimal32Type, Decimal256Type>(
995                array.as_primitive(),
996                *p1,
997                *s1,
998                *p2,
999                *s2,
1000                cast_options,
1001            )
1002        }
1003        (Decimal64(p1, s1), Decimal32(p2, s2)) => {
1004            cast_decimal_to_decimal::<Decimal64Type, Decimal32Type>(
1005                array.as_primitive(),
1006                *p1,
1007                *s1,
1008                *p2,
1009                *s2,
1010                cast_options,
1011            )
1012        }
1013        (Decimal64(p1, s1), Decimal128(p2, s2)) => {
1014            cast_decimal_to_decimal::<Decimal64Type, Decimal128Type>(
1015                array.as_primitive(),
1016                *p1,
1017                *s1,
1018                *p2,
1019                *s2,
1020                cast_options,
1021            )
1022        }
1023        (Decimal64(p1, s1), Decimal256(p2, s2)) => {
1024            cast_decimal_to_decimal::<Decimal64Type, Decimal256Type>(
1025                array.as_primitive(),
1026                *p1,
1027                *s1,
1028                *p2,
1029                *s2,
1030                cast_options,
1031            )
1032        }
1033        (Decimal128(p1, s1), Decimal32(p2, s2)) => {
1034            cast_decimal_to_decimal::<Decimal128Type, Decimal32Type>(
1035                array.as_primitive(),
1036                *p1,
1037                *s1,
1038                *p2,
1039                *s2,
1040                cast_options,
1041            )
1042        }
1043        (Decimal128(p1, s1), Decimal64(p2, s2)) => {
1044            cast_decimal_to_decimal::<Decimal128Type, Decimal64Type>(
1045                array.as_primitive(),
1046                *p1,
1047                *s1,
1048                *p2,
1049                *s2,
1050                cast_options,
1051            )
1052        }
1053        (Decimal128(p1, s1), Decimal256(p2, s2)) => {
1054            cast_decimal_to_decimal::<Decimal128Type, Decimal256Type>(
1055                array.as_primitive(),
1056                *p1,
1057                *s1,
1058                *p2,
1059                *s2,
1060                cast_options,
1061            )
1062        }
1063        (Decimal256(p1, s1), Decimal32(p2, s2)) => {
1064            cast_decimal_to_decimal::<Decimal256Type, Decimal32Type>(
1065                array.as_primitive(),
1066                *p1,
1067                *s1,
1068                *p2,
1069                *s2,
1070                cast_options,
1071            )
1072        }
1073        (Decimal256(p1, s1), Decimal64(p2, s2)) => {
1074            cast_decimal_to_decimal::<Decimal256Type, Decimal64Type>(
1075                array.as_primitive(),
1076                *p1,
1077                *s1,
1078                *p2,
1079                *s2,
1080                cast_options,
1081            )
1082        }
1083        (Decimal256(p1, s1), Decimal128(p2, s2)) => {
1084            cast_decimal_to_decimal::<Decimal256Type, Decimal128Type>(
1085                array.as_primitive(),
1086                *p1,
1087                *s1,
1088                *p2,
1089                *s2,
1090                cast_options,
1091            )
1092        }
1093        // Decimal to non-decimal
1094        (Decimal32(_, scale), _) if !to_type.is_temporal() => {
1095            cast_from_decimal::<Decimal32Type, _>(
1096                array,
1097                10_i32,
1098                scale,
1099                from_type,
1100                to_type,
1101                |x: i32| x as f64,
1102                cast_options,
1103            )
1104        }
1105        (Decimal64(_, scale), _) if !to_type.is_temporal() => {
1106            cast_from_decimal::<Decimal64Type, _>(
1107                array,
1108                10_i64,
1109                scale,
1110                from_type,
1111                to_type,
1112                |x: i64| x as f64,
1113                cast_options,
1114            )
1115        }
1116        (Decimal128(_, scale), _) if !to_type.is_temporal() => {
1117            cast_from_decimal::<Decimal128Type, _>(
1118                array,
1119                10_i128,
1120                scale,
1121                from_type,
1122                to_type,
1123                |x: i128| x as f64,
1124                cast_options,
1125            )
1126        }
1127        (Decimal256(_, scale), _) if !to_type.is_temporal() => {
1128            cast_from_decimal::<Decimal256Type, _>(
1129                array,
1130                i256::from_i128(10_i128),
1131                scale,
1132                from_type,
1133                to_type,
1134                |x: i256| x.to_f64().expect("All i256 values fit in f64"),
1135                cast_options,
1136            )
1137        }
1138        // Non-decimal to decimal
1139        (_, Decimal32(precision, scale)) if !from_type.is_temporal() => {
1140            cast_to_decimal::<Decimal32Type, _>(
1141                array,
1142                10_i32,
1143                precision,
1144                scale,
1145                from_type,
1146                to_type,
1147                cast_options,
1148            )
1149        }
1150        (_, Decimal64(precision, scale)) if !from_type.is_temporal() => {
1151            cast_to_decimal::<Decimal64Type, _>(
1152                array,
1153                10_i64,
1154                precision,
1155                scale,
1156                from_type,
1157                to_type,
1158                cast_options,
1159            )
1160        }
1161        (_, Decimal128(precision, scale)) if !from_type.is_temporal() => {
1162            cast_to_decimal::<Decimal128Type, _>(
1163                array,
1164                10_i128,
1165                precision,
1166                scale,
1167                from_type,
1168                to_type,
1169                cast_options,
1170            )
1171        }
1172        (_, Decimal256(precision, scale)) if !from_type.is_temporal() => {
1173            cast_to_decimal::<Decimal256Type, _>(
1174                array,
1175                i256::from_i128(10_i128),
1176                precision,
1177                scale,
1178                from_type,
1179                to_type,
1180                cast_options,
1181            )
1182        }
1183        (Struct(from_fields), Struct(to_fields)) => cast_struct_to_struct(
1184            array.as_struct(),
1185            from_fields.clone(),
1186            to_fields.clone(),
1187            cast_options,
1188        ),
1189        (Struct(_), _) => Err(ArrowError::CastError(format!(
1190            "Casting from {from_type} to {to_type} not supported"
1191        ))),
1192        (_, Struct(_)) => Err(ArrowError::CastError(format!(
1193            "Casting from {from_type} to {to_type} not supported"
1194        ))),
1195        (_, Boolean) => match from_type {
1196            UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
1197            UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
1198            UInt32 => cast_numeric_to_bool::<UInt32Type>(array),
1199            UInt64 => cast_numeric_to_bool::<UInt64Type>(array),
1200            Int8 => cast_numeric_to_bool::<Int8Type>(array),
1201            Int16 => cast_numeric_to_bool::<Int16Type>(array),
1202            Int32 => cast_numeric_to_bool::<Int32Type>(array),
1203            Int64 => cast_numeric_to_bool::<Int64Type>(array),
1204            Float16 => cast_numeric_to_bool::<Float16Type>(array),
1205            Float32 => cast_numeric_to_bool::<Float32Type>(array),
1206            Float64 => cast_numeric_to_bool::<Float64Type>(array),
1207            Utf8View => cast_utf8view_to_boolean(array, cast_options),
1208            Utf8 => cast_utf8_to_boolean::<i32>(array, cast_options),
1209            LargeUtf8 => cast_utf8_to_boolean::<i64>(array, cast_options),
1210            _ => Err(ArrowError::CastError(format!(
1211                "Casting from {from_type} to {to_type} not supported",
1212            ))),
1213        },
1214        (Boolean, _) => match to_type {
1215            UInt8 => cast_bool_to_numeric::<UInt8Type>(array, cast_options),
1216            UInt16 => cast_bool_to_numeric::<UInt16Type>(array, cast_options),
1217            UInt32 => cast_bool_to_numeric::<UInt32Type>(array, cast_options),
1218            UInt64 => cast_bool_to_numeric::<UInt64Type>(array, cast_options),
1219            Int8 => cast_bool_to_numeric::<Int8Type>(array, cast_options),
1220            Int16 => cast_bool_to_numeric::<Int16Type>(array, cast_options),
1221            Int32 => cast_bool_to_numeric::<Int32Type>(array, cast_options),
1222            Int64 => cast_bool_to_numeric::<Int64Type>(array, cast_options),
1223            Float16 => cast_bool_to_numeric::<Float16Type>(array, cast_options),
1224            Float32 => cast_bool_to_numeric::<Float32Type>(array, cast_options),
1225            Float64 => cast_bool_to_numeric::<Float64Type>(array, cast_options),
1226            Utf8View => value_to_string_view(array, cast_options),
1227            Utf8 => value_to_string::<i32>(array, cast_options),
1228            LargeUtf8 => value_to_string::<i64>(array, cast_options),
1229            _ => Err(ArrowError::CastError(format!(
1230                "Casting from {from_type} to {to_type} not supported",
1231            ))),
1232        },
1233        (Utf8, _) => match to_type {
1234            UInt8 => parse_string::<UInt8Type, i32>(array, cast_options),
1235            UInt16 => parse_string::<UInt16Type, i32>(array, cast_options),
1236            UInt32 => parse_string::<UInt32Type, i32>(array, cast_options),
1237            UInt64 => parse_string::<UInt64Type, i32>(array, cast_options),
1238            Int8 => parse_string::<Int8Type, i32>(array, cast_options),
1239            Int16 => parse_string::<Int16Type, i32>(array, cast_options),
1240            Int32 => parse_string::<Int32Type, i32>(array, cast_options),
1241            Int64 => parse_string::<Int64Type, i32>(array, cast_options),
1242            Float16 => parse_string::<Float16Type, i32>(array, cast_options),
1243            Float32 => parse_string::<Float32Type, i32>(array, cast_options),
1244            Float64 => parse_string::<Float64Type, i32>(array, cast_options),
1245            Date32 => parse_string::<Date32Type, i32>(array, cast_options),
1246            Date64 => parse_string::<Date64Type, i32>(array, cast_options),
1247            Binary => Ok(Arc::new(BinaryArray::from(
1248                array.as_string::<i32>().clone(),
1249            ))),
1250            LargeBinary => {
1251                let binary = BinaryArray::from(array.as_string::<i32>().clone());
1252                cast_byte_container::<BinaryType, LargeBinaryType>(&binary)
1253            }
1254            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i32>()))),
1255            BinaryView => Ok(Arc::new(
1256                StringViewArray::from(array.as_string::<i32>()).to_binary_view(),
1257            )),
1258            LargeUtf8 => cast_byte_container::<Utf8Type, LargeUtf8Type>(array),
1259            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i32>(array, cast_options),
1260            Time32(TimeUnit::Millisecond) => {
1261                parse_string::<Time32MillisecondType, i32>(array, cast_options)
1262            }
1263            Time64(TimeUnit::Microsecond) => {
1264                parse_string::<Time64MicrosecondType, i32>(array, cast_options)
1265            }
1266            Time64(TimeUnit::Nanosecond) => {
1267                parse_string::<Time64NanosecondType, i32>(array, cast_options)
1268            }
1269            Timestamp(TimeUnit::Second, to_tz) => {
1270                cast_string_to_timestamp::<i32, TimestampSecondType>(array, to_tz, cast_options)
1271            }
1272            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1273                i32,
1274                TimestampMillisecondType,
1275            >(array, to_tz, cast_options),
1276            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1277                i32,
1278                TimestampMicrosecondType,
1279            >(array, to_tz, cast_options),
1280            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1281                cast_string_to_timestamp::<i32, TimestampNanosecondType>(array, to_tz, cast_options)
1282            }
1283            Interval(IntervalUnit::YearMonth) => {
1284                cast_string_to_year_month_interval::<i32>(array, cast_options)
1285            }
1286            Interval(IntervalUnit::DayTime) => {
1287                cast_string_to_day_time_interval::<i32>(array, cast_options)
1288            }
1289            Interval(IntervalUnit::MonthDayNano) => {
1290                cast_string_to_month_day_nano_interval::<i32>(array, cast_options)
1291            }
1292            _ => Err(ArrowError::CastError(format!(
1293                "Casting from {from_type} to {to_type} not supported",
1294            ))),
1295        },
1296        (Utf8View, _) => match to_type {
1297            UInt8 => parse_string_view::<UInt8Type>(array, cast_options),
1298            UInt16 => parse_string_view::<UInt16Type>(array, cast_options),
1299            UInt32 => parse_string_view::<UInt32Type>(array, cast_options),
1300            UInt64 => parse_string_view::<UInt64Type>(array, cast_options),
1301            Int8 => parse_string_view::<Int8Type>(array, cast_options),
1302            Int16 => parse_string_view::<Int16Type>(array, cast_options),
1303            Int32 => parse_string_view::<Int32Type>(array, cast_options),
1304            Int64 => parse_string_view::<Int64Type>(array, cast_options),
1305            Float16 => parse_string_view::<Float16Type>(array, cast_options),
1306            Float32 => parse_string_view::<Float32Type>(array, cast_options),
1307            Float64 => parse_string_view::<Float64Type>(array, cast_options),
1308            Date32 => parse_string_view::<Date32Type>(array, cast_options),
1309            Date64 => parse_string_view::<Date64Type>(array, cast_options),
1310            Binary => cast_view_to_byte::<StringViewType, GenericBinaryType<i32>>(array),
1311            LargeBinary => cast_view_to_byte::<StringViewType, GenericBinaryType<i64>>(array),
1312            BinaryView => Ok(Arc::new(array.as_string_view().clone().to_binary_view())),
1313            Utf8 => cast_view_to_byte::<StringViewType, GenericStringType<i32>>(array),
1314            LargeUtf8 => cast_view_to_byte::<StringViewType, GenericStringType<i64>>(array),
1315            Time32(TimeUnit::Second) => parse_string_view::<Time32SecondType>(array, cast_options),
1316            Time32(TimeUnit::Millisecond) => {
1317                parse_string_view::<Time32MillisecondType>(array, cast_options)
1318            }
1319            Time64(TimeUnit::Microsecond) => {
1320                parse_string_view::<Time64MicrosecondType>(array, cast_options)
1321            }
1322            Time64(TimeUnit::Nanosecond) => {
1323                parse_string_view::<Time64NanosecondType>(array, cast_options)
1324            }
1325            Timestamp(TimeUnit::Second, to_tz) => {
1326                cast_view_to_timestamp::<TimestampSecondType>(array, to_tz, cast_options)
1327            }
1328            Timestamp(TimeUnit::Millisecond, to_tz) => {
1329                cast_view_to_timestamp::<TimestampMillisecondType>(array, to_tz, cast_options)
1330            }
1331            Timestamp(TimeUnit::Microsecond, to_tz) => {
1332                cast_view_to_timestamp::<TimestampMicrosecondType>(array, to_tz, cast_options)
1333            }
1334            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1335                cast_view_to_timestamp::<TimestampNanosecondType>(array, to_tz, cast_options)
1336            }
1337            Interval(IntervalUnit::YearMonth) => {
1338                cast_view_to_year_month_interval(array, cast_options)
1339            }
1340            Interval(IntervalUnit::DayTime) => cast_view_to_day_time_interval(array, cast_options),
1341            Interval(IntervalUnit::MonthDayNano) => {
1342                cast_view_to_month_day_nano_interval(array, cast_options)
1343            }
1344            _ => Err(ArrowError::CastError(format!(
1345                "Casting from {from_type} to {to_type} not supported",
1346            ))),
1347        },
1348        (LargeUtf8, _) => match to_type {
1349            UInt8 => parse_string::<UInt8Type, i64>(array, cast_options),
1350            UInt16 => parse_string::<UInt16Type, i64>(array, cast_options),
1351            UInt32 => parse_string::<UInt32Type, i64>(array, cast_options),
1352            UInt64 => parse_string::<UInt64Type, i64>(array, cast_options),
1353            Int8 => parse_string::<Int8Type, i64>(array, cast_options),
1354            Int16 => parse_string::<Int16Type, i64>(array, cast_options),
1355            Int32 => parse_string::<Int32Type, i64>(array, cast_options),
1356            Int64 => parse_string::<Int64Type, i64>(array, cast_options),
1357            Float16 => parse_string::<Float16Type, i64>(array, cast_options),
1358            Float32 => parse_string::<Float32Type, i64>(array, cast_options),
1359            Float64 => parse_string::<Float64Type, i64>(array, cast_options),
1360            Date32 => parse_string::<Date32Type, i64>(array, cast_options),
1361            Date64 => parse_string::<Date64Type, i64>(array, cast_options),
1362            Utf8 => cast_byte_container::<LargeUtf8Type, Utf8Type>(array),
1363            Binary => {
1364                let large_binary = LargeBinaryArray::from(array.as_string::<i64>().clone());
1365                cast_byte_container::<LargeBinaryType, BinaryType>(&large_binary)
1366            }
1367            LargeBinary => Ok(Arc::new(LargeBinaryArray::from(
1368                array.as_string::<i64>().clone(),
1369            ))),
1370            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i64>()))),
1371            BinaryView => Ok(Arc::new(BinaryViewArray::from(
1372                array
1373                    .as_string::<i64>()
1374                    .into_iter()
1375                    .map(|x| x.map(|x| x.as_bytes()))
1376                    .collect::<Vec<_>>(),
1377            ))),
1378            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i64>(array, cast_options),
1379            Time32(TimeUnit::Millisecond) => {
1380                parse_string::<Time32MillisecondType, i64>(array, cast_options)
1381            }
1382            Time64(TimeUnit::Microsecond) => {
1383                parse_string::<Time64MicrosecondType, i64>(array, cast_options)
1384            }
1385            Time64(TimeUnit::Nanosecond) => {
1386                parse_string::<Time64NanosecondType, i64>(array, cast_options)
1387            }
1388            Timestamp(TimeUnit::Second, to_tz) => {
1389                cast_string_to_timestamp::<i64, TimestampSecondType>(array, to_tz, cast_options)
1390            }
1391            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1392                i64,
1393                TimestampMillisecondType,
1394            >(array, to_tz, cast_options),
1395            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1396                i64,
1397                TimestampMicrosecondType,
1398            >(array, to_tz, cast_options),
1399            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1400                cast_string_to_timestamp::<i64, TimestampNanosecondType>(array, to_tz, cast_options)
1401            }
1402            Interval(IntervalUnit::YearMonth) => {
1403                cast_string_to_year_month_interval::<i64>(array, cast_options)
1404            }
1405            Interval(IntervalUnit::DayTime) => {
1406                cast_string_to_day_time_interval::<i64>(array, cast_options)
1407            }
1408            Interval(IntervalUnit::MonthDayNano) => {
1409                cast_string_to_month_day_nano_interval::<i64>(array, cast_options)
1410            }
1411            _ => Err(ArrowError::CastError(format!(
1412                "Casting from {from_type} to {to_type} not supported",
1413            ))),
1414        },
1415        (Binary, _) => match to_type {
1416            Utf8 => cast_binary_to_string::<i32>(array, cast_options),
1417            LargeUtf8 => {
1418                let array = cast_binary_to_string::<i32>(array, cast_options)?;
1419                cast_byte_container::<Utf8Type, LargeUtf8Type>(array.as_ref())
1420            }
1421            LargeBinary => cast_byte_container::<BinaryType, LargeBinaryType>(array),
1422            FixedSizeBinary(size) => {
1423                cast_binary_to_fixed_size_binary::<i32>(array, *size, cast_options)
1424            }
1425            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i32>()))),
1426            Utf8View => Ok(Arc::new(StringViewArray::from(
1427                cast_binary_to_string::<i32>(array, cast_options)?.as_string::<i32>(),
1428            ))),
1429            _ => Err(ArrowError::CastError(format!(
1430                "Casting from {from_type} to {to_type} not supported",
1431            ))),
1432        },
1433        (LargeBinary, _) => match to_type {
1434            Utf8 => {
1435                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1436                cast_byte_container::<LargeUtf8Type, Utf8Type>(array.as_ref())
1437            }
1438            LargeUtf8 => cast_binary_to_string::<i64>(array, cast_options),
1439            Binary => cast_byte_container::<LargeBinaryType, BinaryType>(array),
1440            FixedSizeBinary(size) => {
1441                cast_binary_to_fixed_size_binary::<i64>(array, *size, cast_options)
1442            }
1443            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i64>()))),
1444            Utf8View => {
1445                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1446                Ok(Arc::new(StringViewArray::from(array.as_string::<i64>())))
1447            }
1448            _ => Err(ArrowError::CastError(format!(
1449                "Casting from {from_type} to {to_type} not supported",
1450            ))),
1451        },
1452        (FixedSizeBinary(size), _) => match to_type {
1453            Binary => cast_fixed_size_binary_to_binary::<i32>(array, *size),
1454            LargeBinary => cast_fixed_size_binary_to_binary::<i64>(array, *size),
1455            BinaryView => cast_fixed_size_binary_to_binary_view(array, *size),
1456            _ => Err(ArrowError::CastError(format!(
1457                "Casting from {from_type} to {to_type} not supported",
1458            ))),
1459        },
1460        (BinaryView, Binary) => cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array),
1461        (BinaryView, LargeBinary) => {
1462            cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)
1463        }
1464        (BinaryView, Utf8) => {
1465            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array)?;
1466            cast_binary_to_string::<i32>(&binary_arr, cast_options)
1467        }
1468        (BinaryView, LargeUtf8) => {
1469            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)?;
1470            cast_binary_to_string::<i64>(&binary_arr, cast_options)
1471        }
1472        (BinaryView, Utf8View) => cast_binary_view_to_string_view(array, cast_options),
1473        (BinaryView, _) => Err(ArrowError::CastError(format!(
1474            "Casting from {from_type} to {to_type} not supported",
1475        ))),
1476        (from_type, Utf8View) if from_type.is_primitive() => {
1477            value_to_string_view(array, cast_options)
1478        }
1479        (from_type, LargeUtf8) if from_type.is_primitive() => {
1480            value_to_string::<i64>(array, cast_options)
1481        }
1482        (from_type, Utf8) if from_type.is_primitive() => {
1483            value_to_string::<i32>(array, cast_options)
1484        }
1485        (from_type, Binary) if from_type.is_integer() => match from_type {
1486            UInt8 => cast_numeric_to_binary::<UInt8Type, i32>(array),
1487            UInt16 => cast_numeric_to_binary::<UInt16Type, i32>(array),
1488            UInt32 => cast_numeric_to_binary::<UInt32Type, i32>(array),
1489            UInt64 => cast_numeric_to_binary::<UInt64Type, i32>(array),
1490            Int8 => cast_numeric_to_binary::<Int8Type, i32>(array),
1491            Int16 => cast_numeric_to_binary::<Int16Type, i32>(array),
1492            Int32 => cast_numeric_to_binary::<Int32Type, i32>(array),
1493            Int64 => cast_numeric_to_binary::<Int64Type, i32>(array),
1494            _ => unreachable!(),
1495        },
1496        (from_type, LargeBinary) if from_type.is_integer() => match from_type {
1497            UInt8 => cast_numeric_to_binary::<UInt8Type, i64>(array),
1498            UInt16 => cast_numeric_to_binary::<UInt16Type, i64>(array),
1499            UInt32 => cast_numeric_to_binary::<UInt32Type, i64>(array),
1500            UInt64 => cast_numeric_to_binary::<UInt64Type, i64>(array),
1501            Int8 => cast_numeric_to_binary::<Int8Type, i64>(array),
1502            Int16 => cast_numeric_to_binary::<Int16Type, i64>(array),
1503            Int32 => cast_numeric_to_binary::<Int32Type, i64>(array),
1504            Int64 => cast_numeric_to_binary::<Int64Type, i64>(array),
1505            _ => unreachable!(),
1506        },
1507        // start numeric casts
1508        (UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array, cast_options),
1509        (UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array, cast_options),
1510        (UInt8, UInt64) => cast_numeric_arrays::<UInt8Type, UInt64Type>(array, cast_options),
1511        (UInt8, Int8) => cast_numeric_arrays::<UInt8Type, Int8Type>(array, cast_options),
1512        (UInt8, Int16) => cast_numeric_arrays::<UInt8Type, Int16Type>(array, cast_options),
1513        (UInt8, Int32) => cast_numeric_arrays::<UInt8Type, Int32Type>(array, cast_options),
1514        (UInt8, Int64) => cast_numeric_arrays::<UInt8Type, Int64Type>(array, cast_options),
1515        (UInt8, Float16) => cast_numeric_arrays::<UInt8Type, Float16Type>(array, cast_options),
1516        (UInt8, Float32) => cast_numeric_arrays::<UInt8Type, Float32Type>(array, cast_options),
1517        (UInt8, Float64) => cast_numeric_arrays::<UInt8Type, Float64Type>(array, cast_options),
1518
1519        (UInt16, UInt8) => cast_numeric_arrays::<UInt16Type, UInt8Type>(array, cast_options),
1520        (UInt16, UInt32) => cast_numeric_arrays::<UInt16Type, UInt32Type>(array, cast_options),
1521        (UInt16, UInt64) => cast_numeric_arrays::<UInt16Type, UInt64Type>(array, cast_options),
1522        (UInt16, Int8) => cast_numeric_arrays::<UInt16Type, Int8Type>(array, cast_options),
1523        (UInt16, Int16) => cast_numeric_arrays::<UInt16Type, Int16Type>(array, cast_options),
1524        (UInt16, Int32) => cast_numeric_arrays::<UInt16Type, Int32Type>(array, cast_options),
1525        (UInt16, Int64) => cast_numeric_arrays::<UInt16Type, Int64Type>(array, cast_options),
1526        (UInt16, Float16) => cast_numeric_arrays::<UInt16Type, Float16Type>(array, cast_options),
1527        (UInt16, Float32) => cast_numeric_arrays::<UInt16Type, Float32Type>(array, cast_options),
1528        (UInt16, Float64) => cast_numeric_arrays::<UInt16Type, Float64Type>(array, cast_options),
1529
1530        (UInt32, UInt8) => cast_numeric_arrays::<UInt32Type, UInt8Type>(array, cast_options),
1531        (UInt32, UInt16) => cast_numeric_arrays::<UInt32Type, UInt16Type>(array, cast_options),
1532        (UInt32, UInt64) => cast_numeric_arrays::<UInt32Type, UInt64Type>(array, cast_options),
1533        (UInt32, Int8) => cast_numeric_arrays::<UInt32Type, Int8Type>(array, cast_options),
1534        (UInt32, Int16) => cast_numeric_arrays::<UInt32Type, Int16Type>(array, cast_options),
1535        (UInt32, Int32) => cast_numeric_arrays::<UInt32Type, Int32Type>(array, cast_options),
1536        (UInt32, Int64) => cast_numeric_arrays::<UInt32Type, Int64Type>(array, cast_options),
1537        (UInt32, Float16) => cast_numeric_arrays::<UInt32Type, Float16Type>(array, cast_options),
1538        (UInt32, Float32) => cast_numeric_arrays::<UInt32Type, Float32Type>(array, cast_options),
1539        (UInt32, Float64) => cast_numeric_arrays::<UInt32Type, Float64Type>(array, cast_options),
1540
1541        (UInt64, UInt8) => cast_numeric_arrays::<UInt64Type, UInt8Type>(array, cast_options),
1542        (UInt64, UInt16) => cast_numeric_arrays::<UInt64Type, UInt16Type>(array, cast_options),
1543        (UInt64, UInt32) => cast_numeric_arrays::<UInt64Type, UInt32Type>(array, cast_options),
1544        (UInt64, Int8) => cast_numeric_arrays::<UInt64Type, Int8Type>(array, cast_options),
1545        (UInt64, Int16) => cast_numeric_arrays::<UInt64Type, Int16Type>(array, cast_options),
1546        (UInt64, Int32) => cast_numeric_arrays::<UInt64Type, Int32Type>(array, cast_options),
1547        (UInt64, Int64) => cast_numeric_arrays::<UInt64Type, Int64Type>(array, cast_options),
1548        (UInt64, Float16) => cast_numeric_arrays::<UInt64Type, Float16Type>(array, cast_options),
1549        (UInt64, Float32) => cast_numeric_arrays::<UInt64Type, Float32Type>(array, cast_options),
1550        (UInt64, Float64) => cast_numeric_arrays::<UInt64Type, Float64Type>(array, cast_options),
1551
1552        (Int8, UInt8) => cast_numeric_arrays::<Int8Type, UInt8Type>(array, cast_options),
1553        (Int8, UInt16) => cast_numeric_arrays::<Int8Type, UInt16Type>(array, cast_options),
1554        (Int8, UInt32) => cast_numeric_arrays::<Int8Type, UInt32Type>(array, cast_options),
1555        (Int8, UInt64) => cast_numeric_arrays::<Int8Type, UInt64Type>(array, cast_options),
1556        (Int8, Int16) => cast_numeric_arrays::<Int8Type, Int16Type>(array, cast_options),
1557        (Int8, Int32) => cast_numeric_arrays::<Int8Type, Int32Type>(array, cast_options),
1558        (Int8, Int64) => cast_numeric_arrays::<Int8Type, Int64Type>(array, cast_options),
1559        (Int8, Float16) => cast_numeric_arrays::<Int8Type, Float16Type>(array, cast_options),
1560        (Int8, Float32) => cast_numeric_arrays::<Int8Type, Float32Type>(array, cast_options),
1561        (Int8, Float64) => cast_numeric_arrays::<Int8Type, Float64Type>(array, cast_options),
1562
1563        (Int16, UInt8) => cast_numeric_arrays::<Int16Type, UInt8Type>(array, cast_options),
1564        (Int16, UInt16) => cast_numeric_arrays::<Int16Type, UInt16Type>(array, cast_options),
1565        (Int16, UInt32) => cast_numeric_arrays::<Int16Type, UInt32Type>(array, cast_options),
1566        (Int16, UInt64) => cast_numeric_arrays::<Int16Type, UInt64Type>(array, cast_options),
1567        (Int16, Int8) => cast_numeric_arrays::<Int16Type, Int8Type>(array, cast_options),
1568        (Int16, Int32) => cast_numeric_arrays::<Int16Type, Int32Type>(array, cast_options),
1569        (Int16, Int64) => cast_numeric_arrays::<Int16Type, Int64Type>(array, cast_options),
1570        (Int16, Float16) => cast_numeric_arrays::<Int16Type, Float16Type>(array, cast_options),
1571        (Int16, Float32) => cast_numeric_arrays::<Int16Type, Float32Type>(array, cast_options),
1572        (Int16, Float64) => cast_numeric_arrays::<Int16Type, Float64Type>(array, cast_options),
1573
1574        (Int32, UInt8) => cast_numeric_arrays::<Int32Type, UInt8Type>(array, cast_options),
1575        (Int32, UInt16) => cast_numeric_arrays::<Int32Type, UInt16Type>(array, cast_options),
1576        (Int32, UInt32) => cast_numeric_arrays::<Int32Type, UInt32Type>(array, cast_options),
1577        (Int32, UInt64) => cast_numeric_arrays::<Int32Type, UInt64Type>(array, cast_options),
1578        (Int32, Int8) => cast_numeric_arrays::<Int32Type, Int8Type>(array, cast_options),
1579        (Int32, Int16) => cast_numeric_arrays::<Int32Type, Int16Type>(array, cast_options),
1580        (Int32, Int64) => cast_numeric_arrays::<Int32Type, Int64Type>(array, cast_options),
1581        (Int32, Float16) => cast_numeric_arrays::<Int32Type, Float16Type>(array, cast_options),
1582        (Int32, Float32) => cast_numeric_arrays::<Int32Type, Float32Type>(array, cast_options),
1583        (Int32, Float64) => cast_numeric_arrays::<Int32Type, Float64Type>(array, cast_options),
1584
1585        (Int64, UInt8) => cast_numeric_arrays::<Int64Type, UInt8Type>(array, cast_options),
1586        (Int64, UInt16) => cast_numeric_arrays::<Int64Type, UInt16Type>(array, cast_options),
1587        (Int64, UInt32) => cast_numeric_arrays::<Int64Type, UInt32Type>(array, cast_options),
1588        (Int64, UInt64) => cast_numeric_arrays::<Int64Type, UInt64Type>(array, cast_options),
1589        (Int64, Int8) => cast_numeric_arrays::<Int64Type, Int8Type>(array, cast_options),
1590        (Int64, Int16) => cast_numeric_arrays::<Int64Type, Int16Type>(array, cast_options),
1591        (Int64, Int32) => cast_numeric_arrays::<Int64Type, Int32Type>(array, cast_options),
1592        (Int64, Float16) => cast_numeric_arrays::<Int64Type, Float16Type>(array, cast_options),
1593        (Int64, Float32) => cast_numeric_arrays::<Int64Type, Float32Type>(array, cast_options),
1594        (Int64, Float64) => cast_numeric_arrays::<Int64Type, Float64Type>(array, cast_options),
1595
1596        (Float16, UInt8) => cast_numeric_arrays::<Float16Type, UInt8Type>(array, cast_options),
1597        (Float16, UInt16) => cast_numeric_arrays::<Float16Type, UInt16Type>(array, cast_options),
1598        (Float16, UInt32) => cast_numeric_arrays::<Float16Type, UInt32Type>(array, cast_options),
1599        (Float16, UInt64) => cast_numeric_arrays::<Float16Type, UInt64Type>(array, cast_options),
1600        (Float16, Int8) => cast_numeric_arrays::<Float16Type, Int8Type>(array, cast_options),
1601        (Float16, Int16) => cast_numeric_arrays::<Float16Type, Int16Type>(array, cast_options),
1602        (Float16, Int32) => cast_numeric_arrays::<Float16Type, Int32Type>(array, cast_options),
1603        (Float16, Int64) => cast_numeric_arrays::<Float16Type, Int64Type>(array, cast_options),
1604        (Float16, Float32) => cast_numeric_arrays::<Float16Type, Float32Type>(array, cast_options),
1605        (Float16, Float64) => cast_numeric_arrays::<Float16Type, Float64Type>(array, cast_options),
1606
1607        (Float32, UInt8) => cast_numeric_arrays::<Float32Type, UInt8Type>(array, cast_options),
1608        (Float32, UInt16) => cast_numeric_arrays::<Float32Type, UInt16Type>(array, cast_options),
1609        (Float32, UInt32) => cast_numeric_arrays::<Float32Type, UInt32Type>(array, cast_options),
1610        (Float32, UInt64) => cast_numeric_arrays::<Float32Type, UInt64Type>(array, cast_options),
1611        (Float32, Int8) => cast_numeric_arrays::<Float32Type, Int8Type>(array, cast_options),
1612        (Float32, Int16) => cast_numeric_arrays::<Float32Type, Int16Type>(array, cast_options),
1613        (Float32, Int32) => cast_numeric_arrays::<Float32Type, Int32Type>(array, cast_options),
1614        (Float32, Int64) => cast_numeric_arrays::<Float32Type, Int64Type>(array, cast_options),
1615        (Float32, Float16) => cast_numeric_arrays::<Float32Type, Float16Type>(array, cast_options),
1616        (Float32, Float64) => cast_numeric_arrays::<Float32Type, Float64Type>(array, cast_options),
1617
1618        (Float64, UInt8) => cast_numeric_arrays::<Float64Type, UInt8Type>(array, cast_options),
1619        (Float64, UInt16) => cast_numeric_arrays::<Float64Type, UInt16Type>(array, cast_options),
1620        (Float64, UInt32) => cast_numeric_arrays::<Float64Type, UInt32Type>(array, cast_options),
1621        (Float64, UInt64) => cast_numeric_arrays::<Float64Type, UInt64Type>(array, cast_options),
1622        (Float64, Int8) => cast_numeric_arrays::<Float64Type, Int8Type>(array, cast_options),
1623        (Float64, Int16) => cast_numeric_arrays::<Float64Type, Int16Type>(array, cast_options),
1624        (Float64, Int32) => cast_numeric_arrays::<Float64Type, Int32Type>(array, cast_options),
1625        (Float64, Int64) => cast_numeric_arrays::<Float64Type, Int64Type>(array, cast_options),
1626        (Float64, Float16) => cast_numeric_arrays::<Float64Type, Float16Type>(array, cast_options),
1627        (Float64, Float32) => cast_numeric_arrays::<Float64Type, Float32Type>(array, cast_options),
1628        // end numeric casts
1629
1630        // temporal casts
1631        (Int32, Date32) => cast_reinterpret_arrays::<Int32Type, Date32Type>(array),
1632        (Int32, Date64) => cast_with_options(
1633            &cast_with_options(array, &Date32, cast_options)?,
1634            &Date64,
1635            cast_options,
1636        ),
1637        (Int32, Time32(TimeUnit::Second)) => {
1638            cast_reinterpret_arrays::<Int32Type, Time32SecondType>(array)
1639        }
1640        (Int32, Time32(TimeUnit::Millisecond)) => {
1641            cast_reinterpret_arrays::<Int32Type, Time32MillisecondType>(array)
1642        }
1643        // No support for microsecond/nanosecond with i32
1644        (Date32, Int32) => cast_reinterpret_arrays::<Date32Type, Int32Type>(array),
1645        (Date32, Int64) => cast_with_options(
1646            &cast_with_options(array, &Int32, cast_options)?,
1647            &Int64,
1648            cast_options,
1649        ),
1650        (Time32(TimeUnit::Second), Int32) => {
1651            cast_reinterpret_arrays::<Time32SecondType, Int32Type>(array)
1652        }
1653        (Time32(TimeUnit::Millisecond), Int32) => {
1654            cast_reinterpret_arrays::<Time32MillisecondType, Int32Type>(array)
1655        }
1656        (Time32(TimeUnit::Second), Int64) => cast_with_options(
1657            &cast_with_options(array, &Int32, cast_options)?,
1658            &Int64,
1659            cast_options,
1660        ),
1661        (Time32(TimeUnit::Millisecond), Int64) => cast_with_options(
1662            &cast_with_options(array, &Int32, cast_options)?,
1663            &Int64,
1664            cast_options,
1665        ),
1666        (Int64, Date64) => cast_reinterpret_arrays::<Int64Type, Date64Type>(array),
1667        (Int64, Date32) => cast_with_options(
1668            &cast_with_options(array, &Int32, cast_options)?,
1669            &Date32,
1670            cast_options,
1671        ),
1672        // No support for second/milliseconds with i64
1673        (Int64, Time64(TimeUnit::Microsecond)) => {
1674            cast_reinterpret_arrays::<Int64Type, Time64MicrosecondType>(array)
1675        }
1676        (Int64, Time64(TimeUnit::Nanosecond)) => {
1677            cast_reinterpret_arrays::<Int64Type, Time64NanosecondType>(array)
1678        }
1679
1680        (Date64, Int64) => cast_reinterpret_arrays::<Date64Type, Int64Type>(array),
1681        (Date64, Int32) => cast_with_options(
1682            &cast_with_options(array, &Int64, cast_options)?,
1683            &Int32,
1684            cast_options,
1685        ),
1686        (Time64(TimeUnit::Microsecond), Int64) => {
1687            cast_reinterpret_arrays::<Time64MicrosecondType, Int64Type>(array)
1688        }
1689        (Time64(TimeUnit::Nanosecond), Int64) => {
1690            cast_reinterpret_arrays::<Time64NanosecondType, Int64Type>(array)
1691        }
1692        (Date32, Date64) => Ok(Arc::new(
1693            array
1694                .as_primitive::<Date32Type>()
1695                .unary::<_, Date64Type>(|x| x as i64 * MILLISECONDS_IN_DAY),
1696        )),
1697        (Date64, Date32) => Ok(Arc::new(
1698            array
1699                .as_primitive::<Date64Type>()
1700                .unary::<_, Date32Type>(|x| (x / MILLISECONDS_IN_DAY) as i32),
1701        )),
1702
1703        (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1704            array
1705                .as_primitive::<Time32SecondType>()
1706                .unary::<_, Time32MillisecondType>(|x| x * MILLISECONDS as i32),
1707        )),
1708        (Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1709            array
1710                .as_primitive::<Time32SecondType>()
1711                .unary::<_, Time64MicrosecondType>(|x| x as i64 * MICROSECONDS),
1712        )),
1713        (Time32(TimeUnit::Second), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1714            array
1715                .as_primitive::<Time32SecondType>()
1716                .unary::<_, Time64NanosecondType>(|x| x as i64 * NANOSECONDS),
1717        )),
1718
1719        (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1720            array
1721                .as_primitive::<Time32MillisecondType>()
1722                .unary::<_, Time32SecondType>(|x| x / MILLISECONDS as i32),
1723        )),
1724        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1725            array
1726                .as_primitive::<Time32MillisecondType>()
1727                .unary::<_, Time64MicrosecondType>(|x| x as i64 * (MICROSECONDS / MILLISECONDS)),
1728        )),
1729        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1730            array
1731                .as_primitive::<Time32MillisecondType>()
1732                .unary::<_, Time64NanosecondType>(|x| x as i64 * (MICROSECONDS / NANOSECONDS)),
1733        )),
1734
1735        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1736            array
1737                .as_primitive::<Time64MicrosecondType>()
1738                .unary::<_, Time32SecondType>(|x| (x / MICROSECONDS) as i32),
1739        )),
1740        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1741            array
1742                .as_primitive::<Time64MicrosecondType>()
1743                .unary::<_, Time32MillisecondType>(|x| (x / (MICROSECONDS / MILLISECONDS)) as i32),
1744        )),
1745        (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1746            array
1747                .as_primitive::<Time64MicrosecondType>()
1748                .unary::<_, Time64NanosecondType>(|x| x * (NANOSECONDS / MICROSECONDS)),
1749        )),
1750
1751        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1752            array
1753                .as_primitive::<Time64NanosecondType>()
1754                .unary::<_, Time32SecondType>(|x| (x / NANOSECONDS) as i32),
1755        )),
1756        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1757            array
1758                .as_primitive::<Time64NanosecondType>()
1759                .unary::<_, Time32MillisecondType>(|x| (x / (NANOSECONDS / MILLISECONDS)) as i32),
1760        )),
1761        (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1762            array
1763                .as_primitive::<Time64NanosecondType>()
1764                .unary::<_, Time64MicrosecondType>(|x| x / (NANOSECONDS / MICROSECONDS)),
1765        )),
1766
1767        // Timestamp to integer/floating/decimals
1768        (Timestamp(TimeUnit::Second, _), _) if to_type.is_numeric() => {
1769            let array = cast_reinterpret_arrays::<TimestampSecondType, Int64Type>(array)?;
1770            cast_with_options(&array, to_type, cast_options)
1771        }
1772        (Timestamp(TimeUnit::Millisecond, _), _) if to_type.is_numeric() => {
1773            let array = cast_reinterpret_arrays::<TimestampMillisecondType, Int64Type>(array)?;
1774            cast_with_options(&array, to_type, cast_options)
1775        }
1776        (Timestamp(TimeUnit::Microsecond, _), _) if to_type.is_numeric() => {
1777            let array = cast_reinterpret_arrays::<TimestampMicrosecondType, Int64Type>(array)?;
1778            cast_with_options(&array, to_type, cast_options)
1779        }
1780        (Timestamp(TimeUnit::Nanosecond, _), _) if to_type.is_numeric() => {
1781            let array = cast_reinterpret_arrays::<TimestampNanosecondType, Int64Type>(array)?;
1782            cast_with_options(&array, to_type, cast_options)
1783        }
1784
1785        (_, Timestamp(unit, tz)) if from_type.is_numeric() => {
1786            let array = cast_with_options(array, &Int64, cast_options)?;
1787            Ok(make_timestamp_array(
1788                array.as_primitive(),
1789                *unit,
1790                tz.clone(),
1791            ))
1792        }
1793
1794        (Timestamp(from_unit, from_tz), Timestamp(to_unit, to_tz)) => {
1795            let array = cast_with_options(array, &Int64, cast_options)?;
1796            let time_array = array.as_primitive::<Int64Type>();
1797            let from_size = time_unit_multiple(from_unit);
1798            let to_size = time_unit_multiple(to_unit);
1799            // we either divide or multiply, depending on size of each unit
1800            // units are never the same when the types are the same
1801            let converted = match from_size.cmp(&to_size) {
1802                Ordering::Greater => {
1803                    let divisor = from_size / to_size;
1804                    time_array.unary::<_, Int64Type>(|o| o / divisor)
1805                }
1806                Ordering::Equal => time_array.clone(),
1807                Ordering::Less => {
1808                    let mul = to_size / from_size;
1809                    if cast_options.safe {
1810                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1811                    } else {
1812                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1813                    }
1814                }
1815            };
1816            // Normalize timezone
1817            let adjusted = match (from_tz, to_tz) {
1818                // Only this case needs to be adjusted because we're casting from
1819                // unknown time offset to some time offset, we want the time to be
1820                // unchanged.
1821                //
1822                // i.e. Timestamp('2001-01-01T00:00', None) -> Timestamp('2001-01-01T00:00', '+0700')
1823                (None, Some(to_tz)) => {
1824                    let to_tz: Tz = to_tz.parse()?;
1825                    match to_unit {
1826                        TimeUnit::Second => adjust_timestamp_to_timezone::<TimestampSecondType>(
1827                            converted,
1828                            &to_tz,
1829                            cast_options,
1830                        )?,
1831                        TimeUnit::Millisecond => adjust_timestamp_to_timezone::<
1832                            TimestampMillisecondType,
1833                        >(
1834                            converted, &to_tz, cast_options
1835                        )?,
1836                        TimeUnit::Microsecond => adjust_timestamp_to_timezone::<
1837                            TimestampMicrosecondType,
1838                        >(
1839                            converted, &to_tz, cast_options
1840                        )?,
1841                        TimeUnit::Nanosecond => adjust_timestamp_to_timezone::<
1842                            TimestampNanosecondType,
1843                        >(
1844                            converted, &to_tz, cast_options
1845                        )?,
1846                    }
1847                }
1848                _ => converted,
1849            };
1850            Ok(make_timestamp_array(&adjusted, *to_unit, to_tz.clone()))
1851        }
1852        (Timestamp(TimeUnit::Microsecond, _), Date32) => {
1853            timestamp_to_date32(array.as_primitive::<TimestampMicrosecondType>())
1854        }
1855        (Timestamp(TimeUnit::Millisecond, _), Date32) => {
1856            timestamp_to_date32(array.as_primitive::<TimestampMillisecondType>())
1857        }
1858        (Timestamp(TimeUnit::Second, _), Date32) => {
1859            timestamp_to_date32(array.as_primitive::<TimestampSecondType>())
1860        }
1861        (Timestamp(TimeUnit::Nanosecond, _), Date32) => {
1862            timestamp_to_date32(array.as_primitive::<TimestampNanosecondType>())
1863        }
1864        (Timestamp(TimeUnit::Second, _), Date64) => Ok(Arc::new(match cast_options.safe {
1865            true => {
1866                // change error to None
1867                array
1868                    .as_primitive::<TimestampSecondType>()
1869                    .unary_opt::<_, Date64Type>(|x| x.checked_mul(MILLISECONDS))
1870            }
1871            false => array
1872                .as_primitive::<TimestampSecondType>()
1873                .try_unary::<_, Date64Type, _>(|x| x.mul_checked(MILLISECONDS))?,
1874        })),
1875        (Timestamp(TimeUnit::Millisecond, _), Date64) => {
1876            cast_reinterpret_arrays::<TimestampMillisecondType, Date64Type>(array)
1877        }
1878        (Timestamp(TimeUnit::Microsecond, _), Date64) => Ok(Arc::new(
1879            array
1880                .as_primitive::<TimestampMicrosecondType>()
1881                .unary::<_, Date64Type>(|x| x / (MICROSECONDS / MILLISECONDS)),
1882        )),
1883        (Timestamp(TimeUnit::Nanosecond, _), Date64) => Ok(Arc::new(
1884            array
1885                .as_primitive::<TimestampNanosecondType>()
1886                .unary::<_, Date64Type>(|x| x / (NANOSECONDS / MILLISECONDS)),
1887        )),
1888        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Microsecond)) => {
1889            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1890            Ok(Arc::new(
1891                array
1892                    .as_primitive::<TimestampSecondType>()
1893                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1894                        Ok(time_to_time64us(as_time_res_with_timezone::<
1895                            TimestampSecondType,
1896                        >(x, tz)?))
1897                    })?,
1898            ))
1899        }
1900        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Nanosecond)) => {
1901            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1902            Ok(Arc::new(
1903                array
1904                    .as_primitive::<TimestampSecondType>()
1905                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1906                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1907                            TimestampSecondType,
1908                        >(x, tz)?))
1909                    })?,
1910            ))
1911        }
1912        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Microsecond)) => {
1913            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1914            Ok(Arc::new(
1915                array
1916                    .as_primitive::<TimestampMillisecondType>()
1917                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1918                        Ok(time_to_time64us(as_time_res_with_timezone::<
1919                            TimestampMillisecondType,
1920                        >(x, tz)?))
1921                    })?,
1922            ))
1923        }
1924        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Nanosecond)) => {
1925            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1926            Ok(Arc::new(
1927                array
1928                    .as_primitive::<TimestampMillisecondType>()
1929                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1930                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1931                            TimestampMillisecondType,
1932                        >(x, tz)?))
1933                    })?,
1934            ))
1935        }
1936        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Microsecond)) => {
1937            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1938            Ok(Arc::new(
1939                array
1940                    .as_primitive::<TimestampMicrosecondType>()
1941                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1942                        Ok(time_to_time64us(as_time_res_with_timezone::<
1943                            TimestampMicrosecondType,
1944                        >(x, tz)?))
1945                    })?,
1946            ))
1947        }
1948        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Nanosecond)) => {
1949            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1950            Ok(Arc::new(
1951                array
1952                    .as_primitive::<TimestampMicrosecondType>()
1953                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1954                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1955                            TimestampMicrosecondType,
1956                        >(x, tz)?))
1957                    })?,
1958            ))
1959        }
1960        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Microsecond)) => {
1961            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1962            Ok(Arc::new(
1963                array
1964                    .as_primitive::<TimestampNanosecondType>()
1965                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1966                        Ok(time_to_time64us(as_time_res_with_timezone::<
1967                            TimestampNanosecondType,
1968                        >(x, tz)?))
1969                    })?,
1970            ))
1971        }
1972        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Nanosecond)) => {
1973            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1974            Ok(Arc::new(
1975                array
1976                    .as_primitive::<TimestampNanosecondType>()
1977                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1978                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1979                            TimestampNanosecondType,
1980                        >(x, tz)?))
1981                    })?,
1982            ))
1983        }
1984        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Second)) => {
1985            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1986            Ok(Arc::new(
1987                array
1988                    .as_primitive::<TimestampSecondType>()
1989                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1990                        Ok(time_to_time32s(as_time_res_with_timezone::<
1991                            TimestampSecondType,
1992                        >(x, tz)?))
1993                    })?,
1994            ))
1995        }
1996        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Millisecond)) => {
1997            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1998            Ok(Arc::new(
1999                array
2000                    .as_primitive::<TimestampSecondType>()
2001                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2002                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2003                            TimestampSecondType,
2004                        >(x, tz)?))
2005                    })?,
2006            ))
2007        }
2008        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Second)) => {
2009            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2010            Ok(Arc::new(
2011                array
2012                    .as_primitive::<TimestampMillisecondType>()
2013                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2014                        Ok(time_to_time32s(as_time_res_with_timezone::<
2015                            TimestampMillisecondType,
2016                        >(x, tz)?))
2017                    })?,
2018            ))
2019        }
2020        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Millisecond)) => {
2021            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2022            Ok(Arc::new(
2023                array
2024                    .as_primitive::<TimestampMillisecondType>()
2025                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2026                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2027                            TimestampMillisecondType,
2028                        >(x, tz)?))
2029                    })?,
2030            ))
2031        }
2032        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Second)) => {
2033            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2034            Ok(Arc::new(
2035                array
2036                    .as_primitive::<TimestampMicrosecondType>()
2037                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2038                        Ok(time_to_time32s(as_time_res_with_timezone::<
2039                            TimestampMicrosecondType,
2040                        >(x, tz)?))
2041                    })?,
2042            ))
2043        }
2044        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Millisecond)) => {
2045            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2046            Ok(Arc::new(
2047                array
2048                    .as_primitive::<TimestampMicrosecondType>()
2049                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2050                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2051                            TimestampMicrosecondType,
2052                        >(x, tz)?))
2053                    })?,
2054            ))
2055        }
2056        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Second)) => {
2057            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2058            Ok(Arc::new(
2059                array
2060                    .as_primitive::<TimestampNanosecondType>()
2061                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2062                        Ok(time_to_time32s(as_time_res_with_timezone::<
2063                            TimestampNanosecondType,
2064                        >(x, tz)?))
2065                    })?,
2066            ))
2067        }
2068        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Millisecond)) => {
2069            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2070            Ok(Arc::new(
2071                array
2072                    .as_primitive::<TimestampNanosecondType>()
2073                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2074                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2075                            TimestampNanosecondType,
2076                        >(x, tz)?))
2077                    })?,
2078            ))
2079        }
2080        (Date64, Timestamp(TimeUnit::Second, _)) => {
2081            let array = array
2082                .as_primitive::<Date64Type>()
2083                .unary::<_, TimestampSecondType>(|x| x / MILLISECONDS);
2084
2085            cast_with_options(&array, to_type, cast_options)
2086        }
2087        (Date64, Timestamp(TimeUnit::Millisecond, _)) => {
2088            let array = array
2089                .as_primitive::<Date64Type>()
2090                .reinterpret_cast::<TimestampMillisecondType>();
2091
2092            cast_with_options(&array, to_type, cast_options)
2093        }
2094
2095        (Date64, Timestamp(TimeUnit::Microsecond, _)) => {
2096            let array = array
2097                .as_primitive::<Date64Type>()
2098                .unary::<_, TimestampMicrosecondType>(|x| x * (MICROSECONDS / MILLISECONDS));
2099
2100            cast_with_options(&array, to_type, cast_options)
2101        }
2102        (Date64, Timestamp(TimeUnit::Nanosecond, _)) => {
2103            let array = array
2104                .as_primitive::<Date64Type>()
2105                .unary::<_, TimestampNanosecondType>(|x| x * (NANOSECONDS / MILLISECONDS));
2106
2107            cast_with_options(&array, to_type, cast_options)
2108        }
2109        (Date32, Timestamp(TimeUnit::Second, _)) => {
2110            let array = array
2111                .as_primitive::<Date32Type>()
2112                .unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY);
2113
2114            cast_with_options(&array, to_type, cast_options)
2115        }
2116        (Date32, Timestamp(TimeUnit::Millisecond, _)) => {
2117            let array = array
2118                .as_primitive::<Date32Type>()
2119                .unary::<_, TimestampMillisecondType>(|x| (x as i64) * MILLISECONDS_IN_DAY);
2120
2121            cast_with_options(&array, to_type, cast_options)
2122        }
2123        (Date32, Timestamp(TimeUnit::Microsecond, _)) => {
2124            let array = array
2125                .as_primitive::<Date32Type>()
2126                .unary::<_, TimestampMicrosecondType>(|x| (x as i64) * MICROSECONDS_IN_DAY);
2127
2128            cast_with_options(&array, to_type, cast_options)
2129        }
2130        (Date32, Timestamp(TimeUnit::Nanosecond, _)) => {
2131            let array = array
2132                .as_primitive::<Date32Type>()
2133                .unary::<_, TimestampNanosecondType>(|x| (x as i64) * NANOSECONDS_IN_DAY);
2134
2135            cast_with_options(&array, to_type, cast_options)
2136        }
2137
2138        (_, Duration(unit)) if from_type.is_numeric() => {
2139            let array = cast_with_options(array, &Int64, cast_options)?;
2140            Ok(make_duration_array(array.as_primitive(), *unit))
2141        }
2142        (Duration(TimeUnit::Second), _) if to_type.is_numeric() => {
2143            let array = cast_reinterpret_arrays::<DurationSecondType, Int64Type>(array)?;
2144            cast_with_options(&array, to_type, cast_options)
2145        }
2146        (Duration(TimeUnit::Millisecond), _) if to_type.is_numeric() => {
2147            let array = cast_reinterpret_arrays::<DurationMillisecondType, Int64Type>(array)?;
2148            cast_with_options(&array, to_type, cast_options)
2149        }
2150        (Duration(TimeUnit::Microsecond), _) if to_type.is_numeric() => {
2151            let array = cast_reinterpret_arrays::<DurationMicrosecondType, Int64Type>(array)?;
2152            cast_with_options(&array, to_type, cast_options)
2153        }
2154        (Duration(TimeUnit::Nanosecond), _) if to_type.is_numeric() => {
2155            let array = cast_reinterpret_arrays::<DurationNanosecondType, Int64Type>(array)?;
2156            cast_with_options(&array, to_type, cast_options)
2157        }
2158
2159        (Duration(from_unit), Duration(to_unit)) => {
2160            let array = cast_with_options(array, &Int64, cast_options)?;
2161            let time_array = array.as_primitive::<Int64Type>();
2162            let from_size = time_unit_multiple(from_unit);
2163            let to_size = time_unit_multiple(to_unit);
2164            // we either divide or multiply, depending on size of each unit
2165            // units are never the same when the types are the same
2166            let converted = match from_size.cmp(&to_size) {
2167                Ordering::Greater => {
2168                    let divisor = from_size / to_size;
2169                    time_array.unary::<_, Int64Type>(|o| o / divisor)
2170                }
2171                Ordering::Equal => time_array.clone(),
2172                Ordering::Less => {
2173                    let mul = to_size / from_size;
2174                    if cast_options.safe {
2175                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
2176                    } else {
2177                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
2178                    }
2179                }
2180            };
2181            Ok(make_duration_array(&converted, *to_unit))
2182        }
2183
2184        (Duration(TimeUnit::Second), Interval(IntervalUnit::MonthDayNano)) => {
2185            cast_duration_to_interval::<DurationSecondType>(array, cast_options)
2186        }
2187        (Duration(TimeUnit::Millisecond), Interval(IntervalUnit::MonthDayNano)) => {
2188            cast_duration_to_interval::<DurationMillisecondType>(array, cast_options)
2189        }
2190        (Duration(TimeUnit::Microsecond), Interval(IntervalUnit::MonthDayNano)) => {
2191            cast_duration_to_interval::<DurationMicrosecondType>(array, cast_options)
2192        }
2193        (Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano)) => {
2194            cast_duration_to_interval::<DurationNanosecondType>(array, cast_options)
2195        }
2196        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Second)) => {
2197            cast_month_day_nano_to_duration::<DurationSecondType>(array, cast_options)
2198        }
2199        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Millisecond)) => {
2200            cast_month_day_nano_to_duration::<DurationMillisecondType>(array, cast_options)
2201        }
2202        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Microsecond)) => {
2203            cast_month_day_nano_to_duration::<DurationMicrosecondType>(array, cast_options)
2204        }
2205        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Nanosecond)) => {
2206            cast_month_day_nano_to_duration::<DurationNanosecondType>(array, cast_options)
2207        }
2208        (Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
2209            cast_interval_year_month_to_interval_month_day_nano(array, cast_options)
2210        }
2211        (Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
2212            cast_interval_day_time_to_interval_month_day_nano(array, cast_options)
2213        }
2214        (Int32, Interval(IntervalUnit::YearMonth)) => {
2215            cast_reinterpret_arrays::<Int32Type, IntervalYearMonthType>(array)
2216        }
2217        (_, _) => Err(ArrowError::CastError(format!(
2218            "Casting from {from_type} to {to_type} not supported",
2219        ))),
2220    }
2221}
2222
2223fn cast_struct_to_struct(
2224    array: &StructArray,
2225    from_fields: Fields,
2226    to_fields: Fields,
2227    cast_options: &CastOptions,
2228) -> Result<ArrayRef, ArrowError> {
2229    // Fast path: if field names are in the same order, we can just zip and cast
2230    let fields_match_order = from_fields.len() == to_fields.len()
2231        && from_fields
2232            .iter()
2233            .zip(to_fields.iter())
2234            .all(|(f1, f2)| f1.name() == f2.name());
2235
2236    let fields = if fields_match_order {
2237        // Fast path: cast columns in order if their names match
2238        cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2239    } else {
2240        let all_fields_match_by_name = to_fields.iter().all(|to_field| {
2241            from_fields
2242                .iter()
2243                .any(|from_field| from_field.name() == to_field.name())
2244        });
2245
2246        if all_fields_match_by_name {
2247            // Slow path: match fields by name and reorder
2248            cast_struct_fields_by_name(array, from_fields.clone(), to_fields.clone(), cast_options)?
2249        } else {
2250            // Fallback: cast field by field in order
2251            cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2252        }
2253    };
2254
2255    let array = StructArray::try_new(to_fields.clone(), fields, array.nulls().cloned())?;
2256    Ok(Arc::new(array) as ArrayRef)
2257}
2258
2259fn cast_struct_fields_by_name(
2260    array: &StructArray,
2261    from_fields: Fields,
2262    to_fields: Fields,
2263    cast_options: &CastOptions,
2264) -> Result<Vec<ArrayRef>, ArrowError> {
2265    to_fields
2266        .iter()
2267        .map(|to_field| {
2268            let from_field_idx = from_fields
2269                .iter()
2270                .position(|from_field| from_field.name() == to_field.name())
2271                .unwrap(); // safe because we checked above
2272            let column = array.column(from_field_idx);
2273            cast_with_options(column, to_field.data_type(), cast_options)
2274        })
2275        .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2276}
2277
2278fn cast_struct_fields_in_order(
2279    array: &StructArray,
2280    to_fields: Fields,
2281    cast_options: &CastOptions,
2282) -> Result<Vec<ArrayRef>, ArrowError> {
2283    array
2284        .columns()
2285        .iter()
2286        .zip(to_fields.iter())
2287        .map(|(l, field)| cast_with_options(l, field.data_type(), cast_options))
2288        .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2289}
2290
2291fn cast_from_decimal<D, F>(
2292    array: &dyn Array,
2293    base: D::Native,
2294    scale: &i8,
2295    from_type: &DataType,
2296    to_type: &DataType,
2297    as_float: F,
2298    cast_options: &CastOptions,
2299) -> Result<ArrayRef, ArrowError>
2300where
2301    D: DecimalType + ArrowPrimitiveType,
2302    <D as ArrowPrimitiveType>::Native: ToPrimitive,
2303    F: Fn(D::Native) -> f64,
2304{
2305    use DataType::*;
2306    // cast decimal to other type
2307    match to_type {
2308        UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, base, *scale, cast_options),
2309        UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, base, *scale, cast_options),
2310        UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, base, *scale, cast_options),
2311        UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, base, *scale, cast_options),
2312        Int8 => cast_decimal_to_integer::<D, Int8Type>(array, base, *scale, cast_options),
2313        Int16 => cast_decimal_to_integer::<D, Int16Type>(array, base, *scale, cast_options),
2314        Int32 => cast_decimal_to_integer::<D, Int32Type>(array, base, *scale, cast_options),
2315        Int64 => cast_decimal_to_integer::<D, Int64Type>(array, base, *scale, cast_options),
2316        Float32 => cast_decimal_to_float::<D, Float32Type, _>(array, |x| {
2317            (as_float(x) / 10_f64.powi(*scale as i32)) as f32
2318        }),
2319        Float64 => cast_decimal_to_float::<D, Float64Type, _>(array, |x| {
2320            as_float(x) / 10_f64.powi(*scale as i32)
2321        }),
2322        Utf8View => value_to_string_view(array, cast_options),
2323        Utf8 => value_to_string::<i32>(array, cast_options),
2324        LargeUtf8 => value_to_string::<i64>(array, cast_options),
2325        Null => Ok(new_null_array(to_type, array.len())),
2326        _ => Err(ArrowError::CastError(format!(
2327            "Casting from {from_type} to {to_type} not supported"
2328        ))),
2329    }
2330}
2331
2332fn cast_to_decimal<D, M>(
2333    array: &dyn Array,
2334    base: M,
2335    precision: &u8,
2336    scale: &i8,
2337    from_type: &DataType,
2338    to_type: &DataType,
2339    cast_options: &CastOptions,
2340) -> Result<ArrayRef, ArrowError>
2341where
2342    D: DecimalType + ArrowPrimitiveType<Native = M>,
2343    M: ArrowNativeTypeOp + DecimalCast,
2344    u8: num_traits::AsPrimitive<M>,
2345    u16: num_traits::AsPrimitive<M>,
2346    u32: num_traits::AsPrimitive<M>,
2347    u64: num_traits::AsPrimitive<M>,
2348    i8: num_traits::AsPrimitive<M>,
2349    i16: num_traits::AsPrimitive<M>,
2350    i32: num_traits::AsPrimitive<M>,
2351    i64: num_traits::AsPrimitive<M>,
2352{
2353    use DataType::*;
2354    // cast data to decimal
2355    match from_type {
2356        UInt8 => cast_integer_to_decimal::<_, D, M>(
2357            array.as_primitive::<UInt8Type>(),
2358            *precision,
2359            *scale,
2360            base,
2361            cast_options,
2362        ),
2363        UInt16 => cast_integer_to_decimal::<_, D, _>(
2364            array.as_primitive::<UInt16Type>(),
2365            *precision,
2366            *scale,
2367            base,
2368            cast_options,
2369        ),
2370        UInt32 => cast_integer_to_decimal::<_, D, _>(
2371            array.as_primitive::<UInt32Type>(),
2372            *precision,
2373            *scale,
2374            base,
2375            cast_options,
2376        ),
2377        UInt64 => cast_integer_to_decimal::<_, D, _>(
2378            array.as_primitive::<UInt64Type>(),
2379            *precision,
2380            *scale,
2381            base,
2382            cast_options,
2383        ),
2384        Int8 => cast_integer_to_decimal::<_, D, _>(
2385            array.as_primitive::<Int8Type>(),
2386            *precision,
2387            *scale,
2388            base,
2389            cast_options,
2390        ),
2391        Int16 => cast_integer_to_decimal::<_, D, _>(
2392            array.as_primitive::<Int16Type>(),
2393            *precision,
2394            *scale,
2395            base,
2396            cast_options,
2397        ),
2398        Int32 => cast_integer_to_decimal::<_, D, _>(
2399            array.as_primitive::<Int32Type>(),
2400            *precision,
2401            *scale,
2402            base,
2403            cast_options,
2404        ),
2405        Int64 => cast_integer_to_decimal::<_, D, _>(
2406            array.as_primitive::<Int64Type>(),
2407            *precision,
2408            *scale,
2409            base,
2410            cast_options,
2411        ),
2412        Float32 => cast_floating_point_to_decimal::<_, D>(
2413            array.as_primitive::<Float32Type>(),
2414            *precision,
2415            *scale,
2416            cast_options,
2417        ),
2418        Float64 => cast_floating_point_to_decimal::<_, D>(
2419            array.as_primitive::<Float64Type>(),
2420            *precision,
2421            *scale,
2422            cast_options,
2423        ),
2424        Utf8View | Utf8 => {
2425            cast_string_to_decimal::<D, i32>(array, *precision, *scale, cast_options)
2426        }
2427        LargeUtf8 => cast_string_to_decimal::<D, i64>(array, *precision, *scale, cast_options),
2428        Null => Ok(new_null_array(to_type, array.len())),
2429        _ => Err(ArrowError::CastError(format!(
2430            "Casting from {from_type} to {to_type} not supported"
2431        ))),
2432    }
2433}
2434
2435/// Get the time unit as a multiple of a second
2436const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
2437    match unit {
2438        TimeUnit::Second => 1,
2439        TimeUnit::Millisecond => MILLISECONDS,
2440        TimeUnit::Microsecond => MICROSECONDS,
2441        TimeUnit::Nanosecond => NANOSECONDS,
2442    }
2443}
2444
2445/// Convert Array into a PrimitiveArray of type, and apply numeric cast
2446fn cast_numeric_arrays<FROM, TO>(
2447    from: &dyn Array,
2448    cast_options: &CastOptions,
2449) -> Result<ArrayRef, ArrowError>
2450where
2451    FROM: ArrowPrimitiveType,
2452    TO: ArrowPrimitiveType,
2453    FROM::Native: NumCast,
2454    TO::Native: NumCast,
2455{
2456    if cast_options.safe {
2457        // If the value can't be casted to the `TO::Native`, return null
2458        Ok(Arc::new(numeric_cast::<FROM, TO>(
2459            from.as_primitive::<FROM>(),
2460        )))
2461    } else {
2462        // If the value can't be casted to the `TO::Native`, return error
2463        Ok(Arc::new(try_numeric_cast::<FROM, TO>(
2464            from.as_primitive::<FROM>(),
2465        )?))
2466    }
2467}
2468
2469// Natural cast between numeric types
2470// If the value of T can't be casted to R, will throw error
2471fn try_numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>, ArrowError>
2472where
2473    T: ArrowPrimitiveType,
2474    R: ArrowPrimitiveType,
2475    T::Native: NumCast,
2476    R::Native: NumCast,
2477{
2478    from.try_unary(|value| {
2479        num_cast::<T::Native, R::Native>(value).ok_or_else(|| {
2480            ArrowError::CastError(format!(
2481                "Can't cast value {:?} to type {}",
2482                value,
2483                R::DATA_TYPE
2484            ))
2485        })
2486    })
2487}
2488
2489/// Natural cast between numeric types
2490/// Return None if the input `value` can't be casted to type `O`.
2491#[inline]
2492pub fn num_cast<I, O>(value: I) -> Option<O>
2493where
2494    I: NumCast,
2495    O: NumCast,
2496{
2497    num_traits::cast::cast::<I, O>(value)
2498}
2499
2500// Natural cast between numeric types
2501// If the value of T can't be casted to R, it will be converted to null
2502fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> PrimitiveArray<R>
2503where
2504    T: ArrowPrimitiveType,
2505    R: ArrowPrimitiveType,
2506    T::Native: NumCast,
2507    R::Native: NumCast,
2508{
2509    from.unary_opt::<_, R>(num_cast::<T::Native, R::Native>)
2510}
2511
2512fn cast_numeric_to_binary<FROM: ArrowPrimitiveType, O: OffsetSizeTrait>(
2513    array: &dyn Array,
2514) -> Result<ArrayRef, ArrowError> {
2515    let array = array.as_primitive::<FROM>();
2516    let size = std::mem::size_of::<FROM::Native>();
2517    let offsets = OffsetBuffer::from_repeated_length(size, array.len());
2518    Ok(Arc::new(GenericBinaryArray::<O>::try_new(
2519        offsets,
2520        array.values().inner().clone(),
2521        array.nulls().cloned(),
2522    )?))
2523}
2524
2525fn adjust_timestamp_to_timezone<T: ArrowTimestampType>(
2526    array: PrimitiveArray<Int64Type>,
2527    to_tz: &Tz,
2528    cast_options: &CastOptions,
2529) -> Result<PrimitiveArray<Int64Type>, ArrowError> {
2530    let adjust = |o| {
2531        let local = as_datetime::<T>(o)?;
2532        let offset = to_tz.offset_from_local_datetime(&local).single()?;
2533        T::from_naive_datetime(local - offset.fix(), None)
2534    };
2535    let adjusted = if cast_options.safe {
2536        array.unary_opt::<_, Int64Type>(adjust)
2537    } else {
2538        array.try_unary::<_, Int64Type, _>(|o| {
2539            adjust(o).ok_or_else(|| {
2540                ArrowError::CastError("Cannot cast timezone to different timezone".to_string())
2541            })
2542        })?
2543    };
2544    Ok(adjusted)
2545}
2546
2547/// Cast numeric types to Boolean
2548///
2549/// Any zero value returns `false` while non-zero returns `true`
2550fn cast_numeric_to_bool<FROM>(from: &dyn Array) -> Result<ArrayRef, ArrowError>
2551where
2552    FROM: ArrowPrimitiveType,
2553{
2554    numeric_to_bool_cast::<FROM>(from.as_primitive::<FROM>()).map(|to| Arc::new(to) as ArrayRef)
2555}
2556
2557fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray, ArrowError>
2558where
2559    T: ArrowPrimitiveType + ArrowPrimitiveType,
2560{
2561    let mut b = BooleanBuilder::with_capacity(from.len());
2562
2563    for i in 0..from.len() {
2564        if from.is_null(i) {
2565            b.append_null();
2566        } else {
2567            b.append_value(cast_num_to_bool::<T::Native>(from.value(i)));
2568        }
2569    }
2570
2571    Ok(b.finish())
2572}
2573
2574/// Cast numeric types to boolean
2575#[inline]
2576pub fn cast_num_to_bool<I>(value: I) -> bool
2577where
2578    I: Default + PartialEq,
2579{
2580    value != I::default()
2581}
2582
2583/// Cast Boolean types to numeric
2584///
2585/// `false` returns 0 while `true` returns 1
2586fn cast_bool_to_numeric<TO>(
2587    from: &dyn Array,
2588    cast_options: &CastOptions,
2589) -> Result<ArrayRef, ArrowError>
2590where
2591    TO: ArrowPrimitiveType,
2592    TO::Native: num_traits::cast::NumCast,
2593{
2594    Ok(Arc::new(bool_to_numeric_cast::<TO>(
2595        from.as_any().downcast_ref::<BooleanArray>().unwrap(),
2596        cast_options,
2597    )))
2598}
2599
2600fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions) -> PrimitiveArray<T>
2601where
2602    T: ArrowPrimitiveType,
2603    T::Native: num_traits::NumCast,
2604{
2605    let iter = (0..from.len()).map(|i| {
2606        if from.is_null(i) {
2607            None
2608        } else {
2609            single_bool_to_numeric::<T::Native>(from.value(i))
2610        }
2611    });
2612    // Benefit:
2613    //     20% performance improvement
2614    // Soundness:
2615    //     The iterator is trustedLen because it comes from a Range
2616    unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
2617}
2618
2619/// Cast single bool value to numeric value.
2620#[inline]
2621pub fn single_bool_to_numeric<O>(value: bool) -> Option<O>
2622where
2623    O: num_traits::NumCast + Default,
2624{
2625    if value {
2626        // a workaround to cast a primitive to type O, infallible
2627        num_traits::cast::cast(1)
2628    } else {
2629        Some(O::default())
2630    }
2631}
2632
2633/// Helper function to cast from one `BinaryArray` or 'LargeBinaryArray' to 'FixedSizeBinaryArray'.
2634fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
2635    array: &dyn Array,
2636    byte_width: i32,
2637    cast_options: &CastOptions,
2638) -> Result<ArrayRef, ArrowError> {
2639    let array = array.as_binary::<O>();
2640    let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(), byte_width);
2641
2642    for i in 0..array.len() {
2643        if array.is_null(i) {
2644            builder.append_null();
2645        } else {
2646            match builder.append_value(array.value(i)) {
2647                Ok(_) => {}
2648                Err(e) => match cast_options.safe {
2649                    true => builder.append_null(),
2650                    false => return Err(e),
2651                },
2652            }
2653        }
2654    }
2655
2656    Ok(Arc::new(builder.finish()))
2657}
2658
2659/// Helper function to cast from 'FixedSizeBinaryArray' to one `BinaryArray` or 'LargeBinaryArray'.
2660/// If the target one is too large for the source array it will return an Error.
2661fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
2662    array: &dyn Array,
2663    byte_width: i32,
2664) -> Result<ArrayRef, ArrowError> {
2665    let array = array
2666        .as_any()
2667        .downcast_ref::<FixedSizeBinaryArray>()
2668        .unwrap();
2669
2670    let offsets: i128 = byte_width as i128 * array.len() as i128;
2671
2672    let is_binary = matches!(GenericBinaryType::<O>::DATA_TYPE, DataType::Binary);
2673    if is_binary && offsets > i32::MAX as i128 {
2674        return Err(ArrowError::ComputeError(
2675            "FixedSizeBinary array too large to cast to Binary array".to_string(),
2676        ));
2677    } else if !is_binary && offsets > i64::MAX as i128 {
2678        return Err(ArrowError::ComputeError(
2679            "FixedSizeBinary array too large to cast to LargeBinary array".to_string(),
2680        ));
2681    }
2682
2683    let mut builder = GenericBinaryBuilder::<O>::with_capacity(array.len(), array.len());
2684
2685    for i in 0..array.len() {
2686        if array.is_null(i) {
2687            builder.append_null();
2688        } else {
2689            builder.append_value(array.value(i));
2690        }
2691    }
2692
2693    Ok(Arc::new(builder.finish()))
2694}
2695
2696fn cast_fixed_size_binary_to_binary_view(
2697    array: &dyn Array,
2698    _byte_width: i32,
2699) -> Result<ArrayRef, ArrowError> {
2700    let array = array
2701        .as_any()
2702        .downcast_ref::<FixedSizeBinaryArray>()
2703        .unwrap();
2704
2705    let mut builder = BinaryViewBuilder::with_capacity(array.len());
2706    for i in 0..array.len() {
2707        if array.is_null(i) {
2708            builder.append_null();
2709        } else {
2710            builder.append_value(array.value(i));
2711        }
2712    }
2713
2714    Ok(Arc::new(builder.finish()))
2715}
2716
2717/// Helper function to cast from one `ByteArrayType` to another and vice versa.
2718/// If the target one (e.g., `LargeUtf8`) is too large for the source array it will return an Error.
2719fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2720where
2721    FROM: ByteArrayType,
2722    TO: ByteArrayType<Native = FROM::Native>,
2723    FROM::Offset: OffsetSizeTrait + ToPrimitive,
2724    TO::Offset: OffsetSizeTrait + NumCast,
2725{
2726    let data = array.to_data();
2727    assert_eq!(data.data_type(), &FROM::DATA_TYPE);
2728    let str_values_buf = data.buffers()[1].clone();
2729    let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
2730
2731    let mut offset_builder = BufferBuilder::<TO::Offset>::new(offsets.len());
2732    offsets
2733        .iter()
2734        .try_for_each::<_, Result<_, ArrowError>>(|offset| {
2735            let offset =
2736                <<TO as ByteArrayType>::Offset as NumCast>::from(*offset).ok_or_else(|| {
2737                    ArrowError::ComputeError(format!(
2738                        "{}{} array too large to cast to {}{} array",
2739                        FROM::Offset::PREFIX,
2740                        FROM::PREFIX,
2741                        TO::Offset::PREFIX,
2742                        TO::PREFIX
2743                    ))
2744                })?;
2745            offset_builder.append(offset);
2746            Ok(())
2747        })?;
2748
2749    let offset_buffer = offset_builder.finish();
2750
2751    let dtype = TO::DATA_TYPE;
2752
2753    let builder = ArrayData::builder(dtype)
2754        .offset(array.offset())
2755        .len(array.len())
2756        .add_buffer(offset_buffer)
2757        .add_buffer(str_values_buf)
2758        .nulls(data.nulls().cloned());
2759
2760    let array_data = unsafe { builder.build_unchecked() };
2761
2762    Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
2763}
2764
2765/// Helper function to cast from one `ByteViewType` array to `ByteArrayType` array.
2766fn cast_view_to_byte<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2767where
2768    FROM: ByteViewType,
2769    TO: ByteArrayType,
2770    FROM::Native: AsRef<TO::Native>,
2771{
2772    let data = array.to_data();
2773    let view_array = GenericByteViewArray::<FROM>::from(data);
2774
2775    let len = view_array.len();
2776    let bytes = view_array
2777        .views()
2778        .iter()
2779        .map(|v| ByteView::from(*v).length as usize)
2780        .sum::<usize>();
2781
2782    let mut byte_array_builder = GenericByteBuilder::<TO>::with_capacity(len, bytes);
2783
2784    for val in view_array.iter() {
2785        byte_array_builder.append_option(val);
2786    }
2787
2788    Ok(Arc::new(byte_array_builder.finish()))
2789}
2790
2791#[cfg(test)]
2792mod tests {
2793    use super::*;
2794    use DataType::*;
2795    use arrow_array::{Int64Array, RunArray, StringArray};
2796    use arrow_buffer::{Buffer, IntervalDayTime, NullBuffer};
2797    use arrow_buffer::{ScalarBuffer, i256};
2798    use arrow_schema::{DataType, Field};
2799    use chrono::NaiveDate;
2800    use half::f16;
2801    use std::sync::Arc;
2802
2803    #[derive(Clone)]
2804    struct DecimalCastTestConfig {
2805        input_prec: u8,
2806        input_scale: i8,
2807        input_repr: i128,
2808        output_prec: u8,
2809        output_scale: i8,
2810        expected_output_repr: Result<i128, String>, // the error variant can contain a string
2811                                                    // template where the "{}" will be
2812                                                    // replaced with the decimal type name
2813                                                    // (e.g. Decimal128)
2814    }
2815
2816    macro_rules! generate_cast_test_case {
2817        ($INPUT_ARRAY: expr, $OUTPUT_TYPE_ARRAY: ident, $OUTPUT_TYPE: expr, $OUTPUT_VALUES: expr) => {
2818            let output =
2819                $OUTPUT_TYPE_ARRAY::from($OUTPUT_VALUES).with_data_type($OUTPUT_TYPE.clone());
2820
2821            // assert cast type
2822            let input_array_type = $INPUT_ARRAY.data_type();
2823            assert!(can_cast_types(input_array_type, $OUTPUT_TYPE));
2824            let result = cast($INPUT_ARRAY, $OUTPUT_TYPE).unwrap();
2825            assert_eq!($OUTPUT_TYPE, result.data_type());
2826            assert_eq!(result.as_ref(), &output);
2827
2828            let cast_option = CastOptions {
2829                safe: false,
2830                format_options: FormatOptions::default(),
2831            };
2832            let result = cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap();
2833            assert_eq!($OUTPUT_TYPE, result.data_type());
2834            assert_eq!(result.as_ref(), &output);
2835        };
2836    }
2837
2838    fn run_decimal_cast_test_case<I, O>(t: DecimalCastTestConfig)
2839    where
2840        I: DecimalType,
2841        O: DecimalType,
2842        I::Native: DecimalCast,
2843        O::Native: DecimalCast,
2844    {
2845        let array = vec![I::Native::from_decimal(t.input_repr)];
2846        let array = array
2847            .into_iter()
2848            .collect::<PrimitiveArray<I>>()
2849            .with_precision_and_scale(t.input_prec, t.input_scale)
2850            .unwrap();
2851        let input_type = array.data_type();
2852        let output_type = O::TYPE_CONSTRUCTOR(t.output_prec, t.output_scale);
2853        assert!(can_cast_types(input_type, &output_type));
2854
2855        let options = CastOptions {
2856            safe: false,
2857            ..Default::default()
2858        };
2859        let result = cast_with_options(&array, &output_type, &options);
2860
2861        match t.expected_output_repr {
2862            Ok(v) => {
2863                let expected_array = vec![O::Native::from_decimal(v)];
2864                let expected_array = expected_array
2865                    .into_iter()
2866                    .collect::<PrimitiveArray<O>>()
2867                    .with_precision_and_scale(t.output_prec, t.output_scale)
2868                    .unwrap();
2869                assert_eq!(*result.unwrap(), expected_array);
2870            }
2871            Err(expected_output_message_template) => {
2872                assert!(result.is_err());
2873                let expected_error_message =
2874                    expected_output_message_template.replace("{}", O::PREFIX);
2875                assert_eq!(result.unwrap_err().to_string(), expected_error_message);
2876            }
2877        }
2878    }
2879
2880    fn create_decimal32_array(
2881        array: Vec<Option<i32>>,
2882        precision: u8,
2883        scale: i8,
2884    ) -> Result<Decimal32Array, ArrowError> {
2885        array
2886            .into_iter()
2887            .collect::<Decimal32Array>()
2888            .with_precision_and_scale(precision, scale)
2889    }
2890
2891    fn create_decimal64_array(
2892        array: Vec<Option<i64>>,
2893        precision: u8,
2894        scale: i8,
2895    ) -> Result<Decimal64Array, ArrowError> {
2896        array
2897            .into_iter()
2898            .collect::<Decimal64Array>()
2899            .with_precision_and_scale(precision, scale)
2900    }
2901
2902    fn create_decimal128_array(
2903        array: Vec<Option<i128>>,
2904        precision: u8,
2905        scale: i8,
2906    ) -> Result<Decimal128Array, ArrowError> {
2907        array
2908            .into_iter()
2909            .collect::<Decimal128Array>()
2910            .with_precision_and_scale(precision, scale)
2911    }
2912
2913    fn create_decimal256_array(
2914        array: Vec<Option<i256>>,
2915        precision: u8,
2916        scale: i8,
2917    ) -> Result<Decimal256Array, ArrowError> {
2918        array
2919            .into_iter()
2920            .collect::<Decimal256Array>()
2921            .with_precision_and_scale(precision, scale)
2922    }
2923
2924    #[test]
2925    #[cfg(not(feature = "force_validate"))]
2926    #[should_panic(
2927        expected = "Cannot cast to Decimal128(20, 3). Overflowing on 57896044618658097711785492504343953926634992332820282019728792003956564819967"
2928    )]
2929    fn test_cast_decimal_to_decimal_round_with_error() {
2930        // decimal256 to decimal128 overflow
2931        let array = vec![
2932            Some(i256::from_i128(1123454)),
2933            Some(i256::from_i128(2123456)),
2934            Some(i256::from_i128(-3123453)),
2935            Some(i256::from_i128(-3123456)),
2936            None,
2937            Some(i256::MAX),
2938            Some(i256::MIN),
2939        ];
2940        let input_decimal_array = create_decimal256_array(array, 76, 4).unwrap();
2941        let array = Arc::new(input_decimal_array) as ArrayRef;
2942        let input_type = DataType::Decimal256(76, 4);
2943        let output_type = DataType::Decimal128(20, 3);
2944        assert!(can_cast_types(&input_type, &output_type));
2945        generate_cast_test_case!(
2946            &array,
2947            Decimal128Array,
2948            &output_type,
2949            vec![
2950                Some(112345_i128),
2951                Some(212346_i128),
2952                Some(-312345_i128),
2953                Some(-312346_i128),
2954                None,
2955                None,
2956                None,
2957            ]
2958        );
2959    }
2960
2961    #[test]
2962    #[cfg(not(feature = "force_validate"))]
2963    fn test_cast_decimal_to_decimal_round() {
2964        let array = vec![
2965            Some(1123454),
2966            Some(2123456),
2967            Some(-3123453),
2968            Some(-3123456),
2969            None,
2970        ];
2971        let array = create_decimal128_array(array, 20, 4).unwrap();
2972        // decimal128 to decimal128
2973        let input_type = DataType::Decimal128(20, 4);
2974        let output_type = DataType::Decimal128(20, 3);
2975        assert!(can_cast_types(&input_type, &output_type));
2976        generate_cast_test_case!(
2977            &array,
2978            Decimal128Array,
2979            &output_type,
2980            vec![
2981                Some(112345_i128),
2982                Some(212346_i128),
2983                Some(-312345_i128),
2984                Some(-312346_i128),
2985                None
2986            ]
2987        );
2988
2989        // decimal128 to decimal256
2990        let input_type = DataType::Decimal128(20, 4);
2991        let output_type = DataType::Decimal256(20, 3);
2992        assert!(can_cast_types(&input_type, &output_type));
2993        generate_cast_test_case!(
2994            &array,
2995            Decimal256Array,
2996            &output_type,
2997            vec![
2998                Some(i256::from_i128(112345_i128)),
2999                Some(i256::from_i128(212346_i128)),
3000                Some(i256::from_i128(-312345_i128)),
3001                Some(i256::from_i128(-312346_i128)),
3002                None
3003            ]
3004        );
3005
3006        // decimal256
3007        let array = vec![
3008            Some(i256::from_i128(1123454)),
3009            Some(i256::from_i128(2123456)),
3010            Some(i256::from_i128(-3123453)),
3011            Some(i256::from_i128(-3123456)),
3012            None,
3013        ];
3014        let array = create_decimal256_array(array, 20, 4).unwrap();
3015
3016        // decimal256 to decimal256
3017        let input_type = DataType::Decimal256(20, 4);
3018        let output_type = DataType::Decimal256(20, 3);
3019        assert!(can_cast_types(&input_type, &output_type));
3020        generate_cast_test_case!(
3021            &array,
3022            Decimal256Array,
3023            &output_type,
3024            vec![
3025                Some(i256::from_i128(112345_i128)),
3026                Some(i256::from_i128(212346_i128)),
3027                Some(i256::from_i128(-312345_i128)),
3028                Some(i256::from_i128(-312346_i128)),
3029                None
3030            ]
3031        );
3032        // decimal256 to decimal128
3033        let input_type = DataType::Decimal256(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
3050    #[test]
3051    fn test_cast_decimal32_to_decimal32() {
3052        // test changing precision
3053        let input_type = DataType::Decimal32(9, 3);
3054        let output_type = DataType::Decimal32(9, 4);
3055        assert!(can_cast_types(&input_type, &output_type));
3056        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3057        let array = create_decimal32_array(array, 9, 3).unwrap();
3058        generate_cast_test_case!(
3059            &array,
3060            Decimal32Array,
3061            &output_type,
3062            vec![
3063                Some(11234560_i32),
3064                Some(21234560_i32),
3065                Some(31234560_i32),
3066                None
3067            ]
3068        );
3069        // negative test
3070        let array = vec![Some(123456), None];
3071        let array = create_decimal32_array(array, 9, 0).unwrap();
3072        let result_safe = cast(&array, &DataType::Decimal32(2, 2));
3073        assert!(result_safe.is_ok());
3074        let options = CastOptions {
3075            safe: false,
3076            ..Default::default()
3077        };
3078
3079        let result_unsafe = cast_with_options(&array, &DataType::Decimal32(2, 2), &options);
3080        assert_eq!(
3081            "Invalid argument error: 123456.00 is too large to store in a Decimal32 of precision 2. Max is 0.99",
3082            result_unsafe.unwrap_err().to_string()
3083        );
3084    }
3085
3086    #[test]
3087    fn test_cast_decimal64_to_decimal64() {
3088        // test changing precision
3089        let input_type = DataType::Decimal64(17, 3);
3090        let output_type = DataType::Decimal64(17, 4);
3091        assert!(can_cast_types(&input_type, &output_type));
3092        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3093        let array = create_decimal64_array(array, 17, 3).unwrap();
3094        generate_cast_test_case!(
3095            &array,
3096            Decimal64Array,
3097            &output_type,
3098            vec![
3099                Some(11234560_i64),
3100                Some(21234560_i64),
3101                Some(31234560_i64),
3102                None
3103            ]
3104        );
3105        // negative test
3106        let array = vec![Some(123456), None];
3107        let array = create_decimal64_array(array, 9, 0).unwrap();
3108        let result_safe = cast(&array, &DataType::Decimal64(2, 2));
3109        assert!(result_safe.is_ok());
3110        let options = CastOptions {
3111            safe: false,
3112            ..Default::default()
3113        };
3114
3115        let result_unsafe = cast_with_options(&array, &DataType::Decimal64(2, 2), &options);
3116        assert_eq!(
3117            "Invalid argument error: 123456.00 is too large to store in a Decimal64 of precision 2. Max is 0.99",
3118            result_unsafe.unwrap_err().to_string()
3119        );
3120    }
3121
3122    #[test]
3123    fn test_cast_decimal128_to_decimal128() {
3124        // test changing precision
3125        let input_type = DataType::Decimal128(20, 3);
3126        let output_type = DataType::Decimal128(20, 4);
3127        assert!(can_cast_types(&input_type, &output_type));
3128        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3129        let array = create_decimal128_array(array, 20, 3).unwrap();
3130        generate_cast_test_case!(
3131            &array,
3132            Decimal128Array,
3133            &output_type,
3134            vec![
3135                Some(11234560_i128),
3136                Some(21234560_i128),
3137                Some(31234560_i128),
3138                None
3139            ]
3140        );
3141        // negative test
3142        let array = vec![Some(123456), None];
3143        let array = create_decimal128_array(array, 10, 0).unwrap();
3144        let result_safe = cast(&array, &DataType::Decimal128(2, 2));
3145        assert!(result_safe.is_ok());
3146        let options = CastOptions {
3147            safe: false,
3148            ..Default::default()
3149        };
3150
3151        let result_unsafe = cast_with_options(&array, &DataType::Decimal128(2, 2), &options);
3152        assert_eq!(
3153            "Invalid argument error: 123456.00 is too large to store in a Decimal128 of precision 2. Max is 0.99",
3154            result_unsafe.unwrap_err().to_string()
3155        );
3156    }
3157
3158    #[test]
3159    fn test_cast_decimal32_to_decimal32_dict() {
3160        let p = 9;
3161        let s = 3;
3162        let input_type = DataType::Decimal32(p, s);
3163        let output_type = DataType::Dictionary(
3164            Box::new(DataType::Int32),
3165            Box::new(DataType::Decimal32(p, s)),
3166        );
3167        assert!(can_cast_types(&input_type, &output_type));
3168        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3169        let array = create_decimal32_array(array, p, s).unwrap();
3170        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3171        assert_eq!(cast_array.data_type(), &output_type);
3172    }
3173
3174    #[test]
3175    fn test_cast_decimal64_to_decimal64_dict() {
3176        let p = 15;
3177        let s = 3;
3178        let input_type = DataType::Decimal64(p, s);
3179        let output_type = DataType::Dictionary(
3180            Box::new(DataType::Int32),
3181            Box::new(DataType::Decimal64(p, s)),
3182        );
3183        assert!(can_cast_types(&input_type, &output_type));
3184        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3185        let array = create_decimal64_array(array, p, s).unwrap();
3186        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3187        assert_eq!(cast_array.data_type(), &output_type);
3188    }
3189
3190    #[test]
3191    fn test_cast_decimal128_to_decimal128_dict() {
3192        let p = 20;
3193        let s = 3;
3194        let input_type = DataType::Decimal128(p, s);
3195        let output_type = DataType::Dictionary(
3196            Box::new(DataType::Int32),
3197            Box::new(DataType::Decimal128(p, s)),
3198        );
3199        assert!(can_cast_types(&input_type, &output_type));
3200        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3201        let array = create_decimal128_array(array, p, s).unwrap();
3202        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3203        assert_eq!(cast_array.data_type(), &output_type);
3204    }
3205
3206    #[test]
3207    fn test_cast_decimal256_to_decimal256_dict() {
3208        let p = 20;
3209        let s = 3;
3210        let input_type = DataType::Decimal256(p, s);
3211        let output_type = DataType::Dictionary(
3212            Box::new(DataType::Int32),
3213            Box::new(DataType::Decimal256(p, s)),
3214        );
3215        assert!(can_cast_types(&input_type, &output_type));
3216        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3217        let array = create_decimal128_array(array, p, s).unwrap();
3218        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3219        assert_eq!(cast_array.data_type(), &output_type);
3220    }
3221
3222    #[test]
3223    fn test_cast_decimal32_to_decimal32_overflow() {
3224        let input_type = DataType::Decimal32(9, 3);
3225        let output_type = DataType::Decimal32(9, 9);
3226        assert!(can_cast_types(&input_type, &output_type));
3227
3228        let array = vec![Some(i32::MAX)];
3229        let array = create_decimal32_array(array, 9, 3).unwrap();
3230        let result = cast_with_options(
3231            &array,
3232            &output_type,
3233            &CastOptions {
3234                safe: false,
3235                format_options: FormatOptions::default(),
3236            },
3237        );
3238        assert_eq!(
3239            "Cast error: Cannot cast to Decimal32(9, 9). Overflowing on 2147483647",
3240            result.unwrap_err().to_string()
3241        );
3242    }
3243
3244    #[test]
3245    fn test_cast_decimal32_to_decimal32_large_scale_reduction() {
3246        let array = vec![Some(-999999999), Some(0), Some(999999999), None];
3247        let array = create_decimal32_array(array, 9, 3).unwrap();
3248
3249        // Divide out all digits of precision -- rounding could still produce +/- 1
3250        let output_type = DataType::Decimal32(9, -6);
3251        assert!(can_cast_types(array.data_type(), &output_type));
3252        generate_cast_test_case!(
3253            &array,
3254            Decimal32Array,
3255            &output_type,
3256            vec![Some(-1), Some(0), Some(1), None]
3257        );
3258
3259        // Divide out more digits than we have precision -- all-zero result
3260        let output_type = DataType::Decimal32(9, -7);
3261        assert!(can_cast_types(array.data_type(), &output_type));
3262        generate_cast_test_case!(
3263            &array,
3264            Decimal32Array,
3265            &output_type,
3266            vec![Some(0), Some(0), Some(0), None]
3267        );
3268    }
3269
3270    #[test]
3271    fn test_cast_decimal64_to_decimal64_overflow() {
3272        let input_type = DataType::Decimal64(18, 3);
3273        let output_type = DataType::Decimal64(18, 18);
3274        assert!(can_cast_types(&input_type, &output_type));
3275
3276        let array = vec![Some(i64::MAX)];
3277        let array = create_decimal64_array(array, 18, 3).unwrap();
3278        let result = cast_with_options(
3279            &array,
3280            &output_type,
3281            &CastOptions {
3282                safe: false,
3283                format_options: FormatOptions::default(),
3284            },
3285        );
3286        assert_eq!(
3287            "Cast error: Cannot cast to Decimal64(18, 18). Overflowing on 9223372036854775807",
3288            result.unwrap_err().to_string()
3289        );
3290    }
3291
3292    #[test]
3293    fn test_cast_decimal64_to_decimal64_large_scale_reduction() {
3294        let array = vec![
3295            Some(-999999999999999999),
3296            Some(0),
3297            Some(999999999999999999),
3298            None,
3299        ];
3300        let array = create_decimal64_array(array, 18, 3).unwrap();
3301
3302        // Divide out all digits of precision -- rounding could still produce +/- 1
3303        let output_type = DataType::Decimal64(18, -15);
3304        assert!(can_cast_types(array.data_type(), &output_type));
3305        generate_cast_test_case!(
3306            &array,
3307            Decimal64Array,
3308            &output_type,
3309            vec![Some(-1), Some(0), Some(1), None]
3310        );
3311
3312        // Divide out more digits than we have precision -- all-zero result
3313        let output_type = DataType::Decimal64(18, -16);
3314        assert!(can_cast_types(array.data_type(), &output_type));
3315        generate_cast_test_case!(
3316            &array,
3317            Decimal64Array,
3318            &output_type,
3319            vec![Some(0), Some(0), Some(0), None]
3320        );
3321    }
3322
3323    #[test]
3324    fn test_cast_floating_to_decimals() {
3325        for output_type in [
3326            DataType::Decimal32(9, 3),
3327            DataType::Decimal64(9, 3),
3328            DataType::Decimal128(9, 3),
3329            DataType::Decimal256(9, 3),
3330        ] {
3331            let input_type = DataType::Float64;
3332            assert!(can_cast_types(&input_type, &output_type));
3333
3334            let array = vec![Some(1.1_f64)];
3335            let array = PrimitiveArray::<Float64Type>::from_iter(array);
3336            let result = cast_with_options(
3337                &array,
3338                &output_type,
3339                &CastOptions {
3340                    safe: false,
3341                    format_options: FormatOptions::default(),
3342                },
3343            );
3344            assert!(
3345                result.is_ok(),
3346                "Failed to cast to {output_type} with: {}",
3347                result.unwrap_err()
3348            );
3349        }
3350    }
3351
3352    #[test]
3353    fn test_cast_decimal128_to_decimal128_overflow() {
3354        let input_type = DataType::Decimal128(38, 3);
3355        let output_type = DataType::Decimal128(38, 38);
3356        assert!(can_cast_types(&input_type, &output_type));
3357
3358        let array = vec![Some(i128::MAX)];
3359        let array = create_decimal128_array(array, 38, 3).unwrap();
3360        let result = cast_with_options(
3361            &array,
3362            &output_type,
3363            &CastOptions {
3364                safe: false,
3365                format_options: FormatOptions::default(),
3366            },
3367        );
3368        assert_eq!(
3369            "Cast error: Cannot cast to Decimal128(38, 38). Overflowing on 170141183460469231731687303715884105727",
3370            result.unwrap_err().to_string()
3371        );
3372    }
3373
3374    #[test]
3375    fn test_cast_decimal128_to_decimal256_overflow() {
3376        let input_type = DataType::Decimal128(38, 3);
3377        let output_type = DataType::Decimal256(76, 76);
3378        assert!(can_cast_types(&input_type, &output_type));
3379
3380        let array = vec![Some(i128::MAX)];
3381        let array = create_decimal128_array(array, 38, 3).unwrap();
3382        let result = cast_with_options(
3383            &array,
3384            &output_type,
3385            &CastOptions {
3386                safe: false,
3387                format_options: FormatOptions::default(),
3388            },
3389        );
3390        assert_eq!(
3391            "Cast error: Cannot cast to Decimal256(76, 76). Overflowing on 170141183460469231731687303715884105727",
3392            result.unwrap_err().to_string()
3393        );
3394    }
3395
3396    #[test]
3397    fn test_cast_decimal32_to_decimal256() {
3398        let input_type = DataType::Decimal32(8, 3);
3399        let output_type = DataType::Decimal256(20, 4);
3400        assert!(can_cast_types(&input_type, &output_type));
3401        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3402        let array = create_decimal32_array(array, 8, 3).unwrap();
3403        generate_cast_test_case!(
3404            &array,
3405            Decimal256Array,
3406            &output_type,
3407            vec![
3408                Some(i256::from_i128(11234560_i128)),
3409                Some(i256::from_i128(21234560_i128)),
3410                Some(i256::from_i128(31234560_i128)),
3411                None
3412            ]
3413        );
3414    }
3415    #[test]
3416    fn test_cast_decimal64_to_decimal256() {
3417        let input_type = DataType::Decimal64(12, 3);
3418        let output_type = DataType::Decimal256(20, 4);
3419        assert!(can_cast_types(&input_type, &output_type));
3420        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3421        let array = create_decimal64_array(array, 12, 3).unwrap();
3422        generate_cast_test_case!(
3423            &array,
3424            Decimal256Array,
3425            &output_type,
3426            vec![
3427                Some(i256::from_i128(11234560_i128)),
3428                Some(i256::from_i128(21234560_i128)),
3429                Some(i256::from_i128(31234560_i128)),
3430                None
3431            ]
3432        );
3433    }
3434    #[test]
3435    fn test_cast_decimal128_to_decimal256() {
3436        let input_type = DataType::Decimal128(20, 3);
3437        let output_type = DataType::Decimal256(20, 4);
3438        assert!(can_cast_types(&input_type, &output_type));
3439        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3440        let array = create_decimal128_array(array, 20, 3).unwrap();
3441        generate_cast_test_case!(
3442            &array,
3443            Decimal256Array,
3444            &output_type,
3445            vec![
3446                Some(i256::from_i128(11234560_i128)),
3447                Some(i256::from_i128(21234560_i128)),
3448                Some(i256::from_i128(31234560_i128)),
3449                None
3450            ]
3451        );
3452    }
3453
3454    #[test]
3455    fn test_cast_decimal256_to_decimal128_overflow() {
3456        let input_type = DataType::Decimal256(76, 5);
3457        let output_type = DataType::Decimal128(38, 7);
3458        assert!(can_cast_types(&input_type, &output_type));
3459        let array = vec![Some(i256::from_i128(i128::MAX))];
3460        let array = create_decimal256_array(array, 76, 5).unwrap();
3461        let result = cast_with_options(
3462            &array,
3463            &output_type,
3464            &CastOptions {
3465                safe: false,
3466                format_options: FormatOptions::default(),
3467            },
3468        );
3469        assert_eq!(
3470            "Cast error: Cannot cast to Decimal128(38, 7). Overflowing on 170141183460469231731687303715884105727",
3471            result.unwrap_err().to_string()
3472        );
3473    }
3474
3475    #[test]
3476    fn test_cast_decimal256_to_decimal256_overflow() {
3477        let input_type = DataType::Decimal256(76, 5);
3478        let output_type = DataType::Decimal256(76, 55);
3479        assert!(can_cast_types(&input_type, &output_type));
3480        let array = vec![Some(i256::from_i128(i128::MAX))];
3481        let array = create_decimal256_array(array, 76, 5).unwrap();
3482        let result = cast_with_options(
3483            &array,
3484            &output_type,
3485            &CastOptions {
3486                safe: false,
3487                format_options: FormatOptions::default(),
3488            },
3489        );
3490        assert_eq!(
3491            "Cast error: Cannot cast to Decimal256(76, 55). Overflowing on 170141183460469231731687303715884105727",
3492            result.unwrap_err().to_string()
3493        );
3494    }
3495
3496    #[test]
3497    fn test_cast_decimal256_to_decimal128() {
3498        let input_type = DataType::Decimal256(20, 3);
3499        let output_type = DataType::Decimal128(20, 4);
3500        assert!(can_cast_types(&input_type, &output_type));
3501        let array = vec![
3502            Some(i256::from_i128(1123456)),
3503            Some(i256::from_i128(2123456)),
3504            Some(i256::from_i128(3123456)),
3505            None,
3506        ];
3507        let array = create_decimal256_array(array, 20, 3).unwrap();
3508        generate_cast_test_case!(
3509            &array,
3510            Decimal128Array,
3511            &output_type,
3512            vec![
3513                Some(11234560_i128),
3514                Some(21234560_i128),
3515                Some(31234560_i128),
3516                None
3517            ]
3518        );
3519    }
3520
3521    #[test]
3522    fn test_cast_decimal256_to_decimal256() {
3523        let input_type = DataType::Decimal256(20, 3);
3524        let output_type = DataType::Decimal256(20, 4);
3525        assert!(can_cast_types(&input_type, &output_type));
3526        let array = vec![
3527            Some(i256::from_i128(1123456)),
3528            Some(i256::from_i128(2123456)),
3529            Some(i256::from_i128(3123456)),
3530            None,
3531        ];
3532        let array = create_decimal256_array(array, 20, 3).unwrap();
3533        generate_cast_test_case!(
3534            &array,
3535            Decimal256Array,
3536            &output_type,
3537            vec![
3538                Some(i256::from_i128(11234560_i128)),
3539                Some(i256::from_i128(21234560_i128)),
3540                Some(i256::from_i128(31234560_i128)),
3541                None
3542            ]
3543        );
3544    }
3545
3546    fn generate_decimal_to_numeric_cast_test_case<T>(array: &PrimitiveArray<T>)
3547    where
3548        T: ArrowPrimitiveType + DecimalType,
3549    {
3550        // u8
3551        generate_cast_test_case!(
3552            array,
3553            UInt8Array,
3554            &DataType::UInt8,
3555            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3556        );
3557        // u16
3558        generate_cast_test_case!(
3559            array,
3560            UInt16Array,
3561            &DataType::UInt16,
3562            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3563        );
3564        // u32
3565        generate_cast_test_case!(
3566            array,
3567            UInt32Array,
3568            &DataType::UInt32,
3569            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3570        );
3571        // u64
3572        generate_cast_test_case!(
3573            array,
3574            UInt64Array,
3575            &DataType::UInt64,
3576            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3577        );
3578        // i8
3579        generate_cast_test_case!(
3580            array,
3581            Int8Array,
3582            &DataType::Int8,
3583            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3584        );
3585        // i16
3586        generate_cast_test_case!(
3587            array,
3588            Int16Array,
3589            &DataType::Int16,
3590            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3591        );
3592        // i32
3593        generate_cast_test_case!(
3594            array,
3595            Int32Array,
3596            &DataType::Int32,
3597            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3598        );
3599        // i64
3600        generate_cast_test_case!(
3601            array,
3602            Int64Array,
3603            &DataType::Int64,
3604            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3605        );
3606        // f32
3607        generate_cast_test_case!(
3608            array,
3609            Float32Array,
3610            &DataType::Float32,
3611            vec![
3612                Some(1.25_f32),
3613                Some(2.25_f32),
3614                Some(3.25_f32),
3615                None,
3616                Some(5.25_f32)
3617            ]
3618        );
3619        // f64
3620        generate_cast_test_case!(
3621            array,
3622            Float64Array,
3623            &DataType::Float64,
3624            vec![
3625                Some(1.25_f64),
3626                Some(2.25_f64),
3627                Some(3.25_f64),
3628                None,
3629                Some(5.25_f64)
3630            ]
3631        );
3632    }
3633
3634    #[test]
3635    fn test_cast_decimal32_to_numeric() {
3636        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3637        let array = create_decimal32_array(value_array, 8, 2).unwrap();
3638
3639        generate_decimal_to_numeric_cast_test_case(&array);
3640    }
3641
3642    #[test]
3643    fn test_cast_decimal64_to_numeric() {
3644        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3645        let array = create_decimal64_array(value_array, 8, 2).unwrap();
3646
3647        generate_decimal_to_numeric_cast_test_case(&array);
3648    }
3649
3650    #[test]
3651    fn test_cast_decimal128_to_numeric() {
3652        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3653        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3654
3655        generate_decimal_to_numeric_cast_test_case(&array);
3656
3657        // overflow test: out of range of max u8
3658        let value_array: Vec<Option<i128>> = vec![Some(51300)];
3659        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3660        let casted_array = cast_with_options(
3661            &array,
3662            &DataType::UInt8,
3663            &CastOptions {
3664                safe: false,
3665                format_options: FormatOptions::default(),
3666            },
3667        );
3668        assert_eq!(
3669            "Cast error: value of 513 is out of range UInt8".to_string(),
3670            casted_array.unwrap_err().to_string()
3671        );
3672
3673        let casted_array = cast_with_options(
3674            &array,
3675            &DataType::UInt8,
3676            &CastOptions {
3677                safe: true,
3678                format_options: FormatOptions::default(),
3679            },
3680        );
3681        assert!(casted_array.is_ok());
3682        assert!(casted_array.unwrap().is_null(0));
3683
3684        // overflow test: out of range of max i8
3685        let value_array: Vec<Option<i128>> = vec![Some(24400)];
3686        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3687        let casted_array = cast_with_options(
3688            &array,
3689            &DataType::Int8,
3690            &CastOptions {
3691                safe: false,
3692                format_options: FormatOptions::default(),
3693            },
3694        );
3695        assert_eq!(
3696            "Cast error: value of 244 is out of range Int8".to_string(),
3697            casted_array.unwrap_err().to_string()
3698        );
3699
3700        let casted_array = cast_with_options(
3701            &array,
3702            &DataType::Int8,
3703            &CastOptions {
3704                safe: true,
3705                format_options: FormatOptions::default(),
3706            },
3707        );
3708        assert!(casted_array.is_ok());
3709        assert!(casted_array.unwrap().is_null(0));
3710
3711        // loss the precision: convert decimal to f32、f64
3712        // f32
3713        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3714        let value_array: Vec<Option<i128>> = vec![
3715            Some(125),
3716            Some(225),
3717            Some(325),
3718            None,
3719            Some(525),
3720            Some(112345678),
3721            Some(112345679),
3722        ];
3723        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3724        generate_cast_test_case!(
3725            &array,
3726            Float32Array,
3727            &DataType::Float32,
3728            vec![
3729                Some(1.25_f32),
3730                Some(2.25_f32),
3731                Some(3.25_f32),
3732                None,
3733                Some(5.25_f32),
3734                Some(1_123_456.7_f32),
3735                Some(1_123_456.7_f32)
3736            ]
3737        );
3738
3739        // f64
3740        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3741        let value_array: Vec<Option<i128>> = vec![
3742            Some(125),
3743            Some(225),
3744            Some(325),
3745            None,
3746            Some(525),
3747            Some(112345678901234568),
3748            Some(112345678901234560),
3749        ];
3750        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3751        generate_cast_test_case!(
3752            &array,
3753            Float64Array,
3754            &DataType::Float64,
3755            vec![
3756                Some(1.25_f64),
3757                Some(2.25_f64),
3758                Some(3.25_f64),
3759                None,
3760                Some(5.25_f64),
3761                Some(1_123_456_789_012_345.6_f64),
3762                Some(1_123_456_789_012_345.6_f64),
3763            ]
3764        );
3765    }
3766
3767    #[test]
3768    fn test_cast_decimal256_to_numeric() {
3769        let value_array: Vec<Option<i256>> = vec![
3770            Some(i256::from_i128(125)),
3771            Some(i256::from_i128(225)),
3772            Some(i256::from_i128(325)),
3773            None,
3774            Some(i256::from_i128(525)),
3775        ];
3776        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3777        // u8
3778        generate_cast_test_case!(
3779            &array,
3780            UInt8Array,
3781            &DataType::UInt8,
3782            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3783        );
3784        // u16
3785        generate_cast_test_case!(
3786            &array,
3787            UInt16Array,
3788            &DataType::UInt16,
3789            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3790        );
3791        // u32
3792        generate_cast_test_case!(
3793            &array,
3794            UInt32Array,
3795            &DataType::UInt32,
3796            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3797        );
3798        // u64
3799        generate_cast_test_case!(
3800            &array,
3801            UInt64Array,
3802            &DataType::UInt64,
3803            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3804        );
3805        // i8
3806        generate_cast_test_case!(
3807            &array,
3808            Int8Array,
3809            &DataType::Int8,
3810            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3811        );
3812        // i16
3813        generate_cast_test_case!(
3814            &array,
3815            Int16Array,
3816            &DataType::Int16,
3817            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3818        );
3819        // i32
3820        generate_cast_test_case!(
3821            &array,
3822            Int32Array,
3823            &DataType::Int32,
3824            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3825        );
3826        // i64
3827        generate_cast_test_case!(
3828            &array,
3829            Int64Array,
3830            &DataType::Int64,
3831            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3832        );
3833        // f32
3834        generate_cast_test_case!(
3835            &array,
3836            Float32Array,
3837            &DataType::Float32,
3838            vec![
3839                Some(1.25_f32),
3840                Some(2.25_f32),
3841                Some(3.25_f32),
3842                None,
3843                Some(5.25_f32)
3844            ]
3845        );
3846        // f64
3847        generate_cast_test_case!(
3848            &array,
3849            Float64Array,
3850            &DataType::Float64,
3851            vec![
3852                Some(1.25_f64),
3853                Some(2.25_f64),
3854                Some(3.25_f64),
3855                None,
3856                Some(5.25_f64)
3857            ]
3858        );
3859
3860        // overflow test: out of range of max i8
3861        let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128(24400))];
3862        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3863        let casted_array = cast_with_options(
3864            &array,
3865            &DataType::Int8,
3866            &CastOptions {
3867                safe: false,
3868                format_options: FormatOptions::default(),
3869            },
3870        );
3871        assert_eq!(
3872            "Cast error: value of 244 is out of range Int8".to_string(),
3873            casted_array.unwrap_err().to_string()
3874        );
3875
3876        let casted_array = cast_with_options(
3877            &array,
3878            &DataType::Int8,
3879            &CastOptions {
3880                safe: true,
3881                format_options: FormatOptions::default(),
3882            },
3883        );
3884        assert!(casted_array.is_ok());
3885        assert!(casted_array.unwrap().is_null(0));
3886
3887        // loss the precision: convert decimal to f32、f64
3888        // f32
3889        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3890        let value_array: Vec<Option<i256>> = vec![
3891            Some(i256::from_i128(125)),
3892            Some(i256::from_i128(225)),
3893            Some(i256::from_i128(325)),
3894            None,
3895            Some(i256::from_i128(525)),
3896            Some(i256::from_i128(112345678)),
3897            Some(i256::from_i128(112345679)),
3898        ];
3899        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3900        generate_cast_test_case!(
3901            &array,
3902            Float32Array,
3903            &DataType::Float32,
3904            vec![
3905                Some(1.25_f32),
3906                Some(2.25_f32),
3907                Some(3.25_f32),
3908                None,
3909                Some(5.25_f32),
3910                Some(1_123_456.7_f32),
3911                Some(1_123_456.7_f32)
3912            ]
3913        );
3914
3915        // f64
3916        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3917        let value_array: Vec<Option<i256>> = vec![
3918            Some(i256::from_i128(125)),
3919            Some(i256::from_i128(225)),
3920            Some(i256::from_i128(325)),
3921            None,
3922            Some(i256::from_i128(525)),
3923            Some(i256::from_i128(112345678901234568)),
3924            Some(i256::from_i128(112345678901234560)),
3925        ];
3926        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3927        generate_cast_test_case!(
3928            &array,
3929            Float64Array,
3930            &DataType::Float64,
3931            vec![
3932                Some(1.25_f64),
3933                Some(2.25_f64),
3934                Some(3.25_f64),
3935                None,
3936                Some(5.25_f64),
3937                Some(1_123_456_789_012_345.6_f64),
3938                Some(1_123_456_789_012_345.6_f64),
3939            ]
3940        );
3941    }
3942
3943    #[test]
3944    fn test_cast_decimal_to_numeric_negative_scale() {
3945        let value_array: Vec<Option<i256>> = vec![
3946            Some(i256::from_i128(125)),
3947            Some(i256::from_i128(225)),
3948            Some(i256::from_i128(325)),
3949            None,
3950            Some(i256::from_i128(525)),
3951        ];
3952        let array = create_decimal256_array(value_array, 38, -1).unwrap();
3953
3954        generate_cast_test_case!(
3955            &array,
3956            Int64Array,
3957            &DataType::Int64,
3958            vec![Some(1_250), Some(2_250), Some(3_250), None, Some(5_250)]
3959        );
3960
3961        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3962        let array = create_decimal32_array(value_array, 8, -2).unwrap();
3963        generate_cast_test_case!(
3964            &array,
3965            Int64Array,
3966            &DataType::Int64,
3967            vec![Some(12_500), Some(22_500), Some(32_500), None, Some(52_500)]
3968        );
3969
3970        let value_array: Vec<Option<i32>> = vec![Some(2), Some(1), None];
3971        let array = create_decimal32_array(value_array, 9, -9).unwrap();
3972        generate_cast_test_case!(
3973            &array,
3974            Int64Array,
3975            &DataType::Int64,
3976            vec![Some(2_000_000_000), Some(1_000_000_000), None]
3977        );
3978
3979        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3980        let array = create_decimal64_array(value_array, 18, -3).unwrap();
3981        generate_cast_test_case!(
3982            &array,
3983            Int64Array,
3984            &DataType::Int64,
3985            vec![
3986                Some(125_000),
3987                Some(225_000),
3988                Some(325_000),
3989                None,
3990                Some(525_000)
3991            ]
3992        );
3993
3994        let value_array: Vec<Option<i64>> = vec![Some(12), Some(34), None];
3995        let array = create_decimal64_array(value_array, 18, -10).unwrap();
3996        generate_cast_test_case!(
3997            &array,
3998            Int64Array,
3999            &DataType::Int64,
4000            vec![Some(120_000_000_000), Some(340_000_000_000), None]
4001        );
4002
4003        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
4004        let array = create_decimal128_array(value_array, 38, -4).unwrap();
4005        generate_cast_test_case!(
4006            &array,
4007            Int64Array,
4008            &DataType::Int64,
4009            vec![
4010                Some(1_250_000),
4011                Some(2_250_000),
4012                Some(3_250_000),
4013                None,
4014                Some(5_250_000)
4015            ]
4016        );
4017
4018        let value_array: Vec<Option<i128>> = vec![Some(9), Some(1), None];
4019        let array = create_decimal128_array(value_array, 38, -18).unwrap();
4020        generate_cast_test_case!(
4021            &array,
4022            Int64Array,
4023            &DataType::Int64,
4024            vec![
4025                Some(9_000_000_000_000_000_000),
4026                Some(1_000_000_000_000_000_000),
4027                None
4028            ]
4029        );
4030
4031        let array = create_decimal32_array(vec![Some(999_999_999)], 9, -1).unwrap();
4032        let casted_array = cast_with_options(
4033            &array,
4034            &DataType::Int64,
4035            &CastOptions {
4036                safe: false,
4037                format_options: FormatOptions::default(),
4038            },
4039        );
4040        assert_eq!(
4041            "Arithmetic overflow: Overflow happened on: 999999999 * 10".to_string(),
4042            casted_array.unwrap_err().to_string()
4043        );
4044
4045        let casted_array = cast_with_options(
4046            &array,
4047            &DataType::Int64,
4048            &CastOptions {
4049                safe: true,
4050                format_options: FormatOptions::default(),
4051            },
4052        );
4053        assert!(casted_array.is_ok());
4054        assert!(casted_array.unwrap().is_null(0));
4055
4056        let array = create_decimal64_array(vec![Some(13)], 18, -1).unwrap();
4057        let casted_array = cast_with_options(
4058            &array,
4059            &DataType::Int8,
4060            &CastOptions {
4061                safe: false,
4062                format_options: FormatOptions::default(),
4063            },
4064        );
4065        assert_eq!(
4066            "Cast error: value of 130 is out of range Int8".to_string(),
4067            casted_array.unwrap_err().to_string()
4068        );
4069
4070        let casted_array = cast_with_options(
4071            &array,
4072            &DataType::Int8,
4073            &CastOptions {
4074                safe: true,
4075                format_options: FormatOptions::default(),
4076            },
4077        );
4078        assert!(casted_array.is_ok());
4079        assert!(casted_array.unwrap().is_null(0));
4080    }
4081
4082    #[test]
4083    fn test_cast_numeric_to_decimal128() {
4084        let decimal_type = DataType::Decimal128(38, 6);
4085        // u8, u16, u32, u64
4086        let input_datas = vec![
4087            Arc::new(UInt8Array::from(vec![
4088                Some(1),
4089                Some(2),
4090                Some(3),
4091                None,
4092                Some(5),
4093            ])) as ArrayRef, // u8
4094            Arc::new(UInt16Array::from(vec![
4095                Some(1),
4096                Some(2),
4097                Some(3),
4098                None,
4099                Some(5),
4100            ])) as ArrayRef, // u16
4101            Arc::new(UInt32Array::from(vec![
4102                Some(1),
4103                Some(2),
4104                Some(3),
4105                None,
4106                Some(5),
4107            ])) as ArrayRef, // u32
4108            Arc::new(UInt64Array::from(vec![
4109                Some(1),
4110                Some(2),
4111                Some(3),
4112                None,
4113                Some(5),
4114            ])) as ArrayRef, // u64
4115        ];
4116
4117        for array in input_datas {
4118            generate_cast_test_case!(
4119                &array,
4120                Decimal128Array,
4121                &decimal_type,
4122                vec![
4123                    Some(1000000_i128),
4124                    Some(2000000_i128),
4125                    Some(3000000_i128),
4126                    None,
4127                    Some(5000000_i128)
4128                ]
4129            );
4130        }
4131
4132        // i8, i16, i32, i64
4133        let input_datas = vec![
4134            Arc::new(Int8Array::from(vec![
4135                Some(1),
4136                Some(2),
4137                Some(3),
4138                None,
4139                Some(5),
4140            ])) as ArrayRef, // i8
4141            Arc::new(Int16Array::from(vec![
4142                Some(1),
4143                Some(2),
4144                Some(3),
4145                None,
4146                Some(5),
4147            ])) as ArrayRef, // i16
4148            Arc::new(Int32Array::from(vec![
4149                Some(1),
4150                Some(2),
4151                Some(3),
4152                None,
4153                Some(5),
4154            ])) as ArrayRef, // i32
4155            Arc::new(Int64Array::from(vec![
4156                Some(1),
4157                Some(2),
4158                Some(3),
4159                None,
4160                Some(5),
4161            ])) as ArrayRef, // i64
4162        ];
4163        for array in input_datas {
4164            generate_cast_test_case!(
4165                &array,
4166                Decimal128Array,
4167                &decimal_type,
4168                vec![
4169                    Some(1000000_i128),
4170                    Some(2000000_i128),
4171                    Some(3000000_i128),
4172                    None,
4173                    Some(5000000_i128)
4174                ]
4175            );
4176        }
4177
4178        // test u8 to decimal type with overflow the result type
4179        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4180        let array = UInt8Array::from(vec![1, 2, 3, 4, 100]);
4181        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4182        assert!(casted_array.is_ok());
4183        let array = casted_array.unwrap();
4184        let array: &Decimal128Array = array.as_primitive();
4185        assert!(array.is_null(4));
4186
4187        // test i8 to decimal type with overflow the result type
4188        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4189        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4190        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4191        assert!(casted_array.is_ok());
4192        let array = casted_array.unwrap();
4193        let array: &Decimal128Array = array.as_primitive();
4194        assert!(array.is_null(4));
4195
4196        // test f32 to decimal type
4197        let array = Float32Array::from(vec![
4198            Some(1.1),
4199            Some(2.2),
4200            Some(4.4),
4201            None,
4202            Some(1.123_456_4), // round down
4203            Some(1.123_456_7), // round up
4204        ]);
4205        let array = Arc::new(array) as ArrayRef;
4206        generate_cast_test_case!(
4207            &array,
4208            Decimal128Array,
4209            &decimal_type,
4210            vec![
4211                Some(1100000_i128),
4212                Some(2200000_i128),
4213                Some(4400000_i128),
4214                None,
4215                Some(1123456_i128), // round down
4216                Some(1123457_i128), // round up
4217            ]
4218        );
4219
4220        // test f64 to decimal type
4221        let array = Float64Array::from(vec![
4222            Some(1.1),
4223            Some(2.2),
4224            Some(4.4),
4225            None,
4226            Some(1.123_456_489_123_4),     // round up
4227            Some(1.123_456_789_123_4),     // round up
4228            Some(1.123_456_489_012_345_6), // round down
4229            Some(1.123_456_789_012_345_6), // round up
4230        ]);
4231        generate_cast_test_case!(
4232            &array,
4233            Decimal128Array,
4234            &decimal_type,
4235            vec![
4236                Some(1100000_i128),
4237                Some(2200000_i128),
4238                Some(4400000_i128),
4239                None,
4240                Some(1123456_i128), // round down
4241                Some(1123457_i128), // round up
4242                Some(1123456_i128), // round down
4243                Some(1123457_i128), // round up
4244            ]
4245        );
4246    }
4247
4248    #[test]
4249    fn test_cast_numeric_to_decimal256() {
4250        let decimal_type = DataType::Decimal256(76, 6);
4251        // u8, u16, u32, u64
4252        let input_datas = vec![
4253            Arc::new(UInt8Array::from(vec![
4254                Some(1),
4255                Some(2),
4256                Some(3),
4257                None,
4258                Some(5),
4259            ])) as ArrayRef, // u8
4260            Arc::new(UInt16Array::from(vec![
4261                Some(1),
4262                Some(2),
4263                Some(3),
4264                None,
4265                Some(5),
4266            ])) as ArrayRef, // u16
4267            Arc::new(UInt32Array::from(vec![
4268                Some(1),
4269                Some(2),
4270                Some(3),
4271                None,
4272                Some(5),
4273            ])) as ArrayRef, // u32
4274            Arc::new(UInt64Array::from(vec![
4275                Some(1),
4276                Some(2),
4277                Some(3),
4278                None,
4279                Some(5),
4280            ])) as ArrayRef, // u64
4281        ];
4282
4283        for array in input_datas {
4284            generate_cast_test_case!(
4285                &array,
4286                Decimal256Array,
4287                &decimal_type,
4288                vec![
4289                    Some(i256::from_i128(1000000_i128)),
4290                    Some(i256::from_i128(2000000_i128)),
4291                    Some(i256::from_i128(3000000_i128)),
4292                    None,
4293                    Some(i256::from_i128(5000000_i128))
4294                ]
4295            );
4296        }
4297
4298        // i8, i16, i32, i64
4299        let input_datas = vec![
4300            Arc::new(Int8Array::from(vec![
4301                Some(1),
4302                Some(2),
4303                Some(3),
4304                None,
4305                Some(5),
4306            ])) as ArrayRef, // i8
4307            Arc::new(Int16Array::from(vec![
4308                Some(1),
4309                Some(2),
4310                Some(3),
4311                None,
4312                Some(5),
4313            ])) as ArrayRef, // i16
4314            Arc::new(Int32Array::from(vec![
4315                Some(1),
4316                Some(2),
4317                Some(3),
4318                None,
4319                Some(5),
4320            ])) as ArrayRef, // i32
4321            Arc::new(Int64Array::from(vec![
4322                Some(1),
4323                Some(2),
4324                Some(3),
4325                None,
4326                Some(5),
4327            ])) as ArrayRef, // i64
4328        ];
4329        for array in input_datas {
4330            generate_cast_test_case!(
4331                &array,
4332                Decimal256Array,
4333                &decimal_type,
4334                vec![
4335                    Some(i256::from_i128(1000000_i128)),
4336                    Some(i256::from_i128(2000000_i128)),
4337                    Some(i256::from_i128(3000000_i128)),
4338                    None,
4339                    Some(i256::from_i128(5000000_i128))
4340                ]
4341            );
4342        }
4343
4344        // test i8 to decimal type with overflow the result type
4345        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4346        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4347        let array = Arc::new(array) as ArrayRef;
4348        let casted_array = cast(&array, &DataType::Decimal256(3, 1));
4349        assert!(casted_array.is_ok());
4350        let array = casted_array.unwrap();
4351        let array: &Decimal256Array = array.as_primitive();
4352        assert!(array.is_null(4));
4353
4354        // test f32 to decimal type
4355        let array = Float32Array::from(vec![
4356            Some(1.1),
4357            Some(2.2),
4358            Some(4.4),
4359            None,
4360            Some(1.123_456_4), // round down
4361            Some(1.123_456_7), // round up
4362        ]);
4363        generate_cast_test_case!(
4364            &array,
4365            Decimal256Array,
4366            &decimal_type,
4367            vec![
4368                Some(i256::from_i128(1100000_i128)),
4369                Some(i256::from_i128(2200000_i128)),
4370                Some(i256::from_i128(4400000_i128)),
4371                None,
4372                Some(i256::from_i128(1123456_i128)), // round down
4373                Some(i256::from_i128(1123457_i128)), // round up
4374            ]
4375        );
4376
4377        // test f64 to decimal type
4378        let array = Float64Array::from(vec![
4379            Some(1.1),
4380            Some(2.2),
4381            Some(4.4),
4382            None,
4383            Some(1.123_456_489_123_4),     // round down
4384            Some(1.123_456_789_123_4),     // round up
4385            Some(1.123_456_489_012_345_6), // round down
4386            Some(1.123_456_789_012_345_6), // round up
4387        ]);
4388        generate_cast_test_case!(
4389            &array,
4390            Decimal256Array,
4391            &decimal_type,
4392            vec![
4393                Some(i256::from_i128(1100000_i128)),
4394                Some(i256::from_i128(2200000_i128)),
4395                Some(i256::from_i128(4400000_i128)),
4396                None,
4397                Some(i256::from_i128(1123456_i128)), // round down
4398                Some(i256::from_i128(1123457_i128)), // round up
4399                Some(i256::from_i128(1123456_i128)), // round down
4400                Some(i256::from_i128(1123457_i128)), // round up
4401            ]
4402        );
4403    }
4404
4405    #[test]
4406    fn test_cast_i32_to_f64() {
4407        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4408        let b = cast(&array, &DataType::Float64).unwrap();
4409        let c = b.as_primitive::<Float64Type>();
4410        assert_eq!(5.0, c.value(0));
4411        assert_eq!(6.0, c.value(1));
4412        assert_eq!(7.0, c.value(2));
4413        assert_eq!(8.0, c.value(3));
4414        assert_eq!(9.0, c.value(4));
4415    }
4416
4417    #[test]
4418    fn test_cast_i32_to_u8() {
4419        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4420        let b = cast(&array, &DataType::UInt8).unwrap();
4421        let c = b.as_primitive::<UInt8Type>();
4422        assert!(!c.is_valid(0));
4423        assert_eq!(6, c.value(1));
4424        assert!(!c.is_valid(2));
4425        assert_eq!(8, c.value(3));
4426        // overflows return None
4427        assert!(!c.is_valid(4));
4428    }
4429
4430    #[test]
4431    #[should_panic(expected = "Can't cast value -5 to type UInt8")]
4432    fn test_cast_int32_to_u8_with_error() {
4433        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4434        // overflow with the error
4435        let cast_option = CastOptions {
4436            safe: false,
4437            format_options: FormatOptions::default(),
4438        };
4439        let result = cast_with_options(&array, &DataType::UInt8, &cast_option);
4440        assert!(result.is_err());
4441        result.unwrap();
4442    }
4443
4444    #[test]
4445    fn test_cast_i32_to_u8_sliced() {
4446        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4447        assert_eq!(0, array.offset());
4448        let array = array.slice(2, 3);
4449        let b = cast(&array, &DataType::UInt8).unwrap();
4450        assert_eq!(3, b.len());
4451        let c = b.as_primitive::<UInt8Type>();
4452        assert!(!c.is_valid(0));
4453        assert_eq!(8, c.value(1));
4454        // overflows return None
4455        assert!(!c.is_valid(2));
4456    }
4457
4458    #[test]
4459    fn test_cast_i32_to_i32() {
4460        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4461        let b = cast(&array, &DataType::Int32).unwrap();
4462        let c = b.as_primitive::<Int32Type>();
4463        assert_eq!(5, c.value(0));
4464        assert_eq!(6, c.value(1));
4465        assert_eq!(7, c.value(2));
4466        assert_eq!(8, c.value(3));
4467        assert_eq!(9, c.value(4));
4468    }
4469
4470    #[test]
4471    fn test_cast_i32_to_list_i32() {
4472        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4473        let b = cast(
4474            &array,
4475            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4476        )
4477        .unwrap();
4478        assert_eq!(5, b.len());
4479        let arr = b.as_list::<i32>();
4480        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4481        assert_eq!(1, arr.value_length(0));
4482        assert_eq!(1, arr.value_length(1));
4483        assert_eq!(1, arr.value_length(2));
4484        assert_eq!(1, arr.value_length(3));
4485        assert_eq!(1, arr.value_length(4));
4486        let c = arr.values().as_primitive::<Int32Type>();
4487        assert_eq!(5, c.value(0));
4488        assert_eq!(6, c.value(1));
4489        assert_eq!(7, c.value(2));
4490        assert_eq!(8, c.value(3));
4491        assert_eq!(9, c.value(4));
4492    }
4493
4494    #[test]
4495    fn test_cast_i32_to_list_i32_nullable() {
4496        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), Some(9)]);
4497        let b = cast(
4498            &array,
4499            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4500        )
4501        .unwrap();
4502        assert_eq!(5, b.len());
4503        assert_eq!(0, b.null_count());
4504        let arr = b.as_list::<i32>();
4505        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4506        assert_eq!(1, arr.value_length(0));
4507        assert_eq!(1, arr.value_length(1));
4508        assert_eq!(1, arr.value_length(2));
4509        assert_eq!(1, arr.value_length(3));
4510        assert_eq!(1, arr.value_length(4));
4511
4512        let c = arr.values().as_primitive::<Int32Type>();
4513        assert_eq!(1, c.null_count());
4514        assert_eq!(5, c.value(0));
4515        assert!(!c.is_valid(1));
4516        assert_eq!(7, c.value(2));
4517        assert_eq!(8, c.value(3));
4518        assert_eq!(9, c.value(4));
4519    }
4520
4521    #[test]
4522    fn test_cast_i32_to_list_f64_nullable_sliced() {
4523        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), None, Some(10)]);
4524        let array = array.slice(2, 4);
4525        let b = cast(
4526            &array,
4527            &DataType::List(Arc::new(Field::new_list_field(DataType::Float64, true))),
4528        )
4529        .unwrap();
4530        assert_eq!(4, b.len());
4531        assert_eq!(0, b.null_count());
4532        let arr = b.as_list::<i32>();
4533        assert_eq!(&[0, 1, 2, 3, 4], arr.value_offsets());
4534        assert_eq!(1, arr.value_length(0));
4535        assert_eq!(1, arr.value_length(1));
4536        assert_eq!(1, arr.value_length(2));
4537        assert_eq!(1, arr.value_length(3));
4538        let c = arr.values().as_primitive::<Float64Type>();
4539        assert_eq!(1, c.null_count());
4540        assert_eq!(7.0, c.value(0));
4541        assert_eq!(8.0, c.value(1));
4542        assert!(!c.is_valid(2));
4543        assert_eq!(10.0, c.value(3));
4544    }
4545
4546    #[test]
4547    fn test_cast_int_to_utf8view() {
4548        let inputs = vec![
4549            Arc::new(Int8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4550            Arc::new(Int16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4551            Arc::new(Int32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4552            Arc::new(Int64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4553            Arc::new(UInt8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4554            Arc::new(UInt16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4555            Arc::new(UInt32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4556            Arc::new(UInt64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4557        ];
4558        let expected: ArrayRef = Arc::new(StringViewArray::from(vec![
4559            None,
4560            Some("8"),
4561            Some("9"),
4562            Some("10"),
4563        ]));
4564
4565        for array in inputs {
4566            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4567            let arr = cast(&array, &DataType::Utf8View).unwrap();
4568            assert_eq!(expected.as_ref(), arr.as_ref());
4569        }
4570    }
4571
4572    #[test]
4573    fn test_cast_float_to_utf8view() {
4574        let inputs = vec![
4575            Arc::new(Float16Array::from(vec![
4576                Some(f16::from_f64(1.5)),
4577                Some(f16::from_f64(2.5)),
4578                None,
4579            ])) as ArrayRef,
4580            Arc::new(Float32Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4581            Arc::new(Float64Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4582        ];
4583
4584        let expected: ArrayRef =
4585            Arc::new(StringViewArray::from(vec![Some("1.5"), Some("2.5"), None]));
4586
4587        for array in inputs {
4588            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4589            let arr = cast(&array, &DataType::Utf8View).unwrap();
4590            assert_eq!(expected.as_ref(), arr.as_ref());
4591        }
4592    }
4593
4594    #[test]
4595    fn test_cast_utf8_to_i32() {
4596        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4597        let b = cast(&array, &DataType::Int32).unwrap();
4598        let c = b.as_primitive::<Int32Type>();
4599        assert_eq!(5, c.value(0));
4600        assert_eq!(6, c.value(1));
4601        assert!(!c.is_valid(2));
4602        assert_eq!(8, c.value(3));
4603        assert!(!c.is_valid(4));
4604    }
4605
4606    #[test]
4607    fn test_cast_utf8view_to_i32() {
4608        let array = StringViewArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4609        let b = cast(&array, &DataType::Int32).unwrap();
4610        let c = b.as_primitive::<Int32Type>();
4611        assert_eq!(5, c.value(0));
4612        assert_eq!(6, c.value(1));
4613        assert!(!c.is_valid(2));
4614        assert_eq!(8, c.value(3));
4615        assert!(!c.is_valid(4));
4616    }
4617
4618    #[test]
4619    fn test_cast_utf8view_to_f32() {
4620        let array = StringViewArray::from(vec!["3", "4.56", "seven", "8.9"]);
4621        let b = cast(&array, &DataType::Float32).unwrap();
4622        let c = b.as_primitive::<Float32Type>();
4623        assert_eq!(3.0, c.value(0));
4624        assert_eq!(4.56, c.value(1));
4625        assert!(!c.is_valid(2));
4626        assert_eq!(8.9, c.value(3));
4627    }
4628
4629    #[test]
4630    fn test_cast_string_to_f16() {
4631        let arrays = [
4632            Arc::new(StringViewArray::from(vec!["3", "4.56", "seven", "8.9"])) as ArrayRef,
4633            Arc::new(StringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4634            Arc::new(LargeStringArray::from(vec!["3", "4.56", "seven", "8.9"])),
4635        ];
4636        for array in arrays {
4637            let b = cast(&array, &DataType::Float16).unwrap();
4638            let c = b.as_primitive::<Float16Type>();
4639            assert_eq!(half::f16::from_f32(3.0), c.value(0));
4640            assert_eq!(half::f16::from_f32(4.56), c.value(1));
4641            assert!(!c.is_valid(2));
4642            assert_eq!(half::f16::from_f32(8.9), c.value(3));
4643        }
4644    }
4645
4646    #[test]
4647    fn test_cast_utf8view_to_decimal128() {
4648        let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
4649        let arr = Arc::new(array) as ArrayRef;
4650        generate_cast_test_case!(
4651            &arr,
4652            Decimal128Array,
4653            &DataType::Decimal128(4, 2),
4654            vec![None, Some(400_i128), Some(560_i128), Some(789_i128)]
4655        );
4656    }
4657
4658    #[test]
4659    fn test_cast_with_options_utf8_to_i32() {
4660        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4661        let result = cast_with_options(
4662            &array,
4663            &DataType::Int32,
4664            &CastOptions {
4665                safe: false,
4666                format_options: FormatOptions::default(),
4667            },
4668        );
4669        match result {
4670            Ok(_) => panic!("expected error"),
4671            Err(e) => {
4672                assert!(
4673                    e.to_string()
4674                        .contains("Cast error: Cannot cast string 'seven' to value of Int32 type",),
4675                    "Error: {e}"
4676                )
4677            }
4678        }
4679    }
4680
4681    #[test]
4682    fn test_cast_utf8_to_bool() {
4683        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4684        let casted = cast(&strings, &DataType::Boolean).unwrap();
4685        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4686        assert_eq!(*as_boolean_array(&casted), expected);
4687    }
4688
4689    #[test]
4690    fn test_cast_utf8view_to_bool() {
4691        let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4692        let casted = cast(&strings, &DataType::Boolean).unwrap();
4693        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4694        assert_eq!(*as_boolean_array(&casted), expected);
4695    }
4696
4697    #[test]
4698    fn test_cast_with_options_utf8_to_bool() {
4699        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4700        let casted = cast_with_options(
4701            &strings,
4702            &DataType::Boolean,
4703            &CastOptions {
4704                safe: false,
4705                format_options: FormatOptions::default(),
4706            },
4707        );
4708        match casted {
4709            Ok(_) => panic!("expected error"),
4710            Err(e) => {
4711                assert!(
4712                    e.to_string().contains(
4713                        "Cast error: Cannot cast value 'invalid' to value of Boolean type"
4714                    )
4715                )
4716            }
4717        }
4718    }
4719
4720    #[test]
4721    fn test_cast_bool_to_i32() {
4722        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4723        let b = cast(&array, &DataType::Int32).unwrap();
4724        let c = b.as_primitive::<Int32Type>();
4725        assert_eq!(1, c.value(0));
4726        assert_eq!(0, c.value(1));
4727        assert!(!c.is_valid(2));
4728    }
4729
4730    #[test]
4731    fn test_cast_bool_to_utf8view() {
4732        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4733        let b = cast(&array, &DataType::Utf8View).unwrap();
4734        let c = b.as_any().downcast_ref::<StringViewArray>().unwrap();
4735        assert_eq!("true", c.value(0));
4736        assert_eq!("false", c.value(1));
4737        assert!(!c.is_valid(2));
4738    }
4739
4740    #[test]
4741    fn test_cast_bool_to_utf8() {
4742        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4743        let b = cast(&array, &DataType::Utf8).unwrap();
4744        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
4745        assert_eq!("true", c.value(0));
4746        assert_eq!("false", c.value(1));
4747        assert!(!c.is_valid(2));
4748    }
4749
4750    #[test]
4751    fn test_cast_bool_to_large_utf8() {
4752        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4753        let b = cast(&array, &DataType::LargeUtf8).unwrap();
4754        let c = b.as_any().downcast_ref::<LargeStringArray>().unwrap();
4755        assert_eq!("true", c.value(0));
4756        assert_eq!("false", c.value(1));
4757        assert!(!c.is_valid(2));
4758    }
4759
4760    #[test]
4761    fn test_cast_bool_to_f64() {
4762        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4763        let b = cast(&array, &DataType::Float64).unwrap();
4764        let c = b.as_primitive::<Float64Type>();
4765        assert_eq!(1.0, c.value(0));
4766        assert_eq!(0.0, c.value(1));
4767        assert!(!c.is_valid(2));
4768    }
4769
4770    #[test]
4771    fn test_cast_integer_to_timestamp() {
4772        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4773        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4774
4775        let array = Int8Array::from(vec![Some(2), Some(10), None]);
4776        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4777
4778        assert_eq!(&actual, &expected);
4779
4780        let array = Int16Array::from(vec![Some(2), Some(10), None]);
4781        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4782
4783        assert_eq!(&actual, &expected);
4784
4785        let array = Int32Array::from(vec![Some(2), Some(10), None]);
4786        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4787
4788        assert_eq!(&actual, &expected);
4789
4790        let array = UInt8Array::from(vec![Some(2), Some(10), None]);
4791        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4792
4793        assert_eq!(&actual, &expected);
4794
4795        let array = UInt16Array::from(vec![Some(2), Some(10), None]);
4796        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4797
4798        assert_eq!(&actual, &expected);
4799
4800        let array = UInt32Array::from(vec![Some(2), Some(10), None]);
4801        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4802
4803        assert_eq!(&actual, &expected);
4804
4805        let array = UInt64Array::from(vec![Some(2), Some(10), None]);
4806        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4807
4808        assert_eq!(&actual, &expected);
4809    }
4810
4811    #[test]
4812    fn test_cast_timestamp_to_integer() {
4813        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4814            .with_timezone("UTC".to_string());
4815        let expected = cast(&array, &DataType::Int64).unwrap();
4816
4817        let actual = cast(&cast(&array, &DataType::Int8).unwrap(), &DataType::Int64).unwrap();
4818        assert_eq!(&actual, &expected);
4819
4820        let actual = cast(&cast(&array, &DataType::Int16).unwrap(), &DataType::Int64).unwrap();
4821        assert_eq!(&actual, &expected);
4822
4823        let actual = cast(&cast(&array, &DataType::Int32).unwrap(), &DataType::Int64).unwrap();
4824        assert_eq!(&actual, &expected);
4825
4826        let actual = cast(&cast(&array, &DataType::UInt8).unwrap(), &DataType::Int64).unwrap();
4827        assert_eq!(&actual, &expected);
4828
4829        let actual = cast(&cast(&array, &DataType::UInt16).unwrap(), &DataType::Int64).unwrap();
4830        assert_eq!(&actual, &expected);
4831
4832        let actual = cast(&cast(&array, &DataType::UInt32).unwrap(), &DataType::Int64).unwrap();
4833        assert_eq!(&actual, &expected);
4834
4835        let actual = cast(&cast(&array, &DataType::UInt64).unwrap(), &DataType::Int64).unwrap();
4836        assert_eq!(&actual, &expected);
4837    }
4838
4839    #[test]
4840    fn test_cast_floating_to_timestamp() {
4841        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4842        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4843
4844        let array = Float16Array::from(vec![
4845            Some(f16::from_f32(2.0)),
4846            Some(f16::from_f32(10.6)),
4847            None,
4848        ]);
4849        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4850
4851        assert_eq!(&actual, &expected);
4852
4853        let array = Float32Array::from(vec![Some(2.0), Some(10.6), None]);
4854        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4855
4856        assert_eq!(&actual, &expected);
4857
4858        let array = Float64Array::from(vec![Some(2.1), Some(10.2), None]);
4859        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4860
4861        assert_eq!(&actual, &expected);
4862    }
4863
4864    #[test]
4865    fn test_cast_timestamp_to_floating() {
4866        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4867            .with_timezone("UTC".to_string());
4868        let expected = cast(&array, &DataType::Int64).unwrap();
4869
4870        let actual = cast(&cast(&array, &DataType::Float16).unwrap(), &DataType::Int64).unwrap();
4871        assert_eq!(&actual, &expected);
4872
4873        let actual = cast(&cast(&array, &DataType::Float32).unwrap(), &DataType::Int64).unwrap();
4874        assert_eq!(&actual, &expected);
4875
4876        let actual = cast(&cast(&array, &DataType::Float64).unwrap(), &DataType::Int64).unwrap();
4877        assert_eq!(&actual, &expected);
4878    }
4879
4880    #[test]
4881    fn test_cast_decimal_to_timestamp() {
4882        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4883        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4884
4885        let array = Decimal128Array::from(vec![Some(200), Some(1000), None])
4886            .with_precision_and_scale(4, 2)
4887            .unwrap();
4888        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4889
4890        assert_eq!(&actual, &expected);
4891
4892        let array = Decimal256Array::from(vec![
4893            Some(i256::from_i128(2000)),
4894            Some(i256::from_i128(10000)),
4895            None,
4896        ])
4897        .with_precision_and_scale(5, 3)
4898        .unwrap();
4899        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4900
4901        assert_eq!(&actual, &expected);
4902    }
4903
4904    #[test]
4905    fn test_cast_timestamp_to_decimal() {
4906        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4907            .with_timezone("UTC".to_string());
4908        let expected = cast(&array, &DataType::Int64).unwrap();
4909
4910        let actual = cast(
4911            &cast(&array, &DataType::Decimal128(5, 2)).unwrap(),
4912            &DataType::Int64,
4913        )
4914        .unwrap();
4915        assert_eq!(&actual, &expected);
4916
4917        let actual = cast(
4918            &cast(&array, &DataType::Decimal256(10, 5)).unwrap(),
4919            &DataType::Int64,
4920        )
4921        .unwrap();
4922        assert_eq!(&actual, &expected);
4923    }
4924
4925    #[test]
4926    fn test_cast_list_i32_to_list_u16() {
4927        let values = vec![
4928            Some(vec![Some(0), Some(0), Some(0)]),
4929            Some(vec![Some(-1), Some(-2), Some(-1)]),
4930            Some(vec![Some(2), Some(100000000)]),
4931        ];
4932        let list_array = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
4933
4934        let target_type = DataType::List(Arc::new(Field::new("item", DataType::UInt16, true)));
4935        assert!(can_cast_types(list_array.data_type(), &target_type));
4936        let cast_array = cast(&list_array, &target_type).unwrap();
4937
4938        // For the ListArray itself, there are no null values (as there were no nulls when they went in)
4939        //
4940        // 3 negative values should get lost when casting to unsigned,
4941        // 1 value should overflow
4942        assert_eq!(0, cast_array.null_count());
4943
4944        // offsets should be the same
4945        let array = cast_array.as_list::<i32>();
4946        assert_eq!(list_array.value_offsets(), array.value_offsets());
4947
4948        assert_eq!(DataType::UInt16, array.value_type());
4949        assert_eq!(3, array.value_length(0));
4950        assert_eq!(3, array.value_length(1));
4951        assert_eq!(2, array.value_length(2));
4952
4953        // expect 4 nulls: negative numbers and overflow
4954        let u16arr = array.values().as_primitive::<UInt16Type>();
4955        assert_eq!(4, u16arr.null_count());
4956
4957        // expect 4 nulls: negative numbers and overflow
4958        let expected: UInt16Array =
4959            vec![Some(0), Some(0), Some(0), None, None, None, Some(2), None]
4960                .into_iter()
4961                .collect();
4962
4963        assert_eq!(u16arr, &expected);
4964    }
4965
4966    #[test]
4967    fn test_cast_list_i32_to_list_timestamp() {
4968        // Construct a value array
4969        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 8, 100000000]).into_data();
4970
4971        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 9]);
4972
4973        // Construct a list array from the above two
4974        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
4975        let list_data = ArrayData::builder(list_data_type)
4976            .len(3)
4977            .add_buffer(value_offsets)
4978            .add_child_data(value_data)
4979            .build()
4980            .unwrap();
4981        let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
4982
4983        let actual = cast(
4984            &list_array,
4985            &DataType::List(Arc::new(Field::new_list_field(
4986                DataType::Timestamp(TimeUnit::Microsecond, None),
4987                true,
4988            ))),
4989        )
4990        .unwrap();
4991
4992        let expected = cast(
4993            &cast(
4994                &list_array,
4995                &DataType::List(Arc::new(Field::new_list_field(DataType::Int64, true))),
4996            )
4997            .unwrap(),
4998            &DataType::List(Arc::new(Field::new_list_field(
4999                DataType::Timestamp(TimeUnit::Microsecond, None),
5000                true,
5001            ))),
5002        )
5003        .unwrap();
5004
5005        assert_eq!(&actual, &expected);
5006    }
5007
5008    #[test]
5009    fn test_cast_date32_to_date64() {
5010        let a = Date32Array::from(vec![10000, 17890]);
5011        let array = Arc::new(a) as ArrayRef;
5012        let b = cast(&array, &DataType::Date64).unwrap();
5013        let c = b.as_primitive::<Date64Type>();
5014        assert_eq!(864000000000, c.value(0));
5015        assert_eq!(1545696000000, c.value(1));
5016    }
5017
5018    #[test]
5019    fn test_cast_date64_to_date32() {
5020        let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
5021        let array = Arc::new(a) as ArrayRef;
5022        let b = cast(&array, &DataType::Date32).unwrap();
5023        let c = b.as_primitive::<Date32Type>();
5024        assert_eq!(10000, c.value(0));
5025        assert_eq!(17890, c.value(1));
5026        assert!(c.is_null(2));
5027    }
5028
5029    #[test]
5030    fn test_cast_string_to_integral_overflow() {
5031        let str = Arc::new(StringArray::from(vec![
5032            Some("123"),
5033            Some("-123"),
5034            Some("86374"),
5035            None,
5036        ])) as ArrayRef;
5037
5038        let options = CastOptions {
5039            safe: true,
5040            format_options: FormatOptions::default(),
5041        };
5042        let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
5043        let expected =
5044            Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
5045        assert_eq!(&res, &expected);
5046    }
5047
5048    #[test]
5049    fn test_cast_string_to_timestamp() {
5050        let a0 = Arc::new(StringViewArray::from(vec![
5051            Some("2020-09-08T12:00:00.123456789+00:00"),
5052            Some("Not a valid date"),
5053            None,
5054        ])) as ArrayRef;
5055        let a1 = Arc::new(StringArray::from(vec![
5056            Some("2020-09-08T12:00:00.123456789+00:00"),
5057            Some("Not a valid date"),
5058            None,
5059        ])) as ArrayRef;
5060        let a2 = Arc::new(LargeStringArray::from(vec![
5061            Some("2020-09-08T12:00:00.123456789+00:00"),
5062            Some("Not a valid date"),
5063            None,
5064        ])) as ArrayRef;
5065        for array in &[a0, a1, a2] {
5066            for time_unit in &[
5067                TimeUnit::Second,
5068                TimeUnit::Millisecond,
5069                TimeUnit::Microsecond,
5070                TimeUnit::Nanosecond,
5071            ] {
5072                let to_type = DataType::Timestamp(*time_unit, None);
5073                let b = cast(array, &to_type).unwrap();
5074
5075                match time_unit {
5076                    TimeUnit::Second => {
5077                        let c = b.as_primitive::<TimestampSecondType>();
5078                        assert_eq!(1599566400, c.value(0));
5079                        assert!(c.is_null(1));
5080                        assert!(c.is_null(2));
5081                    }
5082                    TimeUnit::Millisecond => {
5083                        let c = b
5084                            .as_any()
5085                            .downcast_ref::<TimestampMillisecondArray>()
5086                            .unwrap();
5087                        assert_eq!(1599566400123, c.value(0));
5088                        assert!(c.is_null(1));
5089                        assert!(c.is_null(2));
5090                    }
5091                    TimeUnit::Microsecond => {
5092                        let c = b
5093                            .as_any()
5094                            .downcast_ref::<TimestampMicrosecondArray>()
5095                            .unwrap();
5096                        assert_eq!(1599566400123456, c.value(0));
5097                        assert!(c.is_null(1));
5098                        assert!(c.is_null(2));
5099                    }
5100                    TimeUnit::Nanosecond => {
5101                        let c = b
5102                            .as_any()
5103                            .downcast_ref::<TimestampNanosecondArray>()
5104                            .unwrap();
5105                        assert_eq!(1599566400123456789, c.value(0));
5106                        assert!(c.is_null(1));
5107                        assert!(c.is_null(2));
5108                    }
5109                }
5110
5111                let options = CastOptions {
5112                    safe: false,
5113                    format_options: FormatOptions::default(),
5114                };
5115                let err = cast_with_options(array, &to_type, &options).unwrap_err();
5116                assert_eq!(
5117                    err.to_string(),
5118                    "Parser error: Error parsing timestamp from 'Not a valid date': error parsing date"
5119                );
5120            }
5121        }
5122    }
5123
5124    #[test]
5125    fn test_cast_string_to_timestamp_overflow() {
5126        let array = StringArray::from(vec!["9800-09-08T12:00:00.123456789"]);
5127        let result = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5128        let result = result.as_primitive::<TimestampSecondType>();
5129        assert_eq!(result.values(), &[247112596800]);
5130    }
5131
5132    #[test]
5133    fn test_cast_string_to_date32() {
5134        let a0 = Arc::new(StringViewArray::from(vec![
5135            Some("2018-12-25"),
5136            Some("Not a valid date"),
5137            None,
5138        ])) as ArrayRef;
5139        let a1 = Arc::new(StringArray::from(vec![
5140            Some("2018-12-25"),
5141            Some("Not a valid date"),
5142            None,
5143        ])) as ArrayRef;
5144        let a2 = Arc::new(LargeStringArray::from(vec![
5145            Some("2018-12-25"),
5146            Some("Not a valid date"),
5147            None,
5148        ])) as ArrayRef;
5149        for array in &[a0, a1, a2] {
5150            let to_type = DataType::Date32;
5151            let b = cast(array, &to_type).unwrap();
5152            let c = b.as_primitive::<Date32Type>();
5153            assert_eq!(17890, c.value(0));
5154            assert!(c.is_null(1));
5155            assert!(c.is_null(2));
5156
5157            let options = CastOptions {
5158                safe: false,
5159                format_options: FormatOptions::default(),
5160            };
5161            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5162            assert_eq!(
5163                err.to_string(),
5164                "Cast error: Cannot cast string 'Not a valid date' to value of Date32 type"
5165            );
5166        }
5167    }
5168
5169    #[test]
5170    fn test_cast_string_with_large_date_to_date32() {
5171        let array = Arc::new(StringArray::from(vec![
5172            Some("+10999-12-31"),
5173            Some("-0010-02-28"),
5174            Some("0010-02-28"),
5175            Some("0000-01-01"),
5176            Some("-0000-01-01"),
5177            Some("-0001-01-01"),
5178        ])) as ArrayRef;
5179        let to_type = DataType::Date32;
5180        let options = CastOptions {
5181            safe: false,
5182            format_options: FormatOptions::default(),
5183        };
5184        let b = cast_with_options(&array, &to_type, &options).unwrap();
5185        let c = b.as_primitive::<Date32Type>();
5186        assert_eq!(3298139, c.value(0)); // 10999-12-31
5187        assert_eq!(-723122, c.value(1)); // -0010-02-28
5188        assert_eq!(-715817, c.value(2)); // 0010-02-28
5189        assert_eq!(c.value(3), c.value(4)); // Expect 0000-01-01 and -0000-01-01 to be parsed the same
5190        assert_eq!(-719528, c.value(3)); // 0000-01-01
5191        assert_eq!(-719528, c.value(4)); // -0000-01-01
5192        assert_eq!(-719893, c.value(5)); // -0001-01-01
5193    }
5194
5195    #[test]
5196    fn test_cast_invalid_string_with_large_date_to_date32() {
5197        // Large dates need to be prefixed with a + or - sign, otherwise they are not parsed correctly
5198        let array = Arc::new(StringArray::from(vec![Some("10999-12-31")])) as ArrayRef;
5199        let to_type = DataType::Date32;
5200        let options = CastOptions {
5201            safe: false,
5202            format_options: FormatOptions::default(),
5203        };
5204        let err = cast_with_options(&array, &to_type, &options).unwrap_err();
5205        assert_eq!(
5206            err.to_string(),
5207            "Cast error: Cannot cast string '10999-12-31' to value of Date32 type"
5208        );
5209    }
5210
5211    #[test]
5212    fn test_cast_string_format_yyyymmdd_to_date32() {
5213        let a0 = Arc::new(StringViewArray::from(vec![
5214            Some("2020-12-25"),
5215            Some("20201117"),
5216        ])) as ArrayRef;
5217        let a1 = Arc::new(StringArray::from(vec![
5218            Some("2020-12-25"),
5219            Some("20201117"),
5220        ])) as ArrayRef;
5221        let a2 = Arc::new(LargeStringArray::from(vec![
5222            Some("2020-12-25"),
5223            Some("20201117"),
5224        ])) as ArrayRef;
5225
5226        for array in &[a0, a1, a2] {
5227            let to_type = DataType::Date32;
5228            let options = CastOptions {
5229                safe: false,
5230                format_options: FormatOptions::default(),
5231            };
5232            let result = cast_with_options(&array, &to_type, &options).unwrap();
5233            let c = result.as_primitive::<Date32Type>();
5234            assert_eq!(
5235                chrono::NaiveDate::from_ymd_opt(2020, 12, 25),
5236                c.value_as_date(0)
5237            );
5238            assert_eq!(
5239                chrono::NaiveDate::from_ymd_opt(2020, 11, 17),
5240                c.value_as_date(1)
5241            );
5242        }
5243    }
5244
5245    #[test]
5246    fn test_cast_string_to_time32second() {
5247        let a0 = Arc::new(StringViewArray::from(vec![
5248            Some("08:08:35.091323414"),
5249            Some("08:08:60.091323414"), // leap second
5250            Some("08:08:61.091323414"), // not valid
5251            Some("Not a valid time"),
5252            None,
5253        ])) as ArrayRef;
5254        let a1 = Arc::new(StringArray::from(vec![
5255            Some("08:08:35.091323414"),
5256            Some("08:08:60.091323414"), // leap second
5257            Some("08:08:61.091323414"), // not valid
5258            Some("Not a valid time"),
5259            None,
5260        ])) as ArrayRef;
5261        let a2 = Arc::new(LargeStringArray::from(vec![
5262            Some("08:08:35.091323414"),
5263            Some("08:08:60.091323414"), // leap second
5264            Some("08:08:61.091323414"), // not valid
5265            Some("Not a valid time"),
5266            None,
5267        ])) as ArrayRef;
5268        for array in &[a0, a1, a2] {
5269            let to_type = DataType::Time32(TimeUnit::Second);
5270            let b = cast(array, &to_type).unwrap();
5271            let c = b.as_primitive::<Time32SecondType>();
5272            assert_eq!(29315, c.value(0));
5273            assert_eq!(29340, c.value(1));
5274            assert!(c.is_null(2));
5275            assert!(c.is_null(3));
5276            assert!(c.is_null(4));
5277
5278            let options = CastOptions {
5279                safe: false,
5280                format_options: FormatOptions::default(),
5281            };
5282            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5283            assert_eq!(
5284                err.to_string(),
5285                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(s) type"
5286            );
5287        }
5288    }
5289
5290    #[test]
5291    fn test_cast_string_to_time32millisecond() {
5292        let a0 = Arc::new(StringViewArray::from(vec![
5293            Some("08:08:35.091323414"),
5294            Some("08:08:60.091323414"), // leap second
5295            Some("08:08:61.091323414"), // not valid
5296            Some("Not a valid time"),
5297            None,
5298        ])) as ArrayRef;
5299        let a1 = Arc::new(StringArray::from(vec![
5300            Some("08:08:35.091323414"),
5301            Some("08:08:60.091323414"), // leap second
5302            Some("08:08:61.091323414"), // not valid
5303            Some("Not a valid time"),
5304            None,
5305        ])) as ArrayRef;
5306        let a2 = Arc::new(LargeStringArray::from(vec![
5307            Some("08:08:35.091323414"),
5308            Some("08:08:60.091323414"), // leap second
5309            Some("08:08:61.091323414"), // not valid
5310            Some("Not a valid time"),
5311            None,
5312        ])) as ArrayRef;
5313        for array in &[a0, a1, a2] {
5314            let to_type = DataType::Time32(TimeUnit::Millisecond);
5315            let b = cast(array, &to_type).unwrap();
5316            let c = b.as_primitive::<Time32MillisecondType>();
5317            assert_eq!(29315091, c.value(0));
5318            assert_eq!(29340091, c.value(1));
5319            assert!(c.is_null(2));
5320            assert!(c.is_null(3));
5321            assert!(c.is_null(4));
5322
5323            let options = CastOptions {
5324                safe: false,
5325                format_options: FormatOptions::default(),
5326            };
5327            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5328            assert_eq!(
5329                err.to_string(),
5330                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(ms) type"
5331            );
5332        }
5333    }
5334
5335    #[test]
5336    fn test_cast_string_to_time64microsecond() {
5337        let a0 = Arc::new(StringViewArray::from(vec![
5338            Some("08:08:35.091323414"),
5339            Some("Not a valid time"),
5340            None,
5341        ])) as ArrayRef;
5342        let a1 = Arc::new(StringArray::from(vec![
5343            Some("08:08:35.091323414"),
5344            Some("Not a valid time"),
5345            None,
5346        ])) as ArrayRef;
5347        let a2 = Arc::new(LargeStringArray::from(vec![
5348            Some("08:08:35.091323414"),
5349            Some("Not a valid time"),
5350            None,
5351        ])) as ArrayRef;
5352        for array in &[a0, a1, a2] {
5353            let to_type = DataType::Time64(TimeUnit::Microsecond);
5354            let b = cast(array, &to_type).unwrap();
5355            let c = b.as_primitive::<Time64MicrosecondType>();
5356            assert_eq!(29315091323, c.value(0));
5357            assert!(c.is_null(1));
5358            assert!(c.is_null(2));
5359
5360            let options = CastOptions {
5361                safe: false,
5362                format_options: FormatOptions::default(),
5363            };
5364            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5365            assert_eq!(
5366                err.to_string(),
5367                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(µs) type"
5368            );
5369        }
5370    }
5371
5372    #[test]
5373    fn test_cast_string_to_time64nanosecond() {
5374        let a0 = Arc::new(StringViewArray::from(vec![
5375            Some("08:08:35.091323414"),
5376            Some("Not a valid time"),
5377            None,
5378        ])) as ArrayRef;
5379        let a1 = Arc::new(StringArray::from(vec![
5380            Some("08:08:35.091323414"),
5381            Some("Not a valid time"),
5382            None,
5383        ])) as ArrayRef;
5384        let a2 = Arc::new(LargeStringArray::from(vec![
5385            Some("08:08:35.091323414"),
5386            Some("Not a valid time"),
5387            None,
5388        ])) as ArrayRef;
5389        for array in &[a0, a1, a2] {
5390            let to_type = DataType::Time64(TimeUnit::Nanosecond);
5391            let b = cast(array, &to_type).unwrap();
5392            let c = b.as_primitive::<Time64NanosecondType>();
5393            assert_eq!(29315091323414, c.value(0));
5394            assert!(c.is_null(1));
5395            assert!(c.is_null(2));
5396
5397            let options = CastOptions {
5398                safe: false,
5399                format_options: FormatOptions::default(),
5400            };
5401            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5402            assert_eq!(
5403                err.to_string(),
5404                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(ns) type"
5405            );
5406        }
5407    }
5408
5409    #[test]
5410    fn test_cast_string_to_date64() {
5411        let a0 = Arc::new(StringViewArray::from(vec![
5412            Some("2020-09-08T12:00:00"),
5413            Some("Not a valid date"),
5414            None,
5415        ])) as ArrayRef;
5416        let a1 = Arc::new(StringArray::from(vec![
5417            Some("2020-09-08T12:00:00"),
5418            Some("Not a valid date"),
5419            None,
5420        ])) as ArrayRef;
5421        let a2 = Arc::new(LargeStringArray::from(vec![
5422            Some("2020-09-08T12:00:00"),
5423            Some("Not a valid date"),
5424            None,
5425        ])) as ArrayRef;
5426        for array in &[a0, a1, a2] {
5427            let to_type = DataType::Date64;
5428            let b = cast(array, &to_type).unwrap();
5429            let c = b.as_primitive::<Date64Type>();
5430            assert_eq!(1599566400000, c.value(0));
5431            assert!(c.is_null(1));
5432            assert!(c.is_null(2));
5433
5434            let options = CastOptions {
5435                safe: false,
5436                format_options: FormatOptions::default(),
5437            };
5438            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5439            assert_eq!(
5440                err.to_string(),
5441                "Cast error: Cannot cast string 'Not a valid date' to value of Date64 type"
5442            );
5443        }
5444    }
5445
5446    macro_rules! test_safe_string_to_interval {
5447        ($data_vec:expr, $interval_unit:expr, $array_ty:ty, $expect_vec:expr) => {
5448            let source_string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5449
5450            let options = CastOptions {
5451                safe: true,
5452                format_options: FormatOptions::default(),
5453            };
5454
5455            let target_interval_array = cast_with_options(
5456                &source_string_array.clone(),
5457                &DataType::Interval($interval_unit),
5458                &options,
5459            )
5460            .unwrap()
5461            .as_any()
5462            .downcast_ref::<$array_ty>()
5463            .unwrap()
5464            .clone() as $array_ty;
5465
5466            let target_string_array =
5467                cast_with_options(&target_interval_array, &DataType::Utf8, &options)
5468                    .unwrap()
5469                    .as_any()
5470                    .downcast_ref::<StringArray>()
5471                    .unwrap()
5472                    .clone();
5473
5474            let expect_string_array = StringArray::from($expect_vec);
5475
5476            assert_eq!(target_string_array, expect_string_array);
5477
5478            let target_large_string_array =
5479                cast_with_options(&target_interval_array, &DataType::LargeUtf8, &options)
5480                    .unwrap()
5481                    .as_any()
5482                    .downcast_ref::<LargeStringArray>()
5483                    .unwrap()
5484                    .clone();
5485
5486            let expect_large_string_array = LargeStringArray::from($expect_vec);
5487
5488            assert_eq!(target_large_string_array, expect_large_string_array);
5489        };
5490    }
5491
5492    #[test]
5493    fn test_cast_string_to_interval_year_month() {
5494        test_safe_string_to_interval!(
5495            vec![
5496                Some("1 year 1 month"),
5497                Some("1.5 years 13 month"),
5498                Some("30 days"),
5499                Some("31 days"),
5500                Some("2 months 31 days"),
5501                Some("2 months 31 days 1 second"),
5502                Some("foobar"),
5503            ],
5504            IntervalUnit::YearMonth,
5505            IntervalYearMonthArray,
5506            vec![
5507                Some("1 years 1 mons"),
5508                Some("2 years 7 mons"),
5509                None,
5510                None,
5511                None,
5512                None,
5513                None,
5514            ]
5515        );
5516    }
5517
5518    #[test]
5519    fn test_cast_string_to_interval_day_time() {
5520        test_safe_string_to_interval!(
5521            vec![
5522                Some("1 year 1 month"),
5523                Some("1.5 years 13 month"),
5524                Some("30 days"),
5525                Some("1 day 2 second 3.5 milliseconds"),
5526                Some("foobar"),
5527            ],
5528            IntervalUnit::DayTime,
5529            IntervalDayTimeArray,
5530            vec![
5531                Some("390 days"),
5532                Some("930 days"),
5533                Some("30 days"),
5534                None,
5535                None,
5536            ]
5537        );
5538    }
5539
5540    #[test]
5541    fn test_cast_string_to_interval_month_day_nano() {
5542        test_safe_string_to_interval!(
5543            vec![
5544                Some("1 year 1 month 1 day"),
5545                None,
5546                Some("1.5 years 13 month 35 days 1.4 milliseconds"),
5547                Some("3 days"),
5548                Some("8 seconds"),
5549                None,
5550                Some("1 day 29800 milliseconds"),
5551                Some("3 months 1 second"),
5552                Some("6 minutes 120 second"),
5553                Some("2 years 39 months 9 days 19 hours 1 minute 83 seconds 399222 milliseconds"),
5554                Some("foobar"),
5555            ],
5556            IntervalUnit::MonthDayNano,
5557            IntervalMonthDayNanoArray,
5558            vec![
5559                Some("13 mons 1 days"),
5560                None,
5561                Some("31 mons 35 days 0.001400000 secs"),
5562                Some("3 days"),
5563                Some("8.000000000 secs"),
5564                None,
5565                Some("1 days 29.800000000 secs"),
5566                Some("3 mons 1.000000000 secs"),
5567                Some("8 mins"),
5568                Some("63 mons 9 days 19 hours 9 mins 2.222000000 secs"),
5569                None,
5570            ]
5571        );
5572    }
5573
5574    macro_rules! test_unsafe_string_to_interval_err {
5575        ($data_vec:expr, $interval_unit:expr, $error_msg:expr) => {
5576            let string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5577            let options = CastOptions {
5578                safe: false,
5579                format_options: FormatOptions::default(),
5580            };
5581            let arrow_err = cast_with_options(
5582                &string_array.clone(),
5583                &DataType::Interval($interval_unit),
5584                &options,
5585            )
5586            .unwrap_err();
5587            assert_eq!($error_msg, arrow_err.to_string());
5588        };
5589    }
5590
5591    #[test]
5592    fn test_cast_string_to_interval_err() {
5593        test_unsafe_string_to_interval_err!(
5594            vec![Some("foobar")],
5595            IntervalUnit::YearMonth,
5596            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5597        );
5598        test_unsafe_string_to_interval_err!(
5599            vec![Some("foobar")],
5600            IntervalUnit::DayTime,
5601            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5602        );
5603        test_unsafe_string_to_interval_err!(
5604            vec![Some("foobar")],
5605            IntervalUnit::MonthDayNano,
5606            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5607        );
5608        test_unsafe_string_to_interval_err!(
5609            vec![Some("2 months 31 days 1 second")],
5610            IntervalUnit::YearMonth,
5611            r#"Cast error: Cannot cast 2 months 31 days 1 second to IntervalYearMonth. Only year and month fields are allowed."#
5612        );
5613        test_unsafe_string_to_interval_err!(
5614            vec![Some("1 day 1.5 milliseconds")],
5615            IntervalUnit::DayTime,
5616            r#"Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"#
5617        );
5618
5619        // overflow
5620        test_unsafe_string_to_interval_err!(
5621            vec![Some(format!(
5622                "{} century {} year {} month",
5623                i64::MAX - 2,
5624                i64::MAX - 2,
5625                i64::MAX - 2
5626            ))],
5627            IntervalUnit::DayTime,
5628            format!(
5629                "Arithmetic overflow: Overflow happened on: {} * 100",
5630                i64::MAX - 2
5631            )
5632        );
5633        test_unsafe_string_to_interval_err!(
5634            vec![Some(format!(
5635                "{} year {} month {} day",
5636                i64::MAX - 2,
5637                i64::MAX - 2,
5638                i64::MAX - 2
5639            ))],
5640            IntervalUnit::MonthDayNano,
5641            format!(
5642                "Arithmetic overflow: Overflow happened on: {} * 12",
5643                i64::MAX - 2
5644            )
5645        );
5646    }
5647
5648    #[test]
5649    fn test_cast_binary_to_fixed_size_binary() {
5650        let bytes_1 = "Hiiii".as_bytes();
5651        let bytes_2 = "Hello".as_bytes();
5652
5653        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5654        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5655        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5656
5657        let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
5658        let down_cast = array_ref
5659            .as_any()
5660            .downcast_ref::<FixedSizeBinaryArray>()
5661            .unwrap();
5662        assert_eq!(bytes_1, down_cast.value(0));
5663        assert_eq!(bytes_2, down_cast.value(1));
5664        assert!(down_cast.is_null(2));
5665
5666        let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
5667        let down_cast = array_ref
5668            .as_any()
5669            .downcast_ref::<FixedSizeBinaryArray>()
5670            .unwrap();
5671        assert_eq!(bytes_1, down_cast.value(0));
5672        assert_eq!(bytes_2, down_cast.value(1));
5673        assert!(down_cast.is_null(2));
5674
5675        // test error cases when the length of binary are not same
5676        let bytes_1 = "Hi".as_bytes();
5677        let bytes_2 = "Hello".as_bytes();
5678
5679        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5680        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5681        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5682
5683        let array_ref = cast_with_options(
5684            &a1,
5685            &DataType::FixedSizeBinary(5),
5686            &CastOptions {
5687                safe: false,
5688                format_options: FormatOptions::default(),
5689            },
5690        );
5691        assert!(array_ref.is_err());
5692
5693        let array_ref = cast_with_options(
5694            &a2,
5695            &DataType::FixedSizeBinary(5),
5696            &CastOptions {
5697                safe: false,
5698                format_options: FormatOptions::default(),
5699            },
5700        );
5701        assert!(array_ref.is_err());
5702    }
5703
5704    #[test]
5705    fn test_fixed_size_binary_to_binary() {
5706        let bytes_1 = "Hiiii".as_bytes();
5707        let bytes_2 = "Hello".as_bytes();
5708
5709        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5710        let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
5711
5712        let array_ref = cast(&a1, &DataType::Binary).unwrap();
5713        let down_cast = array_ref.as_binary::<i32>();
5714        assert_eq!(bytes_1, down_cast.value(0));
5715        assert_eq!(bytes_2, down_cast.value(1));
5716        assert!(down_cast.is_null(2));
5717
5718        let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
5719        let down_cast = array_ref.as_binary::<i64>();
5720        assert_eq!(bytes_1, down_cast.value(0));
5721        assert_eq!(bytes_2, down_cast.value(1));
5722        assert!(down_cast.is_null(2));
5723
5724        let array_ref = cast(&a1, &DataType::BinaryView).unwrap();
5725        let down_cast = array_ref.as_binary_view();
5726        assert_eq!(bytes_1, down_cast.value(0));
5727        assert_eq!(bytes_2, down_cast.value(1));
5728        assert!(down_cast.is_null(2));
5729    }
5730
5731    #[test]
5732    fn test_fixed_size_binary_to_dictionary() {
5733        let bytes_1 = "Hiiii".as_bytes();
5734        let bytes_2 = "Hello".as_bytes();
5735
5736        let binary_data = vec![Some(bytes_1), Some(bytes_2), Some(bytes_1), None];
5737        let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
5738
5739        let cast_type = DataType::Dictionary(
5740            Box::new(DataType::Int8),
5741            Box::new(DataType::FixedSizeBinary(5)),
5742        );
5743        let cast_array = cast(&a1, &cast_type).unwrap();
5744        assert_eq!(cast_array.data_type(), &cast_type);
5745        assert_eq!(
5746            array_to_strings(&cast_array),
5747            vec!["4869696969", "48656c6c6f", "4869696969", "null"]
5748        );
5749        // dictionary should only have two distinct values
5750        let dict_array = cast_array.as_dictionary::<Int8Type>();
5751        assert_eq!(dict_array.values().len(), 2);
5752    }
5753
5754    #[test]
5755    fn test_binary_to_dictionary() {
5756        let mut builder = GenericBinaryBuilder::<i32>::new();
5757        builder.append_value(b"hello");
5758        builder.append_value(b"hiiii");
5759        builder.append_value(b"hiiii"); // duplicate
5760        builder.append_null();
5761        builder.append_value(b"rustt");
5762
5763        let a1 = builder.finish();
5764
5765        let cast_type = DataType::Dictionary(
5766            Box::new(DataType::Int8),
5767            Box::new(DataType::FixedSizeBinary(5)),
5768        );
5769        let cast_array = cast(&a1, &cast_type).unwrap();
5770        assert_eq!(cast_array.data_type(), &cast_type);
5771        assert_eq!(
5772            array_to_strings(&cast_array),
5773            vec![
5774                "68656c6c6f",
5775                "6869696969",
5776                "6869696969",
5777                "null",
5778                "7275737474"
5779            ]
5780        );
5781        // dictionary should only have three distinct values
5782        let dict_array = cast_array.as_dictionary::<Int8Type>();
5783        assert_eq!(dict_array.values().len(), 3);
5784    }
5785
5786    #[test]
5787    fn test_cast_string_array_to_dict_utf8_view() {
5788        let array = StringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
5789
5790        let cast_type =
5791            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5792        assert!(can_cast_types(array.data_type(), &cast_type));
5793        let cast_array = cast(&array, &cast_type).unwrap();
5794        assert_eq!(cast_array.data_type(), &cast_type);
5795
5796        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5797        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5798        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
5799
5800        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
5801        let actual: Vec<Option<&str>> = typed.into_iter().collect();
5802        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
5803
5804        let keys = dict_array.keys();
5805        assert!(keys.is_null(1));
5806        assert_eq!(keys.value(0), keys.value(3));
5807        assert_ne!(keys.value(0), keys.value(2));
5808    }
5809
5810    #[test]
5811    fn test_cast_string_array_to_dict_utf8_view_null_vs_literal_null() {
5812        let array = StringArray::from(vec![Some("one"), None, Some("null"), Some("one")]);
5813
5814        let cast_type =
5815            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5816        assert!(can_cast_types(array.data_type(), &cast_type));
5817        let cast_array = cast(&array, &cast_type).unwrap();
5818        assert_eq!(cast_array.data_type(), &cast_type);
5819
5820        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5821        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5822        assert_eq!(dict_array.values().len(), 2);
5823
5824        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
5825        let actual: Vec<Option<&str>> = typed.into_iter().collect();
5826        assert_eq!(actual, vec![Some("one"), None, Some("null"), Some("one")]);
5827
5828        let keys = dict_array.keys();
5829        assert!(keys.is_null(1));
5830        assert_eq!(keys.value(0), keys.value(3));
5831        assert_ne!(keys.value(0), keys.value(2));
5832    }
5833
5834    #[test]
5835    fn test_cast_string_view_array_to_dict_utf8_view() {
5836        let array = StringViewArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
5837
5838        let cast_type =
5839            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5840        assert!(can_cast_types(array.data_type(), &cast_type));
5841        let cast_array = cast(&array, &cast_type).unwrap();
5842        assert_eq!(cast_array.data_type(), &cast_type);
5843
5844        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5845        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5846        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
5847
5848        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
5849        let actual: Vec<Option<&str>> = typed.into_iter().collect();
5850        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
5851
5852        let keys = dict_array.keys();
5853        assert!(keys.is_null(1));
5854        assert_eq!(keys.value(0), keys.value(3));
5855        assert_ne!(keys.value(0), keys.value(2));
5856    }
5857
5858    #[test]
5859    fn test_cast_string_view_slice_to_dict_utf8_view() {
5860        let array = StringViewArray::from(vec![
5861            Some("zero"),
5862            Some("one"),
5863            None,
5864            Some("three"),
5865            Some("one"),
5866        ]);
5867        let view = array.slice(1, 4);
5868
5869        let cast_type =
5870            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
5871        assert!(can_cast_types(view.data_type(), &cast_type));
5872        let cast_array = cast(&view, &cast_type).unwrap();
5873        assert_eq!(cast_array.data_type(), &cast_type);
5874
5875        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5876        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
5877        assert_eq!(dict_array.values().len(), 2);
5878
5879        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
5880        let actual: Vec<Option<&str>> = typed.into_iter().collect();
5881        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
5882
5883        let keys = dict_array.keys();
5884        assert!(keys.is_null(1));
5885        assert_eq!(keys.value(0), keys.value(3));
5886        assert_ne!(keys.value(0), keys.value(2));
5887    }
5888
5889    #[test]
5890    fn test_cast_binary_array_to_dict_binary_view() {
5891        let mut builder = GenericBinaryBuilder::<i32>::new();
5892        builder.append_value(b"hello");
5893        builder.append_value(b"hiiii");
5894        builder.append_value(b"hiiii"); // duplicate
5895        builder.append_null();
5896        builder.append_value(b"rustt");
5897
5898        let array = builder.finish();
5899
5900        let cast_type =
5901            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
5902        assert!(can_cast_types(array.data_type(), &cast_type));
5903        let cast_array = cast(&array, &cast_type).unwrap();
5904        assert_eq!(cast_array.data_type(), &cast_type);
5905
5906        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5907        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
5908        assert_eq!(dict_array.values().len(), 3);
5909
5910        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
5911        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
5912        assert_eq!(
5913            actual,
5914            vec![
5915                Some(b"hello".as_slice()),
5916                Some(b"hiiii".as_slice()),
5917                Some(b"hiiii".as_slice()),
5918                None,
5919                Some(b"rustt".as_slice())
5920            ]
5921        );
5922
5923        let keys = dict_array.keys();
5924        assert!(keys.is_null(3));
5925        assert_eq!(keys.value(1), keys.value(2));
5926        assert_ne!(keys.value(0), keys.value(1));
5927    }
5928
5929    #[test]
5930    fn test_cast_binary_view_array_to_dict_binary_view() {
5931        let view = BinaryViewArray::from_iter([
5932            Some(b"hello".as_slice()),
5933            Some(b"hiiii".as_slice()),
5934            Some(b"hiiii".as_slice()), // duplicate
5935            None,
5936            Some(b"rustt".as_slice()),
5937        ]);
5938
5939        let cast_type =
5940            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
5941        assert!(can_cast_types(view.data_type(), &cast_type));
5942        let cast_array = cast(&view, &cast_type).unwrap();
5943        assert_eq!(cast_array.data_type(), &cast_type);
5944
5945        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5946        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
5947        assert_eq!(dict_array.values().len(), 3);
5948
5949        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
5950        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
5951        assert_eq!(
5952            actual,
5953            vec![
5954                Some(b"hello".as_slice()),
5955                Some(b"hiiii".as_slice()),
5956                Some(b"hiiii".as_slice()),
5957                None,
5958                Some(b"rustt".as_slice())
5959            ]
5960        );
5961
5962        let keys = dict_array.keys();
5963        assert!(keys.is_null(3));
5964        assert_eq!(keys.value(1), keys.value(2));
5965        assert_ne!(keys.value(0), keys.value(1));
5966    }
5967
5968    #[test]
5969    fn test_cast_binary_view_slice_to_dict_binary_view() {
5970        let view = BinaryViewArray::from_iter([
5971            Some(b"hello".as_slice()),
5972            Some(b"hiiii".as_slice()),
5973            Some(b"hiiii".as_slice()), // duplicate
5974            None,
5975            Some(b"rustt".as_slice()),
5976        ]);
5977        let sliced = view.slice(1, 4);
5978
5979        let cast_type =
5980            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
5981        assert!(can_cast_types(sliced.data_type(), &cast_type));
5982        let cast_array = cast(&sliced, &cast_type).unwrap();
5983        assert_eq!(cast_array.data_type(), &cast_type);
5984
5985        let dict_array = cast_array.as_dictionary::<UInt16Type>();
5986        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
5987        assert_eq!(dict_array.values().len(), 2);
5988
5989        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
5990        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
5991        assert_eq!(
5992            actual,
5993            vec![
5994                Some(b"hiiii".as_slice()),
5995                Some(b"hiiii".as_slice()),
5996                None,
5997                Some(b"rustt".as_slice())
5998            ]
5999        );
6000
6001        let keys = dict_array.keys();
6002        assert!(keys.is_null(2));
6003        assert_eq!(keys.value(0), keys.value(1));
6004        assert_ne!(keys.value(0), keys.value(3));
6005    }
6006
6007    #[test]
6008    fn test_cast_string_array_to_dict_utf8_view_key_overflow_u8() {
6009        let array = StringArray::from_iter_values((0..257).map(|i| format!("v{i}")));
6010
6011        let cast_type =
6012            DataType::Dictionary(Box::new(DataType::UInt8), Box::new(DataType::Utf8View));
6013        assert!(can_cast_types(array.data_type(), &cast_type));
6014        let err = cast(&array, &cast_type).unwrap_err();
6015        assert!(matches!(err, ArrowError::DictionaryKeyOverflowError));
6016    }
6017
6018    #[test]
6019    fn test_cast_large_string_array_to_dict_utf8_view() {
6020        let array = LargeStringArray::from(vec![Some("one"), None, Some("three"), Some("one")]);
6021
6022        let cast_type =
6023            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6024        assert!(can_cast_types(array.data_type(), &cast_type));
6025        let cast_array = cast(&array, &cast_type).unwrap();
6026        assert_eq!(cast_array.data_type(), &cast_type);
6027
6028        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6029        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6030        assert_eq!(dict_array.values().len(), 2); // "one" and "three" deduplicated
6031
6032        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6033        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6034        assert_eq!(actual, vec![Some("one"), None, Some("three"), Some("one")]);
6035
6036        let keys = dict_array.keys();
6037        assert!(keys.is_null(1));
6038        assert_eq!(keys.value(0), keys.value(3));
6039        assert_ne!(keys.value(0), keys.value(2));
6040    }
6041
6042    #[test]
6043    fn test_cast_large_binary_array_to_dict_binary_view() {
6044        let mut builder = GenericBinaryBuilder::<i64>::new();
6045        builder.append_value(b"hello");
6046        builder.append_value(b"world");
6047        builder.append_value(b"hello"); // duplicate
6048        builder.append_null();
6049
6050        let array = builder.finish();
6051
6052        let cast_type =
6053            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6054        assert!(can_cast_types(array.data_type(), &cast_type));
6055        let cast_array = cast(&array, &cast_type).unwrap();
6056        assert_eq!(cast_array.data_type(), &cast_type);
6057
6058        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6059        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6060        assert_eq!(dict_array.values().len(), 2); // "hello" and "world" deduplicated
6061
6062        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6063        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6064        assert_eq!(
6065            actual,
6066            vec![
6067                Some(b"hello".as_slice()),
6068                Some(b"world".as_slice()),
6069                Some(b"hello".as_slice()),
6070                None
6071            ]
6072        );
6073
6074        let keys = dict_array.keys();
6075        assert!(keys.is_null(3));
6076        assert_eq!(keys.value(0), keys.value(2));
6077        assert_ne!(keys.value(0), keys.value(1));
6078    }
6079
6080    #[test]
6081    fn test_cast_empty_string_array_to_dict_utf8_view() {
6082        let array = StringArray::from(Vec::<Option<&str>>::new());
6083
6084        let cast_type =
6085            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6086        assert!(can_cast_types(array.data_type(), &cast_type));
6087        let cast_array = cast(&array, &cast_type).unwrap();
6088        assert_eq!(cast_array.data_type(), &cast_type);
6089        assert_eq!(cast_array.len(), 0);
6090    }
6091
6092    #[test]
6093    fn test_cast_empty_binary_array_to_dict_binary_view() {
6094        let array = BinaryArray::from(Vec::<Option<&[u8]>>::new());
6095
6096        let cast_type =
6097            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6098        assert!(can_cast_types(array.data_type(), &cast_type));
6099        let cast_array = cast(&array, &cast_type).unwrap();
6100        assert_eq!(cast_array.data_type(), &cast_type);
6101        assert_eq!(cast_array.len(), 0);
6102    }
6103
6104    #[test]
6105    fn test_cast_all_null_string_array_to_dict_utf8_view() {
6106        let array = StringArray::from(vec![None::<&str>, None, None]);
6107
6108        let cast_type =
6109            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::Utf8View));
6110        assert!(can_cast_types(array.data_type(), &cast_type));
6111        let cast_array = cast(&array, &cast_type).unwrap();
6112        assert_eq!(cast_array.data_type(), &cast_type);
6113        assert_eq!(cast_array.null_count(), 3);
6114
6115        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6116        assert_eq!(dict_array.values().data_type(), &DataType::Utf8View);
6117        assert_eq!(dict_array.values().len(), 0);
6118        assert_eq!(dict_array.keys().null_count(), 3);
6119
6120        let typed = dict_array.downcast_dict::<StringViewArray>().unwrap();
6121        let actual: Vec<Option<&str>> = typed.into_iter().collect();
6122        assert_eq!(actual, vec![None, None, None]);
6123    }
6124
6125    #[test]
6126    fn test_cast_all_null_binary_array_to_dict_binary_view() {
6127        let array = BinaryArray::from(vec![None::<&[u8]>, None, None]);
6128
6129        let cast_type =
6130            DataType::Dictionary(Box::new(DataType::UInt16), Box::new(DataType::BinaryView));
6131        assert!(can_cast_types(array.data_type(), &cast_type));
6132        let cast_array = cast(&array, &cast_type).unwrap();
6133        assert_eq!(cast_array.data_type(), &cast_type);
6134        assert_eq!(cast_array.null_count(), 3);
6135
6136        let dict_array = cast_array.as_dictionary::<UInt16Type>();
6137        assert_eq!(dict_array.values().data_type(), &DataType::BinaryView);
6138        assert_eq!(dict_array.values().len(), 0);
6139        assert_eq!(dict_array.keys().null_count(), 3);
6140
6141        let typed = dict_array.downcast_dict::<BinaryViewArray>().unwrap();
6142        let actual: Vec<Option<&[u8]>> = typed.into_iter().collect();
6143        assert_eq!(actual, vec![None, None, None]);
6144    }
6145
6146    #[test]
6147    fn test_numeric_to_binary() {
6148        let a = Int16Array::from(vec![Some(1), Some(511), None]);
6149
6150        let array_ref = cast(&a, &DataType::Binary).unwrap();
6151        let down_cast = array_ref.as_binary::<i32>();
6152        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6153        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6154        assert!(down_cast.is_null(2));
6155
6156        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6157
6158        let array_ref = cast(&a, &DataType::Binary).unwrap();
6159        let down_cast = array_ref.as_binary::<i32>();
6160        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6161        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6162        assert!(down_cast.is_null(2));
6163    }
6164
6165    #[test]
6166    fn test_numeric_to_large_binary() {
6167        let a = Int16Array::from(vec![Some(1), Some(511), None]);
6168
6169        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6170        let down_cast = array_ref.as_binary::<i64>();
6171        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
6172        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
6173        assert!(down_cast.is_null(2));
6174
6175        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
6176
6177        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
6178        let down_cast = array_ref.as_binary::<i64>();
6179        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
6180        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
6181        assert!(down_cast.is_null(2));
6182    }
6183
6184    #[test]
6185    fn test_cast_date32_to_int32() {
6186        let array = Date32Array::from(vec![10000, 17890]);
6187        let b = cast(&array, &DataType::Int32).unwrap();
6188        let c = b.as_primitive::<Int32Type>();
6189        assert_eq!(10000, c.value(0));
6190        assert_eq!(17890, c.value(1));
6191    }
6192
6193    #[test]
6194    fn test_cast_int32_to_date32() {
6195        let array = Int32Array::from(vec![10000, 17890]);
6196        let b = cast(&array, &DataType::Date32).unwrap();
6197        let c = b.as_primitive::<Date32Type>();
6198        assert_eq!(10000, c.value(0));
6199        assert_eq!(17890, c.value(1));
6200    }
6201
6202    #[test]
6203    fn test_cast_timestamp_to_date32() {
6204        let array =
6205            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6206                .with_timezone("+00:00".to_string());
6207        let b = cast(&array, &DataType::Date32).unwrap();
6208        let c = b.as_primitive::<Date32Type>();
6209        assert_eq!(10000, c.value(0));
6210        assert_eq!(17890, c.value(1));
6211        assert!(c.is_null(2));
6212    }
6213    #[test]
6214    fn test_cast_timestamp_to_date32_zone() {
6215        let strings = StringArray::from_iter([
6216            Some("1970-01-01T00:00:01"),
6217            Some("1970-01-01T23:59:59"),
6218            None,
6219            Some("2020-03-01T02:00:23+00:00"),
6220        ]);
6221        let dt = DataType::Timestamp(TimeUnit::Millisecond, Some("-07:00".into()));
6222        let timestamps = cast(&strings, &dt).unwrap();
6223        let dates = cast(timestamps.as_ref(), &DataType::Date32).unwrap();
6224
6225        let c = dates.as_primitive::<Date32Type>();
6226        let expected = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
6227        assert_eq!(c.value_as_date(0).unwrap(), expected);
6228        assert_eq!(c.value_as_date(1).unwrap(), expected);
6229        assert!(c.is_null(2));
6230        let expected = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
6231        assert_eq!(c.value_as_date(3).unwrap(), expected);
6232    }
6233    #[test]
6234    fn test_cast_timestamp_to_date64() {
6235        let array =
6236            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None]);
6237        let b = cast(&array, &DataType::Date64).unwrap();
6238        let c = b.as_primitive::<Date64Type>();
6239        assert_eq!(864000000005, c.value(0));
6240        assert_eq!(1545696000001, c.value(1));
6241        assert!(c.is_null(2));
6242
6243        let array = TimestampSecondArray::from(vec![Some(864000000005), Some(1545696000001)]);
6244        let b = cast(&array, &DataType::Date64).unwrap();
6245        let c = b.as_primitive::<Date64Type>();
6246        assert_eq!(864000000005000, c.value(0));
6247        assert_eq!(1545696000001000, c.value(1));
6248
6249        // test overflow, safe cast
6250        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6251        let b = cast(&array, &DataType::Date64).unwrap();
6252        assert!(b.is_null(0));
6253        // test overflow, unsafe cast
6254        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
6255        let options = CastOptions {
6256            safe: false,
6257            format_options: FormatOptions::default(),
6258        };
6259        let b = cast_with_options(&array, &DataType::Date64, &options);
6260        assert!(b.is_err());
6261    }
6262
6263    #[test]
6264    fn test_cast_timestamp_to_time64() {
6265        // test timestamp secs
6266        let array = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6267            .with_timezone("+01:00".to_string());
6268        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6269        let c = b.as_primitive::<Time64MicrosecondType>();
6270        assert_eq!(3605000000, c.value(0));
6271        assert_eq!(3601000000, c.value(1));
6272        assert!(c.is_null(2));
6273        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6274        let c = b.as_primitive::<Time64NanosecondType>();
6275        assert_eq!(3605000000000, c.value(0));
6276        assert_eq!(3601000000000, c.value(1));
6277        assert!(c.is_null(2));
6278
6279        // test timestamp milliseconds
6280        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6281            .with_timezone("+01:00".to_string());
6282        let array = Arc::new(a) as ArrayRef;
6283        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6284        let c = b.as_primitive::<Time64MicrosecondType>();
6285        assert_eq!(3605000000, c.value(0));
6286        assert_eq!(3601000000, c.value(1));
6287        assert!(c.is_null(2));
6288        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6289        let c = b.as_primitive::<Time64NanosecondType>();
6290        assert_eq!(3605000000000, c.value(0));
6291        assert_eq!(3601000000000, c.value(1));
6292        assert!(c.is_null(2));
6293
6294        // test timestamp microseconds
6295        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6296            .with_timezone("+01:00".to_string());
6297        let array = Arc::new(a) as ArrayRef;
6298        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6299        let c = b.as_primitive::<Time64MicrosecondType>();
6300        assert_eq!(3605000000, c.value(0));
6301        assert_eq!(3601000000, c.value(1));
6302        assert!(c.is_null(2));
6303        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6304        let c = b.as_primitive::<Time64NanosecondType>();
6305        assert_eq!(3605000000000, c.value(0));
6306        assert_eq!(3601000000000, c.value(1));
6307        assert!(c.is_null(2));
6308
6309        // test timestamp nanoseconds
6310        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
6311            .with_timezone("+01:00".to_string());
6312        let array = Arc::new(a) as ArrayRef;
6313        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
6314        let c = b.as_primitive::<Time64MicrosecondType>();
6315        assert_eq!(3605000000, c.value(0));
6316        assert_eq!(3601000000, c.value(1));
6317        assert!(c.is_null(2));
6318        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
6319        let c = b.as_primitive::<Time64NanosecondType>();
6320        assert_eq!(3605000000000, c.value(0));
6321        assert_eq!(3601000000000, c.value(1));
6322        assert!(c.is_null(2));
6323
6324        // test overflow
6325        let a =
6326            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6327        let array = Arc::new(a) as ArrayRef;
6328        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond));
6329        assert!(b.is_err());
6330        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond));
6331        assert!(b.is_err());
6332        let b = cast(&array, &DataType::Time64(TimeUnit::Millisecond));
6333        assert!(b.is_err());
6334    }
6335
6336    #[test]
6337    fn test_cast_timestamp_to_time32() {
6338        // test timestamp secs
6339        let a = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
6340            .with_timezone("+01:00".to_string());
6341        let array = Arc::new(a) as ArrayRef;
6342        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6343        let c = b.as_primitive::<Time32SecondType>();
6344        assert_eq!(3605, c.value(0));
6345        assert_eq!(3601, c.value(1));
6346        assert!(c.is_null(2));
6347        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6348        let c = b.as_primitive::<Time32MillisecondType>();
6349        assert_eq!(3605000, c.value(0));
6350        assert_eq!(3601000, c.value(1));
6351        assert!(c.is_null(2));
6352
6353        // test timestamp milliseconds
6354        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
6355            .with_timezone("+01:00".to_string());
6356        let array = Arc::new(a) as ArrayRef;
6357        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6358        let c = b.as_primitive::<Time32SecondType>();
6359        assert_eq!(3605, c.value(0));
6360        assert_eq!(3601, c.value(1));
6361        assert!(c.is_null(2));
6362        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6363        let c = b.as_primitive::<Time32MillisecondType>();
6364        assert_eq!(3605000, c.value(0));
6365        assert_eq!(3601000, c.value(1));
6366        assert!(c.is_null(2));
6367
6368        // test timestamp microseconds
6369        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
6370            .with_timezone("+01:00".to_string());
6371        let array = Arc::new(a) as ArrayRef;
6372        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6373        let c = b.as_primitive::<Time32SecondType>();
6374        assert_eq!(3605, c.value(0));
6375        assert_eq!(3601, c.value(1));
6376        assert!(c.is_null(2));
6377        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6378        let c = b.as_primitive::<Time32MillisecondType>();
6379        assert_eq!(3605000, c.value(0));
6380        assert_eq!(3601000, c.value(1));
6381        assert!(c.is_null(2));
6382
6383        // test timestamp nanoseconds
6384        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
6385            .with_timezone("+01:00".to_string());
6386        let array = Arc::new(a) as ArrayRef;
6387        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
6388        let c = b.as_primitive::<Time32SecondType>();
6389        assert_eq!(3605, c.value(0));
6390        assert_eq!(3601, c.value(1));
6391        assert!(c.is_null(2));
6392        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
6393        let c = b.as_primitive::<Time32MillisecondType>();
6394        assert_eq!(3605000, c.value(0));
6395        assert_eq!(3601000, c.value(1));
6396        assert!(c.is_null(2));
6397
6398        // test overflow
6399        let a =
6400            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
6401        let array = Arc::new(a) as ArrayRef;
6402        let b = cast(&array, &DataType::Time32(TimeUnit::Second));
6403        assert!(b.is_err());
6404        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond));
6405        assert!(b.is_err());
6406    }
6407
6408    // Cast Timestamp(_, None) -> Timestamp(_, Some(timezone))
6409    #[test]
6410    fn test_cast_timestamp_with_timezone_1() {
6411        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6412            Some("2000-01-01T00:00:00.123456789"),
6413            Some("2010-01-01T00:00:00.123456789"),
6414            None,
6415        ]));
6416        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6417        let timestamp_array = cast(&string_array, &to_type).unwrap();
6418
6419        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6420        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6421
6422        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6423        let result = string_array.as_string::<i32>();
6424        assert_eq!("2000-01-01T00:00:00.123456+07:00", result.value(0));
6425        assert_eq!("2010-01-01T00:00:00.123456+07:00", result.value(1));
6426        assert!(result.is_null(2));
6427    }
6428
6429    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, None)
6430    #[test]
6431    fn test_cast_timestamp_with_timezone_2() {
6432        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6433            Some("2000-01-01T07:00:00.123456789"),
6434            Some("2010-01-01T07:00:00.123456789"),
6435            None,
6436        ]));
6437        let to_type = DataType::Timestamp(TimeUnit::Millisecond, Some("+0700".into()));
6438        let timestamp_array = cast(&string_array, &to_type).unwrap();
6439
6440        // Check intermediate representation is correct
6441        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6442        let result = string_array.as_string::<i32>();
6443        assert_eq!("2000-01-01T07:00:00.123+07:00", result.value(0));
6444        assert_eq!("2010-01-01T07:00:00.123+07:00", result.value(1));
6445        assert!(result.is_null(2));
6446
6447        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6448        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6449
6450        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6451        let result = string_array.as_string::<i32>();
6452        assert_eq!("2000-01-01T00:00:00.123", result.value(0));
6453        assert_eq!("2010-01-01T00:00:00.123", result.value(1));
6454        assert!(result.is_null(2));
6455    }
6456
6457    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, Some(timezone))
6458    #[test]
6459    fn test_cast_timestamp_with_timezone_3() {
6460        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6461            Some("2000-01-01T07:00:00.123456789"),
6462            Some("2010-01-01T07:00:00.123456789"),
6463            None,
6464        ]));
6465        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6466        let timestamp_array = cast(&string_array, &to_type).unwrap();
6467
6468        // Check intermediate representation is correct
6469        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6470        let result = string_array.as_string::<i32>();
6471        assert_eq!("2000-01-01T07:00:00.123456+07:00", result.value(0));
6472        assert_eq!("2010-01-01T07:00:00.123456+07:00", result.value(1));
6473        assert!(result.is_null(2));
6474
6475        let to_type = DataType::Timestamp(TimeUnit::Second, Some("-08:00".into()));
6476        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6477
6478        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6479        let result = string_array.as_string::<i32>();
6480        assert_eq!("1999-12-31T16:00:00-08:00", result.value(0));
6481        assert_eq!("2009-12-31T16:00:00-08:00", result.value(1));
6482        assert!(result.is_null(2));
6483    }
6484
6485    #[test]
6486    fn test_cast_date64_to_timestamp() {
6487        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6488        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6489        let c = b.as_primitive::<TimestampSecondType>();
6490        assert_eq!(864000000, c.value(0));
6491        assert_eq!(1545696000, c.value(1));
6492        assert!(c.is_null(2));
6493    }
6494
6495    #[test]
6496    fn test_cast_date64_to_timestamp_ms() {
6497        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6498        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
6499        let c = b
6500            .as_any()
6501            .downcast_ref::<TimestampMillisecondArray>()
6502            .unwrap();
6503        assert_eq!(864000000005, c.value(0));
6504        assert_eq!(1545696000001, c.value(1));
6505        assert!(c.is_null(2));
6506    }
6507
6508    #[test]
6509    fn test_cast_date64_to_timestamp_us() {
6510        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6511        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
6512        let c = b
6513            .as_any()
6514            .downcast_ref::<TimestampMicrosecondArray>()
6515            .unwrap();
6516        assert_eq!(864000000005000, c.value(0));
6517        assert_eq!(1545696000001000, c.value(1));
6518        assert!(c.is_null(2));
6519    }
6520
6521    #[test]
6522    fn test_cast_date64_to_timestamp_ns() {
6523        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6524        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
6525        let c = b
6526            .as_any()
6527            .downcast_ref::<TimestampNanosecondArray>()
6528            .unwrap();
6529        assert_eq!(864000000005000000, c.value(0));
6530        assert_eq!(1545696000001000000, c.value(1));
6531        assert!(c.is_null(2));
6532    }
6533
6534    #[test]
6535    fn test_cast_timestamp_to_i64() {
6536        let array =
6537            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6538                .with_timezone("UTC".to_string());
6539        let b = cast(&array, &DataType::Int64).unwrap();
6540        let c = b.as_primitive::<Int64Type>();
6541        assert_eq!(&DataType::Int64, c.data_type());
6542        assert_eq!(864000000005, c.value(0));
6543        assert_eq!(1545696000001, c.value(1));
6544        assert!(c.is_null(2));
6545    }
6546
6547    macro_rules! assert_cast {
6548        ($array:expr, $datatype:expr, $output_array_type: ty, $expected:expr) => {{
6549            assert!(can_cast_types($array.data_type(), &$datatype));
6550            let out = cast(&$array, &$datatype).unwrap();
6551            let actual = out
6552                .as_any()
6553                .downcast_ref::<$output_array_type>()
6554                .unwrap()
6555                .into_iter()
6556                .collect::<Vec<_>>();
6557            assert_eq!(actual, $expected);
6558        }};
6559        ($array:expr, $datatype:expr, $output_array_type: ty, $options:expr, $expected:expr) => {{
6560            assert!(can_cast_types($array.data_type(), &$datatype));
6561            let out = cast_with_options(&$array, &$datatype, &$options).unwrap();
6562            let actual = out
6563                .as_any()
6564                .downcast_ref::<$output_array_type>()
6565                .unwrap()
6566                .into_iter()
6567                .collect::<Vec<_>>();
6568            assert_eq!(actual, $expected);
6569        }};
6570    }
6571
6572    #[test]
6573    fn test_cast_date32_to_string() {
6574        let array = Date32Array::from(vec![Some(0), Some(10000), Some(13036), Some(17890), None]);
6575        let expected = vec![
6576            Some("1970-01-01"),
6577            Some("1997-05-19"),
6578            Some("2005-09-10"),
6579            Some("2018-12-25"),
6580            None,
6581        ];
6582
6583        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6584        assert_cast!(array, DataType::Utf8, StringArray, expected);
6585        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6586    }
6587
6588    #[test]
6589    fn test_cast_date64_to_string() {
6590        let array = Date64Array::from(vec![
6591            Some(0),
6592            Some(10000 * 86400000),
6593            Some(13036 * 86400000),
6594            Some(17890 * 86400000),
6595            None,
6596        ]);
6597        let expected = vec![
6598            Some("1970-01-01T00:00:00"),
6599            Some("1997-05-19T00:00:00"),
6600            Some("2005-09-10T00:00:00"),
6601            Some("2018-12-25T00:00:00"),
6602            None,
6603        ];
6604
6605        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6606        assert_cast!(array, DataType::Utf8, StringArray, expected);
6607        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6608    }
6609
6610    #[test]
6611    fn test_cast_date32_to_timestamp_and_timestamp_with_timezone() {
6612        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6613        let a = Date32Array::from(vec![Some(18628), None, None]); // 2021-1-1, 2022-1-1
6614        let array = Arc::new(a) as ArrayRef;
6615
6616        let b = cast(
6617            &array,
6618            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6619        )
6620        .unwrap();
6621        let c = b.as_primitive::<TimestampSecondType>();
6622        let string_array = cast(&c, &DataType::Utf8).unwrap();
6623        let result = string_array.as_string::<i32>();
6624        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6625
6626        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6627        let c = b.as_primitive::<TimestampSecondType>();
6628        let string_array = cast(&c, &DataType::Utf8).unwrap();
6629        let result = string_array.as_string::<i32>();
6630        assert_eq!("2021-01-01T00:00:00", result.value(0));
6631    }
6632
6633    #[test]
6634    fn test_cast_date32_to_timestamp_with_timezone() {
6635        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6636        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6637        let array = Arc::new(a) as ArrayRef;
6638        let b = cast(
6639            &array,
6640            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6641        )
6642        .unwrap();
6643        let c = b.as_primitive::<TimestampSecondType>();
6644        assert_eq!(1609438500, c.value(0));
6645        assert_eq!(1640974500, c.value(1));
6646        assert!(c.is_null(2));
6647
6648        let string_array = cast(&c, &DataType::Utf8).unwrap();
6649        let result = string_array.as_string::<i32>();
6650        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6651        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6652    }
6653
6654    #[test]
6655    fn test_cast_date32_to_timestamp_with_timezone_ms() {
6656        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6657        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6658        let array = Arc::new(a) as ArrayRef;
6659        let b = cast(
6660            &array,
6661            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
6662        )
6663        .unwrap();
6664        let c = b.as_primitive::<TimestampMillisecondType>();
6665        assert_eq!(1609438500000, c.value(0));
6666        assert_eq!(1640974500000, c.value(1));
6667        assert!(c.is_null(2));
6668
6669        let string_array = cast(&c, &DataType::Utf8).unwrap();
6670        let result = string_array.as_string::<i32>();
6671        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6672        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6673    }
6674
6675    #[test]
6676    fn test_cast_date32_to_timestamp_with_timezone_us() {
6677        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6678        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6679        let array = Arc::new(a) as ArrayRef;
6680        let b = cast(
6681            &array,
6682            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
6683        )
6684        .unwrap();
6685        let c = b.as_primitive::<TimestampMicrosecondType>();
6686        assert_eq!(1609438500000000, c.value(0));
6687        assert_eq!(1640974500000000, c.value(1));
6688        assert!(c.is_null(2));
6689
6690        let string_array = cast(&c, &DataType::Utf8).unwrap();
6691        let result = string_array.as_string::<i32>();
6692        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6693        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6694    }
6695
6696    #[test]
6697    fn test_cast_date32_to_timestamp_with_timezone_ns() {
6698        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6699        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6700        let array = Arc::new(a) as ArrayRef;
6701        let b = cast(
6702            &array,
6703            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
6704        )
6705        .unwrap();
6706        let c = b.as_primitive::<TimestampNanosecondType>();
6707        assert_eq!(1609438500000000000, c.value(0));
6708        assert_eq!(1640974500000000000, c.value(1));
6709        assert!(c.is_null(2));
6710
6711        let string_array = cast(&c, &DataType::Utf8).unwrap();
6712        let result = string_array.as_string::<i32>();
6713        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6714        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6715    }
6716
6717    #[test]
6718    fn test_cast_date64_to_timestamp_with_timezone() {
6719        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6720        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6721        let b = cast(
6722            &array,
6723            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6724        )
6725        .unwrap();
6726
6727        let c = b.as_primitive::<TimestampSecondType>();
6728        assert_eq!(863979300, c.value(0));
6729        assert_eq!(1545675300, c.value(1));
6730        assert!(c.is_null(2));
6731
6732        let string_array = cast(&c, &DataType::Utf8).unwrap();
6733        let result = string_array.as_string::<i32>();
6734        assert_eq!("1997-05-19T00:00:00+05:45", result.value(0));
6735        assert_eq!("2018-12-25T00:00:00+05:45", result.value(1));
6736    }
6737
6738    #[test]
6739    fn test_cast_date64_to_timestamp_with_timezone_ms() {
6740        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6741        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6742        let b = cast(
6743            &array,
6744            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
6745        )
6746        .unwrap();
6747
6748        let c = b.as_primitive::<TimestampMillisecondType>();
6749        assert_eq!(863979300005, c.value(0));
6750        assert_eq!(1545675300001, c.value(1));
6751        assert!(c.is_null(2));
6752
6753        let string_array = cast(&c, &DataType::Utf8).unwrap();
6754        let result = string_array.as_string::<i32>();
6755        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6756        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6757    }
6758
6759    #[test]
6760    fn test_cast_date64_to_timestamp_with_timezone_us() {
6761        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6762        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6763        let b = cast(
6764            &array,
6765            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
6766        )
6767        .unwrap();
6768
6769        let c = b.as_primitive::<TimestampMicrosecondType>();
6770        assert_eq!(863979300005000, c.value(0));
6771        assert_eq!(1545675300001000, c.value(1));
6772        assert!(c.is_null(2));
6773
6774        let string_array = cast(&c, &DataType::Utf8).unwrap();
6775        let result = string_array.as_string::<i32>();
6776        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6777        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6778    }
6779
6780    #[test]
6781    fn test_cast_date64_to_timestamp_with_timezone_ns() {
6782        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6783        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6784        let b = cast(
6785            &array,
6786            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
6787        )
6788        .unwrap();
6789
6790        let c = b.as_primitive::<TimestampNanosecondType>();
6791        assert_eq!(863979300005000000, c.value(0));
6792        assert_eq!(1545675300001000000, c.value(1));
6793        assert!(c.is_null(2));
6794
6795        let string_array = cast(&c, &DataType::Utf8).unwrap();
6796        let result = string_array.as_string::<i32>();
6797        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6798        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6799    }
6800
6801    #[test]
6802    fn test_cast_timestamp_to_strings() {
6803        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
6804        let array =
6805            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6806        let expected = vec![
6807            Some("1997-05-19T00:00:03.005"),
6808            Some("2018-12-25T00:00:02.001"),
6809            None,
6810        ];
6811
6812        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6813        assert_cast!(array, DataType::Utf8, StringArray, expected);
6814        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6815    }
6816
6817    #[test]
6818    fn test_cast_timestamp_to_strings_opt() {
6819        let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
6820        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6821        let cast_options = CastOptions {
6822            safe: true,
6823            format_options: FormatOptions::default()
6824                .with_timestamp_format(Some(ts_format))
6825                .with_timestamp_tz_format(Some(ts_format)),
6826        };
6827
6828        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
6829        let array_without_tz =
6830            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6831        let expected = vec![
6832            Some("1997-05-19 00:00:03.005000"),
6833            Some("2018-12-25 00:00:02.001000"),
6834            None,
6835        ];
6836        assert_cast!(
6837            array_without_tz,
6838            DataType::Utf8View,
6839            StringViewArray,
6840            cast_options,
6841            expected
6842        );
6843        assert_cast!(
6844            array_without_tz,
6845            DataType::Utf8,
6846            StringArray,
6847            cast_options,
6848            expected
6849        );
6850        assert_cast!(
6851            array_without_tz,
6852            DataType::LargeUtf8,
6853            LargeStringArray,
6854            cast_options,
6855            expected
6856        );
6857
6858        let array_with_tz =
6859            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None])
6860                .with_timezone(tz.to_string());
6861        let expected = vec![
6862            Some("1997-05-19 05:45:03.005000"),
6863            Some("2018-12-25 05:45:02.001000"),
6864            None,
6865        ];
6866        assert_cast!(
6867            array_with_tz,
6868            DataType::Utf8View,
6869            StringViewArray,
6870            cast_options,
6871            expected
6872        );
6873        assert_cast!(
6874            array_with_tz,
6875            DataType::Utf8,
6876            StringArray,
6877            cast_options,
6878            expected
6879        );
6880        assert_cast!(
6881            array_with_tz,
6882            DataType::LargeUtf8,
6883            LargeStringArray,
6884            cast_options,
6885            expected
6886        );
6887    }
6888
6889    #[test]
6890    fn test_cast_between_timestamps() {
6891        let array =
6892            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6893        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6894        let c = b.as_primitive::<TimestampSecondType>();
6895        assert_eq!(864000003, c.value(0));
6896        assert_eq!(1545696002, c.value(1));
6897        assert!(c.is_null(2));
6898    }
6899
6900    #[test]
6901    fn test_cast_duration_to_i64() {
6902        let base = vec![5, 6, 7, 8, 100000000];
6903
6904        let duration_arrays = vec![
6905            Arc::new(DurationNanosecondArray::from(base.clone())) as ArrayRef,
6906            Arc::new(DurationMicrosecondArray::from(base.clone())) as ArrayRef,
6907            Arc::new(DurationMillisecondArray::from(base.clone())) as ArrayRef,
6908            Arc::new(DurationSecondArray::from(base.clone())) as ArrayRef,
6909        ];
6910
6911        for arr in duration_arrays {
6912            assert!(can_cast_types(arr.data_type(), &DataType::Int64));
6913            let result = cast(&arr, &DataType::Int64).unwrap();
6914            let result = result.as_primitive::<Int64Type>();
6915            assert_eq!(base.as_slice(), result.values());
6916        }
6917    }
6918
6919    #[test]
6920    fn test_cast_between_durations_and_numerics() {
6921        fn test_cast_between_durations<FromType, ToType>()
6922        where
6923            FromType: ArrowPrimitiveType<Native = i64>,
6924            ToType: ArrowPrimitiveType<Native = i64>,
6925            PrimitiveArray<FromType>: From<Vec<Option<i64>>>,
6926        {
6927            let from_unit = match FromType::DATA_TYPE {
6928                DataType::Duration(unit) => unit,
6929                _ => panic!("Expected a duration type"),
6930            };
6931            let to_unit = match ToType::DATA_TYPE {
6932                DataType::Duration(unit) => unit,
6933                _ => panic!("Expected a duration type"),
6934            };
6935            let from_size = time_unit_multiple(&from_unit);
6936            let to_size = time_unit_multiple(&to_unit);
6937
6938            let (v1_before, v2_before) = (8640003005, 1696002001);
6939            let (v1_after, v2_after) = if from_size >= to_size {
6940                (
6941                    v1_before / (from_size / to_size),
6942                    v2_before / (from_size / to_size),
6943                )
6944            } else {
6945                (
6946                    v1_before * (to_size / from_size),
6947                    v2_before * (to_size / from_size),
6948                )
6949            };
6950
6951            let array =
6952                PrimitiveArray::<FromType>::from(vec![Some(v1_before), Some(v2_before), None]);
6953            let b = cast(&array, &ToType::DATA_TYPE).unwrap();
6954            let c = b.as_primitive::<ToType>();
6955            assert_eq!(v1_after, c.value(0));
6956            assert_eq!(v2_after, c.value(1));
6957            assert!(c.is_null(2));
6958        }
6959
6960        // between each individual duration type
6961        test_cast_between_durations::<DurationSecondType, DurationMillisecondType>();
6962        test_cast_between_durations::<DurationSecondType, DurationMicrosecondType>();
6963        test_cast_between_durations::<DurationSecondType, DurationNanosecondType>();
6964        test_cast_between_durations::<DurationMillisecondType, DurationSecondType>();
6965        test_cast_between_durations::<DurationMillisecondType, DurationMicrosecondType>();
6966        test_cast_between_durations::<DurationMillisecondType, DurationNanosecondType>();
6967        test_cast_between_durations::<DurationMicrosecondType, DurationSecondType>();
6968        test_cast_between_durations::<DurationMicrosecondType, DurationMillisecondType>();
6969        test_cast_between_durations::<DurationMicrosecondType, DurationNanosecondType>();
6970        test_cast_between_durations::<DurationNanosecondType, DurationSecondType>();
6971        test_cast_between_durations::<DurationNanosecondType, DurationMillisecondType>();
6972        test_cast_between_durations::<DurationNanosecondType, DurationMicrosecondType>();
6973
6974        // cast failed
6975        let array = DurationSecondArray::from(vec![
6976            Some(i64::MAX),
6977            Some(8640203410378005),
6978            Some(10241096),
6979            None,
6980        ]);
6981        let b = cast(&array, &DataType::Duration(TimeUnit::Nanosecond)).unwrap();
6982        let c = b.as_primitive::<DurationNanosecondType>();
6983        assert!(c.is_null(0));
6984        assert!(c.is_null(1));
6985        assert_eq!(10241096000000000, c.value(2));
6986        assert!(c.is_null(3));
6987
6988        // durations to numerics
6989        let array = DurationSecondArray::from(vec![
6990            Some(i64::MAX),
6991            Some(8640203410378005),
6992            Some(10241096),
6993            None,
6994        ]);
6995        let b = cast(&array, &DataType::Int64).unwrap();
6996        let c = b.as_primitive::<Int64Type>();
6997        assert_eq!(i64::MAX, c.value(0));
6998        assert_eq!(8640203410378005, c.value(1));
6999        assert_eq!(10241096, c.value(2));
7000        assert!(c.is_null(3));
7001
7002        let b = cast(&array, &DataType::Int32).unwrap();
7003        let c = b.as_primitive::<Int32Type>();
7004        assert_eq!(0, c.value(0));
7005        assert_eq!(0, c.value(1));
7006        assert_eq!(10241096, c.value(2));
7007        assert!(c.is_null(3));
7008
7009        // numerics to durations
7010        let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), Some(10241096), None]);
7011        let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
7012        let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
7013        assert_eq!(i32::MAX as i64, c.value(0));
7014        assert_eq!(802034103, c.value(1));
7015        assert_eq!(10241096, c.value(2));
7016        assert!(c.is_null(3));
7017    }
7018
7019    #[test]
7020    fn test_cast_to_strings() {
7021        let a = Int32Array::from(vec![1, 2, 3]);
7022        let out = cast(&a, &DataType::Utf8).unwrap();
7023        let out = out
7024            .as_any()
7025            .downcast_ref::<StringArray>()
7026            .unwrap()
7027            .into_iter()
7028            .collect::<Vec<_>>();
7029        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
7030        let out = cast(&a, &DataType::LargeUtf8).unwrap();
7031        let out = out
7032            .as_any()
7033            .downcast_ref::<LargeStringArray>()
7034            .unwrap()
7035            .into_iter()
7036            .collect::<Vec<_>>();
7037        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
7038    }
7039
7040    #[test]
7041    fn test_str_to_str_casts() {
7042        for data in [
7043            vec![Some("foo"), Some("bar"), Some("ham")],
7044            vec![Some("foo"), None, Some("bar")],
7045        ] {
7046            let a = LargeStringArray::from(data.clone());
7047            let to = cast(&a, &DataType::Utf8).unwrap();
7048            let expect = a
7049                .as_any()
7050                .downcast_ref::<LargeStringArray>()
7051                .unwrap()
7052                .into_iter()
7053                .collect::<Vec<_>>();
7054            let out = to
7055                .as_any()
7056                .downcast_ref::<StringArray>()
7057                .unwrap()
7058                .into_iter()
7059                .collect::<Vec<_>>();
7060            assert_eq!(expect, out);
7061
7062            let a = StringArray::from(data);
7063            let to = cast(&a, &DataType::LargeUtf8).unwrap();
7064            let expect = a
7065                .as_any()
7066                .downcast_ref::<StringArray>()
7067                .unwrap()
7068                .into_iter()
7069                .collect::<Vec<_>>();
7070            let out = to
7071                .as_any()
7072                .downcast_ref::<LargeStringArray>()
7073                .unwrap()
7074                .into_iter()
7075                .collect::<Vec<_>>();
7076            assert_eq!(expect, out);
7077        }
7078    }
7079
7080    const VIEW_TEST_DATA: [Option<&str>; 5] = [
7081        Some("hello"),
7082        Some("repeated"),
7083        None,
7084        Some("large payload over 12 bytes"),
7085        Some("repeated"),
7086    ];
7087
7088    #[test]
7089    fn test_string_view_to_binary_view() {
7090        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7091
7092        assert!(can_cast_types(
7093            string_view_array.data_type(),
7094            &DataType::BinaryView
7095        ));
7096
7097        let binary_view_array = cast(&string_view_array, &DataType::BinaryView).unwrap();
7098        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7099
7100        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7101        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7102    }
7103
7104    #[test]
7105    fn test_binary_view_to_string_view() {
7106        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7107
7108        assert!(can_cast_types(
7109            binary_view_array.data_type(),
7110            &DataType::Utf8View
7111        ));
7112
7113        let string_view_array = cast(&binary_view_array, &DataType::Utf8View).unwrap();
7114        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7115
7116        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7117        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7118    }
7119
7120    #[test]
7121    fn test_binary_view_to_string_view_with_invalid_utf8() {
7122        let binary_view_array = BinaryViewArray::from_iter(vec![
7123            Some("valid".as_bytes()),
7124            Some(&[0xff]),
7125            Some("utf8".as_bytes()),
7126            None,
7127        ]);
7128
7129        let strict_options = CastOptions {
7130            safe: false,
7131            ..Default::default()
7132        };
7133
7134        assert!(
7135            cast_with_options(&binary_view_array, &DataType::Utf8View, &strict_options).is_err()
7136        );
7137
7138        let safe_options = CastOptions {
7139            safe: true,
7140            ..Default::default()
7141        };
7142
7143        let string_view_array =
7144            cast_with_options(&binary_view_array, &DataType::Utf8View, &safe_options).unwrap();
7145        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7146
7147        let values: Vec<_> = string_view_array.as_string_view().iter().collect();
7148
7149        assert_eq!(values, vec![Some("valid"), None, Some("utf8"), None]);
7150    }
7151
7152    #[test]
7153    fn test_string_to_view() {
7154        _test_string_to_view::<i32>();
7155        _test_string_to_view::<i64>();
7156    }
7157
7158    fn _test_string_to_view<O>()
7159    where
7160        O: OffsetSizeTrait,
7161    {
7162        let string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7163
7164        assert!(can_cast_types(
7165            string_array.data_type(),
7166            &DataType::Utf8View
7167        ));
7168
7169        assert!(can_cast_types(
7170            string_array.data_type(),
7171            &DataType::BinaryView
7172        ));
7173
7174        let string_view_array = cast(&string_array, &DataType::Utf8View).unwrap();
7175        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7176
7177        let binary_view_array = cast(&string_array, &DataType::BinaryView).unwrap();
7178        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7179
7180        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7181        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7182
7183        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7184        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7185    }
7186
7187    #[test]
7188    fn test_bianry_to_view() {
7189        _test_binary_to_view::<i32>();
7190        _test_binary_to_view::<i64>();
7191    }
7192
7193    fn _test_binary_to_view<O>()
7194    where
7195        O: OffsetSizeTrait,
7196    {
7197        let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7198
7199        assert!(can_cast_types(
7200            binary_array.data_type(),
7201            &DataType::Utf8View
7202        ));
7203
7204        assert!(can_cast_types(
7205            binary_array.data_type(),
7206            &DataType::BinaryView
7207        ));
7208
7209        let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
7210        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
7211
7212        let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
7213        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
7214
7215        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7216        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
7217
7218        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7219        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
7220    }
7221
7222    #[test]
7223    fn test_dict_to_view() {
7224        let values = StringArray::from_iter(VIEW_TEST_DATA);
7225        let keys = Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
7226        let string_dict_array =
7227            DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
7228        let typed_dict = string_dict_array.downcast_dict::<StringArray>().unwrap();
7229
7230        let string_view_array = {
7231            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7232            for v in typed_dict.into_iter() {
7233                builder.append_option(v);
7234            }
7235            builder.finish()
7236        };
7237        let expected_string_array_type = string_view_array.data_type();
7238        let casted_string_array = cast(&string_dict_array, expected_string_array_type).unwrap();
7239        assert_eq!(casted_string_array.data_type(), expected_string_array_type);
7240        assert_eq!(casted_string_array.as_ref(), &string_view_array);
7241
7242        let binary_buffer = cast(&typed_dict.values(), &DataType::Binary).unwrap();
7243        let binary_dict_array =
7244            DictionaryArray::<Int8Type>::new(typed_dict.keys().clone(), binary_buffer);
7245        let typed_binary_dict = binary_dict_array.downcast_dict::<BinaryArray>().unwrap();
7246
7247        let binary_view_array = {
7248            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7249            for v in typed_binary_dict.into_iter() {
7250                builder.append_option(v);
7251            }
7252            builder.finish()
7253        };
7254        let expected_binary_array_type = binary_view_array.data_type();
7255        let casted_binary_array = cast(&binary_dict_array, expected_binary_array_type).unwrap();
7256        assert_eq!(casted_binary_array.data_type(), expected_binary_array_type);
7257        assert_eq!(casted_binary_array.as_ref(), &binary_view_array);
7258    }
7259
7260    #[test]
7261    fn test_view_to_dict() {
7262        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
7263        let string_dict_array: DictionaryArray<Int8Type> = VIEW_TEST_DATA.into_iter().collect();
7264        let casted_type = string_dict_array.data_type();
7265        let casted_dict_array = cast(&string_view_array, casted_type).unwrap();
7266        assert_eq!(casted_dict_array.data_type(), casted_type);
7267        assert_eq!(casted_dict_array.as_ref(), &string_dict_array);
7268
7269        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7270        let binary_dict_array = string_dict_array.downcast_dict::<StringArray>().unwrap();
7271        let binary_buffer = cast(&binary_dict_array.values(), &DataType::Binary).unwrap();
7272        let binary_dict_array =
7273            DictionaryArray::<Int8Type>::new(binary_dict_array.keys().clone(), binary_buffer);
7274        let casted_type = binary_dict_array.data_type();
7275        let casted_binary_array = cast(&binary_view_array, casted_type).unwrap();
7276        assert_eq!(casted_binary_array.data_type(), casted_type);
7277        assert_eq!(casted_binary_array.as_ref(), &binary_dict_array);
7278    }
7279
7280    #[test]
7281    fn test_view_to_string() {
7282        _test_view_to_string::<i32>();
7283        _test_view_to_string::<i64>();
7284    }
7285
7286    fn _test_view_to_string<O>()
7287    where
7288        O: OffsetSizeTrait,
7289    {
7290        let string_view_array = {
7291            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7292            for s in VIEW_TEST_DATA.iter() {
7293                builder.append_option(*s);
7294            }
7295            builder.finish()
7296        };
7297
7298        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
7299
7300        let expected_string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
7301        let expected_type = expected_string_array.data_type();
7302
7303        assert!(can_cast_types(string_view_array.data_type(), expected_type));
7304        assert!(can_cast_types(binary_view_array.data_type(), expected_type));
7305
7306        let string_view_casted_array = cast(&string_view_array, expected_type).unwrap();
7307        assert_eq!(string_view_casted_array.data_type(), expected_type);
7308        assert_eq!(string_view_casted_array.as_ref(), &expected_string_array);
7309
7310        let binary_view_casted_array = cast(&binary_view_array, expected_type).unwrap();
7311        assert_eq!(binary_view_casted_array.data_type(), expected_type);
7312        assert_eq!(binary_view_casted_array.as_ref(), &expected_string_array);
7313    }
7314
7315    #[test]
7316    fn test_view_to_binary() {
7317        _test_view_to_binary::<i32>();
7318        _test_view_to_binary::<i64>();
7319    }
7320
7321    fn _test_view_to_binary<O>()
7322    where
7323        O: OffsetSizeTrait,
7324    {
7325        let view_array = {
7326            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
7327            for s in VIEW_TEST_DATA.iter() {
7328                builder.append_option(*s);
7329            }
7330            builder.finish()
7331        };
7332
7333        let expected_binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
7334        let expected_type = expected_binary_array.data_type();
7335
7336        assert!(can_cast_types(view_array.data_type(), expected_type));
7337
7338        let binary_array = cast(&view_array, expected_type).unwrap();
7339        assert_eq!(binary_array.data_type(), expected_type);
7340
7341        assert_eq!(binary_array.as_ref(), &expected_binary_array);
7342    }
7343
7344    #[test]
7345    fn test_cast_from_f64() {
7346        let f64_values: Vec<f64> = vec![
7347            i64::MIN as f64,
7348            i32::MIN as f64,
7349            i16::MIN as f64,
7350            i8::MIN as f64,
7351            0_f64,
7352            u8::MAX as f64,
7353            u16::MAX as f64,
7354            u32::MAX as f64,
7355            u64::MAX as f64,
7356        ];
7357        let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values));
7358
7359        let f64_expected = vec![
7360            -9223372036854776000.0,
7361            -2147483648.0,
7362            -32768.0,
7363            -128.0,
7364            0.0,
7365            255.0,
7366            65535.0,
7367            4294967295.0,
7368            18446744073709552000.0,
7369        ];
7370        assert_eq!(
7371            f64_expected,
7372            get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
7373                .iter()
7374                .map(|i| i.parse::<f64>().unwrap())
7375                .collect::<Vec<f64>>()
7376        );
7377
7378        let f32_expected = vec![
7379            -9223372000000000000.0,
7380            -2147483600.0,
7381            -32768.0,
7382            -128.0,
7383            0.0,
7384            255.0,
7385            65535.0,
7386            4294967300.0,
7387            18446744000000000000.0,
7388        ];
7389        assert_eq!(
7390            f32_expected,
7391            get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
7392                .iter()
7393                .map(|i| i.parse::<f32>().unwrap())
7394                .collect::<Vec<f32>>()
7395        );
7396
7397        let f16_expected = vec![
7398            f16::from_f64(-9223372000000000000.0),
7399            f16::from_f64(-2147483600.0),
7400            f16::from_f64(-32768.0),
7401            f16::from_f64(-128.0),
7402            f16::from_f64(0.0),
7403            f16::from_f64(255.0),
7404            f16::from_f64(65535.0),
7405            f16::from_f64(4294967300.0),
7406            f16::from_f64(18446744000000000000.0),
7407        ];
7408        assert_eq!(
7409            f16_expected,
7410            get_cast_values::<Float16Type>(&f64_array, &DataType::Float16)
7411                .iter()
7412                .map(|i| i.parse::<f16>().unwrap())
7413                .collect::<Vec<f16>>()
7414        );
7415
7416        let i64_expected = vec![
7417            "-9223372036854775808",
7418            "-2147483648",
7419            "-32768",
7420            "-128",
7421            "0",
7422            "255",
7423            "65535",
7424            "4294967295",
7425            "null",
7426        ];
7427        assert_eq!(
7428            i64_expected,
7429            get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
7430        );
7431
7432        let i32_expected = vec![
7433            "null",
7434            "-2147483648",
7435            "-32768",
7436            "-128",
7437            "0",
7438            "255",
7439            "65535",
7440            "null",
7441            "null",
7442        ];
7443        assert_eq!(
7444            i32_expected,
7445            get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
7446        );
7447
7448        let i16_expected = vec![
7449            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7450        ];
7451        assert_eq!(
7452            i16_expected,
7453            get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
7454        );
7455
7456        let i8_expected = vec![
7457            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7458        ];
7459        assert_eq!(
7460            i8_expected,
7461            get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
7462        );
7463
7464        let u64_expected = vec![
7465            "null",
7466            "null",
7467            "null",
7468            "null",
7469            "0",
7470            "255",
7471            "65535",
7472            "4294967295",
7473            "null",
7474        ];
7475        assert_eq!(
7476            u64_expected,
7477            get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
7478        );
7479
7480        let u32_expected = vec![
7481            "null",
7482            "null",
7483            "null",
7484            "null",
7485            "0",
7486            "255",
7487            "65535",
7488            "4294967295",
7489            "null",
7490        ];
7491        assert_eq!(
7492            u32_expected,
7493            get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
7494        );
7495
7496        let u16_expected = vec![
7497            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7498        ];
7499        assert_eq!(
7500            u16_expected,
7501            get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
7502        );
7503
7504        let u8_expected = vec![
7505            "null", "null", "null", "null", "0", "255", "null", "null", "null",
7506        ];
7507        assert_eq!(
7508            u8_expected,
7509            get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
7510        );
7511    }
7512
7513    #[test]
7514    fn test_cast_from_f32() {
7515        let f32_values: Vec<f32> = vec![
7516            i32::MIN as f32,
7517            i32::MIN as f32,
7518            i16::MIN as f32,
7519            i8::MIN as f32,
7520            0_f32,
7521            u8::MAX as f32,
7522            u16::MAX as f32,
7523            u32::MAX as f32,
7524            u32::MAX as f32,
7525        ];
7526        let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values));
7527
7528        let f64_expected = vec![
7529            "-2147483648.0",
7530            "-2147483648.0",
7531            "-32768.0",
7532            "-128.0",
7533            "0.0",
7534            "255.0",
7535            "65535.0",
7536            "4294967296.0",
7537            "4294967296.0",
7538        ];
7539        assert_eq!(
7540            f64_expected,
7541            get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
7542        );
7543
7544        let f32_expected = vec![
7545            "-2147483600.0",
7546            "-2147483600.0",
7547            "-32768.0",
7548            "-128.0",
7549            "0.0",
7550            "255.0",
7551            "65535.0",
7552            "4294967300.0",
7553            "4294967300.0",
7554        ];
7555        assert_eq!(
7556            f32_expected,
7557            get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
7558        );
7559
7560        let f16_expected = vec![
7561            "-inf", "-inf", "-32768.0", "-128.0", "0.0", "255.0", "inf", "inf", "inf",
7562        ];
7563        assert_eq!(
7564            f16_expected,
7565            get_cast_values::<Float16Type>(&f32_array, &DataType::Float16)
7566        );
7567
7568        let i64_expected = vec![
7569            "-2147483648",
7570            "-2147483648",
7571            "-32768",
7572            "-128",
7573            "0",
7574            "255",
7575            "65535",
7576            "4294967296",
7577            "4294967296",
7578        ];
7579        assert_eq!(
7580            i64_expected,
7581            get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
7582        );
7583
7584        let i32_expected = vec![
7585            "-2147483648",
7586            "-2147483648",
7587            "-32768",
7588            "-128",
7589            "0",
7590            "255",
7591            "65535",
7592            "null",
7593            "null",
7594        ];
7595        assert_eq!(
7596            i32_expected,
7597            get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
7598        );
7599
7600        let i16_expected = vec![
7601            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7602        ];
7603        assert_eq!(
7604            i16_expected,
7605            get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
7606        );
7607
7608        let i8_expected = vec![
7609            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7610        ];
7611        assert_eq!(
7612            i8_expected,
7613            get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
7614        );
7615
7616        let u64_expected = vec![
7617            "null",
7618            "null",
7619            "null",
7620            "null",
7621            "0",
7622            "255",
7623            "65535",
7624            "4294967296",
7625            "4294967296",
7626        ];
7627        assert_eq!(
7628            u64_expected,
7629            get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
7630        );
7631
7632        let u32_expected = vec![
7633            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7634        ];
7635        assert_eq!(
7636            u32_expected,
7637            get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
7638        );
7639
7640        let u16_expected = vec![
7641            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7642        ];
7643        assert_eq!(
7644            u16_expected,
7645            get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
7646        );
7647
7648        let u8_expected = vec![
7649            "null", "null", "null", "null", "0", "255", "null", "null", "null",
7650        ];
7651        assert_eq!(
7652            u8_expected,
7653            get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
7654        );
7655    }
7656
7657    #[test]
7658    fn test_cast_from_uint64() {
7659        let u64_values: Vec<u64> = vec![
7660            0,
7661            u8::MAX as u64,
7662            u16::MAX as u64,
7663            u32::MAX as u64,
7664            u64::MAX,
7665        ];
7666        let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values));
7667
7668        let f64_expected = vec![0.0, 255.0, 65535.0, 4294967295.0, 18446744073709552000.0];
7669        assert_eq!(
7670            f64_expected,
7671            get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
7672                .iter()
7673                .map(|i| i.parse::<f64>().unwrap())
7674                .collect::<Vec<f64>>()
7675        );
7676
7677        let f32_expected = vec![0.0, 255.0, 65535.0, 4294967300.0, 18446744000000000000.0];
7678        assert_eq!(
7679            f32_expected,
7680            get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
7681                .iter()
7682                .map(|i| i.parse::<f32>().unwrap())
7683                .collect::<Vec<f32>>()
7684        );
7685
7686        let f16_expected = vec![
7687            f16::from_f64(0.0),
7688            f16::from_f64(255.0),
7689            f16::from_f64(65535.0),
7690            f16::from_f64(4294967300.0),
7691            f16::from_f64(18446744000000000000.0),
7692        ];
7693        assert_eq!(
7694            f16_expected,
7695            get_cast_values::<Float16Type>(&u64_array, &DataType::Float16)
7696                .iter()
7697                .map(|i| i.parse::<f16>().unwrap())
7698                .collect::<Vec<f16>>()
7699        );
7700
7701        let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
7702        assert_eq!(
7703            i64_expected,
7704            get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
7705        );
7706
7707        let i32_expected = vec!["0", "255", "65535", "null", "null"];
7708        assert_eq!(
7709            i32_expected,
7710            get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
7711        );
7712
7713        let i16_expected = vec!["0", "255", "null", "null", "null"];
7714        assert_eq!(
7715            i16_expected,
7716            get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
7717        );
7718
7719        let i8_expected = vec!["0", "null", "null", "null", "null"];
7720        assert_eq!(
7721            i8_expected,
7722            get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
7723        );
7724
7725        let u64_expected = vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
7726        assert_eq!(
7727            u64_expected,
7728            get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
7729        );
7730
7731        let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
7732        assert_eq!(
7733            u32_expected,
7734            get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
7735        );
7736
7737        let u16_expected = vec!["0", "255", "65535", "null", "null"];
7738        assert_eq!(
7739            u16_expected,
7740            get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
7741        );
7742
7743        let u8_expected = vec!["0", "255", "null", "null", "null"];
7744        assert_eq!(
7745            u8_expected,
7746            get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
7747        );
7748    }
7749
7750    #[test]
7751    fn test_cast_from_uint32() {
7752        let u32_values: Vec<u32> = vec![0, u8::MAX as u32, u16::MAX as u32, u32::MAX];
7753        let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values));
7754
7755        let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
7756        assert_eq!(
7757            f64_expected,
7758            get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
7759        );
7760
7761        let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
7762        assert_eq!(
7763            f32_expected,
7764            get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
7765        );
7766
7767        let f16_expected = vec!["0.0", "255.0", "inf", "inf"];
7768        assert_eq!(
7769            f16_expected,
7770            get_cast_values::<Float16Type>(&u32_array, &DataType::Float16)
7771        );
7772
7773        let i64_expected = vec!["0", "255", "65535", "4294967295"];
7774        assert_eq!(
7775            i64_expected,
7776            get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
7777        );
7778
7779        let i32_expected = vec!["0", "255", "65535", "null"];
7780        assert_eq!(
7781            i32_expected,
7782            get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
7783        );
7784
7785        let i16_expected = vec!["0", "255", "null", "null"];
7786        assert_eq!(
7787            i16_expected,
7788            get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
7789        );
7790
7791        let i8_expected = vec!["0", "null", "null", "null"];
7792        assert_eq!(
7793            i8_expected,
7794            get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
7795        );
7796
7797        let u64_expected = vec!["0", "255", "65535", "4294967295"];
7798        assert_eq!(
7799            u64_expected,
7800            get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
7801        );
7802
7803        let u32_expected = vec!["0", "255", "65535", "4294967295"];
7804        assert_eq!(
7805            u32_expected,
7806            get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
7807        );
7808
7809        let u16_expected = vec!["0", "255", "65535", "null"];
7810        assert_eq!(
7811            u16_expected,
7812            get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
7813        );
7814
7815        let u8_expected = vec!["0", "255", "null", "null"];
7816        assert_eq!(
7817            u8_expected,
7818            get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
7819        );
7820    }
7821
7822    #[test]
7823    fn test_cast_from_uint16() {
7824        let u16_values: Vec<u16> = vec![0, u8::MAX as u16, u16::MAX];
7825        let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values));
7826
7827        let f64_expected = vec!["0.0", "255.0", "65535.0"];
7828        assert_eq!(
7829            f64_expected,
7830            get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
7831        );
7832
7833        let f32_expected = vec!["0.0", "255.0", "65535.0"];
7834        assert_eq!(
7835            f32_expected,
7836            get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
7837        );
7838
7839        let f16_expected = vec!["0.0", "255.0", "inf"];
7840        assert_eq!(
7841            f16_expected,
7842            get_cast_values::<Float16Type>(&u16_array, &DataType::Float16)
7843        );
7844
7845        let i64_expected = vec!["0", "255", "65535"];
7846        assert_eq!(
7847            i64_expected,
7848            get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
7849        );
7850
7851        let i32_expected = vec!["0", "255", "65535"];
7852        assert_eq!(
7853            i32_expected,
7854            get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
7855        );
7856
7857        let i16_expected = vec!["0", "255", "null"];
7858        assert_eq!(
7859            i16_expected,
7860            get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
7861        );
7862
7863        let i8_expected = vec!["0", "null", "null"];
7864        assert_eq!(
7865            i8_expected,
7866            get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
7867        );
7868
7869        let u64_expected = vec!["0", "255", "65535"];
7870        assert_eq!(
7871            u64_expected,
7872            get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
7873        );
7874
7875        let u32_expected = vec!["0", "255", "65535"];
7876        assert_eq!(
7877            u32_expected,
7878            get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
7879        );
7880
7881        let u16_expected = vec!["0", "255", "65535"];
7882        assert_eq!(
7883            u16_expected,
7884            get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
7885        );
7886
7887        let u8_expected = vec!["0", "255", "null"];
7888        assert_eq!(
7889            u8_expected,
7890            get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
7891        );
7892    }
7893
7894    #[test]
7895    fn test_cast_from_uint8() {
7896        let u8_values: Vec<u8> = vec![0, u8::MAX];
7897        let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values));
7898
7899        let f64_expected = vec!["0.0", "255.0"];
7900        assert_eq!(
7901            f64_expected,
7902            get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
7903        );
7904
7905        let f32_expected = vec!["0.0", "255.0"];
7906        assert_eq!(
7907            f32_expected,
7908            get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
7909        );
7910
7911        let f16_expected = vec!["0.0", "255.0"];
7912        assert_eq!(
7913            f16_expected,
7914            get_cast_values::<Float16Type>(&u8_array, &DataType::Float16)
7915        );
7916
7917        let i64_expected = vec!["0", "255"];
7918        assert_eq!(
7919            i64_expected,
7920            get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
7921        );
7922
7923        let i32_expected = vec!["0", "255"];
7924        assert_eq!(
7925            i32_expected,
7926            get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
7927        );
7928
7929        let i16_expected = vec!["0", "255"];
7930        assert_eq!(
7931            i16_expected,
7932            get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
7933        );
7934
7935        let i8_expected = vec!["0", "null"];
7936        assert_eq!(
7937            i8_expected,
7938            get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
7939        );
7940
7941        let u64_expected = vec!["0", "255"];
7942        assert_eq!(
7943            u64_expected,
7944            get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
7945        );
7946
7947        let u32_expected = vec!["0", "255"];
7948        assert_eq!(
7949            u32_expected,
7950            get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
7951        );
7952
7953        let u16_expected = vec!["0", "255"];
7954        assert_eq!(
7955            u16_expected,
7956            get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
7957        );
7958
7959        let u8_expected = vec!["0", "255"];
7960        assert_eq!(
7961            u8_expected,
7962            get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
7963        );
7964    }
7965
7966    #[test]
7967    fn test_cast_from_int64() {
7968        let i64_values: Vec<i64> = vec![
7969            i64::MIN,
7970            i32::MIN as i64,
7971            i16::MIN as i64,
7972            i8::MIN as i64,
7973            0,
7974            i8::MAX as i64,
7975            i16::MAX as i64,
7976            i32::MAX as i64,
7977            i64::MAX,
7978        ];
7979        let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values));
7980
7981        let f64_expected = vec![
7982            -9223372036854776000.0,
7983            -2147483648.0,
7984            -32768.0,
7985            -128.0,
7986            0.0,
7987            127.0,
7988            32767.0,
7989            2147483647.0,
7990            9223372036854776000.0,
7991        ];
7992        assert_eq!(
7993            f64_expected,
7994            get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
7995                .iter()
7996                .map(|i| i.parse::<f64>().unwrap())
7997                .collect::<Vec<f64>>()
7998        );
7999
8000        let f32_expected = vec![
8001            -9223372000000000000.0,
8002            -2147483600.0,
8003            -32768.0,
8004            -128.0,
8005            0.0,
8006            127.0,
8007            32767.0,
8008            2147483600.0,
8009            9223372000000000000.0,
8010        ];
8011        assert_eq!(
8012            f32_expected,
8013            get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
8014                .iter()
8015                .map(|i| i.parse::<f32>().unwrap())
8016                .collect::<Vec<f32>>()
8017        );
8018
8019        let f16_expected = vec![
8020            f16::from_f64(-9223372000000000000.0),
8021            f16::from_f64(-2147483600.0),
8022            f16::from_f64(-32768.0),
8023            f16::from_f64(-128.0),
8024            f16::from_f64(0.0),
8025            f16::from_f64(127.0),
8026            f16::from_f64(32767.0),
8027            f16::from_f64(2147483600.0),
8028            f16::from_f64(9223372000000000000.0),
8029        ];
8030        assert_eq!(
8031            f16_expected,
8032            get_cast_values::<Float16Type>(&i64_array, &DataType::Float16)
8033                .iter()
8034                .map(|i| i.parse::<f16>().unwrap())
8035                .collect::<Vec<f16>>()
8036        );
8037
8038        let i64_expected = vec![
8039            "-9223372036854775808",
8040            "-2147483648",
8041            "-32768",
8042            "-128",
8043            "0",
8044            "127",
8045            "32767",
8046            "2147483647",
8047            "9223372036854775807",
8048        ];
8049        assert_eq!(
8050            i64_expected,
8051            get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
8052        );
8053
8054        let i32_expected = vec![
8055            "null",
8056            "-2147483648",
8057            "-32768",
8058            "-128",
8059            "0",
8060            "127",
8061            "32767",
8062            "2147483647",
8063            "null",
8064        ];
8065        assert_eq!(
8066            i32_expected,
8067            get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
8068        );
8069
8070        assert_eq!(
8071            i32_expected,
8072            get_cast_values::<Date32Type>(&i64_array, &DataType::Date32)
8073        );
8074
8075        let i16_expected = vec![
8076            "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
8077        ];
8078        assert_eq!(
8079            i16_expected,
8080            get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
8081        );
8082
8083        let i8_expected = vec![
8084            "null", "null", "null", "-128", "0", "127", "null", "null", "null",
8085        ];
8086        assert_eq!(
8087            i8_expected,
8088            get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
8089        );
8090
8091        let u64_expected = vec![
8092            "null",
8093            "null",
8094            "null",
8095            "null",
8096            "0",
8097            "127",
8098            "32767",
8099            "2147483647",
8100            "9223372036854775807",
8101        ];
8102        assert_eq!(
8103            u64_expected,
8104            get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
8105        );
8106
8107        let u32_expected = vec![
8108            "null",
8109            "null",
8110            "null",
8111            "null",
8112            "0",
8113            "127",
8114            "32767",
8115            "2147483647",
8116            "null",
8117        ];
8118        assert_eq!(
8119            u32_expected,
8120            get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
8121        );
8122
8123        let u16_expected = vec![
8124            "null", "null", "null", "null", "0", "127", "32767", "null", "null",
8125        ];
8126        assert_eq!(
8127            u16_expected,
8128            get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
8129        );
8130
8131        let u8_expected = vec![
8132            "null", "null", "null", "null", "0", "127", "null", "null", "null",
8133        ];
8134        assert_eq!(
8135            u8_expected,
8136            get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
8137        );
8138    }
8139
8140    #[test]
8141    fn test_cast_from_int32() {
8142        let i32_values: Vec<i32> = vec![
8143            i32::MIN,
8144            i16::MIN as i32,
8145            i8::MIN as i32,
8146            0,
8147            i8::MAX as i32,
8148            i16::MAX as i32,
8149            i32::MAX,
8150        ];
8151        let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values));
8152
8153        let f64_expected = vec![
8154            "-2147483648.0",
8155            "-32768.0",
8156            "-128.0",
8157            "0.0",
8158            "127.0",
8159            "32767.0",
8160            "2147483647.0",
8161        ];
8162        assert_eq!(
8163            f64_expected,
8164            get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
8165        );
8166
8167        let f32_expected = vec![
8168            "-2147483600.0",
8169            "-32768.0",
8170            "-128.0",
8171            "0.0",
8172            "127.0",
8173            "32767.0",
8174            "2147483600.0",
8175        ];
8176        assert_eq!(
8177            f32_expected,
8178            get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
8179        );
8180
8181        let f16_expected = vec![
8182            f16::from_f64(-2147483600.0),
8183            f16::from_f64(-32768.0),
8184            f16::from_f64(-128.0),
8185            f16::from_f64(0.0),
8186            f16::from_f64(127.0),
8187            f16::from_f64(32767.0),
8188            f16::from_f64(2147483600.0),
8189        ];
8190        assert_eq!(
8191            f16_expected,
8192            get_cast_values::<Float16Type>(&i32_array, &DataType::Float16)
8193                .iter()
8194                .map(|i| i.parse::<f16>().unwrap())
8195                .collect::<Vec<f16>>()
8196        );
8197
8198        let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
8199        assert_eq!(
8200            i16_expected,
8201            get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
8202        );
8203
8204        let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
8205        assert_eq!(
8206            i8_expected,
8207            get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
8208        );
8209
8210        let u64_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8211        assert_eq!(
8212            u64_expected,
8213            get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
8214        );
8215
8216        let u32_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
8217        assert_eq!(
8218            u32_expected,
8219            get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
8220        );
8221
8222        let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
8223        assert_eq!(
8224            u16_expected,
8225            get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
8226        );
8227
8228        let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
8229        assert_eq!(
8230            u8_expected,
8231            get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
8232        );
8233
8234        // The date32 to date64 cast increases the numerical values in order to keep the same dates.
8235        let i64_expected = vec![
8236            "-185542587187200000",
8237            "-2831155200000",
8238            "-11059200000",
8239            "0",
8240            "10972800000",
8241            "2831068800000",
8242            "185542587100800000",
8243        ];
8244        assert_eq!(
8245            i64_expected,
8246            get_cast_values::<Date64Type>(&i32_array, &DataType::Date64)
8247        );
8248    }
8249
8250    #[test]
8251    fn test_cast_from_int16() {
8252        let i16_values: Vec<i16> = vec![i16::MIN, i8::MIN as i16, 0, i8::MAX as i16, i16::MAX];
8253        let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values));
8254
8255        let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8256        assert_eq!(
8257            f64_expected,
8258            get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
8259        );
8260
8261        let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
8262        assert_eq!(
8263            f32_expected,
8264            get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
8265        );
8266
8267        let f16_expected = vec![
8268            f16::from_f64(-32768.0),
8269            f16::from_f64(-128.0),
8270            f16::from_f64(0.0),
8271            f16::from_f64(127.0),
8272            f16::from_f64(32767.0),
8273        ];
8274        assert_eq!(
8275            f16_expected,
8276            get_cast_values::<Float16Type>(&i16_array, &DataType::Float16)
8277                .iter()
8278                .map(|i| i.parse::<f16>().unwrap())
8279                .collect::<Vec<f16>>()
8280        );
8281
8282        let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
8283        assert_eq!(
8284            i64_expected,
8285            get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
8286        );
8287
8288        let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
8289        assert_eq!(
8290            i32_expected,
8291            get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
8292        );
8293
8294        let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
8295        assert_eq!(
8296            i16_expected,
8297            get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
8298        );
8299
8300        let i8_expected = vec!["null", "-128", "0", "127", "null"];
8301        assert_eq!(
8302            i8_expected,
8303            get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
8304        );
8305
8306        let u64_expected = vec!["null", "null", "0", "127", "32767"];
8307        assert_eq!(
8308            u64_expected,
8309            get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
8310        );
8311
8312        let u32_expected = vec!["null", "null", "0", "127", "32767"];
8313        assert_eq!(
8314            u32_expected,
8315            get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
8316        );
8317
8318        let u16_expected = vec!["null", "null", "0", "127", "32767"];
8319        assert_eq!(
8320            u16_expected,
8321            get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
8322        );
8323
8324        let u8_expected = vec!["null", "null", "0", "127", "null"];
8325        assert_eq!(
8326            u8_expected,
8327            get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
8328        );
8329    }
8330
8331    #[test]
8332    fn test_cast_from_date32() {
8333        let i32_values: Vec<i32> = vec![
8334            i32::MIN,
8335            i16::MIN as i32,
8336            i8::MIN as i32,
8337            0,
8338            i8::MAX as i32,
8339            i16::MAX as i32,
8340            i32::MAX,
8341        ];
8342        let date32_array: ArrayRef = Arc::new(Date32Array::from(i32_values));
8343
8344        let i64_expected = vec![
8345            "-2147483648",
8346            "-32768",
8347            "-128",
8348            "0",
8349            "127",
8350            "32767",
8351            "2147483647",
8352        ];
8353        assert_eq!(
8354            i64_expected,
8355            get_cast_values::<Int64Type>(&date32_array, &DataType::Int64)
8356        );
8357    }
8358
8359    #[test]
8360    fn test_cast_from_int8() {
8361        let i8_values: Vec<i8> = vec![i8::MIN, 0, i8::MAX];
8362        let i8_array = Int8Array::from(i8_values);
8363
8364        let f64_expected = vec!["-128.0", "0.0", "127.0"];
8365        assert_eq!(
8366            f64_expected,
8367            get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
8368        );
8369
8370        let f32_expected = vec!["-128.0", "0.0", "127.0"];
8371        assert_eq!(
8372            f32_expected,
8373            get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
8374        );
8375
8376        let f16_expected = vec!["-128.0", "0.0", "127.0"];
8377        assert_eq!(
8378            f16_expected,
8379            get_cast_values::<Float16Type>(&i8_array, &DataType::Float16)
8380        );
8381
8382        let i64_expected = vec!["-128", "0", "127"];
8383        assert_eq!(
8384            i64_expected,
8385            get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
8386        );
8387
8388        let i32_expected = vec!["-128", "0", "127"];
8389        assert_eq!(
8390            i32_expected,
8391            get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
8392        );
8393
8394        let i16_expected = vec!["-128", "0", "127"];
8395        assert_eq!(
8396            i16_expected,
8397            get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
8398        );
8399
8400        let i8_expected = vec!["-128", "0", "127"];
8401        assert_eq!(
8402            i8_expected,
8403            get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
8404        );
8405
8406        let u64_expected = vec!["null", "0", "127"];
8407        assert_eq!(
8408            u64_expected,
8409            get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
8410        );
8411
8412        let u32_expected = vec!["null", "0", "127"];
8413        assert_eq!(
8414            u32_expected,
8415            get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
8416        );
8417
8418        let u16_expected = vec!["null", "0", "127"];
8419        assert_eq!(
8420            u16_expected,
8421            get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
8422        );
8423
8424        let u8_expected = vec!["null", "0", "127"];
8425        assert_eq!(
8426            u8_expected,
8427            get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
8428        );
8429    }
8430
8431    /// Convert `array` into a vector of strings by casting to data type dt
8432    fn get_cast_values<T>(array: &dyn Array, dt: &DataType) -> Vec<String>
8433    where
8434        T: ArrowPrimitiveType,
8435    {
8436        let c = cast(array, dt).unwrap();
8437        let a = c.as_primitive::<T>();
8438        let mut v: Vec<String> = vec![];
8439        for i in 0..array.len() {
8440            if a.is_null(i) {
8441                v.push("null".to_string())
8442            } else {
8443                v.push(format!("{:?}", a.value(i)));
8444            }
8445        }
8446        v
8447    }
8448
8449    #[test]
8450    fn test_cast_utf8_dict() {
8451        // FROM a dictionary with of Utf8 values
8452        let mut builder = StringDictionaryBuilder::<Int8Type>::new();
8453        builder.append("one").unwrap();
8454        builder.append_null();
8455        builder.append("three").unwrap();
8456        let array: ArrayRef = Arc::new(builder.finish());
8457
8458        let expected = vec!["one", "null", "three"];
8459
8460        // Test casting TO StringArray
8461        let cast_type = Utf8;
8462        let cast_array = cast(&array, &cast_type).expect("cast to UTF-8 failed");
8463        assert_eq!(cast_array.data_type(), &cast_type);
8464        assert_eq!(array_to_strings(&cast_array), expected);
8465
8466        // Test casting TO Dictionary (with different index sizes)
8467
8468        let cast_type = Dictionary(Box::new(Int16), Box::new(Utf8));
8469        let cast_array = cast(&array, &cast_type).expect("cast failed");
8470        assert_eq!(cast_array.data_type(), &cast_type);
8471        assert_eq!(array_to_strings(&cast_array), expected);
8472
8473        let cast_type = Dictionary(Box::new(Int32), Box::new(Utf8));
8474        let cast_array = cast(&array, &cast_type).expect("cast failed");
8475        assert_eq!(cast_array.data_type(), &cast_type);
8476        assert_eq!(array_to_strings(&cast_array), expected);
8477
8478        let cast_type = Dictionary(Box::new(Int64), Box::new(Utf8));
8479        let cast_array = cast(&array, &cast_type).expect("cast failed");
8480        assert_eq!(cast_array.data_type(), &cast_type);
8481        assert_eq!(array_to_strings(&cast_array), expected);
8482
8483        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8484        let cast_array = cast(&array, &cast_type).expect("cast failed");
8485        assert_eq!(cast_array.data_type(), &cast_type);
8486        assert_eq!(array_to_strings(&cast_array), expected);
8487
8488        let cast_type = Dictionary(Box::new(UInt16), Box::new(Utf8));
8489        let cast_array = cast(&array, &cast_type).expect("cast failed");
8490        assert_eq!(cast_array.data_type(), &cast_type);
8491        assert_eq!(array_to_strings(&cast_array), expected);
8492
8493        let cast_type = Dictionary(Box::new(UInt32), Box::new(Utf8));
8494        let cast_array = cast(&array, &cast_type).expect("cast failed");
8495        assert_eq!(cast_array.data_type(), &cast_type);
8496        assert_eq!(array_to_strings(&cast_array), expected);
8497
8498        let cast_type = Dictionary(Box::new(UInt64), Box::new(Utf8));
8499        let cast_array = cast(&array, &cast_type).expect("cast failed");
8500        assert_eq!(cast_array.data_type(), &cast_type);
8501        assert_eq!(array_to_strings(&cast_array), expected);
8502    }
8503
8504    #[test]
8505    fn test_cast_dict_to_dict_bad_index_value_primitive() {
8506        // test converting from an array that has indexes of a type
8507        // that are out of bounds for a particular other kind of
8508        // index.
8509
8510        let mut builder = PrimitiveDictionaryBuilder::<Int32Type, Int64Type>::new();
8511
8512        // add 200 distinct values (which can be stored by a
8513        // dictionary indexed by int32, but not a dictionary indexed
8514        // with int8)
8515        for i in 0..200 {
8516            builder.append(i).unwrap();
8517        }
8518        let array: ArrayRef = Arc::new(builder.finish());
8519
8520        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8521        let res = cast(&array, &cast_type);
8522        assert!(res.is_err());
8523        let actual_error = format!("{res:?}");
8524        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8525        assert!(
8526            actual_error.contains(expected_error),
8527            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8528        );
8529    }
8530
8531    #[test]
8532    fn test_cast_dict_to_dict_bad_index_value_utf8() {
8533        // Same test as test_cast_dict_to_dict_bad_index_value but use
8534        // string values (and encode the expected behavior here);
8535
8536        let mut builder = StringDictionaryBuilder::<Int32Type>::new();
8537
8538        // add 200 distinct values (which can be stored by a
8539        // dictionary indexed by int32, but not a dictionary indexed
8540        // with int8)
8541        for i in 0..200 {
8542            let val = format!("val{i}");
8543            builder.append(&val).unwrap();
8544        }
8545        let array = builder.finish();
8546
8547        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8548        let res = cast(&array, &cast_type);
8549        assert!(res.is_err());
8550        let actual_error = format!("{res:?}");
8551        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8552        assert!(
8553            actual_error.contains(expected_error),
8554            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8555        );
8556    }
8557
8558    #[test]
8559    fn test_cast_primitive_dict() {
8560        // FROM a dictionary with of INT32 values
8561        let mut builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
8562        builder.append(1).unwrap();
8563        builder.append_null();
8564        builder.append(3).unwrap();
8565        let array: ArrayRef = Arc::new(builder.finish());
8566
8567        let expected = vec!["1", "null", "3"];
8568
8569        // Test casting TO PrimitiveArray, different dictionary type
8570        let cast_array = cast(&array, &Utf8).expect("cast to UTF-8 failed");
8571        assert_eq!(array_to_strings(&cast_array), expected);
8572        assert_eq!(cast_array.data_type(), &Utf8);
8573
8574        let cast_array = cast(&array, &Int64).expect("cast to int64 failed");
8575        assert_eq!(array_to_strings(&cast_array), expected);
8576        assert_eq!(cast_array.data_type(), &Int64);
8577    }
8578
8579    #[test]
8580    fn test_cast_primitive_array_to_dict() {
8581        let mut builder = PrimitiveBuilder::<Int32Type>::new();
8582        builder.append_value(1);
8583        builder.append_null();
8584        builder.append_value(3);
8585        let array: ArrayRef = Arc::new(builder.finish());
8586
8587        let expected = vec!["1", "null", "3"];
8588
8589        // Cast to a dictionary (same value type, Int32)
8590        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int32));
8591        let cast_array = cast(&array, &cast_type).expect("cast failed");
8592        assert_eq!(cast_array.data_type(), &cast_type);
8593        assert_eq!(array_to_strings(&cast_array), expected);
8594
8595        // Cast to a dictionary (different value type, Int8)
8596        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int8));
8597        let cast_array = cast(&array, &cast_type).expect("cast failed");
8598        assert_eq!(cast_array.data_type(), &cast_type);
8599        assert_eq!(array_to_strings(&cast_array), expected);
8600    }
8601
8602    #[test]
8603    fn test_cast_time_array_to_dict() {
8604        use DataType::*;
8605
8606        let array = Arc::new(Date32Array::from(vec![Some(1000), None, Some(2000)])) as ArrayRef;
8607
8608        let expected = vec!["1972-09-27", "null", "1975-06-24"];
8609
8610        let cast_type = Dictionary(Box::new(UInt8), Box::new(Date32));
8611        let cast_array = cast(&array, &cast_type).expect("cast failed");
8612        assert_eq!(cast_array.data_type(), &cast_type);
8613        assert_eq!(array_to_strings(&cast_array), expected);
8614    }
8615
8616    #[test]
8617    fn test_cast_timestamp_array_to_dict() {
8618        use DataType::*;
8619
8620        let array = Arc::new(
8621            TimestampSecondArray::from(vec![Some(1000), None, Some(2000)]).with_timezone_utc(),
8622        ) as ArrayRef;
8623
8624        let expected = vec!["1970-01-01T00:16:40", "null", "1970-01-01T00:33:20"];
8625
8626        let cast_type = Dictionary(Box::new(UInt8), Box::new(Timestamp(TimeUnit::Second, None)));
8627        let cast_array = cast(&array, &cast_type).expect("cast failed");
8628        assert_eq!(cast_array.data_type(), &cast_type);
8629        assert_eq!(array_to_strings(&cast_array), expected);
8630    }
8631
8632    #[test]
8633    fn test_cast_string_array_to_dict() {
8634        use DataType::*;
8635
8636        let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) as ArrayRef;
8637
8638        let expected = vec!["one", "null", "three"];
8639
8640        // Cast to a dictionary (same value type, Utf8)
8641        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8642        let cast_array = cast(&array, &cast_type).expect("cast failed");
8643        assert_eq!(cast_array.data_type(), &cast_type);
8644        assert_eq!(array_to_strings(&cast_array), expected);
8645    }
8646
8647    #[test]
8648    fn test_cast_null_array_to_from_decimal_array() {
8649        let data_type = DataType::Decimal128(12, 4);
8650        let array = new_null_array(&DataType::Null, 4);
8651        assert_eq!(array.data_type(), &DataType::Null);
8652        let cast_array = cast(&array, &data_type).expect("cast failed");
8653        assert_eq!(cast_array.data_type(), &data_type);
8654        for i in 0..4 {
8655            assert!(cast_array.is_null(i));
8656        }
8657
8658        let array = new_null_array(&data_type, 4);
8659        assert_eq!(array.data_type(), &data_type);
8660        let cast_array = cast(&array, &DataType::Null).expect("cast failed");
8661        assert_eq!(cast_array.data_type(), &DataType::Null);
8662        assert_eq!(cast_array.len(), 4);
8663        assert_eq!(cast_array.logical_nulls().unwrap().null_count(), 4);
8664    }
8665
8666    #[test]
8667    fn test_cast_null_array_from_and_to_primitive_array() {
8668        macro_rules! typed_test {
8669            ($ARR_TYPE:ident, $DATATYPE:ident, $TYPE:tt) => {{
8670                {
8671                    let array = Arc::new(NullArray::new(6)) as ArrayRef;
8672                    let expected = $ARR_TYPE::from(vec![None; 6]);
8673                    let cast_type = DataType::$DATATYPE;
8674                    let cast_array = cast(&array, &cast_type).expect("cast failed");
8675                    let cast_array = cast_array.as_primitive::<$TYPE>();
8676                    assert_eq!(cast_array.data_type(), &cast_type);
8677                    assert_eq!(cast_array, &expected);
8678                }
8679            }};
8680        }
8681
8682        typed_test!(Int16Array, Int16, Int16Type);
8683        typed_test!(Int32Array, Int32, Int32Type);
8684        typed_test!(Int64Array, Int64, Int64Type);
8685
8686        typed_test!(UInt16Array, UInt16, UInt16Type);
8687        typed_test!(UInt32Array, UInt32, UInt32Type);
8688        typed_test!(UInt64Array, UInt64, UInt64Type);
8689
8690        typed_test!(Float16Array, Float16, Float16Type);
8691        typed_test!(Float32Array, Float32, Float32Type);
8692        typed_test!(Float64Array, Float64, Float64Type);
8693
8694        typed_test!(Date32Array, Date32, Date32Type);
8695        typed_test!(Date64Array, Date64, Date64Type);
8696    }
8697
8698    fn cast_from_null_to_other_base(data_type: &DataType, is_complex: bool) {
8699        // Cast from null to data_type
8700        let array = new_null_array(&DataType::Null, 4);
8701        assert_eq!(array.data_type(), &DataType::Null);
8702        let cast_array = cast(&array, data_type).expect("cast failed");
8703        assert_eq!(cast_array.data_type(), data_type);
8704        for i in 0..4 {
8705            if is_complex {
8706                assert!(cast_array.logical_nulls().unwrap().is_null(i));
8707            } else {
8708                assert!(cast_array.is_null(i));
8709            }
8710        }
8711    }
8712
8713    fn cast_from_null_to_other(data_type: &DataType) {
8714        cast_from_null_to_other_base(data_type, false);
8715    }
8716
8717    fn cast_from_null_to_other_complex(data_type: &DataType) {
8718        cast_from_null_to_other_base(data_type, true);
8719    }
8720
8721    #[test]
8722    fn test_cast_null_from_and_to_variable_sized() {
8723        cast_from_null_to_other(&DataType::Utf8);
8724        cast_from_null_to_other(&DataType::LargeUtf8);
8725        cast_from_null_to_other(&DataType::Binary);
8726        cast_from_null_to_other(&DataType::LargeBinary);
8727    }
8728
8729    #[test]
8730    fn test_cast_null_from_and_to_nested_type() {
8731        // Cast null from and to map
8732        let data_type = DataType::Map(
8733            Arc::new(Field::new_struct(
8734                "entry",
8735                vec![
8736                    Field::new("key", DataType::Utf8, false),
8737                    Field::new("value", DataType::Int32, true),
8738                ],
8739                false,
8740            )),
8741            false,
8742        );
8743        cast_from_null_to_other(&data_type);
8744
8745        // Cast null from and to list
8746        let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
8747        cast_from_null_to_other(&data_type);
8748        let data_type = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8749        cast_from_null_to_other(&data_type);
8750        let data_type =
8751            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
8752        cast_from_null_to_other(&data_type);
8753
8754        // Cast null from and to dictionary
8755        let values = vec![None, None, None, None] as Vec<Option<&str>>;
8756        let array: DictionaryArray<Int8Type> = values.into_iter().collect();
8757        let array = Arc::new(array) as ArrayRef;
8758        let data_type = array.data_type().to_owned();
8759        cast_from_null_to_other(&data_type);
8760
8761        // Cast null from and to struct
8762        let data_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
8763        cast_from_null_to_other(&data_type);
8764
8765        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
8766        cast_from_null_to_other(&target_type);
8767
8768        let target_type =
8769            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
8770        cast_from_null_to_other(&target_type);
8771
8772        let fields = UnionFields::from_fields(vec![Field::new("a", DataType::Int64, false)]);
8773        let target_type = DataType::Union(fields, UnionMode::Sparse);
8774        cast_from_null_to_other_complex(&target_type);
8775
8776        let target_type = DataType::RunEndEncoded(
8777            Arc::new(Field::new("item", DataType::Int32, true)),
8778            Arc::new(Field::new("item", DataType::Int32, true)),
8779        );
8780        cast_from_null_to_other_complex(&target_type);
8781    }
8782
8783    /// Print the `DictionaryArray` `array` as a vector of strings
8784    fn array_to_strings(array: &ArrayRef) -> Vec<String> {
8785        let options = FormatOptions::new().with_null("null");
8786        let formatter = ArrayFormatter::try_new(array.as_ref(), &options).unwrap();
8787        (0..array.len())
8788            .map(|i| formatter.value(i).to_string())
8789            .collect()
8790    }
8791
8792    #[test]
8793    fn test_cast_utf8_to_date32() {
8794        use chrono::NaiveDate;
8795        let from_ymd = chrono::NaiveDate::from_ymd_opt;
8796        let since = chrono::NaiveDate::signed_duration_since;
8797
8798        let a = StringArray::from(vec![
8799            "2000-01-01",          // valid date with leading 0s
8800            "2000-01-01T12:00:00", // valid datetime, will throw away the time part
8801            "2000-2-2",            // valid date without leading 0s
8802            "2000-00-00",          // invalid month and day
8803            "2000",                // just a year is invalid
8804        ]);
8805        let array = Arc::new(a) as ArrayRef;
8806        let b = cast(&array, &DataType::Date32).unwrap();
8807        let c = b.as_primitive::<Date32Type>();
8808
8809        // test valid inputs
8810        let date_value = since(
8811            NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
8812            from_ymd(1970, 1, 1).unwrap(),
8813        )
8814        .num_days() as i32;
8815        assert!(c.is_valid(0)); // "2000-01-01"
8816        assert_eq!(date_value, c.value(0));
8817
8818        assert!(c.is_valid(1)); // "2000-01-01T12:00:00"
8819        assert_eq!(date_value, c.value(1));
8820
8821        let date_value = since(
8822            NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
8823            from_ymd(1970, 1, 1).unwrap(),
8824        )
8825        .num_days() as i32;
8826        assert!(c.is_valid(2)); // "2000-2-2"
8827        assert_eq!(date_value, c.value(2));
8828
8829        // test invalid inputs
8830        assert!(!c.is_valid(3)); // "2000-00-00"
8831        assert!(!c.is_valid(4)); // "2000"
8832    }
8833
8834    #[test]
8835    fn test_cast_utf8_to_date64() {
8836        let a = StringArray::from(vec![
8837            "2000-01-01T12:00:00", // date + time valid
8838            "2020-12-15T12:34:56", // date + time valid
8839            "2020-2-2T12:34:56",   // valid date time without leading 0s
8840            "2000-00-00T12:00:00", // invalid month and day
8841            "2000-01-01 12:00:00", // missing the 'T'
8842            "2000-01-01",          // just a date is invalid
8843        ]);
8844        let array = Arc::new(a) as ArrayRef;
8845        let b = cast(&array, &DataType::Date64).unwrap();
8846        let c = b.as_primitive::<Date64Type>();
8847
8848        // test valid inputs
8849        assert!(c.is_valid(0)); // "2000-01-01T12:00:00"
8850        assert_eq!(946728000000, c.value(0));
8851        assert!(c.is_valid(1)); // "2020-12-15T12:34:56"
8852        assert_eq!(1608035696000, c.value(1));
8853        assert!(!c.is_valid(2)); // "2020-2-2T12:34:56"
8854
8855        assert!(!c.is_valid(3)); // "2000-00-00T12:00:00"
8856        assert!(c.is_valid(4)); // "2000-01-01 12:00:00"
8857        assert_eq!(946728000000, c.value(4));
8858        assert!(c.is_valid(5)); // "2000-01-01"
8859        assert_eq!(946684800000, c.value(5));
8860    }
8861
8862    #[test]
8863    fn test_can_cast_fsl_to_fsl() {
8864        let from_array = Arc::new(
8865            FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
8866                [Some([Some(1.0), Some(2.0)]), None],
8867                2,
8868            ),
8869        ) as ArrayRef;
8870        let to_array = Arc::new(
8871            FixedSizeListArray::from_iter_primitive::<Float16Type, _, _>(
8872                [
8873                    Some([Some(f16::from_f32(1.0)), Some(f16::from_f32(2.0))]),
8874                    None,
8875                ],
8876                2,
8877            ),
8878        ) as ArrayRef;
8879
8880        assert!(can_cast_types(from_array.data_type(), to_array.data_type()));
8881        let actual = cast(&from_array, to_array.data_type()).unwrap();
8882        assert_eq!(actual.data_type(), to_array.data_type());
8883
8884        let invalid_target =
8885            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Binary, true)), 2);
8886        assert!(!can_cast_types(from_array.data_type(), &invalid_target));
8887
8888        let invalid_size =
8889            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Float16, true)), 5);
8890        assert!(!can_cast_types(from_array.data_type(), &invalid_size));
8891    }
8892
8893    #[test]
8894    fn test_can_cast_types_fixed_size_list_to_list() {
8895        // DataType::List
8896        let array1 = make_fixed_size_list_array();
8897        assert!(can_cast_types(
8898            array1.data_type(),
8899            &DataType::List(Arc::new(Field::new("", DataType::Int32, false)))
8900        ));
8901
8902        // DataType::LargeList
8903        let array2 = make_fixed_size_list_array_for_large_list();
8904        assert!(can_cast_types(
8905            array2.data_type(),
8906            &DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false)))
8907        ));
8908    }
8909
8910    #[test]
8911    fn test_cast_fixed_size_list_to_list() {
8912        // Important cases:
8913        // 1. With/without nulls
8914        // 2. List/LargeList/ListView/LargeListView
8915        // 3. With and without inner casts
8916
8917        let cases = [
8918            // fixed_size_list<i32, 2> => list<i32>
8919            (
8920                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8921                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8922                    2,
8923                )) as ArrayRef,
8924                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
8925                    Some([Some(1), Some(1)]),
8926                    Some([Some(2), Some(2)]),
8927                ])) as ArrayRef,
8928            ),
8929            // fixed_size_list<i32, 2> => list<i32> (nullable)
8930            (
8931                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8932                    [None, Some([Some(2), Some(2)])],
8933                    2,
8934                )) as ArrayRef,
8935                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
8936                    None,
8937                    Some([Some(2), Some(2)]),
8938                ])) as ArrayRef,
8939            ),
8940            // fixed_size_list<i32, 2> => large_list<i64>
8941            (
8942                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8943                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8944                    2,
8945                )) as ArrayRef,
8946                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
8947                    Some([Some(1), Some(1)]),
8948                    Some([Some(2), Some(2)]),
8949                ])) as ArrayRef,
8950            ),
8951            // fixed_size_list<i32, 2> => large_list<i64> (nullable)
8952            (
8953                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8954                    [None, Some([Some(2), Some(2)])],
8955                    2,
8956                )) as ArrayRef,
8957                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
8958                    None,
8959                    Some([Some(2), Some(2)]),
8960                ])) as ArrayRef,
8961            ),
8962            // fixed_size_list<i32, 2> => list_view<i32>
8963            (
8964                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8965                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8966                    2,
8967                )) as ArrayRef,
8968                Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>([
8969                    Some([Some(1), Some(1)]),
8970                    Some([Some(2), Some(2)]),
8971                ])) as ArrayRef,
8972            ),
8973            // fixed_size_list<i32, 2> => list_view<i32> (nullable)
8974            (
8975                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8976                    [None, Some([Some(2), Some(2)])],
8977                    2,
8978                )) as ArrayRef,
8979                Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>([
8980                    None,
8981                    Some([Some(2), Some(2)]),
8982                ])) as ArrayRef,
8983            ),
8984            // fixed_size_list<i32, 2> => large_list_view<i64>
8985            (
8986                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8987                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8988                    2,
8989                )) as ArrayRef,
8990                Arc::new(LargeListViewArray::from_iter_primitive::<Int64Type, _, _>(
8991                    [Some([Some(1), Some(1)]), Some([Some(2), Some(2)])],
8992                )) as ArrayRef,
8993            ),
8994            // fixed_size_list<i32, 2> => large_list_view<i64> (nullable)
8995            (
8996                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8997                    [None, Some([Some(2), Some(2)])],
8998                    2,
8999                )) as ArrayRef,
9000                Arc::new(LargeListViewArray::from_iter_primitive::<Int64Type, _, _>(
9001                    [None, Some([Some(2), Some(2)])],
9002                )) as ArrayRef,
9003            ),
9004        ];
9005
9006        for (array, expected) in cases {
9007            assert!(
9008                can_cast_types(array.data_type(), expected.data_type()),
9009                "can_cast_types claims we cannot cast {:?} to {:?}",
9010                array.data_type(),
9011                expected.data_type()
9012            );
9013
9014            let list_array = cast(&array, expected.data_type())
9015                .unwrap_or_else(|_| panic!("Failed to cast {array:?} to {expected:?}"));
9016            assert_eq!(
9017                list_array.as_ref(),
9018                &expected,
9019                "Incorrect result from casting {array:?} to {expected:?}",
9020            );
9021        }
9022    }
9023
9024    #[test]
9025    fn test_cast_utf8_to_list() {
9026        // DataType::List
9027        let array = Arc::new(StringArray::from(vec!["5"])) as ArrayRef;
9028        let field = Arc::new(Field::new("", DataType::Int32, false));
9029        let list_array = cast(&array, &DataType::List(field.clone())).unwrap();
9030        let actual = list_array.as_list_opt::<i32>().unwrap();
9031        let expect = ListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
9032        assert_eq!(&expect.value(0), &actual.value(0));
9033
9034        // DataType::LargeList
9035        let list_array = cast(&array, &DataType::LargeList(field.clone())).unwrap();
9036        let actual = list_array.as_list_opt::<i64>().unwrap();
9037        let expect = LargeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
9038        assert_eq!(&expect.value(0), &actual.value(0));
9039
9040        // DataType::FixedSizeList
9041        let list_array = cast(&array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9042        let actual = list_array.as_fixed_size_list_opt().unwrap();
9043        let expect =
9044            FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])], 1);
9045        assert_eq!(&expect.value(0), &actual.value(0));
9046    }
9047
9048    #[test]
9049    fn test_cast_single_element_fixed_size_list() {
9050        // FixedSizeList<T>[1] => T
9051        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9052            [(Some([Some(5)]))],
9053            1,
9054        )) as ArrayRef;
9055        let casted_array = cast(&from_array, &DataType::Int32).unwrap();
9056        let actual: &Int32Array = casted_array.as_primitive();
9057        let expected = Int32Array::from(vec![Some(5)]);
9058        assert_eq!(&expected, actual);
9059
9060        // FixedSizeList<T>[1] => FixedSizeList<U>[1]
9061        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9062            [(Some([Some(5)]))],
9063            1,
9064        )) as ArrayRef;
9065        let to_field = Arc::new(Field::new("dummy", DataType::Float32, false));
9066        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
9067        let expected = Arc::new(FixedSizeListArray::new(
9068            to_field.clone(),
9069            1,
9070            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9071            None,
9072        )) as ArrayRef;
9073        assert_eq!(*expected, *actual);
9074
9075        // FixedSizeList<T>[1] => FixedSizeList<FixdSizedList<U>[1]>[1]
9076        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
9077            [(Some([Some(5)]))],
9078            1,
9079        )) as ArrayRef;
9080        let to_field_inner = Arc::new(Field::new_list_field(DataType::Float32, false));
9081        let to_field = Arc::new(Field::new(
9082            "dummy",
9083            DataType::FixedSizeList(to_field_inner.clone(), 1),
9084            false,
9085        ));
9086        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
9087        let expected = Arc::new(FixedSizeListArray::new(
9088            to_field.clone(),
9089            1,
9090            Arc::new(FixedSizeListArray::new(
9091                to_field_inner.clone(),
9092                1,
9093                Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9094                None,
9095            )) as ArrayRef,
9096            None,
9097        )) as ArrayRef;
9098        assert_eq!(*expected, *actual);
9099
9100        // T => FixedSizeList<T>[1] (non-nullable)
9101        let field = Arc::new(Field::new("dummy", DataType::Float32, false));
9102        let from_array = Arc::new(Int8Array::from(vec![Some(5)])) as ArrayRef;
9103        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9104        let actual = casted_array.as_fixed_size_list();
9105        let expected = Arc::new(FixedSizeListArray::new(
9106            field.clone(),
9107            1,
9108            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
9109            None,
9110        )) as ArrayRef;
9111        assert_eq!(expected.as_ref(), actual);
9112
9113        // T => FixedSizeList<T>[1] (nullable)
9114        let field = Arc::new(Field::new("nullable", DataType::Float32, true));
9115        let from_array = Arc::new(Int8Array::from(vec![None])) as ArrayRef;
9116        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
9117        let actual = casted_array.as_fixed_size_list();
9118        let expected = Arc::new(FixedSizeListArray::new(
9119            field.clone(),
9120            1,
9121            Arc::new(Float32Array::from(vec![None])) as ArrayRef,
9122            None,
9123        )) as ArrayRef;
9124        assert_eq!(expected.as_ref(), actual);
9125    }
9126
9127    #[test]
9128    fn test_cast_list_containers() {
9129        // large-list to list
9130        let array = make_large_list_array();
9131        let list_array = cast(
9132            &array,
9133            &DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
9134        )
9135        .unwrap();
9136        let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
9137        let expected = array.as_any().downcast_ref::<LargeListArray>().unwrap();
9138
9139        assert_eq!(&expected.value(0), &actual.value(0));
9140        assert_eq!(&expected.value(1), &actual.value(1));
9141        assert_eq!(&expected.value(2), &actual.value(2));
9142
9143        // list to large-list
9144        let array = make_list_array();
9145        let large_list_array = cast(
9146            &array,
9147            &DataType::LargeList(Arc::new(Field::new("", DataType::Int32, false))),
9148        )
9149        .unwrap();
9150        let actual = large_list_array
9151            .as_any()
9152            .downcast_ref::<LargeListArray>()
9153            .unwrap();
9154        let expected = array.as_any().downcast_ref::<ListArray>().unwrap();
9155
9156        assert_eq!(&expected.value(0), &actual.value(0));
9157        assert_eq!(&expected.value(1), &actual.value(1));
9158        assert_eq!(&expected.value(2), &actual.value(2));
9159    }
9160
9161    #[test]
9162    fn test_cast_list_view() {
9163        // cast between list view and list view
9164        let array = make_list_view_array();
9165        let to = DataType::ListView(Field::new_list_field(DataType::Float32, true).into());
9166        assert!(can_cast_types(array.data_type(), &to));
9167        let actual = cast(&array, &to).unwrap();
9168        let actual = actual.as_list_view::<i32>();
9169
9170        assert_eq!(
9171            &Float32Array::from(vec![0.0, 1.0, 2.0]) as &dyn Array,
9172            actual.value(0).as_ref()
9173        );
9174        assert_eq!(
9175            &Float32Array::from(vec![3.0, 4.0, 5.0]) as &dyn Array,
9176            actual.value(1).as_ref()
9177        );
9178        assert_eq!(
9179            &Float32Array::from(vec![6.0, 7.0]) as &dyn Array,
9180            actual.value(2).as_ref()
9181        );
9182
9183        // cast between large list view and large list view
9184        let array = make_large_list_view_array();
9185        let to = DataType::LargeListView(Field::new_list_field(DataType::Float32, true).into());
9186        assert!(can_cast_types(array.data_type(), &to));
9187        let actual = cast(&array, &to).unwrap();
9188        let actual = actual.as_list_view::<i64>();
9189
9190        assert_eq!(
9191            &Float32Array::from(vec![0.0, 1.0, 2.0]) as &dyn Array,
9192            actual.value(0).as_ref()
9193        );
9194        assert_eq!(
9195            &Float32Array::from(vec![3.0, 4.0, 5.0]) as &dyn Array,
9196            actual.value(1).as_ref()
9197        );
9198        assert_eq!(
9199            &Float32Array::from(vec![6.0, 7.0]) as &dyn Array,
9200            actual.value(2).as_ref()
9201        );
9202    }
9203
9204    #[test]
9205    fn test_non_list_to_list_view() {
9206        let input = Arc::new(Int32Array::from(vec![Some(0), None, Some(2)])) as ArrayRef;
9207        let expected_primitive =
9208            Arc::new(Float32Array::from(vec![Some(0.0), None, Some(2.0)])) as ArrayRef;
9209
9210        // [[0], [NULL], [2]]
9211        let expected = ListViewArray::new(
9212            Field::new_list_field(DataType::Float32, true).into(),
9213            vec![0, 1, 2].into(),
9214            vec![1, 1, 1].into(),
9215            expected_primitive.clone(),
9216            None,
9217        );
9218        assert!(can_cast_types(input.data_type(), expected.data_type()));
9219        let actual = cast(&input, expected.data_type()).unwrap();
9220        assert_eq!(actual.as_ref(), &expected);
9221
9222        // [[0], [NULL], [2]]
9223        let expected = LargeListViewArray::new(
9224            Field::new_list_field(DataType::Float32, true).into(),
9225            vec![0, 1, 2].into(),
9226            vec![1, 1, 1].into(),
9227            expected_primitive.clone(),
9228            None,
9229        );
9230        assert!(can_cast_types(input.data_type(), expected.data_type()));
9231        let actual = cast(&input, expected.data_type()).unwrap();
9232        assert_eq!(actual.as_ref(), &expected);
9233    }
9234
9235    #[test]
9236    fn test_cast_list_to_fsl() {
9237        // There four noteworthy cases we should handle:
9238        // 1. No nulls
9239        // 2. Nulls that are always empty
9240        // 3. Nulls that have varying lengths
9241        // 4. Nulls that are correctly sized (same as target list size)
9242
9243        // Non-null case
9244        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9245        let values = vec![
9246            Some(vec![Some(1), Some(2), Some(3)]),
9247            Some(vec![Some(4), Some(5), Some(6)]),
9248        ];
9249        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
9250            values.clone(),
9251        )) as ArrayRef;
9252        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9253            values, 3,
9254        )) as ArrayRef;
9255        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9256        assert_eq!(expected.as_ref(), actual.as_ref());
9257
9258        // Null cases
9259        // Array is [[1, 2, 3], null, [4, 5, 6], null]
9260        let cases = [
9261            (
9262                // Zero-length nulls
9263                vec![1, 2, 3, 4, 5, 6],
9264                vec![3, 0, 3, 0],
9265            ),
9266            (
9267                // Varying-length nulls
9268                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
9269                vec![3, 2, 3, 1],
9270            ),
9271            (
9272                // Correctly-sized nulls
9273                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
9274                vec![3, 3, 3, 3],
9275            ),
9276            (
9277                // Mixed nulls
9278                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
9279                vec![3, 0, 3, 3],
9280            ),
9281        ];
9282        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
9283
9284        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9285            vec![
9286                Some(vec![Some(1), Some(2), Some(3)]),
9287                None,
9288                Some(vec![Some(4), Some(5), Some(6)]),
9289                None,
9290            ],
9291            3,
9292        )) as ArrayRef;
9293
9294        for (values, lengths) in cases.iter() {
9295            let array = Arc::new(ListArray::new(
9296                field.clone(),
9297                OffsetBuffer::from_lengths(lengths.clone()),
9298                Arc::new(Int32Array::from(values.clone())),
9299                Some(null_buffer.clone()),
9300            )) as ArrayRef;
9301            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9302            assert_eq!(expected.as_ref(), actual.as_ref());
9303        }
9304    }
9305
9306    #[test]
9307    fn test_cast_list_view_to_fsl() {
9308        // There four noteworthy cases we should handle:
9309        // 1. No nulls
9310        // 2. Nulls that are always empty
9311        // 3. Nulls that have varying lengths
9312        // 4. Nulls that are correctly sized (same as target list size)
9313
9314        // Non-null case
9315        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
9316        let values = vec![
9317            Some(vec![Some(1), Some(2), Some(3)]),
9318            Some(vec![Some(4), Some(5), Some(6)]),
9319        ];
9320        let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(
9321            values.clone(),
9322        )) as ArrayRef;
9323        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9324            values, 3,
9325        )) as ArrayRef;
9326        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9327        assert_eq!(expected.as_ref(), actual.as_ref());
9328
9329        // Null cases
9330        // Array is [[1, 2, 3], null, [4, 5, 6], null]
9331        let cases = [
9332            (
9333                // Zero-length nulls
9334                vec![1, 2, 3, 4, 5, 6],
9335                vec![0, 0, 3, 0],
9336                vec![3, 0, 3, 0],
9337            ),
9338            (
9339                // Varying-length nulls
9340                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
9341                vec![0, 1, 5, 0],
9342                vec![3, 2, 3, 1],
9343            ),
9344            (
9345                // Correctly-sized nulls
9346                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
9347                vec![0, 3, 6, 9],
9348                vec![3, 3, 3, 3],
9349            ),
9350            (
9351                // Mixed nulls
9352                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
9353                vec![0, 0, 3, 6],
9354                vec![3, 0, 3, 3],
9355            ),
9356        ];
9357        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
9358
9359        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9360            vec![
9361                Some(vec![Some(1), Some(2), Some(3)]),
9362                None,
9363                Some(vec![Some(4), Some(5), Some(6)]),
9364                None,
9365            ],
9366            3,
9367        )) as ArrayRef;
9368
9369        for (values, offsets, lengths) in cases.iter() {
9370            let array = Arc::new(ListViewArray::new(
9371                field.clone(),
9372                offsets.clone().into(),
9373                lengths.clone().into(),
9374                Arc::new(Int32Array::from(values.clone())),
9375                Some(null_buffer.clone()),
9376            )) as ArrayRef;
9377            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
9378            assert_eq!(expected.as_ref(), actual.as_ref());
9379        }
9380    }
9381
9382    #[test]
9383    fn test_cast_list_to_fsl_safety() {
9384        let values = vec![
9385            Some(vec![Some(1), Some(2), Some(3)]),
9386            Some(vec![Some(4), Some(5)]),
9387            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
9388            Some(vec![Some(3), Some(4), Some(5)]),
9389        ];
9390        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
9391            values.clone(),
9392        )) as ArrayRef;
9393
9394        let res = cast_with_options(
9395            array.as_ref(),
9396            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9397            &CastOptions {
9398                safe: false,
9399                ..Default::default()
9400            },
9401        );
9402        assert!(res.is_err());
9403        assert!(
9404            format!("{res:?}")
9405                .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
9406        );
9407
9408        // When safe=true (default), the cast will fill nulls for lists that are
9409        // too short and truncate lists that are too long.
9410        let res = cast(
9411            array.as_ref(),
9412            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9413        )
9414        .unwrap();
9415        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9416            vec![
9417                Some(vec![Some(1), Some(2), Some(3)]),
9418                None, // Too short -> replaced with null
9419                None, // Too long -> replaced with null
9420                Some(vec![Some(3), Some(4), Some(5)]),
9421            ],
9422            3,
9423        )) as ArrayRef;
9424        assert_eq!(expected.as_ref(), res.as_ref());
9425
9426        // The safe option is false and the source array contains a null list.
9427        // issue: https://github.com/apache/arrow-rs/issues/5642
9428        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
9429            Some(vec![Some(1), Some(2), Some(3)]),
9430            None,
9431        ])) as ArrayRef;
9432        let res = cast_with_options(
9433            array.as_ref(),
9434            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9435            &CastOptions {
9436                safe: false,
9437                ..Default::default()
9438            },
9439        )
9440        .unwrap();
9441        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9442            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
9443            3,
9444        )) as ArrayRef;
9445        assert_eq!(expected.as_ref(), res.as_ref());
9446    }
9447
9448    #[test]
9449    fn test_cast_list_view_to_fsl_safety() {
9450        let values = vec![
9451            Some(vec![Some(1), Some(2), Some(3)]),
9452            Some(vec![Some(4), Some(5)]),
9453            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
9454            Some(vec![Some(3), Some(4), Some(5)]),
9455        ];
9456        let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(
9457            values.clone(),
9458        )) as ArrayRef;
9459
9460        let res = cast_with_options(
9461            array.as_ref(),
9462            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9463            &CastOptions {
9464                safe: false,
9465                ..Default::default()
9466            },
9467        );
9468        assert!(res.is_err());
9469        assert!(
9470            format!("{res:?}")
9471                .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
9472        );
9473
9474        // When safe=true (default), the cast will fill nulls for lists that are
9475        // too short and truncate lists that are too long.
9476        let res = cast(
9477            array.as_ref(),
9478            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9479        )
9480        .unwrap();
9481        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9482            vec![
9483                Some(vec![Some(1), Some(2), Some(3)]),
9484                None, // Too short -> replaced with null
9485                None, // Too long -> replaced with null
9486                Some(vec![Some(3), Some(4), Some(5)]),
9487            ],
9488            3,
9489        )) as ArrayRef;
9490        assert_eq!(expected.as_ref(), res.as_ref());
9491
9492        // The safe option is false and the source array contains a null list.
9493        // issue: https://github.com/apache/arrow-rs/issues/5642
9494        let array = Arc::new(ListViewArray::from_iter_primitive::<Int32Type, _, _>(vec![
9495            Some(vec![Some(1), Some(2), Some(3)]),
9496            None,
9497        ])) as ArrayRef;
9498        let res = cast_with_options(
9499            array.as_ref(),
9500            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
9501            &CastOptions {
9502                safe: false,
9503                ..Default::default()
9504            },
9505        )
9506        .unwrap();
9507        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9508            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
9509            3,
9510        )) as ArrayRef;
9511        assert_eq!(expected.as_ref(), res.as_ref());
9512    }
9513
9514    #[test]
9515    fn test_cast_large_list_to_fsl() {
9516        let values = vec![Some(vec![Some(1), Some(2)]), Some(vec![Some(3), Some(4)])];
9517        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
9518            values.clone(),
9519            2,
9520        )) as ArrayRef;
9521        let target_type =
9522            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 2);
9523
9524        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
9525            values.clone(),
9526        )) as ArrayRef;
9527        let actual = cast(array.as_ref(), &target_type).unwrap();
9528        assert_eq!(expected.as_ref(), actual.as_ref());
9529
9530        let array = Arc::new(LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(
9531            values.clone(),
9532        )) as ArrayRef;
9533        let actual = cast(array.as_ref(), &target_type).unwrap();
9534        assert_eq!(expected.as_ref(), actual.as_ref());
9535    }
9536
9537    #[test]
9538    fn test_cast_list_to_fsl_subcast() {
9539        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
9540            vec![
9541                Some(vec![Some(1), Some(2)]),
9542                Some(vec![Some(3), Some(i32::MAX)]),
9543            ],
9544        )) as ArrayRef;
9545        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
9546            vec![
9547                Some(vec![Some(1), Some(2)]),
9548                Some(vec![Some(3), Some(i32::MAX as i64)]),
9549            ],
9550            2,
9551        )) as ArrayRef;
9552        let actual = cast(
9553            array.as_ref(),
9554            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 2),
9555        )
9556        .unwrap();
9557        assert_eq!(expected.as_ref(), actual.as_ref());
9558
9559        let res = cast_with_options(
9560            array.as_ref(),
9561            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int16, true)), 2),
9562            &CastOptions {
9563                safe: false,
9564                ..Default::default()
9565            },
9566        );
9567        assert!(res.is_err());
9568        assert!(format!("{res:?}").contains("Can't cast value 2147483647 to type Int16"));
9569    }
9570
9571    #[test]
9572    fn test_cast_list_to_fsl_empty() {
9573        let inner_field = Arc::new(Field::new_list_field(DataType::Int32, true));
9574        let target_type = DataType::FixedSizeList(inner_field.clone(), 3);
9575        let expected = new_empty_array(&target_type);
9576
9577        // list
9578        let array = new_empty_array(&DataType::List(inner_field.clone()));
9579        assert!(can_cast_types(array.data_type(), &target_type));
9580        let actual = cast(array.as_ref(), &target_type).unwrap();
9581        assert_eq!(expected.as_ref(), actual.as_ref());
9582
9583        // largelist
9584        let array = new_empty_array(&DataType::LargeList(inner_field.clone()));
9585        assert!(can_cast_types(array.data_type(), &target_type));
9586        let actual = cast(array.as_ref(), &target_type).unwrap();
9587        assert_eq!(expected.as_ref(), actual.as_ref());
9588
9589        // listview
9590        let array = new_empty_array(&DataType::ListView(inner_field.clone()));
9591        assert!(can_cast_types(array.data_type(), &target_type));
9592        let actual = cast(array.as_ref(), &target_type).unwrap();
9593        assert_eq!(expected.as_ref(), actual.as_ref());
9594
9595        // largelistview
9596        let array = new_empty_array(&DataType::LargeListView(inner_field.clone()));
9597        assert!(can_cast_types(array.data_type(), &target_type));
9598        let actual = cast(array.as_ref(), &target_type).unwrap();
9599        assert_eq!(expected.as_ref(), actual.as_ref());
9600    }
9601
9602    fn make_list_array() -> ArrayRef {
9603        // [[0, 1, 2], [3, 4, 5], [6, 7]]
9604        Arc::new(ListArray::new(
9605            Field::new_list_field(DataType::Int32, true).into(),
9606            OffsetBuffer::from_lengths(vec![3, 3, 2]),
9607            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9608            None,
9609        ))
9610    }
9611
9612    fn make_large_list_array() -> ArrayRef {
9613        // [[0, 1, 2], [3, 4, 5], [6, 7]]
9614        Arc::new(LargeListArray::new(
9615            Field::new_list_field(DataType::Int32, true).into(),
9616            OffsetBuffer::from_lengths(vec![3, 3, 2]),
9617            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9618            None,
9619        ))
9620    }
9621
9622    fn make_list_view_array() -> ArrayRef {
9623        // [[0, 1, 2], [3, 4, 5], [6, 7]]
9624        Arc::new(ListViewArray::new(
9625            Field::new_list_field(DataType::Int32, true).into(),
9626            vec![0, 3, 6].into(),
9627            vec![3, 3, 2].into(),
9628            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9629            None,
9630        ))
9631    }
9632
9633    fn make_large_list_view_array() -> ArrayRef {
9634        // [[0, 1, 2], [3, 4, 5], [6, 7]]
9635        Arc::new(LargeListViewArray::new(
9636            Field::new_list_field(DataType::Int32, true).into(),
9637            vec![0, 3, 6].into(),
9638            vec![3, 3, 2].into(),
9639            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9640            None,
9641        ))
9642    }
9643
9644    fn make_fixed_size_list_array() -> ArrayRef {
9645        // [[0, 1, 2, 3], [4, 5, 6, 7]]
9646        Arc::new(FixedSizeListArray::new(
9647            Field::new_list_field(DataType::Int32, true).into(),
9648            4,
9649            Arc::new(Int32Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9650            None,
9651        ))
9652    }
9653
9654    fn make_fixed_size_list_array_for_large_list() -> ArrayRef {
9655        // [[0, 1, 2, 3], [4, 5, 6, 7]]
9656        Arc::new(FixedSizeListArray::new(
9657            Field::new_list_field(DataType::Int64, true).into(),
9658            4,
9659            Arc::new(Int64Array::from(vec![0, 1, 2, 3, 4, 5, 6, 7])),
9660            None,
9661        ))
9662    }
9663
9664    #[test]
9665    fn test_cast_map_dont_allow_change_of_order() {
9666        let string_builder = StringBuilder::new();
9667        let value_builder = StringBuilder::new();
9668        let mut builder = MapBuilder::new(
9669            Some(MapFieldNames {
9670                entry: "entries".to_string(),
9671                key: "key".to_string(),
9672                value: "value".to_string(),
9673            }),
9674            string_builder,
9675            value_builder,
9676        );
9677
9678        builder.keys().append_value("0");
9679        builder.values().append_value("test_val_1");
9680        builder.append(true).unwrap();
9681        builder.keys().append_value("1");
9682        builder.values().append_value("test_val_2");
9683        builder.append(true).unwrap();
9684
9685        // map builder returns unsorted map by default
9686        let array = builder.finish();
9687
9688        let new_ordered = true;
9689        let new_type = DataType::Map(
9690            Arc::new(Field::new(
9691                "entries",
9692                DataType::Struct(
9693                    vec![
9694                        Field::new("key", DataType::Utf8, false),
9695                        Field::new("value", DataType::Utf8, false),
9696                    ]
9697                    .into(),
9698                ),
9699                false,
9700            )),
9701            new_ordered,
9702        );
9703
9704        let new_array_result = cast(&array, &new_type.clone());
9705        assert!(!can_cast_types(array.data_type(), &new_type));
9706        let Err(ArrowError::CastError(t)) = new_array_result else {
9707            panic!();
9708        };
9709        assert_eq!(
9710            t,
9711            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"#
9712        );
9713    }
9714
9715    #[test]
9716    fn test_cast_map_dont_allow_when_container_cant_cast() {
9717        let string_builder = StringBuilder::new();
9718        let value_builder = IntervalDayTimeArray::builder(2);
9719        let mut builder = MapBuilder::new(
9720            Some(MapFieldNames {
9721                entry: "entries".to_string(),
9722                key: "key".to_string(),
9723                value: "value".to_string(),
9724            }),
9725            string_builder,
9726            value_builder,
9727        );
9728
9729        builder.keys().append_value("0");
9730        builder.values().append_value(IntervalDayTime::new(1, 1));
9731        builder.append(true).unwrap();
9732        builder.keys().append_value("1");
9733        builder.values().append_value(IntervalDayTime::new(2, 2));
9734        builder.append(true).unwrap();
9735
9736        // map builder returns unsorted map by default
9737        let array = builder.finish();
9738
9739        let new_ordered = true;
9740        let new_type = DataType::Map(
9741            Arc::new(Field::new(
9742                "entries",
9743                DataType::Struct(
9744                    vec![
9745                        Field::new("key", DataType::Utf8, false),
9746                        Field::new("value", DataType::Duration(TimeUnit::Second), false),
9747                    ]
9748                    .into(),
9749                ),
9750                false,
9751            )),
9752            new_ordered,
9753        );
9754
9755        let new_array_result = cast(&array, &new_type.clone());
9756        assert!(!can_cast_types(array.data_type(), &new_type));
9757        let Err(ArrowError::CastError(t)) = new_array_result else {
9758            panic!();
9759        };
9760        assert_eq!(
9761            t,
9762            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"#
9763        );
9764    }
9765
9766    #[test]
9767    fn test_cast_map_field_names() {
9768        let string_builder = StringBuilder::new();
9769        let value_builder = StringBuilder::new();
9770        let mut builder = MapBuilder::new(
9771            Some(MapFieldNames {
9772                entry: "entries".to_string(),
9773                key: "key".to_string(),
9774                value: "value".to_string(),
9775            }),
9776            string_builder,
9777            value_builder,
9778        );
9779
9780        builder.keys().append_value("0");
9781        builder.values().append_value("test_val_1");
9782        builder.append(true).unwrap();
9783        builder.keys().append_value("1");
9784        builder.values().append_value("test_val_2");
9785        builder.append(true).unwrap();
9786        builder.append(false).unwrap();
9787
9788        let array = builder.finish();
9789
9790        let new_type = DataType::Map(
9791            Arc::new(Field::new(
9792                "entries_new",
9793                DataType::Struct(
9794                    vec![
9795                        Field::new("key_new", DataType::Utf8, false),
9796                        Field::new("value_values", DataType::Utf8, false),
9797                    ]
9798                    .into(),
9799                ),
9800                false,
9801            )),
9802            false,
9803        );
9804
9805        assert_ne!(new_type, array.data_type().clone());
9806
9807        let new_array = cast(&array, &new_type.clone()).unwrap();
9808        assert_eq!(new_type, new_array.data_type().clone());
9809        let map_array = new_array.as_map();
9810
9811        assert_ne!(new_type, array.data_type().clone());
9812        assert_eq!(new_type, map_array.data_type().clone());
9813
9814        let key_string = map_array
9815            .keys()
9816            .as_any()
9817            .downcast_ref::<StringArray>()
9818            .unwrap()
9819            .into_iter()
9820            .flatten()
9821            .collect::<Vec<_>>();
9822        assert_eq!(&key_string, &vec!["0", "1"]);
9823
9824        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
9825        let values_string = values_string_array
9826            .as_any()
9827            .downcast_ref::<StringArray>()
9828            .unwrap()
9829            .into_iter()
9830            .flatten()
9831            .collect::<Vec<_>>();
9832        assert_eq!(&values_string, &vec!["test_val_1", "test_val_2"]);
9833
9834        assert_eq!(
9835            map_array.nulls(),
9836            Some(&NullBuffer::from(vec![true, true, false]))
9837        );
9838    }
9839
9840    #[test]
9841    fn test_cast_map_contained_values() {
9842        let string_builder = StringBuilder::new();
9843        let value_builder = Int8Builder::new();
9844        let mut builder = MapBuilder::new(
9845            Some(MapFieldNames {
9846                entry: "entries".to_string(),
9847                key: "key".to_string(),
9848                value: "value".to_string(),
9849            }),
9850            string_builder,
9851            value_builder,
9852        );
9853
9854        builder.keys().append_value("0");
9855        builder.values().append_value(44);
9856        builder.append(true).unwrap();
9857        builder.keys().append_value("1");
9858        builder.values().append_value(22);
9859        builder.append(true).unwrap();
9860
9861        let array = builder.finish();
9862
9863        let new_type = DataType::Map(
9864            Arc::new(Field::new(
9865                "entries",
9866                DataType::Struct(
9867                    vec![
9868                        Field::new("key", DataType::Utf8, false),
9869                        Field::new("value", DataType::Utf8, false),
9870                    ]
9871                    .into(),
9872                ),
9873                false,
9874            )),
9875            false,
9876        );
9877
9878        let new_array = cast(&array, &new_type.clone()).unwrap();
9879        assert_eq!(new_type, new_array.data_type().clone());
9880        let map_array = new_array.as_map();
9881
9882        assert_ne!(new_type, array.data_type().clone());
9883        assert_eq!(new_type, map_array.data_type().clone());
9884
9885        let key_string = map_array
9886            .keys()
9887            .as_any()
9888            .downcast_ref::<StringArray>()
9889            .unwrap()
9890            .into_iter()
9891            .flatten()
9892            .collect::<Vec<_>>();
9893        assert_eq!(&key_string, &vec!["0", "1"]);
9894
9895        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
9896        let values_string = values_string_array
9897            .as_any()
9898            .downcast_ref::<StringArray>()
9899            .unwrap()
9900            .into_iter()
9901            .flatten()
9902            .collect::<Vec<_>>();
9903        assert_eq!(&values_string, &vec!["44", "22"]);
9904    }
9905
9906    #[test]
9907    fn test_utf8_cast_offsets() {
9908        // test if offset of the array is taken into account during cast
9909        let str_array = StringArray::from(vec!["a", "b", "c"]);
9910        let str_array = str_array.slice(1, 2);
9911
9912        let out = cast(&str_array, &DataType::LargeUtf8).unwrap();
9913
9914        let large_str_array = out.as_any().downcast_ref::<LargeStringArray>().unwrap();
9915        let strs = large_str_array.into_iter().flatten().collect::<Vec<_>>();
9916        assert_eq!(strs, &["b", "c"])
9917    }
9918
9919    #[test]
9920    fn test_list_cast_offsets() {
9921        // test if offset of the array is taken into account during cast
9922        let array1 = make_list_array().slice(1, 2);
9923        let array2 = make_list_array();
9924
9925        let dt = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
9926        let out1 = cast(&array1, &dt).unwrap();
9927        let out2 = cast(&array2, &dt).unwrap();
9928
9929        assert_eq!(&out1, &out2.slice(1, 2))
9930    }
9931
9932    #[test]
9933    fn test_list_to_string() {
9934        fn assert_cast(array: &ArrayRef, expected: &[&str]) {
9935            assert!(can_cast_types(array.data_type(), &DataType::Utf8));
9936            let out = cast(array, &DataType::Utf8).unwrap();
9937            let out = out
9938                .as_string::<i32>()
9939                .into_iter()
9940                .flatten()
9941                .collect::<Vec<_>>();
9942            assert_eq!(out, expected);
9943
9944            assert!(can_cast_types(array.data_type(), &DataType::LargeUtf8));
9945            let out = cast(array, &DataType::LargeUtf8).unwrap();
9946            let out = out
9947                .as_string::<i64>()
9948                .into_iter()
9949                .flatten()
9950                .collect::<Vec<_>>();
9951            assert_eq!(out, expected);
9952
9953            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
9954            let out = cast(array, &DataType::Utf8View).unwrap();
9955            let out = out
9956                .as_string_view()
9957                .into_iter()
9958                .flatten()
9959                .collect::<Vec<_>>();
9960            assert_eq!(out, expected);
9961        }
9962
9963        let array = Arc::new(ListArray::new(
9964            Field::new_list_field(DataType::Utf8, true).into(),
9965            OffsetBuffer::from_lengths(vec![3, 3, 2]),
9966            Arc::new(StringArray::from(vec![
9967                "a", "b", "c", "d", "e", "f", "g", "h",
9968            ])),
9969            None,
9970        )) as ArrayRef;
9971
9972        assert_cast(&array, &["[a, b, c]", "[d, e, f]", "[g, h]"]);
9973
9974        let array = make_list_array();
9975        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
9976
9977        let array = make_large_list_array();
9978        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
9979
9980        let array = make_list_view_array();
9981        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
9982
9983        let array = make_large_list_view_array();
9984        assert_cast(&array, &["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
9985    }
9986
9987    #[test]
9988    fn test_cast_f64_to_decimal128() {
9989        // to reproduce https://github.com/apache/arrow-rs/issues/2997
9990
9991        let decimal_type = DataType::Decimal128(18, 2);
9992        let array = Float64Array::from(vec![
9993            Some(0.0699999999),
9994            Some(0.0659999999),
9995            Some(0.0650000000),
9996            Some(0.0649999999),
9997        ]);
9998        let array = Arc::new(array) as ArrayRef;
9999        generate_cast_test_case!(
10000            &array,
10001            Decimal128Array,
10002            &decimal_type,
10003            vec![
10004                Some(7_i128), // round up
10005                Some(7_i128), // round up
10006                Some(7_i128), // round up
10007                Some(6_i128), // round down
10008            ]
10009        );
10010
10011        let decimal_type = DataType::Decimal128(18, 3);
10012        let array = Float64Array::from(vec![
10013            Some(0.0699999999),
10014            Some(0.0659999999),
10015            Some(0.0650000000),
10016            Some(0.0649999999),
10017        ]);
10018        let array = Arc::new(array) as ArrayRef;
10019        generate_cast_test_case!(
10020            &array,
10021            Decimal128Array,
10022            &decimal_type,
10023            vec![
10024                Some(70_i128), // round up
10025                Some(66_i128), // round up
10026                Some(65_i128), // round down
10027                Some(65_i128), // round up
10028            ]
10029        );
10030    }
10031
10032    #[test]
10033    fn test_cast_numeric_to_decimal128_overflow() {
10034        let array = Int64Array::from(vec![i64::MAX]);
10035        let array = Arc::new(array) as ArrayRef;
10036        let casted_array = cast_with_options(
10037            &array,
10038            &DataType::Decimal128(38, 30),
10039            &CastOptions {
10040                safe: true,
10041                format_options: FormatOptions::default(),
10042            },
10043        );
10044        assert!(casted_array.is_ok());
10045        assert!(casted_array.unwrap().is_null(0));
10046
10047        let casted_array = cast_with_options(
10048            &array,
10049            &DataType::Decimal128(38, 30),
10050            &CastOptions {
10051                safe: false,
10052                format_options: FormatOptions::default(),
10053            },
10054        );
10055        assert!(casted_array.is_err());
10056    }
10057
10058    #[test]
10059    fn test_cast_numeric_to_decimal256_overflow() {
10060        let array = Int64Array::from(vec![i64::MAX]);
10061        let array = Arc::new(array) as ArrayRef;
10062        let casted_array = cast_with_options(
10063            &array,
10064            &DataType::Decimal256(76, 76),
10065            &CastOptions {
10066                safe: true,
10067                format_options: FormatOptions::default(),
10068            },
10069        );
10070        assert!(casted_array.is_ok());
10071        assert!(casted_array.unwrap().is_null(0));
10072
10073        let casted_array = cast_with_options(
10074            &array,
10075            &DataType::Decimal256(76, 76),
10076            &CastOptions {
10077                safe: false,
10078                format_options: FormatOptions::default(),
10079            },
10080        );
10081        assert!(casted_array.is_err());
10082    }
10083
10084    #[test]
10085    fn test_cast_floating_point_to_decimal128_precision_overflow() {
10086        let array = Float64Array::from(vec![1.1]);
10087        let array = Arc::new(array) as ArrayRef;
10088        let casted_array = cast_with_options(
10089            &array,
10090            &DataType::Decimal128(2, 2),
10091            &CastOptions {
10092                safe: true,
10093                format_options: FormatOptions::default(),
10094            },
10095        );
10096        assert!(casted_array.is_ok());
10097        assert!(casted_array.unwrap().is_null(0));
10098
10099        let casted_array = cast_with_options(
10100            &array,
10101            &DataType::Decimal128(2, 2),
10102            &CastOptions {
10103                safe: false,
10104                format_options: FormatOptions::default(),
10105            },
10106        );
10107        let err = casted_array.unwrap_err().to_string();
10108        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
10109        assert!(
10110            err.contains(expected_error),
10111            "did not find expected error '{expected_error}' in actual error '{err}'"
10112        );
10113    }
10114
10115    #[test]
10116    fn test_cast_floating_point_to_decimal256_precision_overflow() {
10117        let array = Float64Array::from(vec![1.1]);
10118        let array = Arc::new(array) as ArrayRef;
10119        let casted_array = cast_with_options(
10120            &array,
10121            &DataType::Decimal256(2, 2),
10122            &CastOptions {
10123                safe: true,
10124                format_options: FormatOptions::default(),
10125            },
10126        );
10127        assert!(casted_array.is_ok());
10128        assert!(casted_array.unwrap().is_null(0));
10129
10130        let casted_array = cast_with_options(
10131            &array,
10132            &DataType::Decimal256(2, 2),
10133            &CastOptions {
10134                safe: false,
10135                format_options: FormatOptions::default(),
10136            },
10137        );
10138        let err = casted_array.unwrap_err().to_string();
10139        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
10140        assert_eq!(err, expected_error);
10141    }
10142
10143    #[test]
10144    fn test_cast_floating_point_to_decimal128_overflow() {
10145        let array = Float64Array::from(vec![f64::MAX]);
10146        let array = Arc::new(array) as ArrayRef;
10147        let casted_array = cast_with_options(
10148            &array,
10149            &DataType::Decimal128(38, 30),
10150            &CastOptions {
10151                safe: true,
10152                format_options: FormatOptions::default(),
10153            },
10154        );
10155        assert!(casted_array.is_ok());
10156        assert!(casted_array.unwrap().is_null(0));
10157
10158        let casted_array = cast_with_options(
10159            &array,
10160            &DataType::Decimal128(38, 30),
10161            &CastOptions {
10162                safe: false,
10163                format_options: FormatOptions::default(),
10164            },
10165        );
10166        let err = casted_array.unwrap_err().to_string();
10167        let expected_error = "Cast error: Cannot cast to Decimal128(38, 30)";
10168        assert!(
10169            err.contains(expected_error),
10170            "did not find expected error '{expected_error}' in actual error '{err}'"
10171        );
10172    }
10173
10174    #[test]
10175    fn test_cast_floating_point_to_decimal256_overflow() {
10176        let array = Float64Array::from(vec![f64::MAX]);
10177        let array = Arc::new(array) as ArrayRef;
10178        let casted_array = cast_with_options(
10179            &array,
10180            &DataType::Decimal256(76, 50),
10181            &CastOptions {
10182                safe: true,
10183                format_options: FormatOptions::default(),
10184            },
10185        );
10186        assert!(casted_array.is_ok());
10187        assert!(casted_array.unwrap().is_null(0));
10188
10189        let casted_array = cast_with_options(
10190            &array,
10191            &DataType::Decimal256(76, 50),
10192            &CastOptions {
10193                safe: false,
10194                format_options: FormatOptions::default(),
10195            },
10196        );
10197        let err = casted_array.unwrap_err().to_string();
10198        let expected_error = "Cast error: Cannot cast to Decimal256(76, 50)";
10199        assert!(
10200            err.contains(expected_error),
10201            "did not find expected error '{expected_error}' in actual error '{err}'"
10202        );
10203    }
10204    #[test]
10205    fn test_cast_decimal256_to_f64_no_overflow() {
10206        // Test casting i256::MAX: should produce a large finite positive value
10207        let array = vec![Some(i256::MAX)];
10208        let array = create_decimal256_array(array, 76, 2).unwrap();
10209        let array = Arc::new(array) as ArrayRef;
10210
10211        let result = cast(&array, &DataType::Float64).unwrap();
10212        let result = result.as_primitive::<Float64Type>();
10213        assert!(result.value(0).is_finite());
10214        assert!(result.value(0) > 0.0); // Positive result
10215
10216        // Test casting i256::MIN: should produce a large finite negative value
10217        let array = vec![Some(i256::MIN)];
10218        let array = create_decimal256_array(array, 76, 2).unwrap();
10219        let array = Arc::new(array) as ArrayRef;
10220
10221        let result = cast(&array, &DataType::Float64).unwrap();
10222        let result = result.as_primitive::<Float64Type>();
10223        assert!(result.value(0).is_finite());
10224        assert!(result.value(0) < 0.0); // Negative result
10225    }
10226
10227    #[test]
10228    fn test_cast_decimal128_to_decimal128_negative_scale() {
10229        let input_type = DataType::Decimal128(20, 0);
10230        let output_type = DataType::Decimal128(20, -1);
10231        assert!(can_cast_types(&input_type, &output_type));
10232        let array = vec![Some(1123450), Some(2123455), Some(3123456), None];
10233        let input_decimal_array = create_decimal128_array(array, 20, 0).unwrap();
10234        let array = Arc::new(input_decimal_array) as ArrayRef;
10235        generate_cast_test_case!(
10236            &array,
10237            Decimal128Array,
10238            &output_type,
10239            vec![
10240                Some(112345_i128),
10241                Some(212346_i128),
10242                Some(312346_i128),
10243                None
10244            ]
10245        );
10246
10247        let casted_array = cast(&array, &output_type).unwrap();
10248        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10249
10250        assert_eq!("1123450", decimal_arr.value_as_string(0));
10251        assert_eq!("2123460", decimal_arr.value_as_string(1));
10252        assert_eq!("3123460", decimal_arr.value_as_string(2));
10253    }
10254
10255    #[test]
10256    fn decimal128_min_max_to_f64() {
10257        // Ensure Decimal128 i128::MIN/MAX round-trip cast
10258        let min128 = i128::MIN;
10259        let max128 = i128::MAX;
10260        assert_eq!(min128 as f64, min128 as f64);
10261        assert_eq!(max128 as f64, max128 as f64);
10262    }
10263
10264    #[test]
10265    fn test_cast_numeric_to_decimal128_negative() {
10266        let decimal_type = DataType::Decimal128(38, -1);
10267        let array = Arc::new(Int32Array::from(vec![
10268            Some(1123456),
10269            Some(2123456),
10270            Some(3123456),
10271        ])) as ArrayRef;
10272
10273        let casted_array = cast(&array, &decimal_type).unwrap();
10274        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10275
10276        assert_eq!("1123450", decimal_arr.value_as_string(0));
10277        assert_eq!("2123450", decimal_arr.value_as_string(1));
10278        assert_eq!("3123450", decimal_arr.value_as_string(2));
10279
10280        let array = Arc::new(Float32Array::from(vec![
10281            Some(1123.456),
10282            Some(2123.456),
10283            Some(3123.456),
10284        ])) as ArrayRef;
10285
10286        let casted_array = cast(&array, &decimal_type).unwrap();
10287        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10288
10289        assert_eq!("1120", decimal_arr.value_as_string(0));
10290        assert_eq!("2120", decimal_arr.value_as_string(1));
10291        assert_eq!("3120", decimal_arr.value_as_string(2));
10292    }
10293
10294    #[test]
10295    fn test_cast_decimal128_to_decimal128_negative() {
10296        let input_type = DataType::Decimal128(10, -1);
10297        let output_type = DataType::Decimal128(10, -2);
10298        assert!(can_cast_types(&input_type, &output_type));
10299        let array = vec![Some(123)];
10300        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
10301        let array = Arc::new(input_decimal_array) as ArrayRef;
10302        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(12_i128),]);
10303
10304        let casted_array = cast(&array, &output_type).unwrap();
10305        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10306
10307        assert_eq!("1200", decimal_arr.value_as_string(0));
10308
10309        let array = vec![Some(125)];
10310        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
10311        let array = Arc::new(input_decimal_array) as ArrayRef;
10312        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(13_i128),]);
10313
10314        let casted_array = cast(&array, &output_type).unwrap();
10315        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10316
10317        assert_eq!("1300", decimal_arr.value_as_string(0));
10318    }
10319
10320    #[test]
10321    fn test_cast_decimal128_to_decimal256_negative() {
10322        let input_type = DataType::Decimal128(10, 3);
10323        let output_type = DataType::Decimal256(10, 5);
10324        assert!(can_cast_types(&input_type, &output_type));
10325        let array = vec![Some(123456), Some(-123456)];
10326        let input_decimal_array = create_decimal128_array(array, 10, 3).unwrap();
10327        let array = Arc::new(input_decimal_array) as ArrayRef;
10328
10329        let hundred = i256::from_i128(100);
10330        generate_cast_test_case!(
10331            &array,
10332            Decimal256Array,
10333            &output_type,
10334            vec![
10335                Some(i256::from_i128(123456).mul_wrapping(hundred)),
10336                Some(i256::from_i128(-123456).mul_wrapping(hundred))
10337            ]
10338        );
10339    }
10340
10341    #[test]
10342    fn test_parse_string_to_decimal() {
10343        assert_eq!(
10344            Decimal128Type::format_decimal(
10345                parse_string_to_decimal_native::<Decimal128Type>("123.45", 2).unwrap(),
10346                38,
10347                2,
10348            ),
10349            "123.45"
10350        );
10351        assert_eq!(
10352            Decimal128Type::format_decimal(
10353                parse_string_to_decimal_native::<Decimal128Type>("12345", 2).unwrap(),
10354                38,
10355                2,
10356            ),
10357            "12345.00"
10358        );
10359        assert_eq!(
10360            Decimal128Type::format_decimal(
10361                parse_string_to_decimal_native::<Decimal128Type>("0.12345", 2).unwrap(),
10362                38,
10363                2,
10364            ),
10365            "0.12"
10366        );
10367        assert_eq!(
10368            Decimal128Type::format_decimal(
10369                parse_string_to_decimal_native::<Decimal128Type>(".12345", 2).unwrap(),
10370                38,
10371                2,
10372            ),
10373            "0.12"
10374        );
10375        assert_eq!(
10376            Decimal128Type::format_decimal(
10377                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
10378                38,
10379                2,
10380            ),
10381            "0.13"
10382        );
10383        assert_eq!(
10384            Decimal128Type::format_decimal(
10385                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
10386                38,
10387                2,
10388            ),
10389            "0.13"
10390        );
10391
10392        assert_eq!(
10393            Decimal256Type::format_decimal(
10394                parse_string_to_decimal_native::<Decimal256Type>("123.45", 3).unwrap(),
10395                38,
10396                3,
10397            ),
10398            "123.450"
10399        );
10400        assert_eq!(
10401            Decimal256Type::format_decimal(
10402                parse_string_to_decimal_native::<Decimal256Type>("12345", 3).unwrap(),
10403                38,
10404                3,
10405            ),
10406            "12345.000"
10407        );
10408        assert_eq!(
10409            Decimal256Type::format_decimal(
10410                parse_string_to_decimal_native::<Decimal256Type>("0.12345", 3).unwrap(),
10411                38,
10412                3,
10413            ),
10414            "0.123"
10415        );
10416        assert_eq!(
10417            Decimal256Type::format_decimal(
10418                parse_string_to_decimal_native::<Decimal256Type>(".12345", 3).unwrap(),
10419                38,
10420                3,
10421            ),
10422            "0.123"
10423        );
10424        assert_eq!(
10425            Decimal256Type::format_decimal(
10426                parse_string_to_decimal_native::<Decimal256Type>(".1265", 3).unwrap(),
10427                38,
10428                3,
10429            ),
10430            "0.127"
10431        );
10432    }
10433
10434    fn test_cast_string_to_decimal(array: ArrayRef) {
10435        // Decimal128
10436        let output_type = DataType::Decimal128(38, 2);
10437        assert!(can_cast_types(array.data_type(), &output_type));
10438
10439        let casted_array = cast(&array, &output_type).unwrap();
10440        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10441
10442        assert_eq!("123.45", decimal_arr.value_as_string(0));
10443        assert_eq!("1.23", decimal_arr.value_as_string(1));
10444        assert_eq!("0.12", decimal_arr.value_as_string(2));
10445        assert_eq!("0.13", decimal_arr.value_as_string(3));
10446        assert_eq!("1.26", decimal_arr.value_as_string(4));
10447        assert_eq!("12345.00", decimal_arr.value_as_string(5));
10448        assert_eq!("12345.00", decimal_arr.value_as_string(6));
10449        assert_eq!("0.12", decimal_arr.value_as_string(7));
10450        assert_eq!("12.23", decimal_arr.value_as_string(8));
10451        assert!(decimal_arr.is_null(9));
10452        assert_eq!("0.00", decimal_arr.value_as_string(10));
10453        assert_eq!("0.00", decimal_arr.value_as_string(11));
10454        assert!(decimal_arr.is_null(12));
10455        assert_eq!("-1.23", decimal_arr.value_as_string(13));
10456        assert_eq!("-1.24", decimal_arr.value_as_string(14));
10457        assert_eq!("0.00", decimal_arr.value_as_string(15));
10458        assert_eq!("-123.00", decimal_arr.value_as_string(16));
10459        assert_eq!("-123.23", decimal_arr.value_as_string(17));
10460        assert_eq!("-0.12", decimal_arr.value_as_string(18));
10461        assert_eq!("1.23", decimal_arr.value_as_string(19));
10462        assert_eq!("1.24", decimal_arr.value_as_string(20));
10463        assert_eq!("0.00", decimal_arr.value_as_string(21));
10464        assert_eq!("123.00", decimal_arr.value_as_string(22));
10465        assert_eq!("123.23", decimal_arr.value_as_string(23));
10466        assert_eq!("0.12", decimal_arr.value_as_string(24));
10467        assert!(decimal_arr.is_null(25));
10468        assert!(decimal_arr.is_null(26));
10469        assert!(decimal_arr.is_null(27));
10470        assert_eq!("0.00", decimal_arr.value_as_string(28));
10471        assert_eq!("0.00", decimal_arr.value_as_string(29));
10472        assert_eq!("12345.00", decimal_arr.value_as_string(30));
10473        assert_eq!(decimal_arr.len(), 31);
10474
10475        // Decimal256
10476        let output_type = DataType::Decimal256(76, 3);
10477        assert!(can_cast_types(array.data_type(), &output_type));
10478
10479        let casted_array = cast(&array, &output_type).unwrap();
10480        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
10481
10482        assert_eq!("123.450", decimal_arr.value_as_string(0));
10483        assert_eq!("1.235", decimal_arr.value_as_string(1));
10484        assert_eq!("0.123", decimal_arr.value_as_string(2));
10485        assert_eq!("0.127", decimal_arr.value_as_string(3));
10486        assert_eq!("1.263", decimal_arr.value_as_string(4));
10487        assert_eq!("12345.000", decimal_arr.value_as_string(5));
10488        assert_eq!("12345.000", decimal_arr.value_as_string(6));
10489        assert_eq!("0.123", decimal_arr.value_as_string(7));
10490        assert_eq!("12.234", decimal_arr.value_as_string(8));
10491        assert!(decimal_arr.is_null(9));
10492        assert_eq!("0.000", decimal_arr.value_as_string(10));
10493        assert_eq!("0.000", decimal_arr.value_as_string(11));
10494        assert!(decimal_arr.is_null(12));
10495        assert_eq!("-1.235", decimal_arr.value_as_string(13));
10496        assert_eq!("-1.236", decimal_arr.value_as_string(14));
10497        assert_eq!("0.000", decimal_arr.value_as_string(15));
10498        assert_eq!("-123.000", decimal_arr.value_as_string(16));
10499        assert_eq!("-123.234", decimal_arr.value_as_string(17));
10500        assert_eq!("-0.123", decimal_arr.value_as_string(18));
10501        assert_eq!("1.235", decimal_arr.value_as_string(19));
10502        assert_eq!("1.236", decimal_arr.value_as_string(20));
10503        assert_eq!("0.000", decimal_arr.value_as_string(21));
10504        assert_eq!("123.000", decimal_arr.value_as_string(22));
10505        assert_eq!("123.234", decimal_arr.value_as_string(23));
10506        assert_eq!("0.123", decimal_arr.value_as_string(24));
10507        assert!(decimal_arr.is_null(25));
10508        assert!(decimal_arr.is_null(26));
10509        assert!(decimal_arr.is_null(27));
10510        assert_eq!("0.000", decimal_arr.value_as_string(28));
10511        assert_eq!("0.000", decimal_arr.value_as_string(29));
10512        assert_eq!("12345.000", decimal_arr.value_as_string(30));
10513        assert_eq!(decimal_arr.len(), 31);
10514    }
10515
10516    #[test]
10517    fn test_cast_utf8_to_decimal() {
10518        let str_array = StringArray::from(vec![
10519            Some("123.45"),
10520            Some("1.2345"),
10521            Some("0.12345"),
10522            Some("0.1267"),
10523            Some("1.263"),
10524            Some("12345.0"),
10525            Some("12345"),
10526            Some("000.123"),
10527            Some("12.234000"),
10528            None,
10529            Some(""),
10530            Some(" "),
10531            None,
10532            Some("-1.23499999"),
10533            Some("-1.23599999"),
10534            Some("-0.00001"),
10535            Some("-123"),
10536            Some("-123.234000"),
10537            Some("-000.123"),
10538            Some("+1.23499999"),
10539            Some("+1.23599999"),
10540            Some("+0.00001"),
10541            Some("+123"),
10542            Some("+123.234000"),
10543            Some("+000.123"),
10544            Some("1.-23499999"),
10545            Some("-1.-23499999"),
10546            Some("--1.23499999"),
10547            Some("0"),
10548            Some("000.000"),
10549            Some("0000000000000000012345.000"),
10550        ]);
10551        let array = Arc::new(str_array) as ArrayRef;
10552
10553        test_cast_string_to_decimal(array);
10554
10555        let test_cases = [
10556            (None, None),
10557            // (Some(""), None),
10558            // (Some("   "), None),
10559            (Some("0"), Some("0")),
10560            (Some("000.000"), Some("0")),
10561            (Some("12345"), Some("12345")),
10562            (Some("000000000000000000000000000012345"), Some("12345")),
10563            (Some("-123"), Some("-123")),
10564            (Some("+123"), Some("123")),
10565        ];
10566        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
10567        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
10568
10569        let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
10570        test_cast_string_to_decimal_scale_zero(array, &expected);
10571    }
10572
10573    #[test]
10574    fn test_cast_large_utf8_to_decimal() {
10575        let str_array = LargeStringArray::from(vec![
10576            Some("123.45"),
10577            Some("1.2345"),
10578            Some("0.12345"),
10579            Some("0.1267"),
10580            Some("1.263"),
10581            Some("12345.0"),
10582            Some("12345"),
10583            Some("000.123"),
10584            Some("12.234000"),
10585            None,
10586            Some(""),
10587            Some(" "),
10588            None,
10589            Some("-1.23499999"),
10590            Some("-1.23599999"),
10591            Some("-0.00001"),
10592            Some("-123"),
10593            Some("-123.234000"),
10594            Some("-000.123"),
10595            Some("+1.23499999"),
10596            Some("+1.23599999"),
10597            Some("+0.00001"),
10598            Some("+123"),
10599            Some("+123.234000"),
10600            Some("+000.123"),
10601            Some("1.-23499999"),
10602            Some("-1.-23499999"),
10603            Some("--1.23499999"),
10604            Some("0"),
10605            Some("000.000"),
10606            Some("0000000000000000012345.000"),
10607        ]);
10608        let array = Arc::new(str_array) as ArrayRef;
10609
10610        test_cast_string_to_decimal(array);
10611
10612        let test_cases = [
10613            (None, None),
10614            (Some(""), None),
10615            (Some("   "), None),
10616            (Some("0"), Some("0")),
10617            (Some("000.000"), Some("0")),
10618            (Some("12345"), Some("12345")),
10619            (Some("000000000000000000000000000012345"), Some("12345")),
10620            (Some("-123"), Some("-123")),
10621            (Some("+123"), Some("123")),
10622        ];
10623        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
10624        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
10625
10626        let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
10627        test_cast_string_to_decimal_scale_zero(array, &expected);
10628    }
10629
10630    fn test_cast_string_to_decimal_scale_zero(
10631        array: ArrayRef,
10632        expected_as_string: &[Option<&str>],
10633    ) {
10634        // Decimal128
10635        let output_type = DataType::Decimal128(38, 0);
10636        assert!(can_cast_types(array.data_type(), &output_type));
10637        let casted_array = cast(&array, &output_type).unwrap();
10638        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10639        assert_decimal_array_contents(decimal_arr, expected_as_string);
10640
10641        // Decimal256
10642        let output_type = DataType::Decimal256(76, 0);
10643        assert!(can_cast_types(array.data_type(), &output_type));
10644        let casted_array = cast(&array, &output_type).unwrap();
10645        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
10646        assert_decimal_array_contents(decimal_arr, expected_as_string);
10647    }
10648
10649    fn assert_decimal_array_contents<T>(
10650        array: &PrimitiveArray<T>,
10651        expected_as_string: &[Option<&str>],
10652    ) where
10653        T: DecimalType + ArrowPrimitiveType,
10654    {
10655        assert_eq!(array.len(), expected_as_string.len());
10656        for (i, expected) in expected_as_string.iter().enumerate() {
10657            let actual = if array.is_null(i) {
10658                None
10659            } else {
10660                Some(array.value_as_string(i))
10661            };
10662            let actual = actual.as_ref().map(|s| s.as_ref());
10663            assert_eq!(*expected, actual, "Expected at position {i}");
10664        }
10665    }
10666
10667    #[test]
10668    fn test_cast_invalid_utf8_to_decimal() {
10669        let str_array = StringArray::from(vec!["4.4.5", ". 0.123"]);
10670        let array = Arc::new(str_array) as ArrayRef;
10671
10672        // Safe cast
10673        let output_type = DataType::Decimal128(38, 2);
10674        let casted_array = cast(&array, &output_type).unwrap();
10675        assert!(casted_array.is_null(0));
10676        assert!(casted_array.is_null(1));
10677
10678        let output_type = DataType::Decimal256(76, 2);
10679        let casted_array = cast(&array, &output_type).unwrap();
10680        assert!(casted_array.is_null(0));
10681        assert!(casted_array.is_null(1));
10682
10683        // Non-safe cast
10684        let output_type = DataType::Decimal128(38, 2);
10685        let str_array = StringArray::from(vec!["4.4.5"]);
10686        let array = Arc::new(str_array) as ArrayRef;
10687        let option = CastOptions {
10688            safe: false,
10689            format_options: FormatOptions::default(),
10690        };
10691        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
10692        assert!(
10693            casted_err
10694                .to_string()
10695                .contains("Cannot cast string '4.4.5' to value of Decimal128(38, 10) type")
10696        );
10697
10698        let str_array = StringArray::from(vec![". 0.123"]);
10699        let array = Arc::new(str_array) as ArrayRef;
10700        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
10701        assert!(
10702            casted_err
10703                .to_string()
10704                .contains("Cannot cast string '. 0.123' to value of Decimal128(38, 10) type")
10705        );
10706    }
10707
10708    fn test_cast_string_to_decimal128_overflow(overflow_array: ArrayRef) {
10709        let output_type = DataType::Decimal128(38, 2);
10710        let casted_array = cast(&overflow_array, &output_type).unwrap();
10711        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10712
10713        assert!(decimal_arr.is_null(0));
10714        assert!(decimal_arr.is_null(1));
10715        assert!(decimal_arr.is_null(2));
10716        assert_eq!(
10717            "999999999999999999999999999999999999.99",
10718            decimal_arr.value_as_string(3)
10719        );
10720        assert_eq!(
10721            "100000000000000000000000000000000000.00",
10722            decimal_arr.value_as_string(4)
10723        );
10724    }
10725
10726    #[test]
10727    fn test_cast_string_to_decimal128_precision_overflow() {
10728        let array = StringArray::from(vec!["1000".to_string()]);
10729        let array = Arc::new(array) as ArrayRef;
10730        let casted_array = cast_with_options(
10731            &array,
10732            &DataType::Decimal128(10, 8),
10733            &CastOptions {
10734                safe: true,
10735                format_options: FormatOptions::default(),
10736            },
10737        );
10738        assert!(casted_array.is_ok());
10739        assert!(casted_array.unwrap().is_null(0));
10740
10741        let err = cast_with_options(
10742            &array,
10743            &DataType::Decimal128(10, 8),
10744            &CastOptions {
10745                safe: false,
10746                format_options: FormatOptions::default(),
10747            },
10748        );
10749        assert_eq!(
10750            "Invalid argument error: 1000.00000000 is too large to store in a Decimal128 of precision 10. Max is 99.99999999",
10751            err.unwrap_err().to_string()
10752        );
10753    }
10754
10755    #[test]
10756    fn test_cast_utf8_to_decimal128_overflow() {
10757        let overflow_str_array = StringArray::from(vec![
10758            i128::MAX.to_string(),
10759            i128::MIN.to_string(),
10760            "99999999999999999999999999999999999999".to_string(),
10761            "999999999999999999999999999999999999.99".to_string(),
10762            "99999999999999999999999999999999999.999".to_string(),
10763        ]);
10764        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10765
10766        test_cast_string_to_decimal128_overflow(overflow_array);
10767    }
10768
10769    #[test]
10770    fn test_cast_large_utf8_to_decimal128_overflow() {
10771        let overflow_str_array = LargeStringArray::from(vec![
10772            i128::MAX.to_string(),
10773            i128::MIN.to_string(),
10774            "99999999999999999999999999999999999999".to_string(),
10775            "999999999999999999999999999999999999.99".to_string(),
10776            "99999999999999999999999999999999999.999".to_string(),
10777        ]);
10778        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10779
10780        test_cast_string_to_decimal128_overflow(overflow_array);
10781    }
10782
10783    fn test_cast_string_to_decimal256_overflow(overflow_array: ArrayRef) {
10784        let output_type = DataType::Decimal256(76, 2);
10785        let casted_array = cast(&overflow_array, &output_type).unwrap();
10786        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
10787
10788        assert_eq!(
10789            "170141183460469231731687303715884105727.00",
10790            decimal_arr.value_as_string(0)
10791        );
10792        assert_eq!(
10793            "-170141183460469231731687303715884105728.00",
10794            decimal_arr.value_as_string(1)
10795        );
10796        assert_eq!(
10797            "99999999999999999999999999999999999999.00",
10798            decimal_arr.value_as_string(2)
10799        );
10800        assert_eq!(
10801            "999999999999999999999999999999999999.99",
10802            decimal_arr.value_as_string(3)
10803        );
10804        assert_eq!(
10805            "100000000000000000000000000000000000.00",
10806            decimal_arr.value_as_string(4)
10807        );
10808        assert!(decimal_arr.is_null(5));
10809        assert!(decimal_arr.is_null(6));
10810    }
10811
10812    #[test]
10813    fn test_cast_string_to_decimal256_precision_overflow() {
10814        let array = StringArray::from(vec!["1000".to_string()]);
10815        let array = Arc::new(array) as ArrayRef;
10816        let casted_array = cast_with_options(
10817            &array,
10818            &DataType::Decimal256(10, 8),
10819            &CastOptions {
10820                safe: true,
10821                format_options: FormatOptions::default(),
10822            },
10823        );
10824        assert!(casted_array.is_ok());
10825        assert!(casted_array.unwrap().is_null(0));
10826
10827        let err = cast_with_options(
10828            &array,
10829            &DataType::Decimal256(10, 8),
10830            &CastOptions {
10831                safe: false,
10832                format_options: FormatOptions::default(),
10833            },
10834        );
10835        assert_eq!(
10836            "Invalid argument error: 1000.00000000 is too large to store in a Decimal256 of precision 10. Max is 99.99999999",
10837            err.unwrap_err().to_string()
10838        );
10839    }
10840
10841    #[test]
10842    fn test_cast_utf8_to_decimal256_overflow() {
10843        let overflow_str_array = StringArray::from(vec![
10844            i128::MAX.to_string(),
10845            i128::MIN.to_string(),
10846            "99999999999999999999999999999999999999".to_string(),
10847            "999999999999999999999999999999999999.99".to_string(),
10848            "99999999999999999999999999999999999.999".to_string(),
10849            i256::MAX.to_string(),
10850            i256::MIN.to_string(),
10851        ]);
10852        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10853
10854        test_cast_string_to_decimal256_overflow(overflow_array);
10855    }
10856
10857    #[test]
10858    fn test_cast_large_utf8_to_decimal256_overflow() {
10859        let overflow_str_array = LargeStringArray::from(vec![
10860            i128::MAX.to_string(),
10861            i128::MIN.to_string(),
10862            "99999999999999999999999999999999999999".to_string(),
10863            "999999999999999999999999999999999999.99".to_string(),
10864            "99999999999999999999999999999999999.999".to_string(),
10865            i256::MAX.to_string(),
10866            i256::MIN.to_string(),
10867        ]);
10868        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10869
10870        test_cast_string_to_decimal256_overflow(overflow_array);
10871    }
10872
10873    #[test]
10874    fn test_cast_outside_supported_range_for_nanoseconds() {
10875        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";
10876
10877        let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
10878
10879        let cast_options = CastOptions {
10880            safe: false,
10881            format_options: FormatOptions::default(),
10882        };
10883
10884        let result = cast_string_to_timestamp::<i32, TimestampNanosecondType>(
10885            &array,
10886            &None::<Arc<str>>,
10887            &cast_options,
10888        );
10889
10890        let err = result.unwrap_err();
10891        assert_eq!(
10892            err.to_string(),
10893            format!(
10894                "Cast error: Overflow converting {} to Nanosecond. {}",
10895                array.value(0),
10896                EXPECTED_ERROR_MESSAGE
10897            )
10898        );
10899    }
10900
10901    #[test]
10902    fn test_cast_date32_to_timestamp() {
10903        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10904        let array = Arc::new(a) as ArrayRef;
10905        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
10906        let c = b.as_primitive::<TimestampSecondType>();
10907        assert_eq!(1609459200, c.value(0));
10908        assert_eq!(1640995200, c.value(1));
10909        assert!(c.is_null(2));
10910    }
10911
10912    #[test]
10913    fn test_cast_date32_to_timestamp_ms() {
10914        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10915        let array = Arc::new(a) as ArrayRef;
10916        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
10917        let c = b
10918            .as_any()
10919            .downcast_ref::<TimestampMillisecondArray>()
10920            .unwrap();
10921        assert_eq!(1609459200000, c.value(0));
10922        assert_eq!(1640995200000, c.value(1));
10923        assert!(c.is_null(2));
10924    }
10925
10926    #[test]
10927    fn test_cast_date32_to_timestamp_us() {
10928        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10929        let array = Arc::new(a) as ArrayRef;
10930        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
10931        let c = b
10932            .as_any()
10933            .downcast_ref::<TimestampMicrosecondArray>()
10934            .unwrap();
10935        assert_eq!(1609459200000000, c.value(0));
10936        assert_eq!(1640995200000000, c.value(1));
10937        assert!(c.is_null(2));
10938    }
10939
10940    #[test]
10941    fn test_cast_date32_to_timestamp_ns() {
10942        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10943        let array = Arc::new(a) as ArrayRef;
10944        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
10945        let c = b
10946            .as_any()
10947            .downcast_ref::<TimestampNanosecondArray>()
10948            .unwrap();
10949        assert_eq!(1609459200000000000, c.value(0));
10950        assert_eq!(1640995200000000000, c.value(1));
10951        assert!(c.is_null(2));
10952    }
10953
10954    #[test]
10955    fn test_timezone_cast() {
10956        let a = StringArray::from(vec![
10957            "2000-01-01T12:00:00", // date + time valid
10958            "2020-12-15T12:34:56", // date + time valid
10959        ]);
10960        let array = Arc::new(a) as ArrayRef;
10961        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
10962        let v = b.as_primitive::<TimestampNanosecondType>();
10963
10964        assert_eq!(v.value(0), 946728000000000000);
10965        assert_eq!(v.value(1), 1608035696000000000);
10966
10967        let b = cast(
10968            &b,
10969            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10970        )
10971        .unwrap();
10972        let v = b.as_primitive::<TimestampNanosecondType>();
10973
10974        assert_eq!(v.value(0), 946728000000000000);
10975        assert_eq!(v.value(1), 1608035696000000000);
10976
10977        let b = cast(
10978            &b,
10979            &DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
10980        )
10981        .unwrap();
10982        let v = b.as_primitive::<TimestampMillisecondType>();
10983
10984        assert_eq!(v.value(0), 946728000000);
10985        assert_eq!(v.value(1), 1608035696000);
10986    }
10987
10988    #[test]
10989    fn test_cast_utf8_to_timestamp() {
10990        fn test_tz(tz: Arc<str>) {
10991            let valid = StringArray::from(vec![
10992                "2023-01-01 04:05:06.789000-08:00",
10993                "2023-01-01 04:05:06.789000-07:00",
10994                "2023-01-01 04:05:06.789 -0800",
10995                "2023-01-01 04:05:06.789 -08:00",
10996                "2023-01-01 040506 +0730",
10997                "2023-01-01 040506 +07:30",
10998                "2023-01-01 04:05:06.789",
10999                "2023-01-01 04:05:06",
11000                "2023-01-01",
11001            ]);
11002
11003            let array = Arc::new(valid) as ArrayRef;
11004            let b = cast_with_options(
11005                &array,
11006                &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
11007                &CastOptions {
11008                    safe: false,
11009                    format_options: FormatOptions::default(),
11010                },
11011            )
11012            .unwrap();
11013
11014            let tz = tz.as_ref().parse().unwrap();
11015
11016            let as_tz =
11017                |v: i64| as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap();
11018
11019            let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
11020            let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
11021
11022            let values = b.as_primitive::<TimestampNanosecondType>().values();
11023            let utc_results: Vec<_> = values.iter().map(as_utc).collect();
11024            let local_results: Vec<_> = values.iter().map(as_local).collect();
11025
11026            // Absolute timestamps should be parsed preserving the same UTC instant
11027            assert_eq!(
11028                &utc_results[..6],
11029                &[
11030                    "2023-01-01 12:05:06.789".to_string(),
11031                    "2023-01-01 11:05:06.789".to_string(),
11032                    "2023-01-01 12:05:06.789".to_string(),
11033                    "2023-01-01 12:05:06.789".to_string(),
11034                    "2022-12-31 20:35:06".to_string(),
11035                    "2022-12-31 20:35:06".to_string(),
11036                ]
11037            );
11038            // Non-absolute timestamps should be parsed preserving the same local instant
11039            assert_eq!(
11040                &local_results[6..],
11041                &[
11042                    "2023-01-01 04:05:06.789".to_string(),
11043                    "2023-01-01 04:05:06".to_string(),
11044                    "2023-01-01 00:00:00".to_string()
11045                ]
11046            )
11047        }
11048
11049        test_tz("+00:00".into());
11050        test_tz("+02:00".into());
11051    }
11052
11053    #[test]
11054    fn test_cast_invalid_utf8() {
11055        let v1: &[u8] = b"\xFF invalid";
11056        let v2: &[u8] = b"\x00 Foo";
11057        let s = BinaryArray::from(vec![v1, v2]);
11058        let options = CastOptions {
11059            safe: true,
11060            format_options: FormatOptions::default(),
11061        };
11062        let array = cast_with_options(&s, &DataType::Utf8, &options).unwrap();
11063        let a = array.as_string::<i32>();
11064        a.to_data().validate_full().unwrap();
11065
11066        assert_eq!(a.null_count(), 1);
11067        assert_eq!(a.len(), 2);
11068        assert!(a.is_null(0));
11069        assert_eq!(a.value(0), "");
11070        assert_eq!(a.value(1), "\x00 Foo");
11071    }
11072
11073    #[test]
11074    fn test_cast_utf8_to_timestamptz() {
11075        let valid = StringArray::from(vec!["2023-01-01"]);
11076
11077        let array = Arc::new(valid) as ArrayRef;
11078        let b = cast(
11079            &array,
11080            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11081        )
11082        .unwrap();
11083
11084        let expect = DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into()));
11085
11086        assert_eq!(b.data_type(), &expect);
11087        let c = b
11088            .as_any()
11089            .downcast_ref::<TimestampNanosecondArray>()
11090            .unwrap();
11091        assert_eq!(1672531200000000000, c.value(0));
11092    }
11093
11094    #[test]
11095    fn test_cast_decimal_to_string() {
11096        assert!(can_cast_types(
11097            &DataType::Decimal32(9, 4),
11098            &DataType::Utf8View
11099        ));
11100        assert!(can_cast_types(
11101            &DataType::Decimal64(16, 4),
11102            &DataType::Utf8View
11103        ));
11104        assert!(can_cast_types(
11105            &DataType::Decimal128(10, 4),
11106            &DataType::Utf8View
11107        ));
11108        assert!(can_cast_types(
11109            &DataType::Decimal256(38, 10),
11110            &DataType::Utf8View
11111        ));
11112
11113        macro_rules! assert_decimal_values {
11114            ($array:expr) => {
11115                let c = $array;
11116                assert_eq!("1123.454", c.value(0));
11117                assert_eq!("2123.456", c.value(1));
11118                assert_eq!("-3123.453", c.value(2));
11119                assert_eq!("-3123.456", c.value(3));
11120                assert_eq!("0.000", c.value(4));
11121                assert_eq!("0.123", c.value(5));
11122                assert_eq!("1234.567", c.value(6));
11123                assert_eq!("-1234.567", c.value(7));
11124                assert!(c.is_null(8));
11125            };
11126        }
11127
11128        fn test_decimal_to_string<IN: ArrowPrimitiveType, OffsetSize: OffsetSizeTrait>(
11129            output_type: DataType,
11130            array: PrimitiveArray<IN>,
11131        ) {
11132            let b = cast(&array, &output_type).unwrap();
11133
11134            assert_eq!(b.data_type(), &output_type);
11135            match b.data_type() {
11136                DataType::Utf8View => {
11137                    let c = b.as_string_view();
11138                    assert_decimal_values!(c);
11139                }
11140                DataType::Utf8 | DataType::LargeUtf8 => {
11141                    let c = b.as_string::<OffsetSize>();
11142                    assert_decimal_values!(c);
11143                }
11144                _ => (),
11145            }
11146        }
11147
11148        let array32: Vec<Option<i32>> = vec![
11149            Some(1123454),
11150            Some(2123456),
11151            Some(-3123453),
11152            Some(-3123456),
11153            Some(0),
11154            Some(123),
11155            Some(123456789),
11156            Some(-123456789),
11157            None,
11158        ];
11159        let array64: Vec<Option<i64>> = array32.iter().map(|num| num.map(|x| x as i64)).collect();
11160        let array128: Vec<Option<i128>> =
11161            array64.iter().map(|num| num.map(|x| x as i128)).collect();
11162        let array256: Vec<Option<i256>> = array128
11163            .iter()
11164            .map(|num| num.map(i256::from_i128))
11165            .collect();
11166
11167        test_decimal_to_string::<Decimal32Type, i32>(
11168            DataType::Utf8View,
11169            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
11170        );
11171        test_decimal_to_string::<Decimal32Type, i32>(
11172            DataType::Utf8,
11173            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
11174        );
11175        test_decimal_to_string::<Decimal32Type, i64>(
11176            DataType::LargeUtf8,
11177            create_decimal32_array(array32, 7, 3).unwrap(),
11178        );
11179
11180        test_decimal_to_string::<Decimal64Type, i32>(
11181            DataType::Utf8View,
11182            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
11183        );
11184        test_decimal_to_string::<Decimal64Type, i32>(
11185            DataType::Utf8,
11186            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
11187        );
11188        test_decimal_to_string::<Decimal64Type, i64>(
11189            DataType::LargeUtf8,
11190            create_decimal64_array(array64, 7, 3).unwrap(),
11191        );
11192
11193        test_decimal_to_string::<Decimal128Type, i32>(
11194            DataType::Utf8View,
11195            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
11196        );
11197        test_decimal_to_string::<Decimal128Type, i32>(
11198            DataType::Utf8,
11199            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
11200        );
11201        test_decimal_to_string::<Decimal128Type, i64>(
11202            DataType::LargeUtf8,
11203            create_decimal128_array(array128, 7, 3).unwrap(),
11204        );
11205
11206        test_decimal_to_string::<Decimal256Type, i32>(
11207            DataType::Utf8View,
11208            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
11209        );
11210        test_decimal_to_string::<Decimal256Type, i32>(
11211            DataType::Utf8,
11212            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
11213        );
11214        test_decimal_to_string::<Decimal256Type, i64>(
11215            DataType::LargeUtf8,
11216            create_decimal256_array(array256, 7, 3).unwrap(),
11217        );
11218    }
11219
11220    #[test]
11221    fn test_cast_numeric_to_decimal128_precision_overflow() {
11222        let array = Int64Array::from(vec![1234567]);
11223        let array = Arc::new(array) as ArrayRef;
11224        let casted_array = cast_with_options(
11225            &array,
11226            &DataType::Decimal128(7, 3),
11227            &CastOptions {
11228                safe: true,
11229                format_options: FormatOptions::default(),
11230            },
11231        );
11232        assert!(casted_array.is_ok());
11233        assert!(casted_array.unwrap().is_null(0));
11234
11235        let err = cast_with_options(
11236            &array,
11237            &DataType::Decimal128(7, 3),
11238            &CastOptions {
11239                safe: false,
11240                format_options: FormatOptions::default(),
11241            },
11242        );
11243        assert_eq!(
11244            "Invalid argument error: 1234567.000 is too large to store in a Decimal128 of precision 7. Max is 9999.999",
11245            err.unwrap_err().to_string()
11246        );
11247    }
11248
11249    #[test]
11250    fn test_cast_numeric_to_decimal256_precision_overflow() {
11251        let array = Int64Array::from(vec![1234567]);
11252        let array = Arc::new(array) as ArrayRef;
11253        let casted_array = cast_with_options(
11254            &array,
11255            &DataType::Decimal256(7, 3),
11256            &CastOptions {
11257                safe: true,
11258                format_options: FormatOptions::default(),
11259            },
11260        );
11261        assert!(casted_array.is_ok());
11262        assert!(casted_array.unwrap().is_null(0));
11263
11264        let err = cast_with_options(
11265            &array,
11266            &DataType::Decimal256(7, 3),
11267            &CastOptions {
11268                safe: false,
11269                format_options: FormatOptions::default(),
11270            },
11271        );
11272        assert_eq!(
11273            "Invalid argument error: 1234567.000 is too large to store in a Decimal256 of precision 7. Max is 9999.999",
11274            err.unwrap_err().to_string()
11275        );
11276    }
11277
11278    /// helper function to test casting from duration to interval
11279    fn cast_from_duration_to_interval<T: ArrowTemporalType<Native = i64>>(
11280        array: Vec<i64>,
11281        cast_options: &CastOptions,
11282    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
11283        let array = PrimitiveArray::<T>::new(array.into(), None);
11284        let array = Arc::new(array) as ArrayRef;
11285        let interval = DataType::Interval(IntervalUnit::MonthDayNano);
11286        let out = cast_with_options(&array, &interval, cast_options)?;
11287        let out = out.as_primitive::<IntervalMonthDayNanoType>().clone();
11288        Ok(out)
11289    }
11290
11291    #[test]
11292    fn test_cast_from_duration_to_interval() {
11293        // from duration second to interval month day nano
11294        let array = vec![1234567];
11295        let casted_array =
11296            cast_from_duration_to_interval::<DurationSecondType>(array, &CastOptions::default())
11297                .unwrap();
11298        assert_eq!(
11299            casted_array.data_type(),
11300            &DataType::Interval(IntervalUnit::MonthDayNano)
11301        );
11302        assert_eq!(
11303            casted_array.value(0),
11304            IntervalMonthDayNano::new(0, 0, 1234567000000000)
11305        );
11306
11307        let array = vec![i64::MAX];
11308        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
11309            array.clone(),
11310            &CastOptions::default(),
11311        )
11312        .unwrap();
11313        assert!(!casted_array.is_valid(0));
11314
11315        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
11316            array,
11317            &CastOptions {
11318                safe: false,
11319                format_options: FormatOptions::default(),
11320            },
11321        );
11322        assert!(casted_array.is_err());
11323
11324        // from duration millisecond to interval month day nano
11325        let array = vec![1234567];
11326        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11327            array,
11328            &CastOptions::default(),
11329        )
11330        .unwrap();
11331        assert_eq!(
11332            casted_array.data_type(),
11333            &DataType::Interval(IntervalUnit::MonthDayNano)
11334        );
11335        assert_eq!(
11336            casted_array.value(0),
11337            IntervalMonthDayNano::new(0, 0, 1234567000000)
11338        );
11339
11340        let array = vec![i64::MAX];
11341        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11342            array.clone(),
11343            &CastOptions::default(),
11344        )
11345        .unwrap();
11346        assert!(!casted_array.is_valid(0));
11347
11348        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
11349            array,
11350            &CastOptions {
11351                safe: false,
11352                format_options: FormatOptions::default(),
11353            },
11354        );
11355        assert!(casted_array.is_err());
11356
11357        // from duration microsecond to interval month day nano
11358        let array = vec![1234567];
11359        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11360            array,
11361            &CastOptions::default(),
11362        )
11363        .unwrap();
11364        assert_eq!(
11365            casted_array.data_type(),
11366            &DataType::Interval(IntervalUnit::MonthDayNano)
11367        );
11368        assert_eq!(
11369            casted_array.value(0),
11370            IntervalMonthDayNano::new(0, 0, 1234567000)
11371        );
11372
11373        let array = vec![i64::MAX];
11374        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11375            array.clone(),
11376            &CastOptions::default(),
11377        )
11378        .unwrap();
11379        assert!(!casted_array.is_valid(0));
11380
11381        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
11382            array,
11383            &CastOptions {
11384                safe: false,
11385                format_options: FormatOptions::default(),
11386            },
11387        );
11388        assert!(casted_array.is_err());
11389
11390        // from duration nanosecond to interval month day nano
11391        let array = vec![1234567];
11392        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
11393            array,
11394            &CastOptions::default(),
11395        )
11396        .unwrap();
11397        assert_eq!(
11398            casted_array.data_type(),
11399            &DataType::Interval(IntervalUnit::MonthDayNano)
11400        );
11401        assert_eq!(
11402            casted_array.value(0),
11403            IntervalMonthDayNano::new(0, 0, 1234567)
11404        );
11405
11406        let array = vec![i64::MAX];
11407        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
11408            array,
11409            &CastOptions {
11410                safe: false,
11411                format_options: FormatOptions::default(),
11412            },
11413        )
11414        .unwrap();
11415        assert_eq!(
11416            casted_array.value(0),
11417            IntervalMonthDayNano::new(0, 0, i64::MAX)
11418        );
11419    }
11420
11421    /// helper function to test casting from interval to duration
11422    fn cast_from_interval_to_duration<T: ArrowTemporalType>(
11423        array: &IntervalMonthDayNanoArray,
11424        cast_options: &CastOptions,
11425    ) -> Result<PrimitiveArray<T>, ArrowError> {
11426        let casted_array = cast_with_options(&array, &T::DATA_TYPE, cast_options)?;
11427        casted_array
11428            .as_any()
11429            .downcast_ref::<PrimitiveArray<T>>()
11430            .ok_or_else(|| {
11431                ArrowError::ComputeError(format!("Failed to downcast to {}", T::DATA_TYPE))
11432            })
11433            .cloned()
11434    }
11435
11436    #[test]
11437    fn test_cast_from_interval_to_duration() {
11438        let nullable = CastOptions::default();
11439        let fallible = CastOptions {
11440            safe: false,
11441            format_options: FormatOptions::default(),
11442        };
11443        let v = IntervalMonthDayNano::new(0, 0, 1234567);
11444
11445        // from interval month day nano to duration second
11446        let array = vec![v].into();
11447        let casted_array: DurationSecondArray =
11448            cast_from_interval_to_duration(&array, &nullable).unwrap();
11449        assert_eq!(casted_array.value(0), 0);
11450
11451        let array = vec![IntervalMonthDayNano::MAX].into();
11452        let casted_array: DurationSecondArray =
11453            cast_from_interval_to_duration(&array, &nullable).unwrap();
11454        assert!(!casted_array.is_valid(0));
11455
11456        let res = cast_from_interval_to_duration::<DurationSecondType>(&array, &fallible);
11457        assert!(res.is_err());
11458
11459        // from interval month day nano to duration millisecond
11460        let array = vec![v].into();
11461        let casted_array: DurationMillisecondArray =
11462            cast_from_interval_to_duration(&array, &nullable).unwrap();
11463        assert_eq!(casted_array.value(0), 1);
11464
11465        let array = vec![IntervalMonthDayNano::MAX].into();
11466        let casted_array: DurationMillisecondArray =
11467            cast_from_interval_to_duration(&array, &nullable).unwrap();
11468        assert!(!casted_array.is_valid(0));
11469
11470        let res = cast_from_interval_to_duration::<DurationMillisecondType>(&array, &fallible);
11471        assert!(res.is_err());
11472
11473        // from interval month day nano to duration microsecond
11474        let array = vec![v].into();
11475        let casted_array: DurationMicrosecondArray =
11476            cast_from_interval_to_duration(&array, &nullable).unwrap();
11477        assert_eq!(casted_array.value(0), 1234);
11478
11479        let array = vec![IntervalMonthDayNano::MAX].into();
11480        let casted_array =
11481            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &nullable).unwrap();
11482        assert!(!casted_array.is_valid(0));
11483
11484        let casted_array =
11485            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &fallible);
11486        assert!(casted_array.is_err());
11487
11488        // from interval month day nano to duration nanosecond
11489        let array = vec![v].into();
11490        let casted_array: DurationNanosecondArray =
11491            cast_from_interval_to_duration(&array, &nullable).unwrap();
11492        assert_eq!(casted_array.value(0), 1234567);
11493
11494        let array = vec![IntervalMonthDayNano::MAX].into();
11495        let casted_array: DurationNanosecondArray =
11496            cast_from_interval_to_duration(&array, &nullable).unwrap();
11497        assert!(!casted_array.is_valid(0));
11498
11499        let casted_array =
11500            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &fallible);
11501        assert!(casted_array.is_err());
11502
11503        let array = vec![
11504            IntervalMonthDayNanoType::make_value(0, 1, 0),
11505            IntervalMonthDayNanoType::make_value(-1, 0, 0),
11506            IntervalMonthDayNanoType::make_value(1, 1, 0),
11507            IntervalMonthDayNanoType::make_value(1, 0, 1),
11508            IntervalMonthDayNanoType::make_value(0, 0, -1),
11509        ]
11510        .into();
11511        let casted_array =
11512            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable).unwrap();
11513        assert!(!casted_array.is_valid(0));
11514        assert!(!casted_array.is_valid(1));
11515        assert!(!casted_array.is_valid(2));
11516        assert!(!casted_array.is_valid(3));
11517        assert!(casted_array.is_valid(4));
11518        assert_eq!(casted_array.value(4), -1);
11519    }
11520
11521    /// helper function to test casting from interval year month to interval month day nano
11522    fn cast_from_interval_year_month_to_interval_month_day_nano(
11523        array: Vec<i32>,
11524        cast_options: &CastOptions,
11525    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
11526        let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
11527        let array = Arc::new(array) as ArrayRef;
11528        let casted_array = cast_with_options(
11529            &array,
11530            &DataType::Interval(IntervalUnit::MonthDayNano),
11531            cast_options,
11532        )?;
11533        casted_array
11534            .as_any()
11535            .downcast_ref::<IntervalMonthDayNanoArray>()
11536            .ok_or_else(|| {
11537                ArrowError::ComputeError(
11538                    "Failed to downcast to IntervalMonthDayNanoArray".to_string(),
11539                )
11540            })
11541            .cloned()
11542    }
11543
11544    #[test]
11545    fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
11546        // from interval year month to interval month day nano
11547        let array = vec![1234567];
11548        let casted_array = cast_from_interval_year_month_to_interval_month_day_nano(
11549            array,
11550            &CastOptions::default(),
11551        )
11552        .unwrap();
11553        assert_eq!(
11554            casted_array.data_type(),
11555            &DataType::Interval(IntervalUnit::MonthDayNano)
11556        );
11557        assert_eq!(
11558            casted_array.value(0),
11559            IntervalMonthDayNano::new(1234567, 0, 0)
11560        );
11561    }
11562
11563    /// helper function to test casting from interval day time to interval month day nano
11564    fn cast_from_interval_day_time_to_interval_month_day_nano(
11565        array: Vec<IntervalDayTime>,
11566        cast_options: &CastOptions,
11567    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
11568        let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
11569        let array = Arc::new(array) as ArrayRef;
11570        let casted_array = cast_with_options(
11571            &array,
11572            &DataType::Interval(IntervalUnit::MonthDayNano),
11573            cast_options,
11574        )?;
11575        Ok(casted_array
11576            .as_primitive::<IntervalMonthDayNanoType>()
11577            .clone())
11578    }
11579
11580    #[test]
11581    fn test_cast_from_interval_day_time_to_interval_month_day_nano() {
11582        // from interval day time to interval month day nano
11583        let array = vec![IntervalDayTime::new(123, 0)];
11584        let casted_array =
11585            cast_from_interval_day_time_to_interval_month_day_nano(array, &CastOptions::default())
11586                .unwrap();
11587        assert_eq!(
11588            casted_array.data_type(),
11589            &DataType::Interval(IntervalUnit::MonthDayNano)
11590        );
11591        assert_eq!(casted_array.value(0), IntervalMonthDayNano::new(0, 123, 0));
11592    }
11593
11594    #[test]
11595    fn test_cast_below_unixtimestamp() {
11596        let valid = StringArray::from(vec![
11597            "1900-01-03 23:59:59",
11598            "1969-12-31 00:00:01",
11599            "1989-12-31 00:00:01",
11600        ]);
11601
11602        let array = Arc::new(valid) as ArrayRef;
11603        let casted_array = cast_with_options(
11604            &array,
11605            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
11606            &CastOptions {
11607                safe: false,
11608                format_options: FormatOptions::default(),
11609            },
11610        )
11611        .unwrap();
11612
11613        let ts_array = casted_array
11614            .as_primitive::<TimestampNanosecondType>()
11615            .values()
11616            .iter()
11617            .map(|ts| ts / 1_000_000)
11618            .collect::<Vec<_>>();
11619
11620        let array = TimestampMillisecondArray::from(ts_array).with_timezone("+00:00".to_string());
11621        let casted_array = cast(&array, &DataType::Date32).unwrap();
11622        let date_array = casted_array.as_primitive::<Date32Type>();
11623        let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
11624        let string_array = casted_array.as_string::<i32>();
11625        assert_eq!("1900-01-03", string_array.value(0));
11626        assert_eq!("1969-12-31", string_array.value(1));
11627        assert_eq!("1989-12-31", string_array.value(2));
11628    }
11629
11630    #[test]
11631    fn test_nested_list() {
11632        let mut list = ListBuilder::new(Int32Builder::new());
11633        list.append_value([Some(1), Some(2), Some(3)]);
11634        list.append_value([Some(4), None, Some(6)]);
11635        let list = list.finish();
11636
11637        let to_field = Field::new("nested", list.data_type().clone(), false);
11638        let to = DataType::List(Arc::new(to_field));
11639        let out = cast(&list, &to).unwrap();
11640        let opts = FormatOptions::default().with_null("null");
11641        let formatted = ArrayFormatter::try_new(out.as_ref(), &opts).unwrap();
11642
11643        assert_eq!(formatted.value(0).to_string(), "[[1], [2], [3]]");
11644        assert_eq!(formatted.value(1).to_string(), "[[4], [null], [6]]");
11645    }
11646
11647    #[test]
11648    fn test_nested_list_cast() {
11649        let mut builder = ListBuilder::new(ListBuilder::new(Int32Builder::new()));
11650        builder.append_value([Some([Some(1), Some(2), None]), None]);
11651        builder.append_value([None, Some([]), None]);
11652        builder.append_null();
11653        builder.append_value([Some([Some(2), Some(3)])]);
11654        let start = builder.finish();
11655
11656        let mut builder = LargeListBuilder::new(LargeListBuilder::new(Int8Builder::new()));
11657        builder.append_value([Some([Some(1), Some(2), None]), None]);
11658        builder.append_value([None, Some([]), None]);
11659        builder.append_null();
11660        builder.append_value([Some([Some(2), Some(3)])]);
11661        let expected = builder.finish();
11662
11663        let actual = cast(&start, expected.data_type()).unwrap();
11664        assert_eq!(actual.as_ref(), &expected);
11665    }
11666
11667    const CAST_OPTIONS: CastOptions<'static> = CastOptions {
11668        safe: true,
11669        format_options: FormatOptions::new(),
11670    };
11671
11672    #[test]
11673    #[allow(clippy::assertions_on_constants)]
11674    fn test_const_options() {
11675        assert!(CAST_OPTIONS.safe)
11676    }
11677
11678    #[test]
11679    fn test_list_format_options() {
11680        let options = CastOptions {
11681            safe: false,
11682            format_options: FormatOptions::default().with_null("null"),
11683        };
11684        let array = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
11685            Some(vec![Some(0), Some(1), Some(2)]),
11686            Some(vec![Some(0), None, Some(2)]),
11687        ]);
11688        let a = cast_with_options(&array, &DataType::Utf8, &options).unwrap();
11689        let r: Vec<_> = a.as_string::<i32>().iter().flatten().collect();
11690        assert_eq!(r, &["[0, 1, 2]", "[0, null, 2]"]);
11691    }
11692    #[test]
11693    fn test_cast_string_to_timestamp_invalid_tz() {
11694        // content after Z should be ignored
11695        let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
11696        let array = StringArray::from(vec![Some(bad_timestamp)]);
11697
11698        let data_types = [
11699            DataType::Timestamp(TimeUnit::Second, None),
11700            DataType::Timestamp(TimeUnit::Millisecond, None),
11701            DataType::Timestamp(TimeUnit::Microsecond, None),
11702            DataType::Timestamp(TimeUnit::Nanosecond, None),
11703        ];
11704
11705        let cast_options = CastOptions {
11706            safe: false,
11707            ..Default::default()
11708        };
11709
11710        for dt in data_types {
11711            assert_eq!(
11712                cast_with_options(&array, &dt, &cast_options)
11713                    .unwrap_err()
11714                    .to_string(),
11715                "Parser error: Invalid timezone \"ZZTOP\": only offset based timezones supported without chrono-tz feature"
11716            );
11717        }
11718    }
11719    #[test]
11720    fn test_cast_struct_to_struct() {
11721        let struct_type = DataType::Struct(
11722            vec![
11723                Field::new("a", DataType::Boolean, false),
11724                Field::new("b", DataType::Int32, false),
11725            ]
11726            .into(),
11727        );
11728        let to_type = DataType::Struct(
11729            vec![
11730                Field::new("a", DataType::Utf8, false),
11731                Field::new("b", DataType::Utf8, false),
11732            ]
11733            .into(),
11734        );
11735        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11736        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
11737        let struct_array = StructArray::from(vec![
11738            (
11739                Arc::new(Field::new("b", DataType::Boolean, false)),
11740                boolean.clone() as ArrayRef,
11741            ),
11742            (
11743                Arc::new(Field::new("c", DataType::Int32, false)),
11744                int.clone() as ArrayRef,
11745            ),
11746        ]);
11747        let casted_array = cast(&struct_array, &to_type).unwrap();
11748        let casted_array = casted_array.as_struct();
11749        assert_eq!(casted_array.data_type(), &to_type);
11750        let casted_boolean_array = casted_array
11751            .column(0)
11752            .as_string::<i32>()
11753            .into_iter()
11754            .flatten()
11755            .collect::<Vec<_>>();
11756        let casted_int_array = casted_array
11757            .column(1)
11758            .as_string::<i32>()
11759            .into_iter()
11760            .flatten()
11761            .collect::<Vec<_>>();
11762        assert_eq!(casted_boolean_array, vec!["false", "false", "true", "true"]);
11763        assert_eq!(casted_int_array, vec!["42", "28", "19", "31"]);
11764
11765        // test for can't cast
11766        let to_type = DataType::Struct(
11767            vec![
11768                Field::new("a", DataType::Date32, false),
11769                Field::new("b", DataType::Utf8, false),
11770            ]
11771            .into(),
11772        );
11773        assert!(!can_cast_types(&struct_type, &to_type));
11774        let result = cast(&struct_array, &to_type);
11775        assert_eq!(
11776            "Cast error: Casting from Boolean to Date32 not supported",
11777            result.unwrap_err().to_string()
11778        );
11779    }
11780
11781    #[test]
11782    fn test_cast_struct_to_struct_nullability() {
11783        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11784        let int = Arc::new(Int32Array::from(vec![Some(42), None, Some(19), None]));
11785        let struct_array = StructArray::from(vec![
11786            (
11787                Arc::new(Field::new("b", DataType::Boolean, false)),
11788                boolean.clone() as ArrayRef,
11789            ),
11790            (
11791                Arc::new(Field::new("c", DataType::Int32, true)),
11792                int.clone() as ArrayRef,
11793            ),
11794        ]);
11795
11796        // okay: nullable to nullable
11797        let to_type = DataType::Struct(
11798            vec![
11799                Field::new("a", DataType::Utf8, false),
11800                Field::new("b", DataType::Utf8, true),
11801            ]
11802            .into(),
11803        );
11804        cast(&struct_array, &to_type).expect("Cast nullable to nullable struct field should work");
11805
11806        // error: nullable to non-nullable
11807        let to_type = DataType::Struct(
11808            vec![
11809                Field::new("a", DataType::Utf8, false),
11810                Field::new("b", DataType::Utf8, false),
11811            ]
11812            .into(),
11813        );
11814        cast(&struct_array, &to_type)
11815            .expect_err("Cast nullable to non-nullable struct field should fail");
11816
11817        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11818        let int = Arc::new(Int32Array::from(vec![i32::MAX, 25, 1, 100]));
11819        let struct_array = StructArray::from(vec![
11820            (
11821                Arc::new(Field::new("b", DataType::Boolean, false)),
11822                boolean.clone() as ArrayRef,
11823            ),
11824            (
11825                Arc::new(Field::new("c", DataType::Int32, false)),
11826                int.clone() as ArrayRef,
11827            ),
11828        ]);
11829
11830        // okay: non-nullable to non-nullable
11831        let to_type = DataType::Struct(
11832            vec![
11833                Field::new("a", DataType::Utf8, false),
11834                Field::new("b", DataType::Utf8, false),
11835            ]
11836            .into(),
11837        );
11838        cast(&struct_array, &to_type)
11839            .expect("Cast non-nullable to non-nullable struct field should work");
11840
11841        // err: non-nullable to non-nullable but overflowing return null during casting
11842        let to_type = DataType::Struct(
11843            vec![
11844                Field::new("a", DataType::Utf8, false),
11845                Field::new("b", DataType::Int8, false),
11846            ]
11847            .into(),
11848        );
11849        cast(&struct_array, &to_type).expect_err(
11850            "Cast non-nullable to non-nullable struct field returning null should fail",
11851        );
11852    }
11853
11854    #[test]
11855    fn test_cast_struct_to_non_struct() {
11856        let boolean = Arc::new(BooleanArray::from(vec![true, false]));
11857        let struct_array = StructArray::from(vec![(
11858            Arc::new(Field::new("a", DataType::Boolean, false)),
11859            boolean.clone() as ArrayRef,
11860        )]);
11861        let to_type = DataType::Utf8;
11862        let result = cast(&struct_array, &to_type);
11863        assert_eq!(
11864            r#"Cast error: Casting from Struct("a": non-null Boolean) to Utf8 not supported"#,
11865            result.unwrap_err().to_string()
11866        );
11867    }
11868
11869    #[test]
11870    fn test_cast_non_struct_to_struct() {
11871        let array = StringArray::from(vec!["a", "b"]);
11872        let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
11873        let result = cast(&array, &to_type);
11874        assert_eq!(
11875            r#"Cast error: Casting from Utf8 to Struct("a": non-null Boolean) not supported"#,
11876            result.unwrap_err().to_string()
11877        );
11878    }
11879
11880    #[test]
11881    fn test_cast_struct_with_different_field_order() {
11882        // Test slow path: fields are in different order
11883        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11884        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
11885        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
11886
11887        let struct_array = StructArray::from(vec![
11888            (
11889                Arc::new(Field::new("a", DataType::Boolean, false)),
11890                boolean.clone() as ArrayRef,
11891            ),
11892            (
11893                Arc::new(Field::new("b", DataType::Int32, false)),
11894                int.clone() as ArrayRef,
11895            ),
11896            (
11897                Arc::new(Field::new("c", DataType::Utf8, false)),
11898                string.clone() as ArrayRef,
11899            ),
11900        ]);
11901
11902        // Target has fields in different order: c, a, b instead of a, b, c
11903        let to_type = DataType::Struct(
11904            vec![
11905                Field::new("c", DataType::Utf8, false),
11906                Field::new("a", DataType::Utf8, false), // Boolean to Utf8
11907                Field::new("b", DataType::Utf8, false), // Int32 to Utf8
11908            ]
11909            .into(),
11910        );
11911
11912        let result = cast(&struct_array, &to_type).unwrap();
11913        let result_struct = result.as_struct();
11914
11915        assert_eq!(result_struct.data_type(), &to_type);
11916        assert_eq!(result_struct.num_columns(), 3);
11917
11918        // Verify field "c" (originally position 2, now position 0) remains Utf8
11919        let c_column = result_struct.column(0).as_string::<i32>();
11920        assert_eq!(
11921            c_column.into_iter().flatten().collect::<Vec<_>>(),
11922            vec!["foo", "bar", "baz", "qux"]
11923        );
11924
11925        // Verify field "a" (originally position 0, now position 1) was cast from Boolean to Utf8
11926        let a_column = result_struct.column(1).as_string::<i32>();
11927        assert_eq!(
11928            a_column.into_iter().flatten().collect::<Vec<_>>(),
11929            vec!["false", "false", "true", "true"]
11930        );
11931
11932        // Verify field "b" (originally position 1, now position 2) was cast from Int32 to Utf8
11933        let b_column = result_struct.column(2).as_string::<i32>();
11934        assert_eq!(
11935            b_column.into_iter().flatten().collect::<Vec<_>>(),
11936            vec!["42", "28", "19", "31"]
11937        );
11938    }
11939
11940    #[test]
11941    fn test_cast_struct_with_missing_field() {
11942        // Test that casting fails when target has a field not present in source
11943        let boolean = Arc::new(BooleanArray::from(vec![false, true]));
11944        let struct_array = StructArray::from(vec![(
11945            Arc::new(Field::new("a", DataType::Boolean, false)),
11946            boolean.clone() as ArrayRef,
11947        )]);
11948
11949        let to_type = DataType::Struct(
11950            vec![
11951                Field::new("a", DataType::Utf8, false),
11952                Field::new("b", DataType::Int32, false), // Field "b" doesn't exist in source
11953            ]
11954            .into(),
11955        );
11956
11957        let result = cast(&struct_array, &to_type);
11958        assert!(result.is_err());
11959        assert_eq!(
11960            result.unwrap_err().to_string(),
11961            "Invalid argument error: Incorrect number of arrays for StructArray fields, expected 2 got 1"
11962        );
11963    }
11964
11965    #[test]
11966    fn test_cast_struct_with_subset_of_fields() {
11967        // Test casting to a struct with fewer fields (selecting a subset)
11968        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11969        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
11970        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
11971
11972        let struct_array = StructArray::from(vec![
11973            (
11974                Arc::new(Field::new("a", DataType::Boolean, false)),
11975                boolean.clone() as ArrayRef,
11976            ),
11977            (
11978                Arc::new(Field::new("b", DataType::Int32, false)),
11979                int.clone() as ArrayRef,
11980            ),
11981            (
11982                Arc::new(Field::new("c", DataType::Utf8, false)),
11983                string.clone() as ArrayRef,
11984            ),
11985        ]);
11986
11987        // Target has only fields "c" and "a", omitting "b"
11988        let to_type = DataType::Struct(
11989            vec![
11990                Field::new("c", DataType::Utf8, false),
11991                Field::new("a", DataType::Utf8, false),
11992            ]
11993            .into(),
11994        );
11995
11996        let result = cast(&struct_array, &to_type).unwrap();
11997        let result_struct = result.as_struct();
11998
11999        assert_eq!(result_struct.data_type(), &to_type);
12000        assert_eq!(result_struct.num_columns(), 2);
12001
12002        // Verify field "c" remains Utf8
12003        let c_column = result_struct.column(0).as_string::<i32>();
12004        assert_eq!(
12005            c_column.into_iter().flatten().collect::<Vec<_>>(),
12006            vec!["foo", "bar", "baz", "qux"]
12007        );
12008
12009        // Verify field "a" was cast from Boolean to Utf8
12010        let a_column = result_struct.column(1).as_string::<i32>();
12011        assert_eq!(
12012            a_column.into_iter().flatten().collect::<Vec<_>>(),
12013            vec!["false", "false", "true", "true"]
12014        );
12015    }
12016
12017    #[test]
12018    fn test_can_cast_struct_rename_field() {
12019        // Test that can_cast_types returns false when target has a field not in source
12020        let from_type = DataType::Struct(
12021            vec![
12022                Field::new("a", DataType::Int32, false),
12023                Field::new("b", DataType::Utf8, false),
12024            ]
12025            .into(),
12026        );
12027
12028        let to_type = DataType::Struct(
12029            vec![
12030                Field::new("a", DataType::Int64, false),
12031                Field::new("c", DataType::Boolean, false), // Field "c" not in source
12032            ]
12033            .into(),
12034        );
12035
12036        assert!(can_cast_types(&from_type, &to_type));
12037    }
12038
12039    fn run_decimal_cast_test_case_between_multiple_types(t: DecimalCastTestConfig) {
12040        run_decimal_cast_test_case::<Decimal128Type, Decimal128Type>(t.clone());
12041        run_decimal_cast_test_case::<Decimal128Type, Decimal256Type>(t.clone());
12042        run_decimal_cast_test_case::<Decimal256Type, Decimal128Type>(t.clone());
12043        run_decimal_cast_test_case::<Decimal256Type, Decimal256Type>(t.clone());
12044    }
12045
12046    #[test]
12047    fn test_decimal_to_decimal_coverage() {
12048        let test_cases = [
12049            // increase precision, increase scale, infallible
12050            DecimalCastTestConfig {
12051                input_prec: 5,
12052                input_scale: 1,
12053                input_repr: 99999, // 9999.9
12054                output_prec: 10,
12055                output_scale: 6,
12056                expected_output_repr: Ok(9999900000), // 9999.900000
12057            },
12058            // increase precision, increase scale, fallible, safe
12059            DecimalCastTestConfig {
12060                input_prec: 5,
12061                input_scale: 1,
12062                input_repr: 99, // 9999.9
12063                output_prec: 7,
12064                output_scale: 6,
12065                expected_output_repr: Ok(9900000), // 9.900000
12066            },
12067            // increase precision, increase scale, fallible, unsafe
12068            DecimalCastTestConfig {
12069                input_prec: 5,
12070                input_scale: 1,
12071                input_repr: 99999, // 9999.9
12072                output_prec: 7,
12073                output_scale: 6,
12074                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
12075            },
12076            // increase precision, decrease scale, always infallible
12077            DecimalCastTestConfig {
12078                input_prec: 5,
12079                input_scale: 3,
12080                input_repr: 99999, // 99.999
12081                output_prec: 10,
12082                output_scale: 2,
12083                expected_output_repr: Ok(10000), // 100.00
12084            },
12085            // increase precision, decrease scale, no rouding
12086            DecimalCastTestConfig {
12087                input_prec: 5,
12088                input_scale: 3,
12089                input_repr: 99994, // 99.994
12090                output_prec: 10,
12091                output_scale: 2,
12092                expected_output_repr: Ok(9999), // 99.99
12093            },
12094            // increase precision, don't change scale, always infallible
12095            DecimalCastTestConfig {
12096                input_prec: 5,
12097                input_scale: 3,
12098                input_repr: 99999, // 99.999
12099                output_prec: 10,
12100                output_scale: 3,
12101                expected_output_repr: Ok(99999), // 99.999
12102            },
12103            // decrease precision, increase scale, safe
12104            DecimalCastTestConfig {
12105                input_prec: 10,
12106                input_scale: 5,
12107                input_repr: 999999, // 9.99999
12108                output_prec: 8,
12109                output_scale: 7,
12110                expected_output_repr: Ok(99999900), // 9.9999900
12111            },
12112            // decrease precision, increase scale, unsafe
12113            DecimalCastTestConfig {
12114                input_prec: 10,
12115                input_scale: 5,
12116                input_repr: 9999999, // 99.99999
12117                output_prec: 8,
12118                output_scale: 7,
12119                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
12120            },
12121            // decrease precision, decrease scale, safe, infallible
12122            DecimalCastTestConfig {
12123                input_prec: 7,
12124                input_scale: 4,
12125                input_repr: 9999999, // 999.9999
12126                output_prec: 6,
12127                output_scale: 2,
12128                expected_output_repr: Ok(100000),
12129            },
12130            // decrease precision, decrease scale, safe, fallible
12131            DecimalCastTestConfig {
12132                input_prec: 10,
12133                input_scale: 5,
12134                input_repr: 12345678, // 123.45678
12135                output_prec: 8,
12136                output_scale: 3,
12137                expected_output_repr: Ok(123457), // 123.457
12138            },
12139            // decrease precision, decrease scale, unsafe
12140            DecimalCastTestConfig {
12141                input_prec: 10,
12142                input_scale: 5,
12143                input_repr: 9999999, // 99.99999
12144                output_prec: 4,
12145                output_scale: 3,
12146                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
12147            },
12148            // decrease precision, same scale, safe
12149            DecimalCastTestConfig {
12150                input_prec: 10,
12151                input_scale: 5,
12152                input_repr: 999999, // 9.99999
12153                output_prec: 6,
12154                output_scale: 5,
12155                expected_output_repr: Ok(999999), // 9.99999
12156            },
12157            // decrease precision, same scale, unsafe
12158            DecimalCastTestConfig {
12159                input_prec: 10,
12160                input_scale: 5,
12161                input_repr: 9999999, // 99.99999
12162                output_prec: 6,
12163                output_scale: 5,
12164                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
12165            },
12166            // same precision, increase scale, safe
12167            DecimalCastTestConfig {
12168                input_prec: 7,
12169                input_scale: 4,
12170                input_repr: 12345, // 1.2345
12171                output_prec: 7,
12172                output_scale: 6,
12173                expected_output_repr: Ok(1234500), // 1.234500
12174            },
12175            // same precision, increase scale, unsafe
12176            DecimalCastTestConfig {
12177                input_prec: 7,
12178                input_scale: 4,
12179                input_repr: 123456, // 12.3456
12180                output_prec: 7,
12181                output_scale: 6,
12182                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
12183            },
12184            // same precision, decrease scale, infallible
12185            DecimalCastTestConfig {
12186                input_prec: 7,
12187                input_scale: 5,
12188                input_repr: 1234567, // 12.34567
12189                output_prec: 7,
12190                output_scale: 4,
12191                expected_output_repr: Ok(123457), // 12.3457
12192            },
12193            // same precision, same scale, infallible
12194            DecimalCastTestConfig {
12195                input_prec: 7,
12196                input_scale: 5,
12197                input_repr: 9999999, // 99.99999
12198                output_prec: 7,
12199                output_scale: 5,
12200                expected_output_repr: Ok(9999999), // 99.99999
12201            },
12202            // precision increase, input scale & output scale = 0, infallible
12203            DecimalCastTestConfig {
12204                input_prec: 7,
12205                input_scale: 0,
12206                input_repr: 1234567, // 1234567
12207                output_prec: 8,
12208                output_scale: 0,
12209                expected_output_repr: Ok(1234567), // 1234567
12210            },
12211            // precision decrease, input scale & output scale = 0, failure
12212            DecimalCastTestConfig {
12213                input_prec: 7,
12214                input_scale: 0,
12215                input_repr: 1234567, // 1234567
12216                output_prec: 6,
12217                output_scale: 0,
12218                expected_output_repr: Err("Invalid argument error: 1234567 is too large to store in a {} of precision 6. Max is 999999".to_string())
12219            },
12220            // precision decrease, input scale & output scale = 0, success
12221            DecimalCastTestConfig {
12222                input_prec: 7,
12223                input_scale: 0,
12224                input_repr: 123456, // 123456
12225                output_prec: 6,
12226                output_scale: 0,
12227                expected_output_repr: Ok(123456), // 123456
12228            },
12229        ];
12230
12231        for t in test_cases {
12232            run_decimal_cast_test_case_between_multiple_types(t);
12233        }
12234    }
12235
12236    #[test]
12237    fn test_decimal_to_decimal_increase_scale_and_precision_unchecked() {
12238        let test_cases = [
12239            DecimalCastTestConfig {
12240                input_prec: 5,
12241                input_scale: 0,
12242                input_repr: 99999,
12243                output_prec: 10,
12244                output_scale: 5,
12245                expected_output_repr: Ok(9999900000),
12246            },
12247            DecimalCastTestConfig {
12248                input_prec: 5,
12249                input_scale: 0,
12250                input_repr: -99999,
12251                output_prec: 10,
12252                output_scale: 5,
12253                expected_output_repr: Ok(-9999900000),
12254            },
12255            DecimalCastTestConfig {
12256                input_prec: 5,
12257                input_scale: 2,
12258                input_repr: 99999,
12259                output_prec: 10,
12260                output_scale: 5,
12261                expected_output_repr: Ok(99999000),
12262            },
12263            DecimalCastTestConfig {
12264                input_prec: 5,
12265                input_scale: -2,
12266                input_repr: -99999,
12267                output_prec: 10,
12268                output_scale: 3,
12269                expected_output_repr: Ok(-9999900000),
12270            },
12271            DecimalCastTestConfig {
12272                input_prec: 5,
12273                input_scale: 3,
12274                input_repr: -12345,
12275                output_prec: 6,
12276                output_scale: 5,
12277                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())
12278            },
12279        ];
12280
12281        for t in test_cases {
12282            run_decimal_cast_test_case_between_multiple_types(t);
12283        }
12284    }
12285
12286    #[test]
12287    fn test_decimal_to_decimal_decrease_scale_and_precision_unchecked() {
12288        let test_cases = [
12289            DecimalCastTestConfig {
12290                input_prec: 5,
12291                input_scale: 0,
12292                input_repr: 99999,
12293                output_scale: -3,
12294                output_prec: 3,
12295                expected_output_repr: Ok(100),
12296            },
12297            DecimalCastTestConfig {
12298                input_prec: 5,
12299                input_scale: 0,
12300                input_repr: -99999,
12301                output_prec: 1,
12302                output_scale: -5,
12303                expected_output_repr: Ok(-1),
12304            },
12305            DecimalCastTestConfig {
12306                input_prec: 10,
12307                input_scale: 2,
12308                input_repr: 123456789,
12309                output_prec: 5,
12310                output_scale: -2,
12311                expected_output_repr: Ok(12346),
12312            },
12313            DecimalCastTestConfig {
12314                input_prec: 10,
12315                input_scale: 4,
12316                input_repr: -9876543210,
12317                output_prec: 7,
12318                output_scale: 0,
12319                expected_output_repr: Ok(-987654),
12320            },
12321            DecimalCastTestConfig {
12322                input_prec: 7,
12323                input_scale: 4,
12324                input_repr: 9999999,
12325                output_prec: 6,
12326                output_scale: 3,
12327                expected_output_repr:
12328                    Err("Invalid argument error: 1000.000 is too large to store in a {} of precision 6. Max is 999.999".to_string()),
12329            },
12330        ];
12331        for t in test_cases {
12332            run_decimal_cast_test_case_between_multiple_types(t);
12333        }
12334    }
12335
12336    #[test]
12337    fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
12338        let array = vec![Some(123456789)];
12339        let array = create_decimal128_array(array, 24, 2).unwrap();
12340        let input_type = DataType::Decimal128(24, 2);
12341        let output_type = DataType::Decimal128(6, 2);
12342        assert!(can_cast_types(&input_type, &output_type));
12343
12344        let options = CastOptions {
12345            safe: false,
12346            ..Default::default()
12347        };
12348        let result = cast_with_options(&array, &output_type, &options);
12349        assert_eq!(
12350            result.unwrap_err().to_string(),
12351            "Invalid argument error: 1234567.89 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
12352        );
12353    }
12354
12355    #[test]
12356    fn test_decimal_to_decimal_same_scale() {
12357        let array = vec![Some(520)];
12358        let array = create_decimal128_array(array, 4, 2).unwrap();
12359        let input_type = DataType::Decimal128(4, 2);
12360        let output_type = DataType::Decimal128(3, 2);
12361        assert!(can_cast_types(&input_type, &output_type));
12362
12363        let options = CastOptions {
12364            safe: false,
12365            ..Default::default()
12366        };
12367        let result = cast_with_options(&array, &output_type, &options);
12368        assert_eq!(
12369            result.unwrap().as_primitive::<Decimal128Type>().value(0),
12370            520
12371        );
12372
12373        // Cast 0 of decimal(3, 0) type to decimal(2, 0)
12374        assert_eq!(
12375            &cast(
12376                &create_decimal128_array(vec![Some(0)], 3, 0).unwrap(),
12377                &DataType::Decimal128(2, 0)
12378            )
12379            .unwrap(),
12380            &(Arc::new(create_decimal128_array(vec![Some(0)], 2, 0).unwrap()) as ArrayRef)
12381        );
12382    }
12383
12384    #[test]
12385    fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale() {
12386        let array = vec![Some(123456789)];
12387        let array = create_decimal128_array(array, 24, 4).unwrap();
12388        let input_type = DataType::Decimal128(24, 4);
12389        let output_type = DataType::Decimal128(6, 2);
12390        assert!(can_cast_types(&input_type, &output_type));
12391
12392        let options = CastOptions {
12393            safe: false,
12394            ..Default::default()
12395        };
12396        let result = cast_with_options(&array, &output_type, &options);
12397        assert_eq!(
12398            result.unwrap_err().to_string(),
12399            "Invalid argument error: 12345.68 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
12400        );
12401    }
12402
12403    #[test]
12404    fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale() {
12405        let array = vec![Some(123456789)];
12406        let array = create_decimal128_array(array, 24, 2).unwrap();
12407        let input_type = DataType::Decimal128(24, 2);
12408        let output_type = DataType::Decimal128(6, 3);
12409        assert!(can_cast_types(&input_type, &output_type));
12410
12411        let options = CastOptions {
12412            safe: false,
12413            ..Default::default()
12414        };
12415        let result = cast_with_options(&array, &output_type, &options);
12416        assert_eq!(
12417            result.unwrap_err().to_string(),
12418            "Invalid argument error: 1234567.890 is too large to store in a Decimal128 of precision 6. Max is 999.999"
12419        );
12420    }
12421
12422    #[test]
12423    fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type() {
12424        let array = vec![Some(123456789)];
12425        let array = create_decimal128_array(array, 24, 2).unwrap();
12426        let input_type = DataType::Decimal128(24, 2);
12427        let output_type = DataType::Decimal256(6, 2);
12428        assert!(can_cast_types(&input_type, &output_type));
12429
12430        let options = CastOptions {
12431            safe: false,
12432            ..Default::default()
12433        };
12434        let result = cast_with_options(&array, &output_type, &options).unwrap_err();
12435        assert_eq!(
12436            result.to_string(),
12437            "Invalid argument error: 1234567.89 is too large to store in a Decimal256 of precision 6. Max is 9999.99"
12438        );
12439    }
12440
12441    #[test]
12442    fn test_first_none() {
12443        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
12444            None,
12445            Some(vec![Some(1), Some(2)]),
12446        ])) as ArrayRef;
12447        let data_type =
12448            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
12449        let opt = CastOptions::default();
12450        let r = cast_with_options(&array, &data_type, &opt).unwrap();
12451
12452        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
12453            vec![None, Some(vec![Some(1), Some(2)])],
12454            2,
12455        )) as ArrayRef;
12456        assert_eq!(*fixed_array, *r);
12457    }
12458
12459    #[test]
12460    fn test_first_last_none() {
12461        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
12462            None,
12463            Some(vec![Some(1), Some(2)]),
12464            None,
12465        ])) as ArrayRef;
12466        let data_type =
12467            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
12468        let opt = CastOptions::default();
12469        let r = cast_with_options(&array, &data_type, &opt).unwrap();
12470
12471        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
12472            vec![None, Some(vec![Some(1), Some(2)]), None],
12473            2,
12474        )) as ArrayRef;
12475        assert_eq!(*fixed_array, *r);
12476    }
12477
12478    #[test]
12479    fn test_cast_decimal_error_output() {
12480        let array = Int64Array::from(vec![1]);
12481        let error = cast_with_options(
12482            &array,
12483            &DataType::Decimal32(1, 1),
12484            &CastOptions {
12485                safe: false,
12486                format_options: FormatOptions::default(),
12487            },
12488        )
12489        .unwrap_err();
12490        assert_eq!(
12491            error.to_string(),
12492            "Invalid argument error: 1.0 is too large to store in a Decimal32 of precision 1. Max is 0.9"
12493        );
12494
12495        let array = Int64Array::from(vec![-1]);
12496        let error = cast_with_options(
12497            &array,
12498            &DataType::Decimal32(1, 1),
12499            &CastOptions {
12500                safe: false,
12501                format_options: FormatOptions::default(),
12502            },
12503        )
12504        .unwrap_err();
12505        assert_eq!(
12506            error.to_string(),
12507            "Invalid argument error: -1.0 is too small to store in a Decimal32 of precision 1. Min is -0.9"
12508        );
12509    }
12510
12511    #[test]
12512    fn test_run_end_encoded_to_primitive() {
12513        // Create a RunEndEncoded array: [1, 1, 2, 2, 2, 3]
12514        let run_ends = Int32Array::from(vec![2, 5, 6]);
12515        let values = Int32Array::from(vec![1, 2, 3]);
12516        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12517        let array_ref = Arc::new(run_array) as ArrayRef;
12518        // Cast to Int64
12519        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
12520        // Verify the result is a RunArray with Int64 values
12521        let result_run_array = cast_result.as_any().downcast_ref::<Int64Array>().unwrap();
12522        assert_eq!(
12523            result_run_array.values(),
12524            &[1i64, 1i64, 2i64, 2i64, 2i64, 3i64]
12525        );
12526    }
12527
12528    #[test]
12529    fn test_sliced_run_end_encoded_to_primitive() {
12530        let run_ends = Int32Array::from(vec![2, 5, 6]);
12531        let values = Int32Array::from(vec![1, 2, 3]);
12532        // [1, 1, 2, 2, 2, 3]
12533        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12534        let run_array = run_array.slice(3, 3); // [2, 2, 3]
12535        let array_ref = Arc::new(run_array) as ArrayRef;
12536
12537        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
12538        let result_run_array = cast_result.as_primitive::<Int64Type>();
12539        assert_eq!(result_run_array.values(), &[2, 2, 3]);
12540    }
12541
12542    #[test]
12543    fn test_run_end_encoded_to_string() {
12544        let run_ends = Int32Array::from(vec![2, 3, 5]);
12545        let values = Int32Array::from(vec![10, 20, 30]);
12546        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12547        let array_ref = Arc::new(run_array) as ArrayRef;
12548
12549        // Cast to String
12550        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
12551
12552        // Verify the result is a RunArray with String values
12553        let result_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
12554        // Check that values are correct
12555        assert_eq!(result_array.value(0), "10");
12556        assert_eq!(result_array.value(1), "10");
12557        assert_eq!(result_array.value(2), "20");
12558    }
12559
12560    #[test]
12561    fn test_primitive_to_run_end_encoded() {
12562        // Create an Int32 array with repeated values: [1, 1, 2, 2, 2, 3]
12563        let source_array = Int32Array::from(vec![1, 1, 2, 2, 2, 3]);
12564        let array_ref = Arc::new(source_array) as ArrayRef;
12565
12566        // Cast to RunEndEncoded<Int32, Int32>
12567        let target_type = DataType::RunEndEncoded(
12568            Arc::new(Field::new("run_ends", DataType::Int32, false)),
12569            Arc::new(Field::new("values", DataType::Int32, true)),
12570        );
12571        let cast_result = cast(&array_ref, &target_type).unwrap();
12572
12573        // Verify the result is a RunArray
12574        let result_run_array = cast_result
12575            .as_any()
12576            .downcast_ref::<RunArray<Int32Type>>()
12577            .unwrap();
12578
12579        // Check run structure: runs should end at positions [2, 5, 6]
12580        assert_eq!(result_run_array.run_ends().values(), &[2, 5, 6]);
12581
12582        // Check values: should be [1, 2, 3]
12583        let values_array = result_run_array.values().as_primitive::<Int32Type>();
12584        assert_eq!(values_array.values(), &[1, 2, 3]);
12585    }
12586
12587    #[test]
12588    fn test_primitive_to_run_end_encoded_with_nulls() {
12589        let source_array = Int32Array::from(vec![
12590            Some(1),
12591            Some(1),
12592            None,
12593            None,
12594            Some(2),
12595            Some(2),
12596            Some(3),
12597            Some(3),
12598            None,
12599            None,
12600            Some(4),
12601            Some(4),
12602            Some(5),
12603            Some(5),
12604            None,
12605            None,
12606        ]);
12607        let array_ref = Arc::new(source_array) as ArrayRef;
12608        let target_type = DataType::RunEndEncoded(
12609            Arc::new(Field::new("run_ends", DataType::Int32, false)),
12610            Arc::new(Field::new("values", DataType::Int32, true)),
12611        );
12612        let cast_result = cast(&array_ref, &target_type).unwrap();
12613        let result_run_array = cast_result
12614            .as_any()
12615            .downcast_ref::<RunArray<Int32Type>>()
12616            .unwrap();
12617        assert_eq!(
12618            result_run_array.run_ends().values(),
12619            &[2, 4, 6, 8, 10, 12, 14, 16]
12620        );
12621        assert_eq!(
12622            result_run_array
12623                .values()
12624                .as_primitive::<Int32Type>()
12625                .values(),
12626            &[1, 0, 2, 3, 0, 4, 5, 0]
12627        );
12628        assert_eq!(result_run_array.values().null_count(), 3);
12629    }
12630
12631    #[test]
12632    fn test_primitive_to_run_end_encoded_with_nulls_consecutive() {
12633        let source_array = Int64Array::from(vec![
12634            Some(1),
12635            Some(1),
12636            None,
12637            None,
12638            None,
12639            None,
12640            None,
12641            None,
12642            None,
12643            None,
12644            Some(4),
12645            Some(20),
12646            Some(500),
12647            Some(500),
12648            None,
12649            None,
12650        ]);
12651        let array_ref = Arc::new(source_array) as ArrayRef;
12652        let target_type = DataType::RunEndEncoded(
12653            Arc::new(Field::new("run_ends", DataType::Int16, false)),
12654            Arc::new(Field::new("values", DataType::Int64, true)),
12655        );
12656        let cast_result = cast(&array_ref, &target_type).unwrap();
12657        let result_run_array = cast_result
12658            .as_any()
12659            .downcast_ref::<RunArray<Int16Type>>()
12660            .unwrap();
12661        assert_eq!(
12662            result_run_array.run_ends().values(),
12663            &[2, 10, 11, 12, 14, 16]
12664        );
12665        assert_eq!(
12666            result_run_array
12667                .values()
12668                .as_primitive::<Int64Type>()
12669                .values(),
12670            &[1, 0, 4, 20, 500, 0]
12671        );
12672        assert_eq!(result_run_array.values().null_count(), 2);
12673    }
12674
12675    #[test]
12676    fn test_string_to_run_end_encoded() {
12677        // Create a String array with repeated values: ["a", "a", "b", "c", "c"]
12678        let source_array = StringArray::from(vec!["a", "a", "b", "c", "c"]);
12679        let array_ref = Arc::new(source_array) as ArrayRef;
12680
12681        // Cast to RunEndEncoded<Int32, String>
12682        let target_type = DataType::RunEndEncoded(
12683            Arc::new(Field::new("run_ends", DataType::Int32, false)),
12684            Arc::new(Field::new("values", DataType::Utf8, true)),
12685        );
12686        let cast_result = cast(&array_ref, &target_type).unwrap();
12687
12688        // Verify the result is a RunArray
12689        let result_run_array = cast_result
12690            .as_any()
12691            .downcast_ref::<RunArray<Int32Type>>()
12692            .unwrap();
12693
12694        // Check run structure: runs should end at positions [2, 3, 5]
12695        assert_eq!(result_run_array.run_ends().values(), &[2, 3, 5]);
12696
12697        // Check values: should be ["a", "b", "c"]
12698        let values_array = result_run_array.values().as_string::<i32>();
12699        assert_eq!(values_array.value(0), "a");
12700        assert_eq!(values_array.value(1), "b");
12701        assert_eq!(values_array.value(2), "c");
12702    }
12703
12704    #[test]
12705    fn test_empty_array_to_run_end_encoded() {
12706        // Create an empty Int32 array
12707        let source_array = Int32Array::from(Vec::<i32>::new());
12708        let array_ref = Arc::new(source_array) as ArrayRef;
12709
12710        // Cast to RunEndEncoded<Int32, Int32>
12711        let target_type = DataType::RunEndEncoded(
12712            Arc::new(Field::new("run_ends", DataType::Int32, false)),
12713            Arc::new(Field::new("values", DataType::Int32, true)),
12714        );
12715        let cast_result = cast(&array_ref, &target_type).unwrap();
12716
12717        // Verify the result is an empty RunArray
12718        let result_run_array = cast_result
12719            .as_any()
12720            .downcast_ref::<RunArray<Int32Type>>()
12721            .unwrap();
12722
12723        // Check that both run_ends and values are empty
12724        assert_eq!(result_run_array.run_ends().len(), 0);
12725        assert_eq!(result_run_array.values().len(), 0);
12726    }
12727
12728    #[test]
12729    fn test_run_end_encoded_with_nulls() {
12730        // Create a RunEndEncoded array with nulls: [1, 1, null, 2, 2]
12731        let run_ends = Int32Array::from(vec![2, 3, 5]);
12732        let values = Int32Array::from(vec![Some(1), None, Some(2)]);
12733        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12734        let array_ref = Arc::new(run_array) as ArrayRef;
12735
12736        // Cast to String
12737        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
12738
12739        // Verify the result preserves nulls
12740        let result_run_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
12741        assert_eq!(result_run_array.value(0), "1");
12742        assert!(result_run_array.is_null(2));
12743        assert_eq!(result_run_array.value(4), "2");
12744    }
12745
12746    #[test]
12747    fn test_different_index_types() {
12748        // Test with Int16 index type
12749        let source_array = Int32Array::from(vec![1, 1, 2, 3, 3]);
12750        let array_ref = Arc::new(source_array) as ArrayRef;
12751
12752        let target_type = DataType::RunEndEncoded(
12753            Arc::new(Field::new("run_ends", DataType::Int16, false)),
12754            Arc::new(Field::new("values", DataType::Int32, true)),
12755        );
12756        let cast_result = cast(&array_ref, &target_type).unwrap();
12757        assert_eq!(cast_result.data_type(), &target_type);
12758
12759        // Verify the cast worked correctly: values are [1, 2, 3]
12760        // and run-ends are [2, 3, 5]
12761        let run_array = cast_result
12762            .as_any()
12763            .downcast_ref::<RunArray<Int16Type>>()
12764            .unwrap();
12765        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
12766        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
12767        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
12768        assert_eq!(run_array.run_ends().values(), &[2i16, 3i16, 5i16]);
12769
12770        // Test again with Int64 index type
12771        let target_type = DataType::RunEndEncoded(
12772            Arc::new(Field::new("run_ends", DataType::Int64, false)),
12773            Arc::new(Field::new("values", DataType::Int32, true)),
12774        );
12775        let cast_result = cast(&array_ref, &target_type).unwrap();
12776        assert_eq!(cast_result.data_type(), &target_type);
12777
12778        // Verify the cast worked correctly: values are [1, 2, 3]
12779        // and run-ends are [2, 3, 5]
12780        let run_array = cast_result
12781            .as_any()
12782            .downcast_ref::<RunArray<Int64Type>>()
12783            .unwrap();
12784        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
12785        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
12786        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
12787        assert_eq!(run_array.run_ends().values(), &[2i64, 3i64, 5i64]);
12788    }
12789
12790    #[test]
12791    fn test_unsupported_cast_to_run_end_encoded() {
12792        // Create a Struct array - complex nested type that might not be supported
12793        let field = Field::new("item", DataType::Int32, false);
12794        let struct_array = StructArray::from(vec![(
12795            Arc::new(field),
12796            Arc::new(Int32Array::from(vec![1, 2, 3])) as ArrayRef,
12797        )]);
12798        let array_ref = Arc::new(struct_array) as ArrayRef;
12799
12800        // This should fail because:
12801        // 1. The target type is not RunEndEncoded
12802        // 2. The target type is not supported for casting from StructArray
12803        let cast_result = cast(&array_ref, &DataType::FixedSizeBinary(10));
12804
12805        // Expect this to fail
12806        assert!(cast_result.is_err());
12807    }
12808
12809    /// Test casting RunEndEncoded<Int64, String> to RunEndEncoded<Int16, String> should fail
12810    #[test]
12811    fn test_cast_run_end_encoded_int64_to_int16_should_fail() {
12812        // Construct a valid REE array with Int64 run-ends
12813        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
12814        let values = StringArray::from(vec!["a", "b", "c"]);
12815
12816        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
12817        let array_ref = Arc::new(ree_array) as ArrayRef;
12818
12819        // Attempt to cast to RunEndEncoded<Int16, Utf8>
12820        let target_type = DataType::RunEndEncoded(
12821            Arc::new(Field::new("run_ends", DataType::Int16, false)),
12822            Arc::new(Field::new("values", DataType::Utf8, true)),
12823        );
12824        let cast_options = CastOptions {
12825            safe: false, // This should make it fail instead of returning nulls
12826            format_options: FormatOptions::default(),
12827        };
12828
12829        // This should fail due to run-end overflow
12830        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12831            cast_with_options(&array_ref, &target_type, &cast_options);
12832
12833        let e = result.expect_err("Cast should have failed but succeeded");
12834        assert!(
12835            e.to_string()
12836                .contains("Cast error: Can't cast value 100000 to type Int16")
12837        );
12838    }
12839
12840    #[test]
12841    fn test_cast_run_end_encoded_int64_to_int16_with_safe_should_fail_with_null_invalid_error() {
12842        // Construct a valid REE array with Int64 run-ends
12843        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
12844        let values = StringArray::from(vec!["a", "b", "c"]);
12845
12846        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
12847        let array_ref = Arc::new(ree_array) as ArrayRef;
12848
12849        // Attempt to cast to RunEndEncoded<Int16, Utf8>
12850        let target_type = DataType::RunEndEncoded(
12851            Arc::new(Field::new("run_ends", DataType::Int16, false)),
12852            Arc::new(Field::new("values", DataType::Utf8, true)),
12853        );
12854        let cast_options = CastOptions {
12855            safe: true,
12856            format_options: FormatOptions::default(),
12857        };
12858
12859        // This fails even though safe is true because the run_ends array has null values
12860        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12861            cast_with_options(&array_ref, &target_type, &cast_options);
12862        let e = result.expect_err("Cast should have failed but succeeded");
12863        assert!(
12864            e.to_string()
12865                .contains("Invalid argument error: Found null values in run_ends array. The run_ends array should not have null values.")
12866        );
12867    }
12868
12869    /// Test casting RunEndEncoded<Int16, String> to RunEndEncoded<Int64, String> should succeed
12870    #[test]
12871    fn test_cast_run_end_encoded_int16_to_int64_should_succeed() {
12872        // Construct a valid REE array with Int16 run-ends
12873        let run_ends = Int16Array::from(vec![2, 5, 8]); // values that fit in Int16
12874        let values = StringArray::from(vec!["a", "b", "c"]);
12875
12876        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
12877        let array_ref = Arc::new(ree_array) as ArrayRef;
12878
12879        // Attempt to cast to RunEndEncoded<Int64, Utf8> (upcast should succeed)
12880        let target_type = DataType::RunEndEncoded(
12881            Arc::new(Field::new("run_ends", DataType::Int64, false)),
12882            Arc::new(Field::new("values", DataType::Utf8, true)),
12883        );
12884        let cast_options = CastOptions {
12885            safe: false,
12886            format_options: FormatOptions::default(),
12887        };
12888
12889        // This should succeed due to valid upcast
12890        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12891            cast_with_options(&array_ref, &target_type, &cast_options);
12892
12893        let array_ref = result.expect("Cast should have succeeded but failed");
12894        // Downcast to RunArray<Int64Type>
12895        let run_array = array_ref
12896            .as_any()
12897            .downcast_ref::<RunArray<Int64Type>>()
12898            .unwrap();
12899
12900        // Verify the cast worked correctly
12901        // Assert the values were cast correctly
12902        assert_eq!(run_array.run_ends().values(), &[2i64, 5i64, 8i64]);
12903        assert_eq!(run_array.values().as_string::<i32>().value(0), "a");
12904        assert_eq!(run_array.values().as_string::<i32>().value(1), "b");
12905        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
12906    }
12907
12908    #[test]
12909    fn test_cast_run_end_encoded_dictionary_to_run_end_encoded() {
12910        // Construct a valid dictionary encoded array
12911        let values = StringArray::from_iter([Some("a"), Some("b"), Some("c")]);
12912        let keys = UInt64Array::from_iter(vec![1, 1, 1, 0, 0, 0, 2, 2, 2]);
12913        let array_ref = Arc::new(DictionaryArray::new(keys, Arc::new(values))) as ArrayRef;
12914
12915        // Attempt to cast to RunEndEncoded<Int64, Utf8>
12916        let target_type = DataType::RunEndEncoded(
12917            Arc::new(Field::new("run_ends", DataType::Int64, false)),
12918            Arc::new(Field::new("values", DataType::Utf8, true)),
12919        );
12920        let cast_options = CastOptions {
12921            safe: false,
12922            format_options: FormatOptions::default(),
12923        };
12924
12925        // This should succeed
12926        let result = cast_with_options(&array_ref, &target_type, &cast_options)
12927            .expect("Cast should have succeeded but failed");
12928
12929        // Verify the cast worked correctly
12930        // Assert the values were cast correctly
12931        let run_array = result
12932            .as_any()
12933            .downcast_ref::<RunArray<Int64Type>>()
12934            .unwrap();
12935        assert_eq!(run_array.values().as_string::<i32>().value(0), "b");
12936        assert_eq!(run_array.values().as_string::<i32>().value(1), "a");
12937        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
12938
12939        // Verify the run-ends were cast correctly (run ends at 3, 6, 9)
12940        assert_eq!(run_array.run_ends().values(), &[3i64, 6i64, 9i64]);
12941    }
12942
12943    fn int32_list_values() -> Vec<Option<Vec<Option<i32>>>> {
12944        vec![
12945            Some(vec![Some(1), Some(2), Some(3)]),
12946            Some(vec![Some(4), Some(5), Some(6)]),
12947            None,
12948            Some(vec![Some(7), Some(8), Some(9)]),
12949            Some(vec![None, Some(10)]),
12950        ]
12951    }
12952
12953    #[test]
12954    fn test_cast_list_view_to_list() {
12955        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12956        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
12957        assert!(can_cast_types(list_view.data_type(), &target_type));
12958        let cast_result = cast(&list_view, &target_type).unwrap();
12959        let got_list = cast_result.as_list::<i32>();
12960        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12961        assert_eq!(got_list, &expected_list);
12962    }
12963
12964    #[test]
12965    fn test_cast_list_view_to_large_list() {
12966        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12967        let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
12968        assert!(can_cast_types(list_view.data_type(), &target_type));
12969        let cast_result = cast(&list_view, &target_type).unwrap();
12970        let got_list = cast_result.as_list::<i64>();
12971        let expected_list =
12972            LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12973        assert_eq!(got_list, &expected_list);
12974    }
12975
12976    #[test]
12977    fn test_cast_list_to_list_view() {
12978        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12979        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
12980        assert!(can_cast_types(list.data_type(), &target_type));
12981        let cast_result = cast(&list, &target_type).unwrap();
12982
12983        let got_list_view = cast_result.as_list_view::<i32>();
12984        let expected_list_view =
12985            ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12986        assert_eq!(got_list_view, &expected_list_view);
12987
12988        // inner types get cast
12989        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
12990            Some(vec![Some(1), Some(2)]),
12991            None,
12992            Some(vec![None, Some(3)]),
12993        ]);
12994        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Float32, true)));
12995        assert!(can_cast_types(list.data_type(), &target_type));
12996        let cast_result = cast(&list, &target_type).unwrap();
12997
12998        let got_list_view = cast_result.as_list_view::<i32>();
12999        let expected_list_view = ListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13000            Some(vec![Some(1.0), Some(2.0)]),
13001            None,
13002            Some(vec![None, Some(3.0)]),
13003        ]);
13004        assert_eq!(got_list_view, &expected_list_view);
13005    }
13006
13007    #[test]
13008    fn test_cast_list_to_large_list_view() {
13009        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13010            Some(vec![Some(1), Some(2)]),
13011            None,
13012            Some(vec![None, Some(3)]),
13013        ]);
13014        let target_type =
13015            DataType::LargeListView(Arc::new(Field::new("item", DataType::Float32, true)));
13016        assert!(can_cast_types(list.data_type(), &target_type));
13017        let cast_result = cast(&list, &target_type).unwrap();
13018
13019        let got_list_view = cast_result.as_list_view::<i64>();
13020        let expected_list_view =
13021            LargeListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13022                Some(vec![Some(1.0), Some(2.0)]),
13023                None,
13024                Some(vec![None, Some(3.0)]),
13025            ]);
13026        assert_eq!(got_list_view, &expected_list_view);
13027    }
13028
13029    #[test]
13030    fn test_cast_large_list_view_to_large_list() {
13031        let list_view =
13032            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13033        let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
13034        assert!(can_cast_types(list_view.data_type(), &target_type));
13035        let cast_result = cast(&list_view, &target_type).unwrap();
13036        let got_list = cast_result.as_list::<i64>();
13037
13038        let expected_list =
13039            LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13040        assert_eq!(got_list, &expected_list);
13041    }
13042
13043    #[test]
13044    fn test_cast_large_list_view_to_list() {
13045        let list_view =
13046            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13047        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13048        assert!(can_cast_types(list_view.data_type(), &target_type));
13049        let cast_result = cast(&list_view, &target_type).unwrap();
13050        let got_list = cast_result.as_list::<i32>();
13051
13052        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13053        assert_eq!(got_list, &expected_list);
13054    }
13055
13056    #[test]
13057    fn test_cast_large_list_to_large_list_view() {
13058        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13059        let target_type =
13060            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
13061        assert!(can_cast_types(list.data_type(), &target_type));
13062        let cast_result = cast(&list, &target_type).unwrap();
13063
13064        let got_list_view = cast_result.as_list_view::<i64>();
13065        let expected_list_view =
13066            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13067        assert_eq!(got_list_view, &expected_list_view);
13068
13069        // inner types get cast
13070        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13071            Some(vec![Some(1), Some(2)]),
13072            None,
13073            Some(vec![None, Some(3)]),
13074        ]);
13075        let target_type =
13076            DataType::LargeListView(Arc::new(Field::new("item", DataType::Float32, true)));
13077        assert!(can_cast_types(list.data_type(), &target_type));
13078        let cast_result = cast(&list, &target_type).unwrap();
13079
13080        let got_list_view = cast_result.as_list_view::<i64>();
13081        let expected_list_view =
13082            LargeListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13083                Some(vec![Some(1.0), Some(2.0)]),
13084                None,
13085                Some(vec![None, Some(3.0)]),
13086            ]);
13087        assert_eq!(got_list_view, &expected_list_view);
13088    }
13089
13090    #[test]
13091    fn test_cast_large_list_to_list_view() {
13092        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13093            Some(vec![Some(1), Some(2)]),
13094            None,
13095            Some(vec![None, Some(3)]),
13096        ]);
13097        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Float32, true)));
13098        assert!(can_cast_types(list.data_type(), &target_type));
13099        let cast_result = cast(&list, &target_type).unwrap();
13100
13101        let got_list_view = cast_result.as_list_view::<i32>();
13102        let expected_list_view = ListViewArray::from_iter_primitive::<Float32Type, _, _>(vec![
13103            Some(vec![Some(1.0), Some(2.0)]),
13104            None,
13105            Some(vec![None, Some(3.0)]),
13106        ]);
13107        assert_eq!(got_list_view, &expected_list_view);
13108    }
13109
13110    #[test]
13111    fn test_cast_list_view_to_list_out_of_order() {
13112        let list_view = ListViewArray::new(
13113            Arc::new(Field::new("item", DataType::Int32, true)),
13114            ScalarBuffer::from(vec![0, 6, 3]),
13115            ScalarBuffer::from(vec![3, 3, 3]),
13116            Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
13117            None,
13118        );
13119        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13120        assert!(can_cast_types(list_view.data_type(), &target_type));
13121        let cast_result = cast(&list_view, &target_type).unwrap();
13122        let got_list = cast_result.as_list::<i32>();
13123        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13124            Some(vec![Some(1), Some(2), Some(3)]),
13125            Some(vec![Some(7), Some(8), Some(9)]),
13126            Some(vec![Some(4), Some(5), Some(6)]),
13127        ]);
13128        assert_eq!(got_list, &expected_list);
13129    }
13130
13131    #[test]
13132    fn test_cast_list_view_to_list_overlapping() {
13133        let list_view = ListViewArray::new(
13134            Arc::new(Field::new("item", DataType::Int32, true)),
13135            ScalarBuffer::from(vec![0, 0]),
13136            ScalarBuffer::from(vec![1, 2]),
13137            Arc::new(Int32Array::from(vec![1, 2])),
13138            None,
13139        );
13140        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13141        assert!(can_cast_types(list_view.data_type(), &target_type));
13142        let cast_result = cast(&list_view, &target_type).unwrap();
13143        let got_list = cast_result.as_list::<i32>();
13144        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
13145            Some(vec![Some(1)]),
13146            Some(vec![Some(1), Some(2)]),
13147        ]);
13148        assert_eq!(got_list, &expected_list);
13149    }
13150
13151    #[test]
13152    fn test_cast_list_view_to_list_empty() {
13153        let values: Vec<Option<Vec<Option<i32>>>> = vec![];
13154        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
13155        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13156        assert!(can_cast_types(list_view.data_type(), &target_type));
13157        let cast_result = cast(&list_view, &target_type).unwrap();
13158        let got_list = cast_result.as_list::<i32>();
13159        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
13160        assert_eq!(got_list, &expected_list);
13161    }
13162
13163    #[test]
13164    fn test_cast_list_view_to_list_different_inner_type() {
13165        let values = int32_list_values();
13166        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
13167        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int64, true)));
13168        assert!(can_cast_types(list_view.data_type(), &target_type));
13169        let cast_result = cast(&list_view, &target_type).unwrap();
13170        let got_list = cast_result.as_list::<i32>();
13171
13172        let expected_list =
13173            ListArray::from_iter_primitive::<Int64Type, _, _>(values.into_iter().map(|list| {
13174                list.map(|list| {
13175                    list.into_iter()
13176                        .map(|v| v.map(|v| v as i64))
13177                        .collect::<Vec<_>>()
13178                })
13179            }));
13180        assert_eq!(got_list, &expected_list);
13181    }
13182
13183    #[test]
13184    fn test_cast_list_view_to_list_out_of_order_with_nulls() {
13185        let list_view = ListViewArray::new(
13186            Arc::new(Field::new("item", DataType::Int32, true)),
13187            ScalarBuffer::from(vec![0, 6, 3]),
13188            ScalarBuffer::from(vec![3, 3, 3]),
13189            Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
13190            Some(NullBuffer::from(vec![false, true, false])),
13191        );
13192        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
13193        assert!(can_cast_types(list_view.data_type(), &target_type));
13194        let cast_result = cast(&list_view, &target_type).unwrap();
13195        let got_list = cast_result.as_list::<i32>();
13196        let expected_list = ListArray::new(
13197            Arc::new(Field::new("item", DataType::Int32, true)),
13198            OffsetBuffer::from_lengths([3, 3, 3]),
13199            Arc::new(Int32Array::from(vec![1, 2, 3, 7, 8, 9, 4, 5, 6])),
13200            Some(NullBuffer::from(vec![false, true, false])),
13201        );
13202        assert_eq!(got_list, &expected_list);
13203    }
13204
13205    #[test]
13206    fn test_cast_list_view_to_large_list_view() {
13207        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13208        let target_type =
13209            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
13210        assert!(can_cast_types(list_view.data_type(), &target_type));
13211        let cast_result = cast(&list_view, &target_type).unwrap();
13212        let got = cast_result.as_list_view::<i64>();
13213
13214        let expected =
13215            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13216        assert_eq!(got, &expected);
13217    }
13218
13219    #[test]
13220    fn test_cast_large_list_view_to_list_view() {
13221        let list_view =
13222            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13223        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
13224        assert!(can_cast_types(list_view.data_type(), &target_type));
13225        let cast_result = cast(&list_view, &target_type).unwrap();
13226        let got = cast_result.as_list_view::<i32>();
13227
13228        let expected = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
13229        assert_eq!(got, &expected);
13230    }
13231
13232    #[test]
13233    fn test_cast_time32_second_to_int64() {
13234        let array = Time32SecondArray::from(vec![1000, 2000, 3000]);
13235        let array = Arc::new(array) as Arc<dyn Array>;
13236        let to_type = DataType::Int64;
13237        let cast_options = CastOptions::default();
13238
13239        assert!(can_cast_types(array.data_type(), &to_type));
13240
13241        let result = cast_with_options(&array, &to_type, &cast_options);
13242        assert!(
13243            result.is_ok(),
13244            "Failed to cast Time32(Second) to Int64: {:?}",
13245            result.err()
13246        );
13247
13248        let cast_array = result.unwrap();
13249        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13250
13251        assert_eq!(cast_array.value(0), 1000);
13252        assert_eq!(cast_array.value(1), 2000);
13253        assert_eq!(cast_array.value(2), 3000);
13254    }
13255
13256    #[test]
13257    fn test_cast_time32_millisecond_to_int64() {
13258        let array = Time32MillisecondArray::from(vec![1000, 2000, 3000]);
13259        let array = Arc::new(array) as Arc<dyn Array>;
13260        let to_type = DataType::Int64;
13261        let cast_options = CastOptions::default();
13262
13263        assert!(can_cast_types(array.data_type(), &to_type));
13264
13265        let result = cast_with_options(&array, &to_type, &cast_options);
13266        assert!(
13267            result.is_ok(),
13268            "Failed to cast Time32(Millisecond) to Int64: {:?}",
13269            result.err()
13270        );
13271
13272        let cast_array = result.unwrap();
13273        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13274
13275        assert_eq!(cast_array.value(0), 1000);
13276        assert_eq!(cast_array.value(1), 2000);
13277        assert_eq!(cast_array.value(2), 3000);
13278    }
13279
13280    #[test]
13281    fn test_cast_string_to_time32_second_to_int64() {
13282        // Mimic: select arrow_cast('03:12:44'::time, 'Time32(Second)')::bigint;
13283        // raised in https://github.com/apache/datafusion/issues/19036
13284        let array = StringArray::from(vec!["03:12:44"]);
13285        let array = Arc::new(array) as Arc<dyn Array>;
13286        let cast_options = CastOptions::default();
13287
13288        // 1. Cast String to Time32(Second)
13289        let time32_type = DataType::Time32(TimeUnit::Second);
13290        let time32_array = cast_with_options(&array, &time32_type, &cast_options).unwrap();
13291
13292        // 2. Cast Time32(Second) to Int64
13293        let int64_type = DataType::Int64;
13294        assert!(can_cast_types(time32_array.data_type(), &int64_type));
13295
13296        let result = cast_with_options(&time32_array, &int64_type, &cast_options);
13297
13298        assert!(
13299            result.is_ok(),
13300            "Failed to cast Time32(Second) to Int64: {:?}",
13301            result.err()
13302        );
13303
13304        let cast_array = result.unwrap();
13305        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
13306
13307        // 03:12:44 = 3*3600 + 12*60 + 44 = 10800 + 720 + 44 = 11564
13308        assert_eq!(cast_array.value(0), 11564);
13309    }
13310    #[test]
13311    fn test_string_dicts_to_binary_view() {
13312        let expected = BinaryViewArray::from_iter(vec![
13313            VIEW_TEST_DATA[1],
13314            VIEW_TEST_DATA[0],
13315            None,
13316            VIEW_TEST_DATA[3],
13317            None,
13318            VIEW_TEST_DATA[1],
13319            VIEW_TEST_DATA[4],
13320        ]);
13321
13322        let values_arrays: [ArrayRef; _] = [
13323            Arc::new(StringArray::from_iter(VIEW_TEST_DATA)),
13324            Arc::new(StringViewArray::from_iter(VIEW_TEST_DATA)),
13325            Arc::new(LargeStringArray::from_iter(VIEW_TEST_DATA)),
13326        ];
13327        for values in values_arrays {
13328            let keys =
13329                Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
13330            let string_dict_array = DictionaryArray::<Int8Type>::try_new(keys, values).unwrap();
13331
13332            let casted = cast(&string_dict_array, &DataType::BinaryView).unwrap();
13333            assert_eq!(casted.as_ref(), &expected);
13334        }
13335    }
13336
13337    #[test]
13338    fn test_binary_dicts_to_string_view() {
13339        let expected = StringViewArray::from_iter(vec![
13340            VIEW_TEST_DATA[1],
13341            VIEW_TEST_DATA[0],
13342            None,
13343            VIEW_TEST_DATA[3],
13344            None,
13345            VIEW_TEST_DATA[1],
13346            VIEW_TEST_DATA[4],
13347        ]);
13348
13349        let values_arrays: [ArrayRef; _] = [
13350            Arc::new(BinaryArray::from_iter(VIEW_TEST_DATA)),
13351            Arc::new(BinaryViewArray::from_iter(VIEW_TEST_DATA)),
13352            Arc::new(LargeBinaryArray::from_iter(VIEW_TEST_DATA)),
13353        ];
13354        for values in values_arrays {
13355            let keys =
13356                Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
13357            let string_dict_array = DictionaryArray::<Int8Type>::try_new(keys, values).unwrap();
13358
13359            let casted = cast(&string_dict_array, &DataType::Utf8View).unwrap();
13360            assert_eq!(casted.as_ref(), &expected);
13361        }
13362    }
13363
13364    #[test]
13365    fn test_cast_between_sliced_run_end_encoded() {
13366        let run_ends = Int16Array::from(vec![2, 5, 8]);
13367        let values = StringArray::from(vec!["a", "b", "c"]);
13368
13369        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
13370        let ree_array = ree_array.slice(1, 2);
13371        let array_ref = Arc::new(ree_array) as ArrayRef;
13372
13373        let target_type = DataType::RunEndEncoded(
13374            Arc::new(Field::new("run_ends", DataType::Int64, false)),
13375            Arc::new(Field::new("values", DataType::Utf8, true)),
13376        );
13377        let cast_options = CastOptions {
13378            safe: false,
13379            format_options: FormatOptions::default(),
13380        };
13381
13382        let result = cast_with_options(&array_ref, &target_type, &cast_options).unwrap();
13383        let run_array = result.as_run::<Int64Type>();
13384        let run_array = run_array.downcast::<StringArray>().unwrap();
13385
13386        let expected = vec!["a", "b"];
13387        let actual = run_array.into_iter().flatten().collect::<Vec<_>>();
13388
13389        assert_eq!(expected, actual);
13390    }
13391}