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 list_view;
44mod map;
45mod run_array;
46mod string;
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::*;
54
55use arrow_buffer::IntervalMonthDayNano;
56use arrow_data::ByteView;
57use chrono::{NaiveTime, Offset, TimeZone, Utc};
58use std::cmp::Ordering;
59use std::sync::Arc;
60
61use crate::display::{ArrayFormatter, FormatOptions};
62use crate::parse::{
63    Parser, parse_interval_day_time, parse_interval_month_day_nano, parse_interval_year_month,
64    string_to_datetime,
65};
66use arrow_array::{builder::*, cast::*, temporal_conversions::*, timezone::Tz, types::*, *};
67use arrow_buffer::{ArrowNativeType, OffsetBuffer, i256};
68use arrow_data::ArrayData;
69use arrow_data::transform::MutableArrayData;
70use arrow_schema::*;
71use arrow_select::take::take;
72use num_traits::{NumCast, ToPrimitive, cast::AsPrimitive};
73
74use crate::cast::list_view::{cast_list_to_list_view, cast_list_view, cast_list_view_to_list};
75pub use decimal::{DecimalCast, rescale_decimal};
76
77/// CastOptions provides a way to override the default cast behaviors
78#[derive(Debug, Clone, PartialEq, Eq, Hash)]
79pub struct CastOptions<'a> {
80    /// how to handle cast failures, either return NULL (safe=true) or return ERR (safe=false)
81    pub safe: bool,
82    /// Formatting options when casting from temporal types to string
83    pub format_options: FormatOptions<'a>,
84}
85
86impl Default for CastOptions<'_> {
87    fn default() -> Self {
88        Self {
89            safe: true,
90            format_options: FormatOptions::default(),
91        }
92    }
93}
94
95/// Return true if a value of type `from_type` can be cast into a value of `to_type`.
96///
97/// See [`cast_with_options`] for more information
98pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
99    use self::DataType::*;
100    use self::IntervalUnit::*;
101    use self::TimeUnit::*;
102    if from_type == to_type {
103        return true;
104    }
105
106    match (from_type, to_type) {
107        (Null, _) => true,
108        // Dictionary/List conditions should be put in front of others
109        (Dictionary(_, from_value_type), Dictionary(_, to_value_type)) => {
110            can_cast_types(from_value_type, to_value_type)
111        }
112        (Dictionary(_, value_type), _) => can_cast_types(value_type, to_type),
113        (RunEndEncoded(_, value_type), _) => can_cast_types(value_type.data_type(), to_type),
114        (_, RunEndEncoded(_, value_type)) => can_cast_types(from_type, value_type.data_type()),
115        (_, Dictionary(_, value_type)) => can_cast_types(from_type, value_type),
116        (List(list_from) | LargeList(list_from), List(list_to) | LargeList(list_to)) => {
117            can_cast_types(list_from.data_type(), list_to.data_type())
118        }
119        (List(list_from) | LargeList(list_from), Utf8 | LargeUtf8) => {
120            can_cast_types(list_from.data_type(), to_type)
121        }
122        (List(list_from) | LargeList(list_from), FixedSizeList(list_to, _)) => {
123            can_cast_types(list_from.data_type(), list_to.data_type())
124        }
125        (List(list_from) | LargeList(list_from), ListView(list_to) | LargeListView(list_to)) => {
126            can_cast_types(list_from.data_type(), list_to.data_type())
127        }
128        (List(_), _) => false,
129        (ListView(list_from) | LargeListView(list_from), List(list_to) | LargeList(list_to)) => {
130            can_cast_types(list_from.data_type(), list_to.data_type())
131        }
132        (ListView(list_from), LargeListView(list_to)) => {
133            can_cast_types(list_from.data_type(), list_to.data_type())
134        }
135        (LargeListView(list_from), ListView(list_to)) => {
136            can_cast_types(list_from.data_type(), list_to.data_type())
137        }
138        (FixedSizeList(list_from, _), List(list_to))
139        | (FixedSizeList(list_from, _), LargeList(list_to)) => {
140            can_cast_types(list_from.data_type(), list_to.data_type())
141        }
142        (FixedSizeList(inner, size), FixedSizeList(inner_to, size_to)) if size == size_to => {
143            can_cast_types(inner.data_type(), inner_to.data_type())
144        }
145        (_, List(list_to)) => can_cast_types(from_type, list_to.data_type()),
146        (_, LargeList(list_to)) => can_cast_types(from_type, list_to.data_type()),
147        (_, FixedSizeList(list_to, size)) if *size == 1 => {
148            can_cast_types(from_type, list_to.data_type())
149        }
150        (FixedSizeList(list_from, size), _) if *size == 1 => {
151            can_cast_types(list_from.data_type(), to_type)
152        }
153        (Map(from_entries, ordered_from), Map(to_entries, ordered_to))
154            if ordered_from == ordered_to =>
155        {
156            match (
157                key_field(from_entries),
158                key_field(to_entries),
159                value_field(from_entries),
160                value_field(to_entries),
161            ) {
162                (Some(from_key), Some(to_key), Some(from_value), Some(to_value)) => {
163                    can_cast_types(from_key.data_type(), to_key.data_type())
164                        && can_cast_types(from_value.data_type(), to_value.data_type())
165                }
166                _ => false,
167            }
168        }
169        // cast one decimal type to another decimal type
170        (
171            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
172            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
173        ) => true,
174        // unsigned integer to decimal
175        (
176            UInt8 | UInt16 | UInt32 | UInt64,
177            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
178        ) => true,
179        // signed numeric to decimal
180        (
181            Int8 | Int16 | Int32 | Int64 | Float32 | Float64,
182            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
183        ) => true,
184        // decimal to unsigned numeric
185        (
186            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
187            UInt8 | UInt16 | UInt32 | UInt64,
188        ) => true,
189        // decimal to signed numeric
190        (
191            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
192            Null | Int8 | Int16 | Int32 | Int64 | Float32 | Float64,
193        ) => true,
194        // decimal to string
195        (
196            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
197            Utf8View | Utf8 | LargeUtf8,
198        ) => true,
199        // string to decimal
200        (
201            Utf8View | Utf8 | LargeUtf8,
202            Decimal32(_, _) | Decimal64(_, _) | Decimal128(_, _) | Decimal256(_, _),
203        ) => true,
204        (Struct(from_fields), Struct(to_fields)) => {
205            if from_fields.len() != to_fields.len() {
206                return false;
207            }
208
209            // fast path, all field names are in the same order and same number of fields
210            if from_fields
211                .iter()
212                .zip(to_fields.iter())
213                .all(|(f1, f2)| f1.name() == f2.name())
214            {
215                return from_fields.iter().zip(to_fields.iter()).all(|(f1, f2)| {
216                    // Assume that nullability between two structs are compatible, if not,
217                    // cast kernel will return error.
218                    can_cast_types(f1.data_type(), f2.data_type())
219                });
220            }
221
222            // slow path, we match the fields by name
223            if to_fields.iter().all(|to_field| {
224                from_fields
225                    .iter()
226                    .find(|from_field| from_field.name() == to_field.name())
227                    .is_some_and(|from_field| {
228                        // Assume that nullability between two structs are compatible, if not,
229                        // cast kernel will return error.
230                        can_cast_types(from_field.data_type(), to_field.data_type())
231                    })
232            }) {
233                return true;
234            }
235
236            // if we couldn't match by name, we try to see if they can be matched by position
237            from_fields
238                .iter()
239                .zip(to_fields.iter())
240                .all(|(f1, f2)| can_cast_types(f1.data_type(), f2.data_type()))
241        }
242        (Struct(_), _) => false,
243        (_, Struct(_)) => false,
244        (_, Boolean) => {
245            DataType::is_integer(from_type)
246                || DataType::is_floating(from_type)
247                || from_type == &Utf8View
248                || from_type == &Utf8
249                || from_type == &LargeUtf8
250        }
251        (Boolean, _) => {
252            DataType::is_integer(to_type)
253                || DataType::is_floating(to_type)
254                || to_type == &Utf8View
255                || to_type == &Utf8
256                || to_type == &LargeUtf8
257        }
258
259        (Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
260            true
261        }
262        (LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
263            true
264        }
265        (FixedSizeBinary(_), Binary | LargeBinary | BinaryView) => true,
266        (
267            Utf8 | LargeUtf8 | Utf8View,
268            Binary
269            | LargeBinary
270            | Utf8
271            | LargeUtf8
272            | Date32
273            | Date64
274            | Time32(Second)
275            | Time32(Millisecond)
276            | Time64(Microsecond)
277            | Time64(Nanosecond)
278            | Timestamp(Second, _)
279            | Timestamp(Millisecond, _)
280            | Timestamp(Microsecond, _)
281            | Timestamp(Nanosecond, _)
282            | Interval(_)
283            | BinaryView,
284        ) => true,
285        (Utf8 | LargeUtf8, Utf8View) => true,
286        (BinaryView, Binary | LargeBinary | Utf8 | LargeUtf8 | Utf8View) => true,
287        (Utf8View | Utf8 | LargeUtf8, _) => to_type.is_numeric() && to_type != &Float16,
288        (_, Utf8 | Utf8View | LargeUtf8) => from_type.is_primitive(),
289
290        (_, Binary | LargeBinary) => from_type.is_integer(),
291
292        // start numeric casts
293        (
294            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
295            | Float64,
296            UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float16 | Float32
297            | Float64,
298        ) => true,
299        // end numeric casts
300
301        // temporal casts
302        (Int32, Date32 | Date64 | Time32(_)) => true,
303        (Date32, Int32 | Int64) => true,
304        (Time32(_), Int32 | Int64) => true,
305        (Int64, Date64 | Date32 | Time64(_)) => true,
306        (Date64, Int64 | Int32) => true,
307        (Time64(_), Int64) => true,
308        (Date32 | Date64, Date32 | Date64) => true,
309        // time casts
310        (Time32(_), Time32(_)) => true,
311        (Time32(_), Time64(_)) => true,
312        (Time64(_), Time64(_)) => true,
313        (Time64(_), Time32(to_unit)) => {
314            matches!(to_unit, Second | Millisecond)
315        }
316        (Timestamp(_, _), _) if to_type.is_numeric() => true,
317        (_, Timestamp(_, _)) if from_type.is_numeric() => true,
318        (Date64, Timestamp(_, _)) => true,
319        (Date32, Timestamp(_, _)) => true,
320        (
321            Timestamp(_, _),
322            Timestamp(_, _)
323            | Date32
324            | Date64
325            | Time32(Second)
326            | Time32(Millisecond)
327            | Time64(Microsecond)
328            | Time64(Nanosecond),
329        ) => true,
330        (_, Duration(_)) if from_type.is_numeric() => true,
331        (Duration(_), _) if to_type.is_numeric() => true,
332        (Duration(_), Duration(_)) => true,
333        (Interval(from_type), Int64) => {
334            match from_type {
335                YearMonth => true,
336                DayTime => true,
337                MonthDayNano => false, // Native type is i128
338            }
339        }
340        (Int32, Interval(to_type)) => match to_type {
341            YearMonth => true,
342            DayTime => false,
343            MonthDayNano => false,
344        },
345        (Duration(_), Interval(MonthDayNano)) => true,
346        (Interval(MonthDayNano), Duration(_)) => true,
347        (Interval(YearMonth), Interval(MonthDayNano)) => true,
348        (Interval(DayTime), Interval(MonthDayNano)) => true,
349        (_, _) => false,
350    }
351}
352
353/// Cast `array` to the provided data type and return a new Array with type `to_type`, if possible.
354///
355/// See [`cast_with_options`] for more information
356pub fn cast(array: &dyn Array, to_type: &DataType) -> Result<ArrayRef, ArrowError> {
357    cast_with_options(array, to_type, &CastOptions::default())
358}
359
360fn cast_integer_to_decimal<
361    T: ArrowPrimitiveType,
362    D: DecimalType + ArrowPrimitiveType<Native = M>,
363    M,
364>(
365    array: &PrimitiveArray<T>,
366    precision: u8,
367    scale: i8,
368    base: M,
369    cast_options: &CastOptions,
370) -> Result<ArrayRef, ArrowError>
371where
372    <T as ArrowPrimitiveType>::Native: AsPrimitive<M>,
373    M: ArrowNativeTypeOp,
374{
375    let scale_factor = base.pow_checked(scale.unsigned_abs() as u32).map_err(|_| {
376        ArrowError::CastError(format!(
377            "Cannot cast to {:?}({}, {}). The scale causes overflow.",
378            D::PREFIX,
379            precision,
380            scale,
381        ))
382    })?;
383
384    let array = if scale < 0 {
385        match cast_options.safe {
386            true => array.unary_opt::<_, D>(|v| {
387                v.as_()
388                    .div_checked(scale_factor)
389                    .ok()
390                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
391            }),
392            false => array.try_unary::<_, D, _>(|v| {
393                v.as_()
394                    .div_checked(scale_factor)
395                    .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|_| v))
396            })?,
397        }
398    } else {
399        match cast_options.safe {
400            true => array.unary_opt::<_, D>(|v| {
401                v.as_()
402                    .mul_checked(scale_factor)
403                    .ok()
404                    .and_then(|v| (D::is_valid_decimal_precision(v, precision)).then_some(v))
405            }),
406            false => array.try_unary::<_, D, _>(|v| {
407                v.as_()
408                    .mul_checked(scale_factor)
409                    .and_then(|v| D::validate_decimal_precision(v, precision, scale).map(|_| v))
410            })?,
411        }
412    };
413
414    Ok(Arc::new(array.with_precision_and_scale(precision, scale)?))
415}
416
417/// Cast the array from interval year month to month day nano
418fn cast_interval_year_month_to_interval_month_day_nano(
419    array: &dyn Array,
420    _cast_options: &CastOptions,
421) -> Result<ArrayRef, ArrowError> {
422    let array = array.as_primitive::<IntervalYearMonthType>();
423
424    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
425        let months = IntervalYearMonthType::to_months(v);
426        IntervalMonthDayNanoType::make_value(months, 0, 0)
427    })))
428}
429
430/// Cast the array from interval day time to month day nano
431fn cast_interval_day_time_to_interval_month_day_nano(
432    array: &dyn Array,
433    _cast_options: &CastOptions,
434) -> Result<ArrayRef, ArrowError> {
435    let array = array.as_primitive::<IntervalDayTimeType>();
436    let mul = 1_000_000;
437
438    Ok(Arc::new(array.unary::<_, IntervalMonthDayNanoType>(|v| {
439        let (days, ms) = IntervalDayTimeType::to_parts(v);
440        IntervalMonthDayNanoType::make_value(0, days, ms as i64 * mul)
441    })))
442}
443
444/// Cast the array from interval to duration
445fn cast_month_day_nano_to_duration<D: ArrowTemporalType<Native = i64>>(
446    array: &dyn Array,
447    cast_options: &CastOptions,
448) -> Result<ArrayRef, ArrowError> {
449    let array = array.as_primitive::<IntervalMonthDayNanoType>();
450    let scale = match D::DATA_TYPE {
451        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
452        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
453        DataType::Duration(TimeUnit::Microsecond) => 1_000,
454        DataType::Duration(TimeUnit::Nanosecond) => 1,
455        _ => unreachable!(),
456    };
457
458    if cast_options.safe {
459        let iter = array.iter().map(|v| {
460            v.and_then(|v| (v.days == 0 && v.months == 0).then_some(v.nanoseconds / scale))
461        });
462        Ok(Arc::new(unsafe {
463            PrimitiveArray::<D>::from_trusted_len_iter(iter)
464        }))
465    } else {
466        let vec = array
467            .iter()
468            .map(|v| {
469                v.map(|v| match v.days == 0 && v.months == 0 {
470                    true => Ok((v.nanoseconds) / scale),
471                    _ => Err(ArrowError::ComputeError(
472                        "Cannot convert interval containing non-zero months or days to duration"
473                            .to_string(),
474                    )),
475                })
476                .transpose()
477            })
478            .collect::<Result<Vec<_>, _>>()?;
479        Ok(Arc::new(unsafe {
480            PrimitiveArray::<D>::from_trusted_len_iter(vec.iter())
481        }))
482    }
483}
484
485/// Cast the array from duration and interval
486fn cast_duration_to_interval<D: ArrowTemporalType<Native = i64>>(
487    array: &dyn Array,
488    cast_options: &CastOptions,
489) -> Result<ArrayRef, ArrowError> {
490    let array = array
491        .as_any()
492        .downcast_ref::<PrimitiveArray<D>>()
493        .ok_or_else(|| {
494            ArrowError::ComputeError(
495                "Internal Error: Cannot cast duration to DurationArray of expected type"
496                    .to_string(),
497            )
498        })?;
499
500    let scale = match array.data_type() {
501        DataType::Duration(TimeUnit::Second) => 1_000_000_000,
502        DataType::Duration(TimeUnit::Millisecond) => 1_000_000,
503        DataType::Duration(TimeUnit::Microsecond) => 1_000,
504        DataType::Duration(TimeUnit::Nanosecond) => 1,
505        _ => unreachable!(),
506    };
507
508    if cast_options.safe {
509        let iter = array.iter().map(|v| {
510            v.and_then(|v| {
511                v.checked_mul(scale)
512                    .map(|v| IntervalMonthDayNano::new(0, 0, v))
513            })
514        });
515        Ok(Arc::new(unsafe {
516            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(iter)
517        }))
518    } else {
519        let vec = array
520            .iter()
521            .map(|v| {
522                v.map(|v| {
523                    if let Ok(v) = v.mul_checked(scale) {
524                        Ok(IntervalMonthDayNano::new(0, 0, v))
525                    } else {
526                        Err(ArrowError::ComputeError(format!(
527                            "Cannot cast to {:?}. Overflowing on {:?}",
528                            IntervalMonthDayNanoType::DATA_TYPE,
529                            v
530                        )))
531                    }
532                })
533                .transpose()
534            })
535            .collect::<Result<Vec<_>, _>>()?;
536        Ok(Arc::new(unsafe {
537            PrimitiveArray::<IntervalMonthDayNanoType>::from_trusted_len_iter(vec.iter())
538        }))
539    }
540}
541
542/// Cast the primitive array using [`PrimitiveArray::reinterpret_cast`]
543fn cast_reinterpret_arrays<I: ArrowPrimitiveType, O: ArrowPrimitiveType<Native = I::Native>>(
544    array: &dyn Array,
545) -> Result<ArrayRef, ArrowError> {
546    Ok(Arc::new(array.as_primitive::<I>().reinterpret_cast::<O>()))
547}
548
549fn make_timestamp_array(
550    array: &PrimitiveArray<Int64Type>,
551    unit: TimeUnit,
552    tz: Option<Arc<str>>,
553) -> ArrayRef {
554    match unit {
555        TimeUnit::Second => Arc::new(
556            array
557                .reinterpret_cast::<TimestampSecondType>()
558                .with_timezone_opt(tz),
559        ),
560        TimeUnit::Millisecond => Arc::new(
561            array
562                .reinterpret_cast::<TimestampMillisecondType>()
563                .with_timezone_opt(tz),
564        ),
565        TimeUnit::Microsecond => Arc::new(
566            array
567                .reinterpret_cast::<TimestampMicrosecondType>()
568                .with_timezone_opt(tz),
569        ),
570        TimeUnit::Nanosecond => Arc::new(
571            array
572                .reinterpret_cast::<TimestampNanosecondType>()
573                .with_timezone_opt(tz),
574        ),
575    }
576}
577
578fn make_duration_array(array: &PrimitiveArray<Int64Type>, unit: TimeUnit) -> ArrayRef {
579    match unit {
580        TimeUnit::Second => Arc::new(array.reinterpret_cast::<DurationSecondType>()),
581        TimeUnit::Millisecond => Arc::new(array.reinterpret_cast::<DurationMillisecondType>()),
582        TimeUnit::Microsecond => Arc::new(array.reinterpret_cast::<DurationMicrosecondType>()),
583        TimeUnit::Nanosecond => Arc::new(array.reinterpret_cast::<DurationNanosecondType>()),
584    }
585}
586
587fn as_time_res_with_timezone<T: ArrowPrimitiveType>(
588    v: i64,
589    tz: Option<Tz>,
590) -> Result<NaiveTime, ArrowError> {
591    let time = match tz {
592        Some(tz) => as_datetime_with_timezone::<T>(v, tz).map(|d| d.time()),
593        None => as_datetime::<T>(v).map(|d| d.time()),
594    };
595
596    time.ok_or_else(|| {
597        ArrowError::CastError(format!(
598            "Failed to create naive time with {} {}",
599            std::any::type_name::<T>(),
600            v
601        ))
602    })
603}
604
605fn timestamp_to_date32<T: ArrowTimestampType>(
606    array: &PrimitiveArray<T>,
607) -> Result<ArrayRef, ArrowError> {
608    let err = |x: i64| {
609        ArrowError::CastError(format!(
610            "Cannot convert {} {x} to datetime",
611            std::any::type_name::<T>()
612        ))
613    };
614
615    let array: Date32Array = match array.timezone() {
616        Some(tz) => {
617            let tz: Tz = tz.parse()?;
618            array.try_unary(|x| {
619                as_datetime_with_timezone::<T>(x, tz)
620                    .ok_or_else(|| err(x))
621                    .map(|d| Date32Type::from_naive_date(d.date_naive()))
622            })?
623        }
624        None => array.try_unary(|x| {
625            as_datetime::<T>(x)
626                .ok_or_else(|| err(x))
627                .map(|d| Date32Type::from_naive_date(d.date()))
628        })?,
629    };
630    Ok(Arc::new(array))
631}
632
633/// Try to cast `array` to `to_type` if possible.
634///
635/// Returns a new Array with type `to_type` if possible.
636///
637/// Accepts [`CastOptions`] to specify cast behavior. See also [`cast()`].
638///
639/// # Behavior
640/// * `Boolean` to `Utf8`: `true` => '1', `false` => `0`
641/// * `Utf8` to `Boolean`: `true`, `yes`, `on`, `1` => `true`, `false`, `no`, `off`, `0` => `false`,
642///   short variants are accepted, other strings return null or error
643/// * `Utf8` to Numeric: strings that can't be parsed to numbers return null, float strings
644///   in integer casts return null
645/// * Numeric to `Boolean`: 0 returns `false`, any other value returns `true`
646/// * `List` to `List`: the underlying data type is cast
647/// * `List` to `FixedSizeList`: the underlying data type is cast. If safe is true and a list element
648///   has the wrong length it will be replaced with NULL, otherwise an error will be returned
649/// * Primitive to `List`: a list array with 1 value per slot is created
650/// * `Date32` and `Date64`: precision lost when going to higher interval
651/// * `Time32 and `Time64`: precision lost when going to higher interval
652/// * `Timestamp` and `Date{32|64}`: precision lost when going to higher interval
653/// * Temporal to/from backing Primitive: zero-copy with data type change
654/// * `Float32/Float64` to `Decimal(precision, scale)` rounds to the `scale` decimals
655///   (i.e. casting `6.4999` to `Decimal(10, 1)` becomes `6.5`).
656/// * `Decimal` to `Float32/Float64` is lossy and values outside the representable
657///   range become `INFINITY` or `-INFINITY` without error.
658///
659/// Unsupported Casts (check with `can_cast_types` before calling):
660/// * To or from `StructArray`
661/// * `List` to `Primitive`
662/// * `Interval` and `Duration`
663///
664/// # Durations and Intervals
665///
666/// Casting integer types directly to interval types such as
667/// [`IntervalMonthDayNano`] is not supported because the meaning of the integer
668/// is ambiguous. For example, the integer  could represent either nanoseconds
669/// or months.
670///
671/// To cast an integer type to an interval type, first convert to a Duration
672/// type, and then cast that to the desired interval type.
673///
674/// For example, to convert an `Int64` representing nanoseconds to an
675/// `IntervalMonthDayNano` you would first convert the `Int64` to a
676/// `DurationNanoseconds`, and then cast that to `IntervalMonthDayNano`.
677///
678/// # Timestamps and Timezones
679///
680/// Timestamps are stored with an optional timezone in Arrow.
681///
682/// ## Casting timestamps to a timestamp without timezone / UTC
683/// ```
684/// # use arrow_array::Int64Array;
685/// # use arrow_array::types::TimestampSecondType;
686/// # use arrow_cast::{cast, display};
687/// # use arrow_array::cast::AsArray;
688/// # use arrow_schema::{DataType, TimeUnit};
689/// // can use "UTC" if chrono-tz feature is enabled, here use offset based timezone
690/// let data_type = DataType::Timestamp(TimeUnit::Second, None);
691/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
692/// let b = cast(&a, &data_type).unwrap();
693/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
694/// assert_eq!(2_000_000_000, b.value(1)); // values are the same as the type has no timezone
695/// // use display to show them (note has no trailing Z)
696/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
697/// ```
698///
699/// ## Casting timestamps to a timestamp with timezone
700///
701/// Similarly to the previous example, if you cast numeric values to a timestamp
702/// with timezone, the cast kernel will not change the underlying values
703/// but display and other functions will interpret them as being in the provided timezone.
704///
705/// ```
706/// # use arrow_array::Int64Array;
707/// # use arrow_array::types::TimestampSecondType;
708/// # use arrow_cast::{cast, display};
709/// # use arrow_array::cast::AsArray;
710/// # use arrow_schema::{DataType, TimeUnit};
711/// // can use "Americas/New_York" if chrono-tz feature is enabled, here use offset based timezone
712/// let data_type = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
713/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
714/// let b = cast(&a, &data_type).unwrap();
715/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
716/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
717/// // displayed in the target timezone (note the offset -05:00)
718/// assert_eq!("2033-05-17T22:33:20-05:00", display::array_value_to_string(&b, 1).unwrap());
719/// ```
720/// # Casting timestamps without timezone to timestamps with timezone
721///
722/// When casting from a timestamp without timezone to a timestamp with
723/// timezone, the cast kernel interprets the timestamp values as being in
724/// the destination timezone and then adjusts the underlying value to UTC as required
725///
726/// However, note that when casting from a timestamp with timezone BACK to a
727/// timestamp without timezone the cast kernel does not adjust the values.
728///
729/// Thus round trip casting a timestamp without timezone to a timestamp with
730/// timezone and back to a timestamp without timezone results in different
731/// values than the starting values.
732///
733/// ```
734/// # use arrow_array::Int64Array;
735/// # use arrow_array::types::{TimestampSecondType};
736/// # use arrow_cast::{cast, display};
737/// # use arrow_array::cast::AsArray;
738/// # use arrow_schema::{DataType, TimeUnit};
739/// let data_type  = DataType::Timestamp(TimeUnit::Second, None);
740/// let data_type_tz = DataType::Timestamp(TimeUnit::Second, Some("-05:00".into()));
741/// let a = Int64Array::from(vec![1_000_000_000, 2_000_000_000, 3_000_000_000]);
742/// let b = cast(&a, &data_type).unwrap(); // cast to timestamp without timezone
743/// let b = b.as_primitive::<TimestampSecondType>(); // downcast to result type
744/// assert_eq!(2_000_000_000, b.value(1)); // values are still the same
745/// // displayed without a timezone (note lack of offset or Z)
746/// assert_eq!("2033-05-18T03:33:20", display::array_value_to_string(&b, 1).unwrap());
747///
748/// // Convert timestamps without a timezone to timestamps with a timezone
749/// let c = cast(&b, &data_type_tz).unwrap();
750/// let c = c.as_primitive::<TimestampSecondType>(); // downcast to result type
751/// assert_eq!(2_000_018_000, c.value(1)); // value has been adjusted by offset
752/// // displayed with the target timezone offset (-05:00)
753/// assert_eq!("2033-05-18T03:33:20-05:00", display::array_value_to_string(&c, 1).unwrap());
754///
755/// // Convert from timestamp with timezone back to timestamp without timezone
756/// let d = cast(&c, &data_type).unwrap();
757/// let d = d.as_primitive::<TimestampSecondType>(); // downcast to result type
758/// assert_eq!(2_000_018_000, d.value(1)); // value has not been adjusted
759/// // NOTE: the timestamp is adjusted (08:33:20 instead of 03:33:20 as in previous example)
760/// assert_eq!("2033-05-18T08:33:20", display::array_value_to_string(&d, 1).unwrap());
761/// ```
762pub fn cast_with_options(
763    array: &dyn Array,
764    to_type: &DataType,
765    cast_options: &CastOptions,
766) -> Result<ArrayRef, ArrowError> {
767    use DataType::*;
768    let from_type = array.data_type();
769    // clone array if types are the same
770    if from_type == to_type {
771        return Ok(make_array(array.to_data()));
772    }
773    match (from_type, to_type) {
774        (Null, _) => Ok(new_null_array(to_type, array.len())),
775        (RunEndEncoded(index_type, _), _) => match index_type.data_type() {
776            Int16 => run_end_encoded_cast::<Int16Type>(array, to_type, cast_options),
777            Int32 => run_end_encoded_cast::<Int32Type>(array, to_type, cast_options),
778            Int64 => run_end_encoded_cast::<Int64Type>(array, to_type, cast_options),
779            _ => Err(ArrowError::CastError(format!(
780                "Casting from run end encoded type {from_type:?} to {to_type:?} not supported",
781            ))),
782        },
783        (_, RunEndEncoded(index_type, value_type)) => {
784            let array_ref = make_array(array.to_data());
785            match index_type.data_type() {
786                Int16 => cast_to_run_end_encoded::<Int16Type>(
787                    &array_ref,
788                    value_type.data_type(),
789                    cast_options,
790                ),
791                Int32 => cast_to_run_end_encoded::<Int32Type>(
792                    &array_ref,
793                    value_type.data_type(),
794                    cast_options,
795                ),
796                Int64 => cast_to_run_end_encoded::<Int64Type>(
797                    &array_ref,
798                    value_type.data_type(),
799                    cast_options,
800                ),
801                _ => Err(ArrowError::CastError(format!(
802                    "Casting from type {from_type:?} to run end encoded type {to_type:?} not supported",
803                ))),
804            }
805        }
806        (Dictionary(index_type, _), _) => match **index_type {
807            Int8 => dictionary_cast::<Int8Type>(array, to_type, cast_options),
808            Int16 => dictionary_cast::<Int16Type>(array, to_type, cast_options),
809            Int32 => dictionary_cast::<Int32Type>(array, to_type, cast_options),
810            Int64 => dictionary_cast::<Int64Type>(array, to_type, cast_options),
811            UInt8 => dictionary_cast::<UInt8Type>(array, to_type, cast_options),
812            UInt16 => dictionary_cast::<UInt16Type>(array, to_type, cast_options),
813            UInt32 => dictionary_cast::<UInt32Type>(array, to_type, cast_options),
814            UInt64 => dictionary_cast::<UInt64Type>(array, to_type, cast_options),
815            _ => Err(ArrowError::CastError(format!(
816                "Casting from dictionary type {from_type} to {to_type} not supported",
817            ))),
818        },
819        (_, Dictionary(index_type, value_type)) => match **index_type {
820            Int8 => cast_to_dictionary::<Int8Type>(array, value_type, cast_options),
821            Int16 => cast_to_dictionary::<Int16Type>(array, value_type, cast_options),
822            Int32 => cast_to_dictionary::<Int32Type>(array, value_type, cast_options),
823            Int64 => cast_to_dictionary::<Int64Type>(array, value_type, cast_options),
824            UInt8 => cast_to_dictionary::<UInt8Type>(array, value_type, cast_options),
825            UInt16 => cast_to_dictionary::<UInt16Type>(array, value_type, cast_options),
826            UInt32 => cast_to_dictionary::<UInt32Type>(array, value_type, cast_options),
827            UInt64 => cast_to_dictionary::<UInt64Type>(array, value_type, cast_options),
828            _ => Err(ArrowError::CastError(format!(
829                "Casting from type {from_type} to dictionary type {to_type} not supported",
830            ))),
831        },
832        (List(_), List(to)) => cast_list_values::<i32>(array, to, cast_options),
833        (LargeList(_), LargeList(to)) => cast_list_values::<i64>(array, to, cast_options),
834        (List(_), LargeList(list_to)) => cast_list::<i32, i64>(array, list_to, cast_options),
835        (LargeList(_), List(list_to)) => cast_list::<i64, i32>(array, list_to, cast_options),
836        (List(_), FixedSizeList(field, size)) => {
837            let array = array.as_list::<i32>();
838            cast_list_to_fixed_size_list::<i32>(array, field, *size, cast_options)
839        }
840        (LargeList(_), FixedSizeList(field, size)) => {
841            let array = array.as_list::<i64>();
842            cast_list_to_fixed_size_list::<i64>(array, field, *size, cast_options)
843        }
844        (ListView(_), List(list_to)) => cast_list_view_to_list::<i32>(array, list_to, cast_options),
845        (LargeListView(_), LargeList(list_to)) => {
846            cast_list_view_to_list::<i64>(array, list_to, cast_options)
847        }
848        (ListView(_), LargeListView(list_to)) => {
849            cast_list_view::<i32, i64>(array, list_to, cast_options)
850        }
851        (LargeListView(_), ListView(list_to)) => {
852            cast_list_view::<i64, i32>(array, list_to, cast_options)
853        }
854        (List(_), ListView(_)) => cast_list_to_list_view::<i32>(array),
855        (LargeList(_), LargeListView(_)) => cast_list_to_list_view::<i64>(array),
856        (List(_) | LargeList(_), _) => match to_type {
857            Utf8 => value_to_string::<i32>(array, cast_options),
858            LargeUtf8 => value_to_string::<i64>(array, cast_options),
859            _ => Err(ArrowError::CastError(
860                "Cannot cast list to non-list data types".to_string(),
861            )),
862        },
863        (FixedSizeList(list_from, size), List(list_to)) => {
864            if list_to.data_type() != list_from.data_type() {
865                // To transform inner type, can first cast to FSL with new inner type.
866                let fsl_to = DataType::FixedSizeList(list_to.clone(), *size);
867                let array = cast_with_options(array, &fsl_to, cast_options)?;
868                cast_fixed_size_list_to_list::<i32>(array.as_ref())
869            } else {
870                cast_fixed_size_list_to_list::<i32>(array)
871            }
872        }
873        (FixedSizeList(list_from, size), LargeList(list_to)) => {
874            if list_to.data_type() != list_from.data_type() {
875                // To transform inner type, can first cast to FSL with new inner type.
876                let fsl_to = DataType::FixedSizeList(list_to.clone(), *size);
877                let array = cast_with_options(array, &fsl_to, cast_options)?;
878                cast_fixed_size_list_to_list::<i64>(array.as_ref())
879            } else {
880                cast_fixed_size_list_to_list::<i64>(array)
881            }
882        }
883        (FixedSizeList(_, size_from), FixedSizeList(list_to, size_to)) => {
884            if size_from != size_to {
885                return Err(ArrowError::CastError(
886                    "cannot cast fixed-size-list to fixed-size-list with different size".into(),
887                ));
888            }
889            let array = array.as_any().downcast_ref::<FixedSizeListArray>().unwrap();
890            let values = cast_with_options(array.values(), list_to.data_type(), cast_options)?;
891            Ok(Arc::new(FixedSizeListArray::try_new(
892                list_to.clone(),
893                *size_from,
894                values,
895                array.nulls().cloned(),
896            )?))
897        }
898        (_, List(to)) => cast_values_to_list::<i32>(array, to, cast_options),
899        (_, LargeList(to)) => cast_values_to_list::<i64>(array, to, cast_options),
900        (_, FixedSizeList(to, size)) if *size == 1 => {
901            cast_values_to_fixed_size_list(array, to, *size, cast_options)
902        }
903        (FixedSizeList(_, size), _) if *size == 1 => {
904            cast_single_element_fixed_size_list_to_values(array, to_type, cast_options)
905        }
906        (Map(_, ordered1), Map(_, ordered2)) if ordered1 == ordered2 => {
907            cast_map_values(array.as_map(), to_type, cast_options, ordered1.to_owned())
908        }
909        // Decimal to decimal, same width
910        (Decimal32(p1, s1), Decimal32(p2, s2)) => {
911            cast_decimal_to_decimal_same_type::<Decimal32Type>(
912                array.as_primitive(),
913                *p1,
914                *s1,
915                *p2,
916                *s2,
917                cast_options,
918            )
919        }
920        (Decimal64(p1, s1), Decimal64(p2, s2)) => {
921            cast_decimal_to_decimal_same_type::<Decimal64Type>(
922                array.as_primitive(),
923                *p1,
924                *s1,
925                *p2,
926                *s2,
927                cast_options,
928            )
929        }
930        (Decimal128(p1, s1), Decimal128(p2, s2)) => {
931            cast_decimal_to_decimal_same_type::<Decimal128Type>(
932                array.as_primitive(),
933                *p1,
934                *s1,
935                *p2,
936                *s2,
937                cast_options,
938            )
939        }
940        (Decimal256(p1, s1), Decimal256(p2, s2)) => {
941            cast_decimal_to_decimal_same_type::<Decimal256Type>(
942                array.as_primitive(),
943                *p1,
944                *s1,
945                *p2,
946                *s2,
947                cast_options,
948            )
949        }
950        // Decimal to decimal, different width
951        (Decimal32(p1, s1), Decimal64(p2, s2)) => {
952            cast_decimal_to_decimal::<Decimal32Type, Decimal64Type>(
953                array.as_primitive(),
954                *p1,
955                *s1,
956                *p2,
957                *s2,
958                cast_options,
959            )
960        }
961        (Decimal32(p1, s1), Decimal128(p2, s2)) => {
962            cast_decimal_to_decimal::<Decimal32Type, Decimal128Type>(
963                array.as_primitive(),
964                *p1,
965                *s1,
966                *p2,
967                *s2,
968                cast_options,
969            )
970        }
971        (Decimal32(p1, s1), Decimal256(p2, s2)) => {
972            cast_decimal_to_decimal::<Decimal32Type, Decimal256Type>(
973                array.as_primitive(),
974                *p1,
975                *s1,
976                *p2,
977                *s2,
978                cast_options,
979            )
980        }
981        (Decimal64(p1, s1), Decimal32(p2, s2)) => {
982            cast_decimal_to_decimal::<Decimal64Type, Decimal32Type>(
983                array.as_primitive(),
984                *p1,
985                *s1,
986                *p2,
987                *s2,
988                cast_options,
989            )
990        }
991        (Decimal64(p1, s1), Decimal128(p2, s2)) => {
992            cast_decimal_to_decimal::<Decimal64Type, Decimal128Type>(
993                array.as_primitive(),
994                *p1,
995                *s1,
996                *p2,
997                *s2,
998                cast_options,
999            )
1000        }
1001        (Decimal64(p1, s1), Decimal256(p2, s2)) => {
1002            cast_decimal_to_decimal::<Decimal64Type, Decimal256Type>(
1003                array.as_primitive(),
1004                *p1,
1005                *s1,
1006                *p2,
1007                *s2,
1008                cast_options,
1009            )
1010        }
1011        (Decimal128(p1, s1), Decimal32(p2, s2)) => {
1012            cast_decimal_to_decimal::<Decimal128Type, Decimal32Type>(
1013                array.as_primitive(),
1014                *p1,
1015                *s1,
1016                *p2,
1017                *s2,
1018                cast_options,
1019            )
1020        }
1021        (Decimal128(p1, s1), Decimal64(p2, s2)) => {
1022            cast_decimal_to_decimal::<Decimal128Type, Decimal64Type>(
1023                array.as_primitive(),
1024                *p1,
1025                *s1,
1026                *p2,
1027                *s2,
1028                cast_options,
1029            )
1030        }
1031        (Decimal128(p1, s1), Decimal256(p2, s2)) => {
1032            cast_decimal_to_decimal::<Decimal128Type, Decimal256Type>(
1033                array.as_primitive(),
1034                *p1,
1035                *s1,
1036                *p2,
1037                *s2,
1038                cast_options,
1039            )
1040        }
1041        (Decimal256(p1, s1), Decimal32(p2, s2)) => {
1042            cast_decimal_to_decimal::<Decimal256Type, Decimal32Type>(
1043                array.as_primitive(),
1044                *p1,
1045                *s1,
1046                *p2,
1047                *s2,
1048                cast_options,
1049            )
1050        }
1051        (Decimal256(p1, s1), Decimal64(p2, s2)) => {
1052            cast_decimal_to_decimal::<Decimal256Type, Decimal64Type>(
1053                array.as_primitive(),
1054                *p1,
1055                *s1,
1056                *p2,
1057                *s2,
1058                cast_options,
1059            )
1060        }
1061        (Decimal256(p1, s1), Decimal128(p2, s2)) => {
1062            cast_decimal_to_decimal::<Decimal256Type, Decimal128Type>(
1063                array.as_primitive(),
1064                *p1,
1065                *s1,
1066                *p2,
1067                *s2,
1068                cast_options,
1069            )
1070        }
1071        // Decimal to non-decimal
1072        (Decimal32(_, scale), _) if !to_type.is_temporal() => {
1073            cast_from_decimal::<Decimal32Type, _>(
1074                array,
1075                10_i32,
1076                scale,
1077                from_type,
1078                to_type,
1079                |x: i32| x as f64,
1080                cast_options,
1081            )
1082        }
1083        (Decimal64(_, scale), _) if !to_type.is_temporal() => {
1084            cast_from_decimal::<Decimal64Type, _>(
1085                array,
1086                10_i64,
1087                scale,
1088                from_type,
1089                to_type,
1090                |x: i64| x as f64,
1091                cast_options,
1092            )
1093        }
1094        (Decimal128(_, scale), _) if !to_type.is_temporal() => {
1095            cast_from_decimal::<Decimal128Type, _>(
1096                array,
1097                10_i128,
1098                scale,
1099                from_type,
1100                to_type,
1101                |x: i128| x as f64,
1102                cast_options,
1103            )
1104        }
1105        (Decimal256(_, scale), _) if !to_type.is_temporal() => {
1106            cast_from_decimal::<Decimal256Type, _>(
1107                array,
1108                i256::from_i128(10_i128),
1109                scale,
1110                from_type,
1111                to_type,
1112                |x: i256| x.to_f64().expect("All i256 values fit in f64"),
1113                cast_options,
1114            )
1115        }
1116        // Non-decimal to decimal
1117        (_, Decimal32(precision, scale)) if !from_type.is_temporal() => {
1118            cast_to_decimal::<Decimal32Type, _>(
1119                array,
1120                10_i32,
1121                precision,
1122                scale,
1123                from_type,
1124                to_type,
1125                cast_options,
1126            )
1127        }
1128        (_, Decimal64(precision, scale)) if !from_type.is_temporal() => {
1129            cast_to_decimal::<Decimal64Type, _>(
1130                array,
1131                10_i64,
1132                precision,
1133                scale,
1134                from_type,
1135                to_type,
1136                cast_options,
1137            )
1138        }
1139        (_, Decimal128(precision, scale)) if !from_type.is_temporal() => {
1140            cast_to_decimal::<Decimal128Type, _>(
1141                array,
1142                10_i128,
1143                precision,
1144                scale,
1145                from_type,
1146                to_type,
1147                cast_options,
1148            )
1149        }
1150        (_, Decimal256(precision, scale)) if !from_type.is_temporal() => {
1151            cast_to_decimal::<Decimal256Type, _>(
1152                array,
1153                i256::from_i128(10_i128),
1154                precision,
1155                scale,
1156                from_type,
1157                to_type,
1158                cast_options,
1159            )
1160        }
1161        (Struct(from_fields), Struct(to_fields)) => cast_struct_to_struct(
1162            array.as_struct(),
1163            from_fields.clone(),
1164            to_fields.clone(),
1165            cast_options,
1166        ),
1167        (Struct(_), _) => Err(ArrowError::CastError(format!(
1168            "Casting from {from_type} to {to_type} not supported"
1169        ))),
1170        (_, Struct(_)) => Err(ArrowError::CastError(format!(
1171            "Casting from {from_type} to {to_type} not supported"
1172        ))),
1173        (_, Boolean) => match from_type {
1174            UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
1175            UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
1176            UInt32 => cast_numeric_to_bool::<UInt32Type>(array),
1177            UInt64 => cast_numeric_to_bool::<UInt64Type>(array),
1178            Int8 => cast_numeric_to_bool::<Int8Type>(array),
1179            Int16 => cast_numeric_to_bool::<Int16Type>(array),
1180            Int32 => cast_numeric_to_bool::<Int32Type>(array),
1181            Int64 => cast_numeric_to_bool::<Int64Type>(array),
1182            Float16 => cast_numeric_to_bool::<Float16Type>(array),
1183            Float32 => cast_numeric_to_bool::<Float32Type>(array),
1184            Float64 => cast_numeric_to_bool::<Float64Type>(array),
1185            Utf8View => cast_utf8view_to_boolean(array, cast_options),
1186            Utf8 => cast_utf8_to_boolean::<i32>(array, cast_options),
1187            LargeUtf8 => cast_utf8_to_boolean::<i64>(array, cast_options),
1188            _ => Err(ArrowError::CastError(format!(
1189                "Casting from {from_type} to {to_type} not supported",
1190            ))),
1191        },
1192        (Boolean, _) => match to_type {
1193            UInt8 => cast_bool_to_numeric::<UInt8Type>(array, cast_options),
1194            UInt16 => cast_bool_to_numeric::<UInt16Type>(array, cast_options),
1195            UInt32 => cast_bool_to_numeric::<UInt32Type>(array, cast_options),
1196            UInt64 => cast_bool_to_numeric::<UInt64Type>(array, cast_options),
1197            Int8 => cast_bool_to_numeric::<Int8Type>(array, cast_options),
1198            Int16 => cast_bool_to_numeric::<Int16Type>(array, cast_options),
1199            Int32 => cast_bool_to_numeric::<Int32Type>(array, cast_options),
1200            Int64 => cast_bool_to_numeric::<Int64Type>(array, cast_options),
1201            Float16 => cast_bool_to_numeric::<Float16Type>(array, cast_options),
1202            Float32 => cast_bool_to_numeric::<Float32Type>(array, cast_options),
1203            Float64 => cast_bool_to_numeric::<Float64Type>(array, cast_options),
1204            Utf8View => value_to_string_view(array, cast_options),
1205            Utf8 => value_to_string::<i32>(array, cast_options),
1206            LargeUtf8 => value_to_string::<i64>(array, cast_options),
1207            _ => Err(ArrowError::CastError(format!(
1208                "Casting from {from_type} to {to_type} not supported",
1209            ))),
1210        },
1211        (Utf8, _) => match to_type {
1212            UInt8 => parse_string::<UInt8Type, i32>(array, cast_options),
1213            UInt16 => parse_string::<UInt16Type, i32>(array, cast_options),
1214            UInt32 => parse_string::<UInt32Type, i32>(array, cast_options),
1215            UInt64 => parse_string::<UInt64Type, i32>(array, cast_options),
1216            Int8 => parse_string::<Int8Type, i32>(array, cast_options),
1217            Int16 => parse_string::<Int16Type, i32>(array, cast_options),
1218            Int32 => parse_string::<Int32Type, i32>(array, cast_options),
1219            Int64 => parse_string::<Int64Type, i32>(array, cast_options),
1220            Float32 => parse_string::<Float32Type, i32>(array, cast_options),
1221            Float64 => parse_string::<Float64Type, i32>(array, cast_options),
1222            Date32 => parse_string::<Date32Type, i32>(array, cast_options),
1223            Date64 => parse_string::<Date64Type, i32>(array, cast_options),
1224            Binary => Ok(Arc::new(BinaryArray::from(
1225                array.as_string::<i32>().clone(),
1226            ))),
1227            LargeBinary => {
1228                let binary = BinaryArray::from(array.as_string::<i32>().clone());
1229                cast_byte_container::<BinaryType, LargeBinaryType>(&binary)
1230            }
1231            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i32>()))),
1232            BinaryView => Ok(Arc::new(
1233                StringViewArray::from(array.as_string::<i32>()).to_binary_view(),
1234            )),
1235            LargeUtf8 => cast_byte_container::<Utf8Type, LargeUtf8Type>(array),
1236            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i32>(array, cast_options),
1237            Time32(TimeUnit::Millisecond) => {
1238                parse_string::<Time32MillisecondType, i32>(array, cast_options)
1239            }
1240            Time64(TimeUnit::Microsecond) => {
1241                parse_string::<Time64MicrosecondType, i32>(array, cast_options)
1242            }
1243            Time64(TimeUnit::Nanosecond) => {
1244                parse_string::<Time64NanosecondType, i32>(array, cast_options)
1245            }
1246            Timestamp(TimeUnit::Second, to_tz) => {
1247                cast_string_to_timestamp::<i32, TimestampSecondType>(array, to_tz, cast_options)
1248            }
1249            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1250                i32,
1251                TimestampMillisecondType,
1252            >(array, to_tz, cast_options),
1253            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1254                i32,
1255                TimestampMicrosecondType,
1256            >(array, to_tz, cast_options),
1257            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1258                cast_string_to_timestamp::<i32, TimestampNanosecondType>(array, to_tz, cast_options)
1259            }
1260            Interval(IntervalUnit::YearMonth) => {
1261                cast_string_to_year_month_interval::<i32>(array, cast_options)
1262            }
1263            Interval(IntervalUnit::DayTime) => {
1264                cast_string_to_day_time_interval::<i32>(array, cast_options)
1265            }
1266            Interval(IntervalUnit::MonthDayNano) => {
1267                cast_string_to_month_day_nano_interval::<i32>(array, cast_options)
1268            }
1269            _ => Err(ArrowError::CastError(format!(
1270                "Casting from {from_type} to {to_type} not supported",
1271            ))),
1272        },
1273        (Utf8View, _) => match to_type {
1274            UInt8 => parse_string_view::<UInt8Type>(array, cast_options),
1275            UInt16 => parse_string_view::<UInt16Type>(array, cast_options),
1276            UInt32 => parse_string_view::<UInt32Type>(array, cast_options),
1277            UInt64 => parse_string_view::<UInt64Type>(array, cast_options),
1278            Int8 => parse_string_view::<Int8Type>(array, cast_options),
1279            Int16 => parse_string_view::<Int16Type>(array, cast_options),
1280            Int32 => parse_string_view::<Int32Type>(array, cast_options),
1281            Int64 => parse_string_view::<Int64Type>(array, cast_options),
1282            Float32 => parse_string_view::<Float32Type>(array, cast_options),
1283            Float64 => parse_string_view::<Float64Type>(array, cast_options),
1284            Date32 => parse_string_view::<Date32Type>(array, cast_options),
1285            Date64 => parse_string_view::<Date64Type>(array, cast_options),
1286            Binary => cast_view_to_byte::<StringViewType, GenericBinaryType<i32>>(array),
1287            LargeBinary => cast_view_to_byte::<StringViewType, GenericBinaryType<i64>>(array),
1288            BinaryView => Ok(Arc::new(array.as_string_view().clone().to_binary_view())),
1289            Utf8 => cast_view_to_byte::<StringViewType, GenericStringType<i32>>(array),
1290            LargeUtf8 => cast_view_to_byte::<StringViewType, GenericStringType<i64>>(array),
1291            Time32(TimeUnit::Second) => parse_string_view::<Time32SecondType>(array, cast_options),
1292            Time32(TimeUnit::Millisecond) => {
1293                parse_string_view::<Time32MillisecondType>(array, cast_options)
1294            }
1295            Time64(TimeUnit::Microsecond) => {
1296                parse_string_view::<Time64MicrosecondType>(array, cast_options)
1297            }
1298            Time64(TimeUnit::Nanosecond) => {
1299                parse_string_view::<Time64NanosecondType>(array, cast_options)
1300            }
1301            Timestamp(TimeUnit::Second, to_tz) => {
1302                cast_view_to_timestamp::<TimestampSecondType>(array, to_tz, cast_options)
1303            }
1304            Timestamp(TimeUnit::Millisecond, to_tz) => {
1305                cast_view_to_timestamp::<TimestampMillisecondType>(array, to_tz, cast_options)
1306            }
1307            Timestamp(TimeUnit::Microsecond, to_tz) => {
1308                cast_view_to_timestamp::<TimestampMicrosecondType>(array, to_tz, cast_options)
1309            }
1310            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1311                cast_view_to_timestamp::<TimestampNanosecondType>(array, to_tz, cast_options)
1312            }
1313            Interval(IntervalUnit::YearMonth) => {
1314                cast_view_to_year_month_interval(array, cast_options)
1315            }
1316            Interval(IntervalUnit::DayTime) => cast_view_to_day_time_interval(array, cast_options),
1317            Interval(IntervalUnit::MonthDayNano) => {
1318                cast_view_to_month_day_nano_interval(array, cast_options)
1319            }
1320            _ => Err(ArrowError::CastError(format!(
1321                "Casting from {from_type} to {to_type} not supported",
1322            ))),
1323        },
1324        (LargeUtf8, _) => match to_type {
1325            UInt8 => parse_string::<UInt8Type, i64>(array, cast_options),
1326            UInt16 => parse_string::<UInt16Type, i64>(array, cast_options),
1327            UInt32 => parse_string::<UInt32Type, i64>(array, cast_options),
1328            UInt64 => parse_string::<UInt64Type, i64>(array, cast_options),
1329            Int8 => parse_string::<Int8Type, i64>(array, cast_options),
1330            Int16 => parse_string::<Int16Type, i64>(array, cast_options),
1331            Int32 => parse_string::<Int32Type, i64>(array, cast_options),
1332            Int64 => parse_string::<Int64Type, i64>(array, cast_options),
1333            Float32 => parse_string::<Float32Type, i64>(array, cast_options),
1334            Float64 => parse_string::<Float64Type, i64>(array, cast_options),
1335            Date32 => parse_string::<Date32Type, i64>(array, cast_options),
1336            Date64 => parse_string::<Date64Type, i64>(array, cast_options),
1337            Utf8 => cast_byte_container::<LargeUtf8Type, Utf8Type>(array),
1338            Binary => {
1339                let large_binary = LargeBinaryArray::from(array.as_string::<i64>().clone());
1340                cast_byte_container::<LargeBinaryType, BinaryType>(&large_binary)
1341            }
1342            LargeBinary => Ok(Arc::new(LargeBinaryArray::from(
1343                array.as_string::<i64>().clone(),
1344            ))),
1345            Utf8View => Ok(Arc::new(StringViewArray::from(array.as_string::<i64>()))),
1346            BinaryView => Ok(Arc::new(BinaryViewArray::from(
1347                array
1348                    .as_string::<i64>()
1349                    .into_iter()
1350                    .map(|x| x.map(|x| x.as_bytes()))
1351                    .collect::<Vec<_>>(),
1352            ))),
1353            Time32(TimeUnit::Second) => parse_string::<Time32SecondType, i64>(array, cast_options),
1354            Time32(TimeUnit::Millisecond) => {
1355                parse_string::<Time32MillisecondType, i64>(array, cast_options)
1356            }
1357            Time64(TimeUnit::Microsecond) => {
1358                parse_string::<Time64MicrosecondType, i64>(array, cast_options)
1359            }
1360            Time64(TimeUnit::Nanosecond) => {
1361                parse_string::<Time64NanosecondType, i64>(array, cast_options)
1362            }
1363            Timestamp(TimeUnit::Second, to_tz) => {
1364                cast_string_to_timestamp::<i64, TimestampSecondType>(array, to_tz, cast_options)
1365            }
1366            Timestamp(TimeUnit::Millisecond, to_tz) => cast_string_to_timestamp::<
1367                i64,
1368                TimestampMillisecondType,
1369            >(array, to_tz, cast_options),
1370            Timestamp(TimeUnit::Microsecond, to_tz) => cast_string_to_timestamp::<
1371                i64,
1372                TimestampMicrosecondType,
1373            >(array, to_tz, cast_options),
1374            Timestamp(TimeUnit::Nanosecond, to_tz) => {
1375                cast_string_to_timestamp::<i64, TimestampNanosecondType>(array, to_tz, cast_options)
1376            }
1377            Interval(IntervalUnit::YearMonth) => {
1378                cast_string_to_year_month_interval::<i64>(array, cast_options)
1379            }
1380            Interval(IntervalUnit::DayTime) => {
1381                cast_string_to_day_time_interval::<i64>(array, cast_options)
1382            }
1383            Interval(IntervalUnit::MonthDayNano) => {
1384                cast_string_to_month_day_nano_interval::<i64>(array, cast_options)
1385            }
1386            _ => Err(ArrowError::CastError(format!(
1387                "Casting from {from_type} to {to_type} not supported",
1388            ))),
1389        },
1390        (Binary, _) => match to_type {
1391            Utf8 => cast_binary_to_string::<i32>(array, cast_options),
1392            LargeUtf8 => {
1393                let array = cast_binary_to_string::<i32>(array, cast_options)?;
1394                cast_byte_container::<Utf8Type, LargeUtf8Type>(array.as_ref())
1395            }
1396            LargeBinary => cast_byte_container::<BinaryType, LargeBinaryType>(array),
1397            FixedSizeBinary(size) => {
1398                cast_binary_to_fixed_size_binary::<i32>(array, *size, cast_options)
1399            }
1400            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i32>()))),
1401            Utf8View => Ok(Arc::new(StringViewArray::from(
1402                cast_binary_to_string::<i32>(array, cast_options)?.as_string::<i32>(),
1403            ))),
1404            _ => Err(ArrowError::CastError(format!(
1405                "Casting from {from_type} to {to_type} not supported",
1406            ))),
1407        },
1408        (LargeBinary, _) => match to_type {
1409            Utf8 => {
1410                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1411                cast_byte_container::<LargeUtf8Type, Utf8Type>(array.as_ref())
1412            }
1413            LargeUtf8 => cast_binary_to_string::<i64>(array, cast_options),
1414            Binary => cast_byte_container::<LargeBinaryType, BinaryType>(array),
1415            FixedSizeBinary(size) => {
1416                cast_binary_to_fixed_size_binary::<i64>(array, *size, cast_options)
1417            }
1418            BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i64>()))),
1419            Utf8View => {
1420                let array = cast_binary_to_string::<i64>(array, cast_options)?;
1421                Ok(Arc::new(StringViewArray::from(array.as_string::<i64>())))
1422            }
1423            _ => Err(ArrowError::CastError(format!(
1424                "Casting from {from_type} to {to_type} not supported",
1425            ))),
1426        },
1427        (FixedSizeBinary(size), _) => match to_type {
1428            Binary => cast_fixed_size_binary_to_binary::<i32>(array, *size),
1429            LargeBinary => cast_fixed_size_binary_to_binary::<i64>(array, *size),
1430            BinaryView => cast_fixed_size_binary_to_binary_view(array, *size),
1431            _ => Err(ArrowError::CastError(format!(
1432                "Casting from {from_type} to {to_type} not supported",
1433            ))),
1434        },
1435        (BinaryView, Binary) => cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array),
1436        (BinaryView, LargeBinary) => {
1437            cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)
1438        }
1439        (BinaryView, Utf8) => {
1440            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i32>>(array)?;
1441            cast_binary_to_string::<i32>(&binary_arr, cast_options)
1442        }
1443        (BinaryView, LargeUtf8) => {
1444            let binary_arr = cast_view_to_byte::<BinaryViewType, GenericBinaryType<i64>>(array)?;
1445            cast_binary_to_string::<i64>(&binary_arr, cast_options)
1446        }
1447        (BinaryView, Utf8View) => cast_binary_view_to_string_view(array, cast_options),
1448        (BinaryView, _) => Err(ArrowError::CastError(format!(
1449            "Casting from {from_type} to {to_type} not supported",
1450        ))),
1451        (from_type, Utf8View) if from_type.is_primitive() => {
1452            value_to_string_view(array, cast_options)
1453        }
1454        (from_type, LargeUtf8) if from_type.is_primitive() => {
1455            value_to_string::<i64>(array, cast_options)
1456        }
1457        (from_type, Utf8) if from_type.is_primitive() => {
1458            value_to_string::<i32>(array, cast_options)
1459        }
1460        (from_type, Binary) if from_type.is_integer() => match from_type {
1461            UInt8 => cast_numeric_to_binary::<UInt8Type, i32>(array),
1462            UInt16 => cast_numeric_to_binary::<UInt16Type, i32>(array),
1463            UInt32 => cast_numeric_to_binary::<UInt32Type, i32>(array),
1464            UInt64 => cast_numeric_to_binary::<UInt64Type, i32>(array),
1465            Int8 => cast_numeric_to_binary::<Int8Type, i32>(array),
1466            Int16 => cast_numeric_to_binary::<Int16Type, i32>(array),
1467            Int32 => cast_numeric_to_binary::<Int32Type, i32>(array),
1468            Int64 => cast_numeric_to_binary::<Int64Type, i32>(array),
1469            _ => unreachable!(),
1470        },
1471        (from_type, LargeBinary) if from_type.is_integer() => match from_type {
1472            UInt8 => cast_numeric_to_binary::<UInt8Type, i64>(array),
1473            UInt16 => cast_numeric_to_binary::<UInt16Type, i64>(array),
1474            UInt32 => cast_numeric_to_binary::<UInt32Type, i64>(array),
1475            UInt64 => cast_numeric_to_binary::<UInt64Type, i64>(array),
1476            Int8 => cast_numeric_to_binary::<Int8Type, i64>(array),
1477            Int16 => cast_numeric_to_binary::<Int16Type, i64>(array),
1478            Int32 => cast_numeric_to_binary::<Int32Type, i64>(array),
1479            Int64 => cast_numeric_to_binary::<Int64Type, i64>(array),
1480            _ => unreachable!(),
1481        },
1482        // start numeric casts
1483        (UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array, cast_options),
1484        (UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array, cast_options),
1485        (UInt8, UInt64) => cast_numeric_arrays::<UInt8Type, UInt64Type>(array, cast_options),
1486        (UInt8, Int8) => cast_numeric_arrays::<UInt8Type, Int8Type>(array, cast_options),
1487        (UInt8, Int16) => cast_numeric_arrays::<UInt8Type, Int16Type>(array, cast_options),
1488        (UInt8, Int32) => cast_numeric_arrays::<UInt8Type, Int32Type>(array, cast_options),
1489        (UInt8, Int64) => cast_numeric_arrays::<UInt8Type, Int64Type>(array, cast_options),
1490        (UInt8, Float16) => cast_numeric_arrays::<UInt8Type, Float16Type>(array, cast_options),
1491        (UInt8, Float32) => cast_numeric_arrays::<UInt8Type, Float32Type>(array, cast_options),
1492        (UInt8, Float64) => cast_numeric_arrays::<UInt8Type, Float64Type>(array, cast_options),
1493
1494        (UInt16, UInt8) => cast_numeric_arrays::<UInt16Type, UInt8Type>(array, cast_options),
1495        (UInt16, UInt32) => cast_numeric_arrays::<UInt16Type, UInt32Type>(array, cast_options),
1496        (UInt16, UInt64) => cast_numeric_arrays::<UInt16Type, UInt64Type>(array, cast_options),
1497        (UInt16, Int8) => cast_numeric_arrays::<UInt16Type, Int8Type>(array, cast_options),
1498        (UInt16, Int16) => cast_numeric_arrays::<UInt16Type, Int16Type>(array, cast_options),
1499        (UInt16, Int32) => cast_numeric_arrays::<UInt16Type, Int32Type>(array, cast_options),
1500        (UInt16, Int64) => cast_numeric_arrays::<UInt16Type, Int64Type>(array, cast_options),
1501        (UInt16, Float16) => cast_numeric_arrays::<UInt16Type, Float16Type>(array, cast_options),
1502        (UInt16, Float32) => cast_numeric_arrays::<UInt16Type, Float32Type>(array, cast_options),
1503        (UInt16, Float64) => cast_numeric_arrays::<UInt16Type, Float64Type>(array, cast_options),
1504
1505        (UInt32, UInt8) => cast_numeric_arrays::<UInt32Type, UInt8Type>(array, cast_options),
1506        (UInt32, UInt16) => cast_numeric_arrays::<UInt32Type, UInt16Type>(array, cast_options),
1507        (UInt32, UInt64) => cast_numeric_arrays::<UInt32Type, UInt64Type>(array, cast_options),
1508        (UInt32, Int8) => cast_numeric_arrays::<UInt32Type, Int8Type>(array, cast_options),
1509        (UInt32, Int16) => cast_numeric_arrays::<UInt32Type, Int16Type>(array, cast_options),
1510        (UInt32, Int32) => cast_numeric_arrays::<UInt32Type, Int32Type>(array, cast_options),
1511        (UInt32, Int64) => cast_numeric_arrays::<UInt32Type, Int64Type>(array, cast_options),
1512        (UInt32, Float16) => cast_numeric_arrays::<UInt32Type, Float16Type>(array, cast_options),
1513        (UInt32, Float32) => cast_numeric_arrays::<UInt32Type, Float32Type>(array, cast_options),
1514        (UInt32, Float64) => cast_numeric_arrays::<UInt32Type, Float64Type>(array, cast_options),
1515
1516        (UInt64, UInt8) => cast_numeric_arrays::<UInt64Type, UInt8Type>(array, cast_options),
1517        (UInt64, UInt16) => cast_numeric_arrays::<UInt64Type, UInt16Type>(array, cast_options),
1518        (UInt64, UInt32) => cast_numeric_arrays::<UInt64Type, UInt32Type>(array, cast_options),
1519        (UInt64, Int8) => cast_numeric_arrays::<UInt64Type, Int8Type>(array, cast_options),
1520        (UInt64, Int16) => cast_numeric_arrays::<UInt64Type, Int16Type>(array, cast_options),
1521        (UInt64, Int32) => cast_numeric_arrays::<UInt64Type, Int32Type>(array, cast_options),
1522        (UInt64, Int64) => cast_numeric_arrays::<UInt64Type, Int64Type>(array, cast_options),
1523        (UInt64, Float16) => cast_numeric_arrays::<UInt64Type, Float16Type>(array, cast_options),
1524        (UInt64, Float32) => cast_numeric_arrays::<UInt64Type, Float32Type>(array, cast_options),
1525        (UInt64, Float64) => cast_numeric_arrays::<UInt64Type, Float64Type>(array, cast_options),
1526
1527        (Int8, UInt8) => cast_numeric_arrays::<Int8Type, UInt8Type>(array, cast_options),
1528        (Int8, UInt16) => cast_numeric_arrays::<Int8Type, UInt16Type>(array, cast_options),
1529        (Int8, UInt32) => cast_numeric_arrays::<Int8Type, UInt32Type>(array, cast_options),
1530        (Int8, UInt64) => cast_numeric_arrays::<Int8Type, UInt64Type>(array, cast_options),
1531        (Int8, Int16) => cast_numeric_arrays::<Int8Type, Int16Type>(array, cast_options),
1532        (Int8, Int32) => cast_numeric_arrays::<Int8Type, Int32Type>(array, cast_options),
1533        (Int8, Int64) => cast_numeric_arrays::<Int8Type, Int64Type>(array, cast_options),
1534        (Int8, Float16) => cast_numeric_arrays::<Int8Type, Float16Type>(array, cast_options),
1535        (Int8, Float32) => cast_numeric_arrays::<Int8Type, Float32Type>(array, cast_options),
1536        (Int8, Float64) => cast_numeric_arrays::<Int8Type, Float64Type>(array, cast_options),
1537
1538        (Int16, UInt8) => cast_numeric_arrays::<Int16Type, UInt8Type>(array, cast_options),
1539        (Int16, UInt16) => cast_numeric_arrays::<Int16Type, UInt16Type>(array, cast_options),
1540        (Int16, UInt32) => cast_numeric_arrays::<Int16Type, UInt32Type>(array, cast_options),
1541        (Int16, UInt64) => cast_numeric_arrays::<Int16Type, UInt64Type>(array, cast_options),
1542        (Int16, Int8) => cast_numeric_arrays::<Int16Type, Int8Type>(array, cast_options),
1543        (Int16, Int32) => cast_numeric_arrays::<Int16Type, Int32Type>(array, cast_options),
1544        (Int16, Int64) => cast_numeric_arrays::<Int16Type, Int64Type>(array, cast_options),
1545        (Int16, Float16) => cast_numeric_arrays::<Int16Type, Float16Type>(array, cast_options),
1546        (Int16, Float32) => cast_numeric_arrays::<Int16Type, Float32Type>(array, cast_options),
1547        (Int16, Float64) => cast_numeric_arrays::<Int16Type, Float64Type>(array, cast_options),
1548
1549        (Int32, UInt8) => cast_numeric_arrays::<Int32Type, UInt8Type>(array, cast_options),
1550        (Int32, UInt16) => cast_numeric_arrays::<Int32Type, UInt16Type>(array, cast_options),
1551        (Int32, UInt32) => cast_numeric_arrays::<Int32Type, UInt32Type>(array, cast_options),
1552        (Int32, UInt64) => cast_numeric_arrays::<Int32Type, UInt64Type>(array, cast_options),
1553        (Int32, Int8) => cast_numeric_arrays::<Int32Type, Int8Type>(array, cast_options),
1554        (Int32, Int16) => cast_numeric_arrays::<Int32Type, Int16Type>(array, cast_options),
1555        (Int32, Int64) => cast_numeric_arrays::<Int32Type, Int64Type>(array, cast_options),
1556        (Int32, Float16) => cast_numeric_arrays::<Int32Type, Float16Type>(array, cast_options),
1557        (Int32, Float32) => cast_numeric_arrays::<Int32Type, Float32Type>(array, cast_options),
1558        (Int32, Float64) => cast_numeric_arrays::<Int32Type, Float64Type>(array, cast_options),
1559
1560        (Int64, UInt8) => cast_numeric_arrays::<Int64Type, UInt8Type>(array, cast_options),
1561        (Int64, UInt16) => cast_numeric_arrays::<Int64Type, UInt16Type>(array, cast_options),
1562        (Int64, UInt32) => cast_numeric_arrays::<Int64Type, UInt32Type>(array, cast_options),
1563        (Int64, UInt64) => cast_numeric_arrays::<Int64Type, UInt64Type>(array, cast_options),
1564        (Int64, Int8) => cast_numeric_arrays::<Int64Type, Int8Type>(array, cast_options),
1565        (Int64, Int16) => cast_numeric_arrays::<Int64Type, Int16Type>(array, cast_options),
1566        (Int64, Int32) => cast_numeric_arrays::<Int64Type, Int32Type>(array, cast_options),
1567        (Int64, Float16) => cast_numeric_arrays::<Int64Type, Float16Type>(array, cast_options),
1568        (Int64, Float32) => cast_numeric_arrays::<Int64Type, Float32Type>(array, cast_options),
1569        (Int64, Float64) => cast_numeric_arrays::<Int64Type, Float64Type>(array, cast_options),
1570
1571        (Float16, UInt8) => cast_numeric_arrays::<Float16Type, UInt8Type>(array, cast_options),
1572        (Float16, UInt16) => cast_numeric_arrays::<Float16Type, UInt16Type>(array, cast_options),
1573        (Float16, UInt32) => cast_numeric_arrays::<Float16Type, UInt32Type>(array, cast_options),
1574        (Float16, UInt64) => cast_numeric_arrays::<Float16Type, UInt64Type>(array, cast_options),
1575        (Float16, Int8) => cast_numeric_arrays::<Float16Type, Int8Type>(array, cast_options),
1576        (Float16, Int16) => cast_numeric_arrays::<Float16Type, Int16Type>(array, cast_options),
1577        (Float16, Int32) => cast_numeric_arrays::<Float16Type, Int32Type>(array, cast_options),
1578        (Float16, Int64) => cast_numeric_arrays::<Float16Type, Int64Type>(array, cast_options),
1579        (Float16, Float32) => cast_numeric_arrays::<Float16Type, Float32Type>(array, cast_options),
1580        (Float16, Float64) => cast_numeric_arrays::<Float16Type, Float64Type>(array, cast_options),
1581
1582        (Float32, UInt8) => cast_numeric_arrays::<Float32Type, UInt8Type>(array, cast_options),
1583        (Float32, UInt16) => cast_numeric_arrays::<Float32Type, UInt16Type>(array, cast_options),
1584        (Float32, UInt32) => cast_numeric_arrays::<Float32Type, UInt32Type>(array, cast_options),
1585        (Float32, UInt64) => cast_numeric_arrays::<Float32Type, UInt64Type>(array, cast_options),
1586        (Float32, Int8) => cast_numeric_arrays::<Float32Type, Int8Type>(array, cast_options),
1587        (Float32, Int16) => cast_numeric_arrays::<Float32Type, Int16Type>(array, cast_options),
1588        (Float32, Int32) => cast_numeric_arrays::<Float32Type, Int32Type>(array, cast_options),
1589        (Float32, Int64) => cast_numeric_arrays::<Float32Type, Int64Type>(array, cast_options),
1590        (Float32, Float16) => cast_numeric_arrays::<Float32Type, Float16Type>(array, cast_options),
1591        (Float32, Float64) => cast_numeric_arrays::<Float32Type, Float64Type>(array, cast_options),
1592
1593        (Float64, UInt8) => cast_numeric_arrays::<Float64Type, UInt8Type>(array, cast_options),
1594        (Float64, UInt16) => cast_numeric_arrays::<Float64Type, UInt16Type>(array, cast_options),
1595        (Float64, UInt32) => cast_numeric_arrays::<Float64Type, UInt32Type>(array, cast_options),
1596        (Float64, UInt64) => cast_numeric_arrays::<Float64Type, UInt64Type>(array, cast_options),
1597        (Float64, Int8) => cast_numeric_arrays::<Float64Type, Int8Type>(array, cast_options),
1598        (Float64, Int16) => cast_numeric_arrays::<Float64Type, Int16Type>(array, cast_options),
1599        (Float64, Int32) => cast_numeric_arrays::<Float64Type, Int32Type>(array, cast_options),
1600        (Float64, Int64) => cast_numeric_arrays::<Float64Type, Int64Type>(array, cast_options),
1601        (Float64, Float16) => cast_numeric_arrays::<Float64Type, Float16Type>(array, cast_options),
1602        (Float64, Float32) => cast_numeric_arrays::<Float64Type, Float32Type>(array, cast_options),
1603        // end numeric casts
1604
1605        // temporal casts
1606        (Int32, Date32) => cast_reinterpret_arrays::<Int32Type, Date32Type>(array),
1607        (Int32, Date64) => cast_with_options(
1608            &cast_with_options(array, &Date32, cast_options)?,
1609            &Date64,
1610            cast_options,
1611        ),
1612        (Int32, Time32(TimeUnit::Second)) => {
1613            cast_reinterpret_arrays::<Int32Type, Time32SecondType>(array)
1614        }
1615        (Int32, Time32(TimeUnit::Millisecond)) => {
1616            cast_reinterpret_arrays::<Int32Type, Time32MillisecondType>(array)
1617        }
1618        // No support for microsecond/nanosecond with i32
1619        (Date32, Int32) => cast_reinterpret_arrays::<Date32Type, Int32Type>(array),
1620        (Date32, Int64) => cast_with_options(
1621            &cast_with_options(array, &Int32, cast_options)?,
1622            &Int64,
1623            cast_options,
1624        ),
1625        (Time32(TimeUnit::Second), Int32) => {
1626            cast_reinterpret_arrays::<Time32SecondType, Int32Type>(array)
1627        }
1628        (Time32(TimeUnit::Millisecond), Int32) => {
1629            cast_reinterpret_arrays::<Time32MillisecondType, Int32Type>(array)
1630        }
1631        (Time32(TimeUnit::Second), Int64) => cast_with_options(
1632            &cast_with_options(array, &Int32, cast_options)?,
1633            &Int64,
1634            cast_options,
1635        ),
1636        (Time32(TimeUnit::Millisecond), Int64) => cast_with_options(
1637            &cast_with_options(array, &Int32, cast_options)?,
1638            &Int64,
1639            cast_options,
1640        ),
1641        (Int64, Date64) => cast_reinterpret_arrays::<Int64Type, Date64Type>(array),
1642        (Int64, Date32) => cast_with_options(
1643            &cast_with_options(array, &Int32, cast_options)?,
1644            &Date32,
1645            cast_options,
1646        ),
1647        // No support for second/milliseconds with i64
1648        (Int64, Time64(TimeUnit::Microsecond)) => {
1649            cast_reinterpret_arrays::<Int64Type, Time64MicrosecondType>(array)
1650        }
1651        (Int64, Time64(TimeUnit::Nanosecond)) => {
1652            cast_reinterpret_arrays::<Int64Type, Time64NanosecondType>(array)
1653        }
1654
1655        (Date64, Int64) => cast_reinterpret_arrays::<Date64Type, Int64Type>(array),
1656        (Date64, Int32) => cast_with_options(
1657            &cast_with_options(array, &Int64, cast_options)?,
1658            &Int32,
1659            cast_options,
1660        ),
1661        (Time64(TimeUnit::Microsecond), Int64) => {
1662            cast_reinterpret_arrays::<Time64MicrosecondType, Int64Type>(array)
1663        }
1664        (Time64(TimeUnit::Nanosecond), Int64) => {
1665            cast_reinterpret_arrays::<Time64NanosecondType, Int64Type>(array)
1666        }
1667        (Date32, Date64) => Ok(Arc::new(
1668            array
1669                .as_primitive::<Date32Type>()
1670                .unary::<_, Date64Type>(|x| x as i64 * MILLISECONDS_IN_DAY),
1671        )),
1672        (Date64, Date32) => Ok(Arc::new(
1673            array
1674                .as_primitive::<Date64Type>()
1675                .unary::<_, Date32Type>(|x| (x / MILLISECONDS_IN_DAY) as i32),
1676        )),
1677
1678        (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1679            array
1680                .as_primitive::<Time32SecondType>()
1681                .unary::<_, Time32MillisecondType>(|x| x * MILLISECONDS as i32),
1682        )),
1683        (Time32(TimeUnit::Second), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1684            array
1685                .as_primitive::<Time32SecondType>()
1686                .unary::<_, Time64MicrosecondType>(|x| x as i64 * MICROSECONDS),
1687        )),
1688        (Time32(TimeUnit::Second), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1689            array
1690                .as_primitive::<Time32SecondType>()
1691                .unary::<_, Time64NanosecondType>(|x| x as i64 * NANOSECONDS),
1692        )),
1693
1694        (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1695            array
1696                .as_primitive::<Time32MillisecondType>()
1697                .unary::<_, Time32SecondType>(|x| x / MILLISECONDS as i32),
1698        )),
1699        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1700            array
1701                .as_primitive::<Time32MillisecondType>()
1702                .unary::<_, Time64MicrosecondType>(|x| x as i64 * (MICROSECONDS / MILLISECONDS)),
1703        )),
1704        (Time32(TimeUnit::Millisecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1705            array
1706                .as_primitive::<Time32MillisecondType>()
1707                .unary::<_, Time64NanosecondType>(|x| x as i64 * (MICROSECONDS / NANOSECONDS)),
1708        )),
1709
1710        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1711            array
1712                .as_primitive::<Time64MicrosecondType>()
1713                .unary::<_, Time32SecondType>(|x| (x / MICROSECONDS) as i32),
1714        )),
1715        (Time64(TimeUnit::Microsecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1716            array
1717                .as_primitive::<Time64MicrosecondType>()
1718                .unary::<_, Time32MillisecondType>(|x| (x / (MICROSECONDS / MILLISECONDS)) as i32),
1719        )),
1720        (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => Ok(Arc::new(
1721            array
1722                .as_primitive::<Time64MicrosecondType>()
1723                .unary::<_, Time64NanosecondType>(|x| x * (NANOSECONDS / MICROSECONDS)),
1724        )),
1725
1726        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Second)) => Ok(Arc::new(
1727            array
1728                .as_primitive::<Time64NanosecondType>()
1729                .unary::<_, Time32SecondType>(|x| (x / NANOSECONDS) as i32),
1730        )),
1731        (Time64(TimeUnit::Nanosecond), Time32(TimeUnit::Millisecond)) => Ok(Arc::new(
1732            array
1733                .as_primitive::<Time64NanosecondType>()
1734                .unary::<_, Time32MillisecondType>(|x| (x / (NANOSECONDS / MILLISECONDS)) as i32),
1735        )),
1736        (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => Ok(Arc::new(
1737            array
1738                .as_primitive::<Time64NanosecondType>()
1739                .unary::<_, Time64MicrosecondType>(|x| x / (NANOSECONDS / MICROSECONDS)),
1740        )),
1741
1742        // Timestamp to integer/floating/decimals
1743        (Timestamp(TimeUnit::Second, _), _) if to_type.is_numeric() => {
1744            let array = cast_reinterpret_arrays::<TimestampSecondType, Int64Type>(array)?;
1745            cast_with_options(&array, to_type, cast_options)
1746        }
1747        (Timestamp(TimeUnit::Millisecond, _), _) if to_type.is_numeric() => {
1748            let array = cast_reinterpret_arrays::<TimestampMillisecondType, Int64Type>(array)?;
1749            cast_with_options(&array, to_type, cast_options)
1750        }
1751        (Timestamp(TimeUnit::Microsecond, _), _) if to_type.is_numeric() => {
1752            let array = cast_reinterpret_arrays::<TimestampMicrosecondType, Int64Type>(array)?;
1753            cast_with_options(&array, to_type, cast_options)
1754        }
1755        (Timestamp(TimeUnit::Nanosecond, _), _) if to_type.is_numeric() => {
1756            let array = cast_reinterpret_arrays::<TimestampNanosecondType, Int64Type>(array)?;
1757            cast_with_options(&array, to_type, cast_options)
1758        }
1759
1760        (_, Timestamp(unit, tz)) if from_type.is_numeric() => {
1761            let array = cast_with_options(array, &Int64, cast_options)?;
1762            Ok(make_timestamp_array(
1763                array.as_primitive(),
1764                *unit,
1765                tz.clone(),
1766            ))
1767        }
1768
1769        (Timestamp(from_unit, from_tz), Timestamp(to_unit, to_tz)) => {
1770            let array = cast_with_options(array, &Int64, cast_options)?;
1771            let time_array = array.as_primitive::<Int64Type>();
1772            let from_size = time_unit_multiple(from_unit);
1773            let to_size = time_unit_multiple(to_unit);
1774            // we either divide or multiply, depending on size of each unit
1775            // units are never the same when the types are the same
1776            let converted = match from_size.cmp(&to_size) {
1777                Ordering::Greater => {
1778                    let divisor = from_size / to_size;
1779                    time_array.unary::<_, Int64Type>(|o| o / divisor)
1780                }
1781                Ordering::Equal => time_array.clone(),
1782                Ordering::Less => {
1783                    let mul = to_size / from_size;
1784                    if cast_options.safe {
1785                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
1786                    } else {
1787                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
1788                    }
1789                }
1790            };
1791            // Normalize timezone
1792            let adjusted = match (from_tz, to_tz) {
1793                // Only this case needs to be adjusted because we're casting from
1794                // unknown time offset to some time offset, we want the time to be
1795                // unchanged.
1796                //
1797                // i.e. Timestamp('2001-01-01T00:00', None) -> Timestamp('2001-01-01T00:00', '+0700')
1798                (None, Some(to_tz)) => {
1799                    let to_tz: Tz = to_tz.parse()?;
1800                    match to_unit {
1801                        TimeUnit::Second => adjust_timestamp_to_timezone::<TimestampSecondType>(
1802                            converted,
1803                            &to_tz,
1804                            cast_options,
1805                        )?,
1806                        TimeUnit::Millisecond => adjust_timestamp_to_timezone::<
1807                            TimestampMillisecondType,
1808                        >(
1809                            converted, &to_tz, cast_options
1810                        )?,
1811                        TimeUnit::Microsecond => adjust_timestamp_to_timezone::<
1812                            TimestampMicrosecondType,
1813                        >(
1814                            converted, &to_tz, cast_options
1815                        )?,
1816                        TimeUnit::Nanosecond => adjust_timestamp_to_timezone::<
1817                            TimestampNanosecondType,
1818                        >(
1819                            converted, &to_tz, cast_options
1820                        )?,
1821                    }
1822                }
1823                _ => converted,
1824            };
1825            Ok(make_timestamp_array(&adjusted, *to_unit, to_tz.clone()))
1826        }
1827        (Timestamp(TimeUnit::Microsecond, _), Date32) => {
1828            timestamp_to_date32(array.as_primitive::<TimestampMicrosecondType>())
1829        }
1830        (Timestamp(TimeUnit::Millisecond, _), Date32) => {
1831            timestamp_to_date32(array.as_primitive::<TimestampMillisecondType>())
1832        }
1833        (Timestamp(TimeUnit::Second, _), Date32) => {
1834            timestamp_to_date32(array.as_primitive::<TimestampSecondType>())
1835        }
1836        (Timestamp(TimeUnit::Nanosecond, _), Date32) => {
1837            timestamp_to_date32(array.as_primitive::<TimestampNanosecondType>())
1838        }
1839        (Timestamp(TimeUnit::Second, _), Date64) => Ok(Arc::new(match cast_options.safe {
1840            true => {
1841                // change error to None
1842                array
1843                    .as_primitive::<TimestampSecondType>()
1844                    .unary_opt::<_, Date64Type>(|x| x.checked_mul(MILLISECONDS))
1845            }
1846            false => array
1847                .as_primitive::<TimestampSecondType>()
1848                .try_unary::<_, Date64Type, _>(|x| x.mul_checked(MILLISECONDS))?,
1849        })),
1850        (Timestamp(TimeUnit::Millisecond, _), Date64) => {
1851            cast_reinterpret_arrays::<TimestampMillisecondType, Date64Type>(array)
1852        }
1853        (Timestamp(TimeUnit::Microsecond, _), Date64) => Ok(Arc::new(
1854            array
1855                .as_primitive::<TimestampMicrosecondType>()
1856                .unary::<_, Date64Type>(|x| x / (MICROSECONDS / MILLISECONDS)),
1857        )),
1858        (Timestamp(TimeUnit::Nanosecond, _), Date64) => Ok(Arc::new(
1859            array
1860                .as_primitive::<TimestampNanosecondType>()
1861                .unary::<_, Date64Type>(|x| x / (NANOSECONDS / MILLISECONDS)),
1862        )),
1863        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Microsecond)) => {
1864            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1865            Ok(Arc::new(
1866                array
1867                    .as_primitive::<TimestampSecondType>()
1868                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1869                        Ok(time_to_time64us(as_time_res_with_timezone::<
1870                            TimestampSecondType,
1871                        >(x, tz)?))
1872                    })?,
1873            ))
1874        }
1875        (Timestamp(TimeUnit::Second, tz), Time64(TimeUnit::Nanosecond)) => {
1876            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1877            Ok(Arc::new(
1878                array
1879                    .as_primitive::<TimestampSecondType>()
1880                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1881                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1882                            TimestampSecondType,
1883                        >(x, tz)?))
1884                    })?,
1885            ))
1886        }
1887        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Microsecond)) => {
1888            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1889            Ok(Arc::new(
1890                array
1891                    .as_primitive::<TimestampMillisecondType>()
1892                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1893                        Ok(time_to_time64us(as_time_res_with_timezone::<
1894                            TimestampMillisecondType,
1895                        >(x, tz)?))
1896                    })?,
1897            ))
1898        }
1899        (Timestamp(TimeUnit::Millisecond, tz), Time64(TimeUnit::Nanosecond)) => {
1900            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1901            Ok(Arc::new(
1902                array
1903                    .as_primitive::<TimestampMillisecondType>()
1904                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1905                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1906                            TimestampMillisecondType,
1907                        >(x, tz)?))
1908                    })?,
1909            ))
1910        }
1911        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Microsecond)) => {
1912            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1913            Ok(Arc::new(
1914                array
1915                    .as_primitive::<TimestampMicrosecondType>()
1916                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1917                        Ok(time_to_time64us(as_time_res_with_timezone::<
1918                            TimestampMicrosecondType,
1919                        >(x, tz)?))
1920                    })?,
1921            ))
1922        }
1923        (Timestamp(TimeUnit::Microsecond, tz), Time64(TimeUnit::Nanosecond)) => {
1924            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1925            Ok(Arc::new(
1926                array
1927                    .as_primitive::<TimestampMicrosecondType>()
1928                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1929                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1930                            TimestampMicrosecondType,
1931                        >(x, tz)?))
1932                    })?,
1933            ))
1934        }
1935        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Microsecond)) => {
1936            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1937            Ok(Arc::new(
1938                array
1939                    .as_primitive::<TimestampNanosecondType>()
1940                    .try_unary::<_, Time64MicrosecondType, ArrowError>(|x| {
1941                        Ok(time_to_time64us(as_time_res_with_timezone::<
1942                            TimestampNanosecondType,
1943                        >(x, tz)?))
1944                    })?,
1945            ))
1946        }
1947        (Timestamp(TimeUnit::Nanosecond, tz), Time64(TimeUnit::Nanosecond)) => {
1948            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1949            Ok(Arc::new(
1950                array
1951                    .as_primitive::<TimestampNanosecondType>()
1952                    .try_unary::<_, Time64NanosecondType, ArrowError>(|x| {
1953                        Ok(time_to_time64ns(as_time_res_with_timezone::<
1954                            TimestampNanosecondType,
1955                        >(x, tz)?))
1956                    })?,
1957            ))
1958        }
1959        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Second)) => {
1960            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1961            Ok(Arc::new(
1962                array
1963                    .as_primitive::<TimestampSecondType>()
1964                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1965                        Ok(time_to_time32s(as_time_res_with_timezone::<
1966                            TimestampSecondType,
1967                        >(x, tz)?))
1968                    })?,
1969            ))
1970        }
1971        (Timestamp(TimeUnit::Second, tz), Time32(TimeUnit::Millisecond)) => {
1972            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1973            Ok(Arc::new(
1974                array
1975                    .as_primitive::<TimestampSecondType>()
1976                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
1977                        Ok(time_to_time32ms(as_time_res_with_timezone::<
1978                            TimestampSecondType,
1979                        >(x, tz)?))
1980                    })?,
1981            ))
1982        }
1983        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Second)) => {
1984            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1985            Ok(Arc::new(
1986                array
1987                    .as_primitive::<TimestampMillisecondType>()
1988                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
1989                        Ok(time_to_time32s(as_time_res_with_timezone::<
1990                            TimestampMillisecondType,
1991                        >(x, tz)?))
1992                    })?,
1993            ))
1994        }
1995        (Timestamp(TimeUnit::Millisecond, tz), Time32(TimeUnit::Millisecond)) => {
1996            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
1997            Ok(Arc::new(
1998                array
1999                    .as_primitive::<TimestampMillisecondType>()
2000                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2001                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2002                            TimestampMillisecondType,
2003                        >(x, tz)?))
2004                    })?,
2005            ))
2006        }
2007        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Second)) => {
2008            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2009            Ok(Arc::new(
2010                array
2011                    .as_primitive::<TimestampMicrosecondType>()
2012                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2013                        Ok(time_to_time32s(as_time_res_with_timezone::<
2014                            TimestampMicrosecondType,
2015                        >(x, tz)?))
2016                    })?,
2017            ))
2018        }
2019        (Timestamp(TimeUnit::Microsecond, tz), Time32(TimeUnit::Millisecond)) => {
2020            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2021            Ok(Arc::new(
2022                array
2023                    .as_primitive::<TimestampMicrosecondType>()
2024                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2025                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2026                            TimestampMicrosecondType,
2027                        >(x, tz)?))
2028                    })?,
2029            ))
2030        }
2031        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Second)) => {
2032            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2033            Ok(Arc::new(
2034                array
2035                    .as_primitive::<TimestampNanosecondType>()
2036                    .try_unary::<_, Time32SecondType, ArrowError>(|x| {
2037                        Ok(time_to_time32s(as_time_res_with_timezone::<
2038                            TimestampNanosecondType,
2039                        >(x, tz)?))
2040                    })?,
2041            ))
2042        }
2043        (Timestamp(TimeUnit::Nanosecond, tz), Time32(TimeUnit::Millisecond)) => {
2044            let tz = tz.as_ref().map(|tz| tz.parse()).transpose()?;
2045            Ok(Arc::new(
2046                array
2047                    .as_primitive::<TimestampNanosecondType>()
2048                    .try_unary::<_, Time32MillisecondType, ArrowError>(|x| {
2049                        Ok(time_to_time32ms(as_time_res_with_timezone::<
2050                            TimestampNanosecondType,
2051                        >(x, tz)?))
2052                    })?,
2053            ))
2054        }
2055        (Date64, Timestamp(TimeUnit::Second, _)) => {
2056            let array = array
2057                .as_primitive::<Date64Type>()
2058                .unary::<_, TimestampSecondType>(|x| x / MILLISECONDS);
2059
2060            cast_with_options(&array, to_type, cast_options)
2061        }
2062        (Date64, Timestamp(TimeUnit::Millisecond, _)) => {
2063            let array = array
2064                .as_primitive::<Date64Type>()
2065                .reinterpret_cast::<TimestampMillisecondType>();
2066
2067            cast_with_options(&array, to_type, cast_options)
2068        }
2069
2070        (Date64, Timestamp(TimeUnit::Microsecond, _)) => {
2071            let array = array
2072                .as_primitive::<Date64Type>()
2073                .unary::<_, TimestampMicrosecondType>(|x| x * (MICROSECONDS / MILLISECONDS));
2074
2075            cast_with_options(&array, to_type, cast_options)
2076        }
2077        (Date64, Timestamp(TimeUnit::Nanosecond, _)) => {
2078            let array = array
2079                .as_primitive::<Date64Type>()
2080                .unary::<_, TimestampNanosecondType>(|x| x * (NANOSECONDS / MILLISECONDS));
2081
2082            cast_with_options(&array, to_type, cast_options)
2083        }
2084        (Date32, Timestamp(TimeUnit::Second, _)) => {
2085            let array = array
2086                .as_primitive::<Date32Type>()
2087                .unary::<_, TimestampSecondType>(|x| (x as i64) * SECONDS_IN_DAY);
2088
2089            cast_with_options(&array, to_type, cast_options)
2090        }
2091        (Date32, Timestamp(TimeUnit::Millisecond, _)) => {
2092            let array = array
2093                .as_primitive::<Date32Type>()
2094                .unary::<_, TimestampMillisecondType>(|x| (x as i64) * MILLISECONDS_IN_DAY);
2095
2096            cast_with_options(&array, to_type, cast_options)
2097        }
2098        (Date32, Timestamp(TimeUnit::Microsecond, _)) => {
2099            let array = array
2100                .as_primitive::<Date32Type>()
2101                .unary::<_, TimestampMicrosecondType>(|x| (x as i64) * MICROSECONDS_IN_DAY);
2102
2103            cast_with_options(&array, to_type, cast_options)
2104        }
2105        (Date32, Timestamp(TimeUnit::Nanosecond, _)) => {
2106            let array = array
2107                .as_primitive::<Date32Type>()
2108                .unary::<_, TimestampNanosecondType>(|x| (x as i64) * NANOSECONDS_IN_DAY);
2109
2110            cast_with_options(&array, to_type, cast_options)
2111        }
2112
2113        (_, Duration(unit)) if from_type.is_numeric() => {
2114            let array = cast_with_options(array, &Int64, cast_options)?;
2115            Ok(make_duration_array(array.as_primitive(), *unit))
2116        }
2117        (Duration(TimeUnit::Second), _) if to_type.is_numeric() => {
2118            let array = cast_reinterpret_arrays::<DurationSecondType, Int64Type>(array)?;
2119            cast_with_options(&array, to_type, cast_options)
2120        }
2121        (Duration(TimeUnit::Millisecond), _) if to_type.is_numeric() => {
2122            let array = cast_reinterpret_arrays::<DurationMillisecondType, Int64Type>(array)?;
2123            cast_with_options(&array, to_type, cast_options)
2124        }
2125        (Duration(TimeUnit::Microsecond), _) if to_type.is_numeric() => {
2126            let array = cast_reinterpret_arrays::<DurationMicrosecondType, Int64Type>(array)?;
2127            cast_with_options(&array, to_type, cast_options)
2128        }
2129        (Duration(TimeUnit::Nanosecond), _) if to_type.is_numeric() => {
2130            let array = cast_reinterpret_arrays::<DurationNanosecondType, Int64Type>(array)?;
2131            cast_with_options(&array, to_type, cast_options)
2132        }
2133
2134        (Duration(from_unit), Duration(to_unit)) => {
2135            let array = cast_with_options(array, &Int64, cast_options)?;
2136            let time_array = array.as_primitive::<Int64Type>();
2137            let from_size = time_unit_multiple(from_unit);
2138            let to_size = time_unit_multiple(to_unit);
2139            // we either divide or multiply, depending on size of each unit
2140            // units are never the same when the types are the same
2141            let converted = match from_size.cmp(&to_size) {
2142                Ordering::Greater => {
2143                    let divisor = from_size / to_size;
2144                    time_array.unary::<_, Int64Type>(|o| o / divisor)
2145                }
2146                Ordering::Equal => time_array.clone(),
2147                Ordering::Less => {
2148                    let mul = to_size / from_size;
2149                    if cast_options.safe {
2150                        time_array.unary_opt::<_, Int64Type>(|o| o.checked_mul(mul))
2151                    } else {
2152                        time_array.try_unary::<_, Int64Type, _>(|o| o.mul_checked(mul))?
2153                    }
2154                }
2155            };
2156            Ok(make_duration_array(&converted, *to_unit))
2157        }
2158
2159        (Duration(TimeUnit::Second), Interval(IntervalUnit::MonthDayNano)) => {
2160            cast_duration_to_interval::<DurationSecondType>(array, cast_options)
2161        }
2162        (Duration(TimeUnit::Millisecond), Interval(IntervalUnit::MonthDayNano)) => {
2163            cast_duration_to_interval::<DurationMillisecondType>(array, cast_options)
2164        }
2165        (Duration(TimeUnit::Microsecond), Interval(IntervalUnit::MonthDayNano)) => {
2166            cast_duration_to_interval::<DurationMicrosecondType>(array, cast_options)
2167        }
2168        (Duration(TimeUnit::Nanosecond), Interval(IntervalUnit::MonthDayNano)) => {
2169            cast_duration_to_interval::<DurationNanosecondType>(array, cast_options)
2170        }
2171        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Second)) => {
2172            cast_month_day_nano_to_duration::<DurationSecondType>(array, cast_options)
2173        }
2174        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Millisecond)) => {
2175            cast_month_day_nano_to_duration::<DurationMillisecondType>(array, cast_options)
2176        }
2177        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Microsecond)) => {
2178            cast_month_day_nano_to_duration::<DurationMicrosecondType>(array, cast_options)
2179        }
2180        (Interval(IntervalUnit::MonthDayNano), Duration(TimeUnit::Nanosecond)) => {
2181            cast_month_day_nano_to_duration::<DurationNanosecondType>(array, cast_options)
2182        }
2183        (Interval(IntervalUnit::YearMonth), Interval(IntervalUnit::MonthDayNano)) => {
2184            cast_interval_year_month_to_interval_month_day_nano(array, cast_options)
2185        }
2186        (Interval(IntervalUnit::DayTime), Interval(IntervalUnit::MonthDayNano)) => {
2187            cast_interval_day_time_to_interval_month_day_nano(array, cast_options)
2188        }
2189        (Int32, Interval(IntervalUnit::YearMonth)) => {
2190            cast_reinterpret_arrays::<Int32Type, IntervalYearMonthType>(array)
2191        }
2192        (_, _) => Err(ArrowError::CastError(format!(
2193            "Casting from {from_type} to {to_type} not supported",
2194        ))),
2195    }
2196}
2197
2198fn cast_struct_to_struct(
2199    array: &StructArray,
2200    from_fields: Fields,
2201    to_fields: Fields,
2202    cast_options: &CastOptions,
2203) -> Result<ArrayRef, ArrowError> {
2204    // Fast path: if field names are in the same order, we can just zip and cast
2205    let fields_match_order = from_fields.len() == to_fields.len()
2206        && from_fields
2207            .iter()
2208            .zip(to_fields.iter())
2209            .all(|(f1, f2)| f1.name() == f2.name());
2210
2211    let fields = if fields_match_order {
2212        // Fast path: cast columns in order if their names match
2213        cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2214    } else {
2215        let all_fields_match_by_name = to_fields.iter().all(|to_field| {
2216            from_fields
2217                .iter()
2218                .any(|from_field| from_field.name() == to_field.name())
2219        });
2220
2221        if all_fields_match_by_name {
2222            // Slow path: match fields by name and reorder
2223            cast_struct_fields_by_name(array, from_fields.clone(), to_fields.clone(), cast_options)?
2224        } else {
2225            // Fallback: cast field by field in order
2226            cast_struct_fields_in_order(array, to_fields.clone(), cast_options)?
2227        }
2228    };
2229
2230    let array = StructArray::try_new(to_fields.clone(), fields, array.nulls().cloned())?;
2231    Ok(Arc::new(array) as ArrayRef)
2232}
2233
2234fn cast_struct_fields_by_name(
2235    array: &StructArray,
2236    from_fields: Fields,
2237    to_fields: Fields,
2238    cast_options: &CastOptions,
2239) -> Result<Vec<ArrayRef>, ArrowError> {
2240    to_fields
2241        .iter()
2242        .map(|to_field| {
2243            let from_field_idx = from_fields
2244                .iter()
2245                .position(|from_field| from_field.name() == to_field.name())
2246                .unwrap(); // safe because we checked above
2247            let column = array.column(from_field_idx);
2248            cast_with_options(column, to_field.data_type(), cast_options)
2249        })
2250        .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2251}
2252
2253fn cast_struct_fields_in_order(
2254    array: &StructArray,
2255    to_fields: Fields,
2256    cast_options: &CastOptions,
2257) -> Result<Vec<ArrayRef>, ArrowError> {
2258    array
2259        .columns()
2260        .iter()
2261        .zip(to_fields.iter())
2262        .map(|(l, field)| cast_with_options(l, field.data_type(), cast_options))
2263        .collect::<Result<Vec<ArrayRef>, ArrowError>>()
2264}
2265
2266fn cast_from_decimal<D, F>(
2267    array: &dyn Array,
2268    base: D::Native,
2269    scale: &i8,
2270    from_type: &DataType,
2271    to_type: &DataType,
2272    as_float: F,
2273    cast_options: &CastOptions,
2274) -> Result<ArrayRef, ArrowError>
2275where
2276    D: DecimalType + ArrowPrimitiveType,
2277    <D as ArrowPrimitiveType>::Native: ArrowNativeTypeOp + ToPrimitive,
2278    F: Fn(D::Native) -> f64,
2279{
2280    use DataType::*;
2281    // cast decimal to other type
2282    match to_type {
2283        UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, base, *scale, cast_options),
2284        UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, base, *scale, cast_options),
2285        UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, base, *scale, cast_options),
2286        UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, base, *scale, cast_options),
2287        Int8 => cast_decimal_to_integer::<D, Int8Type>(array, base, *scale, cast_options),
2288        Int16 => cast_decimal_to_integer::<D, Int16Type>(array, base, *scale, cast_options),
2289        Int32 => cast_decimal_to_integer::<D, Int32Type>(array, base, *scale, cast_options),
2290        Int64 => cast_decimal_to_integer::<D, Int64Type>(array, base, *scale, cast_options),
2291        Float32 => cast_decimal_to_float::<D, Float32Type, _>(array, |x| {
2292            (as_float(x) / 10_f64.powi(*scale as i32)) as f32
2293        }),
2294        Float64 => cast_decimal_to_float::<D, Float64Type, _>(array, |x| {
2295            as_float(x) / 10_f64.powi(*scale as i32)
2296        }),
2297        Utf8View => value_to_string_view(array, cast_options),
2298        Utf8 => value_to_string::<i32>(array, cast_options),
2299        LargeUtf8 => value_to_string::<i64>(array, cast_options),
2300        Null => Ok(new_null_array(to_type, array.len())),
2301        _ => Err(ArrowError::CastError(format!(
2302            "Casting from {from_type} to {to_type} not supported"
2303        ))),
2304    }
2305}
2306
2307fn cast_to_decimal<D, M>(
2308    array: &dyn Array,
2309    base: M,
2310    precision: &u8,
2311    scale: &i8,
2312    from_type: &DataType,
2313    to_type: &DataType,
2314    cast_options: &CastOptions,
2315) -> Result<ArrayRef, ArrowError>
2316where
2317    D: DecimalType + ArrowPrimitiveType<Native = M>,
2318    M: ArrowNativeTypeOp + DecimalCast,
2319    u8: num_traits::AsPrimitive<M>,
2320    u16: num_traits::AsPrimitive<M>,
2321    u32: num_traits::AsPrimitive<M>,
2322    u64: num_traits::AsPrimitive<M>,
2323    i8: num_traits::AsPrimitive<M>,
2324    i16: num_traits::AsPrimitive<M>,
2325    i32: num_traits::AsPrimitive<M>,
2326    i64: num_traits::AsPrimitive<M>,
2327{
2328    use DataType::*;
2329    // cast data to decimal
2330    match from_type {
2331        UInt8 => cast_integer_to_decimal::<_, D, M>(
2332            array.as_primitive::<UInt8Type>(),
2333            *precision,
2334            *scale,
2335            base,
2336            cast_options,
2337        ),
2338        UInt16 => cast_integer_to_decimal::<_, D, _>(
2339            array.as_primitive::<UInt16Type>(),
2340            *precision,
2341            *scale,
2342            base,
2343            cast_options,
2344        ),
2345        UInt32 => cast_integer_to_decimal::<_, D, _>(
2346            array.as_primitive::<UInt32Type>(),
2347            *precision,
2348            *scale,
2349            base,
2350            cast_options,
2351        ),
2352        UInt64 => cast_integer_to_decimal::<_, D, _>(
2353            array.as_primitive::<UInt64Type>(),
2354            *precision,
2355            *scale,
2356            base,
2357            cast_options,
2358        ),
2359        Int8 => cast_integer_to_decimal::<_, D, _>(
2360            array.as_primitive::<Int8Type>(),
2361            *precision,
2362            *scale,
2363            base,
2364            cast_options,
2365        ),
2366        Int16 => cast_integer_to_decimal::<_, D, _>(
2367            array.as_primitive::<Int16Type>(),
2368            *precision,
2369            *scale,
2370            base,
2371            cast_options,
2372        ),
2373        Int32 => cast_integer_to_decimal::<_, D, _>(
2374            array.as_primitive::<Int32Type>(),
2375            *precision,
2376            *scale,
2377            base,
2378            cast_options,
2379        ),
2380        Int64 => cast_integer_to_decimal::<_, D, _>(
2381            array.as_primitive::<Int64Type>(),
2382            *precision,
2383            *scale,
2384            base,
2385            cast_options,
2386        ),
2387        Float32 => cast_floating_point_to_decimal::<_, D>(
2388            array.as_primitive::<Float32Type>(),
2389            *precision,
2390            *scale,
2391            cast_options,
2392        ),
2393        Float64 => cast_floating_point_to_decimal::<_, D>(
2394            array.as_primitive::<Float64Type>(),
2395            *precision,
2396            *scale,
2397            cast_options,
2398        ),
2399        Utf8View | Utf8 => {
2400            cast_string_to_decimal::<D, i32>(array, *precision, *scale, cast_options)
2401        }
2402        LargeUtf8 => cast_string_to_decimal::<D, i64>(array, *precision, *scale, cast_options),
2403        Null => Ok(new_null_array(to_type, array.len())),
2404        _ => Err(ArrowError::CastError(format!(
2405            "Casting from {from_type} to {to_type} not supported"
2406        ))),
2407    }
2408}
2409
2410/// Get the time unit as a multiple of a second
2411const fn time_unit_multiple(unit: &TimeUnit) -> i64 {
2412    match unit {
2413        TimeUnit::Second => 1,
2414        TimeUnit::Millisecond => MILLISECONDS,
2415        TimeUnit::Microsecond => MICROSECONDS,
2416        TimeUnit::Nanosecond => NANOSECONDS,
2417    }
2418}
2419
2420/// Convert Array into a PrimitiveArray of type, and apply numeric cast
2421fn cast_numeric_arrays<FROM, TO>(
2422    from: &dyn Array,
2423    cast_options: &CastOptions,
2424) -> Result<ArrayRef, ArrowError>
2425where
2426    FROM: ArrowPrimitiveType,
2427    TO: ArrowPrimitiveType,
2428    FROM::Native: NumCast,
2429    TO::Native: NumCast,
2430{
2431    if cast_options.safe {
2432        // If the value can't be casted to the `TO::Native`, return null
2433        Ok(Arc::new(numeric_cast::<FROM, TO>(
2434            from.as_primitive::<FROM>(),
2435        )))
2436    } else {
2437        // If the value can't be casted to the `TO::Native`, return error
2438        Ok(Arc::new(try_numeric_cast::<FROM, TO>(
2439            from.as_primitive::<FROM>(),
2440        )?))
2441    }
2442}
2443
2444// Natural cast between numeric types
2445// If the value of T can't be casted to R, will throw error
2446fn try_numeric_cast<T, R>(from: &PrimitiveArray<T>) -> Result<PrimitiveArray<R>, ArrowError>
2447where
2448    T: ArrowPrimitiveType,
2449    R: ArrowPrimitiveType,
2450    T::Native: NumCast,
2451    R::Native: NumCast,
2452{
2453    from.try_unary(|value| {
2454        num_traits::cast::cast::<T::Native, R::Native>(value).ok_or_else(|| {
2455            ArrowError::CastError(format!(
2456                "Can't cast value {:?} to type {}",
2457                value,
2458                R::DATA_TYPE
2459            ))
2460        })
2461    })
2462}
2463
2464// Natural cast between numeric types
2465// If the value of T can't be casted to R, it will be converted to null
2466fn numeric_cast<T, R>(from: &PrimitiveArray<T>) -> PrimitiveArray<R>
2467where
2468    T: ArrowPrimitiveType,
2469    R: ArrowPrimitiveType,
2470    T::Native: NumCast,
2471    R::Native: NumCast,
2472{
2473    from.unary_opt::<_, R>(num_traits::cast::cast::<T::Native, R::Native>)
2474}
2475
2476fn cast_numeric_to_binary<FROM: ArrowPrimitiveType, O: OffsetSizeTrait>(
2477    array: &dyn Array,
2478) -> Result<ArrayRef, ArrowError> {
2479    let array = array.as_primitive::<FROM>();
2480    let size = std::mem::size_of::<FROM::Native>();
2481    let offsets = OffsetBuffer::from_repeated_length(size, array.len());
2482    Ok(Arc::new(GenericBinaryArray::<O>::try_new(
2483        offsets,
2484        array.values().inner().clone(),
2485        array.nulls().cloned(),
2486    )?))
2487}
2488
2489fn adjust_timestamp_to_timezone<T: ArrowTimestampType>(
2490    array: PrimitiveArray<Int64Type>,
2491    to_tz: &Tz,
2492    cast_options: &CastOptions,
2493) -> Result<PrimitiveArray<Int64Type>, ArrowError> {
2494    let adjust = |o| {
2495        let local = as_datetime::<T>(o)?;
2496        let offset = to_tz.offset_from_local_datetime(&local).single()?;
2497        T::make_value(local - offset.fix())
2498    };
2499    let adjusted = if cast_options.safe {
2500        array.unary_opt::<_, Int64Type>(adjust)
2501    } else {
2502        array.try_unary::<_, Int64Type, _>(|o| {
2503            adjust(o).ok_or_else(|| {
2504                ArrowError::CastError("Cannot cast timezone to different timezone".to_string())
2505            })
2506        })?
2507    };
2508    Ok(adjusted)
2509}
2510
2511/// Cast numeric types to Boolean
2512///
2513/// Any zero value returns `false` while non-zero returns `true`
2514fn cast_numeric_to_bool<FROM>(from: &dyn Array) -> Result<ArrayRef, ArrowError>
2515where
2516    FROM: ArrowPrimitiveType,
2517{
2518    numeric_to_bool_cast::<FROM>(from.as_primitive::<FROM>()).map(|to| Arc::new(to) as ArrayRef)
2519}
2520
2521fn numeric_to_bool_cast<T>(from: &PrimitiveArray<T>) -> Result<BooleanArray, ArrowError>
2522where
2523    T: ArrowPrimitiveType + ArrowPrimitiveType,
2524{
2525    let mut b = BooleanBuilder::with_capacity(from.len());
2526
2527    for i in 0..from.len() {
2528        if from.is_null(i) {
2529            b.append_null();
2530        } else if from.value(i) != T::default_value() {
2531            b.append_value(true);
2532        } else {
2533            b.append_value(false);
2534        }
2535    }
2536
2537    Ok(b.finish())
2538}
2539
2540/// Cast Boolean types to numeric
2541///
2542/// `false` returns 0 while `true` returns 1
2543fn cast_bool_to_numeric<TO>(
2544    from: &dyn Array,
2545    cast_options: &CastOptions,
2546) -> Result<ArrayRef, ArrowError>
2547where
2548    TO: ArrowPrimitiveType,
2549    TO::Native: num_traits::cast::NumCast,
2550{
2551    Ok(Arc::new(bool_to_numeric_cast::<TO>(
2552        from.as_any().downcast_ref::<BooleanArray>().unwrap(),
2553        cast_options,
2554    )))
2555}
2556
2557fn bool_to_numeric_cast<T>(from: &BooleanArray, _cast_options: &CastOptions) -> PrimitiveArray<T>
2558where
2559    T: ArrowPrimitiveType,
2560    T::Native: num_traits::NumCast,
2561{
2562    let iter = (0..from.len()).map(|i| {
2563        if from.is_null(i) {
2564            None
2565        } else if from.value(i) {
2566            // a workaround to cast a primitive to T::Native, infallible
2567            num_traits::cast::cast(1)
2568        } else {
2569            Some(T::default_value())
2570        }
2571    });
2572    // Benefit:
2573    //     20% performance improvement
2574    // Soundness:
2575    //     The iterator is trustedLen because it comes from a Range
2576    unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
2577}
2578
2579/// Helper function to cast from one `BinaryArray` or 'LargeBinaryArray' to 'FixedSizeBinaryArray'.
2580fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
2581    array: &dyn Array,
2582    byte_width: i32,
2583    cast_options: &CastOptions,
2584) -> Result<ArrayRef, ArrowError> {
2585    let array = array.as_binary::<O>();
2586    let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(), byte_width);
2587
2588    for i in 0..array.len() {
2589        if array.is_null(i) {
2590            builder.append_null();
2591        } else {
2592            match builder.append_value(array.value(i)) {
2593                Ok(_) => {}
2594                Err(e) => match cast_options.safe {
2595                    true => builder.append_null(),
2596                    false => return Err(e),
2597                },
2598            }
2599        }
2600    }
2601
2602    Ok(Arc::new(builder.finish()))
2603}
2604
2605/// Helper function to cast from 'FixedSizeBinaryArray' to one `BinaryArray` or 'LargeBinaryArray'.
2606/// If the target one is too large for the source array it will return an Error.
2607fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
2608    array: &dyn Array,
2609    byte_width: i32,
2610) -> Result<ArrayRef, ArrowError> {
2611    let array = array
2612        .as_any()
2613        .downcast_ref::<FixedSizeBinaryArray>()
2614        .unwrap();
2615
2616    let offsets: i128 = byte_width as i128 * array.len() as i128;
2617
2618    let is_binary = matches!(GenericBinaryType::<O>::DATA_TYPE, DataType::Binary);
2619    if is_binary && offsets > i32::MAX as i128 {
2620        return Err(ArrowError::ComputeError(
2621            "FixedSizeBinary array too large to cast to Binary array".to_string(),
2622        ));
2623    } else if !is_binary && offsets > i64::MAX as i128 {
2624        return Err(ArrowError::ComputeError(
2625            "FixedSizeBinary array too large to cast to LargeBinary array".to_string(),
2626        ));
2627    }
2628
2629    let mut builder = GenericBinaryBuilder::<O>::with_capacity(array.len(), array.len());
2630
2631    for i in 0..array.len() {
2632        if array.is_null(i) {
2633            builder.append_null();
2634        } else {
2635            builder.append_value(array.value(i));
2636        }
2637    }
2638
2639    Ok(Arc::new(builder.finish()))
2640}
2641
2642fn cast_fixed_size_binary_to_binary_view(
2643    array: &dyn Array,
2644    _byte_width: i32,
2645) -> Result<ArrayRef, ArrowError> {
2646    let array = array
2647        .as_any()
2648        .downcast_ref::<FixedSizeBinaryArray>()
2649        .unwrap();
2650
2651    let mut builder = BinaryViewBuilder::with_capacity(array.len());
2652    for i in 0..array.len() {
2653        if array.is_null(i) {
2654            builder.append_null();
2655        } else {
2656            builder.append_value(array.value(i));
2657        }
2658    }
2659
2660    Ok(Arc::new(builder.finish()))
2661}
2662
2663/// Helper function to cast from one `ByteArrayType` to another and vice versa.
2664/// If the target one (e.g., `LargeUtf8`) is too large for the source array it will return an Error.
2665fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2666where
2667    FROM: ByteArrayType,
2668    TO: ByteArrayType<Native = FROM::Native>,
2669    FROM::Offset: OffsetSizeTrait + ToPrimitive,
2670    TO::Offset: OffsetSizeTrait + NumCast,
2671{
2672    let data = array.to_data();
2673    assert_eq!(data.data_type(), &FROM::DATA_TYPE);
2674    let str_values_buf = data.buffers()[1].clone();
2675    let offsets = data.buffers()[0].typed_data::<FROM::Offset>();
2676
2677    let mut offset_builder = BufferBuilder::<TO::Offset>::new(offsets.len());
2678    offsets
2679        .iter()
2680        .try_for_each::<_, Result<_, ArrowError>>(|offset| {
2681            let offset =
2682                <<TO as ByteArrayType>::Offset as NumCast>::from(*offset).ok_or_else(|| {
2683                    ArrowError::ComputeError(format!(
2684                        "{}{} array too large to cast to {}{} array",
2685                        FROM::Offset::PREFIX,
2686                        FROM::PREFIX,
2687                        TO::Offset::PREFIX,
2688                        TO::PREFIX
2689                    ))
2690                })?;
2691            offset_builder.append(offset);
2692            Ok(())
2693        })?;
2694
2695    let offset_buffer = offset_builder.finish();
2696
2697    let dtype = TO::DATA_TYPE;
2698
2699    let builder = ArrayData::builder(dtype)
2700        .offset(array.offset())
2701        .len(array.len())
2702        .add_buffer(offset_buffer)
2703        .add_buffer(str_values_buf)
2704        .nulls(data.nulls().cloned());
2705
2706    let array_data = unsafe { builder.build_unchecked() };
2707
2708    Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
2709}
2710
2711/// Helper function to cast from one `ByteViewType` array to `ByteArrayType` array.
2712fn cast_view_to_byte<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
2713where
2714    FROM: ByteViewType,
2715    TO: ByteArrayType,
2716    FROM::Native: AsRef<TO::Native>,
2717{
2718    let data = array.to_data();
2719    let view_array = GenericByteViewArray::<FROM>::from(data);
2720
2721    let len = view_array.len();
2722    let bytes = view_array
2723        .views()
2724        .iter()
2725        .map(|v| ByteView::from(*v).length as usize)
2726        .sum::<usize>();
2727
2728    let mut byte_array_builder = GenericByteBuilder::<TO>::with_capacity(len, bytes);
2729
2730    for val in view_array.iter() {
2731        byte_array_builder.append_option(val);
2732    }
2733
2734    Ok(Arc::new(byte_array_builder.finish()))
2735}
2736
2737#[cfg(test)]
2738mod tests {
2739    use super::*;
2740    use DataType::*;
2741    use arrow_array::{Int64Array, RunArray, StringArray};
2742    use arrow_buffer::{Buffer, IntervalDayTime, NullBuffer};
2743    use arrow_buffer::{ScalarBuffer, i256};
2744    use arrow_schema::{DataType, Field};
2745    use chrono::NaiveDate;
2746    use half::f16;
2747    use std::sync::Arc;
2748
2749    #[derive(Clone)]
2750    struct DecimalCastTestConfig {
2751        input_prec: u8,
2752        input_scale: i8,
2753        input_repr: i128,
2754        output_prec: u8,
2755        output_scale: i8,
2756        expected_output_repr: Result<i128, String>, // the error variant can contain a string
2757                                                    // template where the "{}" will be
2758                                                    // replaced with the decimal type name
2759                                                    // (e.g. Decimal128)
2760    }
2761
2762    macro_rules! generate_cast_test_case {
2763        ($INPUT_ARRAY: expr, $OUTPUT_TYPE_ARRAY: ident, $OUTPUT_TYPE: expr, $OUTPUT_VALUES: expr) => {
2764            let output =
2765                $OUTPUT_TYPE_ARRAY::from($OUTPUT_VALUES).with_data_type($OUTPUT_TYPE.clone());
2766
2767            // assert cast type
2768            let input_array_type = $INPUT_ARRAY.data_type();
2769            assert!(can_cast_types(input_array_type, $OUTPUT_TYPE));
2770            let result = cast($INPUT_ARRAY, $OUTPUT_TYPE).unwrap();
2771            assert_eq!($OUTPUT_TYPE, result.data_type());
2772            assert_eq!(result.as_ref(), &output);
2773
2774            let cast_option = CastOptions {
2775                safe: false,
2776                format_options: FormatOptions::default(),
2777            };
2778            let result = cast_with_options($INPUT_ARRAY, $OUTPUT_TYPE, &cast_option).unwrap();
2779            assert_eq!($OUTPUT_TYPE, result.data_type());
2780            assert_eq!(result.as_ref(), &output);
2781        };
2782    }
2783
2784    fn run_decimal_cast_test_case<I, O>(t: DecimalCastTestConfig)
2785    where
2786        I: DecimalType,
2787        O: DecimalType,
2788        I::Native: DecimalCast,
2789        O::Native: DecimalCast,
2790    {
2791        let array = vec![I::Native::from_decimal(t.input_repr)];
2792        let array = array
2793            .into_iter()
2794            .collect::<PrimitiveArray<I>>()
2795            .with_precision_and_scale(t.input_prec, t.input_scale)
2796            .unwrap();
2797        let input_type = array.data_type();
2798        let output_type = O::TYPE_CONSTRUCTOR(t.output_prec, t.output_scale);
2799        assert!(can_cast_types(input_type, &output_type));
2800
2801        let options = CastOptions {
2802            safe: false,
2803            ..Default::default()
2804        };
2805        let result = cast_with_options(&array, &output_type, &options);
2806
2807        match t.expected_output_repr {
2808            Ok(v) => {
2809                let expected_array = vec![O::Native::from_decimal(v)];
2810                let expected_array = expected_array
2811                    .into_iter()
2812                    .collect::<PrimitiveArray<O>>()
2813                    .with_precision_and_scale(t.output_prec, t.output_scale)
2814                    .unwrap();
2815                assert_eq!(*result.unwrap(), expected_array);
2816            }
2817            Err(expected_output_message_template) => {
2818                assert!(result.is_err());
2819                let expected_error_message =
2820                    expected_output_message_template.replace("{}", O::PREFIX);
2821                assert_eq!(result.unwrap_err().to_string(), expected_error_message);
2822            }
2823        }
2824    }
2825
2826    fn create_decimal32_array(
2827        array: Vec<Option<i32>>,
2828        precision: u8,
2829        scale: i8,
2830    ) -> Result<Decimal32Array, ArrowError> {
2831        array
2832            .into_iter()
2833            .collect::<Decimal32Array>()
2834            .with_precision_and_scale(precision, scale)
2835    }
2836
2837    fn create_decimal64_array(
2838        array: Vec<Option<i64>>,
2839        precision: u8,
2840        scale: i8,
2841    ) -> Result<Decimal64Array, ArrowError> {
2842        array
2843            .into_iter()
2844            .collect::<Decimal64Array>()
2845            .with_precision_and_scale(precision, scale)
2846    }
2847
2848    fn create_decimal128_array(
2849        array: Vec<Option<i128>>,
2850        precision: u8,
2851        scale: i8,
2852    ) -> Result<Decimal128Array, ArrowError> {
2853        array
2854            .into_iter()
2855            .collect::<Decimal128Array>()
2856            .with_precision_and_scale(precision, scale)
2857    }
2858
2859    fn create_decimal256_array(
2860        array: Vec<Option<i256>>,
2861        precision: u8,
2862        scale: i8,
2863    ) -> Result<Decimal256Array, ArrowError> {
2864        array
2865            .into_iter()
2866            .collect::<Decimal256Array>()
2867            .with_precision_and_scale(precision, scale)
2868    }
2869
2870    #[test]
2871    #[cfg(not(feature = "force_validate"))]
2872    #[should_panic(
2873        expected = "Cannot cast to Decimal128(20, 3). Overflowing on 57896044618658097711785492504343953926634992332820282019728792003956564819967"
2874    )]
2875    fn test_cast_decimal_to_decimal_round_with_error() {
2876        // decimal256 to decimal128 overflow
2877        let array = vec![
2878            Some(i256::from_i128(1123454)),
2879            Some(i256::from_i128(2123456)),
2880            Some(i256::from_i128(-3123453)),
2881            Some(i256::from_i128(-3123456)),
2882            None,
2883            Some(i256::MAX),
2884            Some(i256::MIN),
2885        ];
2886        let input_decimal_array = create_decimal256_array(array, 76, 4).unwrap();
2887        let array = Arc::new(input_decimal_array) as ArrayRef;
2888        let input_type = DataType::Decimal256(76, 4);
2889        let output_type = DataType::Decimal128(20, 3);
2890        assert!(can_cast_types(&input_type, &output_type));
2891        generate_cast_test_case!(
2892            &array,
2893            Decimal128Array,
2894            &output_type,
2895            vec![
2896                Some(112345_i128),
2897                Some(212346_i128),
2898                Some(-312345_i128),
2899                Some(-312346_i128),
2900                None,
2901                None,
2902                None,
2903            ]
2904        );
2905    }
2906
2907    #[test]
2908    #[cfg(not(feature = "force_validate"))]
2909    fn test_cast_decimal_to_decimal_round() {
2910        let array = vec![
2911            Some(1123454),
2912            Some(2123456),
2913            Some(-3123453),
2914            Some(-3123456),
2915            None,
2916        ];
2917        let array = create_decimal128_array(array, 20, 4).unwrap();
2918        // decimal128 to decimal128
2919        let input_type = DataType::Decimal128(20, 4);
2920        let output_type = DataType::Decimal128(20, 3);
2921        assert!(can_cast_types(&input_type, &output_type));
2922        generate_cast_test_case!(
2923            &array,
2924            Decimal128Array,
2925            &output_type,
2926            vec![
2927                Some(112345_i128),
2928                Some(212346_i128),
2929                Some(-312345_i128),
2930                Some(-312346_i128),
2931                None
2932            ]
2933        );
2934
2935        // decimal128 to decimal256
2936        let input_type = DataType::Decimal128(20, 4);
2937        let output_type = DataType::Decimal256(20, 3);
2938        assert!(can_cast_types(&input_type, &output_type));
2939        generate_cast_test_case!(
2940            &array,
2941            Decimal256Array,
2942            &output_type,
2943            vec![
2944                Some(i256::from_i128(112345_i128)),
2945                Some(i256::from_i128(212346_i128)),
2946                Some(i256::from_i128(-312345_i128)),
2947                Some(i256::from_i128(-312346_i128)),
2948                None
2949            ]
2950        );
2951
2952        // decimal256
2953        let array = vec![
2954            Some(i256::from_i128(1123454)),
2955            Some(i256::from_i128(2123456)),
2956            Some(i256::from_i128(-3123453)),
2957            Some(i256::from_i128(-3123456)),
2958            None,
2959        ];
2960        let array = create_decimal256_array(array, 20, 4).unwrap();
2961
2962        // decimal256 to decimal256
2963        let input_type = DataType::Decimal256(20, 4);
2964        let output_type = DataType::Decimal256(20, 3);
2965        assert!(can_cast_types(&input_type, &output_type));
2966        generate_cast_test_case!(
2967            &array,
2968            Decimal256Array,
2969            &output_type,
2970            vec![
2971                Some(i256::from_i128(112345_i128)),
2972                Some(i256::from_i128(212346_i128)),
2973                Some(i256::from_i128(-312345_i128)),
2974                Some(i256::from_i128(-312346_i128)),
2975                None
2976            ]
2977        );
2978        // decimal256 to decimal128
2979        let input_type = DataType::Decimal256(20, 4);
2980        let output_type = DataType::Decimal128(20, 3);
2981        assert!(can_cast_types(&input_type, &output_type));
2982        generate_cast_test_case!(
2983            &array,
2984            Decimal128Array,
2985            &output_type,
2986            vec![
2987                Some(112345_i128),
2988                Some(212346_i128),
2989                Some(-312345_i128),
2990                Some(-312346_i128),
2991                None
2992            ]
2993        );
2994    }
2995
2996    #[test]
2997    fn test_cast_decimal32_to_decimal32() {
2998        // test changing precision
2999        let input_type = DataType::Decimal32(9, 3);
3000        let output_type = DataType::Decimal32(9, 4);
3001        assert!(can_cast_types(&input_type, &output_type));
3002        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3003        let array = create_decimal32_array(array, 9, 3).unwrap();
3004        generate_cast_test_case!(
3005            &array,
3006            Decimal32Array,
3007            &output_type,
3008            vec![
3009                Some(11234560_i32),
3010                Some(21234560_i32),
3011                Some(31234560_i32),
3012                None
3013            ]
3014        );
3015        // negative test
3016        let array = vec![Some(123456), None];
3017        let array = create_decimal32_array(array, 9, 0).unwrap();
3018        let result_safe = cast(&array, &DataType::Decimal32(2, 2));
3019        assert!(result_safe.is_ok());
3020        let options = CastOptions {
3021            safe: false,
3022            ..Default::default()
3023        };
3024
3025        let result_unsafe = cast_with_options(&array, &DataType::Decimal32(2, 2), &options);
3026        assert_eq!(
3027            "Invalid argument error: 123456.00 is too large to store in a Decimal32 of precision 2. Max is 0.99",
3028            result_unsafe.unwrap_err().to_string()
3029        );
3030    }
3031
3032    #[test]
3033    fn test_cast_decimal64_to_decimal64() {
3034        // test changing precision
3035        let input_type = DataType::Decimal64(17, 3);
3036        let output_type = DataType::Decimal64(17, 4);
3037        assert!(can_cast_types(&input_type, &output_type));
3038        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3039        let array = create_decimal64_array(array, 17, 3).unwrap();
3040        generate_cast_test_case!(
3041            &array,
3042            Decimal64Array,
3043            &output_type,
3044            vec![
3045                Some(11234560_i64),
3046                Some(21234560_i64),
3047                Some(31234560_i64),
3048                None
3049            ]
3050        );
3051        // negative test
3052        let array = vec![Some(123456), None];
3053        let array = create_decimal64_array(array, 9, 0).unwrap();
3054        let result_safe = cast(&array, &DataType::Decimal64(2, 2));
3055        assert!(result_safe.is_ok());
3056        let options = CastOptions {
3057            safe: false,
3058            ..Default::default()
3059        };
3060
3061        let result_unsafe = cast_with_options(&array, &DataType::Decimal64(2, 2), &options);
3062        assert_eq!(
3063            "Invalid argument error: 123456.00 is too large to store in a Decimal64 of precision 2. Max is 0.99",
3064            result_unsafe.unwrap_err().to_string()
3065        );
3066    }
3067
3068    #[test]
3069    fn test_cast_decimal128_to_decimal128() {
3070        // test changing precision
3071        let input_type = DataType::Decimal128(20, 3);
3072        let output_type = DataType::Decimal128(20, 4);
3073        assert!(can_cast_types(&input_type, &output_type));
3074        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3075        let array = create_decimal128_array(array, 20, 3).unwrap();
3076        generate_cast_test_case!(
3077            &array,
3078            Decimal128Array,
3079            &output_type,
3080            vec![
3081                Some(11234560_i128),
3082                Some(21234560_i128),
3083                Some(31234560_i128),
3084                None
3085            ]
3086        );
3087        // negative test
3088        let array = vec![Some(123456), None];
3089        let array = create_decimal128_array(array, 10, 0).unwrap();
3090        let result_safe = cast(&array, &DataType::Decimal128(2, 2));
3091        assert!(result_safe.is_ok());
3092        let options = CastOptions {
3093            safe: false,
3094            ..Default::default()
3095        };
3096
3097        let result_unsafe = cast_with_options(&array, &DataType::Decimal128(2, 2), &options);
3098        assert_eq!(
3099            "Invalid argument error: 123456.00 is too large to store in a Decimal128 of precision 2. Max is 0.99",
3100            result_unsafe.unwrap_err().to_string()
3101        );
3102    }
3103
3104    #[test]
3105    fn test_cast_decimal32_to_decimal32_dict() {
3106        let p = 9;
3107        let s = 3;
3108        let input_type = DataType::Decimal32(p, s);
3109        let output_type = DataType::Dictionary(
3110            Box::new(DataType::Int32),
3111            Box::new(DataType::Decimal32(p, s)),
3112        );
3113        assert!(can_cast_types(&input_type, &output_type));
3114        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3115        let array = create_decimal32_array(array, p, s).unwrap();
3116        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3117        assert_eq!(cast_array.data_type(), &output_type);
3118    }
3119
3120    #[test]
3121    fn test_cast_decimal64_to_decimal64_dict() {
3122        let p = 15;
3123        let s = 3;
3124        let input_type = DataType::Decimal64(p, s);
3125        let output_type = DataType::Dictionary(
3126            Box::new(DataType::Int32),
3127            Box::new(DataType::Decimal64(p, s)),
3128        );
3129        assert!(can_cast_types(&input_type, &output_type));
3130        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3131        let array = create_decimal64_array(array, p, s).unwrap();
3132        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3133        assert_eq!(cast_array.data_type(), &output_type);
3134    }
3135
3136    #[test]
3137    fn test_cast_decimal128_to_decimal128_dict() {
3138        let p = 20;
3139        let s = 3;
3140        let input_type = DataType::Decimal128(p, s);
3141        let output_type = DataType::Dictionary(
3142            Box::new(DataType::Int32),
3143            Box::new(DataType::Decimal128(p, s)),
3144        );
3145        assert!(can_cast_types(&input_type, &output_type));
3146        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3147        let array = create_decimal128_array(array, p, s).unwrap();
3148        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3149        assert_eq!(cast_array.data_type(), &output_type);
3150    }
3151
3152    #[test]
3153    fn test_cast_decimal256_to_decimal256_dict() {
3154        let p = 20;
3155        let s = 3;
3156        let input_type = DataType::Decimal256(p, s);
3157        let output_type = DataType::Dictionary(
3158            Box::new(DataType::Int32),
3159            Box::new(DataType::Decimal256(p, s)),
3160        );
3161        assert!(can_cast_types(&input_type, &output_type));
3162        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3163        let array = create_decimal128_array(array, p, s).unwrap();
3164        let cast_array = cast_with_options(&array, &output_type, &CastOptions::default()).unwrap();
3165        assert_eq!(cast_array.data_type(), &output_type);
3166    }
3167
3168    #[test]
3169    fn test_cast_decimal32_to_decimal32_overflow() {
3170        let input_type = DataType::Decimal32(9, 3);
3171        let output_type = DataType::Decimal32(9, 9);
3172        assert!(can_cast_types(&input_type, &output_type));
3173
3174        let array = vec![Some(i32::MAX)];
3175        let array = create_decimal32_array(array, 9, 3).unwrap();
3176        let result = cast_with_options(
3177            &array,
3178            &output_type,
3179            &CastOptions {
3180                safe: false,
3181                format_options: FormatOptions::default(),
3182            },
3183        );
3184        assert_eq!(
3185            "Cast error: Cannot cast to Decimal32(9, 9). Overflowing on 2147483647",
3186            result.unwrap_err().to_string()
3187        );
3188    }
3189
3190    #[test]
3191    fn test_cast_decimal32_to_decimal32_large_scale_reduction() {
3192        let array = vec![Some(-999999999), Some(0), Some(999999999), None];
3193        let array = create_decimal32_array(array, 9, 3).unwrap();
3194
3195        // Divide out all digits of precision -- rounding could still produce +/- 1
3196        let output_type = DataType::Decimal32(9, -6);
3197        assert!(can_cast_types(array.data_type(), &output_type));
3198        generate_cast_test_case!(
3199            &array,
3200            Decimal32Array,
3201            &output_type,
3202            vec![Some(-1), Some(0), Some(1), None]
3203        );
3204
3205        // Divide out more digits than we have precision -- all-zero result
3206        let output_type = DataType::Decimal32(9, -7);
3207        assert!(can_cast_types(array.data_type(), &output_type));
3208        generate_cast_test_case!(
3209            &array,
3210            Decimal32Array,
3211            &output_type,
3212            vec![Some(0), Some(0), Some(0), None]
3213        );
3214    }
3215
3216    #[test]
3217    fn test_cast_decimal64_to_decimal64_overflow() {
3218        let input_type = DataType::Decimal64(18, 3);
3219        let output_type = DataType::Decimal64(18, 18);
3220        assert!(can_cast_types(&input_type, &output_type));
3221
3222        let array = vec![Some(i64::MAX)];
3223        let array = create_decimal64_array(array, 18, 3).unwrap();
3224        let result = cast_with_options(
3225            &array,
3226            &output_type,
3227            &CastOptions {
3228                safe: false,
3229                format_options: FormatOptions::default(),
3230            },
3231        );
3232        assert_eq!(
3233            "Cast error: Cannot cast to Decimal64(18, 18). Overflowing on 9223372036854775807",
3234            result.unwrap_err().to_string()
3235        );
3236    }
3237
3238    #[test]
3239    fn test_cast_decimal64_to_decimal64_large_scale_reduction() {
3240        let array = vec![
3241            Some(-999999999999999999),
3242            Some(0),
3243            Some(999999999999999999),
3244            None,
3245        ];
3246        let array = create_decimal64_array(array, 18, 3).unwrap();
3247
3248        // Divide out all digits of precision -- rounding could still produce +/- 1
3249        let output_type = DataType::Decimal64(18, -15);
3250        assert!(can_cast_types(array.data_type(), &output_type));
3251        generate_cast_test_case!(
3252            &array,
3253            Decimal64Array,
3254            &output_type,
3255            vec![Some(-1), Some(0), Some(1), None]
3256        );
3257
3258        // Divide out more digits than we have precision -- all-zero result
3259        let output_type = DataType::Decimal64(18, -16);
3260        assert!(can_cast_types(array.data_type(), &output_type));
3261        generate_cast_test_case!(
3262            &array,
3263            Decimal64Array,
3264            &output_type,
3265            vec![Some(0), Some(0), Some(0), None]
3266        );
3267    }
3268
3269    #[test]
3270    fn test_cast_floating_to_decimals() {
3271        for output_type in [
3272            DataType::Decimal32(9, 3),
3273            DataType::Decimal64(9, 3),
3274            DataType::Decimal128(9, 3),
3275            DataType::Decimal256(9, 3),
3276        ] {
3277            let input_type = DataType::Float64;
3278            assert!(can_cast_types(&input_type, &output_type));
3279
3280            let array = vec![Some(1.1_f64)];
3281            let array = PrimitiveArray::<Float64Type>::from_iter(array);
3282            let result = cast_with_options(
3283                &array,
3284                &output_type,
3285                &CastOptions {
3286                    safe: false,
3287                    format_options: FormatOptions::default(),
3288                },
3289            );
3290            assert!(
3291                result.is_ok(),
3292                "Failed to cast to {output_type} with: {}",
3293                result.unwrap_err()
3294            );
3295        }
3296    }
3297
3298    #[test]
3299    fn test_cast_decimal128_to_decimal128_overflow() {
3300        let input_type = DataType::Decimal128(38, 3);
3301        let output_type = DataType::Decimal128(38, 38);
3302        assert!(can_cast_types(&input_type, &output_type));
3303
3304        let array = vec![Some(i128::MAX)];
3305        let array = create_decimal128_array(array, 38, 3).unwrap();
3306        let result = cast_with_options(
3307            &array,
3308            &output_type,
3309            &CastOptions {
3310                safe: false,
3311                format_options: FormatOptions::default(),
3312            },
3313        );
3314        assert_eq!(
3315            "Cast error: Cannot cast to Decimal128(38, 38). Overflowing on 170141183460469231731687303715884105727",
3316            result.unwrap_err().to_string()
3317        );
3318    }
3319
3320    #[test]
3321    fn test_cast_decimal128_to_decimal256_overflow() {
3322        let input_type = DataType::Decimal128(38, 3);
3323        let output_type = DataType::Decimal256(76, 76);
3324        assert!(can_cast_types(&input_type, &output_type));
3325
3326        let array = vec![Some(i128::MAX)];
3327        let array = create_decimal128_array(array, 38, 3).unwrap();
3328        let result = cast_with_options(
3329            &array,
3330            &output_type,
3331            &CastOptions {
3332                safe: false,
3333                format_options: FormatOptions::default(),
3334            },
3335        );
3336        assert_eq!(
3337            "Cast error: Cannot cast to Decimal256(76, 76). Overflowing on 170141183460469231731687303715884105727",
3338            result.unwrap_err().to_string()
3339        );
3340    }
3341
3342    #[test]
3343    fn test_cast_decimal32_to_decimal256() {
3344        let input_type = DataType::Decimal32(8, 3);
3345        let output_type = DataType::Decimal256(20, 4);
3346        assert!(can_cast_types(&input_type, &output_type));
3347        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3348        let array = create_decimal32_array(array, 8, 3).unwrap();
3349        generate_cast_test_case!(
3350            &array,
3351            Decimal256Array,
3352            &output_type,
3353            vec![
3354                Some(i256::from_i128(11234560_i128)),
3355                Some(i256::from_i128(21234560_i128)),
3356                Some(i256::from_i128(31234560_i128)),
3357                None
3358            ]
3359        );
3360    }
3361    #[test]
3362    fn test_cast_decimal64_to_decimal256() {
3363        let input_type = DataType::Decimal64(12, 3);
3364        let output_type = DataType::Decimal256(20, 4);
3365        assert!(can_cast_types(&input_type, &output_type));
3366        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3367        let array = create_decimal64_array(array, 12, 3).unwrap();
3368        generate_cast_test_case!(
3369            &array,
3370            Decimal256Array,
3371            &output_type,
3372            vec![
3373                Some(i256::from_i128(11234560_i128)),
3374                Some(i256::from_i128(21234560_i128)),
3375                Some(i256::from_i128(31234560_i128)),
3376                None
3377            ]
3378        );
3379    }
3380    #[test]
3381    fn test_cast_decimal128_to_decimal256() {
3382        let input_type = DataType::Decimal128(20, 3);
3383        let output_type = DataType::Decimal256(20, 4);
3384        assert!(can_cast_types(&input_type, &output_type));
3385        let array = vec![Some(1123456), Some(2123456), Some(3123456), None];
3386        let array = create_decimal128_array(array, 20, 3).unwrap();
3387        generate_cast_test_case!(
3388            &array,
3389            Decimal256Array,
3390            &output_type,
3391            vec![
3392                Some(i256::from_i128(11234560_i128)),
3393                Some(i256::from_i128(21234560_i128)),
3394                Some(i256::from_i128(31234560_i128)),
3395                None
3396            ]
3397        );
3398    }
3399
3400    #[test]
3401    fn test_cast_decimal256_to_decimal128_overflow() {
3402        let input_type = DataType::Decimal256(76, 5);
3403        let output_type = DataType::Decimal128(38, 7);
3404        assert!(can_cast_types(&input_type, &output_type));
3405        let array = vec![Some(i256::from_i128(i128::MAX))];
3406        let array = create_decimal256_array(array, 76, 5).unwrap();
3407        let result = cast_with_options(
3408            &array,
3409            &output_type,
3410            &CastOptions {
3411                safe: false,
3412                format_options: FormatOptions::default(),
3413            },
3414        );
3415        assert_eq!(
3416            "Cast error: Cannot cast to Decimal128(38, 7). Overflowing on 170141183460469231731687303715884105727",
3417            result.unwrap_err().to_string()
3418        );
3419    }
3420
3421    #[test]
3422    fn test_cast_decimal256_to_decimal256_overflow() {
3423        let input_type = DataType::Decimal256(76, 5);
3424        let output_type = DataType::Decimal256(76, 55);
3425        assert!(can_cast_types(&input_type, &output_type));
3426        let array = vec![Some(i256::from_i128(i128::MAX))];
3427        let array = create_decimal256_array(array, 76, 5).unwrap();
3428        let result = cast_with_options(
3429            &array,
3430            &output_type,
3431            &CastOptions {
3432                safe: false,
3433                format_options: FormatOptions::default(),
3434            },
3435        );
3436        assert_eq!(
3437            "Cast error: Cannot cast to Decimal256(76, 55). Overflowing on 170141183460469231731687303715884105727",
3438            result.unwrap_err().to_string()
3439        );
3440    }
3441
3442    #[test]
3443    fn test_cast_decimal256_to_decimal128() {
3444        let input_type = DataType::Decimal256(20, 3);
3445        let output_type = DataType::Decimal128(20, 4);
3446        assert!(can_cast_types(&input_type, &output_type));
3447        let array = vec![
3448            Some(i256::from_i128(1123456)),
3449            Some(i256::from_i128(2123456)),
3450            Some(i256::from_i128(3123456)),
3451            None,
3452        ];
3453        let array = create_decimal256_array(array, 20, 3).unwrap();
3454        generate_cast_test_case!(
3455            &array,
3456            Decimal128Array,
3457            &output_type,
3458            vec![
3459                Some(11234560_i128),
3460                Some(21234560_i128),
3461                Some(31234560_i128),
3462                None
3463            ]
3464        );
3465    }
3466
3467    #[test]
3468    fn test_cast_decimal256_to_decimal256() {
3469        let input_type = DataType::Decimal256(20, 3);
3470        let output_type = DataType::Decimal256(20, 4);
3471        assert!(can_cast_types(&input_type, &output_type));
3472        let array = vec![
3473            Some(i256::from_i128(1123456)),
3474            Some(i256::from_i128(2123456)),
3475            Some(i256::from_i128(3123456)),
3476            None,
3477        ];
3478        let array = create_decimal256_array(array, 20, 3).unwrap();
3479        generate_cast_test_case!(
3480            &array,
3481            Decimal256Array,
3482            &output_type,
3483            vec![
3484                Some(i256::from_i128(11234560_i128)),
3485                Some(i256::from_i128(21234560_i128)),
3486                Some(i256::from_i128(31234560_i128)),
3487                None
3488            ]
3489        );
3490    }
3491
3492    fn generate_decimal_to_numeric_cast_test_case<T>(array: &PrimitiveArray<T>)
3493    where
3494        T: ArrowPrimitiveType + DecimalType,
3495    {
3496        // u8
3497        generate_cast_test_case!(
3498            array,
3499            UInt8Array,
3500            &DataType::UInt8,
3501            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3502        );
3503        // u16
3504        generate_cast_test_case!(
3505            array,
3506            UInt16Array,
3507            &DataType::UInt16,
3508            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3509        );
3510        // u32
3511        generate_cast_test_case!(
3512            array,
3513            UInt32Array,
3514            &DataType::UInt32,
3515            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3516        );
3517        // u64
3518        generate_cast_test_case!(
3519            array,
3520            UInt64Array,
3521            &DataType::UInt64,
3522            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3523        );
3524        // i8
3525        generate_cast_test_case!(
3526            array,
3527            Int8Array,
3528            &DataType::Int8,
3529            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3530        );
3531        // i16
3532        generate_cast_test_case!(
3533            array,
3534            Int16Array,
3535            &DataType::Int16,
3536            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3537        );
3538        // i32
3539        generate_cast_test_case!(
3540            array,
3541            Int32Array,
3542            &DataType::Int32,
3543            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3544        );
3545        // i64
3546        generate_cast_test_case!(
3547            array,
3548            Int64Array,
3549            &DataType::Int64,
3550            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3551        );
3552        // f32
3553        generate_cast_test_case!(
3554            array,
3555            Float32Array,
3556            &DataType::Float32,
3557            vec![
3558                Some(1.25_f32),
3559                Some(2.25_f32),
3560                Some(3.25_f32),
3561                None,
3562                Some(5.25_f32)
3563            ]
3564        );
3565        // f64
3566        generate_cast_test_case!(
3567            array,
3568            Float64Array,
3569            &DataType::Float64,
3570            vec![
3571                Some(1.25_f64),
3572                Some(2.25_f64),
3573                Some(3.25_f64),
3574                None,
3575                Some(5.25_f64)
3576            ]
3577        );
3578    }
3579
3580    #[test]
3581    fn test_cast_decimal32_to_numeric() {
3582        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3583        let array = create_decimal32_array(value_array, 8, 2).unwrap();
3584
3585        generate_decimal_to_numeric_cast_test_case(&array);
3586    }
3587
3588    #[test]
3589    fn test_cast_decimal64_to_numeric() {
3590        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3591        let array = create_decimal64_array(value_array, 8, 2).unwrap();
3592
3593        generate_decimal_to_numeric_cast_test_case(&array);
3594    }
3595
3596    #[test]
3597    fn test_cast_decimal128_to_numeric() {
3598        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3599        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3600
3601        generate_decimal_to_numeric_cast_test_case(&array);
3602
3603        // overflow test: out of range of max u8
3604        let value_array: Vec<Option<i128>> = vec![Some(51300)];
3605        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3606        let casted_array = cast_with_options(
3607            &array,
3608            &DataType::UInt8,
3609            &CastOptions {
3610                safe: false,
3611                format_options: FormatOptions::default(),
3612            },
3613        );
3614        assert_eq!(
3615            "Cast error: value of 513 is out of range UInt8".to_string(),
3616            casted_array.unwrap_err().to_string()
3617        );
3618
3619        let casted_array = cast_with_options(
3620            &array,
3621            &DataType::UInt8,
3622            &CastOptions {
3623                safe: true,
3624                format_options: FormatOptions::default(),
3625            },
3626        );
3627        assert!(casted_array.is_ok());
3628        assert!(casted_array.unwrap().is_null(0));
3629
3630        // overflow test: out of range of max i8
3631        let value_array: Vec<Option<i128>> = vec![Some(24400)];
3632        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3633        let casted_array = cast_with_options(
3634            &array,
3635            &DataType::Int8,
3636            &CastOptions {
3637                safe: false,
3638                format_options: FormatOptions::default(),
3639            },
3640        );
3641        assert_eq!(
3642            "Cast error: value of 244 is out of range Int8".to_string(),
3643            casted_array.unwrap_err().to_string()
3644        );
3645
3646        let casted_array = cast_with_options(
3647            &array,
3648            &DataType::Int8,
3649            &CastOptions {
3650                safe: true,
3651                format_options: FormatOptions::default(),
3652            },
3653        );
3654        assert!(casted_array.is_ok());
3655        assert!(casted_array.unwrap().is_null(0));
3656
3657        // loss the precision: convert decimal to f32、f64
3658        // f32
3659        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3660        let value_array: Vec<Option<i128>> = vec![
3661            Some(125),
3662            Some(225),
3663            Some(325),
3664            None,
3665            Some(525),
3666            Some(112345678),
3667            Some(112345679),
3668        ];
3669        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3670        generate_cast_test_case!(
3671            &array,
3672            Float32Array,
3673            &DataType::Float32,
3674            vec![
3675                Some(1.25_f32),
3676                Some(2.25_f32),
3677                Some(3.25_f32),
3678                None,
3679                Some(5.25_f32),
3680                Some(1_123_456.7_f32),
3681                Some(1_123_456.7_f32)
3682            ]
3683        );
3684
3685        // f64
3686        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3687        let value_array: Vec<Option<i128>> = vec![
3688            Some(125),
3689            Some(225),
3690            Some(325),
3691            None,
3692            Some(525),
3693            Some(112345678901234568),
3694            Some(112345678901234560),
3695        ];
3696        let array = create_decimal128_array(value_array, 38, 2).unwrap();
3697        generate_cast_test_case!(
3698            &array,
3699            Float64Array,
3700            &DataType::Float64,
3701            vec![
3702                Some(1.25_f64),
3703                Some(2.25_f64),
3704                Some(3.25_f64),
3705                None,
3706                Some(5.25_f64),
3707                Some(1_123_456_789_012_345.6_f64),
3708                Some(1_123_456_789_012_345.6_f64),
3709            ]
3710        );
3711    }
3712
3713    #[test]
3714    fn test_cast_decimal256_to_numeric() {
3715        let value_array: Vec<Option<i256>> = vec![
3716            Some(i256::from_i128(125)),
3717            Some(i256::from_i128(225)),
3718            Some(i256::from_i128(325)),
3719            None,
3720            Some(i256::from_i128(525)),
3721        ];
3722        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3723        // u8
3724        generate_cast_test_case!(
3725            &array,
3726            UInt8Array,
3727            &DataType::UInt8,
3728            vec![Some(1_u8), Some(2_u8), Some(3_u8), None, Some(5_u8)]
3729        );
3730        // u16
3731        generate_cast_test_case!(
3732            &array,
3733            UInt16Array,
3734            &DataType::UInt16,
3735            vec![Some(1_u16), Some(2_u16), Some(3_u16), None, Some(5_u16)]
3736        );
3737        // u32
3738        generate_cast_test_case!(
3739            &array,
3740            UInt32Array,
3741            &DataType::UInt32,
3742            vec![Some(1_u32), Some(2_u32), Some(3_u32), None, Some(5_u32)]
3743        );
3744        // u64
3745        generate_cast_test_case!(
3746            &array,
3747            UInt64Array,
3748            &DataType::UInt64,
3749            vec![Some(1_u64), Some(2_u64), Some(3_u64), None, Some(5_u64)]
3750        );
3751        // i8
3752        generate_cast_test_case!(
3753            &array,
3754            Int8Array,
3755            &DataType::Int8,
3756            vec![Some(1_i8), Some(2_i8), Some(3_i8), None, Some(5_i8)]
3757        );
3758        // i16
3759        generate_cast_test_case!(
3760            &array,
3761            Int16Array,
3762            &DataType::Int16,
3763            vec![Some(1_i16), Some(2_i16), Some(3_i16), None, Some(5_i16)]
3764        );
3765        // i32
3766        generate_cast_test_case!(
3767            &array,
3768            Int32Array,
3769            &DataType::Int32,
3770            vec![Some(1_i32), Some(2_i32), Some(3_i32), None, Some(5_i32)]
3771        );
3772        // i64
3773        generate_cast_test_case!(
3774            &array,
3775            Int64Array,
3776            &DataType::Int64,
3777            vec![Some(1_i64), Some(2_i64), Some(3_i64), None, Some(5_i64)]
3778        );
3779        // f32
3780        generate_cast_test_case!(
3781            &array,
3782            Float32Array,
3783            &DataType::Float32,
3784            vec![
3785                Some(1.25_f32),
3786                Some(2.25_f32),
3787                Some(3.25_f32),
3788                None,
3789                Some(5.25_f32)
3790            ]
3791        );
3792        // f64
3793        generate_cast_test_case!(
3794            &array,
3795            Float64Array,
3796            &DataType::Float64,
3797            vec![
3798                Some(1.25_f64),
3799                Some(2.25_f64),
3800                Some(3.25_f64),
3801                None,
3802                Some(5.25_f64)
3803            ]
3804        );
3805
3806        // overflow test: out of range of max i8
3807        let value_array: Vec<Option<i256>> = vec![Some(i256::from_i128(24400))];
3808        let array = create_decimal256_array(value_array, 38, 2).unwrap();
3809        let casted_array = cast_with_options(
3810            &array,
3811            &DataType::Int8,
3812            &CastOptions {
3813                safe: false,
3814                format_options: FormatOptions::default(),
3815            },
3816        );
3817        assert_eq!(
3818            "Cast error: value of 244 is out of range Int8".to_string(),
3819            casted_array.unwrap_err().to_string()
3820        );
3821
3822        let casted_array = cast_with_options(
3823            &array,
3824            &DataType::Int8,
3825            &CastOptions {
3826                safe: true,
3827                format_options: FormatOptions::default(),
3828            },
3829        );
3830        assert!(casted_array.is_ok());
3831        assert!(casted_array.unwrap().is_null(0));
3832
3833        // loss the precision: convert decimal to f32、f64
3834        // f32
3835        // 112345678_f32 and 112345679_f32 are same, so the 112345679_f32 will lose precision.
3836        let value_array: Vec<Option<i256>> = vec![
3837            Some(i256::from_i128(125)),
3838            Some(i256::from_i128(225)),
3839            Some(i256::from_i128(325)),
3840            None,
3841            Some(i256::from_i128(525)),
3842            Some(i256::from_i128(112345678)),
3843            Some(i256::from_i128(112345679)),
3844        ];
3845        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3846        generate_cast_test_case!(
3847            &array,
3848            Float32Array,
3849            &DataType::Float32,
3850            vec![
3851                Some(1.25_f32),
3852                Some(2.25_f32),
3853                Some(3.25_f32),
3854                None,
3855                Some(5.25_f32),
3856                Some(1_123_456.7_f32),
3857                Some(1_123_456.7_f32)
3858            ]
3859        );
3860
3861        // f64
3862        // 112345678901234568_f64 and 112345678901234560_f64 are same, so the 112345678901234568_f64 will lose precision.
3863        let value_array: Vec<Option<i256>> = vec![
3864            Some(i256::from_i128(125)),
3865            Some(i256::from_i128(225)),
3866            Some(i256::from_i128(325)),
3867            None,
3868            Some(i256::from_i128(525)),
3869            Some(i256::from_i128(112345678901234568)),
3870            Some(i256::from_i128(112345678901234560)),
3871        ];
3872        let array = create_decimal256_array(value_array, 76, 2).unwrap();
3873        generate_cast_test_case!(
3874            &array,
3875            Float64Array,
3876            &DataType::Float64,
3877            vec![
3878                Some(1.25_f64),
3879                Some(2.25_f64),
3880                Some(3.25_f64),
3881                None,
3882                Some(5.25_f64),
3883                Some(1_123_456_789_012_345.6_f64),
3884                Some(1_123_456_789_012_345.6_f64),
3885            ]
3886        );
3887    }
3888
3889    #[test]
3890    fn test_cast_decimal_to_numeric_negative_scale() {
3891        let value_array: Vec<Option<i256>> = vec![
3892            Some(i256::from_i128(125)),
3893            Some(i256::from_i128(225)),
3894            Some(i256::from_i128(325)),
3895            None,
3896            Some(i256::from_i128(525)),
3897        ];
3898        let array = create_decimal256_array(value_array, 38, -1).unwrap();
3899
3900        generate_cast_test_case!(
3901            &array,
3902            Int64Array,
3903            &DataType::Int64,
3904            vec![Some(1_250), Some(2_250), Some(3_250), None, Some(5_250)]
3905        );
3906
3907        let value_array: Vec<Option<i32>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3908        let array = create_decimal32_array(value_array, 8, -2).unwrap();
3909        generate_cast_test_case!(
3910            &array,
3911            Int64Array,
3912            &DataType::Int64,
3913            vec![Some(12_500), Some(22_500), Some(32_500), None, Some(52_500)]
3914        );
3915
3916        let value_array: Vec<Option<i32>> = vec![Some(2), Some(1), None];
3917        let array = create_decimal32_array(value_array, 9, -9).unwrap();
3918        generate_cast_test_case!(
3919            &array,
3920            Int64Array,
3921            &DataType::Int64,
3922            vec![Some(2_000_000_000), Some(1_000_000_000), None]
3923        );
3924
3925        let value_array: Vec<Option<i64>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3926        let array = create_decimal64_array(value_array, 18, -3).unwrap();
3927        generate_cast_test_case!(
3928            &array,
3929            Int64Array,
3930            &DataType::Int64,
3931            vec![
3932                Some(125_000),
3933                Some(225_000),
3934                Some(325_000),
3935                None,
3936                Some(525_000)
3937            ]
3938        );
3939
3940        let value_array: Vec<Option<i64>> = vec![Some(12), Some(34), None];
3941        let array = create_decimal64_array(value_array, 18, -10).unwrap();
3942        generate_cast_test_case!(
3943            &array,
3944            Int64Array,
3945            &DataType::Int64,
3946            vec![Some(120_000_000_000), Some(340_000_000_000), None]
3947        );
3948
3949        let value_array: Vec<Option<i128>> = vec![Some(125), Some(225), Some(325), None, Some(525)];
3950        let array = create_decimal128_array(value_array, 38, -4).unwrap();
3951        generate_cast_test_case!(
3952            &array,
3953            Int64Array,
3954            &DataType::Int64,
3955            vec![
3956                Some(1_250_000),
3957                Some(2_250_000),
3958                Some(3_250_000),
3959                None,
3960                Some(5_250_000)
3961            ]
3962        );
3963
3964        let value_array: Vec<Option<i128>> = vec![Some(9), Some(1), None];
3965        let array = create_decimal128_array(value_array, 38, -18).unwrap();
3966        generate_cast_test_case!(
3967            &array,
3968            Int64Array,
3969            &DataType::Int64,
3970            vec![
3971                Some(9_000_000_000_000_000_000),
3972                Some(1_000_000_000_000_000_000),
3973                None
3974            ]
3975        );
3976
3977        let array = create_decimal32_array(vec![Some(999_999_999)], 9, -1).unwrap();
3978        let casted_array = cast_with_options(
3979            &array,
3980            &DataType::Int64,
3981            &CastOptions {
3982                safe: false,
3983                format_options: FormatOptions::default(),
3984            },
3985        );
3986        assert_eq!(
3987            "Arithmetic overflow: Overflow happened on: 999999999 * 10".to_string(),
3988            casted_array.unwrap_err().to_string()
3989        );
3990
3991        let casted_array = cast_with_options(
3992            &array,
3993            &DataType::Int64,
3994            &CastOptions {
3995                safe: true,
3996                format_options: FormatOptions::default(),
3997            },
3998        );
3999        assert!(casted_array.is_ok());
4000        assert!(casted_array.unwrap().is_null(0));
4001
4002        let array = create_decimal64_array(vec![Some(13)], 18, -1).unwrap();
4003        let casted_array = cast_with_options(
4004            &array,
4005            &DataType::Int8,
4006            &CastOptions {
4007                safe: false,
4008                format_options: FormatOptions::default(),
4009            },
4010        );
4011        assert_eq!(
4012            "Cast error: value of 130 is out of range Int8".to_string(),
4013            casted_array.unwrap_err().to_string()
4014        );
4015
4016        let casted_array = cast_with_options(
4017            &array,
4018            &DataType::Int8,
4019            &CastOptions {
4020                safe: true,
4021                format_options: FormatOptions::default(),
4022            },
4023        );
4024        assert!(casted_array.is_ok());
4025        assert!(casted_array.unwrap().is_null(0));
4026    }
4027
4028    #[test]
4029    fn test_cast_numeric_to_decimal128() {
4030        let decimal_type = DataType::Decimal128(38, 6);
4031        // u8, u16, u32, u64
4032        let input_datas = vec![
4033            Arc::new(UInt8Array::from(vec![
4034                Some(1),
4035                Some(2),
4036                Some(3),
4037                None,
4038                Some(5),
4039            ])) as ArrayRef, // u8
4040            Arc::new(UInt16Array::from(vec![
4041                Some(1),
4042                Some(2),
4043                Some(3),
4044                None,
4045                Some(5),
4046            ])) as ArrayRef, // u16
4047            Arc::new(UInt32Array::from(vec![
4048                Some(1),
4049                Some(2),
4050                Some(3),
4051                None,
4052                Some(5),
4053            ])) as ArrayRef, // u32
4054            Arc::new(UInt64Array::from(vec![
4055                Some(1),
4056                Some(2),
4057                Some(3),
4058                None,
4059                Some(5),
4060            ])) as ArrayRef, // u64
4061        ];
4062
4063        for array in input_datas {
4064            generate_cast_test_case!(
4065                &array,
4066                Decimal128Array,
4067                &decimal_type,
4068                vec![
4069                    Some(1000000_i128),
4070                    Some(2000000_i128),
4071                    Some(3000000_i128),
4072                    None,
4073                    Some(5000000_i128)
4074                ]
4075            );
4076        }
4077
4078        // i8, i16, i32, i64
4079        let input_datas = vec![
4080            Arc::new(Int8Array::from(vec![
4081                Some(1),
4082                Some(2),
4083                Some(3),
4084                None,
4085                Some(5),
4086            ])) as ArrayRef, // i8
4087            Arc::new(Int16Array::from(vec![
4088                Some(1),
4089                Some(2),
4090                Some(3),
4091                None,
4092                Some(5),
4093            ])) as ArrayRef, // i16
4094            Arc::new(Int32Array::from(vec![
4095                Some(1),
4096                Some(2),
4097                Some(3),
4098                None,
4099                Some(5),
4100            ])) as ArrayRef, // i32
4101            Arc::new(Int64Array::from(vec![
4102                Some(1),
4103                Some(2),
4104                Some(3),
4105                None,
4106                Some(5),
4107            ])) as ArrayRef, // i64
4108        ];
4109        for array in input_datas {
4110            generate_cast_test_case!(
4111                &array,
4112                Decimal128Array,
4113                &decimal_type,
4114                vec![
4115                    Some(1000000_i128),
4116                    Some(2000000_i128),
4117                    Some(3000000_i128),
4118                    None,
4119                    Some(5000000_i128)
4120                ]
4121            );
4122        }
4123
4124        // test u8 to decimal type with overflow the result type
4125        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4126        let array = UInt8Array::from(vec![1, 2, 3, 4, 100]);
4127        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4128        assert!(casted_array.is_ok());
4129        let array = casted_array.unwrap();
4130        let array: &Decimal128Array = array.as_primitive();
4131        assert!(array.is_null(4));
4132
4133        // test i8 to decimal type with overflow the result type
4134        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4135        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4136        let casted_array = cast(&array, &DataType::Decimal128(3, 1));
4137        assert!(casted_array.is_ok());
4138        let array = casted_array.unwrap();
4139        let array: &Decimal128Array = array.as_primitive();
4140        assert!(array.is_null(4));
4141
4142        // test f32 to decimal type
4143        let array = Float32Array::from(vec![
4144            Some(1.1),
4145            Some(2.2),
4146            Some(4.4),
4147            None,
4148            Some(1.123_456_4), // round down
4149            Some(1.123_456_7), // round up
4150        ]);
4151        let array = Arc::new(array) as ArrayRef;
4152        generate_cast_test_case!(
4153            &array,
4154            Decimal128Array,
4155            &decimal_type,
4156            vec![
4157                Some(1100000_i128),
4158                Some(2200000_i128),
4159                Some(4400000_i128),
4160                None,
4161                Some(1123456_i128), // round down
4162                Some(1123457_i128), // round up
4163            ]
4164        );
4165
4166        // test f64 to decimal type
4167        let array = Float64Array::from(vec![
4168            Some(1.1),
4169            Some(2.2),
4170            Some(4.4),
4171            None,
4172            Some(1.123_456_489_123_4),     // round up
4173            Some(1.123_456_789_123_4),     // round up
4174            Some(1.123_456_489_012_345_6), // round down
4175            Some(1.123_456_789_012_345_6), // round up
4176        ]);
4177        generate_cast_test_case!(
4178            &array,
4179            Decimal128Array,
4180            &decimal_type,
4181            vec![
4182                Some(1100000_i128),
4183                Some(2200000_i128),
4184                Some(4400000_i128),
4185                None,
4186                Some(1123456_i128), // round down
4187                Some(1123457_i128), // round up
4188                Some(1123456_i128), // round down
4189                Some(1123457_i128), // round up
4190            ]
4191        );
4192    }
4193
4194    #[test]
4195    fn test_cast_numeric_to_decimal256() {
4196        let decimal_type = DataType::Decimal256(76, 6);
4197        // u8, u16, u32, u64
4198        let input_datas = vec![
4199            Arc::new(UInt8Array::from(vec![
4200                Some(1),
4201                Some(2),
4202                Some(3),
4203                None,
4204                Some(5),
4205            ])) as ArrayRef, // u8
4206            Arc::new(UInt16Array::from(vec![
4207                Some(1),
4208                Some(2),
4209                Some(3),
4210                None,
4211                Some(5),
4212            ])) as ArrayRef, // u16
4213            Arc::new(UInt32Array::from(vec![
4214                Some(1),
4215                Some(2),
4216                Some(3),
4217                None,
4218                Some(5),
4219            ])) as ArrayRef, // u32
4220            Arc::new(UInt64Array::from(vec![
4221                Some(1),
4222                Some(2),
4223                Some(3),
4224                None,
4225                Some(5),
4226            ])) as ArrayRef, // u64
4227        ];
4228
4229        for array in input_datas {
4230            generate_cast_test_case!(
4231                &array,
4232                Decimal256Array,
4233                &decimal_type,
4234                vec![
4235                    Some(i256::from_i128(1000000_i128)),
4236                    Some(i256::from_i128(2000000_i128)),
4237                    Some(i256::from_i128(3000000_i128)),
4238                    None,
4239                    Some(i256::from_i128(5000000_i128))
4240                ]
4241            );
4242        }
4243
4244        // i8, i16, i32, i64
4245        let input_datas = vec![
4246            Arc::new(Int8Array::from(vec![
4247                Some(1),
4248                Some(2),
4249                Some(3),
4250                None,
4251                Some(5),
4252            ])) as ArrayRef, // i8
4253            Arc::new(Int16Array::from(vec![
4254                Some(1),
4255                Some(2),
4256                Some(3),
4257                None,
4258                Some(5),
4259            ])) as ArrayRef, // i16
4260            Arc::new(Int32Array::from(vec![
4261                Some(1),
4262                Some(2),
4263                Some(3),
4264                None,
4265                Some(5),
4266            ])) as ArrayRef, // i32
4267            Arc::new(Int64Array::from(vec![
4268                Some(1),
4269                Some(2),
4270                Some(3),
4271                None,
4272                Some(5),
4273            ])) as ArrayRef, // i64
4274        ];
4275        for array in input_datas {
4276            generate_cast_test_case!(
4277                &array,
4278                Decimal256Array,
4279                &decimal_type,
4280                vec![
4281                    Some(i256::from_i128(1000000_i128)),
4282                    Some(i256::from_i128(2000000_i128)),
4283                    Some(i256::from_i128(3000000_i128)),
4284                    None,
4285                    Some(i256::from_i128(5000000_i128))
4286                ]
4287            );
4288        }
4289
4290        // test i8 to decimal type with overflow the result type
4291        // the 100 will be converted to 1000_i128, but it is out of range for max value in the precision 3.
4292        let array = Int8Array::from(vec![1, 2, 3, 4, 100]);
4293        let array = Arc::new(array) as ArrayRef;
4294        let casted_array = cast(&array, &DataType::Decimal256(3, 1));
4295        assert!(casted_array.is_ok());
4296        let array = casted_array.unwrap();
4297        let array: &Decimal256Array = array.as_primitive();
4298        assert!(array.is_null(4));
4299
4300        // test f32 to decimal type
4301        let array = Float32Array::from(vec![
4302            Some(1.1),
4303            Some(2.2),
4304            Some(4.4),
4305            None,
4306            Some(1.123_456_4), // round down
4307            Some(1.123_456_7), // round up
4308        ]);
4309        generate_cast_test_case!(
4310            &array,
4311            Decimal256Array,
4312            &decimal_type,
4313            vec![
4314                Some(i256::from_i128(1100000_i128)),
4315                Some(i256::from_i128(2200000_i128)),
4316                Some(i256::from_i128(4400000_i128)),
4317                None,
4318                Some(i256::from_i128(1123456_i128)), // round down
4319                Some(i256::from_i128(1123457_i128)), // round up
4320            ]
4321        );
4322
4323        // test f64 to decimal type
4324        let array = Float64Array::from(vec![
4325            Some(1.1),
4326            Some(2.2),
4327            Some(4.4),
4328            None,
4329            Some(1.123_456_489_123_4),     // round down
4330            Some(1.123_456_789_123_4),     // round up
4331            Some(1.123_456_489_012_345_6), // round down
4332            Some(1.123_456_789_012_345_6), // round up
4333        ]);
4334        generate_cast_test_case!(
4335            &array,
4336            Decimal256Array,
4337            &decimal_type,
4338            vec![
4339                Some(i256::from_i128(1100000_i128)),
4340                Some(i256::from_i128(2200000_i128)),
4341                Some(i256::from_i128(4400000_i128)),
4342                None,
4343                Some(i256::from_i128(1123456_i128)), // round down
4344                Some(i256::from_i128(1123457_i128)), // round up
4345                Some(i256::from_i128(1123456_i128)), // round down
4346                Some(i256::from_i128(1123457_i128)), // round up
4347            ]
4348        );
4349    }
4350
4351    #[test]
4352    fn test_cast_i32_to_f64() {
4353        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4354        let b = cast(&array, &DataType::Float64).unwrap();
4355        let c = b.as_primitive::<Float64Type>();
4356        assert_eq!(5.0, c.value(0));
4357        assert_eq!(6.0, c.value(1));
4358        assert_eq!(7.0, c.value(2));
4359        assert_eq!(8.0, c.value(3));
4360        assert_eq!(9.0, c.value(4));
4361    }
4362
4363    #[test]
4364    fn test_cast_i32_to_u8() {
4365        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4366        let b = cast(&array, &DataType::UInt8).unwrap();
4367        let c = b.as_primitive::<UInt8Type>();
4368        assert!(!c.is_valid(0));
4369        assert_eq!(6, c.value(1));
4370        assert!(!c.is_valid(2));
4371        assert_eq!(8, c.value(3));
4372        // overflows return None
4373        assert!(!c.is_valid(4));
4374    }
4375
4376    #[test]
4377    #[should_panic(expected = "Can't cast value -5 to type UInt8")]
4378    fn test_cast_int32_to_u8_with_error() {
4379        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4380        // overflow with the error
4381        let cast_option = CastOptions {
4382            safe: false,
4383            format_options: FormatOptions::default(),
4384        };
4385        let result = cast_with_options(&array, &DataType::UInt8, &cast_option);
4386        assert!(result.is_err());
4387        result.unwrap();
4388    }
4389
4390    #[test]
4391    fn test_cast_i32_to_u8_sliced() {
4392        let array = Int32Array::from(vec![-5, 6, -7, 8, 100000000]);
4393        assert_eq!(0, array.offset());
4394        let array = array.slice(2, 3);
4395        let b = cast(&array, &DataType::UInt8).unwrap();
4396        assert_eq!(3, b.len());
4397        let c = b.as_primitive::<UInt8Type>();
4398        assert!(!c.is_valid(0));
4399        assert_eq!(8, c.value(1));
4400        // overflows return None
4401        assert!(!c.is_valid(2));
4402    }
4403
4404    #[test]
4405    fn test_cast_i32_to_i32() {
4406        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4407        let b = cast(&array, &DataType::Int32).unwrap();
4408        let c = b.as_primitive::<Int32Type>();
4409        assert_eq!(5, c.value(0));
4410        assert_eq!(6, c.value(1));
4411        assert_eq!(7, c.value(2));
4412        assert_eq!(8, c.value(3));
4413        assert_eq!(9, c.value(4));
4414    }
4415
4416    #[test]
4417    fn test_cast_i32_to_list_i32() {
4418        let array = Int32Array::from(vec![5, 6, 7, 8, 9]);
4419        let b = cast(
4420            &array,
4421            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4422        )
4423        .unwrap();
4424        assert_eq!(5, b.len());
4425        let arr = b.as_list::<i32>();
4426        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4427        assert_eq!(1, arr.value_length(0));
4428        assert_eq!(1, arr.value_length(1));
4429        assert_eq!(1, arr.value_length(2));
4430        assert_eq!(1, arr.value_length(3));
4431        assert_eq!(1, arr.value_length(4));
4432        let c = arr.values().as_primitive::<Int32Type>();
4433        assert_eq!(5, c.value(0));
4434        assert_eq!(6, c.value(1));
4435        assert_eq!(7, c.value(2));
4436        assert_eq!(8, c.value(3));
4437        assert_eq!(9, c.value(4));
4438    }
4439
4440    #[test]
4441    fn test_cast_i32_to_list_i32_nullable() {
4442        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), Some(9)]);
4443        let b = cast(
4444            &array,
4445            &DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true))),
4446        )
4447        .unwrap();
4448        assert_eq!(5, b.len());
4449        assert_eq!(0, b.null_count());
4450        let arr = b.as_list::<i32>();
4451        assert_eq!(&[0, 1, 2, 3, 4, 5], arr.value_offsets());
4452        assert_eq!(1, arr.value_length(0));
4453        assert_eq!(1, arr.value_length(1));
4454        assert_eq!(1, arr.value_length(2));
4455        assert_eq!(1, arr.value_length(3));
4456        assert_eq!(1, arr.value_length(4));
4457
4458        let c = arr.values().as_primitive::<Int32Type>();
4459        assert_eq!(1, c.null_count());
4460        assert_eq!(5, c.value(0));
4461        assert!(!c.is_valid(1));
4462        assert_eq!(7, c.value(2));
4463        assert_eq!(8, c.value(3));
4464        assert_eq!(9, c.value(4));
4465    }
4466
4467    #[test]
4468    fn test_cast_i32_to_list_f64_nullable_sliced() {
4469        let array = Int32Array::from(vec![Some(5), None, Some(7), Some(8), None, Some(10)]);
4470        let array = array.slice(2, 4);
4471        let b = cast(
4472            &array,
4473            &DataType::List(Arc::new(Field::new_list_field(DataType::Float64, true))),
4474        )
4475        .unwrap();
4476        assert_eq!(4, b.len());
4477        assert_eq!(0, b.null_count());
4478        let arr = b.as_list::<i32>();
4479        assert_eq!(&[0, 1, 2, 3, 4], arr.value_offsets());
4480        assert_eq!(1, arr.value_length(0));
4481        assert_eq!(1, arr.value_length(1));
4482        assert_eq!(1, arr.value_length(2));
4483        assert_eq!(1, arr.value_length(3));
4484        let c = arr.values().as_primitive::<Float64Type>();
4485        assert_eq!(1, c.null_count());
4486        assert_eq!(7.0, c.value(0));
4487        assert_eq!(8.0, c.value(1));
4488        assert!(!c.is_valid(2));
4489        assert_eq!(10.0, c.value(3));
4490    }
4491
4492    #[test]
4493    fn test_cast_int_to_utf8view() {
4494        let inputs = vec![
4495            Arc::new(Int8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4496            Arc::new(Int16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4497            Arc::new(Int32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4498            Arc::new(Int64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4499            Arc::new(UInt8Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4500            Arc::new(UInt16Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4501            Arc::new(UInt32Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4502            Arc::new(UInt64Array::from(vec![None, Some(8), Some(9), Some(10)])) as ArrayRef,
4503        ];
4504        let expected: ArrayRef = Arc::new(StringViewArray::from(vec![
4505            None,
4506            Some("8"),
4507            Some("9"),
4508            Some("10"),
4509        ]));
4510
4511        for array in inputs {
4512            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4513            let arr = cast(&array, &DataType::Utf8View).unwrap();
4514            assert_eq!(expected.as_ref(), arr.as_ref());
4515        }
4516    }
4517
4518    #[test]
4519    fn test_cast_float_to_utf8view() {
4520        let inputs = vec![
4521            Arc::new(Float16Array::from(vec![
4522                Some(f16::from_f64(1.5)),
4523                Some(f16::from_f64(2.5)),
4524                None,
4525            ])) as ArrayRef,
4526            Arc::new(Float32Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4527            Arc::new(Float64Array::from(vec![Some(1.5), Some(2.5), None])) as ArrayRef,
4528        ];
4529
4530        let expected: ArrayRef =
4531            Arc::new(StringViewArray::from(vec![Some("1.5"), Some("2.5"), None]));
4532
4533        for array in inputs {
4534            assert!(can_cast_types(array.data_type(), &DataType::Utf8View));
4535            let arr = cast(&array, &DataType::Utf8View).unwrap();
4536            assert_eq!(expected.as_ref(), arr.as_ref());
4537        }
4538    }
4539
4540    #[test]
4541    fn test_cast_utf8_to_i32() {
4542        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4543        let b = cast(&array, &DataType::Int32).unwrap();
4544        let c = b.as_primitive::<Int32Type>();
4545        assert_eq!(5, c.value(0));
4546        assert_eq!(6, c.value(1));
4547        assert!(!c.is_valid(2));
4548        assert_eq!(8, c.value(3));
4549        assert!(!c.is_valid(4));
4550    }
4551
4552    #[test]
4553    fn test_cast_utf8view_to_i32() {
4554        let array = StringViewArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4555        let b = cast(&array, &DataType::Int32).unwrap();
4556        let c = b.as_primitive::<Int32Type>();
4557        assert_eq!(5, c.value(0));
4558        assert_eq!(6, c.value(1));
4559        assert!(!c.is_valid(2));
4560        assert_eq!(8, c.value(3));
4561        assert!(!c.is_valid(4));
4562    }
4563
4564    #[test]
4565    fn test_cast_utf8view_to_f32() {
4566        let array = StringViewArray::from(vec!["3", "4.56", "seven", "8.9"]);
4567        let b = cast(&array, &DataType::Float32).unwrap();
4568        let c = b.as_primitive::<Float32Type>();
4569        assert_eq!(3.0, c.value(0));
4570        assert_eq!(4.56, c.value(1));
4571        assert!(!c.is_valid(2));
4572        assert_eq!(8.9, c.value(3));
4573    }
4574
4575    #[test]
4576    fn test_cast_utf8view_to_decimal128() {
4577        let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
4578        let arr = Arc::new(array) as ArrayRef;
4579        generate_cast_test_case!(
4580            &arr,
4581            Decimal128Array,
4582            &DataType::Decimal128(4, 2),
4583            vec![None, Some(400_i128), Some(560_i128), Some(789_i128)]
4584        );
4585    }
4586
4587    #[test]
4588    fn test_cast_with_options_utf8_to_i32() {
4589        let array = StringArray::from(vec!["5", "6", "seven", "8", "9.1"]);
4590        let result = cast_with_options(
4591            &array,
4592            &DataType::Int32,
4593            &CastOptions {
4594                safe: false,
4595                format_options: FormatOptions::default(),
4596            },
4597        );
4598        match result {
4599            Ok(_) => panic!("expected error"),
4600            Err(e) => {
4601                assert!(
4602                    e.to_string()
4603                        .contains("Cast error: Cannot cast string 'seven' to value of Int32 type",),
4604                    "Error: {e}"
4605                )
4606            }
4607        }
4608    }
4609
4610    #[test]
4611    fn test_cast_utf8_to_bool() {
4612        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4613        let casted = cast(&strings, &DataType::Boolean).unwrap();
4614        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4615        assert_eq!(*as_boolean_array(&casted), expected);
4616    }
4617
4618    #[test]
4619    fn test_cast_utf8view_to_bool() {
4620        let strings = StringViewArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4621        let casted = cast(&strings, &DataType::Boolean).unwrap();
4622        let expected = BooleanArray::from(vec![Some(true), Some(false), None, Some(true), None]);
4623        assert_eq!(*as_boolean_array(&casted), expected);
4624    }
4625
4626    #[test]
4627    fn test_cast_with_options_utf8_to_bool() {
4628        let strings = StringArray::from(vec!["true", "false", "invalid", " Y ", ""]);
4629        let casted = cast_with_options(
4630            &strings,
4631            &DataType::Boolean,
4632            &CastOptions {
4633                safe: false,
4634                format_options: FormatOptions::default(),
4635            },
4636        );
4637        match casted {
4638            Ok(_) => panic!("expected error"),
4639            Err(e) => {
4640                assert!(
4641                    e.to_string().contains(
4642                        "Cast error: Cannot cast value 'invalid' to value of Boolean type"
4643                    )
4644                )
4645            }
4646        }
4647    }
4648
4649    #[test]
4650    fn test_cast_bool_to_i32() {
4651        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4652        let b = cast(&array, &DataType::Int32).unwrap();
4653        let c = b.as_primitive::<Int32Type>();
4654        assert_eq!(1, c.value(0));
4655        assert_eq!(0, c.value(1));
4656        assert!(!c.is_valid(2));
4657    }
4658
4659    #[test]
4660    fn test_cast_bool_to_utf8view() {
4661        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4662        let b = cast(&array, &DataType::Utf8View).unwrap();
4663        let c = b.as_any().downcast_ref::<StringViewArray>().unwrap();
4664        assert_eq!("true", c.value(0));
4665        assert_eq!("false", c.value(1));
4666        assert!(!c.is_valid(2));
4667    }
4668
4669    #[test]
4670    fn test_cast_bool_to_utf8() {
4671        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4672        let b = cast(&array, &DataType::Utf8).unwrap();
4673        let c = b.as_any().downcast_ref::<StringArray>().unwrap();
4674        assert_eq!("true", c.value(0));
4675        assert_eq!("false", c.value(1));
4676        assert!(!c.is_valid(2));
4677    }
4678
4679    #[test]
4680    fn test_cast_bool_to_large_utf8() {
4681        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4682        let b = cast(&array, &DataType::LargeUtf8).unwrap();
4683        let c = b.as_any().downcast_ref::<LargeStringArray>().unwrap();
4684        assert_eq!("true", c.value(0));
4685        assert_eq!("false", c.value(1));
4686        assert!(!c.is_valid(2));
4687    }
4688
4689    #[test]
4690    fn test_cast_bool_to_f64() {
4691        let array = BooleanArray::from(vec![Some(true), Some(false), None]);
4692        let b = cast(&array, &DataType::Float64).unwrap();
4693        let c = b.as_primitive::<Float64Type>();
4694        assert_eq!(1.0, c.value(0));
4695        assert_eq!(0.0, c.value(1));
4696        assert!(!c.is_valid(2));
4697    }
4698
4699    #[test]
4700    fn test_cast_integer_to_timestamp() {
4701        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4702        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4703
4704        let array = Int8Array::from(vec![Some(2), Some(10), None]);
4705        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4706
4707        assert_eq!(&actual, &expected);
4708
4709        let array = Int16Array::from(vec![Some(2), Some(10), None]);
4710        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4711
4712        assert_eq!(&actual, &expected);
4713
4714        let array = Int32Array::from(vec![Some(2), Some(10), None]);
4715        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4716
4717        assert_eq!(&actual, &expected);
4718
4719        let array = UInt8Array::from(vec![Some(2), Some(10), None]);
4720        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4721
4722        assert_eq!(&actual, &expected);
4723
4724        let array = UInt16Array::from(vec![Some(2), Some(10), None]);
4725        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4726
4727        assert_eq!(&actual, &expected);
4728
4729        let array = UInt32Array::from(vec![Some(2), Some(10), None]);
4730        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4731
4732        assert_eq!(&actual, &expected);
4733
4734        let array = UInt64Array::from(vec![Some(2), Some(10), None]);
4735        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4736
4737        assert_eq!(&actual, &expected);
4738    }
4739
4740    #[test]
4741    fn test_cast_timestamp_to_integer() {
4742        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4743            .with_timezone("UTC".to_string());
4744        let expected = cast(&array, &DataType::Int64).unwrap();
4745
4746        let actual = cast(&cast(&array, &DataType::Int8).unwrap(), &DataType::Int64).unwrap();
4747        assert_eq!(&actual, &expected);
4748
4749        let actual = cast(&cast(&array, &DataType::Int16).unwrap(), &DataType::Int64).unwrap();
4750        assert_eq!(&actual, &expected);
4751
4752        let actual = cast(&cast(&array, &DataType::Int32).unwrap(), &DataType::Int64).unwrap();
4753        assert_eq!(&actual, &expected);
4754
4755        let actual = cast(&cast(&array, &DataType::UInt8).unwrap(), &DataType::Int64).unwrap();
4756        assert_eq!(&actual, &expected);
4757
4758        let actual = cast(&cast(&array, &DataType::UInt16).unwrap(), &DataType::Int64).unwrap();
4759        assert_eq!(&actual, &expected);
4760
4761        let actual = cast(&cast(&array, &DataType::UInt32).unwrap(), &DataType::Int64).unwrap();
4762        assert_eq!(&actual, &expected);
4763
4764        let actual = cast(&cast(&array, &DataType::UInt64).unwrap(), &DataType::Int64).unwrap();
4765        assert_eq!(&actual, &expected);
4766    }
4767
4768    #[test]
4769    fn test_cast_floating_to_timestamp() {
4770        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4771        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4772
4773        let array = Float16Array::from(vec![
4774            Some(f16::from_f32(2.0)),
4775            Some(f16::from_f32(10.6)),
4776            None,
4777        ]);
4778        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4779
4780        assert_eq!(&actual, &expected);
4781
4782        let array = Float32Array::from(vec![Some(2.0), Some(10.6), None]);
4783        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4784
4785        assert_eq!(&actual, &expected);
4786
4787        let array = Float64Array::from(vec![Some(2.1), Some(10.2), None]);
4788        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4789
4790        assert_eq!(&actual, &expected);
4791    }
4792
4793    #[test]
4794    fn test_cast_timestamp_to_floating() {
4795        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4796            .with_timezone("UTC".to_string());
4797        let expected = cast(&array, &DataType::Int64).unwrap();
4798
4799        let actual = cast(&cast(&array, &DataType::Float16).unwrap(), &DataType::Int64).unwrap();
4800        assert_eq!(&actual, &expected);
4801
4802        let actual = cast(&cast(&array, &DataType::Float32).unwrap(), &DataType::Int64).unwrap();
4803        assert_eq!(&actual, &expected);
4804
4805        let actual = cast(&cast(&array, &DataType::Float64).unwrap(), &DataType::Int64).unwrap();
4806        assert_eq!(&actual, &expected);
4807    }
4808
4809    #[test]
4810    fn test_cast_decimal_to_timestamp() {
4811        let array = Int64Array::from(vec![Some(2), Some(10), None]);
4812        let expected = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4813
4814        let array = Decimal128Array::from(vec![Some(200), Some(1000), None])
4815            .with_precision_and_scale(4, 2)
4816            .unwrap();
4817        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4818
4819        assert_eq!(&actual, &expected);
4820
4821        let array = Decimal256Array::from(vec![
4822            Some(i256::from_i128(2000)),
4823            Some(i256::from_i128(10000)),
4824            None,
4825        ])
4826        .with_precision_and_scale(5, 3)
4827        .unwrap();
4828        let actual = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
4829
4830        assert_eq!(&actual, &expected);
4831    }
4832
4833    #[test]
4834    fn test_cast_timestamp_to_decimal() {
4835        let array = TimestampMillisecondArray::from(vec![Some(5), Some(1), None])
4836            .with_timezone("UTC".to_string());
4837        let expected = cast(&array, &DataType::Int64).unwrap();
4838
4839        let actual = cast(
4840            &cast(&array, &DataType::Decimal128(5, 2)).unwrap(),
4841            &DataType::Int64,
4842        )
4843        .unwrap();
4844        assert_eq!(&actual, &expected);
4845
4846        let actual = cast(
4847            &cast(&array, &DataType::Decimal256(10, 5)).unwrap(),
4848            &DataType::Int64,
4849        )
4850        .unwrap();
4851        assert_eq!(&actual, &expected);
4852    }
4853
4854    #[test]
4855    fn test_cast_list_i32_to_list_u16() {
4856        let values = vec![
4857            Some(vec![Some(0), Some(0), Some(0)]),
4858            Some(vec![Some(-1), Some(-2), Some(-1)]),
4859            Some(vec![Some(2), Some(100000000)]),
4860        ];
4861        let list_array = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
4862
4863        let target_type = DataType::List(Arc::new(Field::new("item", DataType::UInt16, true)));
4864        assert!(can_cast_types(list_array.data_type(), &target_type));
4865        let cast_array = cast(&list_array, &target_type).unwrap();
4866
4867        // For the ListArray itself, there are no null values (as there were no nulls when they went in)
4868        //
4869        // 3 negative values should get lost when casting to unsigned,
4870        // 1 value should overflow
4871        assert_eq!(0, cast_array.null_count());
4872
4873        // offsets should be the same
4874        let array = cast_array.as_list::<i32>();
4875        assert_eq!(list_array.value_offsets(), array.value_offsets());
4876
4877        assert_eq!(DataType::UInt16, array.value_type());
4878        assert_eq!(3, array.value_length(0));
4879        assert_eq!(3, array.value_length(1));
4880        assert_eq!(2, array.value_length(2));
4881
4882        // expect 4 nulls: negative numbers and overflow
4883        let u16arr = array.values().as_primitive::<UInt16Type>();
4884        assert_eq!(4, u16arr.null_count());
4885
4886        // expect 4 nulls: negative numbers and overflow
4887        let expected: UInt16Array =
4888            vec![Some(0), Some(0), Some(0), None, None, None, Some(2), None]
4889                .into_iter()
4890                .collect();
4891
4892        assert_eq!(u16arr, &expected);
4893    }
4894
4895    #[test]
4896    fn test_cast_list_i32_to_list_timestamp() {
4897        // Construct a value array
4898        let value_data = Int32Array::from(vec![0, 0, 0, -1, -2, -1, 2, 8, 100000000]).into_data();
4899
4900        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 9]);
4901
4902        // Construct a list array from the above two
4903        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
4904        let list_data = ArrayData::builder(list_data_type)
4905            .len(3)
4906            .add_buffer(value_offsets)
4907            .add_child_data(value_data)
4908            .build()
4909            .unwrap();
4910        let list_array = Arc::new(ListArray::from(list_data)) as ArrayRef;
4911
4912        let actual = cast(
4913            &list_array,
4914            &DataType::List(Arc::new(Field::new_list_field(
4915                DataType::Timestamp(TimeUnit::Microsecond, None),
4916                true,
4917            ))),
4918        )
4919        .unwrap();
4920
4921        let expected = cast(
4922            &cast(
4923                &list_array,
4924                &DataType::List(Arc::new(Field::new_list_field(DataType::Int64, true))),
4925            )
4926            .unwrap(),
4927            &DataType::List(Arc::new(Field::new_list_field(
4928                DataType::Timestamp(TimeUnit::Microsecond, None),
4929                true,
4930            ))),
4931        )
4932        .unwrap();
4933
4934        assert_eq!(&actual, &expected);
4935    }
4936
4937    #[test]
4938    fn test_cast_date32_to_date64() {
4939        let a = Date32Array::from(vec![10000, 17890]);
4940        let array = Arc::new(a) as ArrayRef;
4941        let b = cast(&array, &DataType::Date64).unwrap();
4942        let c = b.as_primitive::<Date64Type>();
4943        assert_eq!(864000000000, c.value(0));
4944        assert_eq!(1545696000000, c.value(1));
4945    }
4946
4947    #[test]
4948    fn test_cast_date64_to_date32() {
4949        let a = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
4950        let array = Arc::new(a) as ArrayRef;
4951        let b = cast(&array, &DataType::Date32).unwrap();
4952        let c = b.as_primitive::<Date32Type>();
4953        assert_eq!(10000, c.value(0));
4954        assert_eq!(17890, c.value(1));
4955        assert!(c.is_null(2));
4956    }
4957
4958    #[test]
4959    fn test_cast_string_to_integral_overflow() {
4960        let str = Arc::new(StringArray::from(vec![
4961            Some("123"),
4962            Some("-123"),
4963            Some("86374"),
4964            None,
4965        ])) as ArrayRef;
4966
4967        let options = CastOptions {
4968            safe: true,
4969            format_options: FormatOptions::default(),
4970        };
4971        let res = cast_with_options(&str, &DataType::Int16, &options).expect("should cast to i16");
4972        let expected =
4973            Arc::new(Int16Array::from(vec![Some(123), Some(-123), None, None])) as ArrayRef;
4974        assert_eq!(&res, &expected);
4975    }
4976
4977    #[test]
4978    fn test_cast_string_to_timestamp() {
4979        let a0 = Arc::new(StringViewArray::from(vec![
4980            Some("2020-09-08T12:00:00.123456789+00:00"),
4981            Some("Not a valid date"),
4982            None,
4983        ])) as ArrayRef;
4984        let a1 = Arc::new(StringArray::from(vec![
4985            Some("2020-09-08T12:00:00.123456789+00:00"),
4986            Some("Not a valid date"),
4987            None,
4988        ])) as ArrayRef;
4989        let a2 = Arc::new(LargeStringArray::from(vec![
4990            Some("2020-09-08T12:00:00.123456789+00:00"),
4991            Some("Not a valid date"),
4992            None,
4993        ])) as ArrayRef;
4994        for array in &[a0, a1, a2] {
4995            for time_unit in &[
4996                TimeUnit::Second,
4997                TimeUnit::Millisecond,
4998                TimeUnit::Microsecond,
4999                TimeUnit::Nanosecond,
5000            ] {
5001                let to_type = DataType::Timestamp(*time_unit, None);
5002                let b = cast(array, &to_type).unwrap();
5003
5004                match time_unit {
5005                    TimeUnit::Second => {
5006                        let c = b.as_primitive::<TimestampSecondType>();
5007                        assert_eq!(1599566400, c.value(0));
5008                        assert!(c.is_null(1));
5009                        assert!(c.is_null(2));
5010                    }
5011                    TimeUnit::Millisecond => {
5012                        let c = b
5013                            .as_any()
5014                            .downcast_ref::<TimestampMillisecondArray>()
5015                            .unwrap();
5016                        assert_eq!(1599566400123, c.value(0));
5017                        assert!(c.is_null(1));
5018                        assert!(c.is_null(2));
5019                    }
5020                    TimeUnit::Microsecond => {
5021                        let c = b
5022                            .as_any()
5023                            .downcast_ref::<TimestampMicrosecondArray>()
5024                            .unwrap();
5025                        assert_eq!(1599566400123456, c.value(0));
5026                        assert!(c.is_null(1));
5027                        assert!(c.is_null(2));
5028                    }
5029                    TimeUnit::Nanosecond => {
5030                        let c = b
5031                            .as_any()
5032                            .downcast_ref::<TimestampNanosecondArray>()
5033                            .unwrap();
5034                        assert_eq!(1599566400123456789, c.value(0));
5035                        assert!(c.is_null(1));
5036                        assert!(c.is_null(2));
5037                    }
5038                }
5039
5040                let options = CastOptions {
5041                    safe: false,
5042                    format_options: FormatOptions::default(),
5043                };
5044                let err = cast_with_options(array, &to_type, &options).unwrap_err();
5045                assert_eq!(
5046                    err.to_string(),
5047                    "Parser error: Error parsing timestamp from 'Not a valid date': error parsing date"
5048                );
5049            }
5050        }
5051    }
5052
5053    #[test]
5054    fn test_cast_string_to_timestamp_overflow() {
5055        let array = StringArray::from(vec!["9800-09-08T12:00:00.123456789"]);
5056        let result = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
5057        let result = result.as_primitive::<TimestampSecondType>();
5058        assert_eq!(result.values(), &[247112596800]);
5059    }
5060
5061    #[test]
5062    fn test_cast_string_to_date32() {
5063        let a0 = Arc::new(StringViewArray::from(vec![
5064            Some("2018-12-25"),
5065            Some("Not a valid date"),
5066            None,
5067        ])) as ArrayRef;
5068        let a1 = Arc::new(StringArray::from(vec![
5069            Some("2018-12-25"),
5070            Some("Not a valid date"),
5071            None,
5072        ])) as ArrayRef;
5073        let a2 = Arc::new(LargeStringArray::from(vec![
5074            Some("2018-12-25"),
5075            Some("Not a valid date"),
5076            None,
5077        ])) as ArrayRef;
5078        for array in &[a0, a1, a2] {
5079            let to_type = DataType::Date32;
5080            let b = cast(array, &to_type).unwrap();
5081            let c = b.as_primitive::<Date32Type>();
5082            assert_eq!(17890, c.value(0));
5083            assert!(c.is_null(1));
5084            assert!(c.is_null(2));
5085
5086            let options = CastOptions {
5087                safe: false,
5088                format_options: FormatOptions::default(),
5089            };
5090            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5091            assert_eq!(
5092                err.to_string(),
5093                "Cast error: Cannot cast string 'Not a valid date' to value of Date32 type"
5094            );
5095        }
5096    }
5097
5098    #[test]
5099    fn test_cast_string_with_large_date_to_date32() {
5100        let array = Arc::new(StringArray::from(vec![
5101            Some("+10999-12-31"),
5102            Some("-0010-02-28"),
5103            Some("0010-02-28"),
5104            Some("0000-01-01"),
5105            Some("-0000-01-01"),
5106            Some("-0001-01-01"),
5107        ])) as ArrayRef;
5108        let to_type = DataType::Date32;
5109        let options = CastOptions {
5110            safe: false,
5111            format_options: FormatOptions::default(),
5112        };
5113        let b = cast_with_options(&array, &to_type, &options).unwrap();
5114        let c = b.as_primitive::<Date32Type>();
5115        assert_eq!(3298139, c.value(0)); // 10999-12-31
5116        assert_eq!(-723122, c.value(1)); // -0010-02-28
5117        assert_eq!(-715817, c.value(2)); // 0010-02-28
5118        assert_eq!(c.value(3), c.value(4)); // Expect 0000-01-01 and -0000-01-01 to be parsed the same
5119        assert_eq!(-719528, c.value(3)); // 0000-01-01
5120        assert_eq!(-719528, c.value(4)); // -0000-01-01
5121        assert_eq!(-719893, c.value(5)); // -0001-01-01
5122    }
5123
5124    #[test]
5125    fn test_cast_invalid_string_with_large_date_to_date32() {
5126        // Large dates need to be prefixed with a + or - sign, otherwise they are not parsed correctly
5127        let array = Arc::new(StringArray::from(vec![Some("10999-12-31")])) as ArrayRef;
5128        let to_type = DataType::Date32;
5129        let options = CastOptions {
5130            safe: false,
5131            format_options: FormatOptions::default(),
5132        };
5133        let err = cast_with_options(&array, &to_type, &options).unwrap_err();
5134        assert_eq!(
5135            err.to_string(),
5136            "Cast error: Cannot cast string '10999-12-31' to value of Date32 type"
5137        );
5138    }
5139
5140    #[test]
5141    fn test_cast_string_format_yyyymmdd_to_date32() {
5142        let a0 = Arc::new(StringViewArray::from(vec![
5143            Some("2020-12-25"),
5144            Some("20201117"),
5145        ])) as ArrayRef;
5146        let a1 = Arc::new(StringArray::from(vec![
5147            Some("2020-12-25"),
5148            Some("20201117"),
5149        ])) as ArrayRef;
5150        let a2 = Arc::new(LargeStringArray::from(vec![
5151            Some("2020-12-25"),
5152            Some("20201117"),
5153        ])) as ArrayRef;
5154
5155        for array in &[a0, a1, a2] {
5156            let to_type = DataType::Date32;
5157            let options = CastOptions {
5158                safe: false,
5159                format_options: FormatOptions::default(),
5160            };
5161            let result = cast_with_options(&array, &to_type, &options).unwrap();
5162            let c = result.as_primitive::<Date32Type>();
5163            assert_eq!(
5164                chrono::NaiveDate::from_ymd_opt(2020, 12, 25),
5165                c.value_as_date(0)
5166            );
5167            assert_eq!(
5168                chrono::NaiveDate::from_ymd_opt(2020, 11, 17),
5169                c.value_as_date(1)
5170            );
5171        }
5172    }
5173
5174    #[test]
5175    fn test_cast_string_to_time32second() {
5176        let a0 = Arc::new(StringViewArray::from(vec![
5177            Some("08:08:35.091323414"),
5178            Some("08:08:60.091323414"), // leap second
5179            Some("08:08:61.091323414"), // not valid
5180            Some("Not a valid time"),
5181            None,
5182        ])) as ArrayRef;
5183        let a1 = Arc::new(StringArray::from(vec![
5184            Some("08:08:35.091323414"),
5185            Some("08:08:60.091323414"), // leap second
5186            Some("08:08:61.091323414"), // not valid
5187            Some("Not a valid time"),
5188            None,
5189        ])) as ArrayRef;
5190        let a2 = Arc::new(LargeStringArray::from(vec![
5191            Some("08:08:35.091323414"),
5192            Some("08:08:60.091323414"), // leap second
5193            Some("08:08:61.091323414"), // not valid
5194            Some("Not a valid time"),
5195            None,
5196        ])) as ArrayRef;
5197        for array in &[a0, a1, a2] {
5198            let to_type = DataType::Time32(TimeUnit::Second);
5199            let b = cast(array, &to_type).unwrap();
5200            let c = b.as_primitive::<Time32SecondType>();
5201            assert_eq!(29315, c.value(0));
5202            assert_eq!(29340, c.value(1));
5203            assert!(c.is_null(2));
5204            assert!(c.is_null(3));
5205            assert!(c.is_null(4));
5206
5207            let options = CastOptions {
5208                safe: false,
5209                format_options: FormatOptions::default(),
5210            };
5211            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5212            assert_eq!(
5213                err.to_string(),
5214                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(s) type"
5215            );
5216        }
5217    }
5218
5219    #[test]
5220    fn test_cast_string_to_time32millisecond() {
5221        let a0 = Arc::new(StringViewArray::from(vec![
5222            Some("08:08:35.091323414"),
5223            Some("08:08:60.091323414"), // leap second
5224            Some("08:08:61.091323414"), // not valid
5225            Some("Not a valid time"),
5226            None,
5227        ])) as ArrayRef;
5228        let a1 = Arc::new(StringArray::from(vec![
5229            Some("08:08:35.091323414"),
5230            Some("08:08:60.091323414"), // leap second
5231            Some("08:08:61.091323414"), // not valid
5232            Some("Not a valid time"),
5233            None,
5234        ])) as ArrayRef;
5235        let a2 = Arc::new(LargeStringArray::from(vec![
5236            Some("08:08:35.091323414"),
5237            Some("08:08:60.091323414"), // leap second
5238            Some("08:08:61.091323414"), // not valid
5239            Some("Not a valid time"),
5240            None,
5241        ])) as ArrayRef;
5242        for array in &[a0, a1, a2] {
5243            let to_type = DataType::Time32(TimeUnit::Millisecond);
5244            let b = cast(array, &to_type).unwrap();
5245            let c = b.as_primitive::<Time32MillisecondType>();
5246            assert_eq!(29315091, c.value(0));
5247            assert_eq!(29340091, c.value(1));
5248            assert!(c.is_null(2));
5249            assert!(c.is_null(3));
5250            assert!(c.is_null(4));
5251
5252            let options = CastOptions {
5253                safe: false,
5254                format_options: FormatOptions::default(),
5255            };
5256            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5257            assert_eq!(
5258                err.to_string(),
5259                "Cast error: Cannot cast string '08:08:61.091323414' to value of Time32(ms) type"
5260            );
5261        }
5262    }
5263
5264    #[test]
5265    fn test_cast_string_to_time64microsecond() {
5266        let a0 = Arc::new(StringViewArray::from(vec![
5267            Some("08:08:35.091323414"),
5268            Some("Not a valid time"),
5269            None,
5270        ])) as ArrayRef;
5271        let a1 = Arc::new(StringArray::from(vec![
5272            Some("08:08:35.091323414"),
5273            Some("Not a valid time"),
5274            None,
5275        ])) as ArrayRef;
5276        let a2 = Arc::new(LargeStringArray::from(vec![
5277            Some("08:08:35.091323414"),
5278            Some("Not a valid time"),
5279            None,
5280        ])) as ArrayRef;
5281        for array in &[a0, a1, a2] {
5282            let to_type = DataType::Time64(TimeUnit::Microsecond);
5283            let b = cast(array, &to_type).unwrap();
5284            let c = b.as_primitive::<Time64MicrosecondType>();
5285            assert_eq!(29315091323, c.value(0));
5286            assert!(c.is_null(1));
5287            assert!(c.is_null(2));
5288
5289            let options = CastOptions {
5290                safe: false,
5291                format_options: FormatOptions::default(),
5292            };
5293            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5294            assert_eq!(
5295                err.to_string(),
5296                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(µs) type"
5297            );
5298        }
5299    }
5300
5301    #[test]
5302    fn test_cast_string_to_time64nanosecond() {
5303        let a0 = Arc::new(StringViewArray::from(vec![
5304            Some("08:08:35.091323414"),
5305            Some("Not a valid time"),
5306            None,
5307        ])) as ArrayRef;
5308        let a1 = Arc::new(StringArray::from(vec![
5309            Some("08:08:35.091323414"),
5310            Some("Not a valid time"),
5311            None,
5312        ])) as ArrayRef;
5313        let a2 = Arc::new(LargeStringArray::from(vec![
5314            Some("08:08:35.091323414"),
5315            Some("Not a valid time"),
5316            None,
5317        ])) as ArrayRef;
5318        for array in &[a0, a1, a2] {
5319            let to_type = DataType::Time64(TimeUnit::Nanosecond);
5320            let b = cast(array, &to_type).unwrap();
5321            let c = b.as_primitive::<Time64NanosecondType>();
5322            assert_eq!(29315091323414, c.value(0));
5323            assert!(c.is_null(1));
5324            assert!(c.is_null(2));
5325
5326            let options = CastOptions {
5327                safe: false,
5328                format_options: FormatOptions::default(),
5329            };
5330            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5331            assert_eq!(
5332                err.to_string(),
5333                "Cast error: Cannot cast string 'Not a valid time' to value of Time64(ns) type"
5334            );
5335        }
5336    }
5337
5338    #[test]
5339    fn test_cast_string_to_date64() {
5340        let a0 = Arc::new(StringViewArray::from(vec![
5341            Some("2020-09-08T12:00:00"),
5342            Some("Not a valid date"),
5343            None,
5344        ])) as ArrayRef;
5345        let a1 = Arc::new(StringArray::from(vec![
5346            Some("2020-09-08T12:00:00"),
5347            Some("Not a valid date"),
5348            None,
5349        ])) as ArrayRef;
5350        let a2 = Arc::new(LargeStringArray::from(vec![
5351            Some("2020-09-08T12:00:00"),
5352            Some("Not a valid date"),
5353            None,
5354        ])) as ArrayRef;
5355        for array in &[a0, a1, a2] {
5356            let to_type = DataType::Date64;
5357            let b = cast(array, &to_type).unwrap();
5358            let c = b.as_primitive::<Date64Type>();
5359            assert_eq!(1599566400000, c.value(0));
5360            assert!(c.is_null(1));
5361            assert!(c.is_null(2));
5362
5363            let options = CastOptions {
5364                safe: false,
5365                format_options: FormatOptions::default(),
5366            };
5367            let err = cast_with_options(array, &to_type, &options).unwrap_err();
5368            assert_eq!(
5369                err.to_string(),
5370                "Cast error: Cannot cast string 'Not a valid date' to value of Date64 type"
5371            );
5372        }
5373    }
5374
5375    macro_rules! test_safe_string_to_interval {
5376        ($data_vec:expr, $interval_unit:expr, $array_ty:ty, $expect_vec:expr) => {
5377            let source_string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5378
5379            let options = CastOptions {
5380                safe: true,
5381                format_options: FormatOptions::default(),
5382            };
5383
5384            let target_interval_array = cast_with_options(
5385                &source_string_array.clone(),
5386                &DataType::Interval($interval_unit),
5387                &options,
5388            )
5389            .unwrap()
5390            .as_any()
5391            .downcast_ref::<$array_ty>()
5392            .unwrap()
5393            .clone() as $array_ty;
5394
5395            let target_string_array =
5396                cast_with_options(&target_interval_array, &DataType::Utf8, &options)
5397                    .unwrap()
5398                    .as_any()
5399                    .downcast_ref::<StringArray>()
5400                    .unwrap()
5401                    .clone();
5402
5403            let expect_string_array = StringArray::from($expect_vec);
5404
5405            assert_eq!(target_string_array, expect_string_array);
5406
5407            let target_large_string_array =
5408                cast_with_options(&target_interval_array, &DataType::LargeUtf8, &options)
5409                    .unwrap()
5410                    .as_any()
5411                    .downcast_ref::<LargeStringArray>()
5412                    .unwrap()
5413                    .clone();
5414
5415            let expect_large_string_array = LargeStringArray::from($expect_vec);
5416
5417            assert_eq!(target_large_string_array, expect_large_string_array);
5418        };
5419    }
5420
5421    #[test]
5422    fn test_cast_string_to_interval_year_month() {
5423        test_safe_string_to_interval!(
5424            vec![
5425                Some("1 year 1 month"),
5426                Some("1.5 years 13 month"),
5427                Some("30 days"),
5428                Some("31 days"),
5429                Some("2 months 31 days"),
5430                Some("2 months 31 days 1 second"),
5431                Some("foobar"),
5432            ],
5433            IntervalUnit::YearMonth,
5434            IntervalYearMonthArray,
5435            vec![
5436                Some("1 years 1 mons"),
5437                Some("2 years 7 mons"),
5438                None,
5439                None,
5440                None,
5441                None,
5442                None,
5443            ]
5444        );
5445    }
5446
5447    #[test]
5448    fn test_cast_string_to_interval_day_time() {
5449        test_safe_string_to_interval!(
5450            vec![
5451                Some("1 year 1 month"),
5452                Some("1.5 years 13 month"),
5453                Some("30 days"),
5454                Some("1 day 2 second 3.5 milliseconds"),
5455                Some("foobar"),
5456            ],
5457            IntervalUnit::DayTime,
5458            IntervalDayTimeArray,
5459            vec![
5460                Some("390 days"),
5461                Some("930 days"),
5462                Some("30 days"),
5463                None,
5464                None,
5465            ]
5466        );
5467    }
5468
5469    #[test]
5470    fn test_cast_string_to_interval_month_day_nano() {
5471        test_safe_string_to_interval!(
5472            vec![
5473                Some("1 year 1 month 1 day"),
5474                None,
5475                Some("1.5 years 13 month 35 days 1.4 milliseconds"),
5476                Some("3 days"),
5477                Some("8 seconds"),
5478                None,
5479                Some("1 day 29800 milliseconds"),
5480                Some("3 months 1 second"),
5481                Some("6 minutes 120 second"),
5482                Some("2 years 39 months 9 days 19 hours 1 minute 83 seconds 399222 milliseconds"),
5483                Some("foobar"),
5484            ],
5485            IntervalUnit::MonthDayNano,
5486            IntervalMonthDayNanoArray,
5487            vec![
5488                Some("13 mons 1 days"),
5489                None,
5490                Some("31 mons 35 days 0.001400000 secs"),
5491                Some("3 days"),
5492                Some("8.000000000 secs"),
5493                None,
5494                Some("1 days 29.800000000 secs"),
5495                Some("3 mons 1.000000000 secs"),
5496                Some("8 mins"),
5497                Some("63 mons 9 days 19 hours 9 mins 2.222000000 secs"),
5498                None,
5499            ]
5500        );
5501    }
5502
5503    macro_rules! test_unsafe_string_to_interval_err {
5504        ($data_vec:expr, $interval_unit:expr, $error_msg:expr) => {
5505            let string_array = Arc::new(StringArray::from($data_vec.clone())) as ArrayRef;
5506            let options = CastOptions {
5507                safe: false,
5508                format_options: FormatOptions::default(),
5509            };
5510            let arrow_err = cast_with_options(
5511                &string_array.clone(),
5512                &DataType::Interval($interval_unit),
5513                &options,
5514            )
5515            .unwrap_err();
5516            assert_eq!($error_msg, arrow_err.to_string());
5517        };
5518    }
5519
5520    #[test]
5521    fn test_cast_string_to_interval_err() {
5522        test_unsafe_string_to_interval_err!(
5523            vec![Some("foobar")],
5524            IntervalUnit::YearMonth,
5525            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5526        );
5527        test_unsafe_string_to_interval_err!(
5528            vec![Some("foobar")],
5529            IntervalUnit::DayTime,
5530            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5531        );
5532        test_unsafe_string_to_interval_err!(
5533            vec![Some("foobar")],
5534            IntervalUnit::MonthDayNano,
5535            r#"Parser error: Invalid input syntax for type interval: "foobar""#
5536        );
5537        test_unsafe_string_to_interval_err!(
5538            vec![Some("2 months 31 days 1 second")],
5539            IntervalUnit::YearMonth,
5540            r#"Cast error: Cannot cast 2 months 31 days 1 second to IntervalYearMonth. Only year and month fields are allowed."#
5541        );
5542        test_unsafe_string_to_interval_err!(
5543            vec![Some("1 day 1.5 milliseconds")],
5544            IntervalUnit::DayTime,
5545            r#"Cast error: Cannot cast 1 day 1.5 milliseconds to IntervalDayTime because the nanos part isn't multiple of milliseconds"#
5546        );
5547
5548        // overflow
5549        test_unsafe_string_to_interval_err!(
5550            vec![Some(format!(
5551                "{} century {} year {} month",
5552                i64::MAX - 2,
5553                i64::MAX - 2,
5554                i64::MAX - 2
5555            ))],
5556            IntervalUnit::DayTime,
5557            format!(
5558                "Arithmetic overflow: Overflow happened on: {} * 100",
5559                i64::MAX - 2
5560            )
5561        );
5562        test_unsafe_string_to_interval_err!(
5563            vec![Some(format!(
5564                "{} year {} month {} day",
5565                i64::MAX - 2,
5566                i64::MAX - 2,
5567                i64::MAX - 2
5568            ))],
5569            IntervalUnit::MonthDayNano,
5570            format!(
5571                "Arithmetic overflow: Overflow happened on: {} * 12",
5572                i64::MAX - 2
5573            )
5574        );
5575    }
5576
5577    #[test]
5578    fn test_cast_binary_to_fixed_size_binary() {
5579        let bytes_1 = "Hiiii".as_bytes();
5580        let bytes_2 = "Hello".as_bytes();
5581
5582        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5583        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5584        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5585
5586        let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
5587        let down_cast = array_ref
5588            .as_any()
5589            .downcast_ref::<FixedSizeBinaryArray>()
5590            .unwrap();
5591        assert_eq!(bytes_1, down_cast.value(0));
5592        assert_eq!(bytes_2, down_cast.value(1));
5593        assert!(down_cast.is_null(2));
5594
5595        let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
5596        let down_cast = array_ref
5597            .as_any()
5598            .downcast_ref::<FixedSizeBinaryArray>()
5599            .unwrap();
5600        assert_eq!(bytes_1, down_cast.value(0));
5601        assert_eq!(bytes_2, down_cast.value(1));
5602        assert!(down_cast.is_null(2));
5603
5604        // test error cases when the length of binary are not same
5605        let bytes_1 = "Hi".as_bytes();
5606        let bytes_2 = "Hello".as_bytes();
5607
5608        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5609        let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
5610        let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
5611
5612        let array_ref = cast_with_options(
5613            &a1,
5614            &DataType::FixedSizeBinary(5),
5615            &CastOptions {
5616                safe: false,
5617                format_options: FormatOptions::default(),
5618            },
5619        );
5620        assert!(array_ref.is_err());
5621
5622        let array_ref = cast_with_options(
5623            &a2,
5624            &DataType::FixedSizeBinary(5),
5625            &CastOptions {
5626                safe: false,
5627                format_options: FormatOptions::default(),
5628            },
5629        );
5630        assert!(array_ref.is_err());
5631    }
5632
5633    #[test]
5634    fn test_fixed_size_binary_to_binary() {
5635        let bytes_1 = "Hiiii".as_bytes();
5636        let bytes_2 = "Hello".as_bytes();
5637
5638        let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
5639        let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
5640
5641        let array_ref = cast(&a1, &DataType::Binary).unwrap();
5642        let down_cast = array_ref.as_binary::<i32>();
5643        assert_eq!(bytes_1, down_cast.value(0));
5644        assert_eq!(bytes_2, down_cast.value(1));
5645        assert!(down_cast.is_null(2));
5646
5647        let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
5648        let down_cast = array_ref.as_binary::<i64>();
5649        assert_eq!(bytes_1, down_cast.value(0));
5650        assert_eq!(bytes_2, down_cast.value(1));
5651        assert!(down_cast.is_null(2));
5652
5653        let array_ref = cast(&a1, &DataType::BinaryView).unwrap();
5654        let down_cast = array_ref.as_binary_view();
5655        assert_eq!(bytes_1, down_cast.value(0));
5656        assert_eq!(bytes_2, down_cast.value(1));
5657        assert!(down_cast.is_null(2));
5658    }
5659
5660    #[test]
5661    fn test_fixed_size_binary_to_dictionary() {
5662        let bytes_1 = "Hiiii".as_bytes();
5663        let bytes_2 = "Hello".as_bytes();
5664
5665        let binary_data = vec![Some(bytes_1), Some(bytes_2), Some(bytes_1), None];
5666        let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
5667
5668        let cast_type = DataType::Dictionary(
5669            Box::new(DataType::Int8),
5670            Box::new(DataType::FixedSizeBinary(5)),
5671        );
5672        let cast_array = cast(&a1, &cast_type).unwrap();
5673        assert_eq!(cast_array.data_type(), &cast_type);
5674        assert_eq!(
5675            array_to_strings(&cast_array),
5676            vec!["4869696969", "48656c6c6f", "4869696969", "null"]
5677        );
5678        // dictionary should only have two distinct values
5679        let dict_array = cast_array
5680            .as_any()
5681            .downcast_ref::<DictionaryArray<Int8Type>>()
5682            .unwrap();
5683        assert_eq!(dict_array.values().len(), 2);
5684    }
5685
5686    #[test]
5687    fn test_binary_to_dictionary() {
5688        let mut builder = GenericBinaryBuilder::<i32>::new();
5689        builder.append_value(b"hello");
5690        builder.append_value(b"hiiii");
5691        builder.append_value(b"hiiii"); // duplicate
5692        builder.append_null();
5693        builder.append_value(b"rustt");
5694
5695        let a1 = builder.finish();
5696
5697        let cast_type = DataType::Dictionary(
5698            Box::new(DataType::Int8),
5699            Box::new(DataType::FixedSizeBinary(5)),
5700        );
5701        let cast_array = cast(&a1, &cast_type).unwrap();
5702        assert_eq!(cast_array.data_type(), &cast_type);
5703        assert_eq!(
5704            array_to_strings(&cast_array),
5705            vec![
5706                "68656c6c6f",
5707                "6869696969",
5708                "6869696969",
5709                "null",
5710                "7275737474"
5711            ]
5712        );
5713        // dictionary should only have three distinct values
5714        let dict_array = cast_array
5715            .as_any()
5716            .downcast_ref::<DictionaryArray<Int8Type>>()
5717            .unwrap();
5718        assert_eq!(dict_array.values().len(), 3);
5719    }
5720
5721    #[test]
5722    fn test_numeric_to_binary() {
5723        let a = Int16Array::from(vec![Some(1), Some(511), None]);
5724
5725        let array_ref = cast(&a, &DataType::Binary).unwrap();
5726        let down_cast = array_ref.as_binary::<i32>();
5727        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
5728        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
5729        assert!(down_cast.is_null(2));
5730
5731        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
5732
5733        let array_ref = cast(&a, &DataType::Binary).unwrap();
5734        let down_cast = array_ref.as_binary::<i32>();
5735        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
5736        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
5737        assert!(down_cast.is_null(2));
5738    }
5739
5740    #[test]
5741    fn test_numeric_to_large_binary() {
5742        let a = Int16Array::from(vec![Some(1), Some(511), None]);
5743
5744        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
5745        let down_cast = array_ref.as_binary::<i64>();
5746        assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
5747        assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
5748        assert!(down_cast.is_null(2));
5749
5750        let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);
5751
5752        let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
5753        let down_cast = array_ref.as_binary::<i64>();
5754        assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
5755        assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
5756        assert!(down_cast.is_null(2));
5757    }
5758
5759    #[test]
5760    fn test_cast_date32_to_int32() {
5761        let array = Date32Array::from(vec![10000, 17890]);
5762        let b = cast(&array, &DataType::Int32).unwrap();
5763        let c = b.as_primitive::<Int32Type>();
5764        assert_eq!(10000, c.value(0));
5765        assert_eq!(17890, c.value(1));
5766    }
5767
5768    #[test]
5769    fn test_cast_int32_to_date32() {
5770        let array = Int32Array::from(vec![10000, 17890]);
5771        let b = cast(&array, &DataType::Date32).unwrap();
5772        let c = b.as_primitive::<Date32Type>();
5773        assert_eq!(10000, c.value(0));
5774        assert_eq!(17890, c.value(1));
5775    }
5776
5777    #[test]
5778    fn test_cast_timestamp_to_date32() {
5779        let array =
5780            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
5781                .with_timezone("+00:00".to_string());
5782        let b = cast(&array, &DataType::Date32).unwrap();
5783        let c = b.as_primitive::<Date32Type>();
5784        assert_eq!(10000, c.value(0));
5785        assert_eq!(17890, c.value(1));
5786        assert!(c.is_null(2));
5787    }
5788    #[test]
5789    fn test_cast_timestamp_to_date32_zone() {
5790        let strings = StringArray::from_iter([
5791            Some("1970-01-01T00:00:01"),
5792            Some("1970-01-01T23:59:59"),
5793            None,
5794            Some("2020-03-01T02:00:23+00:00"),
5795        ]);
5796        let dt = DataType::Timestamp(TimeUnit::Millisecond, Some("-07:00".into()));
5797        let timestamps = cast(&strings, &dt).unwrap();
5798        let dates = cast(timestamps.as_ref(), &DataType::Date32).unwrap();
5799
5800        let c = dates.as_primitive::<Date32Type>();
5801        let expected = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
5802        assert_eq!(c.value_as_date(0).unwrap(), expected);
5803        assert_eq!(c.value_as_date(1).unwrap(), expected);
5804        assert!(c.is_null(2));
5805        let expected = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
5806        assert_eq!(c.value_as_date(3).unwrap(), expected);
5807    }
5808    #[test]
5809    fn test_cast_timestamp_to_date64() {
5810        let array =
5811            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None]);
5812        let b = cast(&array, &DataType::Date64).unwrap();
5813        let c = b.as_primitive::<Date64Type>();
5814        assert_eq!(864000000005, c.value(0));
5815        assert_eq!(1545696000001, c.value(1));
5816        assert!(c.is_null(2));
5817
5818        let array = TimestampSecondArray::from(vec![Some(864000000005), Some(1545696000001)]);
5819        let b = cast(&array, &DataType::Date64).unwrap();
5820        let c = b.as_primitive::<Date64Type>();
5821        assert_eq!(864000000005000, c.value(0));
5822        assert_eq!(1545696000001000, c.value(1));
5823
5824        // test overflow, safe cast
5825        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
5826        let b = cast(&array, &DataType::Date64).unwrap();
5827        assert!(b.is_null(0));
5828        // test overflow, unsafe cast
5829        let array = TimestampSecondArray::from(vec![Some(i64::MAX)]);
5830        let options = CastOptions {
5831            safe: false,
5832            format_options: FormatOptions::default(),
5833        };
5834        let b = cast_with_options(&array, &DataType::Date64, &options);
5835        assert!(b.is_err());
5836    }
5837
5838    #[test]
5839    fn test_cast_timestamp_to_time64() {
5840        // test timestamp secs
5841        let array = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
5842            .with_timezone("+01:00".to_string());
5843        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5844        let c = b.as_primitive::<Time64MicrosecondType>();
5845        assert_eq!(3605000000, c.value(0));
5846        assert_eq!(3601000000, c.value(1));
5847        assert!(c.is_null(2));
5848        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5849        let c = b.as_primitive::<Time64NanosecondType>();
5850        assert_eq!(3605000000000, c.value(0));
5851        assert_eq!(3601000000000, c.value(1));
5852        assert!(c.is_null(2));
5853
5854        // test timestamp milliseconds
5855        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
5856            .with_timezone("+01:00".to_string());
5857        let array = Arc::new(a) as ArrayRef;
5858        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5859        let c = b.as_primitive::<Time64MicrosecondType>();
5860        assert_eq!(3605000000, c.value(0));
5861        assert_eq!(3601000000, c.value(1));
5862        assert!(c.is_null(2));
5863        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5864        let c = b.as_primitive::<Time64NanosecondType>();
5865        assert_eq!(3605000000000, c.value(0));
5866        assert_eq!(3601000000000, c.value(1));
5867        assert!(c.is_null(2));
5868
5869        // test timestamp microseconds
5870        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
5871            .with_timezone("+01:00".to_string());
5872        let array = Arc::new(a) as ArrayRef;
5873        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5874        let c = b.as_primitive::<Time64MicrosecondType>();
5875        assert_eq!(3605000000, c.value(0));
5876        assert_eq!(3601000000, c.value(1));
5877        assert!(c.is_null(2));
5878        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5879        let c = b.as_primitive::<Time64NanosecondType>();
5880        assert_eq!(3605000000000, c.value(0));
5881        assert_eq!(3601000000000, c.value(1));
5882        assert!(c.is_null(2));
5883
5884        // test timestamp nanoseconds
5885        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
5886            .with_timezone("+01:00".to_string());
5887        let array = Arc::new(a) as ArrayRef;
5888        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond)).unwrap();
5889        let c = b.as_primitive::<Time64MicrosecondType>();
5890        assert_eq!(3605000000, c.value(0));
5891        assert_eq!(3601000000, c.value(1));
5892        assert!(c.is_null(2));
5893        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond)).unwrap();
5894        let c = b.as_primitive::<Time64NanosecondType>();
5895        assert_eq!(3605000000000, c.value(0));
5896        assert_eq!(3601000000000, c.value(1));
5897        assert!(c.is_null(2));
5898
5899        // test overflow
5900        let a =
5901            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
5902        let array = Arc::new(a) as ArrayRef;
5903        let b = cast(&array, &DataType::Time64(TimeUnit::Microsecond));
5904        assert!(b.is_err());
5905        let b = cast(&array, &DataType::Time64(TimeUnit::Nanosecond));
5906        assert!(b.is_err());
5907        let b = cast(&array, &DataType::Time64(TimeUnit::Millisecond));
5908        assert!(b.is_err());
5909    }
5910
5911    #[test]
5912    fn test_cast_timestamp_to_time32() {
5913        // test timestamp secs
5914        let a = TimestampSecondArray::from(vec![Some(86405), Some(1), None])
5915            .with_timezone("+01:00".to_string());
5916        let array = Arc::new(a) as ArrayRef;
5917        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5918        let c = b.as_primitive::<Time32SecondType>();
5919        assert_eq!(3605, c.value(0));
5920        assert_eq!(3601, c.value(1));
5921        assert!(c.is_null(2));
5922        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5923        let c = b.as_primitive::<Time32MillisecondType>();
5924        assert_eq!(3605000, c.value(0));
5925        assert_eq!(3601000, c.value(1));
5926        assert!(c.is_null(2));
5927
5928        // test timestamp milliseconds
5929        let a = TimestampMillisecondArray::from(vec![Some(86405000), Some(1000), None])
5930            .with_timezone("+01:00".to_string());
5931        let array = Arc::new(a) as ArrayRef;
5932        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5933        let c = b.as_primitive::<Time32SecondType>();
5934        assert_eq!(3605, c.value(0));
5935        assert_eq!(3601, c.value(1));
5936        assert!(c.is_null(2));
5937        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5938        let c = b.as_primitive::<Time32MillisecondType>();
5939        assert_eq!(3605000, c.value(0));
5940        assert_eq!(3601000, c.value(1));
5941        assert!(c.is_null(2));
5942
5943        // test timestamp microseconds
5944        let a = TimestampMicrosecondArray::from(vec![Some(86405000000), Some(1000000), None])
5945            .with_timezone("+01:00".to_string());
5946        let array = Arc::new(a) as ArrayRef;
5947        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5948        let c = b.as_primitive::<Time32SecondType>();
5949        assert_eq!(3605, c.value(0));
5950        assert_eq!(3601, c.value(1));
5951        assert!(c.is_null(2));
5952        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5953        let c = b.as_primitive::<Time32MillisecondType>();
5954        assert_eq!(3605000, c.value(0));
5955        assert_eq!(3601000, c.value(1));
5956        assert!(c.is_null(2));
5957
5958        // test timestamp nanoseconds
5959        let a = TimestampNanosecondArray::from(vec![Some(86405000000000), Some(1000000000), None])
5960            .with_timezone("+01:00".to_string());
5961        let array = Arc::new(a) as ArrayRef;
5962        let b = cast(&array, &DataType::Time32(TimeUnit::Second)).unwrap();
5963        let c = b.as_primitive::<Time32SecondType>();
5964        assert_eq!(3605, c.value(0));
5965        assert_eq!(3601, c.value(1));
5966        assert!(c.is_null(2));
5967        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond)).unwrap();
5968        let c = b.as_primitive::<Time32MillisecondType>();
5969        assert_eq!(3605000, c.value(0));
5970        assert_eq!(3601000, c.value(1));
5971        assert!(c.is_null(2));
5972
5973        // test overflow
5974        let a =
5975            TimestampSecondArray::from(vec![Some(i64::MAX)]).with_timezone("+01:00".to_string());
5976        let array = Arc::new(a) as ArrayRef;
5977        let b = cast(&array, &DataType::Time32(TimeUnit::Second));
5978        assert!(b.is_err());
5979        let b = cast(&array, &DataType::Time32(TimeUnit::Millisecond));
5980        assert!(b.is_err());
5981    }
5982
5983    // Cast Timestamp(_, None) -> Timestamp(_, Some(timezone))
5984    #[test]
5985    fn test_cast_timestamp_with_timezone_1() {
5986        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
5987            Some("2000-01-01T00:00:00.123456789"),
5988            Some("2010-01-01T00:00:00.123456789"),
5989            None,
5990        ]));
5991        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
5992        let timestamp_array = cast(&string_array, &to_type).unwrap();
5993
5994        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
5995        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
5996
5997        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
5998        let result = string_array.as_string::<i32>();
5999        assert_eq!("2000-01-01T00:00:00.123456+07:00", result.value(0));
6000        assert_eq!("2010-01-01T00:00:00.123456+07:00", result.value(1));
6001        assert!(result.is_null(2));
6002    }
6003
6004    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, None)
6005    #[test]
6006    fn test_cast_timestamp_with_timezone_2() {
6007        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6008            Some("2000-01-01T07:00:00.123456789"),
6009            Some("2010-01-01T07:00:00.123456789"),
6010            None,
6011        ]));
6012        let to_type = DataType::Timestamp(TimeUnit::Millisecond, Some("+0700".into()));
6013        let timestamp_array = cast(&string_array, &to_type).unwrap();
6014
6015        // Check intermediate representation is correct
6016        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6017        let result = string_array.as_string::<i32>();
6018        assert_eq!("2000-01-01T07:00:00.123+07:00", result.value(0));
6019        assert_eq!("2010-01-01T07:00:00.123+07:00", result.value(1));
6020        assert!(result.is_null(2));
6021
6022        let to_type = DataType::Timestamp(TimeUnit::Nanosecond, None);
6023        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6024
6025        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6026        let result = string_array.as_string::<i32>();
6027        assert_eq!("2000-01-01T00:00:00.123", result.value(0));
6028        assert_eq!("2010-01-01T00:00:00.123", result.value(1));
6029        assert!(result.is_null(2));
6030    }
6031
6032    // Cast Timestamp(_, Some(timezone)) -> Timestamp(_, Some(timezone))
6033    #[test]
6034    fn test_cast_timestamp_with_timezone_3() {
6035        let string_array: Arc<dyn Array> = Arc::new(StringArray::from(vec![
6036            Some("2000-01-01T07:00:00.123456789"),
6037            Some("2010-01-01T07:00:00.123456789"),
6038            None,
6039        ]));
6040        let to_type = DataType::Timestamp(TimeUnit::Microsecond, Some("+0700".into()));
6041        let timestamp_array = cast(&string_array, &to_type).unwrap();
6042
6043        // Check intermediate representation is correct
6044        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6045        let result = string_array.as_string::<i32>();
6046        assert_eq!("2000-01-01T07:00:00.123456+07:00", result.value(0));
6047        assert_eq!("2010-01-01T07:00:00.123456+07:00", result.value(1));
6048        assert!(result.is_null(2));
6049
6050        let to_type = DataType::Timestamp(TimeUnit::Second, Some("-08:00".into()));
6051        let timestamp_array = cast(&timestamp_array, &to_type).unwrap();
6052
6053        let string_array = cast(&timestamp_array, &DataType::Utf8).unwrap();
6054        let result = string_array.as_string::<i32>();
6055        assert_eq!("1999-12-31T16:00:00-08:00", result.value(0));
6056        assert_eq!("2009-12-31T16:00:00-08:00", result.value(1));
6057        assert!(result.is_null(2));
6058    }
6059
6060    #[test]
6061    fn test_cast_date64_to_timestamp() {
6062        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6063        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6064        let c = b.as_primitive::<TimestampSecondType>();
6065        assert_eq!(864000000, c.value(0));
6066        assert_eq!(1545696000, c.value(1));
6067        assert!(c.is_null(2));
6068    }
6069
6070    #[test]
6071    fn test_cast_date64_to_timestamp_ms() {
6072        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6073        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
6074        let c = b
6075            .as_any()
6076            .downcast_ref::<TimestampMillisecondArray>()
6077            .unwrap();
6078        assert_eq!(864000000005, c.value(0));
6079        assert_eq!(1545696000001, c.value(1));
6080        assert!(c.is_null(2));
6081    }
6082
6083    #[test]
6084    fn test_cast_date64_to_timestamp_us() {
6085        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6086        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
6087        let c = b
6088            .as_any()
6089            .downcast_ref::<TimestampMicrosecondArray>()
6090            .unwrap();
6091        assert_eq!(864000000005000, c.value(0));
6092        assert_eq!(1545696000001000, c.value(1));
6093        assert!(c.is_null(2));
6094    }
6095
6096    #[test]
6097    fn test_cast_date64_to_timestamp_ns() {
6098        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6099        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
6100        let c = b
6101            .as_any()
6102            .downcast_ref::<TimestampNanosecondArray>()
6103            .unwrap();
6104        assert_eq!(864000000005000000, c.value(0));
6105        assert_eq!(1545696000001000000, c.value(1));
6106        assert!(c.is_null(2));
6107    }
6108
6109    #[test]
6110    fn test_cast_timestamp_to_i64() {
6111        let array =
6112            TimestampMillisecondArray::from(vec![Some(864000000005), Some(1545696000001), None])
6113                .with_timezone("UTC".to_string());
6114        let b = cast(&array, &DataType::Int64).unwrap();
6115        let c = b.as_primitive::<Int64Type>();
6116        assert_eq!(&DataType::Int64, c.data_type());
6117        assert_eq!(864000000005, c.value(0));
6118        assert_eq!(1545696000001, c.value(1));
6119        assert!(c.is_null(2));
6120    }
6121
6122    macro_rules! assert_cast {
6123        ($array:expr, $datatype:expr, $output_array_type: ty, $expected:expr) => {{
6124            assert!(can_cast_types($array.data_type(), &$datatype));
6125            let out = cast(&$array, &$datatype).unwrap();
6126            let actual = out
6127                .as_any()
6128                .downcast_ref::<$output_array_type>()
6129                .unwrap()
6130                .into_iter()
6131                .collect::<Vec<_>>();
6132            assert_eq!(actual, $expected);
6133        }};
6134        ($array:expr, $datatype:expr, $output_array_type: ty, $options:expr, $expected:expr) => {{
6135            assert!(can_cast_types($array.data_type(), &$datatype));
6136            let out = cast_with_options(&$array, &$datatype, &$options).unwrap();
6137            let actual = out
6138                .as_any()
6139                .downcast_ref::<$output_array_type>()
6140                .unwrap()
6141                .into_iter()
6142                .collect::<Vec<_>>();
6143            assert_eq!(actual, $expected);
6144        }};
6145    }
6146
6147    #[test]
6148    fn test_cast_date32_to_string() {
6149        let array = Date32Array::from(vec![Some(0), Some(10000), Some(13036), Some(17890), None]);
6150        let expected = vec![
6151            Some("1970-01-01"),
6152            Some("1997-05-19"),
6153            Some("2005-09-10"),
6154            Some("2018-12-25"),
6155            None,
6156        ];
6157
6158        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6159        assert_cast!(array, DataType::Utf8, StringArray, expected);
6160        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6161    }
6162
6163    #[test]
6164    fn test_cast_date64_to_string() {
6165        let array = Date64Array::from(vec![
6166            Some(0),
6167            Some(10000 * 86400000),
6168            Some(13036 * 86400000),
6169            Some(17890 * 86400000),
6170            None,
6171        ]);
6172        let expected = vec![
6173            Some("1970-01-01T00:00:00"),
6174            Some("1997-05-19T00:00:00"),
6175            Some("2005-09-10T00:00:00"),
6176            Some("2018-12-25T00:00:00"),
6177            None,
6178        ];
6179
6180        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6181        assert_cast!(array, DataType::Utf8, StringArray, expected);
6182        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6183    }
6184
6185    #[test]
6186    fn test_cast_date32_to_timestamp_and_timestamp_with_timezone() {
6187        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6188        let a = Date32Array::from(vec![Some(18628), None, None]); // 2021-1-1, 2022-1-1
6189        let array = Arc::new(a) as ArrayRef;
6190
6191        let b = cast(
6192            &array,
6193            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6194        )
6195        .unwrap();
6196        let c = b.as_primitive::<TimestampSecondType>();
6197        let string_array = cast(&c, &DataType::Utf8).unwrap();
6198        let result = string_array.as_string::<i32>();
6199        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6200
6201        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6202        let c = b.as_primitive::<TimestampSecondType>();
6203        let string_array = cast(&c, &DataType::Utf8).unwrap();
6204        let result = string_array.as_string::<i32>();
6205        assert_eq!("2021-01-01T00:00:00", result.value(0));
6206    }
6207
6208    #[test]
6209    fn test_cast_date32_to_timestamp_with_timezone() {
6210        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6211        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6212        let array = Arc::new(a) as ArrayRef;
6213        let b = cast(
6214            &array,
6215            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6216        )
6217        .unwrap();
6218        let c = b.as_primitive::<TimestampSecondType>();
6219        assert_eq!(1609438500, c.value(0));
6220        assert_eq!(1640974500, c.value(1));
6221        assert!(c.is_null(2));
6222
6223        let string_array = cast(&c, &DataType::Utf8).unwrap();
6224        let result = string_array.as_string::<i32>();
6225        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6226        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6227    }
6228
6229    #[test]
6230    fn test_cast_date32_to_timestamp_with_timezone_ms() {
6231        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6232        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6233        let array = Arc::new(a) as ArrayRef;
6234        let b = cast(
6235            &array,
6236            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
6237        )
6238        .unwrap();
6239        let c = b.as_primitive::<TimestampMillisecondType>();
6240        assert_eq!(1609438500000, c.value(0));
6241        assert_eq!(1640974500000, c.value(1));
6242        assert!(c.is_null(2));
6243
6244        let string_array = cast(&c, &DataType::Utf8).unwrap();
6245        let result = string_array.as_string::<i32>();
6246        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6247        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6248    }
6249
6250    #[test]
6251    fn test_cast_date32_to_timestamp_with_timezone_us() {
6252        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6253        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6254        let array = Arc::new(a) as ArrayRef;
6255        let b = cast(
6256            &array,
6257            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
6258        )
6259        .unwrap();
6260        let c = b.as_primitive::<TimestampMicrosecondType>();
6261        assert_eq!(1609438500000000, c.value(0));
6262        assert_eq!(1640974500000000, c.value(1));
6263        assert!(c.is_null(2));
6264
6265        let string_array = cast(&c, &DataType::Utf8).unwrap();
6266        let result = string_array.as_string::<i32>();
6267        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6268        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6269    }
6270
6271    #[test]
6272    fn test_cast_date32_to_timestamp_with_timezone_ns() {
6273        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6274        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
6275        let array = Arc::new(a) as ArrayRef;
6276        let b = cast(
6277            &array,
6278            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
6279        )
6280        .unwrap();
6281        let c = b.as_primitive::<TimestampNanosecondType>();
6282        assert_eq!(1609438500000000000, c.value(0));
6283        assert_eq!(1640974500000000000, c.value(1));
6284        assert!(c.is_null(2));
6285
6286        let string_array = cast(&c, &DataType::Utf8).unwrap();
6287        let result = string_array.as_string::<i32>();
6288        assert_eq!("2021-01-01T00:00:00+05:45", result.value(0));
6289        assert_eq!("2022-01-01T00:00:00+05:45", result.value(1));
6290    }
6291
6292    #[test]
6293    fn test_cast_date64_to_timestamp_with_timezone() {
6294        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6295        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6296        let b = cast(
6297            &array,
6298            &DataType::Timestamp(TimeUnit::Second, Some(tz.into())),
6299        )
6300        .unwrap();
6301
6302        let c = b.as_primitive::<TimestampSecondType>();
6303        assert_eq!(863979300, c.value(0));
6304        assert_eq!(1545675300, c.value(1));
6305        assert!(c.is_null(2));
6306
6307        let string_array = cast(&c, &DataType::Utf8).unwrap();
6308        let result = string_array.as_string::<i32>();
6309        assert_eq!("1997-05-19T00:00:00+05:45", result.value(0));
6310        assert_eq!("2018-12-25T00:00:00+05:45", result.value(1));
6311    }
6312
6313    #[test]
6314    fn test_cast_date64_to_timestamp_with_timezone_ms() {
6315        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6316        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6317        let b = cast(
6318            &array,
6319            &DataType::Timestamp(TimeUnit::Millisecond, Some(tz.into())),
6320        )
6321        .unwrap();
6322
6323        let c = b.as_primitive::<TimestampMillisecondType>();
6324        assert_eq!(863979300005, c.value(0));
6325        assert_eq!(1545675300001, c.value(1));
6326        assert!(c.is_null(2));
6327
6328        let string_array = cast(&c, &DataType::Utf8).unwrap();
6329        let result = string_array.as_string::<i32>();
6330        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6331        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6332    }
6333
6334    #[test]
6335    fn test_cast_date64_to_timestamp_with_timezone_us() {
6336        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6337        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6338        let b = cast(
6339            &array,
6340            &DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into())),
6341        )
6342        .unwrap();
6343
6344        let c = b.as_primitive::<TimestampMicrosecondType>();
6345        assert_eq!(863979300005000, c.value(0));
6346        assert_eq!(1545675300001000, c.value(1));
6347        assert!(c.is_null(2));
6348
6349        let string_array = cast(&c, &DataType::Utf8).unwrap();
6350        let result = string_array.as_string::<i32>();
6351        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6352        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6353    }
6354
6355    #[test]
6356    fn test_cast_date64_to_timestamp_with_timezone_ns() {
6357        let array = Date64Array::from(vec![Some(864000000005), Some(1545696000001), None]);
6358        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6359        let b = cast(
6360            &array,
6361            &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.into())),
6362        )
6363        .unwrap();
6364
6365        let c = b.as_primitive::<TimestampNanosecondType>();
6366        assert_eq!(863979300005000000, c.value(0));
6367        assert_eq!(1545675300001000000, c.value(1));
6368        assert!(c.is_null(2));
6369
6370        let string_array = cast(&c, &DataType::Utf8).unwrap();
6371        let result = string_array.as_string::<i32>();
6372        assert_eq!("1997-05-19T00:00:00.005+05:45", result.value(0));
6373        assert_eq!("2018-12-25T00:00:00.001+05:45", result.value(1));
6374    }
6375
6376    #[test]
6377    fn test_cast_timestamp_to_strings() {
6378        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
6379        let array =
6380            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6381        let expected = vec![
6382            Some("1997-05-19T00:00:03.005"),
6383            Some("2018-12-25T00:00:02.001"),
6384            None,
6385        ];
6386
6387        assert_cast!(array, DataType::Utf8View, StringViewArray, expected);
6388        assert_cast!(array, DataType::Utf8, StringArray, expected);
6389        assert_cast!(array, DataType::LargeUtf8, LargeStringArray, expected);
6390    }
6391
6392    #[test]
6393    fn test_cast_timestamp_to_strings_opt() {
6394        let ts_format = "%Y-%m-%d %H:%M:%S%.6f";
6395        let tz = "+0545"; // UTC + 0545 is Asia/Kathmandu
6396        let cast_options = CastOptions {
6397            safe: true,
6398            format_options: FormatOptions::default()
6399                .with_timestamp_format(Some(ts_format))
6400                .with_timestamp_tz_format(Some(ts_format)),
6401        };
6402
6403        // "2018-12-25T00:00:02.001", "1997-05-19T00:00:03.005", None
6404        let array_without_tz =
6405            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6406        let expected = vec![
6407            Some("1997-05-19 00:00:03.005000"),
6408            Some("2018-12-25 00:00:02.001000"),
6409            None,
6410        ];
6411        assert_cast!(
6412            array_without_tz,
6413            DataType::Utf8View,
6414            StringViewArray,
6415            cast_options,
6416            expected
6417        );
6418        assert_cast!(
6419            array_without_tz,
6420            DataType::Utf8,
6421            StringArray,
6422            cast_options,
6423            expected
6424        );
6425        assert_cast!(
6426            array_without_tz,
6427            DataType::LargeUtf8,
6428            LargeStringArray,
6429            cast_options,
6430            expected
6431        );
6432
6433        let array_with_tz =
6434            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None])
6435                .with_timezone(tz.to_string());
6436        let expected = vec![
6437            Some("1997-05-19 05:45:03.005000"),
6438            Some("2018-12-25 05:45:02.001000"),
6439            None,
6440        ];
6441        assert_cast!(
6442            array_with_tz,
6443            DataType::Utf8View,
6444            StringViewArray,
6445            cast_options,
6446            expected
6447        );
6448        assert_cast!(
6449            array_with_tz,
6450            DataType::Utf8,
6451            StringArray,
6452            cast_options,
6453            expected
6454        );
6455        assert_cast!(
6456            array_with_tz,
6457            DataType::LargeUtf8,
6458            LargeStringArray,
6459            cast_options,
6460            expected
6461        );
6462    }
6463
6464    #[test]
6465    fn test_cast_between_timestamps() {
6466        let array =
6467            TimestampMillisecondArray::from(vec![Some(864000003005), Some(1545696002001), None]);
6468        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
6469        let c = b.as_primitive::<TimestampSecondType>();
6470        assert_eq!(864000003, c.value(0));
6471        assert_eq!(1545696002, c.value(1));
6472        assert!(c.is_null(2));
6473    }
6474
6475    #[test]
6476    fn test_cast_duration_to_i64() {
6477        let base = vec![5, 6, 7, 8, 100000000];
6478
6479        let duration_arrays = vec![
6480            Arc::new(DurationNanosecondArray::from(base.clone())) as ArrayRef,
6481            Arc::new(DurationMicrosecondArray::from(base.clone())) as ArrayRef,
6482            Arc::new(DurationMillisecondArray::from(base.clone())) as ArrayRef,
6483            Arc::new(DurationSecondArray::from(base.clone())) as ArrayRef,
6484        ];
6485
6486        for arr in duration_arrays {
6487            assert!(can_cast_types(arr.data_type(), &DataType::Int64));
6488            let result = cast(&arr, &DataType::Int64).unwrap();
6489            let result = result.as_primitive::<Int64Type>();
6490            assert_eq!(base.as_slice(), result.values());
6491        }
6492    }
6493
6494    #[test]
6495    fn test_cast_between_durations_and_numerics() {
6496        fn test_cast_between_durations<FromType, ToType>()
6497        where
6498            FromType: ArrowPrimitiveType<Native = i64>,
6499            ToType: ArrowPrimitiveType<Native = i64>,
6500            PrimitiveArray<FromType>: From<Vec<Option<i64>>>,
6501        {
6502            let from_unit = match FromType::DATA_TYPE {
6503                DataType::Duration(unit) => unit,
6504                _ => panic!("Expected a duration type"),
6505            };
6506            let to_unit = match ToType::DATA_TYPE {
6507                DataType::Duration(unit) => unit,
6508                _ => panic!("Expected a duration type"),
6509            };
6510            let from_size = time_unit_multiple(&from_unit);
6511            let to_size = time_unit_multiple(&to_unit);
6512
6513            let (v1_before, v2_before) = (8640003005, 1696002001);
6514            let (v1_after, v2_after) = if from_size >= to_size {
6515                (
6516                    v1_before / (from_size / to_size),
6517                    v2_before / (from_size / to_size),
6518                )
6519            } else {
6520                (
6521                    v1_before * (to_size / from_size),
6522                    v2_before * (to_size / from_size),
6523                )
6524            };
6525
6526            let array =
6527                PrimitiveArray::<FromType>::from(vec![Some(v1_before), Some(v2_before), None]);
6528            let b = cast(&array, &ToType::DATA_TYPE).unwrap();
6529            let c = b.as_primitive::<ToType>();
6530            assert_eq!(v1_after, c.value(0));
6531            assert_eq!(v2_after, c.value(1));
6532            assert!(c.is_null(2));
6533        }
6534
6535        // between each individual duration type
6536        test_cast_between_durations::<DurationSecondType, DurationMillisecondType>();
6537        test_cast_between_durations::<DurationSecondType, DurationMicrosecondType>();
6538        test_cast_between_durations::<DurationSecondType, DurationNanosecondType>();
6539        test_cast_between_durations::<DurationMillisecondType, DurationSecondType>();
6540        test_cast_between_durations::<DurationMillisecondType, DurationMicrosecondType>();
6541        test_cast_between_durations::<DurationMillisecondType, DurationNanosecondType>();
6542        test_cast_between_durations::<DurationMicrosecondType, DurationSecondType>();
6543        test_cast_between_durations::<DurationMicrosecondType, DurationMillisecondType>();
6544        test_cast_between_durations::<DurationMicrosecondType, DurationNanosecondType>();
6545        test_cast_between_durations::<DurationNanosecondType, DurationSecondType>();
6546        test_cast_between_durations::<DurationNanosecondType, DurationMillisecondType>();
6547        test_cast_between_durations::<DurationNanosecondType, DurationMicrosecondType>();
6548
6549        // cast failed
6550        let array = DurationSecondArray::from(vec![
6551            Some(i64::MAX),
6552            Some(8640203410378005),
6553            Some(10241096),
6554            None,
6555        ]);
6556        let b = cast(&array, &DataType::Duration(TimeUnit::Nanosecond)).unwrap();
6557        let c = b.as_primitive::<DurationNanosecondType>();
6558        assert!(c.is_null(0));
6559        assert!(c.is_null(1));
6560        assert_eq!(10241096000000000, c.value(2));
6561        assert!(c.is_null(3));
6562
6563        // durations to numerics
6564        let array = DurationSecondArray::from(vec![
6565            Some(i64::MAX),
6566            Some(8640203410378005),
6567            Some(10241096),
6568            None,
6569        ]);
6570        let b = cast(&array, &DataType::Int64).unwrap();
6571        let c = b.as_primitive::<Int64Type>();
6572        assert_eq!(i64::MAX, c.value(0));
6573        assert_eq!(8640203410378005, c.value(1));
6574        assert_eq!(10241096, c.value(2));
6575        assert!(c.is_null(3));
6576
6577        let b = cast(&array, &DataType::Int32).unwrap();
6578        let c = b.as_primitive::<Int32Type>();
6579        assert_eq!(0, c.value(0));
6580        assert_eq!(0, c.value(1));
6581        assert_eq!(10241096, c.value(2));
6582        assert!(c.is_null(3));
6583
6584        // numerics to durations
6585        let array = Int32Array::from(vec![Some(i32::MAX), Some(802034103), Some(10241096), None]);
6586        let b = cast(&array, &DataType::Duration(TimeUnit::Second)).unwrap();
6587        let c = b.as_any().downcast_ref::<DurationSecondArray>().unwrap();
6588        assert_eq!(i32::MAX as i64, c.value(0));
6589        assert_eq!(802034103, c.value(1));
6590        assert_eq!(10241096, c.value(2));
6591        assert!(c.is_null(3));
6592    }
6593
6594    #[test]
6595    fn test_cast_to_strings() {
6596        let a = Int32Array::from(vec![1, 2, 3]);
6597        let out = cast(&a, &DataType::Utf8).unwrap();
6598        let out = out
6599            .as_any()
6600            .downcast_ref::<StringArray>()
6601            .unwrap()
6602            .into_iter()
6603            .collect::<Vec<_>>();
6604        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
6605        let out = cast(&a, &DataType::LargeUtf8).unwrap();
6606        let out = out
6607            .as_any()
6608            .downcast_ref::<LargeStringArray>()
6609            .unwrap()
6610            .into_iter()
6611            .collect::<Vec<_>>();
6612        assert_eq!(out, vec![Some("1"), Some("2"), Some("3")]);
6613    }
6614
6615    #[test]
6616    fn test_str_to_str_casts() {
6617        for data in [
6618            vec![Some("foo"), Some("bar"), Some("ham")],
6619            vec![Some("foo"), None, Some("bar")],
6620        ] {
6621            let a = LargeStringArray::from(data.clone());
6622            let to = cast(&a, &DataType::Utf8).unwrap();
6623            let expect = a
6624                .as_any()
6625                .downcast_ref::<LargeStringArray>()
6626                .unwrap()
6627                .into_iter()
6628                .collect::<Vec<_>>();
6629            let out = to
6630                .as_any()
6631                .downcast_ref::<StringArray>()
6632                .unwrap()
6633                .into_iter()
6634                .collect::<Vec<_>>();
6635            assert_eq!(expect, out);
6636
6637            let a = StringArray::from(data);
6638            let to = cast(&a, &DataType::LargeUtf8).unwrap();
6639            let expect = a
6640                .as_any()
6641                .downcast_ref::<StringArray>()
6642                .unwrap()
6643                .into_iter()
6644                .collect::<Vec<_>>();
6645            let out = to
6646                .as_any()
6647                .downcast_ref::<LargeStringArray>()
6648                .unwrap()
6649                .into_iter()
6650                .collect::<Vec<_>>();
6651            assert_eq!(expect, out);
6652        }
6653    }
6654
6655    const VIEW_TEST_DATA: [Option<&str>; 5] = [
6656        Some("hello"),
6657        Some("repeated"),
6658        None,
6659        Some("large payload over 12 bytes"),
6660        Some("repeated"),
6661    ];
6662
6663    #[test]
6664    fn test_string_view_to_binary_view() {
6665        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6666
6667        assert!(can_cast_types(
6668            string_view_array.data_type(),
6669            &DataType::BinaryView
6670        ));
6671
6672        let binary_view_array = cast(&string_view_array, &DataType::BinaryView).unwrap();
6673        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
6674
6675        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6676        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
6677    }
6678
6679    #[test]
6680    fn test_binary_view_to_string_view() {
6681        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6682
6683        assert!(can_cast_types(
6684            binary_view_array.data_type(),
6685            &DataType::Utf8View
6686        ));
6687
6688        let string_view_array = cast(&binary_view_array, &DataType::Utf8View).unwrap();
6689        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6690
6691        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6692        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
6693    }
6694
6695    #[test]
6696    fn test_binary_view_to_string_view_with_invalid_utf8() {
6697        let binary_view_array = BinaryViewArray::from_iter(vec![
6698            Some("valid".as_bytes()),
6699            Some(&[0xff]),
6700            Some("utf8".as_bytes()),
6701            None,
6702        ]);
6703
6704        let strict_options = CastOptions {
6705            safe: false,
6706            ..Default::default()
6707        };
6708
6709        assert!(
6710            cast_with_options(&binary_view_array, &DataType::Utf8View, &strict_options).is_err()
6711        );
6712
6713        let safe_options = CastOptions {
6714            safe: true,
6715            ..Default::default()
6716        };
6717
6718        let string_view_array =
6719            cast_with_options(&binary_view_array, &DataType::Utf8View, &safe_options).unwrap();
6720        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6721
6722        let values: Vec<_> = string_view_array.as_string_view().iter().collect();
6723
6724        assert_eq!(values, vec![Some("valid"), None, Some("utf8"), None]);
6725    }
6726
6727    #[test]
6728    fn test_string_to_view() {
6729        _test_string_to_view::<i32>();
6730        _test_string_to_view::<i64>();
6731    }
6732
6733    fn _test_string_to_view<O>()
6734    where
6735        O: OffsetSizeTrait,
6736    {
6737        let string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
6738
6739        assert!(can_cast_types(
6740            string_array.data_type(),
6741            &DataType::Utf8View
6742        ));
6743
6744        assert!(can_cast_types(
6745            string_array.data_type(),
6746            &DataType::BinaryView
6747        ));
6748
6749        let string_view_array = cast(&string_array, &DataType::Utf8View).unwrap();
6750        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6751
6752        let binary_view_array = cast(&string_array, &DataType::BinaryView).unwrap();
6753        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
6754
6755        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6756        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
6757
6758        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6759        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
6760    }
6761
6762    #[test]
6763    fn test_bianry_to_view() {
6764        _test_binary_to_view::<i32>();
6765        _test_binary_to_view::<i64>();
6766    }
6767
6768    fn _test_binary_to_view<O>()
6769    where
6770        O: OffsetSizeTrait,
6771    {
6772        let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
6773
6774        assert!(can_cast_types(
6775            binary_array.data_type(),
6776            &DataType::Utf8View
6777        ));
6778
6779        assert!(can_cast_types(
6780            binary_array.data_type(),
6781            &DataType::BinaryView
6782        ));
6783
6784        let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
6785        assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
6786
6787        let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
6788        assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
6789
6790        let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6791        assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
6792
6793        let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6794        assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
6795    }
6796
6797    #[test]
6798    fn test_dict_to_view() {
6799        let values = StringArray::from_iter(VIEW_TEST_DATA);
6800        let keys = Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
6801        let string_dict_array =
6802            DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
6803        let typed_dict = string_dict_array.downcast_dict::<StringArray>().unwrap();
6804
6805        let string_view_array = {
6806            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
6807            for v in typed_dict.into_iter() {
6808                builder.append_option(v);
6809            }
6810            builder.finish()
6811        };
6812        let expected_string_array_type = string_view_array.data_type();
6813        let casted_string_array = cast(&string_dict_array, expected_string_array_type).unwrap();
6814        assert_eq!(casted_string_array.data_type(), expected_string_array_type);
6815        assert_eq!(casted_string_array.as_ref(), &string_view_array);
6816
6817        let binary_buffer = cast(&typed_dict.values(), &DataType::Binary).unwrap();
6818        let binary_dict_array =
6819            DictionaryArray::<Int8Type>::new(typed_dict.keys().clone(), binary_buffer);
6820        let typed_binary_dict = binary_dict_array.downcast_dict::<BinaryArray>().unwrap();
6821
6822        let binary_view_array = {
6823            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
6824            for v in typed_binary_dict.into_iter() {
6825                builder.append_option(v);
6826            }
6827            builder.finish()
6828        };
6829        let expected_binary_array_type = binary_view_array.data_type();
6830        let casted_binary_array = cast(&binary_dict_array, expected_binary_array_type).unwrap();
6831        assert_eq!(casted_binary_array.data_type(), expected_binary_array_type);
6832        assert_eq!(casted_binary_array.as_ref(), &binary_view_array);
6833    }
6834
6835    #[test]
6836    fn test_view_to_dict() {
6837        let string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
6838        let string_dict_array: DictionaryArray<Int8Type> = VIEW_TEST_DATA.into_iter().collect();
6839        let casted_type = string_dict_array.data_type();
6840        let casted_dict_array = cast(&string_view_array, casted_type).unwrap();
6841        assert_eq!(casted_dict_array.data_type(), casted_type);
6842        assert_eq!(casted_dict_array.as_ref(), &string_dict_array);
6843
6844        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6845        let binary_dict_array = string_dict_array.downcast_dict::<StringArray>().unwrap();
6846        let binary_buffer = cast(&binary_dict_array.values(), &DataType::Binary).unwrap();
6847        let binary_dict_array =
6848            DictionaryArray::<Int8Type>::new(binary_dict_array.keys().clone(), binary_buffer);
6849        let casted_type = binary_dict_array.data_type();
6850        let casted_binary_array = cast(&binary_view_array, casted_type).unwrap();
6851        assert_eq!(casted_binary_array.data_type(), casted_type);
6852        assert_eq!(casted_binary_array.as_ref(), &binary_dict_array);
6853    }
6854
6855    #[test]
6856    fn test_view_to_string() {
6857        _test_view_to_string::<i32>();
6858        _test_view_to_string::<i64>();
6859    }
6860
6861    fn _test_view_to_string<O>()
6862    where
6863        O: OffsetSizeTrait,
6864    {
6865        let string_view_array = {
6866            let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
6867            for s in VIEW_TEST_DATA.iter() {
6868                builder.append_option(*s);
6869            }
6870            builder.finish()
6871        };
6872
6873        let binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
6874
6875        let expected_string_array = GenericStringArray::<O>::from_iter(VIEW_TEST_DATA);
6876        let expected_type = expected_string_array.data_type();
6877
6878        assert!(can_cast_types(string_view_array.data_type(), expected_type));
6879        assert!(can_cast_types(binary_view_array.data_type(), expected_type));
6880
6881        let string_view_casted_array = cast(&string_view_array, expected_type).unwrap();
6882        assert_eq!(string_view_casted_array.data_type(), expected_type);
6883        assert_eq!(string_view_casted_array.as_ref(), &expected_string_array);
6884
6885        let binary_view_casted_array = cast(&binary_view_array, expected_type).unwrap();
6886        assert_eq!(binary_view_casted_array.data_type(), expected_type);
6887        assert_eq!(binary_view_casted_array.as_ref(), &expected_string_array);
6888    }
6889
6890    #[test]
6891    fn test_view_to_binary() {
6892        _test_view_to_binary::<i32>();
6893        _test_view_to_binary::<i64>();
6894    }
6895
6896    fn _test_view_to_binary<O>()
6897    where
6898        O: OffsetSizeTrait,
6899    {
6900        let view_array = {
6901            let mut builder = BinaryViewBuilder::new().with_fixed_block_size(8); // multiple buffers.
6902            for s in VIEW_TEST_DATA.iter() {
6903                builder.append_option(*s);
6904            }
6905            builder.finish()
6906        };
6907
6908        let expected_binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
6909        let expected_type = expected_binary_array.data_type();
6910
6911        assert!(can_cast_types(view_array.data_type(), expected_type));
6912
6913        let binary_array = cast(&view_array, expected_type).unwrap();
6914        assert_eq!(binary_array.data_type(), expected_type);
6915
6916        assert_eq!(binary_array.as_ref(), &expected_binary_array);
6917    }
6918
6919    #[test]
6920    fn test_cast_from_f64() {
6921        let f64_values: Vec<f64> = vec![
6922            i64::MIN as f64,
6923            i32::MIN as f64,
6924            i16::MIN as f64,
6925            i8::MIN as f64,
6926            0_f64,
6927            u8::MAX as f64,
6928            u16::MAX as f64,
6929            u32::MAX as f64,
6930            u64::MAX as f64,
6931        ];
6932        let f64_array: ArrayRef = Arc::new(Float64Array::from(f64_values));
6933
6934        let f64_expected = vec![
6935            -9223372036854776000.0,
6936            -2147483648.0,
6937            -32768.0,
6938            -128.0,
6939            0.0,
6940            255.0,
6941            65535.0,
6942            4294967295.0,
6943            18446744073709552000.0,
6944        ];
6945        assert_eq!(
6946            f64_expected,
6947            get_cast_values::<Float64Type>(&f64_array, &DataType::Float64)
6948                .iter()
6949                .map(|i| i.parse::<f64>().unwrap())
6950                .collect::<Vec<f64>>()
6951        );
6952
6953        let f32_expected = vec![
6954            -9223372000000000000.0,
6955            -2147483600.0,
6956            -32768.0,
6957            -128.0,
6958            0.0,
6959            255.0,
6960            65535.0,
6961            4294967300.0,
6962            18446744000000000000.0,
6963        ];
6964        assert_eq!(
6965            f32_expected,
6966            get_cast_values::<Float32Type>(&f64_array, &DataType::Float32)
6967                .iter()
6968                .map(|i| i.parse::<f32>().unwrap())
6969                .collect::<Vec<f32>>()
6970        );
6971
6972        let f16_expected = vec![
6973            f16::from_f64(-9223372000000000000.0),
6974            f16::from_f64(-2147483600.0),
6975            f16::from_f64(-32768.0),
6976            f16::from_f64(-128.0),
6977            f16::from_f64(0.0),
6978            f16::from_f64(255.0),
6979            f16::from_f64(65535.0),
6980            f16::from_f64(4294967300.0),
6981            f16::from_f64(18446744000000000000.0),
6982        ];
6983        assert_eq!(
6984            f16_expected,
6985            get_cast_values::<Float16Type>(&f64_array, &DataType::Float16)
6986                .iter()
6987                .map(|i| i.parse::<f16>().unwrap())
6988                .collect::<Vec<f16>>()
6989        );
6990
6991        let i64_expected = vec![
6992            "-9223372036854775808",
6993            "-2147483648",
6994            "-32768",
6995            "-128",
6996            "0",
6997            "255",
6998            "65535",
6999            "4294967295",
7000            "null",
7001        ];
7002        assert_eq!(
7003            i64_expected,
7004            get_cast_values::<Int64Type>(&f64_array, &DataType::Int64)
7005        );
7006
7007        let i32_expected = vec![
7008            "null",
7009            "-2147483648",
7010            "-32768",
7011            "-128",
7012            "0",
7013            "255",
7014            "65535",
7015            "null",
7016            "null",
7017        ];
7018        assert_eq!(
7019            i32_expected,
7020            get_cast_values::<Int32Type>(&f64_array, &DataType::Int32)
7021        );
7022
7023        let i16_expected = vec![
7024            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7025        ];
7026        assert_eq!(
7027            i16_expected,
7028            get_cast_values::<Int16Type>(&f64_array, &DataType::Int16)
7029        );
7030
7031        let i8_expected = vec![
7032            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7033        ];
7034        assert_eq!(
7035            i8_expected,
7036            get_cast_values::<Int8Type>(&f64_array, &DataType::Int8)
7037        );
7038
7039        let u64_expected = vec![
7040            "null",
7041            "null",
7042            "null",
7043            "null",
7044            "0",
7045            "255",
7046            "65535",
7047            "4294967295",
7048            "null",
7049        ];
7050        assert_eq!(
7051            u64_expected,
7052            get_cast_values::<UInt64Type>(&f64_array, &DataType::UInt64)
7053        );
7054
7055        let u32_expected = vec![
7056            "null",
7057            "null",
7058            "null",
7059            "null",
7060            "0",
7061            "255",
7062            "65535",
7063            "4294967295",
7064            "null",
7065        ];
7066        assert_eq!(
7067            u32_expected,
7068            get_cast_values::<UInt32Type>(&f64_array, &DataType::UInt32)
7069        );
7070
7071        let u16_expected = vec![
7072            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7073        ];
7074        assert_eq!(
7075            u16_expected,
7076            get_cast_values::<UInt16Type>(&f64_array, &DataType::UInt16)
7077        );
7078
7079        let u8_expected = vec![
7080            "null", "null", "null", "null", "0", "255", "null", "null", "null",
7081        ];
7082        assert_eq!(
7083            u8_expected,
7084            get_cast_values::<UInt8Type>(&f64_array, &DataType::UInt8)
7085        );
7086    }
7087
7088    #[test]
7089    fn test_cast_from_f32() {
7090        let f32_values: Vec<f32> = vec![
7091            i32::MIN as f32,
7092            i32::MIN as f32,
7093            i16::MIN as f32,
7094            i8::MIN as f32,
7095            0_f32,
7096            u8::MAX as f32,
7097            u16::MAX as f32,
7098            u32::MAX as f32,
7099            u32::MAX as f32,
7100        ];
7101        let f32_array: ArrayRef = Arc::new(Float32Array::from(f32_values));
7102
7103        let f64_expected = vec![
7104            "-2147483648.0",
7105            "-2147483648.0",
7106            "-32768.0",
7107            "-128.0",
7108            "0.0",
7109            "255.0",
7110            "65535.0",
7111            "4294967296.0",
7112            "4294967296.0",
7113        ];
7114        assert_eq!(
7115            f64_expected,
7116            get_cast_values::<Float64Type>(&f32_array, &DataType::Float64)
7117        );
7118
7119        let f32_expected = vec![
7120            "-2147483600.0",
7121            "-2147483600.0",
7122            "-32768.0",
7123            "-128.0",
7124            "0.0",
7125            "255.0",
7126            "65535.0",
7127            "4294967300.0",
7128            "4294967300.0",
7129        ];
7130        assert_eq!(
7131            f32_expected,
7132            get_cast_values::<Float32Type>(&f32_array, &DataType::Float32)
7133        );
7134
7135        let f16_expected = vec![
7136            "-inf", "-inf", "-32768.0", "-128.0", "0.0", "255.0", "inf", "inf", "inf",
7137        ];
7138        assert_eq!(
7139            f16_expected,
7140            get_cast_values::<Float16Type>(&f32_array, &DataType::Float16)
7141        );
7142
7143        let i64_expected = vec![
7144            "-2147483648",
7145            "-2147483648",
7146            "-32768",
7147            "-128",
7148            "0",
7149            "255",
7150            "65535",
7151            "4294967296",
7152            "4294967296",
7153        ];
7154        assert_eq!(
7155            i64_expected,
7156            get_cast_values::<Int64Type>(&f32_array, &DataType::Int64)
7157        );
7158
7159        let i32_expected = vec![
7160            "-2147483648",
7161            "-2147483648",
7162            "-32768",
7163            "-128",
7164            "0",
7165            "255",
7166            "65535",
7167            "null",
7168            "null",
7169        ];
7170        assert_eq!(
7171            i32_expected,
7172            get_cast_values::<Int32Type>(&f32_array, &DataType::Int32)
7173        );
7174
7175        let i16_expected = vec![
7176            "null", "null", "-32768", "-128", "0", "255", "null", "null", "null",
7177        ];
7178        assert_eq!(
7179            i16_expected,
7180            get_cast_values::<Int16Type>(&f32_array, &DataType::Int16)
7181        );
7182
7183        let i8_expected = vec![
7184            "null", "null", "null", "-128", "0", "null", "null", "null", "null",
7185        ];
7186        assert_eq!(
7187            i8_expected,
7188            get_cast_values::<Int8Type>(&f32_array, &DataType::Int8)
7189        );
7190
7191        let u64_expected = vec![
7192            "null",
7193            "null",
7194            "null",
7195            "null",
7196            "0",
7197            "255",
7198            "65535",
7199            "4294967296",
7200            "4294967296",
7201        ];
7202        assert_eq!(
7203            u64_expected,
7204            get_cast_values::<UInt64Type>(&f32_array, &DataType::UInt64)
7205        );
7206
7207        let u32_expected = vec![
7208            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7209        ];
7210        assert_eq!(
7211            u32_expected,
7212            get_cast_values::<UInt32Type>(&f32_array, &DataType::UInt32)
7213        );
7214
7215        let u16_expected = vec![
7216            "null", "null", "null", "null", "0", "255", "65535", "null", "null",
7217        ];
7218        assert_eq!(
7219            u16_expected,
7220            get_cast_values::<UInt16Type>(&f32_array, &DataType::UInt16)
7221        );
7222
7223        let u8_expected = vec![
7224            "null", "null", "null", "null", "0", "255", "null", "null", "null",
7225        ];
7226        assert_eq!(
7227            u8_expected,
7228            get_cast_values::<UInt8Type>(&f32_array, &DataType::UInt8)
7229        );
7230    }
7231
7232    #[test]
7233    fn test_cast_from_uint64() {
7234        let u64_values: Vec<u64> = vec![
7235            0,
7236            u8::MAX as u64,
7237            u16::MAX as u64,
7238            u32::MAX as u64,
7239            u64::MAX,
7240        ];
7241        let u64_array: ArrayRef = Arc::new(UInt64Array::from(u64_values));
7242
7243        let f64_expected = vec![0.0, 255.0, 65535.0, 4294967295.0, 18446744073709552000.0];
7244        assert_eq!(
7245            f64_expected,
7246            get_cast_values::<Float64Type>(&u64_array, &DataType::Float64)
7247                .iter()
7248                .map(|i| i.parse::<f64>().unwrap())
7249                .collect::<Vec<f64>>()
7250        );
7251
7252        let f32_expected = vec![0.0, 255.0, 65535.0, 4294967300.0, 18446744000000000000.0];
7253        assert_eq!(
7254            f32_expected,
7255            get_cast_values::<Float32Type>(&u64_array, &DataType::Float32)
7256                .iter()
7257                .map(|i| i.parse::<f32>().unwrap())
7258                .collect::<Vec<f32>>()
7259        );
7260
7261        let f16_expected = vec![
7262            f16::from_f64(0.0),
7263            f16::from_f64(255.0),
7264            f16::from_f64(65535.0),
7265            f16::from_f64(4294967300.0),
7266            f16::from_f64(18446744000000000000.0),
7267        ];
7268        assert_eq!(
7269            f16_expected,
7270            get_cast_values::<Float16Type>(&u64_array, &DataType::Float16)
7271                .iter()
7272                .map(|i| i.parse::<f16>().unwrap())
7273                .collect::<Vec<f16>>()
7274        );
7275
7276        let i64_expected = vec!["0", "255", "65535", "4294967295", "null"];
7277        assert_eq!(
7278            i64_expected,
7279            get_cast_values::<Int64Type>(&u64_array, &DataType::Int64)
7280        );
7281
7282        let i32_expected = vec!["0", "255", "65535", "null", "null"];
7283        assert_eq!(
7284            i32_expected,
7285            get_cast_values::<Int32Type>(&u64_array, &DataType::Int32)
7286        );
7287
7288        let i16_expected = vec!["0", "255", "null", "null", "null"];
7289        assert_eq!(
7290            i16_expected,
7291            get_cast_values::<Int16Type>(&u64_array, &DataType::Int16)
7292        );
7293
7294        let i8_expected = vec!["0", "null", "null", "null", "null"];
7295        assert_eq!(
7296            i8_expected,
7297            get_cast_values::<Int8Type>(&u64_array, &DataType::Int8)
7298        );
7299
7300        let u64_expected = vec!["0", "255", "65535", "4294967295", "18446744073709551615"];
7301        assert_eq!(
7302            u64_expected,
7303            get_cast_values::<UInt64Type>(&u64_array, &DataType::UInt64)
7304        );
7305
7306        let u32_expected = vec!["0", "255", "65535", "4294967295", "null"];
7307        assert_eq!(
7308            u32_expected,
7309            get_cast_values::<UInt32Type>(&u64_array, &DataType::UInt32)
7310        );
7311
7312        let u16_expected = vec!["0", "255", "65535", "null", "null"];
7313        assert_eq!(
7314            u16_expected,
7315            get_cast_values::<UInt16Type>(&u64_array, &DataType::UInt16)
7316        );
7317
7318        let u8_expected = vec!["0", "255", "null", "null", "null"];
7319        assert_eq!(
7320            u8_expected,
7321            get_cast_values::<UInt8Type>(&u64_array, &DataType::UInt8)
7322        );
7323    }
7324
7325    #[test]
7326    fn test_cast_from_uint32() {
7327        let u32_values: Vec<u32> = vec![0, u8::MAX as u32, u16::MAX as u32, u32::MAX];
7328        let u32_array: ArrayRef = Arc::new(UInt32Array::from(u32_values));
7329
7330        let f64_expected = vec!["0.0", "255.0", "65535.0", "4294967295.0"];
7331        assert_eq!(
7332            f64_expected,
7333            get_cast_values::<Float64Type>(&u32_array, &DataType::Float64)
7334        );
7335
7336        let f32_expected = vec!["0.0", "255.0", "65535.0", "4294967300.0"];
7337        assert_eq!(
7338            f32_expected,
7339            get_cast_values::<Float32Type>(&u32_array, &DataType::Float32)
7340        );
7341
7342        let f16_expected = vec!["0.0", "255.0", "inf", "inf"];
7343        assert_eq!(
7344            f16_expected,
7345            get_cast_values::<Float16Type>(&u32_array, &DataType::Float16)
7346        );
7347
7348        let i64_expected = vec!["0", "255", "65535", "4294967295"];
7349        assert_eq!(
7350            i64_expected,
7351            get_cast_values::<Int64Type>(&u32_array, &DataType::Int64)
7352        );
7353
7354        let i32_expected = vec!["0", "255", "65535", "null"];
7355        assert_eq!(
7356            i32_expected,
7357            get_cast_values::<Int32Type>(&u32_array, &DataType::Int32)
7358        );
7359
7360        let i16_expected = vec!["0", "255", "null", "null"];
7361        assert_eq!(
7362            i16_expected,
7363            get_cast_values::<Int16Type>(&u32_array, &DataType::Int16)
7364        );
7365
7366        let i8_expected = vec!["0", "null", "null", "null"];
7367        assert_eq!(
7368            i8_expected,
7369            get_cast_values::<Int8Type>(&u32_array, &DataType::Int8)
7370        );
7371
7372        let u64_expected = vec!["0", "255", "65535", "4294967295"];
7373        assert_eq!(
7374            u64_expected,
7375            get_cast_values::<UInt64Type>(&u32_array, &DataType::UInt64)
7376        );
7377
7378        let u32_expected = vec!["0", "255", "65535", "4294967295"];
7379        assert_eq!(
7380            u32_expected,
7381            get_cast_values::<UInt32Type>(&u32_array, &DataType::UInt32)
7382        );
7383
7384        let u16_expected = vec!["0", "255", "65535", "null"];
7385        assert_eq!(
7386            u16_expected,
7387            get_cast_values::<UInt16Type>(&u32_array, &DataType::UInt16)
7388        );
7389
7390        let u8_expected = vec!["0", "255", "null", "null"];
7391        assert_eq!(
7392            u8_expected,
7393            get_cast_values::<UInt8Type>(&u32_array, &DataType::UInt8)
7394        );
7395    }
7396
7397    #[test]
7398    fn test_cast_from_uint16() {
7399        let u16_values: Vec<u16> = vec![0, u8::MAX as u16, u16::MAX];
7400        let u16_array: ArrayRef = Arc::new(UInt16Array::from(u16_values));
7401
7402        let f64_expected = vec!["0.0", "255.0", "65535.0"];
7403        assert_eq!(
7404            f64_expected,
7405            get_cast_values::<Float64Type>(&u16_array, &DataType::Float64)
7406        );
7407
7408        let f32_expected = vec!["0.0", "255.0", "65535.0"];
7409        assert_eq!(
7410            f32_expected,
7411            get_cast_values::<Float32Type>(&u16_array, &DataType::Float32)
7412        );
7413
7414        let f16_expected = vec!["0.0", "255.0", "inf"];
7415        assert_eq!(
7416            f16_expected,
7417            get_cast_values::<Float16Type>(&u16_array, &DataType::Float16)
7418        );
7419
7420        let i64_expected = vec!["0", "255", "65535"];
7421        assert_eq!(
7422            i64_expected,
7423            get_cast_values::<Int64Type>(&u16_array, &DataType::Int64)
7424        );
7425
7426        let i32_expected = vec!["0", "255", "65535"];
7427        assert_eq!(
7428            i32_expected,
7429            get_cast_values::<Int32Type>(&u16_array, &DataType::Int32)
7430        );
7431
7432        let i16_expected = vec!["0", "255", "null"];
7433        assert_eq!(
7434            i16_expected,
7435            get_cast_values::<Int16Type>(&u16_array, &DataType::Int16)
7436        );
7437
7438        let i8_expected = vec!["0", "null", "null"];
7439        assert_eq!(
7440            i8_expected,
7441            get_cast_values::<Int8Type>(&u16_array, &DataType::Int8)
7442        );
7443
7444        let u64_expected = vec!["0", "255", "65535"];
7445        assert_eq!(
7446            u64_expected,
7447            get_cast_values::<UInt64Type>(&u16_array, &DataType::UInt64)
7448        );
7449
7450        let u32_expected = vec!["0", "255", "65535"];
7451        assert_eq!(
7452            u32_expected,
7453            get_cast_values::<UInt32Type>(&u16_array, &DataType::UInt32)
7454        );
7455
7456        let u16_expected = vec!["0", "255", "65535"];
7457        assert_eq!(
7458            u16_expected,
7459            get_cast_values::<UInt16Type>(&u16_array, &DataType::UInt16)
7460        );
7461
7462        let u8_expected = vec!["0", "255", "null"];
7463        assert_eq!(
7464            u8_expected,
7465            get_cast_values::<UInt8Type>(&u16_array, &DataType::UInt8)
7466        );
7467    }
7468
7469    #[test]
7470    fn test_cast_from_uint8() {
7471        let u8_values: Vec<u8> = vec![0, u8::MAX];
7472        let u8_array: ArrayRef = Arc::new(UInt8Array::from(u8_values));
7473
7474        let f64_expected = vec!["0.0", "255.0"];
7475        assert_eq!(
7476            f64_expected,
7477            get_cast_values::<Float64Type>(&u8_array, &DataType::Float64)
7478        );
7479
7480        let f32_expected = vec!["0.0", "255.0"];
7481        assert_eq!(
7482            f32_expected,
7483            get_cast_values::<Float32Type>(&u8_array, &DataType::Float32)
7484        );
7485
7486        let f16_expected = vec!["0.0", "255.0"];
7487        assert_eq!(
7488            f16_expected,
7489            get_cast_values::<Float16Type>(&u8_array, &DataType::Float16)
7490        );
7491
7492        let i64_expected = vec!["0", "255"];
7493        assert_eq!(
7494            i64_expected,
7495            get_cast_values::<Int64Type>(&u8_array, &DataType::Int64)
7496        );
7497
7498        let i32_expected = vec!["0", "255"];
7499        assert_eq!(
7500            i32_expected,
7501            get_cast_values::<Int32Type>(&u8_array, &DataType::Int32)
7502        );
7503
7504        let i16_expected = vec!["0", "255"];
7505        assert_eq!(
7506            i16_expected,
7507            get_cast_values::<Int16Type>(&u8_array, &DataType::Int16)
7508        );
7509
7510        let i8_expected = vec!["0", "null"];
7511        assert_eq!(
7512            i8_expected,
7513            get_cast_values::<Int8Type>(&u8_array, &DataType::Int8)
7514        );
7515
7516        let u64_expected = vec!["0", "255"];
7517        assert_eq!(
7518            u64_expected,
7519            get_cast_values::<UInt64Type>(&u8_array, &DataType::UInt64)
7520        );
7521
7522        let u32_expected = vec!["0", "255"];
7523        assert_eq!(
7524            u32_expected,
7525            get_cast_values::<UInt32Type>(&u8_array, &DataType::UInt32)
7526        );
7527
7528        let u16_expected = vec!["0", "255"];
7529        assert_eq!(
7530            u16_expected,
7531            get_cast_values::<UInt16Type>(&u8_array, &DataType::UInt16)
7532        );
7533
7534        let u8_expected = vec!["0", "255"];
7535        assert_eq!(
7536            u8_expected,
7537            get_cast_values::<UInt8Type>(&u8_array, &DataType::UInt8)
7538        );
7539    }
7540
7541    #[test]
7542    fn test_cast_from_int64() {
7543        let i64_values: Vec<i64> = vec![
7544            i64::MIN,
7545            i32::MIN as i64,
7546            i16::MIN as i64,
7547            i8::MIN as i64,
7548            0,
7549            i8::MAX as i64,
7550            i16::MAX as i64,
7551            i32::MAX as i64,
7552            i64::MAX,
7553        ];
7554        let i64_array: ArrayRef = Arc::new(Int64Array::from(i64_values));
7555
7556        let f64_expected = vec![
7557            -9223372036854776000.0,
7558            -2147483648.0,
7559            -32768.0,
7560            -128.0,
7561            0.0,
7562            127.0,
7563            32767.0,
7564            2147483647.0,
7565            9223372036854776000.0,
7566        ];
7567        assert_eq!(
7568            f64_expected,
7569            get_cast_values::<Float64Type>(&i64_array, &DataType::Float64)
7570                .iter()
7571                .map(|i| i.parse::<f64>().unwrap())
7572                .collect::<Vec<f64>>()
7573        );
7574
7575        let f32_expected = vec![
7576            -9223372000000000000.0,
7577            -2147483600.0,
7578            -32768.0,
7579            -128.0,
7580            0.0,
7581            127.0,
7582            32767.0,
7583            2147483600.0,
7584            9223372000000000000.0,
7585        ];
7586        assert_eq!(
7587            f32_expected,
7588            get_cast_values::<Float32Type>(&i64_array, &DataType::Float32)
7589                .iter()
7590                .map(|i| i.parse::<f32>().unwrap())
7591                .collect::<Vec<f32>>()
7592        );
7593
7594        let f16_expected = vec![
7595            f16::from_f64(-9223372000000000000.0),
7596            f16::from_f64(-2147483600.0),
7597            f16::from_f64(-32768.0),
7598            f16::from_f64(-128.0),
7599            f16::from_f64(0.0),
7600            f16::from_f64(127.0),
7601            f16::from_f64(32767.0),
7602            f16::from_f64(2147483600.0),
7603            f16::from_f64(9223372000000000000.0),
7604        ];
7605        assert_eq!(
7606            f16_expected,
7607            get_cast_values::<Float16Type>(&i64_array, &DataType::Float16)
7608                .iter()
7609                .map(|i| i.parse::<f16>().unwrap())
7610                .collect::<Vec<f16>>()
7611        );
7612
7613        let i64_expected = vec![
7614            "-9223372036854775808",
7615            "-2147483648",
7616            "-32768",
7617            "-128",
7618            "0",
7619            "127",
7620            "32767",
7621            "2147483647",
7622            "9223372036854775807",
7623        ];
7624        assert_eq!(
7625            i64_expected,
7626            get_cast_values::<Int64Type>(&i64_array, &DataType::Int64)
7627        );
7628
7629        let i32_expected = vec![
7630            "null",
7631            "-2147483648",
7632            "-32768",
7633            "-128",
7634            "0",
7635            "127",
7636            "32767",
7637            "2147483647",
7638            "null",
7639        ];
7640        assert_eq!(
7641            i32_expected,
7642            get_cast_values::<Int32Type>(&i64_array, &DataType::Int32)
7643        );
7644
7645        assert_eq!(
7646            i32_expected,
7647            get_cast_values::<Date32Type>(&i64_array, &DataType::Date32)
7648        );
7649
7650        let i16_expected = vec![
7651            "null", "null", "-32768", "-128", "0", "127", "32767", "null", "null",
7652        ];
7653        assert_eq!(
7654            i16_expected,
7655            get_cast_values::<Int16Type>(&i64_array, &DataType::Int16)
7656        );
7657
7658        let i8_expected = vec![
7659            "null", "null", "null", "-128", "0", "127", "null", "null", "null",
7660        ];
7661        assert_eq!(
7662            i8_expected,
7663            get_cast_values::<Int8Type>(&i64_array, &DataType::Int8)
7664        );
7665
7666        let u64_expected = vec![
7667            "null",
7668            "null",
7669            "null",
7670            "null",
7671            "0",
7672            "127",
7673            "32767",
7674            "2147483647",
7675            "9223372036854775807",
7676        ];
7677        assert_eq!(
7678            u64_expected,
7679            get_cast_values::<UInt64Type>(&i64_array, &DataType::UInt64)
7680        );
7681
7682        let u32_expected = vec![
7683            "null",
7684            "null",
7685            "null",
7686            "null",
7687            "0",
7688            "127",
7689            "32767",
7690            "2147483647",
7691            "null",
7692        ];
7693        assert_eq!(
7694            u32_expected,
7695            get_cast_values::<UInt32Type>(&i64_array, &DataType::UInt32)
7696        );
7697
7698        let u16_expected = vec![
7699            "null", "null", "null", "null", "0", "127", "32767", "null", "null",
7700        ];
7701        assert_eq!(
7702            u16_expected,
7703            get_cast_values::<UInt16Type>(&i64_array, &DataType::UInt16)
7704        );
7705
7706        let u8_expected = vec![
7707            "null", "null", "null", "null", "0", "127", "null", "null", "null",
7708        ];
7709        assert_eq!(
7710            u8_expected,
7711            get_cast_values::<UInt8Type>(&i64_array, &DataType::UInt8)
7712        );
7713    }
7714
7715    #[test]
7716    fn test_cast_from_int32() {
7717        let i32_values: Vec<i32> = vec![
7718            i32::MIN,
7719            i16::MIN as i32,
7720            i8::MIN as i32,
7721            0,
7722            i8::MAX as i32,
7723            i16::MAX as i32,
7724            i32::MAX,
7725        ];
7726        let i32_array: ArrayRef = Arc::new(Int32Array::from(i32_values));
7727
7728        let f64_expected = vec![
7729            "-2147483648.0",
7730            "-32768.0",
7731            "-128.0",
7732            "0.0",
7733            "127.0",
7734            "32767.0",
7735            "2147483647.0",
7736        ];
7737        assert_eq!(
7738            f64_expected,
7739            get_cast_values::<Float64Type>(&i32_array, &DataType::Float64)
7740        );
7741
7742        let f32_expected = vec![
7743            "-2147483600.0",
7744            "-32768.0",
7745            "-128.0",
7746            "0.0",
7747            "127.0",
7748            "32767.0",
7749            "2147483600.0",
7750        ];
7751        assert_eq!(
7752            f32_expected,
7753            get_cast_values::<Float32Type>(&i32_array, &DataType::Float32)
7754        );
7755
7756        let f16_expected = vec![
7757            f16::from_f64(-2147483600.0),
7758            f16::from_f64(-32768.0),
7759            f16::from_f64(-128.0),
7760            f16::from_f64(0.0),
7761            f16::from_f64(127.0),
7762            f16::from_f64(32767.0),
7763            f16::from_f64(2147483600.0),
7764        ];
7765        assert_eq!(
7766            f16_expected,
7767            get_cast_values::<Float16Type>(&i32_array, &DataType::Float16)
7768                .iter()
7769                .map(|i| i.parse::<f16>().unwrap())
7770                .collect::<Vec<f16>>()
7771        );
7772
7773        let i16_expected = vec!["null", "-32768", "-128", "0", "127", "32767", "null"];
7774        assert_eq!(
7775            i16_expected,
7776            get_cast_values::<Int16Type>(&i32_array, &DataType::Int16)
7777        );
7778
7779        let i8_expected = vec!["null", "null", "-128", "0", "127", "null", "null"];
7780        assert_eq!(
7781            i8_expected,
7782            get_cast_values::<Int8Type>(&i32_array, &DataType::Int8)
7783        );
7784
7785        let u64_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
7786        assert_eq!(
7787            u64_expected,
7788            get_cast_values::<UInt64Type>(&i32_array, &DataType::UInt64)
7789        );
7790
7791        let u32_expected = vec!["null", "null", "null", "0", "127", "32767", "2147483647"];
7792        assert_eq!(
7793            u32_expected,
7794            get_cast_values::<UInt32Type>(&i32_array, &DataType::UInt32)
7795        );
7796
7797        let u16_expected = vec!["null", "null", "null", "0", "127", "32767", "null"];
7798        assert_eq!(
7799            u16_expected,
7800            get_cast_values::<UInt16Type>(&i32_array, &DataType::UInt16)
7801        );
7802
7803        let u8_expected = vec!["null", "null", "null", "0", "127", "null", "null"];
7804        assert_eq!(
7805            u8_expected,
7806            get_cast_values::<UInt8Type>(&i32_array, &DataType::UInt8)
7807        );
7808
7809        // The date32 to date64 cast increases the numerical values in order to keep the same dates.
7810        let i64_expected = vec![
7811            "-185542587187200000",
7812            "-2831155200000",
7813            "-11059200000",
7814            "0",
7815            "10972800000",
7816            "2831068800000",
7817            "185542587100800000",
7818        ];
7819        assert_eq!(
7820            i64_expected,
7821            get_cast_values::<Date64Type>(&i32_array, &DataType::Date64)
7822        );
7823    }
7824
7825    #[test]
7826    fn test_cast_from_int16() {
7827        let i16_values: Vec<i16> = vec![i16::MIN, i8::MIN as i16, 0, i8::MAX as i16, i16::MAX];
7828        let i16_array: ArrayRef = Arc::new(Int16Array::from(i16_values));
7829
7830        let f64_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
7831        assert_eq!(
7832            f64_expected,
7833            get_cast_values::<Float64Type>(&i16_array, &DataType::Float64)
7834        );
7835
7836        let f32_expected = vec!["-32768.0", "-128.0", "0.0", "127.0", "32767.0"];
7837        assert_eq!(
7838            f32_expected,
7839            get_cast_values::<Float32Type>(&i16_array, &DataType::Float32)
7840        );
7841
7842        let f16_expected = vec![
7843            f16::from_f64(-32768.0),
7844            f16::from_f64(-128.0),
7845            f16::from_f64(0.0),
7846            f16::from_f64(127.0),
7847            f16::from_f64(32767.0),
7848        ];
7849        assert_eq!(
7850            f16_expected,
7851            get_cast_values::<Float16Type>(&i16_array, &DataType::Float16)
7852                .iter()
7853                .map(|i| i.parse::<f16>().unwrap())
7854                .collect::<Vec<f16>>()
7855        );
7856
7857        let i64_expected = vec!["-32768", "-128", "0", "127", "32767"];
7858        assert_eq!(
7859            i64_expected,
7860            get_cast_values::<Int64Type>(&i16_array, &DataType::Int64)
7861        );
7862
7863        let i32_expected = vec!["-32768", "-128", "0", "127", "32767"];
7864        assert_eq!(
7865            i32_expected,
7866            get_cast_values::<Int32Type>(&i16_array, &DataType::Int32)
7867        );
7868
7869        let i16_expected = vec!["-32768", "-128", "0", "127", "32767"];
7870        assert_eq!(
7871            i16_expected,
7872            get_cast_values::<Int16Type>(&i16_array, &DataType::Int16)
7873        );
7874
7875        let i8_expected = vec!["null", "-128", "0", "127", "null"];
7876        assert_eq!(
7877            i8_expected,
7878            get_cast_values::<Int8Type>(&i16_array, &DataType::Int8)
7879        );
7880
7881        let u64_expected = vec!["null", "null", "0", "127", "32767"];
7882        assert_eq!(
7883            u64_expected,
7884            get_cast_values::<UInt64Type>(&i16_array, &DataType::UInt64)
7885        );
7886
7887        let u32_expected = vec!["null", "null", "0", "127", "32767"];
7888        assert_eq!(
7889            u32_expected,
7890            get_cast_values::<UInt32Type>(&i16_array, &DataType::UInt32)
7891        );
7892
7893        let u16_expected = vec!["null", "null", "0", "127", "32767"];
7894        assert_eq!(
7895            u16_expected,
7896            get_cast_values::<UInt16Type>(&i16_array, &DataType::UInt16)
7897        );
7898
7899        let u8_expected = vec!["null", "null", "0", "127", "null"];
7900        assert_eq!(
7901            u8_expected,
7902            get_cast_values::<UInt8Type>(&i16_array, &DataType::UInt8)
7903        );
7904    }
7905
7906    #[test]
7907    fn test_cast_from_date32() {
7908        let i32_values: Vec<i32> = vec![
7909            i32::MIN,
7910            i16::MIN as i32,
7911            i8::MIN as i32,
7912            0,
7913            i8::MAX as i32,
7914            i16::MAX as i32,
7915            i32::MAX,
7916        ];
7917        let date32_array: ArrayRef = Arc::new(Date32Array::from(i32_values));
7918
7919        let i64_expected = vec![
7920            "-2147483648",
7921            "-32768",
7922            "-128",
7923            "0",
7924            "127",
7925            "32767",
7926            "2147483647",
7927        ];
7928        assert_eq!(
7929            i64_expected,
7930            get_cast_values::<Int64Type>(&date32_array, &DataType::Int64)
7931        );
7932    }
7933
7934    #[test]
7935    fn test_cast_from_int8() {
7936        let i8_values: Vec<i8> = vec![i8::MIN, 0, i8::MAX];
7937        let i8_array = Int8Array::from(i8_values);
7938
7939        let f64_expected = vec!["-128.0", "0.0", "127.0"];
7940        assert_eq!(
7941            f64_expected,
7942            get_cast_values::<Float64Type>(&i8_array, &DataType::Float64)
7943        );
7944
7945        let f32_expected = vec!["-128.0", "0.0", "127.0"];
7946        assert_eq!(
7947            f32_expected,
7948            get_cast_values::<Float32Type>(&i8_array, &DataType::Float32)
7949        );
7950
7951        let f16_expected = vec!["-128.0", "0.0", "127.0"];
7952        assert_eq!(
7953            f16_expected,
7954            get_cast_values::<Float16Type>(&i8_array, &DataType::Float16)
7955        );
7956
7957        let i64_expected = vec!["-128", "0", "127"];
7958        assert_eq!(
7959            i64_expected,
7960            get_cast_values::<Int64Type>(&i8_array, &DataType::Int64)
7961        );
7962
7963        let i32_expected = vec!["-128", "0", "127"];
7964        assert_eq!(
7965            i32_expected,
7966            get_cast_values::<Int32Type>(&i8_array, &DataType::Int32)
7967        );
7968
7969        let i16_expected = vec!["-128", "0", "127"];
7970        assert_eq!(
7971            i16_expected,
7972            get_cast_values::<Int16Type>(&i8_array, &DataType::Int16)
7973        );
7974
7975        let i8_expected = vec!["-128", "0", "127"];
7976        assert_eq!(
7977            i8_expected,
7978            get_cast_values::<Int8Type>(&i8_array, &DataType::Int8)
7979        );
7980
7981        let u64_expected = vec!["null", "0", "127"];
7982        assert_eq!(
7983            u64_expected,
7984            get_cast_values::<UInt64Type>(&i8_array, &DataType::UInt64)
7985        );
7986
7987        let u32_expected = vec!["null", "0", "127"];
7988        assert_eq!(
7989            u32_expected,
7990            get_cast_values::<UInt32Type>(&i8_array, &DataType::UInt32)
7991        );
7992
7993        let u16_expected = vec!["null", "0", "127"];
7994        assert_eq!(
7995            u16_expected,
7996            get_cast_values::<UInt16Type>(&i8_array, &DataType::UInt16)
7997        );
7998
7999        let u8_expected = vec!["null", "0", "127"];
8000        assert_eq!(
8001            u8_expected,
8002            get_cast_values::<UInt8Type>(&i8_array, &DataType::UInt8)
8003        );
8004    }
8005
8006    /// Convert `array` into a vector of strings by casting to data type dt
8007    fn get_cast_values<T>(array: &dyn Array, dt: &DataType) -> Vec<String>
8008    where
8009        T: ArrowPrimitiveType,
8010    {
8011        let c = cast(array, dt).unwrap();
8012        let a = c.as_primitive::<T>();
8013        let mut v: Vec<String> = vec![];
8014        for i in 0..array.len() {
8015            if a.is_null(i) {
8016                v.push("null".to_string())
8017            } else {
8018                v.push(format!("{:?}", a.value(i)));
8019            }
8020        }
8021        v
8022    }
8023
8024    #[test]
8025    fn test_cast_utf8_dict() {
8026        // FROM a dictionary with of Utf8 values
8027        let mut builder = StringDictionaryBuilder::<Int8Type>::new();
8028        builder.append("one").unwrap();
8029        builder.append_null();
8030        builder.append("three").unwrap();
8031        let array: ArrayRef = Arc::new(builder.finish());
8032
8033        let expected = vec!["one", "null", "three"];
8034
8035        // Test casting TO StringArray
8036        let cast_type = Utf8;
8037        let cast_array = cast(&array, &cast_type).expect("cast to UTF-8 failed");
8038        assert_eq!(cast_array.data_type(), &cast_type);
8039        assert_eq!(array_to_strings(&cast_array), expected);
8040
8041        // Test casting TO Dictionary (with different index sizes)
8042
8043        let cast_type = Dictionary(Box::new(Int16), Box::new(Utf8));
8044        let cast_array = cast(&array, &cast_type).expect("cast failed");
8045        assert_eq!(cast_array.data_type(), &cast_type);
8046        assert_eq!(array_to_strings(&cast_array), expected);
8047
8048        let cast_type = Dictionary(Box::new(Int32), Box::new(Utf8));
8049        let cast_array = cast(&array, &cast_type).expect("cast failed");
8050        assert_eq!(cast_array.data_type(), &cast_type);
8051        assert_eq!(array_to_strings(&cast_array), expected);
8052
8053        let cast_type = Dictionary(Box::new(Int64), Box::new(Utf8));
8054        let cast_array = cast(&array, &cast_type).expect("cast failed");
8055        assert_eq!(cast_array.data_type(), &cast_type);
8056        assert_eq!(array_to_strings(&cast_array), expected);
8057
8058        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8059        let cast_array = cast(&array, &cast_type).expect("cast failed");
8060        assert_eq!(cast_array.data_type(), &cast_type);
8061        assert_eq!(array_to_strings(&cast_array), expected);
8062
8063        let cast_type = Dictionary(Box::new(UInt16), Box::new(Utf8));
8064        let cast_array = cast(&array, &cast_type).expect("cast failed");
8065        assert_eq!(cast_array.data_type(), &cast_type);
8066        assert_eq!(array_to_strings(&cast_array), expected);
8067
8068        let cast_type = Dictionary(Box::new(UInt32), Box::new(Utf8));
8069        let cast_array = cast(&array, &cast_type).expect("cast failed");
8070        assert_eq!(cast_array.data_type(), &cast_type);
8071        assert_eq!(array_to_strings(&cast_array), expected);
8072
8073        let cast_type = Dictionary(Box::new(UInt64), Box::new(Utf8));
8074        let cast_array = cast(&array, &cast_type).expect("cast failed");
8075        assert_eq!(cast_array.data_type(), &cast_type);
8076        assert_eq!(array_to_strings(&cast_array), expected);
8077    }
8078
8079    #[test]
8080    fn test_cast_dict_to_dict_bad_index_value_primitive() {
8081        // test converting from an array that has indexes of a type
8082        // that are out of bounds for a particular other kind of
8083        // index.
8084
8085        let mut builder = PrimitiveDictionaryBuilder::<Int32Type, Int64Type>::new();
8086
8087        // add 200 distinct values (which can be stored by a
8088        // dictionary indexed by int32, but not a dictionary indexed
8089        // with int8)
8090        for i in 0..200 {
8091            builder.append(i).unwrap();
8092        }
8093        let array: ArrayRef = Arc::new(builder.finish());
8094
8095        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8096        let res = cast(&array, &cast_type);
8097        assert!(res.is_err());
8098        let actual_error = format!("{res:?}");
8099        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8100        assert!(
8101            actual_error.contains(expected_error),
8102            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8103        );
8104    }
8105
8106    #[test]
8107    fn test_cast_dict_to_dict_bad_index_value_utf8() {
8108        // Same test as test_cast_dict_to_dict_bad_index_value but use
8109        // string values (and encode the expected behavior here);
8110
8111        let mut builder = StringDictionaryBuilder::<Int32Type>::new();
8112
8113        // add 200 distinct values (which can be stored by a
8114        // dictionary indexed by int32, but not a dictionary indexed
8115        // with int8)
8116        for i in 0..200 {
8117            let val = format!("val{i}");
8118            builder.append(&val).unwrap();
8119        }
8120        let array = builder.finish();
8121
8122        let cast_type = Dictionary(Box::new(Int8), Box::new(Utf8));
8123        let res = cast(&array, &cast_type);
8124        assert!(res.is_err());
8125        let actual_error = format!("{res:?}");
8126        let expected_error = "Could not convert 72 dictionary indexes from Int32 to Int8";
8127        assert!(
8128            actual_error.contains(expected_error),
8129            "did not find expected error '{actual_error}' in actual error '{expected_error}'"
8130        );
8131    }
8132
8133    #[test]
8134    fn test_cast_primitive_dict() {
8135        // FROM a dictionary with of INT32 values
8136        let mut builder = PrimitiveDictionaryBuilder::<Int8Type, Int32Type>::new();
8137        builder.append(1).unwrap();
8138        builder.append_null();
8139        builder.append(3).unwrap();
8140        let array: ArrayRef = Arc::new(builder.finish());
8141
8142        let expected = vec!["1", "null", "3"];
8143
8144        // Test casting TO PrimitiveArray, different dictionary type
8145        let cast_array = cast(&array, &Utf8).expect("cast to UTF-8 failed");
8146        assert_eq!(array_to_strings(&cast_array), expected);
8147        assert_eq!(cast_array.data_type(), &Utf8);
8148
8149        let cast_array = cast(&array, &Int64).expect("cast to int64 failed");
8150        assert_eq!(array_to_strings(&cast_array), expected);
8151        assert_eq!(cast_array.data_type(), &Int64);
8152    }
8153
8154    #[test]
8155    fn test_cast_primitive_array_to_dict() {
8156        let mut builder = PrimitiveBuilder::<Int32Type>::new();
8157        builder.append_value(1);
8158        builder.append_null();
8159        builder.append_value(3);
8160        let array: ArrayRef = Arc::new(builder.finish());
8161
8162        let expected = vec!["1", "null", "3"];
8163
8164        // Cast to a dictionary (same value type, Int32)
8165        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int32));
8166        let cast_array = cast(&array, &cast_type).expect("cast failed");
8167        assert_eq!(cast_array.data_type(), &cast_type);
8168        assert_eq!(array_to_strings(&cast_array), expected);
8169
8170        // Cast to a dictionary (different value type, Int8)
8171        let cast_type = Dictionary(Box::new(UInt8), Box::new(Int8));
8172        let cast_array = cast(&array, &cast_type).expect("cast failed");
8173        assert_eq!(cast_array.data_type(), &cast_type);
8174        assert_eq!(array_to_strings(&cast_array), expected);
8175    }
8176
8177    #[test]
8178    fn test_cast_time_array_to_dict() {
8179        use DataType::*;
8180
8181        let array = Arc::new(Date32Array::from(vec![Some(1000), None, Some(2000)])) as ArrayRef;
8182
8183        let expected = vec!["1972-09-27", "null", "1975-06-24"];
8184
8185        let cast_type = Dictionary(Box::new(UInt8), Box::new(Date32));
8186        let cast_array = cast(&array, &cast_type).expect("cast failed");
8187        assert_eq!(cast_array.data_type(), &cast_type);
8188        assert_eq!(array_to_strings(&cast_array), expected);
8189    }
8190
8191    #[test]
8192    fn test_cast_timestamp_array_to_dict() {
8193        use DataType::*;
8194
8195        let array = Arc::new(
8196            TimestampSecondArray::from(vec![Some(1000), None, Some(2000)]).with_timezone_utc(),
8197        ) as ArrayRef;
8198
8199        let expected = vec!["1970-01-01T00:16:40", "null", "1970-01-01T00:33:20"];
8200
8201        let cast_type = Dictionary(Box::new(UInt8), Box::new(Timestamp(TimeUnit::Second, None)));
8202        let cast_array = cast(&array, &cast_type).expect("cast failed");
8203        assert_eq!(cast_array.data_type(), &cast_type);
8204        assert_eq!(array_to_strings(&cast_array), expected);
8205    }
8206
8207    #[test]
8208    fn test_cast_string_array_to_dict() {
8209        use DataType::*;
8210
8211        let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) as ArrayRef;
8212
8213        let expected = vec!["one", "null", "three"];
8214
8215        // Cast to a dictionary (same value type, Utf8)
8216        let cast_type = Dictionary(Box::new(UInt8), Box::new(Utf8));
8217        let cast_array = cast(&array, &cast_type).expect("cast failed");
8218        assert_eq!(cast_array.data_type(), &cast_type);
8219        assert_eq!(array_to_strings(&cast_array), expected);
8220    }
8221
8222    #[test]
8223    fn test_cast_null_array_to_from_decimal_array() {
8224        let data_type = DataType::Decimal128(12, 4);
8225        let array = new_null_array(&DataType::Null, 4);
8226        assert_eq!(array.data_type(), &DataType::Null);
8227        let cast_array = cast(&array, &data_type).expect("cast failed");
8228        assert_eq!(cast_array.data_type(), &data_type);
8229        for i in 0..4 {
8230            assert!(cast_array.is_null(i));
8231        }
8232
8233        let array = new_null_array(&data_type, 4);
8234        assert_eq!(array.data_type(), &data_type);
8235        let cast_array = cast(&array, &DataType::Null).expect("cast failed");
8236        assert_eq!(cast_array.data_type(), &DataType::Null);
8237        assert_eq!(cast_array.len(), 4);
8238        assert_eq!(cast_array.logical_nulls().unwrap().null_count(), 4);
8239    }
8240
8241    #[test]
8242    fn test_cast_null_array_from_and_to_primitive_array() {
8243        macro_rules! typed_test {
8244            ($ARR_TYPE:ident, $DATATYPE:ident, $TYPE:tt) => {{
8245                {
8246                    let array = Arc::new(NullArray::new(6)) as ArrayRef;
8247                    let expected = $ARR_TYPE::from(vec![None; 6]);
8248                    let cast_type = DataType::$DATATYPE;
8249                    let cast_array = cast(&array, &cast_type).expect("cast failed");
8250                    let cast_array = cast_array.as_primitive::<$TYPE>();
8251                    assert_eq!(cast_array.data_type(), &cast_type);
8252                    assert_eq!(cast_array, &expected);
8253                }
8254            }};
8255        }
8256
8257        typed_test!(Int16Array, Int16, Int16Type);
8258        typed_test!(Int32Array, Int32, Int32Type);
8259        typed_test!(Int64Array, Int64, Int64Type);
8260
8261        typed_test!(UInt16Array, UInt16, UInt16Type);
8262        typed_test!(UInt32Array, UInt32, UInt32Type);
8263        typed_test!(UInt64Array, UInt64, UInt64Type);
8264
8265        typed_test!(Float16Array, Float16, Float16Type);
8266        typed_test!(Float32Array, Float32, Float32Type);
8267        typed_test!(Float64Array, Float64, Float64Type);
8268
8269        typed_test!(Date32Array, Date32, Date32Type);
8270        typed_test!(Date64Array, Date64, Date64Type);
8271    }
8272
8273    fn cast_from_null_to_other_base(data_type: &DataType, is_complex: bool) {
8274        // Cast from null to data_type
8275        let array = new_null_array(&DataType::Null, 4);
8276        assert_eq!(array.data_type(), &DataType::Null);
8277        let cast_array = cast(&array, data_type).expect("cast failed");
8278        assert_eq!(cast_array.data_type(), data_type);
8279        for i in 0..4 {
8280            if is_complex {
8281                assert!(cast_array.logical_nulls().unwrap().is_null(i));
8282            } else {
8283                assert!(cast_array.is_null(i));
8284            }
8285        }
8286    }
8287
8288    fn cast_from_null_to_other(data_type: &DataType) {
8289        cast_from_null_to_other_base(data_type, false);
8290    }
8291
8292    fn cast_from_null_to_other_complex(data_type: &DataType) {
8293        cast_from_null_to_other_base(data_type, true);
8294    }
8295
8296    #[test]
8297    fn test_cast_null_from_and_to_variable_sized() {
8298        cast_from_null_to_other(&DataType::Utf8);
8299        cast_from_null_to_other(&DataType::LargeUtf8);
8300        cast_from_null_to_other(&DataType::Binary);
8301        cast_from_null_to_other(&DataType::LargeBinary);
8302    }
8303
8304    #[test]
8305    fn test_cast_null_from_and_to_nested_type() {
8306        // Cast null from and to map
8307        let data_type = DataType::Map(
8308            Arc::new(Field::new_struct(
8309                "entry",
8310                vec![
8311                    Field::new("key", DataType::Utf8, false),
8312                    Field::new("value", DataType::Int32, true),
8313                ],
8314                false,
8315            )),
8316            false,
8317        );
8318        cast_from_null_to_other(&data_type);
8319
8320        // Cast null from and to list
8321        let data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
8322        cast_from_null_to_other(&data_type);
8323        let data_type = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8324        cast_from_null_to_other(&data_type);
8325        let data_type =
8326            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
8327        cast_from_null_to_other(&data_type);
8328
8329        // Cast null from and to dictionary
8330        let values = vec![None, None, None, None] as Vec<Option<&str>>;
8331        let array: DictionaryArray<Int8Type> = values.into_iter().collect();
8332        let array = Arc::new(array) as ArrayRef;
8333        let data_type = array.data_type().to_owned();
8334        cast_from_null_to_other(&data_type);
8335
8336        // Cast null from and to struct
8337        let data_type = DataType::Struct(vec![Field::new("data", DataType::Int64, false)].into());
8338        cast_from_null_to_other(&data_type);
8339
8340        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
8341        cast_from_null_to_other(&target_type);
8342
8343        let target_type =
8344            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
8345        cast_from_null_to_other(&target_type);
8346
8347        let fields = UnionFields::from_fields(vec![Field::new("a", DataType::Int64, false)]);
8348        let target_type = DataType::Union(fields, UnionMode::Sparse);
8349        cast_from_null_to_other_complex(&target_type);
8350
8351        let target_type = DataType::RunEndEncoded(
8352            Arc::new(Field::new("item", DataType::Int32, true)),
8353            Arc::new(Field::new("item", DataType::Int32, true)),
8354        );
8355        cast_from_null_to_other_complex(&target_type);
8356    }
8357
8358    /// Print the `DictionaryArray` `array` as a vector of strings
8359    fn array_to_strings(array: &ArrayRef) -> Vec<String> {
8360        let options = FormatOptions::new().with_null("null");
8361        let formatter = ArrayFormatter::try_new(array.as_ref(), &options).unwrap();
8362        (0..array.len())
8363            .map(|i| formatter.value(i).to_string())
8364            .collect()
8365    }
8366
8367    #[test]
8368    fn test_cast_utf8_to_date32() {
8369        use chrono::NaiveDate;
8370        let from_ymd = chrono::NaiveDate::from_ymd_opt;
8371        let since = chrono::NaiveDate::signed_duration_since;
8372
8373        let a = StringArray::from(vec![
8374            "2000-01-01",          // valid date with leading 0s
8375            "2000-01-01T12:00:00", // valid datetime, will throw away the time part
8376            "2000-2-2",            // valid date without leading 0s
8377            "2000-00-00",          // invalid month and day
8378            "2000",                // just a year is invalid
8379        ]);
8380        let array = Arc::new(a) as ArrayRef;
8381        let b = cast(&array, &DataType::Date32).unwrap();
8382        let c = b.as_primitive::<Date32Type>();
8383
8384        // test valid inputs
8385        let date_value = since(
8386            NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
8387            from_ymd(1970, 1, 1).unwrap(),
8388        )
8389        .num_days() as i32;
8390        assert!(c.is_valid(0)); // "2000-01-01"
8391        assert_eq!(date_value, c.value(0));
8392
8393        assert!(c.is_valid(1)); // "2000-01-01T12:00:00"
8394        assert_eq!(date_value, c.value(1));
8395
8396        let date_value = since(
8397            NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
8398            from_ymd(1970, 1, 1).unwrap(),
8399        )
8400        .num_days() as i32;
8401        assert!(c.is_valid(2)); // "2000-2-2"
8402        assert_eq!(date_value, c.value(2));
8403
8404        // test invalid inputs
8405        assert!(!c.is_valid(3)); // "2000-00-00"
8406        assert!(!c.is_valid(4)); // "2000"
8407    }
8408
8409    #[test]
8410    fn test_cast_utf8_to_date64() {
8411        let a = StringArray::from(vec![
8412            "2000-01-01T12:00:00", // date + time valid
8413            "2020-12-15T12:34:56", // date + time valid
8414            "2020-2-2T12:34:56",   // valid date time without leading 0s
8415            "2000-00-00T12:00:00", // invalid month and day
8416            "2000-01-01 12:00:00", // missing the 'T'
8417            "2000-01-01",          // just a date is invalid
8418        ]);
8419        let array = Arc::new(a) as ArrayRef;
8420        let b = cast(&array, &DataType::Date64).unwrap();
8421        let c = b.as_primitive::<Date64Type>();
8422
8423        // test valid inputs
8424        assert!(c.is_valid(0)); // "2000-01-01T12:00:00"
8425        assert_eq!(946728000000, c.value(0));
8426        assert!(c.is_valid(1)); // "2020-12-15T12:34:56"
8427        assert_eq!(1608035696000, c.value(1));
8428        assert!(!c.is_valid(2)); // "2020-2-2T12:34:56"
8429
8430        assert!(!c.is_valid(3)); // "2000-00-00T12:00:00"
8431        assert!(c.is_valid(4)); // "2000-01-01 12:00:00"
8432        assert_eq!(946728000000, c.value(4));
8433        assert!(c.is_valid(5)); // "2000-01-01"
8434        assert_eq!(946684800000, c.value(5));
8435    }
8436
8437    #[test]
8438    fn test_can_cast_fsl_to_fsl() {
8439        let from_array = Arc::new(
8440            FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
8441                [Some([Some(1.0), Some(2.0)]), None],
8442                2,
8443            ),
8444        ) as ArrayRef;
8445        let to_array = Arc::new(
8446            FixedSizeListArray::from_iter_primitive::<Float16Type, _, _>(
8447                [
8448                    Some([Some(f16::from_f32(1.0)), Some(f16::from_f32(2.0))]),
8449                    None,
8450                ],
8451                2,
8452            ),
8453        ) as ArrayRef;
8454
8455        assert!(can_cast_types(from_array.data_type(), to_array.data_type()));
8456        let actual = cast(&from_array, to_array.data_type()).unwrap();
8457        assert_eq!(actual.data_type(), to_array.data_type());
8458
8459        let invalid_target =
8460            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Binary, true)), 2);
8461        assert!(!can_cast_types(from_array.data_type(), &invalid_target));
8462
8463        let invalid_size =
8464            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Float16, true)), 5);
8465        assert!(!can_cast_types(from_array.data_type(), &invalid_size));
8466    }
8467
8468    #[test]
8469    fn test_can_cast_types_fixed_size_list_to_list() {
8470        // DataType::List
8471        let array1 = Arc::new(make_fixed_size_list_array()) as ArrayRef;
8472        assert!(can_cast_types(
8473            array1.data_type(),
8474            &DataType::List(Arc::new(Field::new("", DataType::Int32, false)))
8475        ));
8476
8477        // DataType::LargeList
8478        let array2 = Arc::new(make_fixed_size_list_array_for_large_list()) as ArrayRef;
8479        assert!(can_cast_types(
8480            array2.data_type(),
8481            &DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false)))
8482        ));
8483    }
8484
8485    #[test]
8486    fn test_cast_fixed_size_list_to_list() {
8487        // Important cases:
8488        // 1. With/without nulls
8489        // 2. LargeList and List
8490        // 3. With and without inner casts
8491
8492        let cases = [
8493            // fixed_size_list<i32, 2> => list<i32>
8494            (
8495                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8496                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8497                    2,
8498                )) as ArrayRef,
8499                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
8500                    Some([Some(1), Some(1)]),
8501                    Some([Some(2), Some(2)]),
8502                ])) as ArrayRef,
8503            ),
8504            // fixed_size_list<i32, 2> => list<i32> (nullable)
8505            (
8506                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8507                    [None, Some([Some(2), Some(2)])],
8508                    2,
8509                )) as ArrayRef,
8510                Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>([
8511                    None,
8512                    Some([Some(2), Some(2)]),
8513                ])) as ArrayRef,
8514            ),
8515            // fixed_size_list<i32, 2> => large_list<i64>
8516            (
8517                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8518                    [[1, 1].map(Some), [2, 2].map(Some)].map(Some),
8519                    2,
8520                )) as ArrayRef,
8521                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
8522                    Some([Some(1), Some(1)]),
8523                    Some([Some(2), Some(2)]),
8524                ])) as ArrayRef,
8525            ),
8526            // fixed_size_list<i32, 2> => large_list<i64> (nullable)
8527            (
8528                Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8529                    [None, Some([Some(2), Some(2)])],
8530                    2,
8531                )) as ArrayRef,
8532                Arc::new(LargeListArray::from_iter_primitive::<Int64Type, _, _>([
8533                    None,
8534                    Some([Some(2), Some(2)]),
8535                ])) as ArrayRef,
8536            ),
8537        ];
8538
8539        for (array, expected) in cases {
8540            let array = Arc::new(array) as ArrayRef;
8541
8542            assert!(
8543                can_cast_types(array.data_type(), expected.data_type()),
8544                "can_cast_types claims we cannot cast {:?} to {:?}",
8545                array.data_type(),
8546                expected.data_type()
8547            );
8548
8549            let list_array = cast(&array, expected.data_type())
8550                .unwrap_or_else(|_| panic!("Failed to cast {array:?} to {expected:?}"));
8551            assert_eq!(
8552                list_array.as_ref(),
8553                &expected,
8554                "Incorrect result from casting {array:?} to {expected:?}",
8555            );
8556        }
8557    }
8558
8559    #[test]
8560    fn test_cast_utf8_to_list() {
8561        // DataType::List
8562        let array = Arc::new(StringArray::from(vec!["5"])) as ArrayRef;
8563        let field = Arc::new(Field::new("", DataType::Int32, false));
8564        let list_array = cast(&array, &DataType::List(field.clone())).unwrap();
8565        let actual = list_array.as_list_opt::<i32>().unwrap();
8566        let expect = ListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
8567        assert_eq!(&expect.value(0), &actual.value(0));
8568
8569        // DataType::LargeList
8570        let list_array = cast(&array, &DataType::LargeList(field.clone())).unwrap();
8571        let actual = list_array.as_list_opt::<i64>().unwrap();
8572        let expect = LargeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
8573        assert_eq!(&expect.value(0), &actual.value(0));
8574
8575        // DataType::FixedSizeList
8576        let list_array = cast(&array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
8577        let actual = list_array.as_fixed_size_list_opt().unwrap();
8578        let expect =
8579            FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])], 1);
8580        assert_eq!(&expect.value(0), &actual.value(0));
8581    }
8582
8583    #[test]
8584    fn test_cast_single_element_fixed_size_list() {
8585        // FixedSizeList<T>[1] => T
8586        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
8587            [(Some([Some(5)]))],
8588            1,
8589        )) as ArrayRef;
8590        let casted_array = cast(&from_array, &DataType::Int32).unwrap();
8591        let actual: &Int32Array = casted_array.as_primitive();
8592        let expected = Int32Array::from(vec![Some(5)]);
8593        assert_eq!(&expected, actual);
8594
8595        // FixedSizeList<T>[1] => FixedSizeList<U>[1]
8596        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
8597            [(Some([Some(5)]))],
8598            1,
8599        )) as ArrayRef;
8600        let to_field = Arc::new(Field::new("dummy", DataType::Float32, false));
8601        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
8602        let expected = Arc::new(FixedSizeListArray::new(
8603            to_field.clone(),
8604            1,
8605            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
8606            None,
8607        )) as ArrayRef;
8608        assert_eq!(*expected, *actual);
8609
8610        // FixedSizeList<T>[1] => FixedSizeList<FixdSizedList<U>[1]>[1]
8611        let from_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int16Type, _, _>(
8612            [(Some([Some(5)]))],
8613            1,
8614        )) as ArrayRef;
8615        let to_field_inner = Arc::new(Field::new_list_field(DataType::Float32, false));
8616        let to_field = Arc::new(Field::new(
8617            "dummy",
8618            DataType::FixedSizeList(to_field_inner.clone(), 1),
8619            false,
8620        ));
8621        let actual = cast(&from_array, &DataType::FixedSizeList(to_field.clone(), 1)).unwrap();
8622        let expected = Arc::new(FixedSizeListArray::new(
8623            to_field.clone(),
8624            1,
8625            Arc::new(FixedSizeListArray::new(
8626                to_field_inner.clone(),
8627                1,
8628                Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
8629                None,
8630            )) as ArrayRef,
8631            None,
8632        )) as ArrayRef;
8633        assert_eq!(*expected, *actual);
8634
8635        // T => FixedSizeList<T>[1] (non-nullable)
8636        let field = Arc::new(Field::new("dummy", DataType::Float32, false));
8637        let from_array = Arc::new(Int8Array::from(vec![Some(5)])) as ArrayRef;
8638        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
8639        let actual = casted_array.as_fixed_size_list();
8640        let expected = Arc::new(FixedSizeListArray::new(
8641            field.clone(),
8642            1,
8643            Arc::new(Float32Array::from(vec![Some(5.0)])) as ArrayRef,
8644            None,
8645        )) as ArrayRef;
8646        assert_eq!(expected.as_ref(), actual);
8647
8648        // T => FixedSizeList<T>[1] (nullable)
8649        let field = Arc::new(Field::new("nullable", DataType::Float32, true));
8650        let from_array = Arc::new(Int8Array::from(vec![None])) as ArrayRef;
8651        let casted_array = cast(&from_array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
8652        let actual = casted_array.as_fixed_size_list();
8653        let expected = Arc::new(FixedSizeListArray::new(
8654            field.clone(),
8655            1,
8656            Arc::new(Float32Array::from(vec![None])) as ArrayRef,
8657            None,
8658        )) as ArrayRef;
8659        assert_eq!(expected.as_ref(), actual);
8660    }
8661
8662    #[test]
8663    fn test_cast_list_containers() {
8664        // large-list to list
8665        let array = Arc::new(make_large_list_array()) as ArrayRef;
8666        let list_array = cast(
8667            &array,
8668            &DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
8669        )
8670        .unwrap();
8671        let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
8672        let expected = array.as_any().downcast_ref::<LargeListArray>().unwrap();
8673
8674        assert_eq!(&expected.value(0), &actual.value(0));
8675        assert_eq!(&expected.value(1), &actual.value(1));
8676        assert_eq!(&expected.value(2), &actual.value(2));
8677
8678        // list to large-list
8679        let array = Arc::new(make_list_array()) as ArrayRef;
8680        let large_list_array = cast(
8681            &array,
8682            &DataType::LargeList(Arc::new(Field::new("", DataType::Int32, false))),
8683        )
8684        .unwrap();
8685        let actual = large_list_array
8686            .as_any()
8687            .downcast_ref::<LargeListArray>()
8688            .unwrap();
8689        let expected = array.as_any().downcast_ref::<ListArray>().unwrap();
8690
8691        assert_eq!(&expected.value(0), &actual.value(0));
8692        assert_eq!(&expected.value(1), &actual.value(1));
8693        assert_eq!(&expected.value(2), &actual.value(2));
8694    }
8695
8696    #[test]
8697    fn test_cast_list_to_fsl() {
8698        // There four noteworthy cases we should handle:
8699        // 1. No nulls
8700        // 2. Nulls that are always empty
8701        // 3. Nulls that have varying lengths
8702        // 4. Nulls that are correctly sized (same as target list size)
8703
8704        // Non-null case
8705        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
8706        let values = vec![
8707            Some(vec![Some(1), Some(2), Some(3)]),
8708            Some(vec![Some(4), Some(5), Some(6)]),
8709        ];
8710        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
8711            values.clone(),
8712        )) as ArrayRef;
8713        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8714            values, 3,
8715        )) as ArrayRef;
8716        let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
8717        assert_eq!(expected.as_ref(), actual.as_ref());
8718
8719        // Null cases
8720        // Array is [[1, 2, 3], null, [4, 5, 6], null]
8721        let cases = [
8722            (
8723                // Zero-length nulls
8724                vec![1, 2, 3, 4, 5, 6],
8725                vec![3, 0, 3, 0],
8726            ),
8727            (
8728                // Varying-length nulls
8729                vec![1, 2, 3, 0, 0, 4, 5, 6, 0],
8730                vec![3, 2, 3, 1],
8731            ),
8732            (
8733                // Correctly-sized nulls
8734                vec![1, 2, 3, 0, 0, 0, 4, 5, 6, 0, 0, 0],
8735                vec![3, 3, 3, 3],
8736            ),
8737            (
8738                // Mixed nulls
8739                vec![1, 2, 3, 4, 5, 6, 0, 0, 0],
8740                vec![3, 0, 3, 3],
8741            ),
8742        ];
8743        let null_buffer = NullBuffer::from(vec![true, false, true, false]);
8744
8745        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8746            vec![
8747                Some(vec![Some(1), Some(2), Some(3)]),
8748                None,
8749                Some(vec![Some(4), Some(5), Some(6)]),
8750                None,
8751            ],
8752            3,
8753        )) as ArrayRef;
8754
8755        for (values, lengths) in cases.iter() {
8756            let array = Arc::new(ListArray::new(
8757                field.clone(),
8758                OffsetBuffer::from_lengths(lengths.clone()),
8759                Arc::new(Int32Array::from(values.clone())),
8760                Some(null_buffer.clone()),
8761            )) as ArrayRef;
8762            let actual = cast(array.as_ref(), &DataType::FixedSizeList(field.clone(), 3)).unwrap();
8763            assert_eq!(expected.as_ref(), actual.as_ref());
8764        }
8765    }
8766
8767    #[test]
8768    fn test_cast_list_to_fsl_safety() {
8769        let values = vec![
8770            Some(vec![Some(1), Some(2), Some(3)]),
8771            Some(vec![Some(4), Some(5)]),
8772            Some(vec![Some(6), Some(7), Some(8), Some(9)]),
8773            Some(vec![Some(3), Some(4), Some(5)]),
8774        ];
8775        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(
8776            values.clone(),
8777        )) as ArrayRef;
8778
8779        let res = cast_with_options(
8780            array.as_ref(),
8781            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
8782            &CastOptions {
8783                safe: false,
8784                ..Default::default()
8785            },
8786        );
8787        assert!(res.is_err());
8788        assert!(
8789            format!("{res:?}")
8790                .contains("Cannot cast to FixedSizeList(3): value at index 1 has length 2")
8791        );
8792
8793        // When safe=true (default), the cast will fill nulls for lists that are
8794        // too short and truncate lists that are too long.
8795        let res = cast(
8796            array.as_ref(),
8797            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
8798        )
8799        .unwrap();
8800        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8801            vec![
8802                Some(vec![Some(1), Some(2), Some(3)]),
8803                None, // Too short -> replaced with null
8804                None, // Too long -> replaced with null
8805                Some(vec![Some(3), Some(4), Some(5)]),
8806            ],
8807            3,
8808        )) as ArrayRef;
8809        assert_eq!(expected.as_ref(), res.as_ref());
8810
8811        // The safe option is false and the source array contains a null list.
8812        // issue: https://github.com/apache/arrow-rs/issues/5642
8813        let array = Arc::new(ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
8814            Some(vec![Some(1), Some(2), Some(3)]),
8815            None,
8816        ])) as ArrayRef;
8817        let res = cast_with_options(
8818            array.as_ref(),
8819            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 3),
8820            &CastOptions {
8821                safe: false,
8822                ..Default::default()
8823            },
8824        )
8825        .unwrap();
8826        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8827            vec![Some(vec![Some(1), Some(2), Some(3)]), None],
8828            3,
8829        )) as ArrayRef;
8830        assert_eq!(expected.as_ref(), res.as_ref());
8831    }
8832
8833    #[test]
8834    fn test_cast_large_list_to_fsl() {
8835        let values = vec![Some(vec![Some(1), Some(2)]), Some(vec![Some(3), Some(4)])];
8836        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
8837            values.clone(),
8838        )) as ArrayRef;
8839        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(
8840            values, 2,
8841        )) as ArrayRef;
8842        let actual = cast(
8843            array.as_ref(),
8844            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 2),
8845        )
8846        .unwrap();
8847        assert_eq!(expected.as_ref(), actual.as_ref());
8848    }
8849
8850    #[test]
8851    fn test_cast_list_to_fsl_subcast() {
8852        let array = Arc::new(LargeListArray::from_iter_primitive::<Int32Type, _, _>(
8853            vec![
8854                Some(vec![Some(1), Some(2)]),
8855                Some(vec![Some(3), Some(i32::MAX)]),
8856            ],
8857        )) as ArrayRef;
8858        let expected = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
8859            vec![
8860                Some(vec![Some(1), Some(2)]),
8861                Some(vec![Some(3), Some(i32::MAX as i64)]),
8862            ],
8863            2,
8864        )) as ArrayRef;
8865        let actual = cast(
8866            array.as_ref(),
8867            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 2),
8868        )
8869        .unwrap();
8870        assert_eq!(expected.as_ref(), actual.as_ref());
8871
8872        let res = cast_with_options(
8873            array.as_ref(),
8874            &DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int16, true)), 2),
8875            &CastOptions {
8876                safe: false,
8877                ..Default::default()
8878            },
8879        );
8880        assert!(res.is_err());
8881        assert!(format!("{res:?}").contains("Can't cast value 2147483647 to type Int16"));
8882    }
8883
8884    #[test]
8885    fn test_cast_list_to_fsl_empty() {
8886        let field = Arc::new(Field::new_list_field(DataType::Int32, true));
8887        let array = new_empty_array(&DataType::List(field.clone()));
8888
8889        let target_type = DataType::FixedSizeList(field.clone(), 3);
8890        let expected = new_empty_array(&target_type);
8891
8892        let actual = cast(array.as_ref(), &target_type).unwrap();
8893        assert_eq!(expected.as_ref(), actual.as_ref());
8894    }
8895
8896    fn make_list_array() -> ListArray {
8897        // Construct a value array
8898        let value_data = ArrayData::builder(DataType::Int32)
8899            .len(8)
8900            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8901            .build()
8902            .unwrap();
8903
8904        // Construct a buffer for value offsets, for the nested array:
8905        //  [[0, 1, 2], [3, 4, 5], [6, 7]]
8906        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
8907
8908        // Construct a list array from the above two
8909        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
8910        let list_data = ArrayData::builder(list_data_type)
8911            .len(3)
8912            .add_buffer(value_offsets)
8913            .add_child_data(value_data)
8914            .build()
8915            .unwrap();
8916        ListArray::from(list_data)
8917    }
8918
8919    fn make_large_list_array() -> LargeListArray {
8920        // Construct a value array
8921        let value_data = ArrayData::builder(DataType::Int32)
8922            .len(8)
8923            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8924            .build()
8925            .unwrap();
8926
8927        // Construct a buffer for value offsets, for the nested array:
8928        //  [[0, 1, 2], [3, 4, 5], [6, 7]]
8929        let value_offsets = Buffer::from_slice_ref([0i64, 3, 6, 8]);
8930
8931        // Construct a list array from the above two
8932        let list_data_type =
8933            DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
8934        let list_data = ArrayData::builder(list_data_type)
8935            .len(3)
8936            .add_buffer(value_offsets)
8937            .add_child_data(value_data)
8938            .build()
8939            .unwrap();
8940        LargeListArray::from(list_data)
8941    }
8942
8943    fn make_fixed_size_list_array() -> FixedSizeListArray {
8944        // Construct a value array
8945        let value_data = ArrayData::builder(DataType::Int32)
8946            .len(8)
8947            .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
8948            .build()
8949            .unwrap();
8950
8951        let list_data_type =
8952            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int32, true)), 4);
8953        let list_data = ArrayData::builder(list_data_type)
8954            .len(2)
8955            .add_child_data(value_data)
8956            .build()
8957            .unwrap();
8958        FixedSizeListArray::from(list_data)
8959    }
8960
8961    fn make_fixed_size_list_array_for_large_list() -> FixedSizeListArray {
8962        // Construct a value array
8963        let value_data = ArrayData::builder(DataType::Int64)
8964            .len(8)
8965            .add_buffer(Buffer::from_slice_ref([0i64, 1, 2, 3, 4, 5, 6, 7]))
8966            .build()
8967            .unwrap();
8968
8969        let list_data_type =
8970            DataType::FixedSizeList(Arc::new(Field::new_list_field(DataType::Int64, true)), 4);
8971        let list_data = ArrayData::builder(list_data_type)
8972            .len(2)
8973            .add_child_data(value_data)
8974            .build()
8975            .unwrap();
8976        FixedSizeListArray::from(list_data)
8977    }
8978
8979    #[test]
8980    fn test_cast_map_dont_allow_change_of_order() {
8981        let string_builder = StringBuilder::new();
8982        let value_builder = StringBuilder::new();
8983        let mut builder = MapBuilder::new(
8984            Some(MapFieldNames {
8985                entry: "entries".to_string(),
8986                key: "key".to_string(),
8987                value: "value".to_string(),
8988            }),
8989            string_builder,
8990            value_builder,
8991        );
8992
8993        builder.keys().append_value("0");
8994        builder.values().append_value("test_val_1");
8995        builder.append(true).unwrap();
8996        builder.keys().append_value("1");
8997        builder.values().append_value("test_val_2");
8998        builder.append(true).unwrap();
8999
9000        // map builder returns unsorted map by default
9001        let array = builder.finish();
9002
9003        let new_ordered = true;
9004        let new_type = DataType::Map(
9005            Arc::new(Field::new(
9006                "entries",
9007                DataType::Struct(
9008                    vec![
9009                        Field::new("key", DataType::Utf8, false),
9010                        Field::new("value", DataType::Utf8, false),
9011                    ]
9012                    .into(),
9013                ),
9014                false,
9015            )),
9016            new_ordered,
9017        );
9018
9019        let new_array_result = cast(&array, &new_type.clone());
9020        assert!(!can_cast_types(array.data_type(), &new_type));
9021        let Err(ArrowError::CastError(t)) = new_array_result else {
9022            panic!();
9023        };
9024        assert_eq!(
9025            t,
9026            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"#
9027        );
9028    }
9029
9030    #[test]
9031    fn test_cast_map_dont_allow_when_container_cant_cast() {
9032        let string_builder = StringBuilder::new();
9033        let value_builder = IntervalDayTimeArray::builder(2);
9034        let mut builder = MapBuilder::new(
9035            Some(MapFieldNames {
9036                entry: "entries".to_string(),
9037                key: "key".to_string(),
9038                value: "value".to_string(),
9039            }),
9040            string_builder,
9041            value_builder,
9042        );
9043
9044        builder.keys().append_value("0");
9045        builder.values().append_value(IntervalDayTime::new(1, 1));
9046        builder.append(true).unwrap();
9047        builder.keys().append_value("1");
9048        builder.values().append_value(IntervalDayTime::new(2, 2));
9049        builder.append(true).unwrap();
9050
9051        // map builder returns unsorted map by default
9052        let array = builder.finish();
9053
9054        let new_ordered = true;
9055        let new_type = DataType::Map(
9056            Arc::new(Field::new(
9057                "entries",
9058                DataType::Struct(
9059                    vec![
9060                        Field::new("key", DataType::Utf8, false),
9061                        Field::new("value", DataType::Duration(TimeUnit::Second), false),
9062                    ]
9063                    .into(),
9064                ),
9065                false,
9066            )),
9067            new_ordered,
9068        );
9069
9070        let new_array_result = cast(&array, &new_type.clone());
9071        assert!(!can_cast_types(array.data_type(), &new_type));
9072        let Err(ArrowError::CastError(t)) = new_array_result else {
9073            panic!();
9074        };
9075        assert_eq!(
9076            t,
9077            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"#
9078        );
9079    }
9080
9081    #[test]
9082    fn test_cast_map_field_names() {
9083        let string_builder = StringBuilder::new();
9084        let value_builder = StringBuilder::new();
9085        let mut builder = MapBuilder::new(
9086            Some(MapFieldNames {
9087                entry: "entries".to_string(),
9088                key: "key".to_string(),
9089                value: "value".to_string(),
9090            }),
9091            string_builder,
9092            value_builder,
9093        );
9094
9095        builder.keys().append_value("0");
9096        builder.values().append_value("test_val_1");
9097        builder.append(true).unwrap();
9098        builder.keys().append_value("1");
9099        builder.values().append_value("test_val_2");
9100        builder.append(true).unwrap();
9101        builder.append(false).unwrap();
9102
9103        let array = builder.finish();
9104
9105        let new_type = DataType::Map(
9106            Arc::new(Field::new(
9107                "entries_new",
9108                DataType::Struct(
9109                    vec![
9110                        Field::new("key_new", DataType::Utf8, false),
9111                        Field::new("value_values", DataType::Utf8, false),
9112                    ]
9113                    .into(),
9114                ),
9115                false,
9116            )),
9117            false,
9118        );
9119
9120        assert_ne!(new_type, array.data_type().clone());
9121
9122        let new_array = cast(&array, &new_type.clone()).unwrap();
9123        assert_eq!(new_type, new_array.data_type().clone());
9124        let map_array = new_array.as_map();
9125
9126        assert_ne!(new_type, array.data_type().clone());
9127        assert_eq!(new_type, map_array.data_type().clone());
9128
9129        let key_string = map_array
9130            .keys()
9131            .as_any()
9132            .downcast_ref::<StringArray>()
9133            .unwrap()
9134            .into_iter()
9135            .flatten()
9136            .collect::<Vec<_>>();
9137        assert_eq!(&key_string, &vec!["0", "1"]);
9138
9139        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
9140        let values_string = values_string_array
9141            .as_any()
9142            .downcast_ref::<StringArray>()
9143            .unwrap()
9144            .into_iter()
9145            .flatten()
9146            .collect::<Vec<_>>();
9147        assert_eq!(&values_string, &vec!["test_val_1", "test_val_2"]);
9148
9149        assert_eq!(
9150            map_array.nulls(),
9151            Some(&NullBuffer::from(vec![true, true, false]))
9152        );
9153    }
9154
9155    #[test]
9156    fn test_cast_map_contained_values() {
9157        let string_builder = StringBuilder::new();
9158        let value_builder = Int8Builder::new();
9159        let mut builder = MapBuilder::new(
9160            Some(MapFieldNames {
9161                entry: "entries".to_string(),
9162                key: "key".to_string(),
9163                value: "value".to_string(),
9164            }),
9165            string_builder,
9166            value_builder,
9167        );
9168
9169        builder.keys().append_value("0");
9170        builder.values().append_value(44);
9171        builder.append(true).unwrap();
9172        builder.keys().append_value("1");
9173        builder.values().append_value(22);
9174        builder.append(true).unwrap();
9175
9176        let array = builder.finish();
9177
9178        let new_type = DataType::Map(
9179            Arc::new(Field::new(
9180                "entries",
9181                DataType::Struct(
9182                    vec![
9183                        Field::new("key", DataType::Utf8, false),
9184                        Field::new("value", DataType::Utf8, false),
9185                    ]
9186                    .into(),
9187                ),
9188                false,
9189            )),
9190            false,
9191        );
9192
9193        let new_array = cast(&array, &new_type.clone()).unwrap();
9194        assert_eq!(new_type, new_array.data_type().clone());
9195        let map_array = new_array.as_map();
9196
9197        assert_ne!(new_type, array.data_type().clone());
9198        assert_eq!(new_type, map_array.data_type().clone());
9199
9200        let key_string = map_array
9201            .keys()
9202            .as_any()
9203            .downcast_ref::<StringArray>()
9204            .unwrap()
9205            .into_iter()
9206            .flatten()
9207            .collect::<Vec<_>>();
9208        assert_eq!(&key_string, &vec!["0", "1"]);
9209
9210        let values_string_array = cast(map_array.values(), &DataType::Utf8).unwrap();
9211        let values_string = values_string_array
9212            .as_any()
9213            .downcast_ref::<StringArray>()
9214            .unwrap()
9215            .into_iter()
9216            .flatten()
9217            .collect::<Vec<_>>();
9218        assert_eq!(&values_string, &vec!["44", "22"]);
9219    }
9220
9221    #[test]
9222    fn test_utf8_cast_offsets() {
9223        // test if offset of the array is taken into account during cast
9224        let str_array = StringArray::from(vec!["a", "b", "c"]);
9225        let str_array = str_array.slice(1, 2);
9226
9227        let out = cast(&str_array, &DataType::LargeUtf8).unwrap();
9228
9229        let large_str_array = out.as_any().downcast_ref::<LargeStringArray>().unwrap();
9230        let strs = large_str_array.into_iter().flatten().collect::<Vec<_>>();
9231        assert_eq!(strs, &["b", "c"])
9232    }
9233
9234    #[test]
9235    fn test_list_cast_offsets() {
9236        // test if offset of the array is taken into account during cast
9237        let array1 = make_list_array().slice(1, 2);
9238        let array2 = Arc::new(make_list_array()) as ArrayRef;
9239
9240        let dt = DataType::LargeList(Arc::new(Field::new_list_field(DataType::Int32, true)));
9241        let out1 = cast(&array1, &dt).unwrap();
9242        let out2 = cast(&array2, &dt).unwrap();
9243
9244        assert_eq!(&out1, &out2.slice(1, 2))
9245    }
9246
9247    #[test]
9248    fn test_list_to_string() {
9249        let str_array = StringArray::from(vec!["a", "b", "c", "d", "e", "f", "g", "h"]);
9250        let value_offsets = Buffer::from_slice_ref([0, 3, 6, 8]);
9251        let value_data = str_array.into_data();
9252
9253        let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Utf8, true)));
9254        let list_data = ArrayData::builder(list_data_type)
9255            .len(3)
9256            .add_buffer(value_offsets)
9257            .add_child_data(value_data)
9258            .build()
9259            .unwrap();
9260        let array = Arc::new(ListArray::from(list_data)) as ArrayRef;
9261
9262        let out = cast(&array, &DataType::Utf8).unwrap();
9263        let out = out
9264            .as_any()
9265            .downcast_ref::<StringArray>()
9266            .unwrap()
9267            .into_iter()
9268            .flatten()
9269            .collect::<Vec<_>>();
9270        assert_eq!(&out, &vec!["[a, b, c]", "[d, e, f]", "[g, h]"]);
9271
9272        let out = cast(&array, &DataType::LargeUtf8).unwrap();
9273        let out = out
9274            .as_any()
9275            .downcast_ref::<LargeStringArray>()
9276            .unwrap()
9277            .into_iter()
9278            .flatten()
9279            .collect::<Vec<_>>();
9280        assert_eq!(&out, &vec!["[a, b, c]", "[d, e, f]", "[g, h]"]);
9281
9282        let array = Arc::new(make_list_array()) as ArrayRef;
9283        let out = cast(&array, &DataType::Utf8).unwrap();
9284        let out = out
9285            .as_any()
9286            .downcast_ref::<StringArray>()
9287            .unwrap()
9288            .into_iter()
9289            .flatten()
9290            .collect::<Vec<_>>();
9291        assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
9292
9293        let array = Arc::new(make_large_list_array()) as ArrayRef;
9294        let out = cast(&array, &DataType::LargeUtf8).unwrap();
9295        let out = out
9296            .as_any()
9297            .downcast_ref::<LargeStringArray>()
9298            .unwrap()
9299            .into_iter()
9300            .flatten()
9301            .collect::<Vec<_>>();
9302        assert_eq!(&out, &vec!["[0, 1, 2]", "[3, 4, 5]", "[6, 7]"]);
9303    }
9304
9305    #[test]
9306    fn test_cast_f64_to_decimal128() {
9307        // to reproduce https://github.com/apache/arrow-rs/issues/2997
9308
9309        let decimal_type = DataType::Decimal128(18, 2);
9310        let array = Float64Array::from(vec![
9311            Some(0.0699999999),
9312            Some(0.0659999999),
9313            Some(0.0650000000),
9314            Some(0.0649999999),
9315        ]);
9316        let array = Arc::new(array) as ArrayRef;
9317        generate_cast_test_case!(
9318            &array,
9319            Decimal128Array,
9320            &decimal_type,
9321            vec![
9322                Some(7_i128), // round up
9323                Some(7_i128), // round up
9324                Some(7_i128), // round up
9325                Some(6_i128), // round down
9326            ]
9327        );
9328
9329        let decimal_type = DataType::Decimal128(18, 3);
9330        let array = Float64Array::from(vec![
9331            Some(0.0699999999),
9332            Some(0.0659999999),
9333            Some(0.0650000000),
9334            Some(0.0649999999),
9335        ]);
9336        let array = Arc::new(array) as ArrayRef;
9337        generate_cast_test_case!(
9338            &array,
9339            Decimal128Array,
9340            &decimal_type,
9341            vec![
9342                Some(70_i128), // round up
9343                Some(66_i128), // round up
9344                Some(65_i128), // round down
9345                Some(65_i128), // round up
9346            ]
9347        );
9348    }
9349
9350    #[test]
9351    fn test_cast_numeric_to_decimal128_overflow() {
9352        let array = Int64Array::from(vec![i64::MAX]);
9353        let array = Arc::new(array) as ArrayRef;
9354        let casted_array = cast_with_options(
9355            &array,
9356            &DataType::Decimal128(38, 30),
9357            &CastOptions {
9358                safe: true,
9359                format_options: FormatOptions::default(),
9360            },
9361        );
9362        assert!(casted_array.is_ok());
9363        assert!(casted_array.unwrap().is_null(0));
9364
9365        let casted_array = cast_with_options(
9366            &array,
9367            &DataType::Decimal128(38, 30),
9368            &CastOptions {
9369                safe: false,
9370                format_options: FormatOptions::default(),
9371            },
9372        );
9373        assert!(casted_array.is_err());
9374    }
9375
9376    #[test]
9377    fn test_cast_numeric_to_decimal256_overflow() {
9378        let array = Int64Array::from(vec![i64::MAX]);
9379        let array = Arc::new(array) as ArrayRef;
9380        let casted_array = cast_with_options(
9381            &array,
9382            &DataType::Decimal256(76, 76),
9383            &CastOptions {
9384                safe: true,
9385                format_options: FormatOptions::default(),
9386            },
9387        );
9388        assert!(casted_array.is_ok());
9389        assert!(casted_array.unwrap().is_null(0));
9390
9391        let casted_array = cast_with_options(
9392            &array,
9393            &DataType::Decimal256(76, 76),
9394            &CastOptions {
9395                safe: false,
9396                format_options: FormatOptions::default(),
9397            },
9398        );
9399        assert!(casted_array.is_err());
9400    }
9401
9402    #[test]
9403    fn test_cast_floating_point_to_decimal128_precision_overflow() {
9404        let array = Float64Array::from(vec![1.1]);
9405        let array = Arc::new(array) as ArrayRef;
9406        let casted_array = cast_with_options(
9407            &array,
9408            &DataType::Decimal128(2, 2),
9409            &CastOptions {
9410                safe: true,
9411                format_options: FormatOptions::default(),
9412            },
9413        );
9414        assert!(casted_array.is_ok());
9415        assert!(casted_array.unwrap().is_null(0));
9416
9417        let casted_array = cast_with_options(
9418            &array,
9419            &DataType::Decimal128(2, 2),
9420            &CastOptions {
9421                safe: false,
9422                format_options: FormatOptions::default(),
9423            },
9424        );
9425        let err = casted_array.unwrap_err().to_string();
9426        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal128 of precision 2. Max is 0.99";
9427        assert!(
9428            err.contains(expected_error),
9429            "did not find expected error '{expected_error}' in actual error '{err}'"
9430        );
9431    }
9432
9433    #[test]
9434    fn test_cast_floating_point_to_decimal256_precision_overflow() {
9435        let array = Float64Array::from(vec![1.1]);
9436        let array = Arc::new(array) as ArrayRef;
9437        let casted_array = cast_with_options(
9438            &array,
9439            &DataType::Decimal256(2, 2),
9440            &CastOptions {
9441                safe: true,
9442                format_options: FormatOptions::default(),
9443            },
9444        );
9445        assert!(casted_array.is_ok());
9446        assert!(casted_array.unwrap().is_null(0));
9447
9448        let casted_array = cast_with_options(
9449            &array,
9450            &DataType::Decimal256(2, 2),
9451            &CastOptions {
9452                safe: false,
9453                format_options: FormatOptions::default(),
9454            },
9455        );
9456        let err = casted_array.unwrap_err().to_string();
9457        let expected_error = "Invalid argument error: 1.10 is too large to store in a Decimal256 of precision 2. Max is 0.99";
9458        assert_eq!(err, expected_error);
9459    }
9460
9461    #[test]
9462    fn test_cast_floating_point_to_decimal128_overflow() {
9463        let array = Float64Array::from(vec![f64::MAX]);
9464        let array = Arc::new(array) as ArrayRef;
9465        let casted_array = cast_with_options(
9466            &array,
9467            &DataType::Decimal128(38, 30),
9468            &CastOptions {
9469                safe: true,
9470                format_options: FormatOptions::default(),
9471            },
9472        );
9473        assert!(casted_array.is_ok());
9474        assert!(casted_array.unwrap().is_null(0));
9475
9476        let casted_array = cast_with_options(
9477            &array,
9478            &DataType::Decimal128(38, 30),
9479            &CastOptions {
9480                safe: false,
9481                format_options: FormatOptions::default(),
9482            },
9483        );
9484        let err = casted_array.unwrap_err().to_string();
9485        let expected_error = "Cast error: Cannot cast to Decimal128(38, 30)";
9486        assert!(
9487            err.contains(expected_error),
9488            "did not find expected error '{expected_error}' in actual error '{err}'"
9489        );
9490    }
9491
9492    #[test]
9493    fn test_cast_floating_point_to_decimal256_overflow() {
9494        let array = Float64Array::from(vec![f64::MAX]);
9495        let array = Arc::new(array) as ArrayRef;
9496        let casted_array = cast_with_options(
9497            &array,
9498            &DataType::Decimal256(76, 50),
9499            &CastOptions {
9500                safe: true,
9501                format_options: FormatOptions::default(),
9502            },
9503        );
9504        assert!(casted_array.is_ok());
9505        assert!(casted_array.unwrap().is_null(0));
9506
9507        let casted_array = cast_with_options(
9508            &array,
9509            &DataType::Decimal256(76, 50),
9510            &CastOptions {
9511                safe: false,
9512                format_options: FormatOptions::default(),
9513            },
9514        );
9515        let err = casted_array.unwrap_err().to_string();
9516        let expected_error = "Cast error: Cannot cast to Decimal256(76, 50)";
9517        assert!(
9518            err.contains(expected_error),
9519            "did not find expected error '{expected_error}' in actual error '{err}'"
9520        );
9521    }
9522    #[test]
9523    fn test_cast_decimal256_to_f64_no_overflow() {
9524        // Test casting i256::MAX: should produce a large finite positive value
9525        let array = vec![Some(i256::MAX)];
9526        let array = create_decimal256_array(array, 76, 2).unwrap();
9527        let array = Arc::new(array) as ArrayRef;
9528
9529        let result = cast(&array, &DataType::Float64).unwrap();
9530        let result = result.as_primitive::<Float64Type>();
9531        assert!(result.value(0).is_finite());
9532        assert!(result.value(0) > 0.0); // Positive result
9533
9534        // Test casting i256::MIN: should produce a large finite negative value
9535        let array = vec![Some(i256::MIN)];
9536        let array = create_decimal256_array(array, 76, 2).unwrap();
9537        let array = Arc::new(array) as ArrayRef;
9538
9539        let result = cast(&array, &DataType::Float64).unwrap();
9540        let result = result.as_primitive::<Float64Type>();
9541        assert!(result.value(0).is_finite());
9542        assert!(result.value(0) < 0.0); // Negative result
9543    }
9544
9545    #[test]
9546    fn test_cast_decimal128_to_decimal128_negative_scale() {
9547        let input_type = DataType::Decimal128(20, 0);
9548        let output_type = DataType::Decimal128(20, -1);
9549        assert!(can_cast_types(&input_type, &output_type));
9550        let array = vec![Some(1123450), Some(2123455), Some(3123456), None];
9551        let input_decimal_array = create_decimal128_array(array, 20, 0).unwrap();
9552        let array = Arc::new(input_decimal_array) as ArrayRef;
9553        generate_cast_test_case!(
9554            &array,
9555            Decimal128Array,
9556            &output_type,
9557            vec![
9558                Some(112345_i128),
9559                Some(212346_i128),
9560                Some(312346_i128),
9561                None
9562            ]
9563        );
9564
9565        let casted_array = cast(&array, &output_type).unwrap();
9566        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9567
9568        assert_eq!("1123450", decimal_arr.value_as_string(0));
9569        assert_eq!("2123460", decimal_arr.value_as_string(1));
9570        assert_eq!("3123460", decimal_arr.value_as_string(2));
9571    }
9572
9573    #[test]
9574    fn decimal128_min_max_to_f64() {
9575        // Ensure Decimal128 i128::MIN/MAX round-trip cast
9576        let min128 = i128::MIN;
9577        let max128 = i128::MAX;
9578        assert_eq!(min128 as f64, min128 as f64);
9579        assert_eq!(max128 as f64, max128 as f64);
9580    }
9581
9582    #[test]
9583    fn test_cast_numeric_to_decimal128_negative() {
9584        let decimal_type = DataType::Decimal128(38, -1);
9585        let array = Arc::new(Int32Array::from(vec![
9586            Some(1123456),
9587            Some(2123456),
9588            Some(3123456),
9589        ])) as ArrayRef;
9590
9591        let casted_array = cast(&array, &decimal_type).unwrap();
9592        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9593
9594        assert_eq!("1123450", decimal_arr.value_as_string(0));
9595        assert_eq!("2123450", decimal_arr.value_as_string(1));
9596        assert_eq!("3123450", decimal_arr.value_as_string(2));
9597
9598        let array = Arc::new(Float32Array::from(vec![
9599            Some(1123.456),
9600            Some(2123.456),
9601            Some(3123.456),
9602        ])) as ArrayRef;
9603
9604        let casted_array = cast(&array, &decimal_type).unwrap();
9605        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9606
9607        assert_eq!("1120", decimal_arr.value_as_string(0));
9608        assert_eq!("2120", decimal_arr.value_as_string(1));
9609        assert_eq!("3120", decimal_arr.value_as_string(2));
9610    }
9611
9612    #[test]
9613    fn test_cast_decimal128_to_decimal128_negative() {
9614        let input_type = DataType::Decimal128(10, -1);
9615        let output_type = DataType::Decimal128(10, -2);
9616        assert!(can_cast_types(&input_type, &output_type));
9617        let array = vec![Some(123)];
9618        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
9619        let array = Arc::new(input_decimal_array) as ArrayRef;
9620        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(12_i128),]);
9621
9622        let casted_array = cast(&array, &output_type).unwrap();
9623        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9624
9625        assert_eq!("1200", decimal_arr.value_as_string(0));
9626
9627        let array = vec![Some(125)];
9628        let input_decimal_array = create_decimal128_array(array, 10, -1).unwrap();
9629        let array = Arc::new(input_decimal_array) as ArrayRef;
9630        generate_cast_test_case!(&array, Decimal128Array, &output_type, vec![Some(13_i128),]);
9631
9632        let casted_array = cast(&array, &output_type).unwrap();
9633        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9634
9635        assert_eq!("1300", decimal_arr.value_as_string(0));
9636    }
9637
9638    #[test]
9639    fn test_cast_decimal128_to_decimal256_negative() {
9640        let input_type = DataType::Decimal128(10, 3);
9641        let output_type = DataType::Decimal256(10, 5);
9642        assert!(can_cast_types(&input_type, &output_type));
9643        let array = vec![Some(123456), Some(-123456)];
9644        let input_decimal_array = create_decimal128_array(array, 10, 3).unwrap();
9645        let array = Arc::new(input_decimal_array) as ArrayRef;
9646
9647        let hundred = i256::from_i128(100);
9648        generate_cast_test_case!(
9649            &array,
9650            Decimal256Array,
9651            &output_type,
9652            vec![
9653                Some(i256::from_i128(123456).mul_wrapping(hundred)),
9654                Some(i256::from_i128(-123456).mul_wrapping(hundred))
9655            ]
9656        );
9657    }
9658
9659    #[test]
9660    fn test_parse_string_to_decimal() {
9661        assert_eq!(
9662            Decimal128Type::format_decimal(
9663                parse_string_to_decimal_native::<Decimal128Type>("123.45", 2).unwrap(),
9664                38,
9665                2,
9666            ),
9667            "123.45"
9668        );
9669        assert_eq!(
9670            Decimal128Type::format_decimal(
9671                parse_string_to_decimal_native::<Decimal128Type>("12345", 2).unwrap(),
9672                38,
9673                2,
9674            ),
9675            "12345.00"
9676        );
9677        assert_eq!(
9678            Decimal128Type::format_decimal(
9679                parse_string_to_decimal_native::<Decimal128Type>("0.12345", 2).unwrap(),
9680                38,
9681                2,
9682            ),
9683            "0.12"
9684        );
9685        assert_eq!(
9686            Decimal128Type::format_decimal(
9687                parse_string_to_decimal_native::<Decimal128Type>(".12345", 2).unwrap(),
9688                38,
9689                2,
9690            ),
9691            "0.12"
9692        );
9693        assert_eq!(
9694            Decimal128Type::format_decimal(
9695                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
9696                38,
9697                2,
9698            ),
9699            "0.13"
9700        );
9701        assert_eq!(
9702            Decimal128Type::format_decimal(
9703                parse_string_to_decimal_native::<Decimal128Type>(".1265", 2).unwrap(),
9704                38,
9705                2,
9706            ),
9707            "0.13"
9708        );
9709
9710        assert_eq!(
9711            Decimal256Type::format_decimal(
9712                parse_string_to_decimal_native::<Decimal256Type>("123.45", 3).unwrap(),
9713                38,
9714                3,
9715            ),
9716            "123.450"
9717        );
9718        assert_eq!(
9719            Decimal256Type::format_decimal(
9720                parse_string_to_decimal_native::<Decimal256Type>("12345", 3).unwrap(),
9721                38,
9722                3,
9723            ),
9724            "12345.000"
9725        );
9726        assert_eq!(
9727            Decimal256Type::format_decimal(
9728                parse_string_to_decimal_native::<Decimal256Type>("0.12345", 3).unwrap(),
9729                38,
9730                3,
9731            ),
9732            "0.123"
9733        );
9734        assert_eq!(
9735            Decimal256Type::format_decimal(
9736                parse_string_to_decimal_native::<Decimal256Type>(".12345", 3).unwrap(),
9737                38,
9738                3,
9739            ),
9740            "0.123"
9741        );
9742        assert_eq!(
9743            Decimal256Type::format_decimal(
9744                parse_string_to_decimal_native::<Decimal256Type>(".1265", 3).unwrap(),
9745                38,
9746                3,
9747            ),
9748            "0.127"
9749        );
9750    }
9751
9752    fn test_cast_string_to_decimal(array: ArrayRef) {
9753        // Decimal128
9754        let output_type = DataType::Decimal128(38, 2);
9755        assert!(can_cast_types(array.data_type(), &output_type));
9756
9757        let casted_array = cast(&array, &output_type).unwrap();
9758        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9759
9760        assert_eq!("123.45", decimal_arr.value_as_string(0));
9761        assert_eq!("1.23", decimal_arr.value_as_string(1));
9762        assert_eq!("0.12", decimal_arr.value_as_string(2));
9763        assert_eq!("0.13", decimal_arr.value_as_string(3));
9764        assert_eq!("1.26", decimal_arr.value_as_string(4));
9765        assert_eq!("12345.00", decimal_arr.value_as_string(5));
9766        assert_eq!("12345.00", decimal_arr.value_as_string(6));
9767        assert_eq!("0.12", decimal_arr.value_as_string(7));
9768        assert_eq!("12.23", decimal_arr.value_as_string(8));
9769        assert!(decimal_arr.is_null(9));
9770        assert_eq!("0.00", decimal_arr.value_as_string(10));
9771        assert_eq!("0.00", decimal_arr.value_as_string(11));
9772        assert!(decimal_arr.is_null(12));
9773        assert_eq!("-1.23", decimal_arr.value_as_string(13));
9774        assert_eq!("-1.24", decimal_arr.value_as_string(14));
9775        assert_eq!("0.00", decimal_arr.value_as_string(15));
9776        assert_eq!("-123.00", decimal_arr.value_as_string(16));
9777        assert_eq!("-123.23", decimal_arr.value_as_string(17));
9778        assert_eq!("-0.12", decimal_arr.value_as_string(18));
9779        assert_eq!("1.23", decimal_arr.value_as_string(19));
9780        assert_eq!("1.24", decimal_arr.value_as_string(20));
9781        assert_eq!("0.00", decimal_arr.value_as_string(21));
9782        assert_eq!("123.00", decimal_arr.value_as_string(22));
9783        assert_eq!("123.23", decimal_arr.value_as_string(23));
9784        assert_eq!("0.12", decimal_arr.value_as_string(24));
9785        assert!(decimal_arr.is_null(25));
9786        assert!(decimal_arr.is_null(26));
9787        assert!(decimal_arr.is_null(27));
9788        assert_eq!("0.00", decimal_arr.value_as_string(28));
9789        assert_eq!("0.00", decimal_arr.value_as_string(29));
9790        assert_eq!("12345.00", decimal_arr.value_as_string(30));
9791        assert_eq!(decimal_arr.len(), 31);
9792
9793        // Decimal256
9794        let output_type = DataType::Decimal256(76, 3);
9795        assert!(can_cast_types(array.data_type(), &output_type));
9796
9797        let casted_array = cast(&array, &output_type).unwrap();
9798        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
9799
9800        assert_eq!("123.450", decimal_arr.value_as_string(0));
9801        assert_eq!("1.235", decimal_arr.value_as_string(1));
9802        assert_eq!("0.123", decimal_arr.value_as_string(2));
9803        assert_eq!("0.127", decimal_arr.value_as_string(3));
9804        assert_eq!("1.263", decimal_arr.value_as_string(4));
9805        assert_eq!("12345.000", decimal_arr.value_as_string(5));
9806        assert_eq!("12345.000", decimal_arr.value_as_string(6));
9807        assert_eq!("0.123", decimal_arr.value_as_string(7));
9808        assert_eq!("12.234", decimal_arr.value_as_string(8));
9809        assert!(decimal_arr.is_null(9));
9810        assert_eq!("0.000", decimal_arr.value_as_string(10));
9811        assert_eq!("0.000", decimal_arr.value_as_string(11));
9812        assert!(decimal_arr.is_null(12));
9813        assert_eq!("-1.235", decimal_arr.value_as_string(13));
9814        assert_eq!("-1.236", decimal_arr.value_as_string(14));
9815        assert_eq!("0.000", decimal_arr.value_as_string(15));
9816        assert_eq!("-123.000", decimal_arr.value_as_string(16));
9817        assert_eq!("-123.234", decimal_arr.value_as_string(17));
9818        assert_eq!("-0.123", decimal_arr.value_as_string(18));
9819        assert_eq!("1.235", decimal_arr.value_as_string(19));
9820        assert_eq!("1.236", decimal_arr.value_as_string(20));
9821        assert_eq!("0.000", decimal_arr.value_as_string(21));
9822        assert_eq!("123.000", decimal_arr.value_as_string(22));
9823        assert_eq!("123.234", decimal_arr.value_as_string(23));
9824        assert_eq!("0.123", decimal_arr.value_as_string(24));
9825        assert!(decimal_arr.is_null(25));
9826        assert!(decimal_arr.is_null(26));
9827        assert!(decimal_arr.is_null(27));
9828        assert_eq!("0.000", decimal_arr.value_as_string(28));
9829        assert_eq!("0.000", decimal_arr.value_as_string(29));
9830        assert_eq!("12345.000", decimal_arr.value_as_string(30));
9831        assert_eq!(decimal_arr.len(), 31);
9832    }
9833
9834    #[test]
9835    fn test_cast_utf8_to_decimal() {
9836        let str_array = StringArray::from(vec![
9837            Some("123.45"),
9838            Some("1.2345"),
9839            Some("0.12345"),
9840            Some("0.1267"),
9841            Some("1.263"),
9842            Some("12345.0"),
9843            Some("12345"),
9844            Some("000.123"),
9845            Some("12.234000"),
9846            None,
9847            Some(""),
9848            Some(" "),
9849            None,
9850            Some("-1.23499999"),
9851            Some("-1.23599999"),
9852            Some("-0.00001"),
9853            Some("-123"),
9854            Some("-123.234000"),
9855            Some("-000.123"),
9856            Some("+1.23499999"),
9857            Some("+1.23599999"),
9858            Some("+0.00001"),
9859            Some("+123"),
9860            Some("+123.234000"),
9861            Some("+000.123"),
9862            Some("1.-23499999"),
9863            Some("-1.-23499999"),
9864            Some("--1.23499999"),
9865            Some("0"),
9866            Some("000.000"),
9867            Some("0000000000000000012345.000"),
9868        ]);
9869        let array = Arc::new(str_array) as ArrayRef;
9870
9871        test_cast_string_to_decimal(array);
9872
9873        let test_cases = [
9874            (None, None),
9875            // (Some(""), None),
9876            // (Some("   "), None),
9877            (Some("0"), Some("0")),
9878            (Some("000.000"), Some("0")),
9879            (Some("12345"), Some("12345")),
9880            (Some("000000000000000000000000000012345"), Some("12345")),
9881            (Some("-123"), Some("-123")),
9882            (Some("+123"), Some("123")),
9883        ];
9884        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
9885        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
9886
9887        let array = Arc::new(StringArray::from(inputs)) as ArrayRef;
9888        test_cast_string_to_decimal_scale_zero(array, &expected);
9889    }
9890
9891    #[test]
9892    fn test_cast_large_utf8_to_decimal() {
9893        let str_array = LargeStringArray::from(vec![
9894            Some("123.45"),
9895            Some("1.2345"),
9896            Some("0.12345"),
9897            Some("0.1267"),
9898            Some("1.263"),
9899            Some("12345.0"),
9900            Some("12345"),
9901            Some("000.123"),
9902            Some("12.234000"),
9903            None,
9904            Some(""),
9905            Some(" "),
9906            None,
9907            Some("-1.23499999"),
9908            Some("-1.23599999"),
9909            Some("-0.00001"),
9910            Some("-123"),
9911            Some("-123.234000"),
9912            Some("-000.123"),
9913            Some("+1.23499999"),
9914            Some("+1.23599999"),
9915            Some("+0.00001"),
9916            Some("+123"),
9917            Some("+123.234000"),
9918            Some("+000.123"),
9919            Some("1.-23499999"),
9920            Some("-1.-23499999"),
9921            Some("--1.23499999"),
9922            Some("0"),
9923            Some("000.000"),
9924            Some("0000000000000000012345.000"),
9925        ]);
9926        let array = Arc::new(str_array) as ArrayRef;
9927
9928        test_cast_string_to_decimal(array);
9929
9930        let test_cases = [
9931            (None, None),
9932            (Some(""), None),
9933            (Some("   "), None),
9934            (Some("0"), Some("0")),
9935            (Some("000.000"), Some("0")),
9936            (Some("12345"), Some("12345")),
9937            (Some("000000000000000000000000000012345"), Some("12345")),
9938            (Some("-123"), Some("-123")),
9939            (Some("+123"), Some("123")),
9940        ];
9941        let inputs = test_cases.iter().map(|entry| entry.0).collect::<Vec<_>>();
9942        let expected = test_cases.iter().map(|entry| entry.1).collect::<Vec<_>>();
9943
9944        let array = Arc::new(LargeStringArray::from(inputs)) as ArrayRef;
9945        test_cast_string_to_decimal_scale_zero(array, &expected);
9946    }
9947
9948    fn test_cast_string_to_decimal_scale_zero(
9949        array: ArrayRef,
9950        expected_as_string: &[Option<&str>],
9951    ) {
9952        // Decimal128
9953        let output_type = DataType::Decimal128(38, 0);
9954        assert!(can_cast_types(array.data_type(), &output_type));
9955        let casted_array = cast(&array, &output_type).unwrap();
9956        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
9957        assert_decimal_array_contents(decimal_arr, expected_as_string);
9958
9959        // Decimal256
9960        let output_type = DataType::Decimal256(76, 0);
9961        assert!(can_cast_types(array.data_type(), &output_type));
9962        let casted_array = cast(&array, &output_type).unwrap();
9963        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
9964        assert_decimal_array_contents(decimal_arr, expected_as_string);
9965    }
9966
9967    fn assert_decimal_array_contents<T>(
9968        array: &PrimitiveArray<T>,
9969        expected_as_string: &[Option<&str>],
9970    ) where
9971        T: DecimalType + ArrowPrimitiveType,
9972    {
9973        assert_eq!(array.len(), expected_as_string.len());
9974        for (i, expected) in expected_as_string.iter().enumerate() {
9975            let actual = if array.is_null(i) {
9976                None
9977            } else {
9978                Some(array.value_as_string(i))
9979            };
9980            let actual = actual.as_ref().map(|s| s.as_ref());
9981            assert_eq!(*expected, actual, "Expected at position {i}");
9982        }
9983    }
9984
9985    #[test]
9986    fn test_cast_invalid_utf8_to_decimal() {
9987        let str_array = StringArray::from(vec!["4.4.5", ". 0.123"]);
9988        let array = Arc::new(str_array) as ArrayRef;
9989
9990        // Safe cast
9991        let output_type = DataType::Decimal128(38, 2);
9992        let casted_array = cast(&array, &output_type).unwrap();
9993        assert!(casted_array.is_null(0));
9994        assert!(casted_array.is_null(1));
9995
9996        let output_type = DataType::Decimal256(76, 2);
9997        let casted_array = cast(&array, &output_type).unwrap();
9998        assert!(casted_array.is_null(0));
9999        assert!(casted_array.is_null(1));
10000
10001        // Non-safe cast
10002        let output_type = DataType::Decimal128(38, 2);
10003        let str_array = StringArray::from(vec!["4.4.5"]);
10004        let array = Arc::new(str_array) as ArrayRef;
10005        let option = CastOptions {
10006            safe: false,
10007            format_options: FormatOptions::default(),
10008        };
10009        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
10010        assert!(
10011            casted_err
10012                .to_string()
10013                .contains("Cannot cast string '4.4.5' to value of Decimal128(38, 10) type")
10014        );
10015
10016        let str_array = StringArray::from(vec![". 0.123"]);
10017        let array = Arc::new(str_array) as ArrayRef;
10018        let casted_err = cast_with_options(&array, &output_type, &option).unwrap_err();
10019        assert!(
10020            casted_err
10021                .to_string()
10022                .contains("Cannot cast string '. 0.123' to value of Decimal128(38, 10) type")
10023        );
10024    }
10025
10026    fn test_cast_string_to_decimal128_overflow(overflow_array: ArrayRef) {
10027        let output_type = DataType::Decimal128(38, 2);
10028        let casted_array = cast(&overflow_array, &output_type).unwrap();
10029        let decimal_arr = casted_array.as_primitive::<Decimal128Type>();
10030
10031        assert!(decimal_arr.is_null(0));
10032        assert!(decimal_arr.is_null(1));
10033        assert!(decimal_arr.is_null(2));
10034        assert_eq!(
10035            "999999999999999999999999999999999999.99",
10036            decimal_arr.value_as_string(3)
10037        );
10038        assert_eq!(
10039            "100000000000000000000000000000000000.00",
10040            decimal_arr.value_as_string(4)
10041        );
10042    }
10043
10044    #[test]
10045    fn test_cast_string_to_decimal128_precision_overflow() {
10046        let array = StringArray::from(vec!["1000".to_string()]);
10047        let array = Arc::new(array) as ArrayRef;
10048        let casted_array = cast_with_options(
10049            &array,
10050            &DataType::Decimal128(10, 8),
10051            &CastOptions {
10052                safe: true,
10053                format_options: FormatOptions::default(),
10054            },
10055        );
10056        assert!(casted_array.is_ok());
10057        assert!(casted_array.unwrap().is_null(0));
10058
10059        let err = cast_with_options(
10060            &array,
10061            &DataType::Decimal128(10, 8),
10062            &CastOptions {
10063                safe: false,
10064                format_options: FormatOptions::default(),
10065            },
10066        );
10067        assert_eq!(
10068            "Invalid argument error: 1000.00000000 is too large to store in a Decimal128 of precision 10. Max is 99.99999999",
10069            err.unwrap_err().to_string()
10070        );
10071    }
10072
10073    #[test]
10074    fn test_cast_utf8_to_decimal128_overflow() {
10075        let overflow_str_array = StringArray::from(vec![
10076            i128::MAX.to_string(),
10077            i128::MIN.to_string(),
10078            "99999999999999999999999999999999999999".to_string(),
10079            "999999999999999999999999999999999999.99".to_string(),
10080            "99999999999999999999999999999999999.999".to_string(),
10081        ]);
10082        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10083
10084        test_cast_string_to_decimal128_overflow(overflow_array);
10085    }
10086
10087    #[test]
10088    fn test_cast_large_utf8_to_decimal128_overflow() {
10089        let overflow_str_array = LargeStringArray::from(vec![
10090            i128::MAX.to_string(),
10091            i128::MIN.to_string(),
10092            "99999999999999999999999999999999999999".to_string(),
10093            "999999999999999999999999999999999999.99".to_string(),
10094            "99999999999999999999999999999999999.999".to_string(),
10095        ]);
10096        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10097
10098        test_cast_string_to_decimal128_overflow(overflow_array);
10099    }
10100
10101    fn test_cast_string_to_decimal256_overflow(overflow_array: ArrayRef) {
10102        let output_type = DataType::Decimal256(76, 2);
10103        let casted_array = cast(&overflow_array, &output_type).unwrap();
10104        let decimal_arr = casted_array.as_primitive::<Decimal256Type>();
10105
10106        assert_eq!(
10107            "170141183460469231731687303715884105727.00",
10108            decimal_arr.value_as_string(0)
10109        );
10110        assert_eq!(
10111            "-170141183460469231731687303715884105728.00",
10112            decimal_arr.value_as_string(1)
10113        );
10114        assert_eq!(
10115            "99999999999999999999999999999999999999.00",
10116            decimal_arr.value_as_string(2)
10117        );
10118        assert_eq!(
10119            "999999999999999999999999999999999999.99",
10120            decimal_arr.value_as_string(3)
10121        );
10122        assert_eq!(
10123            "100000000000000000000000000000000000.00",
10124            decimal_arr.value_as_string(4)
10125        );
10126        assert!(decimal_arr.is_null(5));
10127        assert!(decimal_arr.is_null(6));
10128    }
10129
10130    #[test]
10131    fn test_cast_string_to_decimal256_precision_overflow() {
10132        let array = StringArray::from(vec!["1000".to_string()]);
10133        let array = Arc::new(array) as ArrayRef;
10134        let casted_array = cast_with_options(
10135            &array,
10136            &DataType::Decimal256(10, 8),
10137            &CastOptions {
10138                safe: true,
10139                format_options: FormatOptions::default(),
10140            },
10141        );
10142        assert!(casted_array.is_ok());
10143        assert!(casted_array.unwrap().is_null(0));
10144
10145        let err = cast_with_options(
10146            &array,
10147            &DataType::Decimal256(10, 8),
10148            &CastOptions {
10149                safe: false,
10150                format_options: FormatOptions::default(),
10151            },
10152        );
10153        assert_eq!(
10154            "Invalid argument error: 1000.00000000 is too large to store in a Decimal256 of precision 10. Max is 99.99999999",
10155            err.unwrap_err().to_string()
10156        );
10157    }
10158
10159    #[test]
10160    fn test_cast_utf8_to_decimal256_overflow() {
10161        let overflow_str_array = StringArray::from(vec![
10162            i128::MAX.to_string(),
10163            i128::MIN.to_string(),
10164            "99999999999999999999999999999999999999".to_string(),
10165            "999999999999999999999999999999999999.99".to_string(),
10166            "99999999999999999999999999999999999.999".to_string(),
10167            i256::MAX.to_string(),
10168            i256::MIN.to_string(),
10169        ]);
10170        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10171
10172        test_cast_string_to_decimal256_overflow(overflow_array);
10173    }
10174
10175    #[test]
10176    fn test_cast_large_utf8_to_decimal256_overflow() {
10177        let overflow_str_array = LargeStringArray::from(vec![
10178            i128::MAX.to_string(),
10179            i128::MIN.to_string(),
10180            "99999999999999999999999999999999999999".to_string(),
10181            "999999999999999999999999999999999999.99".to_string(),
10182            "99999999999999999999999999999999999.999".to_string(),
10183            i256::MAX.to_string(),
10184            i256::MIN.to_string(),
10185        ]);
10186        let overflow_array = Arc::new(overflow_str_array) as ArrayRef;
10187
10188        test_cast_string_to_decimal256_overflow(overflow_array);
10189    }
10190
10191    #[test]
10192    fn test_cast_outside_supported_range_for_nanoseconds() {
10193        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";
10194
10195        let array = StringArray::from(vec![Some("1650-01-01 01:01:01.000001")]);
10196
10197        let cast_options = CastOptions {
10198            safe: false,
10199            format_options: FormatOptions::default(),
10200        };
10201
10202        let result = cast_string_to_timestamp::<i32, TimestampNanosecondType>(
10203            &array,
10204            &None::<Arc<str>>,
10205            &cast_options,
10206        );
10207
10208        let err = result.unwrap_err();
10209        assert_eq!(
10210            err.to_string(),
10211            format!(
10212                "Cast error: Overflow converting {} to Nanosecond. {}",
10213                array.value(0),
10214                EXPECTED_ERROR_MESSAGE
10215            )
10216        );
10217    }
10218
10219    #[test]
10220    fn test_cast_date32_to_timestamp() {
10221        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10222        let array = Arc::new(a) as ArrayRef;
10223        let b = cast(&array, &DataType::Timestamp(TimeUnit::Second, None)).unwrap();
10224        let c = b.as_primitive::<TimestampSecondType>();
10225        assert_eq!(1609459200, c.value(0));
10226        assert_eq!(1640995200, c.value(1));
10227        assert!(c.is_null(2));
10228    }
10229
10230    #[test]
10231    fn test_cast_date32_to_timestamp_ms() {
10232        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10233        let array = Arc::new(a) as ArrayRef;
10234        let b = cast(&array, &DataType::Timestamp(TimeUnit::Millisecond, None)).unwrap();
10235        let c = b
10236            .as_any()
10237            .downcast_ref::<TimestampMillisecondArray>()
10238            .unwrap();
10239        assert_eq!(1609459200000, c.value(0));
10240        assert_eq!(1640995200000, c.value(1));
10241        assert!(c.is_null(2));
10242    }
10243
10244    #[test]
10245    fn test_cast_date32_to_timestamp_us() {
10246        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10247        let array = Arc::new(a) as ArrayRef;
10248        let b = cast(&array, &DataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
10249        let c = b
10250            .as_any()
10251            .downcast_ref::<TimestampMicrosecondArray>()
10252            .unwrap();
10253        assert_eq!(1609459200000000, c.value(0));
10254        assert_eq!(1640995200000000, c.value(1));
10255        assert!(c.is_null(2));
10256    }
10257
10258    #[test]
10259    fn test_cast_date32_to_timestamp_ns() {
10260        let a = Date32Array::from(vec![Some(18628), Some(18993), None]); // 2021-1-1, 2022-1-1
10261        let array = Arc::new(a) as ArrayRef;
10262        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
10263        let c = b
10264            .as_any()
10265            .downcast_ref::<TimestampNanosecondArray>()
10266            .unwrap();
10267        assert_eq!(1609459200000000000, c.value(0));
10268        assert_eq!(1640995200000000000, c.value(1));
10269        assert!(c.is_null(2));
10270    }
10271
10272    #[test]
10273    fn test_timezone_cast() {
10274        let a = StringArray::from(vec![
10275            "2000-01-01T12:00:00", // date + time valid
10276            "2020-12-15T12:34:56", // date + time valid
10277        ]);
10278        let array = Arc::new(a) as ArrayRef;
10279        let b = cast(&array, &DataType::Timestamp(TimeUnit::Nanosecond, None)).unwrap();
10280        let v = b.as_primitive::<TimestampNanosecondType>();
10281
10282        assert_eq!(v.value(0), 946728000000000000);
10283        assert_eq!(v.value(1), 1608035696000000000);
10284
10285        let b = cast(
10286            &b,
10287            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10288        )
10289        .unwrap();
10290        let v = b.as_primitive::<TimestampNanosecondType>();
10291
10292        assert_eq!(v.value(0), 946728000000000000);
10293        assert_eq!(v.value(1), 1608035696000000000);
10294
10295        let b = cast(
10296            &b,
10297            &DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
10298        )
10299        .unwrap();
10300        let v = b.as_primitive::<TimestampMillisecondType>();
10301
10302        assert_eq!(v.value(0), 946728000000);
10303        assert_eq!(v.value(1), 1608035696000);
10304    }
10305
10306    #[test]
10307    fn test_cast_utf8_to_timestamp() {
10308        fn test_tz(tz: Arc<str>) {
10309            let valid = StringArray::from(vec![
10310                "2023-01-01 04:05:06.789000-08:00",
10311                "2023-01-01 04:05:06.789000-07:00",
10312                "2023-01-01 04:05:06.789 -0800",
10313                "2023-01-01 04:05:06.789 -08:00",
10314                "2023-01-01 040506 +0730",
10315                "2023-01-01 040506 +07:30",
10316                "2023-01-01 04:05:06.789",
10317                "2023-01-01 04:05:06",
10318                "2023-01-01",
10319            ]);
10320
10321            let array = Arc::new(valid) as ArrayRef;
10322            let b = cast_with_options(
10323                &array,
10324                &DataType::Timestamp(TimeUnit::Nanosecond, Some(tz.clone())),
10325                &CastOptions {
10326                    safe: false,
10327                    format_options: FormatOptions::default(),
10328                },
10329            )
10330            .unwrap();
10331
10332            let tz = tz.as_ref().parse().unwrap();
10333
10334            let as_tz =
10335                |v: i64| as_datetime_with_timezone::<TimestampNanosecondType>(v, tz).unwrap();
10336
10337            let as_utc = |v: &i64| as_tz(*v).naive_utc().to_string();
10338            let as_local = |v: &i64| as_tz(*v).naive_local().to_string();
10339
10340            let values = b.as_primitive::<TimestampNanosecondType>().values();
10341            let utc_results: Vec<_> = values.iter().map(as_utc).collect();
10342            let local_results: Vec<_> = values.iter().map(as_local).collect();
10343
10344            // Absolute timestamps should be parsed preserving the same UTC instant
10345            assert_eq!(
10346                &utc_results[..6],
10347                &[
10348                    "2023-01-01 12:05:06.789".to_string(),
10349                    "2023-01-01 11:05:06.789".to_string(),
10350                    "2023-01-01 12:05:06.789".to_string(),
10351                    "2023-01-01 12:05:06.789".to_string(),
10352                    "2022-12-31 20:35:06".to_string(),
10353                    "2022-12-31 20:35:06".to_string(),
10354                ]
10355            );
10356            // Non-absolute timestamps should be parsed preserving the same local instant
10357            assert_eq!(
10358                &local_results[6..],
10359                &[
10360                    "2023-01-01 04:05:06.789".to_string(),
10361                    "2023-01-01 04:05:06".to_string(),
10362                    "2023-01-01 00:00:00".to_string()
10363                ]
10364            )
10365        }
10366
10367        test_tz("+00:00".into());
10368        test_tz("+02:00".into());
10369    }
10370
10371    #[test]
10372    fn test_cast_invalid_utf8() {
10373        let v1: &[u8] = b"\xFF invalid";
10374        let v2: &[u8] = b"\x00 Foo";
10375        let s = BinaryArray::from(vec![v1, v2]);
10376        let options = CastOptions {
10377            safe: true,
10378            format_options: FormatOptions::default(),
10379        };
10380        let array = cast_with_options(&s, &DataType::Utf8, &options).unwrap();
10381        let a = array.as_string::<i32>();
10382        a.to_data().validate_full().unwrap();
10383
10384        assert_eq!(a.null_count(), 1);
10385        assert_eq!(a.len(), 2);
10386        assert!(a.is_null(0));
10387        assert_eq!(a.value(0), "");
10388        assert_eq!(a.value(1), "\x00 Foo");
10389    }
10390
10391    #[test]
10392    fn test_cast_utf8_to_timestamptz() {
10393        let valid = StringArray::from(vec!["2023-01-01"]);
10394
10395        let array = Arc::new(valid) as ArrayRef;
10396        let b = cast(
10397            &array,
10398            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10399        )
10400        .unwrap();
10401
10402        let expect = DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into()));
10403
10404        assert_eq!(b.data_type(), &expect);
10405        let c = b
10406            .as_any()
10407            .downcast_ref::<TimestampNanosecondArray>()
10408            .unwrap();
10409        assert_eq!(1672531200000000000, c.value(0));
10410    }
10411
10412    #[test]
10413    fn test_cast_decimal_to_string() {
10414        assert!(can_cast_types(
10415            &DataType::Decimal32(9, 4),
10416            &DataType::Utf8View
10417        ));
10418        assert!(can_cast_types(
10419            &DataType::Decimal64(16, 4),
10420            &DataType::Utf8View
10421        ));
10422        assert!(can_cast_types(
10423            &DataType::Decimal128(10, 4),
10424            &DataType::Utf8View
10425        ));
10426        assert!(can_cast_types(
10427            &DataType::Decimal256(38, 10),
10428            &DataType::Utf8View
10429        ));
10430
10431        macro_rules! assert_decimal_values {
10432            ($array:expr) => {
10433                let c = $array;
10434                assert_eq!("1123.454", c.value(0));
10435                assert_eq!("2123.456", c.value(1));
10436                assert_eq!("-3123.453", c.value(2));
10437                assert_eq!("-3123.456", c.value(3));
10438                assert_eq!("0.000", c.value(4));
10439                assert_eq!("0.123", c.value(5));
10440                assert_eq!("1234.567", c.value(6));
10441                assert_eq!("-1234.567", c.value(7));
10442                assert!(c.is_null(8));
10443            };
10444        }
10445
10446        fn test_decimal_to_string<IN: ArrowPrimitiveType, OffsetSize: OffsetSizeTrait>(
10447            output_type: DataType,
10448            array: PrimitiveArray<IN>,
10449        ) {
10450            let b = cast(&array, &output_type).unwrap();
10451
10452            assert_eq!(b.data_type(), &output_type);
10453            match b.data_type() {
10454                DataType::Utf8View => {
10455                    let c = b.as_string_view();
10456                    assert_decimal_values!(c);
10457                }
10458                DataType::Utf8 | DataType::LargeUtf8 => {
10459                    let c = b.as_string::<OffsetSize>();
10460                    assert_decimal_values!(c);
10461                }
10462                _ => (),
10463            }
10464        }
10465
10466        let array32: Vec<Option<i32>> = vec![
10467            Some(1123454),
10468            Some(2123456),
10469            Some(-3123453),
10470            Some(-3123456),
10471            Some(0),
10472            Some(123),
10473            Some(123456789),
10474            Some(-123456789),
10475            None,
10476        ];
10477        let array64: Vec<Option<i64>> = array32.iter().map(|num| num.map(|x| x as i64)).collect();
10478        let array128: Vec<Option<i128>> =
10479            array64.iter().map(|num| num.map(|x| x as i128)).collect();
10480        let array256: Vec<Option<i256>> = array128
10481            .iter()
10482            .map(|num| num.map(i256::from_i128))
10483            .collect();
10484
10485        test_decimal_to_string::<Decimal32Type, i32>(
10486            DataType::Utf8View,
10487            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
10488        );
10489        test_decimal_to_string::<Decimal32Type, i32>(
10490            DataType::Utf8,
10491            create_decimal32_array(array32.clone(), 7, 3).unwrap(),
10492        );
10493        test_decimal_to_string::<Decimal32Type, i64>(
10494            DataType::LargeUtf8,
10495            create_decimal32_array(array32, 7, 3).unwrap(),
10496        );
10497
10498        test_decimal_to_string::<Decimal64Type, i32>(
10499            DataType::Utf8View,
10500            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
10501        );
10502        test_decimal_to_string::<Decimal64Type, i32>(
10503            DataType::Utf8,
10504            create_decimal64_array(array64.clone(), 7, 3).unwrap(),
10505        );
10506        test_decimal_to_string::<Decimal64Type, i64>(
10507            DataType::LargeUtf8,
10508            create_decimal64_array(array64, 7, 3).unwrap(),
10509        );
10510
10511        test_decimal_to_string::<Decimal128Type, i32>(
10512            DataType::Utf8View,
10513            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
10514        );
10515        test_decimal_to_string::<Decimal128Type, i32>(
10516            DataType::Utf8,
10517            create_decimal128_array(array128.clone(), 7, 3).unwrap(),
10518        );
10519        test_decimal_to_string::<Decimal128Type, i64>(
10520            DataType::LargeUtf8,
10521            create_decimal128_array(array128, 7, 3).unwrap(),
10522        );
10523
10524        test_decimal_to_string::<Decimal256Type, i32>(
10525            DataType::Utf8View,
10526            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
10527        );
10528        test_decimal_to_string::<Decimal256Type, i32>(
10529            DataType::Utf8,
10530            create_decimal256_array(array256.clone(), 7, 3).unwrap(),
10531        );
10532        test_decimal_to_string::<Decimal256Type, i64>(
10533            DataType::LargeUtf8,
10534            create_decimal256_array(array256, 7, 3).unwrap(),
10535        );
10536    }
10537
10538    #[test]
10539    fn test_cast_numeric_to_decimal128_precision_overflow() {
10540        let array = Int64Array::from(vec![1234567]);
10541        let array = Arc::new(array) as ArrayRef;
10542        let casted_array = cast_with_options(
10543            &array,
10544            &DataType::Decimal128(7, 3),
10545            &CastOptions {
10546                safe: true,
10547                format_options: FormatOptions::default(),
10548            },
10549        );
10550        assert!(casted_array.is_ok());
10551        assert!(casted_array.unwrap().is_null(0));
10552
10553        let err = cast_with_options(
10554            &array,
10555            &DataType::Decimal128(7, 3),
10556            &CastOptions {
10557                safe: false,
10558                format_options: FormatOptions::default(),
10559            },
10560        );
10561        assert_eq!(
10562            "Invalid argument error: 1234567.000 is too large to store in a Decimal128 of precision 7. Max is 9999.999",
10563            err.unwrap_err().to_string()
10564        );
10565    }
10566
10567    #[test]
10568    fn test_cast_numeric_to_decimal256_precision_overflow() {
10569        let array = Int64Array::from(vec![1234567]);
10570        let array = Arc::new(array) as ArrayRef;
10571        let casted_array = cast_with_options(
10572            &array,
10573            &DataType::Decimal256(7, 3),
10574            &CastOptions {
10575                safe: true,
10576                format_options: FormatOptions::default(),
10577            },
10578        );
10579        assert!(casted_array.is_ok());
10580        assert!(casted_array.unwrap().is_null(0));
10581
10582        let err = cast_with_options(
10583            &array,
10584            &DataType::Decimal256(7, 3),
10585            &CastOptions {
10586                safe: false,
10587                format_options: FormatOptions::default(),
10588            },
10589        );
10590        assert_eq!(
10591            "Invalid argument error: 1234567.000 is too large to store in a Decimal256 of precision 7. Max is 9999.999",
10592            err.unwrap_err().to_string()
10593        );
10594    }
10595
10596    /// helper function to test casting from duration to interval
10597    fn cast_from_duration_to_interval<T: ArrowTemporalType<Native = i64>>(
10598        array: Vec<i64>,
10599        cast_options: &CastOptions,
10600    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
10601        let array = PrimitiveArray::<T>::new(array.into(), None);
10602        let array = Arc::new(array) as ArrayRef;
10603        let interval = DataType::Interval(IntervalUnit::MonthDayNano);
10604        let out = cast_with_options(&array, &interval, cast_options)?;
10605        let out = out.as_primitive::<IntervalMonthDayNanoType>().clone();
10606        Ok(out)
10607    }
10608
10609    #[test]
10610    fn test_cast_from_duration_to_interval() {
10611        // from duration second to interval month day nano
10612        let array = vec![1234567];
10613        let casted_array =
10614            cast_from_duration_to_interval::<DurationSecondType>(array, &CastOptions::default())
10615                .unwrap();
10616        assert_eq!(
10617            casted_array.data_type(),
10618            &DataType::Interval(IntervalUnit::MonthDayNano)
10619        );
10620        assert_eq!(
10621            casted_array.value(0),
10622            IntervalMonthDayNano::new(0, 0, 1234567000000000)
10623        );
10624
10625        let array = vec![i64::MAX];
10626        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
10627            array.clone(),
10628            &CastOptions::default(),
10629        )
10630        .unwrap();
10631        assert!(!casted_array.is_valid(0));
10632
10633        let casted_array = cast_from_duration_to_interval::<DurationSecondType>(
10634            array,
10635            &CastOptions {
10636                safe: false,
10637                format_options: FormatOptions::default(),
10638            },
10639        );
10640        assert!(casted_array.is_err());
10641
10642        // from duration millisecond to interval month day nano
10643        let array = vec![1234567];
10644        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
10645            array,
10646            &CastOptions::default(),
10647        )
10648        .unwrap();
10649        assert_eq!(
10650            casted_array.data_type(),
10651            &DataType::Interval(IntervalUnit::MonthDayNano)
10652        );
10653        assert_eq!(
10654            casted_array.value(0),
10655            IntervalMonthDayNano::new(0, 0, 1234567000000)
10656        );
10657
10658        let array = vec![i64::MAX];
10659        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
10660            array.clone(),
10661            &CastOptions::default(),
10662        )
10663        .unwrap();
10664        assert!(!casted_array.is_valid(0));
10665
10666        let casted_array = cast_from_duration_to_interval::<DurationMillisecondType>(
10667            array,
10668            &CastOptions {
10669                safe: false,
10670                format_options: FormatOptions::default(),
10671            },
10672        );
10673        assert!(casted_array.is_err());
10674
10675        // from duration microsecond to interval month day nano
10676        let array = vec![1234567];
10677        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
10678            array,
10679            &CastOptions::default(),
10680        )
10681        .unwrap();
10682        assert_eq!(
10683            casted_array.data_type(),
10684            &DataType::Interval(IntervalUnit::MonthDayNano)
10685        );
10686        assert_eq!(
10687            casted_array.value(0),
10688            IntervalMonthDayNano::new(0, 0, 1234567000)
10689        );
10690
10691        let array = vec![i64::MAX];
10692        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
10693            array.clone(),
10694            &CastOptions::default(),
10695        )
10696        .unwrap();
10697        assert!(!casted_array.is_valid(0));
10698
10699        let casted_array = cast_from_duration_to_interval::<DurationMicrosecondType>(
10700            array,
10701            &CastOptions {
10702                safe: false,
10703                format_options: FormatOptions::default(),
10704            },
10705        );
10706        assert!(casted_array.is_err());
10707
10708        // from duration nanosecond to interval month day nano
10709        let array = vec![1234567];
10710        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
10711            array,
10712            &CastOptions::default(),
10713        )
10714        .unwrap();
10715        assert_eq!(
10716            casted_array.data_type(),
10717            &DataType::Interval(IntervalUnit::MonthDayNano)
10718        );
10719        assert_eq!(
10720            casted_array.value(0),
10721            IntervalMonthDayNano::new(0, 0, 1234567)
10722        );
10723
10724        let array = vec![i64::MAX];
10725        let casted_array = cast_from_duration_to_interval::<DurationNanosecondType>(
10726            array,
10727            &CastOptions {
10728                safe: false,
10729                format_options: FormatOptions::default(),
10730            },
10731        )
10732        .unwrap();
10733        assert_eq!(
10734            casted_array.value(0),
10735            IntervalMonthDayNano::new(0, 0, i64::MAX)
10736        );
10737    }
10738
10739    /// helper function to test casting from interval to duration
10740    fn cast_from_interval_to_duration<T: ArrowTemporalType>(
10741        array: &IntervalMonthDayNanoArray,
10742        cast_options: &CastOptions,
10743    ) -> Result<PrimitiveArray<T>, ArrowError> {
10744        let casted_array = cast_with_options(&array, &T::DATA_TYPE, cast_options)?;
10745        casted_array
10746            .as_any()
10747            .downcast_ref::<PrimitiveArray<T>>()
10748            .ok_or_else(|| {
10749                ArrowError::ComputeError(format!("Failed to downcast to {}", T::DATA_TYPE))
10750            })
10751            .cloned()
10752    }
10753
10754    #[test]
10755    fn test_cast_from_interval_to_duration() {
10756        let nullable = CastOptions::default();
10757        let fallible = CastOptions {
10758            safe: false,
10759            format_options: FormatOptions::default(),
10760        };
10761        let v = IntervalMonthDayNano::new(0, 0, 1234567);
10762
10763        // from interval month day nano to duration second
10764        let array = vec![v].into();
10765        let casted_array: DurationSecondArray =
10766            cast_from_interval_to_duration(&array, &nullable).unwrap();
10767        assert_eq!(casted_array.value(0), 0);
10768
10769        let array = vec![IntervalMonthDayNano::MAX].into();
10770        let casted_array: DurationSecondArray =
10771            cast_from_interval_to_duration(&array, &nullable).unwrap();
10772        assert!(!casted_array.is_valid(0));
10773
10774        let res = cast_from_interval_to_duration::<DurationSecondType>(&array, &fallible);
10775        assert!(res.is_err());
10776
10777        // from interval month day nano to duration millisecond
10778        let array = vec![v].into();
10779        let casted_array: DurationMillisecondArray =
10780            cast_from_interval_to_duration(&array, &nullable).unwrap();
10781        assert_eq!(casted_array.value(0), 1);
10782
10783        let array = vec![IntervalMonthDayNano::MAX].into();
10784        let casted_array: DurationMillisecondArray =
10785            cast_from_interval_to_duration(&array, &nullable).unwrap();
10786        assert!(!casted_array.is_valid(0));
10787
10788        let res = cast_from_interval_to_duration::<DurationMillisecondType>(&array, &fallible);
10789        assert!(res.is_err());
10790
10791        // from interval month day nano to duration microsecond
10792        let array = vec![v].into();
10793        let casted_array: DurationMicrosecondArray =
10794            cast_from_interval_to_duration(&array, &nullable).unwrap();
10795        assert_eq!(casted_array.value(0), 1234);
10796
10797        let array = vec![IntervalMonthDayNano::MAX].into();
10798        let casted_array =
10799            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &nullable).unwrap();
10800        assert!(!casted_array.is_valid(0));
10801
10802        let casted_array =
10803            cast_from_interval_to_duration::<DurationMicrosecondType>(&array, &fallible);
10804        assert!(casted_array.is_err());
10805
10806        // from interval month day nano to duration nanosecond
10807        let array = vec![v].into();
10808        let casted_array: DurationNanosecondArray =
10809            cast_from_interval_to_duration(&array, &nullable).unwrap();
10810        assert_eq!(casted_array.value(0), 1234567);
10811
10812        let array = vec![IntervalMonthDayNano::MAX].into();
10813        let casted_array: DurationNanosecondArray =
10814            cast_from_interval_to_duration(&array, &nullable).unwrap();
10815        assert!(!casted_array.is_valid(0));
10816
10817        let casted_array =
10818            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &fallible);
10819        assert!(casted_array.is_err());
10820
10821        let array = vec![
10822            IntervalMonthDayNanoType::make_value(0, 1, 0),
10823            IntervalMonthDayNanoType::make_value(-1, 0, 0),
10824            IntervalMonthDayNanoType::make_value(1, 1, 0),
10825            IntervalMonthDayNanoType::make_value(1, 0, 1),
10826            IntervalMonthDayNanoType::make_value(0, 0, -1),
10827        ]
10828        .into();
10829        let casted_array =
10830            cast_from_interval_to_duration::<DurationNanosecondType>(&array, &nullable).unwrap();
10831        assert!(!casted_array.is_valid(0));
10832        assert!(!casted_array.is_valid(1));
10833        assert!(!casted_array.is_valid(2));
10834        assert!(!casted_array.is_valid(3));
10835        assert!(casted_array.is_valid(4));
10836        assert_eq!(casted_array.value(4), -1);
10837    }
10838
10839    /// helper function to test casting from interval year month to interval month day nano
10840    fn cast_from_interval_year_month_to_interval_month_day_nano(
10841        array: Vec<i32>,
10842        cast_options: &CastOptions,
10843    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
10844        let array = PrimitiveArray::<IntervalYearMonthType>::from(array);
10845        let array = Arc::new(array) as ArrayRef;
10846        let casted_array = cast_with_options(
10847            &array,
10848            &DataType::Interval(IntervalUnit::MonthDayNano),
10849            cast_options,
10850        )?;
10851        casted_array
10852            .as_any()
10853            .downcast_ref::<IntervalMonthDayNanoArray>()
10854            .ok_or_else(|| {
10855                ArrowError::ComputeError(
10856                    "Failed to downcast to IntervalMonthDayNanoArray".to_string(),
10857                )
10858            })
10859            .cloned()
10860    }
10861
10862    #[test]
10863    fn test_cast_from_interval_year_month_to_interval_month_day_nano() {
10864        // from interval year month to interval month day nano
10865        let array = vec![1234567];
10866        let casted_array = cast_from_interval_year_month_to_interval_month_day_nano(
10867            array,
10868            &CastOptions::default(),
10869        )
10870        .unwrap();
10871        assert_eq!(
10872            casted_array.data_type(),
10873            &DataType::Interval(IntervalUnit::MonthDayNano)
10874        );
10875        assert_eq!(
10876            casted_array.value(0),
10877            IntervalMonthDayNano::new(1234567, 0, 0)
10878        );
10879    }
10880
10881    /// helper function to test casting from interval day time to interval month day nano
10882    fn cast_from_interval_day_time_to_interval_month_day_nano(
10883        array: Vec<IntervalDayTime>,
10884        cast_options: &CastOptions,
10885    ) -> Result<PrimitiveArray<IntervalMonthDayNanoType>, ArrowError> {
10886        let array = PrimitiveArray::<IntervalDayTimeType>::from(array);
10887        let array = Arc::new(array) as ArrayRef;
10888        let casted_array = cast_with_options(
10889            &array,
10890            &DataType::Interval(IntervalUnit::MonthDayNano),
10891            cast_options,
10892        )?;
10893        Ok(casted_array
10894            .as_primitive::<IntervalMonthDayNanoType>()
10895            .clone())
10896    }
10897
10898    #[test]
10899    fn test_cast_from_interval_day_time_to_interval_month_day_nano() {
10900        // from interval day time to interval month day nano
10901        let array = vec![IntervalDayTime::new(123, 0)];
10902        let casted_array =
10903            cast_from_interval_day_time_to_interval_month_day_nano(array, &CastOptions::default())
10904                .unwrap();
10905        assert_eq!(
10906            casted_array.data_type(),
10907            &DataType::Interval(IntervalUnit::MonthDayNano)
10908        );
10909        assert_eq!(casted_array.value(0), IntervalMonthDayNano::new(0, 123, 0));
10910    }
10911
10912    #[test]
10913    fn test_cast_below_unixtimestamp() {
10914        let valid = StringArray::from(vec![
10915            "1900-01-03 23:59:59",
10916            "1969-12-31 00:00:01",
10917            "1989-12-31 00:00:01",
10918        ]);
10919
10920        let array = Arc::new(valid) as ArrayRef;
10921        let casted_array = cast_with_options(
10922            &array,
10923            &DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
10924            &CastOptions {
10925                safe: false,
10926                format_options: FormatOptions::default(),
10927            },
10928        )
10929        .unwrap();
10930
10931        let ts_array = casted_array
10932            .as_primitive::<TimestampNanosecondType>()
10933            .values()
10934            .iter()
10935            .map(|ts| ts / 1_000_000)
10936            .collect::<Vec<_>>();
10937
10938        let array = TimestampMillisecondArray::from(ts_array).with_timezone("+00:00".to_string());
10939        let casted_array = cast(&array, &DataType::Date32).unwrap();
10940        let date_array = casted_array.as_primitive::<Date32Type>();
10941        let casted_array = cast(&date_array, &DataType::Utf8).unwrap();
10942        let string_array = casted_array.as_string::<i32>();
10943        assert_eq!("1900-01-03", string_array.value(0));
10944        assert_eq!("1969-12-31", string_array.value(1));
10945        assert_eq!("1989-12-31", string_array.value(2));
10946    }
10947
10948    #[test]
10949    fn test_nested_list() {
10950        let mut list = ListBuilder::new(Int32Builder::new());
10951        list.append_value([Some(1), Some(2), Some(3)]);
10952        list.append_value([Some(4), None, Some(6)]);
10953        let list = list.finish();
10954
10955        let to_field = Field::new("nested", list.data_type().clone(), false);
10956        let to = DataType::List(Arc::new(to_field));
10957        let out = cast(&list, &to).unwrap();
10958        let opts = FormatOptions::default().with_null("null");
10959        let formatted = ArrayFormatter::try_new(out.as_ref(), &opts).unwrap();
10960
10961        assert_eq!(formatted.value(0).to_string(), "[[1], [2], [3]]");
10962        assert_eq!(formatted.value(1).to_string(), "[[4], [null], [6]]");
10963    }
10964
10965    #[test]
10966    fn test_nested_list_cast() {
10967        let mut builder = ListBuilder::new(ListBuilder::new(Int32Builder::new()));
10968        builder.append_value([Some([Some(1), Some(2), None]), None]);
10969        builder.append_value([None, Some([]), None]);
10970        builder.append_null();
10971        builder.append_value([Some([Some(2), Some(3)])]);
10972        let start = builder.finish();
10973
10974        let mut builder = LargeListBuilder::new(LargeListBuilder::new(Int8Builder::new()));
10975        builder.append_value([Some([Some(1), Some(2), None]), None]);
10976        builder.append_value([None, Some([]), None]);
10977        builder.append_null();
10978        builder.append_value([Some([Some(2), Some(3)])]);
10979        let expected = builder.finish();
10980
10981        let actual = cast(&start, expected.data_type()).unwrap();
10982        assert_eq!(actual.as_ref(), &expected);
10983    }
10984
10985    const CAST_OPTIONS: CastOptions<'static> = CastOptions {
10986        safe: true,
10987        format_options: FormatOptions::new(),
10988    };
10989
10990    #[test]
10991    #[allow(clippy::assertions_on_constants)]
10992    fn test_const_options() {
10993        assert!(CAST_OPTIONS.safe)
10994    }
10995
10996    #[test]
10997    fn test_list_format_options() {
10998        let options = CastOptions {
10999            safe: false,
11000            format_options: FormatOptions::default().with_null("null"),
11001        };
11002        let array = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
11003            Some(vec![Some(0), Some(1), Some(2)]),
11004            Some(vec![Some(0), None, Some(2)]),
11005        ]);
11006        let a = cast_with_options(&array, &DataType::Utf8, &options).unwrap();
11007        let r: Vec<_> = a.as_string::<i32>().iter().flatten().collect();
11008        assert_eq!(r, &["[0, 1, 2]", "[0, null, 2]"]);
11009    }
11010    #[test]
11011    fn test_cast_string_to_timestamp_invalid_tz() {
11012        // content after Z should be ignored
11013        let bad_timestamp = "2023-12-05T21:58:10.45ZZTOP";
11014        let array = StringArray::from(vec![Some(bad_timestamp)]);
11015
11016        let data_types = [
11017            DataType::Timestamp(TimeUnit::Second, None),
11018            DataType::Timestamp(TimeUnit::Millisecond, None),
11019            DataType::Timestamp(TimeUnit::Microsecond, None),
11020            DataType::Timestamp(TimeUnit::Nanosecond, None),
11021        ];
11022
11023        let cast_options = CastOptions {
11024            safe: false,
11025            ..Default::default()
11026        };
11027
11028        for dt in data_types {
11029            assert_eq!(
11030                cast_with_options(&array, &dt, &cast_options)
11031                    .unwrap_err()
11032                    .to_string(),
11033                "Parser error: Invalid timezone \"ZZTOP\": only offset based timezones supported without chrono-tz feature"
11034            );
11035        }
11036    }
11037    #[test]
11038    fn test_cast_struct_to_struct() {
11039        let struct_type = DataType::Struct(
11040            vec![
11041                Field::new("a", DataType::Boolean, false),
11042                Field::new("b", DataType::Int32, false),
11043            ]
11044            .into(),
11045        );
11046        let to_type = DataType::Struct(
11047            vec![
11048                Field::new("a", DataType::Utf8, false),
11049                Field::new("b", DataType::Utf8, false),
11050            ]
11051            .into(),
11052        );
11053        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11054        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
11055        let struct_array = StructArray::from(vec![
11056            (
11057                Arc::new(Field::new("b", DataType::Boolean, false)),
11058                boolean.clone() as ArrayRef,
11059            ),
11060            (
11061                Arc::new(Field::new("c", DataType::Int32, false)),
11062                int.clone() as ArrayRef,
11063            ),
11064        ]);
11065        let casted_array = cast(&struct_array, &to_type).unwrap();
11066        let casted_array = casted_array.as_struct();
11067        assert_eq!(casted_array.data_type(), &to_type);
11068        let casted_boolean_array = casted_array
11069            .column(0)
11070            .as_string::<i32>()
11071            .into_iter()
11072            .flatten()
11073            .collect::<Vec<_>>();
11074        let casted_int_array = casted_array
11075            .column(1)
11076            .as_string::<i32>()
11077            .into_iter()
11078            .flatten()
11079            .collect::<Vec<_>>();
11080        assert_eq!(casted_boolean_array, vec!["false", "false", "true", "true"]);
11081        assert_eq!(casted_int_array, vec!["42", "28", "19", "31"]);
11082
11083        // test for can't cast
11084        let to_type = DataType::Struct(
11085            vec![
11086                Field::new("a", DataType::Date32, false),
11087                Field::new("b", DataType::Utf8, false),
11088            ]
11089            .into(),
11090        );
11091        assert!(!can_cast_types(&struct_type, &to_type));
11092        let result = cast(&struct_array, &to_type);
11093        assert_eq!(
11094            "Cast error: Casting from Boolean to Date32 not supported",
11095            result.unwrap_err().to_string()
11096        );
11097    }
11098
11099    #[test]
11100    fn test_cast_struct_to_struct_nullability() {
11101        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11102        let int = Arc::new(Int32Array::from(vec![Some(42), None, Some(19), None]));
11103        let struct_array = StructArray::from(vec![
11104            (
11105                Arc::new(Field::new("b", DataType::Boolean, false)),
11106                boolean.clone() as ArrayRef,
11107            ),
11108            (
11109                Arc::new(Field::new("c", DataType::Int32, true)),
11110                int.clone() as ArrayRef,
11111            ),
11112        ]);
11113
11114        // okay: nullable to nullable
11115        let to_type = DataType::Struct(
11116            vec![
11117                Field::new("a", DataType::Utf8, false),
11118                Field::new("b", DataType::Utf8, true),
11119            ]
11120            .into(),
11121        );
11122        cast(&struct_array, &to_type).expect("Cast nullable to nullable struct field should work");
11123
11124        // error: nullable to non-nullable
11125        let to_type = DataType::Struct(
11126            vec![
11127                Field::new("a", DataType::Utf8, false),
11128                Field::new("b", DataType::Utf8, false),
11129            ]
11130            .into(),
11131        );
11132        cast(&struct_array, &to_type)
11133            .expect_err("Cast nullable to non-nullable struct field should fail");
11134
11135        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11136        let int = Arc::new(Int32Array::from(vec![i32::MAX, 25, 1, 100]));
11137        let struct_array = StructArray::from(vec![
11138            (
11139                Arc::new(Field::new("b", DataType::Boolean, false)),
11140                boolean.clone() as ArrayRef,
11141            ),
11142            (
11143                Arc::new(Field::new("c", DataType::Int32, false)),
11144                int.clone() as ArrayRef,
11145            ),
11146        ]);
11147
11148        // okay: non-nullable to non-nullable
11149        let to_type = DataType::Struct(
11150            vec![
11151                Field::new("a", DataType::Utf8, false),
11152                Field::new("b", DataType::Utf8, false),
11153            ]
11154            .into(),
11155        );
11156        cast(&struct_array, &to_type)
11157            .expect("Cast non-nullable to non-nullable struct field should work");
11158
11159        // err: non-nullable to non-nullable but overflowing return null during casting
11160        let to_type = DataType::Struct(
11161            vec![
11162                Field::new("a", DataType::Utf8, false),
11163                Field::new("b", DataType::Int8, false),
11164            ]
11165            .into(),
11166        );
11167        cast(&struct_array, &to_type).expect_err(
11168            "Cast non-nullable to non-nullable struct field returning null should fail",
11169        );
11170    }
11171
11172    #[test]
11173    fn test_cast_struct_to_non_struct() {
11174        let boolean = Arc::new(BooleanArray::from(vec![true, false]));
11175        let struct_array = StructArray::from(vec![(
11176            Arc::new(Field::new("a", DataType::Boolean, false)),
11177            boolean.clone() as ArrayRef,
11178        )]);
11179        let to_type = DataType::Utf8;
11180        let result = cast(&struct_array, &to_type);
11181        assert_eq!(
11182            r#"Cast error: Casting from Struct("a": non-null Boolean) to Utf8 not supported"#,
11183            result.unwrap_err().to_string()
11184        );
11185    }
11186
11187    #[test]
11188    fn test_cast_non_struct_to_struct() {
11189        let array = StringArray::from(vec!["a", "b"]);
11190        let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, false)].into());
11191        let result = cast(&array, &to_type);
11192        assert_eq!(
11193            r#"Cast error: Casting from Utf8 to Struct("a": non-null Boolean) not supported"#,
11194            result.unwrap_err().to_string()
11195        );
11196    }
11197
11198    #[test]
11199    fn test_cast_struct_with_different_field_order() {
11200        // Test slow path: fields are in different order
11201        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11202        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
11203        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
11204
11205        let struct_array = StructArray::from(vec![
11206            (
11207                Arc::new(Field::new("a", DataType::Boolean, false)),
11208                boolean.clone() as ArrayRef,
11209            ),
11210            (
11211                Arc::new(Field::new("b", DataType::Int32, false)),
11212                int.clone() as ArrayRef,
11213            ),
11214            (
11215                Arc::new(Field::new("c", DataType::Utf8, false)),
11216                string.clone() as ArrayRef,
11217            ),
11218        ]);
11219
11220        // Target has fields in different order: c, a, b instead of a, b, c
11221        let to_type = DataType::Struct(
11222            vec![
11223                Field::new("c", DataType::Utf8, false),
11224                Field::new("a", DataType::Utf8, false), // Boolean to Utf8
11225                Field::new("b", DataType::Utf8, false), // Int32 to Utf8
11226            ]
11227            .into(),
11228        );
11229
11230        let result = cast(&struct_array, &to_type).unwrap();
11231        let result_struct = result.as_struct();
11232
11233        assert_eq!(result_struct.data_type(), &to_type);
11234        assert_eq!(result_struct.num_columns(), 3);
11235
11236        // Verify field "c" (originally position 2, now position 0) remains Utf8
11237        let c_column = result_struct.column(0).as_string::<i32>();
11238        assert_eq!(
11239            c_column.into_iter().flatten().collect::<Vec<_>>(),
11240            vec!["foo", "bar", "baz", "qux"]
11241        );
11242
11243        // Verify field "a" (originally position 0, now position 1) was cast from Boolean to Utf8
11244        let a_column = result_struct.column(1).as_string::<i32>();
11245        assert_eq!(
11246            a_column.into_iter().flatten().collect::<Vec<_>>(),
11247            vec!["false", "false", "true", "true"]
11248        );
11249
11250        // Verify field "b" (originally position 1, now position 2) was cast from Int32 to Utf8
11251        let b_column = result_struct.column(2).as_string::<i32>();
11252        assert_eq!(
11253            b_column.into_iter().flatten().collect::<Vec<_>>(),
11254            vec!["42", "28", "19", "31"]
11255        );
11256    }
11257
11258    #[test]
11259    fn test_cast_struct_with_missing_field() {
11260        // Test that casting fails when target has a field not present in source
11261        let boolean = Arc::new(BooleanArray::from(vec![false, true]));
11262        let struct_array = StructArray::from(vec![(
11263            Arc::new(Field::new("a", DataType::Boolean, false)),
11264            boolean.clone() as ArrayRef,
11265        )]);
11266
11267        let to_type = DataType::Struct(
11268            vec![
11269                Field::new("a", DataType::Utf8, false),
11270                Field::new("b", DataType::Int32, false), // Field "b" doesn't exist in source
11271            ]
11272            .into(),
11273        );
11274
11275        let result = cast(&struct_array, &to_type);
11276        assert!(result.is_err());
11277        assert_eq!(
11278            result.unwrap_err().to_string(),
11279            "Invalid argument error: Incorrect number of arrays for StructArray fields, expected 2 got 1"
11280        );
11281    }
11282
11283    #[test]
11284    fn test_cast_struct_with_subset_of_fields() {
11285        // Test casting to a struct with fewer fields (selecting a subset)
11286        let boolean = Arc::new(BooleanArray::from(vec![false, false, true, true]));
11287        let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
11288        let string = Arc::new(StringArray::from(vec!["foo", "bar", "baz", "qux"]));
11289
11290        let struct_array = StructArray::from(vec![
11291            (
11292                Arc::new(Field::new("a", DataType::Boolean, false)),
11293                boolean.clone() as ArrayRef,
11294            ),
11295            (
11296                Arc::new(Field::new("b", DataType::Int32, false)),
11297                int.clone() as ArrayRef,
11298            ),
11299            (
11300                Arc::new(Field::new("c", DataType::Utf8, false)),
11301                string.clone() as ArrayRef,
11302            ),
11303        ]);
11304
11305        // Target has only fields "c" and "a", omitting "b"
11306        let to_type = DataType::Struct(
11307            vec![
11308                Field::new("c", DataType::Utf8, false),
11309                Field::new("a", DataType::Utf8, false),
11310            ]
11311            .into(),
11312        );
11313
11314        let result = cast(&struct_array, &to_type).unwrap();
11315        let result_struct = result.as_struct();
11316
11317        assert_eq!(result_struct.data_type(), &to_type);
11318        assert_eq!(result_struct.num_columns(), 2);
11319
11320        // Verify field "c" remains Utf8
11321        let c_column = result_struct.column(0).as_string::<i32>();
11322        assert_eq!(
11323            c_column.into_iter().flatten().collect::<Vec<_>>(),
11324            vec!["foo", "bar", "baz", "qux"]
11325        );
11326
11327        // Verify field "a" was cast from Boolean to Utf8
11328        let a_column = result_struct.column(1).as_string::<i32>();
11329        assert_eq!(
11330            a_column.into_iter().flatten().collect::<Vec<_>>(),
11331            vec!["false", "false", "true", "true"]
11332        );
11333    }
11334
11335    #[test]
11336    fn test_can_cast_struct_rename_field() {
11337        // Test that can_cast_types returns false when target has a field not in source
11338        let from_type = DataType::Struct(
11339            vec![
11340                Field::new("a", DataType::Int32, false),
11341                Field::new("b", DataType::Utf8, false),
11342            ]
11343            .into(),
11344        );
11345
11346        let to_type = DataType::Struct(
11347            vec![
11348                Field::new("a", DataType::Int64, false),
11349                Field::new("c", DataType::Boolean, false), // Field "c" not in source
11350            ]
11351            .into(),
11352        );
11353
11354        assert!(can_cast_types(&from_type, &to_type));
11355    }
11356
11357    fn run_decimal_cast_test_case_between_multiple_types(t: DecimalCastTestConfig) {
11358        run_decimal_cast_test_case::<Decimal128Type, Decimal128Type>(t.clone());
11359        run_decimal_cast_test_case::<Decimal128Type, Decimal256Type>(t.clone());
11360        run_decimal_cast_test_case::<Decimal256Type, Decimal128Type>(t.clone());
11361        run_decimal_cast_test_case::<Decimal256Type, Decimal256Type>(t.clone());
11362    }
11363
11364    #[test]
11365    fn test_decimal_to_decimal_coverage() {
11366        let test_cases = [
11367            // increase precision, increase scale, infallible
11368            DecimalCastTestConfig {
11369                input_prec: 5,
11370                input_scale: 1,
11371                input_repr: 99999, // 9999.9
11372                output_prec: 10,
11373                output_scale: 6,
11374                expected_output_repr: Ok(9999900000), // 9999.900000
11375            },
11376            // increase precision, increase scale, fallible, safe
11377            DecimalCastTestConfig {
11378                input_prec: 5,
11379                input_scale: 1,
11380                input_repr: 99, // 9999.9
11381                output_prec: 7,
11382                output_scale: 6,
11383                expected_output_repr: Ok(9900000), // 9.900000
11384            },
11385            // increase precision, increase scale, fallible, unsafe
11386            DecimalCastTestConfig {
11387                input_prec: 5,
11388                input_scale: 1,
11389                input_repr: 99999, // 9999.9
11390                output_prec: 7,
11391                output_scale: 6,
11392                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
11393            },
11394            // increase precision, decrease scale, always infallible
11395            DecimalCastTestConfig {
11396                input_prec: 5,
11397                input_scale: 3,
11398                input_repr: 99999, // 99.999
11399                output_prec: 10,
11400                output_scale: 2,
11401                expected_output_repr: Ok(10000), // 100.00
11402            },
11403            // increase precision, decrease scale, no rouding
11404            DecimalCastTestConfig {
11405                input_prec: 5,
11406                input_scale: 3,
11407                input_repr: 99994, // 99.994
11408                output_prec: 10,
11409                output_scale: 2,
11410                expected_output_repr: Ok(9999), // 99.99
11411            },
11412            // increase precision, don't change scale, always infallible
11413            DecimalCastTestConfig {
11414                input_prec: 5,
11415                input_scale: 3,
11416                input_repr: 99999, // 99.999
11417                output_prec: 10,
11418                output_scale: 3,
11419                expected_output_repr: Ok(99999), // 99.999
11420            },
11421            // decrease precision, increase scale, safe
11422            DecimalCastTestConfig {
11423                input_prec: 10,
11424                input_scale: 5,
11425                input_repr: 999999, // 9.99999
11426                output_prec: 8,
11427                output_scale: 7,
11428                expected_output_repr: Ok(99999900), // 9.9999900
11429            },
11430            // decrease precision, increase scale, unsafe
11431            DecimalCastTestConfig {
11432                input_prec: 10,
11433                input_scale: 5,
11434                input_repr: 9999999, // 99.99999
11435                output_prec: 8,
11436                output_scale: 7,
11437                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
11438            },
11439            // decrease precision, decrease scale, safe, infallible
11440            DecimalCastTestConfig {
11441                input_prec: 7,
11442                input_scale: 4,
11443                input_repr: 9999999, // 999.9999
11444                output_prec: 6,
11445                output_scale: 2,
11446                expected_output_repr: Ok(100000),
11447            },
11448            // decrease precision, decrease scale, safe, fallible
11449            DecimalCastTestConfig {
11450                input_prec: 10,
11451                input_scale: 5,
11452                input_repr: 12345678, // 123.45678
11453                output_prec: 8,
11454                output_scale: 3,
11455                expected_output_repr: Ok(123457), // 123.457
11456            },
11457            // decrease precision, decrease scale, unsafe
11458            DecimalCastTestConfig {
11459                input_prec: 10,
11460                input_scale: 5,
11461                input_repr: 9999999, // 99.99999
11462                output_prec: 4,
11463                output_scale: 3,
11464                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
11465            },
11466            // decrease precision, same scale, safe
11467            DecimalCastTestConfig {
11468                input_prec: 10,
11469                input_scale: 5,
11470                input_repr: 999999, // 9.99999
11471                output_prec: 6,
11472                output_scale: 5,
11473                expected_output_repr: Ok(999999), // 9.99999
11474            },
11475            // decrease precision, same scale, unsafe
11476            DecimalCastTestConfig {
11477                input_prec: 10,
11478                input_scale: 5,
11479                input_repr: 9999999, // 99.99999
11480                output_prec: 6,
11481                output_scale: 5,
11482                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
11483            },
11484            // same precision, increase scale, safe
11485            DecimalCastTestConfig {
11486                input_prec: 7,
11487                input_scale: 4,
11488                input_repr: 12345, // 1.2345
11489                output_prec: 7,
11490                output_scale: 6,
11491                expected_output_repr: Ok(1234500), // 1.234500
11492            },
11493            // same precision, increase scale, unsafe
11494            DecimalCastTestConfig {
11495                input_prec: 7,
11496                input_scale: 4,
11497                input_repr: 123456, // 12.3456
11498                output_prec: 7,
11499                output_scale: 6,
11500                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
11501            },
11502            // same precision, decrease scale, infallible
11503            DecimalCastTestConfig {
11504                input_prec: 7,
11505                input_scale: 5,
11506                input_repr: 1234567, // 12.34567
11507                output_prec: 7,
11508                output_scale: 4,
11509                expected_output_repr: Ok(123457), // 12.3457
11510            },
11511            // same precision, same scale, infallible
11512            DecimalCastTestConfig {
11513                input_prec: 7,
11514                input_scale: 5,
11515                input_repr: 9999999, // 99.99999
11516                output_prec: 7,
11517                output_scale: 5,
11518                expected_output_repr: Ok(9999999), // 99.99999
11519            },
11520            // precision increase, input scale & output scale = 0, infallible
11521            DecimalCastTestConfig {
11522                input_prec: 7,
11523                input_scale: 0,
11524                input_repr: 1234567, // 1234567
11525                output_prec: 8,
11526                output_scale: 0,
11527                expected_output_repr: Ok(1234567), // 1234567
11528            },
11529            // precision decrease, input scale & output scale = 0, failure
11530            DecimalCastTestConfig {
11531                input_prec: 7,
11532                input_scale: 0,
11533                input_repr: 1234567, // 1234567
11534                output_prec: 6,
11535                output_scale: 0,
11536                expected_output_repr: Err("Invalid argument error: 1234567 is too large to store in a {} of precision 6. Max is 999999".to_string())
11537            },
11538            // precision decrease, input scale & output scale = 0, success
11539            DecimalCastTestConfig {
11540                input_prec: 7,
11541                input_scale: 0,
11542                input_repr: 123456, // 123456
11543                output_prec: 6,
11544                output_scale: 0,
11545                expected_output_repr: Ok(123456), // 123456
11546            },
11547        ];
11548
11549        for t in test_cases {
11550            run_decimal_cast_test_case_between_multiple_types(t);
11551        }
11552    }
11553
11554    #[test]
11555    fn test_decimal_to_decimal_increase_scale_and_precision_unchecked() {
11556        let test_cases = [
11557            DecimalCastTestConfig {
11558                input_prec: 5,
11559                input_scale: 0,
11560                input_repr: 99999,
11561                output_prec: 10,
11562                output_scale: 5,
11563                expected_output_repr: Ok(9999900000),
11564            },
11565            DecimalCastTestConfig {
11566                input_prec: 5,
11567                input_scale: 0,
11568                input_repr: -99999,
11569                output_prec: 10,
11570                output_scale: 5,
11571                expected_output_repr: Ok(-9999900000),
11572            },
11573            DecimalCastTestConfig {
11574                input_prec: 5,
11575                input_scale: 2,
11576                input_repr: 99999,
11577                output_prec: 10,
11578                output_scale: 5,
11579                expected_output_repr: Ok(99999000),
11580            },
11581            DecimalCastTestConfig {
11582                input_prec: 5,
11583                input_scale: -2,
11584                input_repr: -99999,
11585                output_prec: 10,
11586                output_scale: 3,
11587                expected_output_repr: Ok(-9999900000),
11588            },
11589            DecimalCastTestConfig {
11590                input_prec: 5,
11591                input_scale: 3,
11592                input_repr: -12345,
11593                output_prec: 6,
11594                output_scale: 5,
11595                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())
11596            },
11597        ];
11598
11599        for t in test_cases {
11600            run_decimal_cast_test_case_between_multiple_types(t);
11601        }
11602    }
11603
11604    #[test]
11605    fn test_decimal_to_decimal_decrease_scale_and_precision_unchecked() {
11606        let test_cases = [
11607            DecimalCastTestConfig {
11608                input_prec: 5,
11609                input_scale: 0,
11610                input_repr: 99999,
11611                output_scale: -3,
11612                output_prec: 3,
11613                expected_output_repr: Ok(100),
11614            },
11615            DecimalCastTestConfig {
11616                input_prec: 5,
11617                input_scale: 0,
11618                input_repr: -99999,
11619                output_prec: 1,
11620                output_scale: -5,
11621                expected_output_repr: Ok(-1),
11622            },
11623            DecimalCastTestConfig {
11624                input_prec: 10,
11625                input_scale: 2,
11626                input_repr: 123456789,
11627                output_prec: 5,
11628                output_scale: -2,
11629                expected_output_repr: Ok(12346),
11630            },
11631            DecimalCastTestConfig {
11632                input_prec: 10,
11633                input_scale: 4,
11634                input_repr: -9876543210,
11635                output_prec: 7,
11636                output_scale: 0,
11637                expected_output_repr: Ok(-987654),
11638            },
11639            DecimalCastTestConfig {
11640                input_prec: 7,
11641                input_scale: 4,
11642                input_repr: 9999999,
11643                output_prec: 6,
11644                output_scale: 3,
11645                expected_output_repr:
11646                    Err("Invalid argument error: 1000.000 is too large to store in a {} of precision 6. Max is 999.999".to_string()),
11647            },
11648        ];
11649        for t in test_cases {
11650            run_decimal_cast_test_case_between_multiple_types(t);
11651        }
11652    }
11653
11654    #[test]
11655    fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
11656        let array = vec![Some(123456789)];
11657        let array = create_decimal128_array(array, 24, 2).unwrap();
11658        let input_type = DataType::Decimal128(24, 2);
11659        let output_type = DataType::Decimal128(6, 2);
11660        assert!(can_cast_types(&input_type, &output_type));
11661
11662        let options = CastOptions {
11663            safe: false,
11664            ..Default::default()
11665        };
11666        let result = cast_with_options(&array, &output_type, &options);
11667        assert_eq!(
11668            result.unwrap_err().to_string(),
11669            "Invalid argument error: 1234567.89 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
11670        );
11671    }
11672
11673    #[test]
11674    fn test_decimal_to_decimal_same_scale() {
11675        let array = vec![Some(520)];
11676        let array = create_decimal128_array(array, 4, 2).unwrap();
11677        let input_type = DataType::Decimal128(4, 2);
11678        let output_type = DataType::Decimal128(3, 2);
11679        assert!(can_cast_types(&input_type, &output_type));
11680
11681        let options = CastOptions {
11682            safe: false,
11683            ..Default::default()
11684        };
11685        let result = cast_with_options(&array, &output_type, &options);
11686        assert_eq!(
11687            result.unwrap().as_primitive::<Decimal128Type>().value(0),
11688            520
11689        );
11690
11691        // Cast 0 of decimal(3, 0) type to decimal(2, 0)
11692        assert_eq!(
11693            &cast(
11694                &create_decimal128_array(vec![Some(0)], 3, 0).unwrap(),
11695                &DataType::Decimal128(2, 0)
11696            )
11697            .unwrap(),
11698            &(Arc::new(create_decimal128_array(vec![Some(0)], 2, 0).unwrap()) as ArrayRef)
11699        );
11700    }
11701
11702    #[test]
11703    fn test_decimal_to_decimal_throw_error_on_precision_overflow_lower_scale() {
11704        let array = vec![Some(123456789)];
11705        let array = create_decimal128_array(array, 24, 4).unwrap();
11706        let input_type = DataType::Decimal128(24, 4);
11707        let output_type = DataType::Decimal128(6, 2);
11708        assert!(can_cast_types(&input_type, &output_type));
11709
11710        let options = CastOptions {
11711            safe: false,
11712            ..Default::default()
11713        };
11714        let result = cast_with_options(&array, &output_type, &options);
11715        assert_eq!(
11716            result.unwrap_err().to_string(),
11717            "Invalid argument error: 12345.68 is too large to store in a Decimal128 of precision 6. Max is 9999.99"
11718        );
11719    }
11720
11721    #[test]
11722    fn test_decimal_to_decimal_throw_error_on_precision_overflow_greater_scale() {
11723        let array = vec![Some(123456789)];
11724        let array = create_decimal128_array(array, 24, 2).unwrap();
11725        let input_type = DataType::Decimal128(24, 2);
11726        let output_type = DataType::Decimal128(6, 3);
11727        assert!(can_cast_types(&input_type, &output_type));
11728
11729        let options = CastOptions {
11730            safe: false,
11731            ..Default::default()
11732        };
11733        let result = cast_with_options(&array, &output_type, &options);
11734        assert_eq!(
11735            result.unwrap_err().to_string(),
11736            "Invalid argument error: 1234567.890 is too large to store in a Decimal128 of precision 6. Max is 999.999"
11737        );
11738    }
11739
11740    #[test]
11741    fn test_decimal_to_decimal_throw_error_on_precision_overflow_diff_type() {
11742        let array = vec![Some(123456789)];
11743        let array = create_decimal128_array(array, 24, 2).unwrap();
11744        let input_type = DataType::Decimal128(24, 2);
11745        let output_type = DataType::Decimal256(6, 2);
11746        assert!(can_cast_types(&input_type, &output_type));
11747
11748        let options = CastOptions {
11749            safe: false,
11750            ..Default::default()
11751        };
11752        let result = cast_with_options(&array, &output_type, &options).unwrap_err();
11753        assert_eq!(
11754            result.to_string(),
11755            "Invalid argument error: 1234567.89 is too large to store in a Decimal256 of precision 6. Max is 9999.99"
11756        );
11757    }
11758
11759    #[test]
11760    fn test_first_none() {
11761        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
11762            None,
11763            Some(vec![Some(1), Some(2)]),
11764        ])) as ArrayRef;
11765        let data_type =
11766            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
11767        let opt = CastOptions::default();
11768        let r = cast_with_options(&array, &data_type, &opt).unwrap();
11769
11770        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
11771            vec![None, Some(vec![Some(1), Some(2)])],
11772            2,
11773        )) as ArrayRef;
11774        assert_eq!(*fixed_array, *r);
11775    }
11776
11777    #[test]
11778    fn test_first_last_none() {
11779        let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
11780            None,
11781            Some(vec![Some(1), Some(2)]),
11782            None,
11783        ])) as ArrayRef;
11784        let data_type =
11785            DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
11786        let opt = CastOptions::default();
11787        let r = cast_with_options(&array, &data_type, &opt).unwrap();
11788
11789        let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
11790            vec![None, Some(vec![Some(1), Some(2)]), None],
11791            2,
11792        )) as ArrayRef;
11793        assert_eq!(*fixed_array, *r);
11794    }
11795
11796    #[test]
11797    fn test_cast_decimal_error_output() {
11798        let array = Int64Array::from(vec![1]);
11799        let error = cast_with_options(
11800            &array,
11801            &DataType::Decimal32(1, 1),
11802            &CastOptions {
11803                safe: false,
11804                format_options: FormatOptions::default(),
11805            },
11806        )
11807        .unwrap_err();
11808        assert_eq!(
11809            error.to_string(),
11810            "Invalid argument error: 1.0 is too large to store in a Decimal32 of precision 1. Max is 0.9"
11811        );
11812
11813        let array = Int64Array::from(vec![-1]);
11814        let error = cast_with_options(
11815            &array,
11816            &DataType::Decimal32(1, 1),
11817            &CastOptions {
11818                safe: false,
11819                format_options: FormatOptions::default(),
11820            },
11821        )
11822        .unwrap_err();
11823        assert_eq!(
11824            error.to_string(),
11825            "Invalid argument error: -1.0 is too small to store in a Decimal32 of precision 1. Min is -0.9"
11826        );
11827    }
11828
11829    #[test]
11830    fn test_run_end_encoded_to_primitive() {
11831        // Create a RunEndEncoded array: [1, 1, 2, 2, 2, 3]
11832        let run_ends = Int32Array::from(vec![2, 5, 6]);
11833        let values = Int32Array::from(vec![1, 2, 3]);
11834        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
11835        let array_ref = Arc::new(run_array) as ArrayRef;
11836        // Cast to Int64
11837        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
11838        // Verify the result is a RunArray with Int64 values
11839        let result_run_array = cast_result.as_any().downcast_ref::<Int64Array>().unwrap();
11840        assert_eq!(
11841            result_run_array.values(),
11842            &[1i64, 1i64, 2i64, 2i64, 2i64, 3i64]
11843        );
11844    }
11845
11846    #[test]
11847    fn test_sliced_run_end_encoded_to_primitive() {
11848        let run_ends = Int32Array::from(vec![2, 5, 6]);
11849        let values = Int32Array::from(vec![1, 2, 3]);
11850        // [1, 1, 2, 2, 2, 3]
11851        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
11852        let run_array = run_array.slice(3, 3); // [2, 2, 3]
11853        let array_ref = Arc::new(run_array) as ArrayRef;
11854
11855        let cast_result = cast(&array_ref, &DataType::Int64).unwrap();
11856        let result_run_array = cast_result.as_primitive::<Int64Type>();
11857        assert_eq!(result_run_array.values(), &[2, 2, 3]);
11858    }
11859
11860    #[test]
11861    fn test_run_end_encoded_to_string() {
11862        let run_ends = Int32Array::from(vec![2, 3, 5]);
11863        let values = Int32Array::from(vec![10, 20, 30]);
11864        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
11865        let array_ref = Arc::new(run_array) as ArrayRef;
11866
11867        // Cast to String
11868        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
11869
11870        // Verify the result is a RunArray with String values
11871        let result_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
11872        // Check that values are correct
11873        assert_eq!(result_array.value(0), "10");
11874        assert_eq!(result_array.value(1), "10");
11875        assert_eq!(result_array.value(2), "20");
11876    }
11877
11878    #[test]
11879    fn test_primitive_to_run_end_encoded() {
11880        // Create an Int32 array with repeated values: [1, 1, 2, 2, 2, 3]
11881        let source_array = Int32Array::from(vec![1, 1, 2, 2, 2, 3]);
11882        let array_ref = Arc::new(source_array) as ArrayRef;
11883
11884        // Cast to RunEndEncoded<Int32, Int32>
11885        let target_type = DataType::RunEndEncoded(
11886            Arc::new(Field::new("run_ends", DataType::Int32, false)),
11887            Arc::new(Field::new("values", DataType::Int32, true)),
11888        );
11889        let cast_result = cast(&array_ref, &target_type).unwrap();
11890
11891        // Verify the result is a RunArray
11892        let result_run_array = cast_result
11893            .as_any()
11894            .downcast_ref::<RunArray<Int32Type>>()
11895            .unwrap();
11896
11897        // Check run structure: runs should end at positions [2, 5, 6]
11898        assert_eq!(result_run_array.run_ends().values(), &[2, 5, 6]);
11899
11900        // Check values: should be [1, 2, 3]
11901        let values_array = result_run_array.values().as_primitive::<Int32Type>();
11902        assert_eq!(values_array.values(), &[1, 2, 3]);
11903    }
11904
11905    #[test]
11906    fn test_primitive_to_run_end_encoded_with_nulls() {
11907        let source_array = Int32Array::from(vec![
11908            Some(1),
11909            Some(1),
11910            None,
11911            None,
11912            Some(2),
11913            Some(2),
11914            Some(3),
11915            Some(3),
11916            None,
11917            None,
11918            Some(4),
11919            Some(4),
11920            Some(5),
11921            Some(5),
11922            None,
11923            None,
11924        ]);
11925        let array_ref = Arc::new(source_array) as ArrayRef;
11926        let target_type = DataType::RunEndEncoded(
11927            Arc::new(Field::new("run_ends", DataType::Int32, false)),
11928            Arc::new(Field::new("values", DataType::Int32, true)),
11929        );
11930        let cast_result = cast(&array_ref, &target_type).unwrap();
11931        let result_run_array = cast_result
11932            .as_any()
11933            .downcast_ref::<RunArray<Int32Type>>()
11934            .unwrap();
11935        assert_eq!(
11936            result_run_array.run_ends().values(),
11937            &[2, 4, 6, 8, 10, 12, 14, 16]
11938        );
11939        assert_eq!(
11940            result_run_array
11941                .values()
11942                .as_primitive::<Int32Type>()
11943                .values(),
11944            &[1, 0, 2, 3, 0, 4, 5, 0]
11945        );
11946        assert_eq!(result_run_array.values().null_count(), 3);
11947    }
11948
11949    #[test]
11950    fn test_primitive_to_run_end_encoded_with_nulls_consecutive() {
11951        let source_array = Int64Array::from(vec![
11952            Some(1),
11953            Some(1),
11954            None,
11955            None,
11956            None,
11957            None,
11958            None,
11959            None,
11960            None,
11961            None,
11962            Some(4),
11963            Some(20),
11964            Some(500),
11965            Some(500),
11966            None,
11967            None,
11968        ]);
11969        let array_ref = Arc::new(source_array) as ArrayRef;
11970        let target_type = DataType::RunEndEncoded(
11971            Arc::new(Field::new("run_ends", DataType::Int16, false)),
11972            Arc::new(Field::new("values", DataType::Int64, true)),
11973        );
11974        let cast_result = cast(&array_ref, &target_type).unwrap();
11975        let result_run_array = cast_result
11976            .as_any()
11977            .downcast_ref::<RunArray<Int16Type>>()
11978            .unwrap();
11979        assert_eq!(
11980            result_run_array.run_ends().values(),
11981            &[2, 10, 11, 12, 14, 16]
11982        );
11983        assert_eq!(
11984            result_run_array
11985                .values()
11986                .as_primitive::<Int64Type>()
11987                .values(),
11988            &[1, 0, 4, 20, 500, 0]
11989        );
11990        assert_eq!(result_run_array.values().null_count(), 2);
11991    }
11992
11993    #[test]
11994    fn test_string_to_run_end_encoded() {
11995        // Create a String array with repeated values: ["a", "a", "b", "c", "c"]
11996        let source_array = StringArray::from(vec!["a", "a", "b", "c", "c"]);
11997        let array_ref = Arc::new(source_array) as ArrayRef;
11998
11999        // Cast to RunEndEncoded<Int32, String>
12000        let target_type = DataType::RunEndEncoded(
12001            Arc::new(Field::new("run_ends", DataType::Int32, false)),
12002            Arc::new(Field::new("values", DataType::Utf8, true)),
12003        );
12004        let cast_result = cast(&array_ref, &target_type).unwrap();
12005
12006        // Verify the result is a RunArray
12007        let result_run_array = cast_result
12008            .as_any()
12009            .downcast_ref::<RunArray<Int32Type>>()
12010            .unwrap();
12011
12012        // Check run structure: runs should end at positions [2, 3, 5]
12013        assert_eq!(result_run_array.run_ends().values(), &[2, 3, 5]);
12014
12015        // Check values: should be ["a", "b", "c"]
12016        let values_array = result_run_array.values().as_string::<i32>();
12017        assert_eq!(values_array.value(0), "a");
12018        assert_eq!(values_array.value(1), "b");
12019        assert_eq!(values_array.value(2), "c");
12020    }
12021
12022    #[test]
12023    fn test_empty_array_to_run_end_encoded() {
12024        // Create an empty Int32 array
12025        let source_array = Int32Array::from(Vec::<i32>::new());
12026        let array_ref = Arc::new(source_array) as ArrayRef;
12027
12028        // Cast to RunEndEncoded<Int32, Int32>
12029        let target_type = DataType::RunEndEncoded(
12030            Arc::new(Field::new("run_ends", DataType::Int32, false)),
12031            Arc::new(Field::new("values", DataType::Int32, true)),
12032        );
12033        let cast_result = cast(&array_ref, &target_type).unwrap();
12034
12035        // Verify the result is an empty RunArray
12036        let result_run_array = cast_result
12037            .as_any()
12038            .downcast_ref::<RunArray<Int32Type>>()
12039            .unwrap();
12040
12041        // Check that both run_ends and values are empty
12042        assert_eq!(result_run_array.run_ends().len(), 0);
12043        assert_eq!(result_run_array.values().len(), 0);
12044    }
12045
12046    #[test]
12047    fn test_run_end_encoded_with_nulls() {
12048        // Create a RunEndEncoded array with nulls: [1, 1, null, 2, 2]
12049        let run_ends = Int32Array::from(vec![2, 3, 5]);
12050        let values = Int32Array::from(vec![Some(1), None, Some(2)]);
12051        let run_array = RunArray::<Int32Type>::try_new(&run_ends, &values).unwrap();
12052        let array_ref = Arc::new(run_array) as ArrayRef;
12053
12054        // Cast to String
12055        let cast_result = cast(&array_ref, &DataType::Utf8).unwrap();
12056
12057        // Verify the result preserves nulls
12058        let result_run_array = cast_result.as_any().downcast_ref::<StringArray>().unwrap();
12059        assert_eq!(result_run_array.value(0), "1");
12060        assert!(result_run_array.is_null(2));
12061        assert_eq!(result_run_array.value(4), "2");
12062    }
12063
12064    #[test]
12065    fn test_different_index_types() {
12066        // Test with Int16 index type
12067        let source_array = Int32Array::from(vec![1, 1, 2, 3, 3]);
12068        let array_ref = Arc::new(source_array) as ArrayRef;
12069
12070        let target_type = DataType::RunEndEncoded(
12071            Arc::new(Field::new("run_ends", DataType::Int16, false)),
12072            Arc::new(Field::new("values", DataType::Int32, true)),
12073        );
12074        let cast_result = cast(&array_ref, &target_type).unwrap();
12075        assert_eq!(cast_result.data_type(), &target_type);
12076
12077        // Verify the cast worked correctly: values are [1, 2, 3]
12078        // and run-ends are [2, 3, 5]
12079        let run_array = cast_result
12080            .as_any()
12081            .downcast_ref::<RunArray<Int16Type>>()
12082            .unwrap();
12083        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
12084        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
12085        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
12086        assert_eq!(run_array.run_ends().values(), &[2i16, 3i16, 5i16]);
12087
12088        // Test again with Int64 index type
12089        let target_type = DataType::RunEndEncoded(
12090            Arc::new(Field::new("run_ends", DataType::Int64, false)),
12091            Arc::new(Field::new("values", DataType::Int32, true)),
12092        );
12093        let cast_result = cast(&array_ref, &target_type).unwrap();
12094        assert_eq!(cast_result.data_type(), &target_type);
12095
12096        // Verify the cast worked correctly: values are [1, 2, 3]
12097        // and run-ends are [2, 3, 5]
12098        let run_array = cast_result
12099            .as_any()
12100            .downcast_ref::<RunArray<Int64Type>>()
12101            .unwrap();
12102        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(0), 1);
12103        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(1), 2);
12104        assert_eq!(run_array.values().as_primitive::<Int32Type>().value(2), 3);
12105        assert_eq!(run_array.run_ends().values(), &[2i64, 3i64, 5i64]);
12106    }
12107
12108    #[test]
12109    fn test_unsupported_cast_to_run_end_encoded() {
12110        // Create a Struct array - complex nested type that might not be supported
12111        let field = Field::new("item", DataType::Int32, false);
12112        let struct_array = StructArray::from(vec![(
12113            Arc::new(field),
12114            Arc::new(Int32Array::from(vec![1, 2, 3])) as ArrayRef,
12115        )]);
12116        let array_ref = Arc::new(struct_array) as ArrayRef;
12117
12118        // This should fail because:
12119        // 1. The target type is not RunEndEncoded
12120        // 2. The target type is not supported for casting from StructArray
12121        let cast_result = cast(&array_ref, &DataType::FixedSizeBinary(10));
12122
12123        // Expect this to fail
12124        assert!(cast_result.is_err());
12125    }
12126
12127    /// Test casting RunEndEncoded<Int64, String> to RunEndEncoded<Int16, String> should fail
12128    #[test]
12129    fn test_cast_run_end_encoded_int64_to_int16_should_fail() {
12130        // Construct a valid REE array with Int64 run-ends
12131        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
12132        let values = StringArray::from(vec!["a", "b", "c"]);
12133
12134        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
12135        let array_ref = Arc::new(ree_array) as ArrayRef;
12136
12137        // Attempt to cast to RunEndEncoded<Int16, Utf8>
12138        let target_type = DataType::RunEndEncoded(
12139            Arc::new(Field::new("run_ends", DataType::Int16, false)),
12140            Arc::new(Field::new("values", DataType::Utf8, true)),
12141        );
12142        let cast_options = CastOptions {
12143            safe: false, // This should make it fail instead of returning nulls
12144            format_options: FormatOptions::default(),
12145        };
12146
12147        // This should fail due to run-end overflow
12148        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12149            cast_with_options(&array_ref, &target_type, &cast_options);
12150
12151        let e = result.expect_err("Cast should have failed but succeeded");
12152        assert!(
12153            e.to_string()
12154                .contains("Cast error: Can't cast value 100000 to type Int16")
12155        );
12156    }
12157
12158    #[test]
12159    fn test_cast_run_end_encoded_int64_to_int16_with_safe_should_fail_with_null_invalid_error() {
12160        // Construct a valid REE array with Int64 run-ends
12161        let run_ends = Int64Array::from(vec![100_000, 400_000, 700_000]); // values too large for Int16
12162        let values = StringArray::from(vec!["a", "b", "c"]);
12163
12164        let ree_array = RunArray::<Int64Type>::try_new(&run_ends, &values).unwrap();
12165        let array_ref = Arc::new(ree_array) as ArrayRef;
12166
12167        // Attempt to cast to RunEndEncoded<Int16, Utf8>
12168        let target_type = DataType::RunEndEncoded(
12169            Arc::new(Field::new("run_ends", DataType::Int16, false)),
12170            Arc::new(Field::new("values", DataType::Utf8, true)),
12171        );
12172        let cast_options = CastOptions {
12173            safe: true,
12174            format_options: FormatOptions::default(),
12175        };
12176
12177        // This fails even though safe is true because the run_ends array has null values
12178        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12179            cast_with_options(&array_ref, &target_type, &cast_options);
12180        let e = result.expect_err("Cast should have failed but succeeded");
12181        assert!(
12182            e.to_string()
12183                .contains("Invalid argument error: Found null values in run_ends array. The run_ends array should not have null values.")
12184        );
12185    }
12186
12187    /// Test casting RunEndEncoded<Int16, String> to RunEndEncoded<Int64, String> should succeed
12188    #[test]
12189    fn test_cast_run_end_encoded_int16_to_int64_should_succeed() {
12190        // Construct a valid REE array with Int16 run-ends
12191        let run_ends = Int16Array::from(vec![2, 5, 8]); // values that fit in Int16
12192        let values = StringArray::from(vec!["a", "b", "c"]);
12193
12194        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
12195        let array_ref = Arc::new(ree_array) as ArrayRef;
12196
12197        // Attempt to cast to RunEndEncoded<Int64, Utf8> (upcast should succeed)
12198        let target_type = DataType::RunEndEncoded(
12199            Arc::new(Field::new("run_ends", DataType::Int64, false)),
12200            Arc::new(Field::new("values", DataType::Utf8, true)),
12201        );
12202        let cast_options = CastOptions {
12203            safe: false,
12204            format_options: FormatOptions::default(),
12205        };
12206
12207        // This should succeed due to valid upcast
12208        let result: Result<Arc<dyn Array + 'static>, ArrowError> =
12209            cast_with_options(&array_ref, &target_type, &cast_options);
12210
12211        let array_ref = result.expect("Cast should have succeeded but failed");
12212        // Downcast to RunArray<Int64Type>
12213        let run_array = array_ref
12214            .as_any()
12215            .downcast_ref::<RunArray<Int64Type>>()
12216            .unwrap();
12217
12218        // Verify the cast worked correctly
12219        // Assert the values were cast correctly
12220        assert_eq!(run_array.run_ends().values(), &[2i64, 5i64, 8i64]);
12221        assert_eq!(run_array.values().as_string::<i32>().value(0), "a");
12222        assert_eq!(run_array.values().as_string::<i32>().value(1), "b");
12223        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
12224    }
12225
12226    #[test]
12227    fn test_cast_run_end_encoded_dictionary_to_run_end_encoded() {
12228        // Construct a valid dictionary encoded array
12229        let values = StringArray::from_iter([Some("a"), Some("b"), Some("c")]);
12230        let keys = UInt64Array::from_iter(vec![1, 1, 1, 0, 0, 0, 2, 2, 2]);
12231        let array_ref = Arc::new(DictionaryArray::new(keys, Arc::new(values))) as ArrayRef;
12232
12233        // Attempt to cast to RunEndEncoded<Int64, Utf8>
12234        let target_type = DataType::RunEndEncoded(
12235            Arc::new(Field::new("run_ends", DataType::Int64, false)),
12236            Arc::new(Field::new("values", DataType::Utf8, true)),
12237        );
12238        let cast_options = CastOptions {
12239            safe: false,
12240            format_options: FormatOptions::default(),
12241        };
12242
12243        // This should succeed
12244        let result = cast_with_options(&array_ref, &target_type, &cast_options)
12245            .expect("Cast should have succeeded but failed");
12246
12247        // Verify the cast worked correctly
12248        // Assert the values were cast correctly
12249        let run_array = result
12250            .as_any()
12251            .downcast_ref::<RunArray<Int64Type>>()
12252            .unwrap();
12253        assert_eq!(run_array.values().as_string::<i32>().value(0), "b");
12254        assert_eq!(run_array.values().as_string::<i32>().value(1), "a");
12255        assert_eq!(run_array.values().as_string::<i32>().value(2), "c");
12256
12257        // Verify the run-ends were cast correctly (run ends at 3, 6, 9)
12258        assert_eq!(run_array.run_ends().values(), &[3i64, 6i64, 9i64]);
12259    }
12260
12261    fn int32_list_values() -> Vec<Option<Vec<Option<i32>>>> {
12262        vec![
12263            Some(vec![Some(1), Some(2), Some(3)]),
12264            Some(vec![Some(4), Some(5), Some(6)]),
12265            None,
12266            Some(vec![Some(7), Some(8), Some(9)]),
12267            Some(vec![None, Some(10)]),
12268        ]
12269    }
12270
12271    #[test]
12272    fn test_cast_list_view_to_list() {
12273        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12274        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
12275        assert!(can_cast_types(list_view.data_type(), &target_type));
12276        let cast_result = cast(&list_view, &target_type).unwrap();
12277        let got_list = cast_result.as_any().downcast_ref::<ListArray>().unwrap();
12278        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12279        assert_eq!(got_list, &expected_list);
12280    }
12281
12282    #[test]
12283    fn test_cast_list_to_list_view() {
12284        let list = ListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12285        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
12286        assert!(can_cast_types(list.data_type(), &target_type));
12287        let cast_result = cast(&list, &target_type).unwrap();
12288
12289        let got_list_view = cast_result
12290            .as_any()
12291            .downcast_ref::<ListViewArray>()
12292            .unwrap();
12293        let expected_list_view =
12294            ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12295        assert_eq!(got_list_view, &expected_list_view);
12296    }
12297
12298    #[test]
12299    fn test_cast_large_list_view_to_large_list() {
12300        let list_view =
12301            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12302        let target_type = DataType::LargeList(Arc::new(Field::new("item", DataType::Int32, true)));
12303        assert!(can_cast_types(list_view.data_type(), &target_type));
12304        let cast_result = cast(&list_view, &target_type).unwrap();
12305        let got_list = cast_result
12306            .as_any()
12307            .downcast_ref::<LargeListArray>()
12308            .unwrap();
12309
12310        let expected_list =
12311            LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12312        assert_eq!(got_list, &expected_list);
12313    }
12314
12315    #[test]
12316    fn test_cast_large_list_to_large_list_view() {
12317        let list = LargeListArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12318        let target_type =
12319            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
12320        assert!(can_cast_types(list.data_type(), &target_type));
12321        let cast_result = cast(&list, &target_type).unwrap();
12322
12323        let got_list_view = cast_result
12324            .as_any()
12325            .downcast_ref::<LargeListViewArray>()
12326            .unwrap();
12327        let expected_list_view =
12328            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12329        assert_eq!(got_list_view, &expected_list_view);
12330    }
12331
12332    #[test]
12333    fn test_cast_list_view_to_list_out_of_order() {
12334        let list_view = ListViewArray::new(
12335            Arc::new(Field::new("item", DataType::Int32, true)),
12336            ScalarBuffer::from(vec![0, 6, 3]),
12337            ScalarBuffer::from(vec![3, 3, 3]),
12338            Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
12339            None,
12340        );
12341        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
12342        assert!(can_cast_types(list_view.data_type(), &target_type));
12343        let cast_result = cast(&list_view, &target_type).unwrap();
12344        let got_list = cast_result.as_any().downcast_ref::<ListArray>().unwrap();
12345        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
12346            Some(vec![Some(1), Some(2), Some(3)]),
12347            Some(vec![Some(7), Some(8), Some(9)]),
12348            Some(vec![Some(4), Some(5), Some(6)]),
12349        ]);
12350        assert_eq!(got_list, &expected_list);
12351    }
12352
12353    #[test]
12354    fn test_cast_list_view_to_list_overlapping() {
12355        let list_view = ListViewArray::new(
12356            Arc::new(Field::new("item", DataType::Int32, true)),
12357            ScalarBuffer::from(vec![0, 0]),
12358            ScalarBuffer::from(vec![1, 2]),
12359            Arc::new(Int32Array::from(vec![1, 2])),
12360            None,
12361        );
12362        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
12363        assert!(can_cast_types(list_view.data_type(), &target_type));
12364        let cast_result = cast(&list_view, &target_type).unwrap();
12365        let got_list = cast_result.as_any().downcast_ref::<ListArray>().unwrap();
12366        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
12367            Some(vec![Some(1)]),
12368            Some(vec![Some(1), Some(2)]),
12369        ]);
12370        assert_eq!(got_list, &expected_list);
12371    }
12372
12373    #[test]
12374    fn test_cast_list_view_to_list_empty() {
12375        let values: Vec<Option<Vec<Option<i32>>>> = vec![];
12376        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
12377        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
12378        assert!(can_cast_types(list_view.data_type(), &target_type));
12379        let cast_result = cast(&list_view, &target_type).unwrap();
12380        let got_list = cast_result.as_any().downcast_ref::<ListArray>().unwrap();
12381        let expected_list = ListArray::from_iter_primitive::<Int32Type, _, _>(values);
12382        assert_eq!(got_list, &expected_list);
12383    }
12384
12385    #[test]
12386    fn test_cast_list_view_to_list_different_inner_type() {
12387        let values = int32_list_values();
12388        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(values.clone());
12389        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int64, true)));
12390        assert!(can_cast_types(list_view.data_type(), &target_type));
12391        let cast_result = cast(&list_view, &target_type).unwrap();
12392        let got_list = cast_result.as_any().downcast_ref::<ListArray>().unwrap();
12393
12394        let expected_list =
12395            ListArray::from_iter_primitive::<Int64Type, _, _>(values.into_iter().map(|list| {
12396                list.map(|list| {
12397                    list.into_iter()
12398                        .map(|v| v.map(|v| v as i64))
12399                        .collect::<Vec<_>>()
12400                })
12401            }));
12402        assert_eq!(got_list, &expected_list);
12403    }
12404
12405    #[test]
12406    fn test_cast_list_view_to_list_out_of_order_with_nulls() {
12407        let list_view = ListViewArray::new(
12408            Arc::new(Field::new("item", DataType::Int32, true)),
12409            ScalarBuffer::from(vec![0, 6, 3]),
12410            ScalarBuffer::from(vec![3, 3, 3]),
12411            Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])),
12412            Some(NullBuffer::from(vec![false, true, false])),
12413        );
12414        let target_type = DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
12415        assert!(can_cast_types(list_view.data_type(), &target_type));
12416        let cast_result = cast(&list_view, &target_type).unwrap();
12417        let got_list = cast_result.as_any().downcast_ref::<ListArray>().unwrap();
12418        let expected_list = ListArray::new(
12419            Arc::new(Field::new("item", DataType::Int32, true)),
12420            OffsetBuffer::from_lengths([3, 3, 3]),
12421            Arc::new(Int32Array::from(vec![1, 2, 3, 7, 8, 9, 4, 5, 6])),
12422            Some(NullBuffer::from(vec![false, true, false])),
12423        );
12424        assert_eq!(got_list, &expected_list);
12425    }
12426
12427    #[test]
12428    fn test_cast_list_view_to_large_list_view() {
12429        let list_view = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12430        let target_type =
12431            DataType::LargeListView(Arc::new(Field::new("item", DataType::Int32, true)));
12432        assert!(can_cast_types(list_view.data_type(), &target_type));
12433        let cast_result = cast(&list_view, &target_type).unwrap();
12434        let got = cast_result
12435            .as_any()
12436            .downcast_ref::<LargeListViewArray>()
12437            .unwrap();
12438
12439        let expected =
12440            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12441        assert_eq!(got, &expected);
12442    }
12443
12444    #[test]
12445    fn test_cast_large_list_view_to_list_view() {
12446        let list_view =
12447            LargeListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12448        let target_type = DataType::ListView(Arc::new(Field::new("item", DataType::Int32, true)));
12449        assert!(can_cast_types(list_view.data_type(), &target_type));
12450        let cast_result = cast(&list_view, &target_type).unwrap();
12451        let got = cast_result
12452            .as_any()
12453            .downcast_ref::<ListViewArray>()
12454            .unwrap();
12455
12456        let expected = ListViewArray::from_iter_primitive::<Int32Type, _, _>(int32_list_values());
12457        assert_eq!(got, &expected);
12458    }
12459
12460    #[test]
12461    fn test_cast_time32_second_to_int64() {
12462        let array = Time32SecondArray::from(vec![1000, 2000, 3000]);
12463        let array = Arc::new(array) as Arc<dyn Array>;
12464        let to_type = DataType::Int64;
12465        let cast_options = CastOptions::default();
12466
12467        assert!(can_cast_types(array.data_type(), &to_type));
12468
12469        let result = cast_with_options(&array, &to_type, &cast_options);
12470        assert!(
12471            result.is_ok(),
12472            "Failed to cast Time32(Second) to Int64: {:?}",
12473            result.err()
12474        );
12475
12476        let cast_array = result.unwrap();
12477        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
12478
12479        assert_eq!(cast_array.value(0), 1000);
12480        assert_eq!(cast_array.value(1), 2000);
12481        assert_eq!(cast_array.value(2), 3000);
12482    }
12483
12484    #[test]
12485    fn test_cast_time32_millisecond_to_int64() {
12486        let array = Time32MillisecondArray::from(vec![1000, 2000, 3000]);
12487        let array = Arc::new(array) as Arc<dyn Array>;
12488        let to_type = DataType::Int64;
12489        let cast_options = CastOptions::default();
12490
12491        assert!(can_cast_types(array.data_type(), &to_type));
12492
12493        let result = cast_with_options(&array, &to_type, &cast_options);
12494        assert!(
12495            result.is_ok(),
12496            "Failed to cast Time32(Millisecond) to Int64: {:?}",
12497            result.err()
12498        );
12499
12500        let cast_array = result.unwrap();
12501        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
12502
12503        assert_eq!(cast_array.value(0), 1000);
12504        assert_eq!(cast_array.value(1), 2000);
12505        assert_eq!(cast_array.value(2), 3000);
12506    }
12507
12508    #[test]
12509    fn test_cast_string_to_time32_second_to_int64() {
12510        // Mimic: select arrow_cast('03:12:44'::time, 'Time32(Second)')::bigint;
12511        // raised in https://github.com/apache/datafusion/issues/19036
12512        let array = StringArray::from(vec!["03:12:44"]);
12513        let array = Arc::new(array) as Arc<dyn Array>;
12514        let cast_options = CastOptions::default();
12515
12516        // 1. Cast String to Time32(Second)
12517        let time32_type = DataType::Time32(TimeUnit::Second);
12518        let time32_array = cast_with_options(&array, &time32_type, &cast_options).unwrap();
12519
12520        // 2. Cast Time32(Second) to Int64
12521        let int64_type = DataType::Int64;
12522        assert!(can_cast_types(time32_array.data_type(), &int64_type));
12523
12524        let result = cast_with_options(&time32_array, &int64_type, &cast_options);
12525
12526        assert!(
12527            result.is_ok(),
12528            "Failed to cast Time32(Second) to Int64: {:?}",
12529            result.err()
12530        );
12531
12532        let cast_array = result.unwrap();
12533        let cast_array = cast_array.as_any().downcast_ref::<Int64Array>().unwrap();
12534
12535        // 03:12:44 = 3*3600 + 12*60 + 44 = 10800 + 720 + 44 = 11564
12536        assert_eq!(cast_array.value(0), 11564);
12537    }
12538    #[test]
12539    fn test_string_dicts_to_binary_view() {
12540        let expected = BinaryViewArray::from_iter(vec![
12541            VIEW_TEST_DATA[1],
12542            VIEW_TEST_DATA[0],
12543            None,
12544            VIEW_TEST_DATA[3],
12545            None,
12546            VIEW_TEST_DATA[1],
12547            VIEW_TEST_DATA[4],
12548        ]);
12549
12550        let values_arrays: [ArrayRef; _] = [
12551            Arc::new(StringArray::from_iter(VIEW_TEST_DATA)),
12552            Arc::new(StringViewArray::from_iter(VIEW_TEST_DATA)),
12553            Arc::new(LargeStringArray::from_iter(VIEW_TEST_DATA)),
12554        ];
12555        for values in values_arrays {
12556            let keys =
12557                Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
12558            let string_dict_array =
12559                DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
12560
12561            let casted = cast(&string_dict_array, &DataType::BinaryView).unwrap();
12562            assert_eq!(casted.as_ref(), &expected);
12563        }
12564    }
12565
12566    #[test]
12567    fn test_binary_dicts_to_string_view() {
12568        let expected = StringViewArray::from_iter(vec![
12569            VIEW_TEST_DATA[1],
12570            VIEW_TEST_DATA[0],
12571            None,
12572            VIEW_TEST_DATA[3],
12573            None,
12574            VIEW_TEST_DATA[1],
12575            VIEW_TEST_DATA[4],
12576        ]);
12577
12578        let values_arrays: [ArrayRef; _] = [
12579            Arc::new(BinaryArray::from_iter(VIEW_TEST_DATA)),
12580            Arc::new(BinaryViewArray::from_iter(VIEW_TEST_DATA)),
12581            Arc::new(LargeBinaryArray::from_iter(VIEW_TEST_DATA)),
12582        ];
12583        for values in values_arrays {
12584            let keys =
12585                Int8Array::from_iter([Some(1), Some(0), None, Some(3), None, Some(1), Some(4)]);
12586            let string_dict_array =
12587                DictionaryArray::<Int8Type>::try_new(keys, Arc::new(values)).unwrap();
12588
12589            let casted = cast(&string_dict_array, &DataType::Utf8View).unwrap();
12590            assert_eq!(casted.as_ref(), &expected);
12591        }
12592    }
12593
12594    #[test]
12595    fn test_cast_between_sliced_run_end_encoded() {
12596        let run_ends = Int16Array::from(vec![2, 5, 8]);
12597        let values = StringArray::from(vec!["a", "b", "c"]);
12598
12599        let ree_array = RunArray::<Int16Type>::try_new(&run_ends, &values).unwrap();
12600        let ree_array = ree_array.slice(1, 2);
12601        let array_ref = Arc::new(ree_array) as ArrayRef;
12602
12603        let target_type = DataType::RunEndEncoded(
12604            Arc::new(Field::new("run_ends", DataType::Int64, false)),
12605            Arc::new(Field::new("values", DataType::Utf8, true)),
12606        );
12607        let cast_options = CastOptions {
12608            safe: false,
12609            format_options: FormatOptions::default(),
12610        };
12611
12612        let result = cast_with_options(&array_ref, &target_type, &cast_options).unwrap();
12613        let run_array = result.as_run::<Int64Type>();
12614        let run_array = run_array.downcast::<StringArray>().unwrap();
12615
12616        let expected = vec!["a", "b"];
12617        let actual = run_array.into_iter().flatten().collect::<Vec<_>>();
12618
12619        assert_eq!(expected, actual);
12620    }
12621}